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