intel/decode: VERTEX_ELEMENT_STATE, 1 means valid
[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_WAIT_FOR_EVENT(struct drm_intel_decode *ctx)
144         const char *cc_wait;
145         int cc_shift = 0;
146         uint32_t data = ctx->data[0];
148         if (ctx->gen <= 5)
149                 cc_shift = 9;
150         else
151                 cc_shift = 16;
153         switch ((data >> cc_shift) & 0x1f) {
154         case 1:
155                 cc_wait = ", cc wait 1";
156                 break;
157         case 2:
158                 cc_wait = ", cc wait 2";
159                 break;
160         case 3:
161                 cc_wait = ", cc wait 3";
162                 break;
163         case 4:
164                 cc_wait = ", cc wait 4";
165                 break;
166         case 5:
167                 cc_wait = ", cc wait 4";
168                 break;
169         default:
170                 cc_wait = "";
171                 break;
172         }
174         if (ctx->gen <= 5) {
175                 instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
176                           data & (1<<18)? ", pipe B start vblank wait": "",
177                           data & (1<<17)? ", pipe A start vblank wait": "",
178                           data & (1<<16)? ", overlay flip pending wait": "",
179                           data & (1<<14)? ", pipe B hblank wait": "",
180                           data & (1<<13)? ", pipe A hblank wait": "",
181                           cc_wait,
182                           data & (1<<8)? ", plane C pending flip wait": "",
183                           data & (1<<7)? ", pipe B vblank wait": "",
184                           data & (1<<6)? ", plane B pending flip wait": "",
185                           data & (1<<5)? ", pipe B scan line wait": "",
186                           data & (1<<4)? ", fbc idle wait": "",
187                           data & (1<<3)? ", pipe A vblank wait": "",
188                           data & (1<<2)? ", plane A pending flip wait": "",
189                           data & (1<<1)? ", plane A scan line wait": "");
190         } else {
191                 instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s\n",
192                           data & (1<<20)? ", sprite C pending flip wait": "", /* ivb */
193                           cc_wait,
194                           data & (1<<13)? ", pipe B hblank wait": "",
195                           data & (1<<11)? ", pipe B vblank wait": "",
196                           data & (1<<10)? ", sprite B pending flip wait": "",
197                           data & (1<<9)? ", plane B pending flip wait": "",
198                           data & (1<<8)? ", plane B scan line wait": "",
199                           data & (1<<5)? ", pipe A hblank wait": "",
200                           data & (1<<3)? ", pipe A vblank wait": "",
201                           data & (1<<2)? ", sprite A pending flip wait": "",
202                           data & (1<<1)? ", plane A pending flip wait": "",
203                           data & (1<<0)? ", plane A scan line wait": "");
204         }
206         return 1;
209 static int
210 decode_mi(struct drm_intel_decode *ctx)
212         unsigned int opcode, len = -1;
213         const char *post_sync_op = "";
214         uint32_t *data = ctx->data;
216         struct {
217                 uint32_t opcode;
218                 int len_mask;
219                 unsigned int min_len;
220                 unsigned int max_len;
221                 const char *name;
222                 int (*func)(struct drm_intel_decode *ctx);
223         } opcodes_mi[] = {
224                 { 0x08, 0, 1, 1, "MI_ARB_ON_OFF" },
225                 { 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
226                 { 0x30, 0x3f, 3, 3, "MI_BATCH_BUFFER" },
227                 { 0x31, 0x3f, 2, 2, "MI_BATCH_BUFFER_START" },
228                 { 0x14, 0x3f, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
229                 { 0x04, 0, 1, 1, "MI_FLUSH" },
230                 { 0x22, 0x1f, 3, 3, "MI_LOAD_REGISTER_IMM" },
231                 { 0x13, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
232                 { 0x12, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
233                 { 0x00, 0, 1, 1, "MI_NOOP" },
234                 { 0x11, 0x3f, 2, 2, "MI_OVERLAY_FLIP" },
235                 { 0x07, 0, 1, 1, "MI_REPORT_HEAD" },
236                 { 0x18, 0x3f, 2, 2, "MI_SET_CONTEXT" },
237                 { 0x20, 0x3f, 3, 4, "MI_STORE_DATA_IMM" },
238                 { 0x21, 0x3f, 3, 4, "MI_STORE_DATA_INDEX" },
239                 { 0x24, 0x3f, 3, 3, "MI_STORE_REGISTER_MEM" },
240                 { 0x02, 0, 1, 1, "MI_USER_INTERRUPT" },
241                 { 0x03, 0, 1, 1, "MI_WAIT_FOR_EVENT", decode_MI_WAIT_FOR_EVENT },
242                 { 0x16, 0x7f, 3, 3, "MI_SEMAPHORE_MBOX" },
243                 { 0x26, 0x1f, 3, 4, "MI_FLUSH_DW" },
244                 { 0x0b, 0, 1, 1, "MI_SUSPEND_FLUSH"},
245         }, *opcode_mi = NULL;
247         /* check instruction length */
248         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
249              opcode++) {
250                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
251                         len = 1;
252                         if (opcodes_mi[opcode].max_len > 1) {
253                                 len =
254                                     (data[0] & opcodes_mi[opcode].len_mask) + 2;
255                                 if (len < opcodes_mi[opcode].min_len
256                                     || len > opcodes_mi[opcode].max_len) {
257                                         fprintf(out,
258                                                 "Bad length (%d) in %s, [%d, %d]\n",
259                                                 len, opcodes_mi[opcode].name,
260                                                 opcodes_mi[opcode].min_len,
261                                                 opcodes_mi[opcode].max_len);
262                                 }
263                         }
264                         opcode_mi = &opcodes_mi[opcode];
265                         break;
266                 }
267         }
269         if (opcode_mi && opcode_mi->func)
270                 return opcode_mi->func(ctx);
272         switch ((data[0] & 0x1f800000) >> 23) {
273         case 0x0a:
274                 instr_out(ctx, 0, "MI_BATCH_BUFFER_END\n");
275                 return -1;
276         case 0x16:
277                 instr_out(ctx, 0, "MI_SEMAPHORE_MBOX%s%s%s%s %u\n",
278                           data[0] & (1 << 22) ? " global gtt," : "",
279                           data[0] & (1 << 21) ? " update semaphore," : "",
280                           data[0] & (1 << 20) ? " compare semaphore," : "",
281                           data[0] & (1 << 18) ? " use compare reg" : "",
282                           (data[0] & (0x3 << 16)) >> 16);
283                 instr_out(ctx, 1, "value\n");
284                 instr_out(ctx, 2, "address\n");
285                 return len;
286         case 0x21:
287                 instr_out(ctx, 0, "MI_STORE_DATA_INDEX%s\n",
288                           data[0] & (1 << 21) ? " use per-process HWS," : "");
289                 instr_out(ctx, 1, "index\n");
290                 instr_out(ctx, 2, "dword\n");
291                 if (len == 4)
292                         instr_out(ctx, 3, "upper dword\n");
293                 return len;
294         case 0x00:
295                 if (data[0] & (1 << 22))
296                         instr_out(ctx, 0,
297                                   "MI_NOOP write NOPID reg, val=0x%x\n",
298                                   data[0] & ((1 << 22) - 1));
299                 else
300                         instr_out(ctx, 0, "MI_NOOP\n");
301                 return len;
302         case 0x26:
303                 switch (data[0] & (0x3 << 14)) {
304                 case (0 << 14):
305                         post_sync_op = "no write";
306                         break;
307                 case (1 << 14):
308                         post_sync_op = "write data";
309                         break;
310                 case (2 << 14):
311                         post_sync_op = "reserved";
312                         break;
313                 case (3 << 14):
314                         post_sync_op = "write TIMESTAMP";
315                         break;
316                 }
317                 instr_out(ctx, 0,
318                           "MI_FLUSH_DW%s%s%s%s post_sync_op='%s' %s%s\n",
319                           data[0] & (1 << 22) ?
320                           " enable protected mem (BCS-only)," : "",
321                           data[0] & (1 << 21) ? " store in hws," : "",
322                           data[0] & (1 << 18) ? " invalidate tlb," : "",
323                           data[0] & (1 << 17) ? " flush gfdt," : "",
324                           post_sync_op,
325                           data[0] & (1 << 8) ? " enable notify interrupt," : "",
326                           data[0] & (1 << 7) ?
327                           " invalidate video state (BCS-only)," : "");
328                 if (data[0] & (1 << 21))
329                         instr_out(ctx, 1, "hws index\n");
330                 else
331                         instr_out(ctx, 1, "address\n");
332                 instr_out(ctx, 2, "dword\n");
333                 if (len == 4)
334                         instr_out(ctx, 3, "upper dword\n");
335                 return len;
336         }
338         for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
339              opcode++) {
340                 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
341                         unsigned int i;
343                         instr_out(ctx, 0, "%s\n",
344                                   opcodes_mi[opcode].name);
345                         for (i = 1; i < len; i++) {
346                                 instr_out(ctx, i, "dword %d\n", i);
347                         }
349                         return len;
350                 }
351         }
353         instr_out(ctx, 0, "MI UNKNOWN\n");
354         return 1;
357 static void
358 decode_2d_br00(struct drm_intel_decode *ctx, const char *cmd)
360         instr_out(ctx, 0,
361                   "%s (rgb %sabled, alpha %sabled, src tile %d, dst tile %d)\n",
362                   cmd,
363                   (ctx->data[0] & (1 << 20)) ? "en" : "dis",
364                   (ctx->data[0] & (1 << 21)) ? "en" : "dis",
365                   (ctx->data[0] >> 15) & 1,
366                   (ctx->data[0] >> 11) & 1);
369 static void
370 decode_2d_br01(struct drm_intel_decode *ctx)
372         const char *format;
373         switch ((ctx->data[1] >> 24) & 0x3) {
374         case 0:
375                 format = "8";
376                 break;
377         case 1:
378                 format = "565";
379                 break;
380         case 2:
381                 format = "1555";
382                 break;
383         case 3:
384                 format = "8888";
385                 break;
386         }
388         instr_out(ctx, 1,
389                   "format %s, pitch %d, rop 0x%02x, "
390                   "clipping %sabled, %s%s \n",
391                   format,
392                   (short)(ctx->data[1] & 0xffff),
393                   (ctx->data[1] >> 16) & 0xff,
394                   ctx->data[1] & (1 << 30) ? "en" : "dis",
395                   ctx->data[1] & (1 << 31) ? "solid pattern enabled, " : "",
396                   ctx->data[1] & (1 << 31) ?
397                   "mono pattern transparency enabled, " : "");
401 static int
402 decode_2d(struct drm_intel_decode *ctx)
404         unsigned int opcode, len;
405         uint32_t *data = ctx->data;
407         struct {
408                 uint32_t opcode;
409                 unsigned int min_len;
410                 unsigned int max_len;
411                 const char *name;
412         } opcodes_2d[] = {
413                 { 0x40, 5, 5, "COLOR_BLT" },
414                 { 0x43, 6, 6, "SRC_COPY_BLT" },
415                 { 0x01, 8, 8, "XY_SETUP_BLT" },
416                 { 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
417                 { 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
418                 { 0x24, 2, 2, "XY_PIXEL_BLT" },
419                 { 0x25, 3, 3, "XY_SCANLINES_BLT" },
420                 { 0x26, 4, 4, "Y_TEXT_BLT" },
421                 { 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
422                 { 0x50, 6, 6, "XY_COLOR_BLT" },
423                 { 0x51, 6, 6, "XY_PAT_BLT" },
424                 { 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
425                 { 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
426                 { 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
427                 { 0x52, 9, 9, "XY_MONO_PAT_BLT" },
428                 { 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
429                 { 0x53, 8, 8, "XY_SRC_COPY_BLT" },
430                 { 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
431                 { 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
432                 { 0x55, 9, 9, "XY_FULL_BLT" },
433                 { 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
434                 { 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
435                 { 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
436                 { 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
437                 { 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT"},
438         };
440         switch ((data[0] & 0x1fc00000) >> 22) {
441         case 0x25:
442                 instr_out(ctx, 0,
443                           "XY_SCANLINES_BLT (pattern seed (%d, %d), dst tile %d)\n",
444                           (data[0] >> 12) & 0x8,
445                           (data[0] >> 8) & 0x8, (data[0] >> 11) & 1);
447                 len = (data[0] & 0x000000ff) + 2;
448                 if (len != 3)
449                         fprintf(out, "Bad count in XY_SCANLINES_BLT\n");
451                 instr_out(ctx, 1, "dest (%d,%d)\n",
452                           data[1] & 0xffff, data[1] >> 16);
453                 instr_out(ctx, 2, "dest (%d,%d)\n",
454                           data[2] & 0xffff, data[2] >> 16);
455                 return len;
456         case 0x01:
457                 decode_2d_br00(ctx, "XY_SETUP_BLT");
459                 len = (data[0] & 0x000000ff) + 2;
460                 if (len != 8)
461                         fprintf(out, "Bad count in XY_SETUP_BLT\n");
463                 decode_2d_br01(ctx);
464                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
465                           data[2] & 0xffff, data[2] >> 16);
466                 instr_out(ctx, 3, "cliprect (%d,%d)\n",
467                           data[3] & 0xffff, data[3] >> 16);
468                 instr_out(ctx, 4, "setup dst offset 0x%08x\n",
469                           data[4]);
470                 instr_out(ctx, 5, "setup background color\n");
471                 instr_out(ctx, 6, "setup foreground color\n");
472                 instr_out(ctx, 7, "color pattern offset\n");
473                 return len;
474         case 0x03:
475                 decode_2d_br00(ctx, "XY_SETUP_CLIP_BLT");
477                 len = (data[0] & 0x000000ff) + 2;
478                 if (len != 3)
479                         fprintf(out, "Bad count in XY_SETUP_CLIP_BLT\n");
481                 instr_out(ctx, 1, "cliprect (%d,%d)\n",
482                           data[1] & 0xffff, data[2] >> 16);
483                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
484                           data[2] & 0xffff, data[3] >> 16);
485                 return len;
486         case 0x11:
487                 decode_2d_br00(ctx, "XY_SETUP_MONO_PATTERN_SL_BLT");
489                 len = (data[0] & 0x000000ff) + 2;
490                 if (len != 9)
491                         fprintf(out,
492                                 "Bad count in XY_SETUP_MONO_PATTERN_SL_BLT\n");
494                 decode_2d_br01(ctx);
495                 instr_out(ctx, 2, "cliprect (%d,%d)\n",
496                           data[2] & 0xffff, data[2] >> 16);
497                 instr_out(ctx, 3, "cliprect (%d,%d)\n",
498                           data[3] & 0xffff, data[3] >> 16);
499                 instr_out(ctx, 4, "setup dst offset 0x%08x\n",
500                           data[4]);
501                 instr_out(ctx, 5, "setup background color\n");
502                 instr_out(ctx, 6, "setup foreground color\n");
503                 instr_out(ctx, 7, "mono pattern dw0\n");
504                 instr_out(ctx, 8, "mono pattern dw1\n");
505                 return len;
506         case 0x50:
507                 decode_2d_br00(ctx, "XY_COLOR_BLT");
509                 len = (data[0] & 0x000000ff) + 2;
510                 if (len != 6)
511                         fprintf(out, "Bad count in XY_COLOR_BLT\n");
513                 decode_2d_br01(ctx);
514                 instr_out(ctx, 2, "(%d,%d)\n",
515                           data[2] & 0xffff, data[2] >> 16);
516                 instr_out(ctx, 3, "(%d,%d)\n",
517                           data[3] & 0xffff, data[3] >> 16);
518                 instr_out(ctx, 4, "offset 0x%08x\n", data[4]);
519                 instr_out(ctx, 5, "color\n");
520                 return len;
521         case 0x53:
522                 decode_2d_br00(ctx, "XY_SRC_COPY_BLT");
524                 len = (data[0] & 0x000000ff) + 2;
525                 if (len != 8)
526                         fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
528                 decode_2d_br01(ctx);
529                 instr_out(ctx, 2, "dst (%d,%d)\n",
530                           data[2] & 0xffff, data[2] >> 16);
531                 instr_out(ctx, 3, "dst (%d,%d)\n",
532                           data[3] & 0xffff, data[3] >> 16);
533                 instr_out(ctx, 4, "dst offset 0x%08x\n", data[4]);
534                 instr_out(ctx, 5, "src (%d,%d)\n",
535                           data[5] & 0xffff, data[5] >> 16);
536                 instr_out(ctx, 6, "src pitch %d\n",
537                           (short)(data[6] & 0xffff));
538                 instr_out(ctx, 7, "src offset 0x%08x\n", data[7]);
539                 return len;
540         }
542         for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
543              opcode++) {
544                 if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
545                         unsigned int i;
547                         len = 1;
548                         instr_out(ctx, 0, "%s\n",
549                                   opcodes_2d[opcode].name);
550                         if (opcodes_2d[opcode].max_len > 1) {
551                                 len = (data[0] & 0x000000ff) + 2;
552                                 if (len < opcodes_2d[opcode].min_len ||
553                                     len > opcodes_2d[opcode].max_len) {
554                                         fprintf(out, "Bad count in %s\n",
555                                                 opcodes_2d[opcode].name);
556                                 }
557                         }
559                         for (i = 1; i < len; i++) {
560                                 instr_out(ctx, i, "dword %d\n", i);
561                         }
563                         return len;
564                 }
565         }
567         instr_out(ctx, 0, "2D UNKNOWN\n");
568         return 1;
571 static int
572 decode_3d_1c(struct drm_intel_decode *ctx)
574         uint32_t *data = ctx->data;
575         uint32_t opcode;
577         opcode = (data[0] & 0x00f80000) >> 19;
579         switch (opcode) {
580         case 0x11:
581                 instr_out(ctx, 0,
582                           "3DSTATE_DEPTH_SUBRECTANGLE_DISABLE\n");
583                 return 1;
584         case 0x10:
585                 instr_out(ctx, 0, "3DSTATE_SCISSOR_ENABLE %s\n",
586                           data[0] & 1 ? "enabled" : "disabled");
587                 return 1;
588         case 0x01:
589                 instr_out(ctx, 0, "3DSTATE_MAP_COORD_SET_I830\n");
590                 return 1;
591         case 0x0a:
592                 instr_out(ctx, 0, "3DSTATE_MAP_CUBE_I830\n");
593                 return 1;
594         case 0x05:
595                 instr_out(ctx, 0, "3DSTATE_MAP_TEX_STREAM_I830\n");
596                 return 1;
597         }
599         instr_out(ctx, 0, "3D UNKNOWN: 3d_1c opcode = 0x%x\n",
600                   opcode);
601         return 1;
604 /** Sets the string dstname to describe the destination of the PS instruction */
605 static void
606 i915_get_instruction_dst(uint32_t *data, int i, char *dstname, int do_mask)
608         uint32_t a0 = data[i];
609         int dst_nr = (a0 >> 14) & 0xf;
610         char dstmask[8];
611         const char *sat;
613         if (do_mask) {
614                 if (((a0 >> 10) & 0xf) == 0xf) {
615                         dstmask[0] = 0;
616                 } else {
617                         int dstmask_index = 0;
619                         dstmask[dstmask_index++] = '.';
620                         if (a0 & (1 << 10))
621                                 dstmask[dstmask_index++] = 'x';
622                         if (a0 & (1 << 11))
623                                 dstmask[dstmask_index++] = 'y';
624                         if (a0 & (1 << 12))
625                                 dstmask[dstmask_index++] = 'z';
626                         if (a0 & (1 << 13))
627                                 dstmask[dstmask_index++] = 'w';
628                         dstmask[dstmask_index++] = 0;
629                 }
631                 if (a0 & (1 << 22))
632                         sat = ".sat";
633                 else
634                         sat = "";
635         } else {
636                 dstmask[0] = 0;
637                 sat = "";
638         }
640         switch ((a0 >> 19) & 0x7) {
641         case 0:
642                 if (dst_nr > 15)
643                         fprintf(out, "bad destination reg R%d\n", dst_nr);
644                 sprintf(dstname, "R%d%s%s", dst_nr, dstmask, sat);
645                 break;
646         case 4:
647                 if (dst_nr > 0)
648                         fprintf(out, "bad destination reg oC%d\n", dst_nr);
649                 sprintf(dstname, "oC%s%s", dstmask, sat);
650                 break;
651         case 5:
652                 if (dst_nr > 0)
653                         fprintf(out, "bad destination reg oD%d\n", dst_nr);
654                 sprintf(dstname, "oD%s%s", dstmask, sat);
655                 break;
656         case 6:
657                 if (dst_nr > 3)
658                         fprintf(out, "bad destination reg U%d\n", dst_nr);
659                 sprintf(dstname, "U%d%s%s", dst_nr, dstmask, sat);
660                 break;
661         default:
662                 sprintf(dstname, "RESERVED");
663                 break;
664         }
667 static const char *
668 i915_get_channel_swizzle(uint32_t select)
670         switch (select & 0x7) {
671         case 0:
672                 return (select & 8) ? "-x" : "x";
673         case 1:
674                 return (select & 8) ? "-y" : "y";
675         case 2:
676                 return (select & 8) ? "-z" : "z";
677         case 3:
678                 return (select & 8) ? "-w" : "w";
679         case 4:
680                 return (select & 8) ? "-0" : "0";
681         case 5:
682                 return (select & 8) ? "-1" : "1";
683         default:
684                 return (select & 8) ? "-bad" : "bad";
685         }
688 static void
689 i915_get_instruction_src_name(uint32_t src_type, uint32_t src_nr, char *name)
691         switch (src_type) {
692         case 0:
693                 sprintf(name, "R%d", src_nr);
694                 if (src_nr > 15)
695                         fprintf(out, "bad src reg %s\n", name);
696                 break;
697         case 1:
698                 if (src_nr < 8)
699                         sprintf(name, "T%d", src_nr);
700                 else if (src_nr == 8)
701                         sprintf(name, "DIFFUSE");
702                 else if (src_nr == 9)
703                         sprintf(name, "SPECULAR");
704                 else if (src_nr == 10)
705                         sprintf(name, "FOG");
706                 else {
707                         fprintf(out, "bad src reg T%d\n", src_nr);
708                         sprintf(name, "RESERVED");
709                 }
710                 break;
711         case 2:
712                 sprintf(name, "C%d", src_nr);
713                 if (src_nr > 31)
714                         fprintf(out, "bad src reg %s\n", name);
715                 break;
716         case 4:
717                 sprintf(name, "oC");
718                 if (src_nr > 0)
719                         fprintf(out, "bad src reg oC%d\n", src_nr);
720                 break;
721         case 5:
722                 sprintf(name, "oD");
723                 if (src_nr > 0)
724                         fprintf(out, "bad src reg oD%d\n", src_nr);
725                 break;
726         case 6:
727                 sprintf(name, "U%d", src_nr);
728                 if (src_nr > 3)
729                         fprintf(out, "bad src reg %s\n", name);
730                 break;
731         default:
732                 fprintf(out, "bad src reg type %d\n", src_type);
733                 sprintf(name, "RESERVED");
734                 break;
735         }
738 static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
740         uint32_t a0 = data[i];
741         uint32_t a1 = data[i + 1];
742         int src_nr = (a0 >> 2) & 0x1f;
743         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 28) & 0xf);
744         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 24) & 0xf);
745         const char *swizzle_z = i915_get_channel_swizzle((a1 >> 20) & 0xf);
746         const char *swizzle_w = i915_get_channel_swizzle((a1 >> 16) & 0xf);
747         char swizzle[100];
749         i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
750         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
751                 swizzle_w);
752         if (strcmp(swizzle, ".xyzw") != 0)
753                 strcat(srcname, swizzle);
756 static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
758         uint32_t a1 = data[i + 1];
759         uint32_t a2 = data[i + 2];
760         int src_nr = (a1 >> 8) & 0x1f;
761         const char *swizzle_x = i915_get_channel_swizzle((a1 >> 4) & 0xf);
762         const char *swizzle_y = i915_get_channel_swizzle((a1 >> 0) & 0xf);
763         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 28) & 0xf);
764         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 24) & 0xf);
765         char swizzle[100];
767         i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
768         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
769                 swizzle_w);
770         if (strcmp(swizzle, ".xyzw") != 0)
771                 strcat(srcname, swizzle);
774 static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
776         uint32_t a2 = data[i + 2];
777         int src_nr = (a2 >> 16) & 0x1f;
778         const char *swizzle_x = i915_get_channel_swizzle((a2 >> 12) & 0xf);
779         const char *swizzle_y = i915_get_channel_swizzle((a2 >> 8) & 0xf);
780         const char *swizzle_z = i915_get_channel_swizzle((a2 >> 4) & 0xf);
781         const char *swizzle_w = i915_get_channel_swizzle((a2 >> 0) & 0xf);
782         char swizzle[100];
784         i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
785         sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
786                 swizzle_w);
787         if (strcmp(swizzle, ".xyzw") != 0)
788                 strcat(srcname, swizzle);
791 static void
792 i915_get_instruction_addr(uint32_t src_type, uint32_t src_nr, char *name)
794         switch (src_type) {
795         case 0:
796                 sprintf(name, "R%d", src_nr);
797                 if (src_nr > 15)
798                         fprintf(out, "bad src reg %s\n", name);
799                 break;
800         case 1:
801                 if (src_nr < 8)
802                         sprintf(name, "T%d", src_nr);
803                 else if (src_nr == 8)
804                         sprintf(name, "DIFFUSE");
805                 else if (src_nr == 9)
806                         sprintf(name, "SPECULAR");
807                 else if (src_nr == 10)
808                         sprintf(name, "FOG");
809                 else {
810                         fprintf(out, "bad src reg T%d\n", src_nr);
811                         sprintf(name, "RESERVED");
812                 }
813                 break;
814         case 4:
815                 sprintf(name, "oC");
816                 if (src_nr > 0)
817                         fprintf(out, "bad src reg oC%d\n", src_nr);
818                 break;
819         case 5:
820                 sprintf(name, "oD");
821                 if (src_nr > 0)
822                         fprintf(out, "bad src reg oD%d\n", src_nr);
823                 break;
824         default:
825                 fprintf(out, "bad src reg type %d\n", src_type);
826                 sprintf(name, "RESERVED");
827                 break;
828         }
831 static void
832 i915_decode_alu1(struct drm_intel_decode *ctx,
833                  int i, char *instr_prefix, const char *op_name)
835         char dst[100], src0[100];
837         i915_get_instruction_dst(ctx->data, i, dst, 1);
838         i915_get_instruction_src0(ctx->data, i, src0);
840         instr_out(ctx, i++, "%s: %s %s, %s\n", instr_prefix,
841                   op_name, dst, src0);
842         instr_out(ctx, i++, "%s\n", instr_prefix);
843         instr_out(ctx, i++, "%s\n", instr_prefix);
846 static void
847 i915_decode_alu2(struct drm_intel_decode *ctx,
848                  int i, char *instr_prefix, const char *op_name)
850         char dst[100], src0[100], src1[100];
852         i915_get_instruction_dst(ctx->data, i, dst, 1);
853         i915_get_instruction_src0(ctx->data, i, src0);
854         i915_get_instruction_src1(ctx->data, i, src1);
856         instr_out(ctx, i++, "%s: %s %s, %s, %s\n", instr_prefix,
857                   op_name, dst, src0, src1);
858         instr_out(ctx, i++, "%s\n", instr_prefix);
859         instr_out(ctx, i++, "%s\n", instr_prefix);
862 static void
863 i915_decode_alu3(struct drm_intel_decode *ctx,
864                  int i, char *instr_prefix, const char *op_name)
866         char dst[100], src0[100], src1[100], src2[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);
871         i915_get_instruction_src2(ctx->data, i, src2);
873         instr_out(ctx, i++, "%s: %s %s, %s, %s, %s\n", instr_prefix,
874                   op_name, dst, src0, src1, src2);
875         instr_out(ctx, i++, "%s\n", instr_prefix);
876         instr_out(ctx, i++, "%s\n", instr_prefix);
879 static void
880 i915_decode_tex(struct drm_intel_decode *ctx, int i,
881                 const char *instr_prefix, const char *tex_name)
883         uint32_t t0 = ctx->data[i];
884         uint32_t t1 = ctx->data[i + 1];
885         char dst_name[100];
886         char addr_name[100];
887         int sampler_nr;
889         i915_get_instruction_dst(ctx->data, i, dst_name, 0);
890         i915_get_instruction_addr((t1 >> 24) & 0x7,
891                                   (t1 >> 17) & 0xf, addr_name);
892         sampler_nr = t0 & 0xf;
894         instr_out(ctx, i++, "%s: %s %s, S%d, %s\n", instr_prefix,
895                   tex_name, dst_name, sampler_nr, addr_name);
896         instr_out(ctx, i++, "%s\n", instr_prefix);
897         instr_out(ctx, i++, "%s\n", instr_prefix);
900 static void
901 i915_decode_dcl(struct drm_intel_decode *ctx, int i, char *instr_prefix)
903         uint32_t d0 = ctx->data[i];
904         const char *sampletype;
905         int dcl_nr = (d0 >> 14) & 0xf;
906         const char *dcl_x = d0 & (1 << 10) ? "x" : "";
907         const char *dcl_y = d0 & (1 << 11) ? "y" : "";
908         const char *dcl_z = d0 & (1 << 12) ? "z" : "";
909         const char *dcl_w = d0 & (1 << 13) ? "w" : "";
910         char dcl_mask[10];
912         switch ((d0 >> 19) & 0x3) {
913         case 1:
914                 sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
915                 if (strcmp(dcl_mask, ".") == 0)
916                         fprintf(out, "bad (empty) dcl mask\n");
918                 if (dcl_nr > 10)
919                         fprintf(out, "bad T%d dcl register number\n", dcl_nr);
920                 if (dcl_nr < 8) {
921                         if (strcmp(dcl_mask, ".x") != 0 &&
922                             strcmp(dcl_mask, ".xy") != 0 &&
923                             strcmp(dcl_mask, ".xz") != 0 &&
924                             strcmp(dcl_mask, ".w") != 0 &&
925                             strcmp(dcl_mask, ".xyzw") != 0) {
926                                 fprintf(out, "bad T%d.%s dcl mask\n", dcl_nr,
927                                         dcl_mask);
928                         }
929                         instr_out(ctx, i++, "%s: DCL T%d%s\n",
930                                   instr_prefix, dcl_nr, dcl_mask);
931                 } else {
932                         if (strcmp(dcl_mask, ".xz") == 0)
933                                 fprintf(out, "errataed bad dcl mask %s\n",
934                                         dcl_mask);
935                         else if (strcmp(dcl_mask, ".xw") == 0)
936                                 fprintf(out, "errataed bad dcl mask %s\n",
937                                         dcl_mask);
938                         else if (strcmp(dcl_mask, ".xzw") == 0)
939                                 fprintf(out, "errataed bad dcl mask %s\n",
940                                         dcl_mask);
942                         if (dcl_nr == 8) {
943                                 instr_out(ctx, i++,
944                                           "%s: DCL DIFFUSE%s\n", instr_prefix,
945                                           dcl_mask);
946                         } else if (dcl_nr == 9) {
947                                 instr_out(ctx, i++,
948                                           "%s: DCL SPECULAR%s\n", instr_prefix,
949                                           dcl_mask);
950                         } else if (dcl_nr == 10) {
951                                 instr_out(ctx, i++,
952                                           "%s: DCL FOG%s\n", instr_prefix,
953                                           dcl_mask);
954                         }
955                 }
956                 instr_out(ctx, i++, "%s\n", instr_prefix);
957                 instr_out(ctx, i++, "%s\n", instr_prefix);
958                 break;
959         case 3:
960                 switch ((d0 >> 22) & 0x3) {
961                 case 0:
962                         sampletype = "2D";
963                         break;
964                 case 1:
965                         sampletype = "CUBE";
966                         break;
967                 case 2:
968                         sampletype = "3D";
969                         break;
970                 default:
971                         sampletype = "RESERVED";
972                         break;
973                 }
974                 if (dcl_nr > 15)
975                         fprintf(out, "bad S%d dcl register number\n", dcl_nr);
976                 instr_out(ctx, i++, "%s: DCL S%d %s\n",
977                           instr_prefix, dcl_nr, sampletype);
978                 instr_out(ctx, i++, "%s\n", instr_prefix);
979                 instr_out(ctx, i++, "%s\n", instr_prefix);
980                 break;
981         default:
982                 instr_out(ctx, i++, "%s: DCL RESERVED%d\n",
983                           instr_prefix, dcl_nr);
984                 instr_out(ctx, i++, "%s\n", instr_prefix);
985                 instr_out(ctx, i++, "%s\n", instr_prefix);
986         }
989 static void
990 i915_decode_instruction(struct drm_intel_decode *ctx,
991                         int i, char *instr_prefix)
993         switch ((ctx->data[i] >> 24) & 0x1f) {
994         case 0x0:
995                 instr_out(ctx, i++, "%s: NOP\n", instr_prefix);
996                 instr_out(ctx, i++, "%s\n", instr_prefix);
997                 instr_out(ctx, i++, "%s\n", instr_prefix);
998                 break;
999         case 0x01:
1000                 i915_decode_alu2(ctx, i, instr_prefix, "ADD");
1001                 break;
1002         case 0x02:
1003                 i915_decode_alu1(ctx, i, instr_prefix, "MOV");
1004                 break;
1005         case 0x03:
1006                 i915_decode_alu2(ctx, i, instr_prefix, "MUL");
1007                 break;
1008         case 0x04:
1009                 i915_decode_alu3(ctx, i, instr_prefix, "MAD");
1010                 break;
1011         case 0x05:
1012                 i915_decode_alu3(ctx, i, instr_prefix, "DP2ADD");
1013                 break;
1014         case 0x06:
1015                 i915_decode_alu2(ctx, i, instr_prefix, "DP3");
1016                 break;
1017         case 0x07:
1018                 i915_decode_alu2(ctx, i, instr_prefix, "DP4");
1019                 break;
1020         case 0x08:
1021                 i915_decode_alu1(ctx, i, instr_prefix, "FRC");
1022                 break;
1023         case 0x09:
1024                 i915_decode_alu1(ctx, i, instr_prefix, "RCP");
1025                 break;
1026         case 0x0a:
1027                 i915_decode_alu1(ctx, i, instr_prefix, "RSQ");
1028                 break;
1029         case 0x0b:
1030                 i915_decode_alu1(ctx, i, instr_prefix, "EXP");
1031                 break;
1032         case 0x0c:
1033                 i915_decode_alu1(ctx, i, instr_prefix, "LOG");
1034                 break;
1035         case 0x0d:
1036                 i915_decode_alu2(ctx, i, instr_prefix, "CMP");
1037                 break;
1038         case 0x0e:
1039                 i915_decode_alu2(ctx, i, instr_prefix, "MIN");
1040                 break;
1041         case 0x0f:
1042                 i915_decode_alu2(ctx, i, instr_prefix, "MAX");
1043                 break;
1044         case 0x10:
1045                 i915_decode_alu1(ctx, i, instr_prefix, "FLR");
1046                 break;
1047         case 0x11:
1048                 i915_decode_alu1(ctx, i, instr_prefix, "MOD");
1049                 break;
1050         case 0x12:
1051                 i915_decode_alu1(ctx, i, instr_prefix, "TRC");
1052                 break;
1053         case 0x13:
1054                 i915_decode_alu2(ctx, i, instr_prefix, "SGE");
1055                 break;
1056         case 0x14:
1057                 i915_decode_alu2(ctx, i, instr_prefix, "SLT");
1058                 break;
1059         case 0x15:
1060                 i915_decode_tex(ctx, i, instr_prefix, "TEXLD");
1061                 break;
1062         case 0x16:
1063                 i915_decode_tex(ctx, i, instr_prefix, "TEXLDP");
1064                 break;
1065         case 0x17:
1066                 i915_decode_tex(ctx, i, instr_prefix, "TEXLDB");
1067                 break;
1068         case 0x19:
1069                 i915_decode_dcl(ctx, i, instr_prefix);
1070                 break;
1071         default:
1072                 instr_out(ctx, i++, "%s: unknown\n", instr_prefix);
1073                 instr_out(ctx, i++, "%s\n", instr_prefix);
1074                 instr_out(ctx, i++, "%s\n", instr_prefix);
1075                 break;
1076         }
1079 static const char *
1080 decode_compare_func(uint32_t op)
1082         switch (op & 0x7) {
1083         case 0:
1084                 return "always";
1085         case 1:
1086                 return "never";
1087         case 2:
1088                 return "less";
1089         case 3:
1090                 return "equal";
1091         case 4:
1092                 return "lequal";
1093         case 5:
1094                 return "greater";
1095         case 6:
1096                 return "notequal";
1097         case 7:
1098                 return "gequal";
1099         }
1100         return "";
1103 static const char *
1104 decode_stencil_op(uint32_t op)
1106         switch (op & 0x7) {
1107         case 0:
1108                 return "keep";
1109         case 1:
1110                 return "zero";
1111         case 2:
1112                 return "replace";
1113         case 3:
1114                 return "incr_sat";
1115         case 4:
1116                 return "decr_sat";
1117         case 5:
1118                 return "greater";
1119         case 6:
1120                 return "incr";
1121         case 7:
1122                 return "decr";
1123         }
1124         return "";
1127 #if 0
1128 static const char *
1129 decode_logic_op(uint32_t op)
1131         switch (op & 0xf) {
1132         case 0:
1133                 return "clear";
1134         case 1:
1135                 return "nor";
1136         case 2:
1137                 return "and_inv";
1138         case 3:
1139                 return "copy_inv";
1140         case 4:
1141                 return "and_rvrse";
1142         case 5:
1143                 return "inv";
1144         case 6:
1145                 return "xor";
1146         case 7:
1147                 return "nand";
1148         case 8:
1149                 return "and";
1150         case 9:
1151                 return "equiv";
1152         case 10:
1153                 return "noop";
1154         case 11:
1155                 return "or_inv";
1156         case 12:
1157                 return "copy";
1158         case 13:
1159                 return "or_rvrse";
1160         case 14:
1161                 return "or";
1162         case 15:
1163                 return "set";
1164         }
1165         return "";
1167 #endif
1169 static const char *
1170 decode_blend_fact(uint32_t op)
1172         switch (op & 0xf) {
1173         case 1:
1174                 return "zero";
1175         case 2:
1176                 return "one";
1177         case 3:
1178                 return "src_colr";
1179         case 4:
1180                 return "inv_src_colr";
1181         case 5:
1182                 return "src_alpha";
1183         case 6:
1184                 return "inv_src_alpha";
1185         case 7:
1186                 return "dst_alpha";
1187         case 8:
1188                 return "inv_dst_alpha";
1189         case 9:
1190                 return "dst_colr";
1191         case 10:
1192                 return "inv_dst_colr";
1193         case 11:
1194                 return "src_alpha_sat";
1195         case 12:
1196                 return "cnst_colr";
1197         case 13:
1198                 return "inv_cnst_colr";
1199         case 14:
1200                 return "cnst_alpha";
1201         case 15:
1202                 return "inv_const_alpha";
1203         }
1204         return "";
1207 static const char *
1208 decode_tex_coord_mode(uint32_t mode)
1210         switch (mode & 0x7) {
1211         case 0:
1212                 return "wrap";
1213         case 1:
1214                 return "mirror";
1215         case 2:
1216                 return "clamp_edge";
1217         case 3:
1218                 return "cube";
1219         case 4:
1220                 return "clamp_border";
1221         case 5:
1222                 return "mirror_once";
1223         }
1224         return "";
1227 static const char *
1228 decode_sample_filter(uint32_t mode)
1230         switch (mode & 0x7) {
1231         case 0:
1232                 return "nearest";
1233         case 1:
1234                 return "linear";
1235         case 2:
1236                 return "anisotropic";
1237         case 3:
1238                 return "4x4_1";
1239         case 4:
1240                 return "4x4_2";
1241         case 5:
1242                 return "4x4_flat";
1243         case 6:
1244                 return "6x5_mono";
1245         }
1246         return "";
1249 static int
1250 decode_3d_1d(struct drm_intel_decode *ctx)
1252         unsigned int len, i, c, idx, word, map, sampler, instr;
1253         const char *format, *zformat, *type;
1254         uint32_t opcode;
1255         uint32_t *data = ctx->data;
1256         uint32_t devid = ctx->devid;
1258         struct {
1259                 uint32_t opcode;
1260                 int i830_only;
1261                 unsigned int min_len;
1262                 unsigned int max_len;
1263                 const char *name;
1264         } opcodes_3d_1d[] = {
1265                 { 0x86, 0, 4, 4, "3DSTATE_CHROMA_KEY" },
1266                 { 0x88, 0, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
1267                 { 0x99, 0, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
1268                 { 0x9a, 0, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
1269                 { 0x98, 0, 2, 2, "3DSTATE_DEFAULT_Z" },
1270                 { 0x97, 0, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
1271                 { 0x9d, 0, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
1272                 { 0x9e, 0, 4, 4, "3DSTATE_MONO_FILTER" },
1273                 { 0x89, 0, 4, 4, "3DSTATE_FOG_MODE" },
1274                 { 0x8f, 0, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
1275                 { 0x83, 0, 2, 2, "3DSTATE_SPAN_STIPPLE" },
1276                 { 0x8c, 1, 2, 2, "3DSTATE_MAP_COORD_TRANSFORM_I830" },
1277                 { 0x8b, 1, 2, 2, "3DSTATE_MAP_VERTEX_TRANSFORM_I830" },
1278                 { 0x8d, 1, 3, 3, "3DSTATE_W_STATE_I830" },
1279                 { 0x01, 1, 2, 2, "3DSTATE_COLOR_FACTOR_I830" },
1280                 { 0x02, 1, 2, 2, "3DSTATE_MAP_COORD_SETBIND_I830"},
1281         }, *opcode_3d_1d;
1283         opcode = (data[0] & 0x00ff0000) >> 16;
1285         switch (opcode) {
1286         case 0x07:
1287                 /* This instruction is unusual.  A 0 length means just
1288                  * 1 DWORD instead of 2.  The 0 length is specified in
1289                  * one place to be unsupported, but stated to be
1290                  * required in another, and 0 length LOAD_INDIRECTs
1291                  * appear to cause no harm at least.
1292                  */
1293                 instr_out(ctx, 0, "3DSTATE_LOAD_INDIRECT\n");
1294                 len = (data[0] & 0x000000ff) + 1;
1295                 i = 1;
1296                 if (data[0] & (0x01 << 8)) {
1297                         instr_out(ctx, i++, "SIS.0\n");
1298                         instr_out(ctx, i++, "SIS.1\n");
1299                 }
1300                 if (data[0] & (0x02 << 8)) {
1301                         instr_out(ctx, i++, "DIS.0\n");
1302                 }
1303                 if (data[0] & (0x04 << 8)) {
1304                         instr_out(ctx, i++, "SSB.0\n");
1305                         instr_out(ctx, i++, "SSB.1\n");
1306                 }
1307                 if (data[0] & (0x08 << 8)) {
1308                         instr_out(ctx, i++, "MSB.0\n");
1309                         instr_out(ctx, i++, "MSB.1\n");
1310                 }
1311                 if (data[0] & (0x10 << 8)) {
1312                         instr_out(ctx, i++, "PSP.0\n");
1313                         instr_out(ctx, i++, "PSP.1\n");
1314                 }
1315                 if (data[0] & (0x20 << 8)) {
1316                         instr_out(ctx, i++, "PSC.0\n");
1317                         instr_out(ctx, i++, "PSC.1\n");
1318                 }
1319                 if (len != i) {
1320                         fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
1321                         return len;
1322                 }
1323                 return len;
1324         case 0x04:
1325                 instr_out(ctx, 0,
1326                           "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1327                 len = (data[0] & 0x0000000f) + 2;
1328                 i = 1;
1329                 for (word = 0; word <= 8; word++) {
1330                         if (data[0] & (1 << (4 + word))) {
1331                                 /* save vertex state for decode */
1332                                 if (!IS_GEN2(devid)) {
1333                                         int tex_num;
1335                                         if (word == 2) {
1336                                                 saved_s2_set = 1;
1337                                                 saved_s2 = data[i];
1338                                         }
1339                                         if (word == 4) {
1340                                                 saved_s4_set = 1;
1341                                                 saved_s4 = data[i];
1342                                         }
1344                                         switch (word) {
1345                                         case 0:
1346                                                 instr_out(ctx, i,
1347                                                           "S0: vbo offset: 0x%08x%s\n",
1348                                                           data[i] & (~1),
1349                                                           data[i] & 1 ?
1350                                                           ", auto cache invalidate disabled"
1351                                                           : "");
1352                                                 break;
1353                                         case 1:
1354                                                 instr_out(ctx, i,
1355                                                           "S1: vertex width: %i, vertex pitch: %i\n",
1356                                                           (data[i] >> 24) &
1357                                                           0x3f,
1358                                                           (data[i] >> 16) &
1359                                                           0x3f);
1360                                                 break;
1361                                         case 2:
1362                                                 instr_out(ctx, i,
1363                                                           "S2: texcoord formats: ");
1364                                                 for (tex_num = 0;
1365                                                      tex_num < 8; tex_num++) {
1366                                                         switch ((data[i] >>
1367                                                                  tex_num *
1368                                                                  4) & 0xf) {
1369                                                         case 0:
1370                                                                 fprintf(out,
1371                                                                         "%i=2D ",
1372                                                                         tex_num);
1373                                                                 break;
1374                                                         case 1:
1375                                                                 fprintf(out,
1376                                                                         "%i=3D ",
1377                                                                         tex_num);
1378                                                                 break;
1379                                                         case 2:
1380                                                                 fprintf(out,
1381                                                                         "%i=4D ",
1382                                                                         tex_num);
1383                                                                 break;
1384                                                         case 3:
1385                                                                 fprintf(out,
1386                                                                         "%i=1D ",
1387                                                                         tex_num);
1388                                                                 break;
1389                                                         case 4:
1390                                                                 fprintf(out,
1391                                                                         "%i=2D_16 ",
1392                                                                         tex_num);
1393                                                                 break;
1394                                                         case 5:
1395                                                                 fprintf(out,
1396                                                                         "%i=4D_16 ",
1397                                                                         tex_num);
1398                                                                 break;
1399                                                         case 0xf:
1400                                                                 fprintf(out,
1401                                                                         "%i=NP ",
1402                                                                         tex_num);
1403                                                                 break;
1404                                                         }
1405                                                 }
1406                                                 fprintf(out, "\n");
1408                                                 break;
1409                                         case 3:
1410                                                 instr_out(ctx, i,
1411                                                           "S3: not documented\n");
1412                                                 break;
1413                                         case 4:
1414                                                 {
1415                                                         const char *cullmode = "";
1416                                                         const char *vfmt_xyzw = "";
1417                                                         switch ((data[i] >> 13)
1418                                                                 & 0x3) {
1419                                                         case 0:
1420                                                                 cullmode =
1421                                                                     "both";
1422                                                                 break;
1423                                                         case 1:
1424                                                                 cullmode =
1425                                                                     "none";
1426                                                                 break;
1427                                                         case 2:
1428                                                                 cullmode = "cw";
1429                                                                 break;
1430                                                         case 3:
1431                                                                 cullmode =
1432                                                                     "ccw";
1433                                                                 break;
1434                                                         }
1435                                                         switch (data[i] &
1436                                                                 (7 << 6 | 1 <<
1437                                                                  2)) {
1438                                                         case 1 << 6:
1439                                                                 vfmt_xyzw =
1440                                                                     "XYZ,";
1441                                                                 break;
1442                                                         case 2 << 6:
1443                                                                 vfmt_xyzw =
1444                                                                     "XYZW,";
1445                                                                 break;
1446                                                         case 3 << 6:
1447                                                                 vfmt_xyzw =
1448                                                                     "XY,";
1449                                                                 break;
1450                                                         case 4 << 6:
1451                                                                 vfmt_xyzw =
1452                                                                     "XYW,";
1453                                                                 break;
1454                                                         case 1 << 6 | 1 << 2:
1455                                                                 vfmt_xyzw =
1456                                                                     "XYZF,";
1457                                                                 break;
1458                                                         case 2 << 6 | 1 << 2:
1459                                                                 vfmt_xyzw =
1460                                                                     "XYZWF,";
1461                                                                 break;
1462                                                         case 3 << 6 | 1 << 2:
1463                                                                 vfmt_xyzw =
1464                                                                     "XYF,";
1465                                                                 break;
1466                                                         case 4 << 6 | 1 << 2:
1467                                                                 vfmt_xyzw =
1468                                                                     "XYWF,";
1469                                                                 break;
1470                                                         }
1471                                                         instr_out(ctx, i,
1472                                                                   "S4: point_width=%i, line_width=%.1f,"
1473                                                                   "%s%s%s%s%s cullmode=%s, vfmt=%s%s%s%s%s%s "
1474                                                                   "%s%s%s%s%s\n",
1475                                                                   (data[i] >>
1476                                                                    23) & 0x1ff,
1477                                                                   ((data[i] >>
1478                                                                     19) & 0xf) /
1479                                                                   2.0,
1480                                                                   data[i] & (0xf
1481                                                                              <<
1482                                                                              15)
1483                                                                   ?
1484                                                                   " flatshade="
1485                                                                   : "",
1486                                                                   data[i] & (1
1487                                                                              <<
1488                                                                              18)
1489                                                                   ? "Alpha," :
1490                                                                   "",
1491                                                                   data[i] & (1
1492                                                                              <<
1493                                                                              17)
1494                                                                   ? "Fog," : "",
1495                                                                   data[i] & (1
1496                                                                              <<
1497                                                                              16)
1498                                                                   ? "Specular,"
1499                                                                   : "",
1500                                                                   data[i] & (1
1501                                                                              <<
1502                                                                              15)
1503                                                                   ? "Color," :
1504                                                                   "", cullmode,
1505                                                                   data[i] & (1
1506                                                                              <<
1507                                                                              12)
1508                                                                   ?
1509                                                                   "PointWidth,"
1510                                                                   : "",
1511                                                                   data[i] & (1
1512                                                                              <<
1513                                                                              11)
1514                                                                   ? "SpecFog," :
1515                                                                   "",
1516                                                                   data[i] & (1
1517                                                                              <<
1518                                                                              10)
1519                                                                   ? "Color," :
1520                                                                   "",
1521                                                                   data[i] & (1
1522                                                                              <<
1523                                                                              9)
1524                                                                   ? "DepthOfs,"
1525                                                                   : "",
1526                                                                   vfmt_xyzw,
1527                                                                   data[i] & (1
1528                                                                              <<
1529                                                                              9)
1530                                                                   ? "FogParam,"
1531                                                                   : "",
1532                                                                   data[i] & (1
1533                                                                              <<
1534                                                                              5)
1535                                                                   ?
1536                                                                   "force default diffuse, "
1537                                                                   : "",
1538                                                                   data[i] & (1
1539                                                                              <<
1540                                                                              4)
1541                                                                   ?
1542                                                                   "force default specular, "
1543                                                                   : "",
1544                                                                   data[i] & (1
1545                                                                              <<
1546                                                                              3)
1547                                                                   ?
1548                                                                   "local depth ofs enable, "
1549                                                                   : "",
1550                                                                   data[i] & (1
1551                                                                              <<
1552                                                                              1)
1553                                                                   ?
1554                                                                   "point sprite enable, "
1555                                                                   : "",
1556                                                                   data[i] & (1
1557                                                                              <<
1558                                                                              0)
1559                                                                   ?
1560                                                                   "line AA enable, "
1561                                                                   : "");
1562                                                         break;
1563                                                 }
1564                                         case 5:
1565                                                 {
1566                                                         instr_out(ctx, i,
1567                                                                   "S5:%s%s%s%s%s"
1568                                                                   "%s%s%s%s stencil_ref=0x%x, stencil_test=%s, "
1569                                                                   "stencil_fail=%s, stencil_pass_z_fail=%s, "
1570                                                                   "stencil_pass_z_pass=%s, %s%s%s%s\n",
1571                                                                   data[i] & (0xf
1572                                                                              <<
1573                                                                              28)
1574                                                                   ?
1575                                                                   " write_disable="
1576                                                                   : "",
1577                                                                   data[i] & (1
1578                                                                              <<
1579                                                                              31)
1580                                                                   ? "Alpha," :
1581                                                                   "",
1582                                                                   data[i] & (1
1583                                                                              <<
1584                                                                              30)
1585                                                                   ? "Red," : "",
1586                                                                   data[i] & (1
1587                                                                              <<
1588                                                                              29)
1589                                                                   ? "Green," :
1590                                                                   "",
1591                                                                   data[i] & (1
1592                                                                              <<
1593                                                                              28)
1594                                                                   ? "Blue," :
1595                                                                   "",
1596                                                                   data[i] & (1
1597                                                                              <<
1598                                                                              27)
1599                                                                   ?
1600                                                                   " force default point size,"
1601                                                                   : "",
1602                                                                   data[i] & (1
1603                                                                              <<
1604                                                                              26)
1605                                                                   ?
1606                                                                   " last pixel enable,"
1607                                                                   : "",
1608                                                                   data[i] & (1
1609                                                                              <<
1610                                                                              25)
1611                                                                   ?
1612                                                                   " global depth ofs enable,"
1613                                                                   : "",
1614                                                                   data[i] & (1
1615                                                                              <<
1616                                                                              24)
1617                                                                   ?
1618                                                                   " fog enable,"
1619                                                                   : "",
1620                                                                   (data[i] >>
1621                                                                    16) & 0xff,
1622                                                                   decode_compare_func
1623                                                                   (data[i] >>
1624                                                                    13),
1625                                                                   decode_stencil_op
1626                                                                   (data[i] >>
1627                                                                    10),
1628                                                                   decode_stencil_op
1629                                                                   (data[i] >>
1630                                                                    7),
1631                                                                   decode_stencil_op
1632                                                                   (data[i] >>
1633                                                                    4),
1634                                                                   data[i] & (1
1635                                                                              <<
1636                                                                              3)
1637                                                                   ?
1638                                                                   "stencil write enable, "
1639                                                                   : "",
1640                                                                   data[i] & (1
1641                                                                              <<
1642                                                                              2)
1643                                                                   ?
1644                                                                   "stencil test enable, "
1645                                                                   : "",
1646                                                                   data[i] & (1
1647                                                                              <<
1648                                                                              1)
1649                                                                   ?
1650                                                                   "color dither enable, "
1651                                                                   : "",
1652                                                                   data[i] & (1
1653                                                                              <<
1654                                                                              0)
1655                                                                   ?
1656                                                                   "logicop enable, "
1657                                                                   : "");
1658                                                 }
1659                                                 break;
1660                                         case 6:
1661                                                 instr_out(ctx, i,
1662                                                           "S6: %salpha_test=%s, alpha_ref=0x%x, "
1663                                                           "depth_test=%s, %ssrc_blnd_fct=%s, dst_blnd_fct=%s, "
1664                                                           "%s%stristrip_provoking_vertex=%i\n",
1665                                                           data[i] & (1 << 31) ?
1666                                                           "alpha test enable, "
1667                                                           : "",
1668                                                           decode_compare_func
1669                                                           (data[i] >> 28),
1670                                                           data[i] & (0xff <<
1671                                                                      20),
1672                                                           decode_compare_func
1673                                                           (data[i] >> 16),
1674                                                           data[i] & (1 << 15) ?
1675                                                           "cbuf blend enable, "
1676                                                           : "",
1677                                                           decode_blend_fact(data
1678                                                                             [i]
1679                                                                             >>
1680                                                                             8),
1681                                                           decode_blend_fact(data
1682                                                                             [i]
1683                                                                             >>
1684                                                                             4),
1685                                                           data[i] & (1 << 3) ?
1686                                                           "depth write enable, "
1687                                                           : "",
1688                                                           data[i] & (1 << 2) ?
1689                                                           "cbuf write enable, "
1690                                                           : "",
1691                                                           data[i] & (0x3));
1692                                                 break;
1693                                         case 7:
1694                                                 instr_out(ctx, i,
1695                                                           "S7: depth offset constant: 0x%08x\n",
1696                                                           data[i]);
1697                                                 break;
1698                                         }
1699                                 } else {
1700                                         instr_out(ctx, i,
1701                                                   "S%d: 0x%08x\n", i, data[i]);
1702                                 }
1703                                 i++;
1704                         }
1705                 }
1706                 if (len != i) {
1707                         fprintf(out,
1708                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
1709                 }
1710                 return len;
1711         case 0x03:
1712                 instr_out(ctx, 0,
1713                           "3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1714                 len = (data[0] & 0x0000000f) + 2;
1715                 i = 1;
1716                 for (word = 6; word <= 14; word++) {
1717                         if (data[0] & (1 << word)) {
1718                                 if (word == 6)
1719                                         instr_out(ctx, i++,
1720                                                   "TBCF\n");
1721                                 else if (word >= 7 && word <= 10) {
1722                                         instr_out(ctx, i++,
1723                                                   "TB%dC\n", word - 7);
1724                                         instr_out(ctx, i++,
1725                                                   "TB%dA\n", word - 7);
1726                                 } else if (word >= 11 && word <= 14) {
1727                                         instr_out(ctx, i,
1728                                                   "TM%dS0: offset=0x%08x, %s\n",
1729                                                   word - 11,
1730                                                   data[i] & 0xfffffffe,
1731                                                   data[i] & 1 ? "use fence" :
1732                                                   "");
1733                                         i++;
1734                                         instr_out(ctx, i,
1735                                                   "TM%dS1: height=%i, width=%i, %s\n",
1736                                                   word - 11, data[i] >> 21,
1737                                                   (data[i] >> 10) & 0x3ff,
1738                                                   data[i] & 2 ? (data[i] & 1 ?
1739                                                                  "y-tiled" :
1740                                                                  "x-tiled") :
1741                                                   "");
1742                                         i++;
1743                                         instr_out(ctx, i,
1744                                                   "TM%dS2: pitch=%i, \n",
1745                                                   word - 11,
1746                                                   ((data[i] >> 21) + 1) * 4);
1747                                         i++;
1748                                         instr_out(ctx, i++,
1749                                                   "TM%dS3\n", word - 11);
1750                                         instr_out(ctx, i++,
1751                                                   "TM%dS4: dflt color\n",
1752                                                   word - 11);
1753                                 }
1754                         }
1755                 }
1756                 if (len != i) {
1757                         fprintf(out,
1758                                 "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
1759                 }
1760                 return len;
1761         case 0x00:
1762                 instr_out(ctx, 0, "3DSTATE_MAP_STATE\n");
1763                 len = (data[0] & 0x0000003f) + 2;
1764                 instr_out(ctx, 1, "mask\n");
1766                 i = 2;
1767                 for (map = 0; map <= 15; map++) {
1768                         if (data[1] & (1 << map)) {
1769                                 int width, height, pitch, dword;
1770                                 const char *tiling;
1772                                 dword = data[i];
1773                                 instr_out(ctx, i++,
1774                                           "map %d MS2 %s%s%s\n", map,
1775                                           dword & (1 << 31) ?
1776                                           "untrusted surface, " : "",
1777                                           dword & (1 << 1) ?
1778                                           "vertical line stride enable, " : "",
1779                                           dword & (1 << 0) ?
1780                                           "vertical ofs enable, " : "");
1782                                 dword = data[i];
1783                                 width = ((dword >> 10) & ((1 << 11) - 1)) + 1;
1784                                 height = ((dword >> 21) & ((1 << 11) - 1)) + 1;
1786                                 tiling = "none";
1787                                 if (dword & (1 << 2))
1788                                         tiling = "fenced";
1789                                 else if (dword & (1 << 1))
1790                                         tiling = dword & (1 << 0) ? "Y" : "X";
1791                                 type = " BAD";
1792                                 format = "BAD";
1793                                 switch ((dword >> 7) & 0x7) {
1794                                 case 1:
1795                                         type = "8b";
1796                                         switch ((dword >> 3) & 0xf) {
1797                                         case 0:
1798                                                 format = "I";
1799                                                 break;
1800                                         case 1:
1801                                                 format = "L";
1802                                                 break;
1803                                         case 4:
1804                                                 format = "A";
1805                                                 break;
1806                                         case 5:
1807                                                 format = " mono";
1808                                                 break;
1809                                         }
1810                                         break;
1811                                 case 2:
1812                                         type = "16b";
1813                                         switch ((dword >> 3) & 0xf) {
1814                                         case 0:
1815                                                 format = " rgb565";
1816                                                 break;
1817                                         case 1:
1818                                                 format = " argb1555";
1819                                                 break;
1820                                         case 2:
1821                                                 format = " argb4444";
1822                                                 break;
1823                                         case 5:
1824                                                 format = " ay88";
1825                                                 break;
1826                                         case 6:
1827                                                 format = " bump655";
1828                                                 break;
1829                                         case 7:
1830                                                 format = "I";
1831                                                 break;
1832                                         case 8:
1833                                                 format = "L";
1834                                                 break;
1835                                         case 9:
1836                                                 format = "A";
1837                                                 break;
1838                                         }
1839                                         break;
1840                                 case 3:
1841                                         type = "32b";
1842                                         switch ((dword >> 3) & 0xf) {
1843                                         case 0:
1844                                                 format = " argb8888";
1845                                                 break;
1846                                         case 1:
1847                                                 format = " abgr8888";
1848                                                 break;
1849                                         case 2:
1850                                                 format = " xrgb8888";
1851                                                 break;
1852                                         case 3:
1853                                                 format = " xbgr8888";
1854                                                 break;
1855                                         case 4:
1856                                                 format = " qwvu8888";
1857                                                 break;
1858                                         case 5:
1859                                                 format = " axvu8888";
1860                                                 break;
1861                                         case 6:
1862                                                 format = " lxvu8888";
1863                                                 break;
1864                                         case 7:
1865                                                 format = " xlvu8888";
1866                                                 break;
1867                                         case 8:
1868                                                 format = " argb2101010";
1869                                                 break;
1870                                         case 9:
1871                                                 format = " abgr2101010";
1872                                                 break;
1873                                         case 10:
1874                                                 format = " awvu2101010";
1875                                                 break;
1876                                         case 11:
1877                                                 format = " gr1616";
1878                                                 break;
1879                                         case 12:
1880                                                 format = " vu1616";
1881                                                 break;
1882                                         case 13:
1883                                                 format = " xI824";
1884                                                 break;
1885                                         case 14:
1886                                                 format = " xA824";
1887                                                 break;
1888                                         case 15:
1889                                                 format = " xL824";
1890                                                 break;
1891                                         }
1892                                         break;
1893                                 case 5:
1894                                         type = "422";
1895                                         switch ((dword >> 3) & 0xf) {
1896                                         case 0:
1897                                                 format = " yuv_swapy";
1898                                                 break;
1899                                         case 1:
1900                                                 format = " yuv";
1901                                                 break;
1902                                         case 2:
1903                                                 format = " yuv_swapuv";
1904                                                 break;
1905                                         case 3:
1906                                                 format = " yuv_swapuvy";
1907                                                 break;
1908                                         }
1909                                         break;
1910                                 case 6:
1911                                         type = "compressed";
1912                                         switch ((dword >> 3) & 0x7) {
1913                                         case 0:
1914                                                 format = " dxt1";
1915                                                 break;
1916                                         case 1:
1917                                                 format = " dxt2_3";
1918                                                 break;
1919                                         case 2:
1920                                                 format = " dxt4_5";
1921                                                 break;
1922                                         case 3:
1923                                                 format = " fxt1";
1924                                                 break;
1925                                         case 4:
1926                                                 format = " dxt1_rb";
1927                                                 break;
1928                                         }
1929                                         break;
1930                                 case 7:
1931                                         type = "4b indexed";
1932                                         switch ((dword >> 3) & 0xf) {
1933                                         case 7:
1934                                                 format = " argb8888";
1935                                                 break;
1936                                         }
1937                                         break;
1938                                 }
1939                                 dword = data[i];
1940                                 instr_out(ctx, i++,
1941                                           "map %d MS3 [width=%d, height=%d, format=%s%s, tiling=%s%s]\n",
1942                                           map, width, height, type, format,
1943                                           tiling,
1944                                           dword & (1 << 9) ? " palette select" :
1945                                           "");
1947                                 dword = data[i];
1948                                 pitch =
1949                                     4 * (((dword >> 21) & ((1 << 11) - 1)) + 1);
1950                                 instr_out(ctx, i++,
1951                                           "map %d MS4 [pitch=%d, max_lod=%i, vol_depth=%i, cube_face_ena=%x, %s]\n",
1952                                           map, pitch, (dword >> 9) & 0x3f,
1953                                           dword & 0xff, (dword >> 15) & 0x3f,
1954                                           dword & (1 << 8) ? "miplayout legacy"
1955                                           : "miplayout right");
1956                         }
1957                 }
1958                 if (len != i) {
1959                         fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
1960                         return len;
1961                 }
1962                 return len;
1963         case 0x06:
1964                 instr_out(ctx, 0,
1965                           "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1966                 len = (data[0] & 0x000000ff) + 2;
1968                 i = 2;
1969                 for (c = 0; c <= 31; c++) {
1970                         if (data[1] & (1 << c)) {
1971                                 instr_out(ctx, i, "C%d.X = %f\n", c,
1972                                           int_as_float(data[i]));
1973                                 i++;
1974                                 instr_out(ctx, i, "C%d.Y = %f\n",
1975                                           c, int_as_float(data[i]));
1976                                 i++;
1977                                 instr_out(ctx, i, "C%d.Z = %f\n",
1978                                           c, int_as_float(data[i]));
1979                                 i++;
1980                                 instr_out(ctx, i, "C%d.W = %f\n",
1981                                           c, int_as_float(data[i]));
1982                                 i++;
1983                         }
1984                 }
1985                 if (len != i) {
1986                         fprintf(out,
1987                                 "Bad count in 3DSTATE_PIXEL_SHADER_CONSTANTS\n");
1988                 }
1989                 return len;
1990         case 0x05:
1991                 instr_out(ctx, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
1992                 len = (data[0] & 0x000000ff) + 2;
1993                 if ((len - 1) % 3 != 0 || len > 370) {
1994                         fprintf(out,
1995                                 "Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
1996                 }
1997                 i = 1;
1998                 for (instr = 0; instr < (len - 1) / 3; instr++) {
1999                         char instr_prefix[10];
2001                         sprintf(instr_prefix, "PS%03d", instr);
2002                         i915_decode_instruction(ctx, i,
2003                                                 instr_prefix);
2004                         i += 3;
2005                 }
2006                 return len;
2007         case 0x01:
2008                 if (IS_GEN2(devid))
2009                         break;
2010                 instr_out(ctx, 0, "3DSTATE_SAMPLER_STATE\n");
2011                 instr_out(ctx, 1, "mask\n");
2012                 len = (data[0] & 0x0000003f) + 2;
2013                 i = 2;
2014                 for (sampler = 0; sampler <= 15; sampler++) {
2015                         if (data[1] & (1 << sampler)) {
2016                                 uint32_t dword;
2017                                 const char *mip_filter = "";
2019                                 dword = data[i];
2020                                 switch ((dword >> 20) & 0x3) {
2021                                 case 0:
2022                                         mip_filter = "none";
2023                                         break;
2024                                 case 1:
2025                                         mip_filter = "nearest";
2026                                         break;
2027                                 case 3:
2028                                         mip_filter = "linear";
2029                                         break;
2030                                 }
2031                                 instr_out(ctx, i++,
2032                                           "sampler %d SS2:%s%s%s "
2033                                           "base_mip_level=%i, mip_filter=%s, mag_filter=%s, min_filter=%s "
2034                                           "lod_bias=%.2f,%s max_aniso=%i, shadow_func=%s\n",
2035                                           sampler,
2036                                           dword & (1 << 31) ? " reverse gamma,"
2037                                           : "",
2038                                           dword & (1 << 30) ? " packed2planar,"
2039                                           : "",
2040                                           dword & (1 << 29) ?
2041                                           " colorspace conversion," : "",
2042                                           (dword >> 22) & 0x1f, mip_filter,
2043                                           decode_sample_filter(dword >> 17),
2044                                           decode_sample_filter(dword >> 14),
2045                                           ((dword >> 5) & 0x1ff) / (0x10 * 1.0),
2046                                           dword & (1 << 4) ? " shadow," : "",
2047                                           dword & (1 << 3) ? 4 : 2,
2048                                           decode_compare_func(dword));
2049                                 dword = data[i];
2050                                 instr_out(ctx, i++,
2051                                           "sampler %d SS3: min_lod=%.2f,%s "
2052                                           "tcmode_x=%s, tcmode_y=%s, tcmode_z=%s,%s texmap_idx=%i,%s\n",
2053                                           sampler,
2054                                           ((dword >> 24) & 0xff) / (0x10 * 1.0),
2055                                           dword & (1 << 17) ?
2056                                           " kill pixel enable," : "",
2057                                           decode_tex_coord_mode(dword >> 12),
2058                                           decode_tex_coord_mode(dword >> 9),
2059                                           decode_tex_coord_mode(dword >> 6),
2060                                           dword & (1 << 5) ?
2061                                           " normalized coords," : "",
2062                                           (dword >> 1) & 0xf,
2063                                           dword & (1 << 0) ? " deinterlacer," :
2064                                           "");
2065                                 dword = data[i];
2066                                 instr_out(ctx, i++,
2067                                           "sampler %d SS4: border color\n",
2068                                           sampler);
2069                         }
2070                 }
2071                 if (len != i) {
2072                         fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
2073                 }
2074                 return len;
2075         case 0x85:
2076                 len = (data[0] & 0x0000000f) + 2;
2078                 if (len != 2)
2079                         fprintf(out,
2080                                 "Bad count in 3DSTATE_DEST_BUFFER_VARIABLES\n");
2082                 instr_out(ctx, 0,
2083                           "3DSTATE_DEST_BUFFER_VARIABLES\n");
2085                 switch ((data[1] >> 8) & 0xf) {
2086                 case 0x0:
2087                         format = "g8";
2088                         break;
2089                 case 0x1:
2090                         format = "x1r5g5b5";
2091                         break;
2092                 case 0x2:
2093                         format = "r5g6b5";
2094                         break;
2095                 case 0x3:
2096                         format = "a8r8g8b8";
2097                         break;
2098                 case 0x4:
2099                         format = "ycrcb_swapy";
2100                         break;
2101                 case 0x5:
2102                         format = "ycrcb_normal";
2103                         break;
2104                 case 0x6:
2105                         format = "ycrcb_swapuv";
2106                         break;
2107                 case 0x7:
2108                         format = "ycrcb_swapuvy";
2109                         break;
2110                 case 0x8:
2111                         format = "a4r4g4b4";
2112                         break;
2113                 case 0x9:
2114                         format = "a1r5g5b5";
2115                         break;
2116                 case 0xa:
2117                         format = "a2r10g10b10";
2118                         break;
2119                 default:
2120                         format = "BAD";
2121                         break;
2122                 }
2123                 switch ((data[1] >> 2) & 0x3) {
2124                 case 0x0:
2125                         zformat = "u16";
2126                         break;
2127                 case 0x1:
2128                         zformat = "f16";
2129                         break;
2130                 case 0x2:
2131                         zformat = "u24x8";
2132                         break;
2133                 default:
2134                         zformat = "BAD";
2135                         break;
2136                 }
2137                 instr_out(ctx, 1,
2138                           "%s format, %s depth format, early Z %sabled\n",
2139                           format, zformat,
2140                           (data[1] & (1 << 31)) ? "en" : "dis");
2141                 return len;
2143         case 0x8e:
2144                 {
2145                         const char *name, *tiling;
2147                         len = (data[0] & 0x0000000f) + 2;
2148                         if (len != 3)
2149                                 fprintf(out,
2150                                         "Bad count in 3DSTATE_BUFFER_INFO\n");
2152                         switch ((data[1] >> 24) & 0x7) {
2153                         case 0x3:
2154                                 name = "color";
2155                                 break;
2156                         case 0x7:
2157                                 name = "depth";
2158                                 break;
2159                         default:
2160                                 name = "unknown";
2161                                 break;
2162                         }
2164                         tiling = "none";
2165                         if (data[1] & (1 << 23))
2166                                 tiling = "fenced";
2167                         else if (data[1] & (1 << 22))
2168                                 tiling = data[1] & (1 << 21) ? "Y" : "X";
2170                         instr_out(ctx, 0, "3DSTATE_BUFFER_INFO\n");
2171                         instr_out(ctx, 1,
2172                                   "%s, tiling = %s, pitch=%d\n", name, tiling,
2173                                   data[1] & 0xffff);
2175                         instr_out(ctx, 2, "address\n");
2176                         return len;
2177                 }
2178         case 0x81:
2179                 len = (data[0] & 0x0000000f) + 2;
2181                 if (len != 3)
2182                         fprintf(out,
2183                                 "Bad count in 3DSTATE_SCISSOR_RECTANGLE\n");
2185                 instr_out(ctx, 0, "3DSTATE_SCISSOR_RECTANGLE\n");
2186                 instr_out(ctx, 1, "(%d,%d)\n",
2187                           data[1] & 0xffff, data[1] >> 16);
2188                 instr_out(ctx, 2, "(%d,%d)\n",
2189                           data[2] & 0xffff, data[2] >> 16);
2191                 return len;
2192         case 0x80:
2193                 len = (data[0] & 0x0000000f) + 2;
2195                 if (len != 5)
2196                         fprintf(out,
2197                                 "Bad count in 3DSTATE_DRAWING_RECTANGLE\n");
2199                 instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
2200                 instr_out(ctx, 1, "%s\n",
2201                           data[1] & (1 << 30) ? "depth ofs disabled " : "");
2202                 instr_out(ctx, 2, "(%d,%d)\n",
2203                           data[2] & 0xffff, data[2] >> 16);
2204                 instr_out(ctx, 3, "(%d,%d)\n",
2205                           data[3] & 0xffff, data[3] >> 16);
2206                 instr_out(ctx, 4, "(%d,%d)\n",
2207                           data[4] & 0xffff, data[4] >> 16);
2209                 return len;
2210         case 0x9c:
2211                 len = (data[0] & 0x0000000f) + 2;
2213                 if (len != 7)
2214                         fprintf(out, "Bad count in 3DSTATE_CLEAR_PARAMETERS\n");
2216                 instr_out(ctx, 0, "3DSTATE_CLEAR_PARAMETERS\n");
2217                 instr_out(ctx, 1, "prim_type=%s, clear=%s%s%s\n",
2218                           data[1] & (1 << 16) ? "CLEAR_RECT" : "ZONE_INIT",
2219                           data[1] & (1 << 2) ? "color," : "",
2220                           data[1] & (1 << 1) ? "depth," : "",
2221                           data[1] & (1 << 0) ? "stencil," : "");
2222                 instr_out(ctx, 2, "clear color\n");
2223                 instr_out(ctx, 3, "clear depth/stencil\n");
2224                 instr_out(ctx, 4, "color value (rgba8888)\n");
2225                 instr_out(ctx, 5, "depth value %f\n",
2226                           int_as_float(data[5]));
2227                 instr_out(ctx, 6, "clear stencil\n");
2228                 return len;
2229         }
2231         for (idx = 0; idx < ARRAY_SIZE(opcodes_3d_1d); idx++) {
2232                 opcode_3d_1d = &opcodes_3d_1d[idx];
2233                 if (opcode_3d_1d->i830_only && !IS_GEN2(devid))
2234                         continue;
2236                 if (((data[0] & 0x00ff0000) >> 16) == opcode_3d_1d->opcode) {
2237                         len = 1;
2239                         instr_out(ctx, 0, "%s\n",
2240                                   opcode_3d_1d->name);
2241                         if (opcode_3d_1d->max_len > 1) {
2242                                 len = (data[0] & 0x0000ffff) + 2;
2243                                 if (len < opcode_3d_1d->min_len ||
2244                                     len > opcode_3d_1d->max_len) {
2245                                         fprintf(out, "Bad count in %s\n",
2246                                                 opcode_3d_1d->name);
2247                                 }
2248                         }
2250                         for (i = 1; i < len; i++) {
2251                                 instr_out(ctx, i, "dword %d\n", i);
2252                         }
2254                         return len;
2255                 }
2256         }
2258         instr_out(ctx, 0, "3D UNKNOWN: 3d_1d opcode = 0x%x\n",
2259                   opcode);
2260         return 1;
2263 static int
2264 decode_3d_primitive(struct drm_intel_decode *ctx)
2266         uint32_t *data = ctx->data;
2267         uint32_t count = ctx->count;
2268         char immediate = (data[0] & (1 << 23)) == 0;
2269         unsigned int len, i, j, ret;
2270         const char *primtype;
2271         int original_s2 = saved_s2;
2272         int original_s4 = saved_s4;
2274         switch ((data[0] >> 18) & 0xf) {
2275         case 0x0:
2276                 primtype = "TRILIST";
2277                 break;
2278         case 0x1:
2279                 primtype = "TRISTRIP";
2280                 break;
2281         case 0x2:
2282                 primtype = "TRISTRIP_REVERSE";
2283                 break;
2284         case 0x3:
2285                 primtype = "TRIFAN";
2286                 break;
2287         case 0x4:
2288                 primtype = "POLYGON";
2289                 break;
2290         case 0x5:
2291                 primtype = "LINELIST";
2292                 break;
2293         case 0x6:
2294                 primtype = "LINESTRIP";
2295                 break;
2296         case 0x7:
2297                 primtype = "RECTLIST";
2298                 break;
2299         case 0x8:
2300                 primtype = "POINTLIST";
2301                 break;
2302         case 0x9:
2303                 primtype = "DIB";
2304                 break;
2305         case 0xa:
2306                 primtype = "CLEAR_RECT";
2307                 saved_s4 = 3 << 6;
2308                 saved_s2 = ~0;
2309                 break;
2310         default:
2311                 primtype = "unknown";
2312                 break;
2313         }
2315         /* XXX: 3DPRIM_DIB not supported */
2316         if (immediate) {
2317                 len = (data[0] & 0x0003ffff) + 2;
2318                 instr_out(ctx, 0, "3DPRIMITIVE inline %s\n",
2319                           primtype);
2320                 if (count < len)
2321                         BUFFER_FAIL(count, len, "3DPRIMITIVE inline");
2322                 if (!saved_s2_set || !saved_s4_set) {
2323                         fprintf(out, "unknown vertex format\n");
2324                         for (i = 1; i < len; i++) {
2325                                 instr_out(ctx, i,
2326                                           "           vertex data (%f float)\n",
2327                                           int_as_float(data[i]));
2328                         }
2329                 } else {
2330                         unsigned int vertex = 0;
2331                         for (i = 1; i < len;) {
2332                                 unsigned int tc;
2334 #define VERTEX_OUT(fmt, ...) do {                                       \
2335     if (i < len)                                                        \
2336         instr_out(ctx, i, " V%d."fmt"\n", vertex, __VA_ARGS__); \
2337     else                                                                \
2338         fprintf(out, " missing data in V%d\n", vertex);                 \
2339     i++;                                                                \
2340 } while (0)
2342                                 VERTEX_OUT("X = %f", int_as_float(data[i]));
2343                                 VERTEX_OUT("Y = %f", int_as_float(data[i]));
2344                                 switch (saved_s4 >> 6 & 0x7) {
2345                                 case 0x1:
2346                                         VERTEX_OUT("Z = %f",
2347                                                    int_as_float(data[i]));
2348                                         break;
2349                                 case 0x2:
2350                                         VERTEX_OUT("Z = %f",
2351                                                    int_as_float(data[i]));
2352                                         VERTEX_OUT("W = %f",
2353                                                    int_as_float(data[i]));
2354                                         break;
2355                                 case 0x3:
2356                                         break;
2357                                 case 0x4:
2358                                         VERTEX_OUT("W = %f",
2359                                                    int_as_float(data[i]));
2360                                         break;
2361                                 default:
2362                                         fprintf(out, "bad S4 position mask\n");
2363                                 }
2365                                 if (saved_s4 & (1 << 10)) {
2366                                         VERTEX_OUT
2367                                             ("color = (A=0x%02x, R=0x%02x, G=0x%02x, "
2368                                              "B=0x%02x)", data[i] >> 24,
2369                                              (data[i] >> 16) & 0xff,
2370                                              (data[i] >> 8) & 0xff,
2371                                              data[i] & 0xff);
2372                                 }
2373                                 if (saved_s4 & (1 << 11)) {
2374                                         VERTEX_OUT
2375                                             ("spec = (A=0x%02x, R=0x%02x, G=0x%02x, "
2376                                              "B=0x%02x)", data[i] >> 24,
2377                                              (data[i] >> 16) & 0xff,
2378                                              (data[i] >> 8) & 0xff,
2379                                              data[i] & 0xff);
2380                                 }
2381                                 if (saved_s4 & (1 << 12))
2382                                         VERTEX_OUT("width = 0x%08x)", data[i]);
2384                                 for (tc = 0; tc <= 7; tc++) {
2385                                         switch ((saved_s2 >> (tc * 4)) & 0xf) {
2386                                         case 0x0:
2387                                                 VERTEX_OUT("T%d.X = %f", tc,
2388                                                            int_as_float(data
2389                                                                         [i]));
2390                                                 VERTEX_OUT("T%d.Y = %f", tc,
2391                                                            int_as_float(data
2392                                                                         [i]));
2393                                                 break;
2394                                         case 0x1:
2395                                                 VERTEX_OUT("T%d.X = %f", tc,
2396                                                            int_as_float(data
2397                                                                         [i]));
2398                                                 VERTEX_OUT("T%d.Y = %f", tc,
2399                                                            int_as_float(data
2400                                                                         [i]));
2401                                                 VERTEX_OUT("T%d.Z = %f", tc,
2402                                                            int_as_float(data
2403                                                                         [i]));
2404                                                 break;
2405                                         case 0x2:
2406                                                 VERTEX_OUT("T%d.X = %f", tc,
2407                                                            int_as_float(data
2408                                                                         [i]));
2409                                                 VERTEX_OUT("T%d.Y = %f", tc,
2410                                                            int_as_float(data
2411                                                                         [i]));
2412                                                 VERTEX_OUT("T%d.Z = %f", tc,
2413                                                            int_as_float(data
2414                                                                         [i]));
2415                                                 VERTEX_OUT("T%d.W = %f", tc,
2416                                                            int_as_float(data
2417                                                                         [i]));
2418                                                 break;
2419                                         case 0x3:
2420                                                 VERTEX_OUT("T%d.X = %f", tc,
2421                                                            int_as_float(data
2422                                                                         [i]));
2423                                                 break;
2424                                         case 0x4:
2425                                                 VERTEX_OUT
2426                                                     ("T%d.XY = 0x%08x half-float",
2427                                                      tc, data[i]);
2428                                                 break;
2429                                         case 0x5:
2430                                                 VERTEX_OUT
2431                                                     ("T%d.XY = 0x%08x half-float",
2432                                                      tc, data[i]);
2433                                                 VERTEX_OUT
2434                                                     ("T%d.ZW = 0x%08x half-float",
2435                                                      tc, data[i]);
2436                                                 break;
2437                                         case 0xf:
2438                                                 break;
2439                                         default:
2440                                                 fprintf(out,
2441                                                         "bad S2.T%d format\n",
2442                                                         tc);
2443                                         }
2444                                 }
2445                                 vertex++;
2446                         }
2447                 }
2449                 ret = len;
2450         } else {
2451                 /* indirect vertices */
2452                 len = data[0] & 0x0000ffff;     /* index count */
2453                 if (data[0] & (1 << 17)) {
2454                         /* random vertex access */
2455                         if (count < (len + 1) / 2 + 1) {
2456                                 BUFFER_FAIL(count, (len + 1) / 2 + 1,
2457                                             "3DPRIMITIVE random indirect");
2458                         }
2459                         instr_out(ctx, 0,
2460                                   "3DPRIMITIVE random indirect %s (%d)\n",
2461                                   primtype, len);
2462                         if (len == 0) {
2463                                 /* vertex indices continue until 0xffff is
2464                                  * found
2465                                  */
2466                                 for (i = 1; i < count; i++) {
2467                                         if ((data[i] & 0xffff) == 0xffff) {
2468                                                 instr_out(ctx, i,
2469                                                           "    indices: (terminator)\n");
2470                                                 ret = i;
2471                                                 goto out;
2472                                         } else if ((data[i] >> 16) == 0xffff) {
2473                                                 instr_out(ctx, i,
2474                                                           "    indices: 0x%04x, (terminator)\n",
2475                                                           data[i] & 0xffff);
2476                                                 ret = i;
2477                                                 goto out;
2478                                         } else {
2479                                                 instr_out(ctx, i,
2480                                                           "    indices: 0x%04x, 0x%04x\n",
2481                                                           data[i] & 0xffff,
2482                                                           data[i] >> 16);
2483                                         }
2484                                 }
2485                                 fprintf(out,
2486                                         "3DPRIMITIVE: no terminator found in index buffer\n");
2487                                 ret = count;
2488                                 goto out;
2489                         } else {
2490                                 /* fixed size vertex index buffer */
2491                                 for (j = 1, i = 0; i < len; i += 2, j++) {
2492                                         if (i * 2 == len - 1) {
2493                                                 instr_out(ctx, j,
2494                                                           "    indices: 0x%04x\n",
2495                                                           data[j] & 0xffff);
2496                                         } else {
2497                                                 instr_out(ctx, j,
2498                                                           "    indices: 0x%04x, 0x%04x\n",
2499                                                           data[j] & 0xffff,
2500                                                           data[j] >> 16);
2501                                         }
2502                                 }
2503                         }
2504                         ret = (len + 1) / 2 + 1;
2505                         goto out;
2506                 } else {
2507                         /* sequential vertex access */
2508                         instr_out(ctx, 0,
2509                                   "3DPRIMITIVE sequential indirect %s, %d starting from "
2510                                   "%d\n", primtype, len, data[1] & 0xffff);
2511                         instr_out(ctx, 1, "           start\n");
2512                         ret = 2;
2513                         goto out;
2514                 }
2515         }
2517 out:
2518         saved_s2 = original_s2;
2519         saved_s4 = original_s4;
2520         return ret;
2523 static int
2524 decode_3d(struct drm_intel_decode *ctx)
2526         uint32_t opcode;
2527         unsigned int idx;
2528         uint32_t *data = ctx->data;
2530         struct {
2531                 uint32_t opcode;
2532                 unsigned int min_len;
2533                 unsigned int max_len;
2534                 const char *name;
2535         } opcodes_3d[] = {
2536                 { 0x06, 1, 1, "3DSTATE_ANTI_ALIASING" },
2537                 { 0x08, 1, 1, "3DSTATE_BACKFACE_STENCIL_OPS" },
2538                 { 0x09, 1, 1, "3DSTATE_BACKFACE_STENCIL_MASKS" },
2539                 { 0x16, 1, 1, "3DSTATE_COORD_SET_BINDINGS" },
2540                 { 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
2541                 { 0x0b, 1, 1, "3DSTATE_INDEPENDENT_ALPHA_BLEND" },
2542                 { 0x0d, 1, 1, "3DSTATE_MODES_4" },
2543                 { 0x0c, 1, 1, "3DSTATE_MODES_5" },
2544                 { 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES"},
2545         }, *opcode_3d;
2547         opcode = (data[0] & 0x1f000000) >> 24;
2549         switch (opcode) {
2550         case 0x1f:
2551                 return decode_3d_primitive(ctx);
2552         case 0x1d:
2553     &nb