Merge tag 'pm+acpi-4.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux/fpc-iii.git] / arch / s390 / include / asm / spinlock.h
blob0e37cd0412419ffeb870a8e7f0c51c5ca2833b5f
1 /*
2 * S390 version
3 * Copyright IBM Corp. 1999
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Derived from "include/asm-i386/spinlock.h"
7 */
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;
18 static inline int
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)
46 return ~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)
61 barrier();
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,
73 unsigned long flags)
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);
83 return 1;
86 static inline void arch_spin_unlock(arch_spinlock_t *lp)
88 typecheck(unsigned int, lp->lock);
89 asm volatile(
90 __ASM_BARRIER
91 "st %1,%0\n"
92 : "+Q" (lp->lock)
93 : "d" (0)
94 : "cc", "memory");
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
111 * read-locks.
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) \
153 ({ \
154 unsigned int old_val; \
156 typecheck(unsigned int *, ptr); \
157 asm volatile( \
158 op_string " %0,%2,%1\n" \
159 "bcr 14,0\n" \
160 : "=d" (old_val), "+Q" (*ptr) \
161 : "d" (op_val) \
162 : "cc", "memory"); \
163 old_val; \
166 #define __RAW_UNLOCK(ptr, op_val, op_string) \
167 ({ \
168 unsigned int old_val; \
170 typecheck(unsigned int *, ptr); \
171 asm volatile( \
172 "bcr 14,0\n" \
173 op_string " %0,%2,%1\n" \
174 : "=d" (old_val), "+Q" (*ptr) \
175 : "d" (op_val) \
176 : "cc", "memory"); \
177 old_val; \
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)
185 unsigned int old;
187 old = __RAW_LOCK(&rw->lock, 1, __RAW_OP_ADD);
188 if ((int) old < 0)
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)
199 unsigned int old;
201 old = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
202 if (old != 0)
203 _raw_write_lock_wait(rw, old);
204 rw->owner = SPINLOCK_LOCKVAL;
207 static inline void arch_write_unlock(arch_rwlock_t *rw)
209 rw->owner = 0;
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)
226 unsigned int old;
228 do {
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);
244 rw->owner = 0;
245 asm volatile(
246 __ASM_BARRIER
247 "st %1,%0\n"
248 : "+Q" (rw->lock)
249 : "d" (0)
250 : "cc", "memory");
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);
259 return 1;
262 static inline int arch_write_trylock(arch_rwlock_t *rw)
264 if (!arch_write_trylock_once(rw) && !_raw_write_trylock_retry(rw))
265 return 0;
266 rw->owner = SPINLOCK_LOCKVAL;
267 return 1;
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 */