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
70 static struct bo
*bos
;
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.
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) )
81 static void __attribute__ ((format(__printf__
, 2, 3)))
82 fail_if(int cond
, const char *format
, ...)
89 va_start(args
, format
);
90 fprintf(stderr
, "intel_dump_gpu: ");
91 vfprintf(stderr
, format
, args
);
98 get_bo(unsigned fd
, uint32_t handle
)
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
];
109 static inline uint32_t
110 align_u32(uint32_t v
, uint32_t a
)
112 return (v
+ a
- 1) & ~(a
- 1);
115 static struct gen_device_info devinfo
= {0};
116 static uint32_t device
= 0;
117 static struct aub_file aub_file
;
120 relocate_bo(int fd
, struct bo
*bo
, const struct drm_i915_gem_execbuffer2
*execbuffer2
,
121 const struct drm_i915_gem_exec_object2
*obj
)
123 const struct drm_i915_gem_exec_object2
*exec_objects
=
124 (struct drm_i915_gem_exec_object2
*) (uintptr_t) execbuffer2
->buffers_ptr
;
125 const struct drm_i915_gem_relocation_entry
*relocs
=
126 (const struct drm_i915_gem_relocation_entry
*) (uintptr_t) obj
->relocs_ptr
;
130 relocated
= malloc(bo
->size
);
131 fail_if(relocated
== NULL
, "out of memory\n");
132 memcpy(relocated
, GET_PTR(bo
->map
), bo
->size
);
133 for (size_t i
= 0; i
< obj
->relocation_count
; i
++) {
134 fail_if(relocs
[i
].offset
>= bo
->size
, "reloc outside bo\n");
136 if (execbuffer2
->flags
& I915_EXEC_HANDLE_LUT
)
137 handle
= exec_objects
[relocs
[i
].target_handle
].handle
;
139 handle
= relocs
[i
].target_handle
;
141 aub_write_reloc(&devinfo
, ((char *)relocated
) + relocs
[i
].offset
,
142 get_bo(fd
, handle
)->offset
+ relocs
[i
].delta
);
149 gem_ioctl(int fd
, unsigned long request
, void *argp
)
154 ret
= libc_ioctl(fd
, request
, argp
);
155 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
161 gem_mmap(int fd
, uint32_t handle
, uint64_t offset
, uint64_t size
)
163 struct drm_i915_gem_mmap mmap
= {
169 if (gem_ioctl(fd
, DRM_IOCTL_I915_GEM_MMAP
, &mmap
) == -1)
172 return (void *)(uintptr_t) mmap
.addr_ptr
;
176 gem_get_param(int fd
, uint32_t param
)
179 drm_i915_getparam_t gp
= {
184 if (gem_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
) == -1)
191 dump_execbuffer2(int fd
, struct drm_i915_gem_execbuffer2
*execbuffer2
)
193 struct drm_i915_gem_exec_object2
*exec_objects
=
194 (struct drm_i915_gem_exec_object2
*) (uintptr_t) execbuffer2
->buffers_ptr
;
195 uint32_t ring_flag
= execbuffer2
->flags
& I915_EXEC_RING_MASK
;
197 struct drm_i915_gem_exec_object2
*obj
;
198 struct bo
*bo
, *batch_bo
;
202 /* We can't do this at open time as we're not yet authenticated. */
204 device
= gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
205 fail_if(device
== 0 || devinfo
.gen
== 0, "failed to identify chipset\n");
207 if (devinfo
.gen
== 0) {
208 fail_if(!gen_get_device_info(device
, &devinfo
),
209 "failed to identify chipset=0x%x\n", device
);
211 aub_file_init(&aub_file
, output_file
, device
);
213 aub_file
.verbose_log_file
= stdout
;
214 aub_write_header(&aub_file
, program_invocation_short_name
);
217 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
218 output_filename
, device
, devinfo
.gen
);
221 if (aub_use_execlists(&aub_file
))
224 offset
= aub_gtt_size(&aub_file
);
227 printf("Dumping execbuffer2:\n");
229 for (uint32_t i
= 0; i
< execbuffer2
->buffer_count
; i
++) {
230 obj
= &exec_objects
[i
];
231 bo
= get_bo(fd
, obj
->handle
);
233 /* If bo->size == 0, this means they passed us an invalid
234 * buffer. The kernel will reject it and so should we.
238 printf("BO #%d is invalid!\n", obj
->handle
);
242 if (obj
->flags
& EXEC_OBJECT_PINNED
) {
243 bo
->offset
= obj
->offset
;
245 printf("BO #%d (%dB) pinned @ 0x%lx\n",
246 obj
->handle
, bo
->size
, bo
->offset
);
248 if (obj
->alignment
!= 0)
249 offset
= align_u32(offset
, obj
->alignment
);
252 printf("BO #%d (%dB) @ 0x%lx\n", obj
->handle
,
253 bo
->size
, bo
->offset
);
254 offset
= align_u32(offset
+ bo
->size
+ 4095, 4096);
257 if (bo
->map
== NULL
&& bo
->size
> 0)
258 bo
->map
= gem_mmap(fd
, obj
->handle
, 0, bo
->size
);
259 fail_if(bo
->map
== MAP_FAILED
, "bo mmap failed\n");
261 if (aub_use_execlists(&aub_file
))
262 aub_map_ppgtt(&aub_file
, bo
->offset
, bo
->size
);
265 batch_index
= (execbuffer2
->flags
& I915_EXEC_BATCH_FIRST
) ? 0 :
266 execbuffer2
->buffer_count
- 1;
267 batch_bo
= get_bo(fd
, exec_objects
[batch_index
].handle
);
268 for (uint32_t i
= 0; i
< execbuffer2
->buffer_count
; i
++) {
269 obj
= &exec_objects
[i
];
270 bo
= get_bo(fd
, obj
->handle
);
272 if (obj
->relocation_count
> 0)
273 data
= relocate_bo(fd
, bo
, execbuffer2
, obj
);
277 if (bo
== batch_bo
) {
278 aub_write_trace_block(&aub_file
, AUB_TRACE_TYPE_BATCH
,
279 GET_PTR(data
), bo
->size
, bo
->offset
);
281 aub_write_trace_block(&aub_file
, AUB_TRACE_TYPE_NOTYPE
,
282 GET_PTR(data
), bo
->size
, bo
->offset
);
289 aub_write_exec(&aub_file
,
290 batch_bo
->offset
+ execbuffer2
->batch_start_offset
,
293 if (device_override
&&
294 (execbuffer2
->flags
& I915_EXEC_FENCE_ARRAY
) != 0) {
295 struct drm_i915_gem_exec_fence
*fences
=
296 (void*)(uintptr_t)execbuffer2
->cliprects_ptr
;
297 for (uint32_t i
= 0; i
< execbuffer2
->num_cliprects
; i
++) {
298 if ((fences
[i
].flags
& I915_EXEC_FENCE_SIGNAL
) != 0) {
299 struct drm_syncobj_array arg
= {
300 .handles
= (uintptr_t)&fences
[i
].handle
,
304 libc_ioctl(fd
, DRM_IOCTL_SYNCOBJ_SIGNAL
, &arg
);
311 add_new_bo(unsigned fd
, int handle
, uint64_t size
, void *map
)
313 struct bo
*bo
= &bos
[handle
+ fd
* MAX_BO_COUNT
];
315 fail_if(handle
>= MAX_BO_COUNT
, "bo handle out of range\n");
316 fail_if(fd
>= MAX_FD_COUNT
, "bo fd out of range\n");
317 fail_if(size
== 0, "bo size is invalid\n");
324 remove_bo(int fd
, int handle
)
326 struct bo
*bo
= get_bo(fd
, handle
);
328 if (bo
->map
&& !IS_USERPTR(bo
->map
))
329 munmap(bo
->map
, bo
->size
);
334 __attribute__ ((visibility ("default"))) int
340 return libc_close(fd
);
346 static bool initialized
= false;
355 config
= fopen(getenv("INTEL_DUMP_GPU_CONFIG"), "r");
356 while (fscanf(config
, "%m[^=]=%m[^\n]\n", &key
, &value
) != EOF
) {
357 if (!strcmp(key
, "verbose")) {
358 if (!strcmp(value
, "1")) {
360 } else if (!strcmp(value
, "2")) {
363 } else if (!strcmp(key
, "device")) {
364 fail_if(device
!= 0, "Device/Platform override specified multiple times.");
365 fail_if(sscanf(value
, "%i", &device
) != 1,
366 "failed to parse device id '%s'",
368 device_override
= true;
369 } else if (!strcmp(key
, "platform")) {
370 fail_if(device
!= 0, "Device/Platform override specified multiple times.");
371 device
= gen_device_name_to_pci_device_id(value
);
372 fail_if(device
== -1, "Unknown platform '%s'", value
);
373 device_override
= true;
374 } else if (!strcmp(key
, "file")) {
375 output_filename
= strdup(value
);
376 output_file
= fopen(output_filename
, "w+");
377 fail_if(output_file
== NULL
,
378 "failed to open file '%s'\n",
381 fprintf(stderr
, "unknown option '%s'\n", key
);
389 bos
= calloc(MAX_FD_COUNT
* MAX_BO_COUNT
, sizeof(bos
[0]));
390 fail_if(bos
== NULL
, "out of memory\n");
393 __attribute__ ((visibility ("default"))) int
394 ioctl(int fd
, unsigned long request
, ...)
401 va_start(args
, request
);
402 argp
= va_arg(args
, void *);
405 if (_IOC_TYPE(request
) == DRM_IOCTL_BASE
&&
406 drm_fd
!= fd
&& fstat(fd
, &buf
) == 0 &&
407 (buf
.st_mode
& S_IFMT
) == S_IFCHR
&& major(buf
.st_rdev
) == DRM_MAJOR
) {
410 printf("[intercept drm ioctl on fd %d]\n", fd
);
417 case DRM_IOCTL_I915_GETPARAM
: {
418 struct drm_i915_getparam
*getparam
= argp
;
420 if (device_override
&& getparam
->param
== I915_PARAM_CHIPSET_ID
) {
421 *getparam
->value
= device
;
425 ret
= libc_ioctl(fd
, request
, argp
);
427 /* If the application looks up chipset_id
428 * (they typically do), we'll piggy-back on
429 * their ioctl and store the id for later
431 if (ret
== 0 && getparam
->param
== I915_PARAM_CHIPSET_ID
)
432 device
= *getparam
->value
;
437 case DRM_IOCTL_I915_GEM_EXECBUFFER
: {
441 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
444 return libc_ioctl(fd
, request
, argp
);
447 case DRM_IOCTL_I915_GEM_EXECBUFFER2
:
448 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
: {
449 dump_execbuffer2(fd
, argp
);
453 return libc_ioctl(fd
, request
, argp
);
456 case DRM_IOCTL_I915_GEM_CREATE
: {
457 struct drm_i915_gem_create
*create
= argp
;
459 ret
= libc_ioctl(fd
, request
, argp
);
461 add_new_bo(fd
, create
->handle
, create
->size
, NULL
);
466 case DRM_IOCTL_I915_GEM_USERPTR
: {
467 struct drm_i915_gem_userptr
*userptr
= argp
;
469 ret
= libc_ioctl(fd
, request
, argp
);
471 add_new_bo(fd
, userptr
->handle
, userptr
->user_size
,
472 (void *) (uintptr_t) (userptr
->user_ptr
| USERPTR_FLAG
));
477 case DRM_IOCTL_GEM_CLOSE
: {
478 struct drm_gem_close
*close
= argp
;
480 remove_bo(fd
, close
->handle
);
482 return libc_ioctl(fd
, request
, argp
);
485 case DRM_IOCTL_GEM_OPEN
: {
486 struct drm_gem_open
*open
= argp
;
488 ret
= libc_ioctl(fd
, request
, argp
);
490 add_new_bo(fd
, open
->handle
, open
->size
, NULL
);
495 case DRM_IOCTL_PRIME_FD_TO_HANDLE
: {
496 struct drm_prime_handle
*prime
= argp
;
498 ret
= libc_ioctl(fd
, request
, argp
);
502 size
= lseek(prime
->fd
, 0, SEEK_END
);
503 fail_if(size
== -1, "failed to get prime bo size\n");
504 add_new_bo(fd
, prime
->handle
, size
, NULL
);
512 return libc_ioctl(fd
, request
, argp
);
515 return libc_ioctl(fd
, request
, argp
);
522 libc_close
= dlsym(RTLD_NEXT
, "close");
523 libc_ioctl
= dlsym(RTLD_NEXT
, "ioctl");
524 fail_if(libc_close
== NULL
|| libc_ioctl
== NULL
,
525 "failed to get libc ioctl or close\n");
529 close_init_helper(int fd
)
532 return libc_close(fd
);
536 ioctl_init_helper(int fd
, unsigned long request
, ...)
541 va_start(args
, request
);
542 argp
= va_arg(args
, void *);
546 return libc_ioctl(fd
, request
, argp
);
549 static void __attribute__ ((destructor
))
552 free(output_filename
);
553 aub_file_finish(&aub_file
);