blob: d4549c9645892aef684c60ac73a2c8d22886e58e [file] [log] [blame]
Martin Schwidefsky951f22d2005-07-27 11:44:57 -07001/*
Martin Schwidefsky951f22d2005-07-27 11:44:57 -07002 * Out of line spinlock code.
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2004, 2006
Martin Schwidefsky951f22d2005-07-27 11:44:57 -07005 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 */
7
8#include <linux/types.h>
9#include <linux/module.h>
10#include <linux/spinlock.h>
11#include <linux/init.h>
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040012#include <linux/smp.h>
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070013#include <asm/io.h>
14
Martin Schwidefsky2c72a442015-01-14 17:52:33 +010015int spin_retry = -1;
16
17static int __init spin_retry_init(void)
18{
19 if (spin_retry < 0)
20 spin_retry = MACHINE_HAS_CAD ? 10 : 1000;
21 return 0;
22}
23early_initcall(spin_retry_init);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070024
25/**
26 * spin_retry= parameter
27 */
28static int __init spin_retry_setup(char *str)
29{
30 spin_retry = simple_strtoul(str, &str, 0);
31 return 1;
32}
33__setup("spin_retry=", spin_retry_setup);
34
Martin Schwidefsky2c72a442015-01-14 17:52:33 +010035static inline void _raw_compare_and_delay(unsigned int *lock, unsigned int old)
36{
37 asm(".insn rsy,0xeb0000000022,%0,0,%1" : : "d" (old), "Q" (*lock));
38}
39
Martin Schwidefsky419123f2015-11-19 11:09:45 +010040static inline int cpu_is_preempted(int cpu)
41{
42 if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
43 return 0;
44 if (smp_vcpu_scheduled(cpu))
45 return 0;
46 return 1;
47}
48
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010049void arch_spin_lock_wait(arch_spinlock_t *lp)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070050{
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +020051 unsigned int cpu = SPINLOCK_LOCKVAL;
Gerald Schaefer59b69782010-02-26 22:37:40 +010052 unsigned int owner;
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010053 int count, first_diag;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070054
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010055 first_diag = 1;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070056 while (1) {
Martin Schwidefsky470ada62014-05-16 15:11:12 +020057 owner = ACCESS_ONCE(lp->lock);
58 /* Try to get the lock if it is free. */
59 if (!owner) {
60 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
61 return;
62 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070063 }
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010064 /* First iteration: check if the lock owner is running. */
Martin Schwidefsky419123f2015-11-19 11:09:45 +010065 if (first_diag && cpu_is_preempted(~owner)) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040066 smp_yield_cpu(~owner);
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010067 first_diag = 0;
Martin Schwidefsky470ada62014-05-16 15:11:12 +020068 continue;
69 }
70 /* Loop for a while on the lock value. */
71 count = spin_retry;
72 do {
Martin Schwidefsky2c72a442015-01-14 17:52:33 +010073 if (MACHINE_HAS_CAD)
74 _raw_compare_and_delay(&lp->lock, owner);
Martin Schwidefsky470ada62014-05-16 15:11:12 +020075 owner = ACCESS_ONCE(lp->lock);
76 } while (owner && count-- > 0);
77 if (!owner)
78 continue;
79 /*
80 * For multiple layers of hypervisors, e.g. z/VM + LPAR
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010081 * yield the CPU unconditionally. For LPAR rely on the
82 * sense running status.
Martin Schwidefsky470ada62014-05-16 15:11:12 +020083 */
Martin Schwidefsky419123f2015-11-19 11:09:45 +010084 if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) {
Martin Schwidefsky470ada62014-05-16 15:11:12 +020085 smp_yield_cpu(~owner);
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010086 first_diag = 0;
87 }
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070088 }
89}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010090EXPORT_SYMBOL(arch_spin_lock_wait);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070091
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010092void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010093{
Philipp Hachtmann6c8cd5b2014-04-07 18:25:23 +020094 unsigned int cpu = SPINLOCK_LOCKVAL;
Gerald Schaefer59b69782010-02-26 22:37:40 +010095 unsigned int owner;
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010096 int count, first_diag;
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010097
98 local_irq_restore(flags);
Martin Schwidefskydb1c4512015-11-12 12:51:17 +010099 first_diag = 1;
Hisashi Hifumi894cdde2008-01-26 14:11:28 +0100100 while (1) {
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200101 owner = ACCESS_ONCE(lp->lock);
102 /* Try to get the lock if it is free. */
103 if (!owner) {
104 local_irq_disable();
105 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
106 return;
107 local_irq_restore(flags);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +0100108 }
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200109 /* Check if the lock owner is running. */
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100110 if (first_diag && cpu_is_preempted(~owner)) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400111 smp_yield_cpu(~owner);
Martin Schwidefskydb1c4512015-11-12 12:51:17 +0100112 first_diag = 0;
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200113 continue;
114 }
115 /* Loop for a while on the lock value. */
116 count = spin_retry;
117 do {
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100118 if (MACHINE_HAS_CAD)
119 _raw_compare_and_delay(&lp->lock, owner);
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200120 owner = ACCESS_ONCE(lp->lock);
121 } while (owner && count-- > 0);
122 if (!owner)
123 continue;
124 /*
125 * For multiple layers of hypervisors, e.g. z/VM + LPAR
Martin Schwidefskydb1c4512015-11-12 12:51:17 +0100126 * yield the CPU unconditionally. For LPAR rely on the
127 * sense running status.
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200128 */
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100129 if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) {
Martin Schwidefsky470ada62014-05-16 15:11:12 +0200130 smp_yield_cpu(~owner);
Martin Schwidefskydb1c4512015-11-12 12:51:17 +0100131 first_diag = 0;
132 }
Hisashi Hifumi894cdde2008-01-26 14:11:28 +0100133 }
134}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100135EXPORT_SYMBOL(arch_spin_lock_wait_flags);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +0100136
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200137int arch_spin_trylock_retry(arch_spinlock_t *lp)
138{
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100139 unsigned int cpu = SPINLOCK_LOCKVAL;
140 unsigned int owner;
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200141 int count;
142
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100143 for (count = spin_retry; count > 0; count--) {
144 owner = ACCESS_ONCE(lp->lock);
145 /* Try to get the lock if it is free. */
146 if (!owner) {
147 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
148 return 1;
149 } else if (MACHINE_HAS_CAD)
150 _raw_compare_and_delay(&lp->lock, owner);
151 }
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200152 return 0;
153}
154EXPORT_SYMBOL(arch_spin_trylock_retry);
155
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100156void _raw_read_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700157{
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200158 unsigned int owner, old;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700159 int count = spin_retry;
160
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200161#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
162 __RAW_LOCK(&rw->lock, -1, __RAW_OP_ADD);
163#endif
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200164 owner = 0;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700165 while (1) {
166 if (count-- <= 0) {
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100167 if (owner && cpu_is_preempted(~owner))
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200168 smp_yield_cpu(~owner);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700169 count = spin_retry;
170 }
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200171 old = ACCESS_ONCE(rw->lock);
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200172 owner = ACCESS_ONCE(rw->owner);
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100173 if ((int) old < 0) {
174 if (MACHINE_HAS_CAD)
175 _raw_compare_and_delay(&rw->lock, old);
Christian Ehrhardt96567162006-03-09 17:33:49 -0800176 continue;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100177 }
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200178 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700179 return;
180 }
181}
182EXPORT_SYMBOL(_raw_read_lock_wait);
183
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100184int _raw_read_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700185{
186 unsigned int old;
187 int count = spin_retry;
188
189 while (count-- > 0) {
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200190 old = ACCESS_ONCE(rw->lock);
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100191 if ((int) old < 0) {
192 if (MACHINE_HAS_CAD)
193 _raw_compare_and_delay(&rw->lock, old);
Christian Ehrhardt96567162006-03-09 17:33:49 -0800194 continue;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100195 }
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200196 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700197 return 1;
198 }
199 return 0;
200}
201EXPORT_SYMBOL(_raw_read_trylock_retry);
202
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200203#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
204
205void _raw_write_lock_wait(arch_rwlock_t *rw, unsigned int prev)
206{
207 unsigned int owner, old;
208 int count = spin_retry;
209
210 owner = 0;
211 while (1) {
212 if (count-- <= 0) {
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100213 if (owner && cpu_is_preempted(~owner))
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200214 smp_yield_cpu(~owner);
215 count = spin_retry;
216 }
217 old = ACCESS_ONCE(rw->lock);
218 owner = ACCESS_ONCE(rw->owner);
Christian Borntraegere0af21c2015-09-11 16:09:56 +0200219 smp_mb();
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200220 if ((int) old >= 0) {
221 prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
222 old = prev;
223 }
224 if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
225 break;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100226 if (MACHINE_HAS_CAD)
227 _raw_compare_and_delay(&rw->lock, old);
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200228 }
229}
230EXPORT_SYMBOL(_raw_write_lock_wait);
231
232#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
233
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100234void _raw_write_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700235{
Martin Schwidefsky94232a42014-09-22 16:25:25 +0200236 unsigned int owner, old, prev;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700237 int count = spin_retry;
238
Martin Schwidefsky94232a42014-09-22 16:25:25 +0200239 prev = 0x80000000;
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200240 owner = 0;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700241 while (1) {
242 if (count-- <= 0) {
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100243 if (owner && cpu_is_preempted(~owner))
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200244 smp_yield_cpu(~owner);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700245 count = spin_retry;
246 }
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200247 old = ACCESS_ONCE(rw->lock);
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200248 owner = ACCESS_ONCE(rw->owner);
Martin Schwidefsky94232a42014-09-22 16:25:25 +0200249 if ((int) old >= 0 &&
250 _raw_compare_and_swap(&rw->lock, old, old | 0x80000000))
251 prev = old;
252 else
Christian Borntraegere0af21c2015-09-11 16:09:56 +0200253 smp_mb();
Martin Schwidefsky94232a42014-09-22 16:25:25 +0200254 if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
255 break;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100256 if (MACHINE_HAS_CAD)
257 _raw_compare_and_delay(&rw->lock, old);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700258 }
259}
260EXPORT_SYMBOL(_raw_write_lock_wait);
261
Martin Schwidefskybbae71b2014-09-22 16:34:38 +0200262#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
263
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100264int _raw_write_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700265{
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200266 unsigned int old;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700267 int count = spin_retry;
268
269 while (count-- > 0) {
Martin Schwidefskybae8f562014-05-15 11:00:44 +0200270 old = ACCESS_ONCE(rw->lock);
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100271 if (old) {
272 if (MACHINE_HAS_CAD)
273 _raw_compare_and_delay(&rw->lock, old);
Christian Ehrhardt96567162006-03-09 17:33:49 -0800274 continue;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100275 }
Philipp Hachtmann5b3f6832014-04-07 18:25:23 +0200276 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700277 return 1;
278 }
279 return 0;
280}
281EXPORT_SYMBOL(_raw_write_trylock_retry);
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200282
283void arch_lock_relax(unsigned int cpu)
284{
285 if (!cpu)
286 return;
Martin Schwidefsky419123f2015-11-19 11:09:45 +0100287 if (MACHINE_IS_LPAR && !cpu_is_preempted(~cpu))
Martin Schwidefskyd59b93d2014-09-19 14:29:31 +0200288 return;
289 smp_yield_cpu(~cpu);
290}
291EXPORT_SYMBOL(arch_lock_relax);