spinlock.h 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
#ifndef __ASM_SPINLOCK_H
#define __ASM_SPINLOCK_H

#if __LINUX_ARM_ARCH__ < 6
#error SMP not supported on pre-ARMv6 CPUs
#endif

8 9
#include <asm/processor.h>

10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * sev and wfe are ARMv6K extensions.  Uniprocessor ARMv6 may not have the K
 * extensions, so when running on UP, we have to patch these instructions away.
 */
#define ALT_SMP(smp, up)					\
	"9998:	" smp "\n"					\
	"	.pushsection \".alt.smp.init\", \"a\"\n"	\
	"	.long	9998b\n"				\
	"	" up "\n"					\
	"	.popsection\n"

#ifdef CONFIG_THUMB2_KERNEL
#define SEV		ALT_SMP("sev.w", "nop.w")
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * For Thumb-2, special care is needed to ensure that the conditional WFE
 * instruction really does assemble to exactly 4 bytes (as required by
 * the SMP_ON_UP fixup code).   By itself "wfene" might cause the
 * assembler to insert a extra (16-bit) IT instruction, depending on the
 * presence or absence of neighbouring conditional instructions.
 *
 * To avoid this unpredictableness, an approprite IT is inserted explicitly:
 * the assembler won't change IT instructions which are explicitly present
 * in the input.
 */
#define WFE(cond)	ALT_SMP(		\
	"it " cond "\n\t"			\
	"wfe" cond ".n",			\
						\
	"nop.w"					\
)
40 41 42 43 44
#else
#define SEV		ALT_SMP("sev", "nop")
#define WFE(cond)	ALT_SMP("wfe" cond, "nop")
#endif

45 46 47 48 49
static inline void dsb_sev(void)
{
#if __LINUX_ARM_ARCH__ >= 7
	__asm__ __volatile__ (
		"dsb\n"
50
		SEV
51
	);
52
#else
53 54
	__asm__ __volatile__ (
		"mcr p15, 0, %0, c7, c10, 4\n"
55
		SEV
56 57 58 59 60
		: : "r" (0)
	);
#endif
}

L
Linus Torvalds 已提交
61
/*
62
 * ARMv6 ticket-based spin-locking.
L
Linus Torvalds 已提交
63
 *
64 65 66
 * A memory barrier is required after we get a lock, and before we
 * release it, because V6 CPUs are assumed to have weakly ordered
 * memory.
L
Linus Torvalds 已提交
67 68
 */

69 70
#define arch_spin_unlock_wait(lock) \
	do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
L
Linus Torvalds 已提交
71

72
#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
L
Linus Torvalds 已提交
73

74
static inline void arch_spin_lock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
75 76
{
	unsigned long tmp;
77 78
	u32 newval;
	arch_spinlock_t lockval;
L
Linus Torvalds 已提交
79 80

	__asm__ __volatile__(
81 82 83 84
"1:	ldrex	%0, [%3]\n"
"	add	%1, %0, %4\n"
"	strex	%2, %1, [%3]\n"
"	teq	%2, #0\n"
L
Linus Torvalds 已提交
85
"	bne	1b"
86 87
	: "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
	: "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
88 89
	: "cc");

90 91 92 93 94
	while (lockval.tickets.next != lockval.tickets.owner) {
		wfe();
		lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
	}

95
	smp_mb();
L
Linus Torvalds 已提交
96 97
}

98
static inline int arch_spin_trylock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
99 100
{
	unsigned long tmp;
101
	u32 slock;
L
Linus Torvalds 已提交
102 103

	__asm__ __volatile__(
104 105 106 107 108 109
"	ldrex	%0, [%2]\n"
"	subs	%1, %0, %0, ror #16\n"
"	addeq	%0, %0, %3\n"
"	strexeq	%1, %0, [%2]"
	: "=&r" (slock), "=&r" (tmp)
	: "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
110 111 112 113 114 115 116 117
	: "cc");

	if (tmp == 0) {
		smp_mb();
		return 1;
	} else {
		return 0;
	}
L
Linus Torvalds 已提交
118 119
}

120
static inline void arch_spin_unlock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
121
{
122 123 124
	unsigned long tmp;
	u32 slock;

125 126
	smp_mb();

L
Linus Torvalds 已提交
127
	__asm__ __volatile__(
128 129 130 131 132 133 134 135
"	mov	%1, #1\n"
"1:	ldrex	%0, [%2]\n"
"	uadd16	%0, %0, %1\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (slock), "=&r" (tmp)
	: "r" (&lock->slock)
136
	: "cc");
137 138

	dsb_sev();
L
Linus Torvalds 已提交
139 140
}

141 142 143 144 145 146 147 148 149 150 151 152 153
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
{
	struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
	return tickets.owner != tickets.next;
}

static inline int arch_spin_is_contended(arch_spinlock_t *lock)
{
	struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
	return (tickets.next - tickets.owner) > 1;
}
#define arch_spin_is_contended	arch_spin_is_contended

