2 * Generic implementation of 64-bit atomics using spinlocks,
3 * useful on processors that don't have 64-bit atomic instructions.
5 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
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.
12 #include <linux/types.h>
13 #include <linux/cache.h>
14 #include <linux/spinlock.h>
15 #include <linux/init.h>
16 #include <asm/atomic.h>
19 * We use a hashed array of spinlocks to provide exclusive access
20 * to each atomic64_t variable. Since this is expected to used on
21 * systems with small numbers of CPUs (<= 4 or so), we use a
22 * relatively small array of 16 spinlocks to avoid wasting too much
23 * memory on the spinlock array.
28 * Ensure each lock is in a separate cacheline.
32 char pad[L1_CACHE_BYTES];
33 } atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp;
35 static inline spinlock_t *lock_addr(const atomic64_t *v)
37 unsigned long addr = (unsigned long) v;
39 addr >>= L1_CACHE_SHIFT;
40 addr ^= (addr >> 8) ^ (addr >> 16);
41 return &atomic64_lock[addr & (NR_LOCKS - 1)].lock;
44 long long atomic64_read(const atomic64_t *v)
47 spinlock_t *lock = lock_addr(v);
50 spin_lock_irqsave(lock, flags);
52 spin_unlock_irqrestore(lock, flags);
56 void atomic64_set(atomic64_t *v, long long i)
59 spinlock_t *lock = lock_addr(v);
61 spin_lock_irqsave(lock, flags);
63 spin_unlock_irqrestore(lock, flags);
66 void atomic64_add(long long a, atomic64_t *v)
69 spinlock_t *lock = lock_addr(v);
71 spin_lock_irqsave(lock, flags);
73 spin_unlock_irqrestore(lock, flags);
76 long long atomic64_add_return(long long a, atomic64_t *v)
79 spinlock_t *lock = lock_addr(v);
82 spin_lock_irqsave(lock, flags);
83 val = v->counter += a;
84 spin_unlock_irqrestore(lock, flags);
88 void atomic64_sub(long long a, atomic64_t *v)
91 spinlock_t *lock = lock_addr(v);
93 spin_lock_irqsave(lock, flags);
95 spin_unlock_irqrestore(lock, flags);
98 long long atomic64_sub_return(long long a, atomic64_t *v)
101 spinlock_t *lock = lock_addr(v);
104 spin_lock_irqsave(lock, flags);
105 val = v->counter -= a;
106 spin_unlock_irqrestore(lock, flags);
110 long long atomic64_dec_if_positive(atomic64_t *v)
113 spinlock_t *lock = lock_addr(v);
116 spin_lock_irqsave(lock, flags);
117 val = v->counter - 1;
120 spin_unlock_irqrestore(lock, flags);
124 long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
127 spinlock_t *lock = lock_addr(v);
130 spin_lock_irqsave(lock, flags);
134 spin_unlock_irqrestore(lock, flags);
138 long long atomic64_xchg(atomic64_t *v, long long new)
141 spinlock_t *lock = lock_addr(v);
144 spin_lock_irqsave(lock, flags);
147 spin_unlock_irqrestore(lock, flags);
151 int atomic64_add_unless(atomic64_t *v, long long a, long long u)
154 spinlock_t *lock = lock_addr(v);
157 spin_lock_irqsave(lock, flags);
158 if (v->counter != u) {
162 spin_unlock_irqrestore(lock, flags);
166 static int init_atomic64_lock(void)
170 for (i = 0; i < NR_LOCKS; ++i)
171 spin_lock_init(&atomic64_lock[i].lock);
175 pure_initcall(init_atomic64_lock);