After this has been brought up many times before, rename src/arch/i386 to
[coreboot.git] / src / arch / x86 / include / arch / smp / spinlock.h
1 #ifndef ARCH_SMP_SPINLOCK_H
2 #define ARCH_SMP_SPINLOCK_H
3
4 /*
5  * Your basic SMP spinlocks, allowing only a single CPU anywhere
6  */
7
8 typedef struct {
9         volatile unsigned int lock;
10 } spinlock_t;
11
12
13 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 }
14 #define DECLARE_SPIN_LOCK(x) static spinlock_t x = SPIN_LOCK_UNLOCKED;
15
16 /*
17  * Simple spin lock operations.  There are two variants, one clears IRQ's
18  * on the local processor, one does not.
19  *
20  * We make no fairness assumptions. They have a cost.
21  */
22 #define barrier() __asm__ __volatile__("": : :"memory")
23 #define spin_is_locked(x)       (*(volatile char *)(&(x)->lock) <= 0)
24 #define spin_unlock_wait(x)     do { barrier(); } while(spin_is_locked(x))
25
26 #define spin_lock_string \
27         "\n1:\t" \
28         "lock ; decb %0\n\t" \
29         "js 2f\n" \
30         ".section .text.lock,\"ax\"\n" \
31         "2:\t" \
32         "cmpb $0,%0\n\t" \
33         "rep;nop\n\t" \
34         "jle 2b\n\t" \
35         "jmp 1b\n" \
36         ".previous"
37
38 /*
39  * This works. Despite all the confusion.
40  */
41 #define spin_unlock_string \
42         "movb $1,%0"
43
44 static inline __attribute__((always_inline)) void spin_lock(spinlock_t *lock)
45 {
46         __asm__ __volatile__(
47                 spin_lock_string
48                 :"=m" (lock->lock) : : "memory");
49 }
50
51 static inline __attribute__((always_inline)) void spin_unlock(spinlock_t *lock)
52 {
53         __asm__ __volatile__(
54                 spin_unlock_string
55                 :"=m" (lock->lock) : : "memory");
56 }
57
58 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
59 static inline __attribute__((always_inline)) void cpu_relax(void)
60 {
61         __asm__ __volatile__("rep;nop": : :"memory");
62 }
63
64 #endif /* ARCH_SMP_SPINLOCK_H */