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 <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/i915_drm.h"
64 #include "drm-uapi/v3d_drm.h"
65 
66 #include "v3d_simulator.h"
67 #include "v3d_simulator_wrapper.h"
68 
69 /** Global (across GEM fds) state for the simulator */
70 static struct v3d_simulator_state {
71         mtx_t mutex;
72         mtx_t submit_lock;
73 
74         struct v3d_hw *v3d;
75         int ver;
76 
77         /* Base virtual address of the heap. */
78         void *mem;
79         /* Base hardware address of the heap. */
80         uint32_t mem_base;
81         /* Size of the heap. */
82         size_t mem_size;
83 
84         struct mem_block *heap;
85         struct mem_block *overflow;
86 
87         /** Mapping from GEM fd to struct v3d_simulator_file * */
88         struct hash_table *fd_map;
89 
90         struct util_dynarray bin_oom;
91         int refcount;
92 } sim_state = {
93         .mutex = _MTX_INITIALIZER_NP,
94 };
95 
96 /** Per-GEM-fd state for the simulator. */
97 struct v3d_simulator_file {
98         int fd;
99 
100         /** Mapping from GEM handle to struct v3d_simulator_bo * */
101         struct hash_table *bo_map;
102 
103         struct mem_block *gmp;
104         void *gmp_vaddr;
105 
106         /** Actual GEM fd is i915, so we should use their create ioctl. */
107         bool is_i915;
108 };
109 
110 /** Wrapper for drm_v3d_bo tracking the simulator-specific state. */
111 struct v3d_simulator_bo {
112         struct v3d_simulator_file *file;
113 
114         /** Area for this BO within sim_state->mem */
115         struct mem_block *block;
116         uint32_t size;
117         uint64_t mmap_offset;
118         void *sim_vaddr;
119         void *gem_vaddr;
120 
121         int handle;
122 };
123 
124 static void *
int_to_key(int key)125 int_to_key(int key)
126 {
127         return (void *)(uintptr_t)key;
128 }
129 
130 static struct v3d_simulator_file *
v3d_get_simulator_file_for_fd(int fd)131 v3d_get_simulator_file_for_fd(int fd)
132 {
133         struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,
134                                                            int_to_key(fd + 1));
135         return entry ? entry->data : NULL;
136 }
137 
138 /* A marker placed just after each BO, then checked after rendering to make
139  * sure it's still there.
140  */
141 #define BO_SENTINEL		0xfedcba98
142 
143 /* 128kb */
144 #define GMP_ALIGN2		17
145 
146 /**
147  * Sets the range of GPU virtual address space to have the given GMP
148  * permissions (bit 0 = read, bit 1 = write, write-only forbidden).
149  */
150 static void
set_gmp_flags(struct v3d_simulator_file * file,uint32_t offset,uint32_t size,uint32_t flag)151 set_gmp_flags(struct v3d_simulator_file *file,
152               uint32_t offset, uint32_t size, uint32_t flag)
153 {
154         assert((offset & ((1 << GMP_ALIGN2) - 1)) == 0);
155         int gmp_offset = offset >> GMP_ALIGN2;
156         int gmp_count = align(size, 1 << GMP_ALIGN2) >> GMP_ALIGN2;
157         uint32_t *gmp = file->gmp_vaddr;
158 
159         assert(flag <= 0x3);
160 
161         for (int i = gmp_offset; i < gmp_offset + gmp_count; i++) {
162                 int32_t bitshift = (i % 16) * 2;
163                 gmp[i / 16] &= ~(0x3 << bitshift);
164                 gmp[i / 16] |= flag << bitshift;
165         }
166 }
167 
168 /**
169  * Allocates space in simulator memory and returns a tracking struct for it
170  * that also contains the drm_gem_cma_object struct.
171  */
172 static struct v3d_simulator_bo *
v3d_create_simulator_bo(int fd,unsigned size)173 v3d_create_simulator_bo(int fd, unsigned size)
174 {
175         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
176         struct v3d_simulator_bo *sim_bo = rzalloc(file,
177                                                   struct v3d_simulator_bo);
178         size = align(size, 4096);
179 
180         sim_bo->file = file;
181 
182         mtx_lock(&sim_state.mutex);
183         sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, GMP_ALIGN2, 0);
184         mtx_unlock(&sim_state.mutex);
185         assert(sim_bo->block);
186 
187         set_gmp_flags(file, sim_bo->block->ofs, size, 0x3);
188 
189         sim_bo->size = size;
190 
191         /* Allocate space for the buffer in simulator memory. */
192         sim_bo->sim_vaddr = sim_state.mem + sim_bo->block->ofs - sim_state.mem_base;
193         memset(sim_bo->sim_vaddr, 0xd0, size);
194 
195         *(uint32_t *)(sim_bo->sim_vaddr + sim_bo->size) = BO_SENTINEL;
196 
197         return sim_bo;
198 }
199 
200 static struct v3d_simulator_bo *
v3d_create_simulator_bo_for_gem(int fd,int handle,unsigned size)201 v3d_create_simulator_bo_for_gem(int fd, int handle, unsigned size)
202 {
203         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
204         struct v3d_simulator_bo *sim_bo =
205                 v3d_create_simulator_bo(fd, size);
206 
207         sim_bo->handle = handle;
208 
209         /* Map the GEM buffer for copy in/out to the simulator.  i915 blocks
210          * dumb mmap on render nodes, so use their ioctl directly if we're on
211          * one.
212          */
213         int ret;
214         if (file->is_i915) {
215                 struct drm_i915_gem_mmap_gtt map = {
216                         .handle = handle,
217                 };
218 
219                 /* We could potentially use non-gtt (cached) for LLC systems,
220                  * but the copy-in/out won't be the limiting factor on
221                  * simulation anyway.
222                  */
223                 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map);
224                 sim_bo->mmap_offset = map.offset;
225         } else {
226                 struct drm_mode_map_dumb map = {
227                         .handle = handle,
228                 };
229 
230                 ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
231                 sim_bo->mmap_offset = map.offset;
232         }
233         if (ret) {
234                 fprintf(stderr, "Failed to get MMAP offset: %d\n", ret);
235                 abort();
236         }
237 
238         sim_bo->gem_vaddr = mmap(NULL, sim_bo->size,
239                                  PROT_READ | PROT_WRITE, MAP_SHARED,
240                                  fd, sim_bo->mmap_offset);
241         if (sim_bo->gem_vaddr == MAP_FAILED) {
242                 fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
243                         handle, (long long)sim_bo->mmap_offset, sim_bo->size);
244                 abort();
245         }
246 
247         /* A handle of 0 is used for v3d_gem.c internal allocations that
248          * don't need to go in the lookup table.
249          */
250         if (handle != 0) {
251                 mtx_lock(&sim_state.mutex);
252                 _mesa_hash_table_insert(file->bo_map, int_to_key(handle),
253                                         sim_bo);
254                 mtx_unlock(&sim_state.mutex);
255         }
256 
257         return sim_bo;
258 }
259 
260 static int bin_fd;
261 
262 uint32_t
v3d_simulator_get_spill(uint32_t spill_size)263 v3d_simulator_get_spill(uint32_t spill_size)
264 {
265         struct v3d_simulator_bo *sim_bo =
266                 v3d_create_simulator_bo(bin_fd, spill_size);
267 
268         util_dynarray_append(&sim_state.bin_oom, struct v3d_simulator_bo *,
269                              sim_bo);
270 
271         return sim_bo->block->ofs;
272 }
273 
274 static void
v3d_free_simulator_bo(struct v3d_simulator_bo * sim_bo)275 v3d_free_simulator_bo(struct v3d_simulator_bo *sim_bo)
276 {
277         struct v3d_simulator_file *sim_file = sim_bo->file;
278 
279         set_gmp_flags(sim_file, sim_bo->block->ofs, sim_bo->size, 0x0);
280 
281         if (sim_bo->gem_vaddr)
282                 munmap(sim_bo->gem_vaddr, sim_bo->size);
283 
284         mtx_lock(&sim_state.mutex);
285         u_mmFreeMem(sim_bo->block);
286         if (sim_bo->handle) {
287                 _mesa_hash_table_remove_key(sim_file->bo_map,
288                                             int_to_key(sim_bo->handle));
289         }
290         mtx_unlock(&sim_state.mutex);
291         ralloc_free(sim_bo);
292 }
293 
294 static struct v3d_simulator_bo *
v3d_get_simulator_bo(struct v3d_simulator_file * file,int gem_handle)295 v3d_get_simulator_bo(struct v3d_simulator_file *file, int gem_handle)
296 {
297         if (gem_handle == 0)
298                 return NULL;
299 
300         mtx_lock(&sim_state.mutex);
301         struct hash_entry *entry =
302                 _mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));
303         mtx_unlock(&sim_state.mutex);
304 
305         return entry ? entry->data : NULL;
306 }
307 
308 static void
v3d_simulator_copy_in_handle(struct v3d_simulator_file * file,int handle)309 v3d_simulator_copy_in_handle(struct v3d_simulator_file *file, int handle)
310 {
311         struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
312 
313         if (!sim_bo)
314                 return;
315 
316         memcpy(sim_bo->sim_vaddr, sim_bo->gem_vaddr, sim_bo->size);
317 }
318 
319 static void
v3d_simulator_copy_out_handle(struct v3d_simulator_file * file,int handle)320 v3d_simulator_copy_out_handle(struct v3d_simulator_file *file, int handle)
321 {
322         struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
323 
324         if (!sim_bo)
325                 return;
326 
327         memcpy(sim_bo->gem_vaddr, sim_bo->sim_vaddr, sim_bo->size);
328 
329         if (*(uint32_t *)(sim_bo->sim_vaddr +
330                           sim_bo->size) != BO_SENTINEL) {
331                 fprintf(stderr, "Buffer overflow in handle %d\n",
332                         handle);
333         }
334 }
335 
336 static int
v3d_simulator_pin_bos(struct v3d_simulator_file * file,struct drm_v3d_submit_cl * submit)337 v3d_simulator_pin_bos(struct v3d_simulator_file *file,
338                       struct drm_v3d_submit_cl *submit)
339 {
340         uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
341 
342         for (int i = 0; i < submit->bo_handle_count; i++)
343                 v3d_simulator_copy_in_handle(file, bo_handles[i]);
344 
345         return 0;
346 }
347 
348 static int
v3d_simulator_unpin_bos(struct v3d_simulator_file * file,struct drm_v3d_submit_cl * submit)349 v3d_simulator_unpin_bos(struct v3d_simulator_file *file,
350                         struct drm_v3d_submit_cl *submit)
351 {
352         uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
353 
354         for (int i = 0; i < submit->bo_handle_count; i++)
355                 v3d_simulator_copy_out_handle(file, bo_handles[i]);
356 
357         return 0;
358 }
359 
360 static int
v3d_simulator_submit_cl_ioctl(int fd,struct drm_v3d_submit_cl * submit)361 v3d_simulator_submit_cl_ioctl(int fd, struct drm_v3d_submit_cl *submit)
362 {
363         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
364         int ret;
365 
366         ret = v3d_simulator_pin_bos(file, submit);
367         if (ret)
368                 return ret;
369 
370         mtx_lock(&sim_state.submit_lock);
371         bin_fd = fd;
372         if (sim_state.ver >= 41)
373                 v3d41_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
374         else
375                 v3d33_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
376 
377         util_dynarray_foreach(&sim_state.bin_oom, struct v3d_simulator_bo *,
378                               sim_bo) {
379                 v3d_free_simulator_bo(*sim_bo);
380         }
381         util_dynarray_clear(&sim_state.bin_oom);
382 
383         mtx_unlock(&sim_state.submit_lock);
384 
385         ret = v3d_simulator_unpin_bos(file, submit);
386         if (ret)
387                 return ret;
388 
389         return 0;
390 }
391 
392 /**
393  * Do fixups after a BO has been opened from a handle.
394  *
395  * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE
396  * time, but we're still using drmPrimeFDToHandle() so we have this helper to
397  * be called afterward instead.
398  */
v3d_simulator_open_from_handle(int fd,int handle,uint32_t size)399 void v3d_simulator_open_from_handle(int fd, int handle, uint32_t size)
400 {
401         v3d_create_simulator_bo_for_gem(fd, handle, size);
402 }
403 
404 /**
405  * Simulated ioctl(fd, DRM_VC5_CREATE_BO) implementation.
406  *
407  * Making a VC5 BO is just a matter of making a corresponding BO on the host.
408  */
409 static int
v3d_simulator_create_bo_ioctl(int fd,struct drm_v3d_create_bo * args)410 v3d_simulator_create_bo_ioctl(int fd, struct drm_v3d_create_bo *args)
411 {
412         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
413 
414         /* i915 bans dumb create on render nodes, so we have to use their
415          * native ioctl in case we're on a render node.
416          */
417         int ret;
418         if (file->is_i915) {
419                 struct drm_i915_gem_create create = {
420                         .size = args->size,
421                 };
422                 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
423 
424                 args->handle = create.handle;
425         } else {
426                 struct drm_mode_create_dumb create = {
427                         .width = 128,
428                         .bpp = 8,
429                         .height = (args->size + 127) / 128,
430                 };
431 
432                 ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
433                 assert(ret != 0 || create.size >= args->size);
434 
435                 args->handle = create.handle;
436         }
437 
438         if (ret == 0) {
439                 struct v3d_simulator_bo *sim_bo =
440                         v3d_create_simulator_bo_for_gem(fd, args->handle,
441                                                         args->size);
442 
443                 args->offset = sim_bo->block->ofs;
444         }
445 
446         return ret;
447 }
448 
449 /**
450  * Simulated ioctl(fd, DRM_VC5_MMAP_BO) implementation.
451  *
452  * We've already grabbed the mmap offset when we created the sim bo, so just
453  * return it.
454  */
455 static int
v3d_simulator_mmap_bo_ioctl(int fd,struct drm_v3d_mmap_bo * args)456 v3d_simulator_mmap_bo_ioctl(int fd, struct drm_v3d_mmap_bo *args)
457 {
458         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
459         struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
460                                                                args->handle);
461 
462         args->offset = sim_bo->mmap_offset;
463 
464         return 0;
465 }
466 
467 static int
v3d_simulator_get_bo_offset_ioctl(int fd,struct drm_v3d_get_bo_offset * args)468 v3d_simulator_get_bo_offset_ioctl(int fd, struct drm_v3d_get_bo_offset *args)
469 {
470         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
471         struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
472                                                                args->handle);
473 
474         args->offset = sim_bo->block->ofs;
475 
476         return 0;
477 }
478 
479 static int
v3d_simulator_gem_close_ioctl(int fd,struct drm_gem_close * args)480 v3d_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)
481 {
482         /* Free the simulator's internal tracking. */
483         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
484         struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
485                                                                args->handle);
486 
487         v3d_free_simulator_bo(sim_bo);
488 
489         /* Pass the call on down. */
490         return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);
491 }
492 
493 static int
v3d_simulator_get_param_ioctl(int fd,struct drm_v3d_get_param * args)494 v3d_simulator_get_param_ioctl(int fd, struct drm_v3d_get_param *args)
495 {
496         if (sim_state.ver >= 41)
497                 return v3d41_simulator_get_param_ioctl(sim_state.v3d, args);
498         else
499                 return v3d33_simulator_get_param_ioctl(sim_state.v3d, args);
500 }
501 
502 static int
v3d_simulator_submit_tfu_ioctl(int fd,struct drm_v3d_submit_tfu * args)503 v3d_simulator_submit_tfu_ioctl(int fd, struct drm_v3d_submit_tfu *args)
504 {
505         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
506         int ret;
507 
508         v3d_simulator_copy_in_handle(file, args->bo_handles[0]);
509         v3d_simulator_copy_in_handle(file, args->bo_handles[1]);
510         v3d_simulator_copy_in_handle(file, args->bo_handles[2]);
511         v3d_simulator_copy_in_handle(file, args->bo_handles[3]);
512 
513         if (sim_state.ver >= 41)
514                 ret = v3d41_simulator_submit_tfu_ioctl(sim_state.v3d, args);
515         else
516                 ret = v3d33_simulator_submit_tfu_ioctl(sim_state.v3d, args);
517 
518         v3d_simulator_copy_out_handle(file, args->bo_handles[0]);
519 
520         return ret;
521 }
522 
523 static int
v3d_simulator_submit_csd_ioctl(int fd,struct drm_v3d_submit_csd * args)524 v3d_simulator_submit_csd_ioctl(int fd, struct drm_v3d_submit_csd *args)
525 {
526         struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
527         uint32_t *bo_handles = (uint32_t *)(uintptr_t)args->bo_handles;
528         int ret;
529 
530         for (int i = 0; i < args->bo_handle_count; i++)
531                 v3d_simulator_copy_in_handle(file, bo_handles[i]);
532 
533         if (sim_state.ver >= 41)
534                 ret = v3d41_simulator_submit_csd_ioctl(sim_state.v3d, args,
535                                                        file->gmp->ofs);
536         else
537                 ret = -1;
538 
539         for (int i = 0; i < args->bo_handle_count; i++)
540                 v3d_simulator_copy_out_handle(file, bo_handles[i]);
541 
542         return ret;
543 }
544 
545 int
v3d_simulator_ioctl(int fd,unsigned long request,void * args)546 v3d_simulator_ioctl(int fd, unsigned long request, void *args)
547 {
548         switch (request) {
549         case DRM_IOCTL_V3D_SUBMIT_CL:
550                 return v3d_simulator_submit_cl_ioctl(fd, args);
551         case DRM_IOCTL_V3D_CREATE_BO:
552                 return v3d_simulator_create_bo_ioctl(fd, args);
553         case DRM_IOCTL_V3D_MMAP_BO:
554                 return v3d_simulator_mmap_bo_ioctl(fd, args);
555         case DRM_IOCTL_V3D_GET_BO_OFFSET:
556                 return v3d_simulator_get_bo_offset_ioctl(fd, args);
557 
558         case DRM_IOCTL_V3D_WAIT_BO:
559                 /* We do all of the v3d rendering synchronously, so we just
560                  * return immediately on the wait ioctls.  This ignores any
561                  * native rendering to the host BO, so it does mean we race on
562                  * front buffer rendering.
563                  */
564                 return 0;
565 
566         case DRM_IOCTL_V3D_GET_PARAM:
567                 return v3d_simulator_get_param_ioctl(fd, args);
568 
569         case DRM_IOCTL_GEM_CLOSE:
570                 return v3d_simulator_gem_close_ioctl(fd, args);
571 
572         case DRM_IOCTL_V3D_SUBMIT_TFU:
573                 return v3d_simulator_submit_tfu_ioctl(fd, args);
574 
575         case DRM_IOCTL_V3D_SUBMIT_CSD:
576                 return v3d_simulator_submit_csd_ioctl(fd, args);
577 
578         case DRM_IOCTL_GEM_OPEN:
579         case DRM_IOCTL_GEM_FLINK:
580                 return drmIoctl(fd, request, args);
581         default:
582                 fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);
583                 abort();
584         }
585 }
586 
587 uint32_t
v3d_simulator_get_mem_size(void)588 v3d_simulator_get_mem_size(void)
589 {
590    return sim_state.mem_size;
591 }
592 
593 static void
v3d_simulator_init_global()594 v3d_simulator_init_global()
595 {
596         mtx_lock(&sim_state.mutex);
597         if (sim_state.refcount++) {
598                 mtx_unlock(&sim_state.mutex);
599                 return;
600         }
601 
602         sim_state.v3d = v3d_hw_auto_new(NULL);
603         v3d_hw_alloc_mem(sim_state.v3d, 1024 * 1024 * 1024);
604         sim_state.mem_base =
605                 v3d_hw_get_mem(sim_state.v3d, &sim_state.mem_size,
606                                &sim_state.mem);
607 
608         /* Allocate from anywhere from 4096 up.  We don't allocate at 0,
609          * because for OQs and some other addresses in the HW, 0 means
610          * disabled.
611          */
612         sim_state.heap = u_mmInit(4096, sim_state.mem_size - 4096);
613 
614         /* Make a block of 0xd0 at address 0 to make sure we don't screw up
615          * and land there.
616          */
617         struct mem_block *b = u_mmAllocMem(sim_state.heap, 4096, GMP_ALIGN2, 0);
618         memset(sim_state.mem + b->ofs - sim_state.mem_base, 0xd0, 4096);
619 
620         sim_state.ver = v3d_hw_get_version(sim_state.v3d);
621 
622         mtx_unlock(&sim_state.mutex);
623 
624         sim_state.fd_map =
625                 _mesa_hash_table_create(NULL,
626                                         _mesa_hash_pointer,
627                                         _mesa_key_pointer_equal);
628 
629         util_dynarray_init(&sim_state.bin_oom, NULL);
630 
631         if (sim_state.ver >= 41)
632                 v3d41_simulator_init_regs(sim_state.v3d);
633         else
634                 v3d33_simulator_init_regs(sim_state.v3d);
635 }
636 
637 struct v3d_simulator_file *
v3d_simulator_init(int fd)638 v3d_simulator_init(int fd)
639 {
640         v3d_simulator_init_global();
641 
642         struct v3d_simulator_file *sim_file = rzalloc(NULL, struct v3d_simulator_file);
643 
644         drmVersionPtr version = drmGetVersion(fd);
645         if (version && strncmp(version->name, "i915", version->name_len) == 0)
646                 sim_file->is_i915 = true;
647         drmFreeVersion(version);
648 
649         sim_file->bo_map =
650                 _mesa_hash_table_create(sim_file,
651                                         _mesa_hash_pointer,
652                                         _mesa_key_pointer_equal);
653 
654         mtx_lock(&sim_state.mutex);
655         _mesa_hash_table_insert(sim_state.fd_map, int_to_key(fd + 1),
656                                 sim_file);
657         mtx_unlock(&sim_state.mutex);
658 
659         sim_file->gmp = u_mmAllocMem(sim_state.heap, 8096, GMP_ALIGN2, 0);
660         sim_file->gmp_vaddr = (sim_state.mem + sim_file->gmp->ofs -
661                                sim_state.mem_base);
662         memset(sim_file->gmp_vaddr, 0, 8096);
663 
664         return sim_file;
665 }
666 
667 void
v3d_simulator_destroy(struct v3d_simulator_file * sim_file)668 v3d_simulator_destroy(struct v3d_simulator_file *sim_file)
669 {
670         mtx_lock(&sim_state.mutex);
671         if (!--sim_state.refcount) {
672                 _mesa_hash_table_destroy(sim_state.fd_map, NULL);
673                 util_dynarray_fini(&sim_state.bin_oom);
674                 u_mmDestroy(sim_state.heap);
675                 /* No memsetting the struct, because it contains the mutex. */
676                 sim_state.mem = NULL;
677         }
678         mtx_unlock(&sim_state.mutex);
679         ralloc_free(sim_file);
680 }
681 
682 #endif /* USE_V3D_SIMULATOR */
683