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 | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 45 | s64 generic_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 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 56 | EXPORT_SYMBOL(generic_atomic64_read); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 57 | |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 58 | void generic_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 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 67 | EXPORT_SYMBOL(generic_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 | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 70 | void generic_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 | } \ |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 79 | EXPORT_SYMBOL(generic_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 | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 82 | s64 generic_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 | } \ |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 93 | EXPORT_SYMBOL(generic_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 | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 96 | s64 generic_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 | } \ |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 108 | EXPORT_SYMBOL(generic_atomic64_fetch_##op); |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 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) \ |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 121 | ATOMIC64_FETCH_OP(op, c_op) |
| 122 | |
| 123 | ATOMIC64_OPS(and, &=) |
| 124 | ATOMIC64_OPS(or, |=) |
| 125 | ATOMIC64_OPS(xor, ^=) |
| 126 | |
| 127 | #undef ATOMIC64_OPS |
| 128 | #undef ATOMIC64_FETCH_OP |
Peter Zijlstra | 560cb12 | 2014-04-23 16:12:30 +0200 | [diff] [blame] | 129 | #undef ATOMIC64_OP |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 130 | |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 131 | s64 generic_atomic64_dec_if_positive(atomic64_t *v) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 132 | { |
| 133 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 134 | raw_spinlock_t *lock = lock_addr(v); |
Mark Rutland | 9255813 | 2019-05-22 14:22:35 +0100 | [diff] [blame] | 135 | s64 val; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 136 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 137 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 138 | val = v->counter - 1; |
| 139 | if (val >= 0) |
| 140 | v->counter = val; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 141 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 142 | return val; |
| 143 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 144 | EXPORT_SYMBOL(generic_atomic64_dec_if_positive); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 145 | |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 146 | s64 generic_atomic64_cmpxchg(atomic64_t *v, s64 o, s64 n) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 147 | { |
| 148 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 149 | raw_spinlock_t *lock = lock_addr(v); |
Mark Rutland | 9255813 | 2019-05-22 14:22:35 +0100 | [diff] [blame] | 150 | s64 val; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 151 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 152 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 153 | val = v->counter; |
| 154 | if (val == o) |
| 155 | v->counter = n; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 156 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 157 | return val; |
| 158 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 159 | EXPORT_SYMBOL(generic_atomic64_cmpxchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 160 | |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 161 | s64 generic_atomic64_xchg(atomic64_t *v, s64 new) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 162 | { |
| 163 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 164 | raw_spinlock_t *lock = lock_addr(v); |
Mark Rutland | 9255813 | 2019-05-22 14:22:35 +0100 | [diff] [blame] | 165 | s64 val; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 166 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 167 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 168 | val = v->counter; |
| 169 | v->counter = new; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 170 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 171 | return val; |
| 172 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 173 | EXPORT_SYMBOL(generic_atomic64_xchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 174 | |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 175 | s64 generic_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 176 | { |
| 177 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 178 | raw_spinlock_t *lock = lock_addr(v); |
Mark Rutland | 9255813 | 2019-05-22 14:22:35 +0100 | [diff] [blame] | 179 | s64 val; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 180 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 181 | raw_spin_lock_irqsave(lock, flags); |
Mark Rutland | 00b808a | 2018-06-21 13:13:11 +0100 | [diff] [blame] | 182 | val = v->counter; |
| 183 | if (val != u) |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 184 | v->counter += a; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 185 | raw_spin_unlock_irqrestore(lock, flags); |
Mark Rutland | 00b808a | 2018-06-21 13:13:11 +0100 | [diff] [blame] | 186 | |
| 187 | return val; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 188 | } |
Mark Rutland | 1bdadf4 | 2021-05-25 15:02:09 +0100 | [diff] [blame] | 189 | EXPORT_SYMBOL(generic_atomic64_fetch_add_unless); |