4 * Written by Cort Dougan (cort@cs.nmt.edu)
7 #include <linux/config.h>
8 #include <linux/sched.h>
9 #include <linux/spinlock.h>
10 #include <linux/module.h>
11 #include <asm/ppc_asm.h>
14 #ifdef CONFIG_DEBUG_SPINLOCK
17 #define INIT_STUCK 200000000 /*0xffffffff*/
20 * Try to acquire a spinlock.
21 * Only does the stwcx. if the load returned 0 - the Programming
22 * Environments Manual suggests not doing unnecessary stcwx.'s
23 * since they may inhibit forward progress by other CPUs in getting
26 static inline unsigned long __spin_trylock(volatile unsigned long *lock
)
30 __asm__
__volatile__ ("\n\
46 void _raw_spin_lock(spinlock_t
*lock
)
48 int cpu
= smp_processor_id();
49 unsigned int stuck
= INIT_STUCK
;
50 while (__spin_trylock(&lock
->lock
)) {
51 while ((unsigned volatile long)lock
->lock
!= 0) {
53 printk("_spin_lock(%p) CPU#%d NIP %p"
54 " holder: cpu %ld pc %08lX\n",
55 lock
, cpu
, __builtin_return_address(0),
56 lock
->owner_cpu
,lock
->owner_pc
);
59 /*xchg_u32((void *)&lock->lock,0);*/
63 lock
->owner_pc
= (unsigned long)__builtin_return_address(0);
64 lock
->owner_cpu
= cpu
;
66 EXPORT_SYMBOL(_raw_spin_lock
);
68 int _raw_spin_trylock(spinlock_t
*lock
)
70 if (__spin_trylock(&lock
->lock
))
72 lock
->owner_cpu
= smp_processor_id();
73 lock
->owner_pc
= (unsigned long)__builtin_return_address(0);
76 EXPORT_SYMBOL(_raw_spin_trylock
);
78 void _raw_spin_unlock(spinlock_t
*lp
)
81 printk("_spin_unlock(%p): no lock cpu %d curr PC %p %s/%d\n",
82 lp
, smp_processor_id(), __builtin_return_address(0),
83 current
->comm
, current
->pid
);
84 if ( lp
->owner_cpu
!= smp_processor_id() )
85 printk("_spin_unlock(%p): cpu %d trying clear of cpu %d pc %lx val %lx\n",
86 lp
, smp_processor_id(), (int)lp
->owner_cpu
,
87 lp
->owner_pc
,lp
->lock
);
88 lp
->owner_pc
= lp
->owner_cpu
= 0;
92 EXPORT_SYMBOL(_raw_spin_unlock
);
96 * Just like x86, implement read-write locks as a 32-bit counter
97 * with the high bit (sign) being the "write" bit.
100 void _raw_read_lock(rwlock_t
*rw
)
102 unsigned long stuck
= INIT_STUCK
;
103 int cpu
= smp_processor_id();
106 /* get our read lock in there */
107 atomic_inc((atomic_t
*) &(rw
)->lock
);
108 if ( (signed long)((rw
)->lock
) < 0) /* someone has a write lock */
110 /* turn off our read lock */
111 atomic_dec((atomic_t
*) &(rw
)->lock
);
112 /* wait for the write lock to go away */
113 while ((signed long)((rw
)->lock
) < 0)
117 printk("_read_lock(%p) CPU#%d\n", rw
, cpu
);
121 /* try to get the read lock again */
126 EXPORT_SYMBOL(_raw_read_lock
);
128 void _raw_read_unlock(rwlock_t
*rw
)
131 printk("_read_unlock(): %s/%d (nip %08lX) lock %lx\n",
132 current
->comm
,current
->pid
,current
->thread
.regs
->nip
,
135 atomic_dec((atomic_t
*) &(rw
)->lock
);
137 EXPORT_SYMBOL(_raw_read_unlock
);
139 void _raw_write_lock(rwlock_t
*rw
)
141 unsigned long stuck
= INIT_STUCK
;
142 int cpu
= smp_processor_id();
145 if ( test_and_set_bit(31,&(rw
)->lock
) ) /* someone has a write lock */
147 while ( (rw
)->lock
& (1<<31) ) /* wait for write lock */
151 printk("write_lock(%p) CPU#%d lock %lx)\n",
160 if ( (rw
)->lock
& ~(1<<31)) /* someone has a read lock */
162 /* clear our write lock and wait for reads to go away */
163 clear_bit(31,&(rw
)->lock
);
164 while ( (rw
)->lock
& ~(1<<31) )
168 printk("write_lock(%p) 2 CPU#%d lock %lx)\n",
178 EXPORT_SYMBOL(_raw_write_lock
);
180 int _raw_write_trylock(rwlock_t
*rw
)
182 if (test_and_set_bit(31, &(rw
)->lock
)) /* someone has a write lock */
185 if ((rw
)->lock
& ~(1<<31)) { /* someone has a read lock */
186 /* clear our write lock and wait for reads to go away */
187 clear_bit(31,&(rw
)->lock
);
193 EXPORT_SYMBOL(_raw_write_trylock
);
195 void _raw_write_unlock(rwlock_t
*rw
)
197 if ( !(rw
->lock
& (1<<31)) )
198 printk("_write_lock(): %s/%d (nip %08lX) lock %lx\n",
199 current
->comm
,current
->pid
,current
->thread
.regs
->nip
,
202 clear_bit(31,&(rw
)->lock
);
204 EXPORT_SYMBOL(_raw_write_unlock
);