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_mc.c
| 1 | /* native/kernel/gpu/amd_mc.c -- GMC 8.x memory controller read-only probe for |
| 2 | * the 2017 iMac's AMD Radeon Pro 570/575/580 (Polaris 10, PCI 1002:67df). |
| 3 | * |
| 4 | * Reads MC_VM_FB_LOCATION (VRAM start / end in MC address space), the system |
| 5 | * aperture, and the VM context 0 page-table base address -- everything needed |
| 6 | * to set up a GART mapping so CP / SDMA can read system RAM. Nothing is |
| 7 | * written in this step; we only report what the BIOS left. |
| 8 | * |
| 9 | * Sources: include/asic_reg/gmc/gmc_8_1_d.h MIT (C) 2016 AMD. |
| 10 | * Sequence follows gmc_v8_0.c:gmc_v8_0_mc_resume / gmc_v8_0_gart_init. |
| 11 | * No floats. All waits TSC-bounded. */ |
| 12 | #include "amd_gpu.h" |
| 13 | |
| 14 | /* MC_VM_FB_LOCATION packing (gmc_8_1_sh_mask.h): |
| 15 | * bits [15:0] = MC_FB_START = VRAM start >> 24 (in 16-MiB units) |
| 16 | * bits [31:16] = MC_FB_TOP = VRAM end >> 24 (last occupied 16-MiB chunk) |
| 17 | * VRAM start PA = MC_FB_START << 24 |
| 18 | * VRAM size = ((MC_FB_TOP - MC_FB_START + 1) << 24) bytes */ |
| 19 | #define MC_FB_START_MASK 0x0000ffffu |
| 20 | #define MC_FB_TOP_SHIFT 16u |
| 21 | |
| 22 | /* MC_VM_SYSTEM_APERTURE_*_ADDR packing: |
| 23 | * bits [29:0] = address >> 12 (4-KiB pages) */ |
| 24 | #define MC_SYS_ADDR_SHIFT 12u |
| 25 | |
| 26 | int amd_mc_read(void) |
| 27 | { |
| 28 | struct amd_gpu_state *g = &g_amd_gpu; |
| 29 | |
| 30 | if (!g->regs) { |
| 31 | ck_puts("gpu: mc: no register aperture\n"); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | /* Read MC_VM_FB_LOCATION */ |
| 36 | uint32_t fb_loc = amd_rd(mmMC_VM_FB_LOCATION); |
| 37 | uint64_t fb_start = (uint64_t)(fb_loc & MC_FB_START_MASK) << 24; |
| 38 | uint64_t fb_top = (uint64_t)((fb_loc >> MC_FB_TOP_SHIFT) & 0xffffu) << 24; |
| 39 | uint64_t fb_sz = (fb_top >= fb_start) ? (fb_top - fb_start + (1ull << 24)) : 0ull; |
| 40 | |
| 41 | g->mc_fb_loc = fb_loc; |
| 42 | g->mc_fb_start = fb_start; |
| 43 | g->mc_fb_end = fb_top + (1ull << 24) - 1ull; |
| 44 | if (g->vram_sz == 0) |
| 45 | g->vram_sz = fb_sz; |
| 46 | |
| 47 | ck_printf("gpu: mc: MC_VM_FB_LOCATION=0x%08x VRAM start=0x%llx top=0x%llx size=%llu MiB\n", |
| 48 | fb_loc, |
| 49 | (unsigned long long)fb_start, |
| 50 | (unsigned long long)fb_top, |
| 51 | (unsigned long long)(fb_sz >> 20)); |
| 52 | |
| 53 | /* Cross-check: VRAM start should match BAR0 physical address. */ |
| 54 | if (g->vram_pa != 0 && fb_start != g->vram_pa) { |
| 55 | ck_printf("gpu: mc: WARNING: MC_VM_FB_START 0x%llx != BAR0 PA 0x%llx\n", |
| 56 | (unsigned long long)fb_start, (unsigned long long)g->vram_pa); |
| 57 | } |
| 58 | |
| 59 | /* Read system aperture */ |
| 60 | uint32_t sys_lo = amd_rd(mmMC_VM_SYSTEM_APERTURE_LOW_ADDR); |
| 61 | uint32_t sys_hi = amd_rd(mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR); |
| 62 | g->mc_sys_lo = (uint64_t)sys_lo << MC_SYS_ADDR_SHIFT; |
| 63 | g->mc_sys_hi = (uint64_t)sys_hi << MC_SYS_ADDR_SHIFT; |
| 64 | ck_printf("gpu: mc: system aperture lo=0x%llx hi=0x%llx\n", |
| 65 | (unsigned long long)g->mc_sys_lo, |
| 66 | (unsigned long long)g->mc_sys_hi); |
| 67 | |
| 68 | /* Read AGP aperture (the GART window). BIOS programs this to the GART |
| 69 | * range; for now just report. We will program VM_CONTEXT0 after the |
| 70 | * GART table is built. */ |
| 71 | uint32_t agp_top = amd_rd(mmMC_VM_AGP_TOP); |
| 72 | uint32_t agp_bot = amd_rd(mmMC_VM_AGP_BOT); |
| 73 | ck_printf("gpu: mc: AGP_BOT=0x%08x AGP_TOP=0x%08x\n", agp_bot, agp_top); |
| 74 | |
| 75 | /* Read VM context 0 page-table base (what BIOS set; we will override |
| 76 | * once the GFX ring is up). */ |
| 77 | uint32_t ctx0 = amd_rd(mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR); |
| 78 | ck_printf("gpu: mc: VM_CONTEXT0_PAGE_TABLE_BASE=0x%08x (PA=0x%llx)\n", |
| 79 | ctx0, (unsigned long long)((uint64_t)ctx0 << 12)); |
| 80 | |
| 81 | /* Sanity checks */ |
| 82 | if (fb_loc == 0xffffffffu) { |
| 83 | ck_puts("gpu: mc: MC_VM_FB_LOCATION reads all-ones (chip not awake?)\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | if (fb_sz == 0) { |
| 87 | ck_puts("gpu: mc: VRAM size from FB_LOCATION is zero; not continuing\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | g->steps_done |= AMD_STEP_MC; |
| 92 | ck_printf("gpu: mc: done (VRAM %llu MiB, system aperture 0x%llx-0x%llx)\n", |
| 93 | (unsigned long long)(g->vram_sz >> 20), |
| 94 | (unsigned long long)g->mc_sys_lo, |
| 95 | (unsigned long long)g->mc_sys_hi); |
| 96 | return 0; |
| 97 | } |