Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Arun Sharma | acac43e | 2011-07-26 16:09:08 -0700 | [diff] [blame] | 2 | /* Atomic operations usable in machine independent code */ |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 3 | #ifndef _LINUX_ATOMIC_H |
| 4 | #define _LINUX_ATOMIC_H |
Mark Rutland | ade5ef9 | 2018-06-21 13:13:07 +0100 | [diff] [blame] | 5 | #include <linux/types.h> |
| 6 | |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 7 | #include <asm/atomic.h> |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 8 | #include <asm/barrier.h> |
| 9 | |
| 10 | /* |
| 11 | * Relaxed variants of xchg, cmpxchg and some atomic operations. |
| 12 | * |
| 13 | * We support four variants: |
| 14 | * |
| 15 | * - Fully ordered: The default implementation, no suffix required. |
| 16 | * - Acquire: Provides ACQUIRE semantics, _acquire suffix. |
| 17 | * - Release: Provides RELEASE semantics, _release suffix. |
| 18 | * - Relaxed: No ordering guarantees, _relaxed suffix. |
| 19 | * |
| 20 | * For compound atomics performing both a load and a store, ACQUIRE |
| 21 | * semantics apply only to the load and RELEASE semantics only to the |
| 22 | * store portion of the operation. Note that a failed cmpxchg_acquire |
| 23 | * does -not- imply any memory ordering constraints. |
| 24 | * |
| 25 | * See Documentation/memory-barriers.txt for ACQUIRE/RELEASE definitions. |
| 26 | */ |
| 27 | |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 28 | /* |
| 29 | * The idea here is to build acquire/release variants by adding explicit |
| 30 | * barriers on top of the relaxed variant. In the case where the relaxed |
| 31 | * variant is already fully ordered, no additional barriers are needed. |
Boqun Feng | e1ab7f39 | 2015-12-15 22:24:14 +0800 | [diff] [blame] | 32 | * |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 33 | * If an architecture overrides __atomic_acquire_fence() it will probably |
| 34 | * want to define smp_mb__after_spinlock(). |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 35 | */ |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 36 | #ifndef __atomic_acquire_fence |
| 37 | #define __atomic_acquire_fence smp_mb__after_atomic |
| 38 | #endif |
| 39 | |
| 40 | #ifndef __atomic_release_fence |
| 41 | #define __atomic_release_fence smp_mb__before_atomic |
| 42 | #endif |
| 43 | |
| 44 | #ifndef __atomic_pre_full_fence |
| 45 | #define __atomic_pre_full_fence smp_mb__before_atomic |
| 46 | #endif |
| 47 | |
| 48 | #ifndef __atomic_post_full_fence |
| 49 | #define __atomic_post_full_fence smp_mb__after_atomic |
| 50 | #endif |
| 51 | |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 52 | #define __atomic_op_acquire(op, args...) \ |
| 53 | ({ \ |
| 54 | typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \ |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 55 | __atomic_acquire_fence(); \ |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 56 | __ret; \ |
| 57 | }) |
| 58 | |
| 59 | #define __atomic_op_release(op, args...) \ |
| 60 | ({ \ |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 61 | __atomic_release_fence(); \ |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 62 | op##_relaxed(args); \ |
| 63 | }) |
| 64 | |
| 65 | #define __atomic_op_fence(op, args...) \ |
| 66 | ({ \ |
| 67 | typeof(op##_relaxed(args)) __ret; \ |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 68 | __atomic_pre_full_fence(); \ |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 69 | __ret = op##_relaxed(args); \ |
Mark Rutland | fd2efaa | 2018-07-16 12:30:11 +0100 | [diff] [blame] | 70 | __atomic_post_full_fence(); \ |
Will Deacon | 654672d | 2015-08-06 17:54:37 +0100 | [diff] [blame] | 71 | __ret; \ |
| 72 | }) |
| 73 | |
Mark Rutland | 9fa4507 | 2018-09-04 11:48:26 +0100 | [diff] [blame] | 74 | #include <linux/atomic-fallback.h> |
Will Deacon | 4df714b | 2017-10-12 13:20:48 +0100 | [diff] [blame] | 75 | |
Peter Zijlstra | 90fe651 | 2015-09-18 15:04:59 +0200 | [diff] [blame] | 76 | #include <asm-generic/atomic-long.h> |
| 77 | |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 78 | #endif /* _LINUX_ATOMIC_H */ |