ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / spinlock.h
1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
3
4 #if __LINUX_ARM_ARCH__ < 6
5 #error SMP not supported on pre-ARMv6 CPUs
6 #endif
7
8 /*
9  * ARMv6 Spin-locking.
10  *
11  * We (exclusively) read the old value, and decrement it.  If it
12  * hits zero, we may have won the lock, so we try (exclusively)
13  * storing it.
14  *
15  * Unlocked value: 0
16  * Locked value: 1
17  */
18 typedef struct {
19         volatile unsigned int lock;
20 } spinlock_t;
21
22 #define SPIN_LOCK_UNLOCKED      (spinlock_t) { 0 }
23
24 #define spin_lock_init(x)       do { *(x) = SPIN_LOCK_UNLOCKED; } while (0)
25 #define spin_is_locked(x)       ((x)->lock != 0)
26 #define spin_unlock_wait(x)     do { barrier(); } while (spin_is_locked(x))
27
28 static inline void _raw_spin_lock(spinlock_t *lock)
29 {
30         unsigned long tmp;
31
32         __asm__ __volatile__(
33 "1:     ldrex   %0, [%1]\n"
34 "       teq     %0, #0\n"
35 "       strexeq %0, %2, [%1]\n"
36 "       teqeq   %0, #0\n"
37 "       bne     1b"
38         : "=&r" (tmp)
39         : "r" (&lock->lock), "r" (1)
40         : "cc", "memory");
41 }
42
43 static inline int _raw_spin_trylock(spinlock_t *lock)
44 {
45         unsigned long tmp;
46
47         __asm__ __volatile__(
48 "       ldrex   %0, [%1]\n"
49 "       teq     %0, #0\n"
50 "       strexeq %0, %2, [%1]"
51         : "=&r" (tmp)
52         : "r" (&lock->lock), "r" (1)
53         : "cc", "memory");
54
55         return tmp == 0;
56 }
57
58 static inline void _raw_spin_unlock(spinlock_t *lock)
59 {
60         __asm__ __volatile__(
61 "       str     %1, [%0]"
62         :
63         : "r" (&lock->lock), "r" (0)
64         : "cc", "memory");
65 }
66
67 /*
68  * RWLOCKS
69  */
70 typedef struct {
71         volatile unsigned int lock;
72 } rwlock_t;
73
74 #define RW_LOCK_UNLOCKED        (rwlock_t) { 0 }
75 #define rwlock_init(x)          do { *(x) + RW_LOCK_UNLOCKED; } while (0)
76
77 /*
78  * Write locks are easy - we just set bit 31.  When unlocking, we can
79  * just write zero since the lock is exclusively held.
80  */
81 static inline void _raw_write_lock(rwlock_t *rw)
82 {
83         unsigned long tmp;
84
85         __asm__ __volatile__(
86 "1:     ldrex   %0, [%1]\n"
87 "       teq     %0, #0\n"
88 "       strexeq %0, %2, [%1]\n"
89 "       teq     %0, #0\n"
90 "       bne     1b"
91         : "=r" (tmp)
92         : "r" (&rw->lock), "r" (0x80000000)
93         : "cc", "memory");
94 }
95
96 static inline void _raw_write_unlock(rwlock_t *rw)
97 {
98         __asm__ __volatile__(
99         "str    %1, [%0]"
100         :
101         : "r" (&rw->lock), "r" (0)
102         : "cc", "memory");
103 }
104
105 /*
106  * Read locks are a bit more hairy:
107  *  - Exclusively load the lock value.
108  *  - Increment it.
109  *  - Store new lock value if positive, and we still own this location.
110  *    If the value is negative, we've already failed.
111  *  - If we failed to store the value, we want a negative result.
112  *  - If we failed, try again.
113  * Unlocking is similarly hairy.  We may have multiple read locks
114  * currently active.  However, we know we won't have any write
115  * locks.
116  */
117 static inline void _raw_read_lock(rwlock_t *rw)
118 {
119         unsigned long tmp, tmp2;
120
121         __asm__ __volatile__(
122 "1:     ldrex   %0, [%2]\n"
123 "       adds    %0, %0, #1\n"
124 "       strexpl %1, %0, [%2]\n"
125 "       rsbpls  %0, %1, #0\n"
126 "       bmi     1b"
127         : "=&r" (tmp), "=&r" (tmp2)
128         : "r" (&rw->lock)
129         : "cc", "memory");
130 }
131
132 static inline void _raw_read_unlock(rwlock_t *rw)
133 {
134         __asm__ __volatile__(
135 "1:     ldrex   %0, [%2]\n"
136 "       sub     %0, %0, #1\n"
137 "       strex   %1, %0, [%2]\n"
138 "       teq     %1, #0\n"
139 "       bne     1b"
140         : "=&r" (tmp), "=&r" (tmp2)
141         : "r" (&rw->lock)
142         : "cc", "memory");
143 }
144
145 static inline int _raw_write_trylock(rwlock_t *rw)
146 {
147         unsigned long tmp;
148
149         __asm__ __volatile__(
150 "1:     ldrex   %0, [%1]\n"
151 "       teq     %0, #0\n"
152 "       strexeq %0, %2, [%1]"
153         : "=r" (tmp)
154         : "r" (&rw->lock), "r" (0x80000000)
155         : "cc", "memory");
156
157         return tmp == 0;
158 }
159
160 #endif /* __ASM_SPINLOCK_H */