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/amd_display.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 / amd_display.c` 214 lines ยท 8768 bytes Raw Blame History 1 /* native/kernel/gpu/amd_display.c -- DCE 11.2 display engine: double-buffered 2 * page flip with vsync wait for the 2017 iMac's AMD Radeon Pro 570/575/580 3 * (Polaris 10, PCI 1002:67df). 4 * 5 * The GOP leaves CRTC 0 running and scanning out of the front buffer. This 6 * file: 7 * 1. Reads back the current GRPH_PRIMARY_SURFACE_ADDRESS to confirm we 8 * know which buffer the hardware is scanning. 9 * 2. Allocates a second (back) buffer at the next MiB-aligned offset after 10 * the front buffer inside VRAM. 11 * 3. Maps the back buffer as uncached (UC-) via ck_x86_map_mmio so the 12 * CPU can write to it. (WC mapping is preferred for throughput; that 13 * requires PAT reconfiguration, tracked as a TODO.) 14 * 4. Exposes ck_gpu_flip() and ck_gpu_backbuffer() for the renderer. 15 * 16 * Sequence for each flip (follows dce_v11_0_page_flip in 17 * drivers/gpu/drm/amd/amdgpu/dce_v11_0.c, MIT license): 18 * a. Lock the surface update: set GRPH_UPDATE_LOCK (bit 16). 19 * b. Clear GRPH_FLIP_CONTROL retrace and immediate bits (flip at next vblank). 20 * c. Write new surface address HIGH then LOW. 21 * d. Unlock: clear GRPH_UPDATE_LOCK. 22 * e. Wait for GRPH_SURFACE_UPDATE_PENDING (bit 2) to clear, 23 * bounded by GPU_VBLANK_MS. 24 * f. Swap front/back pointers. 25 * 26 * Register sources: dce/dce_11_2_d.h MIT (C) 2016 AMD. 27 * All writes are read back and logged. Any surprise stops the driver and 28 * falls back to the CPU-only path (ck_gpu_flip returns AMD_RING_ENODEV). 29 * No floats. All waits TSC-bounded. 30 * 31 * QEMU: QEMU q35 has no DCE 11.2; the GOP framebuffer check will fail and 32 * the function says so once. QEMU runs qualify nothing physical. */ 33 #include "amd_gpu.h" 34 #include "../x86/x86.h" 35 36 /* Minimum VRAM aperture size we trust for Polaris. */ 37 #define VRAM_MIN_BYTES (256ull * 1024u * 1024u) /* 256 MiB */ 38 /* One frame of 4K BGRX = 3840*2160*4 = `33177600` = 0x1FA4000 bytes */ 39 #define FRAME_BYTES_4K (3840u * 2160u * 4u) /* 31.6 MiB */ 40 /* Align back buffer to 4 MiB */ 41 #define BB_ALIGN (4u * 1024u * 1024u) 42 43 int amd_display_init(void) 44 { 45 struct amd_gpu_state *g = &g_amd_gpu; 46 47 if (!g->regs) { 48 ck_puts("gpu: display: no register aperture\n"); 49 return -1; 50 } 51 52 /* Read the surface address the firmware programmed into CRTC 0. */ 53 uint32_t sa_lo = amd_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS); 54 uint32_t sa_hi = amd_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH); 55 uint32_t flip = amd_rd(mmGRPH_FLIP_CONTROL); 56 uint32_t upd = amd_rd(mmGRPH_UPDATE); 57 uint32_t crtc = amd_rd(mmCRTC_STATUS); 58 ck_printf("gpu: display: GRPH_SURFACE_ADDR=0x%08x%08x FLIP=0x%x UPDATE=0x%x CRTC_STATUS=0x%x\n", 59 sa_hi, sa_lo, flip, upd, crtc); 60 61 /* Sanity: the firmware-programmed address must be inside BAR0. */ 62 uint64_t fa = ((uint64_t)sa_hi << 32) | sa_lo; 63 if (fa == 0 || fa == 0xffffffffffffffffull) { 64 ck_puts("gpu: display: GRPH surface address reads all-0 or all-1 (chip not scanning)\n"); 65 return -1; 66 } 67 /* Cross-check with the framebuffer physical address we got from the GOP. */ 68 if (g->fb_pa != 0 && g->fb_pa != fa) { 69 ck_printf("gpu: display: WARNING: GOP fb_pa 0x%llx != GRPH addr 0x%llx (using GRPH)\n", 70 (unsigned long long)g->fb_pa, (unsigned long long)fa); 71 } 72 /* Use the hardware's own address as ground truth. */ 73 if (g->fb_pa == 0) 74 g->fb_pa = fa; 75 76 /* Confirm fb_pa is inside VRAM aperture. */ 77 if (g->vram_pa == 0 || fa < g->vram_pa || fa >= g->vram_pa + VRAM_MIN_BYTES) { 78 ck_printf("gpu: display: fb_pa 0x%llx not inside vram_pa 0x%llx\n", 79 (unsigned long long)fa, (unsigned long long)g->vram_pa); 80 return -1; 81 } 82 83 /* Compute VRAM offset of front buffer and back buffer. */ 84 g->fb_off = (uint32_t)(g->fb_pa - g->vram_pa); 85 86 /* Back buffer: place it after the front buffer, aligned to BB_ALIGN. 87 * Frame size for 3840x2160 BGRX = FRAME_BYTES_4K. 88 * Even if we don't know the exact pitch/height, leave enough room. */ 89 uint32_t fb_sz = FRAME_BYTES_4K; 90 uint32_t bb_off_raw = g->fb_off + fb_sz; 91 /* Round up to BB_ALIGN. */ 92 uint32_t bb_off = (bb_off_raw + BB_ALIGN - 1u) & ~(BB_ALIGN - 1u); 93 uint64_t bb_pa = g->vram_pa + bb_off; 94 95 /* Ensure back buffer fits within VRAM. We need at least another frame. */ 96 if ((uint64_t)bb_off + FRAME_BYTES_4K > g->vram_sz && g->vram_sz != 0) { 97 ck_printf("gpu: display: back buffer at VRAM+0x%x would overflow vram_sz 0x%llx\n", 98 bb_off, (unsigned long long)g->vram_sz); 99 return -1; 100 } 101 102 /* Map the back buffer as UC- so the CPU can write to it. 103 * TODO: map as WC (requires setting PAT entry 4 to WC in CR_PAT MSR) 104 * for the write-combined throughput the front buffer already has. */ 105 if (ck_x86_map_mmio(bb_pa, FRAME_BYTES_4K)) { 106 ck_printf("gpu: display: could not map back buffer at 0x%llx\n", 107 (unsigned long long)bb_pa); 108 return -1; 109 } 110 g->bb_pa = bb_pa; 111 g->bb_off = bb_off; 112 g->bb_cpu = (volatile uint32_t *)(uintptr_t)bb_pa; 113 114 /* Clear the back buffer (paint black). */ 115 volatile uint32_t *p = g->bb_cpu; 116 for (uint32_t i = 0; i < FRAME_BYTES_4K / 4u; i++) 117 p[i] = 0; 118 119 ck_printf("gpu: display: front VRAM+0x%x pa=0x%llx back VRAM+0x%x pa=0x%llx " 120 "back_cpu=0x%llx (UC-)\n", 121 g->fb_off, (unsigned long long)g->fb_pa, 122 bb_off, (unsigned long long)bb_pa, 123 (unsigned long long)(uintptr_t)g->bb_cpu); 124 125 g->flipped = 0; 126 g->steps_done |= AMD_STEP_DISPLAY; 127 ck_puts("gpu: display: ready (CRTC 0, double-buffer vsync flip)\n"); 128 return 0; 129 } 130 131 /* ---- ck_gpu_flip: schedule flip at next vsync -------------------------*/ 132 int ck_gpu_flip(void) 133 { 134 struct amd_gpu_state *g = &g_amd_gpu; 135 136 if (!(g->steps_done & AMD_STEP_DISPLAY)) 137 return AMD_RING_ENODEV; 138 139 /* Determine which buffer to flip to (the back buffer). */ 140 uint64_t new_pa = g->flipped ? g->fb_pa : g->bb_pa; 141 142 /* a. Lock surface update. */ 143 uint32_t upd = amd_rd(mmGRPH_UPDATE) | GRPH_UPDATE_LOCK; 144 amd_wr(mmGRPH_UPDATE, upd); 145 __asm__ volatile("" ::: "memory"); 146 147 /* b. Flip at vblank: clear the retrace and immediate-flip bits (dce_v11_0.c:265-269). */ 148 amd_wr(mmGRPH_FLIP_CONTROL, amd_rd(mmGRPH_FLIP_CONTROL) & ~GRPH_FLIP_MODE_BITS); 149 150 /* c. Write new surface address (HIGH then LOW). */ 151 amd_wr(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH, (uint32_t)(new_pa >> 32)); 152 amd_wr(mmGRPH_PRIMARY_SURFACE_ADDRESS, (uint32_t)(new_pa & 0xffffffffu)); 153 154 /* d. Unlock: hardware latches the address at the next vsync. */ 155 upd &= ~GRPH_UPDATE_LOCK; 156 amd_wr(mmGRPH_UPDATE, upd); 157 __asm__ volatile("" ::: "memory"); 158 159 /* e. Wait for GRPH_SURFACE_UPDATE_PENDING to clear. 160 * The hardware clears this once the flip has been sent to the scan 161 * pipeline. Bounded by GPU_VBLANK_MS (50 ms). */ 162 uint64_t t0 = ck_rdtsc(); 163 uint64_t ticks = g->tsc_hz / 1000ull * GPU_VBLANK_MS; 164 int done = 0; 165 do { 166 uint32_t s = amd_rd(mmGRPH_UPDATE); 167 if (!(s & GRPH_SURFACE_UPDATE_PENDING)) { done = 1; break; } 168 ck_pause(); 169 } while (ck_rdtsc() - t0 < ticks); 170 171 if (!done) { 172 ck_printf("gpu: display: flip pending after %u ms (GRPH_UPDATE=0x%x); using anyway\n", 173 GPU_VBLANK_MS, amd_rd(mmGRPH_UPDATE)); 174 /* Not fatal: the hardware may still complete the flip. */ 175 } 176 177 /* Read back to confirm */ 178 uint32_t got_lo = amd_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS); 179 uint32_t got_hi = amd_rd(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH); 180 uint64_t got = ((uint64_t)got_hi << 32) | got_lo; 181 if (got != new_pa) { 182 ck_printf("gpu: display: flip readback mismatch: wrote 0x%llx got 0x%llx\n", 183 (unsigned long long)new_pa, (unsigned long long)got); 184 } 185 186 /* f. Swap front/back tracking. */ 187 g->flipped ^= 1; 188 return done ? AMD_RING_OK : AMD_RING_ETIMEOUT; 189 } 190 191 /* ---- ck_gpu_backbuffer: return current back buffer pointer ------------*/ 192 volatile uint32_t *ck_gpu_backbuffer(void) 193 { 194 struct amd_gpu_state *g = &g_amd_gpu; 195 if (!(g->steps_done & AMD_STEP_DISPLAY)) 196 return (volatile uint32_t *)0; 197 /* After a flip, the previous front becomes the new back. */ 198 if (!g->flipped) 199 return g->bb_cpu; 200 /* After one flip: back is now the original front buffer. */ 201 return (volatile uint32_t *)(uintptr_t)g->fb_pa; 202 } 203 204 uint64_t ck_gpu_backbuffer_pa(void) 205 { 206 struct amd_gpu_state *g = &g_amd_gpu; 207 if (!(g->steps_done & AMD_STEP_DISPLAY)) return 0ull; 208 return g->flipped ? g->fb_pa : g->bb_pa; 209 } 210 211 uint64_t ck_gpu_vram_size(void) 212 { 213 return g_amd_gpu.vram_sz; 214 }