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