aubdump.c 34.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
29
#include <signal.h>
30 31 32
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
33
#include <sys/sysmacros.h>
34 35 36 37 38 39
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <dlfcn.h>
40
#include <i915_drm.h>
41 42 43 44

#include "intel_aub.h"
#include "intel_chipset.h"

45 46 47 48
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#ifndef ALIGN
#define ALIGN(x, y) (((x) + (y)-1) & ~((y)-1))
#endif

#define min(a, b) ({			\
	typeof(a) _a = (a);		\
	typeof(b) _b = (b);		\
	_a < _b ? _a : _b;		\
})

#define HWS_PGA_RCSUNIT		0x02080
#define HWS_PGA_VCSUNIT0	0x12080
#define HWS_PGA_BCSUNIT		0x22080

#define GFX_MODE_RCSUNIT	0x0229c
#define GFX_MODE_VCSUNIT0	0x1229c
#define GFX_MODE_BCSUNIT	0x2229c

#define EXECLIST_SUBMITPORT_RCSUNIT	0x02230
#define EXECLIST_SUBMITPORT_VCSUNIT0	0x12230
#define EXECLIST_SUBMITPORT_BCSUNIT	0x22230

#define EXECLIST_STATUS_RCSUNIT		0x02234
#define EXECLIST_STATUS_VCSUNIT0	0x12234
#define EXECLIST_STATUS_BCSUNIT		0x22234

75 76 77 78 79 80 81 82
#define EXECLIST_SQ_CONTENTS0_RCSUNIT	0x02510
#define EXECLIST_SQ_CONTENTS0_VCSUNIT0	0x12510
#define EXECLIST_SQ_CONTENTS0_BCSUNIT	0x22510

#define EXECLIST_CONTROL_RCSUNIT	0x02550
#define EXECLIST_CONTROL_VCSUNIT0	0x12550
#define EXECLIST_CONTROL_BCSUNIT	0x22550

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#define MEMORY_MAP_SIZE (64 /* MiB */ * 1024 * 1024)

#define PTE_SIZE 4
#define GEN8_PTE_SIZE 8

#define NUM_PT_ENTRIES (ALIGN(MEMORY_MAP_SIZE, 4096) / 4096)
#define PT_SIZE ALIGN(NUM_PT_ENTRIES * GEN8_PTE_SIZE, 4096)

#define RING_SIZE			(1 * 4096)
#define PPHWSP_SIZE			(1 * 4096)
#define GEN10_LR_CONTEXT_RENDER_SIZE	(19 * 4096)
#define GEN8_LR_CONTEXT_OTHER_SIZE	(2 * 4096)

#define STATIC_GGTT_MAP_START 0

#define RENDER_RING_ADDR STATIC_GGTT_MAP_START
#define RENDER_CONTEXT_ADDR (RENDER_RING_ADDR + RING_SIZE)

#define BLITTER_RING_ADDR (RENDER_CONTEXT_ADDR + PPHWSP_SIZE + GEN10_LR_CONTEXT_RENDER_SIZE)
#define BLITTER_CONTEXT_ADDR (BLITTER_RING_ADDR + RING_SIZE)

#define VIDEO_RING_ADDR (BLITTER_CONTEXT_ADDR + PPHWSP_SIZE + GEN8_LR_CONTEXT_OTHER_SIZE)
#define VIDEO_CONTEXT_ADDR (VIDEO_RING_ADDR + RING_SIZE)

#define STATIC_GGTT_MAP_END (VIDEO_CONTEXT_ADDR + PPHWSP_SIZE + GEN8_LR_CONTEXT_OTHER_SIZE)
#define STATIC_GGTT_MAP_SIZE (STATIC_GGTT_MAP_END - STATIC_GGTT_MAP_START)

#define CONTEXT_FLAGS (0x229)	/* Normal Priority | L3-LLC Coherency |
	Legacy Context with no 64 bit VA support | Valid */

#define RENDER_CONTEXT_DESCRIPTOR  ((uint64_t)1 << 32 | RENDER_CONTEXT_ADDR  | CONTEXT_FLAGS)
#define BLITTER_CONTEXT_DESCRIPTOR ((uint64_t)2 << 32 | BLITTER_CONTEXT_ADDR | CONTEXT_FLAGS)
#define VIDEO_CONTEXT_DESCRIPTOR   ((uint64_t)3 << 32 | VIDEO_CONTEXT_ADDR   | CONTEXT_FLAGS)

static const uint32_t render_context_init[GEN10_LR_CONTEXT_RENDER_SIZE /
					  sizeof(uint32_t)] = {
	0 /* MI_NOOP */,
	0x1100101B /* MI_LOAD_REGISTER_IMM */,
	0x2244 /* CONTEXT_CONTROL */,		0x90009 /* Inhibit Synchronous Context Switch | Engine Context Restore Inhibit */,
	0x2034 /* RING_HEAD */,			0,
	0x2030 /* RING_TAIL */,			0,
	0x2038 /* RING_BUFFER_START */,		RENDER_RING_ADDR,
	0x203C /* RING_BUFFER_CONTROL */,	(RING_SIZE - 4096) | 1 /* Buffer Length | Ring Buffer Enable */,
	0x2168 /* BB_HEAD_U */,			0,
	0x2140 /* BB_HEAD_L */,			0,
	0x2110 /* BB_STATE */,			0,
	0x211C /* SECOND_BB_HEAD_U */,		0,
	0x2114 /* SECOND_BB_HEAD_L */,		0,
	0x2118 /* SECOND_BB_STATE */,		0,
	0x21C0 /* BB_PER_CTX_PTR */,		0,
	0x21C4 /* RCS_INDIRECT_CTX */,		0,
	0x21C8 /* RCS_INDIRECT_CTX_OFFSET */,	0,
	/* MI_NOOP */
	0, 0,

	0 /* MI_NOOP */,
	0x11001011 /* MI_LOAD_REGISTER_IMM */,
	0x23A8 /* CTX_TIMESTAMP */,	0,
	0x228C /* PDP3_UDW */,		0,
	0x2288 /* PDP3_LDW */,		0,
	0x2284 /* PDP2_UDW */,		0,
	0x2280 /* PDP2_LDW */,		0,
	0x227C /* PDP1_UDW */,		0,
	0x2278 /* PDP1_LDW */,		0,
	0x2274 /* PDP0_UDW */,		0,
	0x2270 /* PDP0_LDW */,		0,
	/* MI_NOOP */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

	0 /* MI_NOOP */,
	0x11000001 /* MI_LOAD_REGISTER_IMM */,
	0x20C8 /* R_PWR_CLK_STATE */, 0x7FFFFFFF,
	0x05000001 /* MI_BATCH_BUFFER_END */
};

