Debian Codegraph @

src/panfrost/compiler/bifrost/cmdline.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * Copyright (C) 2019 Ryan Houdek <[email protected]>
4 * Copyright (C) 2014 Rob Clark <[email protected]>
5 * Copyright © 2015 Red Hat
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <getopt.h>
10 #include <string.h>
11 #include "bifrost/disassemble.h"
12 #include "util/macros.h"
13 #include "valhall/disassemble.h"
14 #include "panfrost/lib/pan_props.h"
15
16 #include "util/os_file.h"
17
18 uint64_t gpu_id = 0x72120000;
19 int verbose = 0;
20
21 #define BI_FOURCC(ch0, ch1, ch2, ch3) \
22 ((uint32_t)(ch0) | (uint32_t)(ch1) << 8 | (uint32_t)(ch2) << 16 | \
23 (uint32_t)(ch3) << 24)
24
25 static void
26 disassemble(const char *filename)
27 {
28 size_t filesize = 0;
29 uint32_t *code = (uint32_t *)os_read_file(filename, &filesize);
30 if (!code) {
31 fprintf(stderr, "Couldn't read full file\n");
32 return;
33 }
34
35 void *entrypoint = code;
36
37 if (filesize && code[0] == BI_FOURCC('M', 'B', 'S', '2')) {
38 for (int i = 0; i < filesize / 4; ++i) {
39 if (code[i] != BI_FOURCC('O', 'B', 'J', 'C'))
40 continue;
41
42 unsigned size = code[i + 1];
43 unsigned offset = i + 2;
44
45 entrypoint = code + offset;
46 filesize = size;
47 }
48 }
49
50 if (pan_arch(gpu_id) >= 9)
51 disassemble_valhall(stdout, entrypoint, filesize, verbose);
52 else
53 disassemble_bifrost(stdout, entrypoint, filesize, verbose);
54
55 free(code);
56 }
57
58 int
59 main(int argc, char **argv)
60 {
61 int c;
62
63 if (argc < 2) {
64 printf("Pass a command\n");
65 exit(1);
66 }
67
68 static struct option longopts[] = {
69 {"id", optional_argument, NULL, 'i'},
70 {"gpu", optional_argument, NULL, 'g'},
71 {"verbose", no_argument, &verbose, 'v'},
72 {NULL, 0, NULL, 0},
73 };
74
75 static struct {
76 const char *name;
77 unsigned major, minor;
78 } gpus[] = {
79 {"G71", 6, 0}, {"G72", 6, 2}, {"G51", 7, 0}, {"G76", 7, 1},
80 {"G52", 7, 2}, {"G31", 7, 3}, {"G77", 9, 0}, {"G57", 9, 1},
81 {"G78", 9, 2}, {"G57", 9, 3}, {"G68", 9, 4}, {"G78AE", 9, 5},
82 };
83
84 while ((c = getopt_long(argc, argv, "v:", longopts, NULL)) != -1) {
85
86 switch (c) {
87 case 'i':
88 gpu_id = atoi(optarg);
89
90 if (!gpu_id) {
91 fprintf(stderr, "Expected GPU ID, got %s\n", optarg);
92 return 1;
93 }
94
95 gpu_id <<= 16;
96 break;
97 case 'g':
98 gpu_id = 0;
99
100 /* Compatibility with the Arm compiler */
101 if (strncmp(optarg, "Mali-", 5) == 0)
102 optarg += 5;
103
104 for (unsigned i = 0; i < ARRAY_SIZE(gpus); ++i) {
105 if (strcmp(gpus[i].name, optarg))
106 continue;
107
108 unsigned major = gpus[i].major;
109 unsigned minor = gpus[i].minor;
110
111 gpu_id = (major << 12) | (minor << 8);
112 break;
113 }
114
115 if (!gpu_id) {
116 fprintf(stderr, "Unknown GPU %s\n", optarg);
117 return 1;
118 }
119
120 break;
121 default:
122 break;
123 }
124 }
125
126 disassemble(argv[optind + 1]);
127 return 0;
128 }