aarch64_cpu/asm/
barrier.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2//
3// Copyright (c) 2018-2023 by the author(s)
4//
5// Author(s):
6//   - Andre Richter <andre.o.richter@gmail.com>
7
8//! Barrier functions.
9
10mod sealed {
11    pub trait Dmb {
12        fn __dmb(&self);
13    }
14
15    pub trait Dsb {
16        fn __dsb(&self);
17    }
18
19    pub trait Isb {
20        fn __isb(&self);
21    }
22}
23
24macro_rules! dmb_dsb {
25    ($A:ident) => {
26        impl sealed::Dmb for $A {
27            #[inline(always)]
28            fn __dmb(&self) {
29                match () {
30                    #[cfg(target_arch = "aarch64")]
31                    () => unsafe {
32                        core::arch::asm!(concat!("DMB ", stringify!($A)), options(nostack))
33                    },
34
35                    #[cfg(not(target_arch = "aarch64"))]
36                    () => unimplemented!(),
37                }
38            }
39        }
40        impl sealed::Dsb for $A {
41            #[inline(always)]
42            fn __dsb(&self) {
43                match () {
44                    #[cfg(target_arch = "aarch64")]
45                    () => unsafe {
46                        core::arch::asm!(concat!("DSB ", stringify!($A)), options(nostack))
47                    },
48
49                    #[cfg(not(target_arch = "aarch64"))]
50                    () => unimplemented!(),
51                }
52            }
53        }
54    };
55}
56
57pub struct SY;
58pub struct ST;
59pub struct LD;
60pub struct ISH;
61pub struct ISHST;
62pub struct ISHLD;
63pub struct NSH;
64pub struct NSHST;
65pub struct NSHLD;
66pub struct OSH;
67pub struct OSHST;
68pub struct OSHLD;
69
70dmb_dsb!(SY);
71dmb_dsb!(ST);
72dmb_dsb!(LD);
73dmb_dsb!(ISH);
74dmb_dsb!(ISHST);
75dmb_dsb!(ISHLD);
76dmb_dsb!(NSH);
77dmb_dsb!(NSHST);
78dmb_dsb!(NSHLD);
79dmb_dsb!(OSH);
80dmb_dsb!(OSHST);
81dmb_dsb!(OSHLD);
82
83impl sealed::Isb for SY {
84    #[inline(always)]
85    fn __isb(&self) {
86        match () {
87            #[cfg(target_arch = "aarch64")]
88            () => unsafe { core::arch::asm!("ISB SY", options(nostack)) },
89
90            #[cfg(not(target_arch = "aarch64"))]
91            () => unimplemented!(),
92        }
93    }
94}
95
96/// Data Memory Barrier.
97#[inline(always)]
98pub fn dmb<A>(arg: A)
99where
100    A: sealed::Dmb,
101{
102    arg.__dmb()
103}
104
105/// Data Synchronization Barrier.
106#[inline(always)]
107pub fn dsb<A>(arg: A)
108where
109    A: sealed::Dsb,
110{
111    arg.__dsb()
112}
113
114/// Instruction Synchronization Barrier.
115#[inline(always)]
116pub fn isb<A>(arg: A)
117where
118    A: sealed::Isb,
119{
120    arg.__isb()
121}