2 * Copyright © 2015 Intel Corporation
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
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
35 #include <sys/ioctl.h>
40 #include "drm-uapi/i915_drm.h"
43 #include "intel_aub.h"
44 #include "aub_write.h"
46 #include "dev/gen_device_info.h"
47 #include "util/macros.h"
49 static int close_init_helper(int fd
);
50 static int ioctl_init_helper(int fd
, unsigned long request
, ...);
52 static int (*libc_close
)(int fd
) = close_init_helper
;
53 static int (*libc_ioctl
)(int fd
, unsigned long request
, ...) = ioctl_init_helper
;
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
;
61 #define MAX_FD_COUNT 64
62 #define MAX_BO_COUNT 64 * 1024
71 static struct bo
*bos
;
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.
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) )
82 static void __attribute__ ((format(__printf__
, 2, 3)))
83 fail_if(int cond
, const char *format
, ...)
90 va_start(args
, format
);
91 fprintf(stderr
, "intel_dump_gpu: ");
92 vfprintf(stderr
, format
, args
);
99 get_bo(unsigned fd
, uint32_t handle
)
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
];
110 static inline uint32_t
111 align_u32(uint32_t v
, uint32_t a
)
113 return (v
+ a
- 1) & ~(a
- 1);
116 static struct gen_device_info devinfo
= {0};
117 static int device
= 0;
118 static struct aub_file aub_file
;
121 ensure_device_info(int fd
)
123 /* We can't do this at open time as we're not yet authenticated. */
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");
135 relocate_bo(int fd
, struct bo
*bo
, const struct drm_i915_gem_execbuffer2
*execbuffer2
,
136 const struct drm_i915_gem_exec_object2
*obj
)
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
;
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");
151 if (execbuffer2
->flags
& I915_EXEC_HANDLE_LUT
)
152 handle
= exec_objects
[relocs
[i
].target_handle
].handle
;
154 handle
= relocs
[i
].target_handle
;
156 aub_write_reloc(&devinfo
, ((char *)relocated
) + relocs
[i
].offset
,
157 get_bo(fd
, handle
)->offset
+ relocs
[i
].delta
);
164 gem_ioctl(int fd
, unsigned long request
, void *argp
)
169 ret
= libc_ioctl(fd
, request
, argp
);
170 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
176 gem_mmap(int fd
, uint32_t handle
, uint64_t offset
, uint64_t size
)
178 struct drm_i915_gem_mmap mmap
= {
184 if (gem_ioctl(fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap
) == -1)
187 return (void *)(uintptr_t) mmap
.addr_ptr
;
190 static enum drm_i915_gem_engine_class
191 engine_class_from_ring_flag(uint32_t ring_flag
)
194 case I915_EXEC_DEFAULT
:
195 case I915_EXEC_RENDER
:
196 return I915_ENGINE_CLASS_RENDER
;
198 return I915_ENGINE_CLASS_VIDEO
;
200 return I915_ENGINE_CLASS_COPY
;
201 case I915_EXEC_VEBOX
:
202 return I915_ENGINE_CLASS_VIDEO_ENHANCE
;
204 return I915_ENGINE_CLASS_INVALID
;
209 dump_execbuffer2(int fd
, struct drm_i915_gem_execbuffer2
*execbuffer2
)
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
;
215 struct drm_i915_gem_exec_object2
*obj
;
216 struct bo
*bo
, *batch_bo
;
220 ensure_device_info(fd
);
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
);
229 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
230 output_filename
, device
, devinfo
.gen
);
233 if (aub_use_execlists(&aub_file
))
236 offset
= aub_gtt_size(&aub_file
);
239 printf("Dumping execbuffer2:\n");
241 for (uint32_t i
= 0; i
< execbuffer2
->buffer_count
; i
++) {
242 obj
= &exec_objects
[i
];
243 bo
= get_bo(fd
, obj
->handle
);
245 /* If bo->size == 0, this means they passed us an invalid
246 * buffer. The kernel will reject it and so should we.
250 printf("BO #%d is invalid!\n", obj
->handle
);
254 if (obj
->flags
& EXEC_OBJECT_PINNED
) {
255 bo
->offset
= obj
->offset
;
257 printf("BO #%d (%dB) pinned @ 0x%" PRIx64
"\n",
258 obj
->handle
, bo
->size
, bo
->offset
);
260 if (obj
->alignment
!= 0)
261 offset
= align_u32(offset
, obj
->alignment
);
264 printf("BO #%d (%dB) @ 0x%" PRIx64
"\n", obj
->handle
,
265 bo
->size
, bo
->offset
);
266 offset
= align_u32(offset
+ bo
->size
+ 4095, 4096);
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");
273 if (aub_use_execlists(&aub_file
))
274 aub_map_ppgtt(&aub_file
, bo
->offset
, bo
->size
);
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
);
284 if (obj
->relocation_count
> 0)
285 data
= relocate_bo(fd
, bo
, execbuffer2
, obj
);
290 if (bo
== batch_bo
) {
291 aub_write_trace_block(&aub_file
, AUB_TRACE_TYPE_BATCH
,
292 GET_PTR(data
), bo
->size
, bo
->offset
);
294 aub_write_trace_block(&aub_file
, AUB_TRACE_TYPE_NOTYPE
,
295 GET_PTR(data
), bo
->size
, bo
->offset
);
303 uint32_t ctx_id
= execbuffer2
->rsvd1
;
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
));
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
,
320 libc_ioctl(fd
, DRM_IOCTL_SYNCOBJ_SIGNAL
, &arg
);
327 add_new_bo(unsigned fd
, int handle
, uint64_t size
, void *map
)
329 struct bo
*bo
= &bos
[handle
+ fd
* MAX_BO_COUNT
];
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");
341 remove_bo(int fd
, int handle
)
343 struct bo
*bo
= get_bo(fd
, handle
);
345 if (bo
->map
&& !IS_USERPTR(bo
->map
))
346 munmap(bo
->map
, bo
->size
);
352 __attribute__ ((visibility ("default"))) int
358 return libc_close(fd
);
362 get_pci_id(int fd
, int *pci_id
)
364 struct drm_i915_getparam gparam
;
366 if (device_override
) {
371 gparam
.param
= I915_PARAM_CHIPSET_ID
;
372 gparam
.value
= pci_id
;
373 return libc_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gparam
);
379 static bool initialized
= false;
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")) {
393 } else if (!strcmp(value
, "2")) {
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'",
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",
414 fprintf(stderr
, "unknown option '%s'\n", key
);
422 bos
= calloc(MAX_FD_COUNT
* MAX_BO_COUNT
, sizeof(bos
[0]));
423 fail_if(bos
== NULL
, "out of memory\n");
425 int ret
= get_pci_id(fd
, &device
);
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
);
434 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
435 output_filename
, device
, devinfo
.gen
);
438 __attribute__ ((visibility ("default"))) int
439 ioctl(int fd
, unsigned long request
, ...)
446 va_start(args
, request
);
447 argp
= va_arg(args
, void *);
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
) {
455 printf("[intercept drm ioctl on fd %d]\n", fd
);
462 case DRM_IOCTL_SYNCOBJ_WAIT
:
463 case DRM_IOCTL_I915_GEM_WAIT
: {
466 return libc_ioctl(fd
, request
, argp
);
469 case DRM_IOCTL_I915_GET_RESET_STATS
: {
470 if (device_override
) {
471 struct drm_i915_reset_stats
*stats
= argp
;
473 stats
->reset_count
= 0;
474 stats
->batch_active
= 0;
475 stats
->batch_pending
= 0;
478 return libc_ioctl(fd
, request
, argp
);
481 case DRM_IOCTL_I915_GETPARAM
: {
482 struct drm_i915_getparam
*getparam
= argp
;
484 ensure_device_info(fd
);
486 if (getparam
->param
== I915_PARAM_CHIPSET_ID
)
487 return get_pci_id(fd
, getparam
->value
);
489 if (device_override
) {
490 switch (getparam
->param
) {
491 case I915_PARAM_CS_TIMESTAMP_FREQUENCY
:
492 *getparam
->value
= devinfo
.timestamp_frequency
;
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;
504 case I915_PARAM_HAS_EXEC_SOFTPIN
:
505 *getparam
->value
= devinfo
.gen
>= 8 && !devinfo
.is_cherryview
;
513 return libc_ioctl(fd
, request
, argp
);
516 case DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM
: {
517 struct drm_i915_gem_context_param
*getparam
= argp
;
519 ensure_device_info(fd
);
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;
529 getparam
->value
= 1ull << 31;
537 return libc_ioctl(fd
, request
, argp
);
540 case DRM_IOCTL_I915_GEM_EXECBUFFER
: {
544 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
547 return libc_ioctl(fd
, request
, argp
);
550 case DRM_IOCTL_I915_GEM_EXECBUFFER2
:
551 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
: {
552 dump_execbuffer2(fd
, argp
);
556 return libc_ioctl(fd
, request
, argp
);
559 case DRM_IOCTL_I915_GEM_CONTEXT_CREATE
: {
560 uint32_t *ctx_id
= NULL
;
561 struct drm_i915_gem_context_create
*create
= argp
;
563 if (!device_override
) {
564 ret
= libc_ioctl(fd
, request
, argp
);
565 ctx_id
= &create
->ctx_id
;
569 create
->ctx_id
= aub_write_context_create(&aub_file
, ctx_id
);
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
;
578 if (!device_override
) {
579 ret
= libc_ioctl(fd
, request
, argp
);
580 ctx_id
= &create
->ctx_id
;
584 create
->ctx_id
= aub_write_context_create(&aub_file
, ctx_id
);
589 case DRM_IOCTL_I915_GEM_CREATE
: {
590 struct drm_i915_gem_create
*create
= argp
;
592 ret
= libc_ioctl(fd
, request
, argp
);
594 add_new_bo(fd
, create
->handle
, create
->size
, NULL
);
599 case DRM_IOCTL_I915_GEM_USERPTR
: {
600 struct drm_i915_gem_userptr
*userptr
= argp
;
602 ret
= libc_ioctl(fd
, request
, argp
);
604 add_new_bo(fd
, userptr
->handle
, userptr
->user_size
,
605 (void *) (uintptr_t) (userptr
->user_ptr
| USERPTR_FLAG
));
610 case DRM_IOCTL_GEM_CLOSE
: {
611 struct drm_gem_close
*close
= argp
;
613 remove_bo(fd
, close
->handle
);
615 return libc_ioctl(fd
, request
, argp
);
618 case DRM_IOCTL_GEM_OPEN
: {
619 struct drm_gem_open
*open
= argp
;
621 ret
= libc_ioctl(fd
, request
, argp
);
623 add_new_bo(fd
, open
->handle
, open
->size
, NULL
);
628 case DRM_IOCTL_PRIME_FD_TO_HANDLE
: {
629 struct drm_prime_handle
*prime
= argp
;
631 ret
= libc_ioctl(fd
, request
, argp
);
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
);
644 case DRM_IOCTL_I915_GEM_MMAP
: {
645 ret
= libc_ioctl(fd
, request
, argp
);
647 struct drm_i915_gem_mmap
*mmap
= argp
;
648 struct bo
*bo
= get_bo(fd
, mmap
->handle
);
655 return libc_ioctl(fd
, request
, argp
);
658 return libc_ioctl(fd
, request
, argp
);
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");
672 close_init_helper(int fd
)
675 return libc_close(fd
);
679 ioctl_init_helper(int fd
, unsigned long request
, ...)
684 va_start(args
, request
);
685 argp
= va_arg(args
, void *);
689 return libc_ioctl(fd
, request
, argp
);
692 static void __attribute__ ((destructor
))
695 if (devinfo
.gen
!= 0) {
696 free(output_filename
);
697 aub_file_finish(&aub_file
);