Merge branch 'linus' into perf/core, to fix conflicts
[deliverable/linux.git] / arch / x86 / include / asm / spinlock.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_SPINLOCK_H
2#define _ASM_X86_SPINLOCK_H
2fed0c50 3
96f853ea 4#include <linux/jump_label.h>
60063497 5#include <linux/atomic.h>
1075cf7a
TG
6#include <asm/page.h>
7#include <asm/processor.h>
314cdbef 8#include <linux/compiler.h>
74d4affd 9#include <asm/paravirt.h>
96f853ea
JF
10#include <asm/bitops.h>
11
1075cf7a
TG
12/*
13 * Your basic SMP spinlocks, allowing only a single CPU anywhere
14 *
15 * Simple spin lock operations. There are two variants, one clears IRQ's
16 * on the local processor, one does not.
17 *
83be4ffa 18 * These are fair FIFO ticket locks, which support up to 2^16 CPUs.
1075cf7a
TG
19 *
20 * (the type definitions are in asm/spinlock_types.h)
21 */
22
96a388de 23#ifdef CONFIG_X86_32
1075cf7a 24# define LOCK_PTR_REG "a"
96a388de 25#else
1075cf7a
TG
26# define LOCK_PTR_REG "D"
27#endif
28
09df7c4c 29#if defined(CONFIG_X86_32) && (defined(CONFIG_X86_PPRO_FENCE))
3a556b26 30/*
09df7c4c 31 * On PPro SMP, we use a locked operation to unlock
3a556b26
NP
32 * (PPro errata 66, 92)
33 */
34# define UNLOCK_LOCK_PREFIX LOCK_PREFIX
35#else
36# define UNLOCK_LOCK_PREFIX
314cdbef
NP
37#endif
38
545ac138
JF
39/* How long a lock should spin before we consider blocking */
40#define SPIN_THRESHOLD (1 << 15)
41
96f853ea
JF
42extern struct static_key paravirt_ticketlocks_enabled;
43static __always_inline bool static_key_false(struct static_key *key);
545ac138 44
96f853ea
JF
45#ifdef CONFIG_PARAVIRT_SPINLOCKS
46
47static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
545ac138 48{
96f853ea 49 set_bit(0, (volatile unsigned long *)&lock->tickets.tail);
545ac138
JF
50}
51
96f853ea
JF
52#else /* !CONFIG_PARAVIRT_SPINLOCKS */
53static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
54 __ticket_t ticket)
545ac138
JF
55{
56}
96f853ea
JF
57static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
58 __ticket_t ticket)
545ac138 59{
545ac138
JF
60}
61
96f853ea
JF
62#endif /* CONFIG_PARAVIRT_SPINLOCKS */
63
bc08b449
LT
64static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
65{
66 return lock.tickets.head == lock.tickets.tail;
67}
68
3a556b26
NP
69/*
70 * Ticket locks are conceptually two parts, one indicating the current head of
71 * the queue, and the other indicating the current tail. The lock is acquired
72 * by atomically noting the tail and incrementing it by one (thus adding
73 * ourself to the queue and noting our position), then waiting until the head
74 * becomes equal to the the initial value of the tail.
75 *
76 * We use an xadd covering *both* parts of the lock, to increment the tail and
77 * also load the position of the head, which takes care of memory ordering
78 * issues and should be optimal for the uncontended case. Note the tail must be
79 * in the high part, because a wide xadd increment of the low part would carry
80 * up and contaminate the high part.
3a556b26 81 */
96f853ea 82static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
1075cf7a 83{
4a1ed4ca 84 register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
314cdbef 85
2994488f 86 inc = xadd(&lock->tickets, inc);
96f853ea
JF
87 if (likely(inc.head == inc.tail))
88 goto out;
c576a3ea 89
96f853ea 90 inc.tail &= ~TICKET_SLOWPATH_FLAG;
c576a3ea 91 for (;;) {
545ac138
JF
92 unsigned count = SPIN_THRESHOLD;
93
94 do {
96f853ea 95 if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
545ac138
JF
96 goto out;
97 cpu_relax();
545ac138
JF
98 } while (--count);
99 __ticket_lock_spinning(lock, inc.tail);
c576a3ea 100 }
545ac138 101out: barrier(); /* make sure nothing creeps before the lock is taken */
1075cf7a 102}
314cdbef 103
b798df09 104static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
1075cf7a 105{
229855d6
JF
106 arch_spinlock_t old, new;
107
108 old.tickets = ACCESS_ONCE(lock->tickets);
96f853ea 109 if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
229855d6 110 return 0;
314cdbef 111
4a1ed4ca 112 new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
229855d6
JF
113
114 /* cmpxchg is a full barrier, so nothing can move before it */
115 return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
1075cf7a
TG
116}
117
96f853ea
JF
118static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
119 arch_spinlock_t old)
3a556b26 120{
96f853ea
JF
121 arch_spinlock_t new;
122
123 BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
124
125 /* Perform the unlock on the "before" copy */
126 old.tickets.head += TICKET_LOCK_INC;
127
128 /* Clear the slowpath flag */
129 new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
130
131 /*
132 * If the lock is uncontended, clear the flag - use cmpxchg in
133 * case it changes behind our back though.
134 */
135 if (new.tickets.head != new.tickets.tail ||
136 cmpxchg(&lock->head_tail, old.head_tail,
137 new.head_tail) != old.head_tail) {
138 /*
139 * Lock still has someone queued for it, so wake up an
140 * appropriate waiter.
141 */
142 __ticket_unlock_kick(lock, old.tickets.head);
143 }
3a556b26 144}
1075cf7a 145
b798df09 146static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
08f5fcbe 147{
96f853ea
JF
148 if (TICKET_SLOWPATH_FLAG &&
149 static_key_false(&paravirt_ticketlocks_enabled)) {
150 arch_spinlock_t prev;
08f5fcbe 151
96f853ea
JF
152 prev = *lock;
153 add_smp(&lock->tickets.head, TICKET_LOCK_INC);
08f5fcbe 154
96f853ea 155 /* add_smp() is a full mb() */
08f5fcbe 156
96f853ea
JF
157 if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
158 __ticket_unlock_slowpath(lock, prev);
159 } else
160 __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
08f5fcbe 161}
74d4affd 162
0199c4e6 163static inline int arch_spin_is_locked(arch_spinlock_t *lock)
74d4affd 164{
84eb950d 165 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
74d4affd 166
7931d493 167 return tmp.tail != tmp.head;
74d4affd
JF
168}
169
b798df09 170static inline int arch_spin_is_contended(arch_spinlock_t *lock)
74d4affd 171{
84eb950d 172 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
74d4affd 173
4a1ed4ca 174 return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
74d4affd 175}
0199c4e6 176#define arch_spin_is_contended arch_spin_is_contended
63d3a75d 177
0199c4e6 178static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
63d3a75d
JF
179 unsigned long flags)
180{
0199c4e6 181 arch_spin_lock(lock);
63d3a75d
JF
182}
183
0199c4e6 184static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
1075cf7a 185{
0199c4e6 186 while (arch_spin_is_locked(lock))
1075cf7a
TG
187 cpu_relax();
188}
189
bd01ec1a 190#ifndef CONFIG_QUEUE_RWLOCK
1075cf7a
TG
191/*
192 * Read-write spinlocks, allowing multiple readers
193 * but only one writer.
194 *
195 * NOTE! it is quite common to have readers in interrupts
196 * but no interrupt writers. For those circumstances we
197 * can "mix" irq-safe locks - any writer needs to get a
198 * irq-safe write-lock, but readers can get non-irqsafe
199 * read-locks.
200 *
201 * On x86, we implement read-write locks as a 32-bit counter
202 * with the high bit (sign) being the "contended" bit.
203 */
204
314cdbef
NP
205/**
206 * read_can_lock - would read_trylock() succeed?
207 * @lock: the rwlock in question.
208 */
e5931943 209static inline int arch_read_can_lock(arch_rwlock_t *lock)
1075cf7a 210{
a750036f 211 return lock->lock > 0;
1075cf7a
TG
212}
213
314cdbef
NP
214/**
215 * write_can_lock - would write_trylock() succeed?
216 * @lock: the rwlock in question.
217 */
e5931943 218static inline int arch_write_can_lock(arch_rwlock_t *lock)
1075cf7a 219{
a750036f 220 return lock->write == WRITE_LOCK_CMP;
1075cf7a
TG
221}
222
e5931943 223static inline void arch_read_lock(arch_rwlock_t *rw)
1075cf7a 224{
a750036f 225 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
1075cf7a
TG
226 "jns 1f\n"
227 "call __read_lock_failed\n\t"
228 "1:\n"
229 ::LOCK_PTR_REG (rw) : "memory");
230}
231
e5931943 232static inline void arch_write_lock(arch_rwlock_t *rw)
1075cf7a 233{
a750036f 234 asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
1075cf7a
TG
235 "jz 1f\n"
236 "call __write_lock_failed\n\t"
237 "1:\n"
a750036f
JB
238 ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
239 : "memory");
1075cf7a
TG
240}
241
e5931943 242static inline int arch_read_trylock(arch_rwlock_t *lock)
1075cf7a 243{
a750036f 244 READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
1075cf7a 245
a750036f 246 if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
1075cf7a 247 return 1;
a750036f 248 READ_LOCK_ATOMIC(inc)(count);
1075cf7a
TG
249 return 0;
250}
251
e5931943 252static inline int arch_write_trylock(arch_rwlock_t *lock)
1075cf7a 253{
a750036f 254 atomic_t *count = (atomic_t *)&lock->write;
1075cf7a 255
a750036f 256 if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
1075cf7a 257 return 1;
a750036f 258 atomic_add(WRITE_LOCK_CMP, count);
1075cf7a
TG
259 return 0;
260}
261
e5931943 262static inline void arch_read_unlock(arch_rwlock_t *rw)
1075cf7a 263{
a750036f
JB
264 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
265 :"+m" (rw->lock) : : "memory");
1075cf7a
TG
266}
267
e5931943 268static inline void arch_write_unlock(arch_rwlock_t *rw)
1075cf7a 269{
a750036f
JB
270 asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
271 : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
1075cf7a 272}
bd01ec1a
WL
273#else
274#include <asm/qrwlock.h>
275#endif /* CONFIG_QUEUE_RWLOCK */
1075cf7a 276
e5931943
TG
277#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
278#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
f5f7eac4 279
a750036f
JB
280#undef READ_LOCK_SIZE
281#undef READ_LOCK_ATOMIC
282#undef WRITE_LOCK_ADD
283#undef WRITE_LOCK_SUB
284#undef WRITE_LOCK_CMP
285
0199c4e6
TG
286#define arch_spin_relax(lock) cpu_relax()
287#define arch_read_relax(lock) cpu_relax()
288#define arch_write_relax(lock) cpu_relax()
1075cf7a 289
1965aae3 290#endif /* _ASM_X86_SPINLOCK_H */
This page took 0.570825 seconds and 5 git commands to generate.