2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1999, 2000, 06 Ralf Baechle (ralf@linux-mips.org)
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 #ifndef _ASM_SPINLOCK_H
10 #define _ASM_SPINLOCK_H
12 #include <linux/compiler.h>
14 #include <asm/barrier.h>
18 * Your basic SMP spinlocks, allowing only a single CPU anywhere
20 * Simple spin lock operations. There are two variants, one clears IRQ's
21 * on the local processor, one does not.
23 * These are fair FIFO ticket locks
25 * (the type definitions are in asm/spinlock_types.h)
30 * Ticket locks are conceptually two parts, one indicating the current head of
31 * the queue, and the other indicating the current tail. The lock is acquired
32 * by atomically noting the tail and incrementing it by one (thus adding
33 * ourself to the queue and noting our position), then waiting until the head
34 * becomes equal to the the initial value of the tail.
37 static inline int arch_spin_is_locked(arch_spinlock_t
*lock
)
39 u32 counters
= ACCESS_ONCE(lock
->lock
);
41 return ((counters
>> 16) ^ counters
) & 0xffff;
44 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
45 #define arch_spin_unlock_wait(x) \
46 while (arch_spin_is_locked(x)) { cpu_relax(); }
48 static inline int arch_spin_is_contended(arch_spinlock_t
*lock
)
50 u32 counters
= ACCESS_ONCE(lock
->lock
);
52 return (((counters
>> 16) - counters
) & 0xffff) > 1;
54 #define arch_spin_is_contended arch_spin_is_contended
56 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
62 if (R10000_LLSC_WAR
) {
63 __asm__
__volatile__ (
64 " .set push # arch_spin_lock \n"
67 "1: ll %[ticket], %[ticket_ptr] \n"
68 " addu %[my_ticket], %[ticket], %[inc] \n"
69 " sc %[my_ticket], %[ticket_ptr] \n"
70 " beqzl %[my_ticket], 1b \n"
72 " srl %[my_ticket], %[ticket], 16 \n"
73 " andi %[ticket], %[ticket], 0xffff \n"
74 " bne %[ticket], %[my_ticket], 4f \n"
75 " subu %[ticket], %[my_ticket], %[ticket] \n"
78 "4: andi %[ticket], %[ticket], 0xffff \n"
79 " sll %[ticket], 5 \n"
81 "6: bnez %[ticket], 6b \n"
82 " subu %[ticket], 1 \n"
84 " lhu %[ticket], %[serving_now_ptr] \n"
85 " beq %[ticket], %[my_ticket], 2b \n"
86 " subu %[ticket], %[my_ticket], %[ticket] \n"
88 " subu %[ticket], %[ticket], 1 \n"
91 : [ticket_ptr
] "+m" (lock
->lock
),
92 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
94 [my_ticket
] "=&r" (my_ticket
)
97 __asm__
__volatile__ (
98 " .set push # arch_spin_lock \n"
101 "1: ll %[ticket], %[ticket_ptr] \n"
102 " addu %[my_ticket], %[ticket], %[inc] \n"
103 " sc %[my_ticket], %[ticket_ptr] \n"
104 " beqz %[my_ticket], 1b \n"
105 " srl %[my_ticket], %[ticket], 16 \n"
106 " andi %[ticket], %[ticket], 0xffff \n"
107 " bne %[ticket], %[my_ticket], 4f \n"
108 " subu %[ticket], %[my_ticket], %[ticket] \n"
111 "4: andi %[ticket], %[ticket], 0x1fff \n"
112 " sll %[ticket], 5 \n"
114 "6: bnez %[ticket], 6b \n"
115 " subu %[ticket], 1 \n"
117 " lhu %[ticket], %[serving_now_ptr] \n"
118 " beq %[ticket], %[my_ticket], 2b \n"
119 " subu %[ticket], %[my_ticket], %[ticket] \n"
121 " subu %[ticket], %[ticket], 1 \n"
124 : [ticket_ptr
] "+m" (lock
->lock
),
125 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
126 [ticket
] "=&r" (tmp
),
127 [my_ticket
] "=&r" (my_ticket
)
134 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
136 unsigned int serving_now
= lock
->h
.serving_now
+ 1;
138 lock
->h
.serving_now
= (u16
)serving_now
;
142 static inline unsigned int arch_spin_trylock(arch_spinlock_t
*lock
)
147 if (R10000_LLSC_WAR
) {
148 __asm__
__volatile__ (
149 " .set push # arch_spin_trylock \n"
152 "1: ll %[ticket], %[ticket_ptr] \n"
153 " srl %[my_ticket], %[ticket], 16 \n"
154 " andi %[now_serving], %[ticket], 0xffff \n"
155 " bne %[my_ticket], %[now_serving], 3f \n"
156 " addu %[ticket], %[ticket], %[inc] \n"
157 " sc %[ticket], %[ticket_ptr] \n"
158 " beqzl %[ticket], 1b \n"
159 " li %[ticket], 1 \n"
163 " li %[ticket], 0 \n"
166 : [ticket_ptr
] "+m" (lock
->lock
),
167 [ticket
] "=&r" (tmp
),
168 [my_ticket
] "=&r" (tmp2
),
169 [now_serving
] "=&r" (tmp3
)
172 __asm__
__volatile__ (
173 " .set push # arch_spin_trylock \n"
176 "1: ll %[ticket], %[ticket_ptr] \n"
177 " srl %[my_ticket], %[ticket], 16 \n"
178 " andi %[now_serving], %[ticket], 0xffff \n"
179 " bne %[my_ticket], %[now_serving], 3f \n"
180 " addu %[ticket], %[ticket], %[inc] \n"
181 " sc %[ticket], %[ticket_ptr] \n"
182 " beqz %[ticket], 1b \n"
183 " li %[ticket], 1 \n"
187 " li %[ticket], 0 \n"
190 : [ticket_ptr
] "+m" (lock
->lock
),
191 [ticket
] "=&r" (tmp
),
192 [my_ticket
] "=&r" (tmp2
),
193 [now_serving
] "=&r" (tmp3
)
203 * Read-write spinlocks, allowing multiple readers but only one writer.
205 * NOTE! it is quite common to have readers in interrupts but no interrupt
206 * writers. For those circumstances we can "mix" irq-safe locks - any writer
207 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
212 * read_can_lock - would read_trylock() succeed?
213 * @lock: the rwlock in question.
215 #define arch_read_can_lock(rw) ((rw)->lock >= 0)
218 * write_can_lock - would write_trylock() succeed?
219 * @lock: the rwlock in question.
221 #define arch_write_can_lock(rw) (!(rw)->lock)
223 static inline void arch_read_lock(arch_rwlock_t
*rw
)
227 if (R10000_LLSC_WAR
) {
228 __asm__
__volatile__(
229 " .set noreorder # arch_read_lock \n"
237 : "=m" (rw
->lock
), "=&r" (tmp
)
242 __asm__
__volatile__(
243 "1: ll %1, %2 # arch_read_lock \n"
247 : "=m" (rw
->lock
), "=&r" (tmp
)
250 } while (unlikely(!tmp
));
256 /* Note the use of sub, not subu which will make the kernel die with an
257 overflow exception if we ever try to unlock an rwlock that is already
258 unlocked or is being held by a writer. */
259 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
263 smp_mb__before_llsc();
265 if (R10000_LLSC_WAR
) {
266 __asm__
__volatile__(
267 "1: ll %1, %2 # arch_read_unlock \n"
271 : "=m" (rw
->lock
), "=&r" (tmp
)
276 __asm__
__volatile__(
277 "1: ll %1, %2 # arch_read_unlock \n"
280 : "=m" (rw
->lock
), "=&r" (tmp
)
283 } while (unlikely(!tmp
));
287 static inline void arch_write_lock(arch_rwlock_t
*rw
)
291 if (R10000_LLSC_WAR
) {
292 __asm__
__volatile__(
293 " .set noreorder # arch_write_lock \n"
301 : "=m" (rw
->lock
), "=&r" (tmp
)
306 __asm__
__volatile__(
307 "1: ll %1, %2 # arch_write_lock \n"
311 : "=m" (rw
->lock
), "=&r" (tmp
)
314 } while (unlikely(!tmp
));
320 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
324 __asm__
__volatile__(
325 " # arch_write_unlock \n"
332 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
337 if (R10000_LLSC_WAR
) {
338 __asm__
__volatile__(
339 " .set noreorder # arch_read_trylock \n"
351 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
355 __asm__
__volatile__(
356 " .set noreorder # arch_read_trylock \n"
368 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
376 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
381 if (R10000_LLSC_WAR
) {
382 __asm__
__volatile__(
383 " .set noreorder # arch_write_trylock \n"
395 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
400 __asm__
__volatile__(
401 " ll %1, %3 # arch_write_trylock \n"
408 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
411 } while (unlikely(!tmp
));
419 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
420 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
422 #define arch_spin_relax(lock) cpu_relax()
423 #define arch_read_relax(lock) cpu_relax()
424 #define arch_write_relax(lock) cpu_relax()
426 #endif /* _ASM_SPINLOCK_H */