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_ucode.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_ucode.c` 334 lines ยท 13024 bytes Raw Blame History 1 /* native/kernel/gpu/amd_ucode.c -- RLC and CP microcode load for the 2017 iMac's 2 * AMD Radeon Pro 570/575/580 (Polaris 10, PCI 1002:67df, GFX 8.0). 3 * 4 * Steps: 5 * 1. Soft-reset CP and RLC via GRBM_SOFT_RESET (gfx_v8_0.c:gfx_v8_0_cp_reset). 6 * 2. Load RLC firmware: write index 0 to mmRLC_GPM_UCODE_ADDR, stream dwords to 7 * mmRLC_GPM_UCODE_DATA, enable via mmRLC_CNTL. 8 * 3. Load CP PFP, ME, CE, MEC1, MEC2 firmware via their respective 9 * UCODE_ADDR / UCODE_DATA registers. 10 * 4. Configure GFX ring 0 (mmCP_RB0_BASE, CP_RB0_CNTL, CP_RB0_RPTR_ADDR). 11 * 5. Start the CP; wait for CP_STAT.PFP_BUSY to deassert (bounded). 12 * 6. Submit a NOP + WRITE_DATA fence packet; poll the fence slot in memory 13 * to confirm the CP is executing. 14 * 15 * Microcode blobs are fetched at build time by scripts/fetch_gpu_ucode.sh and 16 * embedded as C arrays in target/native-kernel/gpu_ucode.inc. The build 17 * computes and verifies sha256 against the pinned digests in 18 * scripts/gpu_ucode_sha256.txt before embedding. Blobs are never committed. 19 * Source: linux-firmware amdgpu/polaris11_*.bin (AMD redistributable licence). 20 * KNOWN MISMATCH (L213): the iMac chip (1002:67df) is Polaris 10, and Linux v6.12 loads 21 * amdgpu/polaris10_*.bin for it (gfx_v8_0.c:967-968, MODULE_FIRMWARE at gfx_v8_0.c:134-144). 22 * These pinned polaris11 blobs have not been proven to run on it; see ~/handoffs/fixer-imac-gpu-id.md. 23 * 24 * If the include is absent (CK_X86_GPU_UCODE=0, default for now), the step 25 * logs "not available" and skips to Step 5, leaving the CP unfueled. In that 26 * mode the GFX ring and fence test are skipped and amd_sdma_init / amd_compute_init 27 * will not run. 28 * 29 * Sources (MIT): drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c, gca/gfx_8_0_d.h. 30 * No floats. All waits TSC-bounded. QEMU qualifies nothing physical. */ 31 #include "amd_gpu.h" 32 33 /* PM4 packet words (gfx_v8_0.c:891-892 and 4165; PACKET3/PACKET2 layout as 34 * amdgpu_ring.h: type in bits 31:30, opcode 15:8, body count-1 in 29:16). */ 35 #define PM4_PACKET3(op, n) (0xc0000000u | (((uint32_t)(op) & 0xffu) << 8) | (((uint32_t)(n) & 0x3fffu) << 16)) 36 #define PM4_FILLER2 0x80000000u /* type-2 filler: one dword, no body */ 37 #define PM4_IT_WRITE_DATA 0x37u 38 #define PM4_DST_SEL_MEM 5u /* WRITE_DATA_DST_SEL(5): memory */ 39 #define PM4_WRITE_DATA_DST_SEL(v) ((uint32_t)(v) << 8) 40 #define PM4_WR_CONFIRM (1u << 20) 41 42 #ifndef CK_X86_GPU_UCODE 43 #define CK_X86_GPU_UCODE 0 44 #endif 45 46 #if CK_X86_GPU_UCODE 47 /* Embedded firmware arrays. The build generates this file from the blobs. 48 * Found through the -I target/native-kernel that the Makefile adds for 49 * CK_X86_GPU_UCODE=1 (a relative ../ path here would land outside the repo). */ 50 #include "gpu_ucode.inc" 51 #else 52 /* Stubs: the step will log its absence and skip */ 53 static const uint32_t g_rlc_ucode[] __attribute__((unused)) = {0}; 54 static const uint32_t g_pfp_ucode[] __attribute__((unused)) = {0}; 55 static const uint32_t g_me_ucode[] __attribute__((unused)) = {0}; 56 static const uint32_t g_ce_ucode[] __attribute__((unused)) = {0}; 57 static const uint32_t g_mec_ucode[] __attribute__((unused)) = {0}; 58 static const uint32_t g_mec2_ucode[] __attribute__((unused)) = {0}; 59 #define RLC_UCODE_SIZE 0u 60 #define PFP_UCODE_SIZE 0u 61 #define ME_UCODE_SIZE 0u 62 #define CE_UCODE_SIZE 0u 63 #define MEC_UCODE_SIZE 0u 64 #define MEC2_UCODE_SIZE 0u 65 #endif 66 67 /* Ring buffer is 4096 dwords = 16 KiB. Allocated from DMA pool. */ 68 #define GFX_RING_DW AMD_RING_DWORDS /* 4096 */ 69 70 /* CP_RB0_CNTL: RB_BUFSZ = log2(ring bytes / 8), RB_BLKSZ = RB_BUFSZ - 2 (gfx_v8_0.c:4248-4250). 71 * 4096 dwords = 16 KiB = 2^11 qwords: BUFSZ = 11. */ 72 #define GFX_RB_BUFSZ 11u /* 8 << 11 = 16 KiB = 4096 dwords */ 73 74 /* Soft-reset CP and RLC, wait for reset to land, then clear. */ 75 static int __attribute__((unused)) gfx_soft_reset(void) 76 { 77 amd_wr(mmGRBM_SOFT_RESET, 78 GRBM_SOFT_RESET_CP | GRBM_SOFT_RESET_GFX | GRBM_SOFT_RESET_RLC); 79 (void)amd_rd(mmGRBM_SOFT_RESET); /* flush */ 80 amd_wait_ms(1); 81 amd_wr(mmGRBM_SOFT_RESET, 0); 82 (void)amd_rd(mmGRBM_SOFT_RESET); 83 amd_wait_ms(2); 84 return 0; 85 } 86 87 /* Load a block of dwords into an upload register pair (addr=0 clears address). */ 88 static void __attribute__((unused)) ucode_stream(unsigned addr_idx, unsigned data_idx, 89 const uint32_t *dw, unsigned count) 90 { 91 amd_wr(addr_idx, 0); 92 for (unsigned i = 0; i < count; i++) 93 amd_wr(data_idx, dw[i]); 94 } 95 96 int amd_ucode_load(void) 97 { 98 struct amd_gpu_state *g = &g_amd_gpu; 99 100 if (!g->regs) { 101 ck_puts("gpu: ucode: no register aperture\n"); 102 return -1; 103 } 104 105 #if !CK_X86_GPU_UCODE 106 ck_puts("gpu: ucode: CK_X86_GPU_UCODE=0; skipping firmware load " 107 "(build with scripts/fetch_gpu_ucode.sh first)\n"); 108 /* Allow display / MC / SMU steps that are already done; just don't mark 109 * UCODE done so ring init will be skipped. */ 110 return 0; 111 #else 112 ck_puts("gpu: ucode: loading RLC + CP (PFP/ME/CE/MEC1/MEC2) firmware\n"); 113 114 /* 1. Soft-reset */ 115 gfx_soft_reset(); 116 ck_puts("gpu: ucode: soft-reset done\n"); 117 118 /* 2. Load RLC */ 119 if (RLC_UCODE_SIZE) { 120 /* Disable RLC before upload (gfx_v8_0.c:gfx_v8_0_rlc_fini) */ 121 amd_wr(mmRLC_CNTL, 0); 122 ucode_stream(mmRLC_GPM_UCODE_ADDR, mmRLC_GPM_UCODE_DATA, g_rlc_ucode, RLC_UCODE_SIZE); 123 ck_printf("gpu: ucode: RLC loaded (%u dwords)\n", RLC_UCODE_SIZE); 124 } 125 126 /* 3. Load CP: PFP then ME then CE */ 127 if (PFP_UCODE_SIZE) { 128 ucode_stream(mmCP_PFP_UCODE_ADDR, mmCP_PFP_UCODE_DATA, g_pfp_ucode, PFP_UCODE_SIZE); 129 ck_printf("gpu: ucode: PFP loaded (%u dwords)\n", PFP_UCODE_SIZE); 130 } 131 if (ME_UCODE_SIZE) { 132 ucode_stream(mmCP_ME_RAM_WADDR, mmCP_ME_RAM_DATA, g_me_ucode, ME_UCODE_SIZE); 133 ck_printf("gpu: ucode: ME loaded (%u dwords)\n", ME_UCODE_SIZE); 134 } 135 if (CE_UCODE_SIZE) { 136 ucode_stream(mmCP_CE_UCODE_ADDR, mmCP_CE_UCODE_DATA, g_ce_ucode, CE_UCODE_SIZE); 137 ck_printf("gpu: ucode: CE loaded (%u dwords)\n", CE_UCODE_SIZE); 138 } 139 if (MEC_UCODE_SIZE) { 140 ucode_stream(mmCP_MEC_ME1_UCODE_ADDR, mmCP_MEC_ME1_UCODE_DATA, g_mec_ucode, MEC_UCODE_SIZE); 141 ck_printf("gpu: ucode: MEC1 loaded (%u dwords)\n", MEC_UCODE_SIZE); 142 } 143 if (MEC2_UCODE_SIZE) { 144 ucode_stream(mmCP_MEC_ME2_UCODE_ADDR, mmCP_MEC_ME2_UCODE_DATA, g_mec2_ucode, MEC2_UCODE_SIZE); 145 ck_printf("gpu: ucode: MEC2 loaded (%u dwords)\n", MEC2_UCODE_SIZE); 146 } 147 148 /* Enable RLC */ 149 amd_wr(mmRLC_CNTL, RLC_ENABLE); 150 amd_wait_ms(2); 151 152 g->steps_done |= AMD_STEP_UCODE; 153 ck_puts("gpu: ucode: done\n"); 154 return 0; 155 #endif /* CK_X86_GPU_UCODE */ 156 } 157 158 /* ---- GFX ring initialisation ------------------------------------------*/ 159 int amd_gfx_ring_init(void) 160 { 161 struct amd_gpu_state *g = &g_amd_gpu; 162 163 if (!g->regs) return -1; 164 if (!(g->steps_done & AMD_STEP_UCODE)) { 165 ck_puts("gpu: gfx_ring: skipped (no firmware)\n"); 166 return 0; 167 } 168 169 /* Allocate ring dwords, rptr slot, and fence slot from DMA pool. */ 170 uint64_t ring_pa; void *ring_va; 171 uint64_t rptr_pa; void *rptr_va; 172 uint64_t fence_pa; void *fence_va; 173 if (amd_dma_alloc(GFX_RING_DW * 4u, &ring_pa, &ring_va) || 174 amd_dma_alloc(4u, &rptr_pa, &rptr_va) || 175 amd_dma_alloc(4u, &fence_pa, &fence_va)) { 176 ck_puts("gpu: gfx_ring: out of DMA memory\n"); 177 return -1; 178 } 179 180 struct amd_one_ring *r = &g->ring_gfx; 181 r->ring = (volatile uint32_t *)ring_va; 182 r->ring_pa = ring_pa; 183 r->ring_dw = GFX_RING_DW; 184 r->wptr = 0; 185 r->rptr_va = (volatile uint32_t *)rptr_va; 186 r->rptr_pa = rptr_pa; 187 r->fence_va = (volatile uint32_t *)fence_va; 188 r->fence_pa = fence_pa; 189 r->fence_seq = 0; 190 *(r->rptr_va) = 0; 191 *(r->fence_va) = 0; 192 193 /* Program ring into CP_RB0. Sequence from gfx_v8_0.c:gfx_v8_0_cp_gfx_resume. */ 194 /* Write rptr report address (hardware writes RPTR here on retire). */ 195 amd_wr(mmCP_RB0_RPTR_ADDR, (uint32_t)(rptr_pa & 0xffffffffu)); 196 amd_wr(mmCP_RB0_RPTR_ADDR_HI, (uint32_t)(rptr_pa >> 32)); 197 198 /* Set ring buffer size (BUFSZ=11: 4096 dwords, BLKSZ=BUFSZ-2) and NO_UPDATE=0. */ 199 amd_wr(mmCP_RB0_CNTL, CP_RB_BUFSZ(GFX_RB_BUFSZ) | CP_RB_BLKSZ(GFX_RB_BUFSZ - 2u)); 200 201 /* Set ring base address. */ 202 amd_wr(mmCP_RB0_BASE, (uint32_t)(ring_pa >> 8)); /* base in 256-byte units */ 203 amd_wr(mmCP_RB0_BASE_HI, (uint32_t)(ring_pa >> 40)); 204 205 /* Reset write pointer. */ 206 amd_wr(mmCP_RB0_WPTR, 0); 207 208 /* Start the CP: clear ME_HALT. */ 209 uint32_t cp_cntl = amd_rd(mmCP_RB0_CNTL); 210 amd_wr(mmCP_RB0_CNTL, cp_cntl & ~(1u << 28)); /* clear CP_RB0_CNTL__RB_NO_UPDATE_MASK */ 211 212 /* Enable RLC */ 213 amd_wr(mmRLC_CNTL, RLC_ENABLE); 214 215 /* Wait for CP to be ready (CP_STAT.PFP_BUSY = 0). */ 216 uint64_t t0 = ck_rdtsc(), ticks = g->tsc_hz / 1000ull * GPU_RING_WAIT_MS; 217 uint32_t stat; 218 int cp_up = 0; 219 do { 220 stat = amd_rd(mmCP_STAT); 221 if (!(stat & CP_STAT_PFP_BUSY)) { cp_up = 1; break; } 222 amd_wait_us(100); 223 } while (ck_rdtsc() - t0 < ticks); 224 225 ck_printf("gpu: gfx_ring: CP_STAT=0x%08x (%s) ring_pa=0x%llx ring_dw=%u\n", 226 stat, cp_up ? "idle" : "BUSY after timeout", 227 (unsigned long long)ring_pa, GFX_RING_DW); 228 229 if (!cp_up) { 230 ck_puts("gpu: gfx_ring: CP did not come up; GFX ring not ready\n"); 231 return -1; 232 } 233 234 r->ready = 1; 235 g->steps_done |= AMD_STEP_GFX_RING; 236 ck_puts("gpu: gfx_ring: ready\n"); 237 return 0; 238 } 239 240 /* ---- Ring submit / fence (implements amd_ring.h public API) -----------*/ 241 242 int ck_gpu_ring_submit(amd_ring_id_t id, const uint32_t *dw, unsigned count) 243 { 244 struct amd_gpu_state *g = &g_amd_gpu; 245 struct amd_one_ring *r; 246 247 switch (id) { 248 case AMD_RING_GFX: r = &g->ring_gfx; break; 249 case AMD_RING_SDMA: r = &g->ring_sdma; break; 250 case AMD_RING_MEC: r = &g->ring_mec; break; 251 default: return AMD_RING_ENODEV; 252 } 253 if (!r->ready) return AMD_RING_ENODEV; 254 if (count == 0 || count > 256) return AMD_RING_EFULL; 255 256 /* Check for space: need count dwords; ring is full if wptr would lap rptr. */ 257 uint32_t free = r->ring_dw - ((r->wptr - *r->rptr_va) & (r->ring_dw - 1u)); 258 if (count > free) return AMD_RING_EFULL; 259 260 /* Write dwords into ring. */ 261 for (unsigned i = 0; i < count; i++) { 262 r->ring[r->wptr & (r->ring_dw - 1u)] = dw[i]; 263 r->wptr++; 264 } 265 __asm__ volatile("" ::: "memory"); 266 #if defined(__x86_64__) 267 __asm__ volatile("sfence" ::: "memory"); /* WC/UC write ordering (kernel build, x86-64) */ 268 #else 269 __atomic_thread_fence(__ATOMIC_RELEASE); /* host test on other hosts */ 270 #endif 271 272 /* Advance write pointer (hardware polls WPTR). */ 273 if (id == AMD_RING_GFX) 274 amd_wr(mmCP_RB0_WPTR, r->wptr & (r->ring_dw - 1u)); 275 else if (id == AMD_RING_SDMA) 276 amd_wr(mmSDMA0_GFX_RB_WPTR, r->wptr * 4u); /* SDMA uses byte offset */ 277 278 return AMD_RING_OK; 279 } 280 281 int ck_gpu_fence_wait(const volatile uint32_t *fence_va, uint32_t seq, unsigned timeout_ms) 282 { 283 uint64_t t0 = ck_rdtsc(); 284 uint64_t ticks = g_amd_gpu.tsc_hz / 1000ull * (timeout_ms ? timeout_ms : 1u); 285 do { 286 if (*fence_va == seq) return AMD_RING_OK; 287 ck_pause(); 288 } while (ck_rdtsc() - t0 < ticks); 289 return AMD_RING_ETIMEOUT; 290 } 291 292 /* Submit a NOP + WRITE_DATA fence and wait for it to land. 293 * This is the "GFX ring self-test" described in the task brief. */ 294 int amd_gfx_ring_test(void) 295 { 296 struct amd_gpu_state *g = &g_amd_gpu; 297 struct amd_one_ring *r = &g->ring_gfx; 298 299 if (!r->ready) { 300 ck_puts("gpu: gfx_test: ring not ready; skipped\n"); 301 return 0; 302 } 303 304 uint32_t seq = ++r->fence_seq; 305 *r->fence_va = 0; /* clear */ 306 307 /* Packet stream, as gfx_v8_0_ring_test_ib emits it (gfx_v8_0.c:891-892): 308 * a one-dword PACKET2 filler (0x80000000, the padding gfx_v8_0.c:4165 309 * writes), then PACKET3(WRITE_DATA, 3): header, info (DST_SEL=5 memory, 310 * WR_CONFIRM), address low, address high, data. Six dwords in all. */ 311 uint32_t pkt[6]; 312 pkt[0] = PM4_FILLER2; 313 pkt[1] = PM4_PACKET3(PM4_IT_WRITE_DATA, 3); 314 pkt[2] = PM4_WRITE_DATA_DST_SEL(PM4_DST_SEL_MEM) | PM4_WR_CONFIRM; 315 pkt[3] = (uint32_t)(r->fence_pa & 0xffffffffu); 316 pkt[4] = (uint32_t)(r->fence_pa >> 32); 317 pkt[5] = seq; 318 319 int rc = ck_gpu_ring_submit(AMD_RING_GFX, pkt, 6); 320 if (rc != AMD_RING_OK) { 321 ck_printf("gpu: gfx_test: submit failed rc=%d\n", rc); 322 return rc; 323 } 324 325 rc = ck_gpu_fence_wait(r->fence_va, seq, GPU_RING_WAIT_MS); 326 if (rc != AMD_RING_OK) { 327 ck_printf("gpu: gfx_test: fence timeout after %u ms (fence_va=0x%08x wanted %u)\n", 328 GPU_RING_WAIT_MS, *r->fence_va, seq); 329 return rc; 330 } 331 ck_printf("gpu: gfx_test: PASS (fence seq=%u landed at 0x%llx)\n", 332 seq, (unsigned long long)r->fence_pa); 333 return AMD_RING_OK; 334 }