ast10x0: make SCU unlock nesting-safe and re-lock after boot unlock_and_lock_after now tracks a nesting depth so only the outermost call unlocks and re-locks the SCU. An inner call no longer re-locks the SCU under an enclosing closure. Now that the SCU is re-locked, writes that relied on it staying unlocked would be silently dropped, so: - gate/ungate_clock_mask, assert/deassert_reset_mask, apply_pinctrl_group and dcache_invd_all take the lock themselves. - The I3C reset/clock writes and the SGPIOM pin enable in spim_wiring go through ScuRegisters instead of raw PAC writes. - HACE only holds the SCU unlocked for its clock and reset writes. - pre_init re-locks the SCU after init_cache. Fix the unlock_and_lock_after documentation and the stale "call unlock_write_protection()" comments. Validated on the AST1060 EVB: 25/25 non-manual hardware tests pass (rebased on main). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
diff --git a/target/ast10x0/board/src/spim_wiring.rs b/target/ast10x0/board/src/spim_wiring.rs index 051c860..df206e6 100644 --- a/target/ast10x0/board/src/spim_wiring.rs +++ b/target/ast10x0/board/src/spim_wiring.rs
@@ -96,6 +96,24 @@ } } +/// Route the SGPIOM clock, load, data-out and data-in signals to their pins. +fn enable_sgpiom_pins() { + // SAFETY: SCU access is coordinated by the board layer. + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| { + scu.regs().scu41c().modify(|_, w| { + w.enbl_sgpiomaster_ckfn_pin() + .set_bit() + .enbl_sgpiomaster_ldfn_pin() + .set_bit() + .enbl_sgpiomaster_dofn_pin() + .set_bit() + .enbl_sgpiomaster_difn_pin() + .set_bit() + }); + }); +} + /// Apply the default SCU pinctrl group for a SPI monitor instance. /// /// The instance numbering follows the hardware SPIPF blocks: `Spim0` uses @@ -126,17 +144,7 @@ }; let gpio = unsafe { &*device::Gpio::ptr() }; - let scu = unsafe { &*device::Scu::ptr() }; - scu.scu41c().modify(|_, w| { - w.enbl_sgpiomaster_ckfn_pin() - .set_bit() - .enbl_sgpiomaster_ldfn_pin() - .set_bit() - .enbl_sgpiomaster_dofn_pin() - .set_bit() - .enbl_sgpiomaster_difn_pin() - .set_bit() - }); + enable_sgpiom_pins(); match gpio_group { ExternalMuxGpioGroup::Abcd => { gpio.gpio000() @@ -290,17 +298,7 @@ const BMC_SRST_MASK: u32 = 1 << 8; const BMC_EXTRST_MASK: u32 = 1 << 9; - let scu = unsafe { &*device::Scu::ptr() }; - scu.scu41c().modify(|_, w| { - w.enbl_sgpiomaster_ckfn_pin() - .set_bit() - .enbl_sgpiomaster_ldfn_pin() - .set_bit() - .enbl_sgpiomaster_dofn_pin() - .set_bit() - .enbl_sgpiomaster_difn_pin() - .set_bit() - }); + enable_sgpiom_pins(); let sgpio = unsafe { &*device::Sgpiom::ptr() }; sgpio.gpio554().modify(|_, w| unsafe {
diff --git a/target/ast10x0/entry.rs b/target/ast10x0/entry.rs index a668bce..38a2ba9 100644 --- a/target/ast10x0/entry.rs +++ b/target/ast10x0/entry.rs
@@ -85,6 +85,10 @@ // SAFETY: SCU is unlocked above; pre-init has exclusive ownership. unsafe { init_cache() }; + + // Re-lock the SCU. Later users unlock it only for the duration of their + // writes, via `ScuRegisters::unlock_and_lock_after`. + scu.scu000().write(|w| unsafe { w.bits(0) }); } #[unsafe(no_mangle)]
diff --git a/target/ast10x0/peripherals/hace/device.rs b/target/ast10x0/peripherals/hace/device.rs index 846d378..44539b4 100644 --- a/target/ast10x0/peripherals/hace/device.rs +++ b/target/ast10x0/peripherals/hace/device.rs
@@ -119,34 +119,34 @@ // this, the AHB bus hangs on the first HACE register read when the ROM // bootloader (UART/fwspick mode) leaves HACE in reset. scu.deassert_reset_mask(ScuRegisterHalf::Lower, 1 << 4); + }); - // SAFETY: Caller coordinates singleton access. - let regs = unsafe { HaceRegisters::new_global() }; + // SAFETY: Caller coordinates singleton access. + let regs = unsafe { HaceRegisters::new_global() }; - // Drain any in-progress hash operation left by the bootloader. - // On real hardware the engine may be busy when firmware starts; the - // HACE W1C clear of `hash_intflag` is ignored while `HashEngStsFlag` - // (bit 0) is set, causing the first poll to return a stale flag and the - // digest buffer to read back as the IV (never written by the engine). - // We stop the engine, then spin until it goes idle, then clear the - // stale flag before handing the device to callers. - regs.stop_hash_operation(); - while regs.hash_engine_is_busy() { - core::hint::spin_loop(); - } - regs.clear_hash_intflag(); + // Drain any in-progress hash operation left by the bootloader. + // On real hardware the engine may be busy when firmware starts; the + // HACE W1C clear of `hash_intflag` is ignored while `HashEngStsFlag` + // (bit 0) is set, causing the first poll to return a stale flag and the + // digest buffer to read back as the IV (never written by the engine). + // We stop the engine, then spin until it goes idle, then clear the + // stale flag before handing the device to callers. + regs.stop_hash_operation(); + while regs.hash_engine_is_busy() { + core::hint::spin_loop(); + } + regs.clear_hash_intflag(); - Self { - regs, - cache_flush: scu_dcache_invd_all, - // SAFETY: the `unsafe fn new*` single-instance contract makes this - // the sole live device, hence the sole holder of these pointers. - ctx: unsafe { acquire_shared_ctx() }, - crypto_ctx: unsafe { acquire_crypto_ctx() }, - yield_fn, - poll_budget: DEFAULT_POLL_BUDGET, - } - }) + Self { + regs, + cache_flush: scu_dcache_invd_all, + // SAFETY: the `unsafe fn new*` single-instance contract makes this + // the sole live device, hence the sole holder of these pointers. + ctx: unsafe { acquire_shared_ctx() }, + crypto_ctx: unsafe { acquire_crypto_ctx() }, + yield_fn, + poll_budget: DEFAULT_POLL_BUDGET, + } } /// Create a device bound to the singleton HACE instance.
diff --git a/target/ast10x0/peripherals/i3c/registers.rs b/target/ast10x0/peripherals/i3c/registers.rs index f891387..d3ef076 100644 --- a/target/ast10x0/peripherals/i3c/registers.rs +++ b/target/ast10x0/peripherals/i3c/registers.rs
@@ -18,6 +18,7 @@ use core::marker::PhantomData; use super::constants::MAX_BUSES; +use crate::scu::ScuRegisters; // ----------------------------------------------------------------------------- // Per-bus register dispatch (private) @@ -194,57 +195,61 @@ // SCU: per-bus reset and clock // ------------------------------------------------------------------------- + /// Run `f` on the SCU register block with write protection lifted. + /// + /// SCU reset and clock registers are write-protected. The lock is taken + /// only around `f` (see [`ScuRegisters::unlock_and_lock_after`]), so writes + /// made outside it are silently dropped. + #[inline] + fn with_scu_unlocked<R>(&self, f: impl FnOnce(&ast1060_pac::scu::RegisterBlock) -> R) -> R { + // SAFETY: the SCU is the global singleton; this façade's contract + // already serializes SCU access with the owning thread. + let scu = unsafe { ScuRegisters::new_global() }; + scu.unlock_and_lock_after(|scu| f(scu.regs())) + } + /// SCU050: assert this bus's controller reset. pub(crate) fn core_reset_assert(&self) { - match self.bus { - 0 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c0ctrl().set_bit()), - 1 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c1ctrl().set_bit()), - 2 => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c2ctrl().set_bit()), - _ => self - .scu() - .scu050() - .modify(|_, w| w.rst_i3c3ctrl().set_bit()), - }; + self.with_scu_unlocked(|scu| match self.bus { + 0 => scu.scu050().modify(|_, w| w.rst_i3c0ctrl().set_bit()), + 1 => scu.scu050().modify(|_, w| w.rst_i3c1ctrl().set_bit()), + 2 => scu.scu050().modify(|_, w| w.rst_i3c2ctrl().set_bit()), + _ => scu.scu050().modify(|_, w| w.rst_i3c3ctrl().set_bit()), + }); } /// SCU054: deassert this bus's controller reset (write-1-to-clear). pub(crate) fn core_reset_deassert(&self) { let mask = 1u32 << (8 + u32::from(self.bus)); - self.scu() - .scu054() - .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu054() + .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + }); } /// SCU050: assert the shared I3C register/DMA reset. #[allow(dead_code)] pub(crate) fn global_reset_assert(&self) { - self.scu() - .scu050() - .modify(|_, w| w.rst_i3cregdmactrl().set_bit()); + self.with_scu_unlocked(|scu| { + scu.scu050().modify(|_, w| w.rst_i3cregdmactrl().set_bit()); + }); } /// SCU054: deassert the shared I3C register/DMA reset (write-1-to-clear). pub(crate) fn global_reset_deassert(&self, mask: u32) { - self.scu() - .scu054() - .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu054() + .modify(|_, w| unsafe { w.scu050sys_rst_ctrl_clear_reg2().bits(mask) }); + }); } /// SCU094: ungate this bus's clock (write-1-to-clear stop bit). pub(crate) fn clock_on(&self) { let mask = 1u32 << (8 + u32::from(self.bus)); - self.scu() - .scu094() - .modify(|_, w| unsafe { w.scu090clk_stop_ctrl_clear_reg_set2().bits(mask) }); + self.with_scu_unlocked(|scu| { + scu.scu094() + .modify(|_, w| unsafe { w.scu090clk_stop_ctrl_clear_reg_set2().bits(mask) }); + }); } /// SCU004: hardware revision ID.
diff --git a/target/ast10x0/peripherals/scu/cache.rs b/target/ast10x0/peripherals/scu/cache.rs index 0e8a20f..19bebb3 100644 --- a/target/ast10x0/peripherals/scu/cache.rs +++ b/target/ast10x0/peripherals/scu/cache.rs
@@ -18,14 +18,16 @@ #[inline] pub fn dcache_invd_all(&self) { const DCACHE_CLEAN: u32 = 1 << 1; - // SAFETY: SCU MMIO read-modify-write; DSB only orders memory accesses. - unsafe { - let ctrl = self.regs().scua58().read().bits(); - self.regs().scua58().write(|w| w.bits(ctrl & !DCACHE_CLEAN)); - core::arch::asm!("dsb sy", options(nostack, preserves_flags)); - self.regs().scua58().write(|w| w.bits(ctrl | DCACHE_CLEAN)); - core::arch::asm!("dsb sy", options(nostack, preserves_flags)); - } + self.unlock_and_lock_after(|scu| { + // SAFETY: SCU MMIO read-modify-write; DSB only orders memory accesses. + unsafe { + let ctrl = scu.regs().scua58().read().bits(); + scu.regs().scua58().write(|w| w.bits(ctrl & !DCACHE_CLEAN)); + core::arch::asm!("dsb sy", options(nostack, preserves_flags)); + scu.regs().scua58().write(|w| w.bits(ctrl | DCACHE_CLEAN)); + core::arch::asm!("dsb sy", options(nostack, preserves_flags)); + } + }); } }
diff --git a/target/ast10x0/peripherals/scu/clock.rs b/target/ast10x0/peripherals/scu/clock.rs index e258112..4867933 100644 --- a/target/ast10x0/peripherals/scu/clock.rs +++ b/target/ast10x0/peripherals/scu/clock.rs
@@ -9,30 +9,32 @@ impl ScuRegisters { /// Gate clocks by setting bits in the selected clock-stop register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn gate_clock_mask(&self, half: ClockRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ClockRegisterHalf::Lower => { - self.regs().scu080().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu080().write(|w| unsafe { w.bits(mask) }); } ClockRegisterHalf::Upper => { - self.regs().scu090().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu090().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Ungate clocks by clearing bits in the selected clock-stop register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn ungate_clock_mask(&self, half: ClockRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ClockRegisterHalf::Lower => { - self.regs().scu084().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu084().write(|w| unsafe { w.bits(mask) }); } ClockRegisterHalf::Upper => { - self.regs().scu094().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu094().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Read the selected clock-stop register half.
diff --git a/target/ast10x0/peripherals/scu/pinctrl.rs b/target/ast10x0/peripherals/scu/pinctrl.rs index ac64470..7f554fd 100644 --- a/target/ast10x0/peripherals/scu/pinctrl.rs +++ b/target/ast10x0/peripherals/scu/pinctrl.rs
@@ -1104,26 +1104,28 @@ /// # } /// ``` pub fn apply_pinctrl_group(&self, pins: &[PinctrlPin]) { - let regs = self.regs(); - for pin in pins { - match pin.offset { - 0x410 => modify_reg!(regs.scu410(), pin.bit, pin.clear), - 0x414 => modify_reg!(regs.scu414(), pin.bit, pin.clear), - 0x418 => modify_reg!(regs.scu418(), pin.bit, pin.clear), - 0x41C => modify_reg!(regs.scu41c(), pin.bit, pin.clear), - 0x430 => modify_reg!(regs.scu430(), pin.bit, pin.clear), - 0x434 => modify_reg!(regs.scu434(), pin.bit, pin.clear), - 0x4B0 => modify_reg!(regs.scu4b0(), pin.bit, pin.clear), - 0x4B4 => modify_reg!(regs.scu4b4(), pin.bit, pin.clear), - 0x4B8 => modify_reg!(regs.scu4b8(), pin.bit, pin.clear), - 0x4BC => modify_reg!(regs.scu4bc(), pin.bit, pin.clear), - 0x690 => modify_reg!(regs.scu690(), pin.bit, pin.clear), - 0x694 => modify_reg!(regs.scu694(), pin.bit, pin.clear), - 0x698 => modify_reg!(regs.scu698(), pin.bit, pin.clear), - 0x69C => modify_reg!(regs.scu69c(), pin.bit, pin.clear), - 0x6B0 => modify_reg!(regs.scu6b0(), pin.bit, pin.clear), - _ => {} // Unknown offset, silently ignore + self.unlock_and_lock_after(|scu| { + let regs = scu.regs(); + for pin in pins { + match pin.offset { + 0x410 => modify_reg!(regs.scu410(), pin.bit, pin.clear), + 0x414 => modify_reg!(regs.scu414(), pin.bit, pin.clear), + 0x418 => modify_reg!(regs.scu418(), pin.bit, pin.clear), + 0x41C => modify_reg!(regs.scu41c(), pin.bit, pin.clear), + 0x430 => modify_reg!(regs.scu430(), pin.bit, pin.clear), + 0x434 => modify_reg!(regs.scu434(), pin.bit, pin.clear), + 0x4B0 => modify_reg!(regs.scu4b0(), pin.bit, pin.clear), + 0x4B4 => modify_reg!(regs.scu4b4(), pin.bit, pin.clear), + 0x4B8 => modify_reg!(regs.scu4b8(), pin.bit, pin.clear), + 0x4BC => modify_reg!(regs.scu4bc(), pin.bit, pin.clear), + 0x690 => modify_reg!(regs.scu690(), pin.bit, pin.clear), + 0x694 => modify_reg!(regs.scu694(), pin.bit, pin.clear), + 0x698 => modify_reg!(regs.scu698(), pin.bit, pin.clear), + 0x69C => modify_reg!(regs.scu69c(), pin.bit, pin.clear), + 0x6B0 => modify_reg!(regs.scu6b0(), pin.bit, pin.clear), + _ => {} // Unknown offset, silently ignore + } } - } + }); } }
diff --git a/target/ast10x0/peripherals/scu/registers.rs b/target/ast10x0/peripherals/scu/registers.rs index 2b7769d..0dec408 100644 --- a/target/ast10x0/peripherals/scu/registers.rs +++ b/target/ast10x0/peripherals/scu/registers.rs
@@ -7,6 +7,7 @@ use crate::hal::field_mux::RegisterAccess; use ast1060_pac as device; use core::marker::PhantomData; +use core::sync::atomic::{AtomicU32, Ordering}; const SCU_UNLOCK_KEY: u32 = 0x1688_A8A8; @@ -24,6 +25,10 @@ mmio.write_at::<0x000>(0); } +/// Nesting depth of [`ScuRegisters::unlock_and_lock_after`]; the SCU is +/// unlocked while this is non-zero. +static UNLOCK_DEPTH: AtomicU32 = AtomicU32::new(0); + /// Safe wrapper around the AST10x0 SCU register block. pub struct ScuRegisters { base: *const device::scu::RegisterBlock, @@ -65,9 +70,6 @@ } /// Unlock SCU write-protected registers for subsequent write operations. - /// - /// Call this once before a sequence of SCU writes, following the aspeed-rust - /// pattern of a single unlock per batch of register operations. #[inline] fn unlock_write_protection(&self) { self.regs() @@ -75,20 +77,30 @@ .write(|w| unsafe { w.bits(SCU_UNLOCK_KEY) }); } - /// Lock SCU write-protected registers after a sequence of write operations. + /// Lock SCU write-protected registers. #[inline] fn lock_write_protection(&self) { self.regs().scu000().write(|w| unsafe { w.bits(0) }); } - /// Lock SCU write-protected registers after a sequence of write operations. - /// ## Example + /// Run `f` with the SCU write-protected registers unlocked, then re-lock. + /// + /// Calls may be nested: only the outermost call unlocks and only the + /// outermost call re-locks, so a setter that takes the lock itself can be + /// invoked from inside another `unlock_and_lock_after` closure without + /// re-locking the SCU under the caller. + /// + /// The SCU is not re-locked if `f` panics. + /// + /// # Example /// ```rust,no_run - /// let scu = ScuRegisters::new_global(); + /// # use ast10x0_peripherals::scu::ScuRegisters; + /// // SAFETY: example only; the caller owns the singleton SCU. + /// let scu = unsafe { ScuRegisters::new_global() }; /// scu.unlock_and_lock_after(|scu| { - /// let mut bits = scu.regs().scu0f0().read().bits(); - /// bits &= !0xF; - /// scu.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); + /// let mut bits = scu.regs().scu0f0().read().bits(); + /// bits &= !0xF; + /// scu.regs().scu0f0().write(|w| unsafe { w.bits(bits) }); /// }); /// ``` #[inline] @@ -96,9 +108,13 @@ where F: FnOnce(&ScuRegisters) -> R, { - self.unlock_write_protection(); + if UNLOCK_DEPTH.fetch_add(1, Ordering::AcqRel) == 0 { + self.unlock_write_protection(); + } let result = f(self); - self.lock_write_protection(); + if UNLOCK_DEPTH.fetch_sub(1, Ordering::AcqRel) == 1 { + self.lock_write_protection(); + } result } }
diff --git a/target/ast10x0/peripherals/scu/reset.rs b/target/ast10x0/peripherals/scu/reset.rs index 73dc09a..db5c4a9 100644 --- a/target/ast10x0/peripherals/scu/reset.rs +++ b/target/ast10x0/peripherals/scu/reset.rs
@@ -9,30 +9,32 @@ impl ScuRegisters { /// Assert reset bits in the selected reset-control register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn assert_reset_mask(&self, half: ScuRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ScuRegisterHalf::Lower => { - self.regs().scu040().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu040().write(|w| unsafe { w.bits(mask) }); } ScuRegisterHalf::Upper => { - self.regs().scu050().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu050().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Deassert reset bits in the selected reset-control register half. /// - /// Caller must call `unlock_write_protection()` before this. + /// Unlocks the SCU for the duration of the write; see + /// [`ScuRegisters::unlock_and_lock_after`]. pub fn deassert_reset_mask(&self, half: ScuRegisterHalf, mask: u32) { - match half { + self.unlock_and_lock_after(|scu| match half { ScuRegisterHalf::Lower => { - self.regs().scu044().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu044().write(|w| unsafe { w.bits(mask) }); } ScuRegisterHalf::Upper => { - self.regs().scu054().write(|w| unsafe { w.bits(mask) }); + scu.regs().scu054().write(|w| unsafe { w.bits(mask) }); } - } + }); } /// Read the selected reset-control register half.
diff --git a/target/ast10x0/tests/flash/target.rs b/target/ast10x0/tests/flash/target.rs index 8c9f011..524a73d 100644 --- a/target/ast10x0/tests/flash/target.rs +++ b/target/ast10x0/tests/flash/target.rs
@@ -222,7 +222,7 @@ fn run_flash_test() -> Result<(), &'static str> { // SAFETY: kernel main() runs once, single-threaded, with exclusive // hardware ownership. - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); // SAFETY: this image has no other user of the FMC, and this runs once.
diff --git a/target/ast10x0/tests/flash_service/target.rs b/target/ast10x0/tests/flash_service/target.rs index e7cfabc..f90039c 100644 --- a/target/ast10x0/tests/flash_service/target.rs +++ b/target/ast10x0/tests/flash_service/target.rs
@@ -26,7 +26,7 @@ // shared pinctrl registers). // SAFETY: kernel main() runs once, single-threaded, with exclusive // hardware ownership. - let scu = unsafe { ScuRegisters::new_global_unlocked() }; + let scu = unsafe { ScuRegisters::new_global() }; scu.apply_pinctrl_group(PINCTRL_FMC_QUAD); codegen::start();