blob: 978537809d84c4c18c469e1a38005872cdf165f1 [file] [log] [blame]
Paul Mackerras09d4e0e2009-06-12 21:10:05 +00001/*
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 Gortmaker8bc3bcc2011-11-16 21:29:17 -050016#include <linux/export.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000018
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 */
31static union {
Shan Haif59ca052011-09-01 11:32:03 +080032 raw_spinlock_t lock;
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000033 char pad[L1_CACHE_BYTES];
34} atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp;
35
Yong Zhangcb475de2011-09-14 15:49:24 +080036static inline raw_spinlock_t *lock_addr(const atomic64_t *v)
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000037{
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
45long long atomic64_read(const atomic64_t *v)
46{
47 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080048 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000049 long long val;
50
Shan Haif59ca052011-09-01 11:32:03 +080051 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000052 val = v->counter;
Shan Haif59ca052011-09-01 11:32:03 +080053 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000054 return val;
55}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070056EXPORT_SYMBOL(atomic64_read);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000057
58void atomic64_set(atomic64_t *v, long long i)
59{
60 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080061 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000062
Shan Haif59ca052011-09-01 11:32:03 +080063 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000064 v->counter = i;
Shan Haif59ca052011-09-01 11:32:03 +080065 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000066}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070067EXPORT_SYMBOL(atomic64_set);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000068
69void atomic64_add(long long a, atomic64_t *v)
70{
71 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080072 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000073
Shan Haif59ca052011-09-01 11:32:03 +080074 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000075 v->counter += a;
Shan Haif59ca052011-09-01 11:32:03 +080076 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000077}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070078EXPORT_SYMBOL(atomic64_add);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000079
80long long atomic64_add_return(long long a, atomic64_t *v)
81{
82 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080083 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000084 long long val;
85
Shan Haif59ca052011-09-01 11:32:03 +080086 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000087 val = v->counter += a;
Shan Haif59ca052011-09-01 11:32:03 +080088 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000089 return val;
90}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070091EXPORT_SYMBOL(atomic64_add_return);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000092
93void atomic64_sub(long long a, atomic64_t *v)
94{
95 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080096 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000097
Shan Haif59ca052011-09-01 11:32:03 +080098 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000099 v->counter -= a;
Shan Haif59ca052011-09-01 11:32:03 +0800100 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000101}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700102EXPORT_SYMBOL(atomic64_sub);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000103
104long long atomic64_sub_return(long long a, atomic64_t *v)
105{
106 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800107 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000108 long long val;
109
Shan Haif59ca052011-09-01 11:32:03 +0800110 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000111 val = v->counter -= a;
Shan Haif59ca052011-09-01 11:32:03 +0800112 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000113 return val;
114}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700115EXPORT_SYMBOL(atomic64_sub_return);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000116
117long long atomic64_dec_if_positive(atomic64_t *v)
118{
119 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800120 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000121 long long val;
122
Shan Haif59ca052011-09-01 11:32:03 +0800123 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000124 val = v->counter - 1;
125 if (val >= 0)
126 v->counter = val;
Shan Haif59ca052011-09-01 11:32:03 +0800127 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000128 return val;
129}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700130EXPORT_SYMBOL(atomic64_dec_if_positive);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000131
132long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
133{
134 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800135 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000136 long long val;
137
Shan Haif59ca052011-09-01 11:32:03 +0800138 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000139 val = v->counter;
140 if (val == o)
141 v->counter = n;
Shan Haif59ca052011-09-01 11:32:03 +0800142 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000143 return val;
144}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700145EXPORT_SYMBOL(atomic64_cmpxchg);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000146
147long long atomic64_xchg(atomic64_t *v, long long new)
148{
149 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800150 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000151 long long val;
152
Shan Haif59ca052011-09-01 11:32:03 +0800153 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000154 val = v->counter;
155 v->counter = new;
Shan Haif59ca052011-09-01 11:32:03 +0800156 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000157 return val;
158}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700159EXPORT_SYMBOL(atomic64_xchg);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000160
161int atomic64_add_unless(atomic64_t *v, long long a, long long u)
162{
163 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800164 raw_spinlock_t *lock = lock_addr(v);
Luca Barbieri97577892010-03-01 19:55:47 +0100165 int ret = 0;
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000166
Shan Haif59ca052011-09-01 11:32:03 +0800167 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000168 if (v->counter != u) {
169 v->counter += a;
Luca Barbieri97577892010-03-01 19:55:47 +0100170 ret = 1;
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000171 }
Shan Haif59ca052011-09-01 11:32:03 +0800172 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000173 return ret;
174}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700175EXPORT_SYMBOL(atomic64_add_unless);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000176
177static int init_atomic64_lock(void)
178{
179 int i;
180
181 for (i = 0; i < NR_LOCKS; ++i)
Shan Haif59ca052011-09-01 11:32:03 +0800182 raw_spin_lock_init(&atomic64_lock[i].lock);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000183 return 0;
184}
185
186pure_initcall(init_atomic64_lock);