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