Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/interrupt/xtensa.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 /*
4 Refs:
5 - Xtensa Instruction Set Architecture (ISA) Summary for all Xtensa LX Processors
6 https://www.cadence.com/content/dam/cadence-www/global/en_US/documents/tools/silicon-solutions/compute-ip/isa-summary.pdf
7 - Linux kernel's Xtensa atomic implementation
8 https://github.com/torvalds/linux/blob/v6.19/arch/xtensa/include/asm/atomic.h
9
10 See tests/asm-test/asm/portable-atomic for generated assembly.
11 */
12
13 use core::arch::asm;
14 #[cfg_attr(
15 portable_atomic_no_cfg_target_has_atomic,
16 cfg(any(test, portable_atomic_no_atomic_cas))
17 )]
18 #[cfg_attr(
19 not(portable_atomic_no_cfg_target_has_atomic),
20 cfg(any(test, not(target_has_atomic = "ptr")))
21 )]
22 pub(super) use core::sync::atomic;
23
24 pub(crate) type State = u32;
25
26 /// Disables interrupts and returns the previous interrupt state.
27 #[inline(always)]
28 pub(crate) fn disable() -> State {
29 let ps: State;
30 // SAFETY: reading the PS special register and disabling all interrupts is safe.
31 // (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions)
32 //
33 // Interrupt level 15 to disable all interrupts.
34 // SYNC after RSIL is not required.
35 // Use a14 per https://github.com/torvalds/linux/blob/v6.16/arch/xtensa/include/asm/atomic.h#L33-L37.
36 unsafe {
37 asm!(
38 "rsil a14, 15", // a14 = PS; PS.INTLEVEL = 15
39 out("a14") ps,
40 // Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled.
41 options(nostack),
42 );
43 }
44 ps
45 }
46
47 /// Restores the previous interrupt state.
48 ///
49 /// # Safety
50 ///
51 /// The state must be the one retrieved by the previous `disable`.
52 #[inline(always)]
53 pub(crate) unsafe fn restore(prev_ps: State) {
54 // SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`,
55 // and we've checked that interrupts were enabled before disabling interrupts.
56 //
57 // SYNC after WSR is required to guarantee that subsequent RSIL read the written value.
58 // See also 3.8.10 Processor Control Instructions of Xtensa Instruction Set Architecture (ISA) Summary for all Xtensa LX Processors.
59 unsafe {
60 asm!(
61 "wsr.ps a14", // PS = a14
62 "rsync", // wait
63 in("a14") prev_ps,
64 // Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled.
65 options(nostack),
66 );
67 }
68 }