1 /*
2 * Copyright © 2019 Raspberry Pi Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "v3dv_private.h"
25
26 /* We don't expect that the packets we use in this file change across hw
27 * versions, so we just explicitly set the V3D_VERSION and include v3dx_pack
28 * here
29 */
30 #define V3D_VERSION 42
31 #include "broadcom/common/v3d_macros.h"
32 #include "broadcom/cle/v3dx_pack.h"
33
34 void
v3dv_cl_init(struct v3dv_job * job,struct v3dv_cl * cl)35 v3dv_cl_init(struct v3dv_job *job, struct v3dv_cl *cl)
36 {
37 cl->base = NULL;
38 cl->next = cl->base;
39 cl->bo = NULL;
40 cl->size = 0;
41 cl->job = job;
42 list_inithead(&cl->bo_list);
43 }
44
45 void
v3dv_cl_destroy(struct v3dv_cl * cl)46 v3dv_cl_destroy(struct v3dv_cl *cl)
47 {
48 list_for_each_entry_safe(struct v3dv_bo, bo, &cl->bo_list, list_link) {
49 assert(cl->job);
50 list_del(&bo->list_link);
51 v3dv_bo_free(cl->job->device, bo);
52 }
53
54 /* Leave the CL in a reset state to catch use after destroy instances */
55 v3dv_cl_init(NULL, cl);
56 }
57
58 enum v3dv_cl_chain_type {
59 V3D_CL_BO_CHAIN_NONE = 0,
60 V3D_CL_BO_CHAIN_WITH_BRANCH,
61 V3D_CL_BO_CHAIN_WITH_RETURN_FROM_SUB_LIST,
62 };
63
64 static bool
cl_alloc_bo(struct v3dv_cl * cl,uint32_t space,enum v3dv_cl_chain_type chain_type)65 cl_alloc_bo(struct v3dv_cl *cl, uint32_t space, enum
66 v3dv_cl_chain_type chain_type)
67 {
68 /* The last bytes of a CLE buffer are unusable because of readahead
69 * prefetch, so we need to take it into account when allocating a new BO
70 * for the CL. We also reserve space for the BRANCH/RETURN_FROM_SUB_LIST
71 * packet so we can always emit these last packets to the BO when
72 * needed. We will need to increase cl->size by the packet length before
73 * calling cl_submit to use this reserved space.
74 */
75 uint32_t unusable_space = 0;
76 struct v3d_device_info *devinfo = &cl->job->device->devinfo;
77 uint32_t cle_readahead = devinfo->cle_readahead;
78 uint32_t cle_buffer_min_size = devinfo->cle_buffer_min_size;
79 switch (chain_type) {
80 case V3D_CL_BO_CHAIN_WITH_BRANCH:
81 unusable_space = cle_readahead + cl_packet_length(BRANCH);
82 break;
83 case V3D_CL_BO_CHAIN_WITH_RETURN_FROM_SUB_LIST:
84 unusable_space = cle_readahead + cl_packet_length(RETURN_FROM_SUB_LIST);
85 break;
86 case V3D_CL_BO_CHAIN_NONE:
87 break;
88 }
89
90 /* If we are growing, double the BO allocation size to reduce the number
91 * of allocations with large command buffers. This has a very significant
92 * impact on the number of draw calls per second reported by vkoverhead.
93 */
94 space = align(space + unusable_space, cle_buffer_min_size);
95 if (cl->bo)
96 space = MAX2(cl->bo->size * 2, space);
97
98 struct v3dv_bo *bo = v3dv_bo_alloc(cl->job->device, space, "CL", true);
99 if (!bo) {
100 fprintf(stderr, "failed to allocate memory for command list\n");
101 v3dv_flag_oom(NULL, cl->job);
102 return false;
103 }
104
105 list_addtail(&bo->list_link, &cl->bo_list);
106
107 bool ok = v3dv_bo_map(cl->job->device, bo, bo->size);
108 if (!ok) {
109 fprintf(stderr, "failed to map command list buffer\n");
110 v3dv_flag_oom(NULL, cl->job);
111 return false;
112 }
113
114 /* Chain to the new BO from the old one if requested */
115 if (cl->bo) {
116 switch (chain_type) {
117 case V3D_CL_BO_CHAIN_WITH_BRANCH:
118 cl->bo->cl_branch_offset = v3dv_cl_offset(cl);
119 cl->size += cl_packet_length(BRANCH);
120 assert(cl->size + cle_readahead <= cl->bo->size);
121 cl_emit(cl, BRANCH, branch) {
122 branch.address = v3dv_cl_address(bo, 0);
123 }
124 break;
125 case V3D_CL_BO_CHAIN_WITH_RETURN_FROM_SUB_LIST:
126 /* We do not want to emit branches from secondary command lists, instead,
127 * we will branch to them when we execute them in a primary using
128 * 'branch to sub list' commands, expecting each linked secondary to
129 * end with a 'return from sub list' command.
130 */
131 cl->size += cl_packet_length(RETURN_FROM_SUB_LIST);
132 assert(cl->size + cle_readahead <= cl->bo->size);
133 cl_emit(cl, RETURN_FROM_SUB_LIST, ret);
134 FALLTHROUGH;
135 case V3D_CL_BO_CHAIN_NONE:
136 v3dv_job_add_bo_unchecked(cl->job, bo);
137 break;
138 }
139 } else {
140 v3dv_job_add_bo_unchecked(cl->job, bo);
141 }
142
143 cl->bo = bo;
144 cl->base = cl->bo->map;
145 /* Take only into account the usable size of the BO to guarantee that
146 * we never write in the last bytes of the CL buffer because of the
147 * readahead of the CLE
148 */
149 cl->size = cl->bo->size - unusable_space;
150 cl->next = cl->base;
151
152 return true;
153 }
154
155 uint32_t
v3dv_cl_ensure_space(struct v3dv_cl * cl,uint32_t space,uint32_t alignment)156 v3dv_cl_ensure_space(struct v3dv_cl *cl, uint32_t space, uint32_t alignment)
157 {
158 uint32_t offset = align(v3dv_cl_offset(cl), alignment);
159
160 if (offset + space <= cl->size) {
161 cl->next = cl->base + offset;
162 return offset;
163 }
164
165 cl_alloc_bo(cl, space, V3D_CL_BO_CHAIN_NONE);
166
167 return 0;
168 }
169
170 void
v3dv_cl_ensure_space_with_branch(struct v3dv_cl * cl,uint32_t space)171 v3dv_cl_ensure_space_with_branch(struct v3dv_cl *cl, uint32_t space)
172 {
173 if (v3dv_cl_offset(cl) + space <= cl->size)
174 return;
175
176 enum v3dv_cl_chain_type chain_type = V3D_CL_BO_CHAIN_WITH_BRANCH;
177 if (cl->job->type == V3DV_JOB_TYPE_GPU_CL_INCOMPLETE)
178 chain_type = V3D_CL_BO_CHAIN_WITH_RETURN_FROM_SUB_LIST;
179
180 cl_alloc_bo(cl, space, chain_type);
181 }
182