omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / tests / dispatch_ref.c
| 1 | /* tests/dispatch_ref.c -- TEST-ONLY C referee for tests/dispatch_diff.sh. |
| 2 | * Never linked into the product. |
| 3 | * Runs reference prevalidation and gsp_seq_run against gsp_sim for every case in tests/dispatch_cases.txt. |
| 4 | * Opcodes and payload sizes from rmgspseq.h and kernel_gsp.c:5297-5432. |
| 5 | * Input format (decimal): |
| 6 | * name busy budget core corefail perms gen_off tok_off bstate pkt_trunc mode nwords end m w0 .. w(m-1) |
| 7 | */ |
| 8 | #include <stdint.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include "gsp_seq.h" |
| 13 | #include "gsp_sim.h" |
| 14 | |
| 15 | struct ctx { |
| 16 | struct ck_gpu_regs r; |
| 17 | uint32_t fail, ncalls, ops[16], ndelay, us; |
| 18 | }; |
| 19 | |
| 20 | static uint32_t rd(void *c, uint32_t a) { struct ctx *x = c; return x->r.rd(x->r.ctx, a); } |
| 21 | static void wr(void *c, uint32_t a, uint32_t v) { struct ctx *x = c; x->r.wr(x->r.ctx, a, v); } |
| 22 | |
| 23 | static int c_core(void *c, uint32_t op) |
| 24 | { |
| 25 | struct ctx *x = c; |
| 26 | if (x->ncalls < 16) x->ops[x->ncalls] = op; |
| 27 | x->ncalls++; |
| 28 | return op == x->fail ? -1 : 0; |
| 29 | } |
| 30 | |
| 31 | static void delay_us(void *c, uint32_t us) |
| 32 | { |
| 33 | struct ctx *x = c; |
| 34 | x->ndelay++; |
| 35 | x->us += us; |
| 36 | } |
| 37 | |
| 38 | static uint32_t payload_dwords(uint32_t op) |
| 39 | { |
| 40 | switch (op) { |
| 41 | case GSP_SEQ_REG_WRITE: return 2; |
| 42 | case GSP_SEQ_REG_MODIFY: return 3; |
| 43 | case GSP_SEQ_REG_POLL: return 5; |
| 44 | case GSP_SEQ_DELAY_US: return 1; |
| 45 | case GSP_SEQ_REG_STORE: return 2; |
| 46 | case GSP_SEQ_CORE_RESET: |
| 47 | case GSP_SEQ_CORE_START: |
| 48 | case GSP_SEQ_CORE_WAIT: |
| 49 | case GSP_SEQ_CORE_RESUME: return 0; |
| 50 | default: return 0xffffffffu; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static const char *why_text(int rc) |
| 55 | { |
| 56 | switch (rc) { |
| 57 | case -1: return "empty buffer"; |
| 58 | case -2: return "cmdIndex not inside the buffer"; |
| 59 | case -3: return "unknown opcode"; |
| 60 | case -4: return "opcode overruns cmdIndex"; |
| 61 | case -5: return "poll timed out"; |
| 62 | case -6: return "save index out of range"; |
| 63 | case -7: return "core opcode with no core callback"; |
| 64 | case -8: return "core opcode failed"; |
| 65 | case -9: return "nwords over 16384"; |
| 66 | case -10: return "stale token or invalid state"; |
| 67 | case -11: return "poll budget over 999998"; |
| 68 | case -12: return "remote error quarantined"; |
| 69 | case -13: return "packet shape truncated"; |
| 70 | default: return "-"; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static int map_why_to_code(const char *why) |
| 75 | { |
| 76 | if (!why) return 0; |
| 77 | if (!strcmp(why, "empty buffer")) return -1; |
| 78 | if (!strcmp(why, "cmdIndex not inside the buffer")) return -2; |
| 79 | if (!strcmp(why, "unknown opcode")) return -3; |
| 80 | if (!strcmp(why, "opcode overruns cmdIndex")) return -4; |
| 81 | if (!strcmp(why, "poll timed out")) return -5; |
| 82 | if (!strcmp(why, "save index out of range")) return -6; |
| 83 | if (!strcmp(why, "core opcode with no core callback")) return -7; |
| 84 | if (!strcmp(why, "core opcode failed")) return -8; |
| 85 | if (!strcmp(why, "nwords over 16384")) return -9; |
| 86 | if (!strcmp(why, "delay sleep failed")) return -10; |
| 87 | if (!strcmp(why, "poll budget over 999998")) return -11; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | static int prevalidate(const uint32_t *buf, uint32_t nw, uint32_t end, uint32_t perms, int has_core, uint32_t bud, uint32_t mode) |
| 92 | { |
| 93 | if (nw == 0) return -1; |
| 94 | if (end >= nw) return -2; |
| 95 | if (nw > 16384) return -9; |
| 96 | if (bud > 999998) return -11; |
| 97 | uint32_t i = 0; |
| 98 | while (i < end) { |
| 99 | uint32_t op = buf[i++]; |
| 100 | uint32_t n = payload_dwords(op); |
| 101 | if (n == 0xffffffffu) return -3; |
| 102 | if (i + n > end) return -4; |
| 103 | if (op == GSP_SEQ_REG_STORE) { |
| 104 | if (buf[i + 1] >= 8) return -6; |
| 105 | } |
| 106 | if (op >= GSP_SEQ_CORE_RESET && op <= GSP_SEQ_CORE_RESUME) { |
| 107 | uint32_t bit = 1u << (op - GSP_SEQ_CORE_RESET); |
| 108 | /* Host model flags (perms/has_core) authorize host simulation only (mode == 0). |
| 109 | * Hardware core operations require typed engine capabilities; non-sim mode (mode != 0) |
| 110 | * unconditionally refuses core operations. */ |
| 111 | if (mode != 0 || !has_core || !(perms & bit)) return -7; |
| 112 | } |
| 113 | i += n; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int main(void) |
| 119 | { |
| 120 | char line[65536]; |
| 121 | while (fgets(line, sizeof line, stdin)) { |
| 122 | char *p = line; |
| 123 | while (*p == ' ' || *p == '\t') p++; |
| 124 | if (*p == '#' || *p == '\n' || *p == '\0') continue; |
| 125 | char name[64]; |
| 126 | long long busy, bud, core, fail, perms, gen_off, tok_off, bstate, pkt_trunc, mode, nw, end, m; |
| 127 | int off = 0; |
| 128 | if (sscanf(p, "%63s %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld%n", |
| 129 | name, &busy, &bud, &core, &fail, &perms, &gen_off, &tok_off, &bstate, &pkt_trunc, &mode, &nw, &end, &m, &off) != 14) { |
| 130 | fprintf(stderr, "dispatch_ref: malformed line: %s\n", line); |
| 131 | return 2; |
| 132 | } |
| 133 | p += off; |
| 134 | uint32_t alloc_nw = (nw > 0) ? (uint32_t)nw : 1; |
| 135 | uint32_t *buf = calloc(alloc_nw, sizeof(uint32_t)); |
| 136 | for (long long i = 0; i < m && i < nw; i++) { |
| 137 | long long w = 0; |
| 138 | if (sscanf(p, "%lld%n", &w, &off) != 1) { |
| 139 | fprintf(stderr, "dispatch_ref: short words in: %s\n", line); |
| 140 | free(buf); |
| 141 | return 2; |
| 142 | } |
| 143 | buf[i] = (uint32_t)w; |
| 144 | p += off; |
| 145 | } |
| 146 | while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++; |
| 147 | if (*p != '\0') { |
| 148 | fprintf(stderr, "dispatch_ref: trailing words (m does not cover the line): %s\n", line); |
| 149 | free(buf); |
| 150 | return 2; |
| 151 | } |
| 152 | |
| 153 | struct ck_gspsim_cfg cfg; |
| 154 | memset(&cfg, 0, sizeof cfg); |
| 155 | cfg.busy_reads = (uint32_t)busy; |
| 156 | struct ck_gspsim sim; |
| 157 | ck_gspsim_init(&sim, &cfg); |
| 158 | |
| 159 | struct ctx x; |
| 160 | memset(&x, 0, sizeof x); |
| 161 | x.r = ck_gspsim_regs(&sim); |
| 162 | x.fail = (uint32_t)fail; |
| 163 | |
| 164 | uint32_t state = (uint32_t)bstate; |
| 165 | uint32_t bprev = 9; |
| 166 | uint32_t dead = 0; |
| 167 | uint32_t bselen = pkt_trunc ? 100 : (120 + (uint32_t)nw * 4); |
| 168 | uint32_t save[8] = {0}; |
| 169 | int rc = 0; |
| 170 | |
| 171 | if (state != 11 || gen_off != 0 || tok_off != 0) { |
| 172 | rc = -10; |
| 173 | } else if (mode != 0) { |
| 174 | /* No typed MMIO lease/span: refuse the WHOLE dispatch before unpack/effects. |
| 175 | * This is the explicit unavailable-effect gate, not vendor register validation. */ |
| 176 | dead = 1; state = 12; rc = -7; |
| 177 | } else if (bselen < 120 + (uint32_t)nw * 4) { |
| 178 | dead = 1; state = 12; rc = -13; |
| 179 | } else if (nw == 0) { |
| 180 | dead = 1; state = 12; rc = -1; |
| 181 | } else if (nw > 16384) { |
| 182 | dead = 1; state = 12; rc = -9; |
| 183 | } else if ((uint32_t)end >= (uint32_t)nw) { |
| 184 | dead = 1; state = 12; rc = -2; |
| 185 | } else { |
| 186 | int pr = prevalidate(buf, (uint32_t)nw, (uint32_t)end, (uint32_t)perms, (int)core, (uint32_t)bud, (uint32_t)mode); |
| 187 | if (pr != 0) { |
| 188 | dead = 1; state = 12; rc = pr; |
| 189 | } else { |
| 190 | struct gsp_seq_io io; |
| 191 | memset(&io, 0, sizeof io); |
| 192 | io.rd = rd; |
| 193 | io.wr = wr; |
| 194 | io.core = core ? c_core : NULL; |
| 195 | io.delay_us = delay_us; |
| 196 | io.ctx = &x; |
| 197 | io.poll_budget = (uint32_t)bud; |
| 198 | const char *why = NULL; |
| 199 | int s_rc = gsp_seq_run(buf, (uint32_t)nw, (uint32_t)end, &io, &why); |
| 200 | if (s_rc != 0) { |
| 201 | dead = 1; state = 12; |
| 202 | rc = map_why_to_code(why); |
| 203 | } else { |
| 204 | state = bprev; |
| 205 | dead = 0; |
| 206 | rc = 0; |
| 207 | for (int k = 0; k < 8; k++) save[k] = io.save[k]; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | printf("== %s rc=%d state=%u dead=%u why=%s delays=%u/%u core=%u ops=", |
| 213 | name, rc, state, dead, why_text(rc), x.ndelay, x.us, x.ncalls); |
| 214 | for (uint32_t i = 0; i < x.ncalls && i < 16; i++) printf("%u,", x.ops[i]); |
| 215 | printf(" save="); |
| 216 | for (int i = 0; i < 8; i++) printf("%#x,", save[i]); |
| 217 | printf(" nlog=%u violations=%u\n", sim.nlog, sim.violations); |
| 218 | for (uint32_t i = 0; i < sim.nlog && i < CK_GSPSIM_LOG_MAX; i++) |
| 219 | printf("%c %06x %08x\n", sim.log[i].write ? 'W' : 'R', sim.log[i].off, sim.log[i].val); |
| 220 | |
| 221 | free(buf); |
| 222 | } |
| 223 | return 0; |
| 224 | } |