Path: blob/21.2-virgl/src/broadcom/clif/clif_dump.c
4560 views
/*1* Copyright © 2016 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <stdio.h>24#include <stdlib.h>25#include <string.h>26#include "drm-uapi/v3d_drm.h"27#include "clif_dump.h"28#include "clif_private.h"29#include "util/list.h"30#include "util/ralloc.h"3132#include "broadcom/cle/v3d_decoder.h"3334struct reloc_worklist_entry *35clif_dump_add_address_to_worklist(struct clif_dump *clif,36enum reloc_worklist_type type,37uint32_t addr)38{39struct reloc_worklist_entry *entry =40rzalloc(clif, struct reloc_worklist_entry);41if (!entry)42return NULL;4344entry->type = type;45entry->addr = addr;4647list_addtail(&entry->link, &clif->worklist);4849return entry;50}5152struct clif_dump *53clif_dump_init(const struct v3d_device_info *devinfo,54FILE *out, bool pretty)55{56struct clif_dump *clif = rzalloc(NULL, struct clif_dump);5758clif->devinfo = devinfo;59clif->out = out;60clif->spec = v3d_spec_load(devinfo);61clif->pretty = pretty;6263list_inithead(&clif->worklist);6465return clif;66}6768void69clif_dump_destroy(struct clif_dump *clif)70{71ralloc_free(clif);72}7374struct clif_bo *75clif_lookup_bo(struct clif_dump *clif, uint32_t addr)76{77for (int i = 0; i < clif->bo_count; i++) {78struct clif_bo *bo = &clif->bo[i];7980if (addr >= bo->offset &&81addr < bo->offset + bo->size) {82return bo;83}84}8586return NULL;87}8889static bool90clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)91{92struct clif_bo *bo = clif_lookup_bo(clif, addr);93if (!bo)94return false;9596*vaddr = bo->vaddr + addr - bo->offset;97return true;98}99100#define out_uint(_clif, field) out(_clif, " /* %s = */ %u\n", \101#field, values-> field);102103static bool104clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,105uint32_t *size, bool reloc_mode)106{107if (clif->devinfo->ver >= 42)108return v3d42_clif_dump_packet(clif, offset, cl, size, reloc_mode);109else if (clif->devinfo->ver >= 41)110return v3d41_clif_dump_packet(clif, offset, cl, size, reloc_mode);111else112return v3d33_clif_dump_packet(clif, offset, cl, size, reloc_mode);113}114115static uint32_t116clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end,117bool reloc_mode)118{119struct clif_bo *bo = clif_lookup_bo(clif, start);120if (!bo) {121out(clif, "Failed to look up address 0x%08x\n",122start);123return 0;124}125126void *start_vaddr = bo->vaddr + start - bo->offset;127128/* The end address is optional (for example, a BRANCH instruction129* won't set an end), but is used for BCL/RCL termination.130*/131void *end_vaddr = NULL;132if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {133out(clif, "Failed to look up address 0x%08x\n",134end);135return 0;136}137138if (!reloc_mode)139out(clif, "@format ctrllist /* [%s+0x%08x] */\n",140bo->name, start - bo->offset);141142uint32_t size;143uint8_t *cl = start_vaddr;144while (clif_dump_packet(clif, start, cl, &size, reloc_mode)) {145cl += size;146start += size;147148if (cl == end_vaddr)149break;150}151152return (void *)cl - bo->vaddr;153}154155/* Walks the worklist, parsing the relocs for any memory regions that might156* themselves have additional relocations.157*/158static uint32_t159clif_dump_gl_shader_state_record(struct clif_dump *clif,160struct reloc_worklist_entry *reloc,161void *vaddr)162{163struct v3d_group *state = v3d_spec_find_struct(clif->spec,164"GL Shader State Record");165struct v3d_group *attr = v3d_spec_find_struct(clif->spec,166"GL Shader State Attribute Record");167assert(state);168assert(attr);169uint32_t offset = 0;170171out(clif, "@format shadrec_gl_main\n");172v3d_print_group(clif, state, 0, vaddr + offset);173offset += v3d_group_get_length(state);174175for (int i = 0; i < reloc->shader_state.num_attrs; i++) {176out(clif, "@format shadrec_gl_attr /* %d */\n", i);177v3d_print_group(clif, attr, 0, vaddr + offset);178offset += v3d_group_get_length(attr);179}180181return offset;182}183184static void185clif_process_worklist(struct clif_dump *clif)186{187list_for_each_entry_safe(struct reloc_worklist_entry, reloc,188&clif->worklist, link) {189void *vaddr;190if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {191out(clif, "Failed to look up address 0x%08x\n",192reloc->addr);193continue;194}195196switch (reloc->type) {197case reloc_cl:198clif_dump_cl(clif, reloc->addr, reloc->cl.end, true);199break;200201case reloc_gl_shader_state:202break;203case reloc_generic_tile_list:204clif_dump_cl(clif, reloc->addr,205reloc->generic_tile_list.end, true);206break;207}208}209}210211static int212worklist_entry_compare(const void *a, const void *b)213{214return ((*(struct reloc_worklist_entry **)a)->addr -215(*(struct reloc_worklist_entry **)b)->addr);216}217218static bool219clif_dump_if_blank(struct clif_dump *clif, struct clif_bo *bo,220uint32_t start, uint32_t end)221{222for (int i = start; i < end; i++) {223if (((uint8_t *)bo->vaddr)[i] != 0)224return false;225}226227out(clif, "\n");228out(clif, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end - start,229bo->name, start, end - 1);230return true;231}232233/* Dumps the binary data in the BO from start to end (relative to the start of234* the BO).235*/236static void237clif_dump_binary(struct clif_dump *clif, struct clif_bo *bo,238uint32_t start, uint32_t end)239{240if (start == end)241return;242243if (clif_dump_if_blank(clif, bo, start, end))244return;245246out(clif, "@format binary /* [%s+0x%08x] */\n",247bo->name, start);248249uint32_t offset = start;250int dumped_in_line = 0;251while (offset < end) {252if (clif_dump_if_blank(clif, bo, offset, end))253return;254255if (end - offset >= 4) {256out(clif, "0x%08x ", *(uint32_t *)(bo->vaddr + offset));257offset += 4;258} else {259out(clif, "0x%02x ", *(uint8_t *)(bo->vaddr + offset));260offset++;261}262263if (++dumped_in_line == 8) {264out(clif, "\n");265dumped_in_line = 0;266}267}268if (dumped_in_line)269out(clif, "\n");270}271272/* Walks the list of relocations, dumping each buffer's contents (using our273* codegenned dump routines for pretty printing, and most importantly proper274* address references so that the CLIF parser can relocate buffers).275*/276static void277clif_dump_buffers(struct clif_dump *clif)278{279int num_relocs = 0;280list_for_each_entry(struct reloc_worklist_entry, reloc,281&clif->worklist, link) {282num_relocs++;283}284struct reloc_worklist_entry **relocs =285ralloc_array(clif, struct reloc_worklist_entry *, num_relocs);286int i = 0;287list_for_each_entry(struct reloc_worklist_entry, reloc,288&clif->worklist, link) {289relocs[i++] = reloc;290}291qsort(relocs, num_relocs, sizeof(*relocs), worklist_entry_compare);292293struct clif_bo *bo = NULL;294uint32_t offset = 0;295296for (i = 0; i < num_relocs; i++) {297struct reloc_worklist_entry *reloc = relocs[i];298struct clif_bo *new_bo = clif_lookup_bo(clif, reloc->addr);299300if (!new_bo) {301out(clif, "Failed to look up address 0x%08x\n",302reloc->addr);303continue;304}305306if (new_bo != bo) {307if (bo) {308/* Finish out the last of the last BO. */309clif_dump_binary(clif, bo,310offset,311bo->size);312}313314out(clif, "\n");315out(clif, "@buffer %s\n", new_bo->name);316bo = new_bo;317offset = 0;318bo->dumped = true;319}320321int reloc_offset = reloc->addr - bo->offset;322if (offset != reloc_offset)323clif_dump_binary(clif, bo, offset, reloc_offset);324offset = reloc_offset;325326switch (reloc->type) {327case reloc_cl:328offset = clif_dump_cl(clif, reloc->addr, reloc->cl.end,329false);330out(clif, "\n");331break;332333case reloc_gl_shader_state:334offset += clif_dump_gl_shader_state_record(clif,335reloc,336bo->vaddr +337offset);338break;339case reloc_generic_tile_list:340offset = clif_dump_cl(clif, reloc->addr,341reloc->generic_tile_list.end,342false);343break;344}345out(clif, "\n");346}347348if (bo) {349clif_dump_binary(clif, bo, offset, bo->size);350}351352/* For any BOs that didn't have relocations, just dump them raw. */353for (int i = 0; i < clif->bo_count; i++) {354bo = &clif->bo[i];355if (bo->dumped)356continue;357out(clif, "@buffer %s\n", bo->name);358clif_dump_binary(clif, bo, 0, bo->size);359out(clif, "\n");360}361}362363void364clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)365{366struct reloc_worklist_entry *entry =367clif_dump_add_address_to_worklist(clif, reloc_cl, start);368369entry->cl.end = end;370}371372static int373clif_bo_offset_compare(const void *a, const void *b)374{375return ((struct clif_bo *)a)->offset - ((struct clif_bo *)b)->offset;376}377378void379clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)380{381clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);382clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);383384qsort(clif->bo, clif->bo_count, sizeof(clif->bo[0]),385clif_bo_offset_compare);386387/* A buffer needs to be defined before we can emit a CLIF address388* referencing it, so emit them all now.389*/390for (int i = 0; i < clif->bo_count; i++) {391out(clif, "@createbuf_aligned 4096 %s\n", clif->bo[i].name);392}393394/* Walk the worklist figuring out the locations of structs based on395* the CL contents.396*/397clif_process_worklist(clif);398399/* Dump the contents of the buffers using the relocations we found to400* pretty-print structures.401*/402clif_dump_buffers(clif);403404out(clif, "@add_bin 0\n ");405out_address(clif, submit->bcl_start);406out(clif, "\n ");407out_address(clif, submit->bcl_end);408out(clif, "\n ");409out_address(clif, submit->qma);410out(clif, "\n %d\n ", submit->qms);411out_address(clif, submit->qts);412out(clif, "\n");413out(clif, "@wait_bin_all_cores\n");414415out(clif, "@add_render 0\n ");416out_address(clif, submit->rcl_start);417out(clif, "\n ");418out_address(clif, submit->rcl_end);419out(clif, "\n ");420out_address(clif, submit->qma);421out(clif, "\n");422out(clif, "@wait_render_all_cores\n");423}424425void426clif_dump_add_bo(struct clif_dump *clif, const char *name,427uint32_t offset, uint32_t size, void *vaddr)428{429if (clif->bo_count >= clif->bo_array_size) {430clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);431clif->bo = reralloc(clif, clif->bo, struct clif_bo,432clif->bo_array_size);433}434435/* CLIF relocs use the buffer name, so make sure they're unique. */436for (int i = 0; i < clif->bo_count; i++)437assert(strcmp(clif->bo[i].name, name) != 0);438439clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);440clif->bo[clif->bo_count].offset = offset;441clif->bo[clif->bo_count].size = size;442clif->bo[clif->bo_count].vaddr = vaddr;443clif->bo[clif->bo_count].dumped = false;444clif->bo_count++;445}446447448