blob: ca61e846ab0f479406531a0996e14c623505705e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
4 * for more details.
5 *
Ralf Baechlef65e4fa2006-09-28 01:45:21 +01006 * Copyright (C) 1999, 2000, 06 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 */
9#ifndef _ASM_SPINLOCK_H
10#define _ASM_SPINLOCK_H
11
Ralf Baechle2a31b032008-08-28 15:17:49 +010012#include <linux/compiler.h>
13
Ralf Baechle0004a9d2006-10-31 03:45:07 +000014#include <asm/barrier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/war.h>
16
17/*
18 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ralf Baechle2a31b032008-08-28 15:17:49 +010019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Simple spin lock operations. There are two variants, one clears IRQ's
21 * on the local processor, one does not.
22 *
Ralf Baechle2a31b032008-08-28 15:17:49 +010023 * These are fair FIFO ticket locks
24 *
25 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Ralf Baechle2a31b032008-08-28 15:17:49 +010028
29/*
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.
35 */
36
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010037static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Ralf Baechle2a31b032008-08-28 15:17:49 +010038{
David Daney500c2e12010-02-04 11:31:49 -080039 u32 counters = ACCESS_ONCE(lock->lock);
Ralf Baechle2a31b032008-08-28 15:17:49 +010040
David Daney500c2e12010-02-04 11:31:49 -080041 return ((counters >> 16) ^ counters) & 0xffff;
Ralf Baechle2a31b032008-08-28 15:17:49 +010042}
43
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010044#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(); }
Ralf Baechle2a31b032008-08-28 15:17:49 +010047
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010048static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Ralf Baechle2a31b032008-08-28 15:17:49 +010049{
David Daney500c2e12010-02-04 11:31:49 -080050 u32 counters = ACCESS_ONCE(lock->lock);
Ralf Baechle2a31b032008-08-28 15:17:49 +010051
David Daney500c2e12010-02-04 11:31:49 -080052 return (((counters >> 16) - counters) & 0xffff) > 1;
Ralf Baechle2a31b032008-08-28 15:17:49 +010053}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010054#define arch_spin_is_contended arch_spin_is_contended
Ralf Baechle2a31b032008-08-28 15:17:49 +010055
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010056static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Ralf Baechle2a31b032008-08-28 15:17:49 +010058 int my_ticket;
59 int tmp;
David Daney500c2e12010-02-04 11:31:49 -080060 int inc = 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (R10000_LLSC_WAR) {
Ralf Baechle2a31b032008-08-28 15:17:49 +010063 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010064 " .set push # arch_spin_lock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010065 " .set noreorder \n"
66 " \n"
67 "1: ll %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -080068 " addu %[my_ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010069 " sc %[my_ticket], %[ticket_ptr] \n"
70 " beqzl %[my_ticket], 1b \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 " nop \n"
David Daney500c2e12010-02-04 11:31:49 -080072 " srl %[my_ticket], %[ticket], 16 \n"
73 " andi %[ticket], %[ticket], 0xffff \n"
74 " andi %[my_ticket], %[my_ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010075 " bne %[ticket], %[my_ticket], 4f \n"
76 " subu %[ticket], %[my_ticket], %[ticket] \n"
77 "2: \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010078 " .subsection 2 \n"
David Daney500c2e12010-02-04 11:31:49 -080079 "4: andi %[ticket], %[ticket], 0xffff \n"
David Daney0e6826c2009-03-27 10:07:02 -070080 " sll %[ticket], 5 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010081 " \n"
82 "6: bnez %[ticket], 6b \n"
83 " subu %[ticket], 1 \n"
84 " \n"
David Daney500c2e12010-02-04 11:31:49 -080085 " lhu %[ticket], %[serving_now_ptr] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010086 " beq %[ticket], %[my_ticket], 2b \n"
87 " subu %[ticket], %[my_ticket], %[ticket] \n"
David Daney0e6826c2009-03-27 10:07:02 -070088 " b 4b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010089 " subu %[ticket], %[ticket], 1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010090 " .previous \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010091 " .set pop \n"
92 : [ticket_ptr] "+m" (lock->lock),
David Daney500c2e12010-02-04 11:31:49 -080093 [serving_now_ptr] "+m" (lock->h.serving_now),
Ralf Baechle2a31b032008-08-28 15:17:49 +010094 [ticket] "=&r" (tmp),
David Daney500c2e12010-02-04 11:31:49 -080095 [my_ticket] "=&r" (my_ticket)
96 : [inc] "r" (inc));
Ralf Baechle2a31b032008-08-28 15:17:49 +010097 } else {
98 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010099 " .set push # arch_spin_lock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100100 " .set noreorder \n"
101 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800102 "1: ll %[ticket], %[ticket_ptr] \n"
103 " addu %[my_ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100104 " sc %[my_ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800105 " 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"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100109 " bne %[ticket], %[my_ticket], 4f \n"
110 " subu %[ticket], %[my_ticket], %[ticket] \n"
111 "2: \n"
112 " .subsection 2 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100113 "4: andi %[ticket], %[ticket], 0x1fff \n"
David Daney0e6826c2009-03-27 10:07:02 -0700114 " sll %[ticket], 5 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100115 " \n"
116 "6: bnez %[ticket], 6b \n"
117 " subu %[ticket], 1 \n"
118 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800119 " lhu %[ticket], %[serving_now_ptr] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100120 " beq %[ticket], %[my_ticket], 2b \n"
121 " subu %[ticket], %[my_ticket], %[ticket] \n"
David Daney0e6826c2009-03-27 10:07:02 -0700122 " b 4b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100123 " subu %[ticket], %[ticket], 1 \n"
124 " .previous \n"
125 " .set pop \n"
126 : [ticket_ptr] "+m" (lock->lock),
David Daney500c2e12010-02-04 11:31:49 -0800127 [serving_now_ptr] "+m" (lock->h.serving_now),
Ralf Baechle2a31b032008-08-28 15:17:49 +0100128 [ticket] "=&r" (tmp),
David Daney500c2e12010-02-04 11:31:49 -0800129 [my_ticket] "=&r" (my_ticket)
130 : [inc] "r" (inc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000132
Ralf Baechle17099b12007-07-14 13:24:05 +0100133 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100136static inline void arch_spin_unlock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
David Daney500c2e12010-02-04 11:31:49 -0800138 unsigned int serving_now = lock->h.serving_now + 1;
139 wmb();
140 lock->h.serving_now = (u16)serving_now;
141 nudge_writes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100144static inline unsigned int arch_spin_trylock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Ralf Baechle2a31b032008-08-28 15:17:49 +0100146 int tmp, tmp2, tmp3;
David Daney500c2e12010-02-04 11:31:49 -0800147 int inc = 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (R10000_LLSC_WAR) {
Ralf Baechle2a31b032008-08-28 15:17:49 +0100150 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100151 " .set push # arch_spin_trylock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100152 " .set noreorder \n"
153 " \n"
154 "1: ll %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800155 " srl %[my_ticket], %[ticket], 16 \n"
156 " andi %[my_ticket], %[my_ticket], 0xffff \n"
157 " andi %[now_serving], %[ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100158 " bne %[my_ticket], %[now_serving], 3f \n"
David Daney500c2e12010-02-04 11:31:49 -0800159 " addu %[ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100160 " sc %[ticket], %[ticket_ptr] \n"
161 " beqzl %[ticket], 1b \n"
162 " li %[ticket], 1 \n"
163 "2: \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100164 " .subsection 2 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100165 "3: b 2b \n"
166 " li %[ticket], 0 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100167 " .previous \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100168 " .set pop \n"
169 : [ticket_ptr] "+m" (lock->lock),
170 [ticket] "=&r" (tmp),
171 [my_ticket] "=&r" (tmp2),
David Daney500c2e12010-02-04 11:31:49 -0800172 [now_serving] "=&r" (tmp3)
173 : [inc] "r" (inc));
Ralf Baechle2a31b032008-08-28 15:17:49 +0100174 } else {
175 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100176 " .set push # arch_spin_trylock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100177 " .set noreorder \n"
178 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800179 "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"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100183 " bne %[my_ticket], %[now_serving], 3f \n"
David Daney500c2e12010-02-04 11:31:49 -0800184 " addu %[ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100185 " sc %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800186 " beqz %[ticket], 1b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100187 " li %[ticket], 1 \n"
188 "2: \n"
189 " .subsection 2 \n"
190 "3: b 2b \n"
191 " li %[ticket], 0 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100192 " .previous \n"
193 " .set pop \n"
194 : [ticket_ptr] "+m" (lock->lock),
195 [ticket] "=&r" (tmp),
196 [my_ticket] "=&r" (tmp2),
David Daney500c2e12010-02-04 11:31:49 -0800197 [now_serving] "=&r" (tmp3)
198 : [inc] "r" (inc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Ralf Baechle17099b12007-07-14 13:24:05 +0100201 smp_llsc_mb();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000202
Ralf Baechle2a31b032008-08-28 15:17:49 +0100203 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/*
207 * Read-write spinlocks, allowing multiple readers but only one writer.
208 *
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
212 * read-locks.
213 */
214
Ralf Baechlee3c48072005-02-03 13:34:45 +0000215/*
216 * read_can_lock - would read_trylock() succeed?
217 * @lock: the rwlock in question.
218 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100219#define arch_read_can_lock(rw) ((rw)->lock >= 0)
Ralf Baechlee3c48072005-02-03 13:34:45 +0000220
221/*
222 * write_can_lock - would write_trylock() succeed?
223 * @lock: the rwlock in question.
224 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100225#define arch_write_can_lock(rw) (!(rw)->lock)
Ralf Baechlee3c48072005-02-03 13:34:45 +0000226
Thomas Gleixnere5931942009-12-03 20:08:46 +0100227static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned int tmp;
230
231 if (R10000_LLSC_WAR) {
232 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100233 " .set noreorder # arch_read_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "1: ll %1, %2 \n"
235 " bltz %1, 1b \n"
236 " addu %1, 1 \n"
237 " sc %1, %0 \n"
238 " beqzl %1, 1b \n"
239 " nop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 " .set reorder \n"
241 : "=m" (rw->lock), "=&r" (tmp)
242 : "m" (rw->lock)
243 : "memory");
244 } else {
245 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100246 " .set noreorder # arch_read_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 "1: ll %1, %2 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100248 " bltz %1, 3f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 " addu %1, 1 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100250 "2: sc %1, %0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 " beqz %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000252 " nop \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100253 " .subsection 2 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100254 "3: ll %1, %2 \n"
255 " bltz %1, 3b \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100256 " addu %1, 1 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100257 " b 2b \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100258 " nop \n"
259 " .previous \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 " .set reorder \n"
261 : "=m" (rw->lock), "=&r" (tmp)
262 : "m" (rw->lock)
263 : "memory");
264 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000265
Ralf Baechle17099b12007-07-14 13:24:05 +0100266 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
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. */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100272static inline void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 unsigned int tmp;
275
David Daneyf252ffd2010-01-08 17:17:43 -0800276 smp_mb__before_llsc();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (R10000_LLSC_WAR) {
279 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100280 "1: ll %1, %2 # arch_read_unlock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 " sub %1, 1 \n"
282 " sc %1, %0 \n"
283 " beqzl %1, 1b \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 : "=m" (rw->lock), "=&r" (tmp)
285 : "m" (rw->lock)
286 : "memory");
287 } else {
288 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100289 " .set noreorder # arch_read_unlock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 "1: ll %1, %2 \n"
291 " sub %1, 1 \n"
292 " sc %1, %0 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100293 " beqz %1, 2f \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000294 " nop \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100295 " .subsection 2 \n"
296 "2: b 1b \n"
297 " nop \n"
298 " .previous \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 " .set reorder \n"
300 : "=m" (rw->lock), "=&r" (tmp)
301 : "m" (rw->lock)
302 : "memory");
303 }
304}
305
Thomas Gleixnere5931942009-12-03 20:08:46 +0100306static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 unsigned int tmp;
309
310 if (R10000_LLSC_WAR) {
311 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100312 " .set noreorder # arch_write_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 "1: ll %1, %2 \n"
314 " bnez %1, 1b \n"
315 " lui %1, 0x8000 \n"
316 " sc %1, %0 \n"
317 " beqzl %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000318 " nop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 " .set reorder \n"
320 : "=m" (rw->lock), "=&r" (tmp)
321 : "m" (rw->lock)
322 : "memory");
323 } else {
324 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100325 " .set noreorder # arch_write_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 "1: ll %1, %2 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100327 " bnez %1, 3f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 " lui %1, 0x8000 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100329 "2: sc %1, %0 \n"
330 " beqz %1, 3f \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000331 " nop \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100332 " .subsection 2 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100333 "3: ll %1, %2 \n"
334 " bnez %1, 3b \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100335 " lui %1, 0x8000 \n"
Ralf Baechle756cca62011-03-25 18:45:13 +0100336 " b 2b \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100337 " nop \n"
338 " .previous \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 " .set reorder \n"
340 : "=m" (rw->lock), "=&r" (tmp)
341 : "m" (rw->lock)
342 : "memory");
343 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000344
Ralf Baechle17099b12007-07-14 13:24:05 +0100345 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Thomas Gleixnere5931942009-12-03 20:08:46 +0100348static inline void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000350 smp_mb();
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100353 " # arch_write_unlock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 " sw $0, %0 \n"
355 : "=m" (rw->lock)
356 : "m" (rw->lock)
357 : "memory");
358}
359
Thomas Gleixnere5931942009-12-03 20:08:46 +0100360static inline int arch_read_trylock(arch_rwlock_t *rw)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100361{
362 unsigned int tmp;
363 int ret;
364
365 if (R10000_LLSC_WAR) {
366 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100367 " .set noreorder # arch_read_trylock \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100368 " li %2, 0 \n"
369 "1: ll %1, %3 \n"
Dave Johnsond52c2d52007-03-05 20:50:27 -0500370 " bltz %1, 2f \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100371 " addu %1, 1 \n"
372 " sc %1, %0 \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100373 " .set reorder \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000374 " beqzl %1, 1b \n"
375 " nop \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100376 __WEAK_LLSC_MB
Ralf Baechle65316fd2006-08-31 14:16:06 +0100377 " li %2, 1 \n"
378 "2: \n"
379 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
380 : "m" (rw->lock)
381 : "memory");
382 } else {
383 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100384 " .set noreorder # arch_read_trylock \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100385 " li %2, 0 \n"
386 "1: ll %1, %3 \n"
Dave Johnsond52c2d52007-03-05 20:50:27 -0500387 " bltz %1, 2f \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100388 " addu %1, 1 \n"
389 " sc %1, %0 \n"
390 " beqz %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000391 " nop \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100392 " .set reorder \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100393 __WEAK_LLSC_MB
Ralf Baechle65316fd2006-08-31 14:16:06 +0100394 " li %2, 1 \n"
395 "2: \n"
396 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
397 : "m" (rw->lock)
398 : "memory");
399 }
400
401 return ret;
402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Thomas Gleixnere5931942009-12-03 20:08:46 +0100404static inline int arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 unsigned int tmp;
407 int ret;
408
409 if (R10000_LLSC_WAR) {
410 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100411 " .set noreorder # arch_write_trylock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 " li %2, 0 \n"
413 "1: ll %1, %3 \n"
414 " bnez %1, 2f \n"
415 " lui %1, 0x8000 \n"
416 " sc %1, %0 \n"
417 " beqzl %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000418 " nop \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100419 __WEAK_LLSC_MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 " li %2, 1 \n"
421 " .set reorder \n"
422 "2: \n"
423 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
424 : "m" (rw->lock)
425 : "memory");
426 } else {
427 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100428 " .set noreorder # arch_write_trylock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 " li %2, 0 \n"
430 "1: ll %1, %3 \n"
431 " bnez %1, 2f \n"
432 " lui %1, 0x8000 \n"
433 " sc %1, %0 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100434 " beqz %1, 3f \n"
435 " li %2, 1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 "2: \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100437 __WEAK_LLSC_MB
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100438 " .subsection 2 \n"
439 "3: b 1b \n"
440 " li %2, 0 \n"
441 " .previous \n"
442 " .set reorder \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
444 : "m" (rw->lock)
445 : "memory");
446 }
447
448 return ret;
449}
450
Thomas Gleixnere5931942009-12-03 20:08:46 +0100451#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
452#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100453
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100454#define arch_spin_relax(lock) cpu_relax()
455#define arch_read_relax(lock) cpu_relax()
456#define arch_write_relax(lock) cpu_relax()
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif /* _ASM_SPINLOCK_H */