blob: 7e9e09f600fa5932849948de3bc4f66ea871cba2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S390 version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
5 *
6 * Derived from "include/asm-i386/spinlock.h"
7 */
8
9#ifndef __ASM_SPINLOCK_H
10#define __ASM_SPINLOCK_H
11
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070012#include <linux/smp.h>
Peter Zijlstra726328d2016-05-26 10:35:03 +020013#include <asm/barrier.h>
14#include <asm/processor.h>
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070015
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +020016#define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
17
Martin Schwidefsky638ad342011-10-30 15:17:13 +010018extern int spin_retry;
19
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070020static inline int
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020021_raw_compare_and_swap(unsigned int *lock, unsigned int old, unsigned int new)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070022{
Martin Schwidefskyf318a122014-10-29 12:50:31 +010023 return __sync_bool_compare_and_swap(lock, old, new);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070024}
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Simple spin lock operations. There are two variants, one clears IRQ's
28 * on the local processor, one does not.
29 *
30 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070031 *
32 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +020035void arch_lock_relax(unsigned int cpu);
36
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020037void arch_spin_lock_wait(arch_spinlock_t *);
38int arch_spin_trylock_retry(arch_spinlock_t *);
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020039void arch_spin_lock_wait_flags(arch_spinlock_t *, unsigned long flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +020041static inline void arch_spin_relax(arch_spinlock_t *lock)
42{
43 arch_lock_relax(lock->lock);
44}
45
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +020046static inline u32 arch_spin_lockval(int cpu)
47{
48 return ~cpu;
49}
50
Heiko Carstensefc1d232013-09-05 13:26:17 +020051static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
52{
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020053 return lock.lock == 0;
54}
55
56static inline int arch_spin_is_locked(arch_spinlock_t *lp)
57{
58 return ACCESS_ONCE(lp->lock) != 0;
59}
60
61static inline int arch_spin_trylock_once(arch_spinlock_t *lp)
62{
Martin Schwidefskybae8f562014-05-15 11:00:44 +020063 barrier();
64 return likely(arch_spin_value_unlocked(*lp) &&
65 _raw_compare_and_swap(&lp->lock, 0, SPINLOCK_LOCKVAL));
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020066}
67
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010068static inline void arch_spin_lock(arch_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Martin Schwidefskybae8f562014-05-15 11:00:44 +020070 if (!arch_spin_trylock_once(lp))
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020071 arch_spin_lock_wait(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010074static inline void arch_spin_lock_flags(arch_spinlock_t *lp,
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020075 unsigned long flags)
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010076{
Martin Schwidefskybae8f562014-05-15 11:00:44 +020077 if (!arch_spin_trylock_once(lp))
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020078 arch_spin_lock_wait_flags(lp, flags);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010079}
80
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010081static inline int arch_spin_trylock(arch_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Martin Schwidefskybae8f562014-05-15 11:00:44 +020083 if (!arch_spin_trylock_once(lp))
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020084 return arch_spin_trylock_retry(lp);
85 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010088static inline void arch_spin_unlock(arch_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Heiko Carstens44230282014-09-08 08:20:43 +020090 typecheck(unsigned int, lp->lock);
91 asm volatile(
Heiko Carstens44230282014-09-08 08:20:43 +020092 "st %1,%0\n"
93 : "+Q" (lp->lock)
94 : "d" (0)
95 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +020097
98static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
99{
100 while (arch_spin_is_locked(lock))
101 arch_spin_relax(lock);
Peter Zijlstra726328d2016-05-26 10:35:03 +0200102 smp_acquire__after_ctrl_dep();
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Read-write spinlocks, allowing multiple readers
107 * but only one writer.
108 *
109 * NOTE! it is quite common to have readers in interrupts
110 * but no interrupt writers. For those circumstances we
111 * can "mix" irq-safe locks - any writer needs to get a
112 * irq-safe write-lock, but readers can get non-irqsafe
113 * read-locks.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/**
117 * read_can_lock - would read_trylock() succeed?
118 * @lock: the rwlock in question.
119 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100120#define arch_read_can_lock(x) ((int)(x)->lock >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/**
123 * write_can_lock - would write_trylock() succeed?
124 * @lock: the rwlock in question.
125 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100126#define arch_write_can_lock(x) ((x)->lock == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Martin Schwidefsky2684e732014-09-22 14:45:11 +0200128extern int _raw_read_trylock_retry(arch_rwlock_t *lp);
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100129extern int _raw_write_trylock_retry(arch_rwlock_t *lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Martin Schwidefsky2684e732014-09-22 14:45:11 +0200131#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
132#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
133
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200134static inline int arch_read_trylock_once(arch_rwlock_t *rw)
135{
136 unsigned int old = ACCESS_ONCE(rw->lock);
137 return likely((int) old >= 0 &&
138 _raw_compare_and_swap(&rw->lock, old, old + 1));
139}
140
141static inline int arch_write_trylock_once(arch_rwlock_t *rw)
142{
143 unsigned int old = ACCESS_ONCE(rw->lock);
144 return likely(old == 0 &&
145 _raw_compare_and_swap(&rw->lock, 0, 0x80000000));
146}
147
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200148#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
149
150#define __RAW_OP_OR "lao"
151#define __RAW_OP_AND "lan"
152#define __RAW_OP_ADD "laa"
153
154#define __RAW_LOCK(ptr, op_val, op_string) \
155({ \
156 unsigned int old_val; \
157 \
158 typecheck(unsigned int *, ptr); \
159 asm volatile( \
160 op_string " %0,%2,%1\n" \
161 "bcr 14,0\n" \
162 : "=d" (old_val), "+Q" (*ptr) \
163 : "d" (op_val) \
164 : "cc", "memory"); \
165 old_val; \
166})
167
168#define __RAW_UNLOCK(ptr, op_val, op_string) \
169({ \
170 unsigned int old_val; \
171 \
172 typecheck(unsigned int *, ptr); \
173 asm volatile( \
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200174 op_string " %0,%2,%1\n" \
175 : "=d" (old_val), "+Q" (*ptr) \
176 : "d" (op_val) \
177 : "cc", "memory"); \
178 old_val; \
179})
180
181extern void _raw_read_lock_wait(arch_rwlock_t *lp);
182extern void _raw_write_lock_wait(arch_rwlock_t *lp, unsigned int prev);
183
184static inline void arch_read_lock(arch_rwlock_t *rw)
185{
186 unsigned int old;
187
188 old = __RAW_LOCK(&rw->lock, 1, __RAW_OP_ADD);
189 if ((int) old < 0)
190 _raw_read_lock_wait(rw);
191}
192
193static inline void arch_read_unlock(arch_rwlock_t *rw)
194{
195 __RAW_UNLOCK(&rw->lock, -1, __RAW_OP_ADD);
196}
197
198static inline void arch_write_lock(arch_rwlock_t *rw)
199{
200 unsigned int old;
201
202 old = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
203 if (old != 0)
204 _raw_write_lock_wait(rw, old);
205 rw->owner = SPINLOCK_LOCKVAL;
206}
207
208static inline void arch_write_unlock(arch_rwlock_t *rw)
209{
210 rw->owner = 0;
211 __RAW_UNLOCK(&rw->lock, 0x7fffffff, __RAW_OP_AND);
212}
213
214#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
215
216extern void _raw_read_lock_wait(arch_rwlock_t *lp);
217extern void _raw_write_lock_wait(arch_rwlock_t *lp);
218
Thomas Gleixnere5931942009-12-03 20:08:46 +0100219static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200221 if (!arch_read_trylock_once(rw))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700222 _raw_read_lock_wait(rw);
223}
224
Thomas Gleixnere5931942009-12-03 20:08:46 +0100225static inline void arch_read_unlock(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700226{
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200227 unsigned int old;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700228
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700229 do {
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200230 old = ACCESS_ONCE(rw->lock);
231 } while (!_raw_compare_and_swap(&rw->lock, old, old - 1));
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700232}
233
Thomas Gleixnere5931942009-12-03 20:08:46 +0100234static inline void arch_write_lock(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700235{
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200236 if (!arch_write_trylock_once(rw))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700237 _raw_write_lock_wait(rw);
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200238 rw->owner = SPINLOCK_LOCKVAL;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700239}
240
Thomas Gleixnere5931942009-12-03 20:08:46 +0100241static inline void arch_write_unlock(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700242{
Heiko Carstens44230282014-09-08 08:20:43 +0200243 typecheck(unsigned int, rw->lock);
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200244
245 rw->owner = 0;
Heiko Carstens44230282014-09-08 08:20:43 +0200246 asm volatile(
Heiko Carstens44230282014-09-08 08:20:43 +0200247 "st %1,%0\n"
248 : "+Q" (rw->lock)
249 : "d" (0)
250 : "cc", "memory");
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700251}
252
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200253#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
254
Thomas Gleixnere5931942009-12-03 20:08:46 +0100255static inline int arch_read_trylock(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700256{
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200257 if (!arch_read_trylock_once(rw))
258 return _raw_read_trylock_retry(rw);
259 return 1;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700260}
261
Thomas Gleixnere5931942009-12-03 20:08:46 +0100262static inline int arch_write_trylock(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700263{
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200264 if (!arch_write_trylock_once(rw) && !_raw_write_trylock_retry(rw))
265 return 0;
266 rw->owner = SPINLOCK_LOCKVAL;
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200267 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200270static inline void arch_read_relax(arch_rwlock_t *rw)
271{
272 arch_lock_relax(rw->owner);
273}
274
275static inline void arch_write_relax(arch_rwlock_t *rw)
276{
277 arch_lock_relax(rw->owner);
278}
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif /* __ASM_SPINLOCK_H */