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 " andi %[my_ticket], %[my_ticket], 0xffff \n"
75 " bne %[ticket], %[my_ticket], 4f \n"
76 " subu %[ticket], %[my_ticket], %[ticket] \n"
79 "4: andi %[ticket], %[ticket], 0xffff \n"
80 " sll %[ticket], 5 \n"
82 "6: bnez %[ticket], 6b \n"
83 " subu %[ticket], 1 \n"
85 " lhu %[ticket], %[serving_now_ptr] \n"
86 " beq %[ticket], %[my_ticket], 2b \n"
87 " subu %[ticket], %[my_ticket], %[ticket] \n"
89 " subu %[ticket], %[ticket], 1 \n"
92 : [ticket_ptr
] "+m" (lock
->lock
),
93 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
95 [my_ticket
] "=&r" (my_ticket
)
98 __asm__
__volatile__ (
99 " .set push # arch_spin_lock \n"
102 "1: ll %[ticket], %[ticket_ptr] \n"
103 " addu %[my_ticket], %[ticket], %[inc] \n"
104 " sc %[my_ticket], %[ticket_ptr] \n"
105 " beqz %[my_ticket], 1b \n"
106 " srl %[my_ticket], %[ticket], 16 \n"
107 " andi %[ticket], %[ticket], 0xffff \n"
108 " andi %[my_ticket], %[my_ticket], 0xffff \n"
109 " bne %[ticket], %[my_ticket], 4f \n"
110 " subu %[ticket], %[my_ticket], %[ticket] \n"
113 "4: andi %[ticket], %[ticket], 0x1fff \n"
114 " sll %[ticket], 5 \n"
116 "6: bnez %[ticket], 6b \n"
117 " subu %[ticket], 1 \n"
119 " lhu %[ticket], %[serving_now_ptr] \n"
120 " beq %[ticket], %[my_ticket], 2b \n"
121 " subu %[ticket], %[my_ticket], %[ticket] \n"
123 " subu %[ticket], %[ticket], 1 \n"
126 : [ticket_ptr
] "+m" (lock
->lock
),
127 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
128 [ticket
] "=&r" (tmp
),
129 [my_ticket
] "=&r" (my_ticket
)
136 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
138 unsigned int serving_now
= lock
->h
.serving_now
+ 1;
140 lock
->h
.serving_now
= (u16
)serving_now
;
144 static inline unsigned int arch_spin_trylock(arch_spinlock_t
*lock
)
149 if (R10000_LLSC_WAR
) {
150 __asm__
__volatile__ (
151 " .set push # arch_spin_trylock \n"
154 "1: ll %[ticket], %[ticket_ptr] \n"
155 " srl %[my_ticket], %[ticket], 16 \n"
156 " andi %[my_ticket], %[my_ticket], 0xffff \n"
157 " andi %[now_serving], %[ticket], 0xffff \n"
158 " bne %[my_ticket], %[now_serving], 3f \n"
159 " addu %[ticket], %[ticket], %[inc] \n"
160 " sc %[ticket], %[ticket_ptr] \n"
161 " beqzl %[ticket], 1b \n"
162 " li %[ticket], 1 \n"
166 " li %[ticket], 0 \n"
169 : [ticket_ptr
] "+m" (lock
->lock
),
170 [ticket
] "=&r" (tmp
),
171 [my_ticket
] "=&r" (tmp2
),
172 [now_serving
] "=&r" (tmp3
)
175 __asm__
__volatile__ (
176 " .set push # arch_spin_trylock \n"
179 "1: ll %[ticket], %[ticket_ptr] \n"
180 " srl %[my_ticket], %[ticket], 16 \n"
181 " andi %[my_ticket], %[my_ticket], 0xffff \n"
182 " andi %[now_serving], %[ticket], 0xffff \n"
183 " bne %[my_ticket], %[now_serving], 3f \n"
184 " addu %[ticket], %[ticket], %[inc] \n"
185 " sc %[ticket], %[ticket_ptr] \n"
186 " beqz %[ticket], 1b \n"
187 " li %[ticket], 1 \n"
191 " li %[ticket], 0 \n"
194 : [ticket_ptr
] "+m" (lock
->lock
),
195 [ticket
] "=&r" (tmp
),
196 [my_ticket
] "=&r" (tmp2
),
197 [now_serving
] "=&r" (tmp3
)
207 * Read-write spinlocks, allowing multiple readers but only one writer.
209 * NOTE! it is quite common to have readers in interrupts but no interrupt
210 * writers. For those circumstances we can "mix" irq-safe locks - any writer
211 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
216 * read_can_lock - would read_trylock() succeed?
217 * @lock: the rwlock in question.
219 #define arch_read_can_lock(rw) ((rw)->lock >= 0)
222 * write_can_lock - would write_trylock() succeed?
223 * @lock: the rwlock in question.
225 #define arch_write_can_lock(rw) (!(rw)->lock)
227 static inline void arch_read_lock(arch_rwlock_t
*rw
)
231 if (R10000_LLSC_WAR
) {
232 __asm__
__volatile__(
233 " .set noreorder # arch_read_lock \n"
241 : "=m" (rw
->lock
), "=&r" (tmp
)
245 __asm__
__volatile__(
246 " .set noreorder # arch_read_lock \n"
261 : "=m" (rw
->lock
), "=&r" (tmp
)
269 /* Note the use of sub, not subu which will make the kernel die with an
270 overflow exception if we ever try to unlock an rwlock that is already
271 unlocked or is being held by a writer. */
272 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
276 smp_mb__before_llsc();
278 if (R10000_LLSC_WAR
) {
279 __asm__
__volatile__(
280 "1: ll %1, %2 # arch_read_unlock \n"
284 : "=m" (rw
->lock
), "=&r" (tmp
)
288 __asm__
__volatile__(
289 " .set noreorder # arch_read_unlock \n"
300 : "=m" (rw
->lock
), "=&r" (tmp
)
306 static inline void arch_write_lock(arch_rwlock_t
*rw
)
310 if (R10000_LLSC_WAR
) {
311 __asm__
__volatile__(
312 " .set noreorder # arch_write_lock \n"
320 : "=m" (rw
->lock
), "=&r" (tmp
)
324 __asm__
__volatile__(
325 " .set noreorder # arch_write_lock \n"
340 : "=m" (rw
->lock
), "=&r" (tmp
)
348 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
352 __asm__
__volatile__(
353 " # arch_write_unlock \n"
360 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
365 if (R10000_LLSC_WAR
) {
366 __asm__
__volatile__(
367 " .set noreorder # arch_read_trylock \n"
379 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
383 __asm__
__volatile__(
384 " .set noreorder # arch_read_trylock \n"
396 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
404 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
409 if (R10000_LLSC_WAR
) {
410 __asm__
__volatile__(
411 " .set noreorder # arch_write_trylock \n"
423 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
427 __asm__
__volatile__(
428 " .set noreorder # arch_write_trylock \n"
443 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
451 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
452 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
454 #define arch_spin_relax(lock) cpu_relax()
455 #define arch_read_relax(lock) cpu_relax()
456 #define arch_write_relax(lock) cpu_relax()
458 #endif /* _ASM_SPINLOCK_H */