]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/include/asm/spinlock.h
ARM: locks: prefetch the destination word for write prior to strex
[karo-tx-linux.git] / 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 <linux/prefetch.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         prefetchw(&lock->slock);
74         __asm__ __volatile__(
75 "1:     ldrex   %0, [%3]\n"
76 "       add     %1, %0, %4\n"
77 "       strex   %2, %1, [%3]\n"
78 "       teq     %2, #0\n"
79 "       bne     1b"
80         : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
81         : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
82         : "cc");
83
84         while (lockval.tickets.next != lockval.tickets.owner) {
85                 wfe();
86                 lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
87         }
88
89         smp_mb();
90 }
91
92 static inline int arch_spin_trylock(arch_spinlock_t *lock)
93 {
94         unsigned long contended, res;
95         u32 slock;
96
97         prefetchw(&lock->slock);
98         do {
99                 __asm__ __volatile__(
100                 "       ldrex   %0, [%3]\n"
101                 "       mov     %2, #0\n"
102                 "       subs    %1, %0, %0, ror #16\n"
103                 "       addeq   %0, %0, %4\n"
104                 "       strexeq %2, %0, [%3]"
105                 : "=&r" (slock), "=&r" (contended), "=&r" (res)
106                 : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
107                 : "cc");
108         } while (res);
109
110         if (!contended) {
111                 smp_mb();
112                 return 1;
113         } else {
114                 return 0;
115         }
116 }
117
118 static inline void arch_spin_unlock(arch_spinlock_t *lock)
119 {
120         smp_mb();
121         lock->tickets.owner++;
122         dsb_sev();
123 }
124
125 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
126 {
127         struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
128         return tickets.owner != tickets.next;
129 }
130
131 static inline int arch_spin_is_contended(arch_spinlock_t *lock)
132 {
133         struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
134         return (tickets.next - tickets.owner) > 1;
135 }
136 #define arch_spin_is_contended  arch_spin_is_contended
137
138 /*
139  * RWLOCKS
140  *
141  *
142  * Write locks are easy - we just set bit 31.  When unlocking, we can
143  * just write zero since the lock is exclusively held.
144  */
145
146 static inline void arch_write_lock(arch_rwlock_t *rw)
147 {
148         unsigned long tmp;
149
150         prefetchw(&rw->lock);
151         __asm__ __volatile__(
152 "1:     ldrex   %0, [%1]\n"
153 "       teq     %0, #0\n"
154         WFE("ne")
155 "       strexeq %0, %2, [%1]\n"
156 "       teq     %0, #0\n"
157 "       bne     1b"
158         : "=&r" (tmp)
159         : "r" (&rw->lock), "r" (0x80000000)
160         : "cc");
161
162         smp_mb();
163 }
164
165 static inline int arch_write_trylock(arch_rwlock_t *rw)
166 {
167         unsigned long contended, res;
168
169         prefetchw(&rw->lock);
170         do {
171                 __asm__ __volatile__(
172                 "       ldrex   %0, [%2]\n"
173                 "       mov     %1, #0\n"
174                 "       teq     %0, #0\n"
175                 "       strexeq %1, %3, [%2]"
176                 : "=&r" (contended), "=&r" (res)
177                 : "r" (&rw->lock), "r" (0x80000000)
178                 : "cc");
179         } while (res);
180
181         if (!contended) {
182                 smp_mb();
183                 return 1;
184         } else {
185                 return 0;
186         }
187 }
188
189 static inline void arch_write_unlock(arch_rwlock_t *rw)
190 {
191         smp_mb();
192
193         __asm__ __volatile__(
194         "str    %1, [%0]\n"
195         :
196         : "r" (&rw->lock), "r" (0)
197         : "cc");
198
199         dsb_sev();
200 }
201
202 /* write_can_lock - would write_trylock() succeed? */
203 #define arch_write_can_lock(x)          (ACCESS_ONCE((x)->lock) == 0)
204
205 /*
206  * Read locks are a bit more hairy:
207  *  - Exclusively load the lock value.
208  *  - Increment it.
209  *  - Store new lock value if positive, and we still own this location.
210  *    If the value is negative, we've already failed.
211  *  - If we failed to store the value, we want a negative result.
212  *  - If we failed, try again.
213  * Unlocking is similarly hairy.  We may have multiple read locks
214  * currently active.  However, we know we won't have any write
215  * locks.
216  */
217 static inline void arch_read_lock(arch_rwlock_t *rw)
218 {
219         unsigned long tmp, tmp2;
220
221         prefetchw(&rw->lock);
222         __asm__ __volatile__(
223 "1:     ldrex   %0, [%2]\n"
224 "       adds    %0, %0, #1\n"
225 "       strexpl %1, %0, [%2]\n"
226         WFE("mi")
227 "       rsbpls  %0, %1, #0\n"
228 "       bmi     1b"
229         : "=&r" (tmp), "=&r" (tmp2)
230         : "r" (&rw->lock)
231         : "cc");
232
233         smp_mb();
234 }
235
236 static inline void arch_read_unlock(arch_rwlock_t *rw)
237 {
238         unsigned long tmp, tmp2;
239
240         smp_mb();
241
242         prefetchw(&rw->lock);
243         __asm__ __volatile__(
244 "1:     ldrex   %0, [%2]\n"
245 "       sub     %0, %0, #1\n"
246 "       strex   %1, %0, [%2]\n"
247 "       teq     %1, #0\n"
248 "       bne     1b"
249         : "=&r" (tmp), "=&r" (tmp2)
250         : "r" (&rw->lock)
251         : "cc");
252
253         if (tmp == 0)
254                 dsb_sev();
255 }
256
257 static inline int arch_read_trylock(arch_rwlock_t *rw)
258 {
259         unsigned long contended, res;
260
261         prefetchw(&rw->lock);
262         do {
263                 __asm__ __volatile__(
264                 "       ldrex   %0, [%2]\n"
265                 "       mov     %1, #0\n"
266                 "       adds    %0, %0, #1\n"
267                 "       strexpl %1, %0, [%2]"
268                 : "=&r" (contended), "=&r" (res)
269                 : "r" (&rw->lock)
270                 : "cc");
271         } while (res);
272
273         /* If the lock is negative, then it is already held for write. */
274         if (contended < 0x80000000) {
275                 smp_mb();
276                 return 1;
277         } else {
278                 return 0;
279         }
280 }
281
282 /* read_can_lock - would read_trylock() succeed? */
283 #define arch_read_can_lock(x)           (ACCESS_ONCE((x)->lock) < 0x80000000)
284
285 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
286 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
287
288 #define arch_spin_relax(lock)   cpu_relax()
289 #define arch_read_relax(lock)   cpu_relax()
290 #define arch_write_relax(lock)  cpu_relax()
291
292 #endif /* __ASM_SPINLOCK_H */