static const uint32_t blitter_context_init[GEN8_LR_CONTEXT_OTHER_SIZE /
					   sizeof(uint32_t)] = {
	0 /* MI_NOOP */,
	0x11001015 /* MI_LOAD_REGISTER_IMM */,
	0x22244 /* CONTEXT_CONTROL */,		0x90009 /* Inhibit Synchronous Context Switch | Engine Context Restore Inhibit */,
	0x22034 /* RING_HEAD */,		0,
	0x22030 /* RING_TAIL */,		0,
	0x22038 /* RING_BUFFER_START */,	BLITTER_RING_ADDR,
	0x2203C /* RING_BUFFER_CONTROL */,	(RING_SIZE - 4096) | 1 /* Buffer Length | Ring Buffer Enable */,
	0x22168 /* BB_HEAD_U */,		0,
	0x22140 /* BB_HEAD_L */,		0,
	0x22110 /* BB_STATE */,			0,
	0x2211C /* SECOND_BB_HEAD_U */,		0,
	0x22114 /* SECOND_BB_HEAD_L */,		0,
	0x22118 /* SECOND_BB_STATE */,		0,
	/* MI_NOOP */
	0, 0, 0, 0, 0, 0, 0, 0,

	0 /* MI_NOOP */,
	0x11001011,
	0x223A8 /* CTX_TIMESTAMP */,	0,
	0x2228C /* PDP3_UDW */,		0,
	0x22288 /* PDP3_LDW */,		0,
	0x22284 /* PDP2_UDW */,		0,
	0x22280 /* PDP2_LDW */,		0,
	0x2227C /* PDP1_UDW */,		0,
	0x22278 /* PDP1_LDW */,		0,
	0x22274 /* PDP0_UDW */,		0,
	0x22270 /* PDP0_LDW */,		0,
	/* MI_NOOP */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

	0x05000001 /* MI_BATCH_BUFFER_END */
};

static const uint32_t video_context_init[GEN8_LR_CONTEXT_OTHER_SIZE /
					 sizeof(uint32_t)] = {
	0 /* MI_NOOP */,
	0x11001015 /* MI_LOAD_REGISTER_IMM */,
	0x1C244 /* CONTEXT_CONTROL */,		0x90009 /* Inhibit Synchronous Context Switch | Engine Context Restore Inhibit */,
	0x1C034 /* RING_HEAD */,		0,
	0x1C030 /* RING_TAIL */,		0,
	0x1C038 /* RING_BUFFER_START */,	VIDEO_RING_ADDR,
	0x1C03C /* RING_BUFFER_CONTROL */,	(RING_SIZE - 4096) | 1 /* Buffer Length | Ring Buffer Enable */,
	0x1C168 /* BB_HEAD_U */,		0,
	0x1C140 /* BB_HEAD_L */,		0,
	0x1C110 /* BB_STATE */,			0,
	0x1C11C /* SECOND_BB_HEAD_U */,		0,
	0x1C114 /* SECOND_BB_HEAD_L */,		0,
	0x1C118 /* SECOND_BB_STATE */,		0,
	/* MI_NOOP */
	0, 0, 0, 0, 0, 0, 0, 0,

	0 /* MI_NOOP */,
	0x11001011,
	0x1C3A8 /* CTX_TIMESTAMP */,	0,
	0x1C28C /* PDP3_UDW */,		0,
	0x1C288 /* PDP3_LDW */,		0,
	0x1C284 /* PDP2_UDW */,		0,
	0x1C280 /* PDP2_LDW */,		0,
	0x1C27C /* PDP1_UDW */,		0,
	0x1C278 /* PDP1_LDW */,		0,
	0x1C274 /* PDP0_UDW */,		0,
	0x1C270 /* PDP0_LDW */,		0,
	/* MI_NOOP */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

	0x05000001 /* MI_BATCH_BUFFER_END */
};

228 229 230 231 232
static int close_init_helper(int fd);
static int ioctl_init_helper(int fd, unsigned long request, ...);

static int (*libc_close)(int fd) = close_init_helper;
static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
233 234

static int drm_fd = -1;
235 236
static char *filename = NULL;
static FILE *files[2] = { NULL, NULL };
237 238
static int gen = 0;
static int verbose = 0;
239 240
static bool device_override;
static uint32_t device;
241
static int addr_bits = 0;
242 243 244 245 246

#define MAX_BO_COUNT 64 * 1024

struct bo {
	uint32_t size;
247
	uint64_t offset;
248 249 250 251 252 253 254
	void *map;
};

static struct bo *bos;

#define DRM_MAJOR 226

255
#ifndef DRM_I915_GEM_USERPTR
256

257 258
#define DRM_I915_GEM_USERPTR		0x33
#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR, struct drm_i915_gem_userptr)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

struct drm_i915_gem_userptr {
	__u64 user_ptr;
	__u64 user_size;
	__u32 flags;
#define I915_USERPTR_READ_ONLY 0x1
#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
	/**
	 * Returned handle for the object.
	 *
	 * Object handles are nonzero.
	 */
	__u32 handle;
};

274 275
#endif

276 277 278 279 280 281 282
/* We set bit 0 in the map pointer for userptr BOs so we know not to
 * munmap them on DRM_IOCTL_GEM_CLOSE.
 */
#define USERPTR_FLAG 1
#define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
#define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )

