2 * Copyright (c) 2017 Rob Clark <robdclark@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 static struct rnndeccontext
*ctx
;
45 static struct rnndb
*db
;
46 static struct rnndomain
*control_regs
;
47 struct rnndomain
*dom
[2];
50 /* bit lame to hard-code max but fw sizes are small */
51 static struct asm_instruction instructions
[0x2000];
52 static unsigned num_instructions
;
54 static struct asm_label labels
[0x512];
55 static unsigned num_labels
;
57 struct asm_instruction
*next_instr(int tok
)
59 struct asm_instruction
*ai
= &instructions
[num_instructions
++];
60 assert(num_instructions
< ARRAY_SIZE(instructions
));
65 void decl_label(const char *str
)
67 struct asm_label
*label
= &labels
[num_labels
++];
69 assert(num_labels
< ARRAY_SIZE(labels
));
71 label
->offset
= num_instructions
;
75 static int resolve_label(const char *str
)
79 for (i
= 0; i
< num_labels
; i
++) {
80 struct asm_label
*label
= &labels
[i
];
82 if (!strcmp(str
, label
->label
)) {
87 fprintf(stderr
, "Undeclared label: %s\n", str
);
91 static afuc_opc
tok2alu(int tok
)
94 case T_OP_ADD
: return OPC_ADD
;
95 case T_OP_ADDHI
: return OPC_ADDHI
;
96 case T_OP_SUB
: return OPC_SUB
;
97 case T_OP_SUBHI
: return OPC_SUBHI
;
98 case T_OP_AND
: return OPC_AND
;
99 case T_OP_OR
: return OPC_OR
;
100 case T_OP_XOR
: return OPC_XOR
;
101 case T_OP_NOT
: return OPC_NOT
;
102 case T_OP_SHL
: return OPC_SHL
;
103 case T_OP_USHR
: return OPC_USHR
;
104 case T_OP_ISHR
: return OPC_ISHR
;
105 case T_OP_ROT
: return OPC_ROT
;
106 case T_OP_MUL8
: return OPC_MUL8
;
107 case T_OP_MIN
: return OPC_MIN
;
108 case T_OP_MAX
: return OPC_MAX
;
109 case T_OP_CMP
: return OPC_CMP
;
110 case T_OP_MSB
: return OPC_MSB
;
117 static void emit_instructions(int outfd
)
121 /* there is an extra 0x00000000 which kernel strips off.. we could
122 * perhaps use it for versioning.
127 for (i
= 0; i
< num_instructions
; i
++) {
128 struct asm_instruction
*ai
= &instructions
[i
];
129 afuc_instr instr
= {0};
132 /* special case, 2nd dword is patched up w/ # of instructions
133 * (ie. offset of jmptbl)
136 assert(ai
->is_literal
);
137 ai
->literal
&= ~0xffff;
138 ai
->literal
|= num_instructions
;
141 if (ai
->is_literal
) {
142 write(outfd
, &ai
->literal
, 4);
150 instr
.pad
= 0x1000000;
170 /* MSB overlaps with STORE */
171 assert(ai
->tok
!= T_OP_MSB
);
172 opc
= tok2alu(ai
->tok
);
173 instr
.alui
.dst
= ai
->dst
;
174 instr
.alui
.src
= ai
->src1
;
175 instr
.alui
.uimm
= ai
->immed
;
178 instr
.alu
.dst
= ai
->dst
;
179 instr
.alu
.src1
= ai
->src1
;
180 instr
.alu
.src2
= ai
->src2
;
181 instr
.alu
.alu
= tok2alu(ai
->tok
);
185 /* move can either be encoded as movi (ie. move w/ immed) or
190 instr
.movi
.dst
= ai
->dst
;
191 instr
.movi
.uimm
= ai
->immed
;
192 instr
.movi
.shift
= ai
->shift
;
193 } else if (ai
->label
) {
194 /* mov w/ a label is just an alias for an immediate, this
195 * is useful to load the address of a constant table into
199 instr
.movi
.dst
= ai
->dst
;
200 instr
.movi
.uimm
= resolve_label(ai
->label
);
201 instr
.movi
.shift
= ai
->shift
;
203 /* encode as: or $dst, $00, $src */
205 instr
.alu
.dst
= ai
->dst
;
206 instr
.alu
.src1
= 0x00; /* $00 reads-back 0 */
207 instr
.alu
.src2
= ai
->src1
;
208 instr
.alu
.alu
= OPC_OR
;
216 if (ai
->tok
== T_OP_CWRITE
) {
218 } else if (ai
->tok
== T_OP_CREAD
) {
220 } else if (ai
->tok
== T_OP_STORE
) {
222 } else if (ai
->tok
== T_OP_LOAD
) {
226 if (ai
->tok
== T_OP_CWRITE
) {
228 } else if (ai
->tok
== T_OP_CREAD
) {
230 } else if (ai
->tok
== T_OP_STORE
||
231 ai
->tok
== T_OP_LOAD
) {
232 fprintf(stderr
, "load and store do not exist on a5xx\n");
236 instr
.control
.src1
= ai
->src1
;
237 instr
.control
.src2
= ai
->src2
;
238 instr
.control
.flags
= ai
->bit
;
239 instr
.control
.uimm
= ai
->immed
;
244 opc
= (ai
->tok
== T_OP_BRNE
) ? OPC_BRNEI
: OPC_BREQI
;
245 instr
.br
.bit_or_imm
= ai
->immed
;
247 opc
= (ai
->tok
== T_OP_BRNE
) ? OPC_BRNEB
: OPC_BREQB
;
248 instr
.br
.bit_or_imm
= ai
->bit
;
250 instr
.br
.src
= ai
->src1
;
251 instr
.br
.ioff
= resolve_label(ai
->label
) - i
;
258 instr
.call
.uoff
= resolve_label(ai
->label
);
260 case T_OP_PREEMPTLEAVE
:
261 opc
= OPC_PREEMPTLEAVE6
;
262 instr
.call
.uoff
= resolve_label(ai
->label
);
265 /* encode jump as: brne $00, b0, #label */
267 instr
.br
.bit_or_imm
= 0;
268 instr
.br
.src
= 0x00; /* $00 reads-back 0.. compare to 0 */
269 instr
.br
.ioff
= resolve_label(ai
->label
) - i
;
278 afuc_set_opc(&instr
, opc
, ai
->rep
);
280 write(outfd
, &instr
, 4);
285 static int find_enum_val(struct rnnenum
*en
, const char *name
)
289 for (i
= 0; i
< en
->valsnum
; i
++)
290 if (en
->vals
[i
]->valvalid
&& !strcmp(name
, en
->vals
[i
]->name
))
291 return en
->vals
[i
]->value
;
296 static int find_reg(struct rnndomain
*dom
, const char *name
)
300 for (i
= 0; i
< dom
->subelemsnum
; i
++)
301 if (!strcmp(name
, dom
->subelems
[i
]->name
))
302 return dom
->subelems
[i
]->offset
;
307 unsigned parse_control_reg(const char *name
)
309 /* skip leading "@" */
310 int val
= find_reg(control_regs
, name
+ 1);
312 printf("invalid control reg: %s\n", name
);
315 return (unsigned)val
;
318 static void emit_jumptable(int outfd
)
320 struct rnnenum
*en
= rnn_findenum(ctx
->db
, "adreno_pm4_type3_packets");
321 uint32_t jmptable
[0x80] = {0};
324 for (i
= 0; i
< num_labels
; i
++) {
325 struct asm_label
*label
= &labels
[i
];
326 int id
= find_enum_val(en
, label
->label
);
328 /* if it doesn't match a known PM4 packet-id, try to match UNKN%d: */
330 if (sscanf(label
->label
, "UNKN%d", &id
) != 1) {
331 /* if still not found, must not belong in jump-table: */
336 jmptable
[id
] = label
->offset
;
339 write(outfd
, jmptable
, sizeof(jmptable
));
342 static void usage(void)
344 fprintf(stderr
, "Usage:\n"
345 "\tasm [-g GPUVER] filename.asm filename.fw\n"
346 "\t\t-g - specify GPU version (5, etc)\n"
351 int main(int argc
, char **argv
)
354 char *file
, *outfile
, *name
, *control_reg_name
;
357 /* Argument parsing: */
358 while ((c
= getopt (argc
, argv
, "g:")) != -1) {
361 gpuver
= atoi(optarg
);
368 if (optind
>= (argc
+ 1)) {
369 fprintf(stderr
, "no file specified!\n");
374 outfile
= argv
[optind
+ 1];
376 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
378 fprintf(stderr
, "could not open \"%s\"\n", outfile
);
382 in
= fopen(file
, "r");
384 fprintf(stderr
, "could not open \"%s\"\n", file
);
390 /* if gpu version not specified, infer from filename: */
392 if (strstr(file
, "a5")) {
394 } else if (strstr(file
, "a6")) {
402 control_reg_name
= "A6XX_CONTROL_REG";
406 control_reg_name
= "A5XX_CONTROL_REG";
409 fprintf(stderr
, "unknown GPU version!\n");
416 ctx
= rnndec_newcontext(db
);
418 rnn_parsefile(db
, "adreno.xml");
419 dom
[0] = rnn_finddomain(db
, name
);
420 dom
[1] = rnn_finddomain(db
, "AXXX");
421 control_regs
= rnn_finddomain(db
, control_reg_name
);
425 fprintf(stderr
, "parse failed: %d\n", ret
);
429 emit_instructions(outfd
);
430 emit_jumptable(outfd
);