L
Linus Torvalds 已提交
154 155
/*
 * RWLOCKS
I
Ingo Molnar 已提交
156 157
 *
 *
L
Linus Torvalds 已提交
158 159 160
 * Write locks are easy - we just set bit 31.  When unlocking, we can
 * just write zero since the lock is exclusively held.
 */
I
Ingo Molnar 已提交
161

162
static inline void arch_write_lock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
163 164 165 166 167 168
{
	unsigned long tmp;

	__asm__ __volatile__(
"1:	ldrex	%0, [%1]\n"
"	teq	%0, #0\n"
169
	WFE("ne")
L
Linus Torvalds 已提交
170 171 172 173 174
"	strexeq	%0, %2, [%1]\n"
"	teq	%0, #0\n"
"	bne	1b"
	: "=&r" (tmp)
	: "r" (&rw->lock), "r" (0x80000000)
175 176 177
	: "cc");

	smp_mb();
L
Linus Torvalds 已提交
178 179
}

180
static inline int arch_write_trylock(arch_rwlock_t *rw)
181 182 183 184
{
	unsigned long tmp;

	__asm__ __volatile__(
185
"	ldrex	%0, [%1]\n"
186 187 188 189
"	teq	%0, #0\n"
"	strexeq	%0, %2, [%1]"
	: "=&r" (tmp)
	: "r" (&rw->lock), "r" (0x80000000)
190 191 192 193 194 195 196 197
	: "cc");

	if (tmp == 0) {
		smp_mb();
		return 1;
	} else {
		return 0;
	}
198 199
}

200
static inline void arch_write_unlock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
201
{
202 203
	smp_mb();

L
Linus Torvalds 已提交
204
	__asm__ __volatile__(
205
	"str	%1, [%0]\n"
L
Linus Torvalds 已提交
206 207
	:
	: "r" (&rw->lock), "r" (0)
208
	: "cc");
209 210

	dsb_sev();
L
Linus Torvalds 已提交
211 212
}

213
/* write_can_lock - would write_trylock() succeed? */
214
#define arch_write_can_lock(x)		((x)->lock == 0)
215

L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227
/*
 * Read locks are a bit more hairy:
 *  - Exclusively load the lock value.
 *  - Increment it.
 *  - Store new lock value if positive, and we still own this location.
 *    If the value is negative, we've already failed.
 *  - If we failed to store the value, we want a negative result.
 *  - If we failed, try again.
 * Unlocking is similarly hairy.  We may have multiple read locks
 * currently active.  However, we know we won't have any write
 * locks.
 */
228
static inline void arch_read_lock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
229 230 231 232 233 234 235
{
	unsigned long tmp, tmp2;

	__asm__ __volatile__(
"1:	ldrex	%0, [%2]\n"
"	adds	%0, %0, #1\n"
"	strexpl	%1, %0, [%2]\n"
236
	WFE("mi")
L
Linus Torvalds 已提交
237 238 239 240
"	rsbpls	%0, %1, #0\n"
"	bmi	1b"
	: "=&r" (tmp), "=&r" (tmp2)
	: "r" (&rw->lock)
241 242 243
	: "cc");

	smp_mb();
L
Linus Torvalds 已提交
244 245
}

246
static inline void arch_read_unlock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
247
{
248 249
	unsigned long tmp, tmp2;

250 251
	smp_mb();

L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259
	__asm__ __volatile__(
"1:	ldrex	%0, [%2]\n"
"	sub	%0, %0, #1\n"
"	strex	%1, %0, [%2]\n"
"	teq	%1, #0\n"
"	bne	1b"
	: "=&r" (tmp), "=&r" (tmp2)
	: "r" (&rw->lock)
260
	: "cc");
261 262 263

	if (tmp == 0)
		dsb_sev();
L
Linus Torvalds 已提交
264 265
}

266
static inline int arch_read_trylock(arch_rwlock_t *rw)
267
{
268
	unsigned long tmp, tmp2 = 1;
269 270

	__asm__ __volatile__(
271
"	ldrex	%0, [%2]\n"
272 273 274 275 276 277 278 279 280
"	adds	%0, %0, #1\n"
"	strexpl	%1, %0, [%2]\n"
	: "=&r" (tmp), "+r" (tmp2)
	: "r" (&rw->lock)
	: "cc");

	smp_mb();
	return tmp2 == 0;
}
L
Linus Torvalds 已提交
281

282
/* read_can_lock - would read_trylock() succeed? */
283
#define arch_read_can_lock(x)		((x)->lock < 0x80000000)
284

285 286
#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
287

288 289 290
#define arch_spin_relax(lock)	cpu_relax()
#define arch_read_relax(lock)	cpu_relax()
#define arch_write_relax(lock)	cpu_relax()
291

L
Linus Torvalds 已提交
292
#endif /* __ASM_SPINLOCK_H */