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