xref: /kvm-unit-tests/lib/arm/spinlock.c (revision b1a6fd7df55090ea55c50b2145033566b1a52427)
1 /*
2  * ARM spinlock implementation
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Library General Public License version 2.
6  */
7 #include <libcflat.h>
8 #include <asm/spinlock.h>
9 #include <asm/barrier.h>
10 #include <asm/mmu.h>
11 
spin_lock(struct spinlock * lock)12 void spin_lock(struct spinlock *lock)
13 {
14 	u32 val, fail;
15 
16 	if (!mmu_enabled()) {
17 		lock->v = 1;
18 		smp_mb();
19 		return;
20 	}
21 
22 	do {
23 		asm volatile(
24 		"1:	ldrex	%0, [%2]\n"
25 		"	teq	%0, #0\n"
26 		"	bne	1b\n"
27 		"	mov	%0, #1\n"
28 		"	strex	%1, %0, [%2]\n"
29 		: "=&r" (val), "=&r" (fail)
30 		: "r" (&lock->v)
31 		: "cc" );
32 	} while (fail);
33 
34 	smp_mb();
35 }
36 
spin_unlock(struct spinlock * lock)37 void spin_unlock(struct spinlock *lock)
38 {
39 	smp_mb();
40 	lock->v = 0;
41 }
42