OSDN Git Service

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