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 / amd_gpu.h
| 1 | /* native/kernel/gpu/amd_gpu.h -- internal state shared across the AMD GPU driver. |
| 2 | * |
| 3 | * This header is NOT part of the public interface; only files in gpu/ include it. |
| 4 | * The public interface is amd_ring.h and x86/x86.h (ck_x86_gpu_start). |
| 5 | * No floats. All waits are TSC-bounded. */ |
| 6 | #pragma once |
| 7 | #include "../core/ck_internal.h" |
| 8 | #include "../mm/frames.h" |
| 9 | #ifdef AMD_GPU_TEST_HOST |
| 10 | #include "amd_host_shim.h" /* host test: rdtsc and pause without x86 asm */ |
| 11 | #else |
| 12 | #include "../x86/x86.h" |
| 13 | #endif |
| 14 | #include "amd_ring.h" |
| 15 | #include "amd_gpu_regs.h" |
| 16 | |
| 17 | /* ---- geometry -----------------------------------------------------------*/ |
| 18 | #define AMD_RING_DWORDS 4096u /* entries in each ring; must be power of 2 */ |
| 19 | #define AMD_RING_BYTES (AMD_RING_DWORDS * 4u) |
| 20 | #define AMD_FENCE_SLOTS 4u /* fence value slots per ring */ |
| 21 | #define AMD_SDMA_RING_DW 2048u /* SDMA ring dword count */ |
| 22 | #define AMD_MQD_SIZE 256u /* MQD is 64 dwords (256 bytes) */ |
| 23 | #define AMD_EOP_SIZE_DW 4096u /* end-of-pipeline buffer for MEC */ |
| 24 | |
| 25 | /* ---- timing constants ---------------------------------------------------*/ |
| 26 | #define GPU_VBLANK_MS 50u /* max wait for one vblank at 60 Hz panel (16.7 ms) */ |
| 27 | #define GPU_RING_WAIT_MS 5000u /* max for ring idle / fence */ |
| 28 | #define GPU_SMU_MSG_MS 50u /* SMC message round-trip */ |
| 29 | #define GPU_RESET_MS 100u /* soft reset settle time */ |
| 30 | #define GPU_UCODE_MS 200u /* microcode load settle */ |
| 31 | |
| 32 | /* ---- one ring's bookkeeping --------------------------------------------*/ |
| 33 | struct amd_one_ring { |
| 34 | volatile uint32_t *ring; /* ring buffer (physical, mapped uncached-minus or WC) */ |
| 35 | uint64_t ring_pa; |
| 36 | uint32_t wptr; /* next dword to write (in dwords, mod ring_dw) */ |
| 37 | uint32_t ring_dw; /* ring size in dwords */ |
| 38 | /* read pointer written back by hardware */ |
| 39 | volatile uint32_t *rptr_va; |
| 40 | uint64_t rptr_pa; |
| 41 | /* fence: hardware writes seq here when CP reaches PM4_WRITE_DATA */ |
| 42 | volatile uint32_t *fence_va; |
| 43 | uint64_t fence_pa; |
| 44 | uint32_t fence_seq; |
| 45 | int ready; |
| 46 | }; |
| 47 | |
| 48 | /* ---- global GPU state --------------------------------------------------*/ |
| 49 | struct amd_gpu_state { |
| 50 | /* BAR5 register aperture */ |
| 51 | volatile uint32_t *regs; |
| 52 | |
| 53 | /* BAR0 VRAM aperture (as seen by CPU) */ |
| 54 | volatile uint8_t *vram_cpu; /* mapped UC-, or WC for front+back */ |
| 55 | uint64_t vram_pa; /* BAR0 physical address */ |
| 56 | uint64_t vram_sz; /* size in bytes (from MC_VM_FB_LOCATION) */ |
| 57 | |
| 58 | /* GOP front buffer (CRTC 0 is scanning out of here) */ |
| 59 | uint64_t fb_pa; /* physical address */ |
| 60 | uint32_t fb_w, fb_h, fb_pitch; /* pixels */ |
| 61 | uint32_t fb_off; /* offset within VRAM = fb_pa - vram_pa */ |
| 62 | |
| 63 | /* Back buffer (allocated in VRAM after front buffer) */ |
| 64 | uint64_t bb_pa; |
| 65 | uint32_t bb_off; /* offset within VRAM */ |
| 66 | volatile uint32_t *bb_cpu; /* CPU pointer (mapped UC-) */ |
| 67 | |
| 68 | /* Current front/back swap state: 0 = GOP layout active, 1 = swapped */ |
| 69 | int flipped; |
| 70 | |
| 71 | /* TSC Hz (from kmain_x86 measurement) */ |
| 72 | uint64_t tsc_hz; |
| 73 | |
| 74 | /* MC state */ |
| 75 | uint32_t mc_fb_loc; /* raw MC_VM_FB_LOCATION */ |
| 76 | uint64_t mc_fb_start; /* VRAM MC start */ |
| 77 | uint64_t mc_fb_end; /* VRAM MC end */ |
| 78 | uint64_t mc_sys_lo, mc_sys_hi; /* system aperture */ |
| 79 | |
| 80 | /* SMU state as-found */ |
| 81 | int smu_alive; /* SMC firmware header readable (Version not 0 or all-ones) */ |
| 82 | uint32_t smu_fw_ver; |
| 83 | |
| 84 | /* Ring state */ |
| 85 | struct amd_one_ring ring_gfx; |
| 86 | struct amd_one_ring ring_sdma; |
| 87 | struct amd_one_ring ring_mec; |
| 88 | |
| 89 | /* DMA memory pool (allocated from frames) */ |
| 90 | uint64_t dma_pa; /* base PA of contiguous DMA block */ |
| 91 | uint8_t *dma_va; /* CPU-side pointer */ |
| 92 | uint32_t dma_off; /* allocation cursor */ |
| 93 | uint32_t dma_total; /* total bytes in block */ |
| 94 | |
| 95 | /* Overall init state (bitmask of completed steps) */ |
| 96 | unsigned steps_done; /* AMD_STEP_* flags */ |
| 97 | int ready; |
| 98 | }; |
| 99 | |
| 100 | #define AMD_STEP_DISPLAY (1u << 0) |
| 101 | #define AMD_STEP_MC (1u << 1) |
| 102 | #define AMD_STEP_SMU (1u << 2) |
| 103 | #define AMD_STEP_UCODE (1u << 3) |
| 104 | #define AMD_STEP_GFX_RING (1u << 4) |
| 105 | #define AMD_STEP_SDMA (1u << 5) |
| 106 | #define AMD_STEP_COMPUTE (1u << 6) |
| 107 | |
| 108 | /* Singleton (defined in gpu_x86.c). */ |
| 109 | extern struct amd_gpu_state g_amd_gpu; |
| 110 | |
| 111 | /* ---- helpers -----------------------------------------------------------*/ |
| 112 | static inline uint32_t amd_rd(unsigned idx) { |
| 113 | return g_amd_gpu.regs[idx]; |
| 114 | } |
| 115 | static inline void amd_wr(unsigned idx, uint32_t v) { |
| 116 | g_amd_gpu.regs[idx] = v; |
| 117 | } |
| 118 | /* wait_us via TSC */ |
| 119 | static inline void amd_wait_us(uint32_t us) { |
| 120 | uint64_t t0 = ck_rdtsc(); |
| 121 | uint64_t ticks = g_amd_gpu.tsc_hz / 1000000ull * us; |
| 122 | while (ck_rdtsc() - t0 < ticks) ck_pause(); |
| 123 | } |
| 124 | static inline void amd_wait_ms(uint32_t ms) { |
| 125 | amd_wait_us((uint32_t)(ms * 1000u)); |
| 126 | } |
| 127 | /* Allocate contiguous bytes from DMA pool; returns PA and zero-initialises. */ |
| 128 | static inline int amd_dma_alloc(uint32_t bytes, uint64_t *pa_out, void **va_out) { |
| 129 | uint32_t aligned = (bytes + 63u) & ~63u; |
| 130 | if (g_amd_gpu.dma_off + aligned > g_amd_gpu.dma_total) return -1; |
| 131 | *pa_out = g_amd_gpu.dma_pa + g_amd_gpu.dma_off; |
| 132 | *va_out = g_amd_gpu.dma_va + g_amd_gpu.dma_off; |
| 133 | g_amd_gpu.dma_off += aligned; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* Takes npages of contiguous frames from fr, all ending at or below limit, and |
| 138 | * records them as the DMA pool (dma_pa, dma_total, dma_off). The caller maps |
| 139 | * dma_va and zeroes the pool. Returns 0, or -1 if fr is NULL or nothing fits. |
| 140 | * Limit: every ring address the CP and SDMA take is a 40-bit field (gfx_v8_0.c |
| 141 | * gfx_v8_0_cp_gfx_resume: CP_RB0_BASE = gpu_addr >> 8, CP_RB0_BASE_HI = the rest; |
| 142 | * sdma_v3_0.c sdma_v3_0_gfx_resume: SDMA0_GFX_RB_BASE_HI = gpu_addr >> 40). The |
| 143 | * pool sits below 4 GiB, which is stricter than that field. This driver does not |
| 144 | * program the GART, so the GPU reaches a physical address only if the firmware |
| 145 | * left the mapping in place: unverified on the iMac. */ |
| 146 | #define AMD_DMA_LIMIT 0x100000000ull |
| 147 | |
| 148 | static inline int amd_dma_pool_take(struct amd_gpu_state *g, struct ck_frames *fr, |
| 149 | uint64_t npages, uint64_t limit) |
| 150 | { |
| 151 | uint64_t pa; |
| 152 | if (!fr || ck_frames_alloc_below(fr, npages, CK_PAGE, limit, &pa)) |
| 153 | return -1; |
| 154 | g->dma_pa = pa; |
| 155 | g->dma_total = (uint32_t)(npages * CK_PAGE); |
| 156 | g->dma_off = 0; |
| 157 | return 0; |
| 158 | } |
| 159 | /* Read BAR5 register with fence */ |
| 160 | static inline uint32_t amd_rd_fence(unsigned idx) { |
| 161 | uint32_t v = amd_rd(idx); |
| 162 | __asm__ volatile("" ::: "memory"); |
| 163 | return v; |
| 164 | } |
| 165 | |
| 166 | /* ---- step init functions (each implemented in its own .c file) ----------*/ |
| 167 | int amd_display_init(void); /* amd_display.c */ |
| 168 | int amd_mc_read(void); /* amd_mc.c */ |
| 169 | int amd_smu_check(void); /* amd_smu.c */ |
| 170 | int amd_ucode_load(void); /* amd_ucode.c */ |
| 171 | int amd_gfx_ring_init(void); /* amd_ucode.c */ |
| 172 | int amd_gfx_ring_test(void); /* amd_ucode.c */ |
| 173 | void amd_sdma_bench(void); /* amd_sdma.c */ |
| 174 | int amd_sdma_init(void); /* amd_sdma.c */ |
| 175 | int amd_compute_init(void); /* amd_compute.c */ |