2 * Copyright © 2014-2017 Broadcom
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:
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
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
25 * @file v3d_simulator.c
27 * Implements VC5 simulation on top of a non-VC5 GEM fd.
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.
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).
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
49 #ifdef USE_V3D_SIMULATOR
52 #include "util/hash_table.h"
53 #include "util/ralloc.h"
55 #include "util/u_memory.h"
56 #include "util/u_mm.h"
57 #include "v3d_simulator_wrapper.h"
59 #include "v3d_screen.h"
60 #include "v3d_context.h"
62 /** Global (across GEM fds) state for the simulator */
63 static struct v3d_simulator_state
{
69 /* Base virtual address of the heap. */
71 /* Base hardware address of the heap. */
73 /* Size of the heap. */
76 struct mem_block
*heap
;
77 struct mem_block
*overflow
;
79 /** Mapping from GEM fd to struct v3d_simulator_file * */
80 struct hash_table
*fd_map
;
84 .mutex
= _MTX_INITIALIZER_NP
,
87 /** Per-GEM-fd state for the simulator. */
88 struct v3d_simulator_file
{
91 /** Mapping from GEM handle to struct v3d_simulator_bo * */
92 struct hash_table
*bo_map
;
94 struct mem_block
*gmp
;
98 /** Wrapper for drm_v3d_bo tracking the simulator-specific state. */
99 struct v3d_simulator_bo
{
100 struct v3d_simulator_file
*file
;
102 /** Area for this BO within sim_state->mem */
103 struct mem_block
*block
;
108 uint32_t winsys_stride
;
116 return (void *)(uintptr_t)key
;
119 static struct v3d_simulator_file
*
120 v3d_get_simulator_file_for_fd(int fd
)
122 struct hash_entry
*entry
= _mesa_hash_table_search(sim_state
.fd_map
,
124 return entry
? entry
->data
: NULL
;
127 /* A marker placed just after each BO, then checked after rendering to make
128 * sure it's still there.
130 #define BO_SENTINEL 0xfedcba98
133 #define GMP_ALIGN2 17
136 * Sets the range of GPU virtual address space to have the given GMP
137 * permissions (bit 0 = read, bit 1 = write, write-only forbidden).
140 set_gmp_flags(struct v3d_simulator_file
*file
,
141 uint32_t offset
, uint32_t size
, uint32_t flag
)
143 assert((offset
& ((1 << GMP_ALIGN2
) - 1)) == 0);
144 int gmp_offset
= offset
>> GMP_ALIGN2
;
145 int gmp_count
= align(size
, 1 << GMP_ALIGN2
) >> GMP_ALIGN2
;
146 uint32_t *gmp
= file
->gmp_vaddr
;
150 for (int i
= gmp_offset
; i
< gmp_offset
+ gmp_count
; i
++) {
151 int32_t bitshift
= (i
% 16) * 2;
152 gmp
[i
/ 16] &= ~(0x3 << bitshift
);
153 gmp
[i
/ 16] |= flag
<< bitshift
;
158 * Allocates space in simulator memory and returns a tracking struct for it
159 * that also contains the drm_gem_cma_object struct.
161 static struct v3d_simulator_bo
*
162 v3d_create_simulator_bo(int fd
, int handle
, unsigned size
)
164 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
165 struct v3d_simulator_bo
*sim_bo
= rzalloc(file
,
166 struct v3d_simulator_bo
);
167 size
= align(size
, 4096);
170 sim_bo
->handle
= handle
;
172 mtx_lock(&sim_state
.mutex
);
173 sim_bo
->block
= u_mmAllocMem(sim_state
.heap
, size
+ 4, GMP_ALIGN2
, 0);
174 mtx_unlock(&sim_state
.mutex
);
175 assert(sim_bo
->block
);
177 set_gmp_flags(file
, sim_bo
->block
->ofs
, size
, 0x3);
180 sim_bo
->vaddr
= sim_state
.mem
+ sim_bo
->block
->ofs
- sim_state
.mem_base
;
181 memset(sim_bo
->vaddr
, 0xd0, size
);
183 *(uint32_t *)(sim_bo
->vaddr
+ sim_bo
->size
) = BO_SENTINEL
;
185 /* A handle of 0 is used for v3d_gem.c internal allocations that
186 * don't need to go in the lookup table.
189 mtx_lock(&sim_state
.mutex
);
190 _mesa_hash_table_insert(file
->bo_map
, int_to_key(handle
),
192 mtx_unlock(&sim_state
.mutex
);
199 v3d_free_simulator_bo(struct v3d_simulator_bo
*sim_bo
)
201 struct v3d_simulator_file
*sim_file
= sim_bo
->file
;
203 if (sim_bo
->winsys_map
)
204 munmap(sim_bo
->winsys_map
, sim_bo
->size
);
206 set_gmp_flags(sim_file
, sim_bo
->block
->ofs
, sim_bo
->size
, 0x0);
208 mtx_lock(&sim_state
.mutex
);
209 u_mmFreeMem(sim_bo
->block
);
210 if (sim_bo
->handle
) {
211 struct hash_entry
*entry
=
212 _mesa_hash_table_search(sim_file
->bo_map
,
213 int_to_key(sim_bo
->handle
));
214 _mesa_hash_table_remove(sim_file
->bo_map
, entry
);
216 mtx_unlock(&sim_state
.mutex
);
220 static struct v3d_simulator_bo
*
221 v3d_get_simulator_bo(struct v3d_simulator_file
*file
, int gem_handle
)
223 mtx_lock(&sim_state
.mutex
);
224 struct hash_entry
*entry
=
225 _mesa_hash_table_search(file
->bo_map
, int_to_key(gem_handle
));
226 mtx_unlock(&sim_state
.mutex
);
228 return entry
? entry
->data
: NULL
;
232 v3d_simulator_pin_bos(int fd
, struct v3d_job
*job
)
234 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
236 set_foreach(job
->bos
, entry
) {
237 struct v3d_bo
*bo
= (struct v3d_bo
*)entry
->key
;
238 struct v3d_simulator_bo
*sim_bo
=
239 v3d_get_simulator_bo(file
, bo
->handle
);
242 memcpy(sim_bo
->vaddr
, bo
->map
, bo
->size
);
249 v3d_simulator_unpin_bos(int fd
, struct v3d_job
*job
)
251 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
253 set_foreach(job
->bos
, entry
) {
254 struct v3d_bo
*bo
= (struct v3d_bo
*)entry
->key
;
255 struct v3d_simulator_bo
*sim_bo
=
256 v3d_get_simulator_bo(file
, bo
->handle
);
258 if (*(uint32_t *)(sim_bo
->vaddr
+
259 sim_bo
->size
) != BO_SENTINEL
) {
260 fprintf(stderr
, "Buffer overflow in %s\n", bo
->name
);
264 memcpy(bo
->map
, sim_bo
->vaddr
, bo
->size
);
271 v3d_simulator_flush(struct v3d_context
*v3d
,
272 struct drm_v3d_submit_cl
*submit
, struct v3d_job
*job
)
274 struct v3d_screen
*screen
= v3d
->screen
;
276 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
277 struct v3d_surface
*csurf
= v3d_surface(v3d
->framebuffer
.cbufs
[0]);
278 struct v3d_resource
*ctex
= csurf
? v3d_resource(csurf
->base
.texture
) : NULL
;
279 struct v3d_simulator_bo
*csim_bo
= ctex
? v3d_get_simulator_bo(file
, ctex
->bo
->handle
) : NULL
;
280 uint32_t winsys_stride
= ctex
? csim_bo
->winsys_stride
: 0;
281 uint32_t sim_stride
= ctex
? ctex
->slices
[0].stride
: 0;
282 uint32_t row_len
= MIN2(sim_stride
, winsys_stride
);
285 if (ctex
&& csim_bo
->winsys_map
) {
287 fprintf(stderr
, "%dx%d %d %d %d\n",
288 ctex
->base
.b
.width0
, ctex
->base
.b
.height0
,
294 for (int y
= 0; y
< ctex
->base
.height0
; y
++) {
295 memcpy(ctex
->bo
->map
+ y
* sim_stride
,
296 csim_bo
->winsys_map
+ y
* winsys_stride
,
301 ret
= v3d_simulator_pin_bos(fd
, job
);
305 if (sim_state
.ver
>= 41)
306 v3d41_simulator_flush(sim_state
.v3d
, submit
, file
->gmp
->ofs
);
308 v3d33_simulator_flush(sim_state
.v3d
, submit
, file
->gmp
->ofs
);
310 ret
= v3d_simulator_unpin_bos(fd
, job
);
314 if (ctex
&& csim_bo
->winsys_map
) {
315 for (int y
= 0; y
< ctex
->base
.height0
; y
++) {
316 memcpy(csim_bo
->winsys_map
+ y
* winsys_stride
,
317 ctex
->bo
->map
+ y
* sim_stride
,
326 * Map the underlying GEM object from the real hardware GEM handle.
329 v3d_simulator_map_winsys_bo(int fd
, struct v3d_simulator_bo
*sim_bo
)
334 struct drm_mode_map_dumb map_dumb
= {
335 .handle
= sim_bo
->handle
,
337 ret
= drmIoctl(fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map_dumb
);
339 fprintf(stderr
, "map ioctl failure\n");
343 map
= mmap(NULL
, sim_bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
344 fd
, map_dumb
.offset
);
345 if (map
== MAP_FAILED
) {
347 "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
348 sim_bo
->handle
, (long long)map_dumb
.offset
,
357 * Do fixups after a BO has been opened from a handle.
359 * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE
360 * time, but we're still using drmPrimeFDToHandle() so we have this helper to
361 * be called afterward instead.
363 void v3d_simulator_open_from_handle(int fd
, uint32_t winsys_stride
,
364 int handle
, uint32_t size
)
366 struct v3d_simulator_bo
*sim_bo
=
367 v3d_create_simulator_bo(fd
, handle
, size
);
369 sim_bo
->winsys_stride
= winsys_stride
;
370 sim_bo
->winsys_map
= v3d_simulator_map_winsys_bo(fd
, sim_bo
);
374 * Simulated ioctl(fd, DRM_VC5_CREATE_BO) implementation.
376 * Making a VC5 BO is just a matter of making a corresponding BO on the host.
379 v3d_simulator_create_bo_ioctl(int fd
, struct drm_v3d_create_bo
*args
)
382 struct drm_mode_create_dumb create
= {
385 .height
= (args
->size
+ 127) / 128,
388 ret
= drmIoctl(fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
389 assert(create
.size
>= args
->size
);
391 args
->handle
= create
.handle
;
393 struct v3d_simulator_bo
*sim_bo
=
394 v3d_create_simulator_bo(fd
, create
.handle
, args
->size
);
396 args
->offset
= sim_bo
->block
->ofs
;
402 * Simulated ioctl(fd, DRM_VC5_MMAP_BO) implementation.
404 * We just pass this straight through to dumb mmap.
407 v3d_simulator_mmap_bo_ioctl(int fd
, struct drm_v3d_mmap_bo
*args
)
410 struct drm_mode_map_dumb map
= {
411 .handle
= args
->handle
,
414 ret
= drmIoctl(fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map
);
415 args
->offset
= map
.offset
;
421 v3d_simulator_get_bo_offset_ioctl(int fd
, struct drm_v3d_get_bo_offset
*args
)
423 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
424 struct v3d_simulator_bo
*sim_bo
= v3d_get_simulator_bo(file
,
427 args
->offset
= sim_bo
->block
->ofs
;
433 v3d_simulator_gem_close_ioctl(int fd
, struct drm_gem_close
*args
)
435 /* Free the simulator's internal tracking. */
436 struct v3d_simulator_file
*file
= v3d_get_simulator_file_for_fd(fd
);
437 struct v3d_simulator_bo
*sim_bo
= v3d_get_simulator_bo(file
,
440 v3d_free_simulator_bo(sim_bo
);
442 /* Pass the call on down. */
443 return drmIoctl(fd
, DRM_IOCTL_GEM_CLOSE
, args
);
447 v3d_simulator_get_param_ioctl(int fd
, struct drm_v3d_get_param
*args
)
449 if (sim_state
.ver
>= 41)
450 return v3d41_simulator_get_param_ioctl(sim_state
.v3d
, args
);
452 return v3d33_simulator_get_param_ioctl(sim_state
.v3d
, args
);
456 v3d_simulator_ioctl(int fd
, unsigned long request
, void *args
)
459 case DRM_IOCTL_V3D_CREATE_BO
:
460 return v3d_simulator_create_bo_ioctl(fd
, args
);
461 case DRM_IOCTL_V3D_MMAP_BO
:
462 return v3d_simulator_mmap_bo_ioctl(fd
, args
);
463 case DRM_IOCTL_V3D_GET_BO_OFFSET
:
464 return v3d_simulator_get_bo_offset_ioctl(fd
, args
);
466 case DRM_IOCTL_V3D_WAIT_BO
:
467 /* We do all of the v3d rendering synchronously, so we just
468 * return immediately on the wait ioctls. This ignores any
469 * native rendering to the host BO, so it does mean we race on
470 * front buffer rendering.
474 case DRM_IOCTL_V3D_GET_PARAM
:
475 return v3d_simulator_get_param_ioctl(fd
, args
);
477 case DRM_IOCTL_GEM_CLOSE
:
478 return v3d_simulator_gem_close_ioctl(fd
, args
);
480 case DRM_IOCTL_GEM_OPEN
:
481 case DRM_IOCTL_GEM_FLINK
:
482 return drmIoctl(fd
, request
, args
);
484 fprintf(stderr
, "Unknown ioctl 0x%08x\n", (int)request
);
490 v3d_simulator_init_global(const struct v3d_device_info
*devinfo
)
492 mtx_lock(&sim_state
.mutex
);
493 if (sim_state
.refcount
++) {
494 mtx_unlock(&sim_state
.mutex
);
498 sim_state
.v3d
= v3d_hw_auto_new(NULL
);
499 v3d_hw_alloc_mem(sim_state
.v3d
, 1024 * 1024 * 1024);
501 v3d_hw_get_mem(sim_state
.v3d
, &sim_state
.mem_size
,
504 /* Allocate from anywhere from 4096 up. We don't allocate at 0,
505 * because for OQs and some other addresses in the HW, 0 means
508 sim_state
.heap
= u_mmInit(4096, sim_state
.mem_size
- 4096);
510 /* Make a block of 0xd0 at address 0 to make sure we don't screw up
513 struct mem_block
*b
= u_mmAllocMem(sim_state
.heap
, 4096, GMP_ALIGN2
, 0);
514 memset(sim_state
.mem
+ b
->ofs
- sim_state
.mem_base
, 0xd0, 4096);
516 sim_state
.ver
= v3d_hw_get_version(sim_state
.v3d
);
518 mtx_unlock(&sim_state
.mutex
);
521 _mesa_hash_table_create(NULL
,
523 _mesa_key_pointer_equal
);
525 if (sim_state
.ver
>= 41)
526 v3d41_simulator_init_regs(sim_state
.v3d
);
528 v3d33_simulator_init_regs(sim_state
.v3d
);
532 v3d_simulator_init(struct v3d_screen
*screen
)
534 v3d_simulator_init_global(&screen
->devinfo
);
536 screen
->sim_file
= rzalloc(screen
, struct v3d_simulator_file
);
537 struct v3d_simulator_file
*sim_file
= screen
->sim_file
;
539 screen
->sim_file
->bo_map
=
540 _mesa_hash_table_create(screen
->sim_file
,
542 _mesa_key_pointer_equal
);
544 mtx_lock(&sim_state
.mutex
);
545 _mesa_hash_table_insert(sim_state
.fd_map
, int_to_key(screen
->fd
+ 1),
547 mtx_unlock(&sim_state
.mutex
);
549 sim_file
->gmp
= u_mmAllocMem(sim_state
.heap
, 8096, GMP_ALIGN2
, 0);
550 sim_file
->gmp_vaddr
= (sim_state
.mem
+ sim_file
->gmp
->ofs
-
555 v3d_simulator_destroy(struct v3d_screen
*screen
)
557 mtx_lock(&sim_state
.mutex
);
558 if (!--sim_state
.refcount
) {
559 _mesa_hash_table_destroy(sim_state
.fd_map
, NULL
);
560 u_mmDestroy(sim_state
.heap
);
561 /* No memsetting the struct, because it contains the mutex. */
562 sim_state
.mem
= NULL
;
564 mtx_unlock(&sim_state
.mutex
);
567 #endif /* USE_V3D_SIMULATOR */