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