Skip to content

Commit

Permalink
Introduce AffinityShift for safer affinity level readings from an Mpi…
Browse files Browse the repository at this point in the history
…drValue
  • Loading branch information
NathanRoyer committed Mar 31, 2023
1 parent 21b819b commit 5c0d9cc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions kernel/cpu/src/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,28 @@ pub fn current_cpu() -> CpuId {
#[repr(transparent)]
pub struct MpidrValue(u64);

/// Affinity Levels and corresponding bit ranges
///
/// The associated integers are the locations (N..(N+8))
/// of the corresponding bits in an [`MpidrValue`].
#[derive(Copy, Clone, Debug)]
#[repr(u64)]
pub enum AffinityShift {
LevelZero = 0,
LevelOne = 8,
LevelTwo = 16,
LevelThree = 32,
}

impl MpidrValue {
/// Returns the inner raw value read from the `MPIDR_EL1` register.
pub fn value(self) -> u64 {
self.0
}

/// Reads an affinity `level` from this `MpidrValue`.
///
/// Panics if the given affinity level is not 0, 1, 2, or 3.
pub fn affinity(self, level: u8) -> u8 {
let shift = match level {
0 => 0,
1 => 8,
2 => 16,
3 => 32,
_ => panic!("Valid affinity levels are 0, 1, 2, 3"),
};
(self.0 >> shift) as u8
pub fn affinity(self, level: AffinityShift) -> u64 {
(self.0 >> (level as u64)) & (u8::MAX as u64)
}

/// Create an `MpidrValue` from its four affinity numbers
Expand Down

0 comments on commit 5c0d9cc

Please sign in to comment.