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