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