intel/compiler: Get rid of the global compaction table pointers
[mesa.git] / src / intel / common / gen_batch_decoder.c
index a0d6dbd3e589a9666d9bf49b234d71231267e2d0..3d80b4e6bae90d00b22532d5688fd8192049bf0d 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "common/gen_decoder.h"
 #include "gen_disasm.h"
+#include "util/macros.h"
+#include "main/macros.h" /* Needed for ROUND_DOWN_TO */
 
 #include <string.h>
 
@@ -32,15 +34,21 @@ gen_batch_decode_ctx_init(struct gen_batch_decode_ctx *ctx,
                           FILE *fp, enum gen_batch_decode_flags flags,
                           const char *xml_path,
                           struct gen_batch_decode_bo (*get_bo)(void *,
+                                                               bool,
                                                                uint64_t),
+                          unsigned (*get_state_size)(void *, uint64_t,
+                                                     uint64_t),
                           void *user_data)
 {
    memset(ctx, 0, sizeof(*ctx));
 
    ctx->get_bo = get_bo;
+   ctx->get_state_size = get_state_size;
    ctx->user_data = user_data;
    ctx->fp = fp;
    ctx->flags = flags;
+   ctx->max_vbo_decoded_lines = -1; /* No limit! */
+   ctx->engine = I915_ENGINE_CLASS_RENDER;
 
    if (xml_path == NULL)
       ctx->spec = gen_spec_load(devinfo);
@@ -62,8 +70,6 @@ gen_batch_decode_ctx_finish(struct gen_batch_decode_ctx *ctx)
 #define GREEN_HEADER CSI "1;42m"
 #define NORMAL       CSI "0m"
 
-#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
-
 static void
 ctx_print_group(struct gen_batch_decode_ctx *ctx,
                 struct gen_group *group,
@@ -74,7 +80,7 @@ ctx_print_group(struct gen_batch_decode_ctx *ctx,
 }
 
 static struct gen_batch_decode_bo
-ctx_get_bo(struct gen_batch_decode_ctx *ctx, uint64_t addr)
+ctx_get_bo(struct gen_batch_decode_ctx *ctx, bool ppgtt, uint64_t addr)
 {
    if (gen_spec_get_gen(ctx->spec) >= gen_make_gen(8,0)) {
       /* On Broadwell and above, we have 48-bit addresses which consume two
@@ -86,7 +92,7 @@ ctx_get_bo(struct gen_batch_decode_ctx *ctx, uint64_t addr)
       addr &= (~0ull >> 16);
    }
 
-   struct gen_batch_decode_bo bo = ctx->get_bo(ctx->user_data, addr);
+   struct gen_batch_decode_bo bo = ctx->get_bo(ctx->user_data, ppgtt, addr);
 
    if (gen_spec_get_gen(ctx->spec) >= gen_make_gen(8,0))
       bo.addr &= (~0ull >> 16);
@@ -103,17 +109,36 @@ ctx_get_bo(struct gen_batch_decode_ctx *ctx, uint64_t addr)
    return bo;
 }
 
+static int
+update_count(struct gen_batch_decode_ctx *ctx,
+             uint64_t address,
+             uint64_t base_address,
+             unsigned element_dwords,
+             unsigned guess)
+{
+   unsigned size = 0;
+
+   if (ctx->get_state_size)
+      size = ctx->get_state_size(ctx->user_data, address, base_address);
+
+   if (size > 0)
+      return size / (sizeof(uint32_t) * element_dwords);
+
+   /* In the absence of any information, just guess arbitrarily. */
+   return guess;
+}
+
 static void
 ctx_disassemble_program(struct gen_batch_decode_ctx *ctx,
                         uint32_t ksp, const char *type)
 {
-   if (!ctx->instruction_base.map)
+   uint64_t addr = ctx->instruction_base + ksp;
+   struct gen_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);
+   if (!bo.map)
       return;
 
-   printf("\nReferenced %s:\n", type);
-   gen_disasm_disassemble(ctx->disasm,
-                          (void *)ctx->instruction_base.map, ksp,
-                          ctx->fp);
+   fprintf(ctx->fp, "\nReferenced %s:\n", type);
+   gen_disasm_disassemble(ctx->disasm, bo.map, 0, ctx->fp);
 }
 
 /* Heuristic to determine whether a uint32_t is probably actually a float
@@ -145,45 +170,78 @@ static void
 ctx_print_buffer(struct gen_batch_decode_ctx *ctx,
                  struct gen_batch_decode_bo bo,
                  uint32_t read_length,
-                 uint32_t pitch)
+                 uint32_t pitch,
+                 int max_lines)
 {
-   const uint32_t *dw_end = bo.map + MIN2(bo.size, read_length);
+   const uint32_t *dw_end =
+         bo.map + ROUND_DOWN_TO(MIN2(bo.size, read_length), 4);
 
-   unsigned line_count = 0;
+   int column_count = 0, pitch_col_count = 0, line_count = -1;
    for (const uint32_t *dw = bo.map; dw < dw_end; dw++) {
-      if (line_count * 4 == pitch || line_count == 8) {
+      if (pitch_col_count * 4 == pitch || column_count == 8) {
          fprintf(ctx->fp, "\n");
-         line_count = 0;
+         column_count = 0;
+         if (pitch_col_count * 4 == pitch)
+            pitch_col_count = 0;
+         line_count++;
+
+         if (max_lines >= 0 && line_count >= max_lines)
+            break;
       }
-      fprintf(ctx->fp, line_count == 0 ? "  " : " ");
+      fprintf(ctx->fp, column_count == 0 ? "  " : " ");
 
       if ((ctx->flags & GEN_BATCH_DECODE_FLOATS) && probably_float(*dw))
          fprintf(ctx->fp, "  %8.2f", *(float *) dw);
       else
          fprintf(ctx->fp, "  0x%08x", *dw);
 
-      line_count++;
+      column_count++;
+      pitch_col_count++;
    }
    fprintf(ctx->fp, "\n");
 }
 
+static struct gen_group *
+gen_ctx_find_instruction(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
+{
+   return gen_spec_find_instruction(ctx->spec, ctx->engine, p);
+}
+
 static void
 handle_state_base_address(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
 
    struct gen_field_iterator iter;
    gen_field_iterator_init(&iter, inst, p, 0, false);
 
+   uint64_t surface_base = 0, dynamic_base = 0, instruction_base = 0;
+   bool surface_modify = 0, dynamic_modify = 0, instruction_modify = 0;
+
    while (gen_field_iterator_next(&iter)) {
       if (strcmp(iter.name, "Surface State Base Address") == 0) {
-         ctx->surface_base = ctx_get_bo(ctx, iter.raw_value);
+         surface_base = iter.raw_value;
       } else if (strcmp(iter.name, "Dynamic State Base Address") == 0) {
-         ctx->dynamic_base = ctx_get_bo(ctx, iter.raw_value);
+         dynamic_base = iter.raw_value;
       } else if (strcmp(iter.name, "Instruction Base Address") == 0) {
-         ctx->instruction_base = ctx_get_bo(ctx, iter.raw_value);
+         instruction_base = iter.raw_value;
+      } else if (strcmp(iter.name, "Surface State Base Address Modify Enable") == 0) {
+         surface_modify = iter.raw_value;
+      } else if (strcmp(iter.name, "Dynamic State Base Address Modify Enable") == 0) {
+         dynamic_modify = iter.raw_value;
+      } else if (strcmp(iter.name, "Instruction Base Address Modify Enable") == 0) {
+         instruction_modify = iter.raw_value;
       }
    }
+
+   if (dynamic_modify)
+      ctx->dynamic_base = dynamic_base;
+
+   if (surface_modify)
+      ctx->surface_base = surface_base;
+
+   if (instruction_modify)
+      ctx->instruction_base = instruction_base;
 }
 
 static void
@@ -196,35 +254,41 @@ dump_binding_table(struct gen_batch_decode_ctx *ctx, uint32_t offset, int count)
       return;
    }
 
-   /* If we don't know the actual count, guess. */
-   if (count < 0)
-      count = 8;
+   if (count < 0) {
+      count = update_count(ctx, ctx->surface_base + offset,
+                           ctx->surface_base, 1, 8);
+   }
 
-   if (ctx->surface_base.map == NULL) {
-      fprintf(ctx->fp, "  binding table unavailable\n");
+   if (offset % 32 != 0 || offset >= UINT16_MAX) {
+      fprintf(ctx->fp, "  invalid binding table pointer\n");
       return;
    }
 
-   if (offset % 32 != 0 || offset >= UINT16_MAX ||
-       offset >= ctx->surface_base.size) {
-      fprintf(ctx->fp, "  invalid binding table pointer\n");
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, ctx->surface_base + offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, "  binding table unavailable\n");
       return;
    }
 
-   const uint32_t *pointers = ctx->surface_base.map + offset;
+   const uint32_t *pointers = bind_bo.map;
    for (int i = 0; i < count; i++) {
       if (pointers[i] == 0)
          continue;
 
+      uint64_t addr = ctx->surface_base + pointers[i];
+      struct gen_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);
+      uint32_t size = strct->dw_length * 4;
+
       if (pointers[i] % 32 != 0 ||
-          (pointers[i] + strct->dw_length * 4) >= ctx->surface_base.size) {
-         fprintf(ctx->fp, "pointer %u: %08x <not valid>\n", i, pointers[i]);
+          addr < bo.addr || addr + size >= bo.addr + bo.size) {
+         fprintf(ctx->fp, "pointer %u: 0x%08x <not valid>\n", i, pointers[i]);
          continue;
       }
 
-      fprintf(ctx->fp, "pointer %u: %08x\n", i, pointers[i]);
-      ctx_print_group(ctx, strct, ctx->surface_base.addr + pointers[i],
-                      ctx->surface_base.map + pointers[i]);
+      fprintf(ctx->fp, "pointer %u: 0x%08x\n", i, pointers[i]);
+      ctx_print_group(ctx, strct, addr, bo.map + (addr - bo.addr));
    }
 }
 
