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>
18 * Your basic SMP spinlocks, allowing only a single CPU anywhere
20 * (the type definitions are in asm/spinlock_types.h)
22 * Simple spin lock operations. There are two variants, one clears IRQ's
23 * on the local processor, one does not.
25 * We make no fairness assumptions. They have a cost.
28 #define arch_spin_is_locked(x) (*(volatile int *)(&(x)->slock) <= 0)
29 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
30 #define arch_spin_unlock_wait(x) \
31 do { cpu_relax(); } while (arch_spin_is_locked(x))
34 * arch_spin_trylock - Try spin lock and return a result
35 * @lock: Pointer to the lock variable
37 * arch_spin_trylock() tries to get the lock and returns a result.
38 * On the m32r, the result value is 1 (= Success) or 0 (= Failure).
40 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
43 unsigned long tmp1
, tmp2
;
46 * lock->slock : =1 : unlock
49 * oldval = lock->slock; <--+ need atomic operation
50 * lock->slock = 0; <--+
53 __asm__
__volatile__ (
54 "# arch_spin_trylock \n\t"
57 "clrpsw #0x40 -> nop; \n\t"
58 DCACHE_CLEAR("%0", "r6", "%3")
60 "unlock %1, @%3; \n\t"
62 : "=&r" (oldval
), "=&r" (tmp1
), "=&r" (tmp2
)
65 #ifdef CONFIG_CHIP_M32700_TS1
67 #endif /* CONFIG_CHIP_M32700_TS1 */
73 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
75 unsigned long tmp0
, tmp1
;
78 * lock->slock : =1 : unlock
82 * lock->slock -= 1; <-- need atomic operation
83 * if (lock->slock == 0) break;
84 * for ( ; lock->slock <= 0 ; );
87 __asm__
__volatile__ (
88 "# arch_spin_lock \n\t"
92 "clrpsw #0x40 -> nop; \n\t"
93 DCACHE_CLEAR("%0", "r6", "%2")
96 "unlock %0, @%2; \n\t"
99 LOCK_SECTION_START(".balign 4 \n\t")
106 : "=&r" (tmp0
), "=&r" (tmp1
)
109 #ifdef CONFIG_CHIP_M32700_TS1
111 #endif /* CONFIG_CHIP_M32700_TS1 */
115 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
122 * Read-write spinlocks, allowing multiple readers
123 * but only one writer.
125 * NOTE! it is quite common to have readers in interrupts
126 * but no interrupt writers. For those circumstances we
127 * can "mix" irq-safe locks - any writer needs to get a
128 * irq-safe write-lock, but readers can get non-irqsafe
131 * On x86, we implement read-write locks as a 32-bit counter
132 * with the high bit (sign) being the "contended" bit.
134 * The inline assembly is non-obvious. Think about it.
136 * Changed to use the same technique as rw semaphores. See
137 * semaphore.h for details. -ben
141 * read_can_lock - would read_trylock() succeed?
142 * @lock: the rwlock in question.
144 #define arch_read_can_lock(x) ((int)(x)->lock > 0)
147 * write_can_lock - would write_trylock() succeed?
148 * @lock: the rwlock in question.
150 #define arch_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
152 static inline void arch_read_lock(arch_rwlock_t
*rw
)
154 unsigned long tmp0
, tmp1
;
157 * rw->lock : >0 : unlock
161 * rw->lock -= 1; <-- need atomic operation
162 * if (rw->lock >= 0) break;
163 * rw->lock += 1; <-- need atomic operation
164 * for ( ; rw->lock <= 0 ; );
167 __asm__
__volatile__ (
172 "clrpsw #0x40 -> nop; \n\t"
173 DCACHE_CLEAR("%0", "r6", "%2")
176 "unlock %0, @%2; \n\t"
179 LOCK_SECTION_START(".balign 4 \n\t")
182 "clrpsw #0x40 -> nop; \n\t"
183 DCACHE_CLEAR("%0", "r6", "%2")
186 "unlock %0, @%2; \n\t"
194 : "=&r" (tmp0
), "=&r" (tmp1
)
197 #ifdef CONFIG_CHIP_M32700_TS1
199 #endif /* CONFIG_CHIP_M32700_TS1 */
203 static inline void arch_write_lock(arch_rwlock_t
*rw
)
205 unsigned long tmp0
, tmp1
, tmp2
;
208 * rw->lock : =RW_LOCK_BIAS_STR : unlock
209 * : !=RW_LOCK_BIAS_STR : lock
212 * rw->lock -= RW_LOCK_BIAS_STR; <-- need atomic operation
213 * if (rw->lock == 0) break;
214 * rw->lock += RW_LOCK_BIAS_STR; <-- need atomic operation
215 * for ( ; rw->lock != RW_LOCK_BIAS_STR ; ) ;
218 __asm__
__volatile__ (
220 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
221 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
225 "clrpsw #0x40 -> nop; \n\t"
226 DCACHE_CLEAR("%0", "r7", "%3")
229 "unlock %0, @%3; \n\t"
232 LOCK_SECTION_START(".balign 4 \n\t")
235 "clrpsw #0x40 -> nop; \n\t"
236 DCACHE_CLEAR("%0", "r7", "%3")
239 "unlock %0, @%3; \n\t"
244 "beq %0, %1, 1b; \n\t"
247 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
250 #ifdef CONFIG_CHIP_M32700_TS1
252 #endif /* CONFIG_CHIP_M32700_TS1 */
256 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
258 unsigned long tmp0
, tmp1
;
260 __asm__
__volatile__ (
263 "clrpsw #0x40 -> nop; \n\t"
264 DCACHE_CLEAR("%0", "r6", "%2")
267 "unlock %0, @%2; \n\t"
269 : "=&r" (tmp0
), "=&r" (tmp1
)
272 #ifdef CONFIG_CHIP_M32700_TS1
274 #endif /* CONFIG_CHIP_M32700_TS1 */
278 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
280 unsigned long tmp0
, tmp1
, tmp2
;
282 __asm__
__volatile__ (
283 "# write_unlock \n\t"
284 "seth %1, #high(" RW_LOCK_BIAS_STR
"); \n\t"
285 "or3 %1, %1, #low(" RW_LOCK_BIAS_STR
"); \n\t"
287 "clrpsw #0x40 -> nop; \n\t"
288 DCACHE_CLEAR("%0", "r7", "%3")
291 "unlock %0, @%3; \n\t"
293 : "=&r" (tmp0
), "=&r" (tmp1
), "=&r" (tmp2
)
296 #ifdef CONFIG_CHIP_M32700_TS1
298 #endif /* CONFIG_CHIP_M32700_TS1 */
302 static inline int arch_read_trylock(arch_rwlock_t
*lock
)
304 atomic_t
*count
= (atomic_t
*)lock
;
305 if (atomic_dec_return(count
) >= 0)
311 static inline int arch_write_trylock(arch_rwlock_t
*lock
)
313 atomic_t
*count
= (atomic_t
*)lock
;
314 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
316 atomic_add(RW_LOCK_BIAS
, count
);
320 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
321 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
323 #define arch_spin_relax(lock) cpu_relax()
324 #define arch_read_relax(lock) cpu_relax()
325 #define arch_write_relax(lock) cpu_relax()
327 #endif /* _ASM_M32R_SPINLOCK_H */