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/x86/panel_x86.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 / x86 / panel_x86.c` 329 lines ยท 17049 bytes Raw Blame History 1 /* panel_x86.c -- the iMac's built-in display panel switched off for real while 2 * Allen rests, and back on when he wakes (AIENOS-KERNEL-0, x86_64). 3 * 4 * Painting the screen black leaves the backlight lit. This file turns the 5 * panel itself off through the display chip's own panel power sequencer, the 6 * way the chip's firmware does at power-off: the backlight PWM duty first 7 * (BL_PWM_CNTL), then the sequencer's target state to "off" (LVTMA_PWRSEQ_CNTL), 8 * which drops the backlight enable pin (BLON), waits the panel's power-down 9 * delay as the firmware programmed it (LVTMA_PWRSEQ_DELAY1/2) and then the 10 * panel's digital power (DIGON). Waking is the reverse: target state "on" 11 * (DIGON up, the panel's power-up delay, BLON up), then the PWM duty restored, 12 * and only then the caller brings the picture back. 13 * 14 * Only on the chip it was written for: an AMD display controller (PCI vendor 15 * 0x1002, class 0x0300) whose device id is a Polaris part (DCE 11.2, the 16 * 2017 iMac's Radeon Pro 570/575/580 among them), whose VRAM aperture (BAR0) 17 * holds the firmware's framebuffer, and whose 256 KiB register aperture 18 * (BAR5) answers. 19 * The captured iMac inventory has both AMD 1002:67df at 01:00.0 and Intel 20 * 8086:5912, with GOP at 0xc0300000 (evidence/imac_boot0_2026-10-16). 21 * PCI presence alone does not identify the panel owner: pci_x86.c selects 22 * the controller whose BAR0 contains the GOP allocation; recheck it here. 23 * Never use the Intel CPU or the mere presence of its GPU as that proof. 24 * Register indices and bit masks are from Linux 25 * drivers/gpu/drm/amd/include/asic_reg/dce/dce_11_2_d.h and 26 * dce_11_2_sh_mask.h (MIT licensed, Copyright (C) 2016 Advanced Micro Devices, 27 * Inc.), the MMIO byte offset being the index times 4 (amdgpu_device.c, 28 * amdgpu_mm_rreg: registers below the 256 KiB aperture are read directly). The 29 * sequence follows Linux's dce110_hwseq.c (dce110_edp_backlight_control then 30 * dce110_edp_power_control, which the VBIOS command table turns into the same 31 * target-state write) and dce_panel_cntl.c (dce_is_panel_powered_on, 32 * dce_driver_set_backlight: lock BL_PWM_GRP1_REG_LOCK, write the duty, unlock, 33 * wait for the update to land). Linux also keeps the panel off at least 500 ms 34 * (eDP T12) before powering it on again; so does this file. 35 * 36 * Every write is read back and logged; bounded waits require the sequencer's 37 * reported BLON/DIGON state to match, including firmware override mode. 38 * Off returns 0 only after these checks, not merely after painting black. 39 * A failed off remains eligible for a restore attempt, but further off 40 * attempts are disabled. The rest policy in kmain_x86.c uses ACPI S5 when 41 * off cannot be confirmed. These register checks are not optical evidence. 42 * Built with CK_X86_PANEL_OFF=1; disabled or unknown hardware returns -1. 43 * QEMU runs of this file qualify nothing physical. */ 44 #include "../core/ck_internal.h" 45 #include "x86.h" 46 47 #ifndef CK_X86_PANEL_OFF 48 #define CK_X86_PANEL_OFF 0 49 #endif 50 51 /* dce_11_2_d.h: register indices (MMIO byte offset = index * 4) */ 52 #define mmLVTMA_PWRSEQ_CNTL 0x481b 53 #define mmLVTMA_PWRSEQ_STATE 0x481c 54 #define mmLVTMA_PWRSEQ_REF_DIV 0x481d 55 #define mmLVTMA_PWRSEQ_DELAY1 0x481e 56 #define mmLVTMA_PWRSEQ_DELAY2 0x481f 57 #define mmBL_PWM_CNTL 0x4820 58 #define mmBL_PWM_PERIOD_CNTL 0x4822 59 #define mmBL_PWM_GRP1_REG_LOCK 0x4823 60 /* dce_11_2_sh_mask.h: bit masks */ 61 #define PWRSEQ_EN 0x1u /* LVTMA_PWRSEQ_CNTL__LVTMA_PWRSEQ_EN_MASK */ 62 #define PWRSEQ_TARGET_STATE 0x10u /* LVTMA_PWRSEQ_CNTL__LVTMA_PWRSEQ_TARGET_STATE_MASK */ 63 #define PWRSEQ_DIGON 0x10000u /* LVTMA_PWRSEQ_CNTL__LVTMA_DIGON_MASK */ 64 #define PWRSEQ_DIGON_OVRD 0x20000u /* LVTMA_PWRSEQ_CNTL__LVTMA_DIGON_OVRD_MASK */ 65 #define PWRSEQ_BLON 0x1000000u /* LVTMA_PWRSEQ_CNTL__LVTMA_BLON_MASK */ 66 #define PWRSEQ_BLON_OVRD 0x2000000u /* LVTMA_PWRSEQ_CNTL__LVTMA_BLON_OVRD_MASK */ 67 #define STATE_TARGET_R 0x1u /* LVTMA_PWRSEQ_STATE__LVTMA_PWRSEQ_TARGET_STATE_R_MASK */ 68 #define STATE_DIGON 0x2u /* LVTMA_PWRSEQ_STATE__LVTMA_PWRSEQ_DIGON_MASK */ 69 #define STATE_BLON 0x8u /* LVTMA_PWRSEQ_STATE__LVTMA_PWRSEQ_BLON_MASK */ 70 #define STATE_DONE 0x10u /* LVTMA_PWRSEQ_STATE__LVTMA_PWRSEQ_DONE_MASK */ 71 #define BL_DUTY 0xffffu /* BL_PWM_CNTL__BL_ACTIVE_INT_FRAC_CNT_MASK */ 72 #define BL_PWM_EN 0x80000000u /* BL_PWM_CNTL__BL_PWM_EN_MASK */ 73 #define BL_LOCK 0x1u /* BL_PWM_GRP1_REG_LOCK__BL_PWM_GRP1_REG_LOCK_MASK */ 74 #define BL_UPDATE_PENDING 0x100u /* BL_PWM_GRP1_REG_LOCK__BL_PWM_GRP1_REG_UPDATE_PENDING_MASK */ 75 #define BL_IGNORE_MASTER_LOCK 0x80000000u /* BL_PWM_GRP1_REG_LOCK__BL_PWM_GRP1_IGNORE_MASTER_LOCK_EN_MASK */ 76 77 #define REG_BYTES 0x40000u /* the register aperture at BAR5: 256 KiB */ 78 #define VRAM_BAR_BYTES (256ull << 20) /* the smallest VRAM aperture these parts expose */ 79 #define SEQ_MS 3000u /* the sequencer gets this long to reach its target */ 80 #define BL_UPDATE_MS 10u /* the PWM duty update gets this long to land */ 81 #define T_BL_OFF_MS 50u /* override mode: backlight off to panel power off */ 82 #define T_ON_MS 200u /* override mode: panel power on to backlight on */ 83 #define T12_MS 500u /* the panel stays off at least this long (eDP T12) */ 84 85 86 #if CK_X86_PANEL_OFF 87 static struct { 88 volatile uint32_t *regs; /* BAR5, mapped uncached */ 89 uint64_t hz; 90 int ready; /* every guard passed: the panel can be switched */ 91 int ovrd; /* 1: firmware left the override bits set, so they are driven directly */ 92 int off; /* something was written towards "off" (a wake must undo it) */ 93 int broken; /* a readback or a wait failed: no further "off" is attempted */ 94 int blpwm; /* the backlight PWM is on, its duty is taken down and put back */ 95 uint32_t bl0; /* BL_PWM_CNTL as found awake */ 96 uint64_t off_t; /* TSC when the panel went off */ 97 const char *why; /* why the panel is left alone (ready == 0) */ 98 } p; 99 100 static uint32_t cr32(volatile uint8_t *cfg, unsigned off) { return *(volatile uint32_t *)(cfg + off); } 101 static uint32_t rd(unsigned idx) { return p.regs[idx]; } 102 103 /* Writes v, reads back, logs both; -1 (and broken) when the bits in `mask` differ. */ 104 static int wr(const char *when, const char *name, unsigned idx, uint32_t v, uint32_t mask) 105 { 106 uint32_t before = rd(idx); 107 p.regs[idx] = v; 108 uint32_t after = rd(idx); 109 ck_printf("screen: panel %s: %s 0x%08x -> 0x%08x\n", when, name, before, after); 110 if ((after & mask) != (v & mask)) { 111 ck_printf("screen: panel %s: %s did not take the write (wanted 0x%08x); the panel is left alone from here\n", 112 when, name, v); 113 p.broken = 1; 114 return -1; 115 } 116 return 0; 117 } 118 119 static void wait_ms(unsigned ms) 120 { 121 uint64_t t0 = ck_rdtsc(), n = p.hz / 1000 * ms; 122 while (ck_rdtsc() - t0 < n) 123 ck_pause(); 124 } 125 126 /* Waits until (reg & mask) == want, at most ms; -1 (and broken) when it runs out. */ 127 static int wait_reg(const char *when, const char *name, unsigned idx, uint32_t mask, uint32_t want, unsigned ms) 128 { 129 uint64_t t0 = ck_rdtsc(), n = p.hz / 1000 * ms; 130 uint32_t s; 131 do { 132 s = rd(idx); 133 if ((s & mask) == want) { 134 ck_printf("screen: panel %s: %s 0x%08x after %llu ms\n", when, name, s, 135 (unsigned long long)((ck_rdtsc() - t0) * 1000 / p.hz)); 136 return 0; 137 } 138 ck_pause(); 139 } while (ck_rdtsc() - t0 < n); 140 ck_printf("screen: panel %s: %s still 0x%08x after %u ms (wanted 0x%08x under 0x%08x); the panel is left alone " 141 "from here\n", when, name, s, ms, want, mask); 142 p.broken = 1; 143 return -1; 144 } 145 146 /* The backlight PWM duty: lock the group, write, unlock, wait for the update 147 * to land (dce_panel_cntl.c dce_driver_set_backlight), then read back: the 148 * register is double buffered, so the readback counts only once the pending 149 * bit has cleared. */ 150 static int bl_duty(const char *when, uint32_t cntl) 151 { 152 uint32_t before = rd(mmBL_PWM_CNTL), lock = rd(mmBL_PWM_GRP1_REG_LOCK); 153 p.regs[mmBL_PWM_GRP1_REG_LOCK] = lock | BL_IGNORE_MASTER_LOCK | BL_LOCK; 154 p.regs[mmBL_PWM_CNTL] = cntl; 155 p.regs[mmBL_PWM_GRP1_REG_LOCK] = (rd(mmBL_PWM_GRP1_REG_LOCK) | BL_IGNORE_MASTER_LOCK) & ~BL_LOCK; 156 if (wait_reg(when, "BL_PWM_GRP1_REG_LOCK", mmBL_PWM_GRP1_REG_LOCK, BL_UPDATE_PENDING, 0, BL_UPDATE_MS)) 157 return -1; 158 uint32_t after = rd(mmBL_PWM_CNTL); 159 ck_printf("screen: panel %s: BL_PWM_CNTL 0x%08x -> 0x%08x\n", when, before, after); 160 if ((after & (BL_DUTY | BL_PWM_EN)) != (cntl & (BL_DUTY | BL_PWM_EN))) { 161 ck_printf("screen: panel %s: BL_PWM_CNTL did not take the write (wanted 0x%08x); the panel is left alone from " 162 "here\n", when, cntl); 163 p.broken = 1; 164 return -1; 165 } 166 return 0; 167 } 168 169 static int polaris(uint16_t device) 170 { 171 /* Polaris 10 (the iMac18,3: 67df rev c5): 67c0..67df; Polaris 11 and 21: 67e0..67ff; Polaris 12: 6980..699f */ 172 return (device >= 0x67c0 && device <= 0x67ff) || (device >= 0x6980 && device <= 0x699f); 173 } 174 175 void ck_x86_panel_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn, uint16_t vendor, 176 uint16_t device, uint32_t cls, uint64_t fb_pa, uint64_t fb_bytes, uint64_t tsc_hz) 177 { 178 static int seen; 179 if (seen++ || p.ready) 180 return; 181 p.hz = tsc_hz; 182 p.why = "no display chip on the PCI bus owns the framebuffer"; 183 if (!cfg) { 184 ck_printf("screen: panel: %s; reversible panel off unavailable (rest uses ACPI S5)\n", p.why); 185 return; 186 } 187 uint32_t lo0 = cr32(cfg, 0x10), hi0 = (lo0 & 6) == 4 ? cr32(cfg, 0x14) : 0; 188 uint64_t bar0 = ((uint64_t)hi0 << 32 | lo0) & ~0xfull; 189 uint32_t lo5 = cr32(cfg, 0x24); 190 uint64_t bar5 = lo5 & ~0xfu; 191 uint16_t cmd = (uint16_t)cr32(cfg, 4); 192 ck_printf("pci: display %02x:%02x.%u id=%04x:%04x class=%06x bar0=0x%llx bar5=0x%llx mem_decode=%u\n", bus, dev, 193 fn, vendor, device, cls, (unsigned long long)bar0, (unsigned long long)bar5, cmd & 2 ? 1u : 0u); 194 const char *why = 0; 195 if (vendor != 0x1002 || (cls >> 8) != 0x0300) 196 why = "the display chip is not an AMD VGA controller"; 197 else if (!polaris(device)) 198 why = "the AMD chip is not a Polaris part (DCE 11.2), whose panel registers this kernel knows"; 199 else if ((lo0 & 1) || (lo0 & 6) != 4 || !bar0) 200 why = "BAR0 is not a 64-bit memory aperture"; 201 else if (!fb_pa || fb_pa < bar0 || fb_pa + fb_bytes < fb_pa || fb_pa + fb_bytes > bar0 + VRAM_BAR_BYTES) 202 why = "the framebuffer is not inside the chip's VRAM aperture (BAR0)"; 203 else if ((lo5 & 1) || (lo5 & 6) != 0 || !bar5) 204 why = "BAR5 is not a 32-bit memory aperture (the registers)"; 205 else if (!(cmd & 2)) 206 why = "memory decode is off on the display chip"; 207 else if (ck_x86_map_mmio(bar5, REG_BYTES)) 208 why = "the register aperture could not be mapped"; 209 if (why) { 210 p.why = why; 211 ck_printf("screen: panel: %s; reversible panel off unavailable (rest uses ACPI S5)\n", p.why); 212 return; 213 } 214 p.regs = (volatile uint32_t *)(uintptr_t)bar5; 215 uint32_t cntl = rd(mmLVTMA_PWRSEQ_CNTL), state = rd(mmLVTMA_PWRSEQ_STATE), bl = rd(mmBL_PWM_CNTL); 216 ck_printf("screen: panel: DCE 11.2 registers at 0x%llx: PWRSEQ_CNTL=0x%08x PWRSEQ_STATE=0x%08x REF_DIV=0x%08x " 217 "DELAY1=0x%08x DELAY2=0x%08x BL_PWM_CNTL=0x%08x BL_PWM_PERIOD=0x%08x BL_LOCK=0x%08x\n", 218 (unsigned long long)bar5, cntl, state, rd(mmLVTMA_PWRSEQ_REF_DIV), rd(mmLVTMA_PWRSEQ_DELAY1), 219 rd(mmLVTMA_PWRSEQ_DELAY2), bl, rd(mmBL_PWM_PERIOD_CNTL), rd(mmBL_PWM_GRP1_REG_LOCK)); 220 if (cntl == 0xffffffffu || state == 0xffffffffu) { 221 p.why = "the panel registers answer all ones (the chip is not awake, or not this register map)"; 222 } else if ((cntl & PWRSEQ_EN) && (cntl & PWRSEQ_TARGET_STATE) && 223 (state & (STATE_TARGET_R | STATE_DONE | STATE_BLON | STATE_DIGON)) == 224 (STATE_TARGET_R | STATE_DONE | STATE_BLON | STATE_DIGON)) { 225 p.ovrd = 0; 226 p.ready = 1; 227 } else if ((cntl & (PWRSEQ_DIGON | PWRSEQ_DIGON_OVRD | PWRSEQ_BLON | PWRSEQ_BLON_OVRD)) == 228 (PWRSEQ_DIGON | PWRSEQ_DIGON_OVRD | PWRSEQ_BLON | PWRSEQ_BLON_OVRD) && 229 (state & (STATE_BLON | STATE_DIGON)) == (STATE_BLON | STATE_DIGON)) { 230 p.ovrd = 1; /* the firmware drives the pins directly: so will this kernel */ 231 p.ready = 1; 232 } else { 233 p.why = "the panel power sequencer/override state does not confirm an awake panel"; 234 } 235 if (!p.ready) { 236 ck_printf("screen: panel: %s; reversible panel off unavailable (rest uses ACPI S5)\n", p.why); 237 return; 238 } 239 p.blpwm = (bl & BL_PWM_EN) != 0; 240 p.bl0 = bl; 241 ck_printf("screen: panel: ready to switch off while Allen rests (%s; backlight PWM %s)\n", 242 p.ovrd ? "driving the BLON and DIGON pins directly, as the firmware left them" : 243 "through the panel power sequencer, with the delays the firmware set", 244 p.blpwm ? "on, its duty goes to 0 and back" : "not on, left alone"); 245 } 246 247 int ck_x86_panel_ready(void) { return p.ready && !p.broken; } 248 249 int ck_x86_panel_off(void) 250 { 251 if (!p.ready || p.broken || p.off) 252 return -1; 253 p.off = 1; 254 p.off_t = ck_rdtsc(); 255 /* backlight first */ 256 if (p.blpwm && bl_duty("off", p.bl0 & ~BL_DUTY)) 257 return -1; 258 uint32_t cntl = rd(mmLVTMA_PWRSEQ_CNTL); 259 if (!p.ovrd) { 260 if (wr("off", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, cntl & ~PWRSEQ_TARGET_STATE, PWRSEQ_TARGET_STATE)) 261 return -1; 262 /* the sequencer drops BLON, waits the panel's power-down delay, drops DIGON */ 263 if (wait_reg("off", "PWRSEQ_STATE", mmLVTMA_PWRSEQ_STATE, STATE_DONE | STATE_BLON | STATE_DIGON, STATE_DONE, 264 SEQ_MS)) 265 return -1; 266 } else { 267 if (wr("off", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, cntl & ~PWRSEQ_BLON, PWRSEQ_BLON)) 268 return -1; 269 wait_ms(T_BL_OFF_MS); 270 if (wr("off", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, rd(mmLVTMA_PWRSEQ_CNTL) & ~PWRSEQ_DIGON, PWRSEQ_DIGON)) 271 return -1; 272 /* Control readback alone does not establish that the output pins 273 * followed the firmware override. Check the same existing state 274 * register used by the sequencer path; do not invent another path. */ 275 if (wait_reg("off", "PWRSEQ_STATE", mmLVTMA_PWRSEQ_STATE, STATE_BLON | STATE_DIGON, 0, SEQ_MS)) 276 return -1; 277 } 278 ck_puts("screen: panel off (register state confirmed; physical darkness not measured)\n"); 279 return 0; 280 } 281 282 int ck_x86_panel_on(void) 283 { 284 if (!p.ready || !p.off) 285 return -1; 286 /* the panel stays off at least T12 before it is powered again */ 287 uint64_t since = (ck_rdtsc() - p.off_t) * 1000 / p.hz; 288 if (since < T12_MS) 289 wait_ms((unsigned)(T12_MS - since)); 290 int rc = 0; 291 uint32_t cntl = rd(mmLVTMA_PWRSEQ_CNTL); 292 if (!p.ovrd) { 293 rc |= wr("on", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, cntl | PWRSEQ_TARGET_STATE, PWRSEQ_TARGET_STATE); 294 /* the sequencer raises DIGON, waits the panel's power-up delay, raises BLON */ 295 rc |= wait_reg("on", "PWRSEQ_STATE", mmLVTMA_PWRSEQ_STATE, STATE_DONE | STATE_BLON | STATE_DIGON, 296 STATE_DONE | STATE_BLON | STATE_DIGON, SEQ_MS); 297 } else { 298 rc |= wr("on", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, cntl | PWRSEQ_DIGON, PWRSEQ_DIGON); 299 wait_ms(T_ON_MS); 300 rc |= wr("on", "PWRSEQ_CNTL", mmLVTMA_PWRSEQ_CNTL, rd(mmLVTMA_PWRSEQ_CNTL) | PWRSEQ_BLON, PWRSEQ_BLON); 301 rc |= wait_reg("on", "PWRSEQ_STATE", mmLVTMA_PWRSEQ_STATE, STATE_BLON | STATE_DIGON, 302 STATE_BLON | STATE_DIGON, SEQ_MS); 303 } 304 /* then the backlight duty as it was */ 305 if (p.blpwm) 306 rc |= bl_duty("on", p.bl0); 307 if (!rc) 308 p.off = 0; /* a failed restore must not be reported as already awake */ 309 ck_puts(rc ? "screen: panel on not confirmed (see above)\n" : "screen: panel on (register state confirmed)\n"); 310 return rc ? -1 : 0; 311 } 312 #else 313 void ck_x86_panel_start(volatile uint8_t *cfg, unsigned bus, unsigned dev, unsigned fn, uint16_t vendor, 314 uint16_t device, uint32_t cls, uint64_t fb_pa, uint64_t fb_bytes, uint64_t tsc_hz) 315 { 316 static int said; 317 (void)bus, (void)dev, (void)fn, (void)cls, (void)fb_pa, (void)fb_bytes, (void)tsc_hz; 318 if (said++) 319 return; 320 if (cfg) 321 ck_printf("screen: panel: display chip %04x:%04x seen; this build (CK_X86_PANEL_OFF=0) never switches the " 322 "panel; reversible panel off unavailable (rest uses ACPI S5)\n", vendor, device); 323 else 324 ck_puts("screen: panel: no display chip on the PCI bus owns the framebuffer; reversible panel off unavailable (rest uses ACPI S5)\n"); 325 } 326 int ck_x86_panel_ready(void) { return 0; } 327 int ck_x86_panel_off(void) { return -1; } 328 int ck_x86_panel_on(void) { return -1; } 329 #endif