git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/drivers/i915.rs
//! Intel's integrated graphics, Skylake (Gen9) to Alder Lake (Gen12, Xe),
//! as far as Pinnacle goes so far: the panel's backlight. The screen stays
//! in the mode the firmware (UEFI's GOP) set, drawn through `scanout`; there
//! is no modesetting and no 3D (a project of its own).
//!
//! The function is the one at 00:02.0 (vendor 8086, display class); its
//! first window, BAR0 (GTTMMADR), holds the registers. An embedded panel's
//! backlight on these machines is a PWM in the south display engine, in
//! the chipset (the PCH), which the firmware leaves on at the level it
//! chose. Two layouts, as Linux's `intel_backlight.c` drives them:
//!
//! - Sunrise Point and Kaby Point (Skylake, Kaby Lake; Comet Lake V):
//! BLC_PWM_PCH_CTL1 at 0xC8250 (bit 31 on, bit 29 inverted) and
//! BLC_PWM_PCH_CTL2 at 0xC8254, the period in its high 16 bits and the
//! duty cycle in its low 16 (Linux's `spt_` functions).
//! - Cannon Point on (Coffee and Comet Lake, Ice Lake, Tiger Lake, Jasper
//! Lake, Alder and Raptor Lake): two controllers, each its control at
//! 0xC8250 + 0x100·n (bits as above), its period at +4 and its duty
//! cycle at +8, 32 bits each (Linux's `cnp_` functions; which one the
//! panel uses the video BIOS table says, which Pinnacle does not read:
//! the one the firmware left on).
//!
//! Which chipset it is the ISA bridge at 00:1f.0 says, by its device ID
//! (Linux's `intel_pch.c`, the ID masked with 0xff80). Any other (an Atom's
//! Gemini Lake, which has no PCH; Meteor Lake on) is refused, and so is a
//! PWM the firmware left off: many panels (OLED among them) take their
//! level over the DisplayPort AUX channel instead, which needs a driver
//! for the display engine. `backlight` then asks ACPI.
use crate::pci::Function;
/// Which layout the chipset's backlight PWM has.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Layout {
/// One controller; period and duty cycle in one register, 16 bits each.
Spt,
/// Controller `n`; period and duty cycle in registers of their own.
Cnp(usize),
}
/// The PWM's registers, as offsets into BAR0.
const PCH_CTL1: usize = 0xc8250;
const PCH_CTL2: usize = 0xc8254;
const PWM_ENABLE: u32 = 1 << 31;
const PWM_INVERTED: u32 = 1 << 29;
/// The chipsets whose backlight PWM is laid out as Sunrise Point's
/// (Skylake's), by the ISA bridge's device ID masked with 0xff80: SPT,
/// SPT-LP, KBP, CMP-V.
const SPT: &[u16] = &[0xa100, 0x9d00, 0xa280, 0xa380];
/// Those laid out as Cannon Point's: CNP, CNP-LP, CMP, CMP2, ICP, ICP2,
/// MCC, TGP, TGP2, JSP, ADP, ADP2, ADP3, ADP4.
const CNP: &[u16] = &[
0xa300, 0x9d80, 0x0280, 0x0680, 0x3480, 0x3880, 0x4b00, 0xa080, 0x4380, 0x4d80, 0x7a80, 0x5180, 0x7a00,
0x5480,
];
/// The layout a chipset's ISA bridge (its device ID) says, if Pinnacle
/// knows it.
fn layout_of(bridge: u16) -> Option<Layout> {
let id = bridge & 0xff80;
if SPT.contains(&id) {
Some(Layout::Spt)
} else if CNP.contains(&id) {
Some(Layout::Cnp(0))
} else {
None
}
}
/// The panel's backlight, through the chipset's PWM.
pub struct Backlight {
/// BAR0, as the kernel maps it (uncached).
regs: usize,
layout: Layout,
}
impl Backlight {
fn read(&self, offset: usize) -> u32 {
// SAFETY: a register inside BAR0 (16 MiB on every generation here).
unsafe { ((self.regs + offset) as *const u32).read_volatile() }
}
fn write(&self, offset: usize, value: u32) {
// SAFETY: as in `read`.
unsafe { ((self.regs + offset) as *mut u32).write_volatile(value) }
}
fn control(&self) -> u32 {
match self.layout {
Layout::Spt => self.read(PCH_CTL1),
Layout::Cnp(n) => self.read(PCH_CTL1 + 0x100 * n),
}
}
/// The period and the duty cycle as the registers hold them.
fn pwm(&self) -> (u32, u32) {
match self.layout {
Layout::Spt => {
let ctl2 = self.read(PCH_CTL2);
(ctl2 >> 16, ctl2 & 0xffff)
}
Layout::Cnp(n) => (
self.read(PCH_CTL1 + 0x100 * n + 4),
self.read(PCH_CTL1 + 0x100 * n + 8),
),
}
}
/// Whether the firmware left the PWM on, with a period.
fn on(&self) -> bool {
self.control() & PWM_ENABLE != 0 && self.pwm().0 != 0
}
/// The level, 0 to 100, as the PWM is now.
pub fn level(&self) -> u8 {
let (period, duty) = self.pwm();
let duty = if self.control() & PWM_INVERTED != 0 {
period.saturating_sub(duty)
} else {
duty
};
level_of(period, duty)
}
/// Sets the level to `percent`, at least [`MIN_LEVEL`], and returns it.
pub fn set(&self, percent: u8) -> u8 {
let (period, _) = self.pwm();
let mut duty = duty_of(period, percent);
if self.control() & PWM_INVERTED != 0 {
duty = period - duty;
}
match self.layout {
Layout::Spt => self.write(PCH_CTL2, period << 16 | duty),
Layout::Cnp(n) => self.write(PCH_CTL1 + 0x100 * n + 8, duty),
}
self.level()
}
}
/// The least level a panel is set to: 0 would leave the screen black,
/// with nothing on it to turn it back up by.
pub const MIN_LEVEL: u8 = 5;
/// A duty cycle as a level, 0 to 100, rounded.
fn level_of(period: u32, duty: u32) -> u8 {
if period == 0 {
return 0;
}
((u64::from(duty.min(period)) * 100 + u64::from(period) / 2) / u64::from(period)) as u8
}
/// The duty cycle for `percent` (at least [`MIN_LEVEL`], at most 100) of
/// `period`, never 0.
fn duty_of(period: u32, percent: u8) -> u32 {
let percent = u64::from(percent.clamp(MIN_LEVEL, 100));
((u64::from(period) * percent).div_ceil(100) as u32).clamp(1.min(period), period)
}
/// The integrated graphics, if there is one: claimed, its registers'
/// window on, and its backlight if the chipset is one Pinnacle knows and
/// the firmware left the PWM on. Says on the boot log what it found.
pub fn init() -> Option<Backlight> {
let gpu = crate::pci::find_class(0x030000)
.into_iter()
.chain(crate::pci::find_class(0x038000))
.find(|f| f.vendor == 0x8086 && (f.bus, f.device, f.function) == (0, 2, 0))?;
let bridge = crate::pci::find_class(0x060100)
.into_iter()
.find(|f| f.vendor == 0x8086 && (f.bus, f.device) == (0, 0x1f));
crate::pci::claim(&gpu, "intel-graphics", crate::device::Class::Display);
let Some(regs) = gpu.bar_address(0, 0) else {
crate::println!(
"i915: Intel graphics {:04x}, without its registers' window",
gpu.id
);
return None;
};
memory_on(&gpu);
let Some(layout) = bridge.as_ref().and_then(|b| layout_of(b.id)) else {
crate::println!(
"i915: Intel graphics {:04x}, chipset {}: its backlight not one Pinnacle knows",
gpu.id,
bridge.map_or(alloc::string::String::from("unknown"), |b| alloc::format!(
"{:04x}", b.id
))
);
return None;
};
// The controller the firmware left on: the panel's.
let candidates: &[Layout] = match layout {
Layout::Spt => &[Layout::Spt],
Layout::Cnp(_) => &[Layout::Cnp(0), Layout::Cnp(1)],
};
let found = candidates
.iter()
.map(|&layout| Backlight { regs, layout })
.find(Backlight::on);
let Some(backlight) = found else {
crate::println!(
"i915: Intel graphics {:04x}: the backlight's PWM is off (a panel set over DisplayPort AUX?)",
gpu.id
);
return None;
};
let (period, duty) = backlight.pwm();
crate::println!(
"i915: Intel graphics {:04x}, backlight by the chipset's PWM ({:?}, period {period}, duty {duty}): {} %",
gpu.id,
backlight.layout,
backlight.level()
);
Some(backlight)
}
/// Its memory decoding on (the firmware's GOP has left it so; a firmware
/// that did not would have no screen).
fn memory_on(f: &Function) {
const MEMORY: u16 = 1 << 1;
let command = f.read16(0x04);
if command & MEMORY == 0 {
f.write16(0x04, command | MEMORY);
}
}