1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <string.h>
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/macros.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "aubinator_viewer.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_civoid
31bf215546Sopenharmony_ciaub_viewer_decode_ctx_init(struct aub_viewer_decode_ctx *ctx,
32bf215546Sopenharmony_ci                           struct aub_viewer_cfg *cfg,
33bf215546Sopenharmony_ci                           struct aub_viewer_decode_cfg *decode_cfg,
34bf215546Sopenharmony_ci                           const struct intel_device_info *devinfo,
35bf215546Sopenharmony_ci                           struct intel_spec *spec,
36bf215546Sopenharmony_ci                           struct intel_batch_decode_bo (*get_bo)(void *, bool, uint64_t),
37bf215546Sopenharmony_ci                           unsigned (*get_state_size)(void *, uint32_t),
38bf215546Sopenharmony_ci                           void *user_data)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   memset(ctx, 0, sizeof(*ctx));
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   ctx->get_bo = get_bo;
43bf215546Sopenharmony_ci   ctx->get_state_size = get_state_size;
44bf215546Sopenharmony_ci   ctx->user_data = user_data;
45bf215546Sopenharmony_ci   ctx->devinfo = devinfo;
46bf215546Sopenharmony_ci   ctx->engine = I915_ENGINE_CLASS_RENDER;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   ctx->cfg = cfg;
49bf215546Sopenharmony_ci   ctx->decode_cfg = decode_cfg;
50bf215546Sopenharmony_ci   ctx->spec = spec;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_ciaub_viewer_print_group(struct aub_viewer_decode_ctx *ctx,
55bf215546Sopenharmony_ci                       struct intel_group *group,
56bf215546Sopenharmony_ci                       uint64_t address, const void *map)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci   struct intel_field_iterator iter;
59bf215546Sopenharmony_ci   int last_dword = -1;
60bf215546Sopenharmony_ci   const uint32_t *p = (const uint32_t *) map;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, group, p, 0, false);
63bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
64bf215546Sopenharmony_ci      if (ctx->decode_cfg->show_dwords) {
65bf215546Sopenharmony_ci         int iter_dword = iter.end_bit / 32;
66bf215546Sopenharmony_ci         if (last_dword != iter_dword) {
67bf215546Sopenharmony_ci            for (int i = last_dword + 1; i <= iter_dword; i++) {
68bf215546Sopenharmony_ci               ImGui::TextColored(ctx->cfg->dwords_color,
69bf215546Sopenharmony_ci                                  "0x%012" PRIx64 ":  0x%012x : Dword %d",
70bf215546Sopenharmony_ci                                  address + 4 * i, iter.p[i], i);
71bf215546Sopenharmony_ci            }
72bf215546Sopenharmony_ci            last_dword = iter_dword;
73bf215546Sopenharmony_ci         }
74bf215546Sopenharmony_ci      }
75bf215546Sopenharmony_ci      if (!intel_field_is_header(iter.field)) {
76bf215546Sopenharmony_ci         if (ctx->decode_cfg->field_filter.PassFilter(iter.name)) {
77bf215546Sopenharmony_ci            if (iter.field->type.kind == intel_type::INTEL_TYPE_BOOL && iter.raw_value) {
78bf215546Sopenharmony_ci               ImGui::Text("%s: ", iter.name); ImGui::SameLine();
79bf215546Sopenharmony_ci               ImGui::TextColored(ctx->cfg->boolean_color, "true");
80bf215546Sopenharmony_ci            } else {
81bf215546Sopenharmony_ci               ImGui::Text("%s: %s", iter.name, iter.value);
82bf215546Sopenharmony_ci            }
83bf215546Sopenharmony_ci            if (iter.struct_desc) {
84bf215546Sopenharmony_ci               int struct_dword = iter.start_bit / 32;
85bf215546Sopenharmony_ci               uint64_t struct_address = address + 4 * struct_dword;
86bf215546Sopenharmony_ci               aub_viewer_print_group(ctx, iter.struct_desc, struct_address,
87bf215546Sopenharmony_ci                                      &p[struct_dword]);
88bf215546Sopenharmony_ci            }
89bf215546Sopenharmony_ci         }
90bf215546Sopenharmony_ci      }
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic struct intel_batch_decode_bo
95bf215546Sopenharmony_cictx_get_bo(struct aub_viewer_decode_ctx *ctx, bool ppgtt, uint64_t addr)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   if (intel_spec_get_gen(ctx->spec) >= intel_make_gen(8,0)) {
98bf215546Sopenharmony_ci      /* On Broadwell and above, we have 48-bit addresses which consume two
99bf215546Sopenharmony_ci       * dwords.  Some packets require that these get stored in a "canonical
100bf215546Sopenharmony_ci       * form" which means that bit 47 is sign-extended through the upper
101bf215546Sopenharmony_ci       * bits. In order to correctly handle those aub dumps, we need to mask
102bf215546Sopenharmony_ci       * off the top 16 bits.
103bf215546Sopenharmony_ci       */
104bf215546Sopenharmony_ci      addr &= (~0ull >> 16);
105bf215546Sopenharmony_ci   }
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   struct intel_batch_decode_bo bo = ctx->get_bo(ctx->user_data, ppgtt, addr);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   if (intel_spec_get_gen(ctx->spec) >= intel_make_gen(8,0))
110bf215546Sopenharmony_ci      bo.addr &= (~0ull >> 16);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   /* We may actually have an offset into the bo */
113bf215546Sopenharmony_ci   if (bo.map != NULL) {
114bf215546Sopenharmony_ci      assert(bo.addr <= addr);
115bf215546Sopenharmony_ci      uint64_t offset = addr - bo.addr;
116bf215546Sopenharmony_ci      bo.map = (const uint8_t *)bo.map + offset;
117bf215546Sopenharmony_ci      bo.addr += offset;
118bf215546Sopenharmony_ci      bo.size -= offset;
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   return bo;
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cistatic int
125bf215546Sopenharmony_ciupdate_count(struct aub_viewer_decode_ctx *ctx,
126bf215546Sopenharmony_ci             uint32_t offset_from_dsba,
127bf215546Sopenharmony_ci             unsigned element_dwords,
128bf215546Sopenharmony_ci             unsigned guess)
129bf215546Sopenharmony_ci{
130bf215546Sopenharmony_ci   unsigned size = 0;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   if (ctx->get_state_size)
133bf215546Sopenharmony_ci      size = ctx->get_state_size(ctx->user_data, offset_from_dsba);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   if (size > 0)
136bf215546Sopenharmony_ci      return size / (sizeof(uint32_t) * element_dwords);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   /* In the absence of any information, just guess arbitrarily. */
139bf215546Sopenharmony_ci   return guess;
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_cistatic void
143bf215546Sopenharmony_cictx_disassemble_program(struct aub_viewer_decode_ctx *ctx,
144bf215546Sopenharmony_ci                        uint32_t ksp, const char *type)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci   uint64_t addr = ctx->instruction_base + ksp;
147bf215546Sopenharmony_ci   struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);
148bf215546Sopenharmony_ci   if (!bo.map) {
149bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
150bf215546Sopenharmony_ci                         "Shader unavailable addr=0x%012" PRIx64, addr);
151bf215546Sopenharmony_ci      return;
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   ImGui::PushID(addr);
155bf215546Sopenharmony_ci   if (ImGui::Button(type) && ctx->display_shader)
156bf215546Sopenharmony_ci      ctx->display_shader(ctx->user_data, type, addr);
157bf215546Sopenharmony_ci   ImGui::PopID();
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_cistatic void
161bf215546Sopenharmony_cihandle_state_base_address(struct aub_viewer_decode_ctx *ctx,
162bf215546Sopenharmony_ci                          struct intel_group *inst,
163bf215546Sopenharmony_ci                          const uint32_t *p)
164bf215546Sopenharmony_ci{
165bf215546Sopenharmony_ci   struct intel_field_iterator iter;
166bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   uint64_t surface_base = 0, dynamic_base = 0, instruction_base = 0;
169bf215546Sopenharmony_ci   bool surface_modify = 0, dynamic_modify = 0, instruction_modify = 0;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
172bf215546Sopenharmony_ci      if (strcmp(iter.name, "Surface State Base Address") == 0) {
173bf215546Sopenharmony_ci         surface_base = iter.raw_value;
174bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Dynamic State Base Address") == 0) {
175bf215546Sopenharmony_ci         dynamic_base = iter.raw_value;
176bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Instruction Base Address") == 0) {
177bf215546Sopenharmony_ci         instruction_base = iter.raw_value;
178bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Surface State Base Address Modify Enable") == 0) {
179bf215546Sopenharmony_ci         surface_modify = iter.raw_value;
180bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Dynamic State Base Address Modify Enable") == 0) {
181bf215546Sopenharmony_ci         dynamic_modify = iter.raw_value;
182bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Instruction Base Address Modify Enable") == 0) {
183bf215546Sopenharmony_ci         instruction_modify = iter.raw_value;
184bf215546Sopenharmony_ci      }
185bf215546Sopenharmony_ci   }
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   if (dynamic_modify)
188bf215546Sopenharmony_ci      ctx->dynamic_base = dynamic_base;
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   if (surface_modify)
191bf215546Sopenharmony_ci      ctx->surface_base = surface_base;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   if (instruction_modify)
194bf215546Sopenharmony_ci      ctx->instruction_base = instruction_base;
195bf215546Sopenharmony_ci}
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_cistatic void
198bf215546Sopenharmony_cidump_binding_table(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)
199bf215546Sopenharmony_ci{
200bf215546Sopenharmony_ci   struct intel_group *strct =
201bf215546Sopenharmony_ci      intel_spec_find_struct(ctx->spec, "RENDER_SURFACE_STATE");
202bf215546Sopenharmony_ci   if (strct == NULL) {
203bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color, "did not find RENDER_SURFACE_STATE info");
204bf215546Sopenharmony_ci      return;
205bf215546Sopenharmony_ci   }
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   if (count < 0)
208bf215546Sopenharmony_ci      count = update_count(ctx, offset, 1, 8);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   if (offset % 32 != 0 || offset >= UINT16_MAX) {
211bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color, "invalid binding table pointer");
212bf215546Sopenharmony_ci      return;
213bf215546Sopenharmony_ci   }
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci   struct intel_batch_decode_bo bind_bo =
216bf215546Sopenharmony_ci      ctx_get_bo(ctx, true, ctx->surface_base + offset);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   if (bind_bo.map == NULL) {
219bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
220bf215546Sopenharmony_ci                         "binding table unavailable addr=0x%012" PRIx64,
221bf215546Sopenharmony_ci                         ctx->surface_base + offset);
222bf215546Sopenharmony_ci      return;
223bf215546Sopenharmony_ci   }
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   const uint32_t *pointers = (const uint32_t *) bind_bo.map;
226bf215546Sopenharmony_ci   for (int i = 0; i < count; i++) {
227bf215546Sopenharmony_ci      if (pointers[i] == 0)
228bf215546Sopenharmony_ci         continue;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci      uint64_t addr = ctx->surface_base + pointers[i];
231bf215546Sopenharmony_ci      struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, addr);
232bf215546Sopenharmony_ci      uint32_t size = strct->dw_length * 4;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci      if (pointers[i] % 32 != 0 ||
235bf215546Sopenharmony_ci          addr < bo.addr || addr + size >= bo.addr + bo.size) {
236bf215546Sopenharmony_ci         ImGui::TextColored(ctx->cfg->missing_color,
237bf215546Sopenharmony_ci                            "pointer %u: %012x <not valid>", i, pointers[i]);
238bf215546Sopenharmony_ci         continue;
239bf215546Sopenharmony_ci      }
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci      const uint8_t *state = (const uint8_t *) bo.map + (addr - bo.addr);
242bf215546Sopenharmony_ci      if (ImGui::TreeNodeEx(&pointers[i], ImGuiTreeNodeFlags_Framed,
243bf215546Sopenharmony_ci                            "pointer %u: %012x", i, pointers[i])) {
244bf215546Sopenharmony_ci         aub_viewer_print_group(ctx, strct, addr, state);
245bf215546Sopenharmony_ci         ImGui::TreePop();
246bf215546Sopenharmony_ci      }
247bf215546Sopenharmony_ci   }
248bf215546Sopenharmony_ci}
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_cistatic void
251bf215546Sopenharmony_cidump_samplers(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)
252bf215546Sopenharmony_ci{
253bf215546Sopenharmony_ci   struct intel_group *strct = intel_spec_find_struct(ctx->spec, "SAMPLER_STATE");
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   uint64_t state_addr = ctx->dynamic_base + offset;
256bf215546Sopenharmony_ci   struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);
257bf215546Sopenharmony_ci   const uint8_t *state_map = (const uint8_t *) bo.map;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   if (state_map == NULL) {
260bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
261bf215546Sopenharmony_ci                         "samplers unavailable addr=0x%012" PRIx64, state_addr);
262bf215546Sopenharmony_ci      return;
263bf215546Sopenharmony_ci   }
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   if (offset % 32 != 0) {
266bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color, "invalid sampler state pointer");
267bf215546Sopenharmony_ci      return;
268bf215546Sopenharmony_ci   }
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   const unsigned sampler_state_size = strct->dw_length * 4;
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   if (count * sampler_state_size >= bo.size) {
273bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color, "sampler state ends after bo ends");
274bf215546Sopenharmony_ci      return;
275bf215546Sopenharmony_ci   }
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   for (int i = 0; i < count; i++) {
278bf215546Sopenharmony_ci      if (ImGui::TreeNodeEx(state_map, ImGuiTreeNodeFlags_Framed,
279bf215546Sopenharmony_ci                            "sampler state %d", i)) {
280bf215546Sopenharmony_ci         aub_viewer_print_group(ctx, strct, state_addr, state_map);
281bf215546Sopenharmony_ci         ImGui::TreePop();
282bf215546Sopenharmony_ci      }
283bf215546Sopenharmony_ci      state_addr += sampler_state_size;
284bf215546Sopenharmony_ci      state_map += sampler_state_size;
285bf215546Sopenharmony_ci   }
286bf215546Sopenharmony_ci}
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_cistatic void
289bf215546Sopenharmony_cihandle_media_interface_descriptor_load(struct aub_viewer_decode_ctx *ctx,
290bf215546Sopenharmony_ci                                       struct intel_group *inst,
291bf215546Sopenharmony_ci                                       const uint32_t *p)
292bf215546Sopenharmony_ci{
293bf215546Sopenharmony_ci   struct intel_group *desc =
294bf215546Sopenharmony_ci      intel_spec_find_struct(ctx->spec, "INTERFACE_DESCRIPTOR_DATA");
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   struct intel_field_iterator iter;
297bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
298bf215546Sopenharmony_ci   uint32_t descriptor_offset = 0;
299bf215546Sopenharmony_ci   int descriptor_count = 0;
300bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
301bf215546Sopenharmony_ci      if (strcmp(iter.name, "Interface Descriptor Data Start Address") == 0) {
302bf215546Sopenharmony_ci         descriptor_offset = strtol(iter.value, NULL, 16);
303bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Interface Descriptor Total Length") == 0) {
304bf215546Sopenharmony_ci         descriptor_count =
305bf215546Sopenharmony_ci            strtol(iter.value, NULL, 16) / (desc->dw_length * 4);
306bf215546Sopenharmony_ci      }
307bf215546Sopenharmony_ci   }
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   uint64_t desc_addr = ctx->dynamic_base + descriptor_offset;
310bf215546Sopenharmony_ci   struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, desc_addr);
311bf215546Sopenharmony_ci   const uint32_t *desc_map = (const uint32_t *) bo.map;
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   if (desc_map == NULL) {
314bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
315bf215546Sopenharmony_ci                         "interface descriptors unavailable addr=0x%012" PRIx64, desc_addr);
316bf215546Sopenharmony_ci      return;
317bf215546Sopenharmony_ci   }
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci   for (int i = 0; i < descriptor_count; i++) {
320bf215546Sopenharmony_ci      ImGui::Text("descriptor %d: %012x", i, descriptor_offset);
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci      aub_viewer_print_group(ctx, desc, desc_addr, desc_map);
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci      intel_field_iterator_init(&iter, desc, desc_map, 0, false);
325bf215546Sopenharmony_ci      uint64_t ksp = 0;
326bf215546Sopenharmony_ci      uint32_t sampler_offset = 0, sampler_count = 0;
327bf215546Sopenharmony_ci      uint32_t binding_table_offset = 0, binding_entry_count = 0;
328bf215546Sopenharmony_ci      while (intel_field_iterator_next(&iter)) {
329bf215546Sopenharmony_ci         if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
330bf215546Sopenharmony_ci            ksp = strtoll(iter.value, NULL, 16);
331bf215546Sopenharmony_ci         } else if (strcmp(iter.name, "Sampler State Pointer") == 0) {
332bf215546Sopenharmony_ci            sampler_offset = strtol(iter.value, NULL, 16);
333bf215546Sopenharmony_ci         } else if (strcmp(iter.name, "Sampler Count") == 0) {
334bf215546Sopenharmony_ci            sampler_count = strtol(iter.value, NULL, 10);
335bf215546Sopenharmony_ci         } else if (strcmp(iter.name, "Binding Table Pointer") == 0) {
336bf215546Sopenharmony_ci            binding_table_offset = strtol(iter.value, NULL, 16);
337bf215546Sopenharmony_ci         } else if (strcmp(iter.name, "Binding Table Entry Count") == 0) {
338bf215546Sopenharmony_ci            binding_entry_count = strtol(iter.value, NULL, 10);
339bf215546Sopenharmony_ci         }
340bf215546Sopenharmony_ci      }
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci      ctx_disassemble_program(ctx, ksp, "compute shader");
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci      dump_samplers(ctx, sampler_offset, sampler_count);
345bf215546Sopenharmony_ci      dump_binding_table(ctx, binding_table_offset, binding_entry_count);
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci      desc_map += desc->dw_length;
348bf215546Sopenharmony_ci      desc_addr += desc->dw_length * 4;
349bf215546Sopenharmony_ci   }
350bf215546Sopenharmony_ci}
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_cistatic void
353bf215546Sopenharmony_cihandle_3dstate_vertex_buffers(struct aub_viewer_decode_ctx *ctx,
354bf215546Sopenharmony_ci                              struct intel_group *inst,
355bf215546Sopenharmony_ci                              const uint32_t *p)
356bf215546Sopenharmony_ci{
357bf215546Sopenharmony_ci   struct intel_group *vbs = intel_spec_find_struct(ctx->spec, "VERTEX_BUFFER_STATE");
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   struct intel_batch_decode_bo vb = {};
360bf215546Sopenharmony_ci   uint32_t vb_size = 0;
361bf215546Sopenharmony_ci   int index = -1;
362bf215546Sopenharmony_ci   int pitch = -1;
363bf215546Sopenharmony_ci   bool ready = false;
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci   struct intel_field_iterator iter;
366bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
367bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
368bf215546Sopenharmony_ci      if (iter.struct_desc != vbs)
369bf215546Sopenharmony_ci         continue;
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci      uint64_t buffer_addr = 0;
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci      struct intel_field_iterator vbs_iter;
374bf215546Sopenharmony_ci      intel_field_iterator_init(&vbs_iter, vbs, &iter.p[iter.start_bit / 32], 0, false);
375bf215546Sopenharmony_ci      while (intel_field_iterator_next(&vbs_iter)) {
376bf215546Sopenharmony_ci         if (strcmp(vbs_iter.name, "Vertex Buffer Index") == 0) {
377bf215546Sopenharmony_ci            index = vbs_iter.raw_value;
378bf215546Sopenharmony_ci         } else if (strcmp(vbs_iter.name, "Buffer Pitch") == 0) {
379bf215546Sopenharmony_ci            pitch = vbs_iter.raw_value;
380bf215546Sopenharmony_ci         } else if (strcmp(vbs_iter.name, "Buffer Starting Address") == 0) {
381bf215546Sopenharmony_ci            buffer_addr = vbs_iter.raw_value;
382bf215546Sopenharmony_ci            vb = ctx_get_bo(ctx, true, buffer_addr);
383bf215546Sopenharmony_ci         } else if (strcmp(vbs_iter.name, "Buffer Size") == 0) {
384bf215546Sopenharmony_ci            vb_size = vbs_iter.raw_value;
385bf215546Sopenharmony_ci            ready = true;
386bf215546Sopenharmony_ci         } else if (strcmp(vbs_iter.name, "End Address") == 0) {
387bf215546Sopenharmony_ci            if (vb.map && vbs_iter.raw_value >= vb.addr)
388bf215546Sopenharmony_ci               vb_size = vbs_iter.raw_value - vb.addr;
389bf215546Sopenharmony_ci            else
390bf215546Sopenharmony_ci               vb_size = 0;
391bf215546Sopenharmony_ci            ready = true;
392bf215546Sopenharmony_ci         }
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci         if (!ready)
395bf215546Sopenharmony_ci            continue;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci         ImGui::Text("vertex buffer %d, size %d, pitch %d", index, vb_size, pitch);
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci         if (vb.map == NULL) {
400bf215546Sopenharmony_ci            ImGui::TextColored(ctx->cfg->missing_color,
401bf215546Sopenharmony_ci                               "buffer contents unavailable addr=0x%012" PRIx64, buffer_addr);
402bf215546Sopenharmony_ci            continue;
403bf215546Sopenharmony_ci         }
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci         if (vb.map == 0 || vb_size == 0)
406bf215546Sopenharmony_ci            continue;
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci         vb.map = NULL;
409bf215546Sopenharmony_ci         vb_size = 0;
410bf215546Sopenharmony_ci         index = -1;
411bf215546Sopenharmony_ci         pitch = -1;
412bf215546Sopenharmony_ci         ready = false;
413bf215546Sopenharmony_ci      }
414bf215546Sopenharmony_ci   }
415bf215546Sopenharmony_ci}
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_cistatic void
418bf215546Sopenharmony_cihandle_3dstate_index_buffer(struct aub_viewer_decode_ctx *ctx,
419bf215546Sopenharmony_ci                            struct intel_group *inst,
420bf215546Sopenharmony_ci                            const uint32_t *p)
421bf215546Sopenharmony_ci{
422bf215546Sopenharmony_ci   struct intel_batch_decode_bo ib = {};
423bf215546Sopenharmony_ci   uint64_t buffer_addr = 0;
424bf215546Sopenharmony_ci   uint32_t ib_size = 0;
425bf215546Sopenharmony_ci   uint32_t format = 0;
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci   struct intel_field_iterator iter;
428bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
429bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
430bf215546Sopenharmony_ci      if (strcmp(iter.name, "Index Format") == 0) {
431bf215546Sopenharmony_ci         format = iter.raw_value;
432bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Buffer Starting Address") == 0) {
433bf215546Sopenharmony_ci         buffer_addr = iter.raw_value;
434bf215546Sopenharmony_ci         ib = ctx_get_bo(ctx, true, buffer_addr);
435bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Buffer Size") == 0) {
436bf215546Sopenharmony_ci         ib_size = iter.raw_value;
437bf215546Sopenharmony_ci      }
438bf215546Sopenharmony_ci   }
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci   if (ib.map == NULL) {
441bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
442bf215546Sopenharmony_ci                         "buffer contents unavailable addr=0x%012" PRIx64,
443bf215546Sopenharmony_ci                         buffer_addr);
444bf215546Sopenharmony_ci      return;
445bf215546Sopenharmony_ci   }
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci   const uint8_t *m = (const uint8_t *) ib.map;
448bf215546Sopenharmony_ci   const uint8_t *ib_end = m + MIN2(ib.size, ib_size);
449bf215546Sopenharmony_ci   for (int i = 0; m < ib_end && i < 10; i++) {
450bf215546Sopenharmony_ci      switch (format) {
451bf215546Sopenharmony_ci      case 0:
452bf215546Sopenharmony_ci         m += 1;
453bf215546Sopenharmony_ci         break;
454bf215546Sopenharmony_ci      case 1:
455bf215546Sopenharmony_ci         m += 2;
456bf215546Sopenharmony_ci         break;
457bf215546Sopenharmony_ci      case 2:
458bf215546Sopenharmony_ci         m += 4;
459bf215546Sopenharmony_ci         break;
460bf215546Sopenharmony_ci      }
461bf215546Sopenharmony_ci   }
462bf215546Sopenharmony_ci}
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_cistatic void
465bf215546Sopenharmony_cidecode_single_ksp(struct aub_viewer_decode_ctx *ctx,
466bf215546Sopenharmony_ci                  struct intel_group *inst,
467bf215546Sopenharmony_ci                  const uint32_t *p)
468bf215546Sopenharmony_ci{
469bf215546Sopenharmony_ci   uint64_t ksp = 0;
470bf215546Sopenharmony_ci   bool is_simd8 = false; /* vertex shaders on Gfx8+ only */
471bf215546Sopenharmony_ci   bool is_enabled = true;
472bf215546Sopenharmony_ci
473bf215546Sopenharmony_ci   struct intel_field_iterator iter;
474bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
475bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
476bf215546Sopenharmony_ci      if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
477bf215546Sopenharmony_ci         ksp = iter.raw_value;
478bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
479bf215546Sopenharmony_ci         is_simd8 = iter.raw_value;
480bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Dispatch Mode") == 0) {
481bf215546Sopenharmony_ci         is_simd8 = strcmp(iter.value, "SIMD8") == 0;
482bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
483bf215546Sopenharmony_ci         is_simd8 = strcmp(iter.value, "SIMD8") == 0;
484bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "Enable") == 0) {
485bf215546Sopenharmony_ci         is_enabled = iter.raw_value;
486bf215546Sopenharmony_ci      }
487bf215546Sopenharmony_ci   }
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci   const char *type =
490bf215546Sopenharmony_ci      strcmp(inst->name,   "VS_STATE") == 0 ? "vertex shader" :
491bf215546Sopenharmony_ci      strcmp(inst->name,   "GS_STATE") == 0 ? "geometry shader" :
492bf215546Sopenharmony_ci      strcmp(inst->name,   "SF_STATE") == 0 ? "strips and fans shader" :
493bf215546Sopenharmony_ci      strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
494bf215546Sopenharmony_ci      strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation evaluation shader" :
495bf215546Sopenharmony_ci      strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation control shader" :
496bf215546Sopenharmony_ci      strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
497bf215546Sopenharmony_ci      strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
498bf215546Sopenharmony_ci      NULL;
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   if (is_enabled)
501bf215546Sopenharmony_ci      ctx_disassemble_program(ctx, ksp, type);
502bf215546Sopenharmony_ci}
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_cistatic void
505bf215546Sopenharmony_cidecode_ps_kernels(struct aub_viewer_decode_ctx *ctx,
506bf215546Sopenharmony_ci                  struct intel_group *inst,
507bf215546Sopenharmony_ci                  const uint32_t *p)
508bf215546Sopenharmony_ci{
509bf215546Sopenharmony_ci   uint64_t ksp[3] = {0, 0, 0};
510bf215546Sopenharmony_ci   bool enabled[3] = {false, false, false};
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   struct intel_field_iterator iter;
513bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
514bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
515bf215546Sopenharmony_ci      if (strncmp(iter.name, "Kernel Start Pointer ",
516bf215546Sopenharmony_ci                  strlen("Kernel Start Pointer ")) == 0) {
517bf215546Sopenharmony_ci         int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
518bf215546Sopenharmony_ci         ksp[idx] = strtol(iter.value, NULL, 16);
519bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
520bf215546Sopenharmony_ci         enabled[0] = strcmp(iter.value, "true") == 0;
521bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
522bf215546Sopenharmony_ci         enabled[1] = strcmp(iter.value, "true") == 0;
523bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
524bf215546Sopenharmony_ci         enabled[2] = strcmp(iter.value, "true") == 0;
525bf215546Sopenharmony_ci      }
526bf215546Sopenharmony_ci   }
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_ci   /* Reorder KSPs to be [8, 16, 32] instead of the hardware order. */
529bf215546Sopenharmony_ci   if (enabled[0] + enabled[1] + enabled[2] == 1) {
530bf215546Sopenharmony_ci      if (enabled[1]) {
531bf215546Sopenharmony_ci         ksp[1] = ksp[0];
532bf215546Sopenharmony_ci         ksp[0] = 0;
533bf215546Sopenharmony_ci      } else if (enabled[2]) {
534bf215546Sopenharmony_ci         ksp[2] = ksp[0];
535bf215546Sopenharmony_ci         ksp[0] = 0;
536bf215546Sopenharmony_ci      }
537bf215546Sopenharmony_ci   } else {
538bf215546Sopenharmony_ci      uint64_t tmp = ksp[1];
539bf215546Sopenharmony_ci      ksp[1] = ksp[2];
540bf215546Sopenharmony_ci      ksp[2] = tmp;
541bf215546Sopenharmony_ci   }
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci   if (enabled[0])
544bf215546Sopenharmony_ci      ctx_disassemble_program(ctx, ksp[0], "SIMD8 fragment shader");
545bf215546Sopenharmony_ci   if (enabled[1])
546bf215546Sopenharmony_ci      ctx_disassemble_program(ctx, ksp[1], "SIMD16 fragment shader");
547bf215546Sopenharmony_ci   if (enabled[2])
548bf215546Sopenharmony_ci      ctx_disassemble_program(ctx, ksp[2], "SIMD32 fragment shader");
549bf215546Sopenharmony_ci}
550bf215546Sopenharmony_ci
551bf215546Sopenharmony_cistatic void
552bf215546Sopenharmony_cidecode_3dstate_constant(struct aub_viewer_decode_ctx *ctx,
553bf215546Sopenharmony_ci                        struct intel_group *inst,
554bf215546Sopenharmony_ci                        const uint32_t *p)
555bf215546Sopenharmony_ci{
556bf215546Sopenharmony_ci   struct intel_group *body =
557bf215546Sopenharmony_ci      intel_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci   uint32_t read_length[4] = {0};
560bf215546Sopenharmony_ci   uint64_t read_addr[4];
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci   struct intel_field_iterator outer;
563bf215546Sopenharmony_ci   intel_field_iterator_init(&outer, inst, p, 0, false);
564bf215546Sopenharmony_ci   while (intel_field_iterator_next(&outer)) {
565bf215546Sopenharmony_ci      if (outer.struct_desc != body)
566bf215546Sopenharmony_ci         continue;
567bf215546Sopenharmony_ci
568bf215546Sopenharmony_ci      struct intel_field_iterator iter;
569bf215546Sopenharmony_ci      intel_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
570bf215546Sopenharmony_ci                                0, false);
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_ci      while (intel_field_iterator_next(&iter)) {
573bf215546Sopenharmony_ci         int idx;
574bf215546Sopenharmony_ci         if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {
575bf215546Sopenharmony_ci            read_length[idx] = iter.raw_value;
576bf215546Sopenharmony_ci         } else if (sscanf(iter.name, "Buffer[%d]", &idx) == 1) {
577bf215546Sopenharmony_ci            read_addr[idx] = iter.raw_value;
578bf215546Sopenharmony_ci         }
579bf215546Sopenharmony_ci      }
580bf215546Sopenharmony_ci
581bf215546Sopenharmony_ci      for (int i = 0; i < 4; i++) {
582bf215546Sopenharmony_ci         if (read_length[i] == 0)
583bf215546Sopenharmony_ci            continue;
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci         struct intel_batch_decode_bo buffer = ctx_get_bo(ctx, true, read_addr[i]);
586bf215546Sopenharmony_ci         if (!buffer.map) {
587bf215546Sopenharmony_ci            ImGui::TextColored(ctx->cfg->missing_color,
588bf215546Sopenharmony_ci                               "constant buffer %d unavailable addr=0x%012" PRIx64,
589bf215546Sopenharmony_ci                               i, read_addr[i]);
590bf215546Sopenharmony_ci            continue;
591bf215546Sopenharmony_ci         }
592bf215546Sopenharmony_ci
593bf215546Sopenharmony_ci         unsigned size = read_length[i] * 32;
594bf215546Sopenharmony_ci         ImGui::Text("constant buffer %d, size %u", i, size);
595bf215546Sopenharmony_ci
596bf215546Sopenharmony_ci         if (ctx->edit_address) {
597bf215546Sopenharmony_ci            if (ImGui::Button("Show/Edit buffer"))
598bf215546Sopenharmony_ci               ctx->edit_address(ctx->user_data, read_addr[i], size);
599bf215546Sopenharmony_ci         }
600bf215546Sopenharmony_ci      }
601bf215546Sopenharmony_ci   }
602bf215546Sopenharmony_ci}
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_cistatic void
605bf215546Sopenharmony_cidecode_3dstate_binding_table_pointers(struct aub_viewer_decode_ctx *ctx,
606bf215546Sopenharmony_ci                                      struct intel_group *inst,
607bf215546Sopenharmony_ci                                      const uint32_t *p)
608bf215546Sopenharmony_ci{
609bf215546Sopenharmony_ci   dump_binding_table(ctx, p[1], -1);
610bf215546Sopenharmony_ci}
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_cistatic void
613bf215546Sopenharmony_cidecode_3dstate_sampler_state_pointers(struct aub_viewer_decode_ctx *ctx,
614bf215546Sopenharmony_ci                                      struct intel_group *inst,
615bf215546Sopenharmony_ci                                      const uint32_t *p)
616bf215546Sopenharmony_ci{
617bf215546Sopenharmony_ci   dump_samplers(ctx, p[1], 1);
618bf215546Sopenharmony_ci}
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_cistatic void
621bf215546Sopenharmony_cidecode_3dstate_sampler_state_pointers_gfx6(struct aub_viewer_decode_ctx *ctx,
622bf215546Sopenharmony_ci                                           struct intel_group *inst,
623bf215546Sopenharmony_ci                                           const uint32_t *p)
624bf215546Sopenharmony_ci{
625bf215546Sopenharmony_ci   dump_samplers(ctx, p[1], 1);
626bf215546Sopenharmony_ci   dump_samplers(ctx, p[2], 1);
627bf215546Sopenharmony_ci   dump_samplers(ctx, p[3], 1);
628bf215546Sopenharmony_ci}
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_cistatic bool
631bf215546Sopenharmony_cistr_ends_with(const char *str, const char *end)
632bf215546Sopenharmony_ci{
633bf215546Sopenharmony_ci   int offset = strlen(str) - strlen(end);
634bf215546Sopenharmony_ci   if (offset < 0)
635bf215546Sopenharmony_ci      return false;
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_ci   return strcmp(str + offset, end) == 0;
638bf215546Sopenharmony_ci}
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_cistatic void
641bf215546Sopenharmony_cidecode_dynamic_state_pointers(struct aub_viewer_decode_ctx *ctx,
642bf215546Sopenharmony_ci                              struct intel_group *inst, const uint32_t *p,
643bf215546Sopenharmony_ci                              const char *struct_type,  int count)
644bf215546Sopenharmony_ci{
645bf215546Sopenharmony_ci   uint32_t state_offset = 0;
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci   struct intel_field_iterator iter;
648bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
649bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
650bf215546Sopenharmony_ci      if (str_ends_with(iter.name, "Pointer")) {
651bf215546Sopenharmony_ci         state_offset = iter.raw_value;
652bf215546Sopenharmony_ci         break;
653bf215546Sopenharmony_ci      }
654bf215546Sopenharmony_ci   }
655bf215546Sopenharmony_ci
656bf215546Sopenharmony_ci   uint64_t state_addr = ctx->dynamic_base + state_offset;
657bf215546Sopenharmony_ci   struct intel_batch_decode_bo bo = ctx_get_bo(ctx, true, state_addr);
658bf215546Sopenharmony_ci   const uint8_t *state_map = (const uint8_t *) bo.map;
659bf215546Sopenharmony_ci
660bf215546Sopenharmony_ci   if (state_map == NULL) {
661bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->missing_color,
662bf215546Sopenharmony_ci                         "dynamic %s state unavailable addr=0x%012" PRIx64,
663bf215546Sopenharmony_ci                         struct_type, state_addr);
664bf215546Sopenharmony_ci      return;
665bf215546Sopenharmony_ci   }
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci   struct intel_group *state = intel_spec_find_struct(ctx->spec, struct_type);
668bf215546Sopenharmony_ci   if (strcmp(struct_type, "BLEND_STATE") == 0) {
669bf215546Sopenharmony_ci      /* Blend states are different from the others because they have a header
670bf215546Sopenharmony_ci       * struct called BLEND_STATE which is followed by a variable number of
671bf215546Sopenharmony_ci       * BLEND_STATE_ENTRY structs.
672bf215546Sopenharmony_ci       */
673bf215546Sopenharmony_ci      ImGui::Text("%s", struct_type);
674bf215546Sopenharmony_ci      aub_viewer_print_group(ctx, state, state_addr, state_map);
675bf215546Sopenharmony_ci
676bf215546Sopenharmony_ci      state_addr += state->dw_length * 4;
677bf215546Sopenharmony_ci      state_map += state->dw_length * 4;
678bf215546Sopenharmony_ci
679bf215546Sopenharmony_ci      struct_type = "BLEND_STATE_ENTRY";
680bf215546Sopenharmony_ci      state = intel_spec_find_struct(ctx->spec, struct_type);
681bf215546Sopenharmony_ci   }
682bf215546Sopenharmony_ci
683bf215546Sopenharmony_ci   for (int i = 0; i < count; i++) {
684bf215546Sopenharmony_ci      ImGui::Text("%s %d", struct_type, i);
685bf215546Sopenharmony_ci      aub_viewer_print_group(ctx, state, state_addr, state_map);
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci      state_addr += state->dw_length * 4;
688bf215546Sopenharmony_ci      state_map += state->dw_length * 4;
689bf215546Sopenharmony_ci   }
690bf215546Sopenharmony_ci}
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_cistatic void
693bf215546Sopenharmony_cidecode_3dstate_viewport_state_pointers_cc(struct aub_viewer_decode_ctx *ctx,
694bf215546Sopenharmony_ci                                          struct intel_group *inst,
695bf215546Sopenharmony_ci                                          const uint32_t *p)
696bf215546Sopenharmony_ci{
697bf215546Sopenharmony_ci   decode_dynamic_state_pointers(ctx, inst, p, "CC_VIEWPORT", 4);
698bf215546Sopenharmony_ci}
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_cistatic void
701bf215546Sopenharmony_cidecode_3dstate_viewport_state_pointers_sf_clip(struct aub_viewer_decode_ctx *ctx,
702bf215546Sopenharmony_ci                                               struct intel_group *inst,
703bf215546Sopenharmony_ci                                               const uint32_t *p)
704bf215546Sopenharmony_ci{
705bf215546Sopenharmony_ci   decode_dynamic_state_pointers(ctx, inst, p, "SF_CLIP_VIEWPORT", 4);
706bf215546Sopenharmony_ci}
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_cistatic void
709bf215546Sopenharmony_cidecode_3dstate_blend_state_pointers(struct aub_viewer_decode_ctx *ctx,
710bf215546Sopenharmony_ci                                    struct intel_group *inst,
711bf215546Sopenharmony_ci                                    const uint32_t *p)
712bf215546Sopenharmony_ci{
713bf215546Sopenharmony_ci   decode_dynamic_state_pointers(ctx, inst, p, "BLEND_STATE", 1);
714bf215546Sopenharmony_ci}
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_cistatic void
717bf215546Sopenharmony_cidecode_3dstate_cc_state_pointers(struct aub_viewer_decode_ctx *ctx,
718bf215546Sopenharmony_ci                                 struct intel_group *inst,
719bf215546Sopenharmony_ci                                 const uint32_t *p)
720bf215546Sopenharmony_ci{
721bf215546Sopenharmony_ci   decode_dynamic_state_pointers(ctx, inst, p, "COLOR_CALC_STATE", 1);
722bf215546Sopenharmony_ci}
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_cistatic void
725bf215546Sopenharmony_cidecode_3dstate_scissor_state_pointers(struct aub_viewer_decode_ctx *ctx,
726bf215546Sopenharmony_ci                                      struct intel_group *inst,
727bf215546Sopenharmony_ci                                      const uint32_t *p)
728bf215546Sopenharmony_ci{
729bf215546Sopenharmony_ci   decode_dynamic_state_pointers(ctx, inst, p, "SCISSOR_RECT", 1);
730bf215546Sopenharmony_ci}
731bf215546Sopenharmony_ci
732bf215546Sopenharmony_cistatic void
733bf215546Sopenharmony_cidecode_load_register_imm(struct aub_viewer_decode_ctx *ctx,
734bf215546Sopenharmony_ci                         struct intel_group *inst,
735bf215546Sopenharmony_ci                         const uint32_t *p)
736bf215546Sopenharmony_ci{
737bf215546Sopenharmony_ci   struct intel_group *reg = intel_spec_find_register(ctx->spec, p[1]);
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci   if (reg != NULL &&
740bf215546Sopenharmony_ci       ImGui::TreeNodeEx(&p[1], ImGuiTreeNodeFlags_Framed,
741bf215546Sopenharmony_ci                         "%s (0x%x) = 0x%x",
742bf215546Sopenharmony_ci                         reg->name, reg->register_offset, p[2])) {
743bf215546Sopenharmony_ci      aub_viewer_print_group(ctx, reg, reg->register_offset, &p[2]);
744bf215546Sopenharmony_ci      ImGui::TreePop();
745bf215546Sopenharmony_ci   }
746bf215546Sopenharmony_ci}
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_cistatic void
749bf215546Sopenharmony_cidecode_3dprimitive(struct aub_viewer_decode_ctx *ctx,
750bf215546Sopenharmony_ci                   struct intel_group *inst,
751bf215546Sopenharmony_ci                   const uint32_t *p)
752bf215546Sopenharmony_ci{
753bf215546Sopenharmony_ci   if (ctx->display_urb) {
754bf215546Sopenharmony_ci      if (ImGui::Button("Show URB"))
755bf215546Sopenharmony_ci         ctx->display_urb(ctx->user_data, ctx->urb_stages);
756bf215546Sopenharmony_ci   }
757bf215546Sopenharmony_ci}
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_cistatic void
760bf215546Sopenharmony_cihandle_urb(struct aub_viewer_decode_ctx *ctx,
761bf215546Sopenharmony_ci           struct intel_group *inst,
762bf215546Sopenharmony_ci           const uint32_t *p)
763bf215546Sopenharmony_ci{
764bf215546Sopenharmony_ci   struct intel_field_iterator iter;
765bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
766bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
767bf215546Sopenharmony_ci      if (strstr(iter.name, "URB Starting Address")) {
768bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].start = iter.raw_value * 8192;
769bf215546Sopenharmony_ci      } else if (strstr(iter.name, "URB Entry Allocation Size")) {
770bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].size = (iter.raw_value + 1) * 64;
771bf215546Sopenharmony_ci      } else if (strstr(iter.name, "Number of URB Entries")) {
772bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].n_entries = iter.raw_value;
773bf215546Sopenharmony_ci      }
774bf215546Sopenharmony_ci   }
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_ci   ctx->end_urb_offset = MAX2(ctx->urb_stages[ctx->stage].start +
777bf215546Sopenharmony_ci                              ctx->urb_stages[ctx->stage].n_entries *
778bf215546Sopenharmony_ci                              ctx->urb_stages[ctx->stage].size,
779bf215546Sopenharmony_ci                              ctx->end_urb_offset);
780bf215546Sopenharmony_ci}
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_cistatic void
783bf215546Sopenharmony_cihandle_urb_read(struct aub_viewer_decode_ctx *ctx,
784bf215546Sopenharmony_ci                struct intel_group *inst,
785bf215546Sopenharmony_ci                const uint32_t *p)
786bf215546Sopenharmony_ci{
787bf215546Sopenharmony_ci   struct intel_field_iterator iter;
788bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, inst, p, 0, false);
789bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
790bf215546Sopenharmony_ci      /* Workaround the "Force * URB Entry Read Length" fields */
791bf215546Sopenharmony_ci      if (iter.end_bit - iter.start_bit < 2)
792bf215546Sopenharmony_ci         continue;
793bf215546Sopenharmony_ci
794bf215546Sopenharmony_ci      if (strstr(iter.name, "URB Entry Read Offset")) {
795bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].rd_offset = iter.raw_value * 32;
796bf215546Sopenharmony_ci      } else if (strstr(iter.name, "URB Entry Read Length")) {
797bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].rd_length = iter.raw_value * 32;
798bf215546Sopenharmony_ci      } else if (strstr(iter.name, "URB Entry Output Read Offset")) {
799bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].wr_offset = iter.raw_value * 32;
800bf215546Sopenharmony_ci      } else if (strstr(iter.name, "URB Entry Output Length")) {
801bf215546Sopenharmony_ci         ctx->urb_stages[ctx->stage].wr_length = iter.raw_value * 32;
802bf215546Sopenharmony_ci      }
803bf215546Sopenharmony_ci   }
804bf215546Sopenharmony_ci}
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_cistatic void
807bf215546Sopenharmony_cihandle_urb_constant(struct aub_viewer_decode_ctx *ctx,
808bf215546Sopenharmony_ci                    struct intel_group *inst,
809bf215546Sopenharmony_ci                    const uint32_t *p)
810bf215546Sopenharmony_ci{
811bf215546Sopenharmony_ci   struct intel_group *body =
812bf215546Sopenharmony_ci      intel_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci   struct intel_field_iterator outer;
815bf215546Sopenharmony_ci   intel_field_iterator_init(&outer, inst, p, 0, false);
816bf215546Sopenharmony_ci   while (intel_field_iterator_next(&outer)) {
817bf215546Sopenharmony_ci      if (outer.struct_desc != body)
818bf215546Sopenharmony_ci         continue;
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci      struct intel_field_iterator iter;
821bf215546Sopenharmony_ci      intel_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
822bf215546Sopenharmony_ci                                0, false);
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci      ctx->urb_stages[ctx->stage].const_rd_length = 0;
825bf215546Sopenharmony_ci      while (intel_field_iterator_next(&iter)) {
826bf215546Sopenharmony_ci         int idx;
827bf215546Sopenharmony_ci         if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {
828bf215546Sopenharmony_ci            ctx->urb_stages[ctx->stage].const_rd_length += iter.raw_value * 32;
829bf215546Sopenharmony_ci         }
830bf215546Sopenharmony_ci      }
831bf215546Sopenharmony_ci   }
832bf215546Sopenharmony_ci}
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_cistruct custom_decoder {
835bf215546Sopenharmony_ci   const char *cmd_name;
836bf215546Sopenharmony_ci   void (*decode)(struct aub_viewer_decode_ctx *ctx,
837bf215546Sopenharmony_ci                  struct intel_group *inst,
838bf215546Sopenharmony_ci                  const uint32_t *p);
839bf215546Sopenharmony_ci   enum aub_decode_stage stage;
840bf215546Sopenharmony_ci} display_decoders[] = {
841bf215546Sopenharmony_ci   { "STATE_BASE_ADDRESS", handle_state_base_address },
842bf215546Sopenharmony_ci   { "MEDIA_INTERFACE_DESCRIPTOR_LOAD", handle_media_interface_descriptor_load },
843bf215546Sopenharmony_ci   { "3DSTATE_VERTEX_BUFFERS", handle_3dstate_vertex_buffers },
844bf215546Sopenharmony_ci   { "3DSTATE_INDEX_BUFFER", handle_3dstate_index_buffer },
845bf215546Sopenharmony_ci   { "3DSTATE_VS", decode_single_ksp, AUB_DECODE_STAGE_VS, },
846bf215546Sopenharmony_ci   { "3DSTATE_GS", decode_single_ksp, AUB_DECODE_STAGE_GS, },
847bf215546Sopenharmony_ci   { "3DSTATE_DS", decode_single_ksp, AUB_DECODE_STAGE_DS, },
848bf215546Sopenharmony_ci   { "3DSTATE_HS", decode_single_ksp, AUB_DECODE_STAGE_HS, },
849bf215546Sopenharmony_ci   { "3DSTATE_PS", decode_ps_kernels, AUB_DECODE_STAGE_PS, },
850bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_VS", decode_3dstate_constant, AUB_DECODE_STAGE_VS, },
851bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_GS", decode_3dstate_constant, AUB_DECODE_STAGE_GS, },
852bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_DS", decode_3dstate_constant, AUB_DECODE_STAGE_DS, },
853bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_HS", decode_3dstate_constant, AUB_DECODE_STAGE_HS, },
854bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_PS", decode_3dstate_constant, AUB_DECODE_STAGE_PS, },
855bf215546Sopenharmony_ci
856bf215546Sopenharmony_ci   { "3DSTATE_BINDING_TABLE_POINTERS_VS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_VS, },
857bf215546Sopenharmony_ci   { "3DSTATE_BINDING_TABLE_POINTERS_GS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_GS, },
858bf215546Sopenharmony_ci   { "3DSTATE_BINDING_TABLE_POINTERS_HS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_HS, },
859bf215546Sopenharmony_ci   { "3DSTATE_BINDING_TABLE_POINTERS_DS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_DS, },
860bf215546Sopenharmony_ci   { "3DSTATE_BINDING_TABLE_POINTERS_PS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_PS, },
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS_VS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_VS, },
863bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS_GS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_GS, },
864bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS_DS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_DS, },
865bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS_HS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_HS, },
866bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS_PS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_PS, },
867bf215546Sopenharmony_ci   { "3DSTATE_SAMPLER_STATE_POINTERS", decode_3dstate_sampler_state_pointers_gfx6 },
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ci   { "3DSTATE_VIEWPORT_STATE_POINTERS_CC", decode_3dstate_viewport_state_pointers_cc },
870bf215546Sopenharmony_ci   { "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP", decode_3dstate_viewport_state_pointers_sf_clip },
871bf215546Sopenharmony_ci   { "3DSTATE_BLEND_STATE_POINTERS", decode_3dstate_blend_state_pointers },
872bf215546Sopenharmony_ci   { "3DSTATE_CC_STATE_POINTERS", decode_3dstate_cc_state_pointers },
873bf215546Sopenharmony_ci   { "3DSTATE_SCISSOR_STATE_POINTERS", decode_3dstate_scissor_state_pointers },
874bf215546Sopenharmony_ci   { "MI_LOAD_REGISTER_IMM", decode_load_register_imm },
875bf215546Sopenharmony_ci   { "3DPRIMITIVE", decode_3dprimitive },
876bf215546Sopenharmony_ci};
877bf215546Sopenharmony_ci
878bf215546Sopenharmony_cistruct custom_decoder info_decoders[] = {
879bf215546Sopenharmony_ci   { "STATE_BASE_ADDRESS", handle_state_base_address },
880bf215546Sopenharmony_ci   { "3DSTATE_URB_VS", handle_urb, AUB_DECODE_STAGE_VS, },
881bf215546Sopenharmony_ci   { "3DSTATE_URB_GS", handle_urb, AUB_DECODE_STAGE_GS, },
882bf215546Sopenharmony_ci   { "3DSTATE_URB_DS", handle_urb, AUB_DECODE_STAGE_DS, },
883bf215546Sopenharmony_ci   { "3DSTATE_URB_HS", handle_urb, AUB_DECODE_STAGE_HS, },
884bf215546Sopenharmony_ci   { "3DSTATE_VS", handle_urb_read, AUB_DECODE_STAGE_VS, },
885bf215546Sopenharmony_ci   { "3DSTATE_GS", handle_urb_read, AUB_DECODE_STAGE_GS, },
886bf215546Sopenharmony_ci   { "3DSTATE_DS", handle_urb_read, AUB_DECODE_STAGE_DS, },
887bf215546Sopenharmony_ci   { "3DSTATE_HS", handle_urb_read, AUB_DECODE_STAGE_HS, },
888bf215546Sopenharmony_ci   { "3DSTATE_PS", handle_urb_read, AUB_DECODE_STAGE_PS, },
889bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_VS", handle_urb_constant, AUB_DECODE_STAGE_VS, },
890bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_GS", handle_urb_constant, AUB_DECODE_STAGE_GS, },
891bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_DS", handle_urb_constant, AUB_DECODE_STAGE_DS, },
892bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_HS", handle_urb_constant, AUB_DECODE_STAGE_HS, },
893bf215546Sopenharmony_ci   { "3DSTATE_CONSTANT_PS", handle_urb_constant, AUB_DECODE_STAGE_PS, },
894bf215546Sopenharmony_ci};
895bf215546Sopenharmony_ci
896bf215546Sopenharmony_civoid
897bf215546Sopenharmony_ciaub_viewer_render_batch(struct aub_viewer_decode_ctx *ctx,
898bf215546Sopenharmony_ci                        const void *_batch, uint32_t batch_size,
899bf215546Sopenharmony_ci                        uint64_t batch_addr, bool from_ring)
900bf215546Sopenharmony_ci{
901bf215546Sopenharmony_ci   struct intel_group *inst;
902bf215546Sopenharmony_ci   const uint32_t *p, *batch = (const uint32_t *) _batch, *end = batch + batch_size / sizeof(uint32_t);
903bf215546Sopenharmony_ci   int length;
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci   if (ctx->n_batch_buffer_start >= 100) {
906bf215546Sopenharmony_ci      ImGui::TextColored(ctx->cfg->error_color,
907bf215546Sopenharmony_ci                         "0x%08" PRIx64 ": Max batch buffer jumps exceeded", batch_addr);
908bf215546Sopenharmony_ci      return;
909bf215546Sopenharmony_ci   }
910bf215546Sopenharmony_ci
911bf215546Sopenharmony_ci   ctx->n_batch_buffer_start++;
912bf215546Sopenharmony_ci
913bf215546Sopenharmony_ci   for (p = batch; p < end; p += length) {
914bf215546Sopenharmony_ci      inst = intel_spec_find_instruction(ctx->spec, ctx->engine, p);
915bf215546Sopenharmony_ci      length = intel_group_get_length(inst, p);
916bf215546Sopenharmony_ci      assert(inst == NULL || length > 0);
917bf215546Sopenharmony_ci      length = MAX2(1, length);
918bf215546Sopenharmony_ci
919bf215546Sopenharmony_ci      uint64_t offset = batch_addr + ((char *)p - (char *)batch);
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ci      if (inst == NULL) {
922bf215546Sopenharmony_ci         ImGui::TextColored(ctx->cfg->error_color,
923bf215546Sopenharmony_ci                            "0x%012" PRIx64 ": unknown instruction %012x",
924bf215546Sopenharmony_ci                            offset, p[0]);
925bf215546Sopenharmony_ci         continue;
926bf215546Sopenharmony_ci      }
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci      const char *inst_name = intel_group_get_name(inst);
929bf215546Sopenharmony_ci
930bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(info_decoders); i++) {
931bf215546Sopenharmony_ci         if (strcmp(inst_name, info_decoders[i].cmd_name) == 0) {
932bf215546Sopenharmony_ci            ctx->stage = info_decoders[i].stage;
933bf215546Sopenharmony_ci            info_decoders[i].decode(ctx, inst, p);
934bf215546Sopenharmony_ci            break;
935bf215546Sopenharmony_ci         }
936bf215546Sopenharmony_ci      }
937bf215546Sopenharmony_ci
938bf215546Sopenharmony_ci      if (ctx->decode_cfg->command_filter.PassFilter(inst->name) &&
939bf215546Sopenharmony_ci          ImGui::TreeNodeEx(p,
940bf215546Sopenharmony_ci                            ImGuiTreeNodeFlags_Framed,
941bf215546Sopenharmony_ci                            "0x%012" PRIx64 ":  %s",
942bf215546Sopenharmony_ci                            offset, inst->name)) {
943bf215546Sopenharmony_ci         aub_viewer_print_group(ctx, inst, offset, p);
944bf215546Sopenharmony_ci
945bf215546Sopenharmony_ci         for (unsigned i = 0; i < ARRAY_SIZE(display_decoders); i++) {
946bf215546Sopenharmony_ci            if (strcmp(inst_name, display_decoders[i].cmd_name) == 0) {
947bf215546Sopenharmony_ci               ctx->stage = display_decoders[i].stage;
948bf215546Sopenharmony_ci               display_decoders[i].decode(ctx, inst, p);
949bf215546Sopenharmony_ci               break;
950bf215546Sopenharmony_ci            }
951bf215546Sopenharmony_ci         }
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci         if (ctx->edit_address) {
954bf215546Sopenharmony_ci            if (ImGui::Button("Edit instruction"))
955bf215546Sopenharmony_ci               ctx->edit_address(ctx->user_data, offset, length * 4);
956bf215546Sopenharmony_ci         }
957bf215546Sopenharmony_ci
958bf215546Sopenharmony_ci         ImGui::TreePop();
959bf215546Sopenharmony_ci      }
960bf215546Sopenharmony_ci
961bf215546Sopenharmony_ci      if (strcmp(inst_name, "MI_BATCH_BUFFER_START") == 0) {
962bf215546Sopenharmony_ci         uint64_t next_batch_addr = 0xd0d0d0d0;
963bf215546Sopenharmony_ci         bool ppgtt = false;
964bf215546Sopenharmony_ci         bool second_level = false;
965bf215546Sopenharmony_ci         struct intel_field_iterator iter;
966bf215546Sopenharmony_ci         intel_field_iterator_init(&iter, inst, p, 0, false);
967bf215546Sopenharmony_ci         while (intel_field_iterator_next(&iter)) {
968bf215546Sopenharmony_ci            if (strcmp(iter.name, "Batch Buffer Start Address") == 0) {
969bf215546Sopenharmony_ci               next_batch_addr = iter.raw_value;
970bf215546Sopenharmony_ci            } else if (strcmp(iter.name, "Second Level Batch Buffer") == 0) {
971bf215546Sopenharmony_ci               second_level = iter.raw_value;
972bf215546Sopenharmony_ci            } else if (strcmp(iter.name, "Address Space Indicator") == 0) {
973bf215546Sopenharmony_ci               ppgtt = iter.raw_value;
974bf215546Sopenharmony_ci            }
975bf215546Sopenharmony_ci         }
976bf215546Sopenharmony_ci
977bf215546Sopenharmony_ci         struct intel_batch_decode_bo next_batch = ctx_get_bo(ctx, ppgtt, next_batch_addr);
978bf215546Sopenharmony_ci
979bf215546Sopenharmony_ci         if (next_batch.map == NULL) {
980bf215546Sopenharmony_ci            ImGui::TextColored(ctx->cfg->missing_color,
981bf215546Sopenharmony_ci                               "Secondary batch at 0x%012" PRIx64 " unavailable",
982bf215546Sopenharmony_ci                               next_batch_addr);
983bf215546Sopenharmony_ci         } else {
984bf215546Sopenharmony_ci            aub_viewer_render_batch(ctx, next_batch.map, next_batch.size,
985bf215546Sopenharmony_ci                                    next_batch.addr, false);
986bf215546Sopenharmony_ci         }
987bf215546Sopenharmony_ci         if (second_level) {
988bf215546Sopenharmony_ci            /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
989bf215546Sopenharmony_ci             * like a subroutine call.  Commands that come afterwards get
990bf215546Sopenharmony_ci             * processed once the 2nd level batch buffer returns with
991bf215546Sopenharmony_ci             * MI_BATCH_BUFFER_END.
992bf215546Sopenharmony_ci             */
993bf215546Sopenharmony_ci            continue;
994bf215546Sopenharmony_ci         } else if (!from_ring) {
995bf215546Sopenharmony_ci            /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
996bf215546Sopenharmony_ci             * like a goto.  Nothing after it will ever get processed.  In
997bf215546Sopenharmony_ci             * order to prevent the recursion from growing, we just reset the
998bf215546Sopenharmony_ci             * loop and continue;
999bf215546Sopenharmony_ci             */
1000bf215546Sopenharmony_ci            break;
1001bf215546Sopenharmony_ci         }
1002bf215546Sopenharmony_ci      } else if (strcmp(inst_name, "MI_BATCH_BUFFER_END") == 0) {
1003bf215546Sopenharmony_ci         break;
1004bf215546Sopenharmony_ci      }
1005bf215546Sopenharmony_ci   }
1006bf215546Sopenharmony_ci
1007bf215546Sopenharmony_ci   ctx->n_batch_buffer_start--;
1008bf215546Sopenharmony_ci}
1009