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