Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * atomic32.c: 32-bit atomic_t implementation |
| 4 | * |
| 5 | * Copyright (C) 2004 Keith M Wesolowski |
Kyle McMartin | 6197fe4 | 2007-05-29 02:51:13 -0700 | [diff] [blame] | 6 | * Copyright (C) 2007 Kyle McMartin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Based on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf |
| 9 | */ |
| 10 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 11 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #ifdef CONFIG_SMP |
| 16 | #define ATOMIC_HASH_SIZE 4 |
| 17 | #define ATOMIC_HASH(a) (&__atomic_hash[(((unsigned long)a)>>8) & (ATOMIC_HASH_SIZE-1)]) |
| 18 | |
| 19 | spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] = { |
Thomas Gleixner | 24774fb | 2011-01-23 15:19:12 +0100 | [diff] [blame] | 20 | [0 ... (ATOMIC_HASH_SIZE-1)] = __SPIN_LOCK_UNLOCKED(__atomic_hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | #else /* SMP */ |
| 24 | |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 25 | static DEFINE_SPINLOCK(dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define ATOMIC_HASH_SIZE 1 |
| 27 | #define ATOMIC_HASH(a) (&dummy) |
| 28 | |
| 29 | #endif /* SMP */ |
| 30 | |
Peter Zijlstra | 3a1adb2 | 2016-04-18 01:16:04 +0200 | [diff] [blame] | 31 | #define ATOMIC_FETCH_OP(op, c_op) \ |
| 32 | int atomic_fetch_##op(int i, atomic_t *v) \ |
| 33 | { \ |
| 34 | int ret; \ |
| 35 | unsigned long flags; \ |
| 36 | spin_lock_irqsave(ATOMIC_HASH(v), flags); \ |
| 37 | \ |
| 38 | ret = v->counter; \ |
| 39 | v->counter c_op i; \ |
| 40 | \ |
| 41 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \ |
| 42 | return ret; \ |
| 43 | } \ |
| 44 | EXPORT_SYMBOL(atomic_fetch_##op); |
| 45 | |
Peter Zijlstra | 304a0d6 | 2014-04-23 19:40:25 +0200 | [diff] [blame] | 46 | #define ATOMIC_OP_RETURN(op, c_op) \ |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 47 | int atomic_##op##_return(int i, atomic_t *v) \ |
| 48 | { \ |
| 49 | int ret; \ |
| 50 | unsigned long flags; \ |
| 51 | spin_lock_irqsave(ATOMIC_HASH(v), flags); \ |
| 52 | \ |
Peter Zijlstra | 304a0d6 | 2014-04-23 19:40:25 +0200 | [diff] [blame] | 53 | ret = (v->counter c_op i); \ |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 54 | \ |
| 55 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \ |
| 56 | return ret; \ |
| 57 | } \ |
| 58 | EXPORT_SYMBOL(atomic_##op##_return); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Peter Zijlstra | 304a0d6 | 2014-04-23 19:40:25 +0200 | [diff] [blame] | 60 | ATOMIC_OP_RETURN(add, +=) |
Peter Zijlstra | 304a0d6 | 2014-04-23 19:40:25 +0200 | [diff] [blame] | 61 | |
Peter Zijlstra | 3a1adb2 | 2016-04-18 01:16:04 +0200 | [diff] [blame] | 62 | ATOMIC_FETCH_OP(add, +=) |
| 63 | ATOMIC_FETCH_OP(and, &=) |
| 64 | ATOMIC_FETCH_OP(or, |=) |
| 65 | ATOMIC_FETCH_OP(xor, ^=) |
| 66 | |
| 67 | #undef ATOMIC_FETCH_OP |
Peter Zijlstra | 304a0d6 | 2014-04-23 19:40:25 +0200 | [diff] [blame] | 68 | #undef ATOMIC_OP_RETURN |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 69 | |
Andreas Larsson | 1a17fdc | 2014-11-05 15:52:08 +0100 | [diff] [blame] | 70 | int atomic_xchg(atomic_t *v, int new) |
| 71 | { |
| 72 | int ret; |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(ATOMIC_HASH(v), flags); |
| 76 | ret = v->counter; |
| 77 | v->counter = new; |
| 78 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); |
| 79 | return ret; |
| 80 | } |
| 81 | EXPORT_SYMBOL(atomic_xchg); |
| 82 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 83 | int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 84 | { |
| 85 | int ret; |
| 86 | unsigned long flags; |
| 87 | |
| 88 | spin_lock_irqsave(ATOMIC_HASH(v), flags); |
| 89 | ret = v->counter; |
| 90 | if (likely(ret == old)) |
| 91 | v->counter = new; |
| 92 | |
| 93 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); |
| 94 | return ret; |
| 95 | } |
Robert Reif | 74e61de | 2007-03-26 19:10:43 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL(atomic_cmpxchg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 98 | int atomic_fetch_add_unless(atomic_t *v, int a, int u) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 99 | { |
| 100 | int ret; |
| 101 | unsigned long flags; |
| 102 | |
| 103 | spin_lock_irqsave(ATOMIC_HASH(v), flags); |
| 104 | ret = v->counter; |
| 105 | if (ret != u) |
| 106 | v->counter += a; |
| 107 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); |
Josip Rodin | a61b582 | 2011-08-04 02:47:40 -0700 | [diff] [blame] | 108 | return ret; |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 109 | } |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 110 | EXPORT_SYMBOL(atomic_fetch_add_unless); |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 111 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 112 | /* Atomic operations are already serializing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | void atomic_set(atomic_t *v, int i) |
| 114 | { |
| 115 | unsigned long flags; |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | spin_lock_irqsave(ATOMIC_HASH(v), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | v->counter = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | spin_unlock_irqrestore(ATOMIC_HASH(v), flags); |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | EXPORT_SYMBOL(atomic_set); |
David S. Miller | 8a8b836 | 2006-12-17 16:18:47 -0800 | [diff] [blame] | 122 | |
| 123 | unsigned long ___set_bit(unsigned long *addr, unsigned long mask) |
| 124 | { |
| 125 | unsigned long old, flags; |
| 126 | |
| 127 | spin_lock_irqsave(ATOMIC_HASH(addr), flags); |
| 128 | old = *addr; |
| 129 | *addr = old | mask; |
| 130 | spin_unlock_irqrestore(ATOMIC_HASH(addr), flags); |
| 131 | |
| 132 | return old & mask; |
| 133 | } |
| 134 | EXPORT_SYMBOL(___set_bit); |
| 135 | |
| 136 | unsigned long ___clear_bit(unsigned long *addr, unsigned long mask) |
| 137 | { |
| 138 | unsigned long old, flags; |
| 139 | |
| 140 | spin_lock_irqsave(ATOMIC_HASH(addr), flags); |
| 141 | old = *addr; |
| 142 | *addr = old & ~mask; |
| 143 | spin_unlock_irqrestore(ATOMIC_HASH(addr), flags); |
| 144 | |
| 145 | return old & mask; |
| 146 | } |
| 147 | EXPORT_SYMBOL(___clear_bit); |
| 148 | |
| 149 | unsigned long ___change_bit(unsigned long *addr, unsigned long mask) |
| 150 | { |
| 151 | unsigned long old, flags; |
| 152 | |
| 153 | spin_lock_irqsave(ATOMIC_HASH(addr), flags); |
| 154 | old = *addr; |
| 155 | *addr = old ^ mask; |
| 156 | spin_unlock_irqrestore(ATOMIC_HASH(addr), flags); |
| 157 | |
| 158 | return old & mask; |
| 159 | } |
| 160 | EXPORT_SYMBOL(___change_bit); |
Kyle McMartin | 6197fe4 | 2007-05-29 02:51:13 -0700 | [diff] [blame] | 161 | |
| 162 | unsigned long __cmpxchg_u32(volatile u32 *ptr, u32 old, u32 new) |
| 163 | { |
| 164 | unsigned long flags; |
| 165 | u32 prev; |
| 166 | |
Andrew Morton | 1fb8812 | 2007-05-31 01:19:24 -0700 | [diff] [blame] | 167 | spin_lock_irqsave(ATOMIC_HASH(ptr), flags); |
Kyle McMartin | 6197fe4 | 2007-05-29 02:51:13 -0700 | [diff] [blame] | 168 | if ((prev = *ptr) == old) |
| 169 | *ptr = new; |
Andrew Morton | 1fb8812 | 2007-05-31 01:19:24 -0700 | [diff] [blame] | 170 | spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags); |
Kyle McMartin | 6197fe4 | 2007-05-29 02:51:13 -0700 | [diff] [blame] | 171 | |
| 172 | return (unsigned long)prev; |
| 173 | } |
| 174 | EXPORT_SYMBOL(__cmpxchg_u32); |
Andreas Larsson | 1a17fdc | 2014-11-05 15:52:08 +0100 | [diff] [blame] | 175 | |
David S. Miller | 23198dd | 2017-09-27 22:38:19 -0700 | [diff] [blame] | 176 | u64 __cmpxchg_u64(u64 *ptr, u64 old, u64 new) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | u64 prev; |
| 180 | |
| 181 | spin_lock_irqsave(ATOMIC_HASH(ptr), flags); |
| 182 | if ((prev = *ptr) == old) |
| 183 | *ptr = new; |
| 184 | spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags); |
| 185 | |
| 186 | return prev; |
| 187 | } |
| 188 | EXPORT_SYMBOL(__cmpxchg_u64); |
| 189 | |
Andreas Larsson | 1a17fdc | 2014-11-05 15:52:08 +0100 | [diff] [blame] | 190 | unsigned long __xchg_u32(volatile u32 *ptr, u32 new) |
| 191 | { |
| 192 | unsigned long flags; |
| 193 | u32 prev; |
| 194 | |
| 195 | spin_lock_irqsave(ATOMIC_HASH(ptr), flags); |
| 196 | prev = *ptr; |
| 197 | *ptr = new; |
| 198 | spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags); |
| 199 | |
| 200 | return (unsigned long)prev; |
| 201 | } |
| 202 | EXPORT_SYMBOL(__xchg_u32); |