1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_M32R_SPINLOCK_H
3 #define _ASM_M32R_SPINLOCK_H
6 * linux/include/asm-m32r/spinlock.h
9 * Copyright (C) 2001, 2002 Hitoshi Yamamoto
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
13 #include <linux/compiler.h>
14 #include <linux/atomic.h>
15 #include <asm/dcache_clear.h>
17 #include <asm/barrier.h>
18 #include <asm/processor.h>
21 * Your basic SMP spinlocks, allowing only a single CPU anywhere
23 * (the type definitions are in asm/spinlock_types.h)
25 * Simple spin lock operations. There are two variants, one clears IRQ's
26 * on the local processor, one does not.
28 * We make no fairness assumptions. They have a cost.
31 #define arch_spin_is_locked(x) (*(volatile int *)(&(x)->slock) <= 0)
32 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
35 * arch_spin_trylock - Try spin lock and return a result
36 * @lock: Pointer to the lock variable
38 * arch_spin_trylock() tries to get the lock and returns a result.
39 * On the m32r, the result value is 1 (= Success) or 0 (= Failure).
41 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
44 unsigned long tmp1
, tmp2
;
47 * lock->slock : =1 : unlock
50 * oldval = lock->slock; <--+ need atomic operation
51 * lock->slock = 0; <--+
54 __asm__
__volatile__ (
55 "# arch_spin_trylock \n\t"
58 "clrpsw #0x40 -> nop; \n\t"
59 DCACHE_CLEAR("%0", "r6", "%3")
61 "unlock %1, @%3; \n\t"
63 : "=&r" (oldval
), "=&r" (tmp1
), "=&r" (tmp2
)
66 #ifdef CONFIG_CHIP_M32700_TS1
68 #endif /* CONFIG_CHIP_M32700_TS1 */
74 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
76 unsigned long tmp0
, tmp1
;
79 * lock->slock : =1 : unlock
83 * lock->slock -= 1; <-- need atomic operation
84 * if (lock->slock == 0) break;
85 * for ( ; lock->slock <= 0 ; );
88 __asm__
__volatile__ (
89 "# arch_spin_lock \n\t"
93 "clrpsw #0x40 -> nop; \n\t"
94 DCACHE_CLEAR("%0", "r6", "%2")
97 "unlock %0, @%2; \n\t"
100 LOCK_SECTION_START(".balign 4 \n\t")
107 : "=&r" (tmp0
), "=&r" (tmp1
)
110 #ifdef CONFIG_CHIP_M32700_TS1
112 #endif /* CONFIG_CHIP_M32700_TS1 */
116 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
123 * Read-write spinlocks, allowing multiple readers
124 * but only one writer.
126 * NOTE! it is quite common to have readers in interrupts
127 * but no interrupt writers. For those circumstances we
128 * can "mix" irq-safe locks - any writer needs to get a
129 * irq-safe write-lock, but readers can get non-irqsafe
132 * On x86, we implement read-write locks as a 32-bit counter
133 * with the high bit (sign) being the "contended" bit.
135 * The inline assembly is non-obvious. Think about it.
137 * Changed to use the same technique as rw semaphores. See
138 * semaphore.h for details. -ben
142 * read_can_lock - would read_trylock() succeed?
143 * @lock: the rwlock in question.
145 #define arch_read_can_lock(x) ((int)(x)->lock > 0)
148 * write_can_lock - would write_trylock() succeed?
149 * @lock: the rwlock in question.
151 #define arch_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
153 static inline void arch_read_lock(arch_rwlock_t
*rw
)
155 unsigned long tmp0
, tmp1
;
158 * rw->lock : >0 : unlock
162 * rw->lock -= 1; <-- need atomic operation
163 * if (rw->lock >= 0) break;
164 * rw->lock += 1; <-- need atomic operation
165 * for ( ; rw->lock <= 0 ; );
168 __asm__
__volatile__ (
173 "clrpsw #0x40 -> nop; \n\t"
174 DCACHE_CLEAR("%0", "r6", "%2")
177 "unlock %0, @%2; \n\t"
180 LOCK_SECTION_START(".balign 4 \n\t")
183 "clrpsw #0x40 -> nop; \n\t"
184 DCACHE_CLEAR("%0", "r6", "%2")
187 "unlock %0, @%2; \n\t"
195 : "=&r" (tmp0
), "=&r" (tmp1
)
198 #ifdef CONFIG_CHIP_M32700_TS1
200 #endif /* CONFIG_CHIP_M32700_TS1 */
204 static inline void arch_write_lock(arch_rwlock_t
*rw
)
206 unsigned long tmp0
, tmp1
, tmp2
;
209 * rw->lock : =RW_LOCK_BIAS_STR : unlock
210 * : !=RW_LOCK_BIAS_STR : lock
213 * rw->lock -= RW_LOCK_BIAS_STR; <-- need atomic operation
214 * if (rw->lock == 0) break;
215 * rw->lock += RW_LOCK_BIAS_STR; <-- need atomic operation
216 * for ( ; rw->lock != RW_LOCK_BIAS_STR ; ) ;
219 __asm__
__volatile__ (
221 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
222 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
226 "clrpsw #0x40 -> nop; \n\t"
227 DCACHE_CLEAR("%0", "r7", "%3")
230 "unlock %0, @%3; \n\t"
233 LOCK_SECTION_START(".balign 4 \n\t")
236 "clrpsw #0x40 -> nop; \n\t"
237 DCACHE_CLEAR("%0", "r7", "%3")
240 "unlock %0, @%3; \n\t"
245 "beq %0, %1, 1b; \n\t"
248 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
251 #ifdef CONFIG_CHIP_M32700_TS1
253 #endif /* CONFIG_CHIP_M32700_TS1 */
257 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
259 unsigned long tmp0
, tmp1
;
261 __asm__
__volatile__ (
264 "clrpsw #0x40 -> nop; \n\t"
265 DCACHE_CLEAR("%0", "r6", "%2")
268 "unlock %0, @%2; \n\t"
270 : "=&r" (tmp0
), "=&r" (tmp1
)
273 #ifdef CONFIG_CHIP_M32700_TS1
275 #endif /* CONFIG_CHIP_M32700_TS1 */
279 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
281 unsigned long tmp0
, tmp1
, tmp2
;
283 __asm__
__volatile__ (
284 "# write_unlock \n\t"
285 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
286 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
288 "clrpsw #0x40 -> nop; \n\t"
289 DCACHE_CLEAR("%0", "r7", "%3")
292 "unlock %0, @%3; \n\t"
294 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
297 #ifdef CONFIG_CHIP_M32700_TS1
299 #endif /* CONFIG_CHIP_M32700_TS1 */
303 static inline int arch_read_trylock(arch_rwlock_t
*lock
)
305 atomic_t
*count
= (atomic_t
*)lock
;
306 if (atomic_dec_return(count
) >= 0)
312 static inline int arch_write_trylock(arch_rwlock_t
*lock
)
314 atomic_t
*count
= (atomic_t
*)lock
;
315 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
317 atomic_add(RW_LOCK_BIAS
, count
);
321 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
322 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
324 #define arch_spin_relax(lock) cpu_relax()
325 #define arch_read_relax(lock) cpu_relax()
326 #define arch_write_relax(lock) cpu_relax()
328 #endif /* _ASM_M32R_SPINLOCK_H */