Debian Codegraph @

src/panfrost/tools/panfrostdump.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 /*
7 * Debug dump analyser for panfrost. 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 #include <endian.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <drm-uapi/panfrost_drm.h>
33
34 #include "decode.h"
35
36 /* Same as panfrost_dump_object_header, but with field
37 * entries in host byte order
38 */
39 struct panfrost_dump_object_header_ho {
40 uint32_t magic;
41 uint32_t type;
42 uint32_t file_size;
43 uint32_t file_offset;
44
45 union {
46 struct pan_reg_hdr_ho {
47 uint64_t jc;
48 uint64_t gpu_id;
49 uint32_t major;
50 uint32_t minor;
51 uint64_t nbos;
52 } reghdr;
53
54 struct pan_bomap_hdr_ho {
55 uint32_t valid;
56 uint64_t iova;
57 uint32_t data[2];
58 } bomap;
59
60 uint32_t sizer[496];
61 };
62 };
63
64 #define MAX_BODUMP_FILENAME 32
65 #define GPU_PAGE_SIZE 4096
66
67 static bool
68 read_header(FILE *fp, struct panfrost_dump_object_header_ho *pdoh)
69 {
70 /* Fields in the coredump file header structures
71 * are found in little-endian order
72 */
73 struct panfrost_dump_object_header doh_le;
74 size_t nr;
75
76 nr = fread(&doh_le, 1, sizeof(struct panfrost_dump_object_header), fp);
77 if (nr < sizeof(struct panfrost_dump_object_header)) {
78 fprintf(stderr, "Wrong header read\n");
79 return false;
80 }
81
82 /* Convert from little-endian to host byte order */
83 pdoh->magic = le32toh(doh_le.magic);
84 if (pdoh->magic != PANFROSTDUMP_MAGIC) {
85 fprintf(stderr, "Wrong header magic\n");
86 return false;
87 }
88
89 pdoh->type = le32toh(doh_le.type);
90 pdoh->file_offset = le32toh(doh_le.file_offset);
91 pdoh->file_size = le32toh(doh_le.file_size);
92
93 switch (pdoh->type) {
94 case PANFROSTDUMP_BUF_REG:
95 pdoh->reghdr.jc = le64toh(doh_le.reghdr.jc);
96 pdoh->reghdr.gpu_id = le64toh(doh_le.reghdr.gpu_id);
97 pdoh->reghdr.major = le32toh(doh_le.reghdr.major);
98 pdoh->reghdr.minor = le32toh(doh_le.reghdr.minor);
99 pdoh->reghdr.nbos = le64toh(doh_le.reghdr.nbos);
100 break;
101 case PANFROSTDUMP_BUF_BO:
102 pdoh->bomap.iova = le64toh(doh_le.bomap.iova);
103 pdoh->bomap.valid = le32toh(doh_le.bomap.valid);
104 pdoh->bomap.data[0] = le32toh(doh_le.bomap.data[0]);
105 pdoh->bomap.data[1] = le32toh(doh_le.bomap.data[1]);
106 }
107
108 return true;
109 }
110
111 static bool
112 read_register(uint32_t *ro, uint32_t *rv, FILE *fp)
113 {
114 /* Register pair we read form memory is
115 * laid out in little-endian order
116 */
117 struct panfrost_dump_registers reg_le;
118 size_t nr;
119
120 nr = fread(&reg_le, 1, sizeof(reg_le), fp);
121 if (nr < sizeof(reg_le)) {
122 fprintf(stderr, "Wrong register read\n");
123 return false;
124 }
125
126 *ro = le32toh(reg_le.reg);
127 *rv = le32toh(reg_le.value);
128
129 return true;
130 }
131
132 static bool
133 read_page_addr(uint64_t *phys_page, FILE *fp)
134 {
135 uint64_t phys_addr_le;
136 size_t nr;
137
138 nr = fread(&phys_addr_le, 1, sizeof(uint64_t), fp);
139 if (nr < sizeof(uint64_t)) {
140 fprintf(stderr, "Wrong page address read\n");
141
142 /* Skip over to the next address */
143 if (fseek(fp, sizeof(uint64_t) - nr, SEEK_CUR)) {
144 perror("fseek error");
145 return false;
146 }
147
148 return false;
149 }
150
151 *phys_page = le64toh(phys_addr_le);
152
153 return true;
154 }
155
156 /* Keeping these definitions as global/static shouldn't be
157 * an issue because this tool will always be single-threaded
158 */
159 static FILE *hdr_fp;
160 static FILE *data_fp;
161 static char **bos;
162 static uint32_t bo_num;
163
164 static void
165 cleanup(void)
166 {
167 if (hdr_fp != NULL)
168 fclose(hdr_fp);
169 if (data_fp != NULL)
170 fclose(data_fp);
171 if (bos != NULL) {
172 for (int k = 0; k < bo_num; k++)
173 free(bos[k]);
174 free(bos);
175 }
176 }
177
178 static void
179 print_help(const char *progname, FILE *file)
180 {
181 fprintf(file,
182 "Usage: %s [OPTION] inputfile\n"
183 "Decode Panfrost coredump file.\n\n"
184 " -h, --help display this help and exit\n"
185 " -a, --addr print BO physical addresses\n"
186 " -r, --regs print Panfrost HW registers\n"
187 "Example:\n"
188 " panfrostdump -a -r coredump.bin\n",
189 progname);
190 }
191
192 int
193 main(int argc, char *argv[])
194 {
195 struct panfrost_dump_object_header_ho doh;
196 bool print_addr = false;
197 bool print_reg = false;
198 uint64_t gpu_id = 0;
199 uint64_t jc = 0;
200 size_t nbytes;
201 int i, j, k, c;
202
203 if (argc < 2) {
204 printf("Pass a coredump file\n");
205 return EXIT_FAILURE;
206 }
207
208 /* clang-format off */
209 const struct option longopts[] = {
210 { "addr", no_argument, (int *) &print_addr, true },
211 { "regs", no_argument, (int *) &print_reg, true },
212 { "help", no_argument, NULL, 'h' },
213 { NULL, 0, NULL, 0 }
214 };
215 /* clang-format on */
216
217 while ((c = getopt_long(argc, argv, "arh", longopts, NULL)) != -1) {
218 switch (c) {
219 case 'h':
220 print_help(argv[0], stderr);
221 return EXIT_SUCCESS;
222 case 'a':
223 print_addr = true;
224 break;
225 case 'r':
226 print_reg = true;
227 break;
228 default:
229 fprintf(stderr, "Unknown option\n");
230 print_help(argv[0], stderr);
231 return EXIT_FAILURE;
232 }
233 }
234
235 i = j = k = 0;
236
237 atexit(cleanup);
238 struct pandecode_context *ctx = pandecode_create_context(false);
239
240 hdr_fp = fopen(argv[optind], "r");
241 if (!hdr_fp) {
242 perror("failed to open file");
243 return EXIT_FAILURE;
244 }
245
246 data_fp = fopen(argv[optind], "r");
247 if (!data_fp) {
248 perror("failed to open file");
249 return EXIT_FAILURE;
250 }
251
252 /* Read register header */
253 if (!read_header(hdr_fp, &doh))
254 return EXIT_FAILURE;
255
256 if (fseek(data_fp, doh.file_offset, SEEK_SET)) {
257 perror("fseek error");
258 return EXIT_FAILURE;
259 }
260
261 if (doh.type == PANFROSTDUMP_BUF_REG) {
262 jc = doh.reghdr.jc;
263 gpu_id = doh.reghdr.gpu_id;
264 bo_num = doh.reghdr.nbos;
265
266 bos = calloc(bo_num, sizeof(char *));
267 if (!bos) {
268 fprintf(stderr, "Failed to allocate memory for BO pointer array\n");
269 return EXIT_FAILURE;
270 }
271
272 printf("JC: %" PRIX64 ", GPU_ID: %" PRIX64 "\n", jc, gpu_id);
273
274 if (print_reg) {
275 puts("GPU registers:");
276 for (i = 0;
277 i < (doh.file_size / sizeof(struct panfrost_dump_registers));
278 i++) {
279 uint32_t reg_offset;
280 uint32_t reg_val;
281
282 if (read_register(&reg_offset, &reg_val, data_fp))
283 printf("0x%04X : 0x%08X\n", reg_offset, reg_val);
284 }
285 }
286 }
287
288 if (!read_header(hdr_fp, &doh))
289 return EXIT_FAILURE;
290
291 if (doh.type == PANFROSTDUMP_BUF_BOMAP) {
292 uint32_t bomap_offset = doh.file_offset;
293
294 if (!jc || !gpu_id) {
295 fprintf(stderr, "Missing initial dump header\n");
296 return EXIT_FAILURE;
297 }
298
299 if (!read_header(hdr_fp, &doh))
300 return EXIT_FAILURE;
301
302 while (doh.type != PANFROSTDUMP_BUF_TRAILER) {
303 if (doh.bomap.valid) {
304 if (fseek(data_fp, bomap_offset + doh.bomap.data[0], SEEK_SET)) {
305 perror("fseek error");
306 return EXIT_FAILURE;
307 }
308
309 if (print_addr) {
310 printf("BO(%u) VA(%" PRIX64 ") SZ(%" PRIX32
311 ") page addresses:\n",
312 j, doh.bomap.iova, doh.file_size);
313
314 for (k = 0; k < (doh.file_size / GPU_PAGE_SIZE); k++) {
315 uint64_t phys_addr;
316
317 if (!read_page_addr(&phys_addr, data_fp))
318 continue;
319
320 printf("%u: %" PRIX64 "\n", k, phys_addr);
321 }
322 }
323
324 /* Copy the BO into external file */
325 char bodump_filename[MAX_BODUMP_FILENAME];
326 FILE *bodump;
327
328 snprintf(bodump_filename, MAX_BODUMP_FILENAME, "bodump-%u.dump", j);
329
330 if ((bodump = fopen(bodump_filename, "wb"))) {
331 if (fseek(data_fp, doh.file_offset, SEEK_SET)) {
332 perror("fseek error");
333 return EXIT_FAILURE;
334 }
335
336 bos[j] = malloc(doh.file_size);
337 if (!bos[j]) {
338 fprintf(stderr, "Failed to allocate memory for BO\n");
339 return EXIT_FAILURE;
340 }
341
342 if (fseek(data_fp, doh.file_offset, SEEK_SET)) {
343 perror("fseek error");
344 return EXIT_FAILURE;
345 }
346
347 nbytes = fread(bos[j], 1, doh.file_size, data_fp);
348 if (nbytes < doh.file_size) {
349 fprintf(stderr, "Read less than BO size: %u\n", errno);
350 return EXIT_FAILURE;
351 }
352 nbytes = fwrite(bos[j], 1, doh.file_size, bodump);
353 if (nbytes < doh.file_size) {
354 fprintf(stderr, "Failed to write BO contents into file: %u\n",
355 errno);
356 return EXIT_FAILURE;
357 }
358
359 fclose(bodump);
360
361 pandecode_inject_mmap(ctx, doh.bomap.iova, bos[j], doh.file_size,
362 NULL);
363
364 } else {
365 perror("failed to open BO dump file");
366 }
367 } else {
368 fprintf(stderr, "BO(%u) isn't valid\n", j);
369 }
370
371 if (!read_header(hdr_fp, &doh))
372 return EXIT_FAILURE;
373
374 j++;
375 }
376 } else {
377 if (!read_header(hdr_fp, &doh))
378 return EXIT_FAILURE;
379 }
380
381 if (doh.type != PANFROSTDUMP_BUF_TRAILER)
382 fprintf(stderr, "Trailing header isn't right\n");
383
384 pandecode_jc(ctx, jc, gpu_id);
385 pandecode_destroy_context(ctx);
386
387 fclose(data_fp);
388 fclose(hdr_fp);
389 data_fp = hdr_fp = NULL;
390
391 return EXIT_SUCCESS;
392 }