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 / gpu_chan.c
| 1 | /* gpu_chan.c -- see gpu_chan.h. Freestanding: no libc. */ |
| 2 | #include "gpu_chan.h" |
| 3 | |
| 4 | uint32_t ck_gpu_submit_token(uint32_t runlist_id, uint32_t chid) |
| 5 | { |
| 6 | return ((runlist_id & 0x7Fu) << 16) | (chid & CK_GPU_CHID_MAX); |
| 7 | } |
| 8 | |
| 9 | int ck_gpu_rl_userd(uint32_t out[2], uint64_t userd_pa) |
| 10 | { |
| 11 | #ifndef CK_GPU_MUTANT_NO_USERD_WIDTH |
| 12 | if ((userd_pa & 0xFFu) || userd_pa >= (1ull << 52)) |
| 13 | return CK_GCHAN_EINVAL; |
| 14 | #endif |
| 15 | out[0] = (uint32_t)userd_pa & 0xFFFFFF00u; |
| 16 | out[1] = (uint32_t)(userd_pa >> 32) & 0xFFFFFu; |
| 17 | return CK_GCHAN_OK; |
| 18 | } |
| 19 | |
| 20 | static void fence(void) |
| 21 | { |
| 22 | /* physics nvrm_enqueue uses "dsb sy" around the GPPut store; on the |
| 23 | * kernel target this is the same full barrier. */ |
| 24 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 25 | } |
| 26 | |
| 27 | static void zero(uint8_t *p, uint64_t n) |
| 28 | { |
| 29 | for (uint64_t i = 0; i < n; i++) |
| 30 | ((volatile uint8_t *)p)[i] = 0; |
| 31 | } |
| 32 | |
| 33 | int ck_gpu_chan_create(struct ck_gpu_chan *c, struct ck_gmem *mem, const struct ck_gpu_regs *regs, |
| 34 | const struct ck_gpu_chan_ops *ops, void *ops_ctx, uint32_t chid, uint32_t runlist_id, |
| 35 | uint32_t entries) |
| 36 | { |
| 37 | if (!c || !mem || !regs || !ops || !ops->bind || !ops->disable || !ops->is_idle) |
| 38 | return CK_GCHAN_EINVAL; |
| 39 | #ifndef CK_GPU_MUTANT_NO_CHID_WIDTH |
| 40 | if (chid > CK_GPU_CHID_MAX || runlist_id >= CK_GPU_RUNLIST_INVALID) |
| 41 | return CK_GCHAN_EINVAL; |
| 42 | #endif |
| 43 | if (entries < 2 || entries > 65536u || (entries & (entries - 1u))) |
| 44 | return CK_GCHAN_EINVAL; |
| 45 | c->open = 0; |
| 46 | c->mem = mem; |
| 47 | c->regs = regs; |
| 48 | c->ops = *ops; |
| 49 | c->ops_ctx = ops_ctx; |
| 50 | c->put = c->retired = 0; |
| 51 | c->gpfifo = c->userd = c->inst = 0; |
| 52 | |
| 53 | /* Ring and USERD are written by the CPU while the GPU reads them, so |
| 54 | * GPU-uncached (spark-hardware-manual 4.2); USERD is also host-polled |
| 55 | * state in general. */ |
| 56 | if (ck_gmem_alloc(mem, (uint64_t)entries * 8u, CK_GMEM_UNCACHED, CK_GMEM_GPU_RO, &c->gpfifo) != CK_GMEM_OK) |
| 57 | goto nomem; |
| 58 | if (ck_gmem_alloc(mem, CK_GPU_USERD_BYTES, CK_GMEM_UNCACHED, CK_GMEM_HOST_POLLED, &c->userd) != CK_GMEM_OK) |
| 59 | goto nomem; |
| 60 | if (ck_gmem_alloc(mem, CK_GPU_INST_BYTES, CK_GMEM_UNCACHED, 0, &c->inst) != CK_GMEM_OK) |
| 61 | goto nomem; |
| 62 | zero(c->gpfifo->cpu, c->gpfifo->size); |
| 63 | zero(c->userd->cpu, c->userd->size); |
| 64 | zero(c->inst->cpu, c->inst->size); |
| 65 | #ifndef CK_GPU_MUTANT_NO_INST_PDB |
| 66 | /* The channel's address space, as kgmmuInstBlkPageDirBaseGet_GP100 writes it. |
| 67 | * FAULT_REPLAY_* and VOL stay 0 (replay disabled, non-volatile). */ |
| 68 | volatile uint32_t *ramin = (volatile uint32_t *)c->inst->cpu; |
| 69 | uint64_t pdb = mem->as->root_pa; |
| 70 | ramin[CK_GPU_RAMIN_PDB_WORD] = ((uint32_t)pdb & 0xFFFFF000u) | CK_GPU_RAMIN_BIG_PAGE_64K | CK_GPU_RAMIN_TARGET_SYS_COH; |
| 71 | /* USE_NEW_PT_FORMAT (bit 10) stays FALSE: GP100 sets it only for GMMU_FMT_VERSION_2 |
| 72 | * and GB10B uses VERSION_3 (kern_gmmu_gp100.c:165, kern_gmmu_fmt_gh10x.c). */ |
| 73 | ramin[CK_GPU_RAMIN_PDB_WORD + 1u] = (uint32_t)(pdb >> 32); |
| 74 | #endif |
| 75 | |
| 76 | c->d.chid = chid; |
| 77 | c->d.runlist_id = runlist_id; |
| 78 | c->d.token = ck_gpu_submit_token(runlist_id, chid); |
| 79 | c->d.entries = entries; |
| 80 | c->d.pdb = mem->as->root_pa; |
| 81 | c->d.gpfifo_va = c->gpfifo->gpu_va; |
| 82 | c->d.userd_pa = c->userd->pa; |
| 83 | c->d.inst_pa = c->inst->pa; |
| 84 | if (ck_gpu_rl_userd(c->d.rl_entry, c->userd->pa) != CK_GCHAN_OK) |
| 85 | goto nomem; |
| 86 | fence(); |
| 87 | if (c->ops.bind(c->ops_ctx, &c->d) != 0) { |
| 88 | (void)ck_gmem_free(mem, c->inst); |
| 89 | (void)ck_gmem_free(mem, c->userd); |
| 90 | (void)ck_gmem_free(mem, c->gpfifo); |
| 91 | return CK_GCHAN_EBIND; |
| 92 | } |
| 93 | c->open = 1; |
| 94 | return CK_GCHAN_OK; |
| 95 | nomem: |
| 96 | if (c->inst) |
| 97 | (void)ck_gmem_free(mem, c->inst); |
| 98 | if (c->userd) |
| 99 | (void)ck_gmem_free(mem, c->userd); |
| 100 | if (c->gpfifo) |
| 101 | (void)ck_gmem_free(mem, c->gpfifo); |
| 102 | c->gpfifo = c->userd = c->inst = 0; |
| 103 | return CK_GCHAN_ENOMEM; |
| 104 | } |
| 105 | |
| 106 | static int buf_of(const struct ck_gmem *m, const struct ck_gmem_buf *b) |
| 107 | { |
| 108 | for (uint32_t i = 0; i < CK_GMEM_MAX_BUF; i++) |
| 109 | if (&m->buf[i] == b) |
| 110 | return b->state == CK_GMEM_LIVE; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int ck_gpu_chan_submit(struct ck_gpu_chan *c, const struct ck_gmem_buf *pb, uint32_t off, uint32_t nwords) |
| 115 | { |
| 116 | if (!c || !c->open) |
| 117 | return CK_GCHAN_ESTATE; |
| 118 | if (!buf_of(c->mem, pb)) |
| 119 | return CK_GCHAN_EINVAL; |
| 120 | if ((off & 3u) || nwords == 0 || nwords > CK_GPU_GP_LEN_MAX) |
| 121 | return CK_GCHAN_EINVAL; |
| 122 | uint64_t span = (uint64_t)nwords * 4u; |
| 123 | if ((uint64_t)off > pb->size || span > pb->size - off) |
| 124 | return CK_GCHAN_EINVAL; |
| 125 | uint64_t va = pb->gpu_va + off; |
| 126 | #ifndef CK_GPU_MUTANT_NO_GP_VA40 |
| 127 | if (va >= CK_GPU_GP_VA_LIMIT || span > CK_GPU_GP_VA_LIMIT - va) |
| 128 | return CK_GCHAN_EINVAL; /* GP_ENTRY1_GET_HI is 8 bits: 40-bit pushbuffer address */ |
| 129 | #endif |
| 130 | #ifndef CK_GPU_MUTANT_NO_RING_FULL |
| 131 | if (c->put - c->retired >= c->d.entries - 1u) |
| 132 | return CK_GCHAN_EFULL; |
| 133 | #endif |
| 134 | |
| 135 | volatile uint64_t *ring = (volatile uint64_t *)c->gpfifo->cpu; |
| 136 | ring[c->put % c->d.entries] = ck_gpu_gp_entry(va, nwords); |
| 137 | c->put++; |
| 138 | fence(); |
| 139 | *(volatile uint32_t *)(c->userd->cpu + CK_GPU_USERD_GPPUT) = c->put % c->d.entries; |
| 140 | fence(); |
| 141 | c->regs->wr(c->regs->ctx, CK_GPU_REG_DOORBELL, c->d.token); |
| 142 | return CK_GCHAN_OK; |
| 143 | } |
| 144 | |
| 145 | void ck_gpu_chan_retire(struct ck_gpu_chan *c, uint32_t upto) |
| 146 | { |
| 147 | /* Same wrap-safe rule as physics nvrm_retire: only move forward, never past put. */ |
| 148 | if (c && upto - c->retired <= c->put - c->retired) |
| 149 | c->retired = upto; |
| 150 | } |
| 151 | |
| 152 | int ck_gpu_chan_teardown(struct ck_gpu_chan *c, uint32_t idle_budget, struct ck_gmem_buf **extra, uint32_t nextra) |
| 153 | { |
| 154 | if (!c || !c->open) |
| 155 | return CK_GCHAN_ESTATE; |
| 156 | c->open = 0; |
| 157 | #ifndef CK_GPU_MUTANT_NO_DISABLE |
| 158 | (void)c->ops.disable(c->ops_ctx, c->d.chid); |
| 159 | #endif |
| 160 | int idle = 0; |
| 161 | for (uint32_t i = 0; i < idle_budget && !idle; i++) |
| 162 | idle = c->ops.is_idle(c->ops_ctx, c->d.chid) != 0; |
| 163 | #ifdef CK_GPU_MUTANT_NO_IDLE_QUARANTINE |
| 164 | idle = 1; |
| 165 | #endif |
| 166 | struct ck_gmem_buf *own[3] = {c->inst, c->userd, c->gpfifo}; |
| 167 | int rc = CK_GCHAN_OK; |
| 168 | for (uint32_t i = 0; i < 3; i++) { |
| 169 | if (!idle) |
| 170 | (void)ck_gmem_quarantine(c->mem, own[i]); |
| 171 | else if (ck_gmem_free(c->mem, own[i]) != CK_GMEM_OK) |
| 172 | rc = CK_GCHAN_ETIMEDOUT; /* free failed (invalidate timeout or dead address space): ck_gmem quarantined it */ |
| 173 | } |
| 174 | if (!idle) { |
| 175 | for (uint32_t i = 0; extra && i < nextra; i++) |
| 176 | if (extra[i] && extra[i]->state == CK_GMEM_LIVE) |
| 177 | (void)ck_gmem_quarantine(c->mem, extra[i]); |
| 178 | rc = CK_GCHAN_ETIMEDOUT; |
| 179 | } |
| 180 | c->gpfifo = c->userd = c->inst = 0; |
| 181 | return rc; |
| 182 | } |