spinlock.c 6.3 KB
Newer Older
1 2 3
/*
 *    Out of line spinlock code.
 *
4
 *    Copyright IBM Corp. 2004, 2006
5 6 7 8 9 10 11
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 */

#include <linux/types.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/init.h>
M
Martin Schwidefsky 已提交
12
#include <linux/smp.h>
13 14
#include <asm/io.h>

15 16 17 18 19 20 21 22 23
int spin_retry = -1;

static int __init spin_retry_init(void)
{
	if (spin_retry < 0)
		spin_retry = MACHINE_HAS_CAD ? 10 : 1000;
	return 0;
}
early_initcall(spin_retry_init);
24 25 26 27 28 29 30 31 32 33 34

/**
 * spin_retry= parameter
 */
static int __init spin_retry_setup(char *str)
{
	spin_retry = simple_strtoul(str, &str, 0);
	return 1;
}
__setup("spin_retry=", spin_retry_setup);

35 36 37 38 39
static inline void _raw_compare_and_delay(unsigned int *lock, unsigned int old)
{
	asm(".insn rsy,0xeb0000000022,%0,0,%1" : : "d" (old), "Q" (*lock));
}

40 41 42 43 44 45 46 47 48
static inline int cpu_is_preempted(int cpu)
{
	if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu))
		return 0;
	if (smp_vcpu_scheduled(cpu))
		return 0;
	return 1;
}

49
void arch_spin_lock_wait(arch_spinlock_t *lp)
50
{
51
	unsigned int cpu = SPINLOCK_LOCKVAL;
52
	unsigned int owner;
53
	int count, first_diag;
54

55
	first_diag = 1;
56
	while (1) {
57 58 59 60 61 62
		owner = ACCESS_ONCE(lp->lock);
		/* Try to get the lock if it is free. */
		if (!owner) {
			if (_raw_compare_and_swap(&lp->lock, 0, cpu))
				return;
			continue;
63
		}
64
		/* First iteration: check if the lock owner is running. */
65
		if (first_diag && cpu_is_preempted(~owner)) {
66
			smp_yield_cpu(~owner);
67
			first_diag = 0;
68 69 70 71 72
			continue;
		}
		/* Loop for a while on the lock value. */
		count = spin_retry;
		do {
73 74
			if (MACHINE_HAS_CAD)
				_raw_compare_and_delay(&lp->lock, owner);
75 76 77 78 79 80
			owner = ACCESS_ONCE(lp->lock);
		} while (owner && count-- > 0);
		if (!owner)
			continue;
		/*
		 * For multiple layers of hypervisors, e.g. z/VM + LPAR
81 82
		 * yield the CPU unconditionally. For LPAR rely on the
		 * sense running status.
83
		 */
84
		if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) {
M
Martin Schwidefsky 已提交
85
			smp_yield_cpu(~owner);
86 87
			first_diag = 0;
		}
88 89
	}
}
90
EXPORT_SYMBOL(arch_spin_lock_wait);
91

92
void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
93
{
94
	unsigned int cpu = SPINLOCK_LOCKVAL;
95
	unsigned int owner;
96
	int count, first_diag;
97 98

	local_irq_restore(flags);
99
	first_diag = 1;
100
	while (1) {
101 102 103 104 105 106 107
		owner = ACCESS_ONCE(lp->lock);
		/* Try to get the lock if it is free. */
		if (!owner) {
			local_irq_disable();
			if (_raw_compare_and_swap(&lp->lock, 0, cpu))
				return;
			local_irq_restore(flags);
108
			continue;
109 110
		}
		/* Check if the lock owner is running. */
111
		if (first_diag && cpu_is_preempted(~owner)) {
112
			smp_yield_cpu(~owner);
113
			first_diag = 0;
114
			continue;
115
		}
116 117 118
		/* Loop for a while on the lock value. */
		count = spin_retry;
		do {
119 120
			if (MACHINE_HAS_CAD)
				_raw_compare_and_delay(&lp->lock, owner);
121 122 123 124 125 126
			owner = ACCESS_ONCE(lp->lock);
		} while (owner && count-- > 0);
		if (!owner)
			continue;
		/*
		 * For multiple layers of hypervisors, e.g. z/VM + LPAR
127 128
		 * yield the CPU unconditionally. For LPAR rely on the
		 * sense running status.
129
		 */
130
		if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) {
M
Martin Schwidefsky 已提交
131
			smp_yield_cpu(~owner);
132 133
			first_diag = 0;
		}
134 135
	}
}
136
EXPORT_SYMBOL(arch_spin_lock_wait_flags);
137

