blob: ea4aee6b20e6e6a8074b96c46aa8c52cc25c7e40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Locks for smp ppc
3 *
4 * Written by Cort Dougan (cort@cs.nmt.edu)
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/sched.h>
8#include <linux/spinlock.h>
9#include <linux/module.h>
10#include <asm/ppc_asm.h>
11#include <asm/smp.h>
12
13#ifdef CONFIG_DEBUG_SPINLOCK
14
15#undef INIT_STUCK
16#define INIT_STUCK 200000000 /*0xffffffff*/
17
18/*
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
23 * a lock.
24 */
25static inline unsigned long __spin_trylock(volatile unsigned long *lock)
26{
27 unsigned long ret;
28
29 __asm__ __volatile__ ("\n\
301: lwarx %0,0,%1\n\
31 cmpwi 0,%0,0\n\
32 bne 2f\n"
33 PPC405_ERR77(0,%1)
34" stwcx. %2,0,%1\n\
35 bne- 1b\n\
36 isync\n\
372:"
38 : "=&r"(ret)
39 : "r"(lock), "r"(1)
40 : "cr0", "memory");
41
42 return ret;
43}
44
45void _raw_spin_lock(spinlock_t *lock)
46{
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) {
51 if (!--stuck) {
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);
56 stuck = INIT_STUCK;
57 /* steal the lock */
58 /*xchg_u32((void *)&lock->lock,0);*/
59 }
60 }
61 }
62 lock->owner_pc = (unsigned long)__builtin_return_address(0);
63 lock->owner_cpu = cpu;
64}
65EXPORT_SYMBOL(_raw_spin_lock);
66
67int _raw_spin_trylock(spinlock_t *lock)
68{
69 if (__spin_trylock(&lock->lock))
70 return 0;
71 lock->owner_cpu = smp_processor_id();
72 lock->owner_pc = (unsigned long)__builtin_return_address(0);
73 return 1;
74}
75EXPORT_SYMBOL(_raw_spin_trylock);
76
77void _raw_spin_unlock(spinlock_t *lp)
78{
79 if ( !lp->lock )
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;
88 wmb();
89 lp->lock = 0;
90}
91EXPORT_SYMBOL(_raw_spin_unlock);
92
93/*
94 * For rwlocks, zero is unlocked, -1 is write-locked,
95 * positive is read-locked.
96 */
97static __inline__ int __read_trylock(rwlock_t *rw)
98{
99 signed int tmp;
100
101 __asm__ __volatile__(
102"2: lwarx %0,0,%1 # __read_trylock\n\
103 addic. %0,%0,1\n\
104 ble- 1f\n"
105 PPC405_ERR77(0,%1)
106" stwcx. %0,0,%1\n\
107 bne- 2b\n\
108 isync\n\
1091:"
110 : "=&r"(tmp)
111 : "r"(&rw->lock)
112 : "cr0", "memory");
113
114 return tmp;
115}
116
117int _raw_read_trylock(rwlock_t *rw)
118{
119 return __read_trylock(rw) > 0;
120}
121EXPORT_SYMBOL(_raw_read_trylock);
122
123void _raw_read_lock(rwlock_t *rw)
124{
125 unsigned int stuck;
126
127 while (__read_trylock(rw) <= 0) {
128 stuck = INIT_STUCK;
129 while (!read_can_lock(rw)) {
130 if (--stuck == 0) {
131 printk("_read_lock(%p) CPU#%d lock %d\n",
Ingo Molnar39c715b2005-06-21 17:14:34 -0700132 rw, raw_smp_processor_id(), rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 stuck = INIT_STUCK;
134 }
135 }
136 }
137}
138EXPORT_SYMBOL(_raw_read_lock);
139
140void _raw_read_unlock(rwlock_t *rw)
141{
142 if ( rw->lock == 0 )
143 printk("_read_unlock(): %s/%d (nip %08lX) lock %d\n",
144 current->comm,current->pid,current->thread.regs->nip,
145 rw->lock);
146 wmb();
147 atomic_dec((atomic_t *) &(rw)->lock);
148}
149EXPORT_SYMBOL(_raw_read_unlock);
150
151void _raw_write_lock(rwlock_t *rw)
152{
153 unsigned int stuck;
154
155 while (cmpxchg(&rw->lock, 0, -1) != 0) {
156 stuck = INIT_STUCK;
157 while (!write_can_lock(rw)) {
158 if (--stuck == 0) {
159 printk("write_lock(%p) CPU#%d lock %d)\n",
Ingo Molnar39c715b2005-06-21 17:14:34 -0700160 rw, raw_smp_processor_id(), rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 stuck = INIT_STUCK;
162 }
163 }
164 }
165 wmb();
166}
167EXPORT_SYMBOL(_raw_write_lock);
168
169int _raw_write_trylock(rwlock_t *rw)
170{
171 if (cmpxchg(&rw->lock, 0, -1) != 0)
172 return 0;
173 wmb();
174 return 1;
175}
176EXPORT_SYMBOL(_raw_write_trylock);
177
178void _raw_write_unlock(rwlock_t *rw)
179{
180 if (rw->lock >= 0)
181 printk("_write_lock(): %s/%d (nip %08lX) lock %d\n",
182 current->comm,current->pid,current->thread.regs->nip,
183 rw->lock);
184 wmb();
185 rw->lock = 0;
186}
187EXPORT_SYMBOL(_raw_write_unlock);
188
189#endif