1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
5 * Simple spin lock operations.
7 * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
9 * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
10 * Rework to support virtual processors
12 * Type of int is used as a full 64b word is not necessary.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 #include <linux/config.h>
21 #include <asm/hvcall.h>
22 #include <asm/iSeries/HvCall.h>
25 volatile unsigned int lock
;
27 unsigned int break_lock
;
32 volatile signed int lock
;
34 unsigned int break_lock
;
39 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
41 #define spin_is_locked(x) ((x)->lock != 0)
42 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
44 static __inline__
void _raw_spin_unlock(spinlock_t
*lock
)
46 __asm__
__volatile__("lwsync # spin_unlock": : :"memory");
51 * On a system with shared processors (that is, where a physical
52 * processor is multiplexed between several virtual processors),
53 * there is no point spinning on a lock if the holder of the lock
54 * isn't currently scheduled on a physical processor. Instead
55 * we detect this situation and ask the hypervisor to give the
56 * rest of our timeslice to the lock holder.
58 * So that we can tell which virtual processor is holding a lock,
59 * we put 0x80000000 | smp_processor_id() in the lock when it is
60 * held. Conveniently, we have a word in the paca that holds this
64 #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
65 /* We only yield to the hypervisor if we are in shared processor mode */
66 #define SHARED_PROCESSOR (get_paca()->lppaca.shared_proc)
67 extern void __spin_yield(spinlock_t
*lock
);
68 extern void __rw_yield(rwlock_t
*lock
);
69 #else /* SPLPAR || ISERIES */
70 #define __spin_yield(x) barrier()
71 #define __rw_yield(x) barrier()
72 #define SHARED_PROCESSOR 0
74 extern void spin_unlock_wait(spinlock_t
*lock
);
77 * This returns the old value in the lock, so we succeeded
78 * in getting the lock if the return value is 0.
80 static __inline__
unsigned long __spin_trylock(spinlock_t
*lock
)
82 unsigned long tmp
, tmp2
;
85 " lwz %1,%3(13) # __spin_trylock\n\
92 2:" : "=&r" (tmp
), "=&r" (tmp2
)
93 : "r" (&lock
->lock
), "i" (offsetof(struct paca_struct
, lock_token
))
99 static int __inline__
_raw_spin_trylock(spinlock_t
*lock
)
101 return __spin_trylock(lock
) == 0;
104 static void __inline__
_raw_spin_lock(spinlock_t
*lock
)
107 if (likely(__spin_trylock(lock
) == 0))
111 if (SHARED_PROCESSOR
)
113 } while (unlikely(lock
->lock
!= 0));
118 static void __inline__
_raw_spin_lock_flags(spinlock_t
*lock
, unsigned long flags
)
120 unsigned long flags_dis
;
123 if (likely(__spin_trylock(lock
) == 0))
125 local_save_flags(flags_dis
);
126 local_irq_restore(flags
);
129 if (SHARED_PROCESSOR
)
131 } while (unlikely(lock
->lock
!= 0));
133 local_irq_restore(flags_dis
);
138 * Read-write spinlocks, allowing multiple readers
139 * but only one writer.
141 * NOTE! it is quite common to have readers in interrupts
142 * but no interrupt writers. For those circumstances we
143 * can "mix" irq-safe locks - any writer needs to get a
144 * irq-safe write-lock, but readers can get non-irqsafe
147 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
149 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
151 #define read_can_lock(rw) ((rw)->lock >= 0)
152 #define write_can_lock(rw) (!(rw)->lock)
154 static __inline__
void _raw_write_unlock(rwlock_t
*rw
)
156 __asm__
__volatile__("lwsync # write_unlock": : :"memory");
161 * This returns the old value in the lock + 1,
162 * so we got a read lock if the return value is > 0.
164 static long __inline__
__read_trylock(rwlock_t
*rw
)
168 __asm__
__volatile__(
169 "1: lwarx %0,0,%1 # read_trylock\n\
178 : "cr0", "xer", "memory");
183 static int __inline__
_raw_read_trylock(rwlock_t
*rw
)
185 return __read_trylock(rw
) > 0;
188 static void __inline__
_raw_read_lock(rwlock_t
*rw
)
191 if (likely(__read_trylock(rw
) > 0))
195 if (SHARED_PROCESSOR
)
197 } while (unlikely(rw
->lock
< 0));
202 static void __inline__
_raw_read_unlock(rwlock_t
*rw
)
206 __asm__
__volatile__(
207 "eieio # read_unlock\n\
218 * This returns the old value in the lock,
219 * so we got the write lock if the return value is 0.
221 static __inline__
long __write_trylock(rwlock_t
*rw
)
225 __asm__
__volatile__(
226 " lwz %1,%3(13) # write_trylock\n\
233 2:" : "=&r" (tmp
), "=&r" (tmp2
)
234 : "r" (&rw
->lock
), "i" (offsetof(struct paca_struct
, lock_token
))
240 static int __inline__
_raw_write_trylock(rwlock_t
*rw
)
242 return __write_trylock(rw
) == 0;
245 static void __inline__
_raw_write_lock(rwlock_t
*rw
)
248 if (likely(__write_trylock(rw
) == 0))
252 if (SHARED_PROCESSOR
)
254 } while (unlikely(rw
->lock
!= 0));
259 #endif /* __KERNEL__ */
260 #endif /* __ASM_SPINLOCK_H */