vc4: Compute the proper end address of the relocated command lists.
[mesa.git] / src / gallium / drivers / vc4 / vc4_simulator.c
1 /*
2 * Copyright © 2014 Broadcom
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 #ifdef USE_VC4_SIMULATOR
25
26 #include <stdio.h>
27
28 #include "util/u_memory.h"
29
30 #include "vc4_screen.h"
31 #include "vc4_context.h"
32 #include "vc4_simulator_validate.h"
33 #include "simpenrose/simpenrose.h"
34
35 static struct drm_gem_cma_object *
36 vc4_wrap_bo_with_cma(struct drm_device *dev, struct vc4_bo *bo)
37 {
38 struct vc4_context *vc4 = dev->vc4;
39 struct vc4_screen *screen = vc4->screen;
40 struct drm_gem_cma_object *obj = CALLOC_STRUCT(drm_gem_cma_object);
41 uint32_t size = align(bo->size, 4096);
42
43 obj->bo = bo;
44 obj->base.size = size;
45 obj->vaddr = screen->simulator_mem_base + dev->simulator_mem_next;
46 obj->paddr = simpenrose_hw_addr(obj->vaddr);
47
48 dev->simulator_mem_next += size;
49 dev->simulator_mem_next = align(dev->simulator_mem_next, 4096);
50 assert(dev->simulator_mem_next <= screen->simulator_mem_size);
51
52 return obj;
53 }
54
55 static struct drm_gem_cma_object *
56 drm_gem_cma_create(struct drm_device *dev, size_t size)
57 {
58 struct vc4_context *vc4 = dev->vc4;
59 struct vc4_screen *screen = vc4->screen;
60
61 struct vc4_bo *bo = vc4_bo_alloc(screen, size, "simulator validate");
62 return vc4_wrap_bo_with_cma(dev, bo);
63 }
64
65 static int
66 vc4_simulator_pin_bos(struct drm_device *dev, struct exec_info *exec)
67 {
68 struct drm_vc4_submit_cl *args = exec->args;
69 struct vc4_context *vc4 = dev->vc4;
70 struct vc4_bo **bos = vc4->bo_pointers.base;
71
72 exec->bo_count = args->bo_handle_count;
73 exec->bo = calloc(exec->bo_count, sizeof(void *));
74 for (int i = 0; i < exec->bo_count; i++) {
75 struct vc4_bo *bo = bos[i];
76 struct drm_gem_cma_object *obj = vc4_wrap_bo_with_cma(dev, bo);
77
78 memcpy(obj->vaddr, bo->map, bo->size);
79
80 exec->bo[i] = obj;
81 }
82
83 return 0;
84 }
85
86 static int
87 vc4_simulator_unpin_bos(struct exec_info *exec)
88 {
89 for (int i = 0; i < exec->bo_count; i++) {
90 struct drm_gem_cma_object *obj = exec->bo[i];
91 struct vc4_bo *bo = obj->bo;
92
93 memcpy(bo->map, obj->vaddr, bo->size);
94
95 free(obj);
96 }
97
98 free(exec->bo);
99
100 return 0;
101 }
102
103 static int
104 vc4_cl_validate(struct drm_device *dev, struct exec_info *exec)
105 {
106 struct drm_vc4_submit_cl *args = exec->args;
107 void *temp = NULL;
108 void *bin, *render;
109 int ret = 0;
110 uint32_t bin_offset = 0;
111 uint32_t render_offset = bin_offset + args->bin_cl_size;
112 uint32_t shader_rec_offset = roundup(render_offset +
113 args->render_cl_size, 16);
114 uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
115 uint32_t exec_size = uniforms_offset + args->uniforms_size;
116 uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
117 args->shader_rec_count);
118
119 if (shader_rec_offset < render_offset ||
120 uniforms_offset < shader_rec_offset ||
121 exec_size < uniforms_offset ||
122 args->shader_rec_count >= (UINT_MAX /
123 sizeof(struct vc4_shader_state)) ||
124 temp_size < exec_size) {
125 DRM_ERROR("overflow in exec arguments\n");
126 goto fail;
127 }
128
129 /* Allocate space where we'll store the copied in user command lists
130 * and shader records.
131 *
132 * We don't just copy directly into the BOs because we need to
133 * read the contents back for validation, and I think the
134 * bo->vaddr is uncached access.
135 */
136 temp = kmalloc(temp_size, GFP_KERNEL);
137 if (!temp) {
138 DRM_ERROR("Failed to allocate storage for copying "
139 "in bin/render CLs.\n");
140 ret = -ENOMEM;
141 goto fail;
142 }
143 bin = temp + bin_offset;
144 render = temp + render_offset;
145 exec->shader_rec_u = temp + shader_rec_offset;
146 exec->uniforms_u = temp + uniforms_offset;
147 exec->shader_state = temp + exec_size;
148 exec->shader_state_size = args->shader_rec_count;
149
150 ret = copy_from_user(bin, args->bin_cl, args->bin_cl_size);
151 if (ret) {
152 DRM_ERROR("Failed to copy in bin cl\n");
153 goto fail;
154 }
155
156 ret = copy_from_user(render, args->render_cl, args->render_cl_size);
157 if (ret) {
158 DRM_ERROR("Failed to copy in render cl\n");
159 goto fail;
160 }
161
162 ret = copy_from_user(exec->shader_rec_u, args->shader_rec,
163 args->shader_rec_size);
164 if (ret) {
165 DRM_ERROR("Failed to copy in shader recs\n");
166 goto fail;
167 }
168
169 ret = copy_from_user(exec->uniforms_u, args->uniforms,
170 args->uniforms_size);
171 if (ret) {
172 DRM_ERROR("Failed to copy in uniforms cl\n");
173 goto fail;
174 }
175
176 exec->exec_bo = drm_gem_cma_create(dev, exec_size);
177 #if 0
178 if (IS_ERR(exec->exec_bo)) {
179 DRM_ERROR("Couldn't allocate BO for exec\n");
180 ret = PTR_ERR(exec->exec_bo);
181 exec->exec_bo = NULL;
182 goto fail;
183 }
184 #endif
185
186 exec->ct0ca = exec->exec_bo->paddr + bin_offset;
187 exec->ct1ca = exec->exec_bo->paddr + render_offset;
188
189 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
190 exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
191 exec->shader_rec_size = args->shader_rec_size;
192
193 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
194 exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
195 exec->uniforms_size = args->uniforms_size;
196
197 ret = vc4_validate_cl(dev,
198 exec->exec_bo->vaddr + bin_offset,
199 bin,
200 args->bin_cl_size,
201 true,
202 exec);
203 if (ret)
204 goto fail;
205
206 ret = vc4_validate_cl(dev,
207 exec->exec_bo->vaddr + render_offset,
208 render,
209 args->render_cl_size,
210 false,
211 exec);
212 if (ret)
213 goto fail;
214
215 ret = vc4_validate_shader_recs(dev, exec);
216
217 fail:
218 kfree(temp);
219 return ret;
220 }
221
222 int
223 vc4_simulator_flush(struct vc4_context *vc4, struct drm_vc4_submit_cl *args,
224 struct vc4_surface *csurf)
225 {
226 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
227 uint32_t winsys_stride = ctex->bo->simulator_winsys_stride;
228 uint32_t sim_stride = ctex->slices[0].stride;
229 uint32_t row_len = MIN2(sim_stride, winsys_stride);
230 struct exec_info exec;
231 struct drm_device local_dev = {
232 .vc4 = vc4,
233 .simulator_mem_next = 0,
234 };
235 struct drm_device *dev = &local_dev;
236 int ret;
237
238 memset(&exec, 0, sizeof(exec));
239
240 if (ctex->bo->simulator_winsys_map) {
241 #if 0
242 fprintf(stderr, "%dx%d %d %d %d\n",
243 ctex->base.b.width0, ctex->base.b.height0,
244 winsys_stride,
245 sim_stride,
246 ctex->bo->size);
247 #endif
248
249 for (int y = 0; y < ctex->base.b.height0; y++) {
250 memcpy(ctex->bo->map + y * sim_stride,
251 ctex->bo->simulator_winsys_map + y * winsys_stride,
252 row_len);
253 }
254 }
255
256 exec.args = args;
257
258 ret = vc4_simulator_pin_bos(dev, &exec);
259 if (ret)
260 return ret;
261
262 ret = vc4_cl_validate(dev, &exec);
263 if (ret)
264 return ret;
265
266 simpenrose_do_binning(exec.ct0ca, exec.ct0ea);
267 simpenrose_do_rendering(exec.ct1ca, exec.ct1ea);
268
269 ret = vc4_simulator_unpin_bos(&exec);
270 if (ret)
271 return ret;
272
273 free(exec.exec_bo);
274
275 if (ctex->bo->simulator_winsys_map) {
276 for (int y = 0; y < ctex->base.b.height0; y++) {
277 memcpy(ctex->bo->simulator_winsys_map + y * winsys_stride,
278 ctex->bo->map + y * sim_stride,
279 row_len);
280 }
281 }
282
283 return 0;
284 }
285
286 void
287 vc4_simulator_init(struct vc4_screen *screen)
288 {
289 simpenrose_init_hardware();
290 screen->simulator_mem_base = simpenrose_get_mem_start();
291 screen->simulator_mem_size = simpenrose_get_mem_size();
292 }
293
294 #endif /* USE_VC4_SIMULATOR */