283 284 285 286
#ifndef I915_EXEC_BATCH_FIRST
#define I915_EXEC_BATCH_FIRST (1 << 18)
#endif

287 288 289 290 291 292 293 294 295 296 297 298
static void __attribute__ ((format(__printf__, 2, 3)))
fail_if(int cond, const char *format, ...)
{
	va_list args;

	if (!cond)
		return;

	va_start(args, format);
	vfprintf(stderr, format, args);
	va_end(args);

299
	raise(SIGTRAP);
300 301
}

302 303 304 305 306 307 308 309 310 311 312
static struct bo *
get_bo(uint32_t handle)
{
	struct bo *bo;

	fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
	bo = &bos[handle];

	return bo;
}

313 314 315 316 317 318
static inline uint32_t
align_u32(uint32_t v, uint32_t a)
{
	return (v + a - 1) & ~(a - 1);
}

319 320 321 322 323 324
static inline uint64_t
align_u64(uint64_t v, uint64_t a)
{
	return (v + a - 1) & ~(a - 1);
}

325 326 327
static void
dword_out(uint32_t data)
{
328 329 330 331 332 333 334
	for (int i = 0; i < ARRAY_SIZE (files); i++) {
		if (files[i] == NULL)
			continue;

		fail_if(fwrite(&data, 1, 4, files[i]) == 0,
			"Writing to output failed\n");
	}
335 336 337 338 339
}

static void
data_out(const void *data, size_t size)
{
340 341 342 343 344 345 346 347 348 349
	if (size == 0)
		return;

	for (int i = 0; i < ARRAY_SIZE (files); i++) {
		if (files[i] == NULL)
			continue;

		fail_if(fwrite(data, 1, size, files[i]) == 0,
			"Writing to output failed\n");
	}
350 351
}

352
static uint32_t
353
gtt_size(void)
354
{
355
	return NUM_PT_ENTRIES * (addr_bits > 32 ? GEN8_PTE_SIZE : PTE_SIZE);
356 357
}

358 359 360
static void
mem_trace_memory_write_header_out(uint64_t addr, uint32_t len,
				  uint32_t addr_space)
361
{
362 363 364 365 366 367 368
	uint32_t dwords = ALIGN(len, sizeof(uint32_t)) / sizeof(uint32_t);

	dword_out(CMD_MEM_TRACE_MEMORY_WRITE | (5 + dwords - 1));
	dword_out(addr & 0xFFFFFFFF);	/* addr lo */
	dword_out(addr >> 32);	/* addr hi */
	dword_out(addr_space);	/* gtt */
	dword_out(len);
369 370
}

371
static void
372 373 374 375 376 377 378 379 380 381 382 383 384
register_write_out(uint32_t addr, uint32_t value)
{
	uint32_t dwords = 1;

	dword_out(CMD_MEM_TRACE_REGISTER_WRITE | (5 + dwords - 1));
	dword_out(addr);
	dword_out(AUB_MEM_TRACE_REGISTER_SIZE_DWORD |
		  AUB_MEM_TRACE_REGISTER_SPACE_MMIO);
	dword_out(0xFFFFFFFF);	/* mask lo */
	dword_out(0x00000000);	/* mask hi */
	dword_out(value);
}

