1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Generic barrier definitions.
5 * It should be possible to use these on really simple architectures,
6 * but it serves more as a starting point for new ports.
8 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
9 * Written by David Howells (dhowells@redhat.com)
11 #ifndef __ASM_GENERIC_BARRIER_H
12 #define __ASM_GENERIC_BARRIER_H
16 #include <linux/compiler.h>
19 #define nop() asm volatile ("nop")
23 * Force strict CPU ordering. And yes, this is required on UP too when we're
26 * Fall back to compiler barriers if nothing better is provided.
30 #define mb() barrier()
42 #define dma_rmb() rmb()
46 #define dma_wmb() wmb()
49 #ifndef read_barrier_depends
50 #define read_barrier_depends() do { } while (0)
54 #define __smp_mb() mb()
58 #define __smp_rmb() rmb()
62 #define __smp_wmb() wmb()
65 #ifndef __smp_read_barrier_depends
66 #define __smp_read_barrier_depends() read_barrier_depends()
72 #define smp_mb() __smp_mb()
76 #define smp_rmb() __smp_rmb()
80 #define smp_wmb() __smp_wmb()
83 #ifndef smp_read_barrier_depends
84 #define smp_read_barrier_depends() __smp_read_barrier_depends()
87 #else /* !CONFIG_SMP */
90 #define smp_mb() barrier()
94 #define smp_rmb() barrier()
98 #define smp_wmb() barrier()
101 #ifndef smp_read_barrier_depends
102 #define smp_read_barrier_depends() do { } while (0)
105 #endif /* CONFIG_SMP */
107 #ifndef __smp_store_mb
108 #define __smp_store_mb(var, value) do { WRITE_ONCE(var, value); __smp_mb(); } while (0)
111 #ifndef __smp_mb__before_atomic
112 #define __smp_mb__before_atomic() __smp_mb()
115 #ifndef __smp_mb__after_atomic
116 #define __smp_mb__after_atomic() __smp_mb()
119 #ifndef __smp_store_release
120 #define __smp_store_release(p, v) \
122 compiletime_assert_atomic_type(*p); \
128 #ifndef __smp_load_acquire
129 #define __smp_load_acquire(p) \
131 typeof(*p) ___p1 = READ_ONCE(*p); \
132 compiletime_assert_atomic_type(*p); \
141 #define smp_store_mb(var, value) __smp_store_mb(var, value)
144 #ifndef smp_mb__before_atomic
145 #define smp_mb__before_atomic() __smp_mb__before_atomic()
148 #ifndef smp_mb__after_atomic
149 #define smp_mb__after_atomic() __smp_mb__after_atomic()
152 #ifndef smp_store_release
153 #define smp_store_release(p, v) __smp_store_release(p, v)
156 #ifndef smp_load_acquire
157 #define smp_load_acquire(p) __smp_load_acquire(p)
160 #else /* !CONFIG_SMP */
163 #define smp_store_mb(var, value) do { WRITE_ONCE(var, value); barrier(); } while (0)
166 #ifndef smp_mb__before_atomic
167 #define smp_mb__before_atomic() barrier()
170 #ifndef smp_mb__after_atomic
171 #define smp_mb__after_atomic() barrier()
174 #ifndef smp_store_release
175 #define smp_store_release(p, v) \
177 compiletime_assert_atomic_type(*p); \
183 #ifndef smp_load_acquire
184 #define smp_load_acquire(p) \
186 typeof(*p) ___p1 = READ_ONCE(*p); \
187 compiletime_assert_atomic_type(*p); \
193 #endif /* CONFIG_SMP */
195 /* Barriers for virtual machine guests when talking to an SMP host */
196 #define virt_mb() __smp_mb()
197 #define virt_rmb() __smp_rmb()
198 #define virt_wmb() __smp_wmb()
199 #define virt_read_barrier_depends() __smp_read_barrier_depends()
200 #define virt_store_mb(var, value) __smp_store_mb(var, value)
201 #define virt_mb__before_atomic() __smp_mb__before_atomic()
202 #define virt_mb__after_atomic() __smp_mb__after_atomic()
203 #define virt_store_release(p, v) __smp_store_release(p, v)
204 #define virt_load_acquire(p) __smp_load_acquire(p)
207 * smp_acquire__after_ctrl_dep() - Provide ACQUIRE ordering after a control dependency
209 * A control dependency provides a LOAD->STORE order, the additional RMB
210 * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
211 * aka. (load)-ACQUIRE.
213 * Architectures that do not do load speculation can have this be barrier().
215 #ifndef smp_acquire__after_ctrl_dep
216 #define smp_acquire__after_ctrl_dep() smp_rmb()
220 * smp_cond_load_relaxed() - (Spin) wait for cond with no ordering guarantees
221 * @ptr: pointer to the variable to wait on
222 * @cond: boolean expression to wait for
224 * Equivalent to using READ_ONCE() on the condition variable.
226 * Due to C lacking lambda expressions we load the value of *ptr into a
227 * pre-named variable @VAL to be used in @cond.
229 #ifndef smp_cond_load_relaxed
230 #define smp_cond_load_relaxed(ptr, cond_expr) ({ \
231 typeof(ptr) __PTR = (ptr); \
234 VAL = READ_ONCE(*__PTR); \
244 * smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering
245 * @ptr: pointer to the variable to wait on
246 * @cond: boolean expression to wait for
248 * Equivalent to using smp_load_acquire() on the condition variable but employs
249 * the control dependency of the wait to reduce the barrier on many platforms.
251 #ifndef smp_cond_load_acquire
252 #define smp_cond_load_acquire(ptr, cond_expr) ({ \
254 _val = smp_cond_load_relaxed(ptr, cond_expr); \
255 smp_acquire__after_ctrl_dep(); \
260 #endif /* !__ASSEMBLY__ */
261 #endif /* __ASM_GENERIC_BARRIER_H */