OSDN Git Service

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