blob: 1c35b59f6f30c0f528e6538483f764a30218d761 [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\
68 bne- 1b\n\
69 isync\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100702:" : "=&r" (tmp)
71 : "r" (token), "r" (&lock->slock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070072 : "cr0", "memory");
73
74 return tmp;
75}
76
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010077static inline int arch_spin_trylock(arch_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070078{
Paul Mackerrasf007cac2006-09-13 22:08:26 +100079 CLEAR_IO_SYNC;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010080 return __arch_spin_trylock(lock) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83/*
84 * On a system with shared processors (that is, where a physical
85 * processor is multiplexed between several virtual processors),
86 * there is no point spinning on a lock if the holder of the lock
87 * isn't currently scheduled on a physical processor. Instead
88 * we detect this situation and ask the hypervisor to give the
89 * rest of our timeslice to the lock holder.
90 *
91 * So that we can tell which virtual processor is holding a lock,
92 * we put 0x80000000 | smp_processor_id() in the lock when it is
93 * held. Conveniently, we have a word in the paca that holds this
94 * value.
95 */
96
97#if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
98/* We only yield to the hypervisor if we are in shared processor mode */
David Gibson3356bb9f72006-01-13 10:26:42 +110099#define SHARED_PROCESSOR (get_lppaca()->shared_proc)
Thomas Gleixner445c8952009-12-02 19:49:50 +0100100extern void __spin_yield(arch_spinlock_t *lock);
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100101extern void __rw_yield(arch_rwlock_t *lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else /* SPLPAR || ISERIES */
103#define __spin_yield(x) barrier()
104#define __rw_yield(x) barrier()
105#define SHARED_PROCESSOR 0
106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100108static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000110 CLEAR_IO_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 while (1) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100112 if (likely(__arch_spin_trylock(lock) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 do {
115 HMT_low();
116 if (SHARED_PROCESSOR)
117 __spin_yield(lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700118 } while (unlikely(lock->slock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 HMT_medium();
120 }
121}
122
Bart Van Assche89b58102008-06-28 16:51:35 +1000123static inline
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100124void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 unsigned long flags_dis;
127
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000128 CLEAR_IO_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 while (1) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100130 if (likely(__arch_spin_trylock(lock) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132 local_save_flags(flags_dis);
133 local_irq_restore(flags);
134 do {
135 HMT_low();
136 if (SHARED_PROCESSOR)
137 __spin_yield(lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700138 } while (unlikely(lock->slock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 HMT_medium();
140 local_irq_restore(flags_dis);
141 }
142}
143
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100144static inline void arch_spin_unlock(arch_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700145{
Paul Mackerrasf007cac2006-09-13 22:08:26 +1000146 SYNC_IO;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100147 __asm__ __volatile__("# arch_spin_unlock\n\t"
Anton Blanchard144b9c12006-01-13 15:37:17 +1100148 LWSYNC_ON_SMP: : :"memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700149 lock->slock = 0;
150}
151
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100152#ifdef CONFIG_PPC64
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100153extern void arch_spin_unlock_wait(arch_spinlock_t *lock);
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100154#else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100155#define arch_spin_unlock_wait(lock) \
156 do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100157#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Read-write spinlocks, allowing multiple readers
161 * but only one writer.
162 *
163 * NOTE! it is quite common to have readers in interrupts
164 * but no interrupt writers. For those circumstances we
165 * can "mix" irq-safe locks - any writer needs to get a
166 * irq-safe write-lock, but readers can get non-irqsafe
167 * read-locks.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Thomas Gleixnere5931942009-12-03 20:08:46 +0100170#define arch_read_can_lock(rw) ((rw)->lock >= 0)
171#define arch_write_can_lock(rw) (!(rw)->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100173#ifdef CONFIG_PPC64
174#define __DO_SIGN_EXTEND "extsw %0,%0\n"
175#define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
176#else
177#define __DO_SIGN_EXTEND
178#define WRLOCK_TOKEN (-1)
179#endif
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * This returns the old value in the lock + 1,
183 * so we got a read lock if the return value is > 0.
184 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100185static inline long __arch_read_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 long tmp;
188
189 __asm__ __volatile__(
Anton Blanchard4e14a4d2010-02-10 00:57:28 +0000190"1: " PPC_LWARX(%0,0,%1,1) "\n"
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100191 __DO_SIGN_EXTEND
192" addic. %0,%0,1\n\
193 ble- 2f\n"
194 PPC405_ERR77(0,%1)
195" stwcx. %0,0,%1\n\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 bne- 1b\n\
197 isync\n\
1982:" : "=&r" (tmp)
199 : "r" (&rw->lock)
200 : "cr0", "xer", "memory");
201
202 return tmp;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * This returns the old value in the lock,
207 * so we got the write lock if the return value is 0.
208 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100209static inline long __arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100211 long tmp, token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100213 token = WRLOCK_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 __asm__ __volatile__(
Anton Blanchard4e14a4d2010-02-10 00:57:28 +0000215"1: " PPC_LWARX(%0,0,%2,1) "\n\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 cmpwi 0,%0,0\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100217 bne- 2f\n"
218 PPC405_ERR77(0,%1)
219" stwcx. %1,0,%2\n\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 bne- 1b\n\
221 isync\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +11002222:" : "=&r" (tmp)
223 : "r" (token), "r" (&rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 : "cr0", "memory");
225
226 return tmp;
227}
228
Thomas Gleixnere5931942009-12-03 20:08:46 +0100229static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700231 while (1) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100232 if (likely(__arch_read_trylock(rw) > 0))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700233 break;
234 do {
235 HMT_low();
236 if (SHARED_PROCESSOR)
237 __rw_yield(rw);
238 } while (unlikely(rw->lock < 0));
239 HMT_medium();
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Thomas Gleixnere5931942009-12-03 20:08:46 +0100243static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 while (1) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100246 if (likely(__arch_write_trylock(rw) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
248 do {
249 HMT_low();
250 if (SHARED_PROCESSOR)
251 __rw_yield(rw);
Jake Moilanend6374132005-05-01 08:58:47 -0700252 } while (unlikely(rw->lock != 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 HMT_medium();
254 }
255}
256
Thomas Gleixnere5931942009-12-03 20:08:46 +0100257static inline int arch_read_trylock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700258{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100259 return __arch_read_trylock(rw) > 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700260}
261
Thomas Gleixnere5931942009-12-03 20:08:46 +0100262static inline int arch_write_trylock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700263{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100264 return __arch_write_trylock(rw) == 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700265}
266
Thomas Gleixnere5931942009-12-03 20:08:46 +0100267static inline void arch_read_unlock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700268{
269 long tmp;
270
271 __asm__ __volatile__(
Anton Blanchard144b9c12006-01-13 15:37:17 +1100272 "# read_unlock\n\t"
273 LWSYNC_ON_SMP
274"1: lwarx %0,0,%1\n\
Paul Mackerras0212ddd2005-11-19 20:50:46 +1100275 addic %0,%0,-1\n"
276 PPC405_ERR77(0,%1)
277" stwcx. %0,0,%1\n\
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700278 bne- 1b"
279 : "=&r"(tmp)
280 : "r"(&rw->lock)
Paul Mackerrasefc36242008-11-05 18:39:27 +0000281 : "cr0", "xer", "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700282}
283
Thomas Gleixnere5931942009-12-03 20:08:46 +0100284static inline void arch_write_unlock(arch_rwlock_t *rw)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700285{
Anton Blanchard144b9c12006-01-13 15:37:17 +1100286 __asm__ __volatile__("# write_unlock\n\t"
287 LWSYNC_ON_SMP: : :"memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700288 rw->lock = 0;
289}
290
Thomas Gleixnere5931942009-12-03 20:08:46 +0100291#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
292#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700293
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100294#define arch_spin_relax(lock) __spin_yield(lock)
295#define arch_read_relax(lock) __rw_yield(lock)
296#define arch_write_relax(lock) __rw_yield(lock)
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700297
Arnd Bergmann88ced032005-12-16 22:43:46 +0100298#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif /* __ASM_SPINLOCK_H */