2 * Out of line spinlock code.
4 * Copyright IBM Corp. 2004, 2006
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/init.h>
12 #include <linux/smp.h>
15 int spin_retry = 1000;
18 * spin_retry= parameter
20 static int __init spin_retry_setup(char *str)
22 spin_retry = simple_strtoul(str, &str, 0);
25 __setup("spin_retry=", spin_retry_setup);
27 void arch_spin_lock_wait(arch_spinlock_t *lp)
29 unsigned int cpu = SPINLOCK_LOCKVAL;
34 owner = ACCESS_ONCE(lp->lock);
35 /* Try to get the lock if it is free. */
37 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
41 /* Check if the lock owner is running. */
42 if (!smp_vcpu_scheduled(~owner)) {
43 smp_yield_cpu(~owner);
46 /* Loop for a while on the lock value. */
49 owner = ACCESS_ONCE(lp->lock);
50 } while (owner && count-- > 0);
54 * For multiple layers of hypervisors, e.g. z/VM + LPAR
55 * yield the CPU if the lock is still unavailable.
58 smp_yield_cpu(~owner);
61 EXPORT_SYMBOL(arch_spin_lock_wait);
63 void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
65 unsigned int cpu = SPINLOCK_LOCKVAL;
69 local_irq_restore(flags);
71 owner = ACCESS_ONCE(lp->lock);
72 /* Try to get the lock if it is free. */
75 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
77 local_irq_restore(flags);
79 /* Check if the lock owner is running. */
80 if (!smp_vcpu_scheduled(~owner)) {
81 smp_yield_cpu(~owner);
84 /* Loop for a while on the lock value. */
87 owner = ACCESS_ONCE(lp->lock);
88 } while (owner && count-- > 0);
92 * For multiple layers of hypervisors, e.g. z/VM + LPAR
93 * yield the CPU if the lock is still unavailable.
96 smp_yield_cpu(~owner);
99 EXPORT_SYMBOL(arch_spin_lock_wait_flags);
101 int arch_spin_trylock_retry(arch_spinlock_t *lp)
105 for (count = spin_retry; count > 0; count--)
106 if (arch_spin_trylock_once(lp))
110 EXPORT_SYMBOL(arch_spin_trylock_retry);
112 void _raw_read_lock_wait(arch_rwlock_t *rw)
114 unsigned int owner, old;
115 int count = spin_retry;
117 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
118 __RAW_LOCK(&rw->lock, -1, __RAW_OP_ADD);
123 if (owner && !smp_vcpu_scheduled(~owner))
124 smp_yield_cpu(~owner);
127 old = ACCESS_ONCE(rw->lock);
128 owner = ACCESS_ONCE(rw->owner);
131 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
135 EXPORT_SYMBOL(_raw_read_lock_wait);
137 int _raw_read_trylock_retry(arch_rwlock_t *rw)
140 int count = spin_retry;
142 while (count-- > 0) {
143 old = ACCESS_ONCE(rw->lock);
146 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
151 EXPORT_SYMBOL(_raw_read_trylock_retry);
153 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
155 void _raw_write_lock_wait(arch_rwlock_t *rw, unsigned int prev)
157 unsigned int owner, old;
158 int count = spin_retry;
163 if (owner && !smp_vcpu_scheduled(~owner))
164 smp_yield_cpu(~owner);
167 old = ACCESS_ONCE(rw->lock);
168 owner = ACCESS_ONCE(rw->owner);
170 if ((int) old >= 0) {
171 prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
174 if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
178 EXPORT_SYMBOL(_raw_write_lock_wait);
180 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
182 void _raw_write_lock_wait(arch_rwlock_t *rw)
184 unsigned int owner, old, prev;
185 int count = spin_retry;
191 if (owner && !smp_vcpu_scheduled(~owner))
192 smp_yield_cpu(~owner);
195 old = ACCESS_ONCE(rw->lock);
196 owner = ACCESS_ONCE(rw->owner);
197 if ((int) old >= 0 &&
198 _raw_compare_and_swap(&rw->lock, old, old | 0x80000000))
202 if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
206 EXPORT_SYMBOL(_raw_write_lock_wait);
208 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
210 int _raw_write_trylock_retry(arch_rwlock_t *rw)
213 int count = spin_retry;
215 while (count-- > 0) {
216 old = ACCESS_ONCE(rw->lock);
219 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
224 EXPORT_SYMBOL(_raw_write_trylock_retry);
226 void arch_lock_relax(unsigned int cpu)
230 if (MACHINE_IS_LPAR && smp_vcpu_scheduled(~cpu))
234 EXPORT_SYMBOL(arch_lock_relax);