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