intel: Add an interface for setting the output file for decode.
[gstreamer-omap:libdrm.git] / intel / intel_decode.c
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 <stdint.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdarg.h>
29 #include <string.h>
30
31 #include "intel_chipset.h"
32 #include "intel_bufmgr.h"
33
34 /* Struct for tracking drm_intel_decode state. */
35 struct drm_intel_decode {
36         /** stdio file where the output should land.  Defaults to stdout. */
37         FILE *out;
38
39         /** PCI device ID. */
40         uint32_t devid;
41
42         /** GPU address of the start of the batchbuffer data. */
43         uint32_t hw_offset;
44         /** CPU Virtual address of the start of the batchbuffer data. */
45         uint32_t *data;
46         /** Number of DWORDs of batchbuffer data. */
47         uint32_t count;
48
49         /** @{
50          * GPU head and tail pointers, which will be noted in the dump, or ~0.
51          */
52         uint32_t head, tail;
53         /** @} */
54
55         /**
56          * Whether to dump the dwords after MI_BATCHBUFFER_END.
57          *
58          * This sometimes provides clues in corrupted batchbuffers,
59          * and is used by the intel-gpu-tools.
60          */
61         bool dump_past_end;
62 };
63
64 static FILE *out;
65 static uint32_t saved_s2 = 0, saved_s4 = 0;
66 static char saved_s2_set = 0, saved_s4_set = 0;
67 static uint32_t head_offset = 0xffffffff;       /* undefined */
68 static uint32_t tail_offset = 0xffffffff;       /* undefined */
69
70 #ifndef ARRAY_SIZE
71 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
72 #endif
73
74 #define BUFFER_FAIL(_count, _len, _name) do {                   \
75     fprintf(out, "Buffer size too small in %s (%d < %d)\n",     \
76             (_name), (_count), (_len));                         \
77     (*failures)++;                                              \
78     return count;                                               \
79 } while (0)
80
81 static float int_as_float(uint32_t intval)
82 {
83         union intfloat {
84                 uint32_t i;
85                 float f;
86         } uval;
87
88         uval.i = intval;
89         return uval.f;
90 }
91
92 static void
93 instr_out(uint32_t *data, uint32_t hw_offset, unsigned int index,
94           const char *fmt, ...) __attribute__((format(__printf__, 4, 5)));
95
96 static void
97 instr_out(uint32_t *data, uint32_t hw_offset, unsigned int index,
98           const char *fmt, ...)
99 {
100         va_list va;
101         const char *parseinfo;
102         uint32_t offset = hw_offset + index * 4;
103
104         if (offset == head_offset)
105                 parseinfo = "HEAD";
106         else if (offset == tail_offset)
107                 parseinfo = "TAIL";
108         else
109                 parseinfo = "    ";
110
111         fprintf(out, "0x%08x: %s 0x%08x: %s", offset, parseinfo,
112                 data[index], index == 0 ? "" : "   ");
113         va_start(va, fmt);
114         vfprintf(out, fmt, va);
115         va_end(va);
116 }
117
118 static int
119 decode_mi(uint32_t *data, uint32_t count, uint32_t hw_offset, int *failures)
120 {
121         unsigned int opcode, len = -1;
122         const char *post_sync_op = "";
123
124         struct {
125                 uint32_t opcode;
126                 int len_mask;
127                 unsigned int min_len;
128                 unsigned int max_len;
129                 const char *name;
130         } opcodes_mi[] = {
131                 { 0x08, 0, 1, 1, "MI_ARB_ON_OFF" },
132                 { 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
133                 { 0x30, 0x3f, 3, 3, "MI_BATCH_BUFFER" },
134                 { 0x31, 0x3f, 2, 2, "MI_BATCH_BUFFER_START" },
135                 { 0x14, 0x3f, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
136                 { 0x04, 0, 1, 1, "MI_FLUSH" },
137                 { 0x22, 0x1f, 3, 3, "MI_LOAD_REGISTER_IMM" },
138                 { 0x13, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
139                 { 0x12, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
140                 { 0x00, 0, 1, 1, "MI_NOOP" },
141                 { 0x11, 0x3f, 2, 2, "MI_OVERLAY_FLIP" },
142                 { 0x07, 0, 1, 1, "MI_REPORT_HEAD" },
143                 { 0x18, 0x3f, 2, 2, "MI_SET_CONTEXT" },
144                 { 0x20, 0x3f, 3, 4, "MI_STORE_DATA_IMM" },
145                 { 0x21, 0x3f, 3, 4, "MI_STORE_DATA_INDEX" },
146                 { 0x24, 0x3f, 3, 3, "MI_STORE_REGISTER_MEM" },
147                 { 0x02, 0, 1, 1, "MI_USER_INTERRUPT" },
148                 { 0x03, 0, 1, 1, "MI_WAIT_FOR_EVENT" },
149                 { 0x16, 0x7f, 3, 3, "MI_SEMAPHORE_MBOX" },
150                 { 0x26, 0x1f, 3, 4, "MI_FLUSH_DW" },
151                 { 0x0b, 0, 1, 1, "MI_SUSPEND_FLUSH"},
152         };
153
154         /* check instruction length */
155         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
156              opcode++) {
157                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
158                         len = 1;
159                         if (opcodes_mi[opcode].max_len > 1) {
160                                 len =
161                                     (data[0] & opcodes_mi[opcode].len_mask) + 2;
162                                 if (len < opcodes_mi[opcode].min_len
163                                     || len > opcodes_mi[opcode].max_len) {
164                                         fprintf(out,
165                                                 "Bad length (%d) in %s, [%d, %d]\n",
166                                                 len, opcodes_mi[opcode].name,
167                                                 opcodes_mi[opcode].min_len,
168                                                 opcodes_mi[opcode].max_len);
169                                 }
170                         }
171                         break;
172                 }
173         }
174
175         switch ((data[0] & 0x1f800000) >> 23) {
176         case 0x0a:
177                 instr_out(data, hw_offset, 0, "MI_BATCH_BUFFER_END\n");
178                 return -1;
179         case 0x16:
180                 instr_out(data, hw_offset, 0, "MI_SEMAPHORE_MBOX%s%s%s%s %u\n",
181                           data[0] & (1 << 22) ? " global gtt," : "",
182                           data[0] & (1 << 21) ? " update semaphore," : "",
183                           data[0] & (1 << 20) ? " compare semaphore," : "",
184                           data[0] & (1 << 18) ? " use compare reg" : "",
185                           (data[0] & (0x3 << 16)) >> 16);
186                 instr_out(data, hw_offset, 1, "value\n");
187                 instr_out(data, hw_offset, 2, "address\n");
188                 return len;
189         case 0x21:
190                 instr_out(data, hw_offset, 0, "MI_STORE_DATA_INDEX%s\n",
191                           data[0] & (1 << 21) ? " use per-process HWS," : "");
192                 instr_out(data, hw_offset, 1, "index\n");
193                 instr_out(data, hw_offset, 2, "dword\n");
194                 if (len == 4)
195                         instr_out(data, hw_offset, 3, "upper dword\n");
196                 return len;
197         case 0x00:
198                 if (data[0] & (1 << 22))
199                         instr_out(data, hw_offset, 0,
200                                   "MI_NOOP write NOPID reg, val=0x%x\n",
201                                   data[0] & ((1 << 22) - 1));
202                 else
203                         instr_out(data, hw_offset, 0, "MI_NOOP\n");
204                 return len;
205         case 0x26:
206                 switch (data[0] & (0x3 << 14)) {
207                 case (0 << 14):
208                         post_sync_op = "no write";
209                         break;
210                 case (1 << 14):
211                         post_sync_op = "write data";
212                         break;
213                 case (2 << 14):
214                         post_sync_op = "reserved";
215                         break;
216                 case (3 << 14):
217                         post_sync_op = "write TIMESTAMP";
218                         break;
219                 }
220                 instr_out(data, hw_offset, 0,
221                           "MI_FLUSH_DW%s%s%s%s post_sync_op='%s' %s%s\n",
222                           data[0] & (1 << 22) ?
223                           " enable protected mem (BCS-only)," : "",
224                           data[0] & (1 << 21) ? " store in hws," : "",
225                           data[0] & (1 << 18) ? " invalidate tlb," : "",
226                           data[0] & (1 << 17) ? " flush gfdt," : "",
227                           post_sync_op,
228                           data[0] & (1 << 8) ? " enable notify interrupt," : "",
229                           data[0] & (1 << 7) ?
230                           " invalidate video state (BCS-only)," : "");
231                 if (data[0] & (1 << 21))
232                         instr_out(data, hw_offset, 1, "hws index\n");
233                 else
234                         instr_out(data, hw_offset, 1, "address\n");
235                 instr_out(data, hw_offset, 2, "dword\n");
236                 if (len == 4)
237                         instr_out(data, hw_offset, 3, "upper dword\n");
238                 return len;
239         }
240
241         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
242              opcode++) {
243                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
244                         unsigned int i;
245
246                         instr_out(data, hw_offset, 0, "%s\n",
247                                   opcodes_mi[opcode].name);
248                         for (i = 1; i < len; i++) {
249                                 if (i >= count)
250                                         BUFFER_FAIL(count, len,
251                                                     opcodes_mi[opcode].name);
252                                 instr_out(data, hw_offset, i, "dword %d\n", i);
253                         }
254
255                         return len;
256                 }
257         }
258
259         instr_out(data, hw_offset, 0, "MI UNKNOWN\n");
260         (*failures)++;
261         return 1;
262 }
263
264 static void
265 decode_2d_br00(uint32_t *data, uint32_t count, uint32_t hw_offset,
266                const char *cmd)
267 {
268         instr_out(data, hw_offset, 0,
269                   "%s (rgb %sabled, alpha %sabled, src tile %d, dst tile %d)\n",
270                   cmd,
271                   (data[count] & (1 << 20)) ? "en" : "dis",
272                   (data[count] & (1 << 21)) ? "en" : "dis",
273                   (data[count] >> 15) & 1, (data[count] >> 11) & 1);
274 }
275
276 static void decode_2d_br01(uint32_t *data, uint32_t count, uint32_t hw_offset)
277 {
278         const char *format;
279         switch ((data[count] >> 24) & 0x3) {
280         case 0:
281                 format = "8";
282                 break;
283         case 1:
284                 format = "565";
285                 break;
286         case 2:
287                 format = "1555";
288                 break;
289         case 3:
290                 format = "8888";
291                 break;
292         }
293
294         instr_out(data, hw_offset, count, "format %s, pitch %d, rop 0x%02x, "
295                   "clipping %sabled, %s%s \n",
296                   format,
297                   (short)(data[count] & 0xffff),
298                   (data[count] >> 16) & 0xff,
299                   data[count] & (1 << 30) ? "en" : "dis",
300                   data[count] & (1 << 31) ? "solid pattern enabled, " : "",
301                   data[count] & (1 << 31) ?
302                   "mono pattern transparency enabled, " : "");
303
304 }
305
306 static int
307 decode_2d(uint32_t *data, uint32_t count, uint32_t hw_offset, int *failures)
308 {
309         unsigned int opcode, len;
310
311         struct {
312                 uint32_t opcode;
313                 unsigned int min_len;
314                 unsigned int max_len;
315                 const char *name;
316         } opcodes_2d[] = {
317                 { 0x40, 5, 5, "COLOR_BLT" },
318                 { 0x43, 6, 6, "SRC_COPY_BLT" },
319                 { 0x01, 8, 8, "XY_SETUP_BLT" },
320                 { 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
321                 { 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
322                 { 0x24, 2, 2, "XY_PIXEL_BLT" },
323                 { 0x25, 3, 3, "XY_SCANLINES_BLT" },
324                 { 0x26, 4, 4, "Y_TEXT_BLT" },
325                 { 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
326                 { 0x50, 6, 6, "XY_COLOR_BLT" },
327                 { 0x51, 6, 6, "XY_PAT_BLT" },
328                 { 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
329                 { 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
330                 { 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
331                 { 0x52, 9, 9, "XY_MONO_PAT_BLT" },
332                 { 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
333                 { 0x53, 8, 8, "XY_SRC_COPY_BLT" },
334                 { 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
335                 { 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
336                 { 0x55, 9, 9, "XY_FULL_BLT" },
337                 { 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
338                 { 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
339                 { 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
340                 { 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
341                 { 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT"},
342         };
343
344         switch ((data[0] & 0x1fc00000) >> 22) {
345         case 0x25:
346                 instr_out(data, hw_offset, 0,
347                           "XY_SCANLINES_BLT (pattern seed (%d, %d), dst tile %d)\n",
348                           (data[0] >> 12) & 0x8,
349                           (data[0] >> 8) & 0x8, (data[0] >> 11) & 1);
350
351                 len = (data[0] & 0x000000ff) + 2;
352                 if (len != 3)
353                         fprintf(out, "Bad count in XY_SCANLINES_BLT\n");
354                 if (count < 3)
355                         BUFFER_FAIL(count, len, "XY_SCANLINES_BLT");
356
357                 instr_out(data, hw_offset, 1, "dest (%d,%d)\n",
358                           data[1] & 0xffff, data[1] >> 16);
359                 instr_out(data, hw_offset, 2, "dest (%d,%d)\n",
360                           data[2] & 0xffff, data[2] >> 16);
361                 return len;
362         case 0x01:
363                 decode_2d_br00(data, 0, hw_offset, "XY_SETUP_BLT");
364
365                 len = (data[0] & 0x000000ff) + 2;
366                 if (len != 8)
367                         fprintf(out, "Bad count in XY_SETUP_BLT\n");
368                 if (count < 8)
369                         BUFFER_FAIL(count, len, "XY_SETUP_BLT");
370
371                 decode_2d_br01(data, 1, hw_offset);
372                 instr_out(data, hw_offset, 2, "cliprect (%d,%d)\n",
373                           data[2] & 0xffff, data[2] >> 16);
374                 instr_out(data, hw_offset, 3, "cliprect (%d,%d)\n",
375                           data[3] & 0xffff, data[3] >> 16);
376                 instr_out(data, hw_offset, 4, "setup dst offset 0x%08x\n",
377                           data[4]);
378                 instr_out(data, hw_offset, 5, "setup background color\n");
379                 instr_out(data, hw_offset, 6, "setup foreground color\n");
380                 instr_out(data, hw_offset, 7, "color pattern offset\n");
381                 return len;
382         case 0x03:
383                 decode_2d_br00(data, 0, hw_offset, "XY_SETUP_CLIP_BLT");
384
385                 len = (data[0] & 0x000000ff) + 2;
386                 if (len != 3)
387                         fprintf(out, "Bad count in XY_SETUP_CLIP_BLT\n");
388                 if (count < 3)
389                         BUFFER_FAIL(count, len, "XY_SETUP_CLIP_BLT");
390
391                 instr_out(data, hw_offset, 1, "cliprect (%d,%d)\n",
392                           data[1] & 0xffff, data[2] >> 16);
393                 instr_out(data, hw_offset, 2, "cliprect (%d,%d)\n",
394                           data[2] & 0xffff, data[3] >> 16);
395                 return len;
396         case 0x11:
397                 decode_2d_br00(data, 0, hw_offset,
398                                "XY_SETUP_MONO_PATTERN_SL_BLT");
399
400                 len = (data[0] & 0x000000ff) + 2;
401                 if (len != 9)
402                         fprintf(out,
403                                 "Bad count in XY_SETUP_MONO_PATTERN_SL_BLT\n");
404                 if (count < 9)
405                         BUFFER_FAIL(count, len, "XY_SETUP_MONO_PATTERN_SL_BLT");
406
407                 decode_2d_br01(data, 1, hw_offset);
408                 instr_out(data, hw_offset, 2, "cliprect (%d,%d)\n",
409                           data[2] & 0xffff, data[2] >> 16);
410                 instr_out(data, hw_offset, 3, "cliprect (%d,%d)\n",
411                           data[3] & 0xffff, data[3] >> 16);
412                 instr_out(data, hw_offset, 4, "setup dst offset 0x%08x\n",
413                           data[4]);
414                 instr_out(data, hw_offset, 5, "setup background color\n");
415                 instr_out(data, hw_offset, 6, "setup foreground color\n");
416                 instr_out(data, hw_offset, 7, "mono pattern dw0\n");
417                 instr_out(data, hw_offset, 8, "mono pattern dw1\n");
418                 return len;
419         case 0x50:
420                 decode_2d_br00(data, 0, hw_offset, "XY_COLOR_BLT");
421
422                 len = (data[0] & 0x000000ff) + 2;
423                 if (len != 6)
424                         fprintf(out, "Bad count in XY_COLOR_BLT\n");
425                 if (count < 6)
426                         BUFFER_FAIL(count, len, "XY_COLOR_BLT");
427
428                 decode_2d_br01(data, 1, hw_offset);
429                 instr_out(data, hw_offset, 2, "(%d,%d)\n",
430                           data[2] & 0xffff, data[2] >> 16);
431                 instr_out(data, hw_offset, 3, "(%d,%d)\n",
432                           data[3] & 0xffff, data[3] >> 16);
433                 instr_out(data, hw_offset, 4, "offset 0x%08x\n", data[4]);
434                 instr_out(data, hw_offset, 5, "color\n");
435                 return len;
436         case 0x53:
437                 decode_2d_br00(data, 0, hw_offset, "XY_SRC_COPY_BLT");
438
439                 len = (data[0] & 0x000000ff) + 2;
440                 if (len != 8)
441                         fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
442                 if (count < 8)
443                         BUFFER_FAIL(count, len, "XY_SRC_COPY_BLT");
444
445                 decode_2d_br01(data, 1, hw_offset);
446                 instr_out(data, hw_offset, 2, "dst (%d,%d)\n",
447                           data[2] & 0xffff, data[2] >> 16);
448                 instr_out(data, hw_offset, 3, "dst (%d,%d)\n",
449                           data[3] & 0xffff, data[3] >> 16);
450                 instr_out(data, hw_offset, 4, "dst offset 0x%08x\n", data[4]);
451                 instr_out(data, hw_offset, 5, "src (%d,%d)\n",
452                           data[5] & 0xffff, data[5] >> 16);
453                 instr_out(data, hw_offset, 6, "src pitch %d\n",
454                           (short)(data[6] & 0xffff));
455                 instr_out(data, hw_offset, 7, "src offset 0x%08x\n", data[7]);
456                 return len;
457         }
458
459         for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
460              opcode++) {
461                 if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
462                         unsigned int i;
463
464                         len = 1;
465                         instr_out(data, hw_offset, 0, "%s\n",
466                                   opcodes_2d[opcode].name);
467                         if (opcodes_2d[opcode].max_len > 1) {
468                                 len = (data[0] & 0x000000ff) + 2;
469                                 if (len < opcodes_2d[opcode].min_len ||
470                                     len > opcodes_2d[opcode].max_len) {
471                                         fprintf(out, "Bad count in %s\n",
472                                                 opcodes_2d[opcode].name);
473                                 }
474                         }
475
476                         for (i = 1; i < len; i++) {
477                                 if (i >= count)
478                                         BUFFER_FAIL(count, len,
479                                                     opcodes_2d[opcode].name);
480                                 instr_out(data, hw_offset, i, "dword %d\n", i);
481                         }
482
483                         return len;
484                 }
485         }
486
487         instr_out(data, hw_offset, 0, "2D UNKNOWN\n");
488         (*failures)++;
489         return 1;
490 }
491
492 static int
493 decode_3d_1c(uint32_t *data, uint32_t count, uint32_t hw_offset, int *failures)
494 {
495         uint32_t opcode;
496
497         opcode = (data[0] & 0x00f80000) >> 19;
498
499         switch (opcode) {
500         case 0x11:
501                 instr_out(data, hw_offset, 0,
502                           "3DSTATE_DEPTH_SUBRECTANGLE_DISABLE\n");
503                 return 1;
504         case 0x10:
505                 instr_out(data, hw_offset, 0, "3DSTATE_SCISSOR_ENABLE %s\n",
506                           data[0] & 1 ? "enabled" : "disabled");
507                 return 1;
508         case 0x01:
509                 instr_out(data, hw_offset, 0, "3DSTATE_MAP_COORD_SET_I830\n");
510                 return 1;
511         case 0x0a:
512                 instr_out(data, hw_offset, 0, "3DSTATE_MAP_CUBE_I830\n");
513                 return 1;
514         case 0x05:
515                 instr_out(data, hw_offset, 0, "3DSTATE_MAP_TEX_STREAM_I830\n");
516                 return 1;
517         }
518
519         instr_out(data, hw_offset, 0, "3D UNKNOWN: 3d_1c opcode = 0x%x\n",
520                   opcode);
521         (*failures)++;
522         return 1;
523 }
524
525 /** Sets the string dstname to describe the destination of the PS instruction */
526 static void
527 i915_get_instruction_dst(uint32_t *data, int i, char *dstname, int do_mask)
528 {
529         uint32_t a0 = data[i];
530         int dst_nr = (a0 >> 14) & 0xf;
531         char dstmask[8];
532         const char *sat;
533
534         if (do_mask) {
535                 if (((a0 >> 10) & 0xf) == 0xf) {
536                         dstmask[0] = 0;
537                 } else {
538                         int dstmask_index = 0;
539
540                         dstmask[dstmask_index++] = '.';
541                         if (a0 & (1 << 10))
542                                 dstmask[dstmask_index++] = 'x';
543                         if (a0 & (1 << 11))
544                                 dstmask[dstmask_index++] = 'y';
545                         if (a0 & (1 << 12))
546                                 dstmask[dstmask_index++] = 'z';
547                         if (a0 & (1 << 13))
548                                 dstmask[dstmask_index++] = 'w';
549                         dstmask[dstmask_index++] = 0;
550                 }
551
552                 if (a0 & (1 << 22))
553                         sat = ".sat";
554                 else
555                         sat = "";
556         } else {
557                 dstmask[0] = 0;
558                 sat = "";
559         }
560
561         switch ((a0 >> 19) & 0x7) {
562         case 0:
563                 if (dst_nr > 15)
564                         fprintf(out, "bad destination reg R%d\n", dst_nr);
565                 sprintf(dstname, "R%d%s%s", dst_nr, dstmask, sat);
566                 break;
567         case 4:
568                 if (dst_nr > 0)
569                         fprintf(out, "bad destination reg oC%d\n", dst_nr);
570                 sprintf(dstname, "oC%s%s", dstmask, sat);
571                 break;
572         case 5:
573                 if (dst_nr > 0)
574                         fprintf(out, "bad destination reg oD%d\n", dst_nr);
575                 sprintf(dstname, "oD%s%s", dstmask, sat);
576                 break;
577         case 6:
578                 if (dst_nr > 3)
579                         fprintf(out, "bad destination reg U%d\n", dst_nr);
580                 sprintf(dstname, "U%d%s%s", dst_nr, dstmask, sat);
581                 break;
582         default:
583                 sprintf(dstname, "RESERVED");
584                 break;
585         }
586 }
587
588 static const char *
589 i915_get_channel_swizzle(uint32_t select)
590 {
591         switch (select & 0x7) {
592         case 0:
593                 return (select & 8) ? "-x" : "x";
594         case 1:
595                 return (select & 8) ? "-y" : "y";
596         case 2:
597                 return (select & 8) ? "-z" : "z";
598         case 3:
599                 return (select & 8) ? "-w" : "w";
600         case 4:
601                 return (select & 8) ? "-0" : "0";
602         case 5:
603                 return (select & 8) ? "-1" : "1";
604         default:
605                 return (select & 8) ? "-bad" : "bad";
606         }
607 }
608
609 static void
610 i915_get_instruction_src_name(uint32_t src_type, uint32_t src_nr, char *name)
611 {
612         switch (src_type) {
613         case 0:
614                 sprintf(name, "R%d", src_nr);
615                 if (src_nr > 15)
616                         fprintf(out, "bad src reg %s\n", name);
617                 break;
618         case 1:
619                 if (src_nr < 8)
620                         sprintf(name, "T%d", src_nr);
621                 else if (src_nr == 8)
622                         sprintf(name, "DIFFUSE");
623                 else if (src_nr == 9)
624                         sprintf(name, "SPECULAR");
625                 else if (src_nr == 10)
626                         sprintf(name, "FOG");
627                 else {
628                         fprintf(out, "bad src reg T%d\n", src_nr);
629                         sprintf(name, "RESERVED");
630                 }
631                 break;
632         case 2:
633                 sprintf(name, "C%d", src_nr);
634                 if (src_nr > 31)
635                         fprintf(out, "bad src reg %s\n", name);
636                 break;
637         case 4:
638                 sprintf(name, "oC");
639                 if (src_nr > 0)
640                         fprintf(out, "bad src reg oC%d\n", src_nr);
641                 break;
642         case 5:
643                 sprintf(name, "oD");
644                 if (src_nr > 0)
645                         fprintf(out, "bad src reg oD%d\n", src_nr);
646                 break;
647         case 6:
648                 sprintf(name, "U%d", src_nr);
649                 if (src_nr > 3)
650                         fprintf(out, "bad src reg %s\n", name);
651                 break;
652         default:
653                 fprintf(out, "bad src reg type %d\n", src_type);
654                 sprintf(name, "RESERVED");
655                 break;
656         }
657 }
658
659 static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
660 {
661         uint32_t a0 = data[i];
662         uint32_t a1 = data[i + 1];
663         int src_nr = (a0 >> 2) & 0x1f;
664         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 28) & 0xf);
665         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 24) & 0xf);
666         const char *swizzle_z = i915_get_channel_swizzle((a1 >> 20) & 0xf);
667         const char *swizzle_w = i915_get_channel_swizzle((a1 >> 16) & 0xf);
668         char swizzle[100];
669
670         i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
671         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
672                 swizzle_w);
673         if (strcmp(swizzle, ".xyzw") != 0)
674                 strcat(srcname, swizzle);
675 }
676
677 static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
678 {
679         uint32_t a1 = data[i + 1];
680         uint32_t a2 = data[i + 2];
681         int src_nr = (a1 >> 8) & 0x1f;
682         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 4) & 0xf);
683         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 0) & 0xf);
684         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 28) & 0xf);
685         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 24) & 0xf);
686         char swizzle[100];
687
688         i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
689         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
690                 swizzle_w);
691         if (strcmp(swizzle, ".xyzw") != 0)
692                 strcat(srcname, swizzle);
693 }
694
695 static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
696 {
697         uint32_t a2 = data[i + 2];
698         int src_nr = (a2 >> 16) & 0x1f;
699         const char *swizzle_x = i915_get_channel_swizzle((a2 >> 12) & 0xf);
700         const char *swizzle_y = i915_get_channel_swizzle((a2 >> 8) & 0xf);
701         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 4) & 0xf);
702         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 0) & 0xf);
703         char swizzle[100];
704
705         i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
706         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
707                 swizzle_w);
708         if (strcmp(swizzle, ".xyzw") != 0)
709                 strcat(srcname, swizzle);
710 }
711
712 static void
713 i915_get_instruction_addr(uint32_t src_type, uint32_t src_nr, char *name)
714 {
715         switch (src_type) {
716         case 0:
717                 sprintf(name, "R%d", src_nr);
718                 if (src_nr > 15)
719                         fprintf(out, "bad src reg %s\n", name);
720                 break;
721         case 1:
722                 if (src_nr < 8)
723                         sprintf(name, "T%d", src_nr);
724                 else if (src_nr == 8)
725                         sprintf(name, "DIFFUSE");
726                 else if (src_nr == 9)
727                         sprintf(name, "SPECULAR");
728                 else if (src_nr == 10)
729                         sprintf(name, "FOG");
730                 else {
731                         fprintf(out, "bad src reg T%d\n", src_nr);
732                         sprintf(name, "RESERVED");
733                 }
734                 break;
735         case 4:
736                 sprintf(name, "oC");
737                 if (src_nr > 0)
738                         fprintf(out, "bad src reg oC%d\n", src_nr);
739                 break;
740         case 5:
741                 sprintf(name, "oD");
742                 if (src_nr > 0)
743                         fprintf(out, "bad src reg oD%d\n", src_nr);
744                 break;
745         default:
746                 fprintf(out, "bad src reg type %d\n", src_type);
747                 sprintf(name, "RESERVED");
748                 break;
749         }
750 }
751
752 static void
753 i915_decode_alu1(uint32_t *data, uint32_t hw_offset,
754                  int i, char *instr_prefix, const char *op_name)
755 {
756         char dst[100], src0[100];
757
758         i915_get_instruction_dst(data, i, dst, 1);
759         i915_get_instruction_src0(data, i, src0);
760
761         instr_out(data, hw_offset, i++, "%s: %s %s, %s\n", instr_prefix,
762                   op_name, dst, src0);
763         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
764         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
765 }
766
767 static void
768 i915_decode_alu2(uint32_t *data, uint32_t hw_offset,
769                  int i, char *instr_prefix, const char *op_name)
770 {
771         char dst[100], src0[100], src1[100];
772
773         i915_get_instruction_dst(data, i, dst, 1);
774         i915_get_instruction_src0(data, i, src0);
775         i915_get_instruction_src1(data, i, src1);
776
777         instr_out(data, hw_offset, i++, "%s: %s %s, %s, %s\n", instr_prefix,
778                   op_name, dst, src0, src1);
779         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
780         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
781 }
782
783 static void
784 i915_decode_alu3(uint32_t *data, uint32_t hw_offset,
785                  int i, char *instr_prefix, const char *op_name)
786 {
787         char dst[100], src0[100], src1[100], src2[100];
788
789         i915_get_instruction_dst(data, i, dst, 1);
790         i915_get_instruction_src0(data, i, src0);
791         i915_get_instruction_src1(data, i, src1);
792         i915_get_instruction_src2(data, i, src2);
793
794         instr_out(data, hw_offset, i++, "%s: %s %s, %s, %s, %s\n", instr_prefix,
795                   op_name, dst, src0, src1, src2);
796         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
797         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
798 }
799
800 static void
801 i915_decode_tex(uint32_t *data, uint32_t hw_offset, int i,
802                 const char *instr_prefix, const char *tex_name)
803 {
804         uint32_t t0 = data[i];
805         uint32_t t1 = data[i + 1];
806         char dst_name[100];
807         char addr_name[100];
808         int sampler_nr;
809
810         i915_get_instruction_dst(data, i, dst_name, 0);
811         i915_get_instruction_addr((t1 >> 24) & 0x7,
812                                   (t1 >> 17) & 0xf, addr_name);
813         sampler_nr = t0 & 0xf;
814
815         instr_out(data, hw_offset, i++, "%s: %s %s, S%d, %s\n", instr_prefix,
816                   tex_name, dst_name, sampler_nr, addr_name);
817         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
818         instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
819 }
820
821 static void
822 i915_decode_dcl(uint32_t *data, uint32_t hw_offset, int i, char *instr_prefix)
823 {
824         uint32_t d0 = data[i];
825         const char *sampletype;
826         int dcl_nr = (d0 >> 14) & 0xf;
827         const char *dcl_x = d0 & (1 << 10) ? "x" : "";
828         const char *dcl_y = d0 & (1 << 11) ? "y" : "";
829         const char *dcl_z = d0 & (1 << 12) ? "z" : "";
830         const char *dcl_w = d0 & (1 << 13) ? "w" : "";
831         char dcl_mask[10];
832
833         switch ((d0 >> 19) & 0x3) {
834         case 1:
835                 sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
836                 if (strcmp(dcl_mask, ".") == 0)
837                         fprintf(out, "bad (empty) dcl mask\n");
838
839                 if (dcl_nr > 10)
840                         fprintf(out, "bad T%d dcl register number\n", dcl_nr);
841                 if (dcl_nr < 8) {
842                         if (strcmp(dcl_mask, ".x") != 0 &&
843                             strcmp(dcl_mask, ".xy") != 0 &&
844                             strcmp(dcl_mask, ".xz") != 0 &&
845                             strcmp(dcl_mask, ".w") != 0 &&
846                             strcmp(dcl_mask, ".xyzw") != 0) {
847                                 fprintf(out, "bad T%d.%s dcl mask\n", dcl_nr,
848                                         dcl_mask);
849                         }
850                         instr_out(data, hw_offset, i++, "%s: DCL T%d%s\n",
851                                   instr_prefix, dcl_nr, dcl_mask);
852                 } else {
853                         if (strcmp(dcl_mask, ".xz") == 0)
854                                 fprintf(out, "errataed bad dcl mask %s\n",
855                                         dcl_mask);
856                         else if (strcmp(dcl_mask, ".xw") == 0)
857                                 fprintf(out, "errataed bad dcl mask %s\n",
858                                         dcl_mask);
859                         else if (strcmp(dcl_mask, ".xzw") == 0)
860                                 fprintf(out, "errataed bad dcl mask %s\n",
861                                         dcl_mask);
862
863                         if (dcl_nr == 8) {
864                                 instr_out(data, hw_offset, i++,
865                                           "%s: DCL DIFFUSE%s\n", instr_prefix,
866                                           dcl_mask);
867                         } else if (dcl_nr == 9) {
868                                 instr_out(data, hw_offset, i++,
869                                           "%s: DCL SPECULAR%s\n", instr_prefix,
870                                           dcl_mask);
871                         } else if (dcl_nr == 10) {
872                                 instr_out(data, hw_offset, i++,
873                                           "%s: DCL FOG%s\n", instr_prefix,
874                                           dcl_mask);
875                         }
876                 }
877                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
878                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
879                 break;
880         case 3:
881                 switch ((d0 >> 22) & 0x3) {
882                 case 0:
883                         sampletype = "2D";
884                         break;
885                 case 1:
886                         sampletype = "CUBE";
887                         break;
888                 case 2:
889                         sampletype = "3D";
890                         break;
891                 default:
892                         sampletype = "RESERVED";
893                         break;
894                 }
895                 if (dcl_nr > 15)
896                         fprintf(out, "bad S%d dcl register number\n", dcl_nr);
897                 instr_out(data, hw_offset, i++, "%s: DCL S%d %s\n",
898                           instr_prefix, dcl_nr, sampletype);
899                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
900                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
901                 break;
902         default:
903                 instr_out(data, hw_offset, i++, "%s: DCL RESERVED%d\n",
904                           instr_prefix, dcl_nr);
905                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
906                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
907         }
908 }
909
910 static void
911 i915_decode_instruction(uint32_t *data, uint32_t hw_offset,
912                         int i, char *instr_prefix)
913 {
914         switch ((data[i] >> 24) & 0x1f) {
915         case 0x0:
916                 instr_out(data, hw_offset, i++, "%s: NOP\n", instr_prefix);
917                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
918                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
919                 break;
920         case 0x01:
921                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "ADD");
922                 break;
923         case 0x02:
924                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "MOV");
925                 break;
926         case 0x03:
927                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "MUL");
928                 break;
929         case 0x04:
930                 i915_decode_alu3(data, hw_offset, i, instr_prefix, "MAD");
931                 break;
932         case 0x05:
933                 i915_decode_alu3(data, hw_offset, i, instr_prefix, "DP2ADD");
934                 break;
935         case 0x06:
936                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "DP3");
937                 break;
938         case 0x07:
939                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "DP4");
940                 break;
941         case 0x08:
942                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "FRC");
943                 break;
944         case 0x09:
945                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "RCP");
946                 break;
947         case 0x0a:
948                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "RSQ");
949                 break;
950         case 0x0b:
951                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "EXP");
952                 break;
953         case 0x0c:
954                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "LOG");
955                 break;
956         case 0x0d:
957                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "CMP");
958                 break;
959         case 0x0e:
960                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "MIN");
961                 break;
962         case 0x0f:
963                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "MAX");
964                 break;
965         case 0x10:
966                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "FLR");
967                 break;
968         case 0x11:
969                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "MOD");
970                 break;
971         case 0x12:
972                 i915_decode_alu1(data, hw_offset, i, instr_prefix, "TRC");
973                 break;
974         case 0x13:
975                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "SGE");
976                 break;
977         case 0x14:
978                 i915_decode_alu2(data, hw_offset, i, instr_prefix, "SLT");
979                 break;
980         case 0x15:
981                 i915_decode_tex(data, hw_offset, i, instr_prefix, "TEXLD");
982                 break;
983         case 0x16:
984                 i915_decode_tex(data, hw_offset, i, instr_prefix, "TEXLDP");
985                 break;
986         case 0x17:
987                 i915_decode_tex(data, hw_offset, i, instr_prefix, "TEXLDB");
988                 break;
989         case 0x19:
990                 i915_decode_dcl(data, hw_offset, i, instr_prefix);
991                 break;
992         default:
993                 instr_out(data, hw_offset, i++, "%s: unknown\n", instr_prefix);
994                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
995                 instr_out(data, hw_offset, i++, "%s\n", instr_prefix);
996                 break;
997         }
998 }
999
1000 static const char *
1001 decode_compare_func(uint32_t op)
1002 {
1003         switch (op & 0x7) {
1004         case 0:
1005                 return "always";
1006         case 1:
1007                 return "never";
1008         case 2:
1009                 return "less";
1010         case 3:
1011                 return "equal";
1012         case 4:
1013                 return "lequal";
1014         case 5:
1015                 return "greater";
1016         case 6:
1017                 return "notequal";
1018         case 7:
1019                 return "gequal";
1020         }
1021         return "";
1022 }
1023
1024 static const char *
1025 decode_stencil_op(uint32_t op)
1026 {
1027         switch (op & 0x7) {
1028         case 0:
1029                 return "keep";
1030         case 1:
1031                 return "zero";
1032         case 2:
1033                 return "replace";
1034         case 3:
1035                 return "incr_sat";
1036         case 4:
1037                 return "decr_sat";
1038         case 5:
1039                 return "greater";
1040         case 6:
1041                 return "incr";
1042         case 7:
1043                 return "decr";
1044         }
1045         return "";
1046 }
1047
1048 #if 0
1049 static const char *
1050 decode_logic_op(uint32_t op)
1051 {
1052         switch (op & 0xf) {
1053         case 0:
1054                 return "clear";
1055         case 1:
1056                 return "nor";
1057         case 2:
1058                 return "and_inv";
1059         case 3:
1060                 return "copy_inv";
1061         case 4:
1062                 return "and_rvrse";
1063         case 5:
1064                 return "inv";
1065         case 6:
1066                 return "xor";
1067         case 7:
1068                 return "nand";
1069         case 8:
1070                 return "and";
1071         case 9:
1072                 return "equiv";
1073         case 10:
1074                 return "noop";
1075         case 11:
1076                 return "or_inv";
1077         case 12:
1078                 return "copy";
1079         case 13:
1080                 return "or_rvrse";
1081         case 14:
1082                 return "or";
1083         case 15:
1084                 return "set";
1085         }
1086         return "";
1087 }
1088 #endif
1089
1090 static const char *
1091 decode_blend_fact(uint32_t op)
1092 {
1093         switch (op & 0xf) {
1094         case 1:
1095                 return "zero";
1096         case 2:
1097                 return "one";
1098         case 3:
1099                 return "src_colr";
1100         case 4:
1101                 return "inv_src_colr";
1102         case 5:
1103                 return "src_alpha";
1104         case 6:
1105                 return "inv_src_alpha";
1106         case 7:
1107                 return "dst_alpha";
1108         case 8:
1109                 return "inv_dst_alpha";
1110         case 9:
1111                 return "dst_colr";
1112         case 10:
1113                 return "inv_dst_colr";
1114         case 11:
1115                 return "src_alpha_sat";
1116         case 12:
1117                 return "cnst_colr";
1118         case 13:
1119                 return "inv_cnst_colr";
1120         case 14:
1121                 return "cnst_alpha";
1122         case 15:
1123                 return "inv_const_alpha";
1124         }
1125         return "";
1126 }
1127
1128 static const char *
1129 decode_tex_coord_mode(uint32_t mode)
1130 {
1131         switch (mode & 0x7) {
1132         case 0:
1133                 return "wrap";
1134         case 1:
1135                 return "mirror";
1136         case 2:
1137                 return "clamp_edge";
1138         case 3:
1139                 return "cube";
1140         case 4:
1141                 return "clamp_border";
1142         case 5:
1143                 return "mirror_once";
1144         }
1145         return "";
1146 }
1147
1148 static const char *
1149 decode_sample_filter(uint32_t mode)
1150 {
1151         switch (mode & 0x7) {
1152         case 0:
1153                 return "nearest";
1154         case 1:
1155                 return "linear";
1156         case 2:
1157                 return "anisotropic";
1158         case 3:
1159                 return "4x4_1";
1160         case 4:
1161                 return "4x4_2";
1162         case 5:
1163                 return "4x4_flat";
1164         case 6:
1165                 return "6x5_mono";
1166         }
1167         return "";
1168 }
1169
1170 static int
1171 decode_3d_1d(uint32_t *data, uint32_t count,
1172              uint32_t hw_offset, uint32_t devid, int *failures)
1173 {
1174         unsigned int len, i, c, idx, word, map, sampler, instr;
1175         const char *format, *zformat, *type;
1176         uint32_t opcode;
1177
1178         struct {
1179                 uint32_t opcode;
1180                 int i830_only;
1181                 unsigned int min_len;
1182                 unsigned int max_len;
1183                 const char *name;
1184         } opcodes_3d_1d[] = {
1185                 { 0x86, 0, 4, 4, "3DSTATE_CHROMA_KEY" },
1186                 { 0x88, 0, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
1187                 { 0x99, 0, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
1188                 { 0x9a, 0, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
1189                 { 0x98, 0, 2, 2, "3DSTATE_DEFAULT_Z" },
1190                 { 0x97, 0, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
1191                 { 0x9d, 0, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
1192                 { 0x9e, 0, 4, 4, "3DSTATE_MONO_FILTER" },
1193                 { 0x89, 0, 4, 4, "3DSTATE_FOG_MODE" },
1194                 { 0x8f, 0, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
1195                 { 0x83, 0, 2, 2, "3DSTATE_SPAN_STIPPLE" },
1196                 { 0x8c, 1, 2, 2, "3DSTATE_MAP_COORD_TRANSFORM_I830" },
1197                 { 0x8b, 1, 2, 2, "3DSTATE_MAP_VERTEX_TRANSFORM_I830" },
1198                 { 0x8d, 1, 3, 3, "3DSTATE_W_STATE_I830" },
1199                 { 0x01, 1, 2, 2, "3DSTATE_COLOR_FACTOR_I830" },
1200                 { 0x02, 1, 2, 2, "3DSTATE_MAP_COORD_SETBIND_I830"},
1201         }, *opcode_3d_1d;
1202
1203         opcode = (data[0] & 0x00ff0000) >> 16;
1204
1205         switch (opcode) {
1206         case 0x07:
1207                 /* This instruction is unusual.  A 0 length means just
1208                  * 1 DWORD instead of 2.  The 0 length is specified in
1209                  * one place to be unsupported, but stated to be
1210                  * required in another, and 0 length LOAD_INDIRECTs
1211                  * appear to cause no harm at least.
1212                  */
1213                 instr_out(data, hw_offset, 0, "3DSTATE_LOAD_INDIRECT\n");
1214                 len = (data[0] & 0x000000ff) + 1;
1215                 i = 1;
1216                 if (data[0] & (0x01 << 8)) {
1217                         if (i + 2 >= count)
1218                                 BUFFER_FAIL(count, len,
1219                                             "3DSTATE_LOAD_INDIRECT");
1220                         instr_out(data, hw_offset, i++, "SIS.0\n");
1221                         instr_out(data, hw_offset, i++, "SIS.1\n");
1222                 }
1223                 if (data[0] & (0x02 << 8)) {
1224                         if (i + 1 >= count)
1225                                 BUFFER_FAIL(count, len,
1226                                             "3DSTATE_LOAD_INDIRECT");
1227                         instr_out(data, hw_offset, i++, "DIS.0\n");
1228                 }
1229                 if (data[0] & (0x04 << 8)) {
1230                         if (i + 2 >= count)
1231                                 BUFFER_FAIL(count, len,
1232                                             "3DSTATE_LOAD_INDIRECT");
1233                         instr_out(data, hw_offset, i++, "SSB.0\n");
1234                         instr_out(data, hw_offset, i++, "SSB.1\n");
1235                 }
1236                 if (data[0] & (0x08 << 8)) {
1237                         if (i + 2 >= count)
1238                                 BUFFER_FAIL(count, len,
1239                                             "3DSTATE_LOAD_INDIRECT");
1240                         instr_out(data, hw_offset, i++, "MSB.0\n");
1241                         instr_out(data, hw_offset, i++, "MSB.1\n");
1242                 }
1243                 if (data[0] & (0x10 << 8)) {
1244                         if (i + 2 >= count)
1245                                 BUFFER_FAIL(count, len,
1246                                             "3DSTATE_LOAD_INDIRECT");
1247                         instr_out(data, hw_offset, i++, "PSP.0\n");
1248                         instr_out(data, hw_offset, i++, "PSP.1\n");
1249                 }
1250                 if (data[0] & (0x20 << 8)) {
1251                         if (i + 2 >= count)
1252                                 BUFFER_FAIL(count, len,
1253                                             "3DSTATE_LOAD_INDIRECT");
1254                         instr_out(data, hw_offset, i++, "PSC.0\n");
1255                         instr_out(data, hw_offset, i++, "PSC.1\n");
1256                 }
1257                 if (len != i) {
1258                         fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
1259                         (*failures)++;
1260                         return len;
1261                 }
1262                 return len;
1263         case 0x04:
1264                 instr_out(data, hw_offset, 0,
1265                           "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1266                 len = (data[0] & 0x0000000f) + 2;
1267                 i = 1;
1268                 for (word = 0; word <= 8; word++) {
1269                         if (data[0] & (1 << (4 + word))) {
1270                                 if (i >= count)
1271                                         BUFFER_FAIL(count, len,
1272                                                     "3DSTATE_LOAD_STATE_IMMEDIATE_1");
1273
1274                                 /* save vertex state for decode */
1275                                 if (!IS_GEN2(devid)) {
1276                                         int tex_num;
1277
1278                                         if (word == 2) {
1279                                                 saved_s2_set = 1;
1280                                                 saved_s2 = data[i];
1281                                         }
1282                                         if (word == 4) {
1283                                                 saved_s4_set = 1;
1284                                                 saved_s4 = data[i];
1285                                         }
1286
1287                                         switch (word) {
1288                                         case 0:
1289                                                 instr_out(data, hw_offset, i,
1290                                                           "S0: vbo offset: 0x%08x%s\n",
1291                                                           data[i] & (~1),
1292                                                           data[i] & 1 ?
1293                                                           ", auto cache invalidate disabled"
1294                                                           : "");
1295                                                 break;
1296                                         case 1:
1297                                                 instr_out(data, hw_offset, i,
1298                                                           "S1: vertex width: %i, vertex pitch: %i\n",
1299                                                           (data[i] >> 24) &
1300                                                           0x3f,
1301                                                           (data[i] >> 16) &
1302                                                           0x3f);
1303                                                 break;
1304                                         case 2:
1305                                                 instr_out(data, hw_offset, i,
1306                                                           "S2: texcoord formats: ");
1307                                                 for (tex_num = 0;
1308                                                      tex_num < 8; tex_num++) {
1309                                                         switch ((data[i] >>
1310                                                                  tex_num *
1311                                                                  4) & 0xf) {
1312                                                         case 0:
1313                                                                 fprintf(out,
1314                                                                         "%i=2D ",
1315                                                                         tex_num);
1316                                                                 break;
1317                                                         case 1:
1318                                                                 fprintf(out,
1319                                                                         "%i=3D ",
1320                                                                         tex_num);
1321                                                                 break;
1322                                                         case 2:
1323                                                                 fprintf(out,
1324                                                                         "%i=4D ",
1325                                                                         tex_num);
1326                                                                 break;
1327                                                         case 3:
1328                                                                 fprintf(out,
1329                                                                         "%i=1D ",
1330                                                                         tex_num);
1331                                                                 break;
1332                                                         case 4:
1333                                                                 fprintf(out,
1334                                                                         "%i=2D_16 ",
1335                                                                         tex_num);
1336                                                                 break;
1337                                                         case 5:
1338                                                                 fprintf(out,
1339                                                                         "%i=4D_16 ",
1340                                                                         tex_num);
1341                                                                 break;
1342                                                         case 0xf:
1343                                                                 fprintf(out,
1344                                                                         "%i=NP ",
1345                                                                         tex_num);
1346                                                                 break;
1347                                                         }
1348                                                 }
1349                                                 fprintf(out, "\n");
1350
1351                                                 break;
1352                                         case 3:
1353                                                 instr_out(data, hw_offset, i,
1354                                                           "S3: not documented\n");
1355                                                 break;
1356                                         case 4:
1357                                                 {
1358                                                         const char *cullmode = "";
1359                                                         const char *vfmt_xyzw = "";
1360                                                         switch ((data[i] >> 13)
1361                                                                 & 0x3) {
1362                                                         case 0:
1363                                                                 cullmode =
1364                                                                     "both";
1365                                                                 break;
1366                                                         case 1:
1367                                                                 cullmode =
1368                                                                     "none";
1369                                                                 break;
1370                                                         case 2:
1371                                                                 cullmode = "cw";
1372                                                                 break;
1373                                                         case 3:
1374                                                                 cullmode =
1375                                                                     "ccw";
1376                                                                 break;
1377                                                         }
1378                                                         switch (data[i] &
1379                                                                 (7 << 6 | 1 <<
1380                                                                  2)) {
1381                                                         case 1 << 6:
1382                                                                 vfmt_xyzw =
1383                                                                     "XYZ,";
1384                                                                 break;
1385                                                         case 2 << 6:
1386                                                                 vfmt_xyzw =
1387                                                                     "XYZW,";
1388                                                                 break;
1389                                                         case 3 << 6:
1390                                                                 vfmt_xyzw =
1391                                                                     "XY,";
1392                                                                 break;
1393                                                         case 4 << 6:
1394                                                                 vfmt_xyzw =
1395                                                                     "XYW,";
1396                                                                 break;
1397                                                         case 1 << 6 | 1 << 2:
1398                                                                 vfmt_xyzw =
1399                                                                     "XYZF,";
1400                                                                 break;
1401                                                         case 2 << 6 | 1 << 2:
1402                                                                 vfmt_xyzw =
1403                                                                     "XYZWF,";
1404                                                                 break;
1405                                                         case 3 << 6 | 1 << 2:
1406                                                                 vfmt_xyzw =
1407                                                                     "XYF,";
1408                                                                 break;
1409                                                         case 4 << 6 | 1 << 2:
1410                                                                 vfmt_xyzw =
1411                                                                     "XYWF,";
1412                                                                 break;
1413                                                         }
1414                                                         instr_out(data,
1415                                                                   hw_offset, i,
1416                                                                   "S4: point_width=%i, line_width=%.1f,"
1417                                                                   "%s%s%s%s%s cullmode=%s, vfmt=%s%s%s%s%s%s "
1418                                                                   "%s%s%s%s%s\n",
1419                                                                   (data[i] >>
1420                                                                    23) & 0x1ff,
1421                                                                   ((data[i] >>
1422                                                                     19) & 0xf) /
1423                                                                   2.0,
1424                                                                   data[i] & (0xf
1425                                                                              <<
1426                                                                              15)
1427                                                                   ?
1428                                                                   " flatshade="
1429                                                                   : "",
1430                                                                   data[i] & (1
1431                                                                              <<
1432                                                                              18)
1433                                                                   ? "Alpha," :
1434                                                                   "",
1435                                                                   data[i] & (1
1436                                                                              <<
1437                                                                              17)
1438                                                                   ? "Fog," : "",
1439                                                                   data[i] & (1
1440                                                                              <<
1441                                                                              16)
1442                                                                   ? "Specular,"
1443                                                                   : "",
1444                                                                   data[i] & (1
1445                                                                              <<
1446                                                                              15)
1447                                                                   ? "Color," :
1448                                                                   "", cullmode,
1449                                                                   data[i] & (1
1450                                                                              <<
1451                                                                              12)
1452                                                                   ?
1453                                                                   "PointWidth,"
1454                                                                   : "",
1455                                                                   data[i] & (1
1456                                                                              <<
1457                                                                              11)
1458                                                                   ? "SpecFog," :
1459                                                                   "",
1460                                                                   data[i] & (1
1461                                                                              <<
1462                                                                              10)
1463                                                                   ? "Color," :
1464                                                                   "",
1465                                                                   data[i] & (1
1466                                                                              <<
1467                                                                              9)
1468                                                                   ? "DepthOfs,"
1469                                                                   : "",
1470                                                                   vfmt_xyzw,
1471                                                                   data[i] & (1
1472                                                                              <<
1473                                                                              9)
1474                                                                   ? "FogParam,"
1475                                                                   : "",
1476                                                                   data[i] & (1
1477                                                                              <<
1478                                                                              5)
1479                                                                   ?
1480                                                                   "force default diffuse, "
1481                                                                   : "",
1482                                                                   data[i] & (1
1483                                                                              <<
1484                                                                              4)
1485                                                                   ?
1486                                                                   "force default specular, "
1487                                                                   : "",
1488                                                                   data[i] & (1
1489                                                                              <<
1490                                                                              3)
1491                                                                   ?
1492                                                                   "local depth ofs enable, "
1493                                                                   : "",
1494                                                                   data[i] & (1
1495                                                                              <<
1496                                                                              1)
1497                                                                   ?
1498                                                                   "point sprite enable, "
1499                                                                   : "",
1500                                                                   data[i] & (1
1501                                                                              <<
1502                                                                              0)
1503                                                                   ?
1504                                                                   "line AA enable, "
1505                                                                   : "");
1506                                                         break;
1507                                                 }
1508                                         case 5:
1509                                                 {
1510                                                         instr_out(data,
1511                                                                   hw_offset, i,
1512                                                                   "S5:%s%s%s%s%s"
1513                                                                   "%s%s%s%s stencil_ref=0x%x, stencil_test=%s, "
1514                                                                   "stencil_fail=%s, stencil_pass_z_fail=%s, "
1515                                                                   "stencil_pass_z_pass=%s, %s%s%s%s\n",
1516                                                                   data[i] & (0xf
1517                                                                              <<
1518                                                                              28)
1519                                                                   ?
1520                                                                   " write_disable="
1521                                                                   : "",
1522                                                                   data[i] & (1
1523                                                                              <<
1524                                                                              31)
1525                                                                   ? "Alpha," :
1526                                                                   "",
1527                                                                   data[i] & (1
1528                                                                              <<
1529                                                                              30)
1530                                                                   ? "Red," : "",
1531                                                                   data[i] & (1
1532                                                                              <<
1533                                                                              29)
1534                                                                   ? "Green," :
1535                                                                   "",
1536                                                                   data[i] & (1
1537                                                                              <<
1538                                                                              28)
1539                                                                   ? "Blue," :
1540                                                                   "",
1541                                                                   data[i] & (1
1542                                                                              <<
1543                                                                              27)
1544                                                                   ?
1545                                                                   " force default point size,"
1546                                                                   : "",
1547                                                                   data[i] & (1
1548                                                                              <<
1549                                                                              26)
1550                                                                   ?
1551                                                                   " last pixel enable,"
1552                                                                   : "",
1553                                                                   data[i] & (1
1554                                                                              <<
1555                                                                              25)
1556                                                                   ?
1557                                                                   " global depth ofs enable,"
1558                                                                   : "",
1559                                                                   data[i] & (1
1560                                                                              <<
1561                                                                              24)
1562                                                                   ?
1563                                                                   " fog enable,"
1564                                                                   : "",
1565                                                                   (data[i] >>
1566                                                                    16) & 0xff,
1567                                                                   decode_compare_func
1568                                                                   (data[i] >>
1569                                                                    13),
1570                                                                   decode_stencil_op
1571                                                                   (data[i] >>
1572                                                                    10),
1573                                                                   decode_stencil_op
1574                                                                   (data[i] >>
1575                                                                    7),
1576                                                                   decode_stencil_op
1577                                                                   (data[i] >>
1578                                                                    4),
1579                                                                   data[i] & (1
1580                                                                              <<
1581                                                                              3)
1582                                                                   ?
1583                                                                   "stencil write enable, "
1584                                                                   : "",
1585                                                                   data[i] & (1
1586                                                                              <<
1587                                                                              2)
1588                                                                   ?
1589                                                                   "stencil test enable, "
1590                                                                   : "",
1591                                                                   data[i] & (1
1592                                                                              <<
1593                                                                              1)
1594                                                                   ?
1595                                                                   "color dither enable, "
1596                                                                   : "",
1597                                                                   data[i] & (1
1598                                                                              <<
1599                                                                              0)
1600                                                                   ?
1601                                                                   "logicop enable, "
1602                                                                   : "");
1603                                                 }
1604                                                 break;
1605                                         case 6:
1606                                                 instr_out(data, hw_offset, i,
1607                                                           "S6: %salpha_test=%s, alpha_ref=0x%x, "
1608                                                           "depth_test=%s, %ssrc_blnd_fct=%s, dst_blnd_fct=%s, "
1609                                                           "%s%stristrip_provoking_vertex=%i\n",
1610                                                           data[i] & (1 << 31) ?
1611                                                           "alpha test enable, "
1612                                                           : "",
1613                                                           decode_compare_func
1614                                                           (data[i] >> 28),
1615                                                           data[i] & (0xff <<
1616                                                                      20),
1617                                                           decode_compare_func
1618                                                           (data[i] >> 16),
1619                                                           data[i] & (1 << 15) ?
1620                                                           "cbuf blend enable, "
1621                                                           : "",
1622                                                           decode_blend_fact(data
1623                                                                             [i]
1624                                                                             >>
1625                                                                             8),
1626                                                           decode_blend_fact(data
1627                                                                             [i]
1628                                                                             >>
1629                                                                             4),
1630                                                           data[i] & (1 << 3) ?
1631                                                           "depth write enable, "
1632                                                           : "",
1633                                                           data[i] & (1 << 2) ?
1634                                                           "cbuf write enable, "
1635                                                           : "",
1636                                                           data[i] & (0x3));
1637                                                 break;
1638                                         case 7:
1639                                                 instr_out(data, hw_offset, i,
1640                                                           "S7: depth offset constant: 0x%08x\n",
1641                                                           data[i]);
1642                                                 break;
1643                                         }
1644                                 } else {
1645                                         instr_out(data, hw_offset, i,
1646                                                   "S%d: 0x%08x\n", i, data[i]);
1647                                 }
1648                                 i++;
1649                         }
1650                 }
1651                 if (len != i) {
1652                         fprintf(out,
1653                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1654                         (*failures)++;
1655                 }
1656                 return len;
1657         case 0x03:
1658                 instr_out(data, hw_offset, 0,
1659                           "3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1660                 len = (data[0] & 0x0000000f) + 2;
1661                 i = 1;
1662                 for (word = 6; word <= 14; word++) {
1663                         if (data[0] & (1 << word)) {
1664                                 if (i >= count)
1665                                         BUFFER_FAIL(count, len,
1666                                                     "3DSTATE_LOAD_STATE_IMMEDIATE_2");
1667
1668                                 if (word == 6)
1669                                         instr_out(data, hw_offset, i++,
1670                                                   "TBCF\n");
1671                                 else if (word >= 7 && word <= 10) {
1672                                         instr_out(data, hw_offset, i++,
1673                                                   "TB%dC\n", word - 7);
1674                                         instr_out(data, hw_offset, i++,
1675                                                   "TB%dA\n", word - 7);
1676                                 } else if (word >= 11 && word <= 14) {
1677                                         instr_out(data, hw_offset, i,
1678                                                   "TM%dS0: offset=0x%08x, %s\n",
1679                                                   word - 11,
1680                                                   data[i] & 0xfffffffe,
1681                                                   data[i] & 1 ? "use fence" :
1682                                                   "");
1683                                         i++;
1684                                         instr_out(data, hw_offset, i,
1685                                                   "TM%dS1: height=%i, width=%i, %s\n",
1686                                                   word - 11, data[i] >> 21,
1687                                                   (data[i] >> 10) & 0x3ff,
1688                                                   data[i] & 2 ? (data[i] & 1 ?
1689                                                                  "y-tiled" :
1690                                                                  "x-tiled") :
1691                                                   "");
1692                                         i++;
1693                                         instr_out(data, hw_offset, i,
1694                                                   "TM%dS2: pitch=%i, \n",
1695                                                   word - 11,
1696                                                   ((data[i] >> 21) + 1) * 4);
1697                                         i++;
1698                                         instr_out(data, hw_offset, i++,
1699                                                   "TM%dS3\n", word - 11);
1700                                         instr_out(data, hw_offset, i++,
1701                                                   "TM%dS4: dflt color\n",
1702                                                   word - 11);
1703                                 }
1704                         }
1705                 }
1706                 if (len != i) {
1707                         fprintf(out,
1708                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1709                         (*failures)++;
1710                 }
1711                 return len;
1712         case 0x00:
1713                 instr_out(data, hw_offset, 0, "3DSTATE_MAP_STATE\n");
1714                 len = (data[0] & 0x0000003f) + 2;
1715                 instr_out(data, hw_offset, 1, "mask\n");
1716
1717                 i = 2;
1718                 for (map = 0; map <= 15; map++) {
1719                         if (data[1] & (1 << map)) {
1720                                 int width, height, pitch, dword;
1721                                 const char *tiling;
1722
1723                                 if (i + 3 >= count)
1724                                         BUFFER_FAIL(count, len,
1725                                                     "3DSTATE_MAP_STATE");
1726
1727                                 dword = data[i];
1728                                 instr_out(data, hw_offset, i++,
1729                                           "map %d MS2 %s%s%s\n", map,
1730                                           dword & (1 << 31) ?
1731                                           "untrusted surface, " : "",
1732                                           dword & (1 << 1) ?
1733                                           "vertical line stride enable, " : "",
1734                                           dword & (1 << 0) ?
1735                                           "vertical ofs enable, " : "");
1736
1737                                 dword = data[i];
1738                                 width = ((dword >> 10) & ((1 << 11) - 1)) + 1;
1739                                 height = ((dword >> 21) & ((1 << 11) - 1)) + 1;
1740
1741                                 tiling = "none";
1742                                 if (dword & (1 << 2))
1743                                         tiling = "fenced";
1744                                 else if (dword & (1 << 1))
1745                                         tiling = dword & (1 << 0) ? "Y" : "X";
1746                                 type = " BAD";
1747                                 format = "BAD";
1748                                 switch ((dword >> 7) & 0x7) {
1749                                 case 1:
1750                                         type = "8b";
1751                                         switch ((dword >> 3) & 0xf) {
1752                                         case 0:
1753                                                 format = "I";
1754                                                 break;
1755                                         case 1:
1756                                                 format = "L";
1757                                                 break;
1758                                         case 4:
1759                                                 format = "A";
1760                                                 break;
1761                                         case 5:
1762                                                 format = " mono";
1763                                                 break;
1764                                         }
1765                                         break;
1766                                 case 2:
1767                                         type = "16b";
1768                                         switch ((dword >> 3) & 0xf) {
1769                                         case 0:
1770                                                 format = " rgb565";
1771                                                 break;
1772                                         case 1:
1773                                                 format = " argb1555";
1774                                                 break;
1775                                         case 2:
1776                                                 format = " argb4444";
1777                                                 break;
1778                                         case 5:
1779                                                 format = " ay88";
1780                                                 break;
1781                                         case 6:
1782                                                 format = " bump655";
1783                                                 break;
1784                                         case 7:
1785                                                 format = "I";
1786                                                 break;
1787                                         case 8:
1788                                                 format = "L";
1789                                                 break;
1790                                         case 9:
1791                                                 format = "A";
1792                                                 break;
1793                                         }
1794                                         break;
1795                                 case 3:
1796                                         type = "32b";
1797                                         switch ((dword >> 3) & 0xf) {
1798                                         case 0:
1799                                                 format = " argb8888";
1800                                                 break;
1801                                         case 1:
1802                                                 format = " abgr8888";
1803                                                 break;
1804                                         case 2:
1805                                                 format = " xrgb8888";
1806                                                 break;
1807                                         case 3:
1808                                                 format = " xbgr8888";
1809                                                 break;
1810                                         case 4:
1811                                                 format = " qwvu8888";
1812                                                 break;
1813                                         case 5:
1814                                                 format = " axvu8888";
1815                                                 break;
1816                                         case 6:
1817                                                 format = " lxvu8888";
1818                                                 break;
1819                                         case 7:
1820                                                 format = " xlvu8888";
1821                                                 break;
1822                                         case 8:
1823                                                 format = " argb2101010";
1824                                                 break;
1825                                         case 9:
1826                                                 format = " abgr2101010";
1827                                                 break;
1828                                         case 10:
1829                                                 format = " awvu2101010";
1830                                                 break;
1831                                         case 11:
1832                                                 format = " gr1616";
1833                                                 break;
1834                                         case 12:
1835                                                 format = " vu1616";
1836                                                 break;
1837                                         case 13:
1838                                                 format = " xI824";
1839                                                 break;
1840                                         case 14:
1841                                                 format = " xA824";
1842                                                 break;
1843                                         case 15:
1844                                                 format = " xL824";
1845                                                 break;
1846                                         }
1847                                         break;
1848                                 case 5:
1849                                         type = "422";
1850                                         switch ((dword >> 3) & 0xf) {
1851                                         case 0:
1852                                                 format = " yuv_swapy";
1853                                                 break;
1854                                         case 1:
1855                                                 format = " yuv";
1856                                                 break;
1857                                         case 2:
1858                                                 format = " yuv_swapuv";
1859                                                 break;
1860                                         case 3:
1861                                                 format = " yuv_swapuvy";
1862                                                 break;
1863                                         }
1864                                         break;
1865                                 case 6:
1866                                         type = "compressed";
1867                                         switch ((dword >> 3) & 0x7) {
1868                                         case 0:
1869                                                 format = " dxt1";
1870                                                 break;
1871                                         case 1:
1872                                                 format = " dxt2_3";
1873                                                 break;
1874                                         case 2:
1875                                                 format = " dxt4_5";
1876                                                 break;
1877                                         case 3:
1878                                                 format = " fxt1";
1879                                                 break;
1880                                         case 4:
1881                                                 format = " dxt1_rb";
1882                                                 break;
1883                                         }
1884                                         break;
1885                                 case 7:
1886                                         type = "4b indexed";
1887                                         switch ((dword >> 3) & 0xf) {
1888                                         case 7:
1889                                                 format = " argb8888";
1890                                                 break;
1891                                         }
1892                                         break;
1893                                 }
1894                                 dword = data[i];
1895                                 instr_out(data, hw_offset, i++,
1896                                           "map %d MS3 [width=%d, height=%d, format=%s%s, tiling=%s%s]\n",
1897                                           map, width, height, type, format,
1898                                           tiling,
1899                                           dword & (1 << 9) ? " palette select" :
1900                                           "");
1901
1902                                 dword = data[i];
1903                                 pitch =
1904                                     4 * (((dword >> 21) & ((1 << 11) - 1)) + 1);
1905                                 instr_out(data, hw_offset, i++,
1906                                           "map %d MS4 [pitch=%d, max_lod=%i, vol_depth=%i, cube_face_ena=%x, %s]\n",
1907                                           map, pitch, (dword >> 9) & 0x3f,
1908                                           dword & 0xff, (dword >> 15) & 0x3f,
1909                                           dword & (1 << 8) ? "miplayout legacy"
1910                                           : "miplayout right");
1911                         }
1912                 }
1913                 if (len != i) {
1914                         fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
1915                         (*failures)++;
1916                         return len;
1917                 }
1918                 return len;
1919         case 0x06:
1920                 instr_out(data, hw_offset, 0,
1921                           "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1922                 len = (data[0] & 0x000000ff) + 2;
1923
1924                 i = 2;
1925                 for (c = 0; c <= 31; c++) {
1926                         if (data[1] & (1 << c)) {
1927                                 if (i + 4 >= count)
1928                                         BUFFER_FAIL(count, len,
1929                                                     "3DSTATE_PIXEL_SHADER_CONSTANTS");
1930                                 instr_out(data, hw_offset, i, "C%d.X = %f\n", c,
1931                                           int_as_float(data[i]));
1932                                 i++;
1933                                 instr_out(data, hw_offset, i, "C%d.Y = %f\n",
1934                                           c, int_as_float(data[i]));
1935                                 i++;
1936                                 instr_out(data, hw_offset, i, "C%d.Z = %f\n",
1937                                           c, int_as_float(data[i]));
1938                                 i++;
1939                                 instr_out(data, hw_offset, i, "C%d.W = %f\n",
1940                                           c, int_as_float(data[i]));
1941                                 i++;
1942                         }
1943                 }
1944                 if (len != i) {
1945                         fprintf(out,
1946                                 "Bad count in 3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1947                         (*failures)++;
1948                 }
1949                 return len;
1950         case 0x05:
1951                 instr_out(data, hw_offset, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
1952                 len = (data[0] & 0x000000ff) + 2;
1953                 if ((len - 1) % 3 != 0 || len > 370) {
1954                         fprintf(out,
1955                                 "Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
1956                         (*failures)++;
1957                 }
1958                 i = 1;
1959                 for (instr = 0; instr < (len - 1) / 3; instr++) {
1960                         char instr_prefix[10];
1961
1962                         if (i + 3 >= count)
1963                                 BUFFER_FAIL(count, len,
1964                                             "3DSTATE_PIXEL_SHADER_PROGRAM");
1965                         sprintf(instr_prefix, "PS%03d", instr);
1966                         i915_decode_instruction(data, hw_offset, i,
1967                                                 instr_prefix);
1968                         i += 3;
1969                 }
1970                 return len;
1971         case 0x01:
1972                 if (IS_GEN2(devid))
1973                         break;
1974                 instr_out(data, hw_offset, 0, "3DSTATE_SAMPLER_STATE\n");
1975                 instr_out(data, hw_offset, 1, "mask\n");
1976                 len = (data[0] & 0x0000003f) + 2;
1977                 i = 2;
1978                 for (sampler = 0; sampler <= 15; sampler++) {
1979                         if (data[1] & (1 << sampler)) {
1980                                 uint32_t dword;
1981                                 const char *mip_filter = "";
1982                                 if (i + 3 >= count)
1983                                         BUFFER_FAIL(count, len,
1984                                                     "3DSTATE_SAMPLER_STATE");
1985                                 dword = data[i];
1986                                 switch ((dword >> 20) & 0x3) {
1987                                 case 0:
1988                                         mip_filter = "none";
1989                                         break;
1990                                 case 1:
1991                                         mip_filter = "nearest";
1992                                         break;
1993                                 case 3:
1994                                         mip_filter = "linear";
1995                                         break;
1996                                 }
1997                                 instr_out(data, hw_offset, i++,
1998                                           "sampler %d SS2:%s%s%s "
1999                                           "base_mip_level=%i, mip_filter=%s, mag_filter=%s, min_filter=%s "
2000                                           "lod_bias=%.2f,%s max_aniso=%i, shadow_func=%s\n",
2001                                           sampler,
2002                                           dword & (1 << 31) ? " reverse gamma,"
2003                                           : "",
2004                                           dword & (1 << 30) ? " packed2planar,"
2005                                           : "",
2006                                           dword & (1 << 29) ?
2007                                           " colorspace conversion," : "",
2008                                           (dword >> 22) & 0x1f, mip_filter,
2009                                           decode_sample_filter(dword >> 17),
2010                                           decode_sample_filter(dword >> 14),
2011                                           ((dword >> 5) & 0x1ff) / (0x10 * 1.0),
2012                                           dword & (1 << 4) ? " shadow," : "",
2013                                           dword & (1 << 3) ? 4 : 2,
2014                                           decode_compare_func(dword));
2015                                 dword = data[i];
2016                                 instr_out(data, hw_offset, i++,
2017                                           "sampler %d SS3: min_lod=%.2f,%s "
2018                                           "tcmode_x=%s, tcmode_y=%s, tcmode_z=%s,%s texmap_idx=%i,%s\n",
2019                                           sampler,
2020                                           ((dword >> 24) & 0xff) / (0x10 * 1.0),
2021                                           dword & (1 << 17) ?
2022                                           " kill pixel enable," : "",
2023                                           decode_tex_coord_mode(dword >> 12),
2024                                           decode_tex_coord_mode(dword >> 9),
2025                                           decode_tex_coord_mode(dword >> 6),
2026                                           dword & (1 << 5) ?
2027                                           " normalized coords," : "",
2028                                           (dword >> 1) & 0xf,
2029                                           dword & (1 << 0) ? " deinterlacer," :
2030                                           "");
2031                                 dword = data[i];
2032                                 instr_out(data, hw_offset, i++,
2033                                           "sampler %d SS4: border color\n",
2034                                           sampler);
2035                         }
2036                 }
2037                 if (len != i) {
2038                         fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
2039                         (*failures)++;
2040                 }
2041                 return len;
2042         case 0x85:
2043                 len = (data[0] & 0x0000000f) + 2;
2044
2045                 if (len != 2)
2046                         fprintf(out,
2047                                 "Bad count in 3DSTATE_DEST_BUFFER_VARIABLES\n");
2048                 if (count < 2)
2049                         BUFFER_FAIL(count, len,
2050                                     "3DSTATE_DEST_BUFFER_VARIABLES");
2051
2052                 instr_out(data, hw_offset, 0,
2053                           "3DSTATE_DEST_BUFFER_VARIABLES\n");
2054
2055                 switch ((data[1] >> 8) & 0xf) {
2056                 case 0x0:
2057                         format = "g8";
2058                         break;
2059                 case 0x1:
2060                         format = "x1r5g5b5";
2061                         break;
2062                 case 0x2:
2063                         format = "r5g6b5";
2064                         break;
2065                 case 0x3:
2066                         format = "a8r8g8b8";
2067                         break;
2068                 case 0x4:
2069                         format = "ycrcb_swapy";
2070                         break;
2071                 case 0x5:
2072                         format = "ycrcb_normal";
2073                         break;
2074                 case 0x6:
2075                         format = "ycrcb_swapuv";
2076                         break;
2077                 case 0x7:
2078                         format = "ycrcb_swapuvy";
2079                         break;
2080                 case 0x8:
2081                         format = "a4r4g4b4";
2082                         break;
2083                 case 0x9:
2084                         format = "a1r5g5b5";
2085                         break;
2086                 case 0xa:
2087                         format = "a2r10g10b10";
2088                         break;
2089                 default:
2090                         format = "BAD";
2091                         break;
2092                 }
2093                 switch ((data[1] >> 2) & 0x3) {
2094                 case 0x0:
2095                         zformat = "u16";
2096                         break;
2097                 case 0x1:
2098                         zformat = "f16";
2099                         break;
2100                 case 0x2:
2101                         zformat = "u24x8";
2102                         break;
2103                 default:
2104                         zformat = "BAD";
2105                         break;
2106                 }
2107                 instr_out(data, hw_offset, 1,
2108                           "%s format, %s depth format, early Z %sabled\n",
2109                           format, zformat,
2110                           (data[1] & (1 << 31)) ? "en" : "dis");
2111                 return len;
2112
2113         case 0x8e:
2114                 {
2115                         const char *name, *tiling;
2116
2117                         len = (data[0] & 0x0000000f) + 2;
2118                         if (len != 3)
2119                                 fprintf(out,
2120                                         "Bad count in 3DSTATE_BUFFER_INFO\n");
2121                         if (count < 3)
2122                                 BUFFER_FAIL(count, len, "3DSTATE_BUFFER_INFO");
2123
2124                         switch ((data[1] >> 24) & 0x7) {
2125                         case 0x3:
2126                                 name = "color";
2127                                 break;
2128                         case 0x7:
2129                                 name = "depth";
2130                                 break;
2131                         default:
2132                                 name = "unknown";
2133                                 break;
2134                         }
2135
2136                         tiling = "none";
2137                         if (data[1] & (1 << 23))
2138                                 tiling = "fenced";
2139                         else if (data[1] & (1 << 22))
2140                                 tiling = data[1] & (1 << 21) ? "Y" : "X";
2141
2142                         instr_out(data, hw_offset, 0, "3DSTATE_BUFFER_INFO\n");
2143                         instr_out(data, hw_offset, 1,
2144                                   "%s, tiling = %s, pitch=%d\n", name, tiling,
2145                                   data[1] & 0xffff);
2146
2147                         instr_out(data, hw_offset, 2, "address\n");
2148                         return len;
2149                 }
2150         case 0x81:
2151                 len = (data[0] & 0x0000000f) + 2;
2152
2153                 if (len != 3)