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__)
17 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
18 #define arch_spin_unlock_wait(x) \
19 do { while (arch_spin_is_locked(x)) cpu_relax(); } while (0)
21 #ifdef CONFIG_ARC_HAS_LLSC
24 * A normal LLOCK/SCOND based system, w/o need for livelock workaround
26 #ifndef CONFIG_ARC_STAR_9000923308
28 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
35 "1: llock %[val], [%[slock]] \n"
36 " breq %[val], %[LOCKED], 1b \n" /* spin while LOCKED */
37 " scond %[LOCKED], [%[slock]] \n" /* acquire */
41 : [slock
] "r" (&(lock
->slock
)),
42 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
48 /* 1 - lock taken successfully */
49 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
51 unsigned int val
, got_it
= 0;
56 "1: llock %[val], [%[slock]] \n"
57 " breq %[val], %[LOCKED], 4f \n" /* already LOCKED, just bail */
58 " scond %[LOCKED], [%[slock]] \n" /* acquire */
60 " mov %[got_it], 1 \n"
64 [got_it
] "+&r" (got_it
)
65 : [slock
] "r" (&(lock
->slock
)),
66 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
74 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
78 lock
->slock
= __ARCH_SPIN_LOCK_UNLOCKED__
;
84 * Read-write spinlocks, allowing multiple readers but only one writer.
85 * Unfair locking as Writers could be starved indefinitely by Reader(s)
88 static inline void arch_read_lock(arch_rwlock_t
*rw
)
95 * zero means writer holds the lock exclusively, deny Reader.
96 * Otherwise grant lock to first/subseq reader
98 * if (rw->counter > 0) {
104 __asm__
__volatile__(
105 "1: llock %[val], [%[rwlock]] \n"
106 " brls %[val], %[WR_LOCKED], 1b\n" /* <= 0: spin while write locked */
107 " sub %[val], %[val], 1 \n" /* reader lock */
108 " scond %[val], [%[rwlock]] \n"
112 : [rwlock
] "r" (&(rw
->counter
)),
119 /* 1 - lock taken successfully */
120 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
122 unsigned int val
, got_it
= 0;
126 __asm__
__volatile__(
127 "1: llock %[val], [%[rwlock]] \n"
128 " brls %[val], %[WR_LOCKED], 4f\n" /* <= 0: already write locked, bail */
129 " sub %[val], %[val], 1 \n" /* counter-- */
130 " scond %[val], [%[rwlock]] \n"
131 " bnz 1b \n" /* retry if collided with someone */
132 " mov %[got_it], 1 \n"
134 "4: ; --- done --- \n"
137 [got_it
] "+&r" (got_it
)
138 : [rwlock
] "r" (&(rw
->counter
)),
147 static inline void arch_write_lock(arch_rwlock_t
*rw
)
154 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
155 * deny writer. Otherwise if unlocked grant to writer
156 * Hence the claim that Linux rwlocks are unfair to writers.
157 * (can be starved for an indefinite time by readers).
159 * if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
165 __asm__
__volatile__(
166 "1: llock %[val], [%[rwlock]] \n"
167 " brne %[val], %[UNLOCKED], 1b \n" /* while !UNLOCKED spin */
168 " mov %[val], %[WR_LOCKED] \n"
169 " scond %[val], [%[rwlock]] \n"
173 : [rwlock
] "r" (&(rw
->counter
)),
174 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
181 /* 1 - lock taken successfully */
182 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
184 unsigned int val
, got_it
= 0;
188 __asm__
__volatile__(
189 "1: llock %[val], [%[rwlock]] \n"
190 " brne %[val], %[UNLOCKED], 4f \n" /* !UNLOCKED, bail */
191 " mov %[val], %[WR_LOCKED] \n"
192 " scond %[val], [%[rwlock]] \n"
193 " bnz 1b \n" /* retry if collided with someone */
194 " mov %[got_it], 1 \n"
196 "4: ; --- done --- \n"
199 [got_it
] "+&r" (got_it
)
200 : [rwlock
] "r" (&(rw
->counter
)),
201 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
210 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
219 __asm__
__volatile__(
220 "1: llock %[val], [%[rwlock]] \n"
221 " add %[val], %[val], 1 \n"
222 " scond %[val], [%[rwlock]] \n"
226 : [rwlock
] "r" (&(rw
->counter
))
232 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
236 rw
->counter
= __ARCH_RW_LOCK_UNLOCKED__
;
241 #else /* CONFIG_ARC_STAR_9000923308 */
244 * HS38x4 could get into a LLOCK/SCOND livelock in case of multiple overlapping
245 * coherency transactions in the SCU. The exclusive line state keeps rotating
246 * among contenting cores leading to a never ending cycle. So break the cycle
247 * by deferring the retry of failed exclusive access (SCOND). The actual delay
248 * needed is function of number of contending cores as well as the unrelated
249 * coherency traffic from other cores. To keep the code simple, start off with
250 * small delay of 1 which would suffice most cases and in case of contention
251 * double the delay. Eventually the delay is sufficient such that the coherency
252 * pipeline is drained, thus a subsequent exclusive access would succeed.
255 #define SCOND_FAIL_RETRY_VAR_DEF \
256 unsigned int delay, tmp; \
258 #define SCOND_FAIL_RETRY_ASM \
259 " ; --- scond fail delay --- \n" \
260 " mov %[tmp], %[delay] \n" /* tmp = delay */ \
261 "2: brne.d %[tmp], 0, 2b \n" /* while (tmp != 0) */ \
262 " sub %[tmp], %[tmp], 1 \n" /* tmp-- */ \
263 " rol %[delay], %[delay] \n" /* delay *= 2 */ \
264 " b 1b \n" /* start over */ \
266 "4: ; --- done --- \n" \
268 #define SCOND_FAIL_RETRY_VARS \
269 ,[delay] "=&r" (delay), [tmp] "=&r" (tmp) \
271 static inline void arch_spin_lock(arch_spinlock_t *lock)
274 SCOND_FAIL_RETRY_VAR_DEF
;
278 __asm__
__volatile__(
279 "0: mov %[delay], 1 \n"
280 "1: llock %[val], [%[slock]] \n"
281 " breq %[val], %[LOCKED], 0b \n" /* spin while LOCKED */
282 " scond %[LOCKED], [%[slock]] \n" /* acquire */
283 " bz 4f \n" /* done */
288 SCOND_FAIL_RETRY_VARS
289 : [slock
] "r" (&(lock
->slock
)),
290 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
296 /* 1 - lock taken successfully */
297 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
299 unsigned int val
, got_it
= 0;
300 SCOND_FAIL_RETRY_VAR_DEF
;
304 __asm__
__volatile__(
305 "0: mov %[delay], 1 \n"
306 "1: llock %[val], [%[slock]] \n"
307 " breq %[val], %[LOCKED], 4f \n" /* already LOCKED, just bail */
308 " scond %[LOCKED], [%[slock]] \n" /* acquire */
310 " mov.z %[got_it], 1 \n" /* got it */
315 [got_it
] "+&r" (got_it
)
316 SCOND_FAIL_RETRY_VARS
317 : [slock
] "r" (&(lock
->slock
)),
318 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
326 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
330 lock
->slock
= __ARCH_SPIN_LOCK_UNLOCKED__
;
336 * Read-write spinlocks, allowing multiple readers but only one writer.
337 * Unfair locking as Writers could be starved indefinitely by Reader(s)
340 static inline void arch_read_lock(arch_rwlock_t
*rw
)
343 SCOND_FAIL_RETRY_VAR_DEF
;
348 * zero means writer holds the lock exclusively, deny Reader.
349 * Otherwise grant lock to first/subseq reader
351 * if (rw->counter > 0) {
357 __asm__
__volatile__(
358 "0: mov %[delay], 1 \n"
359 "1: llock %[val], [%[rwlock]] \n"
360 " brls %[val], %[WR_LOCKED], 0b\n" /* <= 0: spin while write locked */
361 " sub %[val], %[val], 1 \n" /* reader lock */
362 " scond %[val], [%[rwlock]] \n"
363 " bz 4f \n" /* done */
368 SCOND_FAIL_RETRY_VARS
369 : [rwlock
] "r" (&(rw
->counter
)),
376 /* 1 - lock taken successfully */
377 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
379 unsigned int val
, got_it
= 0;
380 SCOND_FAIL_RETRY_VAR_DEF
;
384 __asm__
__volatile__(
385 "0: mov %[delay], 1 \n"
386 "1: llock %[val], [%[rwlock]] \n"
387 " brls %[val], %[WR_LOCKED], 4f\n" /* <= 0: already write locked, bail */
388 " sub %[val], %[val], 1 \n" /* counter-- */
389 " scond %[val], [%[rwlock]] \n"
391 " mov.z %[got_it], 1 \n" /* got it */
396 [got_it
] "+&r" (got_it
)
397 SCOND_FAIL_RETRY_VARS
398 : [rwlock
] "r" (&(rw
->counter
)),
407 static inline void arch_write_lock(arch_rwlock_t
*rw
)
410 SCOND_FAIL_RETRY_VAR_DEF
;
415 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
416 * deny writer. Otherwise if unlocked grant to writer
417 * Hence the claim that Linux rwlocks are unfair to writers.
418 * (can be starved for an indefinite time by readers).
420 * if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
426 __asm__
__volatile__(
427 "0: mov %[delay], 1 \n"
428 "1: llock %[val], [%[rwlock]] \n"
429 " brne %[val], %[UNLOCKED], 0b \n" /* while !UNLOCKED spin */
430 " mov %[val], %[WR_LOCKED] \n"
431 " scond %[val], [%[rwlock]] \n"
437 SCOND_FAIL_RETRY_VARS
438 : [rwlock
] "r" (&(rw
->counter
)),
439 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
446 /* 1 - lock taken successfully */
447 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
449 unsigned int val
, got_it
= 0;
450 SCOND_FAIL_RETRY_VAR_DEF
;
454 __asm__
__volatile__(
455 "0: mov %[delay], 1 \n"
456 "1: llock %[val], [%[rwlock]] \n"
457 " brne %[val], %[UNLOCKED], 4f \n" /* !UNLOCKED, bail */
458 " mov %[val], %[WR_LOCKED] \n"
459 " scond %[val], [%[rwlock]] \n"
461 " mov.z %[got_it], 1 \n" /* got it */
466 [got_it
] "+&r" (got_it
)
467 SCOND_FAIL_RETRY_VARS
468 : [rwlock
] "r" (&(rw
->counter
)),
469 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
478 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
487 __asm__
__volatile__(
488 "1: llock %[val], [%[rwlock]] \n"
489 " add %[val], %[val], 1 \n"
490 " scond %[val], [%[rwlock]] \n"
494 : [rwlock
] "r" (&(rw
->counter
))
500 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
507 * rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
509 __asm__
__volatile__(
510 "1: llock %[val], [%[rwlock]] \n"
511 " scond %[UNLOCKED], [%[rwlock]]\n"
515 : [rwlock
] "r" (&(rw
->counter
)),
516 [UNLOCKED
] "r" (__ARCH_RW_LOCK_UNLOCKED__
)
522 #undef SCOND_FAIL_RETRY_VAR_DEF
523 #undef SCOND_FAIL_RETRY_ASM
524 #undef SCOND_FAIL_RETRY_VARS
526 #endif /* CONFIG_ARC_STAR_9000923308 */
528 #else /* !CONFIG_ARC_HAS_LLSC */
530 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
532 unsigned int val
= __ARCH_SPIN_LOCK_LOCKED__
;
535 * This smp_mb() is technically superfluous, we only need the one
536 * after the lock for providing the ACQUIRE semantics.
537 * However doing the "right" thing was regressing hackbench
538 * so keeping this, pending further investigation
542 __asm__
__volatile__(
544 " breq %0, %2, 1b \n"
546 : "r"(&(lock
->slock
)), "ir"(__ARCH_SPIN_LOCK_LOCKED__
)
550 * ACQUIRE barrier to ensure load/store after taking the lock
551 * don't "bleed-up" out of the critical section (leak-in is allowed)
552 * http://www.spinics.net/lists/kernel/msg2010409.html
554 * ARCv2 only has load-load, store-store and all-all barrier
555 * thus need the full all-all barrier
560 /* 1 - lock taken successfully */
561 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
563 unsigned int val
= __ARCH_SPIN_LOCK_LOCKED__
;
567 __asm__
__volatile__(
570 : "r"(&(lock
->slock
))
575 return (val
== __ARCH_SPIN_LOCK_UNLOCKED__
);
578 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
580 unsigned int val
= __ARCH_SPIN_LOCK_UNLOCKED__
;
583 * RELEASE barrier: given the instructions avail on ARCv2, full barrier
588 __asm__
__volatile__(
591 : "r"(&(lock
->slock
))
595 * superfluous, but keeping for now - see pairing version in
596 * arch_spin_lock above
602 * Read-write spinlocks, allowing multiple readers but only one writer.
603 * Unfair locking as Writers could be starved indefinitely by Reader(s)
605 * The spinlock itself is contained in @counter and access to it is
606 * serialized with @lock_mutex.
609 /* 1 - lock taken successfully */
610 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
614 arch_spin_lock(&(rw
->lock_mutex
));
617 * zero means writer holds the lock exclusively, deny Reader.
618 * Otherwise grant lock to first/subseq reader
620 if (rw
->counter
> 0) {
625 arch_spin_unlock(&(rw
->lock_mutex
));
631 /* 1 - lock taken successfully */
632 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
636 arch_spin_lock(&(rw
->lock_mutex
));
639 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
640 * deny writer. Otherwise if unlocked grant to writer
641 * Hence the claim that Linux rwlocks are unfair to writers.
642 * (can be starved for an indefinite time by readers).
644 if (rw
->counter
== __ARCH_RW_LOCK_UNLOCKED__
) {
648 arch_spin_unlock(&(rw
->lock_mutex
));
653 static inline void arch_read_lock(arch_rwlock_t
*rw
)
655 while (!arch_read_trylock(rw
))
659 static inline void arch_write_lock(arch_rwlock_t
*rw
)
661 while (!arch_write_trylock(rw
))
665 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
667 arch_spin_lock(&(rw
->lock_mutex
));
669 arch_spin_unlock(&(rw
->lock_mutex
));
672 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
674 arch_spin_lock(&(rw
->lock_mutex
));
675 rw
->counter
= __ARCH_RW_LOCK_UNLOCKED__
;
676 arch_spin_unlock(&(rw
->lock_mutex
));
681 #define arch_read_can_lock(x) ((x)->counter > 0)
682 #define arch_write_can_lock(x) ((x)->counter == __ARCH_RW_LOCK_UNLOCKED__)
684 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
685 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
687 #define arch_spin_relax(lock) cpu_relax()
688 #define arch_read_relax(lock) cpu_relax()
689 #define arch_write_relax(lock) cpu_relax()
691 #endif /* __ASM_SPINLOCK_H */