19a8d368318b8291be17386e3bd3fd3efc8deff1
[glsdk/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  */
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>
32 #include "intel_chipset.h"
33 #include "intel_bufmgr.h"
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;
40         /** PCI device ID. */
41         uint32_t devid;
43         /**
44          * Shorthand device identifier: 3 is 915, 4 is 965, 5 is
45          * Ironlake, etc.
46          */
47         int gen;
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;
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;
63         /** @{
64          * GPU head and tail pointers, which will be noted in the dump, or ~0.
65          */
66         uint32_t head, tail;
67         /** @} */
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;
77         bool overflowed;
78 };
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 */
86 #ifndef ARRAY_SIZE
87 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
88 #endif
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)
96 static float int_as_float(uint32_t intval)
97 {
98         union intfloat {
99                 uint32_t i;
100                 float f;
101         } uval;
103         uval.i = intval;
104         return uval.f;
107 static void
108 instr_out(struct drm_intel_decode *ctx, unsigned int index,
109           const char *fmt, ...) __attribute__((format(__printf__, 3, 4)));
111 static void
112 instr_out(struct drm_intel_decode *ctx, unsigned int index,
113           const char *fmt, ...)
115         va_list va;
116         const char *parseinfo;
117         uint32_t offset = ctx->hw_offset + index * 4;
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         }
127         if (offset == head_offset)
128                 parseinfo = "HEAD";
129         else if (offset == tail_offset)
130                 parseinfo = "TAIL";
131         else
132                 parseinfo = "    ";
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);
141 static int
142 decode_MI_SET_CONTEXT(struct drm_intel_decode *ctx)
144         uint32_t data = ctx->data[1];
145         if (ctx->gen > 7)
146                 return 1;
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": "");
154         return 2;
157 static int
158 decode_MI_WAIT_FOR_EVENT(struct drm_intel_decode *ctx)
160         const char *cc_wait;
161         int cc_shift = 0;
162         uint32_t data = ctx->data[0];
164         if (ctx->gen <= 5)
165                 cc_shift = 9;
166         else
167                 cc_shift = 16;
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         }
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         }
222         return 1;
225 static int
226 decode_mi(struct drm_intel_decode *ctx)
228         unsigned int opcode, len = -1;
229         const char *post_sync_op = "";
230         uint32_t *data = ctx->data;
232         struct {
233                 uint32_t opcode;
234                 int len_mask;
235                 unsigned int min_len;
236                 unsigned int max_len;
237                 const char *name;
238                 int (*func)(struct drm_intel_decode *ctx);
239         } opcodes_mi[] = {
240                 { 0x08, 0, 1, 1, "MI_ARB_ON_OFF" },
241                 { 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
242                 { 0x30, 0x3f, 3, 3, "MI_BATCH_BUFFER" },
243                 { 0x31, 0x3f, 2, 2, "MI_BATCH_BUFFER_START" },
244                 { 0x14, 0x3f, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
245                 { 0x04, 0, 1, 1, "MI_FLUSH" },
246                 { 0x22, 0x1f, 3, 3, "MI_LOAD_REGISTER_IMM" },
247                 { 0x13, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
248                 { 0x12, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
249                 { 0x00, 0, 1, 1, "MI_NOOP" },
250                 { 0x11, 0x3f, 2, 2, "MI_OVERLAY_FLIP" },
251                 { 0x07, 0, 1, 1, "MI_REPORT_HEAD" },
252                 { 0x18, 0x3f, 2, 2, "MI_SET_CONTEXT", decode_MI_SET_CONTEXT },
253                 { 0x20, 0x3f, 3, 4, "MI_STORE_DATA_IMM" },
254                 { 0x21, 0x3f, 3, 4, "MI_STORE_DATA_INDEX" },
255                 { 0x24, 0x3f, 3, 3, "MI_STORE_REGISTER_MEM" },
256                 { 0x02, 0, 1, 1, "MI_USER_INTERRUPT" },
257                 { 0x03, 0, 1, 1, "MI_WAIT_FOR_EVENT", decode_MI_WAIT_FOR_EVENT },
258                 { 0x16, 0x7f, 3, 3, "MI_SEMAPHORE_MBOX" },
259                 { 0x26, 0x1f, 3, 4, "MI_FLUSH_DW" },
260                 { 0x0b, 0, 1, 1, "MI_SUSPEND_FLUSH"},
261         }, *opcode_mi = NULL;
263         /* check instruction length */
264         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
265              opcode++) {
266                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
267                         len = 1;
268                         if (opcodes_mi[opcode].max_len > 1) {
269                                 len =
270                                     (data[0] & opcodes_mi[opcode].len_mask) + 2;
271                                 if (len < opcodes_mi[opcode].min_len
272                                     || len > opcodes_mi[opcode].max_len) {
273                                         fprintf(out,
274                                                 "Bad length (%d) in %s, [%d, %d]\n",
275                                                 len, opcodes_mi[opcode].name,
276                                                 opcodes_mi[opcode].min_len,
277                                                 opcodes_mi[opcode].max_len);
278                                 }
279                         }
280                         opcode_mi = &opcodes_mi[opcode];
281                         break;
282                 }
283         }
285         if (opcode_mi && opcode_mi->func)
286                 return opcode_mi->func(ctx);
288         switch ((data[0] & 0x1f800000) >> 23) {
289         case 0x0a:
290                 instr_out(ctx, 0, "MI_BATCH_BUFFER_END\n");
291                 return -1;
292         case 0x16:
293                 instr_out(ctx, 0, "MI_SEMAPHORE_MBOX%s%s%s%s %u\n",
294                           data[0] & (1 << 22) ? " global gtt," : "",
295                           data[0] & (1 << 21) ? " update semaphore," : "",
296                           data[0] & (1 << 20) ? " compare semaphore," : "",
297                           data[0] & (1 << 18) ? " use compare reg" : "",
298                           (data[0] & (0x3 << 16)) >> 16);
299                 instr_out(ctx, 1, "value\n");
300                 instr_out(ctx, 2, "address\n");
301                 return len;
302         case 0x21:
303                 instr_out(ctx, 0, "MI_STORE_DATA_INDEX%s\n",
304                           data[0] & (1 << 21) ? " use per-process HWS," : "");
305                 instr_out(ctx, 1, "index\n");
306                 instr_out(ctx, 2, "dword\n");
307                 if (len == 4)
308                         instr_out(ctx, 3, "upper dword\n");
309                 return len;
310         case 0x00:
311                 if (data[0] & (1 << 22))
312                         instr_out(ctx, 0,
313                                   "MI_NOOP write NOPID reg, val=0x%x\n",
314                                   data[0] & ((1 << 22) - 1));
315                 else
316                         instr_out(ctx, 0, "MI_NOOP\n");
317                 return len;
318         case 0x26:
319                 switch (data[0] & (0x3 << 14)) {
320                 case (0 << 14):
321                         post_sync_op = "no write";
322                         break;
323                 case (1 << 14):
324                         post_sync_op = "write data";
325                         break;
326                 case (2 << 14):
327                         post_sync_op = "reserved";
328                         break;
329                 case (3 << 14):
330                         post_sync_op = "write TIMESTAMP";
331                         break;
332                 }
333                 instr_out(ctx, 0,
334                           "MI_FLUSH_DW%s%s%s%s post_sync_op='%s' %s%s\n",
335                           data[0] & (1 << 22) ?
336                           " enable protected mem (BCS-only)," : "",
337                           data[0] & (1 << 21) ? " store in hws," : "",
338                           data[0] & (1 << 18) ? " invalidate tlb," : "",
339                           data[0] & (1 << 17) ? " flush gfdt," : "",
340                           post_sync_op,
341                           data[0] & (1 << 8) ? " enable notify interrupt," : "",
342                           data[0] & (1 << 7) ?
343                           " invalidate video state (BCS-only)," : "");
344                 if (data[0] & (1 << 21))
345                         instr_out(ctx, 1, "hws index\n");
346                 else
347                         instr_out(ctx, 1, "address\n");
348                 instr_out(ctx, 2, "dword\n");
349                 if (len == 4)
350                         instr_out(ctx, 3, "upper dword\n");
351                 return len;
352         }
354         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
355              opcode++) {
356                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
357                         unsigned int i;
359                         instr_out(ctx, 0, "%s\n",
360                                   opcodes_mi[opcode].name);
361                         for (i = 1; i < len; i++) {
362                                 instr_out(ctx, i, "dword %d\n", i);
363                         }
365                         return len;
366                 }
367         }
369         instr_out(ctx, 0, "MI UNKNOWN\n");
370         return 1;
373 static void
374 decode_2d_br00(struct drm_intel_decode *ctx, const char *cmd)
376         instr_out(ctx, 0,
377                   "%s (rgb %sabled, alpha %sabled, src tile %d, dst tile %d)\n",
378                   cmd,
379                   (ctx->data[0] & (1 << 20)) ? "en" : "dis",
380                   (ctx->data[0] & (1 << 21)) ? "en" : "dis",
381                   (ctx->data[0] >> 15) & 1,
382                   (ctx->data[0] >> 11) & 1);
385 static void
386 decode_2d_br01(struct drm_intel_decode *ctx)
388         const char *format;
389         switch ((ctx->data[1] >> 24) & 0x3) {
390         case 0:
391                 format = "8";
392                 break;
393         case 1:
394                 format = "565";
395                 break;
396         case 2:
397                 format = "1555";
398                 break;
399         case 3:
400                 format = "8888";
401                 break;
402         }
404         instr_out(ctx, 1,
405                   "format %s, pitch %d, rop 0x%02x, "
406                   "clipping %sabled, %s%s \n",
407                   format,
408                   (short)(ctx->data[1] & 0xffff),
409                   (ctx->data[1] >> 16) & 0xff,
410                   ctx->data[1] & (1 << 30) ? "en" : "dis",
411                   ctx->data[1] & (1 << 31) ? "solid pattern enabled, " : "",
412                   ctx->data[1] & (1 << 31) ?
413                   "mono pattern transparency enabled, " : "");
417 static int
418 decode_2d(struct drm_intel_decode *ctx)
420         unsigned int opcode, len;
421         uint32_t *data = ctx->data;
423         struct {
424                 uint32_t opcode;
425                 unsigned int min_len;
426                 unsigned int max_len;
427                 const char *name;
428         } opcodes_2d[] = {
429                 { 0x40, 5, 5, "COLOR_BLT" },
430                 { 0x43, 6, 6, "SRC_COPY_BLT" },
431                 { 0x01, 8, 8, "XY_SETUP_BLT" },
432                 { 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
433                 { 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
434                 { 0x24, 2, 2, "XY_PIXEL_BLT" },
435                 { 0x25, 3, 3, "XY_SCANLINES_BLT" },
436                 { 0x26, 4, 4, "Y_TEXT_BLT" },
437                 { 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
438                 { 0x50, 6, 6, "XY_COLOR_BLT" },
439                 { 0x51, 6, 6, "XY_PAT_BLT" },
440                 { 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
441                 { 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
442                 { 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
443                 { 0x52, 9, 9, "XY_MONO_PAT_BLT" },
444                 { 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
445                 { 0x53, 8, 8, "XY_SRC_COPY_BLT" },
446                 { 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
447                 { 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
448                 { 0x55, 9, 9, "XY_FULL_BLT" },
449                 { 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
450                 { 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
451                 { 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
452                 { 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
453                 { 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT"},
454         };
456         switch ((data[0] & 0x1fc00000) >> 22) {
457         case 0x25:
458                 instr_out(ctx, 0,
459                           "XY_SCANLINES_BLT (pattern seed (%d, %d), dst tile %d)\n",
460                           (data[0] >> 12) & 0x8,
461                           (data[0] >> 8) & 0x8, (data[0] >> 11) & 1);
463                 len = (data[0] & 0x000000ff) + 2;
464                 if (len != 3)
465                         fprintf(out, "Bad count in XY_SCANLINES_BLT\n");
467                 instr_out(ctx, 1, "dest (%d,%d)\n",
468                           data[1] & 0xffff, data[1] >> 16);
469                 instr_out(ctx, 2, "dest (%d,%d)\n",
470                           data[2] & 0xffff, data[2] >> 16);
471                 return len;
472         case 0x01:
473                 decode_2d_br00(ctx, "XY_SETUP_BLT");
475                 len = (data[0] & 0x000000ff) + 2;
476                 if (len != 8)
477                         fprintf(out, "Bad count in XY_SETUP_BLT\n");
479                 decode_2d_br01(ctx);
480                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
481                           data[2] & 0xffff, data[2] >> 16);
482                 instr_out(ctx, 3, "cliprect (%d,%d)\n",
483                           data[3] & 0xffff, data[3] >> 16);
484                 instr_out(ctx, 4, "setup dst offset 0x%08x\n",
485                           data[4]);
486                 instr_out(ctx, 5, "setup background color\n");
487                 instr_out(ctx, 6, "setup foreground color\n");
488                 instr_out(ctx, 7, "color pattern offset\n");
489                 return len;
490         case 0x03:
491                 decode_2d_br00(ctx, "XY_SETUP_CLIP_BLT");
493                 len = (data[0] & 0x000000ff) + 2;
494                 if (len != 3)
495                         fprintf(out, "Bad count in XY_SETUP_CLIP_BLT\n");
497                 instr_out(ctx, 1, "cliprect (%d,%d)\n",
498                           data[1] & 0xffff, data[2] >> 16);
499                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
500                           data[2] & 0xffff, data[3] >> 16);
501                 return len;
502         case 0x11:
503                 decode_2d_br00(ctx, "XY_SETUP_MONO_PATTERN_SL_BLT");
505                 len = (data[0] & 0x000000ff) + 2;
506                 if (len != 9)
507                         fprintf(out,
508                                 "Bad count in XY_SETUP_MONO_PATTERN_SL_BLT\n");
510                 decode_2d_br01(ctx);
511                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
512                           data[2] & 0xffff, data[2] >> 16);
513                 instr_out(ctx, 3, "cliprect (%d,%d)\n",
514                           data[3] & 0xffff, data[3] >> 16);
515                 instr_out(ctx, 4, "setup dst offset 0x%08x\n",
516                           data[4]);
517                 instr_out(ctx, 5, "setup background color\n");
518                 instr_out(ctx, 6, "setup foreground color\n");
519                 instr_out(ctx, 7, "mono pattern dw0\n");
520                 instr_out(ctx, 8, "mono pattern dw1\n");
521                 return len;
522         case 0x50:
523                 decode_2d_br00(ctx, "XY_COLOR_BLT");
525                 len = (data[0] & 0x000000ff) + 2;
526                 if (len != 6)
527                         fprintf(out, "Bad count in XY_COLOR_BLT\n");
529                 decode_2d_br01(ctx);
530                 instr_out(ctx, 2, "(%d,%d)\n",
531                           data[2] & 0xffff, data[2] >> 16);
532                 instr_out(ctx, 3, "(%d,%d)\n",
533                           data[3] & 0xffff, data[3] >> 16);
534                 instr_out(ctx, 4, "offset 0x%08x\n", data[4]);
535                 instr_out(ctx, 5, "color\n");
536                 return len;
537         case 0x53:
538                 decode_2d_br00(ctx, "XY_SRC_COPY_BLT");
540                 len = (data[0] & 0x000000ff) + 2;
541                 if (len != 8)
542                         fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
544                 decode_2d_br01(ctx);
545                 instr_out(ctx, 2, "dst (%d,%d)\n",
546                           data[2] & 0xffff, data[2] >> 16);
547                 instr_out(ctx, 3, "dst (%d,%d)\n",
548                           data[3] & 0xffff, data[3] >> 16);
549                 instr_out(ctx, 4, "dst offset 0x%08x\n", data[4]);
550                 instr_out(ctx, 5, "src (%d,%d)\n",
551                           data[5] & 0xffff, data[5] >> 16);
552                 instr_out(ctx, 6, "src pitch %d\n",
553                           (short)(data[6] & 0xffff));
554                 instr_out(ctx, 7, "src offset 0x%08x\n", data[7]);
555                 return len;
556         }
558         for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
559              opcode++) {
560                 if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
561                         unsigned int i;
563                         len = 1;
564                         instr_out(ctx, 0, "%s\n",
565                                   opcodes_2d[opcode].name);
566                         if (opcodes_2d[opcode].max_len > 1) {
567                                 len = (data[0] & 0x000000ff) + 2;
568                                 if (len < opcodes_2d[opcode].min_len ||
569                                     len > opcodes_2d[opcode].max_len) {
570                                         fprintf(out, "Bad count in %s\n",
571                                                 opcodes_2d[opcode].name);
572                                 }
573                         }
575                         for (i = 1; i < len; i++) {
576                                 instr_out(ctx, i, "dword %d\n", i);
577                         }
579                         return len;
580                 }
581         }
583         instr_out(ctx, 0, "2D UNKNOWN\n");
584         return 1;
587 static int
588 decode_3d_1c(struct drm_intel_decode *ctx)
590         uint32_t *data = ctx->data;
591         uint32_t opcode;
593         opcode = (data[0] & 0x00f80000) >> 19;
595         switch (opcode) {
596         case 0x11:
597                 instr_out(ctx, 0,
598                           "3DSTATE_DEPTH_SUBRECTANGLE_DISABLE\n");
599                 return 1;
600         case 0x10:
601                 instr_out(ctx, 0, "3DSTATE_SCISSOR_ENABLE %s\n",
602                           data[0] & 1 ? "enabled" : "disabled");
603                 return 1;
604         case 0x01:
605                 instr_out(ctx, 0, "3DSTATE_MAP_COORD_SET_I830\n");
606                 return 1;
607         case 0x0a:
608                 instr_out(ctx, 0, "3DSTATE_MAP_CUBE_I830\n");
609                 return 1;
610         case 0x05:
611                 instr_out(ctx, 0, "3DSTATE_MAP_TEX_STREAM_I830\n");
612                 return 1;
613         }
615         instr_out(ctx, 0, "3D UNKNOWN: 3d_1c opcode = 0x%x\n",
616                   opcode);
617         return 1;
620 /** Sets the string dstname to describe the destination of the PS instruction */
621 static void
622 i915_get_instruction_dst(uint32_t *data, int i, char *dstname, int do_mask)
624         uint32_t a0 = data[i];
625         int dst_nr = (a0 >> 14) & 0xf;
626         char dstmask[8];
627         const char *sat;
629         if (do_mask) {
630                 if (((a0 >> 10) & 0xf) == 0xf) {
631                         dstmask[0] = 0;
632                 } else {
633                         int dstmask_index = 0;
635                         dstmask[dstmask_index++] = '.';
636                         if (a0 & (1 << 10))
637                                 dstmask[dstmask_index++] = 'x';
638                         if (a0 & (1 << 11))
639                                 dstmask[dstmask_index++] = 'y';
640                         if (a0 & (1 << 12))
641                                 dstmask[dstmask_index++] = 'z';
642                         if (a0 & (1 << 13))
643                                 dstmask[dstmask_index++] = 'w';
644                         dstmask[dstmask_index++] = 0;
645                 }
647                 if (a0 & (1 << 22))
648                         sat = ".sat";
649                 else
650                         sat = "";
651         } else {
652                 dstmask[0] = 0;
653                 sat = "";
654         }
656         switch ((a0 >> 19) & 0x7) {
657         case 0:
658                 if (dst_nr > 15)
659                         fprintf(out, "bad destination reg R%d\n", dst_nr);
660                 sprintf(dstname, "R%d%s%s", dst_nr, dstmask, sat);
661                 break;
662         case 4:
663                 if (dst_nr > 0)
664                         fprintf(out, "bad destination reg oC%d\n", dst_nr);
665                 sprintf(dstname, "oC%s%s", dstmask, sat);
666                 break;
667         case 5:
668                 if (dst_nr > 0)
669                         fprintf(out, "bad destination reg oD%d\n", dst_nr);
670                 sprintf(dstname, "oD%s%s", dstmask, sat);
671                 break;
672         case 6:
673                 if (dst_nr > 3)
674                         fprintf(out, "bad destination reg U%d\n", dst_nr);
675                 sprintf(dstname, "U%d%s%s", dst_nr, dstmask, sat);
676                 break;
677         default:
678                 sprintf(dstname, "RESERVED");
679                 break;
680         }
683 static const char *
684 i915_get_channel_swizzle(uint32_t select)
686         switch (select & 0x7) {
687         case 0:
688                 return (select & 8) ? "-x" : "x";
689         case 1:
690                 return (select & 8) ? "-y" : "y";
691         case 2:
692                 return (select & 8) ? "-z" : "z";
693         case 3:
694                 return (select & 8) ? "-w" : "w";
695         case 4:
696                 return (select & 8) ? "-0" : "0";
697         case 5:
698                 return (select & 8) ? "-1" : "1";
699         default:
700                 return (select & 8) ? "-bad" : "bad";
701         }
704 static void
705 i915_get_instruction_src_name(uint32_t src_type, uint32_t src_nr, char *name)
707         switch (src_type) {
708         case 0:
709                 sprintf(name, "R%d", src_nr);
710                 if (src_nr > 15)
711                         fprintf(out, "bad src reg %s\n", name);
712                 break;
713         case 1:
714                 if (src_nr < 8)
715                         sprintf(name, "T%d", src_nr);
716                 else if (src_nr == 8)
717                         sprintf(name, "DIFFUSE");
718                 else if (src_nr == 9)
719                         sprintf(name, "SPECULAR");
720                 else if (src_nr == 10)
721                         sprintf(name, "FOG");
722                 else {
723                         fprintf(out, "bad src reg T%d\n", src_nr);
724                         sprintf(name, "RESERVED");
725                 }
726                 break;
727         case 2:
728                 sprintf(name, "C%d", src_nr);
729                 if (src_nr > 31)
730                         fprintf(out, "bad src reg %s\n", name);
731                 break;
732         case 4:
733                 sprintf(name, "oC");
734                 if (src_nr > 0)
735                         fprintf(out, "bad src reg oC%d\n", src_nr);
736                 break;
737         case 5:
738                 sprintf(name, "oD");
739                 if (src_nr > 0)
740                         fprintf(out, "bad src reg oD%d\n", src_nr);
741                 break;
742         case 6:
743                 sprintf(name, "U%d", src_nr);
744                 if (src_nr > 3)
745                         fprintf(out, "bad src reg %s\n", name);
746                 break;
747         default:
748                 fprintf(out, "bad src reg type %d\n", src_type);
749                 sprintf(name, "RESERVED");
750                 break;
751         }
754 static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
756         uint32_t a0 = data[i];
757         uint32_t a1 = data[i + 1];
758         int src_nr = (a0 >> 2) & 0x1f;
759         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 28) & 0xf);
760         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 24) & 0xf);
761         const char *swizzle_z = i915_get_channel_swizzle((a1 >> 20) & 0xf);
762         const char *swizzle_w = i915_get_channel_swizzle((a1 >> 16) & 0xf);
763         char swizzle[100];
765         i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
766         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
767                 swizzle_w);
768         if (strcmp(swizzle, ".xyzw") != 0)
769                 strcat(srcname, swizzle);
772 static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
774         uint32_t a1 = data[i + 1];
775         uint32_t a2 = data[i + 2];
776         int src_nr = (a1 >> 8) & 0x1f;
777         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 4) & 0xf);
778         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 0) & 0xf);
779         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 28) & 0xf);
780         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 24) & 0xf);
781         char swizzle[100];
783         i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
784         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
785                 swizzle_w);
786         if (strcmp(swizzle, ".xyzw") != 0)
787                 strcat(srcname, swizzle);
790 static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
792         uint32_t a2 = data[i + 2];
793         int src_nr = (a2 >> 16) & 0x1f;
794         const char *swizzle_x = i915_get_channel_swizzle((a2 >> 12) & 0xf);
795         const char *swizzle_y = i915_get_channel_swizzle((a2 >> 8) & 0xf);
796         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 4) & 0xf);
797         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 0) & 0xf);
798         char swizzle[100];
800         i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
801         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
802                 swizzle_w);
803         if (strcmp(swizzle, ".xyzw") != 0)
804                 strcat(srcname, swizzle);
807 static void
808 i915_get_instruction_addr(uint32_t src_type, uint32_t src_nr, char *name)
810         switch (src_type) {
811         case 0:
812                 sprintf(name, "R%d", src_nr);
813                 if (src_nr > 15)
814                         fprintf(out, "bad src reg %s\n", name);
815                 break;
816         case 1:
817                 if (src_nr < 8)
818                         sprintf(name, "T%d", src_nr);
819                 else if (src_nr == 8)
820                         sprintf(name, "DIFFUSE");
821                 else if (src_nr == 9)
822                         sprintf(name, "SPECULAR");
823                 else if (src_nr == 10)
824                         sprintf(name, "FOG");
825                 else {
826                         fprintf(out, "bad src reg T%d\n", src_nr);
827                         sprintf(name, "RESERVED");
828                 }
829                 break;
830         case 4:
831                 sprintf(name, "oC");
832                 if (src_nr > 0)
833                         fprintf(out, "bad src reg oC%d\n", src_nr);
834                 break;
835         case 5:
836                 sprintf(name, "oD");
837                 if (src_nr > 0)
838                         fprintf(out, "bad src reg oD%d\n", src_nr);
839                 break;
840         default:
841                 fprintf(out, "bad src reg type %d\n", src_type);
842                 sprintf(name, "RESERVED");
843                 break;
844         }
847 static void
848 i915_decode_alu1(struct drm_intel_decode *ctx,
849                  int i, char *instr_prefix, const char *op_name)
851         char dst[100], src0[100];
853         i915_get_instruction_dst(ctx->data, i, dst, 1);
854         i915_get_instruction_src0(ctx->data, i, src0);
856         instr_out(ctx, i++, "%s: %s %s, %s\n", instr_prefix,
857                   op_name, dst, src0);
858         instr_out(ctx, i++, "%s\n", instr_prefix);
859         instr_out(ctx, i++, "%s\n", instr_prefix);
862 static void
863 i915_decode_alu2(struct drm_intel_decode *ctx,
864                  int i, char *instr_prefix, const char *op_name)
866         char dst[100], src0[100], src1[100];
868         i915_get_instruction_dst(ctx->data, i, dst, 1);
869         i915_get_instruction_src0(ctx->data, i, src0);
870         i915_get_instruction_src1(ctx->data, i, src1);
872         instr_out(ctx, i++, "%s: %s %s, %s, %s\n", instr_prefix,
873                   op_name, dst, src0, src1);
874         instr_out(ctx, i++, "%s\n", instr_prefix);
875         instr_out(ctx, i++, "%s\n", instr_prefix);
878 static void
879 i915_decode_alu3(struct drm_intel_decode *ctx,
880                  int i, char *instr_prefix, const char *op_name)
882         char dst[100], src0[100], src1[100], src2[100];
884         i915_get_instruction_dst(ctx->data, i, dst, 1);
885         i915_get_instruction_src0(ctx->data, i, src0);
886         i915_get_instruction_src1(ctx->data, i, src1);
887         i915_get_instruction_src2(ctx->data, i, src2);
889         instr_out(ctx, i++, "%s: %s %s, %s, %s, %s\n", instr_prefix,
890                   op_name, dst, src0, src1, src2);
891         instr_out(ctx, i++, "%s\n", instr_prefix);
892         instr_out(ctx, i++, "%s\n", instr_prefix);
895 static void
896 i915_decode_tex(struct drm_intel_decode *ctx, int i,
897                 const char *instr_prefix, const char *tex_name)
899         uint32_t t0 = ctx->data[i];
900         uint32_t t1 = ctx->data[i + 1];
901         char dst_name[100];
902         char addr_name[100];
903         int sampler_nr;
905         i915_get_instruction_dst(ctx->data, i, dst_name, 0);
906         i915_get_instruction_addr((t1 >> 24) & 0x7,
907                                   (t1 >> 17) & 0xf, addr_name);
908         sampler_nr = t0 & 0xf;
910         instr_out(ctx, i++, "%s: %s %s, S%d, %s\n", instr_prefix,
911                   tex_name, dst_name, sampler_nr, addr_name);
912         instr_out(ctx, i++, "%s\n", instr_prefix);
913         instr_out(ctx, i++, "%s\n", instr_prefix);
916 static void
917 i915_decode_dcl(struct drm_intel_decode *ctx, int i, char *instr_prefix)
919         uint32_t d0 = ctx->data[i];
920         const char *sampletype;
921         int dcl_nr = (d0 >> 14) & 0xf;
922         const char *dcl_x = d0 & (1 << 10) ? "x" : "";
923         const char *dcl_y = d0 & (1 << 11) ? "y" : "";
924         const char *dcl_z = d0 & (1 << 12) ? "z" : "";
925         const char *dcl_w = d0 & (1 << 13) ? "w" : "";
926         char dcl_mask[10];
928         switch ((d0 >> 19) & 0x3) {
929         case 1:
930                 sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
931                 if (strcmp(dcl_mask, ".") == 0)
932                         fprintf(out, "bad (empty) dcl mask\n");
934                 if (dcl_nr > 10)
935                         fprintf(out, "bad T%d dcl register number\n", dcl_nr);
936                 if (dcl_nr < 8) {
937                         if (strcmp(dcl_mask, ".x") != 0 &&
938                             strcmp(dcl_mask, ".xy") != 0 &&
939                             strcmp(dcl_mask, ".xz") != 0 &&
940                             strcmp(dcl_mask, ".w") != 0 &&
941                             strcmp(dcl_mask, ".xyzw") != 0) {
942                                 fprintf(out, "bad T%d.%s dcl mask\n", dcl_nr,
943                                         dcl_mask);
944                         }
945                         instr_out(ctx, i++, "%s: DCL T%d%s\n",
946                                   instr_prefix, dcl_nr, dcl_mask);
947                 } else {
948                         if (strcmp(dcl_mask, ".xz") == 0)
949                                 fprintf(out, "errataed bad dcl mask %s\n",
950                                         dcl_mask);
951                         else if (strcmp(dcl_mask, ".xw") == 0)
952                                 fprintf(out, "errataed bad dcl mask %s\n",
953                                         dcl_mask);
954                         else if (strcmp(dcl_mask, ".xzw") == 0)
955                                 fprintf(out, "errataed bad dcl mask %s\n",
956                                         dcl_mask);
958                         if (dcl_nr == 8) {
959                                 instr_out(ctx, i++,
960                                           "%s: DCL DIFFUSE%s\n", instr_prefix,
961                                           dcl_mask);
962                         } else if (dcl_nr == 9) {
963                                 instr_out(ctx, i++,
964                                           "%s: DCL SPECULAR%s\n", instr_prefix,
965                                           dcl_mask);
966                         } else if (dcl_nr == 10) {
967                                 instr_out(ctx, i++,
968                                           "%s: DCL FOG%s\n", instr_prefix,
969                                           dcl_mask);
970                         }
971                 }
972                 instr_out(ctx, i++, "%s\n", instr_prefix);
973                 instr_out(ctx, i++, "%s\n", instr_prefix);
974                 break;
975         case 3:
976                 switch ((d0 >> 22) & 0x3) {
977                 case 0:
978                         sampletype = "2D";
979                         break;
980                 case 1:
981                         sampletype = "CUBE";
982                         break;
983                 case 2:
984                         sampletype = "3D";
985                         break;
986                 default:
987                         sampletype = "RESERVED";
988                         break;
989                 }
990                 if (dcl_nr > 15)
991                         fprintf(out, "bad S%d dcl register number\n", dcl_nr);
992                 instr_out(ctx, i++, "%s: DCL S%d %s\n",
993                           instr_prefix, dcl_nr, sampletype);
994                 instr_out(ctx, i++, "%s\n", instr_prefix);
995                 instr_out(ctx, i++, "%s\n", instr_prefix);
996                 break;
997         default:
998                 instr_out(ctx, i++, "%s: DCL RESERVED%d\n",
999                           instr_prefix, dcl_nr);
1000                 instr_out(ctx, i++, "%s\n", instr_prefix);
1001                 instr_out(ctx, i++, "%s\n", instr_prefix);
1002         }
1005 static void
1006 i915_decode_instruction(struct drm_intel_decode *ctx,
1007                         int i, char *instr_prefix)
1009         switch ((ctx->data[i] >> 24) & 0x1f) {
1010         case 0x0:
1011                 instr_out(ctx, i++, "%s: NOP\n", instr_prefix);
1012                 instr_out(ctx, i++, "%s\n", instr_prefix);
1013                 instr_out(ctx, i++, "%s\n", instr_prefix);
1014                 break;
1015         case 0x01:
1016                 i915_decode_alu2(ctx, i, instr_prefix, "ADD");
1017                 break;
1018         case 0x02:
1019                 i915_decode_alu1(ctx, i, instr_prefix, "MOV");
1020                 break;
1021         case 0x03:
1022                 i915_decode_alu2(ctx, i, instr_prefix, "MUL");
1023                 break;
1024         case 0x04:
1025                 i915_decode_alu3(ctx, i, instr_prefix, "MAD");
1026                 break;
1027         case 0x05:
1028                 i915_decode_alu3(ctx, i, instr_prefix, "DP2ADD");
1029                 break;
1030         case 0x06:
1031                 i915_decode_alu2(ctx, i, instr_prefix, "DP3");
1032                 break;
1033         case 0x07:
1034                 i915_decode_alu2(ctx, i, instr_prefix, "DP4");
1035                 break;
1036         case 0x08:
1037                 i915_decode_alu1(ctx, i, instr_prefix, "FRC");
1038                 break;
1039         case 0x09:
1040                 i915_decode_alu1(ctx, i, instr_prefix, "RCP");
1041                 break;
1042         case 0x0a:
1043                 i915_decode_alu1(ctx, i, instr_prefix, "RSQ");
1044                 break;
1045         case 0x0b:
1046                 i915_decode_alu1(ctx, i, instr_prefix, "EXP");
1047                 break;
1048         case 0x0c:
1049                 i915_decode_alu1(ctx, i, instr_prefix, "LOG");
1050                 break;
1051         case 0x0d:
1052                 i915_decode_alu2(ctx, i, instr_prefix, "CMP");
1053                 break;
1054         case 0x0e:
1055                 i915_decode_alu2(ctx, i, instr_prefix, "MIN");
1056                 break;
1057         case 0x0f:
1058                 i915_decode_alu2(ctx, i, instr_prefix, "MAX");
1059                 break;
1060         case 0x10:
1061                 i915_decode_alu1(ctx, i, instr_prefix, "FLR");
1062                 break;
1063         case 0x11:
1064                 i915_decode_alu1(ctx, i, instr_prefix, "MOD");
1065                 break;
1066         case 0x12:
1067                 i915_decode_alu1(ctx, i, instr_prefix, "TRC");
1068                 break;
1069         case 0x13:
1070                 i915_decode_alu2(ctx, i, instr_prefix, "SGE");
1071                 break;
1072         case 0x14:
1073                 i915_decode_alu2(ctx, i, instr_prefix, "SLT");
1074                 break;
1075         case 0x15:
1076                 i915_decode_tex(ctx, i, instr_prefix, "TEXLD");
1077                 break;
1078         case 0x16:
1079                 i915_decode_tex(ctx, i, instr_prefix, "TEXLDP");
1080                 break;
1081         case 0x17:
1082                 i915_decode_tex(ctx, i, instr_prefix, "TEXLDB");
1083                 break;
1084         case 0x19:
1085                 i915_decode_dcl(ctx, i, instr_prefix);
1086                 break;
1087         default:
1088                 instr_out(ctx, i++, "%s: unknown\n", instr_prefix);
1089                 instr_out(ctx, i++, "%s\n", instr_prefix);
1090                 instr_out(ctx, i++, "%s\n", instr_prefix);
1091                 break;
1092         }
1095 static const char *
1096 decode_compare_func(uint32_t op)
1098         switch (op & 0x7) {
1099         case 0:
1100                 return "always";
1101         case 1:
1102                 return "never";
1103         case 2:
1104                 return "less";
1105         case 3:
1106                 return "equal";
1107         case 4:
1108                 return "lequal";
1109         case 5:
1110                 return "greater";
1111         case 6:
1112                 return "notequal";
1113         case 7:
1114                 return "gequal";
1115         }
1116         return "";
1119 static const char *
1120 decode_stencil_op(uint32_t op)
1122         switch (op & 0x7) {
1123         case 0:
1124                 return "keep";
1125         case 1:
1126                 return "zero";
1127         case 2:
1128                 return "replace";
1129         case 3:
1130                 return "incr_sat";
1131         case 4:
1132                 return "decr_sat";
1133         case 5:
1134                 return "greater";
1135         case 6:
1136                 return "incr";
1137         case 7:
1138                 return "decr";
1139         }
1140         return "";
1143 #if 0
1144 static const char *
1145 decode_logic_op(uint32_t op)
1147         switch (op & 0xf) {
1148         case 0:
1149                 return "clear";
1150         case 1:
1151                 return "nor";
1152         case 2:
1153                 return "and_inv";
1154         case 3:
1155                 return "copy_inv";
1156         case 4:
1157                 return "and_rvrse";
1158         case 5:
1159                 return "inv";
1160         case 6:
1161                 return "xor";
1162         case 7:
1163                 return "nand";
1164         case 8:
1165                 return "and";
1166         case 9:
1167                 return "equiv";
1168         case 10:
1169                 return "noop";
1170         case 11:
1171                 return "or_inv";
1172         case 12:
1173                 return "copy";
1174         case 13:
1175                 return "or_rvrse";
1176         case 14:
1177                 return "or";
1178         case 15:
1179                 return "set";
1180         }
1181         return "";
1183 #endif
1185 static const char *
1186 decode_blend_fact(uint32_t op)
1188         switch (op & 0xf) {
1189         case 1:
1190                 return "zero";
1191         case 2:
1192                 return "one";
1193         case 3:
1194                 return "src_colr";
1195         case 4:
1196                 return "inv_src_colr";
1197         case 5:
1198                 return "src_alpha";
1199         case 6:
1200                 return "inv_src_alpha";
1201         case 7:
1202                 return "dst_alpha";
1203         case 8:
1204                 return "inv_dst_alpha";
1205         case 9:
1206                 return "dst_colr";
1207         case 10:
1208                 return "inv_dst_colr";
1209         case 11:
1210                 return "src_alpha_sat";
1211         case 12:
1212                 return "cnst_colr";
1213         case 13:
1214                 return "inv_cnst_colr";
1215         case 14:
1216                 return "cnst_alpha";
1217         case 15:
1218                 return "inv_const_alpha";
1219         }
1220         return "";
1223 static const char *
1224 decode_tex_coord_mode(uint32_t mode)
1226         switch (mode & 0x7) {
1227         case 0:
1228                 return "wrap";
1229         case 1:
1230                 return "mirror";
1231         case 2:
1232                 return "clamp_edge";
1233         case 3:
1234                 return "cube";
1235         case 4:
1236                 return "clamp_border";
1237         case 5:
1238                 return "mirror_once";
1239         }
1240         return "";
1243 static const char *
1244 decode_sample_filter(uint32_t mode)
1246         switch (mode & 0x7) {
1247         case 0:
1248                 return "nearest";
1249         case 1:
1250                 return "linear";
1251         case 2:
1252                 return "anisotropic";
1253         case 3:
1254                 return "4x4_1";
1255         case 4:
1256                 return "4x4_2";
1257         case 5:
1258                 return "4x4_flat";
1259         case 6:
1260                 return "6x5_mono";
1261         }
1262         return "";
1265 static int
1266 decode_3d_1d(struct drm_intel_decode *ctx)
1268         unsigned int len, i, c, idx, word, map, sampler, instr;
1269         const char *format, *zformat, *type;
1270         uint32_t opcode;
1271         uint32_t *data = ctx->data;
1272         uint32_t devid = ctx->devid;
1274         struct {
1275                 uint32_t opcode;
1276                 int i830_only;
1277                 unsigned int min_len;
1278                 unsigned int max_len;
1279                 const char *name;
1280         } opcodes_3d_1d[] = {
1281                 { 0x86, 0, 4, 4, "3DSTATE_CHROMA_KEY" },
1282                 { 0x88, 0, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
1283                 { 0x99, 0, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
1284                 { 0x9a, 0, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
1285                 { 0x98, 0, 2, 2, "3DSTATE_DEFAULT_Z" },
1286                 { 0x97, 0, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
1287                 { 0x9d, 0, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
1288                 { 0x9e, 0, 4, 4, "3DSTATE_MONO_FILTER" },
1289                 { 0x89, 0, 4, 4, "3DSTATE_FOG_MODE" },
1290                 { 0x8f, 0, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
1291                 { 0x83, 0, 2, 2, "3DSTATE_SPAN_STIPPLE" },
1292                 { 0x8c, 1, 2, 2, "3DSTATE_MAP_COORD_TRANSFORM_I830" },
1293                 { 0x8b, 1, 2, 2, "3DSTATE_MAP_VERTEX_TRANSFORM_I830" },
1294                 { 0x8d, 1, 3, 3, "3DSTATE_W_STATE_I830" },
1295                 { 0x01, 1, 2, 2, "3DSTATE_COLOR_FACTOR_I830" },
1296                 { 0x02, 1, 2, 2, "3DSTATE_MAP_COORD_SETBIND_I830"},
1297         }, *opcode_3d_1d;
1299         opcode = (data[0] & 0x00ff0000) >> 16;
1301         switch (opcode) {
1302         case 0x07:
1303                 /* This instruction is unusual.  A 0 length means just
1304                  * 1 DWORD instead of 2.  The 0 length is specified in
1305                  * one place to be unsupported, but stated to be
1306                  * required in another, and 0 length LOAD_INDIRECTs
1307                  * appear to cause no harm at least.
1308                  */
1309                 instr_out(ctx, 0, "3DSTATE_LOAD_INDIRECT\n");
1310                 len = (data[0] & 0x000000ff) + 1;
1311                 i = 1;
1312                 if (data[0] & (0x01 << 8)) {
1313                         instr_out(ctx, i++, "SIS.0\n");
1314                         instr_out(ctx, i++, "SIS.1\n");
1315                 }
1316                 if (data[0] & (0x02 << 8)) {
1317                         instr_out(ctx, i++, "DIS.0\n");
1318                 }
1319                 if (data[0] & (0x04 << 8)) {
1320                         instr_out(ctx, i++, "SSB.0\n");
1321                         instr_out(ctx, i++, "SSB.1\n");
1322                 }
1323                 if (data[0] & (0x08 << 8)) {
1324                         instr_out(ctx, i++, "MSB.0\n");
1325                         instr_out(ctx, i++, "MSB.1\n");
1326                 }
1327                 if (data[0] & (0x10 << 8)) {
1328                         instr_out(ctx, i++, "PSP.0\n");
1329                         instr_out(ctx, i++, "PSP.1\n");
1330                 }
1331                 if (data[0] & (0x20 << 8)) {
1332                         instr_out(ctx, i++, "PSC.0\n");
1333                         instr_out(ctx, i++, "PSC.1\n");
1334                 }
1335                 if (len != i) {
1336                         fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
1337                         return len;
1338                 }
1339                 return len;
1340         case 0x04:
1341                 instr_out(ctx, 0,
1342                           "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1343                 len = (data[0] & 0x0000000f) + 2;
1344                 i = 1;
1345                 for (word = 0; word <= 8; word++) {
1346                         if (data[0] & (1 << (4 + word))) {
1347                                 /* save vertex state for decode */
1348                                 if (!IS_GEN2(devid)) {
1349                                         int tex_num;
1351                                         if (word == 2) {
1352                                                 saved_s2_set = 1;
1353                                                 saved_s2 = data[i];
1354                                         }
1355                                         if (word == 4) {
1356                                                 saved_s4_set = 1;
1357                                                 saved_s4 = data[i];
1358                                         }
1360                                         switch (word) {
1361                                         case 0:
1362                                                 instr_out(ctx, i,
1363                                                           "S0: vbo offset: 0x%08x%s\n",
1364                                                           data[i] & (~1),
1365                                                           data[i] & 1 ?
1366                                                           ", auto cache invalidate disabled"
1367                                                           : "");
1368                                                 break;
1369                                         case 1:
1370                                                 instr_out(ctx, i,
1371                                                           "S1: vertex width: %i, vertex pitch: %i\n",
1372                                                           (data[i] >> 24) &
1373                                                           0x3f,
1374                                                           (data[i] >> 16) &
1375                                                           0x3f);
1376                                                 break;
1377                                         case 2:
1378                                                 instr_out(ctx, i,
1379                                                           "S2: texcoord formats: ");
1380                                                 for (tex_num = 0;
1381                                                      tex_num < 8; tex_num++) {
1382                                                         switch ((data[i] >>
1383                                                                  tex_num *
1384                                                                  4) & 0xf) {
1385                                                         case 0:
1386                                                                 fprintf(out,
1387                                                                         "%i=2D ",
1388                                                                         tex_num);
1389                                                                 break;
1390                                                         case 1:
1391                                                                 fprintf(out,
1392                                                                         "%i=3D ",
1393                                                                         tex_num);
1394                                                                 break;
1395                                                         case 2:
1396                                                                 fprintf(out,
1397                                                                         "%i=4D ",
1398                                                                         tex_num);
1399                                                                 break;
1400                                                         case 3:
1401                                                                 fprintf(out,
1402                                                                         "%i=1D ",
1403                                                                         tex_num);
1404                                                                 break;
1405                                                         case 4:
1406                                                                 fprintf(out,
1407                                                                         "%i=2D_16 ",
1408                                                                         tex_num);
1409                                                                 break;
1410                                                         case 5:
1411                                                                 fprintf(out,
1412                                                                         "%i=4D_16 ",
1413                                                                         tex_num);
1414                                                                 break;
1415                                                         case 0xf:
1416                                                                 fprintf(out,
1417                                                                         "%i=NP ",
1418                                                                         tex_num);
1419                                                                 break;
1420                                                         }
1421                                                 }
1422                                                 fprintf(out, "\n");
1424                                                 break;
1425                                         case 3:
1426                                                 instr_out(ctx, i,
1427                                                           "S3: not documented\n");
1428                                                 break;
1429                                         case 4:
1430                                                 {
1431                                                         const char *cullmode = "";
1432                                                         const char *vfmt_xyzw = "";
1433                                                         switch ((data[i] >> 13)
1434                                                                 & 0x3) {
1435                                                         case 0:
1436                                                                 cullmode =
1437                                                                     "both";
1438                                                                 break;
1439                                                         case 1:
1440                                                                 cullmode =
1441                                                                     "none";
1442                                                                 break;
1443                                                         case 2:
1444                                                                 cullmode = "cw";
1445                                                                 break;
1446                                                         case 3:
1447                                                                 cullmode =
1448                                                                     "ccw";
1449                                                                 break;
1450                                                         }
1451                                                         switch (data[i] &
1452                                                                 (7 << 6 | 1 <<
1453                                                                  2)) {
1454                                                         case 1 << 6:
1455                                                                 vfmt_xyzw =
1456                                                                     "XYZ,";
1457                                                                 break;
1458                                                         case 2 << 6:
1459                                                                 vfmt_xyzw =
1460                                                                     "XYZW,";
1461                                                                 break;
1462                                                         case 3 << 6:
1463                                                                 vfmt_xyzw =
1464                                                                     "XY,";
1465                                                                 break;
1466                                                         case 4 << 6:
1467                                                                 vfmt_xyzw =
1468                                                                     "XYW,";
1469                                                                 break;
1470                                                         case 1 << 6 | 1 << 2:
1471                                                                 vfmt_xyzw =
1472                                                                     "XYZF,";
1473                                                                 break;
1474                                                         case 2 << 6 | 1 << 2:
1475                                                                 vfmt_xyzw =
1476                                                                     "XYZWF,";
1477                                                                 break;
1478                                                         case 3 << 6 | 1 << 2:
1479                                                                 vfmt_xyzw =
1480                                                                     "XYF,";
1481                                                                 break;
1482                                                         case 4 << 6 | 1 << 2:
1483                                                                 vfmt_xyzw =
1484                                                                     "XYWF,";
1485                                                                 break;
1486                                                         }
1487                                                         instr_out(ctx, i,
1488                                                                   "S4: point_width=%i, line_width=%.1f,"
1489                                                                   "%s%s%s%s%s cullmode=%s, vfmt=%s%s%s%s%s%s "
1490                                                                   "%s%s%s%s%s\n",
1491                                                                   (data[i] >>
1492                                                                    23) & 0x1ff,
1493                                                                   ((data[i] >>
1494                                                                     19) & 0xf) /
1495                                                                   2.0,
1496                                                                   data[i] & (0xf
1497                                                                              <<
1498                                                                              15)
1499                                                                   ?
1500                                                                   " flatshade="
1501                                                                   : "",
1502                                                                   data[i] & (1
1503                                                                              <<
1504                                                                              18)
1505                                                                   ? "Alpha," :
1506                                                                   "",
1507                                                                   data[i] & (1
1508                                                                              <<
1509                                                                              17)
1510                                                                   ? "Fog," : "",
1511                                                                   data[i] & (1
1512                                                                              <<
1513                                                                              16)
1514                                                                   ? "Specular,"
1515                                                                   : "",
1516                                                                   data[i] & (1
1517                                                                              <<
1518                                                                              15)
1519                                                                   ? "Color," :
1520                                                                   "", cullmode,
1521                                                                   data[i] & (1
1522                                                                              <<
1523                                                                              12)
1524                                                                   ?
1525                                                                   "PointWidth,"
1526                                                                   : "",
1527                                                                   data[i] & (1
1528                                                                              <<
1529                                                                              11)
1530                                                                   ? "SpecFog," :
1531                                                                   "",
1532                                                                   data[i] & (1
1533                                                                              <<
1534                                                                              10)
1535                                                                   ? "Color," :
1536                                                                   "",
1537                                                                   data[i] & (1
1538                                                                              <<
1539                                                                              9)
1540                                                                   ? "DepthOfs,"
1541                                                                   : "",
1542                                                                   vfmt_xyzw,
1543                                                                   data[i] & (1
1544                                                                              <<
1545                                                                              9)
1546                                                                   ? "FogParam,"
1547                                                                   : "",
1548                                                                   data[i] & (1
1549                                                                              <<
1550                                                                              5)
1551                                                                   ?
1552                                                                   "force default diffuse, "
1553                                                                   : "",
1554                                                                   data[i] & (1
1555                                                                              <<
1556                                                                              4)
1557                                                                   ?
1558                                                                   "force default specular, "
1559                                                                   : "",
1560                                                                   data[i] & (1
1561                                                                              <<
1562                                                                              3)
1563                                                                   ?
1564                                                                   "local depth ofs enable, "
1565                                                                   : "",
1566                                                                   data[i] & (1
1567                                                                              <<
1568                                                                              1)
1569                                                                   ?
1570                                                                   "point sprite enable, "
1571                                                                   : "",
1572                                                                   data[i] & (1
1573                                                                              <<
1574                                                                              0)
1575                                                                   ?
1576                                                                   "line AA enable, "
1577                                                                   : "");
1578                                                         break;
1579                                                 }
1580                                         case 5:
1581                                                 {
1582                                                         instr_out(ctx, i,
1583                                                                   "S5:%s%s%s%s%s"
1584                                                                   "%s%s%s%s stencil_ref=0x%x, stencil_test=%s, "
1585                                                                   "stencil_fail=%s, stencil_pass_z_fail=%s, "
1586                                                                   "stencil_pass_z_pass=%s, %s%s%s%s\n",
1587                                                                   data[i] & (0xf
1588                                                                              <<
1589                                                                              28)
1590                                                                   ?
1591                                                                   " write_disable="
1592                                                                   : "",
1593                                                                   data[i] & (1
1594                                                                              <<
1595                                                                              31)
1596                                                                   ? "Alpha," :
1597                                                                   "",
1598                                                                   data[i] & (1
1599                                                                              <<
1600                                                                              30)
1601                                                                   ? "Red," : "",
1602                                                                   data[i] & (1
1603                                                                              <<
1604                                                                              29)
1605                                                                   ? "Green," :
1606                                                                   "",
1607                                                                   data[i] & (1
1608                                                                              <<
1609                                                                              28)
1610                                                                   ? "Blue," :
1611                                                                   "",
1612                                                                   data[i] & (1
1613                                                                              <<
1614                                                                              27)
1615                                                                   ?
1616                                                                   " force default point size,"
1617                                                                   : "",
1618                                                                   data[i] & (1
1619                                                                              <<
1620                                                                              26)
1621                                                                   ?
1622                                                                   " last pixel enable,"
1623                                                                   : "",
1624                                                                   data[i] & (1
1625                                                                              <<
1626                                                                              25)
1627                                                                   ?
1628                                                                   " global depth ofs enable,"
1629                                                                   : "",
1630                                                                   data[i] & (1
1631                                                                              <<
1632                                                                              24)
1633                                                                   ?
1634                                                                   " fog enable,"
1635                                                                   : "",
1636                                                                   (data[i] >>
1637                                                                    16) & 0xff,
1638                                                                   decode_compare_func
1639                                                                   (data[i] >>
1640                                                                    13),
1641                                                                   decode_stencil_op
1642                                                                   (data[i] >>
1643                                                                    10),
1644                                                                   decode_stencil_op
1645                                                                   (data[i] >>
1646                                                                    7),
1647                                                                   decode_stencil_op
1648                                                                   (data[i] >>
1649                                                                    4),
1650                                                                   data[i] & (1
1651                                                                              <<
1652                                                                              3)
1653                                                                   ?
1654                                                                   "stencil write enable, "
1655                                                                   : "",
1656                                                                   data[i] & (1
1657                                                                              <<
1658                                                                              2)
1659                                                                   ?
1660                                                                   "stencil test enable, "
1661                                                                   : "",
1662                                                                   data[i] & (1
1663                                                                              <<
1664                                                                              1)
1665                                                                   ?
1666                                                                   "color dither enable, "
1667                                                                   : "",
1668                                                                   data[i] & (1
1669                                                                              <<
1670                                                                              0)
1671                                                                   ?
1672                                                                   "logicop enable, "
1673                                                                   : "");
1674                                                 }
1675                                                 break;
1676                                         case 6:
1677                                                 instr_out(ctx, i,
1678                                                           "S6: %salpha_test=%s, alpha_ref=0x%x, "
1679                                                           "depth_test=%s, %ssrc_blnd_fct=%s, dst_blnd_fct=%s, "
1680                                                           "%s%stristrip_provoking_vertex=%i\n",
1681                                                           data[i] & (1 << 31) ?
1682                                                           "alpha test enable, "
1683                                                           : "",
1684                                                           decode_compare_func
1685                                                           (data[i] >> 28),
1686                                                           data[i] & (0xff <<
1687                                                                      20),
1688                                                           decode_compare_func
1689                                                           (data[i] >> 16),
1690                                                           data[i] & (1 << 15) ?
1691                                                           "cbuf blend enable, "
1692                                                           : "",
1693                                                           decode_blend_fact(data
1694                                                                             [i]
1695                                                                             >>
1696                                                                             8),
1697                                                           decode_blend_fact(data
1698                                                                             [i]
1699                                                                             >>
1700                                                                             4),
1701                                                           data[i] & (1 << 3) ?
1702                                                           "depth write enable, "
1703                                                           : "",
1704                                                           data[i] & (1 << 2) ?
1705                                                           "cbuf write enable, "
1706                                                           : "",
1707                                                           data[i] & (0x3));
1708                                                 break;
1709                                         case 7:
1710                                                 instr_out(ctx, i,
1711                                                           "S7: depth offset constant: 0x%08x\n",
1712                                                           data[i]);
1713                                                 break;
1714                                         }
1715                                 } else {
1716                                         instr_out(ctx, i,
1717                                                   "S%d: 0x%08x\n", i, data[i]);
1718                                 }
1719                                 i++;
1720                         }
1721                 }
1722                 if (len != i) {
1723                         fprintf(out,
1724                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1725                 }
1726                 return len;
1727         case 0x03:
1728                 instr_out(ctx, 0,
1729                           "3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1730                 len = (data[0] & 0x0000000f) + 2;
1731                 i = 1;
1732                 for (word = 6; word <= 14; word++) {
1733                         if (data[0] & (1 << word)) {
1734                                 if (word == 6)
1735                                         instr_out(ctx, i++,
1736                                                   "TBCF\n");
1737                                 else if (word >= 7 && word <= 10) {
1738                                         instr_out(ctx, i++,
1739                                                   "TB%dC\n", word - 7);
1740                                         instr_out(ctx, i++,
1741                                                   "TB%dA\n", word - 7);
1742                                 } else if (word >= 11 && word <= 14) {
1743                                         instr_out(ctx, i,
1744                                                   "TM%dS0: offset=0x%08x, %s\n",
1745                                                   word - 11,
1746                                                   data[i] & 0xfffffffe,
1747                                                   data[i] & 1 ? "use fence" :
1748                                                   "");
1749                                         i++;
1750                                         instr_out(ctx, i,
1751                                                   "TM%dS1: height=%i, width=%i, %s\n",
1752                                                   word - 11, data[i] >> 21,
1753                                                   (data[i] >> 10) & 0x3ff,
1754                                                   data[i] & 2 ? (data[i] & 1 ?
1755                                                                  "y-tiled" :
1756                                                                  "x-tiled") :
1757                                                   "");
1758                                         i++;
1759                                         instr_out(ctx, i,
1760                                                   "TM%dS2: pitch=%i, \n",
1761                                                   word - 11,
1762                                                   ((data[i] >> 21) + 1) * 4);
1763                                         i++;
1764                                         instr_out(ctx, i++,
1765                                                   "TM%dS3\n", word - 11);
1766                                         instr_out(ctx, i++,
1767                                                   "TM%dS4: dflt color\n",
1768                                                   word - 11);
1769                                 }
1770                         }
1771                 }
1772                 if (len != i) {
1773                         fprintf(out,
1774                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1775                 }
1776                 return len;
1777         case 0x00:
1778                 instr_out(ctx, 0, "3DSTATE_MAP_STATE\n");
1779                 len = (data[0] & 0x0000003f) + 2;
1780                 instr_out(ctx, 1, "mask\n");
1782                 i = 2;
1783                 for (map = 0; map <= 15; map++) {
1784                         if (data[1] & (1 << map)) {
1785                                 int width, height, pitch, dword;
1786                                 const char *tiling;
1788                                 dword = data[i];
1789                                 instr_out(ctx, i++,
1790                                           "map %d MS2 %s%s%s\n", map,
1791                                           dword & (1 << 31) ?
1792                                           "untrusted surface, " : "",
1793                                           dword & (1 << 1) ?
1794                                           "vertical line stride enable, " : "",
1795                                           dword & (1 << 0) ?
1796                                           "vertical ofs enable, " : "");
1798                                 dword = data[i];
1799                                 width = ((dword >> 10) & ((1 << 11) - 1)) + 1;
1800                                 height = ((dword >> 21) & ((1 << 11) - 1)) + 1;
1802                                 tiling = "none";
1803                                 if (dword & (1 << 2))
1804                                         tiling = "fenced";
1805                                 else if (dword & (1 << 1))
1806                                         tiling = dword & (1 << 0) ? "Y" : "X";
1807                                 type = " BAD";
1808                                 format = "BAD";
1809                                 switch ((dword >> 7) & 0x7) {
1810                                 case 1:
1811                                         type = "8b";
1812                                         switch ((dword >> 3) & 0xf) {
1813                                         case 0:
1814                                                 format = "I";
1815                                                 break;
1816                                         case 1:
1817                                                 format = "L";
1818                                                 break;
1819                                         case 4:
1820                                                 format = "A";
1821                                                 break;
1822                                         case 5:
1823                                                 format = " mono";
1824                                                 break;
1825                                         }
1826                                         break;
1827                                 case 2:
1828                                         type = "16b";
1829                                         switch ((dword >> 3) & 0xf) {
1830                                         case 0:
1831                                                 format = " rgb565";
1832                                                 break;
1833                                         case 1:
1834                                                 format = " argb1555";
1835                                                 break;
1836                                         case 2:
1837                                                 format = " argb4444";
1838                                                 break;
1839                                         case 5:
1840                                                 format = " ay88";
1841                                                 break;
1842                                         case 6:
1843                                                 format = " bump655";
1844                                                 break;
1845                                         case 7:
1846                                                 format = "I";
1847                                                 break;
1848                                         case 8:
1849                                                 format = "L";
1850                                                 break;
1851                                         case 9:
1852                                                 format = "A";
1853                                                 break;
1854                                         }
1855                                         break;
1856                                 case 3:
1857                                         type = "32b";
1858                                         switch ((dword >> 3) & 0xf) {
1859                                         case 0:
1860                                                 format = " argb8888";
1861                                                 break;
1862                                         case 1:
1863                                                 format = " abgr8888";
1864                                                 break;
1865                                         case 2:
1866                                                 format = " xrgb8888";
1867                                                 break;
1868                                         case 3:
1869                                                 format = " xbgr8888";
1870                                                 break;
1871                                         case 4:
1872                                                 format = " qwvu8888";
1873                                                 break;
1874                                         case 5:
1875                                                 format = " axvu8888";
1876                                                 break;
1877                                         case 6:
1878                                                 format = " lxvu8888";
1879                                                 break;
1880                                         case 7:
1881                                                 format = " xlvu8888";
1882                                                 break;
1883                                         case 8:
1884                                                 format = " argb2101010";
1885                                                 break;
1886                                         case 9:
1887                                                 format = " abgr2101010";
1888                                                 break;
1889                                         case 10:
1890                                                 format = " awvu2101010";
1891                                                 break;
1892                                         case 11:
1893                                                 format = " gr1616";
1894                                                 break;
1895                                         case 12:
1896                                                 format = " vu1616";
1897                                                 break;
1898                                         case 13:
1899                                                 format = " xI824";
1900                                                 break;
1901                                         case 14:
1902                                                 format = " xA824";
1903                                                 break;
1904                                         case 15:
1905                                                 format = " xL824";
1906                                                 break;
1907                                         }
1908                                         break;
1909                                 case 5:
1910                                         type = "422";
1911                                         switch ((dword >> 3) & 0xf) {
1912                                         case 0:
1913                                                 format = " yuv_swapy";
1914                                                 break;
1915                                         case 1:
1916                                                 format = " yuv";
1917                                                 break;
1918                                         case 2:
1919                                                 format = " yuv_swapuv";
1920                                                 break;
1921                                         case 3:
1922                                                 format = " yuv_swapuvy";
1923                                                 break;
1924                                         }
1925                                         break;
1926                                 case 6:
1927                                         type = "compressed";
1928                                         switch ((dword >> 3) & 0x7) {
1929                                         case 0:
1930                                                 format = " dxt1";
1931                                                 break;
1932                                         case 1:
1933                                                 format = " dxt2_3";
1934                                                 break;
1935                                         case 2:
1936                                                 format = " dxt4_5";
1937                                                 break;
1938                                         case 3:
1939                                                 format = " fxt1";
1940                                                 break;
1941                                         case 4:
1942                                                 format = " dxt1_rb";
1943                                                 break;
1944                                         }
1945                                         break;
1946                                 case 7:
1947                                         type = "4b indexed";
1948                                         switch ((dword >> 3) & 0xf) {
1949                                         case 7:
1950                                                 format = " argb8888";
1951                                                 break;
1952                                         }
1953                                         break;
1954                                 }
1955                                 dword = data[i];
1956                                 instr_out(ctx, i++,
1957                                           "map %d MS3 [width=%d, height=%d, format=%s%s, tiling=%s%s]\n",
1958                                           map, width, height, type, format,
1959                                           tiling,
1960                                           dword & (1 << 9) ? " palette select" :
1961                                           "");
1963                                 dword = data[i];
1964                                 pitch =
1965                                     4 * (((dword >> 21) & ((1 << 11) - 1)) + 1);
1966                                 instr_out(ctx, i++,
1967                                           "map %d MS4 [pitch=%d, max_lod=%i, vol_depth=%i, cube_face_ena=%x, %s]\n",
1968                                           map, pitch, (dword >> 9) & 0x3f,
1969                                           dword & 0xff, (dword >> 15) & 0x3f,
1970                                           dword & (1 << 8) ? "miplayout legacy"
1971                                           : "miplayout right");
1972                         }
1973                 }
1974                 if (len != i) {
1975                         fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
1976                         return len;
1977                 }
1978                 return len;
1979         case 0x06:
1980                 instr_out(ctx, 0,
1981                           "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1982                 len = (data[0] & 0x000000ff) + 2;
1984                 i = 2;
1985                 for (c = 0; c <= 31; c++) {
1986                         if (data[1] & (1 << c)) {
1987                                 instr_out(ctx, i, "C%d.X = %f\n", c,
1988                                           int_as_float(data[i]));
1989                                 i++;
1990                                 instr_out(ctx, i, "C%d.Y = %f\n",
1991                                           c, int_as_float(data[i]));
1992                                 i++;
1993                                 instr_out(ctx, i, "C%d.Z = %f\n",
1994                                           c, int_as_float(data[i]));
1995                                 i++;
1996                                 instr_out(ctx, i, "C%d.W = %f\n",
1997                                           c, int_as_float(data[i]));
1998                                 i++;
1999                         }
2000                 }
2001                 if (len != i) {
2002                         fprintf(out,
2003                                 "Bad count in 3DSTATE_PIXEL_SHADER_CONSTANTS\n");
2004                 }
2005                 return len;
2006         case 0x05:
2007                 instr_out(ctx, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
2008                 len = (data[0] & 0x000000ff) + 2;
2009                 if ((len - 1) % 3 != 0 || len > 370) {
2010                         fprintf(out,
2011                                 "Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
2012                 }
2013                 i = 1;
2014                 for (instr = 0; instr < (len - 1) / 3; instr++) {
2015                         char instr_prefix[10];
2017                         sprintf(instr_prefix, "PS%03d", instr);
2018                         i915_decode_instruction(ctx, i,
2019                                                 instr_prefix);
2020                         i += 3;
2021                 }
2022                 return len;
2023         case 0x01:
2024                 if (IS_GEN2(devid))
2025                         break;
2026                 instr_out(ctx, 0, "3DSTATE_SAMPLER_STATE\n");
2027                 instr_out(ctx, 1, "mask\n");
2028                 len = (data[0] & 0x0000003f) + 2;
2029                 i = 2;
2030                 for (sampler = 0; sampler <= 15; sampler++) {
2031                         if (data[1] & (1 << sampler)) {
2032                                 uint32_t dword;
2033                                 const char *mip_filter = "";
2035                                 dword = data[i];
2036                                 switch ((dword >> 20) & 0x3) {
2037                                 case 0:
2038                                         mip_filter = "none";
2039                                         break;
2040                                 case 1:
2041                                         mip_filter = "nearest";
2042                                         break;
2043                                 case 3:
2044                                         mip_filter = "linear";
2045                                         break;
2046                                 }
2047                                 instr_out(ctx, i++,
2048                                           "sampler %d SS2:%s%s%s "
2049                                           "base_mip_level=%i, mip_filter=%s, mag_filter=%s, min_filter=%s "
2050                                           "lod_bias=%.2f,%s max_aniso=%i, shadow_func=%s\n",
2051                                           sampler,
2052                                           dword & (1 << 31) ? " reverse gamma,"
2053                                           : "",
2054                                           dword & (1 << 30) ? " packed2planar,"
2055                                           : "",
2056                                           dword & (1 << 29) ?
2057                                           " colorspace conversion," : "",
2058                                           (dword >> 22) & 0x1f, mip_filter,
2059                                           decode_sample_filter(dword >> 17),
2060                                           decode_sample_filter(dword >> 14),
2061                                           ((dword >> 5) & 0x1ff) / (0x10 * 1.0),
2062                                           dword & (1 << 4) ? " shadow," : "",
2063                                           dword & (1 << 3) ? 4 : 2,
2064                                           decode_compare_func(dword));
2065                                 dword = data[i];
2066                                 instr_out(ctx, i++,
2067                                           "sampler %d SS3: min_lod=%.2f,%s "
2068                                           "tcmode_x=%s, tcmode_y=%s, tcmode_z=%s,%s texmap_idx=%i,%s\n",
2069                                           sampler,
2070                                           ((dword >> 24) & 0xff) / (0x10 * 1.0),
2071                                           dword & (1 << 17) ?
2072                                           " kill pixel enable," : "",
2073                                           decode_tex_coord_mode(dword >> 12),
2074                                           decode_tex_coord_mode(dword >> 9),
2075                                           decode_tex_coord_mode(dword >> 6),
2076                                           dword & (1 << 5) ?
2077                                           " normalized coords," : "",
2078                                           (dword >> 1) & 0xf,
2079                                           dword & (1 << 0) ? " deinterlacer," :
2080                                           "");
2081                                 dword = data[i];
2082                                 instr_out(ctx, i++,
2083                                           "sampler %d SS4: border color\n",
2084                                           sampler);
2085                         }
2086                 }
2087                 if (len != i) {
2088                         fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
2089                 }
2090                 return len;
2091         case 0x85:
2092                 len = (data[0] & 0x0000000f) + 2;
2094                 if (len != 2)
2095                         fprintf(out,
2096                                 "Bad count in 3DSTATE_DEST_BUFFER_VARIABLES\n");
2098                 instr_out(ctx, 0,
2099                           "3DSTATE_DEST_BUFFER_VARIABLES\n");
2101                 switch ((data[1] >> 8) & 0xf) {
2102                 case 0x0:
2103                         format = "g8";
2104                         break;
2105                 case 0x1:
2106                         format = "x1r5g5b5";
2107                         break;
2108                 case 0x2:
2109                         format = "r5g6b5";
2110                         break;
2111                 case 0x3:
2112                         format = "a8r8g8b8";
2113                         break;
2114                 case 0x4:
2115                         format = "ycrcb_swapy";
2116                         break;
2117                 case 0x5:
2118                         format = "ycrcb_normal";
2119                         break;
2120                 case 0x6:
2121                         format = "ycrcb_swapuv";
2122                         break;
2123                 case 0x7:
2124                         format = "ycrcb_swapuvy";
2125                         break;
2126                 case 0x8:
2127                         format = "a4r4g4b4";
2128                         break;
2129                 case 0x9:
2130                         format = "a1r5g5b5";
2131                         break;
2132                 case 0xa:
2133                         format = "a2r10g10b10";
2134                         break;
2135                 default:
2136                         format = "BAD";
2137                         break;
2138                 }
2139                 switch ((data[1] >> 2) & 0x3) {
2140                 case 0x0:
2141                         zformat = "u16";
2142                         break;
2143                 case 0x1:
2144                         zformat = "f16";
2145                         break;
2146                 case 0x2:
2147                         zformat = "u24x8";
2148                         break;
2149                 default:
2150                         zformat = "BAD";
2151                         break;
2152                 }
2153                 instr_out(ctx, 1,
2154                           "%s format, %s depth format, early Z %sabled\n",
2155                           format, zformat,
2156                           (data[1] & (1 << 31)) ? "en" : "dis");
2157                 return len;
2159         case 0x8e:
2160                 {
2161                         const char *name, *tiling;
2163                         len = (data[0] & 0x0000000f) + 2;
2164                         if (len != 3)
2165                                 fprintf(out,
2166                                         "Bad count in 3DSTATE_BUFFER_INFO\n");
2168                         switch ((data[1] >> 24) & 0x7) {
2169                         case 0x3:
2170                                 name = "color";
2171                                 break;
2172                         case 0x7:
2173                                 name = "depth";
2174                                 break;
2175                         default:
2176                                 name = "unknown";
2177                                 break;
2178                         }
2180                         tiling = "none";
2181                         if (data[1] & (1 << 23))
2182                                 tiling = "fenced";
2183                         else if (data[1] & (1 << 22))
2184                                 tiling = data[1] & (1 << 21) ? "Y" : "X";
2186                         instr_out(ctx, 0, "3DSTATE_BUFFER_INFO\n");
2187                         instr_out(ctx, 1,
2188                                   "%s, tiling = %s, pitch=%d\n", name, tiling,
2189                                   data[1] & 0xffff);
2191                         instr_out(ctx, 2, "address\n");
2192                         return len;
2193                 }
2194         case 0x81:
2195                 len = (data[0] & 0x0000000f) + 2;
2197                 if (len != 3)
2198                         fprintf(out,
2199                                 "Bad count in 3DSTATE_SCISSOR_RECTANGLE\n");
2201                 instr_out(ctx, 0, "3DSTATE_SCISSOR_RECTANGLE\n");
2202                 instr_out(ctx, 1, "(%d,%d)\n",
2203                           data[1] & 0xffff, data[1] >> 16);
2204                 instr_out(ctx, 2, "(%d,%d)\n",
2205                           data[2] & 0xffff, data[2] >> 16);
2207                 return len;
2208         case 0x80:
2209                 len = (data[0] & 0x0000000f) + 2;
2211                 if (len != 5)
2212                         fprintf(out,
2213                                 "Bad count in 3DSTATE_DRAWING_RECTANGLE\n");
2215                 instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
2216                 instr_out(ctx, 1, "%s\n",
2217                           data[1] & (1 << 30) ? "depth ofs disabled " : "");
2218                 instr_out(ctx, 2, "(%d,%d)\n",
2219                           data[2] & 0xffff, data[2] >> 16);
2220                 instr_out(ctx, 3, "(%d,%d)\n",
2221                           data[3] & 0xffff, data[3] >> 16);
2222                 instr_out(ctx, 4, "(%d,%d)\n",
2223                           data[4] & 0xffff, data[4] >> 16);
2225                 return len;
2226         case 0x9c:
2227                 len = (data[0] & 0x0000000f) + 2;
2229                 if (len != 7)
2230                         fprintf(out, "Bad count in 3DSTATE_CLEAR_PARAMETERS\n");
2232                 instr_out(ctx, 0, "3DSTATE_CLEAR_PARAMETERS\n");
2233                 instr_out(ctx, 1, "prim_type=%s, clear=%s%s%s\n",
2234                           data[1] & (1 << 16) ? "CLEAR_RECT" : "ZONE_INIT",
2235                           data[1] & (1 << 2) ? "color," : "",
2236                           data[1] & (1 << 1) ? "depth," : "",
2237                           data[1] & (1 << 0) ? "stencil," : "");
2238                 instr_out(ctx, 2, "clear color\n");
2239                 instr_out(ctx, 3, "clear depth/stencil\n");
2240                 instr_out(ctx, 4, "color value (rgba8888)\n");
2241                 instr_out(ctx, 5, "depth value %f\n",
2242                           int_as_float(data[5]));
2243                 instr_out(ctx, 6, "clear stencil\n");
2244                 return len;
2245         }
2247         for (idx = 0; idx < ARRAY_SIZE(opcodes_3d_1d); idx++) {
2248                 opcode_3d_1d = &opcodes_3d_1d[idx];
2249                 if (opcode_3d_1d->i830_only && !IS_GEN2(devid))
2250                         continue;
2252                 if (((data[0] & 0x00ff0000) >> 16) == opcode_3d_1d->opcode) {
2253                         len = 1;
2255                         instr_out(ctx, 0, "%s\n",
2256                                   opcode_3d_1d->name);
2257                         if (opcode_3d_1d->max_len > 1) {
2258                                 len = (data[0] & 0x0000ffff) + 2;
2259                                 if (len < opcode_3d_1d->min_len ||
2260                                     len > opcode_3d_1d->max_len) {
2261                                         fprintf(out, "Bad count in %s\n",
2262                                                 opcode_3d_1d->name);
2263                                 }
2264                         }
2266                         for (i = 1; i < len; i++) {
2267                                 instr_out(ctx, i, "dword %d\n", i);
2268                         }
2270                         return len;
2271                 }
2272         }
2274         instr_out(ctx, 0, "3D UNKNOWN: 3d_1d opcode = 0x%x\n",
2275                   opcode);
2276         return 1;
2279 static int
2280 decode_3d_primitive(struct drm_intel_decode *ctx)
2282         uint32_t *data = ctx->data;
2283         uint32_t count = ctx->count;
2284         char immediate = (data[0] & (1 << 23)) == 0;
2285         unsigned int len, i, j, ret;
2286         const char *primtype;
2287         int original_s2 = saved_s2;
2288         int original_s4 = saved_s4;
2290         switch ((data[0] >> 18) & 0xf) {
2291         case 0x0:
2292                 primtype = "TRILIST";
2293                 break;
2294         case 0x1:
2295                 primtype = "TRISTRIP";
2296                 break;
2297         case 0x2:
2298                 primtype = "TRISTRIP_REVERSE";
2299                 break;
2300         case 0x3:
2301                 primtype = "TRIFAN";
2302                 break;
2303         case 0x4:
2304                 primtype = "POLYGON";
2305                 break;
2306         case 0x5:
2307                 primtype = "LINELIST";
2308                 break;
2309         case 0x6:
2310                 primtype = "LINESTRIP";
2311                 break;
2312         case 0x7:
2313                 primtype = "RECTLIST";
2314                 break;
2315         case 0x8:
2316                 primtype = "POINTLIST";
2317                 break;
2318         case 0x9:
2319                 primtype = "DIB";
2320                 break;
2321         case 0xa:
2322                 primtype = "CLEAR_RECT";
2323                 saved_s4 = 3 << 6;
2324                 saved_s2 = ~0;
2325                 break;
2326         default:
2327                 primtype = "unknown";
2328                 break;
2329         }
2331         /* XXX: 3DPRIM_DIB not supported */
2332         if (immediate) {
2333                 len = (data[0] & 0x0003ffff) + 2;
2334                 instr_out(ctx, 0, "3DPRIMITIVE inline %s\n",
2335                           primtype);
2336                 if (count < len)
2337                         BUFFER_FAIL(count, len, "3DPRIMITIVE inline");
2338                 if (!saved_s2_set || !saved_s4_set) {
2339                         fprintf(out, "unknown vertex format\n");
2340                         for (i = 1; i < len; i++) {
2341                                 instr_out(ctx, i,
2342                                           "           vertex data (%f float)\n",
2343                                           int_as_float(data[i]));
2344                         }
2345                 } else {
2346                         unsigned int vertex = 0;
2347                         for (i = 1; i < len;) {
2348                                 unsigned int tc;
2350 #define VERTEX_OUT(fmt, ...) do {                                       \
2351     if (i < len)                                                        \
2352         instr_out(ctx, i, " V%d."fmt"\n", vertex, __VA_ARGS__); \
2353     else                                                                \
2354         fprintf(out, " missing data in V%d\n", vertex);                 \
2355     i++;                                                                \
2356 } while (0)
2358                                 VERTEX_OUT("X = %f", int_as_float(data[i]));
2359                                 VERTEX_OUT("Y = %f", int_as_float(data[i]));
2360                                 switch (saved_s4 >> 6 & 0x7) {
2361                                 case 0x1:
2362                                         VERTEX_OUT("Z = %f",
2363                                                    int_as_float(data[i]));
2364                                         break;
2365                                 case 0x2:
2366                                         VERTEX_OUT("Z = %f",
2367                                                    int_as_float(data[i]));
2368                                         VERTEX_OUT("W = %f",
2369                                                    int_as_float(data[i]));
2370                                         break;
2371                                 case 0x3:
2372                                         break;
2373                                 case 0x4:
2374                                         VERTEX_OUT("W = %f",
2375                                                    int_as_float(data[i]));
2376                                         break;
2377                                 default:
2378                                         fprintf(out, "bad S4 position mask\n");
2379                                 }
2381                                 if (saved_s4 & (1 << 10)) {
2382                                         VERTEX_OUT
2383                                             ("color = (A=0x%02x, R=0x%02x, G=0x%02x, "
2384                                              "B=0x%02x)", data[i] >> 24,
2385                                              (data[i] >> 16) & 0xff,
2386                                              (data[i] >> 8) & 0xff,
2387                                              data[i] & 0xff);
2388                                 }
2389                                 if (saved_s4 & (1 << 11)) {
2390                                         VERTEX_OUT
2391                                             ("spec = (A=0x%02x, R=0x%02x, G=0x%02x, "
2392                                              "B=0x%02x)", data[i] >> 24,
2393                                              (data[i] >> 16) & 0xff,
2394                                              (data[i] >> 8) & 0xff,
2395                                              data[i] & 0xff);
2396                                 }
2397                                 if (saved_s4 & (1 << 12))
2398                                         VERTEX_OUT("width = 0x%08x)", data[i]);
2400                                 for (tc = 0; tc <= 7; tc++) {
2401                                         switch ((saved_s2 >> (tc * 4)) & 0xf) {
2402                                         case 0x0:
2403                                                 VERTEX_OUT("T%d.X = %f", tc,
2404                                                            int_as_float(data
2405                                                                         [i]));
2406                                                 VERTEX_OUT("T%d.Y = %f", tc,
2407                                                            int_as_float(data
2408                                                                         [i]));
2409                                                 break;
2410                                         case 0x1:
2411                                                 VERTEX_OUT("T%d.X = %f", tc,
2412                                                            int_as_float(data
2413                                                                         [i]));
2414                                                 VERTEX_OUT("T%d.Y = %f", tc,
2415                                                            int_as_float(data
2416                                                                         [i]));
2417                                                 VERTEX_OUT("T%d.Z = %f", tc,
2418                                                            int_as_float(data
2419                                                                         [i]));
2420                                                 break;
2421                                         case 0x2:
2422                                                 VERTEX_OUT("T%d.X = %f", tc,
2423                                                            int_as_float(data
2424                                                                         [i]));
2425                                                 VERTEX_OUT("T%d.Y = %f", tc,
2426                                                            int_as_float(data
2427                                                                         [i]));
2428                                                 VERTEX_OUT("T%d.Z = %f", tc,
2429                                                            int_as_float(data
2430                                                                         [i]));
2431                                                 VERTEX_OUT("T%d.W = %f", tc,
2432                                                            int_as_float(data
2433                                                                         [i]));
2434                                                 break;
2435                                         case 0x3:
2436                                                 VERTEX_OUT("T%d.X = %f", tc,
2437                                                            int_as_float(data
2438                                                                         [i]));
2439                                                 break;
2440                                         case 0x4:
2441                                                 VERTEX_OUT
2442                                                     ("T%d.XY = 0x%08x half-float",
2443                                                      tc, data[i]);
2444                                                 break;
2445                                         case 0x5:
2446                                                 VERTEX_OUT
2447                                                     ("T%d.XY = 0x%08x half-float",
2448                                                      tc, data[i]);
2449                                                 VERTEX_OUT
2450                                                     ("T%d.ZW = 0x%08x half-float",
2451                                                      tc, data[i]);
2452                                                 break;
2453                                         case 0xf:
2454                                                 break;
2455                                         default:
2456                                                 fprintf(out,
2457                                                         "bad S2.T%d format\n",
2458                                                         tc);
2459                                         }
2460                                 }
2461                                 vertex++;
2462                         }
2463                 }
2465                 ret = len;
2466         } else {
2467                 /* indirect vertices */
2468                 len = data[0] & 0x0000ffff;     /* index count */
2469                 if (data[0] & (1 << 17)) {
2470                         /* random vertex access */
2471                         if (count < (len + 1) / 2 + 1) {
2472                                 BUFFER_FAIL(count, (len + 1) / 2 + 1,
2473                                             "3DPRIMITIVE random indirect");
2474                         }
2475                         instr_out(ctx, 0,
2476                                   "3DPRIMITIVE random indirect %s (%d)\n",
2477                                   primtype, len);
2478                         if (len == 0) {
2479                                 /* vertex indices continue until 0xffff is
2480                                  * found
2481                                  */
2482                                 for (i = 1; i < count; i++) {
2483                                         if ((data[i] & 0xffff) == 0xffff) {
2484                                                 instr_out(ctx, i,
2485                                                           "    indices: (terminator)\n");
2486                                                 ret = i;
2487                                                 goto out;
2488                                         } else if ((data[i] >> 16) == 0xffff) {
2489                                                 instr_out(ctx, i,
2490                                                           "    indices: 0x%04x, (terminator)\n",
2491                                                           data[i] & 0xffff);
2492                                                 ret = i;
2493                                                 goto out;
2494                                         } else {
2495                                                 instr_out(ctx, i,
2496                                                           "    indices: 0x%04x, 0x%04x\n",
2497                                                           data[i] & 0xffff,
2498                                                           data[i] >> 16);
2499                                         }
2500                                 }
2501                                 fprintf(out,
2502                                         "3DPRIMITIVE: no terminator found in index buffer\n");
2503                                 ret = count;
2504                                 goto out;
2505                         } else {
2506                                 /* fixed size vertex index buffer */
2507                                 for (j = 1, i = 0; i < len; i += 2, j++) {
2508                                         if (i * 2 == len - 1) {
2509                                                 instr_out(ctx, j,
2510                                                           "    indices: 0x%04x\n",
2511                                                           data[j] & 0xffff);
2512                                         } else {
2513                                                 instr_out(ctx, j,
2514                                                           "    indices: 0x%04x, 0x%04x\n",
2515                                                           data[j] & 0xffff,
2516                                                           data[j] >> 16);
2517                                         }
2518                                 }
2519                         }
2520                         ret = (len + 1) / 2 + 1;
2521                         goto out;
2522                 } else {
2523                         /* sequential vertex access */
2524                         instr_out(ctx, 0,
2525                                   "3DPRIMITIVE sequential indirect %s, %d starting from "
2526                                   "%d\n", primtype, len, data[1] & 0xffff);
2527                         instr_out(ctx, 1, "           start\n");
2528                         ret = 2;
2529                         goto out;
2530                 }
2531         }
2533 out:
2534         saved_s2 = original_s2;
2535         saved_s4 = original_s4;
2536         return ret;
2539 static int
2540 decode_3d(struct drm_intel_decode *ctx)
2542         uint32_t opcode;
2543         unsigned int idx;
2544         uint32_t *data = ctx->data;
2546         struct {
2547                 uint32_t opcode;
2548                 unsigned int min_len;
2549                 unsigned int max_len;
2550                 const char *name;
2551         } opcodes_3d[] = {
2552                 { 0x06, 1, 1, "3DSTATE_ANTI_ALIASING" },
2553                 { 0x08, 1, 1, "3DSTATE_BACKFACE_STENCIL_OPS" },
2554                 { 0x09, 1, 1, "3DSTATE_BACKFACE_STENCIL_MASKS" },
2555                 { 0x16, 1, 1, "3DSTATE_COORD_SET_BINDINGS" },
2556                 { 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
2557                 { 0x0b, 1, 1, "3DSTATE_INDEPENDENT_ALPHA_BLEND" },
2558                 { 0x0d, 1, 1, "3DSTATE_MODES_4" },
2559                 { 0x0c, 1, 1, "3DSTATE_MODES_5" },
2560                 { 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES"},
2561         }, *opcode_3d;
2563         opcode = (data[0] & 0x1f000000) >> 24;
2565         switch (opcode) {
2566         case 0x1f:
2567                 return decode_3d_primitive(ctx);
2568         case 0x1d:
2569                 return decode_3d_1d(ctx);
2570         case 0x1c:
2571                 return decode_3d_1c(ctx);
2572         }
2574         for (idx = 0; idx < ARRAY_SIZE(opcodes_3d); idx++) {
2575                 opcode_3d = &opcodes_3d[idx];
2576                 if (opcode == opcode_3d->opcode) {
2577                         unsigned int len = 1, i;
2579                         instr_out(ctx, 0, "%s\n", opcode_3d->name);
2580                         if (opcode_3d->max_len > 1) {
2581                                 len = (data[0] & 0xff) + 2;
2582                                 if (len < opcode_3d->min_len ||
2583                                     len > opcode_3d->max_len) {
2584                                         fprintf(out, "Bad count in %s\n",
2585                                                 opcode_3d->name);
2586                                 }
2587                         }
2589                         for (i = 1; i < len; i++) {
2590                                 instr_out(ctx, i, "dword %d\n", i);
2591                         }
2592                         return len;
2593                 }
2594         }
2596         instr_out(ctx, 0, "3D UNKNOWN: 3d opcode = 0x%x\n", opcode);
2597         return 1;
2600 static const char *get_965_surfacetype(unsigned int surfacetype)
2602         switch (surfacetype) {
2603         case 0:
2604                 return "1D";
2605         case 1:
2606                 return "2D";
2607         case 2:
2608                 return "3D";
2609         case 3:
2610                 return "CUBE";
2611         case 4:
2612                 return "BUFFER";
2613         case 7:
2614                 return "NULL";
2615         default:
2616                 return "unknown";
2617         }
2620 static const char *get_965_depthformat(unsigned int depthformat)
2622         switch (depthformat) {
2623         case 0:
2624                 return "s8_z24float";
2625         case 1:
2626                 return "z32float";
2627         case 2:
2628                 return "z24s8";
2629         case 5:
2630                 return "z16";
2631         default:
2632                 return "unknown";
2633         }
2636 static const char *get_965_element_component(uint32_t data, int component)
2638         uint32_t component_control = (data >> (16 + (3 - component) * 4)) & 0x7;
2640         switch (component_control) {
2641         case 0:
2642                 return "nostore";
2643         case 1:
2644                 switch (component) {
2645                 case 0:
2646                         return "X";
2647                 case 1:
2648                         return "Y";
2649                 case 2:
2650                         return "Z";
2651                 case 3:
2652                         return "W";
2653                 default:
2654                         return "fail";
2655                 }
2656         case 2:
2657                 return "0.0";
2658         case 3:
2659                 return "1.0";
2660         case 4:
2661                 return "0x1";
2662         case 5:
2663                 return "VID";
2664         default:
2665                 return "fail";
2666         }
2669 static const char *get_965_prim_type(uint32_t primtype)
2671         switch (primtype) {
2672         case 0x01:
2673                 return "point list";
2674         case 0x02:
2675                 return "line list";
2676         case 0x03:
2677                 return "line strip";
2678         case 0x04:
2679                 return "tri list";
2680         case 0x05:
2681                 return "tri strip";
2682         case 0x06:
2683                 return "tri fan";
2684         case 0x07:
2685                 return "quad list";
2686         case 0x08:
2687                 return "quad strip";
2688         case 0x09:
2689                 return "line list adj";
2690         case 0x0a:
2691                 return "line strip adj";
2692         case 0x0b:
2693                 return "tri list adj";
2694         case 0x0c:
2695                 return "tri strip adj";
2696         case 0x0d:
2697                 return "tri strip reverse";
2698         case 0x0e:
2699                 return "polygon";
2700         case 0x0f:
2701                 return "rect list";
2702         case 0x10:
2703                 return "line loop";
2704         case 0x11:
2705                 return "point list bf";
2706         case 0x12:
2707                 return "line strip cont";
2708         case 0x13:
2709                 return "line strip bf";
2710         case 0x14:
2711                 return "line strip cont bf";
2712         case 0x15:
2713                 return "tri fan no stipple";
2714         default:
2715                 return "fail";
2716         }
2719 static int
2720 i965_decode_urb_fence(struct drm_intel_decode *ctx, int len)
2722         uint32_t vs_fence, clip_fence, gs_fence, sf_fence, vfe_fence, cs_fence;
2723         uint32_t *data = ctx->data;
2725         if (len != 3)
2726                 fprintf(out, "Bad count in URB_FENCE\n");
2728         vs_fence = data[1] & 0x3ff;
2729         gs_fence = (data[1] >> 10) & 0x3ff;
2730         clip_fence = (data[1] >> 20) & 0x3ff;
2731         sf_fence = data[2] & 0x3ff;
2732         vfe_fence = (data[2] >> 10) & 0x3ff;
2733         cs_fence = (data[2] >> 20) & 0x7ff;
2735         instr_out(ctx, 0, "URB_FENCE: %s%s%s%s%s%s\n",
2736                   (data[0] >> 13) & 1 ? "cs " : "",
2737                   (data[0] >> 12) & 1 ? "vfe " : "",
2738                   (data[0] >> 11) & 1 ? "sf " : "",
2739                   (data[0] >> 10) & 1 ? "clip " : "",
2740                   (data[0] >> 9) & 1 ? "gs " : "",
2741                   (data[0] >> 8) & 1 ? "vs " : "");
2742         instr_out(ctx, 1,
2743                   "vs fence: %d, clip_fence: %d, gs_fence: %d\n",
2744                   vs_fence, clip_fence, gs_fence);
2745         instr_out(ctx, 2,
2746                   "sf fence: %d, vfe_fence: %d, cs_fence: %d\n",
2747                   sf_fence, vfe_fence, cs_fence);
2748         if (gs_fence < vs_fence)
2749                 fprintf(out, "gs fence < vs fence!\n");
2750         if (clip_fence < gs_fence)
2751                 fprintf(out, "clip fence < gs fence!\n");
2752         if (sf_fence < clip_fence)
2753                 fprintf(out, "sf fence < clip fence!\n");
2754         if (cs_fence < sf_fence)
2755                 fprintf(out, "cs fence < sf fence!\n");
2757         return len;
2760 static void
2761 state_base_out(struct drm_intel_decode *ctx, unsigned int index,
2762                const char *name)
2764         if (ctx->data[index] & 1) {
2765                 instr_out(ctx, index,
2766                           "%s state base address 0x%08x\n", name,
2767                           ctx->data[index] & ~1);
2768         } else {
2769                 instr_out(ctx, index, "%s state base not updated\n",
2770                           name);
2771         }
2774 static void
2775 state_max_out(struct drm_intel_decode *ctx, unsigned int index,
2776               const char *name)
2778         if (ctx->data[index] & 1) {
2779                 if (ctx->data[index] == 1) {
2780                         instr_out(ctx, index,
2781                                   "%s state upper bound disabled\n", name);
2782                 } else {
2783                         instr_out(ctx, index,
2784                                   "%s state upper bound 0x%08x\n", name,
2785                                   ctx->data[index] & ~1);
2786                 }
2787         } else {
2788                 instr_out(ctx, index,
2789                           "%s state upper bound not updated\n", name);
2790         }
2793 static int
2794 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(struct drm_intel_decode *ctx)
2796         instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_CC\n");
2797         instr_out(ctx, 1, "pointer to CC viewport\n");
2799         return 2;
2802 static int
2803 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(struct drm_intel_decode *ctx)
2805         instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP\n");
2806         instr_out(ctx, 1, "pointer to SF_CLIP viewport\n");
2808         return 2;
2811 static int
2812 gen7_3DSTATE_BLEND_STATE_POINTERS(struct drm_intel_decode *ctx)
2814         instr_out(ctx, 0, "3DSTATE_BLEND_STATE_POINTERS\n");
2815         instr_out(ctx, 1, "pointer to BLEND_STATE at 0x%08x (%s)\n",
2816                   ctx->data[1] & ~1,
2817                   (ctx->data[1] & 1) ? "changed" : "unchanged");
2819         return 2;
2822 static int
2823 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(struct drm_intel_decode *ctx)
2825         instr_out(ctx, 0, "3DSTATE_DEPTH_STENCIL_STATE_POINTERS\n");
2826         instr_out(ctx, 1,
2827                   "pointer to DEPTH_STENCIL_STATE at 0x%08x (%s)\n",
2828                   ctx->data[1] & ~1,
2829                   (ctx->data[1] & 1) ? "changed" : "unchanged");
2831         return 2;
2834 static int
2835 gen7_3DSTATE_HIER_DEPTH_BUFFER(struct drm_intel_decode *ctx)
2837         instr_out(ctx, 0, "3DSTATE_HIER_DEPTH_BUFFER\n");
2838         instr_out(ctx, 1, "pitch %db\n",
2839                   (ctx->data[1] & 0x1ffff) + 1);
2840         instr_out(ctx, 2, "pointer to HiZ buffer\n");
2842         return 3;
2845 static int
2846 gen6_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
2848         instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
2849         instr_out(ctx, 1, "blend change %d\n", ctx->data[1] & 1);
2850         instr_out(ctx, 2, "depth stencil change %d\n",
2851                   ctx->data[2] & 1);
2852         instr_out(ctx, 3, "cc change %d\n", ctx->data[3] & 1);
2854         return 4;
2857 static int
2858 gen7_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
2860         instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
2861         instr_out(ctx, 1, "pointer to COLOR_CALC_STATE at 0x%08x "
2862                   "(%s)\n",
2863                   ctx->data[1] & ~1,
2864                   (ctx->data[1] & 1) ? "changed" : "unchanged");
2866         return 2;
2869 static int
2870 gen7_3DSTATE_URB_unit(struct drm_intel_decode *ctx, const char *unit)
2872     int start_kb = ((ctx->data[1] >> 25) & 0x3f) * 8;
2873     /* the field is # of 512-bit rows - 1, we print bytes */
2874     int entry_size = (((ctx->data[1] >> 16) & 0x1ff) + 1);
2875     int nr_entries = ctx->data[1] & 0xffff;
2877     instr_out(ctx, 0, "3DSTATE_URB_%s\n", unit);
2878     instr_out(ctx, 1,
2879               "%dKB start, size=%d 64B rows, nr_entries=%d, total size %dB\n",
2880               start_kb, entry_size, nr_entries, nr_entries * 64 * entry_size);
2882     return 2;
2885 static int
2886 gen7_3DSTATE_URB_VS(struct drm_intel_decode *ctx)
2888         return gen7_3DSTATE_URB_unit(ctx, "VS");
2891 static int
2892 gen7_3DSTATE_URB_HS(struct drm_intel_decode *ctx)
2894         return gen7_3DSTATE_URB_unit(ctx, "HS");
2897 static int
2898 gen7_3DSTATE_URB_DS(struct drm_intel_decode *ctx)
2900         return gen7_3DSTATE_URB_unit(ctx, "DS");
2903 static int
2904 gen7_3DSTATE_URB_GS(struct drm_intel_decode *ctx)
2906         return gen7_3DSTATE_URB_unit(ctx, "GS");
2909 static int
2910 gen7_3DSTATE_CONSTANT(struct drm_intel_decode *ctx, const char *unit)
2912         int rlen[4];
2914         rlen[0] = (ctx->data[1] >> 0) & 0xffff;
2915         rlen[1] = (ctx->data[1] >> 16) & 0xffff;
2916         rlen[2] = (ctx->data[2] >> 0) & 0xffff;
2917         rlen[3] = (ctx->data[2] >> 16) & 0xffff;
2919         instr_out(ctx, 0, "3DSTATE_CONSTANT_%s\n", unit);
2920         instr_out(ctx, 1, "len 0 = %d, len 1 = %d\n", rlen[0], rlen[1]);
2921         instr_out(ctx, 2, "len 2 = %d, len 3 = %d\n", rlen[2], rlen[3]);
2922         instr_out(ctx, 3, "pointer to constbuf 0\n");
2923         instr_out(ctx, 4, "pointer to constbuf 1\n");
2924         instr_out(ctx, 5, "pointer to constbuf 2\n");
2925         instr_out(ctx, 6, "pointer to constbuf 3\n");
2927         return 7;
2930 static int
2931 gen7_3DSTATE_CONSTANT_VS(struct drm_intel_decode *ctx)
2933         return gen7_3DSTATE_CONSTANT(ctx, "VS");
2936 static int
2937 gen7_3DSTATE_CONSTANT_GS(struct drm_intel_decode *ctx)
2939         return gen7_3DSTATE_CONSTANT(ctx, "GS");
2942 static int
2943 gen7_3DSTATE_CONSTANT_PS(struct drm_intel_decode *ctx)
2945         return gen7_3DSTATE_CONSTANT(ctx, "PS");
2948 static int
2949 gen7_3DSTATE_CONSTANT_DS(struct drm_intel_decode *ctx)
2951         return gen7_3DSTATE_CONSTANT(ctx, "DS");
2954 static int
2955 gen7_3DSTATE_CONSTANT_HS(struct drm_intel_decode *ctx)
2957         return gen7_3DSTATE_CONSTANT(ctx, "HS");
2961 static int
2962 gen6_3DSTATE_WM(struct drm_intel_decode *ctx)
2964         instr_out(ctx, 0, "3DSTATE_WM\n");
2965         instr_out(ctx, 1, "kernel start pointer 0\n");
2966         instr_out(ctx, 2,
2967                   "SPF=%d, VME=%d, Sampler Count %d, "
2968                   "Binding table count %d\n",
2969                   (ctx->data[2] >> 31) & 1,
2970                   (ctx->data[2] >> 30) & 1,
2971                   (ctx->data[2] >> 27) & 7,
2972                   (ctx->data[2] >> 18) & 0xff);
2973         instr_out(ctx, 3, "scratch offset\n");
2974         instr_out(ctx, 4,
2975                   "Depth Clear %d, Depth Resolve %d, HiZ Resolve %d, "
2976                   "Dispatch GRF start[0] %d, start[1] %d, start[2] %d\n",
2977                   (ctx->data[4] & (1 << 30)) != 0,
2978                   (ctx->data[4] & (1 << 28)) != 0,
2979                   (ctx->data[4] & (1 << 27)) != 0,
2980                   (ctx->data[4] >> 16) & 0x7f,
2981                   (ctx->data[4] >> 8) & 0x7f,
2982                   (ctx->data[4] & 0x7f));
2983         instr_out(ctx, 5,
2984                   "MaxThreads %d, PS KillPixel %d, PS computed Z %d, "
2985                   "PS use sourceZ %d, Thread Dispatch %d, PS use sourceW %d, "
2986                   "Dispatch32 %d, Dispatch16 %d, Dispatch8 %d\n",
2987