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 __raw_spin_is_locked(raw_spinlock_t
*lock
)
39 unsigned int counters
= ACCESS_ONCE(lock
->lock
);
41 return ((counters
>> 14) ^ counters
) & 0x1fff;
44 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
45 #define __raw_spin_unlock_wait(x) \
46 while (__raw_spin_is_locked(x)) { cpu_relax(); }
48 static inline int __raw_spin_is_contended(raw_spinlock_t
*lock
)
50 unsigned int counters
= ACCESS_ONCE(lock
->lock
);
52 return (((counters
>> 14) - counters
) & 0x1fff) > 1;
54 #define __raw_spin_is_contended __raw_spin_is_contended
56 static inline void __raw_spin_lock(raw_spinlock_t
*lock
)
61 if (R10000_LLSC_WAR
) {
62 __asm__
__volatile__ (
63 " .set push # __raw_spin_lock \n"
66 "1: ll %[ticket], %[ticket_ptr] \n"
67 " addiu %[my_ticket], %[ticket], 0x4000 \n"
68 " sc %[my_ticket], %[ticket_ptr] \n"
69 " beqzl %[my_ticket], 1b \n"
71 " srl %[my_ticket], %[ticket], 14 \n"
72 " andi %[my_ticket], %[my_ticket], 0x1fff \n"
73 " andi %[ticket], %[ticket], 0x1fff \n"
74 " bne %[ticket], %[my_ticket], 4f \n"
75 " subu %[ticket], %[my_ticket], %[ticket] \n"
78 "4: andi %[ticket], %[ticket], 0x1fff \n"
79 " sll %[ticket], 5 \n"
81 "6: bnez %[ticket], 6b \n"
82 " subu %[ticket], 1 \n"
84 " lw %[ticket], %[ticket_ptr] \n"
85 " andi %[ticket], %[ticket], 0x1fff \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
),
94 [my_ticket
] "=&r" (my_ticket
));
96 __asm__
__volatile__ (
97 " .set push # __raw_spin_lock \n"
100 " ll %[ticket], %[ticket_ptr] \n"
101 "1: addiu %[my_ticket], %[ticket], 0x4000 \n"
102 " sc %[my_ticket], %[ticket_ptr] \n"
103 " beqz %[my_ticket], 3f \n"
105 " srl %[my_ticket], %[ticket], 14 \n"
106 " andi %[my_ticket], %[my_ticket], 0x1fff \n"
107 " andi %[ticket], %[ticket], 0x1fff \n"
108 " bne %[ticket], %[my_ticket], 4f \n"
109 " subu %[ticket], %[my_ticket], %[ticket] \n"
113 " ll %[ticket], %[ticket_ptr] \n"
115 "4: andi %[ticket], %[ticket], 0x1fff \n"
116 " sll %[ticket], 5 \n"
118 "6: bnez %[ticket], 6b \n"
119 " subu %[ticket], 1 \n"
121 " lw %[ticket], %[ticket_ptr] \n"
122 " andi %[ticket], %[ticket], 0x1fff \n"
123 " beq %[ticket], %[my_ticket], 2b \n"
124 " subu %[ticket], %[my_ticket], %[ticket] \n"
126 " subu %[ticket], %[ticket], 1 \n"
129 : [ticket_ptr
] "+m" (lock
->lock
),
130 [ticket
] "=&r" (tmp
),
131 [my_ticket
] "=&r" (my_ticket
));
137 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
143 if (R10000_LLSC_WAR
) {
144 __asm__
__volatile__ (
145 " # __raw_spin_unlock \n"
146 "1: ll %[ticket], %[ticket_ptr] \n"
147 " addiu %[ticket], %[ticket], 1 \n"
148 " ori %[ticket], %[ticket], 0x2000 \n"
149 " xori %[ticket], %[ticket], 0x2000 \n"
150 " sc %[ticket], %[ticket_ptr] \n"
151 " beqzl %[ticket], 1b \n"
152 : [ticket_ptr
] "+m" (lock
->lock
),
153 [ticket
] "=&r" (tmp
));
155 __asm__
__volatile__ (
156 " .set push # __raw_spin_unlock \n"
159 " ll %[ticket], %[ticket_ptr] \n"
160 "1: addiu %[ticket], %[ticket], 1 \n"
161 " ori %[ticket], %[ticket], 0x2000 \n"
162 " xori %[ticket], %[ticket], 0x2000 \n"
163 " sc %[ticket], %[ticket_ptr] \n"
164 " beqz %[ticket], 2f \n"
169 " ll %[ticket], %[ticket_ptr] \n"
172 : [ticket_ptr
] "+m" (lock
->lock
),
173 [ticket
] "=&r" (tmp
));
177 static inline unsigned int __raw_spin_trylock(raw_spinlock_t
*lock
)
181 if (R10000_LLSC_WAR
) {
182 __asm__
__volatile__ (
183 " .set push # __raw_spin_trylock \n"
186 "1: ll %[ticket], %[ticket_ptr] \n"
187 " srl %[my_ticket], %[ticket], 14 \n"
188 " andi %[my_ticket], %[my_ticket], 0x1fff \n"
189 " andi %[now_serving], %[ticket], 0x1fff \n"
190 " bne %[my_ticket], %[now_serving], 3f \n"
191 " addiu %[ticket], %[ticket], 0x4000 \n"
192 " sc %[ticket], %[ticket_ptr] \n"
193 " beqzl %[ticket], 1b \n"
194 " li %[ticket], 1 \n"
198 " li %[ticket], 0 \n"
201 : [ticket_ptr
] "+m" (lock
->lock
),
202 [ticket
] "=&r" (tmp
),
203 [my_ticket
] "=&r" (tmp2
),
204 [now_serving
] "=&r" (tmp3
));
206 __asm__
__volatile__ (
207 " .set push # __raw_spin_trylock \n"
210 " ll %[ticket], %[ticket_ptr] \n"
211 "1: srl %[my_ticket], %[ticket], 14 \n"
212 " andi %[my_ticket], %[my_ticket], 0x1fff \n"
213 " andi %[now_serving], %[ticket], 0x1fff \n"
214 " bne %[my_ticket], %[now_serving], 3f \n"
215 " addiu %[ticket], %[ticket], 0x4000 \n"
216 " sc %[ticket], %[ticket_ptr] \n"
217 " beqz %[ticket], 4f \n"
218 " li %[ticket], 1 \n"
222 " li %[ticket], 0 \n"
224 " ll %[ticket], %[ticket_ptr] \n"
227 : [ticket_ptr
] "+m" (lock
->lock
),
228 [ticket
] "=&r" (tmp
),
229 [my_ticket
] "=&r" (tmp2
),
230 [now_serving
] "=&r" (tmp3
));
239 * Read-write spinlocks, allowing multiple readers but only one writer.
241 * NOTE! it is quite common to have readers in interrupts but no interrupt
242 * writers. For those circumstances we can "mix" irq-safe locks - any writer
243 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
248 * read_can_lock - would read_trylock() succeed?
249 * @lock: the rwlock in question.
251 #define __raw_read_can_lock(rw) ((rw)->lock >= 0)
254 * write_can_lock - would write_trylock() succeed?
255 * @lock: the rwlock in question.
257 #define __raw_write_can_lock(rw) (!(rw)->lock)
259 static inline void __raw_read_lock(raw_rwlock_t
*rw
)
263 if (R10000_LLSC_WAR
) {
264 __asm__
__volatile__(
265 " .set noreorder # __raw_read_lock \n"
273 : "=m" (rw
->lock
), "=&r" (tmp
)
277 __asm__
__volatile__(
278 " .set noreorder # __raw_read_lock \n"
293 : "=m" (rw
->lock
), "=&r" (tmp
)
301 /* Note the use of sub, not subu which will make the kernel die with an
302 overflow exception if we ever try to unlock an rwlock that is already
303 unlocked or is being held by a writer. */
304 static inline void __raw_read_unlock(raw_rwlock_t
*rw
)
310 if (R10000_LLSC_WAR
) {
311 __asm__
__volatile__(
312 "1: ll %1, %2 # __raw_read_unlock \n"
316 : "=m" (rw
->lock
), "=&r" (tmp
)
320 __asm__
__volatile__(
321 " .set noreorder # __raw_read_unlock \n"
332 : "=m" (rw
->lock
), "=&r" (tmp
)
338 static inline void __raw_write_lock(raw_rwlock_t
*rw
)
342 if (R10000_LLSC_WAR
) {
343 __asm__
__volatile__(
344 " .set noreorder # __raw_write_lock \n"
352 : "=m" (rw
->lock
), "=&r" (tmp
)
356 __asm__
__volatile__(
357 " .set noreorder # __raw_write_lock \n"
372 : "=m" (rw
->lock
), "=&r" (tmp
)
380 static inline void __raw_write_unlock(raw_rwlock_t
*rw
)
384 __asm__
__volatile__(
385 " # __raw_write_unlock \n"
392 static inline int __raw_read_trylock(raw_rwlock_t
*rw
)
397 if (R10000_LLSC_WAR
) {
398 __asm__
__volatile__(
399 " .set noreorder # __raw_read_trylock \n"
411 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
415 __asm__
__volatile__(
416 " .set noreorder # __raw_read_trylock \n"
428 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
436 static inline int __raw_write_trylock(raw_rwlock_t
*rw
)
441 if (R10000_LLSC_WAR
) {
442 __asm__
__volatile__(
443 " .set noreorder # __raw_write_trylock \n"
455 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
459 __asm__
__volatile__(
460 " .set noreorder # __raw_write_trylock \n"
475 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
483 #define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
484 #define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
486 #define _raw_spin_relax(lock) cpu_relax()
487 #define _raw_read_relax(lock) cpu_relax()
488 #define _raw_write_relax(lock) cpu_relax()
490 #endif /* _ASM_SPINLOCK_H */