@@ -232,23 +296,26 @@ static void
 dump_samplers(struct gen_batch_decode_ctx *ctx, uint32_t offset, int count)
 {
    struct gen_group *strct = gen_spec_find_struct(ctx->spec, "SAMPLER_STATE");
+   uint64_t state_addr = ctx->dynamic_base + offset;
+
+   if (count < 0) {
+      count = update_count(ctx, state_addr, ctx->dynamic_base,
+                           strct->dw_length, 4);
+   }
 
-   /* If we don't know the actual count, guess. */
-   if (count < 0)
-      count = 4;
+   struct gen_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);
+   const void *state_map = bo.map;
 
-   if (ctx->dynamic_base.map == NULL) {
+   if (state_map == NULL) {
       fprintf(ctx->fp, "  samplers unavailable\n");
       return;
    }
 
-   if (offset % 32 != 0 || offset >= ctx->dynamic_base.size) {
+   if (offset % 32 != 0 || state_addr - bo.addr >= bo.size) {
       fprintf(ctx->fp, "  invalid sampler state pointer\n");
       return;
    }
 
-   uint64_t state_addr = ctx->dynamic_base.addr + offset;
-   const void *state_map = ctx->dynamic_base.map + offset;
    for (int i = 0; i < count; i++) {
       fprintf(ctx->fp, "sampler state %d\n", i);
       ctx_print_group(ctx, strct, state_addr, state_map);
@@ -261,10 +328,7 @@ static void
 handle_media_interface_descriptor_load(struct gen_batch_decode_ctx *ctx,
                                        const uint32_t *p)
 {
-   if (ctx->dynamic_base.map == NULL)
-      return;
-
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
    struct gen_group *desc =
       gen_spec_find_struct(ctx->spec, "INTERFACE_DESCRIPTOR_DATA");
 
@@ -281,17 +345,24 @@ handle_media_interface_descriptor_load(struct gen_batch_decode_ctx *ctx,
       }
    }
 
-   uint64_t desc_addr = ctx->dynamic_base.addr + descriptor_offset;
-   const uint32_t *desc_map = ctx->dynamic_base.map + descriptor_offset;
+   uint64_t desc_addr = ctx->dynamic_base + descriptor_offset;
+   struct gen_batch_decode_bo bo = ctx_get_bo(ctx, true, desc_addr);
+   const void *desc_map = bo.map;
+
+   if (desc_map == NULL) {
+      fprintf(ctx->fp, "  interface descriptors unavailable\n");
+      return;
+   }
+
    for (int i = 0; i < descriptor_count; i++) {
       fprintf(ctx->fp, "descriptor %d: %08x\n", i, descriptor_offset);
 
       ctx_print_group(ctx, desc, desc_addr, desc_map);
 
       gen_field_iterator_init(&iter, desc, desc_map, 0, false);
-      uint64_t ksp;
-      uint32_t sampler_offset, sampler_count;
-      uint32_t binding_table_offset, binding_entry_count;
+      uint64_t ksp = 0;
+      uint32_t sampler_offset = 0, sampler_count = 0;
+      uint32_t binding_table_offset = 0, binding_entry_count = 0;
       while (gen_field_iterator_next(&iter)) {
          if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
             ksp = strtoll(iter.value, NULL, 16);
@@ -307,7 +378,7 @@ handle_media_interface_descriptor_load(struct gen_batch_decode_ctx *ctx,
       }
 
       ctx_disassemble_program(ctx, ksp, "compute shader");
-      printf("\n");
+      fprintf(ctx->fp, "\n");
 
       dump_samplers(ctx, sampler_offset, sampler_count);
       dump_binding_table(ctx, binding_table_offset, binding_entry_count);
@@ -321,7 +392,7 @@ static void
 handle_3dstate_vertex_buffers(struct gen_batch_decode_ctx *ctx,
                               const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
    struct gen_group *vbs = gen_spec_find_struct(ctx->spec, "VERTEX_BUFFER_STATE");
 
    struct gen_batch_decode_bo vb = {};
@@ -344,13 +415,13 @@ handle_3dstate_vertex_buffers(struct gen_batch_decode_ctx *ctx,
          } else if (strcmp(vbs_iter.name, "Buffer Pitch") == 0) {
             pitch = vbs_iter.raw_value;
          } else if (strcmp(vbs_iter.name, "Buffer Starting Address") == 0) {
-            vb = ctx_get_bo(ctx, vbs_iter.raw_value);
+            vb = ctx_get_bo(ctx, true, vbs_iter.raw_value);
          } else if (strcmp(vbs_iter.name, "Buffer Size") == 0) {
             vb_size = vbs_iter.raw_value;
             ready = true;
          } else if (strcmp(vbs_iter.name, "End Address") == 0) {
             if (vb.map && vbs_iter.raw_value >= vb.addr)
-               vb_size = vbs_iter.raw_value - vb.addr;
+               vb_size = (vbs_iter.raw_value + 1) - vb.addr;
             else
                vb_size = 0;
             ready = true;
@@ -369,7 +440,7 @@ handle_3dstate_vertex_buffers(struct gen_batch_decode_ctx *ctx,
          if (vb.map == 0 || vb_size == 0)
             continue;
 
-         ctx_print_buffer(ctx, vb, vb_size, pitch);
+         ctx_print_buffer(ctx, vb, vb_size, pitch, ctx->max_vbo_decoded_lines);
 
          vb.map = NULL;
          vb_size = 0;
@@ -384,7 +455,7 @@ static void
 handle_3dstate_index_buffer(struct gen_batch_decode_ctx *ctx,
                             const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
 
    struct gen_batch_decode_bo ib = {};
    uint32_t ib_size = 0;
@@ -396,7 +467,7 @@ handle_3dstate_index_buffer(struct gen_batch_decode_ctx *ctx,
       if (strcmp(iter.name, "Index Format") == 0) {
          format = iter.raw_value;
       } else if (strcmp(iter.name, "Buffer Starting Address") == 0) {
-         ib = ctx_get_bo(ctx, iter.raw_value);
+         ib = ctx_get_bo(ctx, true, iter.raw_value);
       } else if (strcmp(iter.name, "Buffer Size") == 0) {
          ib_size = iter.raw_value;
       }
@@ -434,7 +505,7 @@ handle_3dstate_index_buffer(struct gen_batch_decode_ctx *ctx,
 static void
 decode_single_ksp(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
 
    uint64_t ksp = 0;
    bool is_simd8 = false; /* vertex shaders on Gen8+ only */
@@ -469,14 +540,14 @@ decode_single_ksp(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 
    if (is_enabled) {
       ctx_disassemble_program(ctx, ksp, type);
-      printf("\n");
+      fprintf(ctx->fp, "\n");
    }
 }
 
 static void
 decode_ps_kernels(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
 
    uint64_t ksp[3] = {0, 0, 0};
    bool enabled[3] = {false, false, false};
@@ -518,28 +589,41 @@ decode_ps_kernels(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
       ctx_disassemble_program(ctx, ksp[1], "SIMD16 fragment shader");
    if (enabled[2])
       ctx_disassemble_program(ctx, ksp[2], "SIMD32 fragment shader");
-   fprintf(ctx->fp, "\n");
+
+   if (enabled[0] || enabled[1] || enabled[2])
+      fprintf(ctx->fp, "\n");
 }
 
 static void
-decode_3dstate_constant(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
+decode_3dstate_constant_all(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 {
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
+   struct gen_group *inst =
+      gen_spec_find_instruction(ctx->spec, ctx->engine, p);
+   struct gen_group *body =
+      gen_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_ALL_DATA");
 
    uint32_t read_length[4];
    struct gen_batch_decode_bo buffer[4];
    memset(buffer, 0, sizeof(buffer));
 
-   int rlidx = 0, bidx = 0;
+   struct gen_field_iterator outer;
+   gen_field_iterator_init(&outer, inst, p, 0, false);
+   int idx = 0;
+   while (gen_field_iterator_next(&outer)) {
+      if (outer.struct_desc != body)
+         continue;
 
-   struct gen_field_iterator iter;
-   gen_field_iterator_init(&iter, inst, p, 0, false);
-   while (gen_field_iterator_next(&iter)) {
-      if (strcmp(iter.name, "Read Length") == 0) {
-         read_length[rlidx++] = iter.raw_value;
-      } else if (strcmp(iter.name, "Buffer") == 0) {
-         buffer[bidx++] = ctx_get_bo(ctx, iter.raw_value);
+      struct gen_field_iterator iter;
+      gen_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
+                              0, false);
+      while (gen_field_iterator_next(&iter)) {
+         if (!strcmp(iter.name, "Pointer To Constant Buffer")) {
+            buffer[idx] = ctx_get_bo(ctx, true, iter.raw_value);
+         } else if (!strcmp(iter.name, "Constant Buffer Read Length")) {
+            read_length[idx] = iter.raw_value;
+         }
       }
+      idx++;
    }
 
    for (int i = 0; i < 4; i++) {
@@ -549,10 +633,71 @@ decode_3dstate_constant(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
       unsigned size = read_length[i] * 32;
       fprintf(ctx->fp, "constant buffer %d, size %u\n", i, size);
 
-      ctx_print_buffer(ctx, buffer[i], size, 0);
+      ctx_print_buffer(ctx, buffer[i], size, 0, -1);
+   }
+}
+
+static void
+decode_3dstate_constant(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
+{
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
+   struct gen_group *body =
+      gen_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");
+
+   uint32_t read_length[4] = {0};
+   uint64_t read_addr[4];
+
+   struct gen_field_iterator outer;
+   gen_field_iterator_init(&outer, inst, p, 0, false);
+   while (gen_field_iterator_next(&outer)) {
+      if (outer.struct_desc != body)
+         continue;
+
+      struct gen_field_iterator iter;
+      gen_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
+                              0, false);
+
+      while (gen_field_iterator_next(&iter)) {
+         int idx;
+         if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {
+            read_length[idx] = iter.raw_value;
+         } else if (sscanf(iter.name, "Buffer[%d]", &idx) == 1) {
+            read_addr[idx] = iter.raw_value;
+         }
+      }
+
+      for (int i = 0; i < 4; i++) {
+         if (read_length[i] == 0)
+            continue;
+
+         struct gen_batch_decode_bo buffer = ctx_get_bo(ctx, true, read_addr[i]);
+         if (!buffer.map) {
+            fprintf(ctx->fp, "constant buffer %d unavailable\n", i);
+            continue;
+         }
+
+         unsigned size = read_length[i] * 32;
+         fprintf(ctx->fp, "constant buffer %d, size %u\n", i, size);
+
+         ctx_print_buffer(ctx, buffer, size, 0, -1);
+      }
    }
 }
 
+static void
+decode_gen6_3dstate_binding_table_pointers(struct gen_batch_decode_ctx *ctx,
+                                           const uint32_t *p)
+{
+   fprintf(ctx->fp, "VS Binding Table:\n");
+   dump_binding_table(ctx, p[1], -1);
+
+   fprintf(ctx->fp, "GS Binding Table:\n");
+   dump_binding_table(ctx, p[2], -1);
+
+   fprintf(ctx->fp, "PS Binding Table:\n");
+   dump_binding_table(ctx, p[3], -1);
+}
+
 static void
 decode_3dstate_binding_table_pointers(struct gen_batch_decode_ctx *ctx,
                                       const uint32_t *p)
@@ -591,15 +736,9 @@ decode_dynamic_state_pointers(struct gen_batch_decode_ctx *ctx,
                               const char *struct_type, const uint32_t *p,
                               int count)
 {
-   if (ctx->dynamic_base.map == NULL) {
-      fprintf(ctx->fp, "  dynamic %s state unavailable\n", struct_type);
-      return;
-   }
-
-   struct gen_group *inst = gen_spec_find_instruction(ctx->spec, p);
-   struct gen_group *state = gen_spec_find_struct(ctx->spec, struct_type);
+   struct gen_group *inst = gen_ctx_find_instruction(ctx, p);
 
-   uint32_t state_offset;
+   uint32_t state_offset = 0;
 
    struct gen_field_iterator iter;
    gen_field_iterator_init(&iter, inst, p, 0, false);
@@ -610,14 +749,40 @@ decode_dynamic_state_pointers(struct gen_batch_decode_ctx *ctx,
       }
    }
 
-   uint32_t state_addr = ctx->dynamic_base.addr + state_offset;
-   const uint32_t *state_map = ctx->dynamic_base.map + state_offset;
+   uint64_t state_addr = ctx->dynamic_base + state_offset;
+   struct gen_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);
+   const void *state_map = bo.map;
+
+   if (state_map == NULL) {
+      fprintf(ctx->fp, "  dynamic %s state unavailable\n", struct_type);
+      return;
+   }
+
+   struct gen_group *state = gen_spec_find_struct(ctx->spec, struct_type);
+   if (strcmp(struct_type, "BLEND_STATE") == 0) {
+      /* Blend states are different from the others because they have a header
+       * struct called BLEND_STATE which is followed by a variable number of
+       * BLEND_STATE_ENTRY structs.
+       */
+      fprintf(ctx->fp, "%s\n", struct_type);
+      ctx_print_group(ctx, state, state_addr, state_map);
+
+      state_addr += state->dw_length * 4;
+      state_map += state->dw_length * 4;
+
+      struct_type = "BLEND_STATE_ENTRY";
+      state = gen_spec_find_struct(ctx->spec, struct_type);
+   }
+
+   count = update_count(ctx, ctx->dynamic_base + state_offset,
+                        ctx->dynamic_base, state->dw_length, count);
+
    for (int i = 0; i < count; i++) {
       fprintf(ctx->fp, "%s %d\n", struct_type, i);
-      ctx_print_group(ctx, state, state_offset, state_map);
+      ctx_print_group(ctx, state, state_addr, state_map);
 
       state_addr += state->dw_length * 4;
-      state_map += state->dw_length;
+      state_map += state->dw_length * 4;
    }
 }
 
@@ -656,6 +821,13 @@ decode_3dstate_scissor_state_pointers(struct gen_batch_decode_ctx *ctx,
    decode_dynamic_state_pointers(ctx, "SCISSOR_RECT", p, 1);
 }
 
+static void
+decode_3dstate_slice_table_state_pointers(struct gen_batch_decode_ctx *ctx,
+                                          const uint32_t *p)
+{
+   decode_dynamic_state_pointers(ctx, "SLICE_HASH_TABLE", p, 1);
+}
+
 static void
 decode_load_register_imm(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
 {
@@ -668,6 +840,172 @@ decode_load_register_imm(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
    }
 }
 
+static void
+decode_vs_state(struct gen_batch_decode_ctx *ctx, uint32_t offset)
+{
+   struct gen_group *strct =
+      gen_spec_find_struct(ctx->spec, "VS_STATE");
+   if (strct == NULL) {
+      fprintf(ctx->fp, "did not find VS_STATE info\n");
+      return;
+   }
+
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, " vs state unavailable\n");
+      return;
+   }
+
+   ctx_print_group(ctx, strct, offset, bind_bo.map);
+}
+
+
+static void
+decode_clip_state(struct gen_batch_decode_ctx *ctx, uint32_t offset)
+{
+   struct gen_group *strct =
+      gen_spec_find_struct(ctx->spec, "CLIP_STATE");
+   if (strct == NULL) {
+      fprintf(ctx->fp, "did not find CLIP_STATE info\n");
+      return;
+   }
+
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, " clip state unavailable\n");
+      return;
+   }
+
+   ctx_print_group(ctx, strct, offset, bind_bo.map);
+
+   struct gen_group *vp_strct =
+      gen_spec_find_struct(ctx->spec, "CLIP_VIEWPORT");
+   if (vp_strct == NULL) {
+      fprintf(ctx->fp, "did not find CLIP_VIEWPORT info\n");
+      return;
+   }
+   uint32_t clip_vp_offset = ((uint32_t *)bind_bo.map)[6] & ~0x3;
+   struct gen_batch_decode_bo vp_bo =
+      ctx_get_bo(ctx, true, clip_vp_offset);
+   if (vp_bo.map == NULL) {
+      fprintf(ctx->fp, " clip vp state unavailable\n");
+      return;
+   }
+   ctx_print_group(ctx, vp_strct, clip_vp_offset, vp_bo.map);
+}
+
+static void
+decode_sf_state(struct gen_batch_decode_ctx *ctx, uint32_t offset)
+{
+   struct gen_group *strct =
+      gen_spec_find_struct(ctx->spec, "SF_STATE");
+   if (strct == NULL) {
+      fprintf(ctx->fp, "did not find SF_STATE info\n");
+      return;
+   }
+
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, " sf state unavailable\n");
+      return;
+   }
+
+   ctx_print_group(ctx, strct, offset, bind_bo.map);
+
+   struct gen_group *vp_strct =
+      gen_spec_find_struct(ctx->spec, "SF_VIEWPORT");
+   if (vp_strct == NULL) {
+      fprintf(ctx->fp, "did not find SF_VIEWPORT info\n");
+      return;
+   }
+
+   uint32_t sf_vp_offset = ((uint32_t *)bind_bo.map)[5] & ~0x3;
+   struct gen_batch_decode_bo vp_bo =
+      ctx_get_bo(ctx, true, sf_vp_offset);
+   if (vp_bo.map == NULL) {
+      fprintf(ctx->fp, " sf vp state unavailable\n");
+      return;
+   }
+   ctx_print_group(ctx, vp_strct, sf_vp_offset, vp_bo.map);
+}
+
+static void
+decode_wm_state(struct gen_batch_decode_ctx *ctx, uint32_t offset)
+{
+   struct gen_group *strct =
+      gen_spec_find_struct(ctx->spec, "WM_STATE");
+   if (strct == NULL) {
+      fprintf(ctx->fp, "did not find WM_STATE info\n");
+      return;
+   }
+
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, " wm state unavailable\n");
+      return;
+   }
+
+   ctx_print_group(ctx, strct, offset, bind_bo.map);
+}
+
+static void
+decode_cc_state(struct gen_batch_decode_ctx *ctx, uint32_t offset)
+{
+   struct gen_group *strct =
+      gen_spec_find_struct(ctx->spec, "COLOR_CALC_STATE");
+   if (strct == NULL) {
+      fprintf(ctx->fp, "did not find COLOR_CALC_STATE info\n");
+      return;
+   }
+
+   struct gen_batch_decode_bo bind_bo =
+      ctx_get_bo(ctx, true, offset);
+
+   if (bind_bo.map == NULL) {
+      fprintf(ctx->fp, " cc state unavailable\n");
+      return;
+   }
+
+   ctx_print_group(ctx, strct, offset, bind_bo.map);
+
+   struct gen_group *vp_strct =
+      gen_spec_find_struct(ctx->spec, "CC_VIEWPORT");
+   if (vp_strct == NULL) {
+      fprintf(ctx->fp, "did not find CC_VIEWPORT info\n");
+      return;
+   }
+   uint32_t cc_vp_offset = ((uint32_t *)bind_bo.map)[4] & ~0x3;
+   struct gen_batch_decode_bo vp_bo =
+      ctx_get_bo(ctx, true, cc_vp_offset);
+   if (vp_bo.map == NULL) {
+      fprintf(ctx->fp, " cc vp state unavailable\n");
+      return;
+   }
+   ctx_print_group(ctx, vp_strct, cc_vp_offset, vp_bo.map);
+}
+static void
+decode_pipelined_pointers(struct gen_batch_decode_ctx *ctx, const uint32_t *p)
+{
+   fprintf(ctx->fp, "VS State Table:\n");
+   decode_vs_state(ctx, p[1]);
+   fprintf(ctx->fp, "Clip State Table:\n");
+   decode_clip_state(ctx, p[3] & ~1);
+   fprintf(ctx->fp, "SF State Table:\n");
+   decode_sf_state(ctx, p[4]);
+   fprintf(ctx->fp, "WM State Table:\n");
+   decode_wm_state(ctx, p[5]);
+   fprintf(ctx->fp, "CC State Table:\n");
+   decode_cc_state(ctx, p[6]);
+}
+
 struct custom_decoder {
    const char *cmd_name;
    void (*decode)(struct gen_batch_decode_ctx *ctx, const uint32_t *p);
@@ -681,12 +1019,15 @@ struct custom_decoder {
    { "3DSTATE_DS", decode_single_ksp },
    { "3DSTATE_HS", decode_single_ksp },
    { "3DSTATE_PS", decode_ps_kernels },
+   { "3DSTATE_WM", decode_ps_kernels },
    { "3DSTATE_CONSTANT_VS", decode_3dstate_constant },
    { "3DSTATE_CONSTANT_GS", decode_3dstate_constant },
    { "3DSTATE_CONSTANT_PS", decode_3dstate_constant },
    { "3DSTATE_CONSTANT_HS", decode_3dstate_constant },
    { "3DSTATE_CONSTANT_DS", decode_3dstate_constant },
+   { "3DSTATE_CONSTANT_ALL", decode_3dstate_constant_all },
 
+   { "3DSTATE_BINDING_TABLE_POINTERS", decode_gen6_3dstate_binding_table_pointers },
    { "3DSTATE_BINDING_TABLE_POINTERS_VS", decode_3dstate_binding_table_pointers },
    { "3DSTATE_BINDING_TABLE_POINTERS_HS", decode_3dstate_binding_table_pointers },
    { "3DSTATE_BINDING_TABLE_POINTERS_DS", decode_3dstate_binding_table_pointers },
@@ -705,47 +1046,37 @@ struct custom_decoder {
    { "3DSTATE_BLEND_STATE_POINTERS", decode_3dstate_blend_state_pointers },
    { "3DSTATE_CC_STATE_POINTERS", decode_3dstate_cc_state_pointers },
    { "3DSTATE_SCISSOR_STATE_POINTERS", decode_3dstate_scissor_state_pointers },
-   { "MI_LOAD_REGISTER_IMM", decode_load_register_imm }
+   { "3DSTATE_SLICE_TABLE_STATE_POINTERS", decode_3dstate_slice_table_state_pointers },
+   { "MI_LOAD_REGISTER_IMM", decode_load_register_imm },
+   { "3DSTATE_PIPELINED_POINTERS", decode_pipelined_pointers }
 };
 
-static inline uint64_t
-get_address(struct gen_spec *spec, const uint32_t *p)
-{
-   /* Addresses are always guaranteed to be page-aligned and sometimes
-    * hardware packets have extra stuff stuffed in the bottom 12 bits.
-    */
-   uint64_t addr = p[0] & ~0xfffu;
-
-   if (gen_spec_get_gen(spec) >= gen_make_gen(8,0)) {
-      /* On Broadwell and above, we have 48-bit addresses which consume two
-       * dwords.  Some packets require that these get stored in a "canonical
-       * form" which means that bit 47 is sign-extended through the upper
-       * bits. In order to correctly handle those aub dumps, we need to mask
-       * off the top 16 bits.
-       */
-      addr |= ((uint64_t)p[1] & 0xffff) << 32;
-   }
-
-   return addr;
-}
-
 void
 gen_print_batch(struct gen_batch_decode_ctx *ctx,
                 const uint32_t *batch, uint32_t batch_size,
-                uint64_t batch_addr)
+                uint64_t batch_addr, bool from_ring)
 {
-   const uint32_t *p, *end = batch + batch_size;
+   const uint32_t *p, *end = batch + batch_size / sizeof(uint32_t);
    int length;
    struct gen_group *inst;
+   const char *reset_color = ctx->flags & GEN_BATCH_DECODE_IN_COLOR ? NORMAL : "";
+
+   if (ctx->n_batch_buffer_start >= 100) {
+      fprintf(ctx->fp, "%s0x%08"PRIx64": Max batch buffer jumps exceeded%s\n",
+              (ctx->flags & GEN_BATCH_DECODE_IN_COLOR) ? RED_COLOR : "",
+              (ctx->flags & GEN_BATCH_DECODE_OFFSETS) ? batch_addr : 0,
+              reset_color);
+      return;
+   }
+
+   ctx->n_batch_buffer_start++;
 
    for (p = batch; p < end; p += length) {
-      inst = gen_spec_find_instruction(ctx->spec, p);
+      inst = gen_ctx_find_instruction(ctx, p);
       length = gen_group_get_length(inst, p);
       assert(inst == NULL || length > 0);
       length = MAX2(1, length);
 
-      const char *reset_color = ctx->flags & GEN_BATCH_DECODE_IN_COLOR ? NORMAL : "";
-
       uint64_t offset;
       if (ctx->flags & GEN_BATCH_DECODE_OFFSETS)
          offset = batch_addr + ((char *)p - (char *)batch);
@@ -783,7 +1114,7 @@ gen_print_batch(struct gen_batch_decode_ctx *ctx,
       if (ctx->flags & GEN_BATCH_DECODE_FULL) {
          ctx_print_group(ctx, inst, offset, p);
 
-         for (int i = 0; i < ARRAY_LENGTH(custom_decoders); i++) {
+         for (int i = 0; i < ARRAY_SIZE(custom_decoders); i++) {
             if (strcmp(inst_name, custom_decoders[i].cmd_name) == 0) {
                custom_decoders[i].decode(ctx, p);
                break;
@@ -792,51 +1123,49 @@ gen_print_batch(struct gen_batch_decode_ctx *ctx,
       }
 
       if (strcmp(inst_name, "MI_BATCH_BUFFER_START") == 0) {
-         struct gen_batch_decode_bo next_batch;
-         bool second_level;
+         uint64_t next_batch_addr = 0;
+         bool ppgtt = false;
+         bool second_level = false;
          struct gen_field_iterator iter;
          gen_field_iterator_init(&iter, inst, p, 0, false);
          while (gen_field_iterator_next(&iter)) {
             if (strcmp(iter.name, "Batch Buffer Start Address") == 0) {
-               next_batch = ctx_get_bo(ctx, iter.raw_value);
+               next_batch_addr = iter.raw_value;
             } else if (strcmp(iter.name, "Second Level Batch Buffer") == 0) {
                second_level = iter.raw_value;
+            } else if (strcmp(iter.name, "Address Space Indicator") == 0) {
+               ppgtt = iter.raw_value;
             }
          }
 
+         struct gen_batch_decode_bo next_batch = ctx_get_bo(ctx, ppgtt, next_batch_addr);
+
          if (next_batch.map == NULL) {
-            fprintf(ctx->fp, "Secondary batch at 0x%08"PRIx64" unavailable",
-                    next_batch.addr);
+            fprintf(ctx->fp, "Secondary batch at 0x%08"PRIx64" unavailable\n",
+                    next_batch_addr);
+         } else {
+            gen_print_batch(ctx, next_batch.map, next_batch.size,
+                            next_batch.addr, false);
          }
-
          if (second_level) {
             /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
              * like a subroutine call.  Commands that come afterwards get
              * processed once the 2nd level batch buffer returns with
              * MI_BATCH_BUFFER_END.
              */
-            if (next_batch.map) {
-               gen_print_batch(ctx, next_batch.map, next_batch.size,
-                               next_batch.addr);
-            }
-         } else {
+            continue;
+         } else if (!from_ring) {
             /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
              * like a goto.  Nothing after it will ever get processed.  In
              * order to prevent the recursion from growing, we just reset the
              * loop and continue;
              */
-            if (next_batch.map) {
-               p = next_batch.map;
-               end = next_batch.map + next_batch.size;
-               length = 0;
-               continue;
-            } else {
-               /* Nothing we can do */
-               break;
-            }
+            break;
          }
       } else if (strcmp(inst_name, "MI_BATCH_BUFFER_END") == 0) {
          break;
       }
    }
+
+   ctx->n_batch_buffer_start--;
 }