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_mem.h
| 1 | /* gpu_mem.h -- bounded, reclaiming GPU-visible buffer allocator (cut D, host |
| 2 | * side), freestanding. Pages come from one physical window; each buffer |
| 3 | * gets a CPU address and a separate GPU virtual address, mapped through |
| 4 | * gmmu with the buffer's cache policy. |
| 5 | * |
| 6 | * Cache policy (spark-hardware-manual section 4, nvos.h NVOS32_ATTR2_ |
| 7 | * GPU_CACHEABLE comment): a word the host polls must be GPU-uncached, so |
| 8 | * CK_GMEM_HOST_POLLED forces CK_GMEM_UNCACHED and refuses CACHED. The CPU |
| 9 | * side stays coherent (PTE aperture SYS_COH: kern_gmmu.c picks SYS_COH for |
| 10 | * CPU-cached system memory). |
| 11 | * |
| 12 | * Free = unmap, invalidate, then the pages are reusable. If the invalidate |
| 13 | * does not complete, the pages are QUARANTINED: never handed out again for |
| 14 | * the life of this allocator, because the GPU may still hold translations |
| 15 | * to them. Only a device reset (operator step) can clear that. */ |
| 16 | #ifndef CK_GPU_MEM_H |
| 17 | #define CK_GPU_MEM_H |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include "gmmu.h" |
| 21 | |
| 22 | #define CK_GMEM_MAX_BUF 64u |
| 23 | #define CK_GMEM_MAX_PAGES 1024u /* 4 KiB pages: 4 MiB window max */ |
| 24 | #define CK_GMEM_PAGE CK_GMMU_SZ_4K |
| 25 | |
| 26 | enum ck_gmem_cache { CK_GMEM_CACHED = 0, CK_GMEM_UNCACHED = 1 }; |
| 27 | #define CK_GMEM_HOST_POLLED 0x1u /* the CPU polls a word in it */ |
| 28 | #define CK_GMEM_GPU_RO 0x2u /* GPU may only read */ |
| 29 | |
| 30 | enum ck_gmem_state { CK_GMEM_FREE = 0, CK_GMEM_LIVE = 1, CK_GMEM_QUARANTINED = 2 }; |
| 31 | |
| 32 | enum ck_gmem_status { |
| 33 | CK_GMEM_OK = 0, |
| 34 | CK_GMEM_EINVAL, |
| 35 | CK_GMEM_ENOMEM, /* no buffer slot or no run of free pages */ |
| 36 | CK_GMEM_ESTATE, /* not a live buffer of this allocator (double free, stale) */ |
| 37 | CK_GMEM_EMAP, /* gmmu refused; nothing changed */ |
| 38 | CK_GMEM_ETIMEDOUT, /* invalidate timed out; buffer quarantined */ |
| 39 | }; |
| 40 | |
| 41 | struct ck_gmem_buf { |
| 42 | uint64_t pa; /* GPU physical address */ |
| 43 | uint64_t gpu_va; /* what the GPU uses */ |
| 44 | uint8_t *cpu; /* what the CPU uses; never equal to gpu_va by construction */ |
| 45 | uint64_t size; /* rounded up to 4 KiB */ |
| 46 | uint32_t cache, flags, state, gen; |
| 47 | }; |
| 48 | |
| 49 | struct ck_gmem { |
| 50 | struct ck_gmmu_as *as; |
| 51 | const struct ck_gpu_regs *regs; |
| 52 | uint32_t poll_budget; |
| 53 | uint64_t pa_base, va_base; |
| 54 | uint32_t pages; |
| 55 | uint8_t page_state[CK_GMEM_MAX_PAGES]; /* ck_gmem_state per page */ |
| 56 | struct ck_gmem_buf buf[CK_GMEM_MAX_BUF]; |
| 57 | }; |
| 58 | |
| 59 | /* pa_base: start of the data pages (inside as->win, 4 KiB aligned, not |
| 60 | * overlapping the page-table pool); va_base: GPU VA of page 0 (2 MiB |
| 61 | * aligned, so a page's VA never equals its CPU address by accident: the |
| 62 | * tests also check that directly). */ |
| 63 | int ck_gmem_init(struct ck_gmem *m, struct ck_gmmu_as *as, const struct ck_gpu_regs *regs, uint32_t poll_budget, |
| 64 | uint64_t pa_base, uint32_t pages, uint64_t va_base); |
| 65 | int ck_gmem_alloc(struct ck_gmem *m, uint64_t size, uint32_t cache, uint32_t flags, struct ck_gmem_buf **out); |
| 66 | int ck_gmem_free(struct ck_gmem *m, struct ck_gmem_buf *b); |
| 67 | /* Retire without reuse: unmap and invalidate best-effort, pages stay QUARANTINED. */ |
| 68 | int ck_gmem_quarantine(struct ck_gmem *m, struct ck_gmem_buf *b); |
| 69 | uint32_t ck_gmem_count(const struct ck_gmem *m, uint32_t page_state); |
| 70 | |
| 71 | #endif |