]> Pileus Git - ~andy/linux/blob - arch/arm/include/asm/spinlock.h
ARM: smp_on_up: move inline asm ALT_SMP patching macro out of spinlock.h
[~andy/linux] / arch / arm / include / asm / 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 #include <asm/processor.h>
9
10 /*
11  * sev and wfe are ARMv6K extensions.  Uniprocessor ARMv6 may not have the K
12  * extensions, so when running on UP, we have to patch these instructions away.
13  */
14 #ifdef CONFIG_THUMB2_KERNEL
15 /*
16  * For Thumb-2, special care is needed to ensure that the conditional WFE
17  * instruction really does assemble to exactly 4 bytes (as required by
18  * the SMP_ON_UP fixup code).   By itself "wfene" might cause the
19  * assembler to insert a extra (16-bit) IT instruction, depending on the
20  * presence or absence of neighbouring conditional instructions.
21  *
22  * To avoid this unpredictableness, an approprite IT is inserted explicitly:
23  * the assembler won't change IT instructions which are explicitly present
24  * in the input.
25  */
26 #define WFE(cond)       __ALT_SMP_ASM(          \
27         "it " cond "\n\t"                       \
28         "wfe" cond ".n",                        \
29                                                 \
30         "nop.w"                                 \
31 )
32 #else
33 #define WFE(cond)       __ALT_SMP_ASM("wfe" cond, "nop")
34 #endif
35
36 #define SEV             __ALT_SMP_ASM(WASM(sev), WASM(nop))
37
38 static inline void dsb_sev(void)
39 {
40 #if __LINUX_ARM_ARCH__ >= 7
41         __asm__ __volatile__ (
42                 "dsb ishst\n"
43                 SEV
44         );
45 #else
46         __asm__ __volatile__ (
47                 "mcr p15, 0, %0, c7, c10, 4\n"
48                 SEV
49                 : : "r" (0)
50         );
51 #endif
52 }
53
54 /*
55  * ARMv6 ticket-based spin-locking.
56  *
57  * A memory barrier is required after we get a lock, and before we
58  * release it, because V6 CPUs are assumed to have weakly ordered
59  * memory.
60  */
61
62 #define arch_spin_unlock_wait(lock) \
63         do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
64
65 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
66
67 static inline void arch_spin_lock(arch_spinlock_t *lock)
68 {
69         unsigned long tmp;
70         u32 newval;
71         arch_spinlock_t lockval;
72
73         __asm__ __volatile__(
74 "1:     ldrex   %0, [%3]\n"
75 "       add     %1, %0, %4\n"
76 "       strex   %2, %1, [%3]\n"
77 "       teq     %2, #0\n"
78 "       bne     1b"
79         : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
80         : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
81         : "cc");
82
83         while (lockval.tickets.next != lockval.tickets.owner) {
84                 wfe();
85                 lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
86         }
87
88         smp_mb();
89 }
90
91 static inline int arch_spin_trylock(arch_spinlock_t *lock)
92 {
93         unsigned long contended, res;
94         u32 slock;
95
96         do {
97                 __asm__ __volatile__(
98                 "       ldrex   %0, [%3]\n"
99                 "       mov     %2, #0\n"
100                 "       subs    %1, %0, %0, ror #16\n"
101                 "       addeq   %0, %0, %4\n"
102                 "       strexeq %2, %0, [%3]"
103                 : "=&r" (slock), "=&r" (contended), "=&r" (res)
104                 : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
105                 : "cc");
106         } while (res);
107
108         if (!contended) {
109                 smp_mb();
110                 return 1;
111         } else {
112                 return 0;
113         }
114 }
115
116 static inline void arch_spin_unlock(arch_spinlock_t *lock)
117 {
118         smp_mb();
119         lock->tickets.owner++;
120         dsb_sev();
121 }
122
123 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
124 {
125         struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
126         return tickets.owner != tickets.next;
127 }
128
129 static inline int arch_spin_is_contended(arch_spinlock_t *lock)
130 {
131         struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
132         return (tickets.next - tickets.owner) > 1;
133 }
134 #define arch_spin_is_contended  arch_spin_is_contended
135
136 /*
137  * RWLOCKS
138  *
139  *
140  * Write locks are easy - we just set bit 31.  When unlocking, we can
141  * just write zero since the lock is exclusively held.
142  */
143
144 static inline void arch_write_lock(arch_rwlock_t *rw)
145 {
146         unsigned long tmp;
147
148         __asm__ __volatile__(
149 "1:     ldrex   %0, [%1]\n"
150 "       teq     %0, #0\n"
151         WFE("ne")
152 "       strexeq %0, %2, [%1]\n"
153 "       teq     %0, #0\n"
154 "       bne     1b"
155         : "=&r" (tmp)
156         : "r" (&rw->lock), "r" (0x80000000)
157         : "cc");
158
159         smp_mb();
160 }
161
162 static inline int arch_write_trylock(arch_rwlock_t *rw)
163 {
164         unsigned long contended, res;
165
166         do {
167                 __asm__ __volatile__(
168                 "       ldrex   %0, [%2]\n"
169                 "       mov     %1, #0\n"
170                 "       teq     %0, #0\n"
171                 "       strexeq %1, %3, [%2]"
172                 : "=&r" (contended), "=&r" (res)
173                 : "r" (&rw->lock), "r" (0x80000000)
174                 : "cc");
175         } while (res);
176
177         if (!contended) {
178                 smp_mb();
179                 return 1;
180         } else {
181                 return 0;
182         }
183 }
184
185 static inline void arch_write_unlock(arch_rwlock_t *rw)
186 {
187         smp_mb();
188
189         __asm__ __volatile__(
190         "str    %1, [%0]\n"
191         :
192         : "r" (&rw->lock), "r" (0)
193         : "cc");
194
195         dsb_sev();
196 }
197
198 /* write_can_lock - would write_trylock() succeed? */
199 #define arch_write_can_lock(x)          ((x)->lock == 0)
200
201 /*
202  * Read locks are a bit more hairy:
203  *  - Exclusively load the lock value.
204  *  - Increment it.
205  *  - Store new lock value if positive, and we still own this location.
206  *    If the value is negative, we've already failed.
207  *  - If we failed to store the value, we want a negative result.
208  *  - If we failed, try again.
209  * Unlocking is similarly hairy.  We may have multiple read locks
210  * currently active.  However, we know we won't have any write
211  * locks.
212  */
213 static inline void arch_read_lock(arch_rwlock_t *rw)
214 {
215         unsigned long tmp, tmp2;
216
217         __asm__ __volatile__(
218 "1:     ldrex   %0, [%2]\n"
219 "       adds    %0, %0, #1\n"
220 "       strexpl %1, %0, [%2]\n"
221         WFE("mi")
222 "       rsbpls  %0, %1, #0\n"
223 "       bmi     1b"
224         : "=&r" (tmp), "=&r" (tmp2)
225         : "r" (&rw->lock)
226         : "cc");
227
228         smp_mb();
229 }
230
231 static inline void arch_read_unlock(arch_rwlock_t *rw)
232 {
233         unsigned long tmp, tmp2;
234
235         smp_mb();
236
237         __asm__ __volatile__(
238 "1:     ldrex   %0, [%2]\n"
239 "       sub     %0, %0, #1\n"
240 "       strex   %1, %0, [%2]\n"
241 "       teq     %1, #0\n"
242 "       bne     1b"
243         : "=&r" (tmp), "=&r" (tmp2)
244         : "r" (&rw->lock)
245         : "cc");
246
247         if (tmp == 0)
248                 dsb_sev();
249 }
250
251 static inline int arch_read_trylock(arch_rwlock_t *rw)
252 {
253         unsigned long contended, res;
254
255         do {
256                 __asm__ __volatile__(
257                 "       ldrex   %0, [%2]\n"
258                 "       mov     %1, #0\n"
259                 "       adds    %0, %0, #1\n"
260                 "       strexpl %1, %0, [%2]"
261                 : "=&r" (contended), "=&r" (res)
262                 : "r" (&rw->lock)
263                 : "cc");
264         } while (res);
265
266         /* If the lock is negative, then it is already held for write. */
267         if (contended < 0x80000000) {
268                 smp_mb();
269                 return 1;
270         } else {
271                 return 0;
272         }
273 }
274
275 /* read_can_lock - would read_trylock() succeed? */
276 #define arch_read_can_lock(x)           ((x)->lock < 0x80000000)
277
278 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
279 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
280
281 #define arch_spin_relax(lock)   cpu_relax()
282 #define arch_read_relax(lock)   cpu_relax()
283 #define arch_write_relax(lock)  cpu_relax()
284
285 #endif /* __ASM_SPINLOCK_H */