1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdio.h>
25bf215546Sopenharmony_ci#include <string.h>
26bf215546Sopenharmony_ci#include "util/macros.h"
27bf215546Sopenharmony_ci#include "util/ralloc.h"
28bf215546Sopenharmony_ci#include "broadcom/common/v3d_device_info.h"
29bf215546Sopenharmony_ci#include "broadcom/qpu/qpu_disasm.h"
30bf215546Sopenharmony_ci#include "broadcom/qpu/qpu_instr.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic const struct {
33bf215546Sopenharmony_ci        int ver;
34bf215546Sopenharmony_ci        uint64_t inst;
35bf215546Sopenharmony_ci        const char *expected;
36bf215546Sopenharmony_ci} tests[] = {
37bf215546Sopenharmony_ci        { 33, 0x3d003186bb800000ull, "nop                           ; nop                         ; ldvary" },
38bf215546Sopenharmony_ci        { 33, 0x3c20318105829000ull, "fadd r1, r1, r5               ; nop                         ; thrsw" },
39bf215546Sopenharmony_ci        { 33, 0x3c403186bb81d000ull, "vpmsetup -, r5                ; nop                         ; ldunif" },
40bf215546Sopenharmony_ci        { 33, 0x3f003186bb800000ull, "nop                           ; nop                         ; ldvpm" },
41bf215546Sopenharmony_ci        { 33, 0x3c002380b6edb000ull, "or rf0, r3, r3                ; mov vpm, r3" },
42bf215546Sopenharmony_ci        { 33, 0x57403006bbb80000ull, "nop                           ; fmul r0, rf0, r5            ; ldvpm; ldunif" },
43bf215546Sopenharmony_ci        { 33, 0x9c094adef634b000ull, "ffloor.ifb rf30.l, r3         ; fmul.pushz rf43.l, r5, r1.h" },
44bf215546Sopenharmony_ci        { 33, 0xb0044c56ba326840ull, "flpop rf22, rf33              ; fmul.pushz rf49.l, r4.h, r1.abs" },
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci        /* vfmul input packing */
47bf215546Sopenharmony_ci        { 33, 0x101e8b6e8aad4000ull, "fmax.nornn rf46, r4.l, r2.l   ; vfmul.ifnb rf45, r3, r5" },
48bf215546Sopenharmony_ci        { 33, 0x1857d3c219825000ull, "faddnf.norc r2.l, r5.l, r4    ; vfmul.ifb rf15, r0.ll, r4   ; ldunif" },
49bf215546Sopenharmony_ci        { 33, 0x1c0a0dfde2294000ull, "fcmp.ifna rf61.h, r4.abs, r2.l; vfmul rf55, r2.hh, r1" },
50bf215546Sopenharmony_ci        { 33, 0x2011c89b402cc000ull, "fsub.norz rf27, r4.abs, r1.abs; vfmul.ifa rf34, r3.swp, r1" },
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci        { 33, 0xe01b42ab3bb063c0ull, "vfpack.andnc rf43, rf15.l, r0.h; fmul.ifna rf10.h, r4.l, r5.abs" },
53bf215546Sopenharmony_ci        { 33, 0x600b8b87fb4d1000ull, "fdx.ifnb rf7.h, r1.l          ; fmul.pushn rf46, r3.l, r2.abs" },
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci        /* small immediates */
56bf215546Sopenharmony_ci        { 33, 0x5de24398bbdc6218ull, "vflb.andnn rf24               ; fmul rf14, -8, rf8.h" },
57bf215546Sopenharmony_ci        { 33, 0x25ef83d8b166f00full, "vfmin.pushn rf24, 15.ff, r5   ; smul24.ifnb rf15, r1, r3" },
58bf215546Sopenharmony_ci        { 33, 0xadedcdf70839f990ull, "faddnf.pushc rf55, -16.l, r3.abs; fmul.ifb rf55.l, rf38.l, r1.h" },
59bf215546Sopenharmony_ci        { 33, 0x7dff89fa6a01f020ull, "fsub.nornc rf58.h, 0x3b800000.l, r3.l; fmul.ifnb rf39, r0.h, r0.h" },
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci        /* branch conditions */
62bf215546Sopenharmony_ci        { 33, 0x02000006002034c0ull, "b.anyap  rf19" },
63bf215546Sopenharmony_ci        { 33, 0x02679356b4201000ull, "b.anyap  -1268280496" },
64bf215546Sopenharmony_ci        { 33, 0x02b76a2dd0400000ull, "b.anynaq  zero_addr+0xd0b76a28" },
65bf215546Sopenharmony_ci        { 33, 0x0200000500402000ull, "b.anynaq  lri" },
66bf215546Sopenharmony_ci        { 33, 0x0216fe167301c8c0ull, "bu.anya  zero_addr+0x7316fe10, rf35" },
67bf215546Sopenharmony_ci        { 33, 0x020000050040e000ull, "bu.anynaq  lri, r:unif" },
68bf215546Sopenharmony_ci        { 33, 0x0200000300006000ull, "bu.na0  lri, a:unif" },
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci        /* Special waddr names */
71bf215546Sopenharmony_ci        { 33, 0x3c00318735808000ull, "vfpack tlb, r0, r1            ; nop" },
72bf215546Sopenharmony_ci        { 33, 0xe0571c938e8d5000ull, "fmax.andc recip, r5.h, r2.l   ; fmul.ifb rf50.h, r3.l, r4.abs; ldunif" },
73bf215546Sopenharmony_ci        { 33, 0xc04098d4382c9000ull, "add.pushn rsqrt, r1, r1       ; fmul rf35.h, r3.abs, r1.abs ; ldunif" },
74bf215546Sopenharmony_ci        { 33, 0x481edcd6b3184500ull, "vfmin.norn log, r4.hh, r0     ; fmul.ifnb rf51, rf20.abs, r0.l" },
75bf215546Sopenharmony_ci        { 33, 0x041618d57c453000ull, "shl.andn exp, r3, r2          ; add.ifb rf35, r1, r2" },
76bf215546Sopenharmony_ci        { 33, 0x7048e5da49272800ull, "fsub.ifa rf26, r2.l, rf32     ; fmul.pushc sin, r1.h, r1.abs; ldunif" },
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci        /* v4.1 signals */
79bf215546Sopenharmony_ci        { 41, 0x1f010520cf60a000ull, "fcmp.andz rf32, r2.h, r1.h    ; vfmul rf20, r0.hh, r3       ; ldunifa" },
80bf215546Sopenharmony_ci        { 41, 0x932045e6c16ea000ull, "fcmp rf38, r2.abs, r5         ; fmul rf23.l, r3, r3.abs     ; ldunifarf.rf1" },
81bf215546Sopenharmony_ci        { 41, 0xd72f0434e43ae5c0ull, "fcmp rf52.h, rf23, r5.abs     ; fmul rf16.h, rf23, r1       ; ldunifarf.rf60" },
82bf215546Sopenharmony_ci        { 41, 0xdb3048eb9d533780ull, "fmax rf43.l, r3.h, rf30       ; fmul rf35.h, r4, r2.l       ; ldunifarf.r1" },
83bf215546Sopenharmony_ci        { 41, 0x733620471e6ce700ull, "faddnf rf7.l, rf28.h, r1.l    ; fmul r1, r3.h, r3.abs       ; ldunifarf.rsqrt2" },
84bf215546Sopenharmony_ci        { 41, 0x9c094adef634b000ull, "ffloor.ifb rf30.l, r3         ; fmul.pushz rf43.l, r5, r1.h" },
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        /* v4.1 opcodes */
87bf215546Sopenharmony_ci        { 41, 0x3de020c7bdfd200dull, "ldvpmg_in rf7, r2, r2         ; mov r3, 13" },
88bf215546Sopenharmony_ci        { 41, 0x3de02040f8ff7201ull, "stvpmv 1, rf8                 ; mov r1, 1" },
89bf215546Sopenharmony_ci        { 41, 0xd8000e50bb2d3000ull, "sampid rf16                   ; fmul rf57.h, r3, r1.l" },
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci        /* v4.1 SFU instructions. */
92bf215546Sopenharmony_ci        { 41, 0xe98d60c1ba2aef80ull, "recip rf1, rf62               ; fmul r3.h, r2.l, r1.l       ; ldunifrf.rf53" },
93bf215546Sopenharmony_ci        { 41, 0x7d87c2debc51c000ull, "rsqrt rf30, r4                ; fmul rf11, r4.h, r2.h       ; ldunifrf.rf31" },
94bf215546Sopenharmony_ci        { 41, 0xb182475abc2bb000ull, "rsqrt2 rf26, r3               ; fmul rf29.l, r2.h, r1.abs   ; ldunifrf.rf9" },
95bf215546Sopenharmony_ci        { 41, 0x79880808bc0b6900ull, "sin rf8, rf36                 ; fmul rf32, r2.h, r0.l       ; ldunifrf.rf32" },
96bf215546Sopenharmony_ci        { 41, 0x04092094bc5a28c0ull, "exp.ifb rf20, r2              ; add r2, rf35, r2" },
97bf215546Sopenharmony_ci        { 41, 0xe00648bfbc32a000ull, "log rf63, r2                  ; fmul.andnn rf34.h, r4.l, r1.abs" },
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci        /* v4.2 changes */
100bf215546Sopenharmony_ci        { 42, 0x3c203192bb814000ull, "barrierid syncb               ; nop                         ; thrsw" },
101bf215546Sopenharmony_ci};
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic void
104bf215546Sopenharmony_ciswap_mux(enum v3d_qpu_mux *a, enum v3d_qpu_mux *b)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci        enum v3d_qpu_mux t = *a;
107bf215546Sopenharmony_ci        *a = *b;
108bf215546Sopenharmony_ci        *b = t;
109bf215546Sopenharmony_ci}
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_cistatic void
112bf215546Sopenharmony_ciswap_pack(enum v3d_qpu_input_unpack *a, enum v3d_qpu_input_unpack *b)
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci        enum v3d_qpu_input_unpack t = *a;
115bf215546Sopenharmony_ci        *a = *b;
116bf215546Sopenharmony_ci        *b = t;
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ciint
120bf215546Sopenharmony_cimain(int argc, char **argv)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci        struct v3d_device_info devinfo = { };
123bf215546Sopenharmony_ci        int retval = 0;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci        for (int i = 0; i < ARRAY_SIZE(tests); i++) {
126bf215546Sopenharmony_ci                devinfo.ver = tests[i].ver;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci                printf("Testing v%d.%d 0x%016llx (\"%s\")... ",
129bf215546Sopenharmony_ci                       devinfo.ver / 10, devinfo.ver % 10,
130bf215546Sopenharmony_ci                       (long long)tests[i].inst,
131bf215546Sopenharmony_ci                        tests[i].expected);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci                const char *disasm_output = v3d_qpu_disasm(&devinfo,
134bf215546Sopenharmony_ci                                                           tests[i].inst);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci                printf("%s\n", disasm_output);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci                if (strcmp(disasm_output, tests[i].expected) != 0) {
139bf215546Sopenharmony_ci                        printf("FAIL\n");
140bf215546Sopenharmony_ci                        printf("  Expected: \"%s\"\n", tests[i].expected);
141bf215546Sopenharmony_ci                        printf("  Got:      \"%s\"\n", disasm_output);
142bf215546Sopenharmony_ci                        retval = 1;
143bf215546Sopenharmony_ci                        continue;
144bf215546Sopenharmony_ci                }
145bf215546Sopenharmony_ci                ralloc_free((void *)disasm_output);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci                struct v3d_qpu_instr instr;
148bf215546Sopenharmony_ci                if (!v3d_qpu_instr_unpack(&devinfo, tests[i].inst, &instr)) {
149bf215546Sopenharmony_ci                        printf("FAIL (unpack) %s\n", tests[i].expected);
150bf215546Sopenharmony_ci                        retval = 1;
151bf215546Sopenharmony_ci                        continue;
152bf215546Sopenharmony_ci                }
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci                if (instr.type == V3D_QPU_INSTR_TYPE_ALU) {
155bf215546Sopenharmony_ci                        switch (instr.alu.add.op) {
156bf215546Sopenharmony_ci                        case V3D_QPU_A_FADD:
157bf215546Sopenharmony_ci                        case V3D_QPU_A_FADDNF:
158bf215546Sopenharmony_ci                        case V3D_QPU_A_FMIN:
159bf215546Sopenharmony_ci                        case V3D_QPU_A_FMAX:
160bf215546Sopenharmony_ci                                /* Swap the operands to be sure that we test
161bf215546Sopenharmony_ci                                 * how the QPUs distinguish between these ops.
162bf215546Sopenharmony_ci                                 */
163bf215546Sopenharmony_ci                                swap_mux(&instr.alu.add.a,
164bf215546Sopenharmony_ci                                         &instr.alu.add.b);
165bf215546Sopenharmony_ci                                swap_pack(&instr.alu.add.a_unpack,
166bf215546Sopenharmony_ci                                          &instr.alu.add.b_unpack);
167bf215546Sopenharmony_ci                                break;
168bf215546Sopenharmony_ci                        default:
169bf215546Sopenharmony_ci                                break;
170bf215546Sopenharmony_ci                        }
171bf215546Sopenharmony_ci                }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci                uint64_t repack;
174bf215546Sopenharmony_ci                if (!v3d_qpu_instr_pack(&devinfo, &instr, &repack)) {
175bf215546Sopenharmony_ci                        printf("FAIL (pack) %s\n", tests[i].expected);
176bf215546Sopenharmony_ci                        retval = 1;
177bf215546Sopenharmony_ci                        continue;
178bf215546Sopenharmony_ci                }
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci                if (repack != tests[i].inst) {
181bf215546Sopenharmony_ci                        printf("FAIL (repack) 0x%016llx\n", (long long)repack);
182bf215546Sopenharmony_ci                        printf("  Expected: \"%s\"\n", tests[i].expected);
183bf215546Sopenharmony_ci                        const char *redisasm = v3d_qpu_disasm(&devinfo, repack);
184bf215546Sopenharmony_ci                        printf("  Got:      \"%s\"\n", redisasm);
185bf215546Sopenharmony_ci                        retval = 1;
186bf215546Sopenharmony_ci                        ralloc_free((void *)redisasm);
187bf215546Sopenharmony_ci                }
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci                printf("PASS\n");
190bf215546Sopenharmony_ci        }
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci        return retval;
193bf215546Sopenharmony_ci}
194