Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 1 | /* |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 2 | * Out of line spinlock code. |
| 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2004, 2006 |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 5 | * 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 Schwidefsky | 8b646bd | 2012-03-11 11:59:26 -0400 | [diff] [blame] | 12 | #include <linux/smp.h> |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 15 | int spin_retry = -1; |
| 16 | |
| 17 | static int __init spin_retry_init(void) |
| 18 | { |
| 19 | if (spin_retry < 0) |
| 20 | spin_retry = MACHINE_HAS_CAD ? 10 : 1000; |
| 21 | return 0; |
| 22 | } |
| 23 | early_initcall(spin_retry_init); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * spin_retry= parameter |
| 27 | */ |
| 28 | static 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 Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 35 | static 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 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 40 | void arch_spin_lock_wait(arch_spinlock_t *lp) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 41 | { |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 42 | unsigned int cpu = SPINLOCK_LOCKVAL; |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 43 | unsigned int owner; |
Gerald Schaefer | 2e4006b | 2014-05-06 19:41:36 +0200 | [diff] [blame] | 44 | int count; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 45 | |
| 46 | while (1) { |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 47 | owner = ACCESS_ONCE(lp->lock); |
| 48 | /* Try to get the lock if it is free. */ |
| 49 | if (!owner) { |
| 50 | if (_raw_compare_and_swap(&lp->lock, 0, cpu)) |
| 51 | return; |
| 52 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 53 | } |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 54 | /* Check if the lock owner is running. */ |
| 55 | if (!smp_vcpu_scheduled(~owner)) { |
Martin Schwidefsky | 8b646bd | 2012-03-11 11:59:26 -0400 | [diff] [blame] | 56 | smp_yield_cpu(~owner); |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 57 | continue; |
| 58 | } |
| 59 | /* Loop for a while on the lock value. */ |
| 60 | count = spin_retry; |
| 61 | do { |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 62 | if (MACHINE_HAS_CAD) |
| 63 | _raw_compare_and_delay(&lp->lock, owner); |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 64 | owner = ACCESS_ONCE(lp->lock); |
| 65 | } while (owner && count-- > 0); |
| 66 | if (!owner) |
| 67 | continue; |
| 68 | /* |
| 69 | * For multiple layers of hypervisors, e.g. z/VM + LPAR |
| 70 | * yield the CPU if the lock is still unavailable. |
| 71 | */ |
| 72 | if (!MACHINE_IS_LPAR) |
| 73 | smp_yield_cpu(~owner); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 76 | EXPORT_SYMBOL(arch_spin_lock_wait); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 77 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 78 | void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags) |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 79 | { |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 80 | unsigned int cpu = SPINLOCK_LOCKVAL; |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 81 | unsigned int owner; |
Gerald Schaefer | 2e4006b | 2014-05-06 19:41:36 +0200 | [diff] [blame] | 82 | int count; |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 83 | |
| 84 | local_irq_restore(flags); |
| 85 | while (1) { |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 86 | owner = ACCESS_ONCE(lp->lock); |
| 87 | /* Try to get the lock if it is free. */ |
| 88 | if (!owner) { |
| 89 | local_irq_disable(); |
| 90 | if (_raw_compare_and_swap(&lp->lock, 0, cpu)) |
| 91 | return; |
| 92 | local_irq_restore(flags); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 93 | } |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 94 | /* Check if the lock owner is running. */ |
| 95 | if (!smp_vcpu_scheduled(~owner)) { |
Martin Schwidefsky | 8b646bd | 2012-03-11 11:59:26 -0400 | [diff] [blame] | 96 | smp_yield_cpu(~owner); |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 97 | continue; |
| 98 | } |
| 99 | /* Loop for a while on the lock value. */ |
| 100 | count = spin_retry; |
| 101 | do { |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 102 | if (MACHINE_HAS_CAD) |
| 103 | _raw_compare_and_delay(&lp->lock, owner); |
Martin Schwidefsky | 470ada6 | 2014-05-16 15:11:12 +0200 | [diff] [blame] | 104 | owner = ACCESS_ONCE(lp->lock); |
| 105 | } while (owner && count-- > 0); |
| 106 | if (!owner) |
| 107 | continue; |
| 108 | /* |
| 109 | * For multiple layers of hypervisors, e.g. z/VM + LPAR |
| 110 | * yield the CPU if the lock is still unavailable. |
| 111 | */ |
| 112 | if (!MACHINE_IS_LPAR) |
| 113 | smp_yield_cpu(~owner); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 116 | EXPORT_SYMBOL(arch_spin_lock_wait_flags); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 117 | |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 118 | int arch_spin_trylock_retry(arch_spinlock_t *lp) |
| 119 | { |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 120 | unsigned int cpu = SPINLOCK_LOCKVAL; |
| 121 | unsigned int owner; |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 122 | int count; |
| 123 | |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 124 | for (count = spin_retry; count > 0; count--) { |
| 125 | owner = ACCESS_ONCE(lp->lock); |
| 126 | /* Try to get the lock if it is free. */ |
| 127 | if (!owner) { |
| 128 | if (_raw_compare_and_swap(&lp->lock, 0, cpu)) |
| 129 | return 1; |
| 130 | } else if (MACHINE_HAS_CAD) |
| 131 | _raw_compare_and_delay(&lp->lock, owner); |
| 132 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | EXPORT_SYMBOL(arch_spin_trylock_retry); |
| 136 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 137 | void _raw_read_lock_wait(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 138 | { |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 139 | unsigned int owner, old; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 140 | int count = spin_retry; |
| 141 | |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 142 | #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES |
| 143 | __RAW_LOCK(&rw->lock, -1, __RAW_OP_ADD); |
| 144 | #endif |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 145 | owner = 0; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 146 | while (1) { |
| 147 | if (count-- <= 0) { |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 148 | if (owner && !smp_vcpu_scheduled(~owner)) |
| 149 | smp_yield_cpu(~owner); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 150 | count = spin_retry; |
| 151 | } |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 152 | old = ACCESS_ONCE(rw->lock); |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 153 | owner = ACCESS_ONCE(rw->owner); |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 154 | if ((int) old < 0) { |
| 155 | if (MACHINE_HAS_CAD) |
| 156 | _raw_compare_and_delay(&rw->lock, old); |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 157 | continue; |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 158 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 159 | if (_raw_compare_and_swap(&rw->lock, old, old + 1)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | } |
| 163 | EXPORT_SYMBOL(_raw_read_lock_wait); |
| 164 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 165 | int _raw_read_trylock_retry(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 166 | { |
| 167 | unsigned int old; |
| 168 | int count = spin_retry; |
| 169 | |
| 170 | while (count-- > 0) { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 171 | old = ACCESS_ONCE(rw->lock); |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 172 | if ((int) old < 0) { |
| 173 | if (MACHINE_HAS_CAD) |
| 174 | _raw_compare_and_delay(&rw->lock, old); |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 175 | continue; |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 176 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 177 | if (_raw_compare_and_swap(&rw->lock, old, old + 1)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 178 | return 1; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | EXPORT_SYMBOL(_raw_read_trylock_retry); |
| 183 | |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 184 | #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES |
| 185 | |
| 186 | void _raw_write_lock_wait(arch_rwlock_t *rw, unsigned int prev) |
| 187 | { |
| 188 | unsigned int owner, old; |
| 189 | int count = spin_retry; |
| 190 | |
| 191 | owner = 0; |
| 192 | while (1) { |
| 193 | if (count-- <= 0) { |
| 194 | if (owner && !smp_vcpu_scheduled(~owner)) |
| 195 | smp_yield_cpu(~owner); |
| 196 | count = spin_retry; |
| 197 | } |
| 198 | old = ACCESS_ONCE(rw->lock); |
| 199 | owner = ACCESS_ONCE(rw->owner); |
Christian Borntraeger | e0af21c | 2015-09-11 16:09:56 +0200 | [diff] [blame] | 200 | smp_mb(); |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 201 | if ((int) old >= 0) { |
| 202 | prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR); |
| 203 | old = prev; |
| 204 | } |
| 205 | if ((old & 0x7fffffff) == 0 && (int) prev >= 0) |
| 206 | break; |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 207 | if (MACHINE_HAS_CAD) |
| 208 | _raw_compare_and_delay(&rw->lock, old); |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | EXPORT_SYMBOL(_raw_write_lock_wait); |
| 212 | |
| 213 | #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 214 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 215 | void _raw_write_lock_wait(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 216 | { |
Martin Schwidefsky | 94232a4 | 2014-09-22 16:25:25 +0200 | [diff] [blame] | 217 | unsigned int owner, old, prev; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 218 | int count = spin_retry; |
| 219 | |
Martin Schwidefsky | 94232a4 | 2014-09-22 16:25:25 +0200 | [diff] [blame] | 220 | prev = 0x80000000; |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 221 | owner = 0; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 222 | while (1) { |
| 223 | if (count-- <= 0) { |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 224 | if (owner && !smp_vcpu_scheduled(~owner)) |
| 225 | smp_yield_cpu(~owner); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 226 | count = spin_retry; |
| 227 | } |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 228 | old = ACCESS_ONCE(rw->lock); |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 229 | owner = ACCESS_ONCE(rw->owner); |
Martin Schwidefsky | 94232a4 | 2014-09-22 16:25:25 +0200 | [diff] [blame] | 230 | if ((int) old >= 0 && |
| 231 | _raw_compare_and_swap(&rw->lock, old, old | 0x80000000)) |
| 232 | prev = old; |
| 233 | else |
Christian Borntraeger | e0af21c | 2015-09-11 16:09:56 +0200 | [diff] [blame] | 234 | smp_mb(); |
Martin Schwidefsky | 94232a4 | 2014-09-22 16:25:25 +0200 | [diff] [blame] | 235 | if ((old & 0x7fffffff) == 0 && (int) prev >= 0) |
| 236 | break; |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 237 | if (MACHINE_HAS_CAD) |
| 238 | _raw_compare_and_delay(&rw->lock, old); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | EXPORT_SYMBOL(_raw_write_lock_wait); |
| 242 | |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 243 | #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 244 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 245 | int _raw_write_trylock_retry(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 246 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 247 | unsigned int old; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 248 | int count = spin_retry; |
| 249 | |
| 250 | while (count-- > 0) { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 251 | old = ACCESS_ONCE(rw->lock); |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 252 | if (old) { |
| 253 | if (MACHINE_HAS_CAD) |
| 254 | _raw_compare_and_delay(&rw->lock, old); |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 255 | continue; |
Martin Schwidefsky | 2c72a44 | 2015-01-14 17:52:33 +0100 | [diff] [blame] | 256 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 257 | if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 258 | return 1; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | EXPORT_SYMBOL(_raw_write_trylock_retry); |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 263 | |
| 264 | void arch_lock_relax(unsigned int cpu) |
| 265 | { |
| 266 | if (!cpu) |
| 267 | return; |
| 268 | if (MACHINE_IS_LPAR && smp_vcpu_scheduled(~cpu)) |
| 269 | return; |
| 270 | smp_yield_cpu(~cpu); |
| 271 | } |
| 272 | EXPORT_SYMBOL(arch_lock_relax); |