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