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>
15 #include <asm/compiler.h>
19 * Your basic SMP spinlocks, allowing only a single CPU anywhere
21 * Simple spin lock operations. There are two variants, one clears IRQ's
22 * on the local processor, one does not.
24 * These are fair FIFO ticket locks
26 * (the type definitions are in asm/spinlock_types.h)
31 * Ticket locks are conceptually two parts, one indicating the current head of
32 * the queue, and the other indicating the current tail. The lock is acquired
33 * by atomically noting the tail and incrementing it by one (thus adding
34 * ourself to the queue and noting our position), then waiting until the head
35 * becomes equal to the the initial value of the tail.
38 static inline int arch_spin_is_locked(arch_spinlock_t
*lock
)
40 u32 counters
= ACCESS_ONCE(lock
->lock
);
42 return ((counters
>> 16) ^ counters
) & 0xffff;
45 static inline int arch_spin_value_unlocked(arch_spinlock_t lock
)
47 return lock
.h
.serving_now
== lock
.h
.ticket
;
50 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
51 #define arch_spin_unlock_wait(x) \
52 while (arch_spin_is_locked(x)) { cpu_relax(); }
54 static inline int arch_spin_is_contended(arch_spinlock_t
*lock
)
56 u32 counters
= ACCESS_ONCE(lock
->lock
);
58 return (((counters
>> 16) - counters
) & 0xffff) > 1;
60 #define arch_spin_is_contended arch_spin_is_contended
62 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
68 if (R10000_LLSC_WAR
) {
69 __asm__
__volatile__ (
70 " .set push # arch_spin_lock \n"
73 "1: ll %[ticket], %[ticket_ptr] \n"
74 " addu %[my_ticket], %[ticket], %[inc] \n"
75 " sc %[my_ticket], %[ticket_ptr] \n"
76 " beqzl %[my_ticket], 1b \n"
78 " srl %[my_ticket], %[ticket], 16 \n"
79 " andi %[ticket], %[ticket], 0xffff \n"
80 " bne %[ticket], %[my_ticket], 4f \n"
81 " subu %[ticket], %[my_ticket], %[ticket] \n"
84 "4: andi %[ticket], %[ticket], 0xffff \n"
85 " sll %[ticket], 5 \n"
87 "6: bnez %[ticket], 6b \n"
88 " subu %[ticket], 1 \n"
90 " lhu %[ticket], %[serving_now_ptr] \n"
91 " beq %[ticket], %[my_ticket], 2b \n"
92 " subu %[ticket], %[my_ticket], %[ticket] \n"
94 " subu %[ticket], %[ticket], 1 \n"
97 : [ticket_ptr
] "+" GCC_OFF_SMALL_ASM() (lock
->lock
),
98 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
100 [my_ticket
] "=&r" (my_ticket
)
103 __asm__
__volatile__ (
104 " .set push # arch_spin_lock \n"
107 "1: ll %[ticket], %[ticket_ptr] \n"
108 " addu %[my_ticket], %[ticket], %[inc] \n"
109 " sc %[my_ticket], %[ticket_ptr] \n"
110 " beqz %[my_ticket], 1b \n"
111 " srl %[my_ticket], %[ticket], 16 \n"
112 " andi %[ticket], %[ticket], 0xffff \n"
113 " bne %[ticket], %[my_ticket], 4f \n"
114 " subu %[ticket], %[my_ticket], %[ticket] \n"
117 "4: andi %[ticket], %[ticket], 0xffff \n"
118 " sll %[ticket], 5 \n"
120 "6: bnez %[ticket], 6b \n"
121 " subu %[ticket], 1 \n"
123 " lhu %[ticket], %[serving_now_ptr] \n"
124 " beq %[ticket], %[my_ticket], 2b \n"
125 " subu %[ticket], %[my_ticket], %[ticket] \n"
127 " subu %[ticket], %[ticket], 1 \n"
130 : [ticket_ptr
] "+" GCC_OFF_SMALL_ASM() (lock
->lock
),
131 [serving_now_ptr
] "+m" (lock
->h
.serving_now
),
132 [ticket
] "=&r" (tmp
),
133 [my_ticket
] "=&r" (my_ticket
)
140 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
142 unsigned int serving_now
= lock
->h
.serving_now
+ 1;
144 lock
->h
.serving_now
= (u16
)serving_now
;
148 static inline unsigned int arch_spin_trylock(arch_spinlock_t
*lock
)
153 if (R10000_LLSC_WAR
) {
154 __asm__
__volatile__ (
155 " .set push # arch_spin_trylock \n"
158 "1: ll %[ticket], %[ticket_ptr] \n"
159 " srl %[my_ticket], %[ticket], 16 \n"
160 " andi %[now_serving], %[ticket], 0xffff \n"
161 " bne %[my_ticket], %[now_serving], 3f \n"
162 " addu %[ticket], %[ticket], %[inc] \n"
163 " sc %[ticket], %[ticket_ptr] \n"
164 " beqzl %[ticket], 1b \n"
165 " li %[ticket], 1 \n"
169 " li %[ticket], 0 \n"
172 : [ticket_ptr
] "+" GCC_OFF_SMALL_ASM() (lock
->lock
),
173 [ticket
] "=&r" (tmp
),
174 [my_ticket
] "=&r" (tmp2
),
175 [now_serving
] "=&r" (tmp3
)
178 __asm__
__volatile__ (
179 " .set push # arch_spin_trylock \n"
182 "1: ll %[ticket], %[ticket_ptr] \n"
183 " srl %[my_ticket], %[ticket], 16 \n"
184 " andi %[now_serving], %[ticket], 0xffff \n"
185 " bne %[my_ticket], %[now_serving], 3f \n"
186 " addu %[ticket], %[ticket], %[inc] \n"
187 " sc %[ticket], %[ticket_ptr] \n"
188 " beqz %[ticket], 1b \n"
189 " li %[ticket], 1 \n"
193 " li %[ticket], 0 \n"
196 : [ticket_ptr
] "+" GCC_OFF_SMALL_ASM() (lock
->lock
),
197 [ticket
] "=&r" (tmp
),
198 [my_ticket
] "=&r" (tmp2
),
199 [now_serving
] "=&r" (tmp3
)
209 * Read-write spinlocks, allowing multiple readers but only one writer.
211 * NOTE! it is quite common to have readers in interrupts but no interrupt
212 * writers. For those circumstances we can "mix" irq-safe locks - any writer
213 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
218 * read_can_lock - would read_trylock() succeed?
219 * @lock: the rwlock in question.
221 #define arch_read_can_lock(rw) ((rw)->lock >= 0)
224 * write_can_lock - would write_trylock() succeed?
225 * @lock: the rwlock in question.
227 #define arch_write_can_lock(rw) (!(rw)->lock)
229 static inline void arch_read_lock(arch_rwlock_t
*rw
)
233 if (R10000_LLSC_WAR
) {
234 __asm__
__volatile__(
235 " .set noreorder # arch_read_lock \n"
243 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
244 : GCC_OFF_SMALL_ASM() (rw
->lock
)
248 __asm__
__volatile__(
249 "1: ll %1, %2 # arch_read_lock \n"
253 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
254 : GCC_OFF_SMALL_ASM() (rw
->lock
)
256 } while (unlikely(!tmp
));
262 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
266 smp_mb__before_llsc();
268 if (R10000_LLSC_WAR
) {
269 __asm__
__volatile__(
270 "1: ll %1, %2 # arch_read_unlock \n"
274 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
275 : GCC_OFF_SMALL_ASM() (rw
->lock
)
279 __asm__
__volatile__(
280 "1: ll %1, %2 # arch_read_unlock \n"
283 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
284 : GCC_OFF_SMALL_ASM() (rw
->lock
)
286 } while (unlikely(!tmp
));
290 static inline void arch_write_lock(arch_rwlock_t
*rw
)
294 if (R10000_LLSC_WAR
) {
295 __asm__
__volatile__(
296 " .set noreorder # arch_write_lock \n"
304 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
305 : GCC_OFF_SMALL_ASM() (rw
->lock
)
309 __asm__
__volatile__(
310 "1: ll %1, %2 # arch_write_lock \n"
314 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
)
315 : GCC_OFF_SMALL_ASM() (rw
->lock
)
317 } while (unlikely(!tmp
));
323 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
325 smp_mb__before_llsc();
327 __asm__
__volatile__(
328 " # arch_write_unlock \n"
335 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
340 if (R10000_LLSC_WAR
) {
341 __asm__
__volatile__(
342 " .set noreorder # arch_read_trylock \n"
354 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
355 : GCC_OFF_SMALL_ASM() (rw
->lock
)
358 __asm__
__volatile__(
359 " .set noreorder # arch_read_trylock \n"
371 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
372 : GCC_OFF_SMALL_ASM() (rw
->lock
)
379 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
384 if (R10000_LLSC_WAR
) {
385 __asm__
__volatile__(
386 " .set noreorder # arch_write_trylock \n"
398 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
399 : GCC_OFF_SMALL_ASM() (rw
->lock
)
403 __asm__
__volatile__(
404 " ll %1, %3 # arch_write_trylock \n"
411 : "=" GCC_OFF_SMALL_ASM() (rw
->lock
), "=&r" (tmp
),
413 : GCC_OFF_SMALL_ASM() (rw
->lock
)
415 } while (unlikely(!tmp
));
423 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
424 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
426 #define arch_spin_relax(lock) cpu_relax()
427 #define arch_read_relax(lock) cpu_relax()
428 #define arch_write_relax(lock) cpu_relax()
430 #endif /* _ASM_SPINLOCK_H */