intel/dump_gpu: Handle a bunch of getparam in the no-HW case
[mesa.git] / src / intel / tools / intel_dump_gpu.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/mman.h>
39 #include <dlfcn.h>
40 #include "drm-uapi/i915_drm.h"
41 #include <inttypes.h>
42
43 #include "intel_aub.h"
44 #include "aub_write.h"
45
46 #include "dev/gen_device_info.h"
47 #include "util/macros.h"
48
49 static int close_init_helper(int fd);
50 static int ioctl_init_helper(int fd, unsigned long request, ...);
51
52 static int (*libc_close)(int fd) = close_init_helper;
53 static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
54
55 static int drm_fd = -1;
56 static char *output_filename = NULL;
57 static FILE *output_file = NULL;
58 static int verbose = 0;
59 static bool device_override;
60
61 #define MAX_FD_COUNT 64
62 #define MAX_BO_COUNT 64 * 1024
63
64 struct bo {
65 uint32_t size;
66 uint64_t offset;
67 void *map;
68 };
69
70 static struct bo *bos;
71
72 #define DRM_MAJOR 226
73
74 /* We set bit 0 in the map pointer for userptr BOs so we know not to
75 * munmap them on DRM_IOCTL_GEM_CLOSE.
76 */
77 #define USERPTR_FLAG 1
78 #define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
79 #define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )
80
81 static void __attribute__ ((format(__printf__, 2, 3)))
82 fail_if(int cond, const char *format, ...)
83 {
84 va_list args;
85
86 if (!cond)
87 return;
88
89 va_start(args, format);
90 fprintf(stderr, "intel_dump_gpu: ");
91 vfprintf(stderr, format, args);
92 va_end(args);
93
94 raise(SIGTRAP);
95 }
96
97 static struct bo *
98 get_bo(unsigned fd, uint32_t handle)
99 {
100 struct bo *bo;
101
102 fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
103 fail_if(fd >= MAX_FD_COUNT, "bo fd too large\n");
104 bo = &bos[handle + fd * MAX_BO_COUNT];
105
106 return bo;
107 }
108
109 static inline uint32_t
110 align_u32(uint32_t v, uint32_t a)
111 {
112 return (v + a - 1) & ~(a - 1);
113 }
114
115 static struct gen_device_info devinfo = {0};
116 static int device = 0;
117 static struct aub_file aub_file;
118
119 static void
120 ensure_device_info(int fd)
121 {
122 /* We can't do this at open time as we're not yet authenticated. */
123 if (device == 0) {
124 fail_if(!gen_get_device_info_from_fd(fd, &devinfo),
125 "failed to identify chipset.\n");
126 device = devinfo.chipset_id;
127 } else if (devinfo.gen == 0) {
128 fail_if(!gen_get_device_info_from_pci_id(device, &devinfo),
129 "failed to identify chipset.\n");
130 }
131 }
132
133 static void *
134 relocate_bo(int fd, struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
135 const struct drm_i915_gem_exec_object2 *obj)
136 {
137 const struct drm_i915_gem_exec_object2 *exec_objects =
138 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
139 const struct drm_i915_gem_relocation_entry *relocs =
140 (const struct drm_i915_gem_relocation_entry *) (uintptr_t) obj->relocs_ptr;
141 void *relocated;
142 int handle;
143
144 relocated = malloc(bo->size);
145 fail_if(relocated == NULL, "out of memory\n");
146 memcpy(relocated, GET_PTR(bo->map), bo->size);
147 for (size_t i = 0; i < obj->relocation_count; i++) {
148 fail_if(relocs[i].offset >= bo->size, "reloc outside bo\n");
149
150 if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
151 handle = exec_objects[relocs[i].target_handle].handle;
152 else
153 handle = relocs[i].target_handle;
154
155 aub_write_reloc(&devinfo, ((char *)relocated) + relocs[i].offset,
156 get_bo(fd, handle)->offset + relocs[i].delta);
157 }
158
159 return relocated;
160 }
161
162 static int
163 gem_ioctl(int fd, unsigned long request, void *argp)
164 {
165 int ret;
166
167 do {
168 ret = libc_ioctl(fd, request, argp);
169 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
170
171 return ret;
172 }
173
174 static void *
175 gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size)
176 {
177 struct drm_i915_gem_mmap mmap = {
178 .handle = handle,
179 .offset = offset,
180 .size = size
181 };
182
183 if (gem_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap) == -1)
184 return MAP_FAILED;
185
186 return (void *)(uintptr_t) mmap.addr_ptr;
187 }
188
189 static enum drm_i915_gem_engine_class
190 engine_class_from_ring_flag(uint32_t ring_flag)
191 {
192 switch (ring_flag) {
193 case I915_EXEC_DEFAULT:
194 case I915_EXEC_RENDER:
195 return I915_ENGINE_CLASS_RENDER;
196 case I915_EXEC_BSD:
197 return I915_ENGINE_CLASS_VIDEO;
198 case I915_EXEC_BLT:
199 return I915_ENGINE_CLASS_COPY;
200 case I915_EXEC_VEBOX:
201 return I915_ENGINE_CLASS_VIDEO_ENHANCE;
202 default:
203 return I915_ENGINE_CLASS_INVALID;
204 }
205 }
206
207 static void
208 dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
209 {
210 struct drm_i915_gem_exec_object2 *exec_objects =
211 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
212 uint32_t ring_flag = execbuffer2->flags & I915_EXEC_RING_MASK;
213 uint32_t offset;
214 struct drm_i915_gem_exec_object2 *obj;
215 struct bo *bo, *batch_bo;
216 int batch_index;
217 void *data;
218
219 ensure_device_info(fd);
220
221 if (!aub_file.file) {
222 aub_file_init(&aub_file, output_file,
223 verbose == 2 ? stdout : NULL,
224 device, program_invocation_short_name);
225 aub_write_default_setup(&aub_file);
226
227 if (verbose)
228 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
229 output_filename, device, devinfo.gen);
230 }
231
232 if (aub_use_execlists(&aub_file))
233 offset = 0x1000;
234 else
235 offset = aub_gtt_size(&aub_file);
236
237 if (verbose)
238 printf("Dumping execbuffer2:\n");
239
240 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
241 obj = &exec_objects[i];
242 bo = get_bo(fd, obj->handle);
243
244 /* If bo->size == 0, this means they passed us an invalid
245 * buffer. The kernel will reject it and so should we.
246 */
247 if (bo->size == 0) {
248 if (verbose)
249 printf("BO #%d is invalid!\n", obj->handle);
250 return;
251 }
252
253 if (obj->flags & EXEC_OBJECT_PINNED) {
254 bo->offset = obj->offset;
255 if (verbose)
256 printf("BO #%d (%dB) pinned @ 0x%" PRIx64 "\n",
257 obj->handle, bo->size, bo->offset);
258 } else {
259 if (obj->alignment != 0)
260 offset = align_u32(offset, obj->alignment);
261 bo->offset = offset;
262 if (verbose)
263 printf("BO #%d (%dB) @ 0x%" PRIx64 "\n", obj->handle,
264 bo->size, bo->offset);
265 offset = align_u32(offset + bo->size + 4095, 4096);
266 }
267
268 if (bo->map == NULL && bo->size > 0)
269 bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
270 fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
271
272 if (aub_use_execlists(&aub_file))
273 aub_map_ppgtt(&aub_file, bo->offset, bo->size);
274 }
275
276 batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
277 execbuffer2->buffer_count - 1;
278 batch_bo = get_bo(fd, exec_objects[batch_index].handle);
279 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
280 obj = &exec_objects[i];
281 bo = get_bo(fd, obj->handle);
282
283 if (obj->relocation_count > 0)
284 data = relocate_bo(fd, bo, execbuffer2, obj);
285 else
286 data = bo->map;
287
288 if (bo == batch_bo) {
289 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
290 GET_PTR(data), bo->size, bo->offset);
291 } else {
292 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
293 GET_PTR(data), bo->size, bo->offset);
294 }
295
296 if (data != bo->map)
297 free(data);
298 }
299
300 uint32_t ctx_id = execbuffer2->rsvd1;
301
302 aub_write_exec(&aub_file, ctx_id,
303 batch_bo->offset + execbuffer2->batch_start_offset,
304 offset, engine_class_from_ring_flag(ring_flag));
305
306 if (device_override &&
307 (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
308 struct drm_i915_gem_exec_fence *fences =
309 (void*)(uintptr_t)execbuffer2->cliprects_ptr;
310 for (uint32_t i = 0; i < execbuffer2->num_cliprects; i++) {
311 if ((fences[i].flags & I915_EXEC_FENCE_SIGNAL) != 0) {
312 struct drm_syncobj_array arg = {
313 .handles = (uintptr_t)&fences[i].handle,
314 .count_handles = 1,
315 .pad = 0,
316 };
317 libc_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &arg);
318 }
319 }
320 }
321 }
322
323 static void
324 add_new_bo(unsigned fd, int handle, uint64_t size, void *map)
325 {
326 struct bo *bo = &bos[handle + fd * MAX_BO_COUNT];
327
328 fail_if(handle >= MAX_BO_COUNT, "bo handle out of range\n");
329 fail_if(fd >= MAX_FD_COUNT, "bo fd out of range\n");
330 fail_if(size == 0, "bo size is invalid\n");
331
332 bo->size = size;
333 bo->map = map;
334 }
335
336 static void
337 remove_bo(int fd, int handle)
338 {
339 struct bo *bo = get_bo(fd, handle);
340
341 if (bo->map && !IS_USERPTR(bo->map))
342 munmap(bo->map, bo->size);
343 bo->size = 0;
344 bo->map = NULL;
345 }
346
347 __attribute__ ((visibility ("default"))) int
348 close(int fd)
349 {
350 if (fd == drm_fd)
351 drm_fd = -1;
352
353 return libc_close(fd);
354 }
355
356 static int
357 get_pci_id(int fd, int *pci_id)
358 {
359 struct drm_i915_getparam gparam;
360
361 if (device_override) {
362 *pci_id = device;
363 return 0;
364 }
365
366 gparam.param = I915_PARAM_CHIPSET_ID;
367 gparam.value = pci_id;
368 return libc_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gparam);
369 }
370
371 static void
372 maybe_init(int fd)
373 {
374 static bool initialized = false;
375 FILE *config;
376 char *key, *value;
377
378 if (initialized)
379 return;
380
381 initialized = true;
382
383 config = fopen(getenv("INTEL_DUMP_GPU_CONFIG"), "r");
384 while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
385 if (!strcmp(key, "verbose")) {
386 if (!strcmp(value, "1")) {
387 verbose = 1;
388 } else if (!strcmp(value, "2")) {
389 verbose = 2;
390 }
391 } else if (!strcmp(key, "device")) {
392 fail_if(device != 0, "Device/Platform override specified multiple times.");
393 fail_if(sscanf(value, "%i", &device) != 1,
394 "failed to parse device id '%s'",
395 value);
396 device_override = true;
397 } else if (!strcmp(key, "platform")) {
398 fail_if(device != 0, "Device/Platform override specified multiple times.");
399 device = gen_device_name_to_pci_device_id(value);
400 fail_if(device == -1, "Unknown platform '%s'", value);
401 device_override = true;
402 } else if (!strcmp(key, "file")) {
403 output_filename = strdup(value);
404 output_file = fopen(output_filename, "w+");
405 fail_if(output_file == NULL,
406 "failed to open file '%s'\n",
407 output_filename);
408 } else {
409 fprintf(stderr, "unknown option '%s'\n", key);
410 }
411
412 free(key);
413 free(value);
414 }
415 fclose(config);
416
417 bos = calloc(MAX_FD_COUNT * MAX_BO_COUNT, sizeof(bos[0]));
418 fail_if(bos == NULL, "out of memory\n");
419
420 int ret = get_pci_id(fd, &device);
421 assert(ret == 0);
422
423 aub_file_init(&aub_file, output_file,
424 verbose == 2 ? stdout : NULL,
425 device, program_invocation_short_name);
426 aub_write_default_setup(&aub_file);
427
428 if (verbose)
429 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
430 output_filename, device, devinfo.gen);
431 }
432
433 __attribute__ ((visibility ("default"))) int
434 ioctl(int fd, unsigned long request, ...)
435 {
436 va_list args;
437 void *argp;
438 int ret;
439 struct stat buf;
440
441 va_start(args, request);
442 argp = va_arg(args, void *);
443 va_end(args);
444
445 if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
446 drm_fd != fd && fstat(fd, &buf) == 0 &&
447 (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
448 drm_fd = fd;
449 if (verbose)
450 printf("[intercept drm ioctl on fd %d]\n", fd);
451 }
452
453 if (fd == drm_fd) {
454 maybe_init(fd);
455
456 switch (request) {
457 case DRM_IOCTL_SYNCOBJ_WAIT:
458 case DRM_IOCTL_I915_GEM_WAIT: {
459 if (device_override)
460 return 0;
461 return libc_ioctl(fd, request, argp);
462 }
463
464 case DRM_IOCTL_I915_GET_RESET_STATS: {
465 if (device_override) {
466 struct drm_i915_reset_stats *stats = argp;
467
468 stats->reset_count = 0;
469 stats->batch_active = 0;
470 stats->batch_pending = 0;
471 return 0;
472 }
473 return libc_ioctl(fd, request, argp);
474 }
475
476 case DRM_IOCTL_I915_GETPARAM: {
477 struct drm_i915_getparam *getparam = argp;
478
479 ensure_device_info(fd);
480
481 if (getparam->param == I915_PARAM_CHIPSET_ID)
482 return get_pci_id(fd, getparam->value);
483
484 if (device_override) {
485 switch (getparam->param) {
486 case I915_PARAM_CS_TIMESTAMP_FREQUENCY:
487 *getparam->value = devinfo.timestamp_frequency;
488 return 0;
489
490 case I915_PARAM_HAS_WAIT_TIMEOUT:
491 case I915_PARAM_HAS_EXECBUF2:
492 case I915_PARAM_MMAP_VERSION:
493 case I915_PARAM_HAS_EXEC_ASYNC:
494 case I915_PARAM_HAS_EXEC_FENCE:
495 case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
496 *getparam->value = 1;
497 return 0;
498
499 case I915_PARAM_HAS_EXEC_SOFTPIN:
500 *getparam->value = devinfo.gen >= 8 && !devinfo.is_cherryview;
501 return 0;
502
503 default:
504 return -1;
505 }
506 }
507
508 return libc_ioctl(fd, request, argp);
509 }
510
511 case DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM: {
512 struct drm_i915_gem_context_param *getparam = argp;
513
514 ensure_device_info(fd);
515
516 if (device_override) {
517 switch (getparam->param) {
518 case I915_CONTEXT_PARAM_GTT_SIZE:
519 if (devinfo.is_elkhartlake)
520 getparam->value = 1ull << 36;
521 else if (devinfo.gen >= 8 && !devinfo.is_cherryview)
522 getparam->value = 1ull << 48;
523 else
524 getparam->value = 1ull << 31;
525 return 0;
526
527 default:
528 return -1;
529 }
530 }
531
532 return libc_ioctl(fd, request, argp);
533 }
534
535 case DRM_IOCTL_I915_GEM_EXECBUFFER: {
536 static bool once;
537 if (!once) {
538 fprintf(stderr,
539 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
540 once = true;
541 }
542 return libc_ioctl(fd, request, argp);
543 }
544
545 case DRM_IOCTL_I915_GEM_EXECBUFFER2:
546 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR: {
547 dump_execbuffer2(fd, argp);
548 if (device_override)
549 return 0;
550
551 return libc_ioctl(fd, request, argp);
552 }
553
554 case DRM_IOCTL_I915_GEM_CONTEXT_CREATE: {
555 uint32_t *ctx_id = NULL;
556 struct drm_i915_gem_context_create *create = argp;
557 ret = 0;
558 if (!device_override) {
559 ret = libc_ioctl(fd, request, argp);
560 ctx_id = &create->ctx_id;
561 }
562
563 if (ret == 0)
564 create->ctx_id = aub_write_context_create(&aub_file, ctx_id);
565
566 return ret;
567 }
568
569 case DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT: {
570 uint32_t *ctx_id = NULL;
571 struct drm_i915_gem_context_create_ext *create = argp;
572 ret = 0;
573 if (!device_override) {
574 ret = libc_ioctl(fd, request, argp);
575 ctx_id = &create->ctx_id;
576 }
577
578 if (ret == 0)
579 create->ctx_id = aub_write_context_create(&aub_file, ctx_id);
580
581 return ret;
582 }
583
584 case DRM_IOCTL_I915_GEM_CREATE: {
585 struct drm_i915_gem_create *create = argp;
586
587 ret = libc_ioctl(fd, request, argp);
588 if (ret == 0)
589 add_new_bo(fd, create->handle, create->size, NULL);
590
591 return ret;
592 }
593
594 case DRM_IOCTL_I915_GEM_USERPTR: {
595 struct drm_i915_gem_userptr *userptr = argp;
596
597 ret = libc_ioctl(fd, request, argp);
598 if (ret == 0)
599 add_new_bo(fd, userptr->handle, userptr->user_size,
600 (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
601
602 return ret;
603 }
604
605 case DRM_IOCTL_GEM_CLOSE: {
606 struct drm_gem_close *close = argp;
607
608 remove_bo(fd, close->handle);
609
610 return libc_ioctl(fd, request, argp);
611 }
612
613 case DRM_IOCTL_GEM_OPEN: {
614 struct drm_gem_open *open = argp;
615
616 ret = libc_ioctl(fd, request, argp);
617 if (ret == 0)
618 add_new_bo(fd, open->handle, open->size, NULL);
619
620 return ret;
621 }
622
623 case DRM_IOCTL_PRIME_FD_TO_HANDLE: {
624 struct drm_prime_handle *prime = argp;
625
626 ret = libc_ioctl(fd, request, argp);
627 if (ret == 0) {
628 off_t size;
629
630 size = lseek(prime->fd, 0, SEEK_END);
631 fail_if(size == -1, "failed to get prime bo size\n");
632 add_new_bo(fd, prime->handle, size, NULL);
633
634 }
635
636 return ret;
637 }
638
639 default:
640 return libc_ioctl(fd, request, argp);
641 }
642 } else {
643 return libc_ioctl(fd, request, argp);
644 }
645 }
646
647 static void
648 init(void)
649 {
650 libc_close = dlsym(RTLD_NEXT, "close");
651 libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
652 fail_if(libc_close == NULL || libc_ioctl == NULL,
653 "failed to get libc ioctl or close\n");
654 }
655
656 static int
657 close_init_helper(int fd)
658 {
659 init();
660 return libc_close(fd);
661 }
662
663 static int
664 ioctl_init_helper(int fd, unsigned long request, ...)
665 {
666 va_list args;
667 void *argp;
668
669 va_start(args, request);
670 argp = va_arg(args, void *);
671 va_end(args);
672
673 init();
674 return libc_ioctl(fd, request, argp);
675 }
676
677 static void __attribute__ ((destructor))
678 fini(void)
679 {
680 if (devinfo.gen != 0) {
681 free(output_filename);
682 aub_file_finish(&aub_file);
683 free(bos);
684 }
685 }