blob: 4bec4544207243d477b2e326b50119cc842526fe [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
Will Deacon9bb17be2013-07-02 14:54:33 +01008#include <linux/prefetch.h>
Peter Zijlstra726328d2016-05-26 10:35:03 +02009#include <asm/barrier.h>
10#include <asm/processor.h>
Marc Zyngier603605a2011-05-23 17:16:59 +010011
Russell King000d9c72011-01-15 16:22:12 +000012/*
13 * sev and wfe are ARMv6K extensions. Uniprocessor ARMv6 may not have the K
14 * extensions, so when running on UP, we have to patch these instructions away.
15 */
Russell King000d9c72011-01-15 16:22:12 +000016#ifdef CONFIG_THUMB2_KERNEL
Dave Martin917692f2011-02-09 12:06:59 +010017/*
18 * For Thumb-2, special care is needed to ensure that the conditional WFE
19 * instruction really does assemble to exactly 4 bytes (as required by
20 * the SMP_ON_UP fixup code). By itself "wfene" might cause the
21 * assembler to insert a extra (16-bit) IT instruction, depending on the
22 * presence or absence of neighbouring conditional instructions.
23 *
24 * To avoid this unpredictableness, an approprite IT is inserted explicitly:
25 * the assembler won't change IT instructions which are explicitly present
26 * in the input.
27 */
Will Deacon27a84792013-07-02 12:10:42 +010028#define WFE(cond) __ALT_SMP_ASM( \
Dave Martin917692f2011-02-09 12:06:59 +010029 "it " cond "\n\t" \
30 "wfe" cond ".n", \
31 \
32 "nop.w" \
33)
Russell King000d9c72011-01-15 16:22:12 +000034#else
Will Deacon27a84792013-07-02 12:10:42 +010035#define WFE(cond) __ALT_SMP_ASM("wfe" cond, "nop")
Russell King000d9c72011-01-15 16:22:12 +000036#endif
37
Will Deacon27a84792013-07-02 12:10:42 +010038#define SEV __ALT_SMP_ASM(WASM(sev), WASM(nop))
39
Rabin Vincentc5113b62010-01-25 19:43:03 +010040static inline void dsb_sev(void)
41{
Will Deacon7c8746a2014-02-07 19:12:32 +010042
43 dsb(ishst);
44 __asm__(SEV);
Rabin Vincentc5113b62010-01-25 19:43:03 +010045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
Will Deacon546c2892012-07-06 15:43:41 +010048 * ARMv6 ticket-based spin-locking.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 *
Will Deacon546c2892012-07-06 15:43:41 +010050 * A memory barrier is required after we get a lock, and before we
51 * release it, because V6 CPUs are assumed to have weakly ordered
52 * memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Peter Zijlstra726328d2016-05-26 10:35:03 +020055static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
56{
57 u16 owner = READ_ONCE(lock->tickets.owner);
58
59 for (;;) {
60 arch_spinlock_t tmp = READ_ONCE(*lock);
61
62 if (tmp.tickets.owner == tmp.tickets.next ||
63 tmp.tickets.owner != owner)
64 break;
65
66 wfe();
67 }
68 smp_acquire__after_ctrl_dep();
69}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010071#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010073static inline void arch_spin_lock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 unsigned long tmp;
Will Deacon546c2892012-07-06 15:43:41 +010076 u32 newval;
77 arch_spinlock_t lockval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Will Deacon9bb17be2013-07-02 14:54:33 +010079 prefetchw(&lock->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 __asm__ __volatile__(
Will Deacon546c2892012-07-06 15:43:41 +010081"1: ldrex %0, [%3]\n"
82" add %1, %0, %4\n"
83" strex %2, %1, [%3]\n"
84" teq %2, #0\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085" bne 1b"
Will Deacon546c2892012-07-06 15:43:41 +010086 : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
87 : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
Russell King6d9b37a2005-07-26 19:44:26 +010088 : "cc");
89
Will Deacon546c2892012-07-06 15:43:41 +010090 while (lockval.tickets.next != lockval.tickets.owner) {
91 wfe();
92 lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
93 }
94
Russell King6d9b37a2005-07-26 19:44:26 +010095 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010098static inline int arch_spin_trylock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Will Deacon15e7e5c2013-06-05 11:27:26 +0100100 unsigned long contended, res;
Will Deacon546c2892012-07-06 15:43:41 +0100101 u32 slock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Will Deacon9bb17be2013-07-02 14:54:33 +0100103 prefetchw(&lock->slock);
Will Deacon15e7e5c2013-06-05 11:27:26 +0100104 do {
105 __asm__ __volatile__(
106 " ldrex %0, [%3]\n"
107 " mov %2, #0\n"
108 " subs %1, %0, %0, ror #16\n"
109 " addeq %0, %0, %4\n"
110 " strexeq %2, %0, [%3]"
Will Deaconafa31d82013-08-12 18:03:26 +0100111 : "=&r" (slock), "=&r" (contended), "=&r" (res)
Will Deacon15e7e5c2013-06-05 11:27:26 +0100112 : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
113 : "cc");
114 } while (res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Will Deacon15e7e5c2013-06-05 11:27:26 +0100116 if (!contended) {
Russell King6d9b37a2005-07-26 19:44:26 +0100117 smp_mb();
118 return 1;
119 } else {
120 return 0;
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100124static inline void arch_spin_unlock(arch_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Russell King6d9b37a2005-07-26 19:44:26 +0100126 smp_mb();
Will Deacon20e260b2013-01-24 14:47:38 +0100127 lock->tickets.owner++;
Rabin Vincentc5113b62010-01-25 19:43:03 +0100128 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Will Deacon0cbad9c2013-10-09 17:19:22 +0100131static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
132{
133 return lock.tickets.owner == lock.tickets.next;
134}
135
Will Deacon546c2892012-07-06 15:43:41 +0100136static inline int arch_spin_is_locked(arch_spinlock_t *lock)
137{
Christian Borntraeger488beef2014-11-25 11:44:26 +0100138 return !arch_spin_value_unlocked(READ_ONCE(*lock));
Will Deacon546c2892012-07-06 15:43:41 +0100139}
140
141static inline int arch_spin_is_contended(arch_spinlock_t *lock)
142{
Christian Borntraeger488beef2014-11-25 11:44:26 +0100143 struct __raw_tickets tickets = READ_ONCE(lock->tickets);
Will Deacon546c2892012-07-06 15:43:41 +0100144 return (tickets.next - tickets.owner) > 1;
145}
146#define arch_spin_is_contended arch_spin_is_contended
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * RWLOCKS
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700150 *
151 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Write locks are easy - we just set bit 31. When unlocking, we can
153 * just write zero since the lock is exclusively held.
154 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700155
Thomas Gleixnere5931942009-12-03 20:08:46 +0100156static inline void arch_write_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 unsigned long tmp;
159
Will Deacon9bb17be2013-07-02 14:54:33 +0100160 prefetchw(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 __asm__ __volatile__(
162"1: ldrex %0, [%1]\n"
163" teq %0, #0\n"
Russell King000d9c72011-01-15 16:22:12 +0000164 WFE("ne")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165" strexeq %0, %2, [%1]\n"
166" teq %0, #0\n"
167" bne 1b"
168 : "=&r" (tmp)
169 : "r" (&rw->lock), "r" (0x80000000)
Russell King6d9b37a2005-07-26 19:44:26 +0100170 : "cc");
171
172 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Thomas Gleixnere5931942009-12-03 20:08:46 +0100175static inline int arch_write_trylock(arch_rwlock_t *rw)
Russell King4e8fd222005-07-24 12:13:40 +0100176{
Will Deacon00efaa02013-08-12 18:04:05 +0100177 unsigned long contended, res;
Russell King4e8fd222005-07-24 12:13:40 +0100178
Will Deacon9bb17be2013-07-02 14:54:33 +0100179 prefetchw(&rw->lock);
Will Deacon00efaa02013-08-12 18:04:05 +0100180 do {
181 __asm__ __volatile__(
182 " ldrex %0, [%2]\n"
183 " mov %1, #0\n"
184 " teq %0, #0\n"
185 " strexeq %1, %3, [%2]"
186 : "=&r" (contended), "=&r" (res)
187 : "r" (&rw->lock), "r" (0x80000000)
188 : "cc");
189 } while (res);
Russell King4e8fd222005-07-24 12:13:40 +0100190
Will Deacon00efaa02013-08-12 18:04:05 +0100191 if (!contended) {
Russell King6d9b37a2005-07-26 19:44:26 +0100192 smp_mb();
193 return 1;
194 } else {
195 return 0;
196 }
Russell King4e8fd222005-07-24 12:13:40 +0100197}
198
Thomas Gleixnere5931942009-12-03 20:08:46 +0100199static inline void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Russell King6d9b37a2005-07-26 19:44:26 +0100201 smp_mb();
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 __asm__ __volatile__(
Russell King00b4c902005-12-01 15:47:24 +0000204 "str %1, [%0]\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 :
206 : "r" (&rw->lock), "r" (0)
Russell King6d9b37a2005-07-26 19:44:26 +0100207 : "cc");
Rabin Vincentc5113b62010-01-25 19:43:03 +0100208
209 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100212/* write_can_lock - would write_trylock() succeed? */
Will Deacon9bb17be2013-07-02 14:54:33 +0100213#define arch_write_can_lock(x) (ACCESS_ONCE((x)->lock) == 0)
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
216 * Read locks are a bit more hairy:
217 * - Exclusively load the lock value.
218 * - Increment it.
219 * - Store new lock value if positive, and we still own this location.
220 * If the value is negative, we've already failed.
221 * - If we failed to store the value, we want a negative result.
222 * - If we failed, try again.
223 * Unlocking is similarly hairy. We may have multiple read locks
224 * currently active. However, we know we won't have any write
225 * locks.
226 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100227static inline void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned long tmp, tmp2;
230
Will Deacon9bb17be2013-07-02 14:54:33 +0100231 prefetchw(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 __asm__ __volatile__(
233"1: ldrex %0, [%2]\n"
234" adds %0, %0, #1\n"
235" strexpl %1, %0, [%2]\n"
Russell King000d9c72011-01-15 16:22:12 +0000236 WFE("mi")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237" rsbpls %0, %1, #0\n"
238" bmi 1b"
239 : "=&r" (tmp), "=&r" (tmp2)
240 : "r" (&rw->lock)
Russell King6d9b37a2005-07-26 19:44:26 +0100241 : "cc");
242
243 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Thomas Gleixnere5931942009-12-03 20:08:46 +0100246static inline void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Russell King4e8fd222005-07-24 12:13:40 +0100248 unsigned long tmp, tmp2;
249
Russell King6d9b37a2005-07-26 19:44:26 +0100250 smp_mb();
251
Will Deacon9bb17be2013-07-02 14:54:33 +0100252 prefetchw(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 __asm__ __volatile__(
254"1: ldrex %0, [%2]\n"
255" sub %0, %0, #1\n"
256" strex %1, %0, [%2]\n"
257" teq %1, #0\n"
258" bne 1b"
259 : "=&r" (tmp), "=&r" (tmp2)
260 : "r" (&rw->lock)
Russell King6d9b37a2005-07-26 19:44:26 +0100261 : "cc");
Rabin Vincentc5113b62010-01-25 19:43:03 +0100262
263 if (tmp == 0)
264 dsb_sev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Thomas Gleixnere5931942009-12-03 20:08:46 +0100267static inline int arch_read_trylock(arch_rwlock_t *rw)
Russell King8e347032006-08-31 15:09:30 +0100268{
Will Deacon00efaa02013-08-12 18:04:05 +0100269 unsigned long contended, res;
Russell King8e347032006-08-31 15:09:30 +0100270
Will Deacon9bb17be2013-07-02 14:54:33 +0100271 prefetchw(&rw->lock);
Will Deacon00efaa02013-08-12 18:04:05 +0100272 do {
273 __asm__ __volatile__(
274 " ldrex %0, [%2]\n"
275 " mov %1, #0\n"
276 " adds %0, %0, #1\n"
277 " strexpl %1, %0, [%2]"
278 : "=&r" (contended), "=&r" (res)
279 : "r" (&rw->lock)
280 : "cc");
281 } while (res);
Russell King8e347032006-08-31 15:09:30 +0100282
Will Deacon00efaa02013-08-12 18:04:05 +0100283 /* If the lock is negative, then it is already held for write. */
284 if (contended < 0x80000000) {
285 smp_mb();
286 return 1;
287 } else {
288 return 0;
289 }
Russell King8e347032006-08-31 15:09:30 +0100290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100292/* read_can_lock - would read_trylock() succeed? */
Will Deacon9bb17be2013-07-02 14:54:33 +0100293#define arch_read_can_lock(x) (ACCESS_ONCE((x)->lock) < 0x80000000)
Catalin Marinasc2a4c402006-05-19 21:55:35 +0100294
Thomas Gleixnere5931942009-12-03 20:08:46 +0100295#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
296#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700297
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100298#define arch_spin_relax(lock) cpu_relax()
299#define arch_read_relax(lock) cpu_relax()
300#define arch_write_relax(lock) cpu_relax()
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif /* __ASM_SPINLOCK_H */