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