xref: /aosp_15_r20/external/mesa3d/src/freedreno/decode/crashdec.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Google, Inc.
3  * SPDX-License-Identifier: MIT
4  */
5 
6 /*
7  * Decoder for devcoredump traces from drm/msm.  In case of a gpu crash/hang,
8  * the coredump should be found in:
9  *
10  *    /sys/class/devcoredump/devcd<n>/data
11  *
12  * The crashdump will hang around for 5min, it can be cleared by writing to
13  * the file, ie:
14  *
15  *    echo 1 > /sys/class/devcoredump/devcd<n>/data
16  *
17  * (the driver won't log any new crashdumps until the previous one is cleared
18  * or times out after 5min)
19  */
20 
21 
22 #include "crashdec.h"
23 
24 static FILE *in;
25 bool verbose;
26 
27 struct rnn *rnn_gmu;
28 struct rnn *rnn_control;
29 struct rnn *rnn_pipe;
30 
31 static uint64_t fault_iova;
32 static bool has_fault_iova;
33 
34 struct cffdec_options options = {
35    .draw_filter = -1,
36 };
37 
38 /*
39  * Helpers to read register values:
40  */
41 
42 /* read registers that are 64b on 64b GPUs (ie. a5xx+) */
43 static uint64_t
regval64(const char * name)44 regval64(const char *name)
45 {
46    unsigned reg = regbase(name);
47    assert(reg);
48    uint64_t val = reg_val(reg);
49    if (is_64b())
50       val |= ((uint64_t)reg_val(reg + 1)) << 32;
51    return val;
52 }
53 
54 static uint32_t
regval(const char * name)55 regval(const char *name)
56 {
57    unsigned reg = regbase(name);
58    assert(reg);
59    return reg_val(reg);
60 }
61 
62 /*
63  * Line reading and string helpers:
64  */
65 
66 static char *
replacestr(char * line,const char * find,const char * replace)67 replacestr(char *line, const char *find, const char *replace)
68 {
69    char *tail, *s;
70 
71    if (!(s = strstr(line, find)))
72       return line;
73 
74    tail = s + strlen(find);
75 
76    char *newline;
77    asprintf(&newline, "%.*s%s%s", (int)(s - line), line, replace, tail);
78    free(line);
79 
80    return newline;
81 }
82 
83 static char *lastline;
84 static char *pushedline;
85 
86 static const char *
popline(void)87 popline(void)
88 {
89    char *r = pushedline;
90 
91    if (r) {
92       pushedline = NULL;
93       return r;
94    }
95 
96    free(lastline);
97 
98    size_t n = 0;
99    if (getline(&r, &n, in) < 0)
100       exit(0);
101 
102    /* Handle section name typo's from earlier kernels: */
103    r = replacestr(r, "CP_MEMPOOOL", "CP_MEMPOOL");
104    r = replacestr(r, "CP_SEQ_STAT", "CP_SQE_STAT");
105 
106    lastline = r;
107    return r;
108 }
109 
110 static void
pushline(void)111 pushline(void)
112 {
113    assert(!pushedline);
114    pushedline = lastline;
115 }
116 
117 static uint32_t *
popline_ascii85(uint32_t sizedwords)118 popline_ascii85(uint32_t sizedwords)
119 {
120    const char *line = popline();
121 
122    /* At this point we exepct the ascii85 data to be indented *some*
123     * amount, and to terminate at the end of the line.  So just eat
124     * up the leading whitespace.
125     */
126    assert(*line == ' ');
127    while (*line == ' ')
128       line++;
129 
130    uint32_t *buf = calloc(1, 4 * sizedwords);
131    int idx = 0;
132 
133    while (*line != '\n') {
134       if (*line == 'z') {
135          buf[idx++] = 0;
136          line++;
137          continue;
138       }
139 
140       uint32_t accum = 0;
141       for (int i = 0; (i < 5) && (*line != '\n'); i++) {
142          accum *= 85;
143          accum += *line - '!';
144          line++;
145       }
146 
147       buf[idx++] = accum;
148    }
149 
150    return buf;
151 }
152 
153 static bool
startswith(const char * line,const char * start)154 startswith(const char *line, const char *start)
155 {
156    return strstr(line, start) == line;
157 }
158 
159 static bool
startswith_nowhitespace(const char * line,const char * start)160 startswith_nowhitespace(const char *line, const char *start)
161 {
162    while (*line == ' ' || *line == '\t')
163       line++;
164    return startswith(line, start);
165 }
166 
167 static void
vparseline(const char * line,const char * fmt,va_list ap)168 vparseline(const char *line, const char *fmt, va_list ap)
169 {
170    int fmtlen = strlen(fmt);
171    int n = 0;
172    int l = 0;
173 
174    /* scan fmt string to extract expected # of conversions: */
175    for (int i = 0; i < fmtlen; i++) {
176       if (fmt[i] == '%') {
177          if (i == (l - 1)) { /* prev char was %, ie. we have %% */
178             n--;
179             l = 0;
180          } else {
181             n++;
182             l = i;
183          }
184       }
185    }
186 
187    if (vsscanf(line, fmt, ap) != n) {
188       fprintf(stderr, "parse error scanning: '%s'\n", fmt);
189       exit(1);
190    }
191 }
192 
193 static void
parseline(const char * line,const char * fmt,...)194 parseline(const char *line, const char *fmt, ...)
195 {
196    va_list ap;
197    va_start(ap, fmt);
198    vparseline(line, fmt, ap);
199    va_end(ap);
200 }
201 
202 static void
parseline_nowhitespace(const char * line,const char * fmt,...)203 parseline_nowhitespace(const char *line, const char *fmt, ...)
204 {
205    while (*line == ' ' || *line == '\t')
206       line++;
207 
208    va_list ap;
209    va_start(ap, fmt);
210    vparseline(line, fmt, ap);
211    va_end(ap);
212 }
213 
214 #define foreach_line_in_section(_line)                                         \
215    for (const char *_line = popline(); _line; _line = popline())               \
216       /* check for start of next section */                                    \
217       if (_line[0] != ' ') {                                                   \
218          pushline();                                                           \
219          break;                                                                \
220       } else
221 
222 /*
223  * Decode ringbuffer section:
224  */
225 
226 static struct {
227    uint64_t iova;
228    uint32_t rptr;
229    uint32_t wptr;
230    uint32_t size;
231    uint32_t *buf;
232 } ringbuffers[5];
233 
234 static void
decode_ringbuffer(void)235 decode_ringbuffer(void)
236 {
237    int id = 0;
238 
239    foreach_line_in_section (line) {
240       if (startswith(line, "  - id:")) {
241          parseline(line, "  - id: %d", &id);
242          assert(id < ARRAY_SIZE(ringbuffers));
243       } else if (startswith(line, "    iova:")) {
244          parseline(line, "    iova: %" PRIx64, &ringbuffers[id].iova);
245       } else if (startswith(line, "    rptr:")) {
246          parseline(line, "    rptr: %d", &ringbuffers[id].rptr);
247       } else if (startswith(line, "    wptr:")) {
248          parseline(line, "    wptr: %d", &ringbuffers[id].wptr);
249       } else if (startswith(line, "    size:")) {
250          parseline(line, "    size: %d", &ringbuffers[id].size);
251       } else if (startswith(line, "    data: !!ascii85 |")) {
252          ringbuffers[id].buf = popline_ascii85(ringbuffers[id].size / 4);
253          add_buffer(ringbuffers[id].iova, ringbuffers[id].size,
254                     ringbuffers[id].buf);
255          continue;
256       }
257 
258       printf("%s", line);
259    }
260 }
261 
262 /*
263  * Decode GMU log
264  */
265 
266 static void
decode_gmu_log(void)267 decode_gmu_log(void)
268 {
269    uint64_t iova;
270    uint32_t size;
271 
272    foreach_line_in_section (line) {
273       if (startswith(line, "    iova:")) {
274          parseline(line, "    iova: %" PRIx64, &iova);
275       } else if (startswith(line, "    size:")) {
276          parseline(line, "    size: %u", &size);
277       } else if (startswith(line, "    data: !!ascii85 |")) {
278          void *buf = popline_ascii85(size / 4);
279 
280          dump_hex_ascii(buf, size, 1);
281 
282          free(buf);
283 
284          continue;
285       }
286 
287       printf("%s", line);
288    }
289 }
290 
291 /*
292  * Decode HFI queues
293  */
294 
295 static void
decode_gmu_hfi(void)296 decode_gmu_hfi(void)
297 {
298    struct a6xx_hfi_state hfi = {};
299 
300    /* Initialize the history buffers with invalid entries (-1): */
301    memset(&hfi.history, 0xff, sizeof(hfi.history));
302 
303    foreach_line_in_section (line) {
304       if (startswith(line, "    iova:")) {
305          parseline(line, "    iova: %" PRIx64, &hfi.iova);
306       } else if (startswith(line, "    size:")) {
307          parseline(line, "    size: %u", &hfi.size);
308       } else if (startswith(line, "    queue-history")) {
309          unsigned qidx, dummy;
310 
311          parseline(line, "    queue-history[%u]:", &qidx);
312          assert(qidx < ARRAY_SIZE(hfi.history));
313 
314          parseline(line, "    queue-history[%u]: %d %d %d %d %d %d %d %d", &dummy,
315                    &hfi.history[qidx][0], &hfi.history[qidx][1],
316                    &hfi.history[qidx][2], &hfi.history[qidx][3],
317                    &hfi.history[qidx][4], &hfi.history[qidx][5],
318                    &hfi.history[qidx][6], &hfi.history[qidx][7]);
319       } else if (startswith(line, "    data: !!ascii85 |")) {
320          hfi.buf = popline_ascii85(hfi.size / 4);
321 
322          if (verbose)
323             dump_hex_ascii(hfi.buf, hfi.size, 1);
324 
325          dump_gmu_hfi(&hfi);
326 
327          free(hfi.buf);
328 
329          continue;
330       }
331 
332       printf("%s", line);
333    }
334 }
335 
336 static bool
valid_header(uint32_t pkt)337 valid_header(uint32_t pkt)
338 {
339    if (options.info->chip >= 5) {
340       return pkt_is_type4(pkt) || pkt_is_type7(pkt);
341    } else {
342       /* TODO maybe we can check validish looking pkt3 opc or pkt0
343        * register offset.. the cmds sent by kernel are usually
344        * fairly limited (other than initialization) which confines
345        * the search space a bit..
346        */
347       return true;
348    }
349 }
350 
351 static void
dump_cmdstream(void)352 dump_cmdstream(void)
353 {
354    uint64_t rb_base = regval64("CP_RB_BASE");
355 
356    printf("got rb_base=%" PRIx64 "\n", rb_base);
357 
358    options.ibs[1].base = regval64("CP_IB1_BASE");
359    if (have_rem_info())
360       options.ibs[1].rem = regval("CP_IB1_REM_SIZE");
361    options.ibs[2].base = regval64("CP_IB2_BASE");
362    if (have_rem_info())
363       options.ibs[2].rem = regval("CP_IB2_REM_SIZE");
364 
365    /* Adjust remaining size to account for cmdstream slurped into ROQ
366     * but not yet consumed by SQE
367     *
368     * TODO add support for earlier GPUs once we tease out the needed
369     * registers.. see crashit.c in msmtest for hints.
370     *
371     * TODO it would be nice to be able to extract out register bitfields
372     * by name rather than hard-coding this.
373     */
374    if (have_rem_info()) {
375       uint32_t ib1_rem = regval("CP_ROQ_AVAIL_IB1") >> 16;
376       uint32_t ib2_rem = regval("CP_ROQ_AVAIL_IB2") >> 16;
377       options.ibs[1].rem += ib1_rem ? ib1_rem - 1 : 0;
378       options.ibs[2].rem += ib2_rem ? ib2_rem - 1 : 0;
379    }
380 
381    printf("IB1: %" PRIx64 ", %u\n", options.ibs[1].base, options.ibs[1].rem);
382    printf("IB2: %" PRIx64 ", %u\n", options.ibs[2].base, options.ibs[2].rem);
383 
384    /* now that we've got the regvals we want, reset register state
385     * so we aren't seeing values from decode_registers();
386     */
387    reset_regs();
388 
389    for (int id = 0; id < ARRAY_SIZE(ringbuffers); id++) {
390       if (ringbuffers[id].iova != rb_base)
391          continue;
392       if (!ringbuffers[id].size)
393          continue;
394 
395       printf("found ring!\n");
396 
397       /* The kernel level ringbuffer (RB) wraps around, which
398        * cffdec doesn't really deal with.. so figure out how
399        * many dwords are unread
400        */
401       unsigned ringszdw = ringbuffers[id].size >> 2; /* in dwords */
402 
403       if (verbose) {
404          handle_prefetch(ringbuffers[id].buf, ringszdw);
405          dump_commands(ringbuffers[id].buf, ringszdw, 0);
406          return;
407       }
408 
409 /* helper macro to deal with modulo size math: */
410 #define mod_add(b, v) ((ringszdw + (int)(b) + (int)(v)) % ringszdw)
411 
412       /* The rptr will (most likely) have moved past the IB to
413        * userspace cmdstream, so back up a bit, and then advance
414        * until we find a valid start of a packet.. this is going
415        * to be less reliable on a4xx and before (pkt0/pkt3),
416        * compared to pkt4/pkt7 with parity bits
417        */
418       const int lookback = 12;
419       unsigned rptr = mod_add(ringbuffers[id].rptr, -lookback);
420 
421       for (int idx = 0; idx < lookback; idx++) {
422          if (valid_header(ringbuffers[id].buf[rptr]))
423             break;
424          rptr = mod_add(rptr, 1);
425       }
426 
427       unsigned cmdszdw = mod_add(ringbuffers[id].wptr, -rptr);
428 
429       printf("got cmdszdw=%d\n", cmdszdw);
430       uint32_t *buf = malloc(cmdszdw * 4);
431 
432       for (int idx = 0; idx < cmdszdw; idx++) {
433          int p = mod_add(rptr, idx);
434          buf[idx] = ringbuffers[id].buf[p];
435       }
436 
437       handle_prefetch(buf, cmdszdw);
438       dump_commands(buf, cmdszdw, 0);
439       free(buf);
440    }
441 }
442 
443 /*
444  * Decode optional 'fault-info' section.  We only get this section if
445  * the devcoredump was triggered by an iova fault:
446  */
447 
448 static void
decode_fault_info(void)449 decode_fault_info(void)
450 {
451    foreach_line_in_section (line) {
452       if (startswith(line, "  - far:")) {
453          parseline(line, "  - far: %" PRIx64, &fault_iova);
454          has_fault_iova = true;
455       }
456 
457       printf("%s", line);
458    }
459 }
460 
461 /*
462  * Decode 'bos' (buffers) section:
463  */
464 
465 static void
decode_bos(void)466 decode_bos(void)
467 {
468    uint32_t size = 0;
469    uint64_t iova = 0;
470 
471    foreach_line_in_section (line) {
472       if (startswith(line, "  - iova:")) {
473          parseline(line, "  - iova: %" PRIx64, &iova);
474          continue;
475       } else if (startswith(line, "    size:")) {
476          parseline(line, "    size: %u", &size);
477 
478          /*
479           * This is a bit convoluted, vs just printing the lines as
480           * they come.  But we want to have both the iova and size
481           * so we can print the end address of the buffer
482           */
483 
484          uint64_t end = iova + size;
485 
486          printf("  - iova: 0x%016" PRIx64 "-0x%016" PRIx64, iova, end);
487 
488          if (has_fault_iova) {
489             if ((iova <= fault_iova) && (fault_iova < end)) {
490                /* Fault address was within what should be a mapped buffer!! */
491                printf("\t==");
492             } else if ((iova <= fault_iova) && (fault_iova < (end + size))) {
493                /* Fault address was near this mapped buffer */
494                printf("\t>=");
495             }
496          }
497          printf("\n");
498          printf("    size: %u (0x%x)\n", size, size);
499          continue;
500       } else if (startswith(line, "    data: !!ascii85 |")) {
501          uint32_t *buf = popline_ascii85(size / 4);
502 
503          if (verbose)
504             dump_hex_ascii(buf, size, 1);
505 
506          add_buffer(iova, size, buf);
507 
508          continue;
509       }
510 
511       printf("%s", line);
512    }
513 }
514 
515 /*
516  * Decode registers section:
517  */
518 
519 void
dump_register(struct regacc * r)520 dump_register(struct regacc *r)
521 {
522    struct rnndecaddrinfo *info = rnn_reginfo(r->rnn, r->regbase);
523    if (info && info->typeinfo) {
524       char *decoded = rnndec_decodeval(r->rnn->vc, info->typeinfo, r->value);
525       printf("%s: %s\n", info->name, decoded);
526    } else if (info) {
527       printf("%s: %08"PRIx64"\n", info->name, r->value);
528    } else {
529       printf("<%04x>: %08"PRIx64"\n", r->regbase, r->value);
530    }
531    rnn_reginfo_free(info);
532 }
533 
534 static void
decode_gmu_registers(void)535 decode_gmu_registers(void)
536 {
537    struct regacc r = regacc(rnn_gmu);
538 
539    foreach_line_in_section (line) {
540       uint32_t offset, value;
541       parseline(line, "  - { offset: %x, value: %x }", &offset, &value);
542 
543       if (regacc_push(&r, offset / 4, value)) {
544          printf("\t%08"PRIx64"\t", r.value);
545          dump_register(&r);
546       }
547    }
548 }
549 
550 static void
decode_registers(void)551 decode_registers(void)
552 {
553    struct regacc r = regacc(NULL);
554 
555    foreach_line_in_section (line) {
556       uint32_t offset, value;
557       parseline(line, "  - { offset: %x, value: %x }", &offset, &value);
558 
559       reg_set(offset / 4, value);
560       if (regacc_push(&r, offset / 4, value)) {
561          printf("\t%08"PRIx64, r.value);
562          dump_register_val(&r, 0);
563       }
564    }
565 }
566 
567 /* similar to registers section, but for banked context regs: */
568 static void
decode_clusters(void)569 decode_clusters(void)
570 {
571    struct regacc r = regacc(NULL);
572 
573    foreach_line_in_section (line) {
574       if (startswith_nowhitespace(line, "- cluster-name:") ||
575           startswith_nowhitespace(line, "- context:") ||
576           startswith_nowhitespace(line, "- pipe:")) {
577          printf("%s", line);
578          continue;
579       }
580 
581       uint32_t offset, value;
582       parseline_nowhitespace(line, "- { offset: %x, value: %x }", &offset, &value);
583 
584       if (regacc_push(&r, offset / 4, value)) {
585          printf("\t%08"PRIx64, r.value);
586          dump_register_val(&r, 0);
587       }
588    }
589 }
590 
591 /*
592  * Decode indexed-registers.. these aren't like normal registers, but a
593  * sort of FIFO where successive reads pop out associated debug state.
594  */
595 
596 static void
dump_cp_sqe_stat(uint32_t * stat)597 dump_cp_sqe_stat(uint32_t *stat)
598 {
599    printf("\t PC: %04x\n", stat[0]);
600    stat++;
601 
602    if (!is_a5xx() && valid_header(stat[0])) {
603       if (pkt_is_type7(stat[0])) {
604          unsigned opc = cp_type7_opcode(stat[0]);
605          const char *name = pktname(opc);
606          if (name)
607             printf("\tPKT: %s\n", name);
608       } else {
609          /* Not sure if this case can happen: */
610       }
611    }
612 
613    for (int i = 0; i < 16; i++) {
614       printf("\t$%02x: %08x\t\t$%02x: %08x\n", i + 1, stat[i], i + 16 + 1,
615              stat[i + 16]);
616    }
617 }
618 
619 static void
dump_control_regs(uint32_t * regs)620 dump_control_regs(uint32_t *regs)
621 {
622    if (!rnn_control)
623       return;
624 
625    struct regacc r = regacc(rnn_control);
626 
627    /* Control regs 0x100-0x17f are a scratch space to be used by the
628     * firmware however it wants, unlike lower regs which involve some
629     * fixed-function units. Therefore only these registers get dumped
630     * directly.
631     */
632    for (uint32_t i = 0; i < 0x80; i++) {
633       if (regacc_push(&r, i + 0x100, regs[i])) {
634          printf("\t%08"PRIx64"\t", r.value);
635          dump_register(&r);
636       }
637    }
638 }
639 
640 static void
dump_cp_ucode_dbg(uint32_t * dbg)641 dump_cp_ucode_dbg(uint32_t *dbg)
642 {
643    /* Notes on the data:
644     * There seems to be a section every 4096 DWORD's. The sections aren't
645     * all the same size, so the rest of the 4096 DWORD's are filled with
646     * mirrors of the actual data.
647     */
648 
649    for (int section = 0; section < 6; section++, dbg += 0x1000) {
650       switch (section) {
651       case 0:
652          /* Contains scattered data from a630_sqe.fw: */
653          printf("\tSQE instruction cache:\n");
654          dump_hex_ascii(dbg, 4 * 0x400, 1);
655          break;
656       case 1:
657          printf("\tUnknown 1:\n");
658          dump_hex_ascii(dbg, 4 * 0x80, 1);
659          break;
660       case 2:
661          printf("\tUnknown 2:\n");
662          dump_hex_ascii(dbg, 4 * 0x200, 1);
663          break;
664       case 3:
665          printf("\tUnknown 3:\n");
666          dump_hex_ascii(dbg, 4 * 0x80, 1);
667          break;
668       case 4:
669          /* Don't bother printing this normally */
670          if (verbose) {
671             printf("\tSQE packet jumptable contents:\n");
672             dump_hex_ascii(dbg, 4 * 0x80, 1);
673          }
674          break;
675       case 5:
676          printf("\tSQE scratch control regs:\n");
677          dump_control_regs(dbg);
678          break;
679       }
680    }
681 }
682 
683 static void
decode_indexed_registers(void)684 decode_indexed_registers(void)
685 {
686    char *name = NULL;
687    uint32_t sizedwords = 0;
688 
689    foreach_line_in_section (line) {
690       if (startswith(line, "  - regs-name:")) {
691          free(name);
692          parseline(line, "  - regs-name: %ms", &name);
693       } else if (startswith(line, "    dwords:")) {
694          parseline(line, "    dwords: %u", &sizedwords);
695       } else if (startswith(line, "    data: !!ascii85 |")) {
696          uint32_t *buf = popline_ascii85(sizedwords);
697 
698          /* some of the sections are pretty large, and are (at least
699           * so far) not useful, so skip them if not in verbose mode:
700           */
701          bool dump = verbose || !strcmp(name, "CP_SQE_STAT") ||
702                      !strcmp(name, "CP_BV_SQE_STAT") ||
703                      !strcmp(name, "CP_DRAW_STATE") ||
704                      !strcmp(name, "CP_ROQ") || 0;
705 
706          if (!strcmp(name, "CP_SQE_STAT") || !strcmp(name, "CP_BV_SQE_STAT"))
707             dump_cp_sqe_stat(buf);
708 
709          if (!strcmp(name, "CP_UCODE_DBG_DATA"))
710             dump_cp_ucode_dbg(buf);
711 
712          if (!strcmp(name, "CP_MEMPOOL"))
713             dump_cp_mem_pool(buf);
714 
715          if (dump)
716             dump_hex_ascii(buf, 4 * sizedwords, 1);
717 
718          free(buf);
719 
720          continue;
721       }
722 
723       printf("%s", line);
724    }
725 }
726 
727 /*
728  * Decode shader-blocks:
729  */
730 
731 static void
decode_shader_blocks(void)732 decode_shader_blocks(void)
733 {
734    char *type = NULL;
735    uint32_t sizedwords = 0;
736 
737    foreach_line_in_section (line) {
738       if (startswith(line, "  - type:")) {
739          free(type);
740          parseline(line, "  - type: %ms", &type);
741       } else if (startswith_nowhitespace(line, "size:")) {
742          parseline_nowhitespace(line, "size: %u", &sizedwords);
743       } else if (startswith_nowhitespace(line, "data: !!ascii85 |")) {
744          uint32_t *buf = popline_ascii85(sizedwords);
745 
746          /* some of the sections are pretty large, and are (at least
747           * so far) not useful, so skip them if not in verbose mode:
748           */
749          bool dump = verbose || !strcmp(type, "A6XX_SP_INST_DATA") ||
750                      !strcmp(type, "A6XX_HLSQ_INST_RAM") ||
751                      !strcmp(type, "A7XX_SP_INST_DATA") ||
752                      !strcmp(type, "A7XX_HLSQ_INST_RAM") || 0;
753 
754          if (!strcmp(type, "A6XX_SP_INST_DATA") ||
755              !strcmp(type, "A6XX_HLSQ_INST_RAM") ||
756              !strcmp(type, "A7XX_SP_INST_DATA") ||
757              !strcmp(type, "A7XX_HLSQ_INST_RAM")) {
758             /* TODO this section actually contains multiple shaders
759              * (or parts of shaders?), so perhaps we should search
760              * for ends of shaders and decode each?
761              */
762             try_disasm_a3xx(buf, sizedwords, 1, stdout, options.info->chip * 100);
763          }
764 
765          if (dump)
766             dump_hex_ascii(buf, 4 * sizedwords, 1);
767 
768          free(buf);
769 
770          continue;
771       }
772 
773       printf("%s", line);
774    }
775 
776    free(type);
777 }
778 
779 /*
780  * Decode debugbus section:
781  */
782 
783 static void
decode_debugbus(void)784 decode_debugbus(void)
785 {
786    char *block = NULL;
787    uint32_t sizedwords = 0;
788 
789    foreach_line_in_section (line) {
790       if (startswith(line, "  - debugbus-block:")) {
791          free(block);
792          parseline(line, "  - debugbus-block: %ms", &block);
793       } else if (startswith(line, "    count:")) {
794          parseline(line, "    count: %u", &sizedwords);
795       } else if (startswith(line, "    data: !!ascii85 |")) {
796          uint32_t *buf = popline_ascii85(sizedwords);
797 
798          /* some of the sections are pretty large, and are (at least
799           * so far) not useful, so skip them if not in verbose mode:
800           */
801          bool dump = verbose || 0;
802 
803          if (dump)
804             dump_hex_ascii(buf, 4 * sizedwords, 1);
805 
806          free(buf);
807 
808          continue;
809       }
810 
811       printf("%s", line);
812    }
813 }
814 
815 /*
816  * Main crashdump decode loop:
817  */
818 
819 static void
decode(void)820 decode(void)
821 {
822    const char *line;
823 
824    while ((line = popline())) {
825       printf("%s", line);
826       if (startswith(line, "revision:")) {
827          unsigned core, major, minor, patchid;
828 
829          parseline(line, "revision: %u (%u.%u.%u.%u)", &options.dev_id.gpu_id,
830                    &core, &major, &minor, &patchid);
831 
832          options.dev_id.chip_id = (core << 24) | (major << 16) | (minor << 8) | patchid;
833          options.info = fd_dev_info_raw(&options.dev_id);
834          if (!options.info) {
835             printf("Unsupported device\n");
836             break;
837          }
838 
839          printf("Got chip_id=0x%"PRIx64"\n", options.dev_id.chip_id);
840 
841          cffdec_init(&options);
842 
843          if (is_a7xx()) {
844             rnn_gmu = rnn_new(!options.color);
845             rnn_load_file(rnn_gmu, "adreno/a6xx_gmu.xml", "A6XX");
846             rnn_control = rnn_new(!options.color);
847             rnn_load_file(rnn_control, "adreno/adreno_control_regs.xml",
848                           "A7XX_CONTROL_REG");
849             rnn_pipe = rnn_new(!options.color);
850             rnn_load_file(rnn_pipe, "adreno/adreno_pipe_regs.xml",
851                           "A7XX_PIPE_REG");
852          } else if (is_a6xx()) {
853             rnn_gmu = rnn_new(!options.color);
854             rnn_load_file(rnn_gmu, "adreno/a6xx_gmu.xml", "A6XX");
855             rnn_control = rnn_new(!options.color);
856             rnn_load_file(rnn_control, "adreno/adreno_control_regs.xml",
857                           "A6XX_CONTROL_REG");
858             rnn_pipe = rnn_new(!options.color);
859             rnn_load_file(rnn_pipe, "adreno/adreno_pipe_regs.xml",
860                           "A6XX_PIPE_REG");
861          } else if (is_a5xx()) {
862             rnn_control = rnn_new(!options.color);
863             rnn_load_file(rnn_control, "adreno/adreno_control_regs.xml",
864                           "A5XX_CONTROL_REG");
865          } else {
866             rnn_control = NULL;
867          }
868       } else if (startswith(line, "fault-info:")) {
869          decode_fault_info();
870       } else if (startswith(line, "bos:")) {
871          decode_bos();
872       } else if (startswith(line, "ringbuffer:")) {
873          decode_ringbuffer();
874       } else if (startswith(line, "gmu-log:")) {
875          decode_gmu_log();
876       } else if (startswith(line, "gmu-hfi:")) {
877          decode_gmu_hfi();
878       } else if (startswith(line, "registers:")) {
879          decode_registers();
880 
881          /* after we've recorded buffer contents, and CP register values,
882           * we can take a stab at decoding the cmdstream:
883           */
884          dump_cmdstream();
885       } else if (startswith(line, "registers-gmu:")) {
886          decode_gmu_registers();
887       } else if (startswith(line, "indexed-registers:")) {
888          decode_indexed_registers();
889       } else if (startswith(line, "shader-blocks:")) {
890          decode_shader_blocks();
891       } else if (startswith(line, "clusters:")) {
892          decode_clusters();
893       } else if (startswith(line, "debugbus:")) {
894          decode_debugbus();
895       }
896    }
897 }
898 
899 /*
900  * Usage and argument parsing:
901  */
902 
903 static void
usage(void)904 usage(void)
905 {
906    /* clang-format off */
907    fprintf(stderr, "Usage:\n\n"
908            "\tcrashdec [-achmsv] [-f FILE]\n\n"
909            "Options:\n"
910            "\t-a, --allregs   - show all registers (including ones not written since\n"
911            "\t                  previous draw) at each draw\n"
912            "\t-c, --color     - use colors\n"
913            "\t-f, --file=FILE - read input from specified file (rather than stdin)\n"
914            "\t-h, --help      - this usage message\n"
915            "\t-m, --markers   - try to decode CP_NOP string markers\n"
916            "\t-s, --summary   - don't show individual register writes, but just show\n"
917            "\t                  register values on draws\n"
918            "\t-v, --verbose   - dump more verbose output, including contents of\n"
919            "\t                  less interesting buffers\n"
920            "\n"
921    );
922    /* clang-format on */
923    exit(2);
924 }
925 
926 /* clang-format off */
927 static const struct option opts[] = {
928       { .name = "allregs", .has_arg = 0, NULL, 'a' },
929       { .name = "color",   .has_arg = 0, NULL, 'c' },
930       { .name = "file",    .has_arg = 1, NULL, 'f' },
931       { .name = "help",    .has_arg = 0, NULL, 'h' },
932       { .name = "markers", .has_arg = 0, NULL, 'm' },
933       { .name = "summary", .has_arg = 0, NULL, 's' },
934       { .name = "verbose", .has_arg = 0, NULL, 'v' },
935       {}
936 };
937 /* clang-format on */
938 
939 static bool interactive;
940 
941 static void
cleanup(void)942 cleanup(void)
943 {
944    fflush(stdout);
945 
946    if (interactive) {
947       pager_close();
948    }
949 }
950 
951 int
main(int argc,char ** argv)952 main(int argc, char **argv)
953 {
954    int c;
955 
956    interactive = isatty(STDOUT_FILENO);
957    options.color = interactive;
958 
959    /* default to read from stdin: */
960    in = stdin;
961 
962    while ((c = getopt_long(argc, argv, "acf:hmsv", opts, NULL)) != -1) {
963       switch (c) {
964       case 'a':
965          options.allregs = true;
966          break;
967       case 'c':
968          options.color = true;
969          break;
970       case 'f':
971          in = fopen(optarg, "r");
972          break;
973       case 'm':
974          options.decode_markers = true;
975          break;
976       case 's':
977          options.summary = true;
978          break;
979       case 'v':
980          verbose = true;
981          break;
982       case 'h':
983       default:
984          usage();
985       }
986    }
987 
988    disasm_a3xx_set_debug(PRINT_RAW);
989 
990    if (interactive) {
991       pager_open();
992    }
993 
994    atexit(cleanup);
995 
996    decode();
997    cleanup();
998 }
999