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_mmio.c
| 1 | /* gsp_mmio.c -- the register-level GSP start-up on GB10B (see gsp_mmio.h for the steps and sources). */ |
| 2 | #include "gsp_mmio.h" |
| 3 | |
| 4 | static uint32_t rd(const struct ck_gpu_regs *r, uint32_t off) |
| 5 | { |
| 6 | return r->rd(r->ctx, off); |
| 7 | } |
| 8 | |
| 9 | static void wr(const struct ck_gpu_regs *r, uint32_t off, uint32_t v) |
| 10 | { |
| 11 | r->wr(r->ctx, off, v); |
| 12 | } |
| 13 | |
| 14 | static void between_polls(const struct ck_gsp_mmio_in *in) |
| 15 | { |
| 16 | if (in->pause) |
| 17 | in->pause(in->pause_ctx); |
| 18 | } |
| 19 | |
| 20 | static uint32_t emem_dwords(uint32_t ememc) |
| 21 | { |
| 22 | return ((ememc >> 8) & 0xFFu) * CK_GSP_EMEM_DW_PER_BLK + ((ememc >> 2) & 0x3Fu); |
| 23 | } |
| 24 | |
| 25 | static int mask_released(uint32_t hwcfg2) |
| 26 | { |
| 27 | return hwcfg2 != 0 && (hwcfg2 & CK_GSP_PRIV_LOCKED_MASK) != CK_GSP_PRIV_LOCKED; |
| 28 | } |
| 29 | |
| 30 | /* Step 8 condition. Reads MAILBOX0, MAILBOX1 only on a match, then HWCFG2, in the driver's order. */ |
| 31 | static int lockdown_done(const struct ck_gpu_regs *r, const struct ck_gsp_mmio_in *in, struct ck_gsp_mmio_out *o) |
| 32 | { |
| 33 | o->gsp_mb0 = rd(r, CK_GSP_FLCN_MAILBOX0); |
| 34 | if (o->gsp_mb0 != 0 && o->gsp_mb0 == (uint32_t)in->fmc_args_pa) { |
| 35 | o->gsp_mb1 = rd(r, CK_GSP_FLCN_MAILBOX1); |
| 36 | if (o->gsp_mb1 == (uint32_t)(in->fmc_args_pa >> 32)) |
| 37 | return 0; /* boot arguments not read by the FMC yet */ |
| 38 | } |
| 39 | o->hwcfg2 = rd(r, CK_GSP_FLCN_HWCFG2); |
| 40 | return !(o->hwcfg2 & CK_GSP_HWCFG2_LOCKDOWN) || o->gsp_mb0 != 0; |
| 41 | } |
| 42 | |
| 43 | int ck_gsp_mmio_boot(const struct ck_gpu_regs *r, const struct ck_gsp_mmio_in *in, struct ck_gsp_mmio_out *o) |
| 44 | { |
| 45 | uint32_t i, n; |
| 46 | |
| 47 | for (i = 0; i < sizeof(*o); i++) |
| 48 | ((uint8_t *)o)[i] = 0; |
| 49 | o->stage = CK_GSP_E_ARG; |
| 50 | if (!r || !in || !in->packet || in->len == 0 || in->len > CK_GSP_EMEM_CHANNEL_BYTES) |
| 51 | return o->stage; |
| 52 | |
| 53 | /* 1 */ |
| 54 | for (n = 0;; n++) { |
| 55 | uint32_t head = rd(r, CK_GSP_SEC2_QHEAD0); /* HEAD then TAIL, as _ksec2GetQueueHeadTail_GB10B :103-104 */ |
| 56 | if (head == rd(r, CK_GSP_SEC2_QTAIL0)) |
| 57 | break; |
| 58 | if (n >= in->polls_can_send) { |
| 59 | o->polls[0] = n; |
| 60 | return o->stage = CK_GSP_E_BUSY; |
| 61 | } |
| 62 | between_polls(in); |
| 63 | } |
| 64 | o->polls[0] = n; |
| 65 | |
| 66 | /* 2, 3, 4 */ |
| 67 | wr(r, CK_GSP_SEC2_EMEMC0, CK_GSP_EMEMC_AINCW); |
| 68 | uint32_t start = emem_dwords(rd(r, CK_GSP_SEC2_EMEMC0)); |
| 69 | for (i = 0; i + 4 <= in->len; i += 4) |
| 70 | wr(r, CK_GSP_SEC2_EMEMD0, (uint32_t)in->packet[i] | (uint32_t)in->packet[i + 1] << 8 | |
| 71 | (uint32_t)in->packet[i + 2] << 16 | (uint32_t)in->packet[i + 3] << 24); |
| 72 | if (i < in->len) { |
| 73 | uint32_t v = 0; |
| 74 | for (uint32_t k = 0; i + k < in->len; k++) |
| 75 | v |= (uint32_t)in->packet[i + k] << (8 * k); |
| 76 | wr(r, CK_GSP_SEC2_EMEMD0, v); |
| 77 | } |
| 78 | o->dwords = (in->len + 3) / 4; |
| 79 | o->ememc_after = rd(r, CK_GSP_SEC2_EMEMC0); |
| 80 | if (emem_dwords(o->ememc_after) - start != o->dwords) |
| 81 | return o->stage = CK_GSP_E_EMEM; |
| 82 | |
| 83 | /* 5: TAIL first, HEAD last (HEAD interrupts SEC2) */ |
| 84 | wr(r, CK_GSP_SEC2_QTAIL0, o->dwords * 4 - 4); |
| 85 | wr(r, CK_GSP_SEC2_QHEAD0, 0); |
| 86 | |
| 87 | /* 6 */ |
| 88 | for (n = 0;; n++) { |
| 89 | o->sec2_mb0 = rd(r, CK_GSP_SEC2_MAILBOX0); |
| 90 | if (o->sec2_mb0 != CK_GSP_SEC2_CMD_BUSY) |
| 91 | break; |
| 92 | if (n >= in->polls_cmd) { |
| 93 | o->polls[1] = n; |
| 94 | o->sec2_mb1 = rd(r, CK_GSP_SEC2_MAILBOX1); |
| 95 | return o->stage = CK_GSP_E_SEC2; |
| 96 | } |
| 97 | between_polls(in); |
| 98 | } |
| 99 | o->polls[1] = n; |
| 100 | |
| 101 | /* 7 */ |
| 102 | for (n = 0;; n++) { |
| 103 | o->hwcfg2 = rd(r, CK_GSP_FLCN_HWCFG2); |
| 104 | if (mask_released(o->hwcfg2)) |
| 105 | break; |
| 106 | if (n >= in->polls_mask) { |
| 107 | o->polls[2] = n; |
| 108 | return o->stage = CK_GSP_E_MASK; |
| 109 | } |
| 110 | between_polls(in); |
| 111 | } |
| 112 | o->polls[2] = n; |
| 113 | |
| 114 | /* 8 */ |
| 115 | for (n = 0;; n++) { |
| 116 | if (lockdown_done(r, in, o)) |
| 117 | break; |
| 118 | if (n >= in->polls_lockdown) { |
| 119 | o->polls[3] = n; |
| 120 | o->gsp_mb1 = rd(r, CK_GSP_FLCN_MAILBOX1); |
| 121 | return o->stage = CK_GSP_E_LOCKDOWN; |
| 122 | } |
| 123 | between_polls(in); |
| 124 | } |
| 125 | o->polls[3] = n; |
| 126 | |
| 127 | /* 9 */ |
| 128 | o->gsp_mb0 = rd(r, CK_GSP_FLCN_MAILBOX0); |
| 129 | if (o->gsp_mb0 != 0) |
| 130 | return o->stage = CK_GSP_E_FMC; |
| 131 | |
| 132 | /* 10, 11 */ |
| 133 | wr(r, CK_GSP_FLCN_OS, in->app_version); |
| 134 | o->cpuctl = rd(r, CK_GSP_RISCV_CPUCTL); |
| 135 | if (!(o->cpuctl & CK_GSP_CPUCTL_ACTIVE)) |
| 136 | return o->stage = CK_GSP_E_RISCV; |
| 137 | return o->stage = CK_GSP_OK; |
| 138 | } |
| 139 | |
| 140 | void ck_gsp_mmio_notify(const struct ck_gpu_regs *r) |
| 141 | { |
| 142 | wr(r, CK_GSP_QHEAD0, 0); |
| 143 | } |