2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #ifndef __ASM_SPINLOCK_H
10 #define __ASM_SPINLOCK_H
12 #include <asm/spinlock_types.h>
13 #include <asm/processor.h>
14 #include <asm/barrier.h>
16 #define arch_spin_is_locked(x) ((x)->slock != __ARCH_SPIN_LOCK_UNLOCKED__)
18 #ifdef CONFIG_ARC_HAS_LLSC
20 static inline void arch_spin_lock(arch_spinlock_t *lock)
27 "1: llock %[val], [%[slock]] \n"
28 " breq %[val], %[LOCKED], 1b \n" /* spin while LOCKED */
29 " scond %[LOCKED], [%[slock]] \n" /* acquire */
33 : [slock] "r" (&(lock->slock)),
34 [LOCKED] "r" (__ARCH_SPIN_LOCK_LOCKED__)
40 /* 1 - lock taken successfully */
41 static inline int arch_spin_trylock(arch_spinlock_t *lock)
43 unsigned int val, got_it = 0;
48 "1: llock %[val], [%[slock]] \n"
49 " breq %[val], %[LOCKED], 4f \n" /* already LOCKED, just bail */
50 " scond %[LOCKED], [%[slock]] \n" /* acquire */
52 " mov %[got_it], 1 \n"
56 [got_it] "+&r" (got_it)
57 : [slock] "r" (&(lock->slock)),
58 [LOCKED] "r" (__ARCH_SPIN_LOCK_LOCKED__)
66 static inline void arch_spin_unlock(arch_spinlock_t *lock)
70 lock->slock = __ARCH_SPIN_LOCK_UNLOCKED__;
76 * Read-write spinlocks, allowing multiple readers but only one writer.
77 * Unfair locking as Writers could be starved indefinitely by Reader(s)
80 static inline void arch_read_lock(arch_rwlock_t *rw)
87 * zero means writer holds the lock exclusively, deny Reader.
88 * Otherwise grant lock to first/subseq reader
90 * if (rw->counter > 0) {
97 "1: llock %[val], [%[rwlock]] \n"
98 " brls %[val], %[WR_LOCKED], 1b\n" /* <= 0: spin while write locked */
99 " sub %[val], %[val], 1 \n" /* reader lock */
100 " scond %[val], [%[rwlock]] \n"
104 : [rwlock] "r" (&(rw->counter)),
111 /* 1 - lock taken successfully */
112 static inline int arch_read_trylock(arch_rwlock_t *rw)
114 unsigned int val, got_it = 0;
118 __asm__ __volatile__(
119 "1: llock %[val], [%[rwlock]] \n"
120 " brls %[val], %[WR_LOCKED], 4f\n" /* <= 0: already write locked, bail */
121 " sub %[val], %[val], 1 \n" /* counter-- */
122 " scond %[val], [%[rwlock]] \n"
123 " bnz 1b \n" /* retry if collided with someone */
124 " mov %[got_it], 1 \n"
126 "4: ; --- done --- \n"
129 [got_it] "+&r" (got_it)
130 : [rwlock] "r" (&(rw->counter)),
139 static inline void arch_write_lock(arch_rwlock_t *rw)
146 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
147 * deny writer. Otherwise if unlocked grant to writer
148 * Hence the claim that Linux rwlocks are unfair to writers.
149 * (can be starved for an indefinite time by readers).
151 * if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
157 __asm__ __volatile__(
158 "1: llock %[val], [%[rwlock]] \n"
159 " brne %[val], %[UNLOCKED], 1b \n" /* while !UNLOCKED spin */
160 " mov %[val], %[WR_LOCKED] \n"
161 " scond %[val], [%[rwlock]] \n"
165 : [rwlock] "r" (&(rw->counter)),
166 [UNLOCKED] "ir" (__ARCH_RW_LOCK_UNLOCKED__),
173 /* 1 - lock taken successfully */
174 static inline int arch_write_trylock(arch_rwlock_t *rw)
176 unsigned int val, got_it = 0;
180 __asm__ __volatile__(
181 "1: llock %[val], [%[rwlock]] \n"
182 " brne %[val], %[UNLOCKED], 4f \n" /* !UNLOCKED, bail */
183 " mov %[val], %[WR_LOCKED] \n"
184 " scond %[val], [%[rwlock]] \n"
185 " bnz 1b \n" /* retry if collided with someone */
186 " mov %[got_it], 1 \n"
188 "4: ; --- done --- \n"
191 [got_it] "+&r" (got_it)
192 : [rwlock] "r" (&(rw->counter)),
193 [UNLOCKED] "ir" (__ARCH_RW_LOCK_UNLOCKED__),
202 static inline void arch_read_unlock(arch_rwlock_t *rw)
211 __asm__ __volatile__(
212 "1: llock %[val], [%[rwlock]] \n"
213 " add %[val], %[val], 1 \n"
214 " scond %[val], [%[rwlock]] \n"
218 : [rwlock] "r" (&(rw->counter))
224 static inline void arch_write_unlock(arch_rwlock_t *rw)
228 rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
233 #else /* !CONFIG_ARC_HAS_LLSC */
235 static inline void arch_spin_lock(arch_spinlock_t *lock)
237 unsigned int val = __ARCH_SPIN_LOCK_LOCKED__;
240 * This smp_mb() is technically superfluous, we only need the one
241 * after the lock for providing the ACQUIRE semantics.
242 * However doing the "right" thing was regressing hackbench
243 * so keeping this, pending further investigation
247 __asm__ __volatile__(
249 #ifdef CONFIG_EZNPS_MTM_EXT
252 " breq %0, %2, 1b \n"
254 : "r"(&(lock->slock)), "ir"(__ARCH_SPIN_LOCK_LOCKED__)
255 #ifdef CONFIG_EZNPS_MTM_EXT
256 , "i"(CTOP_INST_SCHD_RW)
261 * ACQUIRE barrier to ensure load/store after taking the lock
262 * don't "bleed-up" out of the critical section (leak-in is allowed)
263 * http://www.spinics.net/lists/kernel/msg2010409.html
265 * ARCv2 only has load-load, store-store and all-all barrier
266 * thus need the full all-all barrier
271 /* 1 - lock taken successfully */
272 static inline int arch_spin_trylock(arch_spinlock_t *lock)
274 unsigned int val = __ARCH_SPIN_LOCK_LOCKED__;
278 __asm__ __volatile__(
281 : "r"(&(lock->slock))
286 return (val == __ARCH_SPIN_LOCK_UNLOCKED__);
289 static inline void arch_spin_unlock(arch_spinlock_t *lock)
291 unsigned int val = __ARCH_SPIN_LOCK_UNLOCKED__;
294 * RELEASE barrier: given the instructions avail on ARCv2, full barrier
300 * EX is not really required here, a simple STore of 0 suffices.
301 * However this causes tasklist livelocks in SystemC based SMP virtual
302 * platforms where the systemc core scheduler uses EX as a cue for
303 * moving to next core. Do a git log of this file for details
305 __asm__ __volatile__(
308 : "r"(&(lock->slock))
312 * superfluous, but keeping for now - see pairing version in
313 * arch_spin_lock above
319 * Read-write spinlocks, allowing multiple readers but only one writer.
320 * Unfair locking as Writers could be starved indefinitely by Reader(s)
322 * The spinlock itself is contained in @counter and access to it is
323 * serialized with @lock_mutex.
326 /* 1 - lock taken successfully */
327 static inline int arch_read_trylock(arch_rwlock_t *rw)
332 local_irq_save(flags);
333 arch_spin_lock(&(rw->lock_mutex));
336 * zero means writer holds the lock exclusively, deny Reader.
337 * Otherwise grant lock to first/subseq reader
339 if (rw->counter > 0) {
344 arch_spin_unlock(&(rw->lock_mutex));
345 local_irq_restore(flags);
351 /* 1 - lock taken successfully */
352 static inline int arch_write_trylock(arch_rwlock_t *rw)
357 local_irq_save(flags);
358 arch_spin_lock(&(rw->lock_mutex));
361 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
362 * deny writer. Otherwise if unlocked grant to writer
363 * Hence the claim that Linux rwlocks are unfair to writers.
364 * (can be starved for an indefinite time by readers).
366 if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
370 arch_spin_unlock(&(rw->lock_mutex));
371 local_irq_restore(flags);
376 static inline void arch_read_lock(arch_rwlock_t *rw)
378 while (!arch_read_trylock(rw))
382 static inline void arch_write_lock(arch_rwlock_t *rw)
384 while (!arch_write_trylock(rw))
388 static inline void arch_read_unlock(arch_rwlock_t *rw)
392 local_irq_save(flags);
393 arch_spin_lock(&(rw->lock_mutex));
395 arch_spin_unlock(&(rw->lock_mutex));
396 local_irq_restore(flags);
399 static inline void arch_write_unlock(arch_rwlock_t *rw)
403 local_irq_save(flags);
404 arch_spin_lock(&(rw->lock_mutex));
405 rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
406 arch_spin_unlock(&(rw->lock_mutex));
407 local_irq_restore(flags);
412 #endif /* __ASM_SPINLOCK_H */