138
int arch_spin_trylock_retry(arch_spinlock_t *lp)
139
{
140 141
	unsigned int cpu = SPINLOCK_LOCKVAL;
	unsigned int owner;
142
	int count;
143

144 145 146 147 148 149 150 151 152
	for (count = spin_retry; count > 0; count--) {
		owner = ACCESS_ONCE(lp->lock);
		/* Try to get the lock if it is free. */
		if (!owner) {
			if (_raw_compare_and_swap(&lp->lock, 0, cpu))
				return 1;
		} else if (MACHINE_HAS_CAD)
			_raw_compare_and_delay(&lp->lock, owner);
	}
153 154
	return 0;
}
155
EXPORT_SYMBOL(arch_spin_trylock_retry);
156

157
void _raw_read_lock_wait(arch_rwlock_t *rw)
158
{
159
	unsigned int owner, old;
160 161
	int count = spin_retry;

162 163 164
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
	__RAW_LOCK(&rw->lock, -1, __RAW_OP_ADD);
#endif
165
	owner = 0;
166 167
	while (1) {
		if (count-- <= 0) {
168
			if (owner && cpu_is_preempted(~owner))
169
				smp_yield_cpu(~owner);
170 171
			count = spin_retry;
		}
172
		old = ACCESS_ONCE(rw->lock);
173
		owner = ACCESS_ONCE(rw->owner);
174 175 176
		if ((int) old < 0) {
			if (MACHINE_HAS_CAD)
				_raw_compare_and_delay(&rw->lock, old);
177
			continue;
178
		}
179
		if (_raw_compare_and_swap(&rw->lock, old, old + 1))
180 181 182 183 184
			return;
	}
}
EXPORT_SYMBOL(_raw_read_lock_wait);

185
int _raw_read_trylock_retry(arch_rwlock_t *rw)
186 187 188 189 190
{
	unsigned int old;
	int count = spin_retry;

	while (count-- > 0) {
191
		old = ACCESS_ONCE(rw->lock);
192 193 194
		if ((int) old < 0) {
			if (MACHINE_HAS_CAD)
				_raw_compare_and_delay(&rw->lock, old);
195
			continue;
196
		}
197
		if (_raw_compare_and_swap(&rw->lock, old, old + 1))
198 199 200 201 202 203
			return 1;
	}
	return 0;
}
EXPORT_SYMBOL(_raw_read_trylock_retry);

204 205 206 207 208 209 210 211 212 213
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES

void _raw_write_lock_wait(arch_rwlock_t *rw, unsigned int prev)
{
	unsigned int owner, old;
	int count = spin_retry;

	owner = 0;
	while (1) {
		if (count-- <= 0) {
214
			if (owner && cpu_is_preempted(~owner))
215 216 217 218 219
				smp_yield_cpu(~owner);
			count = spin_retry;
		}
		old = ACCESS_ONCE(rw->lock);
		owner = ACCESS_ONCE(rw->owner);
220
		smp_mb();
221 222 223 224 225 226
		if ((int) old >= 0) {
			prev = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR);
			old = prev;
		}
		if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
			break;
227 228
		if (MACHINE_HAS_CAD)
			_raw_compare_and_delay(&rw->lock, old);
229 230 231 232 233 234
	}
}
EXPORT_SYMBOL(_raw_write_lock_wait);

#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */

235
void _raw_write_lock_wait(arch_rwlock_t *rw)
236
{
237
	unsigned int owner, old, prev;
238 239
	int count = spin_retry;

240
	prev = 0x80000000;
241
	owner = 0;
242 243
	while (1) {
		if (count-- <= 0) {
244
			if (owner && cpu_is_preempted(~owner))
245
				smp_yield_cpu(~owner);
246 247
			count = spin_retry;
		}
248
		old = ACCESS_ONCE(rw->lock);
249
		owner = ACCESS_ONCE(rw->owner);
250 251 252 253
		if ((int) old >= 0 &&
		    _raw_compare_and_swap(&rw->lock, old, old | 0x80000000))
			prev = old;
		else
254
			smp_mb();
255 256
		if ((old & 0x7fffffff) == 0 && (int) prev >= 0)
			break;
257 258
		if (MACHINE_HAS_CAD)
			_raw_compare_and_delay(&rw->lock, old);
259 260 261 262
	}
}
EXPORT_SYMBOL(_raw_write_lock_wait);

263 264
#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */

265
int _raw_write_trylock_retry(arch_rwlock_t *rw)
266
{
267
	unsigned int old;
268 269 270
	int count = spin_retry;

	while (count-- > 0) {
271
		old = ACCESS_ONCE(rw->lock);
272 273 274
		if (old) {
			if (MACHINE_HAS_CAD)
				_raw_compare_and_delay(&rw->lock, old);
275
			continue;
276
		}
277
		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
278 279 280 281 282
			return 1;
	}
	return 0;
}
EXPORT_SYMBOL(_raw_write_trylock_retry);
283 284 285 286 287

void arch_lock_relax(unsigned int cpu)
{
	if (!cpu)
		return;
288
	if (MACHINE_IS_LPAR && !cpu_is_preempted(~cpu))
289 290 291 292
		return;
	smp_yield_cpu(~cpu);
}
EXPORT_SYMBOL(arch_lock_relax);