diff --git a/crates/stela-runtime/src/machine/apple.rs b/crates/stela-runtime/src/machine/apple.rs index 744e65522e..a99460d1d9 100644 --- a/crates/stela-runtime/src/machine/apple.rs +++ b/crates/stela-runtime/src/machine/apple.rs @@ -636,8 +636,11 @@ impl AppleDevices { .map(|&name| (name, smc::Key::gpio())) .collect(); let endpoint = Smc::new(sram.clone(), placement.region.0, keys).map_err(String::from)?; - let rtkit = RtKit::new(vec![(smc::ENDPOINT, Box::new(endpoint))], Management::User) + let mut rtkit = RtKit::new(vec![(smc::ENDPOINT, Box::new(endpoint))], Management::User) .map_err(String::from)?; + if placement.quiesced { + rtkit = rtkit.quiesced(); + } let wrapper = Asc::booted( Box::new(rtkit), AscLines::from_interrupts(placement.interrupts.map(|n| AicInput::new(0, n))), @@ -1907,6 +1910,7 @@ mod tests { watchdog: Some((WATCHDOG, 0x14)), region: (REGION, 0x10_0000), gpio_keys: alloc::vec![smc::code(b"gP16")], + quiesced: true, }; let mut devices = AppleDevices::new(); devices diff --git a/crates/stela-runtime/src/machine/soc.rs b/crates/stela-runtime/src/machine/soc.rs index acaa10fc65..56d2170ffe 100644 --- a/crates/stela-runtime/src/machine/soc.rs +++ b/crates/stela-runtime/src/machine/soc.rs @@ -1566,6 +1566,7 @@ pub struct AnsController { /// the window AppleSMCKeysEndpoint accepts the shared page in /// (`_keyEndpointHandler`, 0xfffffe0009da5a20). `gpio_keys` are the keys /// the tree's `pKW4` platform functions write through the nub's `smc-pmu`. +/// `quiesced` is the nub's hand-off state (`rtkit::RtKit::quiesced`). #[derive(Debug, Clone, PartialEq, Eq)] pub struct SmcController { pub wrapper: (u64, u64), @@ -1573,6 +1574,7 @@ pub struct SmcController { pub watchdog: Option<(u64, u64)>, pub region: (u64, u64), pub gpio_keys: Vec, + pub quiesced: bool, } /// A platform function property is the provider's phandle, the function's @@ -1654,6 +1656,7 @@ fn smc_controller( watchdog, region: (base, size), gpio_keys, + quiesced: nub.property("quiesced").is_some(), })) } @@ -3723,6 +3726,7 @@ mod tests { ("compatible", &z(RTBUDDY)), ("region-base", &0x2_3fe0_0000u64.to_le_bytes()), ("region-size", ®ion_size.to_le_bytes()), + ("quiesced", &[]), ], &[pmu], ); @@ -3781,6 +3785,7 @@ mod tests { assert_eq!(placed.watchdog, Some((0x2_3e81_0000, 0x14))); assert_eq!(placed.interrupts, [0x1f4, 0x1f3, 0x1f6, 0x1f5]); assert_eq!(placed.region, (0x2_3fe0_0000, 0x10_0000)); + assert!(placed.quiesced); // Sorted; `keyW` is not a GPIO write. assert_eq!( placed.gpio_keys, diff --git a/crates/stela-runtime/src/platform/apple/rtkit.rs b/crates/stela-runtime/src/platform/apple/rtkit.rs index f26fbc4197..2b484b538d 100644 --- a/crates/stela-runtime/src/platform/apple/rtkit.rs +++ b/crates/stela-runtime/src/platform/apple/rtkit.rs @@ -32,6 +32,19 @@ //! enumeration; any other waits for the coprocessor to announce power-on //! with an unsolicited type 7 0x20 (`_iopValidate` 0x465dfd8). //! +//! A nub marked `quiesced` was used and quiesced by the boot chain: +//! `RTBuddy::_initConfigEDT` (0xfffffe000b665158) starts it in power state +//! 3, and `_performPowerStateChangeGated` (0xfffffe000b66187c) wakes it +//! with `wakeQuiesced` (0xfffffe000b674db8): IOP power 0x220, or AP power +//! 0x20 when `automatic-rail-gating-enabled` is set. A quiesced coprocessor +//! sends no HELLO until that wake. It then says HELLO and rolls call, and it +//! acknowledges 0x220 once enumeration completes. That ack's low byte 0x20 +//! moves the AP to running (`_handlePowerAck` 0xfffffe000b674930), as Linux +//! `apple_rtkit_wake` also expects. An ack sent during enumeration would +//! change the AP's status before the last roll-call group, which +//! `_handleEPRollCall` (0xfffffe000b6746e8) requires at VersionOk. The +//! rail-gated AP-power wake is not modeled and refuses. +//! //! The roll call lists exactly the endpoints the model provides. RTKit's //! system endpoints (crashlog 1, syslog 2, kdebug 3, IOReporting 4, OSLog 8, //! ...) are services of Apple's firmware this coprocessor does not have, so @@ -72,7 +85,9 @@ const DISABLE: u64 = 1; pub const VERSION: u64 = 12; /// The IOP power state of a running coprocessor. pub const POWER_ON: u64 = 0x20; -const IOP_STATES: [u64; 4] = [0x201, 0x220, 0x202, 0]; +const IOP_STATES: [u64; 4] = [0x201, WAKE, 0x202, 0]; +/// The IOP power state that wakes a quiesced coprocessor. +const WAKE: u64 = 0x220; const AP_STATES: [u64; 3] = [0x201, POWER_ON, 0]; /// The first application endpoint. @@ -89,6 +104,8 @@ pub trait Endpoint { #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Stage { + /// Quiesced by the boot chain, waiting for the AP's wake. + Quiesced, /// HELLO sent, waiting for the AP's choice. Hello, /// Roll call of `group` sent, waiting for the AP's answer. @@ -111,6 +128,9 @@ pub struct RtKit { endpoints: BTreeMap, bool)>, management: Management, stage: Stage, + quiesced: bool, + /// The wake state acknowledged when enumeration completes. + wake: Option, iop_power: u64, ap_power: u64, power_nap: bool, @@ -143,12 +163,25 @@ impl RtKit { endpoints: map, management, stage: Stage::Hello, + quiesced: false, + wake: None, iop_power: POWER_ON, ap_power: 0, power_nap: false, }) } + /// The boot chain hands the coprocessor off quiesced (DT `quiesced`). + pub fn quiesced(mut self) -> Self { + self.quiesced = true; + self + } + + fn hello(&mut self, outbox: &mut Vec) { + self.stage = Stage::Hello; + outbox.push(management(HELLO, VERSION | VERSION << 16)); + } + /// Group `group`'s bitmap: the management endpoint and the endpoints the /// model provides. fn bitmap(&self, group: u8) -> u32 { @@ -229,13 +262,22 @@ impl RtKit { Some(next) => self.roll_call(next, outbox), None => { self.stage = Stage::Ready; - if self.management == Management::Ordinary { + if let Some(state) = self.wake.take() { + outbox.push(management(IOP_POWER_ACK, state)); + } else if self.management == Management::Ordinary { outbox.push(management(IOP_POWER_ACK, POWER_ON)); } } } Ok(()) } + (IOP_POWER, Stage::Quiesced) if fields == WAKE => { + self.iop_power = fields; + self.wake = Some(fields); + self.hello(outbox); + Ok(()) + } + (_, Stage::Quiesced) => Err("RTKit quiesced coprocessor takes only the IOP power wake"), (ENDPOINT_CONTROL, Stage::Ready) => self.control(fields, outbox), (IOP_POWER, _) if IOP_STATES.contains(&fields) => { self.iop_power = fields; @@ -262,8 +304,11 @@ impl RtKit { impl Firmware for RtKit { fn start(&mut self, outbox: &mut Vec) -> Result<(), &'static str> { - self.stage = Stage::Hello; - outbox.push(management(HELLO, VERSION | VERSION << 16)); + if self.quiesced { + self.stage = Stage::Quiesced; + } else { + self.hello(outbox); + } Ok(()) } @@ -410,6 +455,35 @@ mod tests { assert!(mgmt(&mut rtkit, 0xf, 0).is_err()); } + #[test] + fn a_quiesced_coprocessor_waits_for_its_wake_and_acks_it_after_enumeration() { + let mut rtkit = RtKit::new( + vec![(0x20, Box::new(Complement) as Box)], + Management::User, + ) + .unwrap() + .quiesced(); + let mut outbox = Vec::new(); + rtkit.start(&mut outbox).unwrap(); + assert!(outbox.is_empty()); + assert!(mgmt(&mut rtkit, AP_POWER, POWER_ON).is_err()); + assert!(mgmt(&mut rtkit, HELLO_REPLY, 12 | 12 << 16).is_err()); + assert_eq!( + mgmt(&mut rtkit, IOP_POWER, WAKE).unwrap(), + vec![management(HELLO, 12 | 12 << 16)] + ); + mgmt(&mut rtkit, HELLO_REPLY, 12 | 12 << 16).unwrap(); + // No ack before the last group is answered. + assert_eq!( + mgmt(&mut rtkit, ROLL_CALL, 1).unwrap(), + vec![management(ROLL_CALL, ROLL_CALL_LAST | 1 << 32 | 1)] + ); + assert_eq!( + mgmt(&mut rtkit, ROLL_CALL, ROLL_CALL_LAST | 1 << 32).unwrap(), + vec![management(IOP_POWER_ACK, WAKE)] + ); + } + #[test] fn endpoints_are_application_endpoints_once_each() { let one = || Box::new(Complement) as Box;