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