Back to index

libdrm  2.4.37
intel_decode.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2009-2011 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  */
00023 
00024 #include <assert.h>
00025 #include <stdint.h>
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <stdbool.h>
00029 #include <stdarg.h>
00030 #include <string.h>
00031 
00032 #include "intel_chipset.h"
00033 #include "intel_bufmgr.h"
00034 
00035 /* Struct for tracking drm_intel_decode state. */
00036 struct drm_intel_decode {
00038        FILE *out;
00039 
00041        uint32_t devid;
00042 
00047        int gen;
00048 
00050        uint32_t hw_offset;
00052        uint32_t *data;
00054        uint32_t count;
00055 
00057        uint32_t base_hw_offset;
00059        uint32_t *base_data;
00061        uint32_t base_count;
00062 
00066        uint32_t head, tail;
00075        bool dump_past_end;
00076 
00077        bool overflowed;
00078 };
00079 
00080 static FILE *out;
00081 static uint32_t saved_s2 = 0, saved_s4 = 0;
00082 static char saved_s2_set = 0, saved_s4_set = 0;
00083 static uint32_t head_offset = 0xffffffff; /* undefined */
00084 static uint32_t tail_offset = 0xffffffff; /* undefined */
00085 
00086 #ifndef ARRAY_SIZE
00087 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
00088 #endif
00089 
00090 #define BUFFER_FAIL(_count, _len, _name) do {                  \
00091     fprintf(out, "Buffer size too small in %s (%d < %d)\n",    \
00092            (_name), (_count), (_len));                         \
00093     return _count;                                      \
00094 } while (0)
00095 
00096 static float int_as_float(uint32_t intval)
00097 {
00098        union intfloat {
00099               uint32_t i;
00100               float f;
00101        } uval;
00102 
00103        uval.i = intval;
00104        return uval.f;
00105 }
00106 
00107 static void
00108 instr_out(struct drm_intel_decode *ctx, unsigned int index,
00109          const char *fmt, ...) __attribute__((format(__printf__, 3, 4)));
00110 
00111 static void
00112 instr_out(struct drm_intel_decode *ctx, unsigned int index,
00113          const char *fmt, ...)
00114 {
00115        va_list va;
00116        const char *parseinfo;
00117        uint32_t offset = ctx->hw_offset + index * 4;
00118 
00119        if (index > ctx->count) {
00120               if (!ctx->overflowed) {
00121                      fprintf(out, "ERROR: Decode attempted to continue beyond end of batchbuffer\n");
00122                      ctx->overflowed = true;
00123               }
00124               return;
00125        }
00126 
00127        if (offset == head_offset)
00128               parseinfo = "HEAD";
00129        else if (offset == tail_offset)
00130               parseinfo = "TAIL";
00131        else
00132               parseinfo = "    ";
00133 
00134        fprintf(out, "0x%08x: %s 0x%08x: %s", offset, parseinfo,
00135               ctx->data[index], index == 0 ? "" : "   ");
00136        va_start(va, fmt);
00137        vfprintf(out, fmt, va);
00138        va_end(va);
00139 }
00140 
00141 static int
00142 decode_MI_SET_CONTEXT(struct drm_intel_decode *ctx)
00143 {
00144        uint32_t data = ctx->data[1];
00145        if (ctx->gen > 7)
00146               return 1;
00147 
00148        instr_out(ctx, 0, "MI_SET_CONTEXT\n");
00149        instr_out(ctx, 1, "gtt offset = 0x%x%s%s\n",
00150                 data & ~0xfff,
00151                 data & (1<<1)? ", Force Restore": "",
00152                 data & (1<<0)? ", Restore Inhibit": "");
00153 
00154        return 2;
00155 }
00156 
00157 static int
00158 decode_MI_WAIT_FOR_EVENT(struct drm_intel_decode *ctx)
00159 {
00160        const char *cc_wait;
00161        int cc_shift = 0;
00162        uint32_t data = ctx->data[0];
00163 
00164        if (ctx->gen <= 5)
00165               cc_shift = 9;
00166        else
00167               cc_shift = 16;
00168 
00169        switch ((data >> cc_shift) & 0x1f) {
00170        case 1:
00171               cc_wait = ", cc wait 1";
00172               break;
00173        case 2:
00174               cc_wait = ", cc wait 2";
00175               break;
00176        case 3:
00177               cc_wait = ", cc wait 3";
00178               break;
00179        case 4:
00180               cc_wait = ", cc wait 4";
00181               break;
00182        case 5:
00183               cc_wait = ", cc wait 4";
00184               break;
00185        default:
00186               cc_wait = "";
00187               break;
00188        }
00189 
00190        if (ctx->gen <= 5) {
00191               instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
00192                        data & (1<<18)? ", pipe B start vblank wait": "",
00193                        data & (1<<17)? ", pipe A start vblank wait": "",
00194                        data & (1<<16)? ", overlay flip pending wait": "",
00195                        data & (1<<14)? ", pipe B hblank wait": "",
00196                        data & (1<<13)? ", pipe A hblank wait": "",
00197                        cc_wait,
00198                        data & (1<<8)? ", plane C pending flip wait": "",
00199                        data & (1<<7)? ", pipe B vblank wait": "",
00200                        data & (1<<6)? ", plane B pending flip wait": "",
00201                        data & (1<<5)? ", pipe B scan line wait": "",
00202                        data & (1<<4)? ", fbc idle wait": "",
00203                        data & (1<<3)? ", pipe A vblank wait": "",
00204                        data & (1<<2)? ", plane A pending flip wait": "",
00205                        data & (1<<1)? ", plane A scan line wait": "");
00206        } else {
00207               instr_out(ctx, 0, "MI_WAIT_FOR_EVENT%s%s%s%s%s%s%s%s%s%s%s%s\n",
00208                        data & (1<<20)? ", sprite C pending flip wait": "", /* ivb */
00209                        cc_wait,
00210                        data & (1<<13)? ", pipe B hblank wait": "",
00211                        data & (1<<11)? ", pipe B vblank wait": "",
00212                        data & (1<<10)? ", sprite B pending flip wait": "",
00213                        data & (1<<9)? ", plane B pending flip wait": "",
00214                        data & (1<<8)? ", plane B scan line wait": "",
00215                        data & (1<<5)? ", pipe A hblank wait": "",
00216                        data & (1<<3)? ", pipe A vblank wait": "",
00217                        data & (1<<2)? ", sprite A pending flip wait": "",
00218                        data & (1<<1)? ", plane A pending flip wait": "",
00219                        data & (1<<0)? ", plane A scan line wait": "");
00220        }
00221 
00222        return 1;
00223 }
00224 
00225 static int
00226 decode_mi(struct drm_intel_decode *ctx)
00227 {
00228        unsigned int opcode, len = -1;
00229        const char *post_sync_op = "";
00230        uint32_t *data = ctx->data;
00231 
00232        struct {
00233               uint32_t opcode;
00234               int len_mask;
00235               unsigned int min_len;
00236               unsigned int max_len;
00237               const char *name;
00238               int (*func)(struct drm_intel_decode *ctx);
00239        } opcodes_mi[] = {
00240               { 0x08, 0, 1, 1, "MI_ARB_ON_OFF" },
00241               { 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
00242               { 0x30, 0x3f, 3, 3, "MI_BATCH_BUFFER" },
00243               { 0x31, 0x3f, 2, 2, "MI_BATCH_BUFFER_START" },
00244               { 0x14, 0x3f, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
00245               { 0x04, 0, 1, 1, "MI_FLUSH" },
00246               { 0x22, 0x1f, 3, 3, "MI_LOAD_REGISTER_IMM" },
00247               { 0x13, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
00248               { 0x12, 0x3f, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
00249               { 0x00, 0, 1, 1, "MI_NOOP" },
00250               { 0x11, 0x3f, 2, 2, "MI_OVERLAY_FLIP" },
00251               { 0x07, 0, 1, 1, "MI_REPORT_HEAD" },
00252               { 0x18, 0x3f, 2, 2, "MI_SET_CONTEXT", decode_MI_SET_CONTEXT },
00253               { 0x20, 0x3f, 3, 4, "MI_STORE_DATA_IMM" },
00254               { 0x21, 0x3f, 3, 4, "MI_STORE_DATA_INDEX" },
00255               { 0x24, 0x3f, 3, 3, "MI_STORE_REGISTER_MEM" },
00256               { 0x02, 0, 1, 1, "MI_USER_INTERRUPT" },
00257               { 0x03, 0, 1, 1, "MI_WAIT_FOR_EVENT", decode_MI_WAIT_FOR_EVENT },
00258               { 0x16, 0x7f, 3, 3, "MI_SEMAPHORE_MBOX" },
00259               { 0x26, 0x1f, 3, 4, "MI_FLUSH_DW" },
00260               { 0x0b, 0, 1, 1, "MI_SUSPEND_FLUSH"},
00261        }, *opcode_mi = NULL;
00262 
00263        /* check instruction length */
00264        for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
00265             opcode++) {
00266               if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
00267                      len = 1;
00268                      if (opcodes_mi[opcode].max_len > 1) {
00269                             len =
00270                                 (data[0] & opcodes_mi[opcode].len_mask) + 2;
00271                             if (len < opcodes_mi[opcode].min_len
00272                                 || len > opcodes_mi[opcode].max_len) {
00273                                    fprintf(out,
00274                                           "Bad length (%d) in %s, [%d, %d]\n",
00275                                           len, opcodes_mi[opcode].name,
00276                                           opcodes_mi[opcode].min_len,
00277                                           opcodes_mi[opcode].max_len);
00278                             }
00279                      }
00280                      opcode_mi = &opcodes_mi[opcode];
00281                      break;
00282               }
00283        }
00284 
00285        if (opcode_mi && opcode_mi->func)
00286               return opcode_mi->func(ctx);
00287 
00288        switch ((data[0] & 0x1f800000) >> 23) {
00289        case 0x0a:
00290               instr_out(ctx, 0, "MI_BATCH_BUFFER_END\n");
00291               return -1;
00292        case 0x16:
00293               instr_out(ctx, 0, "MI_SEMAPHORE_MBOX%s%s%s%s %u\n",
00294                        data[0] & (1 << 22) ? " global gtt," : "",
00295                        data[0] & (1 << 21) ? " update semaphore," : "",
00296                        data[0] & (1 << 20) ? " compare semaphore," : "",
00297                        data[0] & (1 << 18) ? " use compare reg" : "",
00298                        (data[0] & (0x3 << 16)) >> 16);
00299               instr_out(ctx, 1, "value\n");
00300               instr_out(ctx, 2, "address\n");
00301               return len;
00302        case 0x21:
00303               instr_out(ctx, 0, "MI_STORE_DATA_INDEX%s\n",
00304                        data[0] & (1 << 21) ? " use per-process HWS," : "");
00305               instr_out(ctx, 1, "index\n");
00306               instr_out(ctx, 2, "dword\n");
00307               if (len == 4)
00308                      instr_out(ctx, 3, "upper dword\n");
00309               return len;
00310        case 0x00:
00311               if (data[0] & (1 << 22))
00312                      instr_out(ctx, 0,
00313                               "MI_NOOP write NOPID reg, val=0x%x\n",
00314                               data[0] & ((1 << 22) - 1));
00315               else
00316                      instr_out(ctx, 0, "MI_NOOP\n");
00317               return len;
00318        case 0x26:
00319               switch (data[0] & (0x3 << 14)) {
00320               case (0 << 14):
00321                      post_sync_op = "no write";
00322                      break;
00323               case (1 << 14):
00324                      post_sync_op = "write data";
00325                      break;
00326               case (2 << 14):
00327                      post_sync_op = "reserved";
00328                      break;
00329               case (3 << 14):
00330                      post_sync_op = "write TIMESTAMP";
00331                      break;
00332               }
00333               instr_out(ctx, 0,
00334                        "MI_FLUSH_DW%s%s%s%s post_sync_op='%s' %s%s\n",
00335                        data[0] & (1 << 22) ?
00336                        " enable protected mem (BCS-only)," : "",
00337                        data[0] & (1 << 21) ? " store in hws," : "",
00338                        data[0] & (1 << 18) ? " invalidate tlb," : "",
00339                        data[0] & (1 << 17) ? " flush gfdt," : "",
00340                        post_sync_op,
00341                        data[0] & (1 << 8) ? " enable notify interrupt," : "",
00342                        data[0] & (1 << 7) ?
00343                        " invalidate video state (BCS-only)," : "");
00344               if (data[0] & (1 << 21))
00345                      instr_out(ctx, 1, "hws index\n");
00346               else
00347                      instr_out(ctx, 1, "address\n");
00348               instr_out(ctx, 2, "dword\n");
00349               if (len == 4)
00350                      instr_out(ctx, 3, "upper dword\n");
00351               return len;
00352        }
00353 
00354        for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
00355             opcode++) {
00356               if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
00357                      unsigned int i;
00358 
00359                      instr_out(ctx, 0, "%s\n",
00360                               opcodes_mi[opcode].name);
00361                      for (i = 1; i < len; i++) {
00362                             instr_out(ctx, i, "dword %d\n", i);
00363                      }
00364 
00365                      return len;
00366               }
00367        }
00368 
00369        instr_out(ctx, 0, "MI UNKNOWN\n");
00370        return 1;
00371 }
00372 
00373 static void
00374 decode_2d_br00(struct drm_intel_decode *ctx, const char *cmd)
00375 {
00376        instr_out(ctx, 0,
00377                 "%s (rgb %sabled, alpha %sabled, src tile %d, dst tile %d)\n",
00378                 cmd,
00379                 (ctx->data[0] & (1 << 20)) ? "en" : "dis",
00380                 (ctx->data[0] & (1 << 21)) ? "en" : "dis",
00381                 (ctx->data[0] >> 15) & 1,
00382                 (ctx->data[0] >> 11) & 1);
00383 }
00384 
00385 static void
00386 decode_2d_br01(struct drm_intel_decode *ctx)
00387 {
00388        const char *format;
00389        switch ((ctx->data[1] >> 24) & 0x3) {
00390        case 0:
00391               format = "8";
00392               break;
00393        case 1:
00394               format = "565";
00395               break;
00396        case 2:
00397               format = "1555";
00398               break;
00399        case 3:
00400               format = "8888";
00401               break;
00402        }
00403 
00404        instr_out(ctx, 1,
00405                 "format %s, pitch %d, rop 0x%02x, "
00406                 "clipping %sabled, %s%s \n",
00407                 format,
00408                 (short)(ctx->data[1] & 0xffff),
00409                 (ctx->data[1] >> 16) & 0xff,
00410                 ctx->data[1] & (1 << 30) ? "en" : "dis",
00411                 ctx->data[1] & (1 << 31) ? "solid pattern enabled, " : "",
00412                 ctx->data[1] & (1 << 31) ?
00413                 "mono pattern transparency enabled, " : "");
00414 
00415 }
00416 
00417 static int
00418 decode_2d(struct drm_intel_decode *ctx)
00419 {
00420        unsigned int opcode, len;
00421        uint32_t *data = ctx->data;
00422 
00423        struct {
00424               uint32_t opcode;
00425               unsigned int min_len;
00426               unsigned int max_len;
00427               const char *name;
00428        } opcodes_2d[] = {
00429               { 0x40, 5, 5, "COLOR_BLT" },
00430               { 0x43, 6, 6, "SRC_COPY_BLT" },
00431               { 0x01, 8, 8, "XY_SETUP_BLT" },
00432               { 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
00433               { 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
00434               { 0x24, 2, 2, "XY_PIXEL_BLT" },
00435               { 0x25, 3, 3, "XY_SCANLINES_BLT" },
00436               { 0x26, 4, 4, "Y_TEXT_BLT" },
00437               { 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
00438               { 0x50, 6, 6, "XY_COLOR_BLT" },
00439               { 0x51, 6, 6, "XY_PAT_BLT" },
00440               { 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
00441               { 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
00442               { 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
00443               { 0x52, 9, 9, "XY_MONO_PAT_BLT" },
00444               { 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
00445               { 0x53, 8, 8, "XY_SRC_COPY_BLT" },
00446               { 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
00447               { 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
00448               { 0x55, 9, 9, "XY_FULL_BLT" },
00449               { 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
00450               { 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
00451               { 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
00452               { 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
00453               { 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT"},
00454        };
00455 
00456        switch ((data[0] & 0x1fc00000) >> 22) {
00457        case 0x25:
00458               instr_out(ctx, 0,
00459                        "XY_SCANLINES_BLT (pattern seed (%d, %d), dst tile %d)\n",
00460                        (data[0] >> 12) & 0x8,
00461                        (data[0] >> 8) & 0x8, (data[0] >> 11) & 1);
00462 
00463               len = (data[0] & 0x000000ff) + 2;
00464               if (len != 3)
00465                      fprintf(out, "Bad count in XY_SCANLINES_BLT\n");
00466 
00467               instr_out(ctx, 1, "dest (%d,%d)\n",
00468                        data[1] & 0xffff, data[1] >> 16);
00469               instr_out(ctx, 2, "dest (%d,%d)\n",
00470                        data[2] & 0xffff, data[2] >> 16);
00471               return len;
00472        case 0x01:
00473               decode_2d_br00(ctx, "XY_SETUP_BLT");
00474 
00475               len = (data[0] & 0x000000ff) + 2;
00476               if (len != 8)
00477                      fprintf(out, "Bad count in XY_SETUP_BLT\n");
00478 
00479               decode_2d_br01(ctx);
00480               instr_out(ctx, 2, "cliprect (%d,%d)\n",
00481                        data[2] & 0xffff, data[2] >> 16);
00482               instr_out(ctx, 3, "cliprect (%d,%d)\n",
00483                        data[3] & 0xffff, data[3] >> 16);
00484               instr_out(ctx, 4, "setup dst offset 0x%08x\n",
00485                        data[4]);
00486               instr_out(ctx, 5, "setup background color\n");
00487               instr_out(ctx, 6, "setup foreground color\n");
00488               instr_out(ctx, 7, "color pattern offset\n");
00489               return len;
00490        case 0x03:
00491               decode_2d_br00(ctx, "XY_SETUP_CLIP_BLT");
00492 
00493               len = (data[0] & 0x000000ff) + 2;
00494               if (len != 3)
00495                      fprintf(out, "Bad count in XY_SETUP_CLIP_BLT\n");
00496 
00497               instr_out(ctx, 1, "cliprect (%d,%d)\n",
00498                        data[1] & 0xffff, data[2] >> 16);
00499               instr_out(ctx, 2, "cliprect (%d,%d)\n",
00500                        data[2] & 0xffff, data[3] >> 16);
00501               return len;
00502        case 0x11:
00503               decode_2d_br00(ctx, "XY_SETUP_MONO_PATTERN_SL_BLT");
00504 
00505               len = (data[0] & 0x000000ff) + 2;
00506               if (len != 9)
00507                      fprintf(out,
00508                             "Bad count in XY_SETUP_MONO_PATTERN_SL_BLT\n");
00509 
00510               decode_2d_br01(ctx);
00511               instr_out(ctx, 2, "cliprect (%d,%d)\n",
00512                        data[2] & 0xffff, data[2] >> 16);
00513               instr_out(ctx, 3, "cliprect (%d,%d)\n",
00514                        data[3] & 0xffff, data[3] >> 16);
00515               instr_out(ctx, 4, "setup dst offset 0x%08x\n",
00516                        data[4]);
00517               instr_out(ctx, 5, "setup background color\n");
00518               instr_out(ctx, 6, "setup foreground color\n");
00519               instr_out(ctx, 7, "mono pattern dw0\n");
00520               instr_out(ctx, 8, "mono pattern dw1\n");
00521               return len;
00522        case 0x50:
00523               decode_2d_br00(ctx, "XY_COLOR_BLT");
00524 
00525               len = (data[0] & 0x000000ff) + 2;
00526               if (len != 6)
00527                      fprintf(out, "Bad count in XY_COLOR_BLT\n");
00528 
00529               decode_2d_br01(ctx);
00530               instr_out(ctx, 2, "(%d,%d)\n",
00531                        data[2] & 0xffff, data[2] >> 16);
00532               instr_out(ctx, 3, "(%d,%d)\n",
00533                        data[3] & 0xffff, data[3] >> 16);
00534               instr_out(ctx, 4, "offset 0x%08x\n", data[4]);
00535               instr_out(ctx, 5, "color\n");
00536               return len;
00537        case 0x53:
00538               decode_2d_br00(ctx, "XY_SRC_COPY_BLT");
00539 
00540               len = (data[0] & 0x000000ff) + 2;
00541               if (len != 8)
00542                      fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
00543 
00544               decode_2d_br01(ctx);
00545               instr_out(ctx, 2, "dst (%d,%d)\n",
00546                        data[2] & 0xffff, data[2] >> 16);
00547               instr_out(ctx, 3, "dst (%d,%d)\n",
00548                        data[3] & 0xffff, data[3] >> 16);
00549               instr_out(ctx, 4, "dst offset 0x%08x\n", data[4]);
00550               instr_out(ctx, 5, "src (%d,%d)\n",
00551                        data[5] & 0xffff, data[5] >> 16);
00552               instr_out(ctx, 6, "src pitch %d\n",
00553                        (short)(data[6] & 0xffff));
00554               instr_out(ctx, 7, "src offset 0x%08x\n", data[7]);
00555               return len;
00556        }
00557 
00558        for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
00559             opcode++) {
00560               if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
00561                      unsigned int i;
00562 
00563                      len = 1;
00564                      instr_out(ctx, 0, "%s\n",
00565                               opcodes_2d[opcode].name);
00566                      if (opcodes_2d[opcode].max_len > 1) {
00567                             len = (data[0] & 0x000000ff) + 2;
00568                             if (len < opcodes_2d[opcode].min_len ||
00569                                 len > opcodes_2d[opcode].max_len) {
00570                                    fprintf(out, "Bad count in %s\n",
00571                                           opcodes_2d[opcode].name);
00572                             }
00573                      }
00574 
00575                      for (i = 1; i < len; i++) {
00576                             instr_out(ctx, i, "dword %d\n", i);
00577                      }
00578 
00579                      return len;
00580               }
00581        }
00582 
00583        instr_out(ctx, 0, "2D UNKNOWN\n");
00584        return 1;
00585 }
00586 
00587 static int
00588 decode_3d_1c(struct drm_intel_decode *ctx)
00589 {
00590        uint32_t *data = ctx->data;
00591        uint32_t opcode;
00592 
00593        opcode = (data[0] & 0x00f80000) >> 19;
00594 
00595        switch (opcode) {
00596        case 0x11:
00597               instr_out(ctx, 0,
00598                        "3DSTATE_DEPTH_SUBRECTANGLE_DISABLE\n");
00599               return 1;
00600        case 0x10:
00601               instr_out(ctx, 0, "3DSTATE_SCISSOR_ENABLE %s\n",
00602                        data[0] & 1 ? "enabled" : "disabled");
00603               return 1;
00604        case 0x01:
00605               instr_out(ctx, 0, "3DSTATE_MAP_COORD_SET_I830\n");
00606               return 1;
00607        case 0x0a:
00608               instr_out(ctx, 0, "3DSTATE_MAP_CUBE_I830\n");
00609               return 1;
00610        case 0x05:
00611               instr_out(ctx, 0, "3DSTATE_MAP_TEX_STREAM_I830\n");
00612               return 1;
00613        }
00614 
00615        instr_out(ctx, 0, "3D UNKNOWN: 3d_1c opcode = 0x%x\n",
00616                 opcode);
00617        return 1;
00618 }
00619 
00621 static void
00622 i915_get_instruction_dst(uint32_t *data, int i, char *dstname, int do_mask)
00623 {
00624        uint32_t a0 = data[i];
00625        int dst_nr = (a0 >> 14) & 0xf;
00626        char dstmask[8];
00627        const char *sat;
00628 
00629        if (do_mask) {
00630               if (((a0 >> 10) & 0xf) == 0xf) {
00631                      dstmask[0] = 0;
00632               } else {
00633                      int dstmask_index = 0;
00634 
00635                      dstmask[dstmask_index++] = '.';
00636                      if (a0 & (1 << 10))
00637                             dstmask[dstmask_index++] = 'x';
00638                      if (a0 & (1 << 11))
00639                             dstmask[dstmask_index++] = 'y';
00640                      if (a0 & (1 << 12))
00641                             dstmask[dstmask_index++] = 'z';
00642                      if (a0 & (1 << 13))
00643                             dstmask[dstmask_index++] = 'w';
00644                      dstmask[dstmask_index++] = 0;
00645               }
00646 
00647               if (a0 & (1 << 22))
00648                      sat = ".sat";
00649               else
00650                      sat = "";
00651        } else {
00652               dstmask[0] = 0;
00653               sat = "";
00654        }
00655 
00656        switch ((a0 >> 19) & 0x7) {
00657        case 0:
00658               if (dst_nr > 15)
00659                      fprintf(out, "bad destination reg R%d\n", dst_nr);
00660               sprintf(dstname, "R%d%s%s", dst_nr, dstmask, sat);
00661               break;
00662        case 4:
00663               if (dst_nr > 0)
00664                      fprintf(out, "bad destination reg oC%d\n", dst_nr);
00665               sprintf(dstname, "oC%s%s", dstmask, sat);
00666               break;
00667        case 5:
00668               if (dst_nr > 0)
00669                      fprintf(out, "bad destination reg oD%d\n", dst_nr);
00670               sprintf(dstname, "oD%s%s", dstmask, sat);
00671               break;
00672        case 6:
00673               if (dst_nr > 3)
00674                      fprintf(out, "bad destination reg U%d\n", dst_nr);
00675               sprintf(dstname, "U%d%s%s", dst_nr, dstmask, sat);
00676               break;
00677        default:
00678               sprintf(dstname, "RESERVED");
00679               break;
00680        }
00681 }
00682 
00683 static const char *
00684 i915_get_channel_swizzle(uint32_t select)
00685 {
00686        switch (select & 0x7) {
00687        case 0:
00688               return (select & 8) ? "-x" : "x";
00689        case 1:
00690               return (select & 8) ? "-y" : "y";
00691        case 2:
00692               return (select & 8) ? "-z" : "z";
00693        case 3:
00694               return (select & 8) ? "-w" : "w";
00695        case 4:
00696               return (select & 8) ? "-0" : "0";
00697        case 5:
00698               return (select & 8) ? "-1" : "1";
00699        default:
00700               return (select & 8) ? "-bad" : "bad";
00701        }
00702 }
00703 
00704 static void
00705 i915_get_instruction_src_name(uint32_t src_type, uint32_t src_nr, char *name)
00706 {
00707        switch (src_type) {
00708        case 0:
00709               sprintf(name, "R%d", src_nr);
00710               if (src_nr > 15)
00711                      fprintf(out, "bad src reg %s\n", name);
00712               break;
00713        case 1:
00714               if (src_nr < 8)
00715                      sprintf(name, "T%d", src_nr);
00716               else if (src_nr == 8)
00717                      sprintf(name, "DIFFUSE");
00718               else if (src_nr == 9)
00719                      sprintf(name, "SPECULAR");
00720               else if (src_nr == 10)
00721                      sprintf(name, "FOG");
00722               else {
00723                      fprintf(out, "bad src reg T%d\n", src_nr);
00724                      sprintf(name, "RESERVED");
00725               }
00726               break;
00727        case 2:
00728               sprintf(name, "C%d", src_nr);
00729               if (src_nr > 31)
00730                      fprintf(out, "bad src reg %s\n", name);
00731               break;
00732        case 4:
00733               sprintf(name, "oC");
00734               if (src_nr > 0)
00735                      fprintf(out, "bad src reg oC%d\n", src_nr);
00736               break;
00737        case 5:
00738               sprintf(name, "oD");
00739               if (src_nr > 0)
00740                      fprintf(out, "bad src reg oD%d\n", src_nr);
00741               break;
00742        case 6:
00743               sprintf(name, "U%d", src_nr);
00744               if (src_nr > 3)
00745                      fprintf(out, "bad src reg %s\n", name);
00746               break;
00747        default:
00748               fprintf(out, "bad src reg type %d\n", src_type);
00749               sprintf(name, "RESERVED");
00750               break;
00751        }
00752 }
00753 
00754 static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
00755 {
00756        uint32_t a0 = data[i];
00757        uint32_t a1 = data[i + 1];
00758        int src_nr = (a0 >> 2) & 0x1f;
00759        const char *swizzle_x = i915_get_channel_swizzle((a1 >> 28) & 0xf);
00760        const char *swizzle_y = i915_get_channel_swizzle((a1 >> 24) & 0xf);
00761        const char *swizzle_z = i915_get_channel_swizzle((a1 >> 20) & 0xf);
00762        const char *swizzle_w = i915_get_channel_swizzle((a1 >> 16) & 0xf);
00763        char swizzle[100];
00764 
00765        i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
00766        sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
00767               swizzle_w);
00768        if (strcmp(swizzle, ".xyzw") != 0)
00769               strcat(srcname, swizzle);
00770 }
00771 
00772 static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
00773 {
00774        uint32_t a1 = data[i + 1];
00775        uint32_t a2 = data[i + 2];
00776        int src_nr = (a1 >> 8) & 0x1f;
00777        const char *swizzle_x = i915_get_channel_swizzle((a1 >> 4) & 0xf);
00778        const char *swizzle_y = i915_get_channel_swizzle((a1 >> 0) & 0xf);
00779        const char *swizzle_z = i915_get_channel_swizzle((a2 >> 28) & 0xf);
00780        const char *swizzle_w = i915_get_channel_swizzle((a2 >> 24) & 0xf);
00781        char swizzle[100];
00782 
00783        i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
00784        sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
00785               swizzle_w);
00786        if (strcmp(swizzle, ".xyzw") != 0)
00787               strcat(srcname, swizzle);
00788 }
00789 
00790 static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
00791 {
00792        uint32_t a2 = data[i + 2];
00793        int src_nr = (a2 >> 16) & 0x1f;
00794        const char *swizzle_x = i915_get_channel_swizzle((a2 >> 12) & 0xf);
00795        const char *swizzle_y = i915_get_channel_swizzle((a2 >> 8) & 0xf);
00796        const char *swizzle_z = i915_get_channel_swizzle((a2 >> 4) & 0xf);
00797        const char *swizzle_w = i915_get_channel_swizzle((a2 >> 0) & 0xf);
00798        char swizzle[100];
00799 
00800        i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
00801        sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
00802               swizzle_w);
00803        if (strcmp(swizzle, ".xyzw") != 0)
00804               strcat(srcname, swizzle);
00805 }
00806 
00807 static void
00808 i915_get_instruction_addr(uint32_t src_type, uint32_t src_nr, char *name)
00809 {
00810        switch (src_type) {
00811        case 0:
00812               sprintf(name, "R%d", src_nr);
00813               if (src_nr > 15)
00814                      fprintf(out, "bad src reg %s\n", name);
00815               break;
00816        case 1:
00817               if (src_nr < 8)
00818                      sprintf(name, "T%d", src_nr);
00819               else if (src_nr == 8)
00820                      sprintf(name, "DIFFUSE");
00821               else if (src_nr == 9)
00822                      sprintf(name, "SPECULAR");
00823               else if (src_nr == 10)
00824                      sprintf(name, "FOG");
00825               else {
00826                      fprintf(out, "bad src reg T%d\n", src_nr);
00827                      sprintf(name, "RESERVED");
00828               }
00829               break;
00830        case 4:
00831               sprintf(name, "oC");
00832               if (src_nr > 0)
00833                      fprintf(out, "bad src reg oC%d\n", src_nr);
00834               break;
00835        case 5:
00836               sprintf(name, "oD");
00837               if (src_nr > 0)
00838                      fprintf(out, "bad src reg oD%d\n", src_nr);
00839               break;
00840        default:
00841               fprintf(out, "bad src reg type %d\n", src_type);
00842               sprintf(name, "RESERVED");
00843               break;
00844        }
00845 }
00846 
00847 static void
00848 i915_decode_alu1(struct drm_intel_decode *ctx,
00849                int i, char *instr_prefix, const char *op_name)
00850 {
00851        char dst[100], src0[100];
00852 
00853        i915_get_instruction_dst(ctx->data, i, dst, 1);
00854        i915_get_instruction_src0(ctx->data, i, src0);
00855 
00856        instr_out(ctx, i++, "%s: %s %s, %s\n", instr_prefix,
00857                 op_name, dst, src0);
00858        instr_out(ctx, i++, "%s\n", instr_prefix);
00859        instr_out(ctx, i++, "%s\n", instr_prefix);
00860 }
00861 
00862 static void
00863 i915_decode_alu2(struct drm_intel_decode *ctx,
00864                int i, char *instr_prefix, const char *op_name)
00865 {
00866        char dst[100], src0[100], src1[100];
00867 
00868        i915_get_instruction_dst(ctx->data, i, dst, 1);
00869        i915_get_instruction_src0(ctx->data, i, src0);
00870        i915_get_instruction_src1(ctx->data, i, src1);
00871 
00872        instr_out(ctx, i++, "%s: %s %s, %s, %s\n", instr_prefix,
00873                 op_name, dst, src0, src1);
00874        instr_out(ctx, i++, "%s\n", instr_prefix);
00875        instr_out(ctx, i++, "%s\n", instr_prefix);
00876 }
00877 
00878 static void
00879 i915_decode_alu3(struct drm_intel_decode *ctx,
00880                int i, char *instr_prefix, const char *op_name)
00881 {
00882        char dst[100], src0[100], src1[100], src2[100];
00883 
00884        i915_get_instruction_dst(ctx->data, i, dst, 1);
00885        i915_get_instruction_src0(ctx->data, i, src0);
00886        i915_get_instruction_src1(ctx->data, i, src1);
00887        i915_get_instruction_src2(ctx->data, i, src2);
00888 
00889        instr_out(ctx, i++, "%s: %s %s, %s, %s, %s\n", instr_prefix,
00890                 op_name, dst, src0, src1, src2);
00891        instr_out(ctx, i++, "%s\n", instr_prefix);
00892        instr_out(ctx, i++, "%s\n", instr_prefix);
00893 }
00894 
00895 static void
00896 i915_decode_tex(struct drm_intel_decode *ctx, int i,
00897               const char *instr_prefix, const char *tex_name)
00898 {
00899        uint32_t t0 = ctx->data[i];
00900        uint32_t t1 = ctx->data[i + 1];
00901        char dst_name[100];
00902        char addr_name[100];
00903        int sampler_nr;
00904 
00905        i915_get_instruction_dst(ctx->data, i, dst_name, 0);
00906        i915_get_instruction_addr((t1 >> 24) & 0x7,
00907                               (t1 >> 17) & 0xf, addr_name);
00908        sampler_nr = t0 & 0xf;
00909 
00910        instr_out(ctx, i++, "%s: %s %s, S%d, %s\n", instr_prefix,
00911                 tex_name, dst_name, sampler_nr, addr_name);
00912        instr_out(ctx, i++, "%s\n", instr_prefix);
00913        instr_out(ctx, i++, "%s\n", instr_prefix);
00914 }
00915 
00916 static void
00917 i915_decode_dcl(struct drm_intel_decode *ctx, int i, char *instr_prefix)
00918 {
00919        uint32_t d0 = ctx->data[i];
00920        const char *sampletype;
00921        int dcl_nr = (d0 >> 14) & 0xf;
00922        const char *dcl_x = d0 & (1 << 10) ? "x" : "";
00923        const char *dcl_y = d0 & (1 << 11) ? "y" : "";
00924        const char *dcl_z = d0 & (1 << 12) ? "z" : "";
00925        const char *dcl_w = d0 & (1 << 13) ? "w" : "";
00926        char dcl_mask[10];
00927 
00928        switch ((d0 >> 19) & 0x3) {
00929        case 1:
00930               sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
00931               if (strcmp(dcl_mask, ".") == 0)
00932                      fprintf(out, "bad (empty) dcl mask\n");
00933 
00934               if (dcl_nr > 10)
00935                      fprintf(out, "bad T%d dcl register number\n", dcl_nr);
00936               if (dcl_nr < 8) {
00937                      if (strcmp(dcl_mask, ".x") != 0 &&
00938                          strcmp(dcl_mask, ".xy") != 0 &&
00939                          strcmp(dcl_mask, ".xz") != 0 &&
00940                          strcmp(dcl_mask, ".w") != 0 &&
00941                          strcmp(dcl_mask, ".xyzw") != 0) {
00942                             fprintf(out, "bad T%d.%s dcl mask\n", dcl_nr,
00943                                    dcl_mask);
00944                      }
00945                      instr_out(ctx, i++, "%s: DCL T%d%s\n",
00946                               instr_prefix, dcl_nr, dcl_mask);
00947               } else {
00948                      if (strcmp(dcl_mask, ".xz") == 0)
00949                             fprintf(out, "errataed bad dcl mask %s\n",
00950                                    dcl_mask);
00951                      else if (strcmp(dcl_mask, ".xw") == 0)
00952                             fprintf(out, "errataed bad dcl mask %s\n",
00953                                    dcl_mask);
00954                      else if (strcmp(dcl_mask, ".xzw") == 0)
00955                             fprintf(out, "errataed bad dcl mask %s\n",
00956                                    dcl_mask);
00957 
00958                      if (dcl_nr == 8) {
00959                             instr_out(ctx, i++,
00960                                      "%s: DCL DIFFUSE%s\n", instr_prefix,
00961                                      dcl_mask);
00962                      } else if (dcl_nr == 9) {
00963                             instr_out(ctx, i++,
00964                                      "%s: DCL SPECULAR%s\n", instr_prefix,
00965                                      dcl_mask);
00966                      } else if (dcl_nr == 10) {
00967                             instr_out(ctx, i++,
00968                                      "%s: DCL FOG%s\n", instr_prefix,
00969                                      dcl_mask);
00970                      }
00971               }
00972               instr_out(ctx, i++, "%s\n", instr_prefix);
00973               instr_out(ctx, i++, "%s\n", instr_prefix);
00974               break;
00975        case 3:
00976               switch ((d0 >> 22) & 0x3) {
00977               case 0:
00978                      sampletype = "2D";
00979                      break;
00980               case 1:
00981                      sampletype = "CUBE";
00982                      break;
00983               case 2:
00984                      sampletype = "3D";
00985                      break;
00986               default:
00987                      sampletype = "RESERVED";
00988                      break;
00989               }
00990               if (dcl_nr > 15)
00991                      fprintf(out, "bad S%d dcl register number\n", dcl_nr);
00992               instr_out(ctx, i++, "%s: DCL S%d %s\n",
00993                        instr_prefix, dcl_nr, sampletype);
00994               instr_out(ctx, i++, "%s\n", instr_prefix);
00995               instr_out(ctx, i++, "%s\n", instr_prefix);
00996               break;
00997        default:
00998               instr_out(ctx, i++, "%s: DCL RESERVED%d\n",
00999                        instr_prefix, dcl_nr);
01000               instr_out(ctx, i++, "%s\n", instr_prefix);
01001               instr_out(ctx, i++, "%s\n", instr_prefix);
01002        }
01003 }
01004 
01005 static void
01006 i915_decode_instruction(struct drm_intel_decode *ctx,
01007                      int i, char *instr_prefix)
01008 {
01009        switch ((ctx->data[i] >> 24) & 0x1f) {
01010        case 0x0:
01011               instr_out(ctx, i++, "%s: NOP\n", instr_prefix);
01012               instr_out(ctx, i++, "%s\n", instr_prefix);
01013               instr_out(ctx, i++, "%s\n", instr_prefix);
01014               break;
01015        case 0x01:
01016               i915_decode_alu2(ctx, i, instr_prefix, "ADD");
01017               break;
01018        case 0x02:
01019               i915_decode_alu1(ctx, i, instr_prefix, "MOV");
01020               break;
01021        case 0x03:
01022               i915_decode_alu2(ctx, i, instr_prefix, "MUL");
01023               break;
01024        case 0x04:
01025               i915_decode_alu3(ctx, i, instr_prefix, "MAD");
01026               break;
01027        case 0x05:
01028               i915_decode_alu3(ctx, i, instr_prefix, "DP2ADD");
01029               break;
01030        case 0x06:
01031               i915_decode_alu2(ctx, i, instr_prefix, "DP3");
01032               break;
01033        case 0x07:
01034               i915_decode_alu2(ctx, i, instr_prefix, "DP4");
01035               break;
01036        case 0x08:
01037               i915_decode_alu1(ctx, i, instr_prefix, "FRC");
01038               break;
01039        case 0x09:
01040               i915_decode_alu1(ctx, i, instr_prefix, "RCP");
01041               break;
01042        case 0x0a:
01043               i915_decode_alu1(ctx, i, instr_prefix, "RSQ");
01044               break;
01045        case 0x0b:
01046               i915_decode_alu1(ctx, i, instr_prefix, "EXP");
01047               break;
01048        case 0x0c:
01049               i915_decode_alu1(ctx, i, instr_prefix, "LOG");
01050               break;
01051        case 0x0d:
01052               i915_decode_alu2(ctx, i, instr_prefix, "CMP");
01053               break;
01054        case 0x0e:
01055               i915_decode_alu2(ctx, i, instr_prefix, "MIN");
01056               break;
01057        case 0x0f:
01058               i915_decode_alu2(ctx, i, instr_prefix, "MAX");
01059               break;
01060        case 0x10:
01061               i915_decode_alu1(ctx, i, instr_prefix, "FLR");
01062               break;
01063        case 0x11:
01064               i915_decode_alu1(ctx, i, instr_prefix, "MOD");
01065               break;
01066        case 0x12:
01067               i915_decode_alu1(ctx, i, instr_prefix, "TRC");
01068               break;
01069        case 0x13:
01070               i915_decode_alu2(ctx, i, instr_prefix, "SGE");
01071               break;
01072        case 0x14:
01073               i915_decode_alu2(ctx, i, instr_prefix, "SLT");
01074               break;
01075        case 0x15:
01076               i915_decode_tex(ctx, i, instr_prefix, "TEXLD");
01077               break;
01078        case 0x16:
01079               i915_decode_tex(ctx, i, instr_prefix, "TEXLDP");
01080               break;
01081        case 0x17:
01082               i915_decode_tex(ctx, i, instr_prefix, "TEXLDB");
01083               break;
01084        case 0x19:
01085               i915_decode_dcl(ctx, i, instr_prefix);
01086               break;
01087        default:
01088               instr_out(ctx, i++, "%s: unknown\n", instr_prefix);
01089               instr_out(ctx, i++, "%s\n", instr_prefix);
01090               instr_out(ctx, i++, "%s\n", instr_prefix);
01091               break;
01092        }
01093 }
01094 
01095 static const char *
01096 decode_compare_func(uint32_t op)
01097 {
01098        switch (op & 0x7) {
01099        case 0:
01100               return "always";
01101        case 1:
01102               return "never";
01103        case 2:
01104               return "less";
01105        case 3:
01106               return "equal";
01107        case 4:
01108               return "lequal";
01109        case 5:
01110               return "greater";
01111        case 6:
01112               return "notequal";
01113        case 7:
01114               return "gequal";
01115        }
01116        return "";
01117 }
01118 
01119 static const char *
01120 decode_stencil_op(uint32_t op)
01121 {
01122        switch (op & 0x7) {
01123        case 0:
01124               return "keep";
01125        case 1:
01126               return "zero";
01127        case 2:
01128               return "replace";
01129        case 3:
01130               return "incr_sat";
01131        case 4:
01132               return "decr_sat";
01133        case 5:
01134               return "greater";
01135        case 6:
01136               return "incr";
01137        case 7:
01138               return "decr";
01139        }
01140        return "";
01141 }
01142 
01143 #if 0
01144 static const char *
01145 decode_logic_op(uint32_t op)
01146 {
01147        switch (op & 0xf) {
01148        case 0:
01149               return "clear";
01150        case 1:
01151               return "nor";
01152        case 2:
01153               return "and_inv";
01154        case 3:
01155               return "copy_inv";
01156        case 4:
01157               return "and_rvrse";
01158        case 5:
01159               return "inv";
01160        case 6:
01161               return "xor";
01162        case 7:
01163               return "nand";
01164        case 8:
01165               return "and";
01166        case 9:
01167               return "equiv";
01168        case 10:
01169               return "noop";
01170        case 11:
01171               return "or_inv";
01172        case 12:
01173               return "copy";
01174        case 13:
01175               return "or_rvrse";
01176        case 14:
01177               return "or";
01178        case 15:
01179               return "set";
01180        }
01181        return "";
01182 }
01183 #endif
01184 
01185 static const char *
01186 decode_blend_fact(uint32_t op)
01187 {
01188        switch (op & 0xf) {
01189        case 1:
01190               return "zero";
01191        case 2:
01192               return "one";
01193        case 3:
01194               return "src_colr";
01195        case 4:
01196               return "inv_src_colr";
01197        case 5:
01198               return "src_alpha";
01199        case 6:
01200               return "inv_src_alpha";
01201        case 7:
01202               return "dst_alpha";
01203        case 8:
01204               return "inv_dst_alpha";
01205        case 9:
01206               return "dst_colr";
01207        case 10:
01208               return "inv_dst_colr";
01209        case 11:
01210               return "src_alpha_sat";
01211        case 12:
01212               return "cnst_colr";
01213        case 13:
01214               return "inv_cnst_colr";
01215        case 14:
01216               return "cnst_alpha";
01217        case 15:
01218               return "inv_const_alpha";
01219        }
01220        return "";
01221 }
01222 
01223 static const char *
01224 decode_tex_coord_mode(uint32_t mode)
01225 {
01226        switch (mode & 0x7) {
01227        case 0:
01228               return "wrap";
01229        case 1:
01230               return "mirror";
01231        case 2:
01232               return "clamp_edge";
01233        case 3:
01234               return "cube";
01235        case 4:
01236               return "clamp_border";
01237        case 5:
01238               return "mirror_once";
01239        }
01240        return "";
01241 }
01242 
01243 static const char *
01244 decode_sample_filter(uint32_t mode)
01245 {
01246        switch (mode & 0x7) {
01247        case 0:
01248               return "nearest";
01249        case 1:
01250               return "linear";
01251        case 2:
01252               return "anisotropic";
01253        case 3:
01254               return "4x4_1";
01255        case 4:
01256               return "4x4_2";
01257        case 5:
01258               return "4x4_flat";
01259        case 6:
01260               return "6x5_mono";
01261        }
01262        return "";
01263 }
01264 
01265 static int
01266 decode_3d_1d(struct drm_intel_decode *ctx)
01267 {
01268        unsigned int len, i, c, idx, word, map, sampler, instr;
01269        const char *format, *zformat, *type;
01270        uint32_t opcode;
01271        uint32_t *data = ctx->data;
01272        uint32_t devid = ctx->devid;
01273 
01274        struct {
01275               uint32_t opcode;
01276               int i830_only;
01277               unsigned int min_len;
01278               unsigned int max_len;
01279               const char *name;
01280        } opcodes_3d_1d[] = {
01281               { 0x86, 0, 4, 4, "3DSTATE_CHROMA_KEY" },
01282               { 0x88, 0, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
01283               { 0x99, 0, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
01284               { 0x9a, 0, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
01285               { 0x98, 0, 2, 2, "3DSTATE_DEFAULT_Z" },
01286               { 0x97, 0, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
01287               { 0x9d, 0, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
01288               { 0x9e, 0, 4, 4, "3DSTATE_MONO_FILTER" },
01289               { 0x89, 0, 4, 4, "3DSTATE_FOG_MODE" },
01290               { 0x8f, 0, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
01291               { 0x83, 0, 2, 2, "3DSTATE_SPAN_STIPPLE" },
01292               { 0x8c, 1, 2, 2, "3DSTATE_MAP_COORD_TRANSFORM_I830" },
01293               { 0x8b, 1, 2, 2, "3DSTATE_MAP_VERTEX_TRANSFORM_I830" },
01294               { 0x8d, 1, 3, 3, "3DSTATE_W_STATE_I830" },
01295               { 0x01, 1, 2, 2, "3DSTATE_COLOR_FACTOR_I830" },
01296               { 0x02, 1, 2, 2, "3DSTATE_MAP_COORD_SETBIND_I830"},
01297        }, *opcode_3d_1d;
01298 
01299        opcode = (data[0] & 0x00ff0000) >> 16;
01300 
01301        switch (opcode) {
01302        case 0x07:
01303               /* This instruction is unusual.  A 0 length means just
01304                * 1 DWORD instead of 2.  The 0 length is specified in
01305                * one place to be unsupported, but stated to be
01306                * required in another, and 0 length LOAD_INDIRECTs
01307                * appear to cause no harm at least.
01308                */
01309               instr_out(ctx, 0, "3DSTATE_LOAD_INDIRECT\n");
01310               len = (data[0] & 0x000000ff) + 1;
01311               i = 1;
01312               if (data[0] & (0x01 << 8)) {
01313                      instr_out(ctx, i++, "SIS.0\n");
01314                      instr_out(ctx, i++, "SIS.1\n");
01315               }
01316               if (data[0] & (0x02 << 8)) {
01317                      instr_out(ctx, i++, "DIS.0\n");
01318               }
01319               if (data[0] & (0x04 << 8)) {
01320                      instr_out(ctx, i++, "SSB.0\n");
01321                      instr_out(ctx, i++, "SSB.1\n");
01322               }
01323               if (data[0] & (0x08 << 8)) {
01324                      instr_out(ctx, i++, "MSB.0\n");
01325                      instr_out(ctx, i++, "MSB.1\n");
01326               }
01327               if (data[0] & (0x10 << 8)) {
01328                      instr_out(ctx, i++, "PSP.0\n");
01329                      instr_out(ctx, i++, "PSP.1\n");
01330               }
01331               if (data[0] & (0x20 << 8)) {
01332                      instr_out(ctx, i++, "PSC.0\n");
01333                      instr_out(ctx, i++, "PSC.1\n");
01334               }
01335               if (len != i) {
01336                      fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
01337                      return len;
01338               }
01339               return len;
01340        case 0x04:
01341               instr_out(ctx, 0,
01342                        "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
01343               len = (data[0] & 0x0000000f) + 2;
01344               i = 1;
01345               for (word = 0; word <= 8; word++) {
01346                      if (data[0] & (1 << (4 + word))) {
01347                             /* save vertex state for decode */
01348                             if (!IS_GEN2(devid)) {
01349                                    int tex_num;
01350 
01351                                    if (word == 2) {
01352                                           saved_s2_set = 1;
01353                                           saved_s2 = data[i];
01354                                    }
01355                                    if (word == 4) {
01356                                           saved_s4_set = 1;
01357                                           saved_s4 = data[i];
01358                                    }
01359 
01360                                    switch (word) {
01361                                    case 0:
01362                                           instr_out(ctx, i,
01363                                                    "S0: vbo offset: 0x%08x%s\n",
01364                                                    data[i] & (~1),
01365                                                    data[i] & 1 ?
01366                                                    ", auto cache invalidate disabled"
01367                                                    : "");
01368                                           break;
01369                                    case 1:
01370                                           instr_out(ctx, i,
01371                                                    "S1: vertex width: %i, vertex pitch: %i\n",
01372                                                    (data[i] >> 24) &
01373                                                    0x3f,
01374                                                    (data[i] >> 16) &
01375                                                    0x3f);
01376                                           break;
01377                                    case 2:
01378                                           instr_out(ctx, i,
01379                                                    "S2: texcoord formats: ");
01380                                           for (tex_num = 0;
01381                                                tex_num < 8; tex_num++) {
01382                                                  switch ((data[i] >>
01383                                                          tex_num *
01384                                                          4) & 0xf) {
01385                                                  case 0:
01386                                                         fprintf(out,
01387                                                                "%i=2D ",
01388                                                                tex_num);
01389                                                         break;
01390                                                  case 1:
01391                                                         fprintf(out,
01392                                                                "%i=3D ",
01393                                                                tex_num);
01394                                                         break;
01395                                                  case 2:
01396                                                         fprintf(out,
01397                                                                "%i=4D ",
01398                                                                tex_num);
01399                                                         break;
01400                                                  case 3:
01401                                                         fprintf(out,
01402                                                                "%i=1D ",
01403                                                                tex_num);
01404                                                         break;
01405                                                  case 4:
01406                                                         fprintf(out,
01407                                                                "%i=2D_16 ",
01408                                                                tex_num);
01409                                                         break;
01410                                                  case 5:
01411                                                         fprintf(out,
01412                                                                "%i=4D_16 ",
01413                                                                tex_num);
01414                                                         break;
01415                                                  case 0xf:
01416                                                         fprintf(out,
01417                                                                "%i=NP ",
01418                                                                tex_num);
01419                                                         break;
01420                                                  }
01421                                           }
01422                                           fprintf(out, "\n");
01423 
01424                                           break;
01425                                    case 3:
01426                                           instr_out(ctx, i,
01427                                                    "S3: not documented\n");
01428                                           break;
01429                                    case 4:
01430                                           {
01431                                                  const char *cullmode = "";
01432                                                  const char *vfmt_xyzw = "";
01433                                                  switch ((data[i] >> 13)
01434                                                         & 0x3) {
01435                                                  case 0:
01436                                                         cullmode =
01437                                                             "both";
01438                                                         break;
01439                                                  case 1:
01440                                                         cullmode =
01441                                                             "none";
01442                                                         break;
01443                                                  case 2:
01444                                                         cullmode = "cw";
01445                                                         break;
01446                                                  case 3:
01447                                                         cullmode =
01448                                                             "ccw";
01449                                                         break;
01450                                                  }
01451                                                  switch (data[i] &
01452                                                         (7 << 6 | 1 <<
01453                                                          2)) {
01454                                                  case 1 << 6:
01455                                                         vfmt_xyzw =
01456                                                             "XYZ,";
01457                                                         break;
01458                                                  case 2 << 6:
01459                                                         vfmt_xyzw =
01460                                                             "XYZW,";
01461                                                         break;
01462                                                  case 3 << 6:
01463                                                         vfmt_xyzw =
01464                                                             "XY,";
01465                                                         break;
01466                                                  case 4 << 6:
01467                                                         vfmt_xyzw =
01468                                                             "XYW,";
01469                                                         break;
01470                                                  case 1 << 6 | 1 << 2:
01471                                                         vfmt_xyzw =
01472                                                             "XYZF,";
01473                                                         break;
01474                                                  case 2 << 6 | 1 << 2:
01475                                                         vfmt_xyzw =
01476                                                             "XYZWF,";
01477                                                         break;
01478                                                  case 3 << 6 | 1 << 2:
01479                                                         vfmt_xyzw =
01480                                                             "XYF,";
01481                                                         break;
01482                                                  case 4 << 6 | 1 << 2:
01483                                                         vfmt_xyzw =
01484                                                             "XYWF,";
01485                                                         break;
01486                                                  }
01487                                                  instr_out(ctx, i,
01488                                                           "S4: point_width=%i, line_width=%.1f,"
01489                                                           "%s%s%s%s%s cullmode=%s, vfmt=%s%s%s%s%s%s "
01490                                                           "%s%s%s%s%s\n",
01491                                                           (data[i] >>
01492                                                            23) & 0x1ff,
01493                                                           ((data[i] >>
01494                                                             19) & 0xf) /
01495                                                           2.0,
01496                                                           data[i] & (0xf
01497                                                                     <<
01498                                                                     15)
01499                                                           ?
01500                                                           " flatshade="
01501                                                           : "",
01502                                                           data[i] & (1
01503                                                                     <<
01504                                                                     18)
01505                                                           ? "Alpha," :
01506                                                           "",
01507                                                           data[i] & (1
01508                                                                     <<
01509                                                                     17)
01510                                                           ? "Fog," : "",
01511                                                           data[i] & (1
01512                                                                     <<
01513                                                                     16)
01514                                                           ? "Specular,"
01515                                                           : "",
01516                                                           data[i] & (1
01517                                                                     <<
01518                                                                     15)
01519                                                           ? "Color," :
01520                                                           "", cullmode,
01521                                                           data[i] & (1
01522                                                                     <<
01523                                                                     12)
01524                                                           ?
01525                                                           "PointWidth,"
01526                                                           : "",
01527                                                           data[i] & (1
01528                                                                     <<
01529                                                                     11)
01530                                                           ? "SpecFog," :
01531                                                           "",
01532                                                           data[i] & (1
01533                                                                     <<
01534                                                                     10)
01535                                                           ? "Color," :
01536                                                           "",
01537                                                           data[i] & (1
01538                                                                     <<
01539                                                                     9)
01540                                                           ? "DepthOfs,"
01541                                                           : "",
01542                                                           vfmt_xyzw,
01543                                                           data[i] & (1
01544                                                                     <<
01545                                                                     9)
01546                                                           ? "FogParam,"
01547                                                           : "",
01548                                                           data[i] & (1
01549                                                                     <<
01550                                                                     5)
01551                                                           ?
01552                                                           "force default diffuse, "
01553                                                           : "",
01554                                                           data[i] & (1
01555                                                                     <<
01556                                                                     4)
01557                                                           ?
01558                                                           "force default specular, "
01559                                                           : "",
01560                                                           data[i] & (1
01561                                                                     <<
01562                                                                     3)
01563                                                           ?
01564                                                           "local depth ofs enable, "
01565                                                           : "",
01566                                                           data[i] & (1
01567                                                                     <<
01568                                                                     1)
01569                                                           ?
01570                                                           "point sprite enable, "
01571                                                           : "",
01572                                                           data[i] & (1
01573                                                                     <<
01574                                                                     0)
01575                                                           ?
01576                                                           "line AA enable, "
01577                                                           : "");
01578                                                  break;
01579                                           }
01580                                    case 5:
01581                                           {
01582                                                  instr_out(ctx, i,
01583                                                           "S5:%s%s%s%s%s"
01584                                                           "%s%s%s%s stencil_ref=0x%x, stencil_test=%s, "
01585                                                           "stencil_fail=%s, stencil_pass_z_fail=%s, "
01586                                                           "stencil_pass_z_pass=%s, %s%s%s%s\n",
01587                                                           data[i] & (0xf
01588                                                                     <<
01589                                                                     28)
01590                                                           ?
01591                                                           " write_disable="
01592                                                           : "",
01593                                                           data[i] & (1
01594                                                                     <<
01595                                                                     31)
01596                                                           ? "Alpha," :
01597                                                           "",
01598                                                           data[i] & (1
01599                                                                     <<
01600                                                                     30)
01601                                                           ? "Red," : "",
01602                                                           data[i] & (1
01603                                                                     <<
01604                                                                     29)
01605                                                           ? "Green," :
01606                                                           "",
01607                                                           data[i] & (1
01608                                                                     <<
01609                                                                     28)
01610                                                           ? "Blue," :
01611                                                           "",
01612                                                           data[i] & (1
01613                                                                     <<
01614                                                                     27)
01615                                                           ?
01616                                                           " force default point size,"
01617                                                           : "",
01618                                                           data[i] & (1
01619                                                                     <<
01620                                                                     26)
01621                                                           ?
01622                                                           " last pixel enable,"
01623                                                           : "",
01624                                                           data[i] & (1
01625                                                                     <<
01626                                                                     25)
01627                                                           ?
01628                                                           " global depth ofs enable,"
01629                                                           : "",
01630                                                           data[i] & (1
01631                                                                     <<
01632                                                                     24)
01633                                                           ?
01634                                                           " fog enable,"
01635                                                           : "",
01636                                                           (data[i] >>
01637                                                            16) & 0xff,
01638                                                           decode_compare_func
01639                                                           (data[i] >>
01640                                                            13),
01641                                                           decode_stencil_op
01642                                                           (data[i] >>
01643                                                            10),
01644                                                           decode_stencil_op
01645                                                           (data[i] >>
01646                                                            7),
01647                                                           decode_stencil_op
01648                                                           (data[i] >>
01649                                                            4),
01650                                                           data[i] & (1
01651                                                                     <<
01652                                                                     3)
01653                                                           ?
01654                                                           "stencil write enable, "
01655                                                           : "",
01656                                                           data[i] & (1
01657                                                                     <<
01658                                                                     2)
01659                                                           ?
01660                                                           "stencil test enable, "
01661                                                           : "",
01662                                                           data[i] & (1
01663                                                                     <<
01664                                                                     1)
01665                                                           ?
01666                                                           "color dither enable, "
01667                                                           : "",
01668                                                           data[i] & (1
01669                                                                     <<
01670                                                                     0)
01671                                                           ?
01672                                                           "logicop enable, "
01673                                                           : "");
01674                                           }
01675                                           break;
01676                                    case 6:
01677                                           instr_out(ctx, i,
01678                                                    "S6: %salpha_test=%s, alpha_ref=0x%x, "
01679                                                    "depth_test=%s, %ssrc_blnd_fct=%s, dst_blnd_fct=%s, "
01680                                                    "%s%stristrip_provoking_vertex=%i\n",
01681                                                    data[i] & (1 << 31) ?
01682                                                    "alpha test enable, "
01683                                                    : "",
01684                                                    decode_compare_func
01685                                                    (data[i] >> 28),
01686                                                    data[i] & (0xff <<
01687                                                              20),
01688                                                    decode_compare_func
01689                                                    (data[i] >> 16),
01690                                                    data[i] & (1 << 15) ?
01691                                                    "cbuf blend enable, "
01692                                                    : "",
01693                                                    decode_blend_fact(data
01694                                                                    [i]
01695                                                                    >>
01696                                                                    8),
01697                                                    decode_blend_fact(data
01698                                                                    [i]
01699                                                                    >>
01700                                                                    4),
01701                                                    data[i] & (1 << 3) ?
01702                                                    "depth write enable, "
01703                                                    : "",
01704                                                    data[i] & (1 << 2) ?
01705                                                    "cbuf write enable, "
01706                                                    : "",
01707                                                    data[i] & (0x3));
01708                                           break;
01709                                    case 7:
01710                                           instr_out(ctx, i,
01711                                                    "S7: depth offset constant: 0x%08x\n",
01712                                                    data[i]);
01713                                           break;
01714                                    }
01715                             } else {
01716                                    instr_out(ctx, i,
01717                                             "S%d: 0x%08x\n", i, data[i]);
01718                             }
01719                             i++;
01720                      }
01721               }
01722               if (len != i) {
01723                      fprintf(out,
01724                             "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
01725               }
01726               return len;
01727        case 0x03:
01728               instr_out(ctx, 0,
01729                        "3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
01730               len = (data[0] & 0x0000000f) + 2;
01731               i = 1;
01732               for (word = 6; word <= 14; word++) {
01733                      if (data[0] & (1 << word)) {
01734                             if (word == 6)
01735                                    instr_out(ctx, i++,
01736                                             "TBCF\n");
01737                             else if (word >= 7 && word <= 10) {
01738                                    instr_out(ctx, i++,
01739                                             "TB%dC\n", word - 7);
01740                                    instr_out(ctx, i++,
01741                                             "TB%dA\n", word - 7);
01742                             } else if (word >= 11 && word <= 14) {
01743                                    instr_out(ctx, i,
01744                                             "TM%dS0: offset=0x%08x, %s\n",
01745                                             word - 11,
01746                                             data[i] & 0xfffffffe,
01747                                             data[i] & 1 ? "use fence" :
01748                                             "");
01749                                    i++;
01750                                    instr_out(ctx, i,
01751                                             "TM%dS1: height=%i, width=%i, %s\n",
01752                                             word - 11, data[i] >> 21,
01753                                             (data[i] >> 10) & 0x3ff,
01754                                             data[i] & 2 ? (data[i] & 1 ?
01755                                                          "y-tiled" :
01756                                                          "x-tiled") :
01757                                             "");
01758                                    i++;
01759                                    instr_out(ctx, i,
01760                                             "TM%dS2: pitch=%i, \n",
01761                                             word - 11,
01762                                             ((data[i] >> 21) + 1) * 4);
01763                                    i++;
01764                                    instr_out(ctx, i++,
01765                                             "TM%dS3\n", word - 11);
01766                                    instr_out(ctx, i++,
01767                                             "TM%dS4: dflt color\n",
01768                                             word - 11);
01769                             }
01770                      }
01771               }
01772               if (len != i) {
01773                      fprintf(out,
01774                             "Bad count in 3DSTATE_LOAD_STATE_IMMEDIATE_2\n");
01775               }
01776               return len;
01777        case 0x00:
01778               instr_out(ctx, 0, "3DSTATE_MAP_STATE\n");
01779               len = (data[0] & 0x0000003f) + 2;
01780               instr_out(ctx, 1, "mask\n");
01781 
01782               i = 2;
01783               for (map = 0; map <= 15; map++) {
01784                      if (data[1] & (1 << map)) {
01785                             int width, height, pitch, dword;
01786                             const char *tiling;
01787 
01788                             dword = data[i];
01789                             instr_out(ctx, i++,
01790                                      "map %d MS2 %s%s%s\n", map,
01791                                      dword & (1 << 31) ?
01792                                      "untrusted surface, " : "",
01793                                      dword & (1 << 1) ?
01794                                      "vertical line stride enable, " : "",
01795                                      dword & (1 << 0) ?
01796                                      "vertical ofs enable, " : "");
01797 
01798                             dword = data[i];
01799                             width = ((dword >> 10) & ((1 << 11) - 1)) + 1;
01800                             height = ((dword >> 21) & ((1 << 11) - 1)) + 1;
01801 
01802                             tiling = "none";
01803                             if (dword & (1 << 2))
01804                                    tiling = "fenced";
01805                             else if (dword & (1 << 1))
01806                                    tiling = dword & (1 << 0) ? "Y" : "X";
01807                             type = " BAD";
01808                             format = "BAD";
01809                             switch ((dword >> 7) & 0x7) {
01810                             case 1:
01811                                    type = "8b";
01812                                    switch ((dword >> 3) & 0xf) {
01813                                    case 0:
01814                                           format = "I";
01815                                           break;
01816                                    case 1:
01817                                           format = "L";
01818                                           break;
01819                                    case 4:
01820                                           format = "A";
01821                                           break;
01822                                    case 5:
01823                                           format = " mono";
01824                                           break;
01825                                    }
01826                                    break;
01827                             case 2:
01828                                    type = "16b";
01829                                    switch ((dword >> 3) & 0xf) {
01830                                    case 0:
01831                                           format = " rgb565";
01832                                           break;
01833                                    case 1:
01834                                           format = " argb1555";
01835                                           break;
01836                                    case 2:
01837                                           format = " argb4444";
01838                                           break;
01839                                    case 5:
01840                                           format = " ay88";
01841                                           break;
01842                                    case 6:
01843                                           format = " bump655";
01844                                           break;
01845                                    case 7:
01846                                           format = "I";
01847                                           break;
01848                                    case 8:
01849                                           format = "L";
01850                                           break;
01851                                    case 9:
01852                                           format = "A";
01853                                           break;
01854                                    }
01855                                    break;
01856                             case 3:
01857                                    type = "32b";
01858                                    switch ((dword >> 3) & 0xf) {
01859                                    case 0:
01860                                           format = " argb8888";
01861                                           break;
01862                                    case 1:
01863                                           format = " abgr8888";
01864                                           break;
01865                                    case 2:
01866                                           format = " xrgb8888";
01867                                           break;
01868                                    case 3:
01869                                           format = " xbgr8888";
01870                                           break;
01871                                    case 4:
01872                                           format = " qwvu8888";
01873                                           break;
01874                                    case 5:
01875                                           format = " axvu8888";
01876                                           break;
01877                                    case 6:
01878                                           format = " lxvu8888";
01879                                           break;
01880                                    case 7:
01881                                           format = " xlvu8888";
01882                                           break;
01883                                    case 8:
01884                                           format = " argb2101010";
01885                                           break;
01886                                    case 9:
01887                                           format = " abgr2101010";
01888                                           break;
01889                                    case 10:
01890                                           format = " awvu2101010";
01891                                           break;
01892                                    case 11:
01893                                           format = " gr1616";
01894                                           break;
01895                                    case 12:
01896                                           format = " vu1616";
01897                                           break;
01898                                    case 13:
01899                                           format = " xI824";
01900                                           break;
01901                                    case 14:
01902                                           format = " xA824";
01903                                           break;
01904                                    case 15:
01905                                           format = " xL824";
01906                                           break;
01907                                    }
01908                                    break;
01909                             case 5:
01910                                    type = "422";
01911                                    switch ((dword >> 3) & 0xf) {
01912                                    case 0:
01913                                           format = " yuv_swapy";
01914                                           break;
01915                                    case 1:
01916                                           format = " yuv";
01917                                           break;
01918                                    case 2:
01919                                           format = " yuv_swapuv";
01920                                           break;
01921                                    case 3:
01922                                           format = " yuv_swapuvy";
01923                                           break;
01924                                    }
01925                                    break;
01926                             case 6:
01927                                    type = "compressed";
01928                                    switch ((dword >> 3) & 0x7) {
01929                                    case 0:
01930                                           format = " dxt1";
01931                                           break;
01932                                    case 1:
01933                                           format = " dxt2_3";
01934                                           break;
01935                                    case 2:
01936                                           format = " dxt4_5";
01937                                           break;
01938                                    case 3:
01939                                           format = " fxt1";
01940                                           break;
01941                                    case 4:
01942                                           format = " dxt1_rb";
01943                                           break;
01944                                    }
01945                                    break;
01946                             case 7:
01947                                    type = "4b indexed";
01948                                    switch ((dword >> 3) & 0xf) {
01949                                    case 7:
01950                                           format = " argb8888";
01951                                           break;
01952                                    }
01953                                    break;
01954                             }
01955                             dword = data[i];
01956                             instr_out(ctx, i++,
01957                                      "map %d MS3 [width=%d, height=%d, format=%s%s, tiling=%s%s]\n",
01958                                      map, width, height, type, format,
01959                                      tiling,
01960                                      dword & (1 << 9) ? " palette select" :
01961                                      "");
01962 
01963                             dword = data[i];
01964                             pitch =
01965                                 4 * (((dword >> 21) & ((1 << 11) - 1)) + 1);
01966                             instr_out(ctx, i++,
01967                                      "map %d MS4 [pitch=%d, max_lod=%i, vol_depth=%i, cube_face_ena=%x, %s]\n",
01968                                      map, pitch, (dword >> 9) & 0x3f,
01969                                      dword & 0xff, (dword >> 15) & 0x3f,
01970                                      dword & (1 << 8) ? "miplayout legacy"
01971                                      : "miplayout right");
01972                      }
01973               }
01974               if (len != i) {
01975                      fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
01976                      return len;
01977               }
01978               return len;
01979        case 0x06:
01980               instr_out(ctx, 0,
01981                        "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
01982               len = (data[0] & 0x000000ff) + 2;
01983 
01984               i = 2;
01985               for (c = 0; c <= 31; c++) {
01986                      if (data[1] & (1 << c)) {
01987                             instr_out(ctx, i, "C%d.X = %f\n", c,
01988                                      int_as_float(data[i]));
01989                             i++;
01990                             instr_out(ctx, i, "C%d.Y = %f\n",
01991                                      c, int_as_float(data[i]));
01992                             i++;
01993                             instr_out(ctx, i, "C%d.Z = %f\n",
01994                                      c, int_as_float(data[i]));
01995                             i++;
01996                             instr_out(ctx, i, "C%d.W = %f\n",
01997                                      c, int_as_float(data[i]));
01998                             i++;
01999                      }
02000               }
02001               if (len != i) {
02002                      fprintf(out,
02003                             "Bad count in 3DSTATE_PIXEL_SHADER_CONSTANTS\n");
02004               }
02005               return len;
02006        case 0x05:
02007               instr_out(ctx, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
02008               len = (data[0] & 0x000000ff) + 2;
02009               if ((len - 1) % 3 != 0 || len > 370) {
02010                      fprintf(out,
02011                             "Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
02012               }
02013               i = 1;
02014               for (instr = 0; instr < (len - 1) / 3; instr++) {
02015                      char instr_prefix[10];
02016 
02017                      sprintf(instr_prefix, "PS%03d", instr);
02018                      i915_decode_instruction(ctx, i,
02019                                           instr_prefix);
02020                      i += 3;
02021               }
02022               return len;
02023        case 0x01:
02024               if (IS_GEN2(devid))
02025                      break;
02026               instr_out(ctx, 0, "3DSTATE_SAMPLER_STATE\n");
02027               instr_out(ctx, 1, "mask\n");
02028               len = (data[0] & 0x0000003f) + 2;
02029               i = 2;
02030               for (sampler = 0; sampler <= 15; sampler++) {
02031                      if (data[1] & (1 << sampler)) {
02032                             uint32_t dword;
02033                             const char *mip_filter = "";
02034 
02035                             dword = data[i];
02036                             switch ((dword >> 20) & 0x3) {
02037                             case 0:
02038                                    mip_filter = "none";
02039                                    break;
02040                             case 1:
02041                                    mip_filter = "nearest";
02042                                    break;
02043                             case 3:
02044                                    mip_filter = "linear";
02045                                    break;
02046                             }
02047                             instr_out(ctx, i++,
02048                                      "sampler %d SS2:%s%s%s "
02049                                      "base_mip_level=%i, mip_filter=%s, mag_filter=%s, min_filter=%s "
02050                                      "lod_bias=%.2f,%s max_aniso=%i, shadow_func=%s\n",
02051                                      sampler,
02052                                      dword & (1 << 31) ? " reverse gamma,"
02053                                      : "",
02054                                      dword & (1 << 30) ? " packed2planar,"
02055                                      : "",
02056                                      dword & (1 << 29) ?
02057                                      " colorspace conversion," : "",
02058                                      (dword >> 22) & 0x1f, mip_filter,
02059                                      decode_sample_filter(dword >> 17),
02060                                      decode_sample_filter(dword >> 14),
02061                                      ((dword >> 5) & 0x1ff) / (0x10 * 1.0),
02062                                      dword & (1 << 4) ? " shadow," : "",
02063                                      dword & (1 << 3) ? 4 : 2,
02064                                      decode_compare_func(dword));
02065                             dword = data[i];
02066                             instr_out(ctx, i++,
02067                                      "sampler %d SS3: min_lod=%.2f,%s "
02068                                      "tcmode_x=%s, tcmode_y=%s, tcmode_z=%s,%s texmap_idx=%i,%s\n",
02069                                      sampler,
02070                                      ((dword >> 24) & 0xff) / (0x10 * 1.0),
02071                                      dword & (1 << 17) ?
02072                                      " kill pixel enable," : "",
02073                                      decode_tex_coord_mode(dword >> 12),
02074                                      decode_tex_coord_mode(dword >> 9),
02075                                      decode_tex_coord_mode(dword >> 6),
02076                                      dword & (1 << 5) ?
02077                                      " normalized coords," : "",
02078                                      (dword >> 1) & 0xf,
02079                                      dword & (1 << 0) ? " deinterlacer," :
02080                                      "");
02081                             dword = data[i];
02082                             instr_out(ctx, i++,
02083                                      "sampler %d SS4: border color\n",
02084                                      sampler);
02085                      }
02086               }
02087               if (len != i) {
02088                      fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
02089               }
02090               return len;
02091        case 0x85:
02092               len = (data[0] & 0x0000000f) + 2;
02093 
02094               if (len != 2)
02095                      fprintf(out,
02096                             "Bad count in 3DSTATE_DEST_BUFFER_VARIABLES\n");
02097 
02098               instr_out(ctx, 0,
02099                        "3DSTATE_DEST_BUFFER_VARIABLES\n");
02100 
02101               switch ((data[1] >> 8) & 0xf) {
02102               case 0x0:
02103                      format = "g8";
02104                      break;
02105               case 0x1:
02106                      format = "x1r5g5b5";
02107                      break;
02108               case 0x2:
02109                      format = "r5g6b5";
02110                      break;
02111               case 0x3:
02112                      format = "a8r8g8b8";
02113                      break;
02114               case 0x4:
02115                      format = "ycrcb_swapy";
02116                      break;
02117               case 0x5:
02118                      format = "ycrcb_normal";
02119                      break;
02120               case 0x6:
02121                      format = "ycrcb_swapuv";
02122                      break;
02123               case 0x7:
02124                      format = "ycrcb_swapuvy";
02125                      break;
02126               case 0x8:
02127                      format = "a4r4g4b4";
02128                      break;
02129               case 0x9:
02130                      format = "a1r5g5b5";
02131                      break;
02132               case 0xa:
02133                      format = "a2r10g10b10";
02134                      break;
02135               default:
02136                      format = "BAD";
02137                      break;
02138               }
02139               switch ((data[1] >> 2) & 0x3) {
02140               case 0x0:
02141                      zformat = "u16";
02142                      break;
02143               case 0x1:
02144                      zformat = "f16";
02145                      break;
02146               case 0x2:
02147                      zformat = "u24x8";
02148                      break;
02149               default:
02150                      zformat = "BAD";
02151                      break;
02152               }
02153               instr_out(ctx, 1,
02154                        "%s format, %s depth format, early Z %sabled\n",
02155                        format, zformat,
02156                        (data[1] & (1 << 31)) ? "en" : "dis");
02157               return len;
02158 
02159        case 0x8e:
02160               {
02161                      const char *name, *tiling;
02162 
02163                      len = (data[0] & 0x0000000f) + 2;
02164                      if (len != 3)
02165                             fprintf(out,
02166                                    "Bad count in 3DSTATE_BUFFER_INFO\n");
02167 
02168                      switch ((data[1] >> 24) & 0x7) {
02169                      case 0x3:
02170                             name = "color";
02171                             break;
02172                      case 0x7:
02173                             name = "depth";
02174                             break;
02175                      default:
02176                             name = "unknown";
02177                             break;
02178                      }
02179 
02180                      tiling = "none";
02181                      if (data[1] & (1 << 23))
02182                             tiling = "fenced";
02183                      else if (data[1] & (1 << 22))
02184                             tiling = data[1] & (1 << 21) ? "Y" : "X";
02185 
02186                      instr_out(ctx, 0, "3DSTATE_BUFFER_INFO\n");
02187                      instr_out(ctx, 1,
02188                               "%s, tiling = %s, pitch=%d\n", name, tiling,
02189                               data[1] & 0xffff);
02190 
02191                      instr_out(ctx, 2, "address\n");
02192                      return len;
02193               }
02194        case 0x81:
02195               len = (data[0] & 0x0000000f) + 2;
02196 
02197               if (len != 3)
02198                      fprintf(out,
02199                             "Bad count in 3DSTATE_SCISSOR_RECTANGLE\n");
02200 
02201               instr_out(ctx, 0, "3DSTATE_SCISSOR_RECTANGLE\n");
02202               instr_out(ctx, 1, "(%d,%d)\n",
02203                        data[1] & 0xffff, data[1] >> 16);
02204               instr_out(ctx, 2, "(%d,%d)\n",
02205                        data[2] & 0xffff, data[2] >> 16);
02206 
02207               return len;
02208        case 0x80:
02209               len = (data[0] & 0x0000000f) + 2;
02210 
02211               if (len != 5)
02212                      fprintf(out,
02213                             "Bad count in 3DSTATE_DRAWING_RECTANGLE\n");
02214 
02215               instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
02216               instr_out(ctx, 1, "%s\n",
02217                        data[1] & (1 << 30) ? "depth ofs disabled " : "");
02218               instr_out(ctx, 2, "(%d,%d)\n",
02219                        data[2] & 0xffff, data[2] >> 16);
02220               instr_out(ctx, 3, "(%d,%d)\n",
02221                        data[3] & 0xffff, data[3] >> 16);
02222               instr_out(ctx, 4, "(%d,%d)\n",
02223                        data[4] & 0xffff, data[4] >> 16);
02224 
02225               return len;
02226        case 0x9c:
02227               len = (data[0] & 0x0000000f) + 2;
02228 
02229               if (len != 7)
02230                      fprintf(out, "Bad count in 3DSTATE_CLEAR_PARAMETERS\n");
02231 
02232               instr_out(ctx, 0, "3DSTATE_CLEAR_PARAMETERS\n");
02233               instr_out(ctx, 1, "prim_type=%s, clear=%s%s%s\n",
02234                        data[1] & (1 << 16) ? "CLEAR_RECT" : "ZONE_INIT",
02235                        data[1] & (1 << 2) ? "color," : "",
02236                        data[1] & (1 << 1) ? "depth," : "",
02237                        data[1] & (1 << 0) ? "stencil," : "");
02238               instr_out(ctx, 2, "clear color\n");
02239               instr_out(ctx, 3, "clear depth/stencil\n");
02240               instr_out(ctx, 4, "color value (rgba8888)\n");
02241               instr_out(ctx, 5, "depth value %f\n",
02242                        int_as_float(data[5]));
02243               instr_out(ctx, 6, "clear stencil\n");
02244               return len;
02245        }
02246 
02247        for (idx = 0; idx < ARRAY_SIZE(opcodes_3d_1d); idx++) {
02248               opcode_3d_1d = &opcodes_3d_1d[idx];
02249               if (opcode_3d_1d->i830_only && !IS_GEN2(devid))
02250                      continue;
02251 
02252               if (((data[0] & 0x00ff0000) >> 16) == opcode_3d_1d->opcode) {
02253                      len = 1;
02254 
02255                      instr_out(ctx, 0, "%s\n",
02256                               opcode_3d_1d->name);
02257                      if (opcode_3d_1d->max_len > 1) {
02258                             len = (data[0] & 0x0000ffff) + 2;
02259                             if (len < opcode_3d_1d->min_len ||
02260                                 len > opcode_3d_1d->max_len) {
02261                                    fprintf(out, "Bad count in %s\n",
02262                                           opcode_3d_1d->name);
02263                             }
02264                      }
02265 
02266                      for (i = 1; i < len; i++) {
02267                             instr_out(ctx, i, "dword %d\n", i);
02268                      }
02269 
02270                      return len;
02271               }
02272        }
02273 
02274        instr_out(ctx, 0, "3D UNKNOWN: 3d_1d opcode = 0x%x\n",
02275                 opcode);
02276        return 1;
02277 }
02278 
02279 static int
02280 decode_3d_primitive(struct drm_intel_decode *ctx)
02281 {
02282        uint32_t *data = ctx->data;
02283        uint32_t count = ctx->count;
02284        char immediate = (data[0] & (1 << 23)) == 0;
02285        unsigned int len, i, j, ret;
02286        const char *primtype;
02287        int original_s2 = saved_s2;
02288        int original_s4 = saved_s4;
02289 
02290        switch ((data[0] >> 18) & 0xf) {
02291        case 0x0:
02292               primtype = "TRILIST";
02293               break;
02294        case 0x1:
02295               primtype = "TRISTRIP";
02296               break;
02297        case 0x2:
02298               primtype = "TRISTRIP_REVERSE";
02299               break;
02300        case 0x3:
02301               primtype = "TRIFAN";
02302               break;
02303        case 0x4:
02304               primtype = "POLYGON";
02305               break;
02306        case 0x5:
02307               primtype = "LINELIST";
02308               break;
02309        case 0x6:
02310               primtype = "LINESTRIP";
02311               break;
02312        case 0x7:
02313               primtype = "RECTLIST";
02314               break;
02315        case 0x8:
02316               primtype = "POINTLIST";
02317               break;
02318        case 0x9:
02319               primtype = "DIB";
02320               break;
02321        case 0xa:
02322               primtype = "CLEAR_RECT";
02323               saved_s4 = 3 << 6;
02324               saved_s2 = ~0;
02325               break;
02326        default:
02327               primtype = "unknown";
02328               break;
02329        }
02330 
02331        /* XXX: 3DPRIM_DIB not supported */
02332        if (immediate) {
02333               len = (data[0] & 0x0003ffff) + 2;
02334               instr_out(ctx, 0, "3DPRIMITIVE inline %s\n",
02335                        primtype);
02336               if (count < len)
02337                      BUFFER_FAIL(count, len, "3DPRIMITIVE inline");
02338               if (!saved_s2_set || !saved_s4_set) {
02339                      fprintf(out, "unknown vertex format\n");
02340                      for (i = 1; i < len; i++) {
02341                             instr_out(ctx, i,
02342                                      "           vertex data (%f float)\n",
02343                                      int_as_float(data[i]));
02344                      }
02345               } else {
02346                      unsigned int vertex = 0;
02347                      for (i = 1; i < len;) {
02348                             unsigned int tc;
02349 
02350 #define VERTEX_OUT(fmt, ...) do {                              \
02351     if (i < len)                                               \
02352        instr_out(ctx, i, " V%d."fmt"\n", vertex, __VA_ARGS__); \
02353     else                                                       \
02354        fprintf(out, " missing data in V%d\n", vertex);                \
02355     i++;                                                       \
02356 } while (0)
02357 
02358                             VERTEX_OUT("X = %f", int_as_float(data[i]));
02359                             VERTEX_OUT("Y = %f", int_as_float(data[i]));
02360                             switch (saved_s4 >> 6 & 0x7) {
02361                             case 0x1:
02362                                    VERTEX_OUT("Z = %f",
02363                                              int_as_float(data[i]));
02364                                    break;
02365                             case 0x2:
02366                                    VERTEX_OUT("Z = %f",
02367                                              int_as_float(data[i]));
02368                                    VERTEX_OUT("W = %f",
02369                                              int_as_float(data[i]));
02370                                    break;
02371                             case 0x3:
02372                                    break;
02373                             case 0x4:
02374                                    VERTEX_OUT("W = %f",
02375                                              int_as_float(data[i]));
02376                                    break;
02377                             default:
02378                                    fprintf(out, "bad S4 position mask\n");
02379                             }
02380 
02381                             if (saved_s4 & (1 << 10)) {
02382                                    VERTEX_OUT
02383                                        ("color = (A=0x%02x, R=0x%02x, G=0x%02x, "
02384                                         "B=0x%02x)", data[i] >> 24,
02385                                         (data[i] >> 16) & 0xff,
02386                                         (data[i] >> 8) & 0xff,
02387                                         data[i] & 0xff);
02388                             }
02389                             if (saved_s4 & (1 << 11)) {
02390                                    VERTEX_OUT
02391                                        ("spec = (A=0x%02x, R=0x%02x, G=0x%02x, "
02392                                         "B=0x%02x)", data[i] >> 24,
02393                                         (data[i] >> 16) & 0xff,
02394                                         (data[i] >> 8) & 0xff,
02395                                         data[i] & 0xff);
02396                             }
02397                             if (saved_s4 & (1 << 12))
02398                                    VERTEX_OUT("width = 0x%08x)", data[i]);
02399 
02400                             for (tc = 0; tc <= 7; tc++) {
02401                                    switch ((saved_s2 >> (tc * 4)) & 0xf) {
02402                                    case 0x0:
02403                                           VERTEX_OUT("T%d.X = %f", tc,
02404                                                     int_as_float(data
02405                                                                [i]));
02406                                           VERTEX_OUT("T%d.Y = %f", tc,
02407                                                     int_as_float(data
02408                                                                [i]));
02409                                           break;
02410                                    case 0x1:
02411                                           VERTEX_OUT("T%d.X = %f", tc,
02412                                                     int_as_float(data
02413                                                                [i]));
02414                                           VERTEX_OUT("T%d.Y = %f", tc,
02415                                                     int_as_float(data
02416                                                                [i]));
02417                                           VERTEX_OUT("T%d.Z = %f", tc,
02418                                                     int_as_float(data
02419                                                                [i]));
02420                                           break;
02421                                    case 0x2:
02422                                           VERTEX_OUT("T%d.X = %f", tc,
02423                                                     int_as_float(data
02424                                                                [i]));
02425                                           VERTEX_OUT("T%d.Y = %f", tc,
02426                                                     int_as_float(data
02427                                                                [i]));
02428                                           VERTEX_OUT("T%d.Z = %f", tc,
02429                                                     int_as_float(data
02430                                                                [i]));
02431                                           VERTEX_OUT("T%d.W = %f", tc,
02432                                                     int_as_float(data
02433                                                                [i]));
02434                                           break;
02435                                    case 0x3:
02436                                           VERTEX_OUT("T%d.X = %f", tc,
02437                                                     int_as_float(data
02438                                                                [i]));
02439                                           break;
02440                                    case 0x4:
02441                                           VERTEX_OUT
02442                                               ("T%d.XY = 0x%08x half-float",
02443                                                tc, data[i]);
02444                                           break;
02445                                    case 0x5:
02446                                           VERTEX_OUT
02447                                               ("T%d.XY = 0x%08x half-float",
02448                                                tc, data[i]);
02449                                           VERTEX_OUT
02450                                               ("T%d.ZW = 0x%08x half-float",
02451                                                tc, data[i]);
02452                                           break;
02453                                    case 0xf:
02454                                           break;
02455                                    default:
02456                                           fprintf(out,
02457                                                  "bad S2.T%d format\n",
02458                                                  tc);
02459                                    }
02460                             }
02461                             vertex++;
02462                      }
02463               }
02464 
02465               ret = len;
02466        } else {
02467               /* indirect vertices */
02468               len = data[0] & 0x0000ffff; /* index count */
02469               if (data[0] & (1 << 17)) {
02470                      /* random vertex access */
02471                      if (count < (len + 1) / 2 + 1) {
02472                             BUFFER_FAIL(count, (len + 1) / 2 + 1,
02473                                        "3DPRIMITIVE random indirect");
02474                      }
02475                      instr_out(ctx, 0,
02476                               "3DPRIMITIVE random indirect %s (%d)\n",
02477                               primtype, len);
02478                      if (len == 0) {
02479                             /* vertex indices continue until 0xffff is
02480                              * found
02481                              */
02482                             for (i = 1; i < count; i++) {
02483                                    if ((data[i] & 0xffff) == 0xffff) {
02484                                           instr_out(ctx, i,
02485                                                    "    indices: (terminator)\n");
02486                                           ret = i;
02487                                           goto out;
02488                                    } else if ((data[i] >> 16) == 0xffff) {
02489                                           instr_out(ctx, i,
02490                                                    "    indices: 0x%04x, (terminator)\n",
02491                                                    data[i] & 0xffff);
02492                                           ret = i;
02493                                           goto out;
02494                                    } else {
02495                                           instr_out(ctx, i,
02496                                                    "    indices: 0x%04x, 0x%04x\n",
02497                                                    data[i] & 0xffff,
02498                                                    data[i] >> 16);
02499                                    }
02500                             }
02501                             fprintf(out,
02502                                    "3DPRIMITIVE: no terminator found in index buffer\n");
02503                             ret = count;
02504                             goto out;
02505                      } else {
02506                             /* fixed size vertex index buffer */
02507                             for (j = 1, i = 0; i < len; i += 2, j++) {
02508                                    if (i * 2 == len - 1) {
02509                                           instr_out(ctx, j,
02510                                                    "    indices: 0x%04x\n",
02511                                                    data[j] & 0xffff);
02512                                    } else {
02513                                           instr_out(ctx, j,
02514                                                    "    indices: 0x%04x, 0x%04x\n",
02515                                                    data[j] & 0xffff,
02516                                                    data[j] >> 16);
02517                                    }
02518                             }
02519                      }
02520                      ret = (len + 1) / 2 + 1;
02521                      goto out;
02522               } else {
02523                      /* sequential vertex access */
02524                      instr_out(ctx, 0,
02525                               "3DPRIMITIVE sequential indirect %s, %d starting from "
02526                               "%d\n", primtype, len, data[1] & 0xffff);
02527                      instr_out(ctx, 1, "           start\n");
02528                      ret = 2;
02529                      goto out;
02530               }
02531        }
02532 
02533 out:
02534        saved_s2 = original_s2;
02535        saved_s4 = original_s4;
02536        return ret;
02537 }
02538 
02539 static int
02540 decode_3d(struct drm_intel_decode *ctx)
02541 {
02542        uint32_t opcode;
02543        unsigned int idx;
02544        uint32_t *data = ctx->data;
02545 
02546        struct {
02547               uint32_t opcode;
02548               unsigned int min_len;
02549               unsigned int max_len;
02550               const char *name;
02551        } opcodes_3d[] = {
02552               { 0x06, 1, 1, "3DSTATE_ANTI_ALIASING" },
02553               { 0x08, 1, 1, "3DSTATE_BACKFACE_STENCIL_OPS" },
02554               { 0x09, 1, 1, "3DSTATE_BACKFACE_STENCIL_MASKS" },
02555               { 0x16, 1, 1, "3DSTATE_COORD_SET_BINDINGS" },
02556               { 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
02557               { 0x0b, 1, 1, "3DSTATE_INDEPENDENT_ALPHA_BLEND" },
02558               { 0x0d, 1, 1, "3DSTATE_MODES_4" },
02559               { 0x0c, 1, 1, "3DSTATE_MODES_5" },
02560               { 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES"},
02561        }, *opcode_3d;
02562 
02563        opcode = (data[0] & 0x1f000000) >> 24;
02564 
02565        switch (opcode) {
02566        case 0x1f:
02567               return decode_3d_primitive(ctx);
02568        case 0x1d:
02569               return decode_3d_1d(ctx);
02570        case 0x1c:
02571               return decode_3d_1c(ctx);
02572        }
02573 
02574        for (idx = 0; idx < ARRAY_SIZE(opcodes_3d); idx++) {
02575               opcode_3d = &opcodes_3d[idx];
02576               if (opcode == opcode_3d->opcode) {
02577                      unsigned int len = 1, i;
02578 
02579                      instr_out(ctx, 0, "%s\n", opcode_3d->name);
02580                      if (opcode_3d->max_len > 1) {
02581                             len = (data[0] & 0xff) + 2;
02582                             if (len < opcode_3d->min_len ||
02583                                 len > opcode_3d->max_len) {
02584                                    fprintf(out, "Bad count in %s\n",
02585                                           opcode_3d->name);
02586                             }
02587                      }
02588 
02589                      for (i = 1; i < len; i++) {
02590                             instr_out(ctx, i, "dword %d\n", i);
02591                      }
02592                      return len;
02593               }
02594        }
02595 
02596        instr_out(ctx, 0, "3D UNKNOWN: 3d opcode = 0x%x\n", opcode);
02597        return 1;
02598 }
02599 
02600 static const char *get_965_surfacetype(unsigned int surfacetype)
02601 {
02602        switch (surfacetype) {
02603        case 0:
02604               return "1D";
02605        case 1:
02606               return "2D";
02607        case 2:
02608               return "3D";
02609        case 3:
02610               return "CUBE";
02611        case 4:
02612               return "BUFFER";
02613        case 7:
02614               return "NULL";
02615        default:
02616               return "unknown";
02617        }
02618 }
02619 
02620 static const char *get_965_depthformat(unsigned int depthformat)
02621 {
02622        switch (depthformat) {
02623        case 0:
02624               return "s8_z24float";
02625        case 1:
02626               return "z32float";
02627        case 2:
02628               return "z24s8";
02629        case 5:
02630               return "z16";
02631        default:
02632               return "unknown";
02633        }
02634 }
02635 
02636 static const char *get_965_element_component(uint32_t data, int component)
02637 {
02638        uint32_t component_control = (data >> (16 + (3 - component) * 4)) & 0x7;
02639 
02640        switch (component_control) {
02641        case 0:
02642               return "nostore";
02643        case 1:
02644               switch (component) {
02645               case 0:
02646                      return "X";
02647               case 1:
02648                      return "Y";
02649               case 2:
02650                      return "Z";
02651               case 3:
02652                      return "W";
02653               default:
02654                      return "fail";
02655               }
02656        case 2:
02657               return "0.0";
02658        case 3:
02659               return "1.0";
02660        case 4:
02661               return "0x1";
02662        case 5:
02663               return "VID";
02664        default:
02665               return "fail";
02666        }
02667 }
02668 
02669 static const char *get_965_prim_type(uint32_t primtype)
02670 {
02671        switch (primtype) {
02672        case 0x01:
02673               return "point list";
02674        case 0x02:
02675               return "line list";
02676        case 0x03:
02677               return "line strip";
02678        case 0x04:
02679               return "tri list";
02680        case 0x05:
02681               return "tri strip";
02682        case 0x06:
02683               return "tri fan";
02684        case 0x07:
02685               return "quad list";
02686        case 0x08:
02687               return "quad strip";
02688        case 0x09:
02689               return "line list adj";
02690        case 0x0a:
02691               return "line strip adj";
02692        case 0x0b:
02693               return "tri list adj";
02694        case 0x0c:
02695               return "tri strip adj";
02696        case 0x0d:
02697               return "tri strip reverse";
02698        case 0x0e:
02699               return "polygon";
02700        case 0x0f:
02701               return "rect list";
02702        case 0x10:
02703               return "line loop";
02704        case 0x11:
02705               return "point list bf";
02706        case 0x12:
02707               return "line strip cont";
02708        case 0x13:
02709               return "line strip bf";
02710        case 0x14:
02711               return "line strip cont bf";
02712        case 0x15:
02713               return "tri fan no stipple";
02714        default:
02715               return "fail";
02716        }
02717 }
02718 
02719 static int
02720 i965_decode_urb_fence(struct drm_intel_decode *ctx, int len)
02721 {
02722        uint32_t vs_fence, clip_fence, gs_fence, sf_fence, vfe_fence, cs_fence;
02723        uint32_t *data = ctx->data;
02724 
02725        if (len != 3)
02726               fprintf(out, "Bad count in URB_FENCE\n");
02727 
02728        vs_fence = data[1] & 0x3ff;
02729        gs_fence = (data[1] >> 10) & 0x3ff;
02730        clip_fence = (data[1] >> 20) & 0x3ff;
02731        sf_fence = data[2] & 0x3ff;
02732        vfe_fence = (data[2] >> 10) & 0x3ff;
02733        cs_fence = (data[2] >> 20) & 0x7ff;
02734 
02735        instr_out(ctx, 0, "URB_FENCE: %s%s%s%s%s%s\n",
02736                 (data[0] >> 13) & 1 ? "cs " : "",
02737                 (data[0] >> 12) & 1 ? "vfe " : "",
02738                 (data[0] >> 11) & 1 ? "sf " : "",
02739                 (data[0] >> 10) & 1 ? "clip " : "",
02740                 (data[0] >> 9) & 1 ? "gs " : "",
02741                 (data[0] >> 8) & 1 ? "vs " : "");
02742        instr_out(ctx, 1,
02743                 "vs fence: %d, clip_fence: %d, gs_fence: %d\n",
02744                 vs_fence, clip_fence, gs_fence);
02745        instr_out(ctx, 2,
02746                 "sf fence: %d, vfe_fence: %d, cs_fence: %d\n",
02747                 sf_fence, vfe_fence, cs_fence);
02748        if (gs_fence < vs_fence)
02749               fprintf(out, "gs fence < vs fence!\n");
02750        if (clip_fence < gs_fence)
02751               fprintf(out, "clip fence < gs fence!\n");
02752        if (sf_fence < clip_fence)
02753               fprintf(out, "sf fence < clip fence!\n");
02754        if (cs_fence < sf_fence)
02755               fprintf(out, "cs fence < sf fence!\n");
02756 
02757        return len;
02758 }
02759 
02760 static void
02761 state_base_out(struct drm_intel_decode *ctx, unsigned int index,
02762               const char *name)
02763 {
02764        if (ctx->data[index] & 1) {
02765               instr_out(ctx, index,
02766                        "%s state base address 0x%08x\n", name,
02767                        ctx->data[index] & ~1);
02768        } else {
02769               instr_out(ctx, index, "%s state base not updated\n",
02770                        name);
02771        }
02772 }
02773 
02774 static void
02775 state_max_out(struct drm_intel_decode *ctx, unsigned int index,
02776              const char *name)
02777 {
02778        if (ctx->data[index] & 1) {
02779               if (ctx->data[index] == 1) {
02780                      instr_out(ctx, index,
02781                               "%s state upper bound disabled\n", name);
02782               } else {
02783                      instr_out(ctx, index,
02784                               "%s state upper bound 0x%08x\n", name,
02785                               ctx->data[index] & ~1);
02786               }
02787        } else {
02788               instr_out(ctx, index,
02789                        "%s state upper bound not updated\n", name);
02790        }
02791 }
02792 
02793 static int
02794 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(struct drm_intel_decode *ctx)
02795 {
02796        instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_CC\n");
02797        instr_out(ctx, 1, "pointer to CC viewport\n");
02798 
02799        return 2;
02800 }
02801 
02802 static int
02803 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(struct drm_intel_decode *ctx)
02804 {
02805        instr_out(ctx, 0, "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP\n");
02806        instr_out(ctx, 1, "pointer to SF_CLIP viewport\n");
02807 
02808        return 2;
02809 }
02810 
02811 static int
02812 gen7_3DSTATE_BLEND_STATE_POINTERS(struct drm_intel_decode *ctx)
02813 {
02814        instr_out(ctx, 0, "3DSTATE_BLEND_STATE_POINTERS\n");
02815        instr_out(ctx, 1, "pointer to BLEND_STATE at 0x%08x (%s)\n",
02816                 ctx->data[1] & ~1,
02817                 (ctx->data[1] & 1) ? "changed" : "unchanged");
02818 
02819        return 2;
02820 }
02821 
02822 static int
02823 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(struct drm_intel_decode *ctx)
02824 {
02825        instr_out(ctx, 0, "3DSTATE_DEPTH_STENCIL_STATE_POINTERS\n");
02826        instr_out(ctx, 1,
02827                 "pointer to DEPTH_STENCIL_STATE at 0x%08x (%s)\n",
02828                 ctx->data[1] & ~1,
02829                 (ctx->data[1] & 1) ? "changed" : "unchanged");
02830 
02831        return 2;
02832 }
02833 
02834 static int
02835 gen7_3DSTATE_HIER_DEPTH_BUFFER(struct drm_intel_decode *ctx)
02836 {
02837        instr_out(ctx, 0, "3DSTATE_HIER_DEPTH_BUFFER\n");
02838        instr_out(ctx, 1, "pitch %db\n",
02839                 (ctx->data[1] & 0x1ffff) + 1);
02840        instr_out(ctx, 2, "pointer to HiZ buffer\n");
02841 
02842        return 3;
02843 }
02844 
02845 static int
02846 gen6_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
02847 {
02848        instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
02849        instr_out(ctx, 1, "blend change %d\n", ctx->data[1] & 1);
02850        instr_out(ctx, 2, "depth stencil change %d\n",
02851                 ctx->data[2] & 1);
02852        instr_out(ctx, 3, "cc change %d\n", ctx->data[3] & 1);
02853 
02854        return 4;
02855 }
02856 
02857 static int
02858 gen7_3DSTATE_CC_STATE_POINTERS(struct drm_intel_decode *ctx)
02859 {
02860        instr_out(ctx, 0, "3DSTATE_CC_STATE_POINTERS\n");
02861        instr_out(ctx, 1, "pointer to COLOR_CALC_STATE at 0x%08x "
02862                 "(%s)\n",
02863                 ctx->data[1] & ~1,
02864                 (ctx->data[1] & 1) ? "changed" : "unchanged");
02865 
02866        return 2;
02867 }
02868 
02869 static int
02870 gen7_3DSTATE_URB_unit(struct drm_intel_decode *ctx, const char *unit)
02871 {
02872     int start_kb = ((ctx->data[1] >> 25) & 0x3f) * 8;
02873     /* the field is # of 512-bit rows - 1, we print bytes */
02874     int entry_size = (((ctx->data[1] >> 16) & 0x1ff) + 1);
02875     int nr_entries = ctx->data[1] & 0xffff;
02876 
02877     instr_out(ctx, 0, "3DSTATE_URB_%s\n", unit);
02878     instr_out(ctx, 1,
02879              "%dKB start, size=%d 64B rows, nr_entries=%d, total size %dB\n",
02880              start_kb, entry_size, nr_entries, nr_entries * 64 * entry_size);
02881 
02882     return 2;
02883 }
02884 
02885 static int
02886 gen7_3DSTATE_URB_VS(struct drm_intel_decode *ctx)
02887 {
02888        return gen7_3DSTATE_URB_unit(ctx, "VS");
02889 }
02890 
02891 static int
02892 gen7_3DSTATE_URB_HS(struct drm_intel_decode *ctx)
02893 {
02894        return gen7_3DSTATE_URB_unit(ctx, "HS");
02895 }
02896 
02897 static int
02898 gen7_3DSTATE_URB_DS(struct drm_intel_decode *ctx)
02899 {
02900        return gen7_3DSTATE_URB_unit(ctx, "DS");
02901 }
02902 
02903 static int
02904 gen7_3DSTATE_URB_GS(struct drm_intel_decode *ctx)
02905 {
02906        return gen7_3DSTATE_URB_unit(ctx, "GS");
02907 }
02908 
02909 static int
02910 gen7_3DSTATE_CONSTANT(struct drm_intel_decode *ctx, const char *unit)
02911 {
02912        int rlen[4];
02913 
02914        rlen[0] = (ctx->data[1] >> 0) & 0xffff;
02915        rlen[1] = (ctx->data[1] >> 16) & 0xffff;
02916        rlen[2] = (ctx->data[2] >> 0) & 0xffff;
02917        rlen[3] = (ctx->data[2] >> 16) & 0xffff;
02918 
02919        instr_out(ctx, 0, "3DSTATE_CONSTANT_%s\n", unit);
02920        instr_out(ctx, 1, "len 0 = %d, len 1 = %d\n", rlen[0], rlen[1]);
02921        instr_out(ctx, 2, "len 2 = %d, len 3 = %d\n", rlen[2], rlen[3]);
02922        instr_out(ctx, 3, "pointer to constbuf 0\n");
02923        instr_out(ctx, 4, "pointer to constbuf 1\n");
02924        instr_out(ctx, 5, "pointer to constbuf 2\n");
02925        instr_out(ctx, 6, "pointer to constbuf 3\n");
02926 
02927        return 7;
02928 }
02929 
02930 static int
02931 gen7_3DSTATE_CONSTANT_VS(struct drm_intel_decode *ctx)
02932 {
02933        return gen7_3DSTATE_CONSTANT(ctx, "VS");
02934 }
02935 
02936 static int
02937 gen7_3DSTATE_CONSTANT_GS(struct drm_intel_decode *ctx)
02938 {
02939        return gen7_3DSTATE_CONSTANT(ctx, "GS");
02940 }
02941 
02942 static int
02943 gen7_3DSTATE_CONSTANT_PS(struct drm_intel_decode *ctx)
02944 {
02945        return gen7_3DSTATE_CONSTANT(ctx, "PS");
02946 }
02947 
02948 static int
02949 gen7_3DSTATE_CONSTANT_DS(struct drm_intel_decode *ctx)
02950 {
02951        return gen7_3DSTATE_CONSTANT(ctx, "DS");
02952 }
02953 
02954 static int
02955 gen7_3DSTATE_CONSTANT_HS(struct drm_intel_decode *ctx)
02956 {
02957        return gen7_3DSTATE_CONSTANT(ctx, "HS");
02958 }
02959 
02960 
02961 static int
02962 gen6_3DSTATE_WM(struct drm_intel_decode *ctx)
02963 {
02964        instr_out(ctx, 0, "3DSTATE_WM\n");
02965        instr_out(ctx, 1, "kernel start pointer 0\n");
02966        instr_out(ctx, 2,
02967                 "SPF=%d, VME=%d, Sampler Count %d, "
02968                 "Binding table count %d\n",
02969                 (ctx->data[2] >> 31) & 1,
02970                 (ctx->data[2] >> 30) & 1,
02971                 (ctx->data[2] >> 27) & 7,
02972                 (ctx->data[2] >> 18) & 0xff);
02973        instr_out(ctx, 3, "scratch offset\n");
02974        instr_out(ctx, 4,
02975                 "Depth Clear %d, Depth Resolve %d, HiZ Resolve %d, "
02976                 "Dispatch GRF start[0] %d, start[1] %d, start[2] %d\n",
02977                 (ctx->data[4] & (1 << 30)) != 0,
02978                 (ctx->data[4] & (1 << 28)) != 0,
02979                 (ctx->data[4] & (1 << 27)) != 0,
02980                 (ctx->data[4] >> 16) & 0x7f,
02981                 (ctx->data[4] >> 8) & 0x7f,
02982                 (ctx->data[4] & 0x7f));
02983        instr_out(ctx, 5,
02984                 "MaxThreads %d, PS KillPixel %d, PS computed Z %d, "
02985                 "PS use sourceZ %d, Thread Dispatch %d, PS use sourceW %d, "
02986                 "Dispatch32 %d, Dispatch16 %d, Dispatch8 %d\n",
02987                 ((ctx->data[5] >> 25) & 0x7f) + 1,
02988                 (ctx->data[5] & (1 << 22)) != 0,
02989                 (ctx->data[5] & (1 << 21)) != 0,
02990                 (ctx->data[5] & (1 << 20)) != 0,
02991                 (ctx->data[5] & (1 << 19)) != 0,
02992                 (ctx->data[5] & (1 << 8)) != 0,
02993                 (ctx->data[5] & (1 << 2)) != 0,
02994                 (ctx->data[5] & (1 << 1)) != 0,
02995                 (ctx->data[5] & (1 << 0)) != 0);
02996        instr_out(ctx, 6,
02997                 "Num SF output %d, Pos XY offset %d, ZW interp mode %d , "
02998                 "Barycentric interp mode 0x%x, Point raster rule %d, "
02999                 "Multisample mode %d, "
03000                 "Multisample Dispatch mode %d\n",
03001                 (ctx->data[6] >> 20) & 0x3f,
03002                 (ctx->data[6] >> 18) & 3,
03003                 (ctx->data[6] >> 16) & 3,
03004                 (ctx->data[6] >> 10) & 0x3f,
03005                 (ctx->data[6] & (1 << 9)) != 0,
03006                 (ctx->data[6] >> 1) & 3,
03007                 (ctx->data[6] & 1));
03008        instr_out(ctx, 7, "kernel start pointer 1\n");
03009        instr_out(ctx, 8, "kernel start pointer 2\n");
03010 
03011        return 9;
03012 }
03013 
03014 static int
03015 gen7_3DSTATE_WM(struct drm_intel_decode *ctx)
03016 {
03017        const char *computed_depth = "";
03018        const char *early_depth = "";
03019        const char *zw_interp = "";
03020 
03021        switch ((ctx->data[1] >> 23) & 0x3) {
03022        case 0:
03023               computed_depth = "";
03024               break;
03025        case 1:
03026               computed_depth = "computed depth";
03027               break;
03028        case 2:
03029               computed_depth = "computed depth >=";
03030               break;
03031        case 3:
03032               computed_depth = "computed depth <=";
03033               break;
03034        }
03035 
03036        switch ((ctx->data[1] >> 21) & 0x3) {
03037        case 0:
03038               early_depth = "";
03039               break;
03040        case 1:
03041               early_depth = ", EDSC_PSEXEC";
03042               break;
03043        case 2:
03044               early_depth = ", EDSC_PREPS";
03045               break;
03046        case 3:
03047               early_depth = ", BAD EDSC";
03048               break;
03049        }
03050 
03051        switch ((ctx->data[1] >> 17) & 0x3) {
03052        case 0:
03053               early_depth = "";
03054               break;
03055        case 1:
03056               early_depth = ", BAD ZW interp";
03057               break;
03058        case 2:
03059               early_depth = ", ZW centroid";
03060               break;
03061        case 3:
03062               early_depth = ", ZW sample";
03063               break;
03064        }
03065 
03066        instr_out(ctx, 0, "3DSTATE_WM\n");
03067        instr_out(ctx, 1, "(%s%s%s%s%s%s)%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
03068                 (ctx->data[1] & (1 << 11)) ? "PP " : "",
03069                 (ctx->data[1] & (1 << 12)) ? "PC " : "",
03070                 (ctx->data[1] & (1 << 13)) ? "PS " : "",
03071                 (ctx->data[1] & (1 << 14)) ? "NPP " : "",
03072                 (ctx->data[1] & (1 << 15)) ? "NPC " : "",
03073                 (ctx->data[1] & (1 << 16)) ? "NPS " : "",
03074                 (ctx->data[1] & (1 << 30)) ? ", depth clear" : "",
03075                 (ctx->data[1] & (1 << 29)) ? "" : ", disabled",
03076                 (ctx->data[1] & (1 << 28)) ? ", depth resolve" : "",
03077                 (ctx->data[1] & (1 << 27)) ? ", hiz resolve" : "",
03078                 (ctx->data[1] & (1 << 25)) ? ", kill" : "",
03079                 computed_depth,
03080                 early_depth,
03081                 zw_interp,
03082                 (ctx->data[1] & (1 << 20)) ? ", source depth" : "",
03083                 (ctx->data[1] & (1 << 19)) ? ", source W" : "",
03084                 (ctx->data[1] & (1 << 10)) ? ", coverage" : "",
03085                 (ctx->data[1] & (1 << 4)) ? ", poly stipple" : "",
03086                 (ctx->data[1] & (1 << 3)) ? ", line stipple" : "",
03087                 (ctx->data[1] & (1 << 2)) ? ", point UL" : ", point UR"
03088                 );
03089        instr_out(ctx, 2, "MS\n");
03090 
03091        return 3;
03092 }
03093 
03094 static int
03095 gen4_3DPRIMITIVE(struct drm_intel_decode *ctx)
03096 {
03097        instr_out(ctx, 0,
03098                 "3DPRIMITIVE: %s %s\n",
03099                 get_965_prim_type((ctx->data[0] >> 10) & 0x1f),
03100                 (ctx->data[0] & (1 << 15)) ? "random" : "sequential");
03101        instr_out(ctx, 1, "vertex count\n");
03102        instr_out(ctx, 2, "start vertex\n");
03103        instr_out(ctx, 3, "instance count\n");
03104        instr_out(ctx, 4, "start instance\n");
03105        instr_out(ctx, 5, "index bias\n");
03106 
03107        return 6;
03108 }
03109 
03110 static int
03111 gen7_3DPRIMITIVE(struct drm_intel_decode *ctx)
03112 {
03113        bool indirect = !!(ctx->data[0] & (1 << 10));
03114 
03115        instr_out(ctx, 0,
03116                 "3DPRIMITIVE: %s%s\n",
03117                 indirect ? " indirect" : "",
03118                 (ctx->data[0] & (1 << 8)) ? " predicated" : "");
03119        instr_out(ctx, 1, "%s %s\n",
03120                 get_965_prim_type(ctx->data[1] & 0x3f),
03121                 (ctx->data[1] & (1 << 8)) ? "random" : "sequential");
03122        instr_out(ctx, 2, indirect ? "ignored" : "vertex count\n");
03123        instr_out(ctx, 3, indirect ? "ignored" : "start vertex\n");
03124        instr_out(ctx, 4, indirect ? "ignored" : "instance count\n");
03125        instr_out(ctx, 5, indirect ? "ignored" : "start instance\n");
03126        instr_out(ctx, 6, indirect ? "ignored" : "index bias\n");
03127 
03128        return 7;
03129 }
03130 
03131 static int
03132 decode_3d_965(struct drm_intel_decode *ctx)
03133 {
03134        uint32_t opcode;
03135        unsigned int len;
03136        unsigned int i, j, sba_len;
03137        const char *desc1 = NULL;
03138        uint32_t *data = ctx->data;
03139        uint32_t devid = ctx->devid;
03140 
03141        struct {
03142               uint32_t opcode;
03143               uint32_t len_mask;
03144               int unsigned min_len;
03145               int unsigned max_len;
03146               const char *name;
03147               int gen;
03148               int (*func)(struct drm_intel_decode *ctx);
03149        } opcodes_3d[] = {
03150               { 0x6000, 0x00ff, 3, 3, "URB_FENCE" },
03151               { 0x6001, 0xffff, 2, 2, "CS_URB_STATE" },
03152               { 0x6002, 0x00ff, 2, 2, "CONSTANT_BUFFER" },
03153               { 0x6101, 0xffff, 6, 10, "STATE_BASE_ADDRESS" },
03154               { 0x6102, 0xffff, 2, 2, "STATE_SIP" },
03155               { 0x6104, 0xffff, 1, 1, "3DSTATE_PIPELINE_SELECT" },
03156               { 0x680b, 0xffff, 1, 1, "3DSTATE_VF_STATISTICS" },
03157               { 0x6904, 0xffff, 1, 1, "3DSTATE_PIPELINE_SELECT" },
03158               { 0x7800, 0xffff, 7, 7, "3DSTATE_PIPELINED_POINTERS" },
03159               { 0x7801, 0x00ff, 4, 6, "3DSTATE_BINDING_TABLE_POINTERS" },
03160               { 0x7802, 0x00ff, 4, 4, "3DSTATE_SAMPLER_STATE_POINTERS" },
03161               { 0x7805, 0x00ff, 7, 7, "3DSTATE_DEPTH_BUFFER", 7 },
03162               { 0x7805, 0x00ff, 3, 3, "3DSTATE_URB" },
03163               { 0x7804, 0x00ff, 3, 3, "3DSTATE_CLEAR_PARAMS" },
03164               { 0x7806, 0x00ff, 3, 3, "3DSTATE_STENCIL_BUFFER" },
03165               { 0x7807, 0x00ff, 4, 4, "3DSTATE_HIER_DEPTH_BUFFER", 6 },
03166               { 0x7807, 0x00ff, 3, 3, "3DSTATE_HIER_DEPTH_BUFFER", 7, gen7_3DSTATE_HIER_DEPTH_BUFFER },
03167               { 0x7808, 0x00ff, 5, 257, "3DSTATE_VERTEX_BUFFERS" },
03168               { 0x7809, 0x00ff, 3, 256, "3DSTATE_VERTEX_ELEMENTS" },
03169               { 0x780a, 0x00ff, 3, 3, "3DSTATE_INDEX_BUFFER" },
03170               { 0x780b, 0xffff, 1, 1, "3DSTATE_VF_STATISTICS" },
03171               { 0x780d, 0x00ff, 4, 4, "3DSTATE_VIEWPORT_STATE_POINTERS" },
03172               { 0x780e, 0xffff, 4, 4, NULL, 6, gen6_3DSTATE_CC_STATE_POINTERS },
03173               { 0x780e, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_CC_STATE_POINTERS },
03174               { 0x780f, 0x00ff, 2, 2, "3DSTATE_SCISSOR_POINTERS" },
03175               { 0x7810, 0x00ff, 6, 6, "3DSTATE_VS" },
03176               { 0x7811, 0x00ff, 7, 7, "3DSTATE_GS" },
03177               { 0x7812, 0x00ff, 4, 4, "3DSTATE_CLIP" },
03178               { 0x7813, 0x00ff, 20, 20, "3DSTATE_SF", 6 },
03179               { 0x7813, 0x00ff, 7, 7, "3DSTATE_SF", 7 },
03180               { 0x7814, 0x00ff, 3, 3, "3DSTATE_WM", 7, gen7_3DSTATE_WM },
03181               { 0x7814, 0x00ff, 9, 9, "3DSTATE_WM", 6, gen6_3DSTATE_WM },
03182               { 0x7815, 0x00ff, 5, 5, "3DSTATE_CONSTANT_VS_STATE", 6 },
03183               { 0x7815, 0x00ff, 7, 7, "3DSTATE_CONSTANT_VS", 7, gen7_3DSTATE_CONSTANT_VS },
03184               { 0x7816, 0x00ff, 5, 5, "3DSTATE_CONSTANT_GS_STATE", 6 },
03185               { 0x7816, 0x00ff, 7, 7, "3DSTATE_CONSTANT_GS", 7, gen7_3DSTATE_CONSTANT_GS },
03186               { 0x7817, 0x00ff, 5, 5, "3DSTATE_CONSTANT_PS_STATE", 6 },
03187               { 0x7817, 0x00ff, 7, 7, "3DSTATE_CONSTANT_PS", 7, gen7_3DSTATE_CONSTANT_PS },
03188               { 0x7818, 0xffff, 2, 2, "3DSTATE_SAMPLE_MASK" },
03189               { 0x7819, 0x00ff, 7, 7, "3DSTATE_CONSTANT_HS", 7, gen7_3DSTATE_CONSTANT_HS },
03190               { 0x781a, 0x00ff, 7, 7, "3DSTATE_CONSTANT_DS", 7, gen7_3DSTATE_CONSTANT_DS },
03191               { 0x781b, 0x00ff, 7, 7, "3DSTATE_HS" },
03192               { 0x781c, 0x00ff, 4, 4, "3DSTATE_TE" },
03193               { 0x781d, 0x00ff, 6, 6, "3DSTATE_DS" },
03194               { 0x781e, 0x00ff, 3, 3, "3DSTATE_STREAMOUT" },
03195               { 0x781f, 0x00ff, 14, 14, "3DSTATE_SBE" },
03196               { 0x7820, 0x00ff, 8, 8, "3DSTATE_PS" },
03197               { 0x7821, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP },
03198               { 0x7823, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC },
03199               { 0x7824, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_BLEND_STATE_POINTERS },
03200               { 0x7825, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS },
03201               { 0x7826, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_VS" },
03202               { 0x7827, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_HS" },
03203               { 0x7828, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_DS" },
03204               { 0x7829, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_GS" },
03205               { 0x782a, 0x00ff, 2, 2, "3DSTATE_BINDING_TABLE_POINTERS_PS" },
03206               { 0x782b, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_VS" },
03207               { 0x782c, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_HS" },
03208               { 0x782d, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_DS" },
03209               { 0x782e, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_GS" },
03210               { 0x782f, 0x00ff, 2, 2, "3DSTATE_SAMPLER_STATE_POINTERS_PS" },
03211               { 0x7830, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_VS },
03212               { 0x7831, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_HS },
03213               { 0x7832, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_DS },
03214               { 0x7833, 0x00ff, 2, 2, NULL, 7, gen7_3DSTATE_URB_GS },
03215               { 0x7900, 0xffff, 4, 4, "3DSTATE_DRAWING_RECTANGLE" },
03216               { 0x7901, 0xffff, 5, 5, "3DSTATE_CONSTANT_COLOR" },
03217               { 0x7905, 0xffff, 5, 7, "3DSTATE_DEPTH_BUFFER" },
03218               { 0x7906, 0xffff, 2, 2, "3DSTATE_POLY_STIPPLE_OFFSET" },
03219               { 0x7907, 0xffff, 33, 33, "3DSTATE_POLY_STIPPLE_PATTERN" },
03220               { 0x7908, 0xffff, 3, 3, "3DSTATE_LINE_STIPPLE" },
03221               { 0x7909, 0xffff, 2, 2, "3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP" },
03222               { 0x7909, 0xffff, 2, 2, "3DSTATE_CLEAR_PARAMS" },
03223               { 0x790a, 0xffff, 3, 3, "3DSTATE_AA_LINE_PARAMETERS" },
03224               { 0x790b, 0xffff, 4, 4, "3DSTATE_GS_SVB_INDEX" },
03225               { 0x790d, 0xffff, 3, 3, "3DSTATE_MULTISAMPLE", 6 },
03226               { 0x790d, 0xffff, 4, 4, "3DSTATE_MULTISAMPLE", 7 },
03227               { 0x7910, 0xffff, 2, 2, "3DSTATE_CLEAR_PARAMS" },
03228               { 0x7912, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_VS" },
03229               { 0x7913, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_HS" },
03230               { 0x7914, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_DS" },
03231               { 0x7915, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_GS" },
03232               { 0x7916, 0x00ff, 2, 2, "3DSTATE_PUSH_CONSTANT_ALLOC_PS" },
03233               { 0x7917, 0x00ff, 2, 2+128*2, "3DSTATE_SO_DECL_LIST" },
03234               { 0x7918, 0x00ff, 4, 4, "3DSTATE_SO_BUFFER" },
03235               { 0x7a00, 0x00ff, 4, 6, "PIPE_CONTROL" },
03236               { 0x7b00, 0x00ff, 7, 7, NULL, 7, gen7_3DPRIMITIVE },
03237               { 0x7b00, 0x00ff, 6, 6, NULL, 0, gen4_3DPRIMITIVE },
03238        }, *opcode_3d = NULL;
03239 
03240        opcode = (data[0] & 0xffff0000) >> 16;
03241 
03242        for (i = 0; i < ARRAY_SIZE(opcodes_3d); i++) {
03243               if (opcode != opcodes_3d[i].opcode)
03244                      continue;
03245 
03246               /* If it's marked as not our gen, skip. */
03247               if (opcodes_3d[i].gen && opcodes_3d[i].gen != ctx->gen)
03248                      continue;
03249 
03250               opcode_3d = &opcodes_3d[i];
03251               break;
03252        }
03253 
03254        if (opcode_3d) {
03255               if (opcode_3d->max_len == 1)
03256                      len = 1;
03257               else
03258                      len = (data[0] & opcode_3d->len_mask) + 2;
03259 
03260               if (len < opcode_3d->min_len ||
03261                   len > opcode_3d->max_len) {
03262                      fprintf(out, "Bad length %d in %s, expected %d-%d\n",
03263                             len, opcode_3d->name,
03264                             opcode_3d->min_len, opcode_3d->max_len);
03265               }
03266        } else {
03267               len = (data[0] & 0x0000ffff) + 2;
03268        }
03269 
03270        switch (opcode) {
03271        case 0x6000:
03272               return i965_decode_urb_fence(ctx, len);
03273        case 0x6001:
03274               instr_out(ctx, 0, "CS_URB_STATE\n");
03275               instr_out(ctx, 1,
03276                        "entry_size: %d [%d bytes], n_entries: %d\n",
03277                        (data[1] >> 4) & 0x1f,
03278                        (((data[1] >> 4) & 0x1f) + 1) * 64, data[1] & 0x7);
03279               return len;
03280        case 0x6002:
03281               instr_out(ctx, 0, "CONSTANT_BUFFER: %s\n",
03282                        (data[0] >> 8) & 1 ? "valid" : "invalid");
03283               instr_out(ctx, 1,
03284                        "offset: 0x%08x, length: %d bytes\n", data[1] & ~0x3f,
03285                        ((data[1] & 0x3f) + 1) * 64);
03286               return len;
03287        case 0x6101:
03288               i = 0;
03289               instr_out(ctx, 0, "STATE_BASE_ADDRESS\n");
03290               i++;
03291 
03292               if (IS_GEN6(devid) || IS_GEN7(devid))
03293                      sba_len = 10;
03294               else if (IS_GEN5(devid))
03295                      sba_len = 8;
03296               else
03297                      sba_len = 6;
03298               if (len != sba_len)
03299                      fprintf(out, "Bad count in STATE_BASE_ADDRESS\n");
03300 
03301               state_base_out(ctx, i++, "general");
03302               state_base_out(ctx, i++, "surface");
03303               if (IS_GEN6(devid) || IS_GEN7(devid))
03304                      state_base_out(ctx, i++, "dynamic");
03305               state_base_out(ctx, i++, "indirect");
03306               if (IS_GEN5(devid) || IS_GEN6(devid) || IS_GEN7(devid))
03307                      state_base_out(ctx, i++, "instruction");
03308 
03309               state_max_out(ctx, i++, "general");
03310               if (IS_GEN6(devid) || IS_GEN7(devid))
03311                      state_max_out(ctx, i++, "dynamic");
03312               state_max_out(ctx, i++, "indirect");
03313               if (IS_GEN5(devid) || IS_GEN6(devid) || IS_GEN7(devid))
03314                      state_max_out(ctx, i++, "instruction");
03315 
03316               return len;
03317        case 0x7800:
03318               instr_out(ctx, 0, "3DSTATE_PIPELINED_POINTERS\n");
03319               instr_out(ctx, 1, "VS state\n");
03320               instr_out(ctx, 2, "GS state\n");
03321               instr_out(ctx, 3, "Clip state\n");
03322               instr_out(ctx, 4, "SF state\n");
03323               instr_out(ctx, 5, "WM state\n");
03324               instr_out(ctx, 6, "CC state\n");
03325               return len;
03326        case 0x7801:
03327               if (len != 6 && len != 4)
03328                      fprintf(out,
03329                             "Bad count in 3DSTATE_BINDING_TABLE_POINTERS\n");
03330               if (len == 6) {
03331                      instr_out(ctx, 0,
03332                               "3DSTATE_BINDING_TABLE_POINTERS\n");
03333                      instr_out(ctx, 1, "VS binding table\n");
03334                      instr_out(ctx, 2, "GS binding table\n");
03335                      instr_out(ctx, 3, "Clip binding table\n");
03336                      instr_out(ctx, 4, "SF binding table\n");
03337                      instr_out(ctx, 5, "WM binding table\n");
03338               } else {
03339                      instr_out(ctx, 0,
03340                               "3DSTATE_BINDING_TABLE_POINTERS: VS mod %d, "
03341                               "GS mod %d, PS mod %d\n",
03342                               (data[0] & (1 << 8)) != 0,
03343                               (data[0] & (1 << 9)) != 0,
03344                               (data[0] & (1 << 12)) != 0);
03345                      instr_out(ctx, 1, "VS binding table\n");
03346                      instr_out(ctx, 2, "GS binding table\n");
03347                      instr_out(ctx, 3, "WM binding table\n");
03348               }
03349 
03350               return len;
03351        case 0x7802:
03352               instr_out(ctx, 0,
03353                        "3DSTATE_SAMPLER_STATE_POINTERS: VS mod %d, "
03354                        "GS mod %d, PS mod %d\n", (data[0] & (1 << 8)) != 0,
03355                        (data[0] & (1 << 9)) != 0,
03356                        (data[0] & (1 << 12)) != 0);
03357               instr_out(ctx, 1, "VS sampler state\n");
03358               instr_out(ctx, 2, "GS sampler state\n");
03359               instr_out(ctx, 3, "WM sampler state\n");
03360               return len;
03361        case 0x7805:
03362               /* Actually 3DSTATE_DEPTH_BUFFER on gen7. */
03363               if (ctx->gen == 7)
03364                      break;
03365 
03366               instr_out(ctx, 0, "3DSTATE_URB\n");
03367               instr_out(ctx, 1,
03368                        "VS entries %d, alloc size %d (1024bit row)\n",
03369                        data[1] & 0xffff, ((data[1] >> 16) & 0x07f) + 1);
03370               instr_out(ctx, 2,
03371                        "GS entries %d, alloc size %d (1024bit row)\n",
03372                        (data[2] >> 8) & 0x3ff, (data[2] & 7) + 1);
03373               return len;
03374 
03375        case 0x7808:
03376               if ((len - 1) % 4 != 0)
03377                      fprintf(out, "Bad count in 3DSTATE_VERTEX_BUFFERS\n");
03378               instr_out(ctx, 0, "3DSTATE_VERTEX_BUFFERS\n");
03379 
03380               for (i = 1; i < len;) {
03381                      int idx, access;
03382                      if (IS_GEN6(devid)) {
03383                             idx = 26;
03384                             access = 20;
03385                      } else {
03386                             idx = 27;
03387                             access = 26;
03388                      }
03389                      instr_out(ctx, i,
03390                               "buffer %d: %s, pitch %db\n", data[i] >> idx,
03391                               data[i] & (1 << access) ? "random" :
03392                               "sequential", data[i] & 0x07ff);
03393                      i++;
03394                      instr_out(ctx, i++, "buffer address\n");
03395                      instr_out(ctx, i++, "max index\n");
03396                      instr_out(ctx, i++, "mbz\n");
03397               }
03398               return len;
03399 
03400        case 0x7809:
03401               if ((len + 1) % 2 != 0)
03402                      fprintf(out, "Bad count in 3DSTATE_VERTEX_ELEMENTS\n");
03403               instr_out(ctx, 0, "3DSTATE_VERTEX_ELEMENTS\n");
03404 
03405               for (i = 1; i < len;) {
03406                      instr_out(ctx, i,
03407                               "buffer %d: %svalid, type 0x%04x, "
03408                               "src offset 0x%04x bytes\n",
03409                               data[i] >> ((IS_GEN6(devid) || IS_GEN7(devid)) ? 26 : 27),
03410                               data[i] & (1 << ((IS_GEN6(devid) || IS_GEN7(devid)) ? 25 : 26)) ?
03411                               "" : "in", (data[i] >> 16) & 0x1ff,
03412                               data[i] & 0x07ff);
03413                      i++;
03414                      instr_out(ctx, i, "(%s, %s, %s, %s), "
03415                               "dst offset 0x%02x bytes\n",
03416                               get_965_element_component(data[i], 0),
03417                               get_965_element_component(data[i], 1),
03418                               get_965_element_component(data[i], 2),
03419                               get_965_element_component(data[i], 3),
03420                               (data[i] & 0xff) * 4);
03421                      i++;
03422               }
03423               return len;
03424 
03425        case 0x780d:
03426               instr_out(ctx, 0,
03427                        "3DSTATE_VIEWPORT_STATE_POINTERS\n");
03428               instr_out(ctx, 1, "clip\n");
03429               instr_out(ctx, 2, "sf\n");
03430               instr_out(ctx, 3, "cc\n");
03431               return len;
03432 
03433        case 0x780a:
03434               instr_out(ctx, 0, "3DSTATE_INDEX_BUFFER\n");
03435               instr_out(ctx, 1, "beginning buffer address\n");
03436               instr_out(ctx, 2, "ending buffer address\n");
03437               return len;
03438 
03439        case 0x780f:
03440               instr_out(ctx, 0, "3DSTATE_SCISSOR_POINTERS\n");
03441               instr_out(ctx, 1, "scissor rect offset\n");
03442               return len;
03443 
03444        case 0x7810:
03445               instr_out(ctx, 0, "3DSTATE_VS\n");
03446               instr_out(ctx, 1, "kernel pointer\n");
03447               instr_out(ctx, 2,
03448                        "SPF=%d, VME=%d, Sampler Count %d, "
03449                        "Binding table count %d\n", (data[2] >> 31) & 1,
03450                        (data[2] >> 30) & 1, (data[2] >> 27) & 7,
03451                        (data[2] >> 18) & 0xff);
03452               instr_out(ctx, 3, "scratch offset\n");
03453               instr_out(ctx, 4,
03454                        "Dispatch GRF start %d, VUE read length %d, "
03455                        "VUE read offset %d\n", (data[4] >> 20) & 0x1f,
03456                        (data[4] >> 11) & 0x3f, (data[4] >> 4) & 0x3f);
03457               instr_out(ctx, 5,
03458                        "Max Threads %d, Vertex Cache %sable, "
03459                        "VS func %sable\n", ((data[5] >> 25) & 0x7f) + 1,
03460                        (data[5] & (1 << 1)) != 0 ? "dis" : "en",
03461                        (data[5] & 1) != 0 ? "en" : "dis");
03462               return len;
03463 
03464        case 0x7811:
03465               instr_out(ctx, 0, "3DSTATE_GS\n");
03466               instr_out(ctx, 1, "kernel pointer\n");
03467               instr_out(ctx, 2,
03468                        "SPF=%d, VME=%d, Sampler Count %d, "
03469                        "Binding table count %d\n", (data[2] >> 31) & 1,
03470                        (data[2] >> 30) & 1, (data[2] >> 27) & 7,
03471                        (data[2] >> 18) & 0xff);
03472               instr_out(ctx, 3, "scratch offset\n");
03473               instr_out(ctx, 4,
03474                        "Dispatch GRF start %d, VUE read length %d, "
03475                        "VUE read offset %d\n", (data[4] & 0xf),
03476                        (data[4] >> 11) & 0x3f, (data[4] >> 4) & 0x3f);
03477               instr_out(ctx, 5,
03478                        "Max Threads %d, Rendering %sable\n",
03479                        ((data[5] >> 25) & 0x7f) + 1,
03480                        (data[5] & (1 << 8)) != 0 ? "en" : "dis");
03481               instr_out(ctx, 6,
03482                        "Reorder %sable, Discard Adjaceny %sable, "
03483                        "GS %sable\n",
03484                        (data[6] & (1 << 30)) != 0 ? "en" : "dis",
03485                        (data[6] & (1 << 29)) != 0 ? "en" : "dis",
03486                        (data[6] & (1 << 15)) != 0 ? "en" : "dis");
03487               return len;
03488 
03489        case 0x7812:
03490               instr_out(ctx, 0, "3DSTATE_CLIP\n");
03491               instr_out(ctx, 1,
03492                        "UserClip distance cull test mask 0x%x\n",
03493                        data[1] & 0xff);
03494               instr_out(ctx, 2,
03495                        "Clip %sable, API mode %s, Viewport XY test %sable, "
03496                        "Viewport Z test %sable, Guardband test %sable, Clip mode %d, "
03497                        "Perspective Divide %sable, Non-Perspective Barycentric %sable, "
03498                        "Tri Provoking %d, Line Provoking %d, Trifan Provoking %d\n",
03499                        (data[2] & (1 << 31)) != 0 ? "en" : "dis",
03500                        (data[2] & (1 << 30)) != 0 ? "D3D" : "OGL",
03501                        (data[2] & (1 << 28)) != 0 ? "en" : "dis",
03502                        (data[2] & (1 << 27)) != 0 ? "en" : "dis",
03503                        (data[2] & (1 << 26)) != 0 ? "en" : "dis",
03504                        (data[2] >> 13) & 7,
03505                        (data[2] & (1 << 9)) != 0 ? "dis" : "en",
03506                        (data[2] & (1 << 8)) != 0 ? "en" : "dis",
03507                        (data[2] >> 4) & 3, (data[2] >> 2) & 3,
03508                        (data[2] & 3));
03509               instr_out(ctx, 3,
03510                        "Min PointWidth %d, Max PointWidth %d, "
03511                        "Force Zero RTAIndex %sable, Max VPIndex %d\n",
03512                        (data[3] >> 17) & 0x7ff, (data[3] >> 6) & 0x7ff,
03513                        (data[3] & (1 << 5)) != 0 ? "en" : "dis",
03514                        (data[3] & 0xf));
03515               return len;
03516 
03517        case 0x7813:
03518               if (ctx->gen == 7)
03519                      break;
03520 
03521               instr_out(ctx, 0, "3DSTATE_SF\n");
03522               instr_out(ctx, 1,
03523                        "Attrib Out %d, Attrib Swizzle %sable, VUE read length %d, "
03524                        "VUE read offset %d\n", (data[1] >> 22) & 0x3f,
03525                        (data[1] & (1 << 21)) != 0 ? "en" : "dis",
03526                        (data[1] >> 11) & 0x1f, (data[1] >> 4) & 0x3f);
03527               instr_out(ctx, 2,
03528                        "Legacy Global DepthBias %sable, FrontFace fill %d, BF fill %d, "
03529                        "VP transform %sable, FrontWinding_%s\n",
03530                        (data[2] & (1 << 11)) != 0 ? "en" : "dis",
03531                        (data[2] >> 5) & 3, (data[2] >> 3) & 3,
03532                        (data[2] & (1 << 1)) != 0 ? "en" : "dis",
03533                        (data[2] & 1) != 0 ? "CCW" : "CW");
03534               instr_out(ctx, 3,
03535                        "AA %sable, CullMode %d, Scissor %sable, Multisample m ode %d\n",
03536                        (data[3] & (1 << 31)) != 0 ? "en" : "dis",
03537                        (data[3] >> 29) & 3,
03538                        (data[3] & (1 << 11)) != 0 ? "en" : "dis",
03539                        (data[3] >> 8) & 3);
03540               instr_out(ctx, 4,
03541                        "Last Pixel %sable, SubPixel Precision %d, Use PixelWidth %d\n",
03542                        (data[4] & (1 << 31)) != 0 ? "en" : "dis",
03543                        (data[4] & (1 << 12)) != 0 ? 4 : 8,
03544                        (data[4] & (1 << 11)) != 0);
03545               instr_out(ctx, 5,
03546                        "Global Depth Offset Constant %f\n",
03547                        *(float *)(&data[5]));
03548               instr_out(ctx, 6, "Global Depth Offset Scale %f\n",
03549                        *(float *)(&data[6]));
03550               instr_out(ctx, 7, "Global Depth Offset Clamp %f\n",
03551                        *(float *)(&data[7]));
03552 
03553               for (i = 0, j = 0; i < 8; i++, j += 2)
03554                      instr_out(ctx, i + 8,
03555                               "Attrib %d (Override %s%s%s%s, Const Source %d, Swizzle Select %d, "
03556                               "Source %d); Attrib %d (Override %s%s%s%s, Const Source %d, Swizzle Select %d, Source %d)\n",
03557                               j + 1,
03558                               (data[8 + i] & (1 << 31)) != 0 ? "W" : "",
03559                               (data[8 + i] & (1 << 30)) != 0 ? "Z" : "",
03560                               (data[8 + i] & (1 << 29)) != 0 ? "Y" : "",
03561                               (data[8 + i] & (1 << 28)) != 0 ? "X" : "",
03562                               (data[8 + i] >> 25) & 3,
03563                               (data[8 + i] >> 22) & 3,
03564                               (data[8 + i] >> 16) & 0x1f, j,
03565                               (data[8 + i] & (1 << 15)) != 0 ? "W" : "",
03566                               (data[8 + i] & (1 << 14)) != 0 ? "Z" : "",
03567                               (data[8 + i] & (1 << 13)) != 0 ? "Y" : "",
03568                               (data[8 + i] & (1 << 12)) != 0 ? "X" : "",
03569                               (data[8 + i] >> 9) & 3,
03570                               (data[8 + i] >> 6) & 3, (data[8 + i] & 0x1f));
03571               instr_out(ctx, 16,
03572                        "Point Sprite TexCoord Enable\n");
03573               instr_out(ctx, 17, "Const Interp Enable\n");
03574               instr_out(ctx, 18,
03575                        "Attrib 7-0 WrapShortest Enable\n");
03576               instr_out(ctx, 19,
03577                        "Attrib 15-8 WrapShortest Enable\n");
03578 
03579               return len;
03580 
03581        case 0x7900:
03582               instr_out(ctx, 0, "3DSTATE_DRAWING_RECTANGLE\n");
03583               instr_out(ctx, 1, "top left: %d,%d\n",
03584                        data[1] & 0xffff, (data[1] >> 16) & 0xffff);
03585               instr_out(ctx, 2, "bottom right: %d,%d\n",
03586                        data[2] & 0xffff, (data[2] >> 16) & 0xffff);
03587               instr_out(ctx, 3, "origin: %d,%d\n",
03588                        (int)data[3] & 0xffff, ((int)data[3] >> 16) & 0xffff);
03589 
03590               return len;
03591 
03592        case 0x7905:
03593               instr_out(ctx, 0, "3DSTATE_DEPTH_BUFFER\n");
03594               if (IS_GEN5(devid) || IS_GEN6(devid))
03595                      instr_out(ctx, 1,
03596                               "%s, %s, pitch = %d bytes, %stiled, HiZ %d, Seperate Stencil %d\n",
03597                               get_965_surfacetype(data[1] >> 29),
03598                               get_965_depthformat((data[1] >> 18) & 0x7),
03599                               (data[1] & 0x0001ffff) + 1,
03600                               data[1] & (1 << 27) ? "" : "not ",
03601                               (data[1] & (1 << 22)) != 0,
03602                               (data[1] & (1 << 21)) != 0);
03603               else
03604                      instr_out(ctx, 1,
03605                               "%s, %s, pitch = %d bytes, %stiled\n",
03606                               get_965_surfacetype(data[1] >> 29),
03607                               get_965_depthformat((data[1] >> 18) & 0x7),
03608                               (data[1] & 0x0001ffff) + 1,
03609                               data[1] & (1 << 27) ? "" : "not ");
03610               instr_out(ctx, 2, "depth offset\n");
03611               instr_out(ctx, 3, "%dx%d\n",
03612                        ((data[3] & 0x0007ffc0) >> 6) + 1,
03613                        ((data[3] & 0xfff80000) >> 19) + 1);
03614               instr_out(ctx, 4, "volume depth\n");
03615               if (len >= 6)
03616                      instr_out(ctx, 5, "\n");
03617               if (len >= 7) {
03618                      if (IS_GEN6(devid))
03619                             instr_out(ctx, 6, "\n");
03620                      else
03621                             instr_out(ctx, 6,
03622                                      "render target view extent\n");
03623               }
03624 
03625               return len;
03626 
03627        case 0x7a00:
03628               if (IS_GEN6(devid) || IS_GEN7(devid)) {
03629                      unsigned int i;
03630                      if (len != 4 && len != 5)
03631                             fprintf(out, "Bad count in PIPE_CONTROL\n");
03632 
03633                      switch ((data[1] >> 14) & 0x3) {
03634                      case 0:
03635                             desc1 = "no write";
03636                             break;
03637                      case 1:
03638                             desc1 = "qword write";
03639                             break;
03640                      case 2:
03641                             desc1 = "PS_DEPTH_COUNT write";
03642                             break;
03643                      case 3:
03644                             desc1 = "TIMESTAMP write";
03645                             break;
03646                      }
03647                      instr_out(ctx, 0, "PIPE_CONTROL\n");
03648                      instr_out(ctx, 1,
03649                               "%s, %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
03650                               desc1,
03651                               data[1] & (1 << 20) ? "cs stall, " : "",
03652                               data[1] & (1 << 19) ?
03653                               "global snapshot count reset, " : "",
03654                               data[1] & (1 << 18) ? "tlb invalidate, " : "",
03655                               data[1] & (1 << 17) ? "gfdt flush, " : "",
03656                               data[1] & (1 << 17) ? "media state clear, " :
03657                               "",
03658                               data[1] & (1 << 13) ? "depth stall, " : "",
03659                               data[1] & (1 << 12) ?
03660                               "render target cache flush, " : "",
03661                               data[1] & (1 << 11) ?
03662                               "instruction cache invalidate, " : "",
03663                               data[1] & (1 << 10) ?
03664                               "texture cache invalidate, " : "",
03665                               data[1] & (1 << 9) ?
03666                               "indirect state invalidate, " : "",
03667                               data[1] & (1 << 8) ? "notify irq, " : "",
03668                               data[1] & (1 << 7) ? "PIPE_CONTROL flush, " :
03669                               "",
03670                               data[1] & (1 << 6) ? "protect mem app_id, " :
03671                               "", data[1] & (1 << 5) ? "DC flush, " : "",
03672                               data[1] & (1 << 4) ? "vf fetch invalidate, " :
03673                               "",
03674                               data[1] & (1 << 3) ?
03675                               "constant cache invalidate, " : "",
03676                               data[1] & (1 << 2) ?
03677                               "state cache invalidate, " : "",
03678                               data[1] & (1 << 1) ? "stall at scoreboard, " :
03679                               "",
03680                               data[1] & (1 << 0) ? "depth cache flush, " :
03681                               "");
03682                      if (len == 5) {
03683                             instr_out(ctx, 2,
03684                                      "destination address\n");
03685                             instr_out(ctx, 3,
03686                                      "immediate dword low\n");
03687                             instr_out(ctx, 4,
03688                                      "immediate dword high\n");
03689                      } else {
03690                             for (i = 2; i < len; i++) {
03691                                    instr_out(ctx, i, "\n");
03692                             }
03693                      }
03694                      return len;
03695               } else {
03696                      if (len != 4)
03697                             fprintf(out, "Bad count in PIPE_CONTROL\n");
03698 
03699                      switch ((data[0] >> 14) & 0x3) {
03700                      case 0:
03701                             desc1 = "no write";
03702                             break;
03703                      case 1:
03704                             desc1 = "qword write";
03705                             break;
03706                      case 2:
03707                             desc1 = "PS_DEPTH_COUNT write";
03708                             break;
03709                      case 3:
03710                             desc1 = "TIMESTAMP write";
03711                             break;
03712                      }
03713                      instr_out(ctx, 0,
03714                               "PIPE_CONTROL: %s, %sdepth stall, %sRC write flush, "
03715                               "%sinst flush\n",
03716                               desc1,
03717                               data[0] & (1 << 13) ? "" : "no ",
03718                               data[0] & (1 << 12) ? "" : "no ",
03719                               data[0] & (1 << 11) ? "" : "no ");
03720                      instr_out(ctx, 1, "destination address\n");
03721                      instr_out(ctx, 2, "immediate dword low\n");
03722                      instr_out(ctx, 3, "immediate dword high\n");
03723                      return len;
03724               }
03725        }
03726 
03727        if (opcode_3d) {
03728               if (opcode_3d->func) {
03729                      return opcode_3d->func(ctx);
03730               } else {
03731                      unsigned int i;
03732 
03733                      instr_out(ctx, 0, "%s\n", opcode_3d->name);
03734 
03735                      for (i = 1; i < len; i++) {
03736                             instr_out(ctx, i, "dword %d\n", i);
03737                      }
03738                      return len;
03739               }
03740        }
03741 
03742        instr_out(ctx, 0, "3D UNKNOWN: 3d_965 opcode = 0x%x\n",
03743                 opcode);
03744        return 1;
03745 }
03746 
03747 static int
03748 decode_3d_i830(struct drm_intel_decode *ctx)
03749 {
03750        unsigned int idx;
03751        uint32_t opcode;
03752        uint32_t *data = ctx->data;
03753 
03754        struct {
03755               uint32_t opcode;
03756               unsigned int min_len;
03757               unsigned int max_len;
03758               const char *name;
03759        } opcodes_3d[] = {
03760               { 0x02, 1, 1, "3DSTATE_MODES_3" },
03761               { 0x03, 1, 1, "3DSTATE_ENABLES_1" },
03762               { 0x04, 1, 1, "3DSTATE_ENABLES_2" },
03763               { 0x05, 1, 1, "3DSTATE_VFT0" },
03764               { 0x06, 1, 1, "3DSTATE_AA" },
03765               { 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES" },
03766               { 0x08, 1, 1, "3DSTATE_MODES_1" },
03767               { 0x09, 1, 1, "3DSTATE_STENCIL_TEST" },
03768               { 0x0a, 1, 1, "3DSTATE_VFT1" },
03769               { 0x0b, 1, 1, "3DSTATE_INDPT_ALPHA_BLEND" },
03770               { 0x0c, 1, 1, "3DSTATE_MODES_5" },
03771               { 0x0d, 1, 1, "3DSTATE_MAP_BLEND_OP" },
03772               { 0x0e, 1, 1, "3DSTATE_MAP_BLEND_ARG" },
03773               { 0x0f, 1, 1, "3DSTATE_MODES_2" },
03774               { 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
03775               { 0x16, 1, 1, "3DSTATE_MODES_4"},
03776        }, *opcode_3d;
03777 
03778        opcode = (data[0] & 0x1f000000) >> 24;
03779 
03780        switch (opcode) {
03781        case 0x1f:
03782               return decode_3d_primitive(ctx);
03783        case 0x1d:
03784               return decode_3d_1d(ctx);
03785        case 0x1c:
03786               return decode_3d_1c(ctx);
03787        }
03788 
03789        for (idx = 0; idx < ARRAY_SIZE(opcodes_3d); idx++) {
03790               opcode_3d = &opcodes_3d[idx];
03791               if ((data[0] & 0x1f000000) >> 24 == opcode_3d->opcode) {
03792                      unsigned int len = 1, i;
03793 
03794                      instr_out(ctx, 0, "%s\n", opcode_3d->name);
03795                      if (opcode_3d->max_len > 1) {
03796                             len = (data[0] & 0xff) + 2;
03797                             if (len < opcode_3d->min_len ||
03798                                 len > opcode_3d->max_len) {
03799                                    fprintf(out, "Bad count in %s\n",
03800                                           opcode_3d->name);
03801                             }
03802                      }
03803 
03804                      for (i = 1; i < len; i++) {
03805                             instr_out(ctx, i, "dword %d\n", i);
03806                      }
03807                      return len;
03808               }
03809        }
03810 
03811        instr_out(ctx, 0, "3D UNKNOWN: 3d_i830 opcode = 0x%x\n",
03812                 opcode);
03813        return 1;
03814 }
03815 
03816 struct drm_intel_decode *
03817 drm_intel_decode_context_alloc(uint32_t devid)
03818 {
03819        struct drm_intel_decode *ctx;
03820 
03821        ctx = calloc(1, sizeof(struct drm_intel_decode));
03822        if (!ctx)
03823               return NULL;
03824 
03825        ctx->devid = devid;
03826        ctx->out = stdout;
03827 
03828        if (IS_GEN7(devid))
03829               ctx->gen = 7;
03830        else if (IS_GEN6(devid))
03831               ctx->gen = 6;
03832        else if (IS_GEN5(devid))
03833               ctx->gen = 5;
03834        else if (IS_GEN4(devid))
03835               ctx->gen = 4;
03836        else if (IS_9XX(devid))
03837               ctx->gen = 3;
03838        else {
03839               assert(IS_GEN2(devid));
03840               ctx->gen = 2;
03841        }
03842 
03843        return ctx;
03844 }
03845 
03846 void
03847 drm_intel_decode_context_free(struct drm_intel_decode *ctx)
03848 {
03849        free(ctx);
03850 }
03851 
03852 void
03853 drm_intel_decode_set_dump_past_end(struct drm_intel_decode *ctx,
03854                                int dump_past_end)
03855 {
03856        ctx->dump_past_end = !!dump_past_end;
03857 }
03858 
03859 void
03860 drm_intel_decode_set_batch_pointer(struct drm_intel_decode *ctx,
03861                                void *data, uint32_t hw_offset, int count)
03862 {
03863        ctx->base_data = data;
03864        ctx->base_hw_offset = hw_offset;
03865        ctx->base_count = count;
03866 }
03867 
03868 void
03869 drm_intel_decode_set_head_tail(struct drm_intel_decode *ctx,
03870                             uint32_t head, uint32_t tail)
03871 {
03872        ctx->head = head;
03873        ctx->tail = tail;
03874 }
03875 
03876 void
03877 drm_intel_decode_set_output_file(struct drm_intel_decode *ctx,
03878                              FILE *out)
03879 {
03880        ctx->out = out;
03881 }
03882 
03890 void
03891 drm_intel_decode(struct drm_intel_decode *ctx)
03892 {
03893        int ret;
03894        unsigned int index = 0;
03895        uint32_t devid;
03896        int size = ctx->base_count * 4;
03897        void *temp;
03898 
03899        if (!ctx)
03900               return;
03901 
03902        /* Put a scratch page full of obviously undefined data after
03903         * the batchbuffer.  This lets us avoid a bunch of length
03904         * checking in statically sized packets.
03905         */
03906        temp = malloc(size + 4096);
03907        memcpy(temp, ctx->base_data, size);
03908        memset((char *)temp + size, 0xd0, 4096);
03909        ctx->data = temp;
03910 
03911        ctx->hw_offset = ctx->base_hw_offset;
03912        ctx->count = ctx->base_count;
03913 
03914        devid = ctx->devid;
03915        head_offset = ctx->head;
03916        tail_offset = ctx->tail;
03917        out = ctx->out;
03918 
03919        saved_s2_set = 0;
03920        saved_s4_set = 1;
03921 
03922        while (ctx->count > 0) {
03923               index = 0;
03924 
03925               switch ((ctx->data[index] & 0xe0000000) >> 29) {
03926               case 0x0:
03927                      ret = decode_mi(ctx);
03928 
03929                      /* If MI_BATCHBUFFER_END happened, then dump
03930                       * the rest of the output in case we some day
03931                       * want it in debugging, but don't decode it
03932                       * since it'll just confuse in the common
03933                       * case.
03934                       */
03935                      if (ret == -1) {
03936                             if (ctx->dump_past_end) {
03937                                    index++;
03938                             } else {
03939                                    for (index = index + 1; index < ctx->count;
03940                                         index++) {
03941                                           instr_out(ctx, index, "\n");
03942                                    }
03943                             }
03944                      } else
03945                             index += ret;
03946                      break;
03947               case 0x2:
03948                      index += decode_2d(ctx);
03949                      break;
03950               case 0x3:
03951                      if (IS_9XX(devid) && !IS_GEN3(devid)) {
03952                             index +=
03953                                 decode_3d_965(ctx);
03954                      } else if (IS_GEN3(devid)) {
03955                             index += decode_3d(ctx);
03956                      } else {
03957                             index +=
03958                                 decode_3d_i830(ctx);
03959                      }
03960                      break;
03961               default:
03962                      instr_out(ctx, index, "UNKNOWN\n");
03963                      index++;
03964                      break;
03965               }
03966               fflush(out);
03967 
03968               if (ctx->count < index)
03969                      break;
03970 
03971               ctx->count -= index;
03972               ctx->data += index;
03973               ctx->hw_offset += 4 * index;
03974        }
03975 
03976        free(temp);
03977 }