[PATCH] ARM SMP: Fix ARMv6 spinlock and semaphore implementations
[deliverable/linux.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 #ifdef CONFIG_PREEMPT
21 unsigned int break_lock;
22 #endif
23 } spinlock_t;
24
25 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
26
27 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while (0)
28 #define spin_is_locked(x) ((x)->lock != 0)
29 #define spin_unlock_wait(x) do { barrier(); } while (spin_is_locked(x))
30 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
31
32 static inline void _raw_spin_lock(spinlock_t *lock)
33 {
34 unsigned long tmp;
35
36 __asm__ __volatile__(
37 "1: ldrex %0, [%1]\n"
38 " teq %0, #0\n"
39 " strexeq %0, %2, [%1]\n"
40 " teqeq %0, #0\n"
41 " bne 1b"
42 : "=&r" (tmp)
43 : "r" (&lock->lock), "r" (1)
44 : "cc", "memory");
45 }
46
47 static inline int _raw_spin_trylock(spinlock_t *lock)
48 {
49 unsigned long tmp;
50
51 __asm__ __volatile__(
52 " ldrex %0, [%1]\n"
53 " teq %0, #0\n"
54 " strexeq %0, %2, [%1]"
55 : "=&r" (tmp)
56 : "r" (&lock->lock), "r" (1)
57 : "cc", "memory");
58
59 return tmp == 0;
60 }
61
62 static inline void _raw_spin_unlock(spinlock_t *lock)
63 {
64 __asm__ __volatile__(
65 " str %1, [%0]"
66 :
67 : "r" (&lock->lock), "r" (0)
68 : "cc", "memory");
69 }
70
71 /*
72 * RWLOCKS
73 */
74 typedef struct {
75 volatile unsigned int lock;
76 #ifdef CONFIG_PREEMPT
77 unsigned int break_lock;
78 #endif
79 } rwlock_t;
80
81 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
82 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while (0)
83 #define rwlock_is_locked(x) (*((volatile unsigned int *)(x)) != 0)
84
85 /*
86 * Write locks are easy - we just set bit 31. When unlocking, we can
87 * just write zero since the lock is exclusively held.
88 */
89 static inline void _raw_write_lock(rwlock_t *rw)
90 {
91 unsigned long tmp;
92
93 __asm__ __volatile__(
94 "1: ldrex %0, [%1]\n"
95 " teq %0, #0\n"
96 " strexeq %0, %2, [%1]\n"
97 " teq %0, #0\n"
98 " bne 1b"
99 : "=&r" (tmp)
100 : "r" (&rw->lock), "r" (0x80000000)
101 : "cc", "memory");
102 }
103
104 static inline int _raw_write_trylock(rwlock_t *rw)
105 {
106 unsigned long tmp;
107
108 __asm__ __volatile__(
109 "1: ldrex %0, [%1]\n"
110 " teq %0, #0\n"
111 " strexeq %0, %2, [%1]"
112 : "=&r" (tmp)
113 : "r" (&rw->lock), "r" (0x80000000)
114 : "cc", "memory");
115
116 return tmp == 0;
117 }
118
119 static inline void _raw_write_unlock(rwlock_t *rw)
120 {
121 __asm__ __volatile__(
122 "str %1, [%0]"
123 :
124 : "r" (&rw->lock), "r" (0)
125 : "cc", "memory");
126 }
127
128 /*
129 * Read locks are a bit more hairy:
130 * - Exclusively load the lock value.
131 * - Increment it.
132 * - Store new lock value if positive, and we still own this location.
133 * If the value is negative, we've already failed.
134 * - If we failed to store the value, we want a negative result.
135 * - If we failed, try again.
136 * Unlocking is similarly hairy. We may have multiple read locks
137 * currently active. However, we know we won't have any write
138 * locks.
139 */
140 static inline void _raw_read_lock(rwlock_t *rw)
141 {
142 unsigned long tmp, tmp2;
143
144 __asm__ __volatile__(
145 "1: ldrex %0, [%2]\n"
146 " adds %0, %0, #1\n"
147 " strexpl %1, %0, [%2]\n"
148 " rsbpls %0, %1, #0\n"
149 " bmi 1b"
150 : "=&r" (tmp), "=&r" (tmp2)
151 : "r" (&rw->lock)
152 : "cc", "memory");
153 }
154
155 static inline void _raw_read_unlock(rwlock_t *rw)
156 {
157 unsigned long tmp, tmp2;
158
159 __asm__ __volatile__(
160 "1: ldrex %0, [%2]\n"
161 " sub %0, %0, #1\n"
162 " strex %1, %0, [%2]\n"
163 " teq %1, #0\n"
164 " bne 1b"
165 : "=&r" (tmp), "=&r" (tmp2)
166 : "r" (&rw->lock)
167 : "cc", "memory");
168 }
169
170 #define _raw_read_trylock(lock) generic_raw_read_trylock(lock)
171
172 #endif /* __ASM_SPINLOCK_H */
This page took 0.037743 seconds and 5 git commands to generate.