v3d: Use the uniform pretty-printer in v3d_write_uniforms()'s debug code.
[mesa.git] / src / gallium / drivers / v3d / v3d_simulator.c
1 /*
2 * Copyright © 2014-2017 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 /**
25 * @file v3d_simulator.c
26 *
27 * Implements VC5 simulation on top of a non-VC5 GEM fd.
28 *
29 * This file's goal is to emulate the VC5 ioctls' behavior in the kernel on
30 * top of the simpenrose software simulator. Generally, VC5 driver BOs have a
31 * GEM-side copy of their contents and a simulator-side memory area that the
32 * GEM contents get copied into during simulation. Once simulation is done,
33 * the simulator's data is copied back out to the GEM BOs, so that rendering
34 * appears on the screen as if actual hardware rendering had been done.
35 *
36 * One of the limitations of this code is that we shouldn't really need a
37 * GEM-side BO for non-window-system BOs. However, do we need unique BO
38 * handles for each of our GEM bos so that this file can look up its state
39 * from the handle passed in at submit ioctl time (also, a couple of places
40 * outside of this file still call ioctls directly on the fd).
41 *
42 * Another limitation is that BO import doesn't work unless the underlying
43 * window system's BO size matches what VC5 is going to use, which of course
44 * doesn't work out in practice. This means that for now, only DRI3 (VC5
45 * makes the winsys BOs) is supported, not DRI2 (window system makes the winys
46 * BOs).
47 */
48
49 #ifdef USE_V3D_SIMULATOR
50
51 #include <sys/mman.h>
52 #include "util/hash_table.h"
53 #include "util/ralloc.h"
54 #include "util/set.h"
55 #include "util/u_memory.h"
56 #include "util/u_mm.h"
57 #include "v3d_simulator_wrapper.h"
58
59 #include "v3d_screen.h"
60 #include "v3d_context.h"
61
62 /** Global (across GEM fds) state for the simulator */
63 static struct v3d_simulator_state {
64 mtx_t mutex;
65
66 struct v3d_hw *v3d;
67 int ver;
68
69 /* Base virtual address of the heap. */
70 void *mem;
71 /* Base hardware address of the heap. */
72 uint32_t mem_base;
73 /* Size of the heap. */
74 size_t mem_size;
75
76 struct mem_block *heap;
77 struct mem_block *overflow;
78
79 /** Mapping from GEM fd to struct v3d_simulator_file * */
80 struct hash_table *fd_map;
81
82 int refcount;
83 } sim_state = {
84 .mutex = _MTX_INITIALIZER_NP,
85 };
86
87 /** Per-GEM-fd state for the simulator. */
88 struct v3d_simulator_file {
89 int fd;
90
91 /** Mapping from GEM handle to struct v3d_simulator_bo * */
92 struct hash_table *bo_map;
93
94 struct mem_block *gmp;
95 void *gmp_vaddr;
96 };
97
98 /** Wrapper for drm_v3d_bo tracking the simulator-specific state. */
99 struct v3d_simulator_bo {
100 struct v3d_simulator_file *file;
101
102 /** Area for this BO within sim_state->mem */
103 struct mem_block *block;
104 uint32_t size;
105 void *sim_vaddr;
106 void *gem_vaddr;
107
108 int handle;
109 };
110
111 static void *
112 int_to_key(int key)
113 {
114 return (void *)(uintptr_t)key;
115 }
116
117 static struct v3d_simulator_file *
118 v3d_get_simulator_file_for_fd(int fd)
119 {
120 struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,
121 int_to_key(fd + 1));
122 return entry ? entry->data : NULL;
123 }
124
125 /* A marker placed just after each BO, then checked after rendering to make
126 * sure it's still there.
127 */
128 #define BO_SENTINEL 0xfedcba98
129
130 /* 128kb */
131 #define GMP_ALIGN2 17
132
133 /**
134 * Sets the range of GPU virtual address space to have the given GMP
135 * permissions (bit 0 = read, bit 1 = write, write-only forbidden).
136 */
137 static void
138 set_gmp_flags(struct v3d_simulator_file *file,
139 uint32_t offset, uint32_t size, uint32_t flag)
140 {
141 assert((offset & ((1 << GMP_ALIGN2) - 1)) == 0);
142 int gmp_offset = offset >> GMP_ALIGN2;
143 int gmp_count = align(size, 1 << GMP_ALIGN2) >> GMP_ALIGN2;
144 uint32_t *gmp = file->gmp_vaddr;
145
146 assert(flag <= 0x3);
147
148 for (int i = gmp_offset; i < gmp_offset + gmp_count; i++) {
149 int32_t bitshift = (i % 16) * 2;
150 gmp[i / 16] &= ~(0x3 << bitshift);
151 gmp[i / 16] |= flag << bitshift;
152 }
153 }
154
155 /**
156 * Allocates space in simulator memory and returns a tracking struct for it
157 * that also contains the drm_gem_cma_object struct.
158 */
159 static struct v3d_simulator_bo *
160 v3d_create_simulator_bo(int fd, int handle, unsigned size)
161 {
162 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
163 struct v3d_simulator_bo *sim_bo = rzalloc(file,
164 struct v3d_simulator_bo);
165 size = align(size, 4096);
166
167 sim_bo->file = file;
168 sim_bo->handle = handle;
169
170 mtx_lock(&sim_state.mutex);
171 sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, GMP_ALIGN2, 0);
172 mtx_unlock(&sim_state.mutex);
173 assert(sim_bo->block);
174
175 set_gmp_flags(file, sim_bo->block->ofs, size, 0x3);
176
177 sim_bo->size = size;
178
179 /* Allocate space for the buffer in simulator memory. */
180 sim_bo->sim_vaddr = sim_state.mem + sim_bo->block->ofs - sim_state.mem_base;
181 memset(sim_bo->sim_vaddr, 0xd0, size);
182
183 *(uint32_t *)(sim_bo->sim_vaddr + sim_bo->size) = BO_SENTINEL;
184
185 /* Map the GEM buffer for copy in/out to the simulator. */
186 struct drm_mode_map_dumb map = {
187 .handle = handle,
188 };
189 int ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
190 if (ret) {
191 fprintf(stderr, "Failed to get MMAP offset: %d\n", ret);
192 abort();
193 }
194 sim_bo->gem_vaddr = mmap(NULL, sim_bo->size,
195 PROT_READ | PROT_WRITE, MAP_SHARED,
196 fd, map.offset);
197 if (sim_bo->gem_vaddr == MAP_FAILED) {
198 fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
199 handle, (long long)map.offset, sim_bo->size);
200 abort();
201 }
202
203 /* A handle of 0 is used for v3d_gem.c internal allocations that
204 * don't need to go in the lookup table.
205 */
206 if (handle != 0) {
207 mtx_lock(&sim_state.mutex);
208 _mesa_hash_table_insert(file->bo_map, int_to_key(handle),
209 sim_bo);
210 mtx_unlock(&sim_state.mutex);
211 }
212
213 return sim_bo;
214 }
215
216 static void
217 v3d_free_simulator_bo(struct v3d_simulator_bo *sim_bo)
218 {
219 struct v3d_simulator_file *sim_file = sim_bo->file;
220
221 set_gmp_flags(sim_file, sim_bo->block->ofs, sim_bo->size, 0x0);
222
223 if (sim_bo->gem_vaddr)
224 munmap(sim_bo->gem_vaddr, sim_bo->size);
225
226 mtx_lock(&sim_state.mutex);
227 u_mmFreeMem(sim_bo->block);
228 if (sim_bo->handle) {
229 _mesa_hash_table_remove_key(sim_file->bo_map,
230 int_to_key(sim_bo->handle));
231 }
232 mtx_unlock(&sim_state.mutex);
233 ralloc_free(sim_bo);
234 }
235
236 static struct v3d_simulator_bo *
237 v3d_get_simulator_bo(struct v3d_simulator_file *file, int gem_handle)
238 {
239 mtx_lock(&sim_state.mutex);
240 struct hash_entry *entry =
241 _mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));
242 mtx_unlock(&sim_state.mutex);
243
244 return entry ? entry->data : NULL;
245 }
246
247 static void
248 v3d_simulator_copy_in_handle(struct v3d_simulator_file *file, int handle)
249 {
250 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
251
252 if (!sim_bo)
253 return;
254
255 memcpy(sim_bo->sim_vaddr, sim_bo->gem_vaddr, sim_bo->size);
256 }
257
258 static void
259 v3d_simulator_copy_out_handle(struct v3d_simulator_file *file, int handle)
260 {
261 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
262
263 if (!sim_bo)
264 return;
265
266 memcpy(sim_bo->gem_vaddr, sim_bo->sim_vaddr, sim_bo->size);
267
268 if (*(uint32_t *)(sim_bo->sim_vaddr +
269 sim_bo->size) != BO_SENTINEL) {
270 fprintf(stderr, "Buffer overflow in handle %d\n",
271 handle);
272 }
273 }
274
275 static int
276 v3d_simulator_pin_bos(struct v3d_simulator_file *file,
277 struct drm_v3d_submit_cl *submit)
278 {
279 uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
280
281 for (int i = 0; i < submit->bo_handle_count; i++)
282 v3d_simulator_copy_in_handle(file, bo_handles[i]);
283
284 return 0;
285 }
286
287 static int
288 v3d_simulator_unpin_bos(struct v3d_simulator_file *file,
289 struct drm_v3d_submit_cl *submit)
290 {
291 uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
292
293 for (int i = 0; i < submit->bo_handle_count; i++)
294 v3d_simulator_copy_out_handle(file, bo_handles[i]);
295
296 return 0;
297 }
298
299 static int
300 v3d_simulator_submit_cl_ioctl(int fd, struct drm_v3d_submit_cl *submit)
301 {
302 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
303 int ret;
304
305 ret = v3d_simulator_pin_bos(file, submit);
306 if (ret)
307 return ret;
308
309 if (sim_state.ver >= 41)
310 v3d41_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
311 else
312 v3d33_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
313
314 ret = v3d_simulator_unpin_bos(file, submit);
315 if (ret)
316 return ret;
317
318 return 0;
319 }
320
321 /**
322 * Do fixups after a BO has been opened from a handle.
323 *
324 * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE
325 * time, but we're still using drmPrimeFDToHandle() so we have this helper to
326 * be called afterward instead.
327 */
328 void v3d_simulator_open_from_handle(int fd, int handle, uint32_t size)
329 {
330 v3d_create_simulator_bo(fd, handle, size);
331 }
332
333 /**
334 * Simulated ioctl(fd, DRM_VC5_CREATE_BO) implementation.
335 *
336 * Making a VC5 BO is just a matter of making a corresponding BO on the host.
337 */
338 static int
339 v3d_simulator_create_bo_ioctl(int fd, struct drm_v3d_create_bo *args)
340 {
341 int ret;
342 struct drm_mode_create_dumb create = {
343 .width = 128,
344 .bpp = 8,
345 .height = (args->size + 127) / 128,
346 };
347
348 ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
349 assert(create.size >= args->size);
350
351 args->handle = create.handle;
352
353 struct v3d_simulator_bo *sim_bo =
354 v3d_create_simulator_bo(fd, create.handle, args->size);
355
356 args->offset = sim_bo->block->ofs;
357
358 return ret;
359 }
360
361 /**
362 * Simulated ioctl(fd, DRM_VC5_MMAP_BO) implementation.
363 *
364 * We just pass this straight through to dumb mmap.
365 */
366 static int
367 v3d_simulator_mmap_bo_ioctl(int fd, struct drm_v3d_mmap_bo *args)
368 {
369 int ret;
370 struct drm_mode_map_dumb map = {
371 .handle = args->handle,
372 };
373
374 ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
375 args->offset = map.offset;
376
377 return ret;
378 }
379
380 static int
381 v3d_simulator_get_bo_offset_ioctl(int fd, struct drm_v3d_get_bo_offset *args)
382 {
383 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
384 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
385 args->handle);
386
387 args->offset = sim_bo->block->ofs;
388
389 return 0;
390 }
391
392 static int
393 v3d_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)
394 {
395 /* Free the simulator's internal tracking. */
396 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
397 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
398 args->handle);
399
400 v3d_free_simulator_bo(sim_bo);
401
402 /* Pass the call on down. */
403 return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);
404 }
405
406 static int
407 v3d_simulator_get_param_ioctl(int fd, struct drm_v3d_get_param *args)
408 {
409 if (sim_state.ver >= 41)
410 return v3d41_simulator_get_param_ioctl(sim_state.v3d, args);
411 else
412 return v3d33_simulator_get_param_ioctl(sim_state.v3d, args);
413 }
414
415 static int
416 v3d_simulator_submit_tfu_ioctl(int fd, struct drm_v3d_submit_tfu *args)
417 {
418 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
419 int ret;
420
421 v3d_simulator_copy_in_handle(file, args->bo_handles[0]);
422 v3d_simulator_copy_in_handle(file, args->bo_handles[1]);
423 v3d_simulator_copy_in_handle(file, args->bo_handles[2]);
424 v3d_simulator_copy_in_handle(file, args->bo_handles[3]);
425
426 if (sim_state.ver >= 41)
427 ret = v3d41_simulator_submit_tfu_ioctl(sim_state.v3d, args);
428 else
429 ret = v3d33_simulator_submit_tfu_ioctl(sim_state.v3d, args);
430
431 v3d_simulator_copy_out_handle(file, args->bo_handles[0]);
432
433 return ret;
434 }
435
436 int
437 v3d_simulator_ioctl(int fd, unsigned long request, void *args)
438 {
439 switch (request) {
440 case DRM_IOCTL_V3D_SUBMIT_CL:
441 return v3d_simulator_submit_cl_ioctl(fd, args);
442 case DRM_IOCTL_V3D_CREATE_BO:
443 return v3d_simulator_create_bo_ioctl(fd, args);
444 case DRM_IOCTL_V3D_MMAP_BO:
445 return v3d_simulator_mmap_bo_ioctl(fd, args);
446 case DRM_IOCTL_V3D_GET_BO_OFFSET:
447 return v3d_simulator_get_bo_offset_ioctl(fd, args);
448
449 case DRM_IOCTL_V3D_WAIT_BO:
450 /* We do all of the v3d rendering synchronously, so we just
451 * return immediately on the wait ioctls. This ignores any
452 * native rendering to the host BO, so it does mean we race on
453 * front buffer rendering.
454 */
455 return 0;
456
457 case DRM_IOCTL_V3D_GET_PARAM:
458 return v3d_simulator_get_param_ioctl(fd, args);
459
460 case DRM_IOCTL_GEM_CLOSE:
461 return v3d_simulator_gem_close_ioctl(fd, args);
462
463 case DRM_IOCTL_V3D_SUBMIT_TFU:
464 return v3d_simulator_submit_tfu_ioctl(fd, args);
465
466 case DRM_IOCTL_GEM_OPEN:
467 case DRM_IOCTL_GEM_FLINK:
468 return drmIoctl(fd, request, args);
469 default:
470 fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);
471 abort();
472 }
473 }
474
475 static void
476 v3d_simulator_init_global(const struct v3d_device_info *devinfo)
477 {
478 mtx_lock(&sim_state.mutex);
479 if (sim_state.refcount++) {
480 mtx_unlock(&sim_state.mutex);
481 return;
482 }
483
484 sim_state.v3d = v3d_hw_auto_new(NULL);
485 v3d_hw_alloc_mem(sim_state.v3d, 1024 * 1024 * 1024);
486 sim_state.mem_base =
487 v3d_hw_get_mem(sim_state.v3d, &sim_state.mem_size,
488 &sim_state.mem);
489
490 /* Allocate from anywhere from 4096 up. We don't allocate at 0,
491 * because for OQs and some other addresses in the HW, 0 means
492 * disabled.
493 */
494 sim_state.heap = u_mmInit(4096, sim_state.mem_size - 4096);
495
496 /* Make a block of 0xd0 at address 0 to make sure we don't screw up
497 * and land there.
498 */
499 struct mem_block *b = u_mmAllocMem(sim_state.heap, 4096, GMP_ALIGN2, 0);
500 memset(sim_state.mem + b->ofs - sim_state.mem_base, 0xd0, 4096);
501
502 sim_state.ver = v3d_hw_get_version(sim_state.v3d);
503
504 mtx_unlock(&sim_state.mutex);
505
506 sim_state.fd_map =
507 _mesa_hash_table_create(NULL,
508 _mesa_hash_pointer,
509 _mesa_key_pointer_equal);
510
511 if (sim_state.ver >= 41)
512 v3d41_simulator_init_regs(sim_state.v3d);
513 else
514 v3d33_simulator_init_regs(sim_state.v3d);
515 }
516
517 void
518 v3d_simulator_init(struct v3d_screen *screen)
519 {
520 v3d_simulator_init_global(&screen->devinfo);
521
522 screen->sim_file = rzalloc(screen, struct v3d_simulator_file);
523 struct v3d_simulator_file *sim_file = screen->sim_file;
524
525 screen->sim_file->bo_map =
526 _mesa_hash_table_create(screen->sim_file,
527 _mesa_hash_pointer,
528 _mesa_key_pointer_equal);
529
530 mtx_lock(&sim_state.mutex);
531 _mesa_hash_table_insert(sim_state.fd_map, int_to_key(screen->fd + 1),
532 screen->sim_file);
533 mtx_unlock(&sim_state.mutex);
534
535 sim_file->gmp = u_mmAllocMem(sim_state.heap, 8096, GMP_ALIGN2, 0);
536 sim_file->gmp_vaddr = (sim_state.mem + sim_file->gmp->ofs -
537 sim_state.mem_base);
538 }
539
540 void
541 v3d_simulator_destroy(struct v3d_screen *screen)
542 {
543 mtx_lock(&sim_state.mutex);
544 if (!--sim_state.refcount) {
545 _mesa_hash_table_destroy(sim_state.fd_map, NULL);
546 u_mmDestroy(sim_state.heap);
547 /* No memsetting the struct, because it contains the mutex. */
548 sim_state.mem = NULL;
549 }
550 mtx_unlock(&sim_state.mutex);
551 }
552
553 #endif /* USE_V3D_SIMULATOR */