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 <linux/kcsan-checks.h>
18 #include <asm/rwonce.h>
21 #define nop() asm volatile ("nop")
25 * Architectures that want generic instrumentation can define __ prefixed
26 * variants of all barriers.
30 #define mb() do { kcsan_mb(); __mb(); } while (0)
34 #define rmb() do { kcsan_rmb(); __rmb(); } while (0)
38 #define wmb() do { kcsan_wmb(); __wmb(); } while (0)
42 #define dma_mb() do { kcsan_mb(); __dma_mb(); } while (0)
46 #define dma_rmb() do { kcsan_rmb(); __dma_rmb(); } while (0)
50 #define dma_wmb() do { kcsan_wmb(); __dma_wmb(); } while (0)
54 * Force strict CPU ordering. And yes, this is required on UP too when we're
57 * Fall back to compiler barriers if nothing better is provided.
61 #define mb() barrier()
77 #define dma_rmb() rmb()
81 #define dma_wmb() wmb()
85 #define __smp_mb() mb()
89 #define __smp_rmb() rmb()
93 #define __smp_wmb() wmb()
99 #define smp_mb() do { kcsan_mb(); __smp_mb(); } while (0)
103 #define smp_rmb() do { kcsan_rmb(); __smp_rmb(); } while (0)
107 #define smp_wmb() do { kcsan_wmb(); __smp_wmb(); } while (0)
110 #else /* !CONFIG_SMP */
113 #define smp_mb() barrier()
117 #define smp_rmb() barrier()
121 #define smp_wmb() barrier()
124 #endif /* CONFIG_SMP */
126 #ifndef __smp_store_mb
127 #define __smp_store_mb(var, value) do { WRITE_ONCE(var, value); __smp_mb(); } while (0)
130 #ifndef __smp_mb__before_atomic
131 #define __smp_mb__before_atomic() __smp_mb()
134 #ifndef __smp_mb__after_atomic
135 #define __smp_mb__after_atomic() __smp_mb()
138 #ifndef __smp_store_release
139 #define __smp_store_release(p, v) \
141 compiletime_assert_atomic_type(*p); \
147 #ifndef __smp_load_acquire
148 #define __smp_load_acquire(p) \
150 __unqual_scalar_typeof(*p) ___p1 = READ_ONCE(*p); \
151 compiletime_assert_atomic_type(*p); \
160 #define smp_store_mb(var, value) do { kcsan_mb(); __smp_store_mb(var, value); } while (0)
163 #ifndef smp_mb__before_atomic
164 #define smp_mb__before_atomic() do { kcsan_mb(); __smp_mb__before_atomic(); } while (0)
167 #ifndef smp_mb__after_atomic
168 #define smp_mb__after_atomic() do { kcsan_mb(); __smp_mb__after_atomic(); } while (0)
171 #ifndef smp_store_release
172 #define smp_store_release(p, v) do { kcsan_release(); __smp_store_release(p, v); } while (0)
175 #ifndef smp_load_acquire
176 #define smp_load_acquire(p) __smp_load_acquire(p)
179 #else /* !CONFIG_SMP */
182 #define smp_store_mb(var, value) do { WRITE_ONCE(var, value); barrier(); } while (0)
185 #ifndef smp_mb__before_atomic
186 #define smp_mb__before_atomic() barrier()
189 #ifndef smp_mb__after_atomic
190 #define smp_mb__after_atomic() barrier()
193 #ifndef smp_store_release
194 #define smp_store_release(p, v) \
196 compiletime_assert_atomic_type(*p); \
202 #ifndef smp_load_acquire
203 #define smp_load_acquire(p) \
205 __unqual_scalar_typeof(*p) ___p1 = READ_ONCE(*p); \
206 compiletime_assert_atomic_type(*p); \
212 #endif /* CONFIG_SMP */
214 /* Barriers for virtual machine guests when talking to an SMP host */
215 #define virt_mb() do { kcsan_mb(); __smp_mb(); } while (0)
216 #define virt_rmb() do { kcsan_rmb(); __smp_rmb(); } while (0)
217 #define virt_wmb() do { kcsan_wmb(); __smp_wmb(); } while (0)
218 #define virt_store_mb(var, value) do { kcsan_mb(); __smp_store_mb(var, value); } while (0)
219 #define virt_mb__before_atomic() do { kcsan_mb(); __smp_mb__before_atomic(); } while (0)
220 #define virt_mb__after_atomic() do { kcsan_mb(); __smp_mb__after_atomic(); } while (0)
221 #define virt_store_release(p, v) do { kcsan_release(); __smp_store_release(p, v); } while (0)
222 #define virt_load_acquire(p) __smp_load_acquire(p)
225 * smp_acquire__after_ctrl_dep() - Provide ACQUIRE ordering after a control dependency
227 * A control dependency provides a LOAD->STORE order, the additional RMB
228 * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
229 * aka. (load)-ACQUIRE.
231 * Architectures that do not do load speculation can have this be barrier().
233 #ifndef smp_acquire__after_ctrl_dep
234 #define smp_acquire__after_ctrl_dep() smp_rmb()
238 * smp_cond_load_relaxed() - (Spin) wait for cond with no ordering guarantees
239 * @ptr: pointer to the variable to wait on
240 * @cond: boolean expression to wait for
242 * Equivalent to using READ_ONCE() on the condition variable.
244 * Due to C lacking lambda expressions we load the value of *ptr into a
245 * pre-named variable @VAL to be used in @cond.
247 #ifndef smp_cond_load_relaxed
248 #define smp_cond_load_relaxed(ptr, cond_expr) ({ \
249 typeof(ptr) __PTR = (ptr); \
250 __unqual_scalar_typeof(*ptr) VAL; \
252 VAL = READ_ONCE(*__PTR); \
262 * smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering
263 * @ptr: pointer to the variable to wait on
264 * @cond: boolean expression to wait for
266 * Equivalent to using smp_load_acquire() on the condition variable but employs
267 * the control dependency of the wait to reduce the barrier on many platforms.
269 #ifndef smp_cond_load_acquire
270 #define smp_cond_load_acquire(ptr, cond_expr) ({ \
271 __unqual_scalar_typeof(*ptr) _val; \
272 _val = smp_cond_load_relaxed(ptr, cond_expr); \
273 smp_acquire__after_ctrl_dep(); \
274 (typeof(*ptr))_val; \
279 * pmem_wmb() ensures that all stores for which the modification
280 * are written to persistent storage by preceding instructions have
281 * updated persistent storage before any data access or data transfer
282 * caused by subsequent instructions is initiated.
285 #define pmem_wmb() wmb()
289 * ioremap_wc() maps I/O memory as memory with write-combining attributes. For
290 * this kind of memory accesses, the CPU may wait for prior accesses to be
291 * merged with subsequent ones. In some situation, such wait is bad for the
292 * performance. io_stop_wc() can be used to prevent the merging of
293 * write-combining memory accesses before this macro with those after it.
296 #define io_stop_wc() do { } while (0)
299 #endif /* !__ASSEMBLY__ */
300 #endif /* __ASM_GENERIC_BARRIER_H */