interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / gpu / gpu_x86.c

180 lines · 7397 bytes Raw Blame History
1/* native/kernel/gpu/gpu_x86.c -- x86 glue: GPU driver entry point for the
2 * 2017 iMac (AIENOS-KERNEL-0 / AIENOS-IMAC-GPU-CORE-1).
3 *
4 * Called from x86/pci_x86.c:ck_x86_display() when a Polaris display chip is
5 * found whose VRAM aperture (BAR0) holds the GOP framebuffer. The PCI BAR5
6 * (register aperture) is expected to already be mapped by panel_x86.c.
7 *
8 * Build: CK_X86_GPU=1 (the default of `make imac-kernel`). Without it, or
9 * when the chip is absent (QEMU), everything here says so once and returns.
10 *
11 * CK_X86_GPU_UCODE=1 additionally embeds the polaris11 CP/RLC/SDMA firmware
12 * blobs (fetched by scripts/fetch_gpu_ucode.sh; never committed). Without
13 * that flag, Steps 4-6 are skipped.
14 *
15 * Steps and log prefixes:
16 * gpu: display: ... -- DCE 11.2 double-buffer flip (amd_display.c)
17 * gpu: mc: ... -- GMC 8 VRAM size + aperture (amd_mc.c)
18 * gpu: smu: ... -- SMU state as Apple left it (amd_smu.c)
19 * gpu: ucode: ... -- RLC + CP firmware load (amd_ucode.c)
20 * gpu: gfx_ring:... -- GFX ring + NOP/fence test (amd_ucode.c)
21 * gpu: sdma: ... -- SDMA0 fill + timed copy (amd_sdma.c)
22 * gpu: compute: ... -- MEC HQD compute queue (amd_compute.c)
23 *
24 * No floats. All waits TSC-bounded. A surprise at any step leaves the step
25 * alone; subsequent steps check their prerequisites and skip gracefully. */
26#include "amd_gpu.h"
27#include "../mm/frames.h"
28#include "../x86/x86.h"
29
30#ifndef CK_X86_GPU
31#define CK_X86_GPU 0
32#endif
33
34/* Global GPU state (all driver files share this via extern in amd_gpu.h). */
35struct amd_gpu_state g_amd_gpu;
36
37/* DMA pool: 4 MiB physically contiguous, below 4 GiB for ring pointers. */
38#define GPU_DMA_PAGES 1024u /* 4 MiB */
39
40/* Polaris device ID range (same as polaris() in panel_x86.c) */
41static int polaris_id(uint16_t device)
42{
43 return (device >= 0x67c0u && device <= 0x67ffu) ||
44 (device >= 0x6980u && device <= 0x699fu);
45}
46
47#if CK_X86_GPU
48
49void ck_x86_gpu_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn,
50 uint16_t vendor, uint16_t device,
51 uint64_t bar0_pa, uint64_t bar5_pa,
52 uint64_t fb_pa, uint32_t fb_w, uint32_t fb_h, uint32_t fb_pitch,
53 struct ck_frames *fr, uint64_t tsc_hz)
54{
55 static int seen;
56 if (seen++) return;
57
58 (void)cfg; (void)bus; (void)dev; (void)fn;
59
60 ck_printf("gpu: start: %04x:%04x bar0=0x%llx bar5=0x%llx fb=0x%llx %ux%u pitch=%u tsc_hz=%llu\n",
61 vendor, device,
62 (unsigned long long)bar0_pa, (unsigned long long)bar5_pa,
63 (unsigned long long)fb_pa, fb_w, fb_h, fb_pitch,
64 (unsigned long long)tsc_hz);
65
66 if (vendor != 0x1002u || !polaris_id(device)) {
67 ck_puts("gpu: start: not a Polaris GPU, skipped\n");
68 return;
69 }
70 if (!bar5_pa) {
71 ck_puts("gpu: start: BAR5 not mapped, skipped\n");
72 return;
73 }
74
75 struct amd_gpu_state *g = &g_amd_gpu;
76 g->tsc_hz = tsc_hz;
77 g->regs = (volatile uint32_t *)(uintptr_t)bar5_pa;
78 g->vram_pa = bar0_pa;
79 g->fb_pa = fb_pa;
80 g->fb_w = fb_w;
81 g->fb_h = fb_h;
82 g->fb_pitch = fb_pitch;
83
84 /* Map BAR0 for back buffer (panels_x86 only maps BAR5). */
85 if (bar0_pa) {
86 /* The GOP framebuffer range is already mapped WC by the EFI stub.
87 * Map the rest of the first 256 MiB of BAR0 as UC- (enough for back
88 * buffer + GART scratch). ck_x86_map_mmio skips if already mapped. */
89 (void)ck_x86_map_mmio(bar0_pa, 256ull * 1024u * 1024u);
90 g->vram_cpu = (volatile uint8_t *)(uintptr_t)bar0_pa;
91 g->vram_sz = 4ull * 1024u * 1024u * 1024u; /* UNMEASURED: assumes 4 GiB (Pro 570/575); a Pro 580 has 8 GiB. amd_mc.c does not override it. */
92 }
93
94 /* DMA pool for the ring buffers, all below AMD_DMA_LIMIT (amd_gpu.h). fr is
95 * NULL when the caller passes no frame allocator; the ring steps then skip. */
96 if (amd_dma_pool_take(g, fr, GPU_DMA_PAGES, AMD_DMA_LIMIT)) {
97 ck_puts("gpu: start: no DMA frames below 4 GiB, ring steps skipped\n");
98 } else {
99 g->dma_va = (uint8_t *)(uintptr_t)g->dma_pa;
100 __builtin_memset(g->dma_va, 0, g->dma_total);
101 ck_printf("gpu: dma_pool: 0x%llx..0x%llx (%u pages)\n",
102 (unsigned long long)g->dma_pa,
103 (unsigned long long)(g->dma_pa + g->dma_total),
104 GPU_DMA_PAGES);
105 }
106
107 /* Step 1: Display (double-buffer page flip). */
108 ck_x86_step("gpu_display");
109 if (amd_display_init() != 0)
110 ck_puts("gpu: display: init failed; CPU-only rendering continues\n");
111
112 /* Step 2: GMC read. */
113 ck_x86_step("gpu_mc");
114 (void)amd_mc_read();
115
116 /* Step 3: SMU state check. */
117 ck_x86_step("gpu_smu");
118 (void)amd_smu_check();
119
120 /* Step 4: Microcode load (if CK_X86_GPU_UCODE=1). */
121 ck_x86_step("gpu_ucode");
122 (void)amd_ucode_load();
123
124 /* Step 5: GFX ring + fence self-test. */
125 ck_x86_step("gpu_gfx");
126 if (amd_gfx_ring_init() == 0)
127 (void)amd_gfx_ring_test();
128
129 /* Step 6: SDMA0. */
130 ck_x86_step("gpu_sdma");
131 if (amd_sdma_init() == 0)
132 amd_sdma_bench();
133
134 /* Step 7: Compute queue. */
135 ck_x86_step("gpu_compute");
136 (void)amd_compute_init();
137
138 g->ready = (g->steps_done & AMD_STEP_DISPLAY) ? 1 : 0;
139 ck_printf("gpu: done: steps=0x%02x (%s%s%s%s%s%s%s) ready=%d\n",
140 g->steps_done,
141 (g->steps_done & AMD_STEP_DISPLAY) ? "display " : "",
142 (g->steps_done & AMD_STEP_MC) ? "mc " : "",
143 (g->steps_done & AMD_STEP_SMU) ? "smu " : "",
144 (g->steps_done & AMD_STEP_UCODE) ? "ucode " : "",
145 (g->steps_done & AMD_STEP_GFX_RING) ? "gfx " : "",
146 (g->steps_done & AMD_STEP_SDMA) ? "sdma " : "",
147 (g->steps_done & AMD_STEP_COMPUTE) ? "compute" : "",
148 g->ready);
149}
150
151#else /* !CK_X86_GPU */
152
153void ck_x86_gpu_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn,
154 uint16_t vendor, uint16_t device,
155 uint64_t bar0_pa, uint64_t bar5_pa,
156 uint64_t fb_pa, uint32_t fb_w, uint32_t fb_h, uint32_t fb_pitch,
157 struct ck_frames *fr, uint64_t tsc_hz)
158{
159 static int said;
160 (void)cfg;(void)bus;(void)dev;(void)fn;(void)bar0_pa;(void)bar5_pa;
161 (void)fb_pa;(void)fb_w;(void)fb_h;(void)fb_pitch;(void)fr;(void)tsc_hz;
162 if (said++) return;
163 if (vendor == 0x1002u && polaris_id(device))
164 ck_printf("gpu: %04x:%04x Polaris found; this build (CK_X86_GPU=0) "
165 "does not drive the GPU\n", vendor, device);
166}
167
168/* Stub implementations of the public API for non-GPU builds */
169int ck_gpu_flip(void) { return AMD_RING_ENODEV; }
170volatile uint32_t *ck_gpu_backbuffer(void) { return (volatile uint32_t *)0; }
171uint64_t ck_gpu_backbuffer_pa(void) { return 0; }
172uint64_t ck_gpu_vram_size(void) { return 0; }
173int ck_gpu_ring_submit(amd_ring_id_t id, const uint32_t *dw, unsigned count) {
174 (void)id;(void)dw;(void)count; return AMD_RING_ENODEV;
175}
176int ck_gpu_fence_wait(const volatile uint32_t *fence_va, uint32_t seq, unsigned timeout_ms) {
177 (void)fence_va;(void)seq;(void)timeout_ms; return AMD_RING_ENODEV;
178}
179
180#endif /* CK_X86_GPU */