4 * Written by Cort Dougan (cort@cs.nmt.edu)
7 #include <linux/sched.h>
8 #include <linux/spinlock.h>
9 #include <linux/module.h>
10 #include <asm/ppc_asm.h>
13 #ifdef CONFIG_DEBUG_SPINLOCK
16 #define INIT_STUCK 200000000 /*0xffffffff*/
19 * Try to acquire a spinlock.
20 * Only does the stwcx. if the load returned 0 - the Programming
21 * Environments Manual suggests not doing unnecessary stcwx.'s
22 * since they may inhibit forward progress by other CPUs in getting
25 static inline unsigned long __spin_trylock(volatile unsigned long *lock)
29 __asm__ __volatile__ ("\n\
45 void _raw_spin_lock(spinlock_t *lock)
47 int cpu = smp_processor_id();
48 unsigned int stuck = INIT_STUCK;
49 while (__spin_trylock(&lock->lock)) {
50 while ((unsigned volatile long)lock->lock != 0) {
52 printk("_spin_lock(%p) CPU#%d NIP %p"
53 " holder: cpu %ld pc %08lX\n",
54 lock, cpu, __builtin_return_address(0),
55 lock->owner_cpu,lock->owner_pc);
58 /*xchg_u32((void *)&lock->lock,0);*/
62 lock->owner_pc = (unsigned long)__builtin_return_address(0);
63 lock->owner_cpu = cpu;
65 EXPORT_SYMBOL(_raw_spin_lock);
67 int _raw_spin_trylock(spinlock_t *lock)
69 if (__spin_trylock(&lock->lock))
71 lock->owner_cpu = smp_processor_id();
72 lock->owner_pc = (unsigned long)__builtin_return_address(0);
75 EXPORT_SYMBOL(_raw_spin_trylock);
77 void _raw_spin_unlock(spinlock_t *lp)
80 printk("_spin_unlock(%p): no lock cpu %d curr PC %p %s/%d\n",
81 lp, smp_processor_id(), __builtin_return_address(0),
82 current->comm, current->pid);
83 if ( lp->owner_cpu != smp_processor_id() )
84 printk("_spin_unlock(%p): cpu %d trying clear of cpu %d pc %lx val %lx\n",
85 lp, smp_processor_id(), (int)lp->owner_cpu,
86 lp->owner_pc,lp->lock);
87 lp->owner_pc = lp->owner_cpu = 0;
91 EXPORT_SYMBOL(_raw_spin_unlock);
94 * For rwlocks, zero is unlocked, -1 is write-locked,
95 * positive is read-locked.
97 static __inline__ int __read_trylock(rwlock_t *rw)
101 __asm__ __volatile__(
102 "2: lwarx %0,0,%1 # __read_trylock\n\
117 int _raw_read_trylock(rwlock_t *rw)
119 return __read_trylock(rw) > 0;
121 EXPORT_SYMBOL(_raw_read_trylock);
123 void _raw_read_lock(rwlock_t *rw)
127 while (__read_trylock(rw) <= 0) {
129 while (!read_can_lock(rw)) {
131 printk("_read_lock(%p) CPU#%d lock %d\n",
132 rw, raw_smp_processor_id(), rw->lock);
138 EXPORT_SYMBOL(_raw_read_lock);
140 void _raw_read_unlock(rwlock_t *rw)
143 printk("_read_unlock(): %s/%d (nip %08lX) lock %d\n",
144 current->comm,current->pid,current->thread.regs->nip,
147 atomic_dec((atomic_t *) &(rw)->lock);
149 EXPORT_SYMBOL(_raw_read_unlock);
151 void _raw_write_lock(rwlock_t *rw)
155 while (cmpxchg(&rw->lock, 0, -1) != 0) {
157 while (!write_can_lock(rw)) {
159 printk("write_lock(%p) CPU#%d lock %d)\n",
160 rw, raw_smp_processor_id(), rw->lock);
167 EXPORT_SYMBOL(_raw_write_lock);
169 int _raw_write_trylock(rwlock_t *rw)
171 if (cmpxchg(&rw->lock, 0, -1) != 0)
176 EXPORT_SYMBOL(_raw_write_trylock);
178 void _raw_write_unlock(rwlock_t *rw)
181 printk("_write_lock(): %s/%d (nip %08lX) lock %d\n",
182 current->comm,current->pid,current->thread.regs->nip,
187 EXPORT_SYMBOL(_raw_write_unlock);