Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/gpu/contract/gen/gsb_contract.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / gpu / contract / gen / gsb_contract.c` 142 lines ยท 5268 bytes Raw Blame History 1 /* GENERATED by aienos-devc from gb10_gsp.devc (sha256 `0bc4fee8da8fa819dd3230c3f3ff9c9d4cf35476b2684676c9b3f8de55b98cc0`). 2 * Do not edit: change the contract and run `aienos-devc gen gpu/contract/gb10_gsp.devc gpu/contract/gen`. 3 * Device gb10-gsp-boot (class gpu-boot, transport pci-bar). API: ../../gsp_mmio.h (the hand twin is ../../gsp_mmio.c). 4 * Register offsets and bits below come from the contract only; gsp_mmio.h's CK_GSP_ values are not used. 5 * Evidence: 25 manual line(s) in the contract. */ 6 #include "gsp_mmio.h" 7 8 #define GSB_EMEM_CHANNEL_BYTES 0x400u 9 #define GSB_EMEM_DW_PER_BLK 0x40u 10 #define GSB_SEC2_CMD_BUSY 0xc001cafeu 11 #define GSB_PRIV_LOCKED 0xbadf4100u 12 #define GSB_PRIV_LOCKED_MASK 0xffffff00u 13 #define GSB_WIN_SEC2 0x840000u /* bar 0 */ 14 #define GSB_WIN_GSP 0x110000u /* bar 0 */ 15 #define GSB_REG_SEC2_MAILBOX0 (GSB_WIN_SEC2 + 0x40u) 16 #define GSB_REG_SEC2_MAILBOX1 (GSB_WIN_SEC2 + 0x44u) 17 #define GSB_REG_EMEMC0 (GSB_WIN_SEC2 + 0xac0u) 18 #define GSB_REG_EMEMD0 (GSB_WIN_SEC2 + 0xac4u) 19 #define GSB_REG_QHEAD0 (GSB_WIN_SEC2 + 0xc00u) 20 #define GSB_REG_QTAIL0 (GSB_WIN_SEC2 + 0xc04u) 21 #define GSB_REG_GSP_MAILBOX0 (GSB_WIN_GSP + 0x40u) 22 #define GSB_REG_GSP_MAILBOX1 (GSB_WIN_GSP + 0x44u) 23 #define GSB_REG_FALCON_OS (GSB_WIN_GSP + 0x80u) 24 #define GSB_REG_HWCFG2 (GSB_WIN_GSP + 0xf4u) 25 #define GSB_REG_GSP_QHEAD0 (GSB_WIN_GSP + 0xc00u) 26 #define GSB_REG_CPUCTL (GSB_WIN_GSP + 0x1388u) 27 #define GSB_BIT_AINCW (1u << 24) 28 #define GSB_BIT_LOCKDOWN (1u << 13) 29 #define GSB_BIT_ACTIVE (1u << 7) 30 #define GSB_FLD_OFFS(v) (((v) >> 2) & 0x3fu) 31 #define GSB_FLD_BLK(v) (((v) >> 8) & 0xffu) 32 33 static uint32_t rd(const struct ck_gpu_regs *r, uint32_t off) 34 { 35 return r->rd(r->ctx, off); 36 } 37 38 static void wr(const struct ck_gpu_regs *r, uint32_t off, uint32_t v) 39 { 40 r->wr(r->ctx, off, v); 41 } 42 43 static void between_polls(const struct ck_gsp_mmio_in *in) 44 { 45 if (in->pause) 46 in->pause(in->pause_ctx); 47 } 48 49 int ck_gsp_mmio_boot(const struct ck_gpu_regs *r, const struct ck_gsp_mmio_in *in, struct ck_gsp_mmio_out *o) 50 { 51 uint32_t i, n; 52 53 for (i = 0; i < sizeof(*o); i++) 54 ((uint8_t *)o)[i] = 0; 55 if (!r || !in || !in->packet || in->len == 0 || in->len > GSB_EMEM_CHANNEL_BYTES) 56 return o->stage = CK_GSP_E_ARG; 57 for (n = 0;; n++) { 58 uint32_t v = rd(r, GSB_REG_QHEAD0); 59 if (v == rd(r, GSB_REG_QTAIL0)) 60 break; 61 if (n >= in->polls_can_send) { 62 o->polls[0] = n; 63 return o->stage = CK_GSP_E_BUSY; 64 } 65 between_polls(in); 66 } 67 o->polls[0] = n; 68 wr(r, GSB_REG_EMEMC0, GSB_BIT_AINCW); 69 uint32_t c0 = rd(r, GSB_REG_EMEMC0); 70 uint32_t start = (GSB_FLD_BLK(c0) * GSB_EMEM_DW_PER_BLK + GSB_FLD_OFFS(c0)); 71 for (i = 0; i + 4 <= in->len; i += 4) 72 wr(r, GSB_REG_EMEMD0, (uint32_t)in->packet[i] | (uint32_t)in->packet[i + 1] << 8 | 73 (uint32_t)in->packet[i + 2] << 16 | (uint32_t)in->packet[i + 3] << 24); 74 if (i < in->len) { 75 uint32_t v = 0; 76 for (uint32_t k = 0; i + k < in->len; k++) 77 v |= (uint32_t)in->packet[i + k] << (8 * k); 78 wr(r, GSB_REG_EMEMD0, v); 79 } 80 o->dwords = (in->len + 3) / 4; 81 o->ememc_after = rd(r, GSB_REG_EMEMC0); 82 if ((GSB_FLD_BLK(o->ememc_after) * GSB_EMEM_DW_PER_BLK + GSB_FLD_OFFS(o->ememc_after)) - start != o->dwords) 83 return o->stage = CK_GSP_E_EMEM; 84 wr(r, GSB_REG_QTAIL0, o->dwords * 4 - 4); 85 wr(r, GSB_REG_QHEAD0, 0); 86 for (n = 0;; n++) { 87 o->sec2_mb0 = rd(r, GSB_REG_SEC2_MAILBOX0); 88 if (o->sec2_mb0 != GSB_SEC2_CMD_BUSY) 89 break; 90 if (n >= in->polls_cmd) { 91 o->polls[1] = n; 92 o->sec2_mb1 = rd(r, GSB_REG_SEC2_MAILBOX1); 93 return o->stage = CK_GSP_E_SEC2; 94 } 95 between_polls(in); 96 } 97 o->polls[1] = n; 98 for (n = 0;; n++) { 99 o->hwcfg2 = rd(r, GSB_REG_HWCFG2); 100 if (o->hwcfg2 != 0 && (o->hwcfg2 & GSB_PRIV_LOCKED_MASK) != GSB_PRIV_LOCKED) 101 break; 102 if (n >= in->polls_mask) { 103 o->polls[2] = n; 104 return o->stage = CK_GSP_E_MASK; 105 } 106 between_polls(in); 107 } 108 o->polls[2] = n; 109 for (n = 0;; n++) { 110 int args_unread = 0; 111 o->gsp_mb0 = rd(r, GSB_REG_GSP_MAILBOX0); 112 if (o->gsp_mb0 != 0 && o->gsp_mb0 == (uint32_t)in->fmc_args_pa) { 113 o->gsp_mb1 = rd(r, GSB_REG_GSP_MAILBOX1); 114 args_unread = o->gsp_mb1 == (uint32_t)(in->fmc_args_pa >> 32); 115 } 116 if (!args_unread) { 117 o->hwcfg2 = rd(r, GSB_REG_HWCFG2); 118 if (!(o->hwcfg2 & GSB_BIT_LOCKDOWN) || o->gsp_mb0 != 0) 119 break; 120 } 121 if (n >= in->polls_lockdown) { 122 o->polls[3] = n; 123 o->gsp_mb1 = rd(r, GSB_REG_GSP_MAILBOX1); 124 return o->stage = CK_GSP_E_LOCKDOWN; 125 } 126 between_polls(in); 127 } 128 o->polls[3] = n; 129 o->gsp_mb0 = rd(r, GSB_REG_GSP_MAILBOX0); 130 if (o->gsp_mb0 != 0) 131 return o->stage = CK_GSP_E_FMC; 132 wr(r, GSB_REG_FALCON_OS, in->app_version); 133 o->cpuctl = rd(r, GSB_REG_CPUCTL); 134 if (!(o->cpuctl & GSB_BIT_ACTIVE)) 135 return o->stage = CK_GSP_E_RISCV; 136 return o->stage = CK_GSP_OK; 137 } 138 139 void ck_gsp_mmio_notify(const struct ck_gpu_regs *r) 140 { 141 wr(r, GSB_REG_GSP_QHEAD0, 0x0u); 142 }