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 * (the type definitions are in asm/spinlock_types.h)
23 #include <asm/hvcall.h>
24 #include <asm/iseries/hv_call.h>
26 #include <asm/asm-compat.h>
27 #include <asm/synch.h>
29 #define __raw_spin_is_locked(x) ((x)->slock != 0)
32 /* use 0x800000yy when locked, where yy == CPU number */
33 #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
39 * This returns the old value in the lock, so we succeeded
40 * in getting the lock if the return value is 0.
42 static __inline__
unsigned long __spin_trylock(raw_spinlock_t
*lock
)
44 unsigned long tmp
, token
;
48 "1: lwarx %0,0,%2 # __spin_trylock\n\
55 : "r" (token
), "r" (&lock
->slock
)
61 static int __inline__
__raw_spin_trylock(raw_spinlock_t
*lock
)
63 return __spin_trylock(lock
) == 0;
67 * On a system with shared processors (that is, where a physical
68 * processor is multiplexed between several virtual processors),
69 * there is no point spinning on a lock if the holder of the lock
70 * isn't currently scheduled on a physical processor. Instead
71 * we detect this situation and ask the hypervisor to give the
72 * rest of our timeslice to the lock holder.
74 * So that we can tell which virtual processor is holding a lock,
75 * we put 0x80000000 | smp_processor_id() in the lock when it is
76 * held. Conveniently, we have a word in the paca that holds this
80 #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
81 /* We only yield to the hypervisor if we are in shared processor mode */
82 #define SHARED_PROCESSOR (get_paca()->lppaca.shared_proc)
83 extern void __spin_yield(raw_spinlock_t
*lock
);
84 extern void __rw_yield(raw_rwlock_t
*lock
);
85 #else /* SPLPAR || ISERIES */
86 #define __spin_yield(x) barrier()
87 #define __rw_yield(x) barrier()
88 #define SHARED_PROCESSOR 0
91 static void __inline__
__raw_spin_lock(raw_spinlock_t
*lock
)
94 if (likely(__spin_trylock(lock
) == 0))
100 } while (unlikely(lock
->slock
!= 0));
105 static void __inline__
__raw_spin_lock_flags(raw_spinlock_t
*lock
, unsigned long flags
)
107 unsigned long flags_dis
;
110 if (likely(__spin_trylock(lock
) == 0))
112 local_save_flags(flags_dis
);
113 local_irq_restore(flags
);
116 if (SHARED_PROCESSOR
)
118 } while (unlikely(lock
->slock
!= 0));
120 local_irq_restore(flags_dis
);
124 static __inline__
void __raw_spin_unlock(raw_spinlock_t
*lock
)
126 __asm__
__volatile__(SYNC_ON_SMP
" # __raw_spin_unlock"
132 extern void __raw_spin_unlock_wait(raw_spinlock_t
*lock
);
134 #define __raw_spin_unlock_wait(lock) \
135 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
139 * Read-write spinlocks, allowing multiple readers
140 * but only one writer.
142 * NOTE! it is quite common to have readers in interrupts
143 * but no interrupt writers. For those circumstances we
144 * can "mix" irq-safe locks - any writer needs to get a
145 * irq-safe write-lock, but readers can get non-irqsafe
149 #define __raw_read_can_lock(rw) ((rw)->lock >= 0)
150 #define __raw_write_can_lock(rw) (!(rw)->lock)
153 #define __DO_SIGN_EXTEND "extsw %0,%0\n"
154 #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
156 #define __DO_SIGN_EXTEND
157 #define WRLOCK_TOKEN (-1)
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(raw_rwlock_t
*rw
)
168 __asm__
__volatile__(
169 "1: lwarx %0,0,%1 # read_trylock\n"
179 : "cr0", "xer", "memory");
185 * This returns the old value in the lock,
186 * so we got the write lock if the return value is 0.
188 static __inline__
long __write_trylock(raw_rwlock_t
*rw
)
192 token
= WRLOCK_TOKEN
;
193 __asm__
__volatile__(
194 "1: lwarx %0,0,%2 # write_trylock\n\
202 : "r" (token
), "r" (&rw
->lock
)
208 static void __inline__
__raw_read_lock(raw_rwlock_t
*rw
)
211 if (likely(__read_trylock(rw
) > 0))
215 if (SHARED_PROCESSOR
)
217 } while (unlikely(rw
->lock
< 0));
222 static void __inline__
__raw_write_lock(raw_rwlock_t
*rw
)
225 if (likely(__write_trylock(rw
) == 0))
229 if (SHARED_PROCESSOR
)
231 } while (unlikely(rw
->lock
!= 0));
236 static int __inline__
__raw_read_trylock(raw_rwlock_t
*rw
)
238 return __read_trylock(rw
) > 0;
241 static int __inline__
__raw_write_trylock(raw_rwlock_t
*rw
)
243 return __write_trylock(rw
) == 0;
246 static void __inline__
__raw_read_unlock(raw_rwlock_t
*rw
)
250 __asm__
__volatile__(
251 "eieio # read_unlock\n\
262 static __inline__
void __raw_write_unlock(raw_rwlock_t
*rw
)
264 __asm__
__volatile__(SYNC_ON_SMP
" # write_unlock"
269 #endif /* __ASM_SPINLOCK_H */