blob: 08a4f068e61e7689f4246ec77683d71b18f72a76 [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];
Stephen Boydfcc16882012-12-19 23:39:48 -080034} atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp = {
35 [0 ... (NR_LOCKS - 1)] = {
36 .lock = __RAW_SPIN_LOCK_UNLOCKED(atomic64_lock.lock),
37 },
38};
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000039
Yong Zhangcb475de2011-09-14 15:49:24 +080040static inline raw_spinlock_t *lock_addr(const atomic64_t *v)
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000041{
42 unsigned long addr = (unsigned long) v;
43
44 addr >>= L1_CACHE_SHIFT;
45 addr ^= (addr >> 8) ^ (addr >> 16);
46 return &atomic64_lock[addr & (NR_LOCKS - 1)].lock;
47}
48
49long long atomic64_read(const atomic64_t *v)
50{
51 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080052 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000053 long long val;
54
Shan Haif59ca052011-09-01 11:32:03 +080055 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000056 val = v->counter;
Shan Haif59ca052011-09-01 11:32:03 +080057 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000058 return val;
59}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070060EXPORT_SYMBOL(atomic64_read);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000061
62void atomic64_set(atomic64_t *v, long long i)
63{
64 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080065 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000066
Shan Haif59ca052011-09-01 11:32:03 +080067 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000068 v->counter = i;
Shan Haif59ca052011-09-01 11:32:03 +080069 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000070}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070071EXPORT_SYMBOL(atomic64_set);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000072
73void atomic64_add(long long a, atomic64_t *v)
74{
75 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080076 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000077
Shan Haif59ca052011-09-01 11:32:03 +080078 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000079 v->counter += a;
Shan Haif59ca052011-09-01 11:32:03 +080080 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000081}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070082EXPORT_SYMBOL(atomic64_add);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000083
84long long atomic64_add_return(long long a, atomic64_t *v)
85{
86 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +080087 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000088 long long val;
89
Shan Haif59ca052011-09-01 11:32:03 +080090 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000091 val = v->counter += a;
Shan Haif59ca052011-09-01 11:32:03 +080092 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000093 return val;
94}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -070095EXPORT_SYMBOL(atomic64_add_return);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +000096
97void atomic64_sub(long long a, atomic64_t *v)
98{
99 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800100 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000101
Shan Haif59ca052011-09-01 11:32:03 +0800102 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000103 v->counter -= a;
Shan Haif59ca052011-09-01 11:32:03 +0800104 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000105}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700106EXPORT_SYMBOL(atomic64_sub);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000107
108long long atomic64_sub_return(long long a, atomic64_t *v)
109{
110 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800111 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000112 long long val;
113
Shan Haif59ca052011-09-01 11:32:03 +0800114 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000115 val = v->counter -= a;
Shan Haif59ca052011-09-01 11:32:03 +0800116 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000117 return val;
118}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700119EXPORT_SYMBOL(atomic64_sub_return);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000120
121long long atomic64_dec_if_positive(atomic64_t *v)
122{
123 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800124 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000125 long long val;
126
Shan Haif59ca052011-09-01 11:32:03 +0800127 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000128 val = v->counter - 1;
129 if (val >= 0)
130 v->counter = val;
Shan Haif59ca052011-09-01 11:32:03 +0800131 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000132 return val;
133}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700134EXPORT_SYMBOL(atomic64_dec_if_positive);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000135
136long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
137{
138 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800139 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000140 long long val;
141
Shan Haif59ca052011-09-01 11:32:03 +0800142 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000143 val = v->counter;
144 if (val == o)
145 v->counter = n;
Shan Haif59ca052011-09-01 11:32:03 +0800146 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000147 return val;
148}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700149EXPORT_SYMBOL(atomic64_cmpxchg);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000150
151long long atomic64_xchg(atomic64_t *v, long long new)
152{
153 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800154 raw_spinlock_t *lock = lock_addr(v);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000155 long long val;
156
Shan Haif59ca052011-09-01 11:32:03 +0800157 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000158 val = v->counter;
159 v->counter = new;
Shan Haif59ca052011-09-01 11:32:03 +0800160 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000161 return val;
162}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700163EXPORT_SYMBOL(atomic64_xchg);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000164
165int atomic64_add_unless(atomic64_t *v, long long a, long long u)
166{
167 unsigned long flags;
Yong Zhangcb475de2011-09-14 15:49:24 +0800168 raw_spinlock_t *lock = lock_addr(v);
Luca Barbieri97577892010-03-01 19:55:47 +0100169 int ret = 0;
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000170
Shan Haif59ca052011-09-01 11:32:03 +0800171 raw_spin_lock_irqsave(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000172 if (v->counter != u) {
173 v->counter += a;
Luca Barbieri97577892010-03-01 19:55:47 +0100174 ret = 1;
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000175 }
Shan Haif59ca052011-09-01 11:32:03 +0800176 raw_spin_unlock_irqrestore(lock, flags);
Paul Mackerras09d4e0e2009-06-12 21:10:05 +0000177 return ret;
178}
Roland Dreier3fc7b4b2009-07-29 15:04:02 -0700179EXPORT_SYMBOL(atomic64_add_unless);