spinlock.c 4.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 15 16 17 18 19 20 21 22 23 24 25 26
#include <asm/io.h>

int spin_retry = 1000;

/**
 * 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);

27
void arch_spin_lock_wait(arch_spinlock_t *lp)
28 29
{
	int count = spin_retry;
30
	unsigned int cpu = SPINLOCK_LOCKVAL;
31
	unsigned int owner;
32 33

	while (1) {
34
		owner = lp->lock;
35 36 37 38
		if (!owner || smp_vcpu_scheduled(~owner)) {
			for (count = spin_retry; count > 0; count--) {
				if (arch_spin_is_locked(lp))
					continue;
39
				if (_raw_compare_and_swap(&lp->lock, 0, cpu))
40 41 42 43
					return;
			}
			if (MACHINE_IS_LPAR)
				continue;
44
		}
45
		owner = lp->lock;
46
		if (owner)
M
Martin Schwidefsky 已提交
47
			smp_yield_cpu(~owner);
48
		if (_raw_compare_and_swap(&lp->lock, 0, cpu))
49 50 51
			return;
	}
}
52
EXPORT_SYMBOL(arch_spin_lock_wait);
53

54
void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
55 56
{
	int count = spin_retry;
57
	unsigned int cpu = SPINLOCK_LOCKVAL;
58
	unsigned int owner;
59 60 61

	local_irq_restore(flags);
	while (1) {
62
		owner = lp->lock;
63 64 65 66 67
		if (!owner || smp_vcpu_scheduled(~owner)) {
			for (count = spin_retry; count > 0; count--) {
				if (arch_spin_is_locked(lp))
					continue;
				local_irq_disable();
68
				if (_raw_compare_and_swap(&lp->lock, 0, cpu))
69 70 71 72 73
					return;
				local_irq_restore(flags);
			}
			if (MACHINE_IS_LPAR)
				continue;
74
		}
75
		owner = lp->lock;
76
		if (owner)
M
Martin Schwidefsky 已提交
77
			smp_yield_cpu(~owner);
78
		local_irq_disable();
79
		if (_raw_compare_and_swap(&lp->lock, 0, cpu))
80 81 82 83
			return;
		local_irq_restore(flags);
	}
}
84
EXPORT_SYMBOL(arch_spin_lock_wait_flags);
85

86 87 88 89 90 91 92 93 94 95 96
void arch_spin_relax(arch_spinlock_t *lp)
{
	unsigned int cpu = lp->lock;
	if (cpu != 0) {
		if (MACHINE_IS_VM || MACHINE_IS_KVM ||
		    !smp_vcpu_scheduled(~cpu))
			smp_yield_cpu(~cpu);
	}
}
EXPORT_SYMBOL(arch_spin_relax);

97
int arch_spin_trylock_retry(arch_spinlock_t *lp)
98
{
99
	int count;
100

101
	for (count = spin_retry; count > 0; count--) {
102
		if (arch_spin_is_locked(lp))
103
			continue;
104
		if (arch_spin_trylock_once(lp))
105 106 107 108
			return 1;
	}
	return 0;
}
109
EXPORT_SYMBOL(arch_spin_trylock_retry);
110

111
void _raw_read_lock_wait(arch_rwlock_t *rw)
112 113 114 115 116 117
{
	unsigned int old;
	int count = spin_retry;

	while (1) {
		if (count-- <= 0) {
M
Martin Schwidefsky 已提交
118
			smp_yield();
119 120
			count = spin_retry;
		}
121
		if (!arch_read_can_lock(rw))
122
			continue;
123
		old = rw->lock & 0x7fffffffU;
124
		if (_raw_compare_and_swap(&rw->lock, old, old + 1))
125 126 127 128 129
			return;
	}
}
EXPORT_SYMBOL(_raw_read_lock_wait);

130
void _raw_read_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
131 132 133 134 135 136 137
{
	unsigned int old;
	int count = spin_retry;

	local_irq_restore(flags);
	while (1) {
		if (count-- <= 0) {
M
Martin Schwidefsky 已提交
138
			smp_yield();
139 140
			count = spin_retry;
		}
141
		if (!arch_read_can_lock(rw))
142 143 144
			continue;
		old = rw->lock & 0x7fffffffU;
		local_irq_disable();
145
		if (_raw_compare_and_swap(&rw->lock, old, old + 1))
146 147 148 149 150
			return;
	}
}
EXPORT_SYMBOL(_raw_read_lock_wait_flags);

151
int _raw_read_trylock_retry(arch_rwlock_t *rw)
152 153 154 155 156
{
	unsigned int old;
	int count = spin_retry;

	while (count-- > 0) {
157
		if (!arch_read_can_lock(rw))
158
			continue;
159
		old = rw->lock & 0x7fffffffU;
160
		if (_raw_compare_and_swap(&rw->lock, old, old + 1))
161 162 163 164 165 166
			return 1;
	}
	return 0;
}
EXPORT_SYMBOL(_raw_read_trylock_retry);

167
void _raw_write_lock_wait(arch_rwlock_t *rw)
168 169 170 171 172
{
	int count = spin_retry;

	while (1) {
		if (count-- <= 0) {
M
Martin Schwidefsky 已提交
173
			smp_yield();
174 175
			count = spin_retry;
		}
176
		if (!arch_write_can_lock(rw))
177
			continue;
178
		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
179 180 181 182 183
			return;
	}
}
EXPORT_SYMBOL(_raw_write_lock_wait);

184
void _raw_write_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
185 186 187 188 189 190
{
	int count = spin_retry;

	local_irq_restore(flags);
	while (1) {
		if (count-- <= 0) {
M
Martin Schwidefsky 已提交
191
			smp_yield();
192 193
			count = spin_retry;
		}
194
		if (!arch_write_can_lock(rw))
195 196
			continue;
		local_irq_disable();
197
		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
198 199 200 201 202
			return;
	}
}
EXPORT_SYMBOL(_raw_write_lock_wait_flags);

203
int _raw_write_trylock_retry(arch_rwlock_t *rw)
204 205 206 207
{
	int count = spin_retry;

	while (count-- > 0) {
208
		if (!arch_write_can_lock(rw))
209
			continue;
210
		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
211 212 213 214 215
			return 1;
	}
	return 0;
}
EXPORT_SYMBOL(_raw_write_trylock_retry);