intel/dump_gpu: add platform option
[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 <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_BO_COUNT 64 * 1024
62
63 struct bo {
64 uint32_t size;
65 uint64_t offset;
66 void *map;
67 };
68
69 static struct bo *bos;
70
71 #define DRM_MAJOR 226
72
73 /* We set bit 0 in the map pointer for userptr BOs so we know not to
74 * munmap them on DRM_IOCTL_GEM_CLOSE.
75 */
76 #define USERPTR_FLAG 1
77 #define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
78 #define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )
79
80 static void __attribute__ ((format(__printf__, 2, 3)))
81 fail_if(int cond, const char *format, ...)
82 {
83 va_list args;
84
85 if (!cond)
86 return;
87
88 va_start(args, format);
89 fprintf(stderr, "intel_dump_gpu: ");
90 vfprintf(stderr, format, args);
91 va_end(args);
92
93 raise(SIGTRAP);
94 }
95
96 static struct bo *
97 get_bo(uint32_t handle)
98 {
99 struct bo *bo;
100
101 fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
102 bo = &bos[handle];
103
104 return bo;
105 }
106
107 static inline uint32_t
108 align_u32(uint32_t v, uint32_t a)
109 {
110 return (v + a - 1) & ~(a - 1);
111 }
112
113 static struct gen_device_info devinfo = {0};
114 static uint32_t device = 0;
115 static struct aub_file aub_file;
116
117 static void *
118 relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
119 const struct drm_i915_gem_exec_object2 *obj)
120 {
121 const struct drm_i915_gem_exec_object2 *exec_objects =
122 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
123 const struct drm_i915_gem_relocation_entry *relocs =
124 (const struct drm_i915_gem_relocation_entry *) (uintptr_t) obj->relocs_ptr;
125 void *relocated;
126 int handle;
127
128 relocated = malloc(bo->size);
129 fail_if(relocated == NULL, "out of memory\n");
130 memcpy(relocated, GET_PTR(bo->map), bo->size);
131 for (size_t i = 0; i < obj->relocation_count; i++) {
132 fail_if(relocs[i].offset >= bo->size, "reloc outside bo\n");
133
134 if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
135 handle = exec_objects[relocs[i].target_handle].handle;
136 else
137 handle = relocs[i].target_handle;
138
139 aub_write_reloc(&devinfo, ((char *)relocated) + relocs[i].offset,
140 get_bo(handle)->offset + relocs[i].delta);
141 }
142
143 return relocated;
144 }
145
146 static int
147 gem_ioctl(int fd, unsigned long request, void *argp)
148 {
149 int ret;
150
151 do {
152 ret = libc_ioctl(fd, request, argp);
153 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
154
155 return ret;
156 }
157
158 static void *
159 gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size)
160 {
161 struct drm_i915_gem_mmap mmap = {
162 .handle = handle,
163 .offset = offset,
164 .size = size
165 };
166
167 if (gem_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap) == -1)
168 return MAP_FAILED;
169
170 return (void *)(uintptr_t) mmap.addr_ptr;
171 }
172
173 static int
174 gem_get_param(int fd, uint32_t param)
175 {
176 int value;
177 drm_i915_getparam_t gp = {
178 .param = param,
179 .value = &value
180 };
181
182 if (gem_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1)
183 return 0;
184
185 return value;
186 }
187
188 static void
189 dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
190 {
191 struct drm_i915_gem_exec_object2 *exec_objects =
192 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
193 uint32_t ring_flag = execbuffer2->flags & I915_EXEC_RING_MASK;
194 uint32_t offset;
195 struct drm_i915_gem_exec_object2 *obj;
196 struct bo *bo, *batch_bo;
197 int batch_index;
198 void *data;
199
200 /* We can't do this at open time as we're not yet authenticated. */
201 if (device == 0) {
202 device = gem_get_param(fd, I915_PARAM_CHIPSET_ID);
203 fail_if(device == 0 || devinfo.gen == 0, "failed to identify chipset\n");
204 }
205 if (devinfo.gen == 0) {
206 fail_if(!gen_get_device_info(device, &devinfo),
207 "failed to identify chipset=0x%x\n", device);
208
209 aub_file_init(&aub_file, output_file, device);
210 if (verbose == 2)
211 aub_file.verbose_log_file = stdout;
212 aub_write_header(&aub_file, program_invocation_short_name);
213
214 if (verbose)
215 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
216 output_filename, device, devinfo.gen);
217 }
218
219 if (aub_use_execlists(&aub_file))
220 offset = 0x1000;
221 else
222 offset = aub_gtt_size(&aub_file);
223
224 if (verbose)
225 printf("Dumping execbuffer2:\n");
226
227 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
228 obj = &exec_objects[i];
229 bo = get_bo(obj->handle);
230
231 /* If bo->size == 0, this means they passed us an invalid
232 * buffer. The kernel will reject it and so should we.
233 */
234 if (bo->size == 0) {
235 if (verbose)
236 printf("BO #%d is invalid!\n", obj->handle);
237 return;
238 }
239
240 if (obj->flags & EXEC_OBJECT_PINNED) {
241 bo->offset = obj->offset;
242 if (verbose)
243 printf("BO #%d (%dB) pinned @ 0x%lx\n",
244 obj->handle, bo->size, bo->offset);
245 } else {
246 if (obj->alignment != 0)
247 offset = align_u32(offset, obj->alignment);
248 bo->offset = offset;
249 if (verbose)
250 printf("BO #%d (%dB) @ 0x%lx\n", obj->handle,
251 bo->size, bo->offset);
252 offset = align_u32(offset + bo->size + 4095, 4096);
253 }
254
255 if (bo->map == NULL && bo->size > 0)
256 bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
257 fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
258
259 if (aub_use_execlists(&aub_file))
260 aub_map_ppgtt(&aub_file, bo->offset, bo->size);
261 }
262
263 batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
264 execbuffer2->buffer_count - 1;
265 batch_bo = get_bo(exec_objects[batch_index].handle);
266 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
267 obj = &exec_objects[i];
268 bo = get_bo(obj->handle);
269
270 if (obj->relocation_count > 0)
271 data = relocate_bo(bo, execbuffer2, obj);
272 else
273 data = bo->map;
274
275 if (bo == batch_bo) {
276 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
277 GET_PTR(data), bo->size, bo->offset);
278 } else {
279 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
280 GET_PTR(data), bo->size, bo->offset);
281 }
282
283 if (data != bo->map)
284 free(data);
285 }
286
287 aub_write_exec(&aub_file,
288 batch_bo->offset + execbuffer2->batch_start_offset,
289 offset, ring_flag);
290
291 if (device_override &&
292 (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
293 struct drm_i915_gem_exec_fence *fences =
294 (void*)(uintptr_t)execbuffer2->cliprects_ptr;
295 for (uint32_t i = 0; i < execbuffer2->num_cliprects; i++) {
296 if ((fences[i].flags & I915_EXEC_FENCE_SIGNAL) != 0) {
297 struct drm_syncobj_array arg = {
298 .handles = (uintptr_t)&fences[i].handle,
299 .count_handles = 1,
300 .pad = 0,
301 };
302 libc_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &arg);
303 }
304 }
305 }
306 }
307
308 static void
309 add_new_bo(int handle, uint64_t size, void *map)
310 {
311 struct bo *bo = &bos[handle];
312
313 fail_if(handle >= MAX_BO_COUNT, "bo handle out of range\n");
314 fail_if(size == 0, "bo size is invalid\n");
315
316 bo->size = size;
317 bo->map = map;
318 }
319
320 static void
321 remove_bo(int handle)
322 {
323 struct bo *bo = get_bo(handle);
324
325 if (bo->map && !IS_USERPTR(bo->map))
326 munmap(bo->map, bo->size);
327 bo->size = 0;
328 bo->map = NULL;
329 }
330
331 __attribute__ ((visibility ("default"))) int
332 close(int fd)
333 {
334 if (fd == drm_fd)
335 drm_fd = -1;
336
337 return libc_close(fd);
338 }
339
340 static void
341 maybe_init(void)
342 {
343 static bool initialized = false;
344 FILE *config;
345 char *key, *value;
346
347 if (initialized)
348 return;
349
350 initialized = true;
351
352 config = fopen(getenv("INTEL_DUMP_GPU_CONFIG"), "r");
353 while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
354 if (!strcmp(key, "verbose")) {
355 if (!strcmp(value, "1")) {
356 verbose = 1;
357 } else if (!strcmp(value, "2")) {
358 verbose = 2;
359 }
360 } else if (!strcmp(key, "device")) {
361 fail_if(device != 0, "Device/Platform override specified multiple times.");
362 fail_if(sscanf(value, "%i", &device) != 1,
363 "failed to parse device id '%s'",
364 value);
365 device_override = true;
366 } else if (!strcmp(key, "platform")) {
367 fail_if(device != 0, "Device/Platform override specified multiple times.");
368 device = gen_device_name_to_pci_device_id(value);
369 fail_if(device == -1, "Unknown platform '%s'", value);
370 device_override = true;
371 } else if (!strcmp(key, "file")) {
372 output_filename = strdup(value);
373 output_file = fopen(output_filename, "w+");
374 fail_if(output_file == NULL,
375 "failed to open file '%s'\n",
376 output_filename);
377 } else {
378 fprintf(stderr, "unknown option '%s'\n", key);
379 }
380
381 free(key);
382 free(value);
383 }
384 fclose(config);
385
386 bos = calloc(MAX_BO_COUNT, sizeof(bos[0]));
387 fail_if(bos == NULL, "out of memory\n");
388 }
389
390 __attribute__ ((visibility ("default"))) int
391 ioctl(int fd, unsigned long request, ...)
392 {
393 va_list args;
394 void *argp;
395 int ret;
396 struct stat buf;
397
398 va_start(args, request);
399 argp = va_arg(args, void *);
400 va_end(args);
401
402 if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
403 drm_fd != fd && fstat(fd, &buf) == 0 &&
404 (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
405 drm_fd = fd;
406 if (verbose)
407 printf("[intercept drm ioctl on fd %d]\n", fd);
408 }
409
410 if (fd == drm_fd) {
411 maybe_init();
412
413 switch (request) {
414 case DRM_IOCTL_I915_GETPARAM: {
415 struct drm_i915_getparam *getparam = argp;
416
417 if (device_override && getparam->param == I915_PARAM_CHIPSET_ID) {
418 *getparam->value = device;
419 return 0;
420 }
421
422 ret = libc_ioctl(fd, request, argp);
423
424 /* If the application looks up chipset_id
425 * (they typically do), we'll piggy-back on
426 * their ioctl and store the id for later
427 * use. */
428 if (ret == 0 && getparam->param == I915_PARAM_CHIPSET_ID)
429 device = *getparam->value;
430
431 return ret;
432 }
433
434 case DRM_IOCTL_I915_GEM_EXECBUFFER: {
435 static bool once;
436 if (!once) {
437 fprintf(stderr,
438 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
439 once = true;
440 }
441 return libc_ioctl(fd, request, argp);
442 }
443
444 case DRM_IOCTL_I915_GEM_EXECBUFFER2:
445 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR: {
446 dump_execbuffer2(fd, argp);
447 if (device_override)
448 return 0;
449
450 return libc_ioctl(fd, request, argp);
451 }
452
453 case DRM_IOCTL_I915_GEM_CREATE: {
454 struct drm_i915_gem_create *create = argp;
455
456 ret = libc_ioctl(fd, request, argp);
457 if (ret == 0)
458 add_new_bo(create->handle, create->size, NULL);
459
460 return ret;
461 }
462
463 case DRM_IOCTL_I915_GEM_USERPTR: {
464 struct drm_i915_gem_userptr *userptr = argp;
465
466 ret = libc_ioctl(fd, request, argp);
467 if (ret == 0)
468 add_new_bo(userptr->handle, userptr->user_size,
469 (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
470 return ret;
471 }
472
473 case DRM_IOCTL_GEM_CLOSE: {
474 struct drm_gem_close *close = argp;
475
476 remove_bo(close->handle);
477
478 return libc_ioctl(fd, request, argp);
479 }
480
481 case DRM_IOCTL_GEM_OPEN: {
482 struct drm_gem_open *open = argp;
483
484 ret = libc_ioctl(fd, request, argp);
485 if (ret == 0)
486 add_new_bo(open->handle, open->size, NULL);
487
488 return ret;
489 }
490
491 case DRM_IOCTL_PRIME_FD_TO_HANDLE: {
492 struct drm_prime_handle *prime = argp;
493
494 ret = libc_ioctl(fd, request, argp);
495 if (ret == 0) {
496 off_t size;
497
498 size = lseek(prime->fd, 0, SEEK_END);
499 fail_if(size == -1, "failed to get prime bo size\n");
500 add_new_bo(prime->handle, size, NULL);
501 }
502
503 return ret;
504 }
505
506 default:
507 return libc_ioctl(fd, request, argp);
508 }
509 } else {
510 return libc_ioctl(fd, request, argp);
511 }
512 }
513
514 static void
515 init(void)
516 {
517 libc_close = dlsym(RTLD_NEXT, "close");
518 libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
519 fail_if(libc_close == NULL || libc_ioctl == NULL,
520 "failed to get libc ioctl or close\n");
521 }
522
523 static int
524 close_init_helper(int fd)
525 {
526 init();
527 return libc_close(fd);
528 }
529
530 static int
531 ioctl_init_helper(int fd, unsigned long request, ...)
532 {
533 va_list args;
534 void *argp;
535
536 va_start(args, request);
537 argp = va_arg(args, void *);
538 va_end(args);
539
540 init();
541 return libc_ioctl(fd, request, argp);
542 }
543
544 static void __attribute__ ((destructor))
545 fini(void)
546 {
547 free(output_filename);
548 aub_file_finish(&aub_file);
549 free(bos);
550 }