blob: f9611bd69ed2a102b38e67da0451be7694c70a5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SPINLOCK_H
2#define __ASM_SPINLOCK_H
Arnd Bergmann88ced032005-12-16 22:43:46 +01003#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5/*
6 * Simple spin lock operations.
7 *
8 * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
11 * Rework to support virtual processors
12 *
13 * Type of int is used as a full 64b word is not necessary.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070019 *
20 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100022#include <linux/irqflags.h>
Paul Mackerras0212ddd2005-11-19 20:50:46 +110023#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/paca.h>
25#include <asm/hvcall.h>
Kelly Daly1da44032005-11-01 16:59:20 +110026#include <asm/iseries/hv_call.h>
Paul Mackerras0212ddd2005-11-19 20:50:46 +110027#endif
28#include <asm/asm-compat.h>
29#include <asm/synch.h>
Anton Blanchard4e14a4d2010-02-10 00:57:28 +000030#include <asm/ppc-opcode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010032#define arch_spin_is_locked(x) ((x)->slock != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Paul Mackerras0212ddd2005-11-19 20:50:46 +110034#ifdef CONFIG_PPC64
35/* use 0x800000yy when locked, where yy == CPU number */
36#define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
37#else
38#define LOCK_TOKEN 1
39#endif
40
Paul Mackerrasf007cac2006-09-13 22:08:26 +100041#if defined(CONFIG_PPC64) && defined(CONFIG_SMP)
42#define CLEAR_IO_SYNC (get_paca()->io_sync = 0)
43#define SYNC_IO do { \
44 if (unlikely(get_paca()->io_sync)) { \
45 mb(); \
46 get_paca()->io_sync = 0; \
47 } \
48 } while (0)
49#else
50#define CLEAR_IO_SYNC
51#define SYNC_IO
52#endif
53
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070054/*
55 * This returns the old value in the lock, so we succeeded
56 * in getting the lock if the return value is 0.
57 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010058static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Paul Mackerras0212ddd2005-11-19 20:50:46 +110060 unsigned long tmp, token;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070061
Paul Mackerras0212ddd2005-11-19 20:50:46 +110062 token = LOCK_TOKEN;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070063 __asm__ __volatile__(
Anton Blanchard4e14a4d2010-02-10 00:57:28 +000064"1: " PPC_LWARX(%0,0,%2,1) "\n\
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070065 cmpwi 0,%0,0\n\
66 bne- 2f\n\
67 stwcx. %1,0,%2\n\
Anton Blanchardf10e2e52010-02-10 01:04:06 +000068 bne- 1b\n"
69 PPC_ACQUIRE_BARRIER
70"2:"
71 : "=&r" (tmp)
Paul Mackerras0212ddd2005-11-19 20:50:46 +110072 : "r" (token), "r" (&lock->slock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070073 : "cr0", "memory");
74
75 return tmp;
76}
77
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010078static inline int arch_spin_trylock(arch_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070079{
Paul Mackerrasf007cac2006-09-13 22:08:26 +100080 CLEAR_IO_SYNC;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010081 return __arch_spin_trylock(lock) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*
85 * On a system with shared processors (that is, where a physical
86 * processor is multiplexed between several virtual processors),
87 * there is no point spinning on a lock if the holder of the lock
88 * isn't currently scheduled on a physical processor. Instead
89 * we detect this situation and ask the hypervisor to give the
90 * rest of our timeslice to the lock holder.
91 *
92 * So that we can tell which virtual processor is holding a lock,
93 * we put 0x80000000 | smp_processor_id() in the lock when it is
94 * held. Conveniently, we have a word in the paca that holds this
95 * value.
96 */
97
98#if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
99/* We only yield to the hypervisor if we are in shared processor mode */
David Gibson3356bb9f72006-01-13 10:26:42 +1100100#define SHARED_PROCESSOR (get_lppaca()->shared_proc)
Thomas Gleixner445c8952009-12-02 19:49:50 +0100101extern void __spin_yield(arch_spinlock_t *lock);
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100102extern void __rw_yield(arch_rwlock_t *lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#else /* SPLPAR || ISERIES */
104#define __spin_yield(x) barrier()
105#define __rw_yield(x) barrier()
106#define SHARED_PROCESSOR 0
107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100109static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000111 CLEAR_IO_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 while (1) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100113 if (likely(__arch_spin_trylock(lock) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115 do {
116 HMT_low();
117 if (SHARED_PROCESSOR)
118 __spin_yield(lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700119 } while (unlikely(lock->slock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 HMT_medium();
121 }
122}
123
Bart Van Assche89b58102008-06-28 16:51:35 +1000124static inline
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100125void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned long flags_dis;
128
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000129 CLEAR_IO_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 while (1) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100131 if (likely(__arch_spin_trylock(lock) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
133 local_save_flags(flags_dis);
134 local_irq_restore(flags);
135 do {
136 HMT_low();
137 if (SHARED_PROCESSOR)
138 __spin_yield(lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700139 } while (unlikely(lock->slock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 HMT_medium();
141 local_irq_restore(flags_dis);
142 }
143}
144
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100145static inline void arch_spin_unlock(arch_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700146{
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000147 SYNC_IO;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100148 __asm__ __volatile__("# arch_spin_unlock\n\t"
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000149 PPC_RELEASE_BARRIER: : :"memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700150 lock->slock = 0;
151}
152
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100153#ifdef CONFIG_PPC64
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100154extern void arch_spin_unlock_wait(arch_spinlock_t *lock);
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100155#else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100156#define arch_spin_unlock_wait(lock) \
157 do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100158#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Read-write spinlocks, allowing multiple readers
162 * but only one writer.
163 *
164 * NOTE! it is quite common to have readers in interrupts
165 * but no interrupt writers. For those circumstances we
166 * can "mix" irq-safe locks - any writer needs to get a
167 * irq-safe write-lock, but readers can get non-irqsafe
168 * read-locks.
169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Thomas Gleixnere5931942009-12-03 20:08:46 +0100171#define arch_read_can_lock(rw) ((rw)->lock >= 0)
172#define arch_write_can_lock(rw) (!(rw)->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100174#ifdef CONFIG_PPC64
175#define __DO_SIGN_EXTEND "extsw %0,%0\n"
176#define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
177#else
178#define __DO_SIGN_EXTEND
179#define WRLOCK_TOKEN (-1)
180#endif
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * This returns the old value in the lock + 1,
184 * so we got a read lock if the return value is > 0.
185 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100186static inline long __arch_read_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 long tmp;
189
190 __asm__ __volatile__(
Anton Blanchard4e14a4d2010-02-10 00:57:28 +0000191"1: " PPC_LWARX(%0,0,%1,1) "\n"
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100192 __DO_SIGN_EXTEND
193" addic. %0,%0,1\n\
194 ble- 2f\n"
195 PPC405_ERR77(0,%1)
196" stwcx. %0,0,%1\n\
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000197 bne- 1b\n"
198 PPC_ACQUIRE_BARRIER
199"2:" : "=&r" (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 : "r" (&rw->lock)
201 : "cr0", "xer", "memory");
202
203 return tmp;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * This returns the old value in the lock,
208 * so we got the write lock if the return value is 0.
209 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100210static inline long __arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100212 long tmp, token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100214 token = WRLOCK_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 __asm__ __volatile__(
Anton Blanchard4e14a4d2010-02-10 00:57:28 +0000216"1: " PPC_LWARX(%0,0,%2,1) "\n\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 cmpwi 0,%0,0\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100218 bne- 2f\n"
219 PPC405_ERR77(0,%1)
220" stwcx. %1,0,%2\n\
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000221 bne- 1b\n"
222 PPC_ACQUIRE_BARRIER
223"2:" : "=&r" (tmp)
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100224 : "r" (token), "r" (&rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 : "cr0", "memory");
226
227 return tmp;
228}
229
Thomas Gleixnere5931942009-12-03 20:08:46 +0100230static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700232 while (1) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100233 if (likely(__arch_read_trylock(rw) > 0))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700234 break;
235 do {
236 HMT_low();
237 if (SHARED_PROCESSOR)
238 __rw_yield(rw);
239 } while (unlikely(rw->lock < 0));
240 HMT_medium();
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Thomas Gleixnere5931942009-12-03 20:08:46 +0100244static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 while (1) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100247 if (likely(__arch_write_trylock(rw) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 do {
250 HMT_low();
251 if (SHARED_PROCESSOR)
252 __rw_yield(rw);
Jake Moilanend6374132005-05-01 08:58:47 -0700253 } while (unlikely(rw->lock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 HMT_medium();
255 }
256}
257
Thomas Gleixnere5931942009-12-03 20:08:46 +0100258static inline int arch_read_trylock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700259{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100260 return __arch_read_trylock(rw) > 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700261}
262
Thomas Gleixnere5931942009-12-03 20:08:46 +0100263static inline int arch_write_trylock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700264{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100265 return __arch_write_trylock(rw) == 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700266}
267
Thomas Gleixnere5931942009-12-03 20:08:46 +0100268static inline void arch_read_unlock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700269{
270 long tmp;
271
272 __asm__ __volatile__(
Anton Blanchard144b9c12006-01-13 15:37:17 +1100273 "# read_unlock\n\t"
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000274 PPC_RELEASE_BARRIER
Anton Blanchard144b9c12006-01-13 15:37:17 +1100275"1: lwarx %0,0,%1\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100276 addic %0,%0,-1\n"
277 PPC405_ERR77(0,%1)
278" stwcx. %0,0,%1\n\
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700279 bne- 1b"
280 : "=&r"(tmp)
281 : "r"(&rw->lock)
Paul Mackerrasefc36242008-11-05 18:39:27 +0000282 : "cr0", "xer", "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700283}
284
Thomas Gleixnere5931942009-12-03 20:08:46 +0100285static inline void arch_write_unlock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700286{
Anton Blanchard144b9c12006-01-13 15:37:17 +1100287 __asm__ __volatile__("# write_unlock\n\t"
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000288 PPC_RELEASE_BARRIER: : :"memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700289 rw->lock = 0;
290}
291
Thomas Gleixnere5931942009-12-03 20:08:46 +0100292#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
293#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700294
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100295#define arch_spin_relax(lock) __spin_yield(lock)
296#define arch_read_relax(lock) __rw_yield(lock)
297#define arch_write_relax(lock) __rw_yield(lock)
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700298
Arnd Bergmann88ced032005-12-16 22:43:46 +0100299#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#endif /* __ASM_SPINLOCK_H */