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