+ return ((*(struct reloc_worklist_entry **)a)->addr -
+ (*(struct reloc_worklist_entry **)b)->addr);
+}
+
+static bool
+clif_dump_if_blank(struct clif_dump *clif, struct clif_bo *bo,
+ uint32_t start, uint32_t end)
+{
+ for (int i = start; i < end; i++) {
+ if (((uint8_t *)bo->vaddr)[i] != 0)
+ return false;
+ }
+
+ out(clif, "\n");
+ out(clif, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end - start,
+ bo->name, start, end - 1);
+ return true;
+}
+
+/* Dumps the binary data in the BO from start to end (relative to the start of
+ * the BO).
+ */
+static void
+clif_dump_binary(struct clif_dump *clif, struct clif_bo *bo,
+ uint32_t start, uint32_t end)
+{
+ if (start == end)
+ return;
+
+ if (clif_dump_if_blank(clif, bo, start, end))
+ return;
+
+ out(clif, "@format binary /* [%s+0x%08x] */\n",
+ bo->name, start);
+
+ uint32_t offset = start;
+ int dumped_in_line = 0;
+ while (offset < end) {
+ if (clif_dump_if_blank(clif, bo, offset, end))
+ return;
+
+ if (end - offset >= 4) {
+ out(clif, "0x%08x ", *(uint32_t *)(bo->vaddr + offset));
+ offset += 4;
+ } else {
+ out(clif, "0x%02x ", *(uint8_t *)(bo->vaddr + offset));
+ offset++;
+ }
+
+ if (++dumped_in_line == 8) {
+ out(clif, "\n");
+ dumped_in_line = 0;
+ }
+ }
+ if (dumped_in_line)
+ out(clif, "\n");
+}
+
+/* Walks the list of relocations, dumping each buffer's contents (using our
+ * codegenned dump routines for pretty printing, and most importantly proper
+ * address references so that the CLIF parser can relocate buffers).
+ */
+static void
+clif_dump_buffers(struct clif_dump *clif)
+{
+ int num_relocs = 0;
+ list_for_each_entry(struct reloc_worklist_entry, reloc,
+ &clif->worklist, link) {
+ num_relocs++;
+ }
+ struct reloc_worklist_entry **relocs =
+ ralloc_array(clif, struct reloc_worklist_entry *, num_relocs);
+ int i = 0;
+ list_for_each_entry(struct reloc_worklist_entry, reloc,
+ &clif->worklist, link) {
+ relocs[i++] = reloc;
+ }
+ qsort(relocs, num_relocs, sizeof(*relocs), worklist_entry_compare);
+
+ struct clif_bo *bo = NULL;
+ uint32_t offset = 0;
+
+ for (i = 0; i < num_relocs; i++) {
+ struct reloc_worklist_entry *reloc = relocs[i];
+ struct clif_bo *new_bo = clif_lookup_bo(clif, reloc->addr);
+
+ if (!new_bo) {
+ out(clif, "Failed to look up address 0x%08x\n",
+ reloc->addr);
+ continue;
+ }
+
+ if (new_bo != bo) {
+ if (bo) {
+ /* Finish out the last of the last BO. */
+ clif_dump_binary(clif, bo,
+ offset,
+ bo->size);
+ }
+
+ out(clif, "\n");
+ out(clif, "@buffer %s\n", new_bo->name);
+ bo = new_bo;
+ offset = 0;
+ bo->dumped = true;
+ }
+
+ int reloc_offset = reloc->addr - bo->offset;
+ if (offset != reloc_offset)
+ clif_dump_binary(clif, bo, offset, reloc_offset);
+ offset = reloc_offset;
+
+ switch (reloc->type) {
+ case reloc_cl:
+ offset = clif_dump_cl(clif, reloc->addr, reloc->cl.end,
+ false);
+ out(clif, "\n");
+ break;
+
+ case reloc_gl_shader_state:
+ offset += clif_dump_gl_shader_state_record(clif,
+ reloc,
+ bo->vaddr +
+ offset);
+ break;
+ case reloc_generic_tile_list:
+ offset = clif_dump_cl(clif, reloc->addr,
+ reloc->generic_tile_list.end,
+ false);
+ break;
+ }
+ out(clif, "\n");
+ }
+
+ if (bo) {
+ clif_dump_binary(clif, bo, offset, bo->size);
+ }
+
+ /* For any BOs that didn't have relocations, just dump them raw. */
+ for (int i = 0; i < clif->bo_count; i++) {
+ bo = &clif->bo[i];
+ if (bo->dumped)
+ continue;
+ out(clif, "@buffer %s\n", bo->name);
+ clif_dump_binary(clif, bo, 0, bo->size);
+ out(clif, "\n");
+ }