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/gch_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 / gch_contract.c` 196 lines ยท 7013 bytes Raw Blame History 1 /* GENERATED by aienos-devc from gb10_chan.devc (sha256 `a138c84a7dbacf025cd3ee2a600c87476f52c07395eb85bcd0238056d3e69289`). 2 * Do not edit: change the contract and run `make -C native/kernel gpu-contract-gen`. 3 * Device gb10-gpu-channel (class gpu-chan, transport pci-bar, ring gpfifo). API: ../../gpu_chan.h (L193 twin). 4 * Evidence: 9 manual line(s) in the contract. */ 5 #include "gpu_chan.h" 6 7 #define GCH_CHID_MAX 0xfffull 8 #define GCH_RUNLIST_INVALID 0x7full 9 #define GCH_ENTRIES_MIN 0x2ull 10 #define GCH_ENTRIES_MAX 0x10000ull 11 #define GCH_GP_LEN_MAX 0x1fffffull 12 #define GCH_GP_VA_BITS 0x28ull 13 #define GCH_USERD_BYTES 0x200ull 14 #define GCH_USERD_GPPUT 0x8cull 15 #define GCH_INST_BYTES 0x1000ull 16 #define GCH_RAMIN_PDB_WORD 0x80ull 17 #define GCH_TARGET_SYS_COH 0x2ull 18 #define GCH_WIN_VF 0xb80000u /* bar 0 */ 19 #define GCH_REG_DOORBELL_OFF (GCH_WIN_VF + 0x30090u) 20 21 static inline uint32_t gch_enc_token(uint64_t runlist_id, uint64_t vector) 22 { 23 return (uint32_t)(((runlist_id & 0x7full) << 16) | ((vector & 0xfffull) << 0)); 24 } 25 26 static inline uint64_t gch_enc_gp_entry(uint64_t get, uint64_t level, uint64_t length) 27 { 28 return (uint64_t)((get & 0xfffffffffcull) | ((level & 0x1ull) << 41) | ((length & 0x1fffffull) << 42)); 29 } 30 31 static inline uint64_t gch_enc_rl_userd(uint64_t ptr_lo, uint64_t ptr_hi) 32 { 33 return (uint64_t)((ptr_lo & 0xffffff00ull) | (ptr_hi & 0xfffff00000000ull)); 34 } 35 36 static inline uint64_t gch_enc_ramin_pdb(uint64_t target, uint64_t big_page_64k, uint64_t addr) 37 { 38 return (uint64_t)(((target & 0x3ull) << 0) | ((big_page_64k & 0x1ull) << 11) | (addr & 0xfffffffffffff000ull)); 39 } 40 41 uint32_t ck_gpu_submit_token(uint32_t runlist_id, uint32_t chid) 42 { 43 return gch_enc_token(runlist_id, chid); 44 } 45 46 int ck_gpu_rl_userd(uint32_t out[2], uint64_t userd_pa) 47 { 48 if ((userd_pa & 0xffull) || userd_pa >= (1ull << 52)) 49 return CK_GCHAN_EINVAL; 50 uint64_t v = gch_enc_rl_userd(userd_pa, userd_pa); 51 out[0] = (uint32_t)v; 52 out[1] = (uint32_t)(v >> 32); 53 return CK_GCHAN_OK; 54 } 55 56 static void fence(void) 57 { 58 __atomic_thread_fence(__ATOMIC_SEQ_CST); 59 } 60 61 static void zero(uint8_t *p, uint64_t n) 62 { 63 for (uint64_t i = 0; i < n; i++) 64 ((volatile uint8_t *)p)[i] = 0; 65 } 66 67 static int buf_of(const struct ck_gmem *m, const struct ck_gmem_buf *b) 68 { 69 for (uint32_t i = 0; i < CK_GMEM_MAX_BUF; i++) 70 if (&m->buf[i] == b) 71 return b->state == CK_GMEM_LIVE; 72 return 0; 73 } 74 75 int ck_gpu_chan_create(struct ck_gpu_chan *c, struct ck_gmem *mem, const struct ck_gpu_regs *regs, 76 const struct ck_gpu_chan_ops *ops, void *ops_ctx, uint32_t chid, uint32_t runlist_id, 77 uint32_t entries) 78 { 79 if (!c || !mem || !regs || !ops || !ops->bind || !ops->disable || !ops->is_idle) 80 return CK_GCHAN_EINVAL; 81 if (chid > GCH_CHID_MAX) 82 return CK_GCHAN_EINVAL; 83 if (runlist_id >= GCH_RUNLIST_INVALID) 84 return CK_GCHAN_EINVAL; 85 if (entries < GCH_ENTRIES_MIN || entries > GCH_ENTRIES_MAX || (entries & (entries - 1u))) 86 return CK_GCHAN_EINVAL; 87 c->open = 0; 88 c->mem = mem; 89 c->regs = regs; 90 c->ops = *ops; 91 c->ops_ctx = ops_ctx; 92 c->put = c->retired = 0; 93 c->gpfifo = c->userd = c->inst = 0; 94 if (ck_gmem_alloc(mem, (uint64_t)entries * 8u, CK_GMEM_UNCACHED, CK_GMEM_GPU_RO, &c->gpfifo) != CK_GMEM_OK) 95 goto nomem; 96 if (ck_gmem_alloc(mem, GCH_USERD_BYTES, CK_GMEM_UNCACHED, CK_GMEM_HOST_POLLED, &c->userd) != CK_GMEM_OK) 97 goto nomem; 98 if (ck_gmem_alloc(mem, GCH_INST_BYTES, CK_GMEM_UNCACHED, 0, &c->inst) != CK_GMEM_OK) 99 goto nomem; 100 zero(c->gpfifo->cpu, c->gpfifo->size); 101 zero(c->userd->cpu, c->userd->size); 102 zero(c->inst->cpu, c->inst->size); 103 { 104 volatile uint32_t *w = (volatile uint32_t *)c->inst->cpu; 105 uint64_t v = gch_enc_ramin_pdb(GCH_TARGET_SYS_COH, 1, mem->as->root_pa); 106 w[GCH_RAMIN_PDB_WORD] = (uint32_t)v; 107 w[GCH_RAMIN_PDB_WORD + 1u] = (uint32_t)(v >> 32); 108 } 109 c->d.chid = chid; 110 c->d.runlist_id = runlist_id; 111 c->d.token = ck_gpu_submit_token(runlist_id, chid); 112 c->d.entries = entries; 113 c->d.pdb = mem->as->root_pa; 114 c->d.gpfifo_va = c->gpfifo->gpu_va; 115 c->d.userd_pa = c->userd->pa; 116 c->d.inst_pa = c->inst->pa; 117 if (ck_gpu_rl_userd(c->d.rl_entry, c->userd->pa) != CK_GCHAN_OK) 118 goto nomem; 119 fence(); 120 if (c->ops.bind(c->ops_ctx, &c->d) != 0) { 121 (void)ck_gmem_free(mem, c->inst); 122 (void)ck_gmem_free(mem, c->userd); 123 (void)ck_gmem_free(mem, c->gpfifo); 124 return CK_GCHAN_EBIND; 125 } 126 c->open = 1; 127 return CK_GCHAN_OK; 128 nomem: 129 if (c->inst) 130 (void)ck_gmem_free(mem, c->inst); 131 if (c->userd) 132 (void)ck_gmem_free(mem, c->userd); 133 if (c->gpfifo) 134 (void)ck_gmem_free(mem, c->gpfifo); 135 c->gpfifo = c->userd = c->inst = 0; 136 return CK_GCHAN_ENOMEM; 137 } 138 139 int ck_gpu_chan_submit(struct ck_gpu_chan *c, const struct ck_gmem_buf *pb, uint32_t off, uint32_t nwords) 140 { 141 if (!c || !c->open) 142 return CK_GCHAN_ESTATE; 143 if (!buf_of(c->mem, pb)) 144 return CK_GCHAN_EINVAL; 145 if ((off & 3u) || nwords == 0 || nwords > GCH_GP_LEN_MAX) 146 return CK_GCHAN_EINVAL; 147 uint64_t span = (uint64_t)nwords * 4u; 148 if ((uint64_t)off > pb->size || span > pb->size - off) 149 return CK_GCHAN_EINVAL; 150 uint64_t va = pb->gpu_va + off; 151 if (va >= (1ull << GCH_GP_VA_BITS) || span > (1ull << GCH_GP_VA_BITS) - va) 152 return CK_GCHAN_EINVAL; 153 if (c->put - c->retired >= c->d.entries - 1u) 154 return CK_GCHAN_EFULL; 155 volatile uint64_t *ring = (volatile uint64_t *)c->gpfifo->cpu; 156 ring[c->put % c->d.entries] = gch_enc_gp_entry(va, 1, nwords); 157 c->put++; 158 fence(); 159 *(volatile uint32_t *)(c->userd->cpu + GCH_USERD_GPPUT) = c->put % c->d.entries; 160 fence(); 161 c->regs->wr(c->regs->ctx, GCH_REG_DOORBELL_OFF, c->d.token); 162 return CK_GCHAN_OK; 163 } 164 165 void ck_gpu_chan_retire(struct ck_gpu_chan *c, uint32_t upto) 166 { 167 if (c && upto - c->retired <= c->put - c->retired) 168 c->retired = upto; 169 } 170 171 int ck_gpu_chan_teardown(struct ck_gpu_chan *c, uint32_t idle_budget, struct ck_gmem_buf **extra, uint32_t nextra) 172 { 173 if (!c || !c->open) 174 return CK_GCHAN_ESTATE; 175 c->open = 0; 176 (void)c->ops.disable(c->ops_ctx, c->d.chid); 177 int idle = 0; 178 for (uint32_t i = 0; i < idle_budget && !idle; i++) 179 idle = c->ops.is_idle(c->ops_ctx, c->d.chid) != 0; 180 struct ck_gmem_buf *own[3] = {c->inst, c->userd, c->gpfifo}; 181 int rc = CK_GCHAN_OK; 182 for (uint32_t i = 0; i < 3; i++) { 183 if (!idle) 184 (void)ck_gmem_quarantine(c->mem, own[i]); 185 else if (ck_gmem_free(c->mem, own[i]) != CK_GMEM_OK) 186 rc = CK_GCHAN_ETIMEDOUT; 187 } 188 if (!idle) { 189 for (uint32_t i = 0; extra && i < nextra; i++) 190 if (extra[i] && extra[i]->state == CK_GMEM_LIVE) 191 (void)ck_gmem_quarantine(c->mem, extra[i]); 192 rc = CK_GCHAN_ETIMEDOUT; 193 } 194 c->inst = c->userd = c->gpfifo = 0; 195 return rc; 196 }