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.c
| 1 | /* gpu_mem.c -- see gpu_mem.h. Freestanding: no libc. */ |
| 2 | #include "gpu_mem.h" |
| 3 | |
| 4 | int ck_gmem_init(struct ck_gmem *m, struct ck_gmmu_as *as, const struct ck_gpu_regs *regs, uint32_t poll_budget, |
| 5 | uint64_t pa_base, uint32_t pages, uint64_t va_base) |
| 6 | { |
| 7 | if (!m || !as || !as->win || !regs || pages == 0 || pages > CK_GMEM_MAX_PAGES || (pa_base & (CK_GMEM_PAGE - 1u)) || |
| 8 | (va_base & (CK_GMMU_SZ_2M - 1u)) || poll_budget == 0) |
| 9 | return CK_GMEM_EINVAL; |
| 10 | const struct ck_gpu_window *w = as->win; |
| 11 | uint64_t bytes = (uint64_t)pages * CK_GMEM_PAGE; |
| 12 | if (pa_base < w->pa_base || pa_base - w->pa_base > w->size || bytes > w->size - (pa_base - w->pa_base)) |
| 13 | return CK_GMEM_EINVAL; |
| 14 | if (va_base >= (1ull << CK_GMMU_VA_BITS) || bytes > (1ull << CK_GMMU_VA_BITS) - va_base) |
| 15 | return CK_GMEM_EINVAL; |
| 16 | /* The page-table pool must not overlap the data pages. */ |
| 17 | for (uint32_t i = 0; i < as->pt_count; i++) |
| 18 | if (as->pt_pa[i] + CK_GMMU_PT_BYTES > pa_base && as->pt_pa[i] < pa_base + bytes) |
| 19 | return CK_GMEM_EINVAL; |
| 20 | m->as = as; |
| 21 | m->regs = regs; |
| 22 | m->poll_budget = poll_budget; |
| 23 | m->pa_base = pa_base; |
| 24 | m->va_base = va_base; |
| 25 | m->pages = pages; |
| 26 | for (uint32_t i = 0; i < CK_GMEM_MAX_PAGES; i++) |
| 27 | m->page_state[i] = CK_GMEM_FREE; |
| 28 | for (uint32_t i = 0; i < CK_GMEM_MAX_BUF; i++) { |
| 29 | m->buf[i].state = CK_GMEM_FREE; |
| 30 | m->buf[i].gen = 0; |
| 31 | } |
| 32 | return CK_GMEM_OK; |
| 33 | } |
| 34 | |
| 35 | static int live_buf(const struct ck_gmem *m, const struct ck_gmem_buf *b) |
| 36 | { |
| 37 | if (!m || !b || b < &m->buf[0] || b >= &m->buf[CK_GMEM_MAX_BUF]) |
| 38 | return 0; |
| 39 | return (uint64_t)((const uint8_t *)b - (const uint8_t *)&m->buf[0]) % sizeof(m->buf[0]) == 0 && |
| 40 | b->state == CK_GMEM_LIVE; |
| 41 | } |
| 42 | |
| 43 | static void set_pages(struct ck_gmem *m, const struct ck_gmem_buf *b, uint8_t st) |
| 44 | { |
| 45 | uint32_t first = (uint32_t)((b->pa - m->pa_base) / CK_GMEM_PAGE); |
| 46 | uint32_t n = (uint32_t)(b->size / CK_GMEM_PAGE); |
| 47 | for (uint32_t i = 0; i < n; i++) |
| 48 | m->page_state[first + i] = st; |
| 49 | } |
| 50 | |
| 51 | int ck_gmem_alloc(struct ck_gmem *m, uint64_t size, uint32_t cache, uint32_t flags, struct ck_gmem_buf **out) |
| 52 | { |
| 53 | if (!m || !out || size == 0 || size > (uint64_t)CK_GMEM_MAX_PAGES * CK_GMEM_PAGE || cache > CK_GMEM_UNCACHED || |
| 54 | (flags & ~(CK_GMEM_HOST_POLLED | CK_GMEM_GPU_RO))) |
| 55 | return CK_GMEM_EINVAL; |
| 56 | #ifndef CK_GPU_MUTANT_NO_POLLED_UNCACHED |
| 57 | if ((flags & CK_GMEM_HOST_POLLED) && cache != CK_GMEM_UNCACHED) |
| 58 | return CK_GMEM_EINVAL; /* host-polled words must be GPU-uncached */ |
| 59 | #endif |
| 60 | *out = 0; |
| 61 | uint32_t n = (uint32_t)((size + CK_GMEM_PAGE - 1u) / CK_GMEM_PAGE); |
| 62 | |
| 63 | struct ck_gmem_buf *b = 0; |
| 64 | for (uint32_t i = 0; i < CK_GMEM_MAX_BUF && !b; i++) |
| 65 | if (m->buf[i].state == CK_GMEM_FREE) |
| 66 | b = &m->buf[i]; |
| 67 | if (!b) |
| 68 | return CK_GMEM_ENOMEM; |
| 69 | |
| 70 | /* First fit over FREE pages only; QUARANTINED pages are never taken. */ |
| 71 | uint32_t run = 0, start = 0; |
| 72 | int found = 0; |
| 73 | for (uint32_t i = 0; i < m->pages && !found; i++) { |
| 74 | if (m->page_state[i] != CK_GMEM_FREE) { |
| 75 | run = 0; |
| 76 | continue; |
| 77 | } |
| 78 | if (run == 0) |
| 79 | start = i; |
| 80 | if (++run == n) |
| 81 | found = 1; |
| 82 | } |
| 83 | if (!found) |
| 84 | return CK_GMEM_ENOMEM; |
| 85 | |
| 86 | uint64_t off = (uint64_t)start * CK_GMEM_PAGE; |
| 87 | uint64_t bytes = (uint64_t)n * CK_GMEM_PAGE; |
| 88 | uint32_t ro = (flags & CK_GMEM_GPU_RO) ? 1u : 0u; |
| 89 | uint32_t pcf = cache == CK_GMEM_UNCACHED ? (ro ? CK_GMMU_PCF_RO_UNCACHED : CK_GMMU_PCF_RW_UNCACHED) |
| 90 | : (ro ? CK_GMMU_PCF_RO_CACHED : CK_GMMU_PCF_RW_CACHED); |
| 91 | /* Zero before the GPU mapping exists, so a reused page cannot show the previous buffer. */ |
| 92 | { |
| 93 | volatile uint8_t *cpu = m->as->win->cpu + (m->pa_base + off - m->as->win->pa_base); |
| 94 | for (uint64_t i = 0; i < bytes; i++) |
| 95 | cpu[i] = 0; |
| 96 | } |
| 97 | if (ck_gmmu_map(m->as, m->va_base + off, m->pa_base + off, bytes, CK_GMMU_SZ_4K, |
| 98 | CK_GMMU_PTE_AP_SYS_COH, pcf) != CK_GMMU_OK) |
| 99 | return CK_GMEM_EMAP; |
| 100 | |
| 101 | b->pa = m->pa_base + off; |
| 102 | b->gpu_va = m->va_base + off; |
| 103 | b->cpu = m->as->win->cpu + (b->pa - m->as->win->pa_base); |
| 104 | b->size = bytes; |
| 105 | b->cache = cache; |
| 106 | b->flags = flags; |
| 107 | b->state = CK_GMEM_LIVE; |
| 108 | b->gen++; |
| 109 | set_pages(m, b, CK_GMEM_LIVE); |
| 110 | *out = b; |
| 111 | return CK_GMEM_OK; |
| 112 | } |
| 113 | |
| 114 | static int retire(struct ck_gmem *m, struct ck_gmem_buf *b, int reuse) |
| 115 | { |
| 116 | if (!live_buf(m, b)) |
| 117 | return CK_GMEM_ESTATE; |
| 118 | int um = ck_gmmu_unmap(m->as, b->gpu_va, b->size, CK_GMMU_SZ_4K); |
| 119 | int iv = um == CK_GMMU_OK ? ck_gmmu_invalidate(m->as, m->regs, m->poll_budget) : um; |
| 120 | if (reuse && iv == CK_GMMU_OK) { |
| 121 | set_pages(m, b, CK_GMEM_FREE); |
| 122 | b->state = CK_GMEM_FREE; |
| 123 | return CK_GMEM_OK; |
| 124 | } |
| 125 | #ifndef CK_GPU_MUTANT_NO_QUARANTINE |
| 126 | set_pages(m, b, CK_GMEM_QUARANTINED); |
| 127 | #else |
| 128 | set_pages(m, b, CK_GMEM_FREE); |
| 129 | #endif |
| 130 | b->state = CK_GMEM_FREE; /* the slot is reusable; the pages are not */ |
| 131 | if (!reuse) |
| 132 | return CK_GMEM_OK; |
| 133 | return iv == CK_GMMU_ETIMEDOUT || iv == CK_GMMU_EDEAD ? CK_GMEM_ETIMEDOUT : CK_GMEM_EMAP; |
| 134 | } |
| 135 | |
| 136 | int ck_gmem_free(struct ck_gmem *m, struct ck_gmem_buf *b) |
| 137 | { |
| 138 | return retire(m, b, 1); |
| 139 | } |
| 140 | |
| 141 | int ck_gmem_quarantine(struct ck_gmem *m, struct ck_gmem_buf *b) |
| 142 | { |
| 143 | return retire(m, b, 0); |
| 144 | } |
| 145 | |
| 146 | uint32_t ck_gmem_count(const struct ck_gmem *m, uint32_t page_state) |
| 147 | { |
| 148 | uint32_t n = 0; |
| 149 | for (uint32_t i = 0; m && i < m->pages; i++) |
| 150 | n += m->page_state[i] == page_state ? 1u : 0u; |
| 151 | return n; |
| 152 | } |