3 * Copyright IBM Corp. 1999
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Derived from "include/asm-i386/spinlock.h"
9 #ifndef __ASM_SPINLOCK_H
10 #define __ASM_SPINLOCK_H
12 #include <linux/smp.h>
14 #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
16 extern int spin_retry
;
19 _raw_compare_and_swap(unsigned int *lock
, unsigned int old
, unsigned int new)
21 unsigned int old_expected
= old
;
25 : "=d" (old
), "=Q" (*lock
)
26 : "0" (old
), "d" (new), "Q" (*lock
)
28 return old
== old_expected
;
32 * Simple spin lock operations. There are two variants, one clears IRQ's
33 * on the local processor, one does not.
35 * We make no fairness assumptions. They have a cost.
37 * (the type definitions are in asm/spinlock_types.h)
40 void arch_lock_relax(unsigned int cpu
);
42 void arch_spin_lock_wait(arch_spinlock_t
*);
43 int arch_spin_trylock_retry(arch_spinlock_t
*);
44 void arch_spin_lock_wait_flags(arch_spinlock_t
*, unsigned long flags
);
46 static inline void arch_spin_relax(arch_spinlock_t
*lock
)
48 arch_lock_relax(lock
->lock
);
51 static inline u32
arch_spin_lockval(int cpu
)
56 static inline int arch_spin_value_unlocked(arch_spinlock_t lock
)
58 return lock
.lock
== 0;
61 static inline int arch_spin_is_locked(arch_spinlock_t
*lp
)
63 return ACCESS_ONCE(lp
->lock
) != 0;
66 static inline int arch_spin_trylock_once(arch_spinlock_t
*lp
)
69 return likely(arch_spin_value_unlocked(*lp
) &&
70 _raw_compare_and_swap(&lp
->lock
, 0, SPINLOCK_LOCKVAL
));
73 static inline void arch_spin_lock(arch_spinlock_t
*lp
)
75 if (!arch_spin_trylock_once(lp
))
76 arch_spin_lock_wait(lp
);
79 static inline void arch_spin_lock_flags(arch_spinlock_t
*lp
,
82 if (!arch_spin_trylock_once(lp
))
83 arch_spin_lock_wait_flags(lp
, flags
);
86 static inline int arch_spin_trylock(arch_spinlock_t
*lp
)
88 if (!arch_spin_trylock_once(lp
))
89 return arch_spin_trylock_retry(lp
);
93 static inline void arch_spin_unlock(arch_spinlock_t
*lp
)
95 typecheck(unsigned int, lp
->lock
);
104 static inline void arch_spin_unlock_wait(arch_spinlock_t
*lock
)
106 while (arch_spin_is_locked(lock
))
107 arch_spin_relax(lock
);
111 * Read-write spinlocks, allowing multiple readers
112 * but only one writer.
114 * NOTE! it is quite common to have readers in interrupts
115 * but no interrupt writers. For those circumstances we
116 * can "mix" irq-safe locks - any writer needs to get a
117 * irq-safe write-lock, but readers can get non-irqsafe
122 * read_can_lock - would read_trylock() succeed?
123 * @lock: the rwlock in question.
125 #define arch_read_can_lock(x) ((int)(x)->lock >= 0)
128 * write_can_lock - would write_trylock() succeed?
129 * @lock: the rwlock in question.
131 #define arch_write_can_lock(x) ((x)->lock == 0)
133 extern int _raw_read_trylock_retry(arch_rwlock_t
*lp
);
134 extern int _raw_write_trylock_retry(arch_rwlock_t
*lp
);
136 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
137 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
139 static inline int arch_read_trylock_once(arch_rwlock_t
*rw
)
141 unsigned int old
= ACCESS_ONCE(rw
->lock
);
142 return likely((int) old
>= 0 &&
143 _raw_compare_and_swap(&rw
->lock
, old
, old
+ 1));
146 static inline int arch_write_trylock_once(arch_rwlock_t
*rw
)
148 unsigned int old
= ACCESS_ONCE(rw
->lock
);
149 return likely(old
== 0 &&
150 _raw_compare_and_swap(&rw
->lock
, 0, 0x80000000));
153 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
155 #define __RAW_OP_OR "lao"
156 #define __RAW_OP_AND "lan"
157 #define __RAW_OP_ADD "laa"
159 #define __RAW_LOCK(ptr, op_val, op_string) \
161 unsigned int old_val; \
163 typecheck(unsigned int *, ptr); \
165 op_string " %0,%2,%1\n" \
167 : "=d" (old_val), "+Q" (*ptr) \
173 #define __RAW_UNLOCK(ptr, op_val, op_string) \
175 unsigned int old_val; \
177 typecheck(unsigned int *, ptr); \
180 op_string " %0,%2,%1\n" \
181 : "=d" (old_val), "+Q" (*ptr) \
187 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
188 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
, unsigned int prev
);
190 static inline void arch_read_lock(arch_rwlock_t
*rw
)
194 old
= __RAW_LOCK(&rw
->lock
, 1, __RAW_OP_ADD
);
196 _raw_read_lock_wait(rw
);
199 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
201 __RAW_UNLOCK(&rw
->lock
, -1, __RAW_OP_ADD
);
204 static inline void arch_write_lock(arch_rwlock_t
*rw
)
208 old
= __RAW_LOCK(&rw
->lock
, 0x80000000, __RAW_OP_OR
);
210 _raw_write_lock_wait(rw
, old
);
211 rw
->owner
= SPINLOCK_LOCKVAL
;
214 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
217 __RAW_UNLOCK(&rw
->lock
, 0x7fffffff, __RAW_OP_AND
);
220 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
222 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
223 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
);
225 static inline void arch_read_lock(arch_rwlock_t
*rw
)
227 if (!arch_read_trylock_once(rw
))
228 _raw_read_lock_wait(rw
);
231 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
236 old
= ACCESS_ONCE(rw
->lock
);
237 } while (!_raw_compare_and_swap(&rw
->lock
, old
, old
- 1));
240 static inline void arch_write_lock(arch_rwlock_t
*rw
)
242 if (!arch_write_trylock_once(rw
))
243 _raw_write_lock_wait(rw
);
244 rw
->owner
= SPINLOCK_LOCKVAL
;
247 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
249 typecheck(unsigned int, rw
->lock
);
260 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
262 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
264 if (!arch_read_trylock_once(rw
))
265 return _raw_read_trylock_retry(rw
);
269 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
271 if (!arch_write_trylock_once(rw
) && !_raw_write_trylock_retry(rw
))
273 rw
->owner
= SPINLOCK_LOCKVAL
;
277 static inline void arch_read_relax(arch_rwlock_t
*rw
)
279 arch_lock_relax(rw
->owner
);
282 static inline void arch_write_relax(arch_rwlock_t
*rw
)
284 arch_lock_relax(rw
->owner
);
287 #endif /* __ASM_SPINLOCK_H */