1b8b572e1SStephen Rothwell #ifndef __ASM_SPINLOCK_H
2b8b572e1SStephen Rothwell #define __ASM_SPINLOCK_H
3b8b572e1SStephen Rothwell #ifdef __KERNEL__
4b8b572e1SStephen Rothwell 
5b8b572e1SStephen Rothwell /*
6b8b572e1SStephen Rothwell  * Simple spin lock operations.
7b8b572e1SStephen Rothwell  *
8b8b572e1SStephen Rothwell  * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
9b8b572e1SStephen Rothwell  * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10b8b572e1SStephen Rothwell  * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
11b8b572e1SStephen Rothwell  *	Rework to support virtual processors
12b8b572e1SStephen Rothwell  *
13b8b572e1SStephen Rothwell  * Type of int is used as a full 64b word is not necessary.
14b8b572e1SStephen Rothwell  *
15b8b572e1SStephen Rothwell  * This program is free software; you can redistribute it and/or
16b8b572e1SStephen Rothwell  * modify it under the terms of the GNU General Public License
17b8b572e1SStephen Rothwell  * as published by the Free Software Foundation; either version
18b8b572e1SStephen Rothwell  * 2 of the License, or (at your option) any later version.
19b8b572e1SStephen Rothwell  *
20b8b572e1SStephen Rothwell  * (the type definitions are in asm/spinlock_types.h)
21b8b572e1SStephen Rothwell  */
22b8b572e1SStephen Rothwell #include <linux/irqflags.h>
23b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
24b8b572e1SStephen Rothwell #include <asm/paca.h>
25b8b572e1SStephen Rothwell #include <asm/hvcall.h>
26b8b572e1SStephen Rothwell #endif
27b8b572e1SStephen Rothwell #include <asm/asm-compat.h>
28b8b572e1SStephen Rothwell #include <asm/synch.h>
294e14a4d1SAnton Blanchard #include <asm/ppc-opcode.h>
30b8b572e1SStephen Rothwell 
31919fc6e3SPaul E. McKenney #define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
32919fc6e3SPaul E. McKenney 
33b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
34b8b572e1SStephen Rothwell /* use 0x800000yy when locked, where yy == CPU number */
3554bb7f4bSAnton Blanchard #ifdef __BIG_ENDIAN__
36b8b572e1SStephen Rothwell #define LOCK_TOKEN	(*(u32 *)(&get_paca()->lock_token))
37b8b572e1SStephen Rothwell #else
3854bb7f4bSAnton Blanchard #define LOCK_TOKEN	(*(u32 *)(&get_paca()->paca_index))
3954bb7f4bSAnton Blanchard #endif
4054bb7f4bSAnton Blanchard #else
41b8b572e1SStephen Rothwell #define LOCK_TOKEN	1
42b8b572e1SStephen Rothwell #endif
43b8b572e1SStephen Rothwell 
44b8b572e1SStephen Rothwell #if defined(CONFIG_PPC64) && defined(CONFIG_SMP)
45b8b572e1SStephen Rothwell #define CLEAR_IO_SYNC	(get_paca()->io_sync = 0)
46b8b572e1SStephen Rothwell #define SYNC_IO		do {						\
47b8b572e1SStephen Rothwell 				if (unlikely(get_paca()->io_sync)) {	\
48b8b572e1SStephen Rothwell 					mb();				\
49b8b572e1SStephen Rothwell 					get_paca()->io_sync = 0;	\
50b8b572e1SStephen Rothwell 				}					\
51b8b572e1SStephen Rothwell 			} while (0)
52b8b572e1SStephen Rothwell #else
53b8b572e1SStephen Rothwell #define CLEAR_IO_SYNC
54b8b572e1SStephen Rothwell #define SYNC_IO
55b8b572e1SStephen Rothwell #endif
56b8b572e1SStephen Rothwell 
573405d230SMichael Ellerman static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
583405d230SMichael Ellerman {
593405d230SMichael Ellerman 	return lock.slock == 0;
603405d230SMichael Ellerman }
613405d230SMichael Ellerman 
627179ba52SMichael Ellerman static inline int arch_spin_is_locked(arch_spinlock_t *lock)
637179ba52SMichael Ellerman {
6451d7d520SMichael Ellerman 	smp_mb();
657179ba52SMichael Ellerman 	return !arch_spin_value_unlocked(*lock);
667179ba52SMichael Ellerman }
677179ba52SMichael Ellerman 
68b8b572e1SStephen Rothwell /*
69b8b572e1SStephen Rothwell  * This returns the old value in the lock, so we succeeded
70b8b572e1SStephen Rothwell  * in getting the lock if the return value is 0.
71b8b572e1SStephen Rothwell  */
720199c4e6SThomas Gleixner static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
73b8b572e1SStephen Rothwell {
74b8b572e1SStephen Rothwell 	unsigned long tmp, token;
75b8b572e1SStephen Rothwell 
76b8b572e1SStephen Rothwell 	token = LOCK_TOKEN;
77b8b572e1SStephen Rothwell 	__asm__ __volatile__(
784e14a4d1SAnton Blanchard "1:	" PPC_LWARX(%0,0,%2,1) "\n\
79b8b572e1SStephen Rothwell 	cmpwi		0,%0,0\n\
80b8b572e1SStephen Rothwell 	bne-		2f\n\
81b8b572e1SStephen Rothwell 	stwcx.		%1,0,%2\n\
82f10e2e5bSAnton Blanchard 	bne-		1b\n"
83f10e2e5bSAnton Blanchard 	PPC_ACQUIRE_BARRIER
84f10e2e5bSAnton Blanchard "2:"
85f10e2e5bSAnton Blanchard 	: "=&r" (tmp)
86b8b572e1SStephen Rothwell 	: "r" (token), "r" (&lock->slock)
87b8b572e1SStephen Rothwell 	: "cr0", "memory");
88b8b572e1SStephen Rothwell 
89b8b572e1SStephen Rothwell 	return tmp;
90b8b572e1SStephen Rothwell }
91b8b572e1SStephen Rothwell 
920199c4e6SThomas Gleixner static inline int arch_spin_trylock(arch_spinlock_t *lock)
93b8b572e1SStephen Rothwell {
94b8b572e1SStephen Rothwell 	CLEAR_IO_SYNC;
950199c4e6SThomas Gleixner 	return __arch_spin_trylock(lock) == 0;
96b8b572e1SStephen Rothwell }
97b8b572e1SStephen Rothwell 
98b8b572e1SStephen Rothwell /*
99b8b572e1SStephen Rothwell  * On a system with shared processors (that is, where a physical
100b8b572e1SStephen Rothwell  * processor is multiplexed between several virtual processors),
101b8b572e1SStephen Rothwell  * there is no point spinning on a lock if the holder of the lock
102b8b572e1SStephen Rothwell  * isn't currently scheduled on a physical processor.  Instead
103b8b572e1SStephen Rothwell  * we detect this situation and ask the hypervisor to give the
104b8b572e1SStephen Rothwell  * rest of our timeslice to the lock holder.
105b8b572e1SStephen Rothwell  *
106b8b572e1SStephen Rothwell  * So that we can tell which virtual processor is holding a lock,
107b8b572e1SStephen Rothwell  * we put 0x80000000 | smp_processor_id() in the lock when it is
108b8b572e1SStephen Rothwell  * held.  Conveniently, we have a word in the paca that holds this
109b8b572e1SStephen Rothwell  * value.
110b8b572e1SStephen Rothwell  */
111b8b572e1SStephen Rothwell 
1121b041885SStephen Rothwell #if defined(CONFIG_PPC_SPLPAR)
113b8b572e1SStephen Rothwell /* We only yield to the hypervisor if we are in shared processor mode */
114f13c13a0SAnton Blanchard #define SHARED_PROCESSOR (lppaca_shared_proc(local_paca->lppaca_ptr))
115445c8951SThomas Gleixner extern void __spin_yield(arch_spinlock_t *lock);
116fb3a6bbcSThomas Gleixner extern void __rw_yield(arch_rwlock_t *lock);
1171b041885SStephen Rothwell #else /* SPLPAR */
118b8b572e1SStephen Rothwell #define __spin_yield(x)	barrier()
119b8b572e1SStephen Rothwell #define __rw_yield(x)	barrier()
120b8b572e1SStephen Rothwell #define SHARED_PROCESSOR	0
121b8b572e1SStephen Rothwell #endif
122b8b572e1SStephen Rothwell 
1230199c4e6SThomas Gleixner static inline void arch_spin_lock(arch_spinlock_t *lock)
124b8b572e1SStephen Rothwell {
125b8b572e1SStephen Rothwell 	CLEAR_IO_SYNC;
126b8b572e1SStephen Rothwell 	while (1) {
1270199c4e6SThomas Gleixner 		if (likely(__arch_spin_trylock(lock) == 0))
128b8b572e1SStephen Rothwell 			break;
129b8b572e1SStephen Rothwell 		do {
130b8b572e1SStephen Rothwell 			HMT_low();
131b8b572e1SStephen Rothwell 			if (SHARED_PROCESSOR)
132b8b572e1SStephen Rothwell 				__spin_yield(lock);
133b8b572e1SStephen Rothwell 		} while (unlikely(lock->slock != 0));
134b8b572e1SStephen Rothwell 		HMT_medium();
135b8b572e1SStephen Rothwell 	}
136b8b572e1SStephen Rothwell }
137b8b572e1SStephen Rothwell 
138b8b572e1SStephen Rothwell static inline
1390199c4e6SThomas Gleixner void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
140b8b572e1SStephen Rothwell {
141b8b572e1SStephen Rothwell 	unsigned long flags_dis;
142b8b572e1SStephen Rothwell 
143b8b572e1SStephen Rothwell 	CLEAR_IO_SYNC;
144b8b572e1SStephen Rothwell 	while (1) {
1450199c4e6SThomas Gleixner 		if (likely(__arch_spin_trylock(lock) == 0))
146b8b572e1SStephen Rothwell 			break;
147b8b572e1SStephen Rothwell 		local_save_flags(flags_dis);
148b8b572e1SStephen Rothwell 		local_irq_restore(flags);
149b8b572e1SStephen Rothwell 		do {
150b8b572e1SStephen Rothwell 			HMT_low();
151b8b572e1SStephen Rothwell 			if (SHARED_PROCESSOR)
152b8b572e1SStephen Rothwell 				__spin_yield(lock);
153b8b572e1SStephen Rothwell 		} while (unlikely(lock->slock != 0));
154b8b572e1SStephen Rothwell 		HMT_medium();
155b8b572e1SStephen Rothwell 		local_irq_restore(flags_dis);
156b8b572e1SStephen Rothwell 	}
157b8b572e1SStephen Rothwell }
158b8b572e1SStephen Rothwell 
1590199c4e6SThomas Gleixner static inline void arch_spin_unlock(arch_spinlock_t *lock)
160b8b572e1SStephen Rothwell {
161b8b572e1SStephen Rothwell 	SYNC_IO;
1620199c4e6SThomas Gleixner 	__asm__ __volatile__("# arch_spin_unlock\n\t"
163f10e2e5bSAnton Blanchard 				PPC_RELEASE_BARRIER: : :"memory");
164b8b572e1SStephen Rothwell 	lock->slock = 0;
165b8b572e1SStephen Rothwell }
166b8b572e1SStephen Rothwell 
167b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
1680199c4e6SThomas Gleixner extern void arch_spin_unlock_wait(arch_spinlock_t *lock);
169b8b572e1SStephen Rothwell #else
1700199c4e6SThomas Gleixner #define arch_spin_unlock_wait(lock) \
1710199c4e6SThomas Gleixner 	do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
172b8b572e1SStephen Rothwell #endif
173b8b572e1SStephen Rothwell 
174b8b572e1SStephen Rothwell /*
175b8b572e1SStephen Rothwell  * Read-write spinlocks, allowing multiple readers
176b8b572e1SStephen Rothwell  * but only one writer.
177b8b572e1SStephen Rothwell  *
178b8b572e1SStephen Rothwell  * NOTE! it is quite common to have readers in interrupts
179b8b572e1SStephen Rothwell  * but no interrupt writers. For those circumstances we
180b8b572e1SStephen Rothwell  * can "mix" irq-safe locks - any writer needs to get a
181b8b572e1SStephen Rothwell  * irq-safe write-lock, but readers can get non-irqsafe
182b8b572e1SStephen Rothwell  * read-locks.
183b8b572e1SStephen Rothwell  */
184b8b572e1SStephen Rothwell 
185e5931943SThomas Gleixner #define arch_read_can_lock(rw)		((rw)->lock >= 0)
186e5931943SThomas Gleixner #define arch_write_can_lock(rw)	(!(rw)->lock)
187b8b572e1SStephen Rothwell 
188b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
189b8b572e1SStephen Rothwell #define __DO_SIGN_EXTEND	"extsw	%0,%0\n"
190b8b572e1SStephen Rothwell #define WRLOCK_TOKEN		LOCK_TOKEN	/* it's negative */
191b8b572e1SStephen Rothwell #else
192b8b572e1SStephen Rothwell #define __DO_SIGN_EXTEND
193b8b572e1SStephen Rothwell #define WRLOCK_TOKEN		(-1)
194b8b572e1SStephen Rothwell #endif
195b8b572e1SStephen Rothwell 
196b8b572e1SStephen Rothwell /*
197b8b572e1SStephen Rothwell  * This returns the old value in the lock + 1,
198b8b572e1SStephen Rothwell  * so we got a read lock if the return value is > 0.
199b8b572e1SStephen Rothwell  */
200e5931943SThomas Gleixner static inline long __arch_read_trylock(arch_rwlock_t *rw)
201b8b572e1SStephen Rothwell {
202b8b572e1SStephen Rothwell 	long tmp;
203b8b572e1SStephen Rothwell 
204b8b572e1SStephen Rothwell 	__asm__ __volatile__(
2054e14a4d1SAnton Blanchard "1:	" PPC_LWARX(%0,0,%1,1) "\n"
206b8b572e1SStephen Rothwell 	__DO_SIGN_EXTEND
207b8b572e1SStephen Rothwell "	addic.		%0,%0,1\n\
208b8b572e1SStephen Rothwell 	ble-		2f\n"
209b8b572e1SStephen Rothwell 	PPC405_ERR77(0,%1)
210b8b572e1SStephen Rothwell "	stwcx.		%0,0,%1\n\
211f10e2e5bSAnton Blanchard 	bne-		1b\n"
212f10e2e5bSAnton Blanchard 	PPC_ACQUIRE_BARRIER
213f10e2e5bSAnton Blanchard "2:"	: "=&r" (tmp)
214b8b572e1SStephen Rothwell 	: "r" (&rw->lock)
215b8b572e1SStephen Rothwell 	: "cr0", "xer", "memory");
216b8b572e1SStephen Rothwell 
217b8b572e1SStephen Rothwell 	return tmp;
218b8b572e1SStephen Rothwell }
219b8b572e1SStephen Rothwell 
220b8b572e1SStephen Rothwell /*
221b8b572e1SStephen Rothwell  * This returns the old value in the lock,
222b8b572e1SStephen Rothwell  * so we got the write lock if the return value is 0.
223b8b572e1SStephen Rothwell  */
224e5931943SThomas Gleixner static inline long __arch_write_trylock(arch_rwlock_t *rw)
225b8b572e1SStephen Rothwell {
226b8b572e1SStephen Rothwell 	long tmp, token;
227b8b572e1SStephen Rothwell 
228b8b572e1SStephen Rothwell 	token = WRLOCK_TOKEN;
229b8b572e1SStephen Rothwell 	__asm__ __volatile__(
2304e14a4d1SAnton Blanchard "1:	" PPC_LWARX(%0,0,%2,1) "\n\
231b8b572e1SStephen Rothwell 	cmpwi		0,%0,0\n\
232b8b572e1SStephen Rothwell 	bne-		2f\n"
233b8b572e1SStephen Rothwell 	PPC405_ERR77(0,%1)
234b8b572e1SStephen Rothwell "	stwcx.		%1,0,%2\n\
235f10e2e5bSAnton Blanchard 	bne-		1b\n"
236f10e2e5bSAnton Blanchard 	PPC_ACQUIRE_BARRIER
237f10e2e5bSAnton Blanchard "2:"	: "=&r" (tmp)
238b8b572e1SStephen Rothwell 	: "r" (token), "r" (&rw->lock)
239b8b572e1SStephen Rothwell 	: "cr0", "memory");
240b8b572e1SStephen Rothwell 
241b8b572e1SStephen Rothwell 	return tmp;
242b8b572e1SStephen Rothwell }
243b8b572e1SStephen Rothwell 
244e5931943SThomas Gleixner static inline void arch_read_lock(arch_rwlock_t *rw)
245b8b572e1SStephen Rothwell {
246b8b572e1SStephen Rothwell 	while (1) {
247e5931943SThomas Gleixner 		if (likely(__arch_read_trylock(rw) > 0))
248b8b572e1SStephen Rothwell 			break;
249b8b572e1SStephen Rothwell 		do {
250b8b572e1SStephen Rothwell 			HMT_low();
251b8b572e1SStephen Rothwell 			if (SHARED_PROCESSOR)
252b8b572e1SStephen Rothwell 				__rw_yield(rw);
253b8b572e1SStephen Rothwell 		} while (unlikely(rw->lock < 0));
254b8b572e1SStephen Rothwell 		HMT_medium();
255b8b572e1SStephen Rothwell 	}
256b8b572e1SStephen Rothwell }
257b8b572e1SStephen Rothwell 
258e5931943SThomas Gleixner static inline void arch_write_lock(arch_rwlock_t *rw)
259b8b572e1SStephen Rothwell {
260b8b572e1SStephen Rothwell 	while (1) {
261e5931943SThomas Gleixner 		if (likely(__arch_write_trylock(rw) == 0))
262b8b572e1SStephen Rothwell 			break;
263b8b572e1SStephen Rothwell 		do {
264b8b572e1SStephen Rothwell 			HMT_low();
265b8b572e1SStephen Rothwell 			if (SHARED_PROCESSOR)
266b8b572e1SStephen Rothwell 				__rw_yield(rw);
267b8b572e1SStephen Rothwell 		} while (unlikely(rw->lock != 0));
268b8b572e1SStephen Rothwell 		HMT_medium();
269b8b572e1SStephen Rothwell 	}
270b8b572e1SStephen Rothwell }
271b8b572e1SStephen Rothwell 
272e5931943SThomas Gleixner static inline int arch_read_trylock(arch_rwlock_t *rw)
273b8b572e1SStephen Rothwell {
274e5931943SThomas Gleixner 	return __arch_read_trylock(rw) > 0;
275b8b572e1SStephen Rothwell }
276b8b572e1SStephen Rothwell 
277e5931943SThomas Gleixner static inline int arch_write_trylock(arch_rwlock_t *rw)
278b8b572e1SStephen Rothwell {
279e5931943SThomas Gleixner 	return __arch_write_trylock(rw) == 0;
280b8b572e1SStephen Rothwell }
281b8b572e1SStephen Rothwell 
282e5931943SThomas Gleixner static inline void arch_read_unlock(arch_rwlock_t *rw)
283b8b572e1SStephen Rothwell {
284b8b572e1SStephen Rothwell 	long tmp;
285b8b572e1SStephen Rothwell 
286b8b572e1SStephen Rothwell 	__asm__ __volatile__(
287b8b572e1SStephen Rothwell 	"# read_unlock\n\t"
288f10e2e5bSAnton Blanchard 	PPC_RELEASE_BARRIER
289b8b572e1SStephen Rothwell "1:	lwarx		%0,0,%1\n\
290b8b572e1SStephen Rothwell 	addic		%0,%0,-1\n"
291b8b572e1SStephen Rothwell 	PPC405_ERR77(0,%1)
292b8b572e1SStephen Rothwell "	stwcx.		%0,0,%1\n\
293b8b572e1SStephen Rothwell 	bne-		1b"
294b8b572e1SStephen Rothwell 	: "=&r"(tmp)
295b8b572e1SStephen Rothwell 	: "r"(&rw->lock)
296efc3624cSPaul Mackerras 	: "cr0", "xer", "memory");
297b8b572e1SStephen Rothwell }
298b8b572e1SStephen Rothwell 
299e5931943SThomas Gleixner static inline void arch_write_unlock(arch_rwlock_t *rw)
300b8b572e1SStephen Rothwell {
301b8b572e1SStephen Rothwell 	__asm__ __volatile__("# write_unlock\n\t"
302f10e2e5bSAnton Blanchard 				PPC_RELEASE_BARRIER: : :"memory");
303b8b572e1SStephen Rothwell 	rw->lock = 0;
304b8b572e1SStephen Rothwell }
305b8b572e1SStephen Rothwell 
306e5931943SThomas Gleixner #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
307e5931943SThomas Gleixner #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
308f5f7eac4SRobin Holt 
3090199c4e6SThomas Gleixner #define arch_spin_relax(lock)	__spin_yield(lock)
3100199c4e6SThomas Gleixner #define arch_read_relax(lock)	__rw_yield(lock)
3110199c4e6SThomas Gleixner #define arch_write_relax(lock)	__rw_yield(lock)
312b8b572e1SStephen Rothwell 
313b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
314b8b572e1SStephen Rothwell #endif /* __ASM_SPINLOCK_H */
315