Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/s390/lib/spinlock.c |
| 3 | * Out of line spinlock code. |
| 4 | * |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 5 | * Copyright (C) IBM Corp. 2004, 2006 |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 6 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <asm/io.h> |
| 14 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 15 | int spin_retry = 1000; |
| 16 | |
| 17 | /** |
| 18 | * spin_retry= parameter |
| 19 | */ |
| 20 | static int __init spin_retry_setup(char *str) |
| 21 | { |
| 22 | spin_retry = simple_strtoul(str, &str, 0); |
| 23 | return 1; |
| 24 | } |
| 25 | __setup("spin_retry=", spin_retry_setup); |
| 26 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 27 | static inline void _raw_yield(void) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 28 | { |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 29 | if (MACHINE_HAS_DIAG44) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 30 | asm volatile("diag 0,0,0x44"); |
| 31 | } |
| 32 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 33 | static inline void _raw_yield_cpu(int cpu) |
| 34 | { |
| 35 | if (MACHINE_HAS_DIAG9C) |
| 36 | asm volatile("diag %0,0,0x9c" |
Heiko Carstens | fb380aa | 2010-01-13 20:44:37 +0100 | [diff] [blame] | 37 | : : "d" (cpu_logical_map(cpu))); |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 38 | else |
| 39 | _raw_yield(); |
| 40 | } |
| 41 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 42 | void arch_spin_lock_wait(arch_spinlock_t *lp) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 43 | { |
| 44 | int count = spin_retry; |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 45 | unsigned int cpu = ~smp_processor_id(); |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 46 | unsigned int owner; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 47 | |
| 48 | while (1) { |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 49 | owner = lp->owner_cpu; |
| 50 | if (!owner || smp_vcpu_scheduled(~owner)) { |
| 51 | for (count = spin_retry; count > 0; count--) { |
| 52 | if (arch_spin_is_locked(lp)) |
| 53 | continue; |
| 54 | if (_raw_compare_and_swap(&lp->owner_cpu, 0, |
| 55 | cpu) == 0) |
| 56 | return; |
| 57 | } |
| 58 | if (MACHINE_IS_LPAR) |
| 59 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 60 | } |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 61 | owner = lp->owner_cpu; |
| 62 | if (owner) |
| 63 | _raw_yield_cpu(~owner); |
Heiko Carstens | 3b4beb3 | 2008-01-26 14:11:03 +0100 | [diff] [blame] | 64 | if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 68 | EXPORT_SYMBOL(arch_spin_lock_wait); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 69 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | int count = spin_retry; |
| 73 | unsigned int cpu = ~smp_processor_id(); |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 74 | unsigned int owner; |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 75 | |
| 76 | local_irq_restore(flags); |
| 77 | while (1) { |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 78 | owner = lp->owner_cpu; |
| 79 | if (!owner || smp_vcpu_scheduled(~owner)) { |
| 80 | for (count = spin_retry; count > 0; count--) { |
| 81 | if (arch_spin_is_locked(lp)) |
| 82 | continue; |
| 83 | local_irq_disable(); |
| 84 | if (_raw_compare_and_swap(&lp->owner_cpu, 0, |
| 85 | cpu) == 0) |
| 86 | return; |
| 87 | local_irq_restore(flags); |
| 88 | } |
| 89 | if (MACHINE_IS_LPAR) |
| 90 | continue; |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 91 | } |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 92 | owner = lp->owner_cpu; |
| 93 | if (owner) |
| 94 | _raw_yield_cpu(~owner); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 95 | local_irq_disable(); |
| 96 | if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0) |
| 97 | return; |
| 98 | local_irq_restore(flags); |
| 99 | } |
| 100 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 101 | EXPORT_SYMBOL(arch_spin_lock_wait_flags); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 102 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 103 | int arch_spin_trylock_retry(arch_spinlock_t *lp) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 104 | { |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 105 | unsigned int cpu = ~smp_processor_id(); |
| 106 | int count; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 107 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 108 | for (count = spin_retry; count > 0; count--) { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 109 | if (arch_spin_is_locked(lp)) |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 110 | continue; |
Heiko Carstens | 3b4beb3 | 2008-01-26 14:11:03 +0100 | [diff] [blame] | 111 | if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 116 | EXPORT_SYMBOL(arch_spin_trylock_retry); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 117 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 118 | void arch_spin_relax(arch_spinlock_t *lock) |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 119 | { |
| 120 | unsigned int cpu = lock->owner_cpu; |
Gerald Schaefer | 59b6978 | 2010-02-26 22:37:40 +0100 | [diff] [blame] | 121 | if (cpu != 0) { |
| 122 | if (MACHINE_IS_VM || MACHINE_IS_KVM || |
| 123 | !smp_vcpu_scheduled(~cpu)) |
| 124 | _raw_yield_cpu(~cpu); |
| 125 | } |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 126 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 127 | EXPORT_SYMBOL(arch_spin_relax); |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 128 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 129 | void _raw_read_lock_wait(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 130 | { |
| 131 | unsigned int old; |
| 132 | int count = spin_retry; |
| 133 | |
| 134 | while (1) { |
| 135 | if (count-- <= 0) { |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 136 | _raw_yield(); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 137 | count = spin_retry; |
| 138 | } |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 139 | if (!arch_read_can_lock(rw)) |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 140 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 141 | old = rw->lock & 0x7fffffffU; |
| 142 | if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old) |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | EXPORT_SYMBOL(_raw_read_lock_wait); |
| 147 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 148 | void _raw_read_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 149 | { |
| 150 | unsigned int old; |
| 151 | int count = spin_retry; |
| 152 | |
| 153 | local_irq_restore(flags); |
| 154 | while (1) { |
| 155 | if (count-- <= 0) { |
| 156 | _raw_yield(); |
| 157 | count = spin_retry; |
| 158 | } |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 159 | if (!arch_read_can_lock(rw)) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 160 | continue; |
| 161 | old = rw->lock & 0x7fffffffU; |
| 162 | local_irq_disable(); |
| 163 | if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old) |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | EXPORT_SYMBOL(_raw_read_lock_wait_flags); |
| 168 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 169 | int _raw_read_trylock_retry(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 170 | { |
| 171 | unsigned int old; |
| 172 | int count = spin_retry; |
| 173 | |
| 174 | while (count-- > 0) { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 175 | if (!arch_read_can_lock(rw)) |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 176 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 177 | old = rw->lock & 0x7fffffffU; |
| 178 | if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old) |
| 179 | return 1; |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | EXPORT_SYMBOL(_raw_read_trylock_retry); |
| 184 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 185 | void _raw_write_lock_wait(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 186 | { |
| 187 | int count = spin_retry; |
| 188 | |
| 189 | while (1) { |
| 190 | if (count-- <= 0) { |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 191 | _raw_yield(); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 192 | count = spin_retry; |
| 193 | } |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 194 | if (!arch_write_can_lock(rw)) |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 195 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 196 | if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0) |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | EXPORT_SYMBOL(_raw_write_lock_wait); |
| 201 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 202 | void _raw_write_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 203 | { |
| 204 | int count = spin_retry; |
| 205 | |
| 206 | local_irq_restore(flags); |
| 207 | while (1) { |
| 208 | if (count-- <= 0) { |
| 209 | _raw_yield(); |
| 210 | count = spin_retry; |
| 211 | } |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 212 | if (!arch_write_can_lock(rw)) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 213 | continue; |
| 214 | local_irq_disable(); |
| 215 | if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0) |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | EXPORT_SYMBOL(_raw_write_lock_wait_flags); |
| 220 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 221 | int _raw_write_trylock_retry(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 222 | { |
| 223 | int count = spin_retry; |
| 224 | |
| 225 | while (count-- > 0) { |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 226 | if (!arch_write_can_lock(rw)) |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame] | 227 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 228 | if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0) |
| 229 | return 1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | EXPORT_SYMBOL(_raw_write_trylock_retry); |