diff --git a/crates/stela-contracts/src/caches.rs b/crates/stela-contracts/src/caches.rs index 7e5af3c9ff..3025877cea 100644 --- a/crates/stela-contracts/src/caches.rs +++ b/crates/stela-contracts/src/caches.rs @@ -158,6 +158,21 @@ impl Caches { pub fn ccsidr(&self, csselr: u64) -> u64 { u64::from(self.ccsidr[(csselr & SELECTION) as usize]) } + + /// The geometry `selection`'s CCSIDR_EL1 reports, as `do_cacheid` + /// decodes it: line 16 << LineSize, NumSets + 1 sets and + /// Associativity + 1 ways. + pub fn cache(&self, selection: CacheSelection) -> Cache { + let value = self.ccsidr[selection as usize]; + let line = 16 << (value & 7); + let ways = (value >> ASSOCIATIVITY & (MAX_WAYS - 1)) + 1; + let sets = (value >> NUM_SETS & (MAX_SETS - 1)) + 1; + Cache { + bytes: sets * ways * line, + ways, + line, + } + } } #[cfg(test)] @@ -194,6 +209,12 @@ mod tests { assert_eq!(decode(caches.ccsidr(0)), (128, 128, 8)); assert_eq!(decode(caches.ccsidr(1)), (128, 256, 6)); assert_eq!(decode(caches.ccsidr(2)), (128, 8192, 16)); + assert_eq!(caches.cache(CacheSelection::L1Data), cache(128 << 10, 8)); + assert_eq!( + caches.cache(CacheSelection::L1Instruction), + cache(192 << 10, 6) + ); + assert_eq!(caches.cache(CacheSelection::L2Unified), cache(16 << 20, 16)); // TnD is outside the selection; absent caches read 0. assert_eq!(caches.ccsidr(0x10), caches.ccsidr(0)); assert_eq!(caches.ccsidr(3), 0); diff --git a/crates/stela-soc-data/src/board.rs b/crates/stela-soc-data/src/board.rs index 9b6a1d6a6b..36f6effb59 100644 --- a/crates/stela-soc-data/src/board.rs +++ b/crates/stela-soc-data/src/board.rs @@ -40,6 +40,7 @@ use alloc::{collections::BTreeMap, format, string::String, vec::Vec}; use stela_contracts::refuse; use {stela_contracts::adt::Node, stela_contracts::identity::Identity}; +pub mod mpidr; pub mod t6000_pmgr; mod apertures; diff --git a/crates/stela-soc-data/src/board/mpidr.rs b/crates/stela-soc-data/src/board/mpidr.rs new file mode 100644 index 0000000000..af3df2d6f0 --- /dev/null +++ b/crates/stela-soc-data/src/board/mpidr.rs @@ -0,0 +1,24 @@ +//! The MPIDR_EL1 identity of an Apple PE, the same on every SoC. XNU +//! xnu-12377.121.6 apple_arm64_regs.h:102–107 defines MPIDR_CORETYPE at +//! bits 18:16 (E=0, P=1, M=4); Aff1 and Aff0 are the DeviceTree `reg`'s +//! physical cluster and core, so core type never creates another cluster. + +/// The `reg` bits MPIDR_EL1 carries: Aff1 and Aff0. +pub const CLUSTER_CORE: u32 = 0xffff; +pub const CORETYPE_MASK: u8 = 7; +pub const CORETYPE_SHIFT: u32 = 16; +const RES1: u64 = 1 << 31; + +/// The MPIDR_EL1 of core type `coretype` at `reg`, or `None` for an +/// out-of-field core type or undeclared `reg` bits. +pub const fn compose(coretype: u8, reg: u32) -> Option { + if coretype & !CORETYPE_MASK != 0 || reg & !CLUSTER_CORE != 0 { + return None; + } + Some(RES1 | (coretype as u64) << CORETYPE_SHIFT | reg as u64) +} + +/// MPIDR_EL1's core type. +pub const fn coretype(mpidr: u64) -> u8 { + (mpidr >> CORETYPE_SHIFT) as u8 & CORETYPE_MASK +} diff --git a/crates/stela-soc-data/src/board/power_resources/cpu.rs b/crates/stela-soc-data/src/board/power_resources/cpu.rs index f3ac9af648..075deec86b 100644 --- a/crates/stela-soc-data/src/board/power_resources/cpu.rs +++ b/crates/stela-soc-data/src/board/power_resources/cpu.rs @@ -80,8 +80,6 @@ const PERF_DOMAIN_BYTES: usize = 28; const DEVICE_BYTES: usize = 48; const DOMAIN_LIMIT: usize = 48; const VIRTUAL: u8 = 1 << 4; -const PHYSICAL_AFFINITY: u64 = 0xffff; -const CORETYPE_SHIFT: u32 = 16; type Error = CpuAssociationError; @@ -223,10 +221,8 @@ fn topology( } let kind = required(node, "cluster-type")?; let expected_kind = if complex == 0 { b"E\0" } else { b"P\0" }; - let expected_aff2 = complex as u64; if kind != expected_kind - || identity.mpidr & PHYSICAL_AFFINITY != u64::from(reg) - || identity.mpidr != (0x8000_0000 | expected_aff2 << CORETYPE_SHIFT | u64::from(reg)) + || crate::board::mpidr::compose(complex as u8, reg) != Some(identity.mpidr) { return Err(Error::Topology( "CPU profile differs from original affinity", diff --git a/crates/stela-soc-data/src/board/power_resources/cpu/t6000.rs b/crates/stela-soc-data/src/board/power_resources/cpu/t6000.rs index 5684960d43..a50d6f4980 100644 --- a/crates/stela-soc-data/src/board/power_resources/cpu/t6000.rs +++ b/crates/stela-soc-data/src/board/power_resources/cpu/t6000.rs @@ -12,7 +12,6 @@ const T6000_KERNEL_MAX_CLUSTERS: usize = 6; const T6000_KERNEL_MAX_PHYSICAL_CLUSTER: u32 = 10; const AFFINITY_PART_MASK: u32 = 0xff; const CLUSTER_SHIFT: u32 = 8; -const MPIDR_RES1: u64 = 1 << 31; const ENABLE_CORE_SELECTOR: u32 = 0x436f_7265; pub(super) const EVIDENCE: &str = "26A434 mac13j SHA256 5109fca5209bdbb89cec29e3241cc42a90418cb737d65e31962b059b9879af90; mode1 complex2b16e08/core2b190e4/skip2b19918, enable2b2dcb0; cpu/t6000-core-membership-r1 source receipt"; @@ -59,7 +58,7 @@ pub(super) fn resolve( for (pe, (node, identity)) in nodes.iter().zip(processors).enumerate() { let reg = word(required(node, "reg")?)?; let aff1 = (reg >> CLUSTER_SHIFT) as usize; - if u64::from(reg) & !PHYSICAL_AFFINITY != 0 + if reg & !crate::board::mpidr::CLUSTER_CORE != 0 || reg >> CLUSTER_SHIFT > T6000_KERNEL_MAX_PHYSICAL_CLUSTER { return Err(Error::Topology("T6000 physical affinity")); @@ -96,7 +95,7 @@ pub(super) fn resolve( || word(required(node, "die-id")?)? != 0 || word(required(node, "die-cluster-id")?)? as usize != aff1 || word(required(node, "cluster-core-id")?)? != reg & AFFINITY_PART_MASK - || identity.mpidr != (MPIDR_RES1 | (aff2 << CORETYPE_SHIFT) | u64::from(reg)) + || crate::board::mpidr::compose(aff2, reg) != Some(identity.mpidr) { return Err(Error::Topology("T6000 source/profile coordinate mismatch")); } diff --git a/crates/stela-soc-data/src/board/power_resources/cpu/tests.rs b/crates/stela-soc-data/src/board/power_resources/cpu/tests.rs index 7f15e39866..6024909c3d 100644 --- a/crates/stela-soc-data/src/board/power_resources/cpu/tests.rs +++ b/crates/stela-soc-data/src/board/power_resources/cpu/tests.rs @@ -129,7 +129,7 @@ impl Fixture { ]); identities.push(Identity { midr: 0, // MPIDR membership alone is under test. - mpidr: 0x8000_0000 | (complex as u64) << CORETYPE_SHIFT | u64::from(reg), + mpidr: crate::board::mpidr::compose(complex as u8, reg).unwrap(), clock: None, caches: None, }); @@ -449,7 +449,7 @@ fn adt_and_live_profile_affinities_must_agree_for_every_pe() { for mutation in 0..7 { let mut f = Fixture::new(); match mutation { - 0 => f.identities[7].mpidr ^= 1 << CORETYPE_SHIFT, + 0 => f.identities[7].mpidr ^= 1 << crate::board::mpidr::CORETYPE_SHIFT, 1 => f.identities[7].mpidr ^= 1 << 8, 2 => { f.identities.pop(); diff --git a/crates/stela-soc-data/src/board/soc.rs b/crates/stela-soc-data/src/board/soc.rs index 109c50fff1..ea9352146d 100644 --- a/crates/stela-soc-data/src/board/soc.rs +++ b/crates/stela-soc-data/src/board/soc.rs @@ -98,15 +98,7 @@ impl CoreType { /// creates another physical cluster. Returns `None` for an unknown /// mapping, out-of-field core type or undeclared `reg` bits. pub fn mpidr(&self, reg: u32) -> Option { - // XNU xnu-12377.121.6 apple_arm64_regs.h:102–107 defines - // MPIDR_CORETYPE at bits 18:16, E=0, P=1, M=4. - const CORETYPE_MASK: u8 = 7; - const CORETYPE_SHIFT: u32 = 16; - let aff2 = self.mpidr_aff2?; - if aff2 & !CORETYPE_MASK != 0 || reg & !MPIDR_CLUSTER_CORE != 0 { - return None; - } - Some(0x8000_0000 | u64::from(aff2) << CORETYPE_SHIFT | u64::from(reg)) + super::mpidr::compose(self.mpidr_aff2?, reg) } } @@ -417,9 +409,6 @@ pub struct Processor { /// (its template's `reg` 0x1000 to 0x1205; XNU's T6050 /// `MAX_CPU_CLUSTER_PHY_ID` is 18). Each chip's kernel limits bound the /// cluster numbers further. A `reg` beyond Aff1 and Aff0 refuses. -/// The `reg` bits a `/cpus` node may set: Aff1 and Aff0. -const MPIDR_CLUSTER_CORE: u32 = 0xffff; - pub fn cpus(chip_id: u32, soc_name: &str, processors: &[Processor]) -> Result { let rows: Vec<&Soc> = SOCS.iter().filter(|soc| soc.chip_id == chip_id).collect(); if rows.is_empty() { @@ -463,7 +452,7 @@ pub fn cpus(chip_id: u32, soc_name: &str, processors: &[Processor]) -> Result> 16) & 7, - u64::from(match pe.cluster_type { - b'E' => 0u8, + crate::board::mpidr::coretype(*mpidr), + match pe.cluster_type { + b'E' => 0, b'P' => 1, b'M' => 4, _ => unreachable!(), - }) + } ); assert_eq!((*mpidr >> 8) & 0xf, u64::from(pe.reg >> 8)); assert_eq!(*mpidr & 0xff, u64::from(pe.reg & 0xff)); diff --git a/crates/stela-soc-data/src/board/topology/assigned.rs b/crates/stela-soc-data/src/board/topology/assigned.rs index de45436ba1..e7e31975f2 100644 --- a/crates/stela-soc-data/src/board/topology/assigned.rs +++ b/crates/stela-soc-data/src/board/topology/assigned.rs @@ -8,16 +8,13 @@ pub use source::{BspSource, SourceIdentity}; use crate::pmgr::coverage::DomainId; use alloc::{collections::BTreeSet, sync::Arc, vec::Vec}; use { - stela_contracts::Chip, stela_contracts::caches::Cache, stela_contracts::caches::Caches, - stela_contracts::identity::Identity, + stela_contracts::Chip, stela_contracts::caches::Cache, stela_contracts::caches::CacheSelection, + stela_contracts::caches::Caches, stela_contracts::identity::Identity, }; const AFFINITY_MASK: u32 = 0xffff; const AFFINITY_PART_MASK: u32 = 0xff; const CLUSTER_SHIFT: u32 = 8; -const CORETYPE_SHIFT: u32 = 16; -const CORETYPE_MASK: u8 = 7; -const MPIDR_RES1: u64 = 1 << 31; macro_rules! ids { ($($id:ident),* $(,)?) => {$( @@ -540,7 +537,7 @@ impl<'a> Plan<'a> { if kind.cluster_type == 0 || kind.midr == 0 || kind.midr > u64::from(u32::MAX) - || kind.aff2 > CORETYPE_MASK + || kind.aff2 > crate::board::mpidr::CORETYPE_MASK || kind.clock == Some(0) { return Err(conflict(id, ResourceKind::CoreKind)); @@ -603,9 +600,8 @@ impl<'a> Plan<'a> { cluster_type: kind.cluster_type, identity: Identity { midr: kind.midr, - mpidr: MPIDR_RES1 - | u64::from(kind.aff2) << CORETYPE_SHIFT - | u64::from(slot.reg), + mpidr: crate::board::mpidr::compose(kind.aff2, slot.reg) + .ok_or_else(|| conflict(id, ResourceKind::CoreKind))?, clock: kind.clock, caches: Some(caches), }, diff --git a/crates/stela-soc-data/src/board/topology/assigned/source.rs b/crates/stela-soc-data/src/board/topology/assigned/source.rs index 4b8ee30a96..a0eb848ad0 100644 --- a/crates/stela-soc-data/src/board/topology/assigned/source.rs +++ b/crates/stela-soc-data/src/board/topology/assigned/source.rs @@ -406,15 +406,10 @@ impl<'a> BspSource<'a> { let identity = cpus.processors[0]; let (instruction, data) = board::level1_caches(chip, b'E') .ok_or(refuse!("assigned source E cache geometry is unqualified"))?; - let encoded = identity + let geometry = identity .caches .ok_or(refuse!("assigned source lacks complete cache identity"))? - .ccsidr(2); - let geometry = Cache { - line: 16 << (encoded & 7), - ways: ((encoded >> 3) & 0x3ff) as u32 + 1, - bytes: word(node, "l2-cache-size")?, - }; + .cache(CacheSelection::L2Unified); let slot = GuestSlotId(0); let cluster = ClusterId(0); let cache = CacheId(0); diff --git a/crates/stela-soc-data/src/board/topology/assigned/source/t6000.rs b/crates/stela-soc-data/src/board/topology/assigned/source/t6000.rs index c6f9785594..36b62b650f 100644 --- a/crates/stela-soc-data/src/board/topology/assigned/source/t6000.rs +++ b/crates/stela-soc-data/src/board/topology/assigned/source/t6000.rs @@ -136,15 +136,10 @@ impl<'a> BspSource<'a> { let acc = region(node, "acc-impl-reg")?; let cpm = region(node, "cpm-impl-reg")?; let cpu_impl = region(node, "cpu-impl-reg")?; - let encoded = identities.processors[pe] + let cache = identities.processors[pe] .caches .ok_or(refuse!("assigned T6000 cache identity is incomplete"))? - .ccsidr(2); - let cache = Cache { - line: 16 << (encoded & 7), - ways: ((encoded >> 3) & 0x3ff) as u32 + 1, - bytes: word(node, "l2-cache-size")?, - }; + .cache(CacheSelection::L2Unified); let cluster = (cpu.reg >> CLUSTER_SHIFT) as usize; if geometry.len() == cluster { geometry.push((pe, acc, cpm, cpu_impl.bytes, cache)); @@ -227,7 +222,7 @@ impl<'a> BspSource<'a> { id: kind_id, cluster_type: cpu.cluster_type, midr: identity.midr, - aff2: (identity.mpidr >> CORETYPE_SHIFT & u64::from(CORETYPE_MASK)) as u8, + aff2: crate::board::mpidr::coretype(identity.mpidr), clock: identity.clock, instruction, data, diff --git a/crates/stela-soc-data/src/board/topology/assigned/source/t8112.rs b/crates/stela-soc-data/src/board/topology/assigned/source/t8112.rs index f17ef7ece9..8355eb207c 100644 --- a/crates/stela-soc-data/src/board/topology/assigned/source/t8112.rs +++ b/crates/stela-soc-data/src/board/topology/assigned/source/t8112.rs @@ -274,10 +274,10 @@ impl<'a> BspSource<'a> { let kind = processors[pe].cluster_type; let (instruction, data) = board::level1_caches(profile.soc, kind) .ok_or(refuse!("assigned T8112 L1 geometry is unqualified"))?; - let encoded = identity + let unified = identity .caches .ok_or(refuse!("assigned T8112 cache identity is incomplete"))? - .ccsidr(2); + .cache(CacheSelection::L2Unified); let slot = GuestSlotId(0); let cluster = ClusterId(0); let cache = CacheId(0); @@ -305,7 +305,7 @@ impl<'a> BspSource<'a> { id: KindId(0), cluster_type: kind, midr: identity.midr, - aff2: (identity.mpidr >> 16 & 7) as u8, + aff2: crate::board::mpidr::coretype(identity.mpidr), clock: identity.clock, instruction, data, @@ -335,11 +335,7 @@ impl<'a> BspSource<'a> { caches: vec![SharedCache { id: cache, cluster, - geometry: Cache { - line: 16 << (encoded & 7), - ways: ((encoded >> 3) & 0x3ff) as u32 + 1, - bytes: word(node, "l2-cache-size")?, - }, + geometry: unified, }], interrupts: vec![InterruptTarget { id: InterruptId(0),