1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2019 Raspberry Pi Ltd 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "v3dv_private.h" 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci/* We don't expect that the packets we use in this file change across hw 27bf215546Sopenharmony_ci * versions, so we just explicitly set the V3D_VERSION and include v3dx_pack 28bf215546Sopenharmony_ci * here 29bf215546Sopenharmony_ci */ 30bf215546Sopenharmony_ci#define V3D_VERSION 33 31bf215546Sopenharmony_ci#include "broadcom/common/v3d_macros.h" 32bf215546Sopenharmony_ci#include "broadcom/cle/v3dx_pack.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_civoid 35bf215546Sopenharmony_civ3dv_cl_init(struct v3dv_job *job, struct v3dv_cl *cl) 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci cl->base = NULL; 38bf215546Sopenharmony_ci cl->next = cl->base; 39bf215546Sopenharmony_ci cl->bo = NULL; 40bf215546Sopenharmony_ci cl->size = 0; 41bf215546Sopenharmony_ci cl->job = job; 42bf215546Sopenharmony_ci list_inithead(&cl->bo_list); 43bf215546Sopenharmony_ci} 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_civoid 46bf215546Sopenharmony_civ3dv_cl_destroy(struct v3dv_cl *cl) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci list_for_each_entry_safe(struct v3dv_bo, bo, &cl->bo_list, list_link) { 49bf215546Sopenharmony_ci assert(cl->job); 50bf215546Sopenharmony_ci list_del(&bo->list_link); 51bf215546Sopenharmony_ci v3dv_bo_free(cl->job->device, bo); 52bf215546Sopenharmony_ci } 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci /* Leave the CL in a reset state to catch use after destroy instances */ 55bf215546Sopenharmony_ci v3dv_cl_init(NULL, cl); 56bf215546Sopenharmony_ci} 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_cistatic bool 59bf215546Sopenharmony_cicl_alloc_bo(struct v3dv_cl *cl, uint32_t space, bool use_branch) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci struct v3dv_bo *bo = v3dv_bo_alloc(cl->job->device, space, "CL", true); 62bf215546Sopenharmony_ci if (!bo) { 63bf215546Sopenharmony_ci fprintf(stderr, "failed to allocate memory for command list\n"); 64bf215546Sopenharmony_ci v3dv_flag_oom(NULL, cl->job); 65bf215546Sopenharmony_ci return false; 66bf215546Sopenharmony_ci } 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci list_addtail(&bo->list_link, &cl->bo_list); 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci bool ok = v3dv_bo_map(cl->job->device, bo, bo->size); 71bf215546Sopenharmony_ci if (!ok) { 72bf215546Sopenharmony_ci fprintf(stderr, "failed to map command list buffer\n"); 73bf215546Sopenharmony_ci v3dv_flag_oom(NULL, cl->job); 74bf215546Sopenharmony_ci return false; 75bf215546Sopenharmony_ci } 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci /* Chain to the new BO from the old one if requested */ 78bf215546Sopenharmony_ci if (use_branch && cl->bo) { 79bf215546Sopenharmony_ci cl_emit(cl, BRANCH, branch) { 80bf215546Sopenharmony_ci branch.address = v3dv_cl_address(bo, 0); 81bf215546Sopenharmony_ci } 82bf215546Sopenharmony_ci } else { 83bf215546Sopenharmony_ci v3dv_job_add_bo_unchecked(cl->job, bo); 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci cl->bo = bo; 87bf215546Sopenharmony_ci cl->base = cl->bo->map; 88bf215546Sopenharmony_ci cl->size = cl->bo->size; 89bf215546Sopenharmony_ci cl->next = cl->base; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci return true; 92bf215546Sopenharmony_ci} 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ciuint32_t 95bf215546Sopenharmony_civ3dv_cl_ensure_space(struct v3dv_cl *cl, uint32_t space, uint32_t alignment) 96bf215546Sopenharmony_ci{ 97bf215546Sopenharmony_ci uint32_t offset = align(v3dv_cl_offset(cl), alignment); 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci if (offset + space <= cl->size) { 100bf215546Sopenharmony_ci cl->next = cl->base + offset; 101bf215546Sopenharmony_ci return offset; 102bf215546Sopenharmony_ci } 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci cl_alloc_bo(cl, space, false); 105bf215546Sopenharmony_ci return 0; 106bf215546Sopenharmony_ci} 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_civoid 109bf215546Sopenharmony_civ3dv_cl_ensure_space_with_branch(struct v3dv_cl *cl, uint32_t space) 110bf215546Sopenharmony_ci{ 111bf215546Sopenharmony_ci /* We do not want to emit branches from secondary command lists, instead, 112bf215546Sopenharmony_ci * we will branch to them when we execute them in a primary using 113bf215546Sopenharmony_ci * 'branch to sub list' commands, expecting each linked secondary to 114bf215546Sopenharmony_ci * end with a 'return from sub list' command. 115bf215546Sopenharmony_ci */ 116bf215546Sopenharmony_ci bool needs_return_from_sub_list = false; 117bf215546Sopenharmony_ci if (cl->job->type == V3DV_JOB_TYPE_GPU_CL_SECONDARY) { 118bf215546Sopenharmony_ci if (cl->size > 0) { 119bf215546Sopenharmony_ci needs_return_from_sub_list = true; 120bf215546Sopenharmony_ci space += cl_packet_length(RETURN_FROM_SUB_LIST); 121bf215546Sopenharmony_ci } 122bf215546Sopenharmony_ci } else { 123bf215546Sopenharmony_ci space += cl_packet_length(BRANCH); 124bf215546Sopenharmony_ci } 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci if (v3dv_cl_offset(cl) + space <= cl->size) 127bf215546Sopenharmony_ci return; 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci if (needs_return_from_sub_list) 130bf215546Sopenharmony_ci cl_emit(cl, RETURN_FROM_SUB_LIST, ret); 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci cl_alloc_bo(cl, space, !needs_return_from_sub_list); 133bf215546Sopenharmony_ci} 134