Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Generic implementation of 64-bit atomics using spinlocks, |
| 3 | * useful on processors that don't have 64-bit atomic instructions. |
| 4 | * |
| 5 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/cache.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/init.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 16 | #include <linux/export.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * We use a hashed array of spinlocks to provide exclusive access |
| 21 | * to each atomic64_t variable. Since this is expected to used on |
| 22 | * systems with small numbers of CPUs (<= 4 or so), we use a |
| 23 | * relatively small array of 16 spinlocks to avoid wasting too much |
| 24 | * memory on the spinlock array. |
| 25 | */ |
| 26 | #define NR_LOCKS 16 |
| 27 | |
| 28 | /* |
| 29 | * Ensure each lock is in a separate cacheline. |
| 30 | */ |
| 31 | static union { |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 32 | raw_spinlock_t lock; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 33 | char pad[L1_CACHE_BYTES]; |
Stephen Boyd | fcc1688 | 2012-12-19 23:39:48 -0800 | [diff] [blame] | 34 | } atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp = { |
| 35 | [0 ... (NR_LOCKS - 1)] = { |
| 36 | .lock = __RAW_SPIN_LOCK_UNLOCKED(atomic64_lock.lock), |
| 37 | }, |
| 38 | }; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 39 | |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 40 | static inline raw_spinlock_t *lock_addr(const atomic64_t *v) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 41 | { |
| 42 | unsigned long addr = (unsigned long) v; |
| 43 | |
| 44 | addr >>= L1_CACHE_SHIFT; |
| 45 | addr ^= (addr >> 8) ^ (addr >> 16); |
| 46 | return &atomic64_lock[addr & (NR_LOCKS - 1)].lock; |
| 47 | } |
| 48 | |
| 49 | long long atomic64_read(const atomic64_t *v) |
| 50 | { |
| 51 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 52 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 53 | long long val; |
| 54 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 55 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 56 | val = v->counter; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 57 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 58 | return val; |
| 59 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 60 | EXPORT_SYMBOL(atomic64_read); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 61 | |
| 62 | void atomic64_set(atomic64_t *v, long long i) |
| 63 | { |
| 64 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 65 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 66 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 67 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 68 | v->counter = i; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 69 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 70 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(atomic64_set); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 72 | |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 73 | #define ATOMIC64_OP(op, c_op) \ |
| 74 | void atomic64_##op(long long a, atomic64_t *v) \ |
| 75 | { \ |
| 76 | unsigned long flags; \ |
| 77 | raw_spinlock_t *lock = lock_addr(v); \ |
| 78 | \ |
| 79 | raw_spin_lock_irqsave(lock, flags); \ |
| 80 | v->counter c_op a; \ |
| 81 | raw_spin_unlock_irqrestore(lock, flags); \ |
| 82 | } \ |
| 83 | EXPORT_SYMBOL(atomic64_##op); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 84 | |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 85 | #define ATOMIC64_OP_RETURN(op, c_op) \ |
| 86 | long long atomic64_##op##_return(long long a, atomic64_t *v) \ |
| 87 | { \ |
| 88 | unsigned long flags; \ |
| 89 | raw_spinlock_t *lock = lock_addr(v); \ |
| 90 | long long val; \ |
| 91 | \ |
| 92 | raw_spin_lock_irqsave(lock, flags); \ |
| 93 | val = (v->counter c_op a); \ |
| 94 | raw_spin_unlock_irqrestore(lock, flags); \ |
| 95 | return val; \ |
| 96 | } \ |
| 97 | EXPORT_SYMBOL(atomic64_##op##_return); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 98 | |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 99 | #define ATOMIC64_FETCH_OP(op, c_op) \ |
| 100 | long long atomic64_fetch_##op(long long a, atomic64_t *v) \ |
| 101 | { \ |
| 102 | unsigned long flags; \ |
| 103 | raw_spinlock_t *lock = lock_addr(v); \ |
| 104 | long long val; \ |
| 105 | \ |
| 106 | raw_spin_lock_irqsave(lock, flags); \ |
| 107 | val = v->counter; \ |
| 108 | v->counter c_op a; \ |
| 109 | raw_spin_unlock_irqrestore(lock, flags); \ |
| 110 | return val; \ |
| 111 | } \ |
| 112 | EXPORT_SYMBOL(atomic64_fetch_##op); |
| 113 | |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 114 | #define ATOMIC64_OPS(op, c_op) \ |
| 115 | ATOMIC64_OP(op, c_op) \ |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 116 | ATOMIC64_OP_RETURN(op, c_op) \ |
| 117 | ATOMIC64_FETCH_OP(op, c_op) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 118 | |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 119 | ATOMIC64_OPS(add, +=) |
| 120 | ATOMIC64_OPS(sub, -=) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 121 | |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 122 | #undef ATOMIC64_OPS |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 123 | #define ATOMIC64_OPS(op, c_op) \ |
| 124 | ATOMIC64_OP(op, c_op) \ |
| 125 | ATOMIC64_OP_RETURN(op, c_op) \ |
| 126 | ATOMIC64_FETCH_OP(op, c_op) |
| 127 | |
| 128 | ATOMIC64_OPS(and, &=) |
| 129 | ATOMIC64_OPS(or, |=) |
| 130 | ATOMIC64_OPS(xor, ^=) |
| 131 | |
| 132 | #undef ATOMIC64_OPS |
| 133 | #undef ATOMIC64_FETCH_OP |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 134 | #undef ATOMIC64_OP_RETURN |
| 135 | #undef ATOMIC64_OP |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 136 | |
| 137 | long long atomic64_dec_if_positive(atomic64_t *v) |
| 138 | { |
| 139 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 140 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 141 | long long val; |
| 142 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 143 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 144 | val = v->counter - 1; |
| 145 | if (val >= 0) |
| 146 | v->counter = val; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 147 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 148 | return val; |
| 149 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 150 | EXPORT_SYMBOL(atomic64_dec_if_positive); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 151 | |
| 152 | long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n) |
| 153 | { |
| 154 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 155 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 156 | long long val; |
| 157 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 158 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 159 | val = v->counter; |
| 160 | if (val == o) |
| 161 | v->counter = n; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 162 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 163 | return val; |
| 164 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 165 | EXPORT_SYMBOL(atomic64_cmpxchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 166 | |
| 167 | long long atomic64_xchg(atomic64_t *v, long long new) |
| 168 | { |
| 169 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 170 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 171 | long long val; |
| 172 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 173 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 174 | val = v->counter; |
| 175 | v->counter = new; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 176 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 177 | return val; |
| 178 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 179 | EXPORT_SYMBOL(atomic64_xchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 180 | |
| 181 | int atomic64_add_unless(atomic64_t *v, long long a, long long u) |
| 182 | { |
| 183 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 184 | raw_spinlock_t *lock = lock_addr(v); |
Luca Barbieri | 9757789 | 2010-03-01 19:55:47 +0100 | [diff] [blame] | 185 | int ret = 0; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 186 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 187 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 188 | if (v->counter != u) { |
| 189 | v->counter += a; |
Luca Barbieri | 9757789 | 2010-03-01 19:55:47 +0100 | [diff] [blame] | 190 | ret = 1; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 191 | } |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 192 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 193 | return ret; |
| 194 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 195 | EXPORT_SYMBOL(atomic64_add_unless); |