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>
17 #include <asm/rwonce.h>
20 #define nop() asm volatile ("nop")
24 * Force strict CPU ordering. And yes, this is required on UP too when we're
27 * Fall back to compiler barriers if nothing better is provided.
31 #define mb() barrier()
43 #define dma_rmb() rmb()
47 #define dma_wmb() wmb()
51 #define __smp_mb() mb()
55 #define __smp_rmb() rmb()
59 #define __smp_wmb() wmb()
65 #define smp_mb() __smp_mb()
69 #define smp_rmb() __smp_rmb()
73 #define smp_wmb() __smp_wmb()
76 #else /* !CONFIG_SMP */
79 #define smp_mb() barrier()
83 #define smp_rmb() barrier()
87 #define smp_wmb() barrier()
90 #endif /* CONFIG_SMP */
92 #ifndef __smp_store_mb
93 #define __smp_store_mb(var, value) do { WRITE_ONCE(var, value); __smp_mb(); } while (0)
96 #ifndef __smp_mb__before_atomic
97 #define __smp_mb__before_atomic() __smp_mb()
100 #ifndef __smp_mb__after_atomic
101 #define __smp_mb__after_atomic() __smp_mb()
104 #ifndef __smp_store_release
105 #define __smp_store_release(p, v) \
107 compiletime_assert_atomic_type(*p); \
113 #ifndef __smp_load_acquire
114 #define __smp_load_acquire(p) \
116 __unqual_scalar_typeof(*p) ___p1 = READ_ONCE(*p); \
117 compiletime_assert_atomic_type(*p); \
126 #define smp_store_mb(var, value) __smp_store_mb(var, value)
129 #ifndef smp_mb__before_atomic
130 #define smp_mb__before_atomic() __smp_mb__before_atomic()
133 #ifndef smp_mb__after_atomic
134 #define smp_mb__after_atomic() __smp_mb__after_atomic()
137 #ifndef smp_store_release
138 #define smp_store_release(p, v) __smp_store_release(p, v)
141 #ifndef smp_load_acquire
142 #define smp_load_acquire(p) __smp_load_acquire(p)
145 #else /* !CONFIG_SMP */
148 #define smp_store_mb(var, value) do { WRITE_ONCE(var, value); barrier(); } while (0)
151 #ifndef smp_mb__before_atomic
152 #define smp_mb__before_atomic() barrier()
155 #ifndef smp_mb__after_atomic
156 #define smp_mb__after_atomic() barrier()
159 #ifndef smp_store_release
160 #define smp_store_release(p, v) \
162 compiletime_assert_atomic_type(*p); \
168 #ifndef smp_load_acquire
169 #define smp_load_acquire(p) \
171 __unqual_scalar_typeof(*p) ___p1 = READ_ONCE(*p); \
172 compiletime_assert_atomic_type(*p); \
178 #endif /* CONFIG_SMP */
180 /* Barriers for virtual machine guests when talking to an SMP host */
181 #define virt_mb() __smp_mb()
182 #define virt_rmb() __smp_rmb()
183 #define virt_wmb() __smp_wmb()
184 #define virt_store_mb(var, value) __smp_store_mb(var, value)
185 #define virt_mb__before_atomic() __smp_mb__before_atomic()
186 #define virt_mb__after_atomic() __smp_mb__after_atomic()
187 #define virt_store_release(p, v) __smp_store_release(p, v)
188 #define virt_load_acquire(p) __smp_load_acquire(p)
191 * smp_acquire__after_ctrl_dep() - Provide ACQUIRE ordering after a control dependency
193 * A control dependency provides a LOAD->STORE order, the additional RMB
194 * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
195 * aka. (load)-ACQUIRE.
197 * Architectures that do not do load speculation can have this be barrier().
199 #ifndef smp_acquire__after_ctrl_dep
200 #define smp_acquire__after_ctrl_dep() smp_rmb()
204 * smp_cond_load_relaxed() - (Spin) wait for cond with no ordering guarantees
205 * @ptr: pointer to the variable to wait on
206 * @cond: boolean expression to wait for
208 * Equivalent to using READ_ONCE() on the condition variable.
210 * Due to C lacking lambda expressions we load the value of *ptr into a
211 * pre-named variable @VAL to be used in @cond.
213 #ifndef smp_cond_load_relaxed
214 #define smp_cond_load_relaxed(ptr, cond_expr) ({ \
215 typeof(ptr) __PTR = (ptr); \
216 __unqual_scalar_typeof(*ptr) VAL; \
218 VAL = READ_ONCE(*__PTR); \
228 * smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering
229 * @ptr: pointer to the variable to wait on
230 * @cond: boolean expression to wait for
232 * Equivalent to using smp_load_acquire() on the condition variable but employs
233 * the control dependency of the wait to reduce the barrier on many platforms.
235 #ifndef smp_cond_load_acquire
236 #define smp_cond_load_acquire(ptr, cond_expr) ({ \
237 __unqual_scalar_typeof(*ptr) _val; \
238 _val = smp_cond_load_relaxed(ptr, cond_expr); \
239 smp_acquire__after_ctrl_dep(); \
240 (typeof(*ptr))_val; \
245 * pmem_wmb() ensures that all stores for which the modification
246 * are written to persistent storage by preceding instructions have
247 * updated persistent storage before any data access or data transfer
248 * caused by subsequent instructions is initiated.
251 #define pmem_wmb() wmb()
254 #endif /* !__ASSEMBLY__ */
255 #endif /* __ASM_GENERIC_BARRIER_H */