aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / gpu / gsp_seq.c
| 1 | /* gsp_seq.c -- see gsp_seq.h. Opcode sizes from rmgspseq.h GSP_SEQUENCER_PAYLOAD_SIZE_DWORDS. |
| 2 | * REG_MODIFY is (old & ~mask) | val, as kernel_gsp.c writes it. REG_POLL waits until |
| 3 | * (reg & mask) == val. REG_STORE refuses an index of 8 or more (GSP_SEQ_BUF_REG_SAVE_SIZE is 8). */ |
| 4 | #include "gsp_seq.h" |
| 5 | |
| 6 | static uint32_t payload_dwords(uint32_t op) |
| 7 | { |
| 8 | switch (op) { |
| 9 | case GSP_SEQ_REG_WRITE: return 2; |
| 10 | case GSP_SEQ_REG_MODIFY: return 3; |
| 11 | case GSP_SEQ_REG_POLL: return 5; |
| 12 | case GSP_SEQ_DELAY_US: return 1; |
| 13 | case GSP_SEQ_REG_STORE: return 2; |
| 14 | case GSP_SEQ_CORE_RESET: |
| 15 | case GSP_SEQ_CORE_START: |
| 16 | case GSP_SEQ_CORE_WAIT: |
| 17 | case GSP_SEQ_CORE_RESUME: return 0; |
| 18 | default: return 0xffffffffu; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | int gsp_seq_run(const uint32_t *buf, uint32_t nwords, uint32_t end, gsp_seq_io *io, const char **why) |
| 23 | { |
| 24 | if (why) *why = 0; |
| 25 | if (!buf || !io || !io->rd || !io->wr) { if (why) *why = "null argument"; return -1; } |
| 26 | if (nwords == 0) { if (why) *why = "empty buffer"; return -1; } |
| 27 | if (end >= nwords) { if (why) *why = "cmdIndex not inside the buffer"; return -1; } |
| 28 | uint32_t i = 0; |
| 29 | while (i < end) { |
| 30 | uint32_t op = buf[i++]; |
| 31 | uint32_t n = payload_dwords(op); |
| 32 | if (n == 0xffffffffu) { if (why) *why = "unknown opcode"; return -1; } |
| 33 | if (i + n > end) { if (why) *why = "opcode overruns cmdIndex"; return -1; } |
| 34 | const uint32_t *a = buf + i; |
| 35 | if (op == GSP_SEQ_REG_WRITE) { |
| 36 | io->wr(io->ctx, a[0], a[1]); |
| 37 | } else if (op == GSP_SEQ_REG_MODIFY) { |
| 38 | uint32_t v = io->rd(io->ctx, a[0]); |
| 39 | io->wr(io->ctx, a[0], (v & ~a[1]) | a[2]); |
| 40 | } else if (op == GSP_SEQ_REG_POLL) { |
| 41 | uint32_t left = io->poll_budget ? io->poll_budget : 1; |
| 42 | uint32_t v = io->rd(io->ctx, a[0]); |
| 43 | while ((v & a[1]) != a[2]) { |
| 44 | if (left == 0) { if (why) *why = "poll timed out"; return -1; } |
| 45 | left--; |
| 46 | v = io->rd(io->ctx, a[0]); |
| 47 | } |
| 48 | } else if (op == GSP_SEQ_DELAY_US) { |
| 49 | if (io->delay_us) io->delay_us(io->ctx, a[0]); |
| 50 | } else if (op == GSP_SEQ_REG_STORE) { |
| 51 | #ifdef GSP_SEQ_MUTANT_INDEX |
| 52 | /* planted bug: an index past the save area is accepted */ |
| 53 | if (a[1] < GSP_SEQ_REG_SAVE) io->save[a[1]] = io->rd(io->ctx, a[0]); |
| 54 | #else |
| 55 | if (a[1] >= GSP_SEQ_REG_SAVE) { if (why) *why = "save index out of range"; return -1; } |
| 56 | io->save[a[1]] = io->rd(io->ctx, a[0]); |
| 57 | #endif |
| 58 | } else { |
| 59 | if (!io->core) { if (why) *why = "core opcode with no core callback"; return -1; } |
| 60 | if (io->core(io->ctx, op)) { if (why) *why = "core opcode failed"; return -1; } |
| 61 | } |
| 62 | i += n; |
| 63 | } |
| 64 | return 0; |
| 65 | } |