385
static void
386
gen8_emit_ggtt_pte_for_range(uint64_t start, uint64_t end)
387 388 389 390 391 392 393 394 395 396
{
	uint64_t entry_addr;
	uint64_t page_num;
	uint64_t end_aligned = align_u64(end, 4096);

	if (start >= end || end > (1ull << 32))
		return;

	entry_addr = start & ~(4096 - 1);
	do {
397 398
		uint64_t last_page_entry, num_entries;

399
		page_num = entry_addr >> 21;
400 401
		last_page_entry = min((page_num + 1) << 21, end_aligned);
		num_entries = (last_page_entry - entry_addr) >> 12;
402 403 404 405 406 407 408 409 410 411 412 413
		mem_trace_memory_write_header_out(
			entry_addr >> 9, num_entries * GEN8_PTE_SIZE,
			AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_GGTT_ENTRY);
		while (num_entries-- > 0) {
			dword_out((entry_addr & ~(4096 - 1)) |
			          3 /* read/write | present */);
			dword_out(entry_addr >> 32);
			entry_addr += 4096;
		}
	} while (entry_addr < end);
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/**
 * Sets bits `start` through `end` - 1 in the bitmap array.
 */
static void
set_bitmap_range(uint32_t *bitmap, uint32_t start, uint32_t end)
{
	uint32_t pos = start;
	while (pos < end) {
		const uint32_t bit = 1 << (pos & 0x1f);
		if (bit == 1 && (end - pos) > 32) {
			bitmap[pos >> 5] = 0xffffffff;
			pos += 32;
		} else {
			bitmap[pos >> 5] |= bit;
			pos++;
		}
	}
}

/**
 * Finds the next `set` (or clear) bit in the bitmap array.
 *
 * The search starts at `*start` and only checks until `end` - 1.
 *
 * If found, returns true, and the found bit index in `*start`.
 */
static bool
find_bitmap_bit(uint32_t *bitmap, bool set, uint32_t *start, uint32_t end)
{
	uint32_t pos = *start;
	const uint32_t neg_dw = set ? 0 : -1;
	while (pos < end) {
		const uint32_t dw = bitmap[pos >> 5];
		const uint32_t bit = 1 << (pos & 0x1f);
		if (!!(dw & bit) == set) {
			*start = pos;
			return true;
		} else if (bit == 1 && dw == neg_dw)
			pos += 32;
		else
			pos++;
	}
	return false;
}

/**
 * Finds a range of clear bits within the bitmap array.
 *
 * The search starts at `*start` and only checks until `*end` - 1.
 *
 * If found, returns true, and `*start` and `*end` are set for the
 * range of clear bits.
 */
static bool
find_bitmap_clear_bit_range(uint32_t *bitmap, uint32_t *start, uint32_t *end)
{
	if (find_bitmap_bit(bitmap, false, start, *end)) {
		uint32_t found_end = *start;
		if (find_bitmap_bit(bitmap, true, &found_end, *end))
			*end = found_end;
		return true;
	}
	return false;
}

static void
gen8_map_ggtt_range(uint64_t start, uint64_t end)
{
	uint32_t pos1, pos2, end_pos;
	static uint32_t *bitmap = NULL;
	if (bitmap == NULL) {
		/* 4GiB (32-bits) of 4KiB pages (12-bits) in dwords (5-bits) */
		bitmap = calloc(1 << (32 - 12 - 5), sizeof(*bitmap));
		if (bitmap == NULL)
			return;
	}

	pos1 = start >> 12;
	end_pos = (end + 4096 - 1) >> 12;
	while (pos1 < end_pos) {
		pos2 = end_pos;
		if (!find_bitmap_clear_bit_range(bitmap, &pos1, &pos2))
			break;

		if (verbose)
Ville Syrjälä's avatar
Ville Syrjälä committed
499
			printf("MAPPING 0x%08"PRIx64"-0x%08"PRIx64"\n",
500 501 502 503 504 505 506 507
			       (uint64_t)pos1 << 12, (uint64_t)pos2 << 12);
		gen8_emit_ggtt_pte_for_range((uint64_t)pos1 << 12,
		                             (uint64_t)pos2 << 12);
		set_bitmap_range(bitmap, (uint64_t)pos1, (uint64_t)pos2);
		pos1 = pos2;
	}
}

508 509 510 511 512 513
static void
gen8_map_base_size(uint64_t base, uint64_t size)
{
	gen8_map_ggtt_range(base, base + size);
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
static void
gen10_write_header(void)
{
	char app_name[8 * 4];
	int app_name_len, dwords;

	app_name_len =
	    snprintf(app_name, sizeof(app_name), "PCI-ID=0x%X %s", device,
		     program_invocation_short_name);
	app_name_len = ALIGN(app_name_len, sizeof(uint32_t));

	dwords = 5 + app_name_len / sizeof(uint32_t);
	dword_out(CMD_MEM_TRACE_VERSION | (dwords - 1));
	dword_out(AUB_MEM_TRACE_VERSION_FILE_VERSION);
	dword_out(AUB_MEM_TRACE_VERSION_DEVICE_CNL |
		  AUB_MEM_TRACE_VERSION_METHOD_PHY);
	dword_out(0);		/* version */
	dword_out(0);		/* version */
	data_out(app_name, app_name_len);

	/* RENDER_RING */
535
	gen8_map_base_size(RENDER_RING_ADDR, RING_SIZE);
536 537 538 539 540 541
	mem_trace_memory_write_header_out(RENDER_RING_ADDR, RING_SIZE,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < RING_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* RENDER_PPHWSP */
542 543
	gen8_map_base_size(RENDER_CONTEXT_ADDR,
	                   PPHWSP_SIZE + sizeof(render_context_init));
544 545 546 547 548 549 550 551 552 553 554
	mem_trace_memory_write_header_out(RENDER_CONTEXT_ADDR,
					  PPHWSP_SIZE +
					  sizeof(render_context_init),
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < PPHWSP_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* RENDER_CONTEXT */
	data_out(render_context_init, sizeof(render_context_init));

	/* BLITTER_RING */
555
	gen8_map_base_size(BLITTER_RING_ADDR, RING_SIZE);
556 557 558 559 560 561
	mem_trace_memory_write_header_out(BLITTER_RING_ADDR, RING_SIZE,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < RING_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* BLITTER_PPHWSP */
562 563
	gen8_map_base_size(BLITTER_CONTEXT_ADDR,
	                   PPHWSP_SIZE + sizeof(blitter_context_init));
564 565 566 567 568 569 570 571 572 573 574
	mem_trace_memory_write_header_out(BLITTER_CONTEXT_ADDR,
					  PPHWSP_SIZE +
					  sizeof(blitter_context_init),
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < PPHWSP_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* BLITTER_CONTEXT */
	data_out(blitter_context_init, sizeof(blitter_context_init));

	/* VIDEO_RING */
575
	gen8_map_base_size(VIDEO_RING_ADDR, RING_SIZE);
576 577 578 579 580 581
	mem_trace_memory_write_header_out(VIDEO_RING_ADDR, RING_SIZE,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < RING_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* VIDEO_PPHWSP */
582 583
	gen8_map_base_size(VIDEO_CONTEXT_ADDR,
	                   PPHWSP_SIZE + sizeof(video_context_init));
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	mem_trace_memory_write_header_out(VIDEO_CONTEXT_ADDR,
					  PPHWSP_SIZE +
					  sizeof(video_context_init),
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	for (uint32_t i = 0; i < PPHWSP_SIZE; i += sizeof(uint32_t))
		dword_out(0);

	/* VIDEO_CONTEXT */
	data_out(video_context_init, sizeof(video_context_init));

	register_write_out(HWS_PGA_RCSUNIT, RENDER_CONTEXT_ADDR);
	register_write_out(HWS_PGA_VCSUNIT0, VIDEO_CONTEXT_ADDR);
	register_write_out(HWS_PGA_BCSUNIT, BLITTER_CONTEXT_ADDR);

	register_write_out(GFX_MODE_RCSUNIT, 0x80008000 /* execlist enable */);
	register_write_out(GFX_MODE_VCSUNIT0, 0x80008000 /* execlist enable */);
	register_write_out(GFX_MODE_BCSUNIT, 0x80008000 /* execlist enable */);
}

static void write_header(void)
604
{
605
	char app_name[8 * 4];
606 607
	char comment[16];
	int comment_len, comment_dwords, dwords;
608 609
	uint32_t entry = 0x200003;

610 611 612
	comment_len = snprintf(comment, sizeof(comment), "PCI-ID=0x%x", device);
	comment_dwords = ((comment_len + 3) / 4);

613
	/* Start with a (required) version packet. */
614 615
	dwords = 13 + comment_dwords;
	dword_out(CMD_AUB_HEADER | (dwords - 2));
616 617
	dword_out((4 << AUB_HEADER_MAJOR_SHIFT) |
		  (0 << AUB_HEADER_MINOR_SHIFT));
618 619 620 621 622

	/* Next comes a 32-byte application name. */
	strncpy(app_name, program_invocation_short_name, sizeof(app_name));
	app_name[sizeof(app_name) - 1] = 0;
	data_out(app_name, sizeof(app_name));
623 624 625

	dword_out(0); /* timestamp */
	dword_out(0); /* timestamp */
626 627
	dword_out(comment_len);
	data_out(comment, comment_dwords * 4);
628

629
	/* Set up the GTT. The max we can handle is 64M */
630
	dword_out(CMD_AUB_TRACE_HEADER_BLOCK | ((addr_bits > 32 ? 6 : 5) - 2));
631 632 633 634
	dword_out(AUB_TRACE_MEMTYPE_GTT_ENTRY |
		  AUB_TRACE_TYPE_NOTYPE | AUB_TRACE_OP_DATA_WRITE);
	dword_out(0); /* subtype */
	dword_out(0); /* offset */
635
	dword_out(gtt_size()); /* size */
636
	if (addr_bits > 32)
637
		dword_out(0);
638
	for (uint32_t i = 0; i < NUM_PT_ENTRIES; i++) {
639
		dword_out(entry + 0x1000 * i);
640
		if (addr_bits > 32)
641 642
			dword_out(0);
	}
643 644 645 646 647 648 649 650
}

/**
 * Break up large objects into multiple writes.  Otherwise a 128kb VBO
 * would overflow the 16 bits of size field in the packet header and
 * everything goes badly after that.
 */
static void
651
aub_write_trace_block(uint32_t type, void *virtual, uint32_t size, uint64_t gtt_offset)
652 653 654 655 656 657 658 659 660 661 662
{
	uint32_t block_size;
	uint32_t subtype = 0;
	static const char null_block[8 * 4096];

	for (uint32_t offset = 0; offset < size; offset += block_size) {
		block_size = size - offset;

		if (block_size > 8 * 4096)
			block_size = 8 * 4096;

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
		if (gen >= 10) {
			mem_trace_memory_write_header_out(gtt_offset + offset,
							  block_size,
							  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
		} else {
			dword_out(CMD_AUB_TRACE_HEADER_BLOCK |
				  ((addr_bits > 32 ? 6 : 5) - 2));
			dword_out(AUB_TRACE_MEMTYPE_GTT |
				  type | AUB_TRACE_OP_DATA_WRITE);
			dword_out(subtype);
			dword_out(gtt_offset + offset);
			dword_out(align_u32(block_size, 4));
			if (addr_bits > 32)
				dword_out((gtt_offset + offset) >> 32);
		}
678 679

		if (virtual)
680
			data_out(((char *) GET_PTR(virtual)) + offset, block_size);
681 682 683 684 685 686 687 688
		else
			data_out(null_block, block_size);

		/* Pad to a multiple of 4 bytes. */
		data_out(null_block, -block_size & 3);
	}
}

689 690 691
static void
write_reloc(void *p, uint64_t v)
{
692
	if (addr_bits > 32) {
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		/* From the Broadwell PRM Vol. 2a,
		 * MI_LOAD_REGISTER_MEM::MemoryAddress:
		 *
		 *	"This field specifies the address of the memory
		 *	location where the register value specified in the
		 *	DWord above will read from.  The address specifies
		 *	the DWord location of the data. Range =
		 *	GraphicsVirtualAddress[63:2] for a DWord register
		 *	GraphicsAddress [63:48] are ignored by the HW and
		 *	assumed to be in correct canonical form [63:48] ==
		 *	[47]."
		 *
		 * In practice, this will always mean the top bits are zero
		 * because of the GTT size limitation of the aubdump tool.
		 */
		const int shift = 63 - 47;
		*(uint64_t *)p = (((int64_t)v) << shift) >> shift;
	} else {
		*(uint32_t *)p = v;
	}
}

715 716 717 718 719 720
static void
aub_dump_execlist(uint64_t batch_offset, int ring_flag)
{
	uint32_t ring_addr;
	uint64_t descriptor;
	uint32_t elsp_reg;
721
	uint32_t elsq_reg;
722
	uint32_t status_reg;
723
	uint32_t control_reg;
724 725 726 727 728 729 730

	switch (ring_flag) {
	case I915_EXEC_DEFAULT:
	case I915_EXEC_RENDER:
		ring_addr = RENDER_RING_ADDR;
		descriptor = RENDER_CONTEXT_DESCRIPTOR;
		elsp_reg = EXECLIST_SUBMITPORT_RCSUNIT;
731
		elsq_reg = EXECLIST_SQ_CONTENTS0_RCSUNIT;
732
		status_reg = EXECLIST_STATUS_RCSUNIT;
733
		control_reg = EXECLIST_CONTROL_RCSUNIT;
734 735 736 737 738
		break;
	case I915_EXEC_BSD:
		ring_addr = VIDEO_RING_ADDR;
		descriptor = VIDEO_CONTEXT_DESCRIPTOR;
		elsp_reg = EXECLIST_SUBMITPORT_VCSUNIT0;
739
		elsq_reg = EXECLIST_SQ_CONTENTS0_VCSUNIT0;
740
		status_reg = EXECLIST_STATUS_VCSUNIT0;
741
		control_reg = EXECLIST_CONTROL_VCSUNIT0;
742 743 744 745 746
		break;
	case I915_EXEC_BLT:
		ring_addr = BLITTER_RING_ADDR;
		descriptor = BLITTER_CONTEXT_DESCRIPTOR;
		elsp_reg = EXECLIST_SUBMITPORT_BCSUNIT;
747
		elsq_reg = EXECLIST_SQ_CONTENTS0_BCSUNIT;
748
		status_reg = EXECLIST_STATUS_BCSUNIT;
749
		control_reg = EXECLIST_CONTROL_BCSUNIT;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		break;
	}

	mem_trace_memory_write_header_out(ring_addr, 16,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	dword_out(AUB_MI_BATCH_BUFFER_START | (3 - 2));
	dword_out(batch_offset & 0xFFFFFFFF);
	dword_out(batch_offset >> 32);
	dword_out(0 /* MI_NOOP */);

	mem_trace_memory_write_header_out(ring_addr + 8192 + 20, 4,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	dword_out(0); /* RING_BUFFER_HEAD */
	mem_trace_memory_write_header_out(ring_addr + 8192 + 28, 4,
					  AUB_MEM_TRACE_MEMORY_ADDRESS_SPACE_LOCAL);
	dword_out(16); /* RING_BUFFER_TAIL */

767 768 769 770 771 772 773 774 775 776
	if (gen >= 11) {
		register_write_out(elsq_reg, descriptor & 0xFFFFFFFF);
		register_write_out(elsq_reg + sizeof(uint32_t), descriptor >> 32);
		register_write_out(control_reg, 1);
	} else {
		register_write_out(elsp_reg, 0);
		register_write_out(elsp_reg, 0);
		register_write_out(elsp_reg, descriptor >> 32);
		register_write_out(elsp_reg, descriptor & 0xFFFFFFFF);
	}
777 778 779 780 781

	dword_out(CMD_MEM_TRACE_REGISTER_POLL | (5 + 1 - 1));
	dword_out(status_reg);
	dword_out(AUB_MEM_TRACE_REGISTER_SIZE_DWORD |
		  AUB_MEM_TRACE_REGISTER_SPACE_MMIO);
782 783 784 785 786 787 788 789 790
	if (gen >= 11) {
		dword_out(0x00000001);	/* mask lo */
		dword_out(0x00000000);	/* mask hi */
		dword_out(0x00000001);
	} else {
		dword_out(0x00000010);	/* mask lo */
		dword_out(0x00000000);	/* mask hi */
		dword_out(0x00000000);
	}
791 792
}

793
static void
794
aub_dump_ringbuffer(uint64_t batch_offset, uint64_t offset, int ring_flag)
795 796
{
	uint32_t ringbuffer[4096];
797
	unsigned aub_mi_bbs_len;
798 799 800 801 802 803 804 805 806 807
	int ring = AUB_TRACE_TYPE_RING_PRB0; /* The default ring */
	int ring_count = 0;

	if (ring_flag == I915_EXEC_BSD)
		ring = AUB_TRACE_TYPE_RING_PRB1;
	else if (ring_flag == I915_EXEC_BLT)
		ring = AUB_TRACE_TYPE_RING_PRB2;

	/* Make a ring buffer to execute our batchbuffer. */
	memset(ringbuffer, 0, sizeof(ringbuffer));
808

809
	aub_mi_bbs_len = addr_bits > 32 ? 3 : 2;
810 811 812
	ringbuffer[ring_count] = AUB_MI_BATCH_BUFFER_START | (aub_mi_bbs_len - 2);
	write_reloc(&ringbuffer[ring_count + 1], batch_offset);
	ring_count += aub_mi_bbs_len;
813 814 815 816 817

	/* Write out the ring.  This appears to trigger execution of
	 * the ring in the simulator.
	 */
	dword_out(CMD_AUB_TRACE_HEADER_BLOCK |
818
		  ((addr_bits > 32 ? 6 : 5) - 2));
819 820 821 822
	dword_out(AUB_TRACE_MEMTYPE_GTT | ring | AUB_TRACE_OP_COMMAND_WRITE);
	dword_out(0); /* general/surface subtype */
	dword_out(offset);
	dword_out(ring_count * 4);
823
	if (addr_bits > 32)
824
		dword_out(offset >> 32);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

	data_out(ringbuffer, ring_count * 4);
}

static void *
relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
	    const struct drm_i915_gem_exec_object2 *obj)
{
	const struct drm_i915_gem_exec_object2 *exec_objects =
		(struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
	const struct drm_i915_gem_relocation_entry *relocs =
		(const struct drm_i915_gem_relocation_entry *) (uintptr_t) obj->relocs_ptr;
	void *relocated;
	int handle;

	relocated = malloc(bo->size);
	fail_if(relocated == NULL, "intel_aubdump: out of memory\n");
	memcpy(relocated, GET_PTR(bo->map), bo->size);
	for (size_t i = 0; i < obj->relocation_count; i++) {
		fail_if(relocs[i].offset >= bo->size, "intel_aubdump: reloc outside bo\n");

		if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
			handle = exec_objects[relocs[i].target_handle].handle;
		else
			handle = relocs[i].target_handle;

851 852
		write_reloc(((char *)relocated) + relocs[i].offset,
			    get_bo(handle)->offset + relocs[i].delta);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	}

	return relocated;
}

static int
gem_ioctl(int fd, unsigned long request, void *argp)
{
	int ret;

	do {
		ret = libc_ioctl(fd, request, argp);
	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));

	return ret;
}

static void *
gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size)
{
	struct drm_i915_gem_mmap mmap = {
		.handle = handle,
		.offset = offset,
		.size = size
	};

	if (gem_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap) == -1)
		return MAP_FAILED;

	return (void *)(uintptr_t) mmap.addr_ptr;
}

static int
gem_get_param(int fd, uint32_t param)
{
	int value;
	drm_i915_getparam_t gp = {
		.param = param,
		.value = &value
	};

	if (gem_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1)
		return 0;

	return value;
}

static void
dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
{
	struct drm_i915_gem_exec_object2 *exec_objects =
		(struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
905
	uint32_t ring_flag = execbuffer2->flags & I915_EXEC_RING_MASK;
906
	uint32_t offset;
907 908
	struct drm_i915_gem_exec_object2 *obj;
	struct bo *bo, *batch_bo;
909
	int batch_index;
910 911 912
	void *data;

	/* We can't do this at open time as we're not yet authenticated. */
913 914 915 916
	if (device == 0) {
		device = gem_get_param(fd, I915_PARAM_CHIPSET_ID);
		fail_if(device == 0 || gen == -1, "failed to identify chipset\n");
	}
917
	if (gen == 0) {
918
		gen = intel_gen(device);
919 920 921 922 923 924 925 926 927

		/* If we don't know the device gen, then it probably is a
		 * newer device. Set gen to some arbitrarily high number.
		 */
		if (gen == 0)
			gen = 9999;

		addr_bits = gen >= 8 ? 48 : 32;

928 929 930 931
		if (gen >= 10)
			gen10_write_header();
		else
			write_header();
932 933

		if (verbose)
934
			printf("[intel_aubdump: running, "
935
			       "output file %s, chipset id 0x%04x, gen %d]\n",
936
			       filename, device, gen);
937 938
	}

939 940 941 942 943
	if (gen >= 10)
		offset = STATIC_GGTT_MAP_END;
	else
		offset = gtt_size();

944 945 946
	if (verbose)
		printf("Dumping execbuffer2:\n");

947 948
	for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
		obj = &exec_objects[i];
949
		bo = get_bo(obj->handle);
950

951 952 953
		/* If bo->size == 0, this means they passed us an invalid
		 * buffer.  The kernel will reject it and so should we.
		 */
954 955 956
		if (bo->size == 0) {
			if (verbose)
				printf("BO #%d is invalid!\n", obj->handle);
957
			return;
958 959
		}

960 961
		if (obj->flags & EXEC_OBJECT_PINNED) {
			bo->offset = obj->offset;
962
			if (verbose)
Ville Syrjälä's avatar
Ville Syrjälä committed
963
				printf("BO #%d (%dB) pinned @ 0x%"PRIx64"\n",
964
				       obj->handle, bo->size, bo->offset);
965
		} else {
966 967
			if (obj->alignment != 0)
				offset = align_u32(offset, obj->alignment);
968
			bo->offset = offset;
969
			if (verbose)
Ville Syrjälä's avatar
Ville Syrjälä committed
970
				printf("BO #%d (%dB) @ 0x%"PRIx64"\n", obj->handle,
971
				       bo->size, bo->offset);
972 973
			offset = align_u32(offset + bo->size + 4095, 4096);
		}
974

975
		if (bo->map == NULL && bo->size > 0)
976 977
			bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
		fail_if(bo->map == MAP_FAILED, "intel_aubdump: bo mmap failed\n");
978 979 980

		if (gen >= 10)
			gen8_map_ggtt_range(bo->offset, bo->offset + bo->size);
981 982
	}

983
	batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
984 985
			  execbuffer2->buffer_count - 1;
	batch_bo = get_bo(exec_objects[batch_index].handle);
986 987
	for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
		obj = &exec_objects[i];
988
		bo = get_bo(obj->handle);
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

		if (obj->relocation_count > 0)
			data = relocate_bo(bo, execbuffer2, obj);
		else
			data = bo->map;

		if (bo == batch_bo) {
			aub_write_trace_block(AUB_TRACE_TYPE_BATCH,
					      data, bo->size, bo->offset);
		} else {
			aub_write_trace_block(AUB_TRACE_TYPE_NOTYPE,
					      data, bo->size, bo->offset);
		}
		if (data != bo->map)
			free(data);
	}

1006 1007 1008 1009 1010 1011 1012 1013 1014
	if (gen >= 10) {
		aub_dump_execlist(batch_bo->offset +
				  execbuffer2->batch_start_offset, ring_flag);
	} else {
		/* Dump ring buffer */
		aub_dump_ringbuffer(batch_bo->offset +
				    execbuffer2->batch_start_offset, offset,
				    ring_flag);
	}
1015

1016 1017 1018 1019
	for (int i = 0; i < ARRAY_SIZE(files); i++) {
		if (files[i] != NULL)
			fflush(files[i]);
	}
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

	if (device_override &&
	    (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
		struct drm_i915_gem_exec_fence *fences =
			(void*)(uintptr_t)execbuffer2->cliprects_ptr;
		for (uint32_t i = 0; i < execbuffer2->num_cliprects; i++) {
			if ((fences[i].flags & I915_EXEC_FENCE_SIGNAL) != 0) {
				struct drm_syncobj_array arg = {
					.handles = (uintptr_t)&fences[i].handle,
					.count_handles = 1,
					.pad = 0,
				};
				libc_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &arg);
			}
		}
	}
1036 1037 1038 1039 1040 1041 1042 1043
}

static void
add_new_bo(int handle, uint64_t size, void *map)
{
	struct bo *bo = &bos[handle];

	fail_if(handle >= MAX_BO_COUNT, "intel_aubdump: bo handle out of range\n");
1044
	fail_if(size == 0, "intel_aubdump: bo size is invalid\n");
1045 1046 1047 1048 1049 1050 1051 1052

	bo->size = size;
	bo->map = map;
}

static void
remove_bo(int handle)
{
1053
	struct bo *bo = get_bo(handle);
1054 1055 1056

	if (bo->map && !IS_USERPTR(bo->map))
		munmap(bo->map, bo->size);
1057
	bo->size = 0;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	bo->map = NULL;
}

int
close(int fd)
{
	if (fd == drm_fd)
		drm_fd = -1;

	return libc_close(fd);
}

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static FILE *
launch_command(char *command)
{
	int i = 0, fds[2];
	char **args = calloc(strlen(command), sizeof(char *));
	char *iter = command;

	args[i++] = iter = command;

	while ((iter = strstr(iter, ",")) != NULL) {
		*iter = '\0';
		iter += 1;
		args[i++] = iter;
	}

	if (pipe(fds) == -1)
		return NULL;

	switch (fork()) {
	case 0:
		dup2(fds[0], 0);
		fail_if(execvp(args[0], args) == -1,
			"intel_aubdump: failed to launch child command\n");
		return NULL;

	default:
		free(args);
		return fdopen(fds[1], "w");

	case -1:
		return NULL;
	}
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
static void
maybe_init(void)
{
	static bool initialized = false;
	FILE *config;
	char *key, *value;

	if (initialized)
		return;

	initialized = true;

	config = fdopen(3, "r");
	while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
		if (!strcmp(key, "verbose")) {
			verbose = 1;
		} else if (!strcmp(key, "device")) {
			fail_if(sscanf(value, "%i", &device) != 1,
				"intel_aubdump: failed to parse device id '%s'",
				value);
			device_override = true;
		} else if (!strcmp(key, "file")) {
1126 1127 1128
			filename = strdup(value);
			files[0] = fopen(filename, "w+");
			fail_if(files[0] == NULL,
1129 1130
				"intel_aubdump: failed to open file '%s'\n",
				filename);
1131 1132 1133 1134 1135
		} else if (!strcmp(key,  "command")) {
			files[1] = launch_command(value);
			fail_if(files[1] == NULL,
				"intel_aubdump: failed to launch command '%s'\n",
				value);
1136 1137 1138 1139 1140 1141 1142 1143 1144
		} else {
			fprintf(stderr, "intel_aubdump: unknown option '%s'\n", key);
		}

		free(key);
		free(value);
	}
	fclose(config);

1145
	bos = calloc(MAX_BO_COUNT, sizeof(bos[0]));
1146 1147 1148
	fail_if(bos == NULL, "intel_aubdump: out of memory\n");
}

1149 1150 1151
#define LOCAL_IOCTL_I915_GEM_EXECBUFFER2_WR \
    DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER2, struct drm_i915_gem_execbuffer2)

1152 1153 1154 1155 1156 1157
int
ioctl(int fd, unsigned long request, ...)
{
	va_list args;
	void *argp;
	int ret;
1158
	struct stat buf;
1159 1160 1161 1162 1163

	va_start(args, request);
	argp = va_arg(args, void *);
	va_end(args);

1164 1165 1166 1167 1168 1169 1170 1171
	if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
	    drm_fd != fd && fstat(fd, &buf) == 0 &&
	    (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
		drm_fd = fd;
		if (verbose)
			printf("[intel_aubdump: intercept drm ioctl on fd %d]\n", fd);
	}

1172
	if (fd == drm_fd) {
1173 1174
		maybe_init();

1175
		switch (request) {
1176 1177 1178 1179 1180 1181 1182
		case DRM_IOCTL_I915_GETPARAM: {
			struct drm_i915_getparam *getparam = argp;

			if (device_override && getparam->param == I915_PARAM_CHIPSET_ID) {
				*getparam->value = device;
				return 0;
			}
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

			ret = libc_ioctl(fd, request, argp);

			/* If the application looks up chipset_id
			 * (they typically do), we'll piggy-back on
			 * their ioctl and store the id for later
			 * use. */
			if (getparam->param == I915_PARAM_CHIPSET_ID)
				device = *getparam->value;

			return ret;
1194 1195
		}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		case DRM_IOCTL_I915_GEM_EXECBUFFER: {
			static bool once;
			if (!once) {
				fprintf(stderr, "intel_aubdump: "
					"application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
				once = true;
			}
			return libc_ioctl(fd, request, argp);
		}

1206
		case DRM_IOCTL_I915_GEM_EXECBUFFER2:
1207
		case LOCAL_IOCTL_I915_GEM_EXECBUFFER2_WR: {
1208
			dump_execbuffer2(fd, argp);
1209 1210 1211
			if (device_override)
				return 0;

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
			return libc_ioctl(fd, request, argp);
		}

		case DRM_IOCTL_I915_GEM_CREATE: {
			struct drm_i915_gem_create *create = argp;

			ret = libc_ioctl(fd, request, argp);
			if (ret == 0)
				add_new_bo(create->handle, create->size, NULL);

			return ret;
		}

		case DRM_IOCTL_I915_GEM_USERPTR: {
			struct drm_i915_gem_userptr *userptr = argp;

			ret = libc_ioctl(fd, request, argp);
			if (ret == 0)
				add_new_bo(userptr->handle, userptr->user_size,
					   (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
			return ret;
		}

		case DRM_IOCTL_GEM_CLOSE: {
			struct drm_gem_close *close = argp;

			remove_bo(close->handle);

			return libc_ioctl(fd, request, argp);
		}

		case DRM_IOCTL_GEM_OPEN: {
			struct drm_gem_open *open = argp;

			ret = libc_ioctl(fd, request, argp);
			if (ret == 0)
				add_new_bo(open->handle, open->size, NULL);

			return ret;
		}

		case DRM_IOCTL_PRIME_FD_TO_HANDLE: {
			struct drm_prime_handle *prime = argp;

			ret = libc_ioctl(fd, request, argp);
			if (ret == 0) {
				off_t size;

				size = lseek(prime->fd, 0, SEEK_END);
				fail_if(size == -1, "intel_aubdump: failed to get prime bo size\n");
				add_new_bo(prime->handle, size, NULL);
			}

			return ret;
		}

		default:
			return libc_ioctl(fd, request, argp);
		}
	} else {
		return libc_ioctl(fd, request, argp);
	}
}

1276
static void
1277 1278 1279 1280
init(void)
{
	libc_close = dlsym(RTLD_NEXT, "close");
	libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
1281 1282
	fail_if(libc_close == NULL || libc_ioctl == NULL,
		"intel_aubdump: failed to get libc ioctl or close\n");
1283 1284
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
static int
close_init_helper(int fd)
{
	init();
	return libc_close(fd);
}

static int
ioctl_init_helper(int fd, unsigned long request, ...)
{
	va_list args;
	void *argp;

	va_start(args, request);
	argp = va_arg(args, void *);
	va_end(args);

	init();
	return libc_ioctl(fd, request, argp);
}

1306 1307 1308 1309
static void __attribute__ ((destructor))
fini(void)
{
	free(filename);
1310 1311 1312 1313
	for (int i = 0; i < ARRAY_SIZE(files); i++) {
		if (files[i] != NULL)
			fclose(files[i]);
	}
1314 1315
	free(bos);
}