blob: fdd3820edff86ed900147d00436df2b6c2afd08f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
Russell King000d9c72011-01-15 16:22:12 +00008/*
9 * sev and wfe are ARMv6K extensions. Uniprocessor ARMv6 may not have the K
10 * extensions, so when running on UP, we have to patch these instructions away.
11 */
12#define ALT_SMP(smp, up) \
13 "9998: " smp "\n" \
14 " .pushsection \".alt.smp.init\", \"a\"\n" \
15 " .long 9998b\n" \
16 " " up "\n" \
17 " .popsection\n"
18
19#ifdef CONFIG_THUMB2_KERNEL
20#define SEV ALT_SMP("sev.w", "nop.w")
Dave Martin917692f2011-02-09 12:06:59 +010021/*
22 * For Thumb-2, special care is needed to ensure that the conditional WFE
23 * instruction really does assemble to exactly 4 bytes (as required by
24 * the SMP_ON_UP fixup code). By itself "wfene" might cause the
25 * assembler to insert a extra (16-bit) IT instruction, depending on the
26 * presence or absence of neighbouring conditional instructions.
27 *
28 * To avoid this unpredictableness, an approprite IT is inserted explicitly:
29 * the assembler won't change IT instructions which are explicitly present
30 * in the input.
31 */
32#define WFE(cond) ALT_SMP( \
33 "it " cond "\n\t" \
34 "wfe" cond ".n", \
35 \
36 "nop.w" \
37)
Russell King000d9c72011-01-15 16:22:12 +000038#else
39#define SEV ALT_SMP("sev", "nop")
40#define WFE(cond) ALT_SMP("wfe" cond, "nop")
41#endif
42
Rabin Vincentc5113b62010-01-25 19:43:03 +010043static inline void dsb_sev(void)
44{
45#if __LINUX_ARM_ARCH__ >= 7
46 __asm__ __volatile__ (
47 "dsb\n"
Russell King000d9c72011-01-15 16:22:12 +000048 SEV
Rabin Vincentc5113b62010-01-25 19:43:03 +010049 );
Russell King000d9c72011-01-15 16:22:12 +000050#else
Rabin Vincentc5113b62010-01-25 19:43:03 +010051 __asm__ __volatile__ (
52 "mcr p15, 0, %0, c7, c10, 4\n"
Russell King000d9c72011-01-15 16:22:12 +000053 SEV
Rabin Vincentc5113b62010-01-25 19:43:03 +010054 : : "r" (0)
55 );
56#endif
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * ARMv6 Spin-locking.
61 *
Russell King6d9b37a2005-07-26 19:44:26 +010062 * We exclusively read the old value. If it is zero, we may have
63 * won the lock, so we try exclusively storing it. A memory barrier
64 * is required after we get a lock, and before we release it, because
65 * V6 CPUs are assumed to have weakly ordered memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * Unlocked value: 0
68 * Locked value: 1
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010071#define arch_spin_is_locked(x) ((x)->lock != 0)
72#define arch_spin_unlock_wait(lock) \
73 do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010075#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010077static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 unsigned long tmp;
80
81 __asm__ __volatile__(
82"1: ldrex %0, [%1]\n"
83" teq %0, #0\n"
Russell King000d9c72011-01-15 16:22:12 +000084 WFE("ne")
Linus Torvalds1da177e2005-04-16 15:20:36 -070085" strexeq %0, %2, [%1]\n"
86" teqeq %0, #0\n"
87" bne 1b"
88 : "=&r" (tmp)
89 : "r" (&lock->lock), "r" (1)
Russell King6d9b37a2005-07-26 19:44:26 +010090 : "cc");
91
92 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010095static inline int arch_spin_trylock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned long tmp;
98
99 __asm__ __volatile__(
100" ldrex %0, [%1]\n"
101" teq %0, #0\n"
102" strexeq %0, %2, [%1]"
103 : "=&r" (tmp)
104 : "r" (&lock->lock), "r" (1)
Russell King6d9b37a2005-07-26 19:44:26 +0100105 : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Russell King6d9b37a2005-07-26 19:44:26 +0100107 if (tmp == 0) {
108 smp_mb();
109 return 1;
110 } else {
111 return 0;
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100115static inline void arch_spin_unlock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Russell King6d9b37a2005-07-26 19:44:26 +0100117 smp_mb();
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 __asm__ __volatile__(
Russell King00b4c902005-12-01 15:47:24 +0000120" str %1, [%0]\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 :
122 : "r" (&lock->lock), "r" (0)
Russell King6d9b37a2005-07-26 19:44:26 +0100123 : "cc");
Rabin Vincentc5113b62010-01-25 19:43:03 +0100124
125 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/*
129 * RWLOCKS
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700130 *
131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * Write locks are easy - we just set bit 31. When unlocking, we can
133 * just write zero since the lock is exclusively held.
134 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700135
Thomas Gleixnere5931942009-12-03 20:08:46 +0100136static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned long tmp;
139
140 __asm__ __volatile__(
141"1: ldrex %0, [%1]\n"
142" teq %0, #0\n"
Russell King000d9c72011-01-15 16:22:12 +0000143 WFE("ne")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144" strexeq %0, %2, [%1]\n"
145" teq %0, #0\n"
146" bne 1b"
147 : "=&r" (tmp)
148 : "r" (&rw->lock), "r" (0x80000000)
Russell King6d9b37a2005-07-26 19:44:26 +0100149 : "cc");
150
151 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Thomas Gleixnere5931942009-12-03 20:08:46 +0100154static inline int arch_write_trylock(arch_rwlock_t *rw)
Russell King4e8fd222005-07-24 12:13:40 +0100155{
156 unsigned long tmp;
157
158 __asm__ __volatile__(
159"1: ldrex %0, [%1]\n"
160" teq %0, #0\n"
161" strexeq %0, %2, [%1]"
162 : "=&r" (tmp)
163 : "r" (&rw->lock), "r" (0x80000000)
Russell King6d9b37a2005-07-26 19:44:26 +0100164 : "cc");
Russell King4e8fd222005-07-24 12:13:40 +0100165
Russell King6d9b37a2005-07-26 19:44:26 +0100166 if (tmp == 0) {
167 smp_mb();
168 return 1;
169 } else {
170 return 0;
171 }
Russell King4e8fd222005-07-24 12:13:40 +0100172}
173
Thomas Gleixnere5931942009-12-03 20:08:46 +0100174static inline void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Russell King6d9b37a2005-07-26 19:44:26 +0100176 smp_mb();
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 __asm__ __volatile__(
Russell King00b4c902005-12-01 15:47:24 +0000179 "str %1, [%0]\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 :
181 : "r" (&rw->lock), "r" (0)
Russell King6d9b37a2005-07-26 19:44:26 +0100182 : "cc");
Rabin Vincentc5113b62010-01-25 19:43:03 +0100183
184 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100187/* write_can_lock - would write_trylock() succeed? */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100188#define arch_write_can_lock(x) ((x)->lock == 0)
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * Read locks are a bit more hairy:
192 * - Exclusively load the lock value.
193 * - Increment it.
194 * - Store new lock value if positive, and we still own this location.
195 * If the value is negative, we've already failed.
196 * - If we failed to store the value, we want a negative result.
197 * - If we failed, try again.
198 * Unlocking is similarly hairy. We may have multiple read locks
199 * currently active. However, we know we won't have any write
200 * locks.
201 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100202static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 unsigned long tmp, tmp2;
205
206 __asm__ __volatile__(
207"1: ldrex %0, [%2]\n"
208" adds %0, %0, #1\n"
209" strexpl %1, %0, [%2]\n"
Russell King000d9c72011-01-15 16:22:12 +0000210 WFE("mi")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211" rsbpls %0, %1, #0\n"
212" bmi 1b"
213 : "=&r" (tmp), "=&r" (tmp2)
214 : "r" (&rw->lock)
Russell King6d9b37a2005-07-26 19:44:26 +0100215 : "cc");
216
217 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Thomas Gleixnere5931942009-12-03 20:08:46 +0100220static inline void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Russell King4e8fd222005-07-24 12:13:40 +0100222 unsigned long tmp, tmp2;
223
Russell King6d9b37a2005-07-26 19:44:26 +0100224 smp_mb();
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 __asm__ __volatile__(
227"1: ldrex %0, [%2]\n"
228" sub %0, %0, #1\n"
229" strex %1, %0, [%2]\n"
230" teq %1, #0\n"
231" bne 1b"
232 : "=&r" (tmp), "=&r" (tmp2)
233 : "r" (&rw->lock)
Russell King6d9b37a2005-07-26 19:44:26 +0100234 : "cc");
Rabin Vincentc5113b62010-01-25 19:43:03 +0100235
236 if (tmp == 0)
237 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Thomas Gleixnere5931942009-12-03 20:08:46 +0100240static inline int arch_read_trylock(arch_rwlock_t *rw)
Russell King8e347032006-08-31 15:09:30 +0100241{
Catalin Marinase89bc812006-09-06 19:03:14 +0100242 unsigned long tmp, tmp2 = 1;
Russell King8e347032006-08-31 15:09:30 +0100243
244 __asm__ __volatile__(
245"1: ldrex %0, [%2]\n"
246" adds %0, %0, #1\n"
247" strexpl %1, %0, [%2]\n"
248 : "=&r" (tmp), "+r" (tmp2)
249 : "r" (&rw->lock)
250 : "cc");
251
252 smp_mb();
253 return tmp2 == 0;
254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100256/* read_can_lock - would read_trylock() succeed? */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100257#define arch_read_can_lock(x) ((x)->lock < 0x80000000)
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100258
Thomas Gleixnere5931942009-12-03 20:08:46 +0100259#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
260#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700261
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100262#define arch_spin_relax(lock) cpu_relax()
263#define arch_read_relax(lock) cpu_relax()
264#define arch_write_relax(lock) cpu_relax()
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#endif /* __ASM_SPINLOCK_H */