1 #ifndef _ASM_M32R_SPINLOCK_H
2 #define _ASM_M32R_SPINLOCK_H
5 * linux/include/asm-m32r/spinlock.h
8 * Copyright (C) 2001, 2002 Hitoshi Yamamoto
9 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
12 #include <linux/compiler.h>
13 #include <linux/atomic.h>
14 #include <asm/dcache_clear.h>
16 #include <asm/barrier.h>
17 #include <asm/processor.h>
20 * Your basic SMP spinlocks, allowing only a single CPU anywhere
22 * (the type definitions are in asm/spinlock_types.h)
24 * Simple spin lock operations. There are two variants, one clears IRQ's
25 * on the local processor, one does not.
27 * We make no fairness assumptions. They have a cost.
30 #define arch_spin_is_locked(x) (*(volatile int *)(&(x)->slock) <= 0)
31 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
33 static inline void arch_spin_unlock_wait(arch_spinlock_t
*lock
)
35 smp_cond_load_acquire(&lock
->slock
, VAL
> 0);
39 * arch_spin_trylock - Try spin lock and return a result
40 * @lock: Pointer to the lock variable
42 * arch_spin_trylock() tries to get the lock and returns a result.
43 * On the m32r, the result value is 1 (= Success) or 0 (= Failure).
45 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
48 unsigned long tmp1
, tmp2
;
51 * lock->slock : =1 : unlock
54 * oldval = lock->slock; <--+ need atomic operation
55 * lock->slock = 0; <--+
58 __asm__
__volatile__ (
59 "# arch_spin_trylock \n\t"
62 "clrpsw #0x40 -> nop; \n\t"
63 DCACHE_CLEAR("%0", "r6", "%3")
65 "unlock %1, @%3; \n\t"
67 : "=&r" (oldval
), "=&r" (tmp1
), "=&r" (tmp2
)
70 #ifdef CONFIG_CHIP_M32700_TS1
72 #endif /* CONFIG_CHIP_M32700_TS1 */
78 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
80 unsigned long tmp0
, tmp1
;
83 * lock->slock : =1 : unlock
87 * lock->slock -= 1; <-- need atomic operation
88 * if (lock->slock == 0) break;
89 * for ( ; lock->slock <= 0 ; );
92 __asm__
__volatile__ (
93 "# arch_spin_lock \n\t"
97 "clrpsw #0x40 -> nop; \n\t"
98 DCACHE_CLEAR("%0", "r6", "%2")
101 "unlock %0, @%2; \n\t"
104 LOCK_SECTION_START(".balign 4 \n\t")
111 : "=&r" (tmp0
), "=&r" (tmp1
)
114 #ifdef CONFIG_CHIP_M32700_TS1
116 #endif /* CONFIG_CHIP_M32700_TS1 */
120 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
127 * Read-write spinlocks, allowing multiple readers
128 * but only one writer.
130 * NOTE! it is quite common to have readers in interrupts
131 * but no interrupt writers. For those circumstances we
132 * can "mix" irq-safe locks - any writer needs to get a
133 * irq-safe write-lock, but readers can get non-irqsafe
136 * On x86, we implement read-write locks as a 32-bit counter
137 * with the high bit (sign) being the "contended" bit.
139 * The inline assembly is non-obvious. Think about it.
141 * Changed to use the same technique as rw semaphores. See
142 * semaphore.h for details. -ben
146 * read_can_lock - would read_trylock() succeed?
147 * @lock: the rwlock in question.
149 #define arch_read_can_lock(x) ((int)(x)->lock > 0)
152 * write_can_lock - would write_trylock() succeed?
153 * @lock: the rwlock in question.
155 #define arch_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
157 static inline void arch_read_lock(arch_rwlock_t
*rw
)
159 unsigned long tmp0
, tmp1
;
162 * rw->lock : >0 : unlock
166 * rw->lock -= 1; <-- need atomic operation
167 * if (rw->lock >= 0) break;
168 * rw->lock += 1; <-- need atomic operation
169 * for ( ; rw->lock <= 0 ; );
172 __asm__
__volatile__ (
177 "clrpsw #0x40 -> nop; \n\t"
178 DCACHE_CLEAR("%0", "r6", "%2")
181 "unlock %0, @%2; \n\t"
184 LOCK_SECTION_START(".balign 4 \n\t")
187 "clrpsw #0x40 -> nop; \n\t"
188 DCACHE_CLEAR("%0", "r6", "%2")
191 "unlock %0, @%2; \n\t"
199 : "=&r" (tmp0
), "=&r" (tmp1
)
202 #ifdef CONFIG_CHIP_M32700_TS1
204 #endif /* CONFIG_CHIP_M32700_TS1 */
208 static inline void arch_write_lock(arch_rwlock_t
*rw
)
210 unsigned long tmp0
, tmp1
, tmp2
;
213 * rw->lock : =RW_LOCK_BIAS_STR : unlock
214 * : !=RW_LOCK_BIAS_STR : lock
217 * rw->lock -= RW_LOCK_BIAS_STR; <-- need atomic operation
218 * if (rw->lock == 0) break;
219 * rw->lock += RW_LOCK_BIAS_STR; <-- need atomic operation
220 * for ( ; rw->lock != RW_LOCK_BIAS_STR ; ) ;
223 __asm__
__volatile__ (
225 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
226 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
230 "clrpsw #0x40 -> nop; \n\t"
231 DCACHE_CLEAR("%0", "r7", "%3")
234 "unlock %0, @%3; \n\t"
237 LOCK_SECTION_START(".balign 4 \n\t")
240 "clrpsw #0x40 -> nop; \n\t"
241 DCACHE_CLEAR("%0", "r7", "%3")
244 "unlock %0, @%3; \n\t"
249 "beq %0, %1, 1b; \n\t"
252 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
255 #ifdef CONFIG_CHIP_M32700_TS1
257 #endif /* CONFIG_CHIP_M32700_TS1 */
261 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
263 unsigned long tmp0
, tmp1
;
265 __asm__
__volatile__ (
268 "clrpsw #0x40 -> nop; \n\t"
269 DCACHE_CLEAR("%0", "r6", "%2")
272 "unlock %0, @%2; \n\t"
274 : "=&r" (tmp0
), "=&r" (tmp1
)
277 #ifdef CONFIG_CHIP_M32700_TS1
279 #endif /* CONFIG_CHIP_M32700_TS1 */
283 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
285 unsigned long tmp0
, tmp1
, tmp2
;
287 __asm__
__volatile__ (
288 "# write_unlock \n\t"
289 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
290 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
292 "clrpsw #0x40 -> nop; \n\t"
293 DCACHE_CLEAR("%0", "r7", "%3")
296 "unlock %0, @%3; \n\t"
298 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
301 #ifdef CONFIG_CHIP_M32700_TS1
303 #endif /* CONFIG_CHIP_M32700_TS1 */
307 static inline int arch_read_trylock(arch_rwlock_t
*lock
)
309 atomic_t
*count
= (atomic_t
*)lock
;
310 if (atomic_dec_return(count
) >= 0)
316 static inline int arch_write_trylock(arch_rwlock_t
*lock
)
318 atomic_t
*count
= (atomic_t
*)lock
;
319 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
321 atomic_add(RW_LOCK_BIAS
, count
);
325 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
326 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
328 #define arch_spin_relax(lock) cpu_relax()
329 #define arch_read_relax(lock) cpu_relax()
330 #define arch_write_relax(lock) cpu_relax()
332 #endif /* _ASM_M32R_SPINLOCK_H */