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