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 V3D simulation on top of a non-V3D GEM fd.
28 *
29 * This file's goal is to emulate the V3D ioctls' behavior in the kernel on
30 * top of the simpenrose software simulator. Generally, V3D 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 V3D is going to use, which of course
44 * doesn't work out in practice. This means that for now, only DRI3 (V3D
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 <stdio.h>
52 #include <sys/mman.h>
53 #include "c11/threads.h"
54 #include "util/hash_table.h"
55 #include "util/ralloc.h"
56 #include "util/set.h"
57 #include "util/u_dynarray.h"
58 #include "util/u_memory.h"
59 #include "util/u_mm.h"
60 #include "util/u_math.h"
61
62 #include <xf86drm.h>
63 #include "drm-uapi/amdgpu_drm.h"
64 #include "drm-uapi/i915_drm.h"
65 #include "drm-uapi/v3d_drm.h"
66
67 #include "v3d_simulator.h"
68 #include "v3d_simulator_wrapper.h"
69
70 /** Global (across GEM fds) state for the simulator */
71 static struct v3d_simulator_state {
72 mtx_t mutex;
73 mtx_t submit_lock;
74
75 struct v3d_hw *v3d;
76 int ver;
77
78 /* Base virtual address of the heap. */
79 void *mem;
80 /* Base hardware address of the heap. */
81 uint32_t mem_base;
82 /* Size of the heap. */
83 uint32_t mem_size;
84
85 struct mem_block *heap;
86 struct mem_block *overflow;
87
88 /** Mapping from GEM fd to struct v3d_simulator_file * */
89 struct hash_table *fd_map;
90
91 /** Last performance monitor ID. */
92 uint32_t last_perfid;
93
94 struct util_dynarray bin_oom;
95 int refcount;
96 } sim_state = {
97 .mutex = _MTX_INITIALIZER_NP,
98 };
99
100 enum gem_type {
101 GEM_I915,
102 GEM_AMDGPU,
103 GEM_DUMB
104 };
105
106 /** Per-GEM-fd state for the simulator. */
107 struct v3d_simulator_file {
108 int fd;
109
110 /** Mapping from GEM handle to struct v3d_simulator_bo * */
111 struct hash_table *bo_map;
112
113 /** Dynamic array with performance monitors */
114 struct v3d_simulator_perfmon **perfmons;
115 uint32_t perfmons_size;
116 uint32_t active_perfid;
117
118 struct mem_block *gmp;
119 void *gmp_vaddr;
120
121 /** For specific gpus, use their create ioctl. Otherwise use dumb bo. */
122 enum gem_type gem_type;
123 };
124
125 /** Wrapper for drm_v3d_bo tracking the simulator-specific state. */
126 struct v3d_simulator_bo {
127 struct v3d_simulator_file *file;
128
129 /** Area for this BO within sim_state->mem */
130 struct mem_block *block;
131 uint32_t size;
132 uint64_t mmap_offset;
133 void *sim_vaddr;
134 void *gem_vaddr;
135
136 int handle;
137 };
138
139 struct v3d_simulator_perfmon {
140 uint32_t ncounters;
141 uint8_t counters[DRM_V3D_MAX_PERF_COUNTERS];
142 uint64_t values[DRM_V3D_MAX_PERF_COUNTERS];
143 };
144
145 static void *
int_to_key(int key)146 int_to_key(int key)
147 {
148 return (void *)(uintptr_t)key;
149 }
150
151 #define PERFMONS_ALLOC_SIZE 100
152
153 static uint32_t
perfmons_next_id(struct v3d_simulator_file * sim_file)154 perfmons_next_id(struct v3d_simulator_file *sim_file) {
155 sim_state.last_perfid++;
156 if (sim_state.last_perfid > sim_file->perfmons_size) {
157 sim_file->perfmons_size += PERFMONS_ALLOC_SIZE;
158 sim_file->perfmons = reralloc(sim_file,
159 sim_file->perfmons,
160 struct v3d_simulator_perfmon *,
161 sim_file->perfmons_size);
162 }
163
164 return sim_state.last_perfid;
165 }
166
167 static struct v3d_simulator_file *
v3d_get_simulator_file_for_fd(int fd)168 v3d_get_simulator_file_for_fd(int fd)
169 {
170 struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,
171 int_to_key(fd + 1));
172 return entry ? entry->data : NULL;
173 }
174
175 /* A marker placed just after each BO, then checked after rendering to make
176 * sure it's still there.
177 */
178 #define BO_SENTINEL 0xfedcba98
179
180 /* 128kb */
181 #define GMP_ALIGN2 17
182
183 /**
184 * Sets the range of GPU virtual address space to have the given GMP
185 * permissions (bit 0 = read, bit 1 = write, write-only forbidden).
186 */
187 static void
set_gmp_flags(struct v3d_simulator_file * file,uint32_t offset,uint32_t size,uint32_t flag)188 set_gmp_flags(struct v3d_simulator_file *file,
189 uint32_t offset, uint32_t size, uint32_t flag)
190 {
191 assert((offset & ((1 << GMP_ALIGN2) - 1)) == 0);
192 int gmp_offset = offset >> GMP_ALIGN2;
193 int gmp_count = align(size, 1 << GMP_ALIGN2) >> GMP_ALIGN2;
194 uint32_t *gmp = file->gmp_vaddr;
195
196 assert(flag <= 0x3);
197
198 for (int i = gmp_offset; i < gmp_offset + gmp_count; i++) {
199 int32_t bitshift = (i % 16) * 2;
200 gmp[i / 16] &= ~(0x3 << bitshift);
201 gmp[i / 16] |= flag << bitshift;
202 }
203 }
204
205 /**
206 * Allocates space in simulator memory and returns a tracking struct for it
207 * that also contains the drm_gem_cma_object struct.
208 */
209 static struct v3d_simulator_bo *
v3d_create_simulator_bo(int fd,unsigned size)210 v3d_create_simulator_bo(int fd, unsigned size)
211 {
212 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
213 struct v3d_simulator_bo *sim_bo = rzalloc(file,
214 struct v3d_simulator_bo);
215 size = align(size, 4096);
216
217 sim_bo->file = file;
218
219 mtx_lock(&sim_state.mutex);
220 sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, GMP_ALIGN2, 0);
221 mtx_unlock(&sim_state.mutex);
222 assert(sim_bo->block);
223
224 set_gmp_flags(file, sim_bo->block->ofs, size, 0x3);
225
226 sim_bo->size = size;
227
228 /* Allocate space for the buffer in simulator memory. */
229 sim_bo->sim_vaddr = sim_state.mem + sim_bo->block->ofs - sim_state.mem_base;
230 memset(sim_bo->sim_vaddr, 0xd0, size);
231
232 *(uint32_t *)(sim_bo->sim_vaddr + sim_bo->size) = BO_SENTINEL;
233
234 return sim_bo;
235 }
236
237 static struct v3d_simulator_bo *
v3d_create_simulator_bo_for_gem(int fd,int handle,unsigned size)238 v3d_create_simulator_bo_for_gem(int fd, int handle, unsigned size)
239 {
240 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
241 struct v3d_simulator_bo *sim_bo =
242 v3d_create_simulator_bo(fd, size);
243
244 sim_bo->handle = handle;
245
246 /* Map the GEM buffer for copy in/out to the simulator. i915 blocks
247 * dumb mmap on render nodes, so use their ioctl directly if we're on
248 * one.
249 */
250 int ret;
251 switch (file->gem_type) {
252 case GEM_I915:
253 {
254 struct drm_i915_gem_mmap_gtt map = {
255 .handle = handle,
256 };
257
258 /* We could potentially use non-gtt (cached) for LLC systems,
259 * but the copy-in/out won't be the limiting factor on
260 * simulation anyway.
261 */
262 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map);
263 sim_bo->mmap_offset = map.offset;
264 break;
265 }
266 case GEM_AMDGPU:
267 {
268 union drm_amdgpu_gem_mmap map = { 0 };
269 map.in.handle = handle;
270
271 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &map);
272 sim_bo->mmap_offset = map.out.addr_ptr;
273 break;
274 }
275 default:
276 {
277 struct drm_mode_map_dumb map = {
278 .handle = handle,
279 };
280 ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
281 sim_bo->mmap_offset = map.offset;
282 }
283 }
284 if (ret) {
285 fprintf(stderr, "Failed to get MMAP offset: %d\n", ret);
286 abort();
287 }
288
289 sim_bo->gem_vaddr = mmap(NULL, sim_bo->size,
290 PROT_READ | PROT_WRITE, MAP_SHARED,
291 fd, sim_bo->mmap_offset);
292 if (sim_bo->gem_vaddr == MAP_FAILED) {
293 fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
294 handle, (long long)sim_bo->mmap_offset, sim_bo->size);
295 abort();
296 }
297
298 /* A handle of 0 is used for v3d_gem.c internal allocations that
299 * don't need to go in the lookup table.
300 */
301 if (handle != 0) {
302 mtx_lock(&sim_state.mutex);
303 _mesa_hash_table_insert(file->bo_map, int_to_key(handle),
304 sim_bo);
305 mtx_unlock(&sim_state.mutex);
306 }
307
308 return sim_bo;
309 }
310
311 static int bin_fd;
312
313 uint32_t
v3d_simulator_get_spill(uint32_t spill_size)314 v3d_simulator_get_spill(uint32_t spill_size)
315 {
316 struct v3d_simulator_bo *sim_bo =
317 v3d_create_simulator_bo(bin_fd, spill_size);
318
319 util_dynarray_append(&sim_state.bin_oom, struct v3d_simulator_bo *,
320 sim_bo);
321
322 return sim_bo->block->ofs;
323 }
324
325 static void
v3d_free_simulator_bo(struct v3d_simulator_bo * sim_bo)326 v3d_free_simulator_bo(struct v3d_simulator_bo *sim_bo)
327 {
328 struct v3d_simulator_file *sim_file = sim_bo->file;
329
330 set_gmp_flags(sim_file, sim_bo->block->ofs, sim_bo->size, 0x0);
331
332 if (sim_bo->gem_vaddr)
333 munmap(sim_bo->gem_vaddr, sim_bo->size);
334
335 mtx_lock(&sim_state.mutex);
336 u_mmFreeMem(sim_bo->block);
337 if (sim_bo->handle) {
338 _mesa_hash_table_remove_key(sim_file->bo_map,
339 int_to_key(sim_bo->handle));
340 }
341 mtx_unlock(&sim_state.mutex);
342 ralloc_free(sim_bo);
343 }
344
345 static struct v3d_simulator_bo *
v3d_get_simulator_bo(struct v3d_simulator_file * file,int gem_handle)346 v3d_get_simulator_bo(struct v3d_simulator_file *file, int gem_handle)
347 {
348 if (gem_handle == 0)
349 return NULL;
350
351 mtx_lock(&sim_state.mutex);
352 struct hash_entry *entry =
353 _mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));
354 mtx_unlock(&sim_state.mutex);
355
356 return entry ? entry->data : NULL;
357 }
358
359 static void
v3d_simulator_copy_in_handle(struct v3d_simulator_file * file,int handle)360 v3d_simulator_copy_in_handle(struct v3d_simulator_file *file, int handle)
361 {
362 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
363
364 if (!sim_bo)
365 return;
366
367 memcpy(sim_bo->sim_vaddr, sim_bo->gem_vaddr, sim_bo->size);
368 }
369
370 static void
v3d_simulator_copy_out_handle(struct v3d_simulator_file * file,int handle)371 v3d_simulator_copy_out_handle(struct v3d_simulator_file *file, int handle)
372 {
373 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
374
375 if (!sim_bo)
376 return;
377
378 memcpy(sim_bo->gem_vaddr, sim_bo->sim_vaddr, sim_bo->size);
379
380 if (*(uint32_t *)(sim_bo->sim_vaddr +
381 sim_bo->size) != BO_SENTINEL) {
382 fprintf(stderr, "Buffer overflow in handle %d\n",
383 handle);
384 }
385 }
386
387 static int
v3d_simulator_pin_bos(struct v3d_simulator_file * file,struct drm_v3d_submit_cl * submit)388 v3d_simulator_pin_bos(struct v3d_simulator_file *file,
389 struct drm_v3d_submit_cl *submit)
390 {
391 uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
392
393 for (int i = 0; i < submit->bo_handle_count; i++)
394 v3d_simulator_copy_in_handle(file, bo_handles[i]);
395
396 return 0;
397 }
398
399 static int
v3d_simulator_unpin_bos(struct v3d_simulator_file * file,struct drm_v3d_submit_cl * submit)400 v3d_simulator_unpin_bos(struct v3d_simulator_file *file,
401 struct drm_v3d_submit_cl *submit)
402 {
403 uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
404
405 for (int i = 0; i < submit->bo_handle_count; i++)
406 v3d_simulator_copy_out_handle(file, bo_handles[i]);
407
408 return 0;
409 }
410
411 static struct v3d_simulator_perfmon *
v3d_get_simulator_perfmon(int fd,uint32_t perfid)412 v3d_get_simulator_perfmon(int fd, uint32_t perfid)
413 {
414 if (!perfid || perfid > sim_state.last_perfid)
415 return NULL;
416
417 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
418
419 mtx_lock(&sim_state.mutex);
420 assert(perfid <= file->perfmons_size);
421 struct v3d_simulator_perfmon *perfmon = file->perfmons[perfid - 1];
422 mtx_unlock(&sim_state.mutex);
423
424 return perfmon;
425 }
426
427 static void
v3d_simulator_perfmon_switch(int fd,uint32_t perfid)428 v3d_simulator_perfmon_switch(int fd, uint32_t perfid)
429 {
430 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
431 struct v3d_simulator_perfmon *perfmon;
432
433 if (perfid == file->active_perfid)
434 return;
435
436 perfmon = v3d_get_simulator_perfmon(fd, file->active_perfid);
437 if (perfmon)
438 v3d41_simulator_perfmon_stop(sim_state.v3d,
439 perfmon->ncounters,
440 perfmon->values);
441
442 perfmon = v3d_get_simulator_perfmon(fd, perfid);
443 if (perfmon)
444 v3d41_simulator_perfmon_start(sim_state.v3d,
445 perfmon->ncounters,
446 perfmon->counters);
447
448 file->active_perfid = perfid;
449 }
450
451 static int
v3d_simulator_signal_syncobjs(int fd,struct drm_v3d_multi_sync * ms)452 v3d_simulator_signal_syncobjs(int fd, struct drm_v3d_multi_sync *ms)
453 {
454 struct drm_v3d_sem *out_syncs = (void *)(uintptr_t)ms->out_syncs;
455 int n_syncobjs = ms->out_sync_count;
456 uint32_t syncobjs[n_syncobjs];
457
458 for (int i = 0; i < n_syncobjs; i++)
459 syncobjs[i] = out_syncs[i].handle;
460 return drmSyncobjSignal(fd, (uint32_t *) &syncobjs, n_syncobjs);
461 }
462
463 static int
v3d_simulator_process_post_deps(int fd,struct drm_v3d_extension * ext)464 v3d_simulator_process_post_deps(int fd, struct drm_v3d_extension *ext)
465 {
466 int ret = 0;
467 while (ext && ext->id != DRM_V3D_EXT_ID_MULTI_SYNC)
468 ext = (void *)(uintptr_t) ext->next;
469
470 if (ext) {
471 struct drm_v3d_multi_sync *ms = (struct drm_v3d_multi_sync *) ext;
472 ret = v3d_simulator_signal_syncobjs(fd, ms);
473 }
474 return ret;
475 }
476
477 static int
v3d_simulator_submit_cl_ioctl(int fd,struct drm_v3d_submit_cl * submit)478 v3d_simulator_submit_cl_ioctl(int fd, struct drm_v3d_submit_cl *submit)
479 {
480 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
481 int ret;
482
483 ret = v3d_simulator_pin_bos(file, submit);
484 if (ret)
485 return ret;
486
487 mtx_lock(&sim_state.submit_lock);
488 bin_fd = fd;
489
490 v3d_simulator_perfmon_switch(fd, submit->perfmon_id);
491
492 if (sim_state.ver >= 41)
493 v3d41_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
494 else
495 v3d33_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
496
497 util_dynarray_foreach(&sim_state.bin_oom, struct v3d_simulator_bo *,
498 sim_bo) {
499 v3d_free_simulator_bo(*sim_bo);
500 }
501 util_dynarray_clear(&sim_state.bin_oom);
502
503 mtx_unlock(&sim_state.submit_lock);
504
505 ret = v3d_simulator_unpin_bos(file, submit);
506 if (ret)
507 return ret;
508
509 if (submit->flags & DRM_V3D_SUBMIT_EXTENSION) {
510 struct drm_v3d_extension *ext = (void *)(uintptr_t)submit->extensions;
511 ret = v3d_simulator_process_post_deps(fd, ext);
512 }
513
514 return ret;
515 }
516
517 /**
518 * Do fixups after a BO has been opened from a handle.
519 *
520 * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE
521 * time, but we're still using drmPrimeFDToHandle() so we have this helper to
522 * be called afterward instead.
523 */
v3d_simulator_open_from_handle(int fd,int handle,uint32_t size)524 void v3d_simulator_open_from_handle(int fd, int handle, uint32_t size)
525 {
526 v3d_create_simulator_bo_for_gem(fd, handle, size);
527 }
528
529 /**
530 * Simulated ioctl(fd, DRM_V3D_CREATE_BO) implementation.
531 *
532 * Making a V3D BO is just a matter of making a corresponding BO on the host.
533 */
534 static int
v3d_simulator_create_bo_ioctl(int fd,struct drm_v3d_create_bo * args)535 v3d_simulator_create_bo_ioctl(int fd, struct drm_v3d_create_bo *args)
536 {
537 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
538
539 /* i915 bans dumb create on render nodes, so we have to use their
540 * native ioctl in case we're on a render node.
541 */
542 int ret;
543 switch (file->gem_type) {
544 case GEM_I915:
545 {
546 struct drm_i915_gem_create create = {
547 .size = args->size,
548 };
549
550 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
551
552 args->handle = create.handle;
553 break;
554 }
555 case GEM_AMDGPU:
556 {
557 union drm_amdgpu_gem_create create = { 0 };
558 create.in.bo_size = args->size;
559
560 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_CREATE, &create);
561
562 args->handle = create.out.handle;
563 break;
564 }
565 default:
566 {
567 struct drm_mode_create_dumb create = {
568 .width = 128,
569 .bpp = 8,
570 .height = (args->size + 127) / 128,
571 };
572
573 ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
574 assert(ret != 0 || create.size >= args->size);
575
576 args->handle = create.handle;
577 }
578 }
579 if (ret == 0) {
580 struct v3d_simulator_bo *sim_bo =
581 v3d_create_simulator_bo_for_gem(fd, args->handle,
582 args->size);
583
584 args->offset = sim_bo->block->ofs;
585 }
586
587 return ret;
588 }
589
590 /**
591 * Simulated ioctl(fd, DRM_V3D_MMAP_BO) implementation.
592 *
593 * We've already grabbed the mmap offset when we created the sim bo, so just
594 * return it.
595 */
596 static int
v3d_simulator_mmap_bo_ioctl(int fd,struct drm_v3d_mmap_bo * args)597 v3d_simulator_mmap_bo_ioctl(int fd, struct drm_v3d_mmap_bo *args)
598 {
599 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
600 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
601 args->handle);
602
603 args->offset = sim_bo->mmap_offset;
604
605 return 0;
606 }
607
608 static int
v3d_simulator_get_bo_offset_ioctl(int fd,struct drm_v3d_get_bo_offset * args)609 v3d_simulator_get_bo_offset_ioctl(int fd, struct drm_v3d_get_bo_offset *args)
610 {
611 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
612 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
613 args->handle);
614
615 args->offset = sim_bo->block->ofs;
616
617 return 0;
618 }
619
620 static int
v3d_simulator_gem_close_ioctl(int fd,struct drm_gem_close * args)621 v3d_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)
622 {
623 /* Free the simulator's internal tracking. */
624 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
625 struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
626 args->handle);
627
628 v3d_free_simulator_bo(sim_bo);
629
630 /* Pass the call on down. */
631 return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);
632 }
633
634 static int
v3d_simulator_get_param_ioctl(int fd,struct drm_v3d_get_param * args)635 v3d_simulator_get_param_ioctl(int fd, struct drm_v3d_get_param *args)
636 {
637 if (sim_state.ver >= 41)
638 return v3d41_simulator_get_param_ioctl(sim_state.v3d, args);
639 else
640 return v3d33_simulator_get_param_ioctl(sim_state.v3d, args);
641 }
642
643 static int
v3d_simulator_submit_tfu_ioctl(int fd,struct drm_v3d_submit_tfu * args)644 v3d_simulator_submit_tfu_ioctl(int fd, struct drm_v3d_submit_tfu *args)
645 {
646 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
647 int ret;
648
649 v3d_simulator_copy_in_handle(file, args->bo_handles[0]);
650 v3d_simulator_copy_in_handle(file, args->bo_handles[1]);
651 v3d_simulator_copy_in_handle(file, args->bo_handles[2]);
652 v3d_simulator_copy_in_handle(file, args->bo_handles[3]);
653
654 if (sim_state.ver >= 41)
655 ret = v3d41_simulator_submit_tfu_ioctl(sim_state.v3d, args);
656 else
657 ret = v3d33_simulator_submit_tfu_ioctl(sim_state.v3d, args);
658
659 v3d_simulator_copy_out_handle(file, args->bo_handles[0]);
660
661 if (ret)
662 return ret;
663
664 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
665 struct drm_v3d_extension *ext = (void *)(uintptr_t)args->extensions;
666 ret = v3d_simulator_process_post_deps(fd, ext);
667 }
668
669 return ret;
670 }
671
672 static int
v3d_simulator_submit_csd_ioctl(int fd,struct drm_v3d_submit_csd * args)673 v3d_simulator_submit_csd_ioctl(int fd, struct drm_v3d_submit_csd *args)
674 {
675 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
676 uint32_t *bo_handles = (uint32_t *)(uintptr_t)args->bo_handles;
677 int ret;
678
679 for (int i = 0; i < args->bo_handle_count; i++)
680 v3d_simulator_copy_in_handle(file, bo_handles[i]);
681
682 v3d_simulator_perfmon_switch(fd, args->perfmon_id);
683
684 if (sim_state.ver >= 41)
685 ret = v3d41_simulator_submit_csd_ioctl(sim_state.v3d, args,
686 file->gmp->ofs);
687 else
688 ret = -1;
689
690 for (int i = 0; i < args->bo_handle_count; i++)
691 v3d_simulator_copy_out_handle(file, bo_handles[i]);
692
693 if (ret < 0)
694 return ret;
695
696 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
697 struct drm_v3d_extension *ext = (void *)(uintptr_t)args->extensions;
698 ret = v3d_simulator_process_post_deps(fd, ext);
699 }
700
701 return ret;
702 }
703
704 static int
v3d_simulator_perfmon_create_ioctl(int fd,struct drm_v3d_perfmon_create * args)705 v3d_simulator_perfmon_create_ioctl(int fd, struct drm_v3d_perfmon_create *args)
706 {
707 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
708
709 if (args->ncounters == 0 ||
710 args->ncounters > DRM_V3D_MAX_PERF_COUNTERS)
711 return -EINVAL;
712
713 struct v3d_simulator_perfmon *perfmon = rzalloc(file,
714 struct v3d_simulator_perfmon);
715
716 perfmon->ncounters = args->ncounters;
717 for (int i = 0; i < args->ncounters; i++) {
718 if (args->counters[i] >= V3D_PERFCNT_NUM) {
719 ralloc_free(perfmon);
720 return -EINVAL;
721 } else {
722 perfmon->counters[i] = args->counters[i];
723 }
724 }
725
726 mtx_lock(&sim_state.mutex);
727 args->id = perfmons_next_id(file);
728 file->perfmons[args->id - 1] = perfmon;
729 mtx_unlock(&sim_state.mutex);
730
731 return 0;
732 }
733
734 static int
v3d_simulator_perfmon_destroy_ioctl(int fd,struct drm_v3d_perfmon_destroy * args)735 v3d_simulator_perfmon_destroy_ioctl(int fd, struct drm_v3d_perfmon_destroy *args)
736 {
737 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
738 struct v3d_simulator_perfmon *perfmon =
739 v3d_get_simulator_perfmon(fd, args->id);
740
741 if (!perfmon)
742 return -EINVAL;
743
744 mtx_lock(&sim_state.mutex);
745 file->perfmons[args->id - 1] = NULL;
746 mtx_unlock(&sim_state.mutex);
747
748 ralloc_free(perfmon);
749
750 return 0;
751 }
752
753 static int
v3d_simulator_perfmon_get_values_ioctl(int fd,struct drm_v3d_perfmon_get_values * args)754 v3d_simulator_perfmon_get_values_ioctl(int fd, struct drm_v3d_perfmon_get_values *args)
755 {
756 struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
757
758 mtx_lock(&sim_state.submit_lock);
759
760 /* Stop the perfmon if it is still active */
761 if (args->id == file->active_perfid)
762 v3d_simulator_perfmon_switch(fd, 0);
763
764 mtx_unlock(&sim_state.submit_lock);
765
766 struct v3d_simulator_perfmon *perfmon =
767 v3d_get_simulator_perfmon(fd, args->id);
768
769 if (!perfmon)
770 return -EINVAL;
771
772 memcpy((void *)args->values_ptr, perfmon->values, perfmon->ncounters * sizeof(uint64_t));
773
774 return 0;
775 }
776
777 int
v3d_simulator_ioctl(int fd,unsigned long request,void * args)778 v3d_simulator_ioctl(int fd, unsigned long request, void *args)
779 {
780 switch (request) {
781 case DRM_IOCTL_V3D_SUBMIT_CL:
782 return v3d_simulator_submit_cl_ioctl(fd, args);
783 case DRM_IOCTL_V3D_CREATE_BO:
784 return v3d_simulator_create_bo_ioctl(fd, args);
785 case DRM_IOCTL_V3D_MMAP_BO:
786 return v3d_simulator_mmap_bo_ioctl(fd, args);
787 case DRM_IOCTL_V3D_GET_BO_OFFSET:
788 return v3d_simulator_get_bo_offset_ioctl(fd, args);
789
790 case DRM_IOCTL_V3D_WAIT_BO:
791 /* We do all of the v3d rendering synchronously, so we just
792 * return immediately on the wait ioctls. This ignores any
793 * native rendering to the host BO, so it does mean we race on
794 * front buffer rendering.
795 */
796 return 0;
797
798 case DRM_IOCTL_V3D_GET_PARAM:
799 return v3d_simulator_get_param_ioctl(fd, args);
800
801 case DRM_IOCTL_GEM_CLOSE:
802 return v3d_simulator_gem_close_ioctl(fd, args);
803
804 case DRM_IOCTL_V3D_SUBMIT_TFU:
805 return v3d_simulator_submit_tfu_ioctl(fd, args);
806
807 case DRM_IOCTL_V3D_SUBMIT_CSD:
808 return v3d_simulator_submit_csd_ioctl(fd, args);
809
810 case DRM_IOCTL_V3D_PERFMON_CREATE:
811 return v3d_simulator_perfmon_create_ioctl(fd, args);
812
813 case DRM_IOCTL_V3D_PERFMON_DESTROY:
814 return v3d_simulator_perfmon_destroy_ioctl(fd, args);
815
816 case DRM_IOCTL_V3D_PERFMON_GET_VALUES:
817 return v3d_simulator_perfmon_get_values_ioctl(fd, args);
818
819 case DRM_IOCTL_GEM_OPEN:
820 case DRM_IOCTL_GEM_FLINK:
821 return drmIoctl(fd, request, args);
822 default:
823 fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);
824 abort();
825 }
826 }
827
828 uint32_t
v3d_simulator_get_mem_size(void)829 v3d_simulator_get_mem_size(void)
830 {
831 return sim_state.mem_size;
832 }
833
834 static void
v3d_simulator_init_global()835 v3d_simulator_init_global()
836 {
837 mtx_lock(&sim_state.mutex);
838 if (sim_state.refcount++) {
839 mtx_unlock(&sim_state.mutex);
840 return;
841 }
842
843 sim_state.v3d = v3d_hw_auto_new(NULL);
844 v3d_hw_alloc_mem(sim_state.v3d, 1024 * 1024 * 1024);
845 sim_state.mem_base =
846 v3d_hw_get_mem(sim_state.v3d, &sim_state.mem_size,
847 &sim_state.mem);
848
849 /* Allocate from anywhere from 4096 up. We don't allocate at 0,
850 * because for OQs and some other addresses in the HW, 0 means
851 * disabled.
852 */
853 sim_state.heap = u_mmInit(4096, sim_state.mem_size - 4096);
854
855 /* Make a block of 0xd0 at address 0 to make sure we don't screw up
856 * and land there.
857 */
858 struct mem_block *b = u_mmAllocMem(sim_state.heap, 4096, GMP_ALIGN2, 0);
859 memset(sim_state.mem + b->ofs - sim_state.mem_base, 0xd0, 4096);
860
861 sim_state.ver = v3d_hw_get_version(sim_state.v3d);
862
863 mtx_unlock(&sim_state.mutex);
864
865 sim_state.fd_map =
866 _mesa_hash_table_create(NULL,
867 _mesa_hash_pointer,
868 _mesa_key_pointer_equal);
869
870 util_dynarray_init(&sim_state.bin_oom, NULL);
871
872 if (sim_state.ver >= 41)
873 v3d41_simulator_init_regs(sim_state.v3d);
874 else
875 v3d33_simulator_init_regs(sim_state.v3d);
876 }
877
878 struct v3d_simulator_file *
v3d_simulator_init(int fd)879 v3d_simulator_init(int fd)
880 {
881 v3d_simulator_init_global();
882
883 struct v3d_simulator_file *sim_file = rzalloc(NULL, struct v3d_simulator_file);
884
885 drmVersionPtr version = drmGetVersion(fd);
886 if (version && strncmp(version->name, "i915", version->name_len) == 0)
887 sim_file->gem_type = GEM_I915;
888 else if (version && strncmp(version->name, "amdgpu", version->name_len) == 0)
889 sim_file->gem_type = GEM_AMDGPU;
890 else
891 sim_file->gem_type = GEM_DUMB;
892 drmFreeVersion(version);
893
894 sim_file->bo_map =
895 _mesa_hash_table_create(sim_file,
896 _mesa_hash_pointer,
897 _mesa_key_pointer_equal);
898
899 mtx_lock(&sim_state.mutex);
900 _mesa_hash_table_insert(sim_state.fd_map, int_to_key(fd + 1),
901 sim_file);
902 mtx_unlock(&sim_state.mutex);
903
904 sim_file->gmp = u_mmAllocMem(sim_state.heap, 8096, GMP_ALIGN2, 0);
905 sim_file->gmp_vaddr = (sim_state.mem + sim_file->gmp->ofs -
906 sim_state.mem_base);
907 memset(sim_file->gmp_vaddr, 0, 8096);
908
909 return sim_file;
910 }
911
912 void
v3d_simulator_destroy(struct v3d_simulator_file * sim_file)913 v3d_simulator_destroy(struct v3d_simulator_file *sim_file)
914 {
915 mtx_lock(&sim_state.mutex);
916 if (!--sim_state.refcount) {
917 _mesa_hash_table_destroy(sim_state.fd_map, NULL);
918 util_dynarray_fini(&sim_state.bin_oom);
919 u_mmDestroy(sim_state.heap);
920 /* No memsetting the struct, because it contains the mutex. */
921 sim_state.mem = NULL;
922 }
923 mtx_unlock(&sim_state.mutex);
924 ralloc_free(sim_file);
925 }
926
927 #endif /* USE_V3D_SIMULATOR */
928