diff --git a/crates/stela-boot/src/engine/remapping/amd.rs b/crates/stela-boot/src/engine/remapping/amd.rs index d11300bc00..413be58ca1 100644 --- a/crates/stela-boot/src/engine/remapping/amd.rs +++ b/crates/stela-boot/src/engine/remapping/amd.rs @@ -311,11 +311,26 @@ struct Cold { reserved: Vec<(u16, Range)>, } -/// Stop a cold unit the policy does not take. False when it did not stop. +/// Stop a cold unit the policy does not take and give back its function's +/// configuration page and its register mapping: a unit dropped with them +/// keeps both claimed, and the check that reopens the PCI bridges then +/// cannot map the configuration space. False when it did not stop or a +/// mapping did not retire. fn retire(mut unit: Unit) -> bool { // SAFETY: no requester was attached and the unit blocks nothing it // translated; retiring stops it. - unsafe { unit.retire() }.is_ok() + if unsafe { unit.retire() }.is_err() { + return false; + } + let Some(mut inputs) = unit.into_inputs() else { + return false; + }; + let mut retired = true; + for mapping in &mut inputs.mappings { + // SAFETY: the stopped unit was the mapping's only user. + retired &= unsafe { mapping.retire() }.is_ok(); + } + retired & inputs.function.config().retire().is_ok() } /// Take every unit of `table` (an IVRS) the policy allows, each in the diff --git a/crates/stela-boot/src/engine/remapping/preparation.rs b/crates/stela-boot/src/engine/remapping/preparation.rs index b9148b0cb0..f44bf7662e 100644 --- a/crates/stela-boot/src/engine/remapping/preparation.rs +++ b/crates/stela-boot/src/engine/remapping/preparation.rs @@ -188,11 +188,13 @@ impl Remappers { .is_some_and(|original| core::ptr::eq(original, source)); // SAFETY: this startup step precedes every driver and competing PCI // writer. The same complete blocked remappers and line owners are kept. + // A unit kept in any mode holds its own function's configuration + // page, which the whole-space view cannot map beside it. let stopped = unsafe { - if amd { - self.recheck_amd_stopped(pci) - } else { + if self.amd.is_empty() { pci.recheck_stopped() + } else { + self.recheck_amd_stopped(pci) } }; if let Err(error) = stopped { diff --git a/crates/stela-driver-base/src/bus/dma/amd/unit/native.rs b/crates/stela-driver-base/src/bus/dma/amd/unit/native.rs index edde52b351..96aaf11ed1 100644 --- a/crates/stela-driver-base/src/bus/dma/amd/unit/native.rs +++ b/crates/stela-driver-base/src/bus/dma/amd/unit/native.rs @@ -785,6 +785,25 @@ impl Unit { self.engine.retired.set(true); Ok(()) } + + /// After a successful [`Self::retire`]: the unit's function and register + /// mappings, for the caller to retire with their original owner. Left + /// inside a dropped unit they stay claimed, and nothing else can map + /// the configuration space they lie in. `None`, with the unit dropped + /// as before, when it was not retired or its engine is still shared. + pub fn into_inputs(self) -> Option> { + let Self { engine, table, .. } = self; + drop(table); + let engine = Rc::try_unwrap(engine).ok()?; + if !engine.retired.get() { + return None; + } + let mut engine = ManuallyDrop::new(engine); + // SAFETY: the sole, retired engine; its `Drop` never runs, so the + // resources are taken exactly once. + let Resources { inspection, .. } = unsafe { ManuallyDrop::take(&mut engine.resources) }; + Some(inspection.into_inputs()) + } } #[cfg(test)] diff --git a/crates/stela-driver-base/src/bus/dma/amd/unit/native/tests.rs b/crates/stela-driver-base/src/bus/dma/amd/unit/native/tests.rs index 7abaf7b4cd..377e4fa8ab 100644 --- a/crates/stela-driver-base/src/bus/dma/amd/unit/native/tests.rs +++ b/crates/stela-driver-base/src/bus/dma/amd/unit/native/tests.rs @@ -380,6 +380,21 @@ fn a_stopped_unit_with_firmware_leftovers_comes_up() { assert_ne!(peer.register(DEVICE_TABLE), 0x7654_3000 | 0x1ff); } +/// A retired unit hands its function and register mappings back for their +/// owner to retire; one that still runs keeps them. +#[test] +fn a_retired_unit_gives_its_mappings_back() { + let (mut unit, peer) = model::unit(); + // SAFETY: the peer has no requester, producer or other register user. + unsafe { unit.retire() }.unwrap(); + let inputs = unit.into_inputs().expect("a retired unit's inputs"); + assert!(!inputs.mappings.is_empty()); + assert_eq!(peer.live_ram(), 0); + + let (unit, _peer) = model::unit(); + assert!(unit.into_inputs().is_none()); +} + fn unit_with_entries(entries: &[u8]) -> (Unit, Peer) { let (inspection, platform, peer, bytes) = inputs_with_entries(entries); let ivrs = Ivrs::parse(&bytes, 8).unwrap();