2 * Generic barrier definitions.
4 * It should be possible to use these on really simple architectures,
5 * but it serves more as a starting point for new ports.
7 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
8 * Written by David Howells (dhowells@redhat.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public Licence
12 * as published by the Free Software Foundation; either version
13 * 2 of the Licence, or (at your option) any later version.
15 #ifndef __ASM_GENERIC_BARRIER_H
16 #define __ASM_GENERIC_BARRIER_H
20 #include <linux/compiler.h>
23 #define nop() asm volatile ("nop")
27 * Force strict CPU ordering. And yes, this is required on UP too when we're
30 * Fall back to compiler barriers if nothing better is provided.
34 #define mb() barrier()
46 #define dma_rmb() rmb()
50 #define dma_wmb() wmb()
53 #ifndef read_barrier_depends
54 #define read_barrier_depends() do { } while (0)
58 #define __smp_mb() mb()
62 #define __smp_rmb() rmb()
66 #define __smp_wmb() wmb()
69 #ifndef __smp_read_barrier_depends
70 #define __smp_read_barrier_depends() read_barrier_depends()
76 #define smp_mb() __smp_mb()
80 #define smp_rmb() __smp_rmb()
84 #define smp_wmb() __smp_wmb()
87 #ifndef smp_read_barrier_depends
88 #define smp_read_barrier_depends() __smp_read_barrier_depends()
91 #else /* !CONFIG_SMP */
94 #define smp_mb() barrier()
98 #define smp_rmb() barrier()
102 #define smp_wmb() barrier()
105 #ifndef smp_read_barrier_depends
106 #define smp_read_barrier_depends() do { } while (0)
109 #endif /* CONFIG_SMP */
111 #ifndef __smp_store_mb
112 #define __smp_store_mb(var, value) do { WRITE_ONCE(var, value); __smp_mb(); } while (0)
115 #ifndef __smp_mb__before_atomic
116 #define __smp_mb__before_atomic() __smp_mb()
119 #ifndef __smp_mb__after_atomic
120 #define __smp_mb__after_atomic() __smp_mb()
123 #ifndef __smp_store_release
124 #define __smp_store_release(p, v) \
126 compiletime_assert_atomic_type(*p); \
132 #ifndef __smp_load_acquire
133 #define __smp_load_acquire(p) \
135 typeof(*p) ___p1 = READ_ONCE(*p); \
136 compiletime_assert_atomic_type(*p); \
145 #define smp_store_mb(var, value) __smp_store_mb(var, value)
148 #ifndef smp_mb__before_atomic
149 #define smp_mb__before_atomic() __smp_mb__before_atomic()
152 #ifndef smp_mb__after_atomic
153 #define smp_mb__after_atomic() __smp_mb__after_atomic()
156 #ifndef smp_store_release
157 #define smp_store_release(p, v) __smp_store_release(p, v)
160 #ifndef smp_load_acquire
161 #define smp_load_acquire(p) __smp_load_acquire(p)
164 #else /* !CONFIG_SMP */
167 #define smp_store_mb(var, value) do { WRITE_ONCE(var, value); barrier(); } while (0)
170 #ifndef smp_mb__before_atomic
171 #define smp_mb__before_atomic() barrier()
174 #ifndef smp_mb__after_atomic
175 #define smp_mb__after_atomic() barrier()
178 #ifndef smp_store_release
179 #define smp_store_release(p, v) \
181 compiletime_assert_atomic_type(*p); \
187 #ifndef smp_load_acquire
188 #define smp_load_acquire(p) \
190 typeof(*p) ___p1 = READ_ONCE(*p); \
191 compiletime_assert_atomic_type(*p); \
197 #endif /* CONFIG_SMP */
199 /* Barriers for virtual machine guests when talking to an SMP host */
200 #define virt_mb() __smp_mb()
201 #define virt_rmb() __smp_rmb()
202 #define virt_wmb() __smp_wmb()
203 #define virt_read_barrier_depends() __smp_read_barrier_depends()
204 #define virt_store_mb(var, value) __smp_store_mb(var, value)
205 #define virt_mb__before_atomic() __smp_mb__before_atomic()
206 #define virt_mb__after_atomic() __smp_mb__after_atomic()
207 #define virt_store_release(p, v) __smp_store_release(p, v)
208 #define virt_load_acquire(p) __smp_load_acquire(p)
211 * smp_acquire__after_ctrl_dep() - Provide ACQUIRE ordering after a control dependency
213 * A control dependency provides a LOAD->STORE order, the additional RMB
214 * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
215 * aka. (load)-ACQUIRE.
217 * Architectures that do not do load speculation can have this be barrier().
219 #ifndef smp_acquire__after_ctrl_dep
220 #define smp_acquire__after_ctrl_dep() smp_rmb()
224 * smp_cond_load_relaxed() - (Spin) wait for cond with no ordering guarantees
225 * @ptr: pointer to the variable to wait on
226 * @cond: boolean expression to wait for
228 * Equivalent to using READ_ONCE() on the condition variable.
230 * Due to C lacking lambda expressions we load the value of *ptr into a
231 * pre-named variable @VAL to be used in @cond.
233 #ifndef smp_cond_load_relaxed
234 #define smp_cond_load_relaxed(ptr, cond_expr) ({ \
235 typeof(ptr) __PTR = (ptr); \
238 VAL = READ_ONCE(*__PTR); \
248 * smp_cond_load_acquire() - (Spin) wait for cond with ACQUIRE ordering
249 * @ptr: pointer to the variable to wait on
250 * @cond: boolean expression to wait for
252 * Equivalent to using smp_load_acquire() on the condition variable but employs
253 * the control dependency of the wait to reduce the barrier on many platforms.
255 #ifndef smp_cond_load_acquire
256 #define smp_cond_load_acquire(ptr, cond_expr) ({ \
258 _val = smp_cond_load_relaxed(ptr, cond_expr); \
259 smp_acquire__after_ctrl_dep(); \
264 #endif /* !__ASSEMBLY__ */
265 #endif /* __ASM_GENERIC_BARRIER_H */