1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 #ifndef __ASM_SPINLOCK_H
7 #define __ASM_SPINLOCK_H
9 #include <asm/spinlock_types.h>
10 #include <asm/processor.h>
11 #include <asm/barrier.h>
13 #define arch_spin_is_locked(x) ((x)->slock != __ARCH_SPIN_LOCK_UNLOCKED__)
15 #ifdef CONFIG_ARC_HAS_LLSC
17 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
22 "1: llock %[val], [%[slock]] \n"
23 " breq %[val], %[LOCKED], 1b \n" /* spin while LOCKED */
24 " scond %[LOCKED], [%[slock]] \n" /* acquire */
28 : [slock
] "r" (&(lock
->slock
)),
29 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
33 * ACQUIRE barrier to ensure load/store after taking the lock
34 * don't "bleed-up" out of the critical section (leak-in is allowed)
35 * http://www.spinics.net/lists/kernel/msg2010409.html
37 * ARCv2 only has load-load, store-store and all-all barrier
38 * thus need the full all-all barrier
43 /* 1 - lock taken successfully */
44 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
46 unsigned int val
, got_it
= 0;
49 "1: llock %[val], [%[slock]] \n"
50 " breq %[val], %[LOCKED], 4f \n" /* already LOCKED, just bail */
51 " scond %[LOCKED], [%[slock]] \n" /* acquire */
53 " mov %[got_it], 1 \n"
57 [got_it
] "+&r" (got_it
)
58 : [slock
] "r" (&(lock
->slock
)),
59 [LOCKED
] "r" (__ARCH_SPIN_LOCK_LOCKED__
)
67 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
71 WRITE_ONCE(lock
->slock
, __ARCH_SPIN_LOCK_UNLOCKED__
);
75 * Read-write spinlocks, allowing multiple readers but only one writer.
76 * Unfair locking as Writers could be starved indefinitely by Reader(s)
79 static inline void arch_read_lock(arch_rwlock_t
*rw
)
84 * zero means writer holds the lock exclusively, deny Reader.
85 * Otherwise grant lock to first/subseq reader
87 * if (rw->counter > 0) {
94 "1: llock %[val], [%[rwlock]] \n"
95 " brls %[val], %[WR_LOCKED], 1b\n" /* <= 0: spin while write locked */
96 " sub %[val], %[val], 1 \n" /* reader lock */
97 " scond %[val], [%[rwlock]] \n"
101 : [rwlock
] "r" (&(rw
->counter
)),
108 /* 1 - lock taken successfully */
109 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
111 unsigned int val
, got_it
= 0;
113 __asm__
__volatile__(
114 "1: llock %[val], [%[rwlock]] \n"
115 " brls %[val], %[WR_LOCKED], 4f\n" /* <= 0: already write locked, bail */
116 " sub %[val], %[val], 1 \n" /* counter-- */
117 " scond %[val], [%[rwlock]] \n"
118 " bnz 1b \n" /* retry if collided with someone */
119 " mov %[got_it], 1 \n"
121 "4: ; --- done --- \n"
124 [got_it
] "+&r" (got_it
)
125 : [rwlock
] "r" (&(rw
->counter
)),
134 static inline void arch_write_lock(arch_rwlock_t
*rw
)
139 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
140 * deny writer. Otherwise if unlocked grant to writer
141 * Hence the claim that Linux rwlocks are unfair to writers.
142 * (can be starved for an indefinite time by readers).
144 * if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
150 __asm__
__volatile__(
151 "1: llock %[val], [%[rwlock]] \n"
152 " brne %[val], %[UNLOCKED], 1b \n" /* while !UNLOCKED spin */
153 " mov %[val], %[WR_LOCKED] \n"
154 " scond %[val], [%[rwlock]] \n"
158 : [rwlock
] "r" (&(rw
->counter
)),
159 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
166 /* 1 - lock taken successfully */
167 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
169 unsigned int val
, got_it
= 0;
171 __asm__
__volatile__(
172 "1: llock %[val], [%[rwlock]] \n"
173 " brne %[val], %[UNLOCKED], 4f \n" /* !UNLOCKED, bail */
174 " mov %[val], %[WR_LOCKED] \n"
175 " scond %[val], [%[rwlock]] \n"
176 " bnz 1b \n" /* retry if collided with someone */
177 " mov %[got_it], 1 \n"
179 "4: ; --- done --- \n"
182 [got_it
] "+&r" (got_it
)
183 : [rwlock
] "r" (&(rw
->counter
)),
184 [UNLOCKED
] "ir" (__ARCH_RW_LOCK_UNLOCKED__
),
193 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
202 __asm__
__volatile__(
203 "1: llock %[val], [%[rwlock]] \n"
204 " add %[val], %[val], 1 \n"
205 " scond %[val], [%[rwlock]] \n"
209 : [rwlock
] "r" (&(rw
->counter
))
213 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
217 WRITE_ONCE(rw
->counter
, __ARCH_RW_LOCK_UNLOCKED__
);
220 #else /* !CONFIG_ARC_HAS_LLSC */
222 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
224 unsigned int val
= __ARCH_SPIN_LOCK_LOCKED__
;
227 * Per lkmm, smp_mb() is only required after _lock (and before_unlock)
228 * for ACQ and REL semantics respectively. However EX based spinlocks
229 * need the extra smp_mb to workaround a hardware quirk.
233 __asm__
__volatile__(
235 " breq %0, %2, 1b \n"
237 : "r"(&(lock
->slock
)), "ir"(__ARCH_SPIN_LOCK_LOCKED__
)
243 /* 1 - lock taken successfully */
244 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
246 unsigned int val
= __ARCH_SPIN_LOCK_LOCKED__
;
250 __asm__
__volatile__(
253 : "r"(&(lock
->slock
))
258 return (val
== __ARCH_SPIN_LOCK_UNLOCKED__
);
261 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
263 unsigned int val
= __ARCH_SPIN_LOCK_UNLOCKED__
;
266 * RELEASE barrier: given the instructions avail on ARCv2, full barrier
272 * EX is not really required here, a simple STore of 0 suffices.
273 * However this causes tasklist livelocks in SystemC based SMP virtual
274 * platforms where the systemc core scheduler uses EX as a cue for
275 * moving to next core. Do a git log of this file for details
277 __asm__
__volatile__(
280 : "r"(&(lock
->slock
))
284 * see pairing version/comment in arch_spin_lock above
290 * Read-write spinlocks, allowing multiple readers but only one writer.
291 * Unfair locking as Writers could be starved indefinitely by Reader(s)
293 * The spinlock itself is contained in @counter and access to it is
294 * serialized with @lock_mutex.
297 /* 1 - lock taken successfully */
298 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
303 local_irq_save(flags
);
304 arch_spin_lock(&(rw
->lock_mutex
));
307 * zero means writer holds the lock exclusively, deny Reader.
308 * Otherwise grant lock to first/subseq reader
310 if (rw
->counter
> 0) {
315 arch_spin_unlock(&(rw
->lock_mutex
));
316 local_irq_restore(flags
);
321 /* 1 - lock taken successfully */
322 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
327 local_irq_save(flags
);
328 arch_spin_lock(&(rw
->lock_mutex
));
331 * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
332 * deny writer. Otherwise if unlocked grant to writer
333 * Hence the claim that Linux rwlocks are unfair to writers.
334 * (can be starved for an indefinite time by readers).
336 if (rw
->counter
== __ARCH_RW_LOCK_UNLOCKED__
) {
340 arch_spin_unlock(&(rw
->lock_mutex
));
341 local_irq_restore(flags
);
346 static inline void arch_read_lock(arch_rwlock_t
*rw
)
348 while (!arch_read_trylock(rw
))
352 static inline void arch_write_lock(arch_rwlock_t
*rw
)
354 while (!arch_write_trylock(rw
))
358 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
362 local_irq_save(flags
);
363 arch_spin_lock(&(rw
->lock_mutex
));
365 arch_spin_unlock(&(rw
->lock_mutex
));
366 local_irq_restore(flags
);
369 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
373 local_irq_save(flags
);
374 arch_spin_lock(&(rw
->lock_mutex
));
375 rw
->counter
= __ARCH_RW_LOCK_UNLOCKED__
;
376 arch_spin_unlock(&(rw
->lock_mutex
));
377 local_irq_restore(flags
);
382 #endif /* __ASM_SPINLOCK_H */