1 /*
2  * Copyright © 2009-2011 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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <assert.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdbool.h>
33 #include <stdarg.h>
34 #include <string.h>
35 
36 #include "libdrm_macros.h"
37 #include "xf86drm.h"
38 #include "intel_chipset.h"
39 #include "intel_bufmgr.h"
40 
41 /* The compiler throws ~90 warnings. Do not spam the build, until we fix them. */
42 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
43 
44 /* Struct for tracking drm_intel_decode state. */
45 struct drm_intel_decode {
46 	/** stdio file where the output should land.  Defaults to stdout. */
47 	FILE *out;
48 
49 	/** PCI device ID. */
50 	uint32_t devid;
51 
52 	/**
53 	 * Shorthand device identifier: 3 is 915, 4 is 965, 5 is
54 	 * Ironlake, etc.
55 	 */
56 	int gen;
57 
58 	/** GPU address of the start of the current packet. */
59 	uint32_t hw_offset;
60 	/** CPU virtual address of the start of the current packet. */
61 	uint32_t *data;
62 	/** DWORDs of remaining batchbuffer data starting from the packet. */
63 	uint32_t count;
64 
65 	/** GPU address of the start of the batchbuffer data. */
66 	uint32_t base_hw_offset;
67 	/** CPU Virtual address of the start of the batchbuffer data. */
68 	uint32_t *base_data;
69 	/** Number of DWORDs of batchbuffer data. */
70 	uint32_t base_count;
71 
72 	/** @{
73 	 * GPU head and tail pointers, which will be noted in the dump, or ~0.
74 	 */
75 	uint32_t head, tail;
76 	/** @} */
77 
78 	/**
79 	 * Whether to dump the dwords after MI_BATCHBUFFER_END.
80 	 *
81 	 * This sometimes provides clues in corrupted batchbuffers,
82 	 * and is used by the intel-gpu-tools.
83 	 */
84 	bool dump_past_end;
85 
86 	bool overflowed;
87 };
88 
89 static FILE *out;
90 static uint32_t saved_s2 = 0, saved_s4 = 0;
91 static char saved_s2_set = 0, saved_s4_set = 0;
92 static uint32_t head_offset = 0xffffffff;	/* undefined */
93 static uint32_t tail_offset = 0xffffffff;	/* undefined */
94 
95 #ifndef ARRAY_SIZE
96 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
97 #endif
98 
99 #define BUFFER_FAIL(_count, _len, _name) do {			\
100     fprintf(out, "Buffer size too small in %s (%d < %d)\n",	\
101 	    (_name), (_count), (_len));				\
102     return _count;						\
103 } while (0)
104 
int_as_float(uint32_t intval)105 static float int_as_float(uint32_t intval)
106 {
107 	union intfloat {
108 		uint32_t i;
109 		float f;
110 	} uval;
111 
112 	uval.i = intval;
113 	return uval.f;
114 }
115 
116 static void DRM_PRINTFLIKE(3, 4)
instr_out(struct drm_intel_decode * ctx,unsigned int index,const char * fmt,...)117 instr_out(struct drm_intel_decode *ctx, unsigned int index,
118 	  const char *fmt, ...)
119 {
120 	va_list va;
121 	const char *parseinfo;
122 	uint32_t offset = ctx->hw_offset + index * 4;
123 
124 	if (index > ctx->count) {
125 		if (!ctx->overflowed) {
126 			fprintf(out, "ERROR: Decode attempted to continue beyond end of batchbuffer\n");
127 			ctx->overflowed = true;
128 		}
129 		return;
130 	}
131 
132 	if (offset == head_offset)
133 		parseinfo = "HEAD";
134 	else if (offset == tail_offset)
135 		parseinfo = "TAIL";
136 	else
137 		parseinfo = "    ";
138 
139 	fprintf(out, "0x%08x: %s 0x%08x: %s", offset, parseinfo,
140 		ctx->data[index], index == 0 ? "" : "   ");
141 	va_start(va, fmt);
142 	vfprintf(out, fmt, va);
143 	va_end(va);
144 }
145 
146 static int
decode_MI_SET_CONTEXT(struct drm_intel_decode * ctx)147 decode_MI_SET_CONTEXT(struct drm_intel_decode *ctx)
148 {
149 	uint32_t data = ctx->data[1];
150 	if (ctx->gen > 7)
151 		return 1;
152 
153 	instr_out(ctx, 0, "MI_SET_CONTEXT\n");
154 	instr_out(ctx, 1, "gtt offset = 0x%x%s%s\n",
155 		  data & ~0xfff,
156 		  data & (1<<1)? ", Force Restore": "",
157 		  data & (1<<0)? ", Restore Inhibit": "");
158 
159 	return 2;
160 }
161 
162 static int
decode_MI_WAIT_FOR_EVENT(struct drm_intel_decode * ctx)163 decode_MI_WAIT_FOR_EVENT(struct drm_intel_decode *ctx)
164 {
165 	const char *cc_wait;
166 	int cc_shift = 0;
167 	uint32_t data = ctx->data[0];
168 
169 	if (ctx->gen <= 5)
170 		cc_shift = 9;
171 	else
172 		cc_shift = 16;
173 
174 	switch ((data >> cc_shift) & 0x1f) {
175 	case 1:
176 		cc_wait = ", cc wait 1";
177 		break;
178 	case 2:
179 		cc_wait = ", cc wait 2";
180 		break;
181 	case 3:
182 		cc_wait = ", cc wait 3";
183 		break;
184 	case 4:
185 		cc_wait = ", cc wait 4";
186 		break;
187 	case 5:
188 		cc_wait = ", cc wait 4";
189 		break;
190 	default:
191 		cc_wait = "";
192 		break;
193 	}
194 
195 	if (ctx->gen <= 5) {
196 		instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
197 			  data & (1<<18)? ", pipe B start vblank wait": "",
198 			  data & (1<<17)? ", pipe A start vblank wait": "",
199 			  data & (1<<16)? ", overlay flip pending wait": "",
200 			  data & (1<<14)? ", pipe B hblank wait": "",
201 			  data & (1<<13)? ", pipe A hblank wait": "",
202 			  cc_wait,
203 			  data & (1<<8)? ", plane C pending flip wait": "",
204 			  data & (1<<7)? ", pipe B vblank wait": "",
205 			  data & (1<<6)? ", plane B pending flip wait": "",
206 			  data & (1<<5)? ", pipe B scan line wait": "",
207 			  data & (1<<4)? ", fbc idle wait": "",
208 			  data & (1<<3)? ", pipe A vblank wait": "",
209 			  data & (1<<2)? ", plane A pending flip wait": "",
210 			  data & (1<<1)? ", plane A scan line wait": "");
211 	} else {
212 		instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s\n",
213 			  data & (1<<20)? ", sprite C pending flip wait": "", /* ivb */
214 			  cc_wait,
215 			  data & (1<<13)? ", pipe B hblank wait": "",
216 			  data & (1<<11)? ", pipe B vblank wait": "",
217 			  data & (1<<10)? ", sprite B pending flip wait": "",
218 			  data & (1<<9)? ", plane B pending flip wait": "",
219 			  data & (1<<8)? ", plane B scan line wait": "",
220 			  data & (1<<5)? ", pipe A hblank wait": "",
221 			  data & (1<<3)? ", pipe A vblank wait": "",
222 			  data & (1<<2)? ", sprite A pending flip wait": "",
223 			  data & (1<<1)? ", plane A pending flip wait": "",
224 			  data & (1<<0)? ", plane A scan line wait": "");
225 	}
226 
227 	return 1;
228 }
229 
230 static int
decode_mi(struct drm_intel_decode * ctx)231 decode_mi(struct drm_intel_decode *ctx)
232 {
233 	unsigned int opcode, len = -1;
234 	const char *post_sync_op = "";
235 	uint32_t *data = ctx->data;
236 
237 	struct {
238 		uint32_t opcode;
239 		int len_mask;
240 		unsigned int min_len;
241 		unsigned int max_len;
242 		const char *name;
243 		int (*func)(struct drm_intel_decode *ctx);
244 	} opcodes_mi[] = {
245 		{ 0x08, 0, 1, 1, "MI_ARB_ON_OFF" },
246 		{ 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
247 		{ 0x30, 0x3f, 3, 3, "MI_BATCH_BUFFER" },
248 		{ 0x31, 0x3f, 2, 2, "MI_BATCH_BUFFER_START" },
249 		{ 0x14, 0x3f, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
250 		{ 0x04, 0, 1, 1, "MI_FLUSH" },
251 		{ 0x22, 0x1f, 3, 3, "MI_LOAD_REGISTER_IMM" },
252 		{ 0x13, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
253 		{ 0x12, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
254 		{ 0x00, 0, 1, 1, "MI_NOOP" },
255 		{ 0x11, 0x3f, 2, 2, "MI_OVERLAY_FLIP" },
256 		{ 0x07, 0, 1, 1, "MI_REPORT_HEAD" },
257 		{ 0x18, 0x3f, 2, 2, "MI_SET_CONTEXT", decode_MI_SET_CONTEXT },
258 		{ 0x20, 0x3f, 3, 4, "MI_STORE_DATA_IMM" },
259 		{ 0x21, 0x3f, 3, 4, "MI_STORE_DATA_INDEX" },
260 		{ 0x24, 0x3f, 3, 3, "MI_STORE_REGISTER_MEM" },
261 		{ 0x02, 0, 1, 1, "MI_USER_INTERRUPT" },
262 		{ 0x03, 0, 1, 1, "MI_WAIT_FOR_EVENT", decode_MI_WAIT_FOR_EVENT },
263 		{ 0x16, 0x7f, 3, 3, "MI_SEMAPHORE_MBOX" },
264 		{ 0x26, 0x1f, 3, 4, "MI_FLUSH_DW" },
265 		{ 0x28, 0x3f, 3, 3, "MI_REPORT_PERF_COUNT" },
266 		{ 0x29, 0xff, 3, 3, "MI_LOAD_REGISTER_MEM" },
267 		{ 0x0b, 0, 1, 1, "MI_SUSPEND_FLUSH"},
268 	}, *opcode_mi = NULL;
269 
270 	/* check instruction length */
271 	for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
272 	     opcode++) {
273 		if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
274 			len = 1;
275 			if (opcodes_mi[opcode].max_len > 1) {
276 				len =
277 				    (data[0] & opcodes_mi[opcode].len_mask) + 2;
278 				if (len < opcodes_mi[opcode].min_len
279 				    || len > opcodes_mi[opcode].max_len) {
280 					fprintf(out,
281 						"Bad length (%d) in %s, [%d, %d]\n",
282 						len, opcodes_mi[opcode].name,
283 						opcodes_mi[opcode].min_len,
284 						opcodes_mi[opcode].max_len);
285 				}
286 			}
287 			opcode_mi = &opcodes_mi[opcode];
288 			break;
289 		}
290 	}
291 
292 	if (opcode_mi && opcode_mi->func)
293 		return opcode_mi->func(ctx);
294 
295 	switch ((data[0] & 0x1f800000) >> 23) {
296 	case 0x0a:
297 		instr_out(ctx, 0, "MI_BATCH_BUFFER_END\n");
298 		return -1;
299 	case 0x16:
300 		instr_out(ctx, 0, "MI_SEMAPHORE_MBOX%s%s%s%s %u\n",
301 			  data[0] & (1 << 22) ? " global gtt," : "",
302 			  data[0] & (1 << 21) ? " update semaphore," : "",
303 			  data[0] & (1 << 20) ? " compare semaphore," : "",
304 			  data[0] & (1 << 18) ? " use compare reg" : "",
305 			  (data[0] & (0x3 << 16)) >> 16);
306 		instr_out(ctx, 1, "value\n");
307 		instr_out(ctx, 2, "address\n");
308 		return len;
309 	case 0x21:
310 		instr_out(ctx, 0, "MI_STORE_DATA_INDEX%s\n",
311 			  data[0] & (1 << 21) ? " use per-process HWS," : "");
312 		instr_out(ctx, 1, "index\n");
313 		instr_out(ctx, 2, "dword\n");
314 		if (len == 4)
315 			instr_out(ctx, 3, "upper dword\n");
316 		return len;
317 	case 0x00:
318 		if (data[0] & (1 << 22))
319 			instr_out(ctx, 0,
320 				  "MI_NOOP write NOPID reg, val=0x%x\n",
321 				  data[0] & ((1 << 22) - 1));
322 		else
323 			instr_out(ctx, 0, "MI_NOOP\n");
324 		return len;
325 	case 0x26:
326 		switch (data[0] & (0x3 << 14)) {
327 		case (0 << 14):
328 			post_sync_op = "no write";
329 			break;
330 		case (1 << 14):
331 			post_sync_op = "write data";
332 			break;
333 		case (2 << 14):
334 			post_sync_op = "reserved";
335 			break;
336 		case (3 << 14):
337 			post_sync_op = "write TIMESTAMP";
338 			break;
339 		}
340 		instr_out(ctx, 0,
341 			  "MI_FLUSH_DW%s%s%s%s post_sync_op='%s' %s%s\n",
342 			  data[0] & (1 << 22) ?
343 			  " enable protected mem (BCS-only)," : "",
344 			  data[0] & (1 << 21) ? " store in hws," : "",
345 			  data[0] & (1 << 18) ? " invalidate tlb," : "",
346 			  data[0] & (1 << 17) ? " flush gfdt," : "",
347 			  post_sync_op,
348 			  data[0] & (1 << 8) ? " enable notify interrupt," : "",
349 			  data[0] & (1 << 7) ?
350 			  " invalidate video state (BCS-only)," : "");
351 		if (data[0] & (1 << 21))
352 			instr_out(ctx, 1, "hws index\n");
353 		else
354 			instr_out(ctx, 1, "address\n");
355 		instr_out(ctx, 2, "dword\n");
356 		if (len == 4)
357 			instr_out(ctx, 3, "upper dword\n");
358 		return len;
359 	}
360 
361 	for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
362 	     opcode++) {
363 		if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
364 			unsigned int i;
365 
366 			instr_out(ctx, 0, "%s\n",
367 				  opcodes_mi[opcode].name);
368 			for (i = 1; i < len; i++) {
369 				instr_out(ctx, i, "dword %d\n", i);
370 			}
371 
372 			return len;
373 		}
374 	}
375 
376 	instr_out(ctx, 0, "MI UNKNOWN\n");
377 	return 1;
378 }
379 
380 static void
decode_2d_br00(struct drm_intel_decode * ctx,const char * cmd)381 decode_2d_br00(struct drm_intel_decode *ctx, const char *cmd)
382 {
383 	instr_out(ctx, 0,
384 		  "%s (rgb %sabled, alpha %sabled, src tile %d, dst tile %d)\n",
385 		  cmd,
386 		  (ctx->data[0] & (1 << 20)) ? "en" : "dis",
387 		  (ctx->data[0] & (1 << 21)) ? "en" : "dis",
388 		  (ctx->data[0] >> 15) & 1,
389 		  (ctx->data[0] >> 11) & 1);
390 }
391 
392 static void
decode_2d_br01(struct drm_intel_decode * ctx)393 decode_2d_br01(struct drm_intel_decode *ctx)
394 {
395 	const char *format;
396 	switch ((ctx->data[1] >> 24) & 0x3) {
397 	case 0:
398 		format = "8";
399 		break;
400 	case 1:
401 		format = "565";
402 		break;
403 	case 2:
404 		format = "1555";
405 		break;
406 	case 3:
407 		format = "8888";
408 		break;
409 	}
410 
411 	instr_out(ctx, 1,
412 		  "format %s, pitch %d, rop 0x%02x, "
413 		  "clipping %sabled, %s%s \n",
414 		  format,
415 		  (short)(ctx->data[1] & 0xffff),
416 		  (ctx->data[1] >> 16) & 0xff,
417 		  ctx->data[1] & (1 << 30) ? "en" : "dis",
418 		  ctx->data[1] & (1 << 31) ? "solid pattern enabled, " : "",
419 		  ctx->data[1] & (1 << 31) ?
420 		  "mono pattern transparency enabled, " : "");
421 
422 }
423 
424 static int
decode_2d(struct drm_intel_decode * ctx)425 decode_2d(struct drm_intel_decode *ctx)
426 {
427 	unsigned int opcode, len;
428 	uint32_t *data = ctx->data;
429 
430 	struct {
431 		uint32_t opcode;
432 		unsigned int min_len;
433 		unsigned int max_len;
434 		const char *name;
435 	} opcodes_2d[] = {
436 		{ 0x40, 5, 5, "COLOR_BLT" },
437 		{ 0x43, 6, 6, "SRC_COPY_BLT" },
438 		{ 0x01, 8, 8, "XY_SETUP_BLT" },
439 		{ 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
440 		{ 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
441 		{ 0x24, 2, 2, "XY_PIXEL_BLT" },
442 		{ 0x25, 3, 3, "XY_SCANLINES_BLT" },
443 		{ 0x26, 4, 4, "Y_TEXT_BLT" },
444 		{ 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
445 		{ 0x50, 6, 6, "XY_COLOR_BLT" },
446 		{ 0x51, 6, 6, "XY_PAT_BLT" },
447 		{ 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
448 		{ 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
449 		{ 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
450 		{ 0x52, 9, 9, "XY_MONO_PAT_BLT" },
451 		{ 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
452 		{ 0x53, 8, 8, "XY_SRC_COPY_BLT" },
453 		{ 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
454 		{ 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
455 		{ 0x55, 9, 9, "XY_FULL_BLT" },
456 		{ 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
457 		{ 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
458 		{ 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
459 		{ 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
460 		{ 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT"},
461 	};
462 
463 	switch ((data[0] & 0x1fc00000) >> 22) {
464 	case 0x25:
465 		instr_out(ctx, 0,
466 			  "XY_SCANLINES_BLT (pattern seed (%d, %d), dst tile %d)\n",
467 			  (data[0] >> 12) & 0x8,
468 			  (data[0] >> 8) & 0x8, (data[0] >> 11) & 1);
469 
470 		len = (data[0] & 0x000000ff) + 2;
471 		if (len != 3)
472 			fprintf(out, "Bad count in XY_SCANLINES_BLT\n");
473 
474 		instr_out(ctx, 1, "dest (%d,%d)\n",
475 			  data[1] & 0xffff, data[1] >> 16);
476 		instr_out(ctx, 2, "dest (%d,%d)\n",
477 			  data[2] & 0xffff, data[2] >> 16);
478 		return len;
479 	case 0x01:
480 		decode_2d_br00(ctx, "XY_SETUP_BLT");
481 
482 		len = (data[0] & 0x000000ff) + 2;
483 		if (len != 8)
484 			fprintf(out, "Bad count in XY_SETUP_BLT\n");
485 
486 		decode_2d_br01(ctx);
487 		instr_out(ctx, 2, "cliprect (%d,%d)\n",
488 			  data[2] & 0xffff, data[2] >> 16);
489 		instr_out(ctx, 3, "cliprect (%d,%d)\n",
490 			  data[3] & 0xffff, data[3] >> 16);
491 		instr_out(ctx, 4, "setup dst offset 0x%08x\n",
492 			  data[4]);
493 		instr_out(ctx, 5, "setup background color\n");
494 		instr_out(ctx, 6, "setup foreground color\n");
495 		instr_out(ctx, 7, "color pattern offset\n");
496 		return len;
497 	case 0x03:
498 		decode_2d_br00(ctx, "XY_SETUP_CLIP_BLT");
499 
500 		len = (data[0] & 0x000000ff) + 2;
501 		if (len != 3)
502 			fprintf(out, "Bad count in XY_SETUP_CLIP_BLT\n");
503 
504 		instr_out(ctx, 1, "cliprect (%d,%d)\n",
505 			  data[1] & 0xffff, data[2] >> 16);
506 		instr_out(ctx, 2, "cliprect (%d,%d)\n",
507 			  data[2] & 0xffff, data[3] >> 16);
508 		return len;
509 	case 0x11:
510 		decode_2d_br00(ctx, "XY_SETUP_MONO_PATTERN_SL_BLT");
511 
512 		len = (data[0] & 0x000000ff) + 2;
513 		if (len != 9)
514 			fprintf(out,
515 				"Bad count in XY_SETUP_MONO_PATTERN_SL_BLT\n");
516 
517 		decode_2d_br01(ctx);
518 		instr_out(ctx, 2, "cliprect (%d,%d)\n",
519 			  data[2] & 0xffff, data[2] >> 16);
520 		instr_out(ctx, 3, "cliprect (%d,%d)\n",
521 			  data[3] & 0xffff, data[3] >> 16);
522 		instr_out(ctx, 4, "setup dst offset 0x%08x\n",
523 			  data[4]);
524 		instr_out(ctx, 5, "setup background color\n");
525 		instr_out(ctx, 6, "setup foreground color\n");
526 		instr_out(ctx, 7, "mono pattern dw0\n");
527 		instr_out(ctx, 8, "mono pattern dw1\n");
528 		return len;
529 	case 0x50:
530 		decode_2d_br00(ctx, "XY_COLOR_BLT");
531 
532 		len = (data[0] & 0x000000ff) + 2;
533 		if (len != 6)
534 			fprintf(out, "Bad count in XY_COLOR_BLT\n");
535 
536 		decode_2d_br01(ctx);
537 		instr_out(ctx, 2, "(%d,%d)\n",
538 			  data[2] & 0xffff, data[2] >> 16);
539 		instr_out(ctx, 3, "(%d,%d)\n",
540 			  data[3] & 0xffff, data[3] >> 16);
541 		instr_out(ctx, 4, "offset 0x%08x\n", data[4]);
542 		instr_out(ctx, 5, "color\n");
543 		return len;
544 	case 0x53:
545 		decode_2d_br00(ctx, "XY_SRC_COPY_BLT");
546 
547 		len = (data[0] & 0x000000ff) + 2;
548 		if (len != 8)
549 			fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
550 
551 		decode_2d_br01(ctx);
552 		instr_out(ctx, 2, "dst (%d,%d)\n",
553 			  data[2] & 0xffff, data[2] >> 16);
554 		instr_out(ctx, 3, "dst (%d,%d)\n",
555 			  data[3] & 0xffff, data[3] >> 16);
556 		instr_out(ctx, 4, "dst offset 0x%08x\n", data[4]);
557 		instr_out(ctx, 5, "src (%d,%d)\n",
558 			  data[5] & 0xffff, data[5] >> 16);
559 		instr_out(ctx, 6, "src pitch %d\n",
560 			  (short)(data[6] & 0xffff));
561 		instr_out(ctx, 7, "src offset 0x%08x\n", data[7]);
562 		return len;
563 	}
564 
565 	for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
566 	     opcode++) {
567 		if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
568 			unsigned int i;
569 
570 			len = 1;
571 			instr_out(ctx, 0, "%s\n",
572 				  opcodes_2d[opcode].name);
573 			if (opcodes_2d[opcode].max_len > 1) {
574 				len = (data[0] & 0x000000ff) + 2;
575 				if (len < opcodes_2d[opcode].min_len ||
576 				    len > opcodes_2d[opcode].max_len) {
577 					fprintf(out, "Bad count in %s\n",
578 						opcodes_2d[opcode].name);
579 				}
580 			}
581 
582 			for (i = 1; i < len; i++) {
583 				instr_out(ctx, i, "dword %d\n", i);
584 			}
585 
586 			return len;
587 		}
588 	}
589 
590 	instr_out(ctx, 0, "2D UNKNOWN\n");
591 	return 1;
592 }
593 
594 static int
decode_3d_1c(struct drm_intel_decode * ctx)595 decode_3d_1c(struct drm_intel_decode *ctx)
596 {
597 	uint32_t *data = ctx->data;
598 	uint32_t opcode;
599 
600 	opcode = (data[0] & 0x00f80000) >> 19;
601 
602 	switch (opcode) {
603 	case 0x11:
604 		instr_out(ctx, 0,
605 			  "3DSTATE_DEPTH_SUBRECTANGLE_DISABLE\n");
606 		return 1;
607 	case 0x10:
608 		instr_out(ctx, 0, "3DSTATE_SCISSOR_ENABLE %s\n",
609 			  data[0] & 1 ? "enabled" : "disabled");
610 		return 1;
611 	case 0x01:
612 		instr_out(ctx, 0, "3DSTATE_MAP_COORD_SET_I830\n");
613 		return 1;
614 	case 0x0a:
615 		instr_out(ctx, 0, "3DSTATE_MAP_CUBE_I830\n");
616 		return 1;
617 	case 0x05:
618 		instr_out(ctx, 0, "3DSTATE_MAP_TEX_STREAM_I830\n");
619 		return 1;
620 	}
621 
622 	instr_out(ctx, 0, "3D UNKNOWN: 3d_1c opcode = 0x%x\n",
623 		  opcode);
624 	return 1;
625 }
626 
627 /** Sets the string dstname to describe the destination of the PS instruction */
628 static void
i915_get_instruction_dst(uint32_t * data,int i,char * dstname,int do_mask)629 i915_get_instruction_dst(uint32_t *data, int i, char *dstname, int do_mask)
630 {
631 	uint32_t a0 = data[i];
632 	int dst_nr = (a0 >> 14) & 0xf;
633 	char dstmask[8];
634 	const char *sat;
635 
636 	if (do_mask) {
637 		if (((a0 >> 10) & 0xf) == 0xf) {
638 			dstmask[0] = 0;
639 		} else {
640 			int dstmask_index = 0;
641 
642 			dstmask[dstmask_index++] = '.';
643 			if (a0 & (1 << 10))
644 				dstmask[dstmask_index++] = 'x';
645 			if (a0 & (1 << 11))
646 				dstmask[dstmask_index++] = 'y';
647 			if (a0 & (1 << 12))
648 				dstmask[dstmask_index++] = 'z';
649 			if (a0 & (1 << 13))
650 				dstmask[dstmask_index++] = 'w';
651 			dstmask[dstmask_index++] = 0;
652 		}
653 
654 		if (a0 & (1 << 22))
655 			sat = ".sat";
656 		else
657 			sat = "";
658 	} else {
659 		dstmask[0] = 0;
660 		sat = "";
661 	}
662 
663 	switch ((a0 >> 19) & 0x7) {
664 	case 0:
665 		if (dst_nr > 15)
666 			fprintf(out, "bad destination reg R%d\n", dst_nr);
667 		sprintf(dstname, "R%d%s%s", dst_nr, dstmask, sat);
668 		break;
669 	case 4:
670 		if (dst_nr > 0)
671 			fprintf(out, "bad destination reg oC%d\n", dst_nr);
672 		sprintf(dstname, "oC%s%s", dstmask, sat);
673 		break;
674 	case 5:
675 		if (dst_nr > 0)
676 			fprintf(out, "bad destination reg oD%d\n", dst_nr);
677 		sprintf(dstname, "oD%s%s", dstmask, sat);
678 		break;
679 	case 6:
680 		if (dst_nr > 3)
681 			fprintf(out, "bad destination reg U%d\n", dst_nr);
682 		sprintf(dstname, "U%d%s%s", dst_nr, dstmask, sat);
683 		break;
684 	default:
685 		sprintf(dstname, "RESERVED");
686 		break;
687 	}
688 }
689 
690 static const char *
i915_get_channel_swizzle(uint32_t select)691 i915_get_channel_swizzle(uint32_t select)
692 {
693 	switch (select & 0x7) {
694 	case 0:
695 		return (select & 8) ? "-x" : "x";
696 	case 1:
697 		return (select & 8) ? "-y" : "y";
698 	case 2:
699 		return (select & 8) ? "-z" : "z";
700 	case 3:
701 		return (select & 8) ? "-w" : "w";
702 	case 4:
703 		return (select & 8) ? "-0" : "0";
704 	case 5:
705 		return (select & 8) ? "-1" : "1";
706 	default:
707 		return (select & 8) ? "-bad" : "bad";
708 	}
709 }
710 
711 static void
i915_get_instruction_src_name(uint32_t src_type,uint32_t src_nr,char * name)712 i915_get_instruction_src_name(uint32_t src_type, uint32_t src_nr, char *name)
713 {
714 	switch (src_type) {
715 	case 0:
716 		sprintf(name, "R%d", src_nr);
717 		if (src_nr > 15)
718 			fprintf(out, "bad src reg %s\n", name);
719 		break;
720 	case 1:
721 		if (src_nr < 8)
722 			sprintf(name, "T%d", src_nr);
723 		else if (src_nr == 8)
724 			sprintf(name, "DIFFUSE");
725 		else if (src_nr == 9)
726 			sprintf(name, "SPECULAR");
727 		else if (src_nr == 10)
728 			sprintf(name, "FOG");
729 		else {
730 			fprintf(out, "bad src reg T%d\n", src_nr);
731 			sprintf(name, "RESERVED");
732 		}
733 		break;
734 	case 2:
735 		sprintf(name, "C%d", src_nr);
736 		if (src_nr > 31)
737 			fprintf(out, "bad src reg %s\n", name);
738 		break;
739 	case 4:
740 		sprintf(name, "oC");
741 		if (src_nr > 0)
742 			fprintf(out, "bad src reg oC%d\n", src_nr);
743 		break;
744 	case 5:
745 		sprintf(name, "oD");
746 		if (src_nr > 0)
747 			fprintf(out, "bad src reg oD%d\n", src_nr);
748 		break;
749 	case 6:
750 		sprintf(name, "U%d", src_nr);
751 		if (src_nr > 3)
752 			fprintf(out, "bad src reg %s\n", name);
753 		break;
754 	default:
755 		fprintf(out, "bad src reg type %d\n", src_type);
756 		sprintf(name, "RESERVED");
757 		break;
758 	}
759 }
760 
i915_get_instruction_src0(uint32_t * data,int i,char * srcname)761 static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
762 {
763 	uint32_t a0 = data[i];
764 	uint32_t a1 = data[i + 1];
765 	int src_nr = (a0 >> 2) & 0x1f;
766 	const char *swizzle_x = i915_get_channel_swizzle((a1 >> 28) & 0xf);
767 	const char *swizzle_y = i915_get_channel_swizzle((a1 >> 24) & 0xf);
768 	const char *swizzle_z = i915_get_channel_swizzle((a1 >> 20) & 0xf);
769 	const char *swizzle_w = i915_get_channel_swizzle((a1 >> 16) & 0xf);
770 	char swizzle[100];
771 
772 	i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
773 	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
774 		swizzle_w);
775 	if (strcmp(swizzle, ".xyzw") != 0)
776 		strcat(srcname, swizzle);
777 }
778 
i915_get_instruction_src1(uint32_t * data,int i,char * srcname)779 static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
780 {
781 	uint32_t a1 = data[i + 1];
782 	uint32_t a2 = data[i + 2];
783 	int src_nr = (a1 >> 8) & 0x1f;
784 	const char *swizzle_x = i915_get_channel_swizzle((a1 >> 4) & 0xf);
785 	const char *swizzle_y = i915_get_channel_swizzle((a1 >> 0) & 0xf);
786 	const char *swizzle_z = i915_get_channel_swizzle((a2 >> 28) & 0xf);
787 	const char *swizzle_w = i915_get_channel_swizzle((a2 >> 24) & 0xf);
788 	char swizzle[100];
789 
790 	i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
791 	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
792 		swizzle_w);
793 	if (strcmp(swizzle, ".xyzw") != 0)
794 		strcat(srcname, swizzle);
795 }
796 
i915_get_instruction_src2(uint32_t * data,int i,char * srcname)797 static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
798 {
799 	uint32_t a2 = data[i + 2];
800 	int src_nr = (a2 >> 16) & 0x1f;
801 	const char *swizzle_x = i915_get_channel_swizzle((a2 >> 12) & 0xf);
802 	const char *swizzle_y = i915_get_channel_swizzle((a2 >> 8) & 0xf);
803 	const char *swizzle_z = i915_get_channel_swizzle((a2 >> 4) & 0xf);
804 	const char *swizzle_w = i915_get_channel_swizzle((a2 >> 0) & 0xf);
805 	char swizzle[100];
806 
807 	i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
808 	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
809 		swizzle_w);
810 	if (strcmp(swizzle, ".xyzw") != 0)
811 		strcat(srcname, swizzle);
812 }
813 
814 static void
i915_get_instruction_addr(uint32_t src_type,uint32_t src_nr,char * name)815 i915_get_instruction_addr(uint32_t src_type, uint32_t src_nr, char *name)
816 {
817 	switch (src_type) {
818 	case 0:
819 		sprintf(name, "R%d", src_nr);
820 		if (src_nr > 15)
821 			fprintf(out, "bad src reg %s\n", name);
822 		break;
823 	case 1:
824 		if (src_nr < 8)
825 			sprintf(name, "T%d", src_nr);
826 		else if (src_nr == 8)
827 			sprintf(name, "DIFFUSE");
828 		else if (src_nr == 9)
829 			sprintf(name, "SPECULAR");
830 		else if (src_nr == 10)
831 			sprintf(name, "FOG");
832 		else {
833 			fprintf(out, "bad src reg T%d\n", src_nr);
834 			sprintf(name, "RESERVED");
835 		}
836 		break;
837 	case 4:
838 		sprintf(name, "oC");
839 		if (src_nr > 0)
840 			fprintf(out, "bad src reg oC%d\n", src_nr);
841 		break;
842 	case 5:
843 		sprintf(name, "oD");
844 		if (src_nr > 0)
845 			fprintf(out, "bad src reg oD%d\n", src_nr);
846 		break;
847 	default:
848 		fprintf(out, "bad src reg type %d\n", src_type);
849 		sprintf(name, "RESERVED");
850 		break;
851 	}
852 }
853 
854 static void
i915_decode_alu1(struct drm_intel_decode * ctx,int i,char * instr_prefix,const char * op_name)855 i915_decode_alu1(struct drm_intel_decode *ctx,
856 		 int i, char *instr_prefix, const char *op_name)
857 {
858 	char dst[100], src0[100];
859 
860 	i915_get_instruction_dst(ctx->data, i, dst, 1);
861 	i915_get_instruction_src0(ctx->data, i, src0);
862 
863 	instr_out(ctx, i++, "%s: %s %s, %s\n", instr_prefix,
864 		  op_name, dst, src0);
865 	instr_out(ctx, i++, "%s\n", instr_prefix);
866 	instr_out(ctx, i++, "%s\n", instr_prefix);
867 }
868 
869 static void
i915_decode_alu2(struct drm_intel_decode * ctx,int i,char * instr_prefix,const char * op_name)870 i915_decode_alu2(struct drm_intel_decode *ctx,
871 		 int i, char *instr_prefix, const char *op_name)
872 {
873 	char dst[100], src0[100], src1[100];
874 
875 	i915_get_instruction_dst(ctx->data, i, dst, 1);
876 	i915_get_instruction_src0(ctx->data, i, src0);
877 	i915_get_instruction_src1(ctx->data, i, src1);
878 
879 	instr_out(ctx, i++, "%s: %s %s, %s, %s\n", instr_prefix,
880 		  op_name, dst, src0, src1);
881 	instr_out(ctx, i++, "%s\n", instr_prefix);
882 	instr_out(ctx, i++, "%s\n", instr_prefix);
883 }
884 
885 static void
i915_decode_alu3(struct drm_intel_decode * ctx,int i,char * instr_prefix,const char * op_name)886 i915_decode_alu3(struct drm_intel_decode *ctx,
887 		 int i, char *instr_prefix, const char *op_name)
888 {
889 	char dst[100], src0[100], src1[100], src2[100];
890 
891 	i915_get_instruction_dst(ctx->data, i, dst, 1);
892 	i915_get_instruction_src0(ctx->data, i, src0);
893 	i915_get_instruction_src1(ctx->data, i, src1);
894 	i915_get_instruction_src2(ctx->data, i, src2);
895 
896 	instr_out(ctx, i++, "%s: %s %s, %s, %s, %s\n", instr_prefix,
897 		  op_name, dst, src0, src1, src2);
898 	instr_out(ctx, i++, "%s\n", instr_prefix);
899 	instr_out(ctx, i++, "%s\n", instr_prefix);
900 }
901 
902 static void
i915_decode_tex(struct drm_intel_decode * ctx,int i,const char * instr_prefix,const char * tex_name)903 i915_decode_tex(struct drm_intel_decode *ctx, int i,
904 		const char *instr_prefix, const char *tex_name)
905 {
906 	uint32_t t0 = ctx->data[i];
907 	uint32_t t1 = ctx->data[i + 1];
908 	char dst_name[100];
909 	char addr_name[100];
910 	int sampler_nr;
911 
912 	i915_get_instruction_dst(ctx->data, i, dst_name, 0);
913 	i915_get_instruction_addr((t1 >> 24) & 0x7,
914 				  (t1 >> 17) & 0xf, addr_name);
915 	sampler_nr = t0 & 0xf;
916 
917 	instr_out(ctx, i++, "%s: %s %s, S%d, %s\n", instr_prefix,
918 		  tex_name, dst_name, sampler_nr, addr_name);
919 	instr_out(ctx, i++, "%s\n", instr_prefix);
920 	instr_out(ctx, i++, "%s\n", instr_prefix);
921 }
922 
923 static void
i915_decode_dcl(struct drm_intel_decode * ctx,int i,char * instr_prefix)924 i915_decode_dcl(struct drm_intel_decode *ctx, int i, char *instr_prefix)
925 {
926 	uint32_t d0 = ctx->data[i];
927 	const char *sampletype;
928 	int dcl_nr = (d0 >> 14) & 0xf;
929 	const char *dcl_x = d0 & (1 << 10) ? "x" : "";
930 	const char *dcl_y = d0 & (1 << 11) ? "y" : "";
931 	const char *dcl_z = d0 & (1 << 12) ? "z" : "";
932 	const char *dcl_w = d0 & (1 << 13) ? "w" : "";
933 	char dcl_mask[10];
934 
935 	switch ((d0 >> 19) & 0x3) {
936 	case 1:
937 		sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
938 		if (strcmp(dcl_mask, ".") == 0)
939 			fprintf(out, "bad (empty) dcl mask\n");
940 
941 		if (dcl_nr > 10)
942 			fprintf(out, "bad T%d dcl register number\n", dcl_nr);
943 		if (dcl_nr < 8) {
944 			if (strcmp(dcl_mask, ".x") != 0 &&
945 			    strcmp(dcl_mask, ".xy") != 0 &&
946 			    strcmp(dcl_mask, ".xz") != 0 &&
947 			    strcmp(dcl_mask, ".w") != 0 &&
948 			    strcmp(dcl_mask, ".xyzw") != 0) {
949 				fprintf(out, "bad T%d.%s dcl mask\n", dcl_nr,
950 					dcl_mask);
951 			}
952 			instr_out(ctx, i++, "%s: DCL T%d%s\n",
953 				  instr_prefix, dcl_nr, dcl_mask);
954 		} else {
955 			if (strcmp(dcl_mask, ".xz") == 0)
956 				fprintf(out, "errataed bad dcl mask %s\n",
957 					dcl_mask);
958 			else if (strcmp(dcl_mask, ".xw") == 0)
959 				fprintf(out, "errataed bad dcl mask %s\n",
960 					dcl_mask);
961 			else if (strcmp(dcl_mask, ".xzw") == 0)
962 				fprintf(out, "errataed bad dcl mask %s\n",
963 					dcl_mask);
964 
965 			if (dcl_nr == 8) {
966 				instr_out(ctx, i++,
967 					  "%s: DCL DIFFUSE%s\n", instr_prefix,
968 					  dcl_mask);
969 			} else if (dcl_nr == 9) {
970 				instr_out(ctx, i++,
971 					  "%s: DCL SPECULAR%s\n", instr_prefix,
972 					  dcl_mask);
973 			} else if (dcl_nr == 10) {
974 				instr_out(ctx, i++,
975 					  "%s: DCL FOG%s\n", instr_prefix,
976 					  dcl_mask);
977 			}
978 		}
979 		instr_out(ctx, i++, "%s\n", instr_prefix);
980 		instr_out(ctx, i++, "%s\n", instr_prefix);
981 		break;
982 	case 3:
983 		switch ((d0 >> 22) & 0x3) {
984 		case 0:
985 			sampletype = "2D";
986 			break;
987 		case 1:
988 			sampletype = "CUBE";
989 			break;
990 		case 2:
991 			sampletype = "3D";
992 			break;
993 		default:
994 			sampletype = "RESERVED";
995 			break;
996 		}
997 		if (dcl_nr > 15)
998 			fprintf(out, "bad S%d dcl register number\n", dcl_nr);
999 		instr_out(ctx, i++, "%s: DCL S%d %s\n",
1000 			  instr_prefix, dcl_nr, sampletype);
1001 		instr_out(ctx, i++, "%s\n", instr_prefix);
1002 		instr_out(ctx, i++, "%s\n", instr_prefix);
1003 		break;
1004 	default:
1005 		instr_out(ctx, i++, "%s: DCL RESERVED%d\n",
1006 			  instr_prefix, dcl_nr);
1007 		instr_out(ctx, i++, "%s\n", instr_prefix);
1008 		instr_out(ctx, i++, "%s\n", instr_prefix);
1009 	}
1010 }
1011 
1012 static void
i915_decode_instruction(struct drm_intel_decode * ctx,int i,char * instr_prefix)1013 i915_decode_instruction(struct drm_intel_decode *ctx,
1014 			int i, char *instr_prefix)
1015 {
1016 	switch ((ctx->data[i] >> 24) & 0x1f) {
1017 	case 0x0:
1018 		instr_out(ctx, i++, "%s: NOP\n", instr_prefix);
1019 		instr_out(ctx, i++, "%s\n", instr_prefix);
1020 		instr_out(ctx, i++, "%s\n", instr_prefix);
1021 		break;
1022 	case 0x01:
1023 		i915_decode_alu2(ctx, i, instr_prefix, "ADD");
1024 		break;
1025 	case 0x02:
1026 		i915_decode_alu1(ctx, i, instr_prefix, "MOV");
1027 		break;
1028 	case 0x03:
1029 		i915_decode_alu2(ctx, i, instr_prefix, "MUL");
1030 		break;
1031 	case 0x04:
1032 		i915_decode_alu3(ctx, i, instr_prefix, "MAD");
1033 		break;
1034 	case 0x05:
1035 		i915_decode_alu3(ctx, i, instr_prefix, "DP2ADD");
1036 		break;
1037 	case 0x06:
1038 		i915_decode_alu2(ctx, i, instr_prefix, "DP3");
1039 		break;
1040 	case 0x07:
1041 		i915_decode_alu2(ctx, i, instr_prefix, "DP4");
1042 		break;
1043 	case 0x08:
1044 		i915_decode_alu1(ctx, i, instr_prefix, "FRC");
1045 		break;
1046 	case 0x09:
1047 		i915_decode_alu1(ctx, i, instr_prefix, "RCP");
1048 		break;
1049 	case 0x0a:
1050 		i915_decode_alu1(ctx, i, instr_prefix, "RSQ");
1051 		break;
1052 	case 0x0b:
1053 		i915_decode_alu1(ctx, i, instr_prefix, "EXP");
1054 		break;
1055 	case 0x0c:
1056 		i915_decode_alu1(ctx, i, instr_prefix, "LOG");
1057 		break;
1058 	case 0x0d:
1059 		i915_decode_alu2(ctx, i, instr_prefix, "CMP");
1060 		break;
1061 	case 0x0e:
1062 		i915_decode_alu2(ctx, i, instr_prefix, "MIN");
1063 		break;
1064 	case 0x0f:
1065 		i915_decode_alu2(ctx, i, instr_prefix, "MAX");
1066 		break;
1067 	case 0x10:
1068 		i915_decode_alu1(ctx, i, instr_prefix, "FLR");
1069 		break;
1070 	case 0x11:
1071 		i915_decode_alu1(ctx, i, instr_prefix, "MOD");
1072 		break;
1073 	case 0x12:
1074 		i915_decode_alu1(ctx, i, instr_prefix, "TRC");
1075 		break;
1076 	case 0x13:
1077 		i915_decode_alu2(ctx, i, instr_prefix, "SGE");
1078 		break;
1079 	case 0x14:
1080 		i915_decode_alu2(ctx, i, instr_prefix, "SLT");
1081 		break;
1082 	case 0x15:
1083 		i915_decode_tex(ctx, i, instr_prefix, "TEXLD");
1084 		break;
1085 	case 0x16:
1086 		i915_decode_tex(ctx, i, instr_prefix, "TEXLDP");
1087 		break;
1088 	case 0x17:
1089 		i915_decode_tex(ctx, i, instr_prefix, "TEXLDB");
1090 		break;
1091 	case 0x19:
1092 		i915_decode_dcl(ctx, i, instr_prefix);
1093 		break;
1094 	default:
1095 		instr_out(ctx, i++, "%s: unknown\n", instr_prefix);
1096 		instr_out(ctx, i++, "%s\n", instr_prefix);
1097 		instr_out(ctx, i++, "%s\n", instr_prefix);
1098 		break;
1099 	}
1100 }
1101 
1102 static const char *
decode_compare_func(uint32_t op)1103 decode_compare_func(uint32_t op)
1104 {
1105 	switch (op & 0x7) {
1106 	case 0:
1107 		return "always";
1108 	case 1:
1109 		return "never";
1110 	case 2:
1111 		return "less";
1112 	case 3:
1113 		return "equal";
1114 	case 4:
1115 		return "lequal";
1116 	case 5:
1117 		return "greater";
1118 	case 6:
1119 		return "notequal";
1120 	case 7:
1121 		return "gequal";
1122 	}
1123 	return "";
1124 }
1125 
1126 static const char *
decode_stencil_op(uint32_t op)1127 decode_stencil_op(uint32_t op)
1128 {
1129 	switch (op & 0x7) {
1130 	case 0:
1131 		return "keep";
1132 	case 1:
1133 		return "zero";
1134 	case 2:
1135 		return "replace";
1136 	case 3:
1137 		return "incr_sat";
1138 	case 4:
1139 		return "decr_sat";
1140 	case 5:
1141 		return "greater";
1142 	case 6:
1143 		return "incr";
1144 	case 7:
1145 		return "decr";
1146 	}
1147 	return "";
1148 }
1149 
1150 #if 0
1151 static const char *
1152 decode_logic_op(uint32_t op)
1153 {
1154 	switch (op & 0xf) {
1155 	case 0:
1156 		return "clear";
1157 	case 1:
1158 		return "nor";
1159 	case 2:
1160 		return "and_inv";
1161 	case 3:
1162 		return "copy_inv";
1163 	case 4:
1164 		return "and_rvrse";
1165 	case 5:
1166 		return "inv";
1167 	case 6:
1168 		return "xor";
1169 	case 7:
1170 		return "nand";
1171 	case 8:
1172 		return "and";
1173 	case 9:
1174 		return "equiv";
1175 	case 10:
1176 		return "noop";
1177 	case 11:
1178 		return "or_inv";
1179 	case 12:
1180 		return "copy";
1181 	case 13:
1182 		return "or_rvrse";
1183 	case 14:
1184 		return "or";
1185 	case 15:
1186 		return "set";
1187 	}
1188 	return "";
1189 }
1190 #endif
1191 
1192 static const char *
decode_blend_fact(uint32_t op)1193 decode_blend_fact(uint32_t op)
1194 {
1195 	switch (op & 0xf) {
1196 	case 1:
1197 		return "zero";
1198 	case 2:
1199 		return "one";
1200 	case 3:
1201 		return "src_colr";
1202 	case 4:
1203 		return "inv_src_colr";
1204 	case 5:
1205 		return "src_alpha";
1206 	case 6:
1207 		return "inv_src_alpha";
1208 	case 7:
1209 		return "dst_alpha";
1210 	case 8:
1211 		return "inv_dst_alpha";
1212 	case 9:
1213 		return "dst_colr";
1214 	case 10:
1215 		return "inv_dst_colr";
1216 	case 11:
1217 		return "src_alpha_sat";
1218 	case 12:
1219 		return "cnst_colr";
1220 	case 13:
1221 		return "inv_cnst_colr";
1222 	case 14:
1223 		return "cnst_alpha";
1224 	case 15:
1225 		return "inv_const_alpha";
1226 	}
1227 	return "";
1228 }
1229 
1230 static const char *
decode_tex_coord_mode(uint32_t mode)1231 decode_tex_coord_mode(uint32_t mode)
1232 {
1233 	switch (mode & 0x7) {
1234 	case 0:
1235 		return "wrap";
1236 	case 1:
1237 		return "mirror";
1238 	case 2:
1239 		return "clamp_edge";
1240 	case 3:
1241 		return "cube";
1242 	case 4:
1243 		return "clamp_border";
1244 	case 5:
1245 		return "mirror_once";
1246 	}
1247 	return "";
1248 }
1249 
1250 static const char *
decode_sample_filter(uint32_t mode)1251 decode_sample_filter(uint32_t mode)
1252 {
1253 	switch (mode & 0x7) {
1254 	case 0:
1255 		return "nearest";
1256 	case 1:
1257 		return "linear";
1258 	case 2:
1259 		return "anisotropic";
1260 	case 3:
1261 		return "4x4_1";
1262 	case 4:
1263 		return "4x4_2";
1264 	case 5:
1265 		return "4x4_flat";
1266 	case 6:
1267 		return "6x5_mono";
1268 	}
1269 	return "";
1270 }
1271 
1272 static int
decode_3d_1d(struct drm_intel_decode * ctx)1273 decode_3d_1d(struct drm_intel_decode *ctx)
1274 {
1275 	unsigned int len, i, c, idx, word, map, sampler, instr;
1276 	const char *format, *zformat, *type;
1277 	uint32_t opcode;
1278 	uint32_t *data = ctx->data;
1279 	uint32_t devid = ctx->devid;
1280 
1281 	struct {
1282 		uint32_t opcode;
1283 		int i830_only;
1284 		unsigned int min_len;
1285 		unsigned int max_len;
1286 		const char *name;
1287 	} opcodes_3d_1d[] = {
1288 		{ 0x86, 0, 4, 4, "3DSTATE_CHROMA_KEY" },
1289 		{ 0x88, 0, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
1290 		{ 0x99, 0, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
1291 		{ 0x9a, 0, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
1292 		{ 0x98, 0, 2, 2, "3DSTATE_DEFAULT_Z" },
1293 		{ 0x97, 0, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
1294 		{ 0x9d, 0, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
1295 		{ 0x9e, 0, 4, 4, "3DSTATE_MONO_FILTER" },
1296 		{ 0x89, 0, 4, 4, "3DSTATE_FOG_MODE" },
1297 		{ 0x8f, 0, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
1298 		{ 0x83, 0, 2, 2, "3DSTATE_SPAN_STIPPLE" },
1299 		{ 0x8c, 1, 2, 2, "3DSTATE_MAP_COORD_TRANSFORM_I830" },
1300 		{ 0x8b, 1, 2, 2, "3DSTATE_MAP_VERTEX_TRANSFORM_I830" },
1301 		{ 0x8d, 1, 3, 3, "3DSTATE_W_STATE_I830" },
1302 		{ 0x01, 1, 2, 2, "3DSTATE_COLOR_FACTOR_I830" },
1303 		{ 0x02, 1, 2, 2, "3DSTATE_MAP_COORD_SETBIND_I830"},
1304 	}, *opcode_3d_1d;
1305 
1306 	opcode = (data[0] & 0x00ff0000) >> 16;
1307 
1308 	switch (opcode) {
1309 	case 0x07:
1310 		/* This instruction is unusual.  A 0 length means just
1311 		 * 1 DWORD instead of 2.  The 0 length is specified in
1312 		 * one place to be unsupported, but stated to be
1313 		 * required in another, and 0 length LOAD_INDIRECTs
1314 		 * appear to cause no harm at least.
1315 		 */
1316 		instr_out(ctx, 0, "3DSTATE_LOAD_INDIRECT\n");
1317 		len = (data[0] & 0x000000ff) + 1;
1318 		i = 1;
1319 		if (data[0] & (0x01 << 8)) {
1320 			instr_out(ctx, i++, "SIS.0\n");
1321 			instr_out(ctx, i++, "SIS.1\n");
1322 		}
1323 		if (data[0] & (0x02 << 8)) {
1324 			instr_out(ctx, i++, "DIS.0\n");
1325 		}
1326 		if (data[0] & (0x04 << 8)) {
1327 			instr_out(ctx, i++, "SSB.0\n");
1328 			instr_out(ctx, i++, "SSB.1\n");
1329 		}
1330 		if (data[0] & (0x08 << 8)) {
1331 			instr_out(ctx, i++, "MSB.0\n");
1332 			instr_out(ctx, i++, "MSB.1\n");
1333 		}
1334 		if (data[0] & (0x10 << 8)) {
1335 			instr_out(ctx, i++, "PSP.0\n");
1336 			instr_out(ctx, i++, "PSP.1\n");
1337 		}
1338 		if (data[0] & (0x20 << 8)) {
1339 			instr_out(ctx, i++, "PSC.0\n");
1340 			instr_out(ctx, i++, "PSC.1\n");
1341 		}
1342 		if (len != i) {
1343 			fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
1344 			return len;
1345 		}
1346 		return len;
1347 	case 0x04:
1348 		instr_out(ctx, 0,
1349 			  "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1350 		len = (data[0] & 0x0000000f) + 2;
1351 		i = 1;
1352 		for (word = 0; word <= 8; word++) {
1353 			if (data[0] & (1 << (4 + word))) {
1354 				/* save vertex state for decode */
1355 				if (!IS_GEN2(devid)) {
1356 					int tex_num;
1357 
1358 					if (word == 2) {
1359 						saved_s2_set = 1;
1360 						saved_s2 = data[i];
1361 					}
1362 					if (word == 4) {
1363 						saved_s4_set = 1;
1364 						saved_s4 = data[i];
1365 					}
1366 
1367 					switch (word) {
1368 					case 0:
1369 						instr_out(ctx, i,
1370 							  "S0: vbo offset: 0x%08x%s\n",
1371 							  data[i] & (~1),
1372 							  data[i] & 1 ?
1373 							  ", auto cache invalidate disabled"
1374 							  : "");
1375 						break;
1376 					case 1:
1377 						instr_out(ctx, i,
1378 							  "S1: vertex width: %i, vertex pitch: %i\n",
1379 							  (data[i] >> 24) &
1380 							  0x3f,
1381 							  (data[i] >> 16) &
1382 							  0x3f);
1383 						break;
1384 					case 2:
1385 						instr_out(ctx, i,
1386 							  "S2: texcoord formats: ");
1387 						for (tex_num = 0;
1388 						     tex_num < 8; tex_num++) {
1389 							switch ((data[i] >>
1390 								 tex_num *
1391 								 4) & 0xf) {
1392 							case 0:
1393 								fprintf(out,
1394 									"%i=2D ",
1395 									tex_num);
1396 								break;
1397 							case 1:
1398 								fprintf(out,
1399 									"%i=3D ",
1400 									tex_num);
1401 								break;
1402 							case 2:
1403 								fprintf(out,
1404 									"%i=4D ",
1405 									tex_num);
1406 								break;
1407 							case 3:
1408 								fprintf(out,
1409 									"%i=1D ",
1410 									tex_num);
1411 								break;
1412 							case 4:
1413 								fprintf(out,
1414 									"%i=2D_16 ",
1415 									tex_num);
1416 								break;
1417 							case 5:
1418 								fprintf(out,
1419 									"%i=4D_16 ",
1420 									tex_num);
1421 								break;
1422 							case 0xf:
1423 								fprintf(out,
1424 									"%i=NP ",
1425 									tex_num);
1426 								break;
1427 							}
1428 						}
1429 						fprintf(out, "\n");
1430 
1431 						break;
1432 					case 3:
1433 						instr_out(ctx, i,
1434 							  "S3: not documented\n");
1435 						break;
1436 					case 4:
1437 						{
1438 							const char *cullmode = "";
1439 							const char *vfmt_xyzw = "";
1440 							switch ((data[i] >> 13)
1441 								& 0x3) {
1442 							case 0:
1443 								cullmode =
1444 								    "both";
1445 								break;
1446 							case 1:
1447 								cullmode =
1448 								    "none";
1449 								break;
1450 							case 2:
1451 								cullmode = "cw";
1452 								break;
1453 							case 3:
1454 								cullmode =
1455 								    "ccw";
1456 								break;
1457 							}
1458 							switch (data[i] &
1459 								(7 << 6 | 1 <<
1460 								 2)) {
1461 							case 1 << 6:
1462 								vfmt_xyzw =
1463 								    "XYZ,";
1464 								break;
1465 							case 2 << 6:
1466 								vfmt_xyzw =
1467 								    "XYZW,";
1468 								break;
1469 							case 3 << 6:
1470 								vfmt_xyzw =
1471 								    "XY,";
1472 								break;
1473 							case 4 << 6:
1474 								vfmt_xyzw =
1475 								    "XYW,";
1476 								break;
1477 							case 1 << 6 | 1 << 2:
1478 								vfmt_xyzw =
1479 								    "XYZF,";
1480 								break;
1481 							case 2 << 6 | 1 << 2:
1482 								vfmt_xyzw =
1483 								    "XYZWF,";
1484 								break;
1485 							case 3 << 6 | 1 << 2:
1486 								vfmt_xyzw =
1487 								    "XYF,";
1488 								break;
1489 							case 4 << 6 | 1 << 2:
1490 								vfmt_xyzw =
1491 								    "XYWF,";
1492 								break;
1493 							}
1494 							instr_out(ctx, i,
1495 								  "S4: point_width=%i, line_width=%.1f,"
1496 								  "%s%s%s%s%s cullmode=%s, vfmt=%s%s%s%s%s%s "
1497 								  "%s%s%s%s%s\n",
1498 								  (data[i] >>
1499 								   23) & 0x1ff,
1500 								  ((data[i] >>
1501 								    19) & 0xf) /
1502 								  2.0,
1503 								  data[i] & (0xf
1504 									     <<
1505 									     15)
1506 								  ?
1507 								  " flatshade="
1508 								  : "",
1509 								  data[i] & (1
1510 									     <<
1511 									     18)
1512 								  ? "Alpha," :
1513 								  "",
1514 								  data[i] & (1
1515 									     <<
1516 									     17)
1517 								  ? "Fog," : "",
1518 								  data[i] & (1
1519 									     <<
1520 									     16)
1521 								  ? "Specular,"
1522 								  : "",
1523 								  data[i] & (1
1524 									     <<
1525 									     15)
1526 								  ? "Color," :
1527 								  "", cullmode,
1528 								  data[i] & (1
1529 									     <<
1530 									     12)
1531 								  ?
1532 								  "PointWidth,"
1533 								  : "",
1534 								  data[i] & (1
1535 									     <<
1536 									     11)
1537 								  ? "SpecFog," :
1538 								  "",
1539 								  data[i] & (1
1540 									     <<
1541 									     10)
1542 								  ? "Color," :
1543 								  "",
1544 								  data[i] & (1
1545 									     <<
1546 									     9)
1547 								  ? "DepthOfs,"
1548 								  : "",
1549 								  vfmt_xyzw,
1550 								  data[i] & (1
1551 									     <<
1552 									     9)
1553 								  ? "FogParam,"
1554 								  : "",
1555 								  data[i] & (1
1556 									     <<
1557 									     5)
1558 								  ?
1559 								  "force default diffuse, "
1560 								  : "",
1561 								  data[i] & (1
1562 									     <<
1563 									     4)
1564 								  ?
1565 								  "force default specular, "
1566 								  : "",
1567 								  data[i] & (1
1568 									     <<
1569 									     3)
1570 								  ?
1571 								  "local depth ofs enable, "
1572 								  : "",
1573 								  data[i] & (1
1574 									     <<
1575 									     1)
1576 								  ?
1577 								  "point sprite enable, "
1578 								  : "",
1579 								  data[i] & (1
1580 									     <<
1581 									     0)
1582 								  ?
1583 								  "line AA enable, "
1584 								  : "");
1585 							break;
1586 						}
1587 					case 5:
1588 						{
1589 							instr_out(ctx, i,
1590 								  "S5:%s%s%s%s%s"
1591 								  "%s%s%s%s stencil_ref=0x%x, stencil_test=%s, "
1592 								  "stencil_fail=%s, stencil_pass_z_fail=%s, "
1593 								  "stencil_pass_z_pass=%s, %s%s%s%s\n",
1594 								  data[i] & (0xf
1595 									     <<
1596 									     28)
1597 								  ?
1598 								  " write_disable="
1599 								  : "",
1600 								  data[i] & (1
1601 									     <<
1602 									     31)
1603 								  ? "Alpha," :
1604 								  "",
1605 								  data[i] & (1
1606 									     <<
1607 									     30)
1608 								  ? "Red," : "",
1609 								  data[i] & (1
1610 									     <<
1611 									     29)
1612 								  ? "Green," :
1613 								  "",
1614 								  data[i] & (1
1615 									     <<
1616 									     28)
1617 								  ? "Blue," :
1618 								  "",
1619 								  data[i] & (1
1620 									     <<
1621 									     27)
1622 								  ?
1623 								  " force default point size,"
1624 								  : "",
1625 								  data[i] & (1
1626 									     <<
1627 									     26)
1628 								  ?
1629 								  " last pixel enable,"
1630 								  : "",
1631 								  data[i] & (1
1632 									     <<
1633 									     25)
1634 								  ?
1635 								  " global depth ofs enable,"
1636 								  : "",
1637 								  data[i] & (1
1638 									     <<
1639 									     24)
1640 								  ?
1641 								  " fog enable,"
1642 								  : "",
1643 								  (data[i] >>
1644 								   16) & 0xff,
1645 								  decode_compare_func
1646 								  (data[i] >>
1647 								   13),
1648 								  decode_stencil_op
1649 								  (data[i] >>
1650 								   10),
1651 								  decode_stencil_op
1652 								  (data[i] >>
1653 								   7),
1654 								  decode_stencil_op
1655 								  (data[i] >>
1656 								   4),
1657 								  data[i] & (1
1658 									     <<
1659 									     3)
1660 								  ?
1661 								  "stencil write enable, "
1662 								  : "",
1663 								  data[i] & (1
1664 									     <<
1665 									     2)
1666 								  ?
1667 								  "stencil test enable, "
1668 								  : "",
1669 								  data[i] & (1
1670 									     <<
1671 									     1)
1672 								  ?
1673 								  "color dither enable, "
1674 								  : "",
1675 								  data[i] & (1
1676 									     <<
1677 									     0)
1678 								  ?
1679 								  "logicop enable, "
1680 								  : "");
1681 						}
1682 						break;
1683 					case 6:
1684 						instr_out(ctx, i,
1685 							  "S6: %salpha_test=%s, alpha_ref=0x%x, "
1686 							  "depth_test=%s, %ssrc_blnd_fct=%s, dst_blnd_fct=%s, "
1687 							  "%s%stristrip_provoking_vertex=%i\n",
1688 							  data[i] & (1 << 31) ?
1689 							  "alpha test enable, "
1690 							  : "",
1691 							  decode_compare_func
1692 							  (data[i] >> 28),
1693 							  data[i] & (0xff <<
1694 								     20),
1695 							  decode_compare_func
1696 							  (data[i] >> 16),
1697 							  data[i] & (1 << 15) ?
1698 							  "cbuf blend enable, "
1699 							  : "",
1700 							  decode_blend_fact(data
1701 									    [i]
1702 									    >>
1703 									    8),
1704 							  decode_blend_fact(data
1705 									    [i]
1706 									    >>
1707 									    4),
1708 							  data[i] & (1 << 3) ?
1709 							  "depth write enable, "
1710 							  : "",
1711 							  data[i] & (1 << 2) ?
1712 							  "cbuf write enable, "
1713 							  : "",
1714 							  data[i] & (0x3));
1715 						break;
1716 					case 7:
1717 						instr_out(ctx, i,
1718 							  "S7: depth offset constant: 0x%08x\n",
1719 							  data[i]);
1720 						break;
1721 					}
1722 				} else {
1723 					instr_out(ctx, i,
1724 						  "S%d: 0x%08x\n", word, data[i]);
1725 				}
1726 				i++;
1727 			}
1728 		}
1729 		if (len != i) {
1730 			fprintf(out,
1731 				"Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1732 		}
1733 		return len;
1734 	case 0x03:
1735 		instr_out(ctx, 0,
1736 			  "3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1737 		len = (data[0] & 0x0000000f) + 2;
1738 		i = 1;
1739 		for (word = 6; word <= 14; word++) {
1740 			if (data[0] & (1 << word)) {
1741 				if (word == 6)
1742 					instr_out(ctx, i++,
1743 						  "TBCF\n");
1744 				else if (word >= 7 && word <= 10) {
1745 					instr_out(ctx, i++,
1746 						  "TB%dC\n", word - 7);
1747 					instr_out(ctx, i++,
1748 						  "TB%dA\n", word - 7);
1749 				} else if (word >= 11 && word <= 14) {
1750 					instr_out(ctx, i,
1751 						  "TM%dS0: offset=0x%08x, %s\n",
1752 						  word - 11,
1753 						  data[i] & 0xfffffffe,
1754 						  data[i] & 1 ? "use fence" :
1755 						  "");
1756 					i++;
1757 					instr_out(ctx, i,
1758 						  "TM%dS1: height=%i, width=%i, %s\n",
1759 						  word - 11, data[i] >> 21,
1760 						  (data[i] >> 10) & 0x3ff,
1761 						  data[i] & 2 ? (data[i] & 1 ?
1762 								 "y-tiled" :
1763 								 "x-tiled") :
1764 						  "");
1765 					i++;
1766 					instr_out(ctx, i,
1767 						  "TM%dS2: pitch=%i, \n",
1768 						  word - 11,
1769 						  ((data[i] >> 21) + 1) * 4);
1770 					i++;
1771 					instr_out(ctx, i++,
1772 						  "TM%dS3\n", word - 11);
1773 					instr_out(ctx, i++,
1774 						  "TM%dS4: dflt color\n",
1775 						  word - 11);
1776 				}
1777 			}
1778 		}
1779 		if (len != i) {
1780 			fprintf(out,
1781 				"Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1782 		}
1783 		return len;
1784 	case 0x00:
1785 		instr_out(ctx, 0, "3DSTATE_MAP_STATE\n");
1786 		len = (data[0] & 0x0000003f) + 2;
1787 		instr_out(ctx, 1, "mask\n");
1788 
1789 		i = 2;
1790 		for (map = 0; map <= 15; map++) {
1791 			if (data[1] & (1 << map)) {
1792 				int width, height, pitch, dword;
1793 				const char *tiling;
1794 
1795 				dword = data[i];
1796 				instr_out(ctx, i++,
1797 					  "map %d MS2 %s%s%s\n", map,
1798 					  dword & (1 << 31) ?
1799 					  "untrusted surface, " : "",
1800 					  dword & (1 << 1) ?
1801 					  "vertical line stride enable, " : "",
1802 					  dword & (1 << 0) ?
1803 					  "vertical ofs enable, " : "");
1804 
1805 				dword = data[i];
1806 				width = ((dword >> 10) & ((1 << 11) - 1)) + 1;
1807 				height = ((dword >> 21) & ((1 << 11) - 1)) + 1;
1808 
1809 				tiling = "none";
1810 				if (dword & (1 << 2))
1811 					tiling = "fenced";
1812 				else if (dword & (1 << 1))
1813 					tiling = dword & (1 << 0) ? "Y" : "X";
1814 				type = " BAD";
1815 				format = "BAD";
1816 				switch ((dword >> 7) & 0x7) {
1817 				case 1:
1818 					type = "8b";
1819 					switch ((dword >> 3) & 0xf) {
1820 					case 0:
1821 						format = "I";
1822 						break;
1823 					case 1:
1824 						format = "L";
1825 						break;
1826 					case 4:
1827 						format = "A";
1828 						break;
1829 					case 5:
1830 						format = " mono";
1831 						break;
1832 					}
1833 					break;
1834 				case 2:
1835 					type = "16b";
1836 					switch ((dword >> 3) & 0xf) {
1837 					case 0:
1838 						format = " rgb565";
1839 						break;
1840 					case 1:
1841 						format = " argb1555";
1842 						break;
1843 					case 2:
1844 						format = " argb4444";
1845 						break;
1846 					case 5:
1847 						format = " ay88";
1848 						break;
1849 					case 6:
1850 						format = " bump655";
1851 						break;
1852 					case 7:
1853 						format = "I";
1854 						break;
1855 					case 8:
1856 						format = "L";
1857 						break;
1858 					case 9:
1859 						format = "A";
1860 						break;
1861 					}
1862 					break;
1863 				case 3:
1864 					type = "32b";
1865 					switch ((dword >> 3) & 0xf) {
1866 					case 0:
1867 						format = " argb8888";
1868 						break;
1869 					case 1:
1870 						format = " abgr8888";
1871 						break;
1872 					case 2:
1873 						format = " xrgb8888";
1874 						break;
1875 					case 3:
1876 						format = " xbgr8888";
1877 						break;
1878 					case 4:
1879 						format = " qwvu8888";
1880 						break;
1881 					case 5:
1882 						format = " axvu8888";
1883 						break;
1884 					case 6:
1885 						format = " lxvu8888";
1886 						break;
1887 					case 7:
1888 						format = " xlvu8888";
1889 						break;
1890 					case 8:
1891 						format = " argb2101010";
1892 						break;
1893 					case 9:
1894 						format = " abgr2101010";
1895 						break;
1896 					case 10:
1897 						format = " awvu2101010";
1898 						break;
1899 					case 11:
1900 						format = " gr1616";
1901 						break;
1902 					case 12:
1903 						format = " vu1616";
1904 						break;
1905 					case 13:
1906 						format = " xI824";
1907 						break;
1908 					case 14:
1909 						format = " xA824";
1910 						break;
1911 					case 15:
1912 						format = " xL824";
1913 						break;
1914 					}
1915 					break;
1916 				case 5:
1917 					type = "422";
1918 					switch ((dword >> 3) & 0xf) {
1919 					case 0:
1920 						format = " yuv_swapy";
1921 						break;
1922 					case 1:
1923 						format = " yuv";
1924 						break;
1925 					case 2:
1926 						format = " yuv_swapuv";
1927 						break;
1928 					case 3:
1929 						format = " yuv_swapuvy";
1930 						break;
1931 					}
1932 					break;
1933 				case 6:
1934 					type = "compressed";
1935 					switch ((dword >> 3) & 0x7) {
1936 					case 0:
1937 						format = " dxt1";
1938 						break;
1939 					case 1:
1940 						format = " dxt2_3";
1941 						break;
1942 					case 2:
1943 						format = " dxt4_5";
1944 						break;
1945 					case 3:
1946 						format = " fxt1";
1947 						break;
1948 					case 4:
1949 						format = " dxt1_rb";
1950 						break;
1951 					}
1952 					break;
1953 				case 7:
1954 					type = "4b indexed";
1955 					switch ((dword >> 3) & 0xf) {
1956 					case 7:
1957 						format = " argb8888";
1958 						break;
1959 					}
1960 					break;
1961 				}
1962 				dword = data[i];
1963 				instr_out(ctx, i++,
1964 					  "map %d MS3 [width=%d, height=%d, format=%s%s, tiling=%s%s]\n",
1965 					  map, width, height, type, format,
1966 					  tiling,
1967 					  dword & (1 << 9) ? " palette select" :
1968 					  "");
1969 
1970 				dword = data[i];
1971 				pitch =
1972 				    4 * (((dword >> 21) & ((1 << 11) - 1)) + 1);
1973 				instr_out(ctx, i++,
1974 					  "map %d MS4 [pitch=%d, max_lod=%i, vol_depth=%i, cube_face_ena=%x, %s]\n",
1975 					  map, pitch, (dword >> 9) & 0x3f,
1976 					  dword & 0xff, (dword >> 15) & 0x3f,
1977 					  dword & (1 << 8) ? "miplayout legacy"
1978 					  : "miplayout right");
1979 			}
1980 		}
1981 		if (len != i) {
1982 			fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
1983 			return len;
1984 		}
1985 		return len;
1986 	case 0x06:
1987 		instr_out(ctx, 0,
1988 			  "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1989 		len = (data[0] & 0x000000ff) + 2;
1990 
1991 		i = 2;
1992 		for (c = 0; c <= 31; c++) {
1993 			if (data[1] & (1 << c)) {
1994 				instr_out(ctx, i, "C%d.X = %f\n", c,
1995 					  int_as_float(data[i]));
1996 				i++;
1997 				instr_out(ctx, i, "C%d.Y = %f\n",
1998 					  c, int_as_float(data[i]));
1999 				i++;
2000 				instr_out(ctx, i, "C%d.Z = %f\n",
2001 					  c, int_as_float(data[i]));
2002 				i++;
2003 				instr_out(ctx, i, "C%d.W = %f\n",
2004 					  c, int_as_float(data[i]));
2005 				i++;
2006 			}
2007 		}
2008 		if (len != i) {
2009 			fprintf(out,
2010 				"Bad count in 3DSTATE_PIXEL_SHADER_CONSTANTS\n");
2011 		}
2012 		return len;
2013 	case 0x05:
2014 		instr_out(ctx, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
2015 		len = (data[0] & 0x000000ff) + 2;
2016 		if ((len - 1) % 3 != 0 || len > 370) {
2017 			fprintf(out,
2018 				"Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
2019 		}
2020 		i = 1;
2021 		for (instr = 0; instr < (len - 1) / 3; instr++) {
2022 			char instr_prefix[10];
2023 
2024 			sprintf(instr_prefix, "PS%03d", instr);
2025 			i915_decode_instruction(ctx, i,
2026 						instr_prefix);
2027 			i += 3;
2028 		}
2029 		return len;
2030 	case 0x01:
2031 		if (IS_GEN2(devid))
2032 			break;
2033 		instr_out(ctx, 0, "3DSTATE_SAMPLER_STATE\n");
2034 		instr_out(ctx, 1, "mask\n");
2035 		len = (data[0] & 0x0000003f) + 2;
2036 		i = 2;
2037 		for (sampler = 0; sampler <= 15; sampler++) {
2038 			if (data[1] & (1 << sampler)) {
2039 				uint32_t dword;
2040 				const char *mip_filter = "";
2041 
2042 				dword = data[i];
2043 				switch ((dword >> 20) & 0x3) {
2044 				case 0:
2045 					mip_filter = "none";
2046 					break;
2047 				case 1:
2048 					mip_filter = "nearest";
2049 					break;
2050 				case 3:
2051 					mip_filter = "linear";
2052 					break;
2053 				}
2054 				instr_out(ctx, i++,
2055 					  "sampler %d SS2:%s%s%s "
2056 					  "base_mip_level=%i, mip_filter=%s, mag_filter=%s, min_filter=%s "
2057 					  "lod_bias=%.2f,%s max_aniso=%i, shadow_func=%s\n",
2058 					  sampler,
2059 					  dword & (1 << 31) ? " reverse gamma,"
2060 					  : "",
2061 					  dword & (1 << 30) ? " packed2planar,"
2062 					  : "",
2063 					  dword & (1 << 29) ?
2064 					  " colorspace conversion," : "",
2065 					  (dword >> 22) & 0x1f, mip_filter,
2066 					  decode_sample_filter(dword >> 17),
2067 					  decode_sample_filter(dword >> 14),
2068 					  ((dword >> 5) & 0x1ff) / (0x10 * 1.0),
2069 					  dword & (1 << 4) ? " shadow," : "",
2070 					  dword & (1 << 3) ? 4 : 2,
2071 					  decode_compare_func(dword));
2072 				dword = data[i];
2073 				instr_out(ctx, i++,
2074 					  "sampler %d SS3: min_lod=%.2f,%s "
2075 					  "tcmode_x=%s, tcmode_y=%s, tcmode_z=%s,%s texmap_idx=%i,%s\n",
2076 					  sampler,
2077 					  ((dword >> 24) & 0xff) / (0x10 * 1.0),
2078 					  dword & (1 << 17) ?
2079 					  " kill pixel enable," : "",
2080 					  decode_tex_coord_mode(dword >> 12),
2081 					  decode_tex_coord_mode(dword >> 9),
2082 					  decode_tex_coord_mode(dword >> 6),
2083 					  dword & (1 << 5) ?
2084 					  " normalized coords," : "",
2085 					  (dword >> 1) & 0xf,
2086 					  dword & (1 << 0) ? " deinterlacer," :
2087 					  "");
2088 				dword = data[i];
2089 				instr_out(ctx, i++,
2090 					  "sampler %d SS4: border color\n",
2091 					  sampler);
2092 			}
2093 		}
2094 		if (len != i) {
2095 			fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
2096 		}
2097 		return len;
2098 	case 0x85:
2099 		len = (data[0] & 0x0000000f) + 2;
2100 
2101 		if (len != 2)
2102 			fprintf(out,
2103 				"Bad count in 3DSTATE_DEST_BUFFER_VARIABLES\n");
2104 
2105 		instr_out(ctx, 0,
2106 			  "3DSTATE_DEST_BUFFER_VARIABLES\n");
2107 
2108 		switch ((data[1] >> 8) & 0xf) {
2109 		case 0x0:
2110 			format = "g8";
2111 			break;
2112 		case 0x1:
2113 			format = "x1r5g5b5";
2114 			break;
2115 		case 0x2:
2116 			format = "r5g6b5";
2117 			break;
2118 		case 0x3:
2119 			format = "a8r8g8b8";
2120 			break;
2121 		case 0x4:
2122 			format = "ycrcb_swapy";
2123 			break;
2124 		case 0x5:
2125 			format = "ycrcb_normal";
2126 			break;
2127 		case 0x6:
2128 			format = "ycrcb_swapuv";
2129 			break;
2130 		case 0x7:
2131 			format = "ycrcb_swapuvy";
2132 			break;
2133 		case 0x8:
2134 			format = "a4r4g4b4";
2135 			break;
2136 		case 0x9:
2137 			format = "a1r5g5b5";
2138 			break;
2139 		case 0xa:
2140 			format = "a2r10g10b10";
2141 			break;
2142 		default:
2143 			format = "BAD";
2144 			break;
2145 		}
2146 		switch ((data[1] >> 2) & 0x3) {
2147 		case 0x0:
2148 			zformat = "u16";
2149 			break;
2150 		case 0x1:
2151 			zformat = "f16";
2152 			break;
2153 		case 0x2:
2154 			zformat = "u24x8";
2155 			break;
2156 		default:
2157 			zformat = "BAD";
2158 			break;
2159 		}
2160 		instr_out(ctx, 1,
2161 			  "%s format, %s depth format, early Z %sabled\n",
2162 			  format, zformat,
2163 			  (data[1] & (1 << 31)) ? "en" : "dis");
2164 		return len;
2165 
2166 	case 0x8e:
2167 		{
2168 			const char *name, *tiling;
2169 
2170 			len = (data[0] & 0x0000000f) + 2;
2171 			if (len != 3)
2172 				fprintf(out,
2173 					"Bad count in 3DSTATE_BUFFER_INFO\n");
2174 
2175 			switch ((data[1] >> 24) & 0x7) {
2176 			case 0x3:
2177 				name = "color";
2178 				break;
2179 			case 0x7:
2180 				name = "depth";
2181 				break;
2182 			default:
2183 				name = "unknown";
2184 				break;
2185 			}
2186 
2187 			tiling = "none";
2188 			if (data[1] & (1 << 23))
2189 				tiling = "fenced";
2190 			else if (data[1] & (1 << 22))
2191 				tiling = data[1] & (1 << 21) ? "Y" : "X";
2192 
2193 			instr_out(ctx, 0, "3DSTATE_BUFFER_INFO\n");
2194 			instr_out(ctx, 1,
2195 				  "%s, tiling = %s, pitch=%d\n", name, tiling,
2196 				  data[1] & 0xffff);
2197 
2198 			instr_out(ctx, 2, "address\n");
2199 			return len;
2200 		}
2201 	case 0x81:
2202 		len = (data[0] & 0x0000000f) + 2;
2203 
2204 		if (len != 3)
2205 			fprintf(out,
2206 				"Bad count in 3DSTATE_SCISSOR_RECTANGLE\n");
2207 
2208 		instr_out(ctx, 0, "3DSTATE_SCISSOR_RECTANGLE\n");
2209 		instr_out(ctx, 1, "(%d,%d)\n",
2210 			  data[1] & 0xffff, data[1] >> 16);
2211 		instr_out(ctx, 2, "(%d,%d)\n",
2212 			  data[2] & 0xffff, data[2] >> 16);
2213 
2214 		return len;
2215 	case 0x80:
2216 		len = (data[0] & 0x0000000f) + 2;
2217 
2218 		if (len != 5)
2219 			fprintf(out,
2220 				"Bad count in 3DSTATE_DRAWING_RECTANGLE\n");
2221 
2222 		instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
2223 		instr_out(ctx, 1, "%s\n",
2224 			  data[1] & (1 << 30) ? "depth ofs disabled " : "");
2225 		instr_out(ctx, 2, "(%d,%d)\n",
2226 			  data[2] & 0xffff, data[2] >> 16);
2227 		instr_out(ctx, 3, "(%d,%d)\n",
2228 			  data[3] & 0xffff, data[3] >> 16);
2229 		instr_out(ctx, 4, "(%d,%d)\n",
2230 			  data[4] & 0xffff, data[4] >> 16);
2231 
2232 		return len;
2233 	case 0x9c:
2234 		len = (data[0] & 0x0000000f) + 2;
2235 
2236 		if (len != 7)
2237 			fprintf(out, "Bad count in 3DSTATE_CLEAR_PARAMETERS\n");
2238 
2239 		instr_out(ctx, 0, "3DSTATE_CLEAR_PARAMETERS\n");
2240 		instr_out(ctx, 1, "prim_type=%s, clear=%s%s%s\n",
2241 			  data[1] & (1 << 16) ? "CLEAR_RECT" : "ZONE_INIT",
2242 			  data[1] & (1 << 2) ? "color," : "",
2243 			  data[1] & (1 << 1) ? "depth," : "",
2244 			  data[1] & (1 << 0) ? "stencil," : "");
2245 		instr_out(ctx, 2, "clear color\n");
2246 		instr_out(ctx, 3, "clear depth/stencil\n");
2247 		instr_out(ctx, 4, "color value (rgba8888)\n");
2248 		instr_out(ctx, 5, "depth value %f\n",
2249 			  int_as_float(data[5]));
2250 		instr_out(ctx, 6, "clear stencil\n");
2251 		return len;
2252 	}
2253 
2254 	for (idx = 0; idx < ARRAY_SIZE(opcodes_3d_1d); idx++) {
2255 		opcode_3d_1d = &opcodes_3d_1d[idx];
2256 		if (opcode_3d_1d->i830_only && !IS_GEN2(devid))
2257 			continue;
2258 
2259 		if (((data[0] & 0x00ff0000) >> 16) == opcode_3d_1d->opcode) {
2260 			len = 1;
2261 
2262 			instr_out(ctx, 0, "%s\n",
2263 				  opcode_3d_1d->name);
2264 			if (opcode_3d_1d->max_len > 1) {
2265 				len = (data[0] & 0x0000ffff) + 2;
2266 				if (len < opcode_3d_1d->min_len ||
2267 				    len > opcode_3d_1d->max_len) {
2268 					fprintf(out, "Bad count in %s\n",
2269 						opcode_3d_1d->name);
2270 				}
2271 			}
2272 
2273 			for (i = 1; i < len; i++) {
2274 				instr_out(ctx, i, "dword %d\n", i);
2275 			}
2276 
2277 			return len;
2278 		}
2279 	}
2280 
2281 	instr_out(ctx, 0, "3D UNKNOWN: 3d_1d opcode = 0x%x\n",
2282 		  opcode);
2283 	return 1;
2284 }
2285 
2286 static int
decode_3d_primitive(struct drm_intel_decode * ctx)2287 decode_3d_primitive(struct drm_intel_decode *ctx)
2288 {
2289 	uint32_t *data = ctx->data;
2290 	uint32_t count = ctx->count;
2291 	char immediate = (data[0] & (1 << 23)) == 0;
2292 	unsigned int len, i, j, ret;
2293 	const char *primtype;
2294 	int original_s2 = saved_s2;
2295 	int original_s4 = saved_s4;
2296 
2297 	switch ((data[0] >> 18) & 0xf) {
2298 	case 0x0:
2299 		primtype = "TRILIST";
2300 		break;
2301 	case 0x1:
2302 		primtype = "TRISTRIP";
2303 		break;
2304 	case 0x2:
2305 		primtype = "TRISTRIP_REVERSE";
2306 		break;
2307 	case 0x3:
2308 		primtype = "TRIFAN";
2309 		break;
2310 	case 0x4:
2311 		primtype = "POLYGON";
2312 		break;
2313 	case 0x5:
2314 		primtype = "LINELIST";
2315 		break;
2316 	case 0x6:
2317 		primtype = "LINESTRIP";
2318 		break;
2319 	case 0x7:
2320 		primtype = "RECTLIST";
2321 		break;
2322 	case 0x8:
2323 		primtype = "POINTLIST";
2324 		break;
2325 	case 0x9:
2326 		primtype = "DIB";
2327 		break;
2328 	case 0xa:
2329 		primtype = "CLEAR_RECT";
2330 		saved_s4 = 3 << 6;
2331 		saved_s2 = ~0;
2332 		break;
2333 	default:
2334 		primtype = "unknown";
2335 		break;
2336 	}
2337 
2338 	/* XXX: 3DPRIM_DIB not supported */
2339 	if (immediate) {
2340 		len = (data[0] & 0x0003ffff) + 2;
2341 		instr_out(ctx, 0, "3DPRIMITIVE inline %s\n",
2342 			  primtype);
2343 		if (count < len)
2344 			BUFFER_FAIL(count, len, "3DPRIMITIVE inline");
2345 		if (!saved_s2_set || !saved_s4_set) {
2346 			fprintf(out, "unknown vertex format\n");
2347 			for (i = 1; i < len; i++) {
2348 				instr_out(ctx, i,
2349 					  "           vertex data (%f float)\n",
2350 					  int_as_float(data[i]));
2351 			}
2352 		} else {
2353 			unsigned int vertex = 0;
2354 			for (i = 1; i < len;) {
2355 				unsigned int tc;
2356 
2357 #define VERTEX_OUT(fmt, ...) do {					\
2358     if (i < len)							\
2359 	instr_out(ctx, i, " V%d."fmt"\n", vertex, __VA_ARGS__); \
2360     else								\
2361 	fprintf(out, " missing data in V%d\n", vertex);			\
2362     i++;								\
2363 } while (0)
2364 
2365 				VERTEX_OUT("X = %f", int_as_float(data[i]));
2366 				VERTEX_OUT("Y = %f", int_as_float(data[i]));
2367 				switch (saved_s4 >> 6 & 0x7) {
2368 				case 0x1:
2369 					VERTEX_OUT("Z = %f",
2370 						   int_as_float(data[i]));
2371 					break;
2372 				case 0x2:
2373 					VERTEX_OUT("Z = %f",
2374 						   int_as_float(data[i]));
2375 					VERTEX_OUT("W = %f",
2376 						   int_as_float(data[i]));
2377 					break;
2378 				case 0x3:
2379 					break;
2380 				case 0x4:
2381 					VERTEX_OUT("W = %f",
2382 						   int_as_float(data[i]));
2383 					break;
2384 				default:
2385 					fprintf(out, "bad S4 position mask\n");
2386 				}
2387 
2388 				if (saved_s4 & (1 << 10)) {
2389 					VERTEX_OUT
2390 					    ("color = (A=0x%02x, R=0x%02x, G=0x%02x, "
2391 					     "B=0x%02x)", data[i] >> 24,
2392 					     (data[i] >> 16) & 0xff,
2393 					     (data[i] >> 8) & 0xff,
2394 					     data[i] & 0xff);
2395 				}
2396 				if (saved_s4 & (1 << 11)) {
2397 					VERTEX_OUT
2398 					    ("spec = (A=0x%02x, R=0x%02x, G=0x%02x, "
2399 					     "B=0x%02x)", data[i] >> 24,
2400 					     (data[i] >> 16) & 0xff,
2401 					     (data[i] >> 8) & 0xff,
2402 					     data[i] & 0xff);
2403 				}
2404 				if (saved_s4 & (1 << 12))
2405 					VERTEX_OUT("width = 0x%08x)", data[i]);
2406 
2407 				for (tc = 0; tc <= 7; tc++) {
2408 					switch ((saved_s2 >> (tc * 4)) & 0xf) {
2409 					case 0x0:
2410 						VERTEX_OUT("T%d.X = %f", tc,
2411 							   int_as_float(data
2412 									[i]));
2413 						VERTEX_OUT("T%d.Y = %f", tc,
2414 							   int_as_float(data
2415 									[i]));
2416 						break;
2417 					case 0x1:
2418 						VERTEX_OUT("T%d.X = %f", tc,
2419 							   int_as_float(data
2420 									[i]));
2421 						VERTEX_OUT("T%d.Y = %f", tc,
2422 							   int_as_float(data
2423 									[i]));
2424 						VERTEX_OUT("T%d.Z = %f", tc,
2425 							   int_as_float(data
2426 									[i]));
2427 						break;
2428 					case 0x2:
2429 						VERTEX_OUT("T%d.X = %f", tc,
2430 							   int_as_float(data
2431 									[i]));
2432 						VERTEX_OUT("T%d.Y = %f", tc,
2433 							   int_as_float(data
2434 									[i]));
2435 						VERTEX_OUT("T%d.Z = %f", tc,
2436 							   int_as_float(data
2437 									[i]));
2438 						VERTEX_OUT("T%d.W = %f", tc,
2439 							   int_as_float(data
2440 									[i]));
2441 						break;
2442 					case 0x3:
2443 						VERTEX_OUT("T%d.X = %f", tc,
2444 							   int_as_float(data
2445 									[i]));
2446 						break;
2447 					case 0x4:
2448 						VERTEX_OUT
2449 						    ("T%d.XY = 0x%08x half-float",
2450 						     tc, data[i]);
2451 						break;
2452 					case 0x5:
2453 						VERTEX_OUT
2454 						    ("T%d.XY = 0x%08x half-float",
2455 						     tc, data[i]);
2456 						VERTEX_OUT
2457 						    ("T%d.ZW = 0x%08x half-float",
2458 						     tc, data[i]);
2459 						break;
2460 					case 0xf:
2461 						break;
2462 					default:
2463 						fprintf(out,
2464 							"bad S2.T%d format\n",
2465 							tc);
2466 					}
2467 				}
2468 				vertex++;
2469 			}
2470 		}
2471 
2472 		ret = len;
2473 	} else {
2474 		/* indirect vertices */
2475 		len = data[0] & 0x0000ffff;	/* index count */
2476 		if (data[0] & (1 << 17)) {
2477 			/* random vertex access */
2478 			if (count < (len + 1) / 2 + 1) {
2479 				BUFFER_FAIL(count, (len + 1) / 2 + 1,
2480 					    "3DPRIMITIVE random indirect");
2481 			}
2482 			instr_out(ctx, 0,
2483 				  "3DPRIMITIVE random indirect %s (%d)\n",
2484 				  primtype, len);
2485 			if (len == 0) {
2486 				/* vertex indices continue until 0xffff is
2487 				 * found
2488 				 */
2489 				for (i = 1; i < count; i++) {
2490 					if ((data[i] & 0xffff) == 0xffff) {
2491 						instr_out(ctx, i,
2492 							  "    indices: (terminator)\n");
2493 						ret = i;
2494 						goto out;
2495 					} else if ((data[i] >> 16) == 0xffff) {
2496 						instr_out(ctx, i,
2497 							  "    indices: 0x%04x, (terminator)\n",
2498 							  data[i] & 0xffff);
2499 						ret = i;
2500 						goto out;
2501 					} else {
2502 						instr_out(ctx, i,
2503 							  "    indices: 0x%04x, 0x%04x\n",
2504 							  data[i] & 0xffff,
2505 							  data[i] >> 16);
2506 					}
2507 				}
2508 				fprintf(out,
2509 					"3DPRIMITIVE: no terminator found in index buffer\n");
2510 				ret = count;
2511 				goto out;
2512 			} else {
2513 				/* fixed size vertex index buffer */
2514 				for (j = 1, i = 0; i < len; i += 2, j++) {
2515 					if (i * 2 == len - 1) {
2516 						instr_out(ctx, j,
2517 							  "    indices: 0x%04x\n",
2518 							  data[j] & 0xffff);
2519 					} else {
2520 						instr_out(ctx, j,
2521 							  "    indices: 0x%04x, 0x%04x\n",
2522 							  data[j] & 0xffff,
2523 							  data[j] >> 16);
2524 					}
2525 				}
2526 			}
2527 			ret = (len + 1) / 2 + 1;
2528 			goto out;
2529 		} else {
2530 			/* sequential vertex access */
2531 			instr_out(ctx, 0,
2532 				  "3DPRIMITIVE sequential indirect %s, %d starting from "
2533 				  "%d\n", primtype, len, data[1] & 0xffff);
2534 			instr_out(ctx, 1, "           start\n");
2535 			ret = 2;
2536 			goto out;
2537 		}
2538 	}
2539 
2540 out:
2541 	saved_s2 = original_s2;
2542 	saved_s4 = original_s4;
2543 	return ret;
2544 }
2545 
2546 static int
decode_3d(struct drm_intel_decode * ctx)2547 decode_3d(struct drm_intel_decode *ctx)
2548 {
2549 	uint32_t opcode;
2550 	unsigned int idx;
2551 	uint32_t *data = ctx->data;
2552 
2553 	struct {
2554 		uint32_t opcode;
2555 		unsigned int min_len;
2556 		unsigned int max_len;
2557 		const char *name;
2558 	} opcodes_3d[] = {
2559 		{ 0x06, 1, 1, "3DSTATE_ANTI_ALIASING" },
2560 		{ 0x08, 1, 1, "3DSTATE_BACKFACE_STENCIL_OPS" },
2561 		{ 0x09, 1, 1, "3DSTATE_BACKFACE_STENCIL_MASKS" },
2562 		{ 0x16, 1, 1, "3DSTATE_COORD_SET_BINDINGS" },
2563 		{ 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
2564 		{ 0x0b, 1, 1, "3DSTATE_INDEPENDENT_ALPHA_BLEND" },
2565 		{ 0x0d, 1, 1, "3DSTATE_MODES_4" },
2566 		{ 0x0c, 1, 1, "3DSTATE_MODES_5" },
2567 		{ 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES"},
2568 	}, *opcode_3d;
2569 
2570 	opcode = (data[0] & 0x1f000000) >> 24;
2571 
2572 	switch (opcode) {
2573 	case 0x1f:
2574 		return decode_3d_primitive(ctx);
2575 	case 0x1d:
2576 		return decode_3d_1d(ctx);
2577 	case 0x1c:
2578 		return decode_3d_1c(ctx);
2579 	}
2580 
2581 	for (idx = 0; idx < ARRAY_SIZE(opcodes_3d); idx++) {
2582 		opcode_3d = &opcodes_3d[idx];
2583 		if (opcode == opcode_3d->opcode) {
2584 			unsigned int len = 1, i;
2585 
2586 			instr_out(ctx, 0, "%s\n", opcode_3d->name);
2587 			if (opcode_3d->max_len > 1) {
2588 				len = (data[0] & 0xff) + 2;
2589 				if (len < opcode_3d->min_len ||
2590 				    len > opcode_3d->max_len) {
2591 					fprintf(out, "Bad count in %s\n",
2592 						opcode_3d->name);
2593 				}
2594 			}
2595 
2596 			for (i = 1; i < len; i++) {
2597 				instr_out(ctx, i, "dword %d\n", i);
2598 			}
2599 			return len;
2600 		}
2601 	}
2602 
2603 	instr_out(ctx, 0, "3D UNKNOWN: 3d opcode = 0x%x\n", opcode);
2604 	return 1;
2605 }
2606 
get_965_surfacetype(unsigned int surfacetype)2607 static const char *get_965_surfacetype(unsigned int surfacetype)
2608 {
2609 	switch (surfacetype) {
2610 	case 0:
2611 		return "1D";
2612 	case 1:
2613 		return "2D";
2614 	case 2:
2615 		return "3D";
2616 	case 3:
2617 		return "CUBE";
2618 	case 4:
2619 		return "BUFFER";
2620 	case 7:
2621 		return "NULL";
2622 	default:
2623 		return "unknown";
2624 	}
2625 }
2626 
get_965_depthformat(unsigned int depthformat)2627 static const char *get_965_depthformat(unsigned int depthformat)
2628 {
2629 	switch (depthformat) {
2630 	case 0:
2631 		return "s8_z24float";
2632 	case 1:
2633 		return "z32float";
2634 	case 2:
2635 		return "z24s8";
2636 	case 5:
2637 		return "z16";
2638 	default:
2639 		return "unknown";
2640 	}
2641 }
2642 
get_965_element_component(uint32_t data,int component)2643 static const char *get_965_element_component(uint32_t data, int component)
2644 {
2645 	uint32_t component_control = (data >> (16 + (3 - component) * 4)) & 0x7;
2646 
2647 	switch (component_control) {
2648 	case 0:
2649 		return "nostore";
2650 	case 1:
2651 		switch (component) {
2652 		case 0:
2653 			return "X";
2654 		case 1:
2655 			return "Y";
2656 		case 2:
2657 			return "Z";
2658 		case 3:
2659 			return "W";
2660 		default:
2661 			return "fail";
2662 		}
2663 	case 2:
2664 		return "0.0";
2665 	case 3:
2666 		return "1.0";
2667 	case 4:
2668 		return "0x1";
2669 	case 5:
2670 		return "VID";
2671 	default:
2672 		return "fail";
2673 	}
2674 }
2675 
get_965_prim_type(uint32_t primtype)2676 static const char *get_965_prim_type(uint32_t primtype)
2677 {
2678 	switch (primtype) {
2679 	case 0x01:
2680 		return "point list";
2681 	case 0x02:
2682 		return "line list";
2683 	case 0x03:
2684 		return "line strip";
2685 	case 0x04:
2686 		return "tri list";
2687 	case 0x05:
2688 		return "tri strip";
2689 	case 0x06:
2690 		return "tri fan";
2691 	case 0x07:
2692 		return "quad list";
2693 	case 0x08:
2694 		return "quad strip";
2695 	case 0x09:
2696 		return "line list adj";
2697 	case 0x0a:
2698 		return "line strip adj";
2699 	case 0x0b:
2700 		return "tri list adj";
2701 	case 0x0c:
2702 		return "tri strip adj";
2703 	case 0x0d:
2704 		return "tri strip reverse";
2705 	case 0x0e:
2706 		return "polygon";
2707 	case 0x0f:
2708 		return "rect list";
2709 	case 0x10:
2710 		return "line loop";
2711 	case 0x11:
2712 		return "point list bf";
2713 	case 0x12:
2714 		return "line strip cont";
2715 	case 0x13:
2716 		return "line strip bf";
2717 	case 0x14:
2718 		return "line strip cont bf";
2719 	case 0x15:
2720 		return "tri fan no stipple";
2721 	default:
2722 		return "fail";
2723 	}
2724 }
2725 
2726 static int
i965_decode_urb_fence(struct drm_intel_decode * ctx,int len)2727 i965_decode_urb_fence(struct drm_intel_decode *ctx, int len)
2728 {
2729 	uint32_t vs_fence, clip_fence, gs_fence, sf_fence, vfe_fence, cs_fence;
2730 	uint32_t *data = ctx->data;
2731 
2732 	if (len != 3)
2733 		fprintf(out, "Bad count in URB_FENCE\n");
2734 
2735 	vs_fence = data[1] & 0x3ff;
2736 	gs_fence = (data[1] >> 10) & 0x3ff;
2737 	clip_fence = (data[1] >> 20) & 0x3ff;
2738 	sf_fence = data[2] & 0x3ff;
2739 	vfe_fence = (data[2] >> 10) & 0x3ff;
2740 	cs_fence = (data[2] >> 20) & 0x7ff;
2741 
2742 	instr_out(ctx, 0, "URB_FENCE: %s%s%s%s%s%s\n",
2743 		  (data[0] >> 13) & 1 ? "cs " : "",
2744 		  (data[0] >> 12) & 1 ? "vfe " : "",
2745 		  (data[0] >> 11) & 1 ? "sf " : "",
2746 		  (data[0] >> 10) & 1 ? "clip " : "",
2747 		  (data[0] >> 9) & 1 ? "gs " : "",
2748 		  (data[0] >> 8) & 1 ? "vs " : "");
2749 	instr_out(ctx, 1,
2750 		  "vs fence: %d, clip_fence: %d, gs_fence: %d\n",
2751 		  vs_fence, clip_fence, gs_fence);
2752 	instr_out(ctx, 2,
2753 		  "sf fence: %d, vfe_fence: %d, cs_fence: %d\n",
2754 		  sf_fence, vfe_fence, cs_fence);
2755 	if (gs_fence < vs_fence)
2756 		fprintf(out, "gs fence < vs fence!\n");
2757 	if (clip_fence < gs_fence)
2758 		fprintf(out, "clip fence < gs fence!\n");
2759 	if (sf_fence < clip_fence)
2760 		fprintf(out, "sf fence < clip fence!\n");
2761 	if (cs_fence < sf_fence)
2762 		fprintf(out, "cs fence < sf fence!\n");
2763 
2764 	return len;
2765 }
2766 
2767 static void
state_base_out(struct drm_intel_decode * ctx,unsigned int index,const char * name)2768 state_base_out(struct drm_intel_decode *ctx, unsigned int index,
2769 	       const char *name)
2770 {
2771 	if (ctx->data[index] & 1) {
2772 		instr_out(ctx, index,
2773 			  "%s state base address 0x%08x\n", name,
2774 			  ctx->data[index] & ~1);
2775 	} else {
2776 		instr_out(ctx, index, "%s state base not updated\n",
2777 			  name);
2778 	}
2779 }
2780 
2781 static void
state_max_out(struct drm_intel_decode * ctx,unsigned int index,const char * name)2782 state_max_out(struct drm_intel_decode *ctx, unsigned int index,
2783 	      const char *name)
2784 {
2785 	if (ctx->data[index] & 1) {
2786 		if (ctx->data[index] == 1) {
2787 			instr_out(ctx, index,
2788 				  "%s state upper bound disabled\n", name);
2789 		} else {
2790 			instr_out(ctx, index,
2791 				  "%s state upper bound 0x%08x\n", name,
2792 				  ctx->data[index] & ~1);
2793 		}
2794 	} else {
2795 		instr_out(ctx, index,
2796 			  "%s state upper bound not updated\n", name);
2797 	}
2798 }
2799 
2800 static int
gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(struct drm_intel_decode * ctx)2801 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(struct drm_intel_decode *ctx)
2802 {
2803 	instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_CC\n");
2804 	instr_out(ctx, 1, "pointer to CC viewport\n");
2805 
2806 	return 2;
2807 }
2808 
2809 static int
gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(struct drm_intel_decode * ctx)2810 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(struct drm_intel_decode *ctx)
2811 {
2812 	instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP\n");
2813 	instr_out(ctx, 1, "pointer to SF_CLIP viewport\n");
2814 
2815 	return 2;
2816 }
2817 
2818 static int
gen7_3DSTATE_BLEND_STATE_POINTERS(struct drm_intel_decode * ctx)2819 gen7_3DSTATE_BLEND_STATE_POINTERS(struct drm_intel_decode *ctx)
2820 {
2821 	instr_out(ctx, 0, "3DSTATE_BLEND_STATE_POINTERS\n");
2822 	instr_out(ctx, 1, "pointer to BLEND_STATE at 0x%08x (%s)\n",
2823 		  ctx->data[1] & ~1,
2824 		  (ctx->data[1] & 1) ? "changed" : "unchanged");
2825 
2826 	return 2;
2827 }
2828 
2829 static int
gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(struct drm_intel_decode * ctx)2830 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(struct drm_intel_decode *ctx)
2831 {
2832 	instr_out(ctx, 0, "3DSTATE_DEPTH_STENCIL_STATE_POINTERS\n");
2833 	instr_out(ctx, 1,
2834 		  "pointer to DEPTH_STENCIL_STATE at 0x%08x (%s)\n",
2835 		  ctx->data[1] & ~1,
2836 		  (ctx->data[1] & 1) ? "changed" : "unchanged");
2837 
2838 	return 2;
2839 }
2840 
2841 static int
gen7_3DSTATE_HIER_DEPTH_BUFFER(struct drm_intel_decode * ctx)2842 gen7_3DSTATE_HIER_DEPTH_BUFFER(struct drm_intel_decode *ctx)
2843 {
2844 	instr_out(ctx, 0, "3DSTATE_HIER_DEPTH_BUFFER\n");
2845 	instr_out(ctx, 1, "pitch %db\n",
2846 		  (ctx->data[1] & 0x1ffff) + 1);
2847 	instr_out(ctx, 2, "pointer to HiZ buffer\n");
2848 
2849 	return 3;
2850 }
2851 
2852 static int
gen6_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode * ctx)2853 gen6_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
2854 {
2855 	instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
2856 	instr_out(ctx, 1, "blend change %d\n", ctx->data[1] & 1);
2857 	instr_out(ctx, 2, "depth stencil change %d\n",
2858 		  ctx->data[2] & 1);
2859 	instr_out(ctx, 3, "cc change %d\n", ctx->data[3] & 1);
2860 
2861 	return 4;
2862 }
2863 
2864 static int
gen7_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode * ctx)2865 gen7_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
2866 {
2867 	instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
2868 	instr_out(ctx, 1, "pointer to COLOR_CALC_STATE at 0x%08x "
2869 		  "(%s)\n",
2870 		  ctx->data[1] & ~1,
2871 		  (ctx->data[1] & 1) ? "changed" : "unchanged");
2872 
2873 	return 2;
2874 }
2875 
2876 static int
gen7_3DSTATE_URB_unit(struct drm_intel_decode * ctx,const char * unit)2877 gen7_3DSTATE_URB_unit(struct drm_intel_decode *ctx, const char *unit)
2878 {
2879     int start_kb = ((ctx->data[1] >> 25) & 0x3f) * 8;
2880     /* the field is # of 512-bit rows - 1, we print bytes */
2881     int entry_size = (((ctx->data[1] >> 16) & 0x1ff) + 1);
2882     int nr_entries = ctx->data[1] & 0xffff;
2883 
2884     instr_out(ctx, 0, "3DSTATE_URB_%s\n", unit);
2885     instr_out(ctx, 1,
2886 	      "%dKB start, size=%d 64B rows, nr_entries=%d, total size %dB\n",
2887 	      start_kb, entry_size, nr_entries, nr_entries * 64 * entry_size);
2888 
2889     return 2;
2890 }
2891 
2892 static int
gen7_3DSTATE_URB_VS(struct drm_intel_decode * ctx)2893 gen7_3DSTATE_URB_VS(struct drm_intel_decode *ctx)
2894 {
2895 	return gen7_3DSTATE_URB_unit(ctx, "VS");
2896 }
2897 
2898 static int
gen7_3DSTATE_URB_HS(struct drm_intel_decode * ctx)2899 gen7_3DSTATE_URB_HS(struct drm_intel_decode *ctx)
2900 {
2901 	return gen7_3DSTATE_URB_unit(ctx, "HS");
2902 }
2903 
2904 static int
gen7_3DSTATE_URB_DS(struct drm_intel_decode * ctx)2905 gen7_3DSTATE_URB_DS(struct drm_intel_decode *ctx)
2906 {
2907 	return gen7_3DSTATE_URB_unit(ctx, "DS");
2908 }
2909 
2910 static int
gen7_3DSTATE_URB_GS(struct drm_intel_decode * ctx)2911 gen7_3DSTATE_URB_GS(struct drm_intel_decode *ctx)
2912 {
2913 	return gen7_3DSTATE_URB_unit(ctx, "GS");
2914 }
2915 
2916 static int
gen7_3DSTATE_CONSTANT(struct drm_intel_decode * ctx,const char * unit)2917 gen7_3DSTATE_CONSTANT(struct drm_intel_decode *ctx, const char *unit)
2918 {
2919 	int rlen[4];
2920 
2921 	rlen[0] = (ctx->data[1] >> 0) & 0xffff;
2922 	rlen[1] = (ctx->data[1] >> 16) & 0xffff;
2923 	rlen[2] = (ctx->data[2] >> 0) & 0xffff;
2924 	rlen[3] = (ctx->data[2] >> 16) & 0xffff;
2925 
2926 	instr_out(ctx, 0, "3DSTATE_CONSTANT_%s\n", unit);
2927 	instr_out(ctx, 1, "len 0 = %d, len 1 = %d\n", rlen[0], rlen[1]);
2928 	instr_out(ctx, 2, "len 2 = %d, len 3 = %d\n", rlen[2], rlen[3]);
2929 	instr_out(ctx, 3, "pointer to constbuf 0\n");
2930 	instr_out(ctx, 4, "pointer to constbuf 1\n");
2931 	instr_out(ctx, 5, "pointer to constbuf 2\n");
2932 	instr_out(ctx, 6, "pointer to constbuf 3\n");
2933 
2934 	return 7;
2935 }
2936 
2937 static int
gen7_3DSTATE_CONSTANT_VS(struct drm_intel_decode * ctx)2938 gen7_3DSTATE_CONSTANT_VS(struct drm_intel_decode *ctx)
2939 {
2940 	return gen7_3DSTATE_CONSTANT(ctx, "VS");
2941 }
2942 
2943 static int
gen7_3DSTATE_CONSTANT_GS(struct drm_intel_decode * ctx)2944 gen7_3DSTATE_CONSTANT_GS(struct drm_intel_decode *ctx)
2945 {
2946 	return gen7_3DSTATE_CONSTANT(ctx, "GS");
2947 }
2948 
2949 static int
gen7_3DSTATE_CONSTANT_PS(struct drm_intel_decode * ctx)2950 gen7_3DSTATE_CONSTANT_PS(struct drm_intel_decode *ctx)
2951 {
2952 	return gen7_3DSTATE_CONSTANT(ctx, "PS");
2953 }
2954 
2955 static int
gen7_3DSTATE_CONSTANT_DS(struct drm_intel_decode * ctx)2956 gen7_3DSTATE_CONSTANT_DS(struct drm_intel_decode *ctx)
2957 {
2958 	return gen7_3DSTATE_CONSTANT(ctx, "DS");
2959 }
2960 
2961 static int
gen7_3DSTATE_CONSTANT_HS(struct drm_intel_decode * ctx)2962 gen7_3DSTATE_CONSTANT_HS(struct drm_intel_decode *ctx)
2963 {
2964 	return gen7_3DSTATE_CONSTANT(ctx, "HS");
2965 }
2966 
2967 
2968 static int
gen6_3DSTATE_WM(struct drm_intel_decode * ctx)2969 gen6_3DSTATE_WM(struct drm_intel_decode *ctx)
2970 {
2971 	instr_out(ctx, 0, "3DSTATE_WM\n");
2972 	instr_out(ctx, 1, "kernel start pointer 0\n");
2973 	instr_out(ctx, 2,
2974 		  "SPF=%d, VME=%d, Sampler Count %d, "
2975 		  "Binding table count %d\n",
2976 		  (ctx->data[2] >> 31) & 1,
2977 		  (ctx->data[2] >> 30) & 1,
2978 		  (ctx->data[2] >> 27) & 7,
2979 		  (ctx->data[2] >> 18) & 0xff);
2980 	instr_out(ctx, 3, "scratch offset\n");
2981 	instr_out(ctx, 4,
2982 		  "Depth Clear %d, Depth Resolve %d, HiZ Resolve %d, "
2983 		  "Dispatch GRF start[0] %d, start[1] %d, start[2] %d\n",
2984 		  (ctx->data[4] & (1 << 30)) != 0,
2985 		  (ctx->data[4] & (1 << 28)) != 0,
2986 		  (ctx->data[4] & (1 << 27)) != 0,
2987 		  (ctx->data[4] >> 16) & 0x7f,
2988 		  (ctx->data[4] >> 8) & 0x7f,
2989 		  (ctx->data[4] & 0x7f));
2990 	instr_out(ctx, 5,
2991 		  "MaxThreads %d, PS KillPixel %d, PS computed Z %d, "
2992 		  "PS use sourceZ %d, Thread Dispatch %d, PS use sourceW %d, "
2993 		  "Dispatch32 %d, Dispatch16 %d, Dispatch8 %d\n",
2994 		  ((ctx->data[5] >> 25) & 0x7f) + 1,
2995 		  (ctx->data[5] & (1 << 22)) != 0,
2996 		  (ctx->data[5] & (1 << 21)) != 0,
2997 		  (ctx->data[5] & (1 << 20)) != 0,
2998 		  (ctx->data[5] & (1 << 19)) != 0,
2999 		  (ctx->data[5] & (1 << 8)) != 0,
3000 		  (ctx->data[5] & (1 << 2)) != 0,
3001 		  (ctx->data[5] & (1 << 1)) != 0,
3002 		  (ctx->data[5] & (1 << 0)) != 0);
3003 	instr_out(ctx, 6,
3004 		  "Num SF output %d, Pos XY offset %d, ZW interp mode %d , "
3005 		  "Barycentric interp mode 0x%x, Point raster rule %d, "
3006 		  "Multisample mode %d, "
3007 		  "Multisample Dispatch mode %d\n",
3008 		  (ctx->data[6] >> 20) & 0x3f,
3009 		  (ctx->data[6] >> 18) & 3,
3010 		  (ctx->data[6] >> 16) & 3,
3011 		  (ctx->data[6] >> 10) & 0x3f,
3012 		  (ctx->data[6] & (1 << 9)) != 0,
3013 		  (ctx->data[6] >> 1) & 3,
3014 		  (ctx->data[6] & 1));
3015 	instr_out(ctx, 7, "kernel start pointer 1\n");
3016 	instr_out(ctx, 8, "kernel start pointer 2\n");
3017 
3018 	return 9;
3019 }
3020 
3021 static int
gen7_3DSTATE_WM(struct drm_intel_decode * ctx)3022 gen7_3DSTATE_WM(struct drm_intel_decode *ctx)
3023 {
3024 	const char *computed_depth = "";
3025 	const char *early_depth = "";
3026 	const char *zw_interp = "";
3027 
3028 	switch ((ctx->data[1] >> 23) & 0x3) {
3029 	case 0:
3030 		computed_depth = "";
3031 		break;
3032 	case 1:
3033 		computed_depth = "computed depth";
3034 		break;
3035 	case 2:
3036 		computed_depth = "computed depth >=";
3037 		break;
3038 	case 3:
3039 		computed_depth = "computed depth <=";
3040 		break;
3041 	}
3042 
3043 	switch ((ctx->data[1] >> 21) & 0x3) {
3044 	case 0:
3045 		early_depth = "";
3046 		break;
3047 	case 1:
3048 		early_depth = ", EDSC_PSEXEC";
3049 		break;
3050 	case 2:
3051 		early_depth = ", EDSC_PREPS";
3052 		break;
3053 	case 3:
3054 		early_depth = ", BAD EDSC";
3055 		break;
3056 	}
3057 
3058 	switch ((ctx->data[1] >> 17) & 0x3) {
3059 	case 0:
3060 		early_depth = "";
3061 		break;
3062 	case 1:
3063 		early_depth = ", BAD ZW interp";
3064 		break;
3065 	case 2:
3066 		early_depth = ", ZW centroid";
3067 		break;
3068 	case 3:
3069 		early_depth = ", ZW sample";
3070 		break;
3071 	}
3072 
3073 	instr_out(ctx, 0, "3DSTATE_WM\n");
3074 	instr_out(ctx, 1, "(%s%s%s%s%s%s)%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
3075 		  (ctx->data[1] & (1 << 11)) ? "PP " : "",
3076 		  (ctx->data[1] & (1 << 12)) ? "PC " : "",
3077 		  (ctx->data[1] & (1 << 13)) ? "PS " : "",
3078 		  (ctx->data[1] & (1 << 14)) ? "NPP " : "",
3079 		  (ctx->data[1] & (1 << 15)) ? "NPC " : "",
3080 		  (ctx->data[1] & (1 << 16)) ? "NPS " : "",
3081 		  (ctx->data[1] & (1 << 30)) ? ", depth clear" : "",
3082 		  (ctx->data[1] & (1 << 29)) ? "" : ", disabled",
3083 		  (ctx->data[1] & (1 << 28)) ? ", depth resolve" : "",
3084 		  (ctx->data[1] & (1 << 27)) ? ", hiz resolve" : "",
3085 		  (ctx->data[1] & (1 << 25)) ? ", kill" : "",
3086 		  computed_depth,
3087 		  early_depth,
3088 		  zw_interp,
3089 		  (ctx->data[1] & (1 << 20)) ? ", source depth" : "",
3090 		  (ctx->data[1] & (1 << 19)) ? ", source W" : "",
3091 		  (ctx->data[1] & (1 << 10)) ? ", coverage" : "",
3092 		  (ctx->data[1] & (1 << 4)) ? ", poly stipple" : "",
3093 		  (ctx->data[1] & (1 << 3)) ? ", line stipple" : "",
3094 		  (ctx->data[1] & (1 << 2)) ? ", point UL" : ", point UR"
3095 		  );
3096 	instr_out(ctx, 2, "MS\n");
3097 
3098 	return 3;
3099 }
3100 
3101 static int
gen4_3DPRIMITIVE(struct drm_intel_decode * ctx)3102 gen4_3DPRIMITIVE(struct drm_intel_decode *ctx)
3103 {
3104 	instr_out(ctx, 0,
3105 		  "3DPRIMITIVE: %s %s\n",
3106 		  get_965_prim_type((ctx->data[0] >> 10) & 0x1f),
3107 		  (ctx->data[0] & (1 << 15)) ? "random" : "sequential");
3108 	instr_out(ctx, 1, "vertex count\n");
3109 	instr_out(ctx, 2, "start vertex\n");
3110 	instr_out(ctx, 3, "instance count\n");
3111 	instr_out(ctx, 4, "start instance\n");
3112 	instr_out(ctx, 5, "index bias\n");
3113 
3114 	return 6;
3115 }
3116 
3117 static int
gen7_3DPRIMITIVE(struct drm_intel_decode * ctx)3118 gen7_3DPRIMITIVE(struct drm_intel_decode *ctx)
3119 {
3120 	bool indirect = !!(ctx->data[0] & (1 << 10));
3121 
3122 	instr_out(ctx, 0,
3123 		  "3DPRIMITIVE: %s%s\n",
3124 		  indirect ? " indirect" : "",
3125 		  (ctx->data[0] & (1 << 8)) ? " predicated" : "");
3126 	instr_out(ctx, 1, "%s %s\n",
3127 		  get_965_prim_type(ctx->data[1] & 0x3f),
3128 		  (ctx->data[1] & (1 << 8)) ? "random" : "sequential");
3129 	instr_out(ctx, 2, indirect ? "ignored" : "vertex count\n");
3130 	instr_out(ctx, 3, indirect ? "ignored" : "start vertex\n");
3131 	instr_out(ctx, 4, indirect ? "ignored" : "instance count\n");
3132 	instr_out(ctx, 5, indirect ? "ignored" : "start instance\n");
3133 	instr_out(ctx, 6, indirect ? "ignored" : "index bias\n");
3134 
3135 	return 7;
3136 }
3137 
3138 static int
decode_3d_965(struct drm_intel_decode * ctx)3139 decode_3d_965(struct drm_intel_decode *ctx)
3140 {
3141 	uint32_t opcode;
3142 	unsigned int len;
3143 	unsigned int i, j, sba_len;
3144 	const char *desc1 = NULL;
3145 	uint32_t *data = ctx->data;
3146 	uint32_t devid = ctx->devid;
3147 
3148 	struct {
3149 		uint32_t opcode;
3150 		uint32_t len_mask;
3151 		int unsigned min_len;
3152 		int unsigned max_len;
3153 		const char *name;
3154 		int gen;
3155 		int (*func)(struct drm_intel_decode *ctx);
3156 	} opcodes_3d[] = {
3157 		{ 0x6000, 0x00ff, 3, 3, "URB_FENCE" },
3158 		{ 0x6001, 0xffff, 2, 2, "CS_URB_STATE" },
3159 		{ 0x6002, 0x00ff, 2, 2, "CONSTANT_BUFFER" },
3160 		{ 0x6101, 0xffff, 6, 10, "STATE_BASE_ADDRESS" },
3161 		{ 0x6102, 0xffff, 2, 2, "STATE_SIP" },
3162 		{ 0x6104, 0xffff, 1, 1, "3DSTATE_PIPELINE_SELECT" },
3163 		{ 0x680b, 0xffff, 1, 1, "3DSTATE_VF_STATISTICS" },
3164 		{ 0x6904, 0xffff, 1, 1, "3DSTATE_PIPELINE_SELECT" },
3165 		{ 0x7800, 0xffff, 7, 7, "3DSTATE_PIPELINED_POINTERS" },
3166 		{ 0x7801, 0x00ff, 4, 6, "3DSTATE_BINDING_TABLE_POINTERS" },
3167 		{ 0x7802, 0x00ff, 4, 4, "3DSTATE_SAMPLER_STATE_POINTERS" },
3168 		{ 0x7805, 0x00ff, 7, 7, "3DSTATE_DEPTH_BUFFER", 7 },
3169 		{ 0x7805, 0x00ff, 3, 3, "3DSTATE_URB" },
3170 		{ 0x7804, 0x00ff, 3, 3, "3DSTATE_CLEAR_PARAMS" },
3171 		{ 0x7806, 0x00ff, 3, 3, "3DSTATE_STENCIL_BUFFER" },
3172 		{ 0x790f, 0x00ff, 3, 3, "3DSTATE_HIER_DEPTH_BUFFER", 6 },
3173 		{ 0x7807, 0x00ff, 3, 3, "3DSTATE_HIER_DEPTH_BUFFER", 7, gen7_3DSTATE_HIER_DEPTH_BUFFER },
3174 		{ 0x7808, 0x00ff, 5, 257, "3DSTATE_VERTEX_BUFFERS" },
3175 		{ 0x7809, 0x00ff, 3, 256, "3DSTATE_VERTEX_ELEMENTS" },
3176 		{ 0x780a, 0x00ff, 3, 3, "3DSTATE_INDEX_BUFFER" },
3177 		{ 0x780b, 0xffff, 1, 1, "3DSTATE_VF_STATISTICS" },
3178 		{ 0x780d, 0x00ff, 4, 4, "3DSTATE_VIEWPORT_STATE_POINTERS" },
3179 		{ 0x780e, 0xffff, 4, 4, NULL, 6, gen6_3DSTATE_CC_STATE_POINTERS },
3180 		{ 0x780e, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_CC_STATE_POINTERS },
3181 		{ 0x780f, 0x00ff, 2, 2, "3DSTATE_SCISSOR_POINTERS" },
3182 		{ 0x7810, 0x00ff, 6, 6, "3DSTATE_VS" },
3183 		{ 0x7811, 0x00ff, 7, 7, "3DSTATE_GS" },
3184 		{ 0x7812, 0x00ff, 4, 4, "3DSTATE_CLIP" },
3185 		{ 0x7813, 0x00ff, 20, 20, "3DSTATE_SF", 6 },
3186 		{ 0x7813, 0x00ff, 7, 7, "3DSTATE_SF", 7 },
3187 		{ 0x7814, 0x00ff, 3, 3, "3DSTATE_WM", 7, gen7_3DSTATE_WM },
3188 		{ 0x7814, 0x00ff, 9, 9, "3DSTATE_WM", 6, gen6_3DSTATE_WM },
3189 		{ 0x7815, 0x00ff, 5, 5, "3DSTATE_CONSTANT_VS_STATE", 6 },
3190 		{ 0x7815, 0x00ff, 7, 7, "3DSTATE_CONSTANT_VS", 7, gen7_3DSTATE_CONSTANT_VS },
3191 		{ 0x7816, 0x00ff, 5, 5, "3DSTATE_CONSTANT_GS_STATE", 6 },
3192 		{ 0x7816, 0x00ff, 7, 7, "3DSTATE_CONSTANT_GS", 7, gen7_3DSTATE_CONSTANT_GS },
3193 		{ 0x7817, 0x00ff, 5, 5, "3DSTATE_CONSTANT_PS_STATE", 6 },
3194 		{ 0x7817, 0x00ff, 7, 7, "3DSTATE_CONSTANT_PS", 7, gen7_3DSTATE_CONSTANT_PS },
3195 		{ 0x7818, 0xffff, 2, 2, "3DSTATE_SAMPLE_MASK" },
3196 		{ 0x7819, 0x00ff, 7, 7, "3DSTATE_CONSTANT_HS", 7, gen7_3DSTATE_CONSTANT_HS },
3197 		{ 0x781a, 0x00ff, 7, 7, "3DSTATE_CONSTANT_DS", 7, gen7_3DSTATE_CONSTANT_DS },
3198 		{ 0x781b, 0x00ff, 7, 7, "3DSTATE_HS" },
3199 		{ 0x781c, 0x00ff, 4, 4, "3DSTATE_TE" },
3200 		{ 0x781d, 0x00ff, 6, 6, "3DSTATE_DS" },
3201 		{ 0x781e, 0x00ff, 3, 3, "3DSTATE_STREAMOUT" },
3202 		{ 0x781f, 0x00ff, 14, 14, "3DSTATE_SBE" },
3203 		{ 0x7820, 0x00ff, 8, 8, "3DSTATE_PS" },
3204 		{ 0x7821, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP },
3205 		{ 0x7823, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC },
3206 		{ 0x7824, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_BLEND_STATE_POINTERS },
3207 		{ 0x7825, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS },
3208 		{ 0x7826, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_VS" },
3209 		{ 0x7827, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_HS" },
3210 		{ 0x7828, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_DS" },
3211 		{ 0x7829, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_GS" },
3212 		{ 0x782a, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_PS" },
3213 		{ 0x782b, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_VS" },
3214 		{ 0x782c, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_HS" },
3215 		{ 0x782d, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_DS" },
3216 		{ 0x782e, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_GS" },
3217 		{ 0x782f, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_PS" },
3218 		{ 0x7830, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_VS },
3219 		{ 0x7831, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_HS },
3220 		{ 0x7832, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_DS },
3221 		{ 0x7833, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_GS },
3222 		{ 0x7900, 0xffff, 4, 4, "3DSTATE_DRAWING_RECTANGLE" },
3223 		{ 0x7901, 0xffff, 5, 5, "3DSTATE_CONSTANT_COLOR" },
3224 		{ 0x7905, 0xffff, 5, 7, "3DSTATE_DEPTH_BUFFER" },
3225 		{ 0x7906, 0xffff, 2, 2, "3DSTATE_POLY_STIPPLE_OFFSET" },
3226 		{ 0x7907, 0xffff, 33, 33, "3DSTATE_POLY_STIPPLE_PATTERN" },
3227 		{ 0x7908, 0xffff, 3, 3, "3DSTATE_LINE_STIPPLE" },
3228 		{ 0x7909, 0xffff, 2, 2, "3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP" },
3229 		{ 0x7909, 0xffff, 2, 2, "3DSTATE_CLEAR_PARAMS" },
3230 		{ 0x790a, 0xffff, 3, 3, "3DSTATE_AA_LINE_PARAMETERS" },
3231 		{ 0x790b, 0xffff, 4, 4, "3DSTATE_GS_SVB_INDEX" },
3232 		{ 0x790d, 0xffff, 3, 3, "3DSTATE_MULTISAMPLE", 6 },
3233 		{ 0x790d, 0xffff, 4, 4, "3DSTATE_MULTISAMPLE", 7 },
3234 		{ 0x7910, 0x00ff, 2, 2, "3DSTATE_CLEAR_PARAMS" },
3235 		{ 0x7912, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_VS" },
3236 		{ 0x7913, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_HS" },
3237 		{ 0x7914, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_DS" },
3238 		{ 0x7915, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_GS" },
3239 		{ 0x7916, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_PS" },
3240 		{ 0x7917, 0x00ff, 2, 2+128*2, "3DSTATE_SO_DECL_LIST" },
3241 		{ 0x7918, 0x00ff, 4, 4, "3DSTATE_SO_BUFFER" },
3242 		{ 0x7a00, 0x00ff, 4, 6, "PIPE_CONTROL" },
3243 		{ 0x7b00, 0x00ff, 7, 7, NULL, 7, gen7_3DPRIMITIVE },
3244 		{ 0x7b00, 0x00ff, 6, 6, NULL, 0, gen4_3DPRIMITIVE },
3245 	}, *opcode_3d = NULL;
3246 
3247 	opcode = (data[0] & 0xffff0000) >> 16;
3248 
3249 	for (i = 0; i < ARRAY_SIZE(opcodes_3d); i++) {
3250 		if (opcode != opcodes_3d[i].opcode)
3251 			continue;
3252 
3253 		/* If it's marked as not our gen, skip. */
3254 		if (opcodes_3d[i].gen && opcodes_3d[i].gen != ctx->gen)
3255 			continue;
3256 
3257 		opcode_3d = &opcodes_3d[i];
3258 		break;
3259 	}
3260 
3261 	if (opcode_3d) {
3262 		if (opcode_3d->max_len == 1)
3263 			len = 1;
3264 		else
3265 			len = (data[0] & opcode_3d->len_mask) + 2;
3266 
3267 		if (len < opcode_3d->min_len ||
3268 		    len > opcode_3d->max_len) {
3269 			fprintf(out, "Bad length %d in %s, expected %d-%d\n",
3270 				len, opcode_3d->name,
3271 				opcode_3d->min_len, opcode_3d->max_len);
3272 		}
3273 	} else {
3274 		len = (data[0] & 0x0000ffff) + 2;
3275 	}
3276 
3277 	switch (opcode) {
3278 	case 0x6000:
3279 		return i965_decode_urb_fence(ctx, len);
3280 	case 0x6001:
3281 		instr_out(ctx, 0, "CS_URB_STATE\n");
3282 		instr_out(ctx, 1,
3283 			  "entry_size: %d [%d bytes], n_entries: %d\n",
3284 			  (data[1] >> 4) & 0x1f,
3285 			  (((data[1] >> 4) & 0x1f) + 1) * 64, data[1] & 0x7);
3286 		return len;
3287 	case 0x6002:
3288 		instr_out(ctx, 0, "CONSTANT_BUFFER: %s\n",
3289 			  (data[0] >> 8) & 1 ? "valid" : "invalid");
3290 		instr_out(ctx, 1,
3291 			  "offset: 0x%08x, length: %d bytes\n", data[1] & ~0x3f,
3292 			  ((data[1] & 0x3f) + 1) * 64);
3293 		return len;
3294 	case 0x6101:
3295 		i = 0;
3296 		instr_out(ctx, 0, "STATE_BASE_ADDRESS\n");
3297 		i++;
3298 
3299 		if (IS_GEN6(devid) || IS_GEN7(devid))
3300 			sba_len = 10;
3301 		else if (IS_GEN5(devid))
3302 			sba_len = 8;
3303 		else
3304 			sba_len = 6;
3305 		if (len != sba_len)
3306 			fprintf(out, "Bad count in STATE_BASE_ADDRESS\n");
3307 
3308 		state_base_out(ctx, i++, "general");
3309 		state_base_out(ctx, i++, "surface");
3310 		if (IS_GEN6(devid) || IS_GEN7(devid))
3311 			state_base_out(ctx, i++, "dynamic");
3312 		state_base_out(ctx, i++, "indirect");
3313 		if (IS_GEN5(devid) || IS_GEN6(devid) || IS_GEN7(devid))
3314 			state_base_out(ctx, i++, "instruction");
3315 
3316 		state_max_out(ctx, i++, "general");
3317 		if (IS_GEN6(devid) || IS_GEN7(devid))
3318 			state_max_out(ctx, i++, "dynamic");
3319 		state_max_out(ctx, i++, "indirect");
3320 		if (IS_GEN5(devid) || IS_GEN6(devid) || IS_GEN7(devid))
3321 			state_max_out(ctx, i++, "instruction");
3322 
3323 		return len;
3324 	case 0x7800:
3325 		instr_out(ctx, 0, "3DSTATE_PIPELINED_POINTERS\n");
3326 		instr_out(ctx, 1, "VS state\n");
3327 		instr_out(ctx, 2, "GS state\n");
3328 		instr_out(ctx, 3, "Clip state\n");
3329 		instr_out(ctx, 4, "SF state\n");
3330 		instr_out(ctx, 5, "WM state\n");
3331 		instr_out(ctx, 6, "CC state\n");
3332 		return len;
3333 	case 0x7801:
3334 		if (len != 6 && len != 4)
3335 			fprintf(out,
3336 				"Bad count in 3DSTATE_BINDING_TABLE_POINTERS\n");
3337 		if (len == 6) {
3338 			instr_out(ctx, 0,
3339 				  "3DSTATE_BINDING_TABLE_POINTERS\n");
3340 			instr_out(ctx, 1, "VS binding table\n");
3341 			instr_out(ctx, 2, "GS binding table\n");
3342 			instr_out(ctx, 3, "Clip binding table\n");
3343 			instr_out(ctx, 4, "SF binding table\n");
3344 			instr_out(ctx, 5, "WM binding table\n");
3345 		} else {
3346 			instr_out(ctx, 0,
3347 				  "3DSTATE_BINDING_TABLE_POINTERS: VS mod %d, "
3348 				  "GS mod %d, PS mod %d\n",
3349 				  (data[0] & (1 << 8)) != 0,
3350 				  (data[0] & (1 << 9)) != 0,
3351 				  (data[0] & (1 << 12)) != 0);
3352 			instr_out(ctx, 1, "VS binding table\n");
3353 			instr_out(ctx, 2, "GS binding table\n");
3354 			instr_out(ctx, 3, "WM binding table\n");
3355 		}
3356 
3357 		return len;
3358 	case 0x7802:
3359 		instr_out(ctx, 0,
3360 			  "3DSTATE_SAMPLER_STATE_POINTERS: VS mod %d, "
3361 			  "GS mod %d, PS mod %d\n", (data[0] & (1 << 8)) != 0,
3362 			  (data[0] & (1 << 9)) != 0,
3363 			  (data[0] & (1 << 12)) != 0);
3364 		instr_out(ctx, 1, "VS sampler state\n");
3365 		instr_out(ctx, 2, "GS sampler state\n");
3366 		instr_out(ctx, 3, "WM sampler state\n");
3367 		return len;
3368 	case 0x7805:
3369 		/* Actually 3DSTATE_DEPTH_BUFFER on gen7. */
3370 		if (ctx->gen == 7)
3371 			break;
3372 
3373 		instr_out(ctx, 0, "3DSTATE_URB\n");
3374 		instr_out(ctx, 1,
3375 			  "VS entries %d, alloc size %d (1024bit row)\n",
3376 			  data[1] & 0xffff, ((data[1] >> 16) & 0x07f) + 1);
3377 		instr_out(ctx, 2,
3378 			  "GS entries %d, alloc size %d (1024bit row)\n",
3379 			  (data[2] >> 8) & 0x3ff, (data[2] & 7) + 1);
3380 		return len;
3381 
3382 	case 0x7808:
3383 		if ((len - 1) % 4 != 0)
3384 			fprintf(out, "Bad count in 3DSTATE_VERTEX_BUFFERS\n");
3385 		instr_out(ctx, 0, "3DSTATE_VERTEX_BUFFERS\n");
3386 
3387 		for (i = 1; i < len;) {
3388 			int idx, access;
3389 			if (IS_GEN6(devid)) {
3390 				idx = 26;
3391 				access = 20;
3392 			} else {
3393 				idx = 27;
3394 				access = 26;
3395 			}
3396 			instr_out(ctx, i,
3397 				  "buffer %d: %s, pitch %db\n", data[i] >> idx,
3398 				  data[i] & (1 << access) ? "random" :
3399 				  "sequential", data[i] & 0x07ff);
3400 			i++;
3401 			instr_out(ctx, i++, "buffer address\n");
3402 			instr_out(ctx, i++, "max index\n");
3403 			instr_out(ctx, i++, "mbz\n");
3404 		}
3405 		return len;
3406 
3407 	case 0x7809:
3408 		if ((len + 1) % 2 != 0)
3409 			fprintf(out, "Bad count in 3DSTATE_VERTEX_ELEMENTS\n");
3410 		instr_out(ctx, 0, "3DSTATE_VERTEX_ELEMENTS\n");
3411 
3412 		for (i = 1; i < len;) {
3413 			instr_out(ctx, i,
3414 				  "buffer %d: %svalid, type 0x%04x, "
3415 				  "src offset 0x%04x bytes\n",
3416 				  data[i] >> ((IS_GEN6(devid) || IS_GEN7(devid)) ? 26 : 27),
3417 				  data[i] & (1 << ((IS_GEN6(devid) || IS_GEN7(devid)) ? 25 : 26)) ?
3418 				  "" : "in", (data[i] >> 16) & 0x1ff,
3419 				  data[i] & 0x07ff);
3420 			i++;
3421 			instr_out(ctx, i, "(%s, %s, %s, %s), "
3422 				  "dst offset 0x%02x bytes\n",
3423 				  get_965_element_component(data[i], 0),
3424 				  get_965_element_component(data[i], 1),
3425 				  get_965_element_component(data[i], 2),
3426 				  get_965_element_component(data[i], 3),
3427 				  (data[i] & 0xff) * 4);
3428 			i++;
3429 		}
3430 		return len;
3431 
3432 	case 0x780d:
3433 		instr_out(ctx, 0,
3434 			  "3DSTATE_VIEWPORT_STATE_POINTERS\n");
3435 		instr_out(ctx, 1, "clip\n");
3436 		instr_out(ctx, 2, "sf\n");
3437 		instr_out(ctx, 3, "cc\n");
3438 		return len;
3439 
3440 	case 0x780a:
3441 		instr_out(ctx, 0, "3DSTATE_INDEX_BUFFER\n");
3442 		instr_out(ctx, 1, "beginning buffer address\n");
3443 		instr_out(ctx, 2, "ending buffer address\n");
3444 		return len;
3445 
3446 	case 0x780f:
3447 		instr_out(ctx, 0, "3DSTATE_SCISSOR_POINTERS\n");
3448 		instr_out(ctx, 1, "scissor rect offset\n");
3449 		return len;
3450 
3451 	case 0x7810:
3452 		instr_out(ctx, 0, "3DSTATE_VS\n");
3453 		instr_out(ctx, 1, "kernel pointer\n");
3454 		instr_out(ctx, 2,
3455 			  "SPF=%d, VME=%d, Sampler Count %d, "
3456 			  "Binding table count %d\n", (data[2] >> 31) & 1,
3457 			  (data[2] >> 30) & 1, (data[2] >> 27) & 7,
3458 			  (data[2] >> 18) & 0xff);
3459 		instr_out(ctx, 3, "scratch offset\n");
3460 		instr_out(ctx, 4,
3461 			  "Dispatch GRF start %d, VUE read length %d, "
3462 			  "VUE read offset %d\n", (data[4] >> 20) & 0x1f,
3463 			  (data[4] >> 11) & 0x3f, (data[4] >> 4) & 0x3f);
3464 		instr_out(ctx, 5,
3465 			  "Max Threads %d, Vertex Cache %sable, "
3466 			  "VS func %sable\n", ((data[5] >> 25) & 0x7f) + 1,
3467 			  (data[5] & (1 << 1)) != 0 ? "dis" : "en",
3468 			  (data[5] & 1) != 0 ? "en" : "dis");
3469 		return len;
3470 
3471 	case 0x7811:
3472 		instr_out(ctx, 0, "3DSTATE_GS\n");
3473 		instr_out(ctx, 1, "kernel pointer\n");
3474 		instr_out(ctx, 2,
3475 			  "SPF=%d, VME=%d, Sampler Count %d, "
3476 			  "Binding table count %d\n", (data[2] >> 31) & 1,
3477 			  (data[2] >> 30) & 1, (data[2] >> 27) & 7,
3478 			  (data[2] >> 18) & 0xff);
3479 		instr_out(ctx, 3, "scratch offset\n");
3480 		instr_out(ctx, 4,
3481 			  "Dispatch GRF start %d, VUE read length %d, "
3482 			  "VUE read offset %d\n", (data[4] & 0xf),
3483 			  (data[4] >> 11) & 0x3f, (data[4] >> 4) & 0x3f);
3484 		instr_out(ctx, 5,
3485 			  "Max Threads %d, Rendering %sable\n",
3486 			  ((data[5] >> 25) & 0x7f) + 1,
3487 			  (data[5] & (1 << 8)) != 0 ? "en" : "dis");
3488 		instr_out(ctx, 6,
3489 			  "Reorder %sable, Discard Adjaceny %sable, "
3490 			  "GS %sable\n",
3491 			  (data[6] & (1 << 30)) != 0 ? "en" : "dis",
3492 			  (data[6] & (1 << 29)) != 0 ? "en" : "dis",
3493 			  (data[6] & (1 << 15)) != 0 ? "en" : "dis");
3494 		return len;
3495 
3496 	case 0x7812:
3497 		instr_out(ctx, 0, "3DSTATE_CLIP\n");
3498 		instr_out(ctx, 1,
3499 			  "UserClip distance cull test mask 0x%x\n",
3500 			  data[1] & 0xff);
3501 		instr_out(ctx, 2,
3502 			  "Clip %sable, API mode %s, Viewport XY test %sable, "
3503 			  "Viewport Z test %sable, Guardband test %sable, Clip mode %d, "
3504 			  "Perspective Divide %sable, Non-Perspective Barycentric %sable, "
3505 			  "Tri Provoking %d, Line Provoking %d, Trifan Provoking %d\n",
3506 			  (data[2] & (1 << 31)) != 0 ? "en" : "dis",
3507 			  (data[2] & (1 << 30)) != 0 ? "D3D" : "OGL",
3508 			  (data[2] & (1 << 28)) != 0 ? "en" : "dis",
3509 			  (data[2] & (1 << 27)) != 0 ? "en" : "dis",
3510 			  (data[2] & (1 << 26)) != 0 ? "en" : "dis",
3511 			  (data[2] >> 13) & 7,
3512 			  (data[2] & (1 << 9)) != 0 ? "dis" : "en",
3513 			  (data[2] & (1 << 8)) != 0 ? "en" : "dis",
3514 			  (data[2] >> 4) & 3, (data[2] >> 2) & 3,
3515 			  (data[2] & 3));
3516 		instr_out(ctx, 3,
3517 			  "Min PointWidth %d, Max PointWidth %d, "
3518 			  "Force Zero RTAIndex %sable, Max VPIndex %d\n",
3519 			  (data[3] >> 17) & 0x7ff, (data[3] >> 6) & 0x7ff,
3520 			  (data[3] & (1 << 5)) != 0 ? "en" : "dis",
3521 			  (data[3] & 0xf));
3522 		return len;
3523 
3524 	case 0x7813:
3525 		if (ctx->gen == 7)
3526 			break;
3527 
3528 		instr_out(ctx, 0, "3DSTATE_SF\n");
3529 		instr_out(ctx, 1,
3530 			  "Attrib Out %d, Attrib Swizzle %sable, VUE read length %d, "
3531 			  "VUE read offset %d\n", (data[1] >> 22) & 0x3f,
3532 			  (data[1] & (1 << 21)) != 0 ? "en" : "dis",
3533 			  (data[1] >> 11) & 0x1f, (data[1] >> 4) & 0x3f);
3534 		instr_out(ctx, 2,
3535 			  "Legacy Global DepthBias %sable, FrontFace fill %d, BF fill %d, "
3536 			  "VP transform %sable, FrontWinding_%s\n",
3537 			  (data[2] & (1 << 11)) != 0 ? "en" : "dis",
3538 			  (data[2] >> 5) & 3, (data[2] >> 3) & 3,
3539 			  (data[2] & (1 << 1)) != 0 ? "en" : "dis",
3540 			  (data[2] & 1) != 0 ? "CCW" : "CW");
3541 		instr_out(ctx, 3,
3542 			  "AA %sable, CullMode %d, Scissor %sable, Multisample m ode %d\n",
3543 			  (data[3] & (1 << 31)) != 0 ? "en" : "dis",
3544 			  (data[3] >> 29) & 3,
3545 			  (data[3] & (1 << 11)) != 0 ? "en" : "dis",
3546 			  (data[3] >> 8) & 3);
3547 		instr_out(ctx, 4,
3548 			  "Last Pixel %sable, SubPixel Precision %d, Use PixelWidth %d\n",
3549 			  (data[4] & (1 << 31)) != 0 ? "en" : "dis",
3550 			  (data[4] & (1 << 12)) != 0 ? 4 : 8,
3551 			  (data[4] & (1 << 11)) != 0);
3552 		instr_out(ctx, 5,
3553 			  "Global Depth Offset Constant %f\n",
3554 			  *(float *)(&data[5]));
3555 		instr_out(ctx, 6, "Global Depth Offset Scale %f\n",
3556 			  *(float *)(&data[6]));
3557 		instr_out(ctx, 7, "Global Depth Offset Clamp %f\n",
3558 			  *(float *)(&data[7]));
3559 
3560 		for (i = 0, j = 0; i < 8; i++, j += 2)
3561 			instr_out(ctx, i + 8,
3562 				  "Attrib %d (Override %s%s%s%s, Const Source %d, Swizzle Select %d, "
3563 				  "Source %d); Attrib %d (Override %s%s%s%s, Const Source %d, Swizzle Select %d, Source %d)\n",
3564 				  j + 1,
3565 				  (data[8 + i] & (1 << 31)) != 0 ? "W" : "",
3566 				  (data[8 + i] & (1 << 30)) != 0 ? "Z" : "",
3567 				  (data[8 + i] & (1 << 29)) != 0 ? "Y" : "",
3568 				  (data[8 + i] & (1 << 28)) != 0 ? "X" : "",
3569 				  (data[8 + i] >> 25) & 3,
3570 				  (data[8 + i] >> 22) & 3,
3571 				  (data[8 + i] >> 16) & 0x1f, j,
3572 				  (data[8 + i] & (1 << 15)) != 0 ? "W" : "",
3573 				  (data[8 + i] & (1 << 14)) != 0 ? "Z" : "",
3574 				  (data[8 + i] & (1 << 13)) != 0 ? "Y" : "",
3575 				  (data[8 + i] & (1 << 12)) != 0 ? "X" : "",
3576 				  (data[8 + i] >> 9) & 3,
3577 				  (data[8 + i] >> 6) & 3, (data[8 + i] & 0x1f));
3578 		instr_out(ctx, 16,
3579 			  "Point Sprite TexCoord Enable\n");
3580 		instr_out(ctx, 17, "Const Interp Enable\n");
3581 		instr_out(ctx, 18,
3582 			  "Attrib 7-0 WrapShortest Enable\n");
3583 		instr_out(ctx, 19,
3584 			  "Attrib 15-8 WrapShortest Enable\n");
3585 
3586 		return len;
3587 
3588 	case 0x7900:
3589 		instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
3590 		instr_out(ctx, 1, "top left: %d,%d\n",
3591 			  data[1] & 0xffff, (data[1] >> 16) & 0xffff);
3592 		instr_out(ctx, 2, "bottom right: %d,%d\n",
3593 			  data[2] & 0xffff, (data[2] >> 16) & 0xffff);
3594 		instr_out(ctx, 3, "origin: %d,%d\n",
3595 			  (int)data[3] & 0xffff, ((int)data[3] >> 16) & 0xffff);
3596 
3597 		return len;
3598 
3599 	case 0x7905:
3600 		instr_out(ctx, 0, "3DSTATE_DEPTH_BUFFER\n");
3601 		if (IS_GEN5(devid) || IS_GEN6(devid))
3602 			instr_out(ctx, 1,
3603 				  "%s, %s, pitch = %d bytes, %stiled, HiZ %d, Seperate Stencil %d\n",
3604 				  get_965_surfacetype(data[1] >> 29),
3605 				  get_965_depthformat((data[1] >> 18) & 0x7),
3606 				  (data[1] & 0x0001ffff) + 1,
3607 				  data[1] & (1 << 27) ? "" : "not ",
3608 				  (data[1] & (1 << 22)) != 0,
3609 				  (data[1] & (1 << 21)) != 0);
3610 		else
3611 			instr_out(ctx, 1,
3612 				  "%s, %s, pitch = %d bytes, %stiled\n",
3613 				  get_965_surfacetype(data[1] >> 29),
3614 				  get_965_depthformat((data[1] >> 18) & 0x7),
3615 				  (data[1] & 0x0001ffff) + 1,
3616 				  data[1] & (1 << 27) ? "" : "not ");
3617 		instr_out(ctx, 2, "depth offset\n");
3618 		instr_out(ctx, 3, "%dx%d\n",
3619 			  ((data[3] & 0x0007ffc0) >> 6) + 1,
3620 			  ((data[3] & 0xfff80000) >> 19) + 1);
3621 		instr_out(ctx, 4, "volume depth\n");
3622 		if (len >= 6)
3623 			instr_out(ctx, 5, "\n");
3624 		if (len >= 7) {
3625 			if (IS_GEN6(devid))
3626 				instr_out(ctx, 6, "\n");
3627 			else
3628 				instr_out(ctx, 6,
3629 					  "render target view extent\n");
3630 		}
3631 
3632 		return len;
3633 
3634 	case 0x7a00:
3635 		if (IS_GEN6(devid) || IS_GEN7(devid)) {
3636 			if (len != 4 && len != 5)
3637 				fprintf(out, "Bad count in PIPE_CONTROL\n");
3638 
3639 			switch ((data[1] >> 14) & 0x3) {
3640 			case 0:
3641 				desc1 = "no write";
3642 				break;
3643 			case 1:
3644 				desc1 = "qword write";
3645 				break;
3646 			case 2:
3647 				desc1 = "PS_DEPTH_COUNT write";
3648 				break;
3649 			case 3:
3650 				desc1 = "TIMESTAMP write";
3651 				break;
3652 			}
3653 			instr_out(ctx, 0, "PIPE_CONTROL\n");
3654 			instr_out(ctx, 1,
3655 				  "%s, %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
3656 				  desc1,
3657 				  data[1] & (1 << 20) ? "cs stall, " : "",
3658 				  data[1] & (1 << 19) ?
3659 				  "global snapshot count reset, " : "",
3660 				  data[1] & (1 << 18) ? "tlb invalidate, " : "",
3661 				  data[1] & (1 << 17) ? "gfdt flush, " : "",
3662 				  data[1] & (1 << 17) ? "media state clear, " :
3663 				  "",
3664 				  data[1] & (1 << 13) ? "depth stall, " : "",
3665 				  data[1] & (1 << 12) ?
3666 				  "render target cache flush, " : "",
3667 				  data[1] & (1 << 11) ?
3668 				  "instruction cache invalidate, " : "",
3669 				  data[1] & (1 << 10) ?
3670 				  "texture cache invalidate, " : "",
3671 				  data[1] & (1 << 9) ?
3672 				  "indirect state invalidate, " : "",
3673 				  data[1] & (1 << 8) ? "notify irq, " : "",
3674 				  data[1] & (1 << 7) ? "PIPE_CONTROL flush, " :
3675 				  "",
3676 				  data[1] & (1 << 6) ? "protect mem app_id, " :
3677 				  "", data[1] & (1 << 5) ? "DC flush, " : "",
3678 				  data[1] & (1 << 4) ? "vf fetch invalidate, " :
3679 				  "",
3680 				  data[1] & (1 << 3) ?
3681 				  "constant cache invalidate, " : "",
3682 				  data[1] & (1 << 2) ?
3683 				  "state cache invalidate, " : "",
3684 				  data[1] & (1 << 1) ? "stall at scoreboard, " :
3685 				  "",
3686 				  data[1] & (1 << 0) ? "depth cache flush, " :
3687 				  "");
3688 			if (len == 5) {
3689 				instr_out(ctx, 2,
3690 					  "destination address\n");
3691 				instr_out(ctx, 3,
3692 					  "immediate dword low\n");
3693 				instr_out(ctx, 4,
3694 					  "immediate dword high\n");
3695 			} else {
3696 				for (i = 2; i < len; i++) {
3697 					instr_out(ctx, i, "\n");
3698 				}
3699 			}
3700 			return len;
3701 		} else {
3702 			if (len != 4)
3703 				fprintf(out, "Bad count in PIPE_CONTROL\n");
3704 
3705 			switch ((data[0] >> 14) & 0x3) {
3706 			case 0:
3707 				desc1 = "no write";
3708 				break;
3709 			case 1:
3710 				desc1 = "qword write";
3711 				break;
3712 			case 2:
3713 				desc1 = "PS_DEPTH_COUNT write";
3714 				break;
3715 			case 3:
3716 				desc1 = "TIMESTAMP write";
3717 				break;
3718 			}
3719 			instr_out(ctx, 0,
3720 				  "PIPE_CONTROL: %s, %sdepth stall, %sRC write flush, "
3721 				  "%sinst flush\n",
3722 				  desc1,
3723 				  data[0] & (1 << 13) ? "" : "no ",
3724 				  data[0] & (1 << 12) ? "" : "no ",
3725 				  data[0] & (1 << 11) ? "" : "no ");
3726 			instr_out(ctx, 1, "destination address\n");
3727 			instr_out(ctx, 2, "immediate dword low\n");
3728 			instr_out(ctx, 3, "immediate dword high\n");
3729 			return len;
3730 		}
3731 	}
3732 
3733 	if (opcode_3d) {
3734 		if (opcode_3d->func) {
3735 			return opcode_3d->func(ctx);
3736 		} else {
3737 			instr_out(ctx, 0, "%s\n", opcode_3d->name);
3738 
3739 			for (i = 1; i < len; i++) {
3740 				instr_out(ctx, i, "dword %d\n", i);
3741 			}
3742 			return len;
3743 		}
3744 	}
3745 
3746 	instr_out(ctx, 0, "3D UNKNOWN: 3d_965 opcode = 0x%x\n",
3747 		  opcode);
3748 	return 1;
3749 }
3750 
3751 static int
decode_3d_i830(struct drm_intel_decode * ctx)3752 decode_3d_i830(struct drm_intel_decode *ctx)
3753 {
3754 	unsigned int idx;
3755 	uint32_t opcode;
3756 	uint32_t *data = ctx->data;
3757 
3758 	struct {
3759 		uint32_t opcode;
3760 		unsigned int min_len;
3761 		unsigned int max_len;
3762 		const char *name;
3763 	} opcodes_3d[] = {
3764 		{ 0x02, 1, 1, "3DSTATE_MODES_3" },
3765 		{ 0x03, 1, 1, "3DSTATE_ENABLES_1" },
3766 		{ 0x04, 1, 1, "3DSTATE_ENABLES_2" },
3767 		{ 0x05, 1, 1, "3DSTATE_VFT0" },
3768 		{ 0x06, 1, 1, "3DSTATE_AA" },
3769 		{ 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES" },
3770 		{ 0x08, 1, 1, "3DSTATE_MODES_1" },
3771 		{ 0x09, 1, 1, "3DSTATE_STENCIL_TEST" },
3772 		{ 0x0a, 1, 1, "3DSTATE_VFT1" },
3773 		{ 0x0b, 1, 1, "3DSTATE_INDPT_ALPHA_BLEND" },
3774 		{ 0x0c, 1, 1, "3DSTATE_MODES_5" },
3775 		{ 0x0d, 1, 1, "3DSTATE_MAP_BLEND_OP" },
3776 		{ 0x0e, 1, 1, "3DSTATE_MAP_BLEND_ARG" },
3777 		{ 0x0f, 1, 1, "3DSTATE_MODES_2" },
3778 		{ 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
3779 		{ 0x16, 1, 1, "3DSTATE_MODES_4"},
3780 	}, *opcode_3d;
3781 
3782 	opcode = (data[0] & 0x1f000000) >> 24;
3783 
3784 	switch (opcode) {
3785 	case 0x1f:
3786 		return decode_3d_primitive(ctx);
3787 	case 0x1d:
3788 		return decode_3d_1d(ctx);
3789 	case 0x1c:
3790 		return decode_3d_1c(ctx);
3791 	}
3792 
3793 	for (idx = 0; idx < ARRAY_SIZE(opcodes_3d); idx++) {
3794 		opcode_3d = &opcodes_3d[idx];
3795 		if ((data[0] & 0x1f000000) >> 24 == opcode_3d->opcode) {
3796 			unsigned int len = 1, i;
3797 
3798 			instr_out(ctx, 0, "%s\n", opcode_3d->name);
3799 			if (opcode_3d->max_len > 1) {
3800 				len = (data[0] & 0xff) + 2;
3801 				if (len < opcode_3d->min_len ||
3802 				    len > opcode_3d->max_len) {
3803 					fprintf(out, "Bad count in %s\n",
3804 						opcode_3d->name);
3805 				}
3806 			}
3807 
3808 			for (i = 1; i < len; i++) {
3809 				instr_out(ctx, i, "dword %d\n", i);
3810 			}
3811 			return len;
3812 		}
3813 	}
3814 
3815 	instr_out(ctx, 0, "3D UNKNOWN: 3d_i830 opcode = 0x%x\n",
3816 		  opcode);
3817 	return 1;
3818 }
3819 
3820 struct drm_intel_decode *
drm_intel_decode_context_alloc(uint32_t devid)3821 drm_intel_decode_context_alloc(uint32_t devid)
3822 {
3823 	struct drm_intel_decode *ctx;
3824 
3825 	ctx = calloc(1, sizeof(struct drm_intel_decode));
3826 	if (!ctx)
3827 		return NULL;
3828 
3829 	ctx->devid = devid;
3830 	ctx->out = stdout;
3831 
3832 	if (IS_GEN9(devid))
3833 		ctx->gen = 9;
3834 	else if (IS_GEN8(devid))
3835 		ctx->gen = 8;
3836 	else if (IS_GEN7(devid))
3837 		ctx->gen = 7;
3838 	else if (IS_GEN6(devid))
3839 		ctx->gen = 6;
3840 	else if (IS_GEN5(devid))
3841 		ctx->gen = 5;
3842 	else if (IS_GEN4(devid))
3843 		ctx->gen = 4;
3844 	else if (IS_9XX(devid))
3845 		ctx->gen = 3;
3846 	else {
3847 		assert(IS_GEN2(devid));
3848 		ctx->gen = 2;
3849 	}
3850 
3851 	return ctx;
3852 }
3853 
3854 void
drm_intel_decode_context_free(struct drm_intel_decode * ctx)3855 drm_intel_decode_context_free(struct drm_intel_decode *ctx)
3856 {
3857 	free(ctx);
3858 }
3859 
3860 void
drm_intel_decode_set_dump_past_end(struct drm_intel_decode * ctx,int dump_past_end)3861 drm_intel_decode_set_dump_past_end(struct drm_intel_decode *ctx,
3862 				   int dump_past_end)
3863 {
3864 	ctx->dump_past_end = !!dump_past_end;
3865 }
3866 
3867 void
drm_intel_decode_set_batch_pointer(struct drm_intel_decode * ctx,void * data,uint32_t hw_offset,int count)3868 drm_intel_decode_set_batch_pointer(struct drm_intel_decode *ctx,
3869 				   void *data, uint32_t hw_offset, int count)
3870 {
3871 	ctx->base_data = data;
3872 	ctx->base_hw_offset = hw_offset;
3873 	ctx->base_count = count;
3874 }
3875 
3876 void
drm_intel_decode_set_head_tail(struct drm_intel_decode * ctx,uint32_t head,uint32_t tail)3877 drm_intel_decode_set_head_tail(struct drm_intel_decode *ctx,
3878 			       uint32_t head, uint32_t tail)
3879 {
3880 	ctx->head = head;
3881 	ctx->tail = tail;
3882 }
3883 
3884 void
drm_intel_decode_set_output_file(struct drm_intel_decode * ctx,FILE * output)3885 drm_intel_decode_set_output_file(struct drm_intel_decode *ctx,
3886 				 FILE *output)
3887 {
3888 	ctx->out = output;
3889 }
3890 
3891 /**
3892  * Decodes an i830-i915 batch buffer, writing the output to stdout.
3893  *
3894  * \param data batch buffer contents
3895  * \param count number of DWORDs to decode in the batch buffer
3896  * \param hw_offset hardware address for the buffer
3897  */
3898 void
drm_intel_decode(struct drm_intel_decode * ctx)3899 drm_intel_decode(struct drm_intel_decode *ctx)
3900 {
3901 	int ret;
3902 	unsigned int index = 0;
3903 	uint32_t devid;
3904 	int size = ctx->base_count * 4;
3905 	void *temp;
3906 
3907 	if (!ctx)
3908 		return;
3909 
3910 	/* Put a scratch page full of obviously undefined data after
3911 	 * the batchbuffer.  This lets us avoid a bunch of length
3912 	 * checking in statically sized packets.
3913 	 */
3914 	temp = malloc(size + 4096);
3915 	memcpy(temp, ctx->base_data, size);
3916 	memset((char *)temp + size, 0xd0, 4096);
3917 	ctx->data = temp;
3918 
3919 	ctx->hw_offset = ctx->base_hw_offset;
3920 	ctx->count = ctx->base_count;
3921 
3922 	devid = ctx->devid;
3923 	head_offset = ctx->head;
3924 	tail_offset = ctx->tail;
3925 	out = ctx->out;
3926 
3927 	saved_s2_set = 0;
3928 	saved_s4_set = 1;
3929 
3930 	while (ctx->count > 0) {
3931 		index = 0;
3932 
3933 		switch ((ctx->data[index] & 0xe0000000) >> 29) {
3934 		case 0x0:
3935 			ret = decode_mi(ctx);
3936 
3937 			/* If MI_BATCHBUFFER_END happened, then dump
3938 			 * the rest of the output in case we some day
3939 			 * want it in debugging, but don't decode it
3940 			 * since it'll just confuse in the common
3941 			 * case.
3942 			 */
3943 			if (ret == -1) {
3944 				if (ctx->dump_past_end) {
3945 					index++;
3946 				} else {
3947 					for (index = index + 1; index < ctx->count;
3948 					     index++) {
3949 						instr_out(ctx, index, "\n");
3950 					}
3951 				}
3952 			} else
3953 				index += ret;
3954 			break;
3955 		case 0x2:
3956 			index += decode_2d(ctx);
3957 			break;
3958 		case 0x3:
3959 			if (IS_9XX(devid) && !IS_GEN3(devid)) {
3960 				index +=
3961 				    decode_3d_965(ctx);
3962 			} else if (IS_GEN3(devid)) {
3963 				index += decode_3d(ctx);
3964 			} else {
3965 				index +=
3966 				    decode_3d_i830(ctx);
3967 			}
3968 			break;
3969 		default:
3970 			instr_out(ctx, index, "UNKNOWN\n");
3971 			index++;
3972 			break;
3973 		}
3974 		fflush(out);
3975 
3976 		if (ctx->count < index)
3977 			break;
3978 
3979 		ctx->count -= index;
3980 		ctx->data += index;
3981 		ctx->hw_offset += 4 * index;
3982 	}
3983 
3984 	free(temp);
3985 }
3986