OSDN Git Service

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