git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/device.rs
//! The hardware this kernel runs on, found rather than listed: every
//! device the boot's description names (the device tree on ARM; ACPI and
//! PCI on a PC to come), with its registers and interrupts, and which
//! driver took it.
//!
//! Adding hardware is adding a driver that claims devices by what they are
//! compatible with ([`claim`]); nothing names a board's addresses. A device
//! no driver claims stays listed, so a board brought up shows at once what
//! it has and what is missing (`device` in Settings, `devices:` at boot).
use crate::fdt::DeviceTree;
use crate::sync::SpinLock;
use alloc::string::String;
use alloc::vec::Vec;
/// What a device is for, as the rest of the system sees it.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Class {
Storage,
Network,
Display,
Input,
Sound,
Serial,
Random,
Clock,
Interrupts,
Power,
Other,
}
impl Class {
pub fn name(self) -> &'static str {
match self {
Class::Storage => "storage",
Class::Network => "network",
Class::Display => "display",
Class::Input => "input",
Class::Sound => "sound",
Class::Serial => "serial",
Class::Random => "random",
Class::Clock => "clock",
Class::Interrupts => "interrupts",
Class::Power => "power",
Class::Other => "other",
}
}
}
/// One device, as the description gives it.
#[derive(Clone)]
pub struct Device {
/// The node's name: `virtio_mmio@a000000`.
pub name: String,
/// What it is compatible with, most specific first.
pub compatible: Vec<String>,
/// Register windows: (physical address, size).
pub regs: Vec<(usize, usize)>,
/// Interrupts, as GIC INTIDs.
pub interrupts: Vec<u32>,
/// The driver that took it, and as what.
pub driver: Option<(&'static str, Class)>,
/// Every property of its node, raw, for what the fields above do not
/// cover (a PCI host's `ranges` and `interrupt-map`).
pub properties: Vec<(&'static str, &'static [u8])>,
}
impl Device {
/// A property of its node, raw.
pub fn property(&self, name: &str) -> Option<&'static [u8]> {
self.properties.iter().find(|(n, _)| *n == name).map(|&(_, v)| v)
}
}
impl Device {
pub fn is(&self, compatible: &str) -> bool {
self.compatible.iter().any(|c| c == compatible)
}
/// Its first register window, as the kernel maps it.
pub fn base(&self) -> Option<usize> {
self.regs.first().map(|&(address, _)| crate::mmu::virt(address))
}
}
static DEVICES: SpinLock<Vec<Device>> = SpinLock::new(Vec::new());
/// Where the description is when the loader did not say: QEMU puts it at
/// the start of RAM for a kernel it boots as an ELF.
pub fn locate(passed: usize) -> usize {
if passed != 0 || DeviceTree::at(crate::mm::RAM.0).is_none() {
passed
} else {
crate::mm::RAM.0
}
}
/// Where the device tree [`discover`] read is (physical), or 0.
static TREE: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
/// The device tree the devices came from, if there was one.
pub fn tree() -> Option<DeviceTree> {
DeviceTree::at(TREE.load(core::sync::atomic::Ordering::Relaxed))
}
/// Reads every device from the device tree at `address`; returns how many.
/// Nodes that only group others (the root, `cpus`, `memory`, `chosen`)
/// are not devices.
pub fn discover(address: usize) -> usize {
let Some(tree) = DeviceTree::at(address) else {
return 0;
};
TREE.store(address, core::sync::atomic::Ordering::Relaxed);
let mut found = Vec::new();
tree.nodes(|node| {
if node.depth == 0
|| !node.enabled()
|| node.device_type().is_some_and(|t| t == "memory" || t == "cpu")
{
return;
}
let compatible: Vec<String> = node.compatible().map(String::from).collect();
if compatible.is_empty() {
return;
}
found.push(Device {
name: String::from(node.name),
compatible,
regs: node.regs().map(|(a, s)| (a as usize, s as usize)).collect(),
interrupts: node.interrupts().collect(),
driver: None,
properties: node.properties.clone(),
});
});
// In address order, as a list reads best and virtio's slots count.
found.sort_by_key(|d| d.regs.first().map_or(usize::MAX, |r| r.0));
let count = found.len();
*DEVICES.lock() = found;
count
}
/// The devices compatible with `compatible` that no other driver has taken,
/// for which `accept` says yes: each becomes `driver`'s, as `class`.
/// `accept` may read the device's registers (a virtio window says what is
/// behind it); it runs with the device list locked, so it must not claim.
pub fn claim(
compatible: &str,
driver: &'static str,
class: Class,
mut accept: impl FnMut(&Device) -> bool,
) -> Vec<Device> {
let mut devices = DEVICES.lock();
let mut taken = Vec::new();
for device in devices.iter_mut() {
if device.driver.is_none_or(|(d, _)| d == driver) && device.is(compatible) && accept(device) {
device.driver = Some((driver, class));
taken.push(device.clone());
}
}
taken
}
/// The first device compatible with `compatible`, taken or not: for what
/// the kernel itself runs on (the interrupt controller), read before any
/// driver.
pub fn find(compatible: &str) -> Option<Device> {
DEVICES.lock().iter().find(|d| d.is(compatible)).cloned()
}
/// Adds a device found by other means than the device tree: a PCI
/// function.
pub fn add(device: Device) {
DEVICES.lock().push(device);
}
/// Marks a device the kernel itself drives (`find` gave it).
pub fn mark(name: &str, driver: &'static str, class: Class) {
if let Some(device) = DEVICES.lock().iter_mut().find(|d| d.name == name) {
device.driver.get_or_insert((driver, class));
}
}
/// Marks every device compatible with `compatible` as one the kernel
/// itself drives, as `driver`.
pub fn mark_compatible(compatible: &str, driver: &'static str, class: Class) {
for device in DEVICES.lock().iter_mut().filter(|d| d.is(compatible)) {
device.driver.get_or_insert((driver, class));
}
}
/// Every device, taken or not.
pub fn all() -> Vec<Device> {
DEVICES.lock().clone()
}
/// What closed firmware a device's driver handed it, by the device's
/// name: `Intel, iwlwifi-so-a0-gf-a0-89.ucode`. A driver of Pinnacle's own
/// with no such note runs no closed code on its device.
static FIRMWARE: SpinLock<Vec<(String, String)>> = SpinLock::new(Vec::new());
/// Notes that `name`'s driver uses closed firmware: `vendor` and its file.
pub fn note_firmware(name: &str, vendor: &str, file: &str) {
let mut notes = FIRMWARE.lock();
notes.retain(|(n, _)| n != name);
notes.push((String::from(name), alloc::format!("{vendor}, {file}")));
}
/// The closed firmware noted for `name`, if any.
pub fn firmware(name: &str) -> Option<String> {
FIRMWARE
.lock()
.iter()
.find(|(n, _)| n == name)
.map(|(_, f)| f.clone())
}