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 return __sync_bool_compare_and_swap(lock
, old
, new);
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.
30 * (the type definitions are in asm/spinlock_types.h)
33 void arch_lock_relax(unsigned int cpu
);
35 void arch_spin_lock_wait(arch_spinlock_t
*);
36 int arch_spin_trylock_retry(arch_spinlock_t
*);
37 void arch_spin_lock_wait_flags(arch_spinlock_t
*, unsigned long flags
);
39 static inline void arch_spin_relax(arch_spinlock_t
*lock
)
41 arch_lock_relax(lock
->lock
);
44 static inline u32
arch_spin_lockval(int cpu
)
49 static inline int arch_spin_value_unlocked(arch_spinlock_t lock
)
51 return lock
.lock
== 0;
54 static inline int arch_spin_is_locked(arch_spinlock_t
*lp
)
56 return ACCESS_ONCE(lp
->lock
) != 0;
59 static inline int arch_spin_trylock_once(arch_spinlock_t
*lp
)
62 return likely(arch_spin_value_unlocked(*lp
) &&
63 _raw_compare_and_swap(&lp
->lock
, 0, SPINLOCK_LOCKVAL
));
66 static inline void arch_spin_lock(arch_spinlock_t
*lp
)
68 if (!arch_spin_trylock_once(lp
))
69 arch_spin_lock_wait(lp
);
72 static inline void arch_spin_lock_flags(arch_spinlock_t
*lp
,
75 if (!arch_spin_trylock_once(lp
))
76 arch_spin_lock_wait_flags(lp
, flags
);
79 static inline int arch_spin_trylock(arch_spinlock_t
*lp
)
81 if (!arch_spin_trylock_once(lp
))
82 return arch_spin_trylock_retry(lp
);
86 static inline void arch_spin_unlock(arch_spinlock_t
*lp
)
88 typecheck(unsigned int, lp
->lock
);
97 static inline void arch_spin_unlock_wait(arch_spinlock_t
*lock
)
99 while (arch_spin_is_locked(lock
))
100 arch_spin_relax(lock
);
104 * Read-write spinlocks, allowing multiple readers
105 * but only one writer.
107 * NOTE! it is quite common to have readers in interrupts
108 * but no interrupt writers. For those circumstances we
109 * can "mix" irq-safe locks - any writer needs to get a
110 * irq-safe write-lock, but readers can get non-irqsafe
115 * read_can_lock - would read_trylock() succeed?
116 * @lock: the rwlock in question.
118 #define arch_read_can_lock(x) ((int)(x)->lock >= 0)
121 * write_can_lock - would write_trylock() succeed?
122 * @lock: the rwlock in question.
124 #define arch_write_can_lock(x) ((x)->lock == 0)
126 extern int _raw_read_trylock_retry(arch_rwlock_t
*lp
);
127 extern int _raw_write_trylock_retry(arch_rwlock_t
*lp
);
129 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
130 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
132 static inline int arch_read_trylock_once(arch_rwlock_t
*rw
)
134 unsigned int old
= ACCESS_ONCE(rw
->lock
);
135 return likely((int) old
>= 0 &&
136 _raw_compare_and_swap(&rw
->lock
, old
, old
+ 1));
139 static inline int arch_write_trylock_once(arch_rwlock_t
*rw
)
141 unsigned int old
= ACCESS_ONCE(rw
->lock
);
142 return likely(old
== 0 &&
143 _raw_compare_and_swap(&rw
->lock
, 0, 0x80000000));
146 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
148 #define __RAW_OP_OR "lao"
149 #define __RAW_OP_AND "lan"
150 #define __RAW_OP_ADD "laa"
152 #define __RAW_LOCK(ptr, op_val, op_string) \
154 unsigned int old_val; \
156 typecheck(unsigned int *, ptr); \
158 op_string " %0,%2,%1\n" \
160 : "=d" (old_val), "+Q" (*ptr) \
166 #define __RAW_UNLOCK(ptr, op_val, op_string) \
168 unsigned int old_val; \
170 typecheck(unsigned int *, ptr); \
173 op_string " %0,%2,%1\n" \
174 : "=d" (old_val), "+Q" (*ptr) \
180 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
181 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
, unsigned int prev
);
183 static inline void arch_read_lock(arch_rwlock_t
*rw
)
187 old
= __RAW_LOCK(&rw
->lock
, 1, __RAW_OP_ADD
);
189 _raw_read_lock_wait(rw
);
192 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
194 __RAW_UNLOCK(&rw
->lock
, -1, __RAW_OP_ADD
);
197 static inline void arch_write_lock(arch_rwlock_t
*rw
)
201 old
= __RAW_LOCK(&rw
->lock
, 0x80000000, __RAW_OP_OR
);
203 _raw_write_lock_wait(rw
, old
);
204 rw
->owner
= SPINLOCK_LOCKVAL
;
207 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
210 __RAW_UNLOCK(&rw
->lock
, 0x7fffffff, __RAW_OP_AND
);
213 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
215 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
216 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
);
218 static inline void arch_read_lock(arch_rwlock_t
*rw
)
220 if (!arch_read_trylock_once(rw
))
221 _raw_read_lock_wait(rw
);
224 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
229 old
= ACCESS_ONCE(rw
->lock
);
230 } while (!_raw_compare_and_swap(&rw
->lock
, old
, old
- 1));
233 static inline void arch_write_lock(arch_rwlock_t
*rw
)
235 if (!arch_write_trylock_once(rw
))
236 _raw_write_lock_wait(rw
);
237 rw
->owner
= SPINLOCK_LOCKVAL
;
240 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
242 typecheck(unsigned int, rw
->lock
);
253 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
255 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
257 if (!arch_read_trylock_once(rw
))
258 return _raw_read_trylock_retry(rw
);
262 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
264 if (!arch_write_trylock_once(rw
) && !_raw_write_trylock_retry(rw
))
266 rw
->owner
= SPINLOCK_LOCKVAL
;
270 static inline void arch_read_relax(arch_rwlock_t
*rw
)
272 arch_lock_relax(rw
->owner
);
275 static inline void arch_write_relax(arch_rwlock_t
*rw
)
277 arch_lock_relax(rw
->owner
);
280 #endif /* __ASM_SPINLOCK_H */