Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 8 | /* |
| 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 Martin | 917692f | 2011-02-09 12:06:59 +0100 | [diff] [blame] | 21 | /* |
| 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 King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 38 | #else |
| 39 | #define SEV ALT_SMP("sev", "nop") |
| 40 | #define WFE(cond) ALT_SMP("wfe" cond, "nop") |
| 41 | #endif |
| 42 | |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 43 | static inline void dsb_sev(void) |
| 44 | { |
| 45 | #if __LINUX_ARM_ARCH__ >= 7 |
| 46 | __asm__ __volatile__ ( |
| 47 | "dsb\n" |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 48 | SEV |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 49 | ); |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 50 | #else |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 51 | __asm__ __volatile__ ( |
| 52 | "mcr p15, 0, %0, c7, c10, 4\n" |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 53 | SEV |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 54 | : : "r" (0) |
| 55 | ); |
| 56 | #endif |
| 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /* |
| 60 | * ARMv6 Spin-locking. |
| 61 | * |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 62 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * |
| 67 | * Unlocked value: 0 |
| 68 | * Locked value: 1 |
| 69 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 71 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 75 | #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 77 | static inline void arch_spin_lock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | unsigned long tmp; |
| 80 | |
| 81 | __asm__ __volatile__( |
| 82 | "1: ldrex %0, [%1]\n" |
| 83 | " teq %0, #0\n" |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 84 | WFE("ne") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | " strexeq %0, %2, [%1]\n" |
| 86 | " teqeq %0, #0\n" |
| 87 | " bne 1b" |
| 88 | : "=&r" (tmp) |
| 89 | : "r" (&lock->lock), "r" (1) |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 90 | : "cc"); |
| 91 | |
| 92 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 95 | static inline int arch_spin_trylock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 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 King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 105 | : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 107 | if (tmp == 0) { |
| 108 | smp_mb(); |
| 109 | return 1; |
| 110 | } else { |
| 111 | return 0; |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 115 | static inline void arch_spin_unlock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 117 | smp_mb(); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | __asm__ __volatile__( |
Russell King | 00b4c90 | 2005-12-01 15:47:24 +0000 | [diff] [blame] | 120 | " str %1, [%0]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | : |
| 122 | : "r" (&lock->lock), "r" (0) |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 123 | : "cc"); |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 124 | |
| 125 | dsb_sev(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * RWLOCKS |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 130 | * |
| 131 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * 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 Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 135 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 136 | static inline void arch_write_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | unsigned long tmp; |
| 139 | |
| 140 | __asm__ __volatile__( |
| 141 | "1: ldrex %0, [%1]\n" |
| 142 | " teq %0, #0\n" |
Russell King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 143 | WFE("ne") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | " strexeq %0, %2, [%1]\n" |
| 145 | " teq %0, #0\n" |
| 146 | " bne 1b" |
| 147 | : "=&r" (tmp) |
| 148 | : "r" (&rw->lock), "r" (0x80000000) |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 149 | : "cc"); |
| 150 | |
| 151 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 154 | static inline int arch_write_trylock(arch_rwlock_t *rw) |
Russell King | 4e8fd22 | 2005-07-24 12:13:40 +0100 | [diff] [blame] | 155 | { |
| 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 King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 164 | : "cc"); |
Russell King | 4e8fd22 | 2005-07-24 12:13:40 +0100 | [diff] [blame] | 165 | |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 166 | if (tmp == 0) { |
| 167 | smp_mb(); |
| 168 | return 1; |
| 169 | } else { |
| 170 | return 0; |
| 171 | } |
Russell King | 4e8fd22 | 2005-07-24 12:13:40 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 174 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 176 | smp_mb(); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | __asm__ __volatile__( |
Russell King | 00b4c90 | 2005-12-01 15:47:24 +0000 | [diff] [blame] | 179 | "str %1, [%0]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | : |
| 181 | : "r" (&rw->lock), "r" (0) |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 182 | : "cc"); |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 183 | |
| 184 | dsb_sev(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Catalin Marinas | c2a4c40 | 2006-05-19 21:55:35 +0100 | [diff] [blame] | 187 | /* write_can_lock - would write_trylock() succeed? */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 188 | #define arch_write_can_lock(x) ((x)->lock == 0) |
Catalin Marinas | c2a4c40 | 2006-05-19 21:55:35 +0100 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
| 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 Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 202 | static inline void arch_read_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 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 King | 000d9c7 | 2011-01-15 16:22:12 +0000 | [diff] [blame] | 210 | WFE("mi") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | " rsbpls %0, %1, #0\n" |
| 212 | " bmi 1b" |
| 213 | : "=&r" (tmp), "=&r" (tmp2) |
| 214 | : "r" (&rw->lock) |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 215 | : "cc"); |
| 216 | |
| 217 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 220 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Russell King | 4e8fd22 | 2005-07-24 12:13:40 +0100 | [diff] [blame] | 222 | unsigned long tmp, tmp2; |
| 223 | |
Russell King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 224 | smp_mb(); |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | __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 King | 6d9b37a | 2005-07-26 19:44:26 +0100 | [diff] [blame] | 234 | : "cc"); |
Rabin Vincent | c5113b6 | 2010-01-25 19:43:03 +0100 | [diff] [blame] | 235 | |
| 236 | if (tmp == 0) |
| 237 | dsb_sev(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 240 | static inline int arch_read_trylock(arch_rwlock_t *rw) |
Russell King | 8e34703 | 2006-08-31 15:09:30 +0100 | [diff] [blame] | 241 | { |
Catalin Marinas | e89bc81 | 2006-09-06 19:03:14 +0100 | [diff] [blame] | 242 | unsigned long tmp, tmp2 = 1; |
Russell King | 8e34703 | 2006-08-31 15:09:30 +0100 | [diff] [blame] | 243 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Catalin Marinas | c2a4c40 | 2006-05-19 21:55:35 +0100 | [diff] [blame] | 256 | /* read_can_lock - would read_trylock() succeed? */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 257 | #define arch_read_can_lock(x) ((x)->lock < 0x80000000) |
Catalin Marinas | c2a4c40 | 2006-05-19 21:55:35 +0100 | [diff] [blame] | 258 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 259 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 260 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
Robin Holt | f5f7eac | 2009-04-02 16:59:46 -0700 | [diff] [blame] | 261 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 262 | #define arch_spin_relax(lock) cpu_relax() |
| 263 | #define arch_read_relax(lock) cpu_relax() |
| 264 | #define arch_write_relax(lock) cpu_relax() |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #endif /* __ASM_SPINLOCK_H */ |