blob: 40196bebe849a0c825cc8a61e40b03440d14f31e [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>
Maciej W. Rozyckib0984c42014-11-15 22:08:48 +000015#include <asm/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/war.h>
17
18/*
19 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ralf Baechle2a31b032008-08-28 15:17:49 +010020 *
Ralf Baechle70342282013-01-22 12:59:30 +010021 * Simple spin lock operations. There are two variants, one clears IRQ's
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * on the local processor, one does not.
23 *
Ralf Baechle2a31b032008-08-28 15:17:49 +010024 * These are fair FIFO ticket locks
25 *
26 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
Ralf Baechle2a31b032008-08-28 15:17:49 +010029
30/*
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.
36 */
37
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010038static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Ralf Baechle2a31b032008-08-28 15:17:49 +010039{
David Daney500c2e12010-02-04 11:31:49 -080040 u32 counters = ACCESS_ONCE(lock->lock);
Ralf Baechle2a31b032008-08-28 15:17:49 +010041
David Daney500c2e12010-02-04 11:31:49 -080042 return ((counters >> 16) ^ counters) & 0xffff;
Ralf Baechle2a31b032008-08-28 15:17:49 +010043}
44
Paul Burton5fac4f72015-07-30 08:16:10 -070045static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
46{
47 return lock.h.serving_now == lock.h.ticket;
48}
49
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010050#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(); }
Ralf Baechle2a31b032008-08-28 15:17:49 +010053
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010054static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Ralf Baechle2a31b032008-08-28 15:17:49 +010055{
David Daney500c2e12010-02-04 11:31:49 -080056 u32 counters = ACCESS_ONCE(lock->lock);
Ralf Baechle2a31b032008-08-28 15:17:49 +010057
David Daney500c2e12010-02-04 11:31:49 -080058 return (((counters >> 16) - counters) & 0xffff) > 1;
Ralf Baechle2a31b032008-08-28 15:17:49 +010059}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010060#define arch_spin_is_contended arch_spin_is_contended
Ralf Baechle2a31b032008-08-28 15:17:49 +010061
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010062static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Ralf Baechle2a31b032008-08-28 15:17:49 +010064 int my_ticket;
65 int tmp;
David Daney500c2e12010-02-04 11:31:49 -080066 int inc = 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (R10000_LLSC_WAR) {
Ralf Baechle2a31b032008-08-28 15:17:49 +010069 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010070 " .set push # arch_spin_lock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010071 " .set noreorder \n"
72 " \n"
73 "1: ll %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -080074 " addu %[my_ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010075 " sc %[my_ticket], %[ticket_ptr] \n"
76 " beqzl %[my_ticket], 1b \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 " nop \n"
David Daney500c2e12010-02-04 11:31:49 -080078 " srl %[my_ticket], %[ticket], 16 \n"
79 " andi %[ticket], %[ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010080 " bne %[ticket], %[my_ticket], 4f \n"
81 " subu %[ticket], %[my_ticket], %[ticket] \n"
82 "2: \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010083 " .subsection 2 \n"
David Daney500c2e12010-02-04 11:31:49 -080084 "4: andi %[ticket], %[ticket], 0xffff \n"
David Daney0e6826c2009-03-27 10:07:02 -070085 " sll %[ticket], 5 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010086 " \n"
87 "6: bnez %[ticket], 6b \n"
88 " subu %[ticket], 1 \n"
89 " \n"
David Daney500c2e12010-02-04 11:31:49 -080090 " lhu %[ticket], %[serving_now_ptr] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010091 " beq %[ticket], %[my_ticket], 2b \n"
92 " subu %[ticket], %[my_ticket], %[ticket] \n"
David Daney0e6826c2009-03-27 10:07:02 -070093 " b 4b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010094 " subu %[ticket], %[ticket], 1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010095 " .previous \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +010096 " .set pop \n"
Markos Chandras94bfb752015-01-26 12:44:11 +000097 : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
David Daney500c2e12010-02-04 11:31:49 -080098 [serving_now_ptr] "+m" (lock->h.serving_now),
Ralf Baechle2a31b032008-08-28 15:17:49 +010099 [ticket] "=&r" (tmp),
David Daney500c2e12010-02-04 11:31:49 -0800100 [my_ticket] "=&r" (my_ticket)
101 : [inc] "r" (inc));
Ralf Baechle2a31b032008-08-28 15:17:49 +0100102 } else {
103 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100104 " .set push # arch_spin_lock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100105 " .set noreorder \n"
106 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800107 "1: ll %[ticket], %[ticket_ptr] \n"
108 " addu %[my_ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100109 " sc %[my_ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800110 " beqz %[my_ticket], 1b \n"
111 " srl %[my_ticket], %[ticket], 16 \n"
112 " andi %[ticket], %[ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100113 " bne %[ticket], %[my_ticket], 4f \n"
114 " subu %[ticket], %[my_ticket], %[ticket] \n"
115 "2: \n"
116 " .subsection 2 \n"
Markos Chandras9ff897c2015-04-20 10:54:34 +0100117 "4: andi %[ticket], %[ticket], 0xffff \n"
David Daney0e6826c2009-03-27 10:07:02 -0700118 " sll %[ticket], 5 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100119 " \n"
120 "6: bnez %[ticket], 6b \n"
121 " subu %[ticket], 1 \n"
122 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800123 " lhu %[ticket], %[serving_now_ptr] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100124 " beq %[ticket], %[my_ticket], 2b \n"
125 " subu %[ticket], %[my_ticket], %[ticket] \n"
David Daney0e6826c2009-03-27 10:07:02 -0700126 " b 4b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100127 " subu %[ticket], %[ticket], 1 \n"
128 " .previous \n"
129 " .set pop \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000130 : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
David Daney500c2e12010-02-04 11:31:49 -0800131 [serving_now_ptr] "+m" (lock->h.serving_now),
Ralf Baechle2a31b032008-08-28 15:17:49 +0100132 [ticket] "=&r" (tmp),
David Daney500c2e12010-02-04 11:31:49 -0800133 [my_ticket] "=&r" (my_ticket)
134 : [inc] "r" (inc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000136
Ralf Baechle17099b12007-07-14 13:24:05 +0100137 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100140static inline void arch_spin_unlock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
David Daney500c2e12010-02-04 11:31:49 -0800142 unsigned int serving_now = lock->h.serving_now + 1;
143 wmb();
144 lock->h.serving_now = (u16)serving_now;
145 nudge_writes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100148static inline unsigned int arch_spin_trylock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Ralf Baechle2a31b032008-08-28 15:17:49 +0100150 int tmp, tmp2, tmp3;
David Daney500c2e12010-02-04 11:31:49 -0800151 int inc = 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (R10000_LLSC_WAR) {
Ralf Baechle2a31b032008-08-28 15:17:49 +0100154 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100155 " .set push # arch_spin_trylock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100156 " .set noreorder \n"
157 " \n"
158 "1: ll %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800159 " srl %[my_ticket], %[ticket], 16 \n"
David Daney500c2e12010-02-04 11:31:49 -0800160 " andi %[now_serving], %[ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100161 " bne %[my_ticket], %[now_serving], 3f \n"
David Daney500c2e12010-02-04 11:31:49 -0800162 " addu %[ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100163 " sc %[ticket], %[ticket_ptr] \n"
164 " beqzl %[ticket], 1b \n"
165 " li %[ticket], 1 \n"
166 "2: \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100167 " .subsection 2 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100168 "3: b 2b \n"
169 " li %[ticket], 0 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100170 " .previous \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100171 " .set pop \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000172 : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
Ralf Baechle2a31b032008-08-28 15:17:49 +0100173 [ticket] "=&r" (tmp),
174 [my_ticket] "=&r" (tmp2),
David Daney500c2e12010-02-04 11:31:49 -0800175 [now_serving] "=&r" (tmp3)
176 : [inc] "r" (inc));
Ralf Baechle2a31b032008-08-28 15:17:49 +0100177 } else {
178 __asm__ __volatile__ (
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100179 " .set push # arch_spin_trylock \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100180 " .set noreorder \n"
181 " \n"
David Daney500c2e12010-02-04 11:31:49 -0800182 "1: ll %[ticket], %[ticket_ptr] \n"
183 " srl %[my_ticket], %[ticket], 16 \n"
David Daney500c2e12010-02-04 11:31:49 -0800184 " andi %[now_serving], %[ticket], 0xffff \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100185 " bne %[my_ticket], %[now_serving], 3f \n"
David Daney500c2e12010-02-04 11:31:49 -0800186 " addu %[ticket], %[ticket], %[inc] \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100187 " sc %[ticket], %[ticket_ptr] \n"
David Daney500c2e12010-02-04 11:31:49 -0800188 " beqz %[ticket], 1b \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100189 " li %[ticket], 1 \n"
190 "2: \n"
191 " .subsection 2 \n"
192 "3: b 2b \n"
193 " li %[ticket], 0 \n"
Ralf Baechle2a31b032008-08-28 15:17:49 +0100194 " .previous \n"
195 " .set pop \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000196 : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
Ralf Baechle2a31b032008-08-28 15:17:49 +0100197 [ticket] "=&r" (tmp),
198 [my_ticket] "=&r" (tmp2),
David Daney500c2e12010-02-04 11:31:49 -0800199 [now_serving] "=&r" (tmp3)
200 : [inc] "r" (inc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Ralf Baechle17099b12007-07-14 13:24:05 +0100203 smp_llsc_mb();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000204
Ralf Baechle2a31b032008-08-28 15:17:49 +0100205 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
209 * Read-write spinlocks, allowing multiple readers but only one writer.
210 *
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
214 * read-locks.
215 */
216
Ralf Baechlee3c48072005-02-03 13:34:45 +0000217/*
218 * read_can_lock - would read_trylock() succeed?
219 * @lock: the rwlock in question.
220 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100221#define arch_read_can_lock(rw) ((rw)->lock >= 0)
Ralf Baechlee3c48072005-02-03 13:34:45 +0000222
223/*
224 * write_can_lock - would write_trylock() succeed?
225 * @lock: the rwlock in question.
226 */
Ralf Baechle70342282013-01-22 12:59:30 +0100227#define arch_write_can_lock(rw) (!(rw)->lock)
Ralf Baechlee3c48072005-02-03 13:34:45 +0000228
Thomas Gleixnere5931942009-12-03 20:08:46 +0100229static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 unsigned int tmp;
232
233 if (R10000_LLSC_WAR) {
234 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100235 " .set noreorder # arch_read_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 "1: ll %1, %2 \n"
237 " bltz %1, 1b \n"
238 " addu %1, 1 \n"
239 " sc %1, %0 \n"
240 " beqzl %1, 1b \n"
241 " nop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 " .set reorder \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000243 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
244 : GCC_OFF_SMALL_ASM() (rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 : "memory");
246 } else {
Ralf Baechlee01961c2013-04-11 00:16:53 +0200247 do {
248 __asm__ __volatile__(
249 "1: ll %1, %2 # arch_read_lock \n"
250 " bltz %1, 1b \n"
251 " addu %1, 1 \n"
252 "2: sc %1, %0 \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000253 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
254 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechlee01961c2013-04-11 00:16:53 +0200255 : "memory");
256 } while (unlikely(!tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000258
Ralf Baechle17099b12007-07-14 13:24:05 +0100259 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Thomas Gleixnere5931942009-12-03 20:08:46 +0100262static inline void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 unsigned int tmp;
265
David Daneyf252ffd2010-01-08 17:17:43 -0800266 smp_mb__before_llsc();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (R10000_LLSC_WAR) {
269 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100270 "1: ll %1, %2 # arch_read_unlock \n"
Markos Chandras51822212015-03-03 18:48:48 +0000271 " addiu %1, -1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 " sc %1, %0 \n"
273 " beqzl %1, 1b \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000274 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
275 : GCC_OFF_SMALL_ASM() (rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 : "memory");
277 } else {
Ralf Baechlee01961c2013-04-11 00:16:53 +0200278 do {
279 __asm__ __volatile__(
280 "1: ll %1, %2 # arch_read_unlock \n"
Markos Chandras57537622014-11-24 14:11:39 +0000281 " addiu %1, -1 \n"
Ralf Baechlee01961c2013-04-11 00:16:53 +0200282 " sc %1, %0 \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000283 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
284 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechlee01961c2013-04-11 00:16:53 +0200285 : "memory");
286 } while (unlikely(!tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288}
289
Thomas Gleixnere5931942009-12-03 20:08:46 +0100290static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned int tmp;
293
294 if (R10000_LLSC_WAR) {
295 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100296 " .set noreorder # arch_write_lock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 "1: ll %1, %2 \n"
298 " bnez %1, 1b \n"
299 " lui %1, 0x8000 \n"
300 " sc %1, %0 \n"
301 " beqzl %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000302 " nop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 " .set reorder \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000304 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
305 : GCC_OFF_SMALL_ASM() (rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 : "memory");
307 } else {
Ralf Baechlee01961c2013-04-11 00:16:53 +0200308 do {
309 __asm__ __volatile__(
310 "1: ll %1, %2 # arch_write_lock \n"
311 " bnez %1, 1b \n"
312 " lui %1, 0x8000 \n"
313 "2: sc %1, %0 \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000314 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
315 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechlee01961c2013-04-11 00:16:53 +0200316 : "memory");
317 } while (unlikely(!tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000319
Ralf Baechle17099b12007-07-14 13:24:05 +0100320 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Thomas Gleixnere5931942009-12-03 20:08:46 +0100323static inline void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Leonid Yegoshin6f6ed482015-06-01 17:09:52 -0700325 smp_mb__before_llsc();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100328 " # arch_write_unlock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 " sw $0, %0 \n"
330 : "=m" (rw->lock)
331 : "m" (rw->lock)
332 : "memory");
333}
334
Thomas Gleixnere5931942009-12-03 20:08:46 +0100335static inline int arch_read_trylock(arch_rwlock_t *rw)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100336{
337 unsigned int tmp;
338 int ret;
339
340 if (R10000_LLSC_WAR) {
341 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100342 " .set noreorder # arch_read_trylock \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100343 " li %2, 0 \n"
344 "1: ll %1, %3 \n"
Dave Johnsond52c2d52007-03-05 20:50:27 -0500345 " bltz %1, 2f \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100346 " addu %1, 1 \n"
347 " sc %1, %0 \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100348 " .set reorder \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000349 " beqzl %1, 1b \n"
350 " nop \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100351 __WEAK_LLSC_MB
Ralf Baechle65316fd2006-08-31 14:16:06 +0100352 " li %2, 1 \n"
353 "2: \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000354 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
355 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100356 : "memory");
357 } else {
358 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100359 " .set noreorder # arch_read_trylock \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100360 " li %2, 0 \n"
361 "1: ll %1, %3 \n"
Dave Johnsond52c2d52007-03-05 20:50:27 -0500362 " bltz %1, 2f \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100363 " addu %1, 1 \n"
364 " sc %1, %0 \n"
365 " beqz %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000366 " nop \n"
Ralf Baechle65316fd2006-08-31 14:16:06 +0100367 " .set reorder \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100368 __WEAK_LLSC_MB
Ralf Baechle65316fd2006-08-31 14:16:06 +0100369 " li %2, 1 \n"
370 "2: \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000371 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
372 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100373 : "memory");
374 }
375
376 return ret;
377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Thomas Gleixnere5931942009-12-03 20:08:46 +0100379static inline int arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 unsigned int tmp;
382 int ret;
383
384 if (R10000_LLSC_WAR) {
385 __asm__ __volatile__(
Thomas Gleixnere5931942009-12-03 20:08:46 +0100386 " .set noreorder # arch_write_trylock \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 " li %2, 0 \n"
388 "1: ll %1, %3 \n"
389 " bnez %1, 2f \n"
390 " lui %1, 0x8000 \n"
391 " sc %1, %0 \n"
392 " beqzl %1, 1b \n"
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000393 " nop \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100394 __WEAK_LLSC_MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 " li %2, 1 \n"
396 " .set reorder \n"
397 "2: \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000398 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
399 : GCC_OFF_SMALL_ASM() (rw->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 : "memory");
401 } else {
Ralf Baechlee01961c2013-04-11 00:16:53 +0200402 do {
403 __asm__ __volatile__(
404 " ll %1, %3 # arch_write_trylock \n"
405 " li %2, 0 \n"
406 " bnez %1, 2f \n"
407 " lui %1, 0x8000 \n"
408 " sc %1, %0 \n"
409 " li %2, 1 \n"
410 "2: \n"
Markos Chandras94bfb752015-01-26 12:44:11 +0000411 : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp),
Maciej W. Rozyckib0984c42014-11-15 22:08:48 +0000412 "=&r" (ret)
Markos Chandras94bfb752015-01-26 12:44:11 +0000413 : GCC_OFF_SMALL_ASM() (rw->lock)
Ralf Baechlee01961c2013-04-11 00:16:53 +0200414 : "memory");
415 } while (unlikely(!tmp));
416
417 smp_llsc_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 return ret;
421}
422
Thomas Gleixnere5931942009-12-03 20:08:46 +0100423#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
424#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Ralf Baechle65316fd2006-08-31 14:16:06 +0100425
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100426#define arch_spin_relax(lock) cpu_relax()
427#define arch_read_relax(lock) cpu_relax()
428#define arch_write_relax(lock) cpu_relax()
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#endif /* _ASM_SPINLOCK_H */