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 | 6006349 | 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]; |
| 34 | } atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp; |
| 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 | |
| 45 | long long atomic64_read(const atomic64_t *v) |
| 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); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 49 | long long val; |
| 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 | |
| 58 | void atomic64_set(atomic64_t *v, long long i) |
| 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 | |
| 69 | void atomic64_add(long long a, atomic64_t *v) |
| 70 | { |
| 71 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 72 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 73 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 74 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 75 | v->counter += a; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 76 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 77 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 78 | EXPORT_SYMBOL(atomic64_add); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 79 | |
| 80 | long long atomic64_add_return(long long a, atomic64_t *v) |
| 81 | { |
| 82 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 83 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 84 | long long val; |
| 85 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 86 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 87 | val = v->counter += a; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 88 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 89 | return val; |
| 90 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 91 | EXPORT_SYMBOL(atomic64_add_return); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 92 | |
| 93 | void atomic64_sub(long long a, atomic64_t *v) |
| 94 | { |
| 95 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 96 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 97 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 98 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 99 | v->counter -= a; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 100 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 101 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 102 | EXPORT_SYMBOL(atomic64_sub); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 103 | |
| 104 | long long atomic64_sub_return(long long a, atomic64_t *v) |
| 105 | { |
| 106 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 107 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 108 | long long val; |
| 109 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 110 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 111 | val = v->counter -= a; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 112 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 113 | return val; |
| 114 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 115 | EXPORT_SYMBOL(atomic64_sub_return); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 116 | |
| 117 | long long atomic64_dec_if_positive(atomic64_t *v) |
| 118 | { |
| 119 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 120 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 121 | long long val; |
| 122 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 123 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 124 | val = v->counter - 1; |
| 125 | if (val >= 0) |
| 126 | v->counter = val; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 127 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 128 | return val; |
| 129 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 130 | EXPORT_SYMBOL(atomic64_dec_if_positive); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 131 | |
| 132 | long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n) |
| 133 | { |
| 134 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 135 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 136 | long long val; |
| 137 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 138 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 139 | val = v->counter; |
| 140 | if (val == o) |
| 141 | v->counter = n; |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 142 | raw_spin_unlock_irqrestore(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 143 | return val; |
| 144 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 145 | EXPORT_SYMBOL(atomic64_cmpxchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 146 | |
| 147 | long long atomic64_xchg(atomic64_t *v, long long new) |
| 148 | { |
| 149 | unsigned long flags; |
Yong Zhang | cb475de | 2011-09-14 15:49:24 +0800 | [diff] [blame] | 150 | raw_spinlock_t *lock = lock_addr(v); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 151 | long long val; |
| 152 | |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 153 | raw_spin_lock_irqsave(lock, flags); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 154 | val = v->counter; |
| 155 | v->counter = new; |
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 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 159 | EXPORT_SYMBOL(atomic64_xchg); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 160 | |
| 161 | int atomic64_add_unless(atomic64_t *v, long long a, long long u) |
| 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); |
Luca Barbieri | 9757789 | 2010-03-01 19:55:47 +0100 | [diff] [blame] | 165 | int ret = 0; |
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 | if (v->counter != u) { |
| 169 | v->counter += a; |
Luca Barbieri | 9757789 | 2010-03-01 19:55:47 +0100 | [diff] [blame] | 170 | ret = 1; |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 171 | } |
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 ret; |
| 174 | } |
Roland Dreier | 3fc7b4b | 2009-07-29 15:04:02 -0700 | [diff] [blame] | 175 | EXPORT_SYMBOL(atomic64_add_unless); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 176 | |
| 177 | static int init_atomic64_lock(void) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < NR_LOCKS; ++i) |
Shan Hai | f59ca05 | 2011-09-01 11:32:03 +0800 | [diff] [blame] | 182 | raw_spin_lock_init(&atomic64_lock[i].lock); |
Paul Mackerras | 09d4e0e | 2009-06-12 21:10:05 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | pure_initcall(init_atomic64_lock); |