git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/drivers/scanout.rs
//! A screen the bootloader lit and left on: memory a display controller
//! already scans out, which Pinnacle draws on without driving the
//! controller, the panel or its power. How a board shows Pinnacle before it
//! has display drivers of its own.
//!
//! Found once at boot, before the page allocator ([`find`]), so that its
//! memory is never handed out: from a `simple-framebuffer` node in the
//! device tree (the standard way a loader passes one), else from the
//! board (on an RK3399, the display controller's first window, as U-Boot
//! set it up). The display server draws in a framebuffer of the kernel's,
//! as everywhere; [`Scanout::copy`] carries each changed rectangle across,
//! in the screen's pixel format and stride, and cleans it out of the cache,
//! which the controller does not see.
use crate::sync::SpinLock;
use core::sync::atomic::{AtomicUsize, Ordering};
/// How the screen's memory holds a pixel.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Format {
/// 32 bits, 0x00RRGGBB in a little-endian word: the kernel's own.
Xrgb8888,
/// 32 bits, red and blue the other way round.
Xbgr8888,
/// 16 bits: five of red, six of green, five of blue.
Rgb565,
}
impl Format {
pub fn bytes(self) -> u32 {
match self {
Format::Xrgb8888 | Format::Xbgr8888 => 4,
Format::Rgb565 => 2,
}
}
/// The `format` of a simple-framebuffer node.
pub fn from_simplefb(name: &str) -> Option<Format> {
match name {
"x8r8g8b8" | "a8r8g8b8" => Some(Format::Xrgb8888),
"x8b8g8r8" | "a8b8g8r8" => Some(Format::Xbgr8888),
"r5g6b5" => Some(Format::Rgb565),
_ => None,
}
}
/// A pixel of the kernel's (0x00RRGGBB) as this format has it.
pub fn encode(self, pixel: u32) -> u32 {
match self {
Format::Xrgb8888 => pixel,
Format::Xbgr8888 => (pixel & 0xff00) | (pixel >> 16 & 0xff) | (pixel & 0xff) << 16,
Format::Rgb565 => (pixel >> 8 & 0xf800) | (pixel >> 5 & 0x07e0) | (pixel >> 3 & 0x001f),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Scanout {
/// Physical address of its first pixel.
pub address: usize,
pub width: u32,
pub height: u32,
/// Bytes from one row to the next.
pub stride: u32,
pub format: Format,
/// Who described it, for the boot log and Settings.
pub source: &'static str,
}
impl Scanout {
/// The physical range its pixels take.
pub fn range(&self) -> (usize, usize) {
(self.address, self.address + (self.stride * self.height) as usize)
}
/// Whether it describes a screen the kernel can draw on: a size it can
/// have a framebuffer of, rows that hold a row, inside RAM (on a PC,
/// inside the direct map: a laptop's firmware screen is in the
/// graphics card's window, often above 4 GiB, never in RAM).
fn sane(&self) -> bool {
let (start, end) = self.range();
#[cfg(target_arch = "x86_64")]
let inside = start < end && end <= crate::mmu::mapped_end();
#[cfg(not(target_arch = "x86_64"))]
let inside = start >= crate::mm::RAM.0 && end <= crate::mm::RAM.1;
super::display::fits(self.width, self.height)
&& self.stride >= self.width * self.format.bytes()
&& inside
}
/// The kernel's address of its first pixel: on a PC the write-combining
/// window [`find`] made, else (or without one) the direct map.
pub fn base(&self) -> usize {
match WINDOW.load(Ordering::Relaxed) {
0 => crate::mmu::virt(self.address),
window => window,
}
}
/// Whether the screen is written through a write-combining window.
#[cfg(target_arch = "x86_64")]
pub fn write_combining(&self) -> bool {
WINDOW.load(Ordering::Relaxed) != 0
}
/// Copies the rectangle at `x`, `y` of `pixels` (the kernel's
/// framebuffer, `self.width` wide) onto the screen, and cleans it to
/// the point of coherency, where the display controller reads (on a
/// PC, drains the write-combining buffers).
pub fn copy(&self, pixels: &[u32], x: i32, y: i32, width: i32, height: i32) {
self.copy_at(self.base(), pixels, x, y, width, height);
}
/// As [`copy`](Self::copy), through the direct map, as before there was
/// a window: uncached on a PC. For the boot log's comparison only.
#[cfg(target_arch = "x86_64")]
pub fn copy_uncached(&self, pixels: &[u32], x: i32, y: i32, width: i32, height: i32) {
self.copy_at(crate::mmu::virt(self.address), pixels, x, y, width, height);
}
fn copy_at(&self, base: usize, pixels: &[u32], x: i32, y: i32, width: i32, height: i32) {
let x0 = x.clamp(0, self.width as i32) as usize;
let y0 = y.clamp(0, self.height as i32) as usize;
let x1 = (x.saturating_add(width)).clamp(0, self.width as i32) as usize;
let y1 = (y.saturating_add(height)).clamp(0, self.height as i32) as usize;
if x0 >= x1 || y0 >= y1 {
return;
}
let bytes = self.format.bytes() as usize;
for row in y0..y1 {
let from = &pixels[row * self.width as usize + x0..row * self.width as usize + x1];
let at = base + row * self.stride as usize + x0 * bytes;
// SAFETY: inside the screen's memory (checked by `sane`), which
// the page allocator never hands out; mapped Normal, or (a PC)
// write-combining in the window, uncached in the direct map.
unsafe {
match self.format.bytes() {
4 => {
let to = core::slice::from_raw_parts_mut(at as *mut u32, from.len());
for (to, &p) in to.iter_mut().zip(from) {
*to = self.format.encode(p);
}
}
_ => {
let to = core::slice::from_raw_parts_mut(at as *mut u16, from.len());
for (to, &p) in to.iter_mut().zip(from) {
*to = self.format.encode(p) as u16;
}
}
}
}
clean(at, from.len() * bytes);
}
#[cfg(target_arch = "aarch64")]
// SAFETY: a barrier; the cleans above complete before what follows.
unsafe {
core::arch::asm!("dsb sy", options(nostack))
};
#[cfg(target_arch = "x86_64")]
crate::mmu::flush_write_combining();
}
}
/// The kernel's address of the screen's first pixel in a write-combining
/// window of its own (a PC), or 0: the direct map, which on a PC is
/// uncached there (the firmware's MTRRs call a graphics card's window so,
/// or the page tables do), and so a word at a time across the bus.
static WINDOW: AtomicUsize = AtomicUsize::new(0);
/// Writes the cache lines over `len` bytes at `at` back to memory, where a
/// device that does not snoop the caches reads them.
fn clean(at: usize, len: usize) {
#[cfg(target_arch = "aarch64")]
{
// The smallest data cache line of every core (CTR_EL0.DminLine).
let ctr: u64;
// SAFETY: reads a register EL1 may read.
unsafe { core::arch::asm!("mrs {}, ctr_el0", out(reg) ctr, options(nostack, nomem)) };
let line = 4usize << (ctr >> 16 & 0xf);
let mut address = at & !(line - 1);
while address < at + len {
// SAFETY: cleaning a line of mapped memory changes no data.
unsafe { core::arch::asm!("dc cvac, {}", in(reg) address, options(nostack)) };
address += line;
}
}
#[cfg(not(target_arch = "aarch64"))]
let _ = (at, len);
}
static FOUND: SpinLock<Option<Scanout>> = SpinLock::new(None);
/// Looks for a lit screen, once, before the page allocator has pages
/// (allocates nothing): the device tree's `simple-framebuffer`, else what
/// the board's display controller is scanning out. Says what it found,
/// or why it took none.
pub fn find(device_tree: usize) {
let tree = crate::fdt::DeviceTree::at(device_tree);
let described = tree.as_ref().and_then(|t| t.simple_framebuffer()).map(|fb| {
(
Format::from_simplefb(fb.format),
Scanout {
address: fb.address as usize,
width: fb.width,
height: fb.height,
stride: fb.stride,
format: Format::Xrgb8888,
source: "simple-framebuffer",
},
fb.format,
)
});
let scanout = match described {
Some((Some(format), scanout, _)) => Some(Scanout { format, ..scanout }),
Some((None, _, name)) => {
crate::println!("display: a simple-framebuffer in format {name}, which is not drawn");
None
}
None => crate::board::lit_screen(),
};
let Some(scanout) = scanout else {
return;
};
if !scanout.sane() {
crate::println!(
"display: the {} at {:#x} ({}x{}, stride {}) is not one to draw on",
scanout.source,
scanout.address,
scanout.width,
scanout.height,
scanout.stride
);
return;
}
// On a PC, a window of its own, write-combining: the direct map is
// uncached there, which a laptop's panel draws a frame through in a
// good part of a second.
#[cfg(target_arch = "x86_64")]
{
let (start, end) = scanout.range();
if let Some(window) = crate::mmu::map_write_combining(start, end - start) {
WINDOW.store(window, Ordering::Relaxed);
}
}
*FOUND.lock() = Some(scanout);
}
/// The lit screen, if [`find`] took one.
pub fn get() -> Option<Scanout> {
*FOUND.lock()
}
/// An RK3399's display controller (VOP), its first window as four of its
/// registers give it: SYS_CTRL (0x08), WIN0_CTRL0 (0x30), WIN0_VIR (0x3c),
/// WIN0_YRGB_MST (0x40) and WIN0_ACT_INFO (0x48), laid out as U-Boot's
/// `vop_rk3288.h` has them (the RK3399's VOPs share the RK3288's). A
/// screen when the window is on, the controller not in standby, and the
/// format one the kernel writes.
pub fn vop_window(sys_ctrl: u32, ctrl0: u32, vir: u32, address: u32, act_info: u32) -> Option<Scanout> {
const STANDBY: u32 = 1 << 22;
const ENABLE: u32 = 1;
const RB_SWAP: u32 = 1 << 12;
if sys_ctrl & STANDBY != 0 || ctrl0 & ENABLE == 0 || address == 0 {
return None;
}
let swapped = ctrl0 & RB_SWAP != 0;
let format = match (ctrl0 >> 1 & 7, swapped) {
(0, false) => Format::Xrgb8888,
(0, true) => Format::Xbgr8888,
(2, false) => Format::Rgb565,
_ => return None,
};
Some(Scanout {
address: address as usize,
width: (act_info & 0x1fff) + 1,
height: (act_info >> 16 & 0x1fff) + 1,
// In 32-bit words, whatever the format.
stride: (vir & 0x3fff) * 4,
format,
source: "rk3399 VOP",
})
}