Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * S390 version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 3 | * Copyright IBM Corp. 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 5 | * |
| 6 | * Derived from "include/asm-i386/spinlock.h" |
| 7 | */ |
| 8 | |
| 9 | #ifndef __ASM_SPINLOCK_H |
| 10 | #define __ASM_SPINLOCK_H |
| 11 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 12 | #include <linux/smp.h> |
| 13 | |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 14 | #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval) |
| 15 | |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 16 | extern int spin_retry; |
| 17 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 18 | static inline int |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 19 | _raw_compare_and_swap(unsigned int *lock, unsigned int old, unsigned int new) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 20 | { |
Martin Schwidefsky | f318a12 | 2014-10-29 12:50:31 +0100 | [diff] [blame] | 21 | return __sync_bool_compare_and_swap(lock, old, new); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 22 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 26 | * on the local processor, one does not. |
| 27 | * |
| 28 | * We make no fairness assumptions. They have a cost. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 29 | * |
| 30 | * (the type definitions are in asm/spinlock_types.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 33 | void arch_lock_relax(unsigned int cpu); |
| 34 | |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 35 | void arch_spin_lock_wait(arch_spinlock_t *); |
| 36 | int arch_spin_trylock_retry(arch_spinlock_t *); |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 37 | void arch_spin_lock_wait_flags(arch_spinlock_t *, unsigned long flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 39 | static inline void arch_spin_relax(arch_spinlock_t *lock) |
| 40 | { |
| 41 | arch_lock_relax(lock->lock); |
| 42 | } |
| 43 | |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 44 | static inline u32 arch_spin_lockval(int cpu) |
| 45 | { |
| 46 | return ~cpu; |
| 47 | } |
| 48 | |
Heiko Carstens | efc1d23 | 2013-09-05 13:26:17 +0200 | [diff] [blame] | 49 | static inline int arch_spin_value_unlocked(arch_spinlock_t lock) |
| 50 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 51 | return lock.lock == 0; |
| 52 | } |
| 53 | |
| 54 | static inline int arch_spin_is_locked(arch_spinlock_t *lp) |
| 55 | { |
| 56 | return ACCESS_ONCE(lp->lock) != 0; |
| 57 | } |
| 58 | |
| 59 | static inline int arch_spin_trylock_once(arch_spinlock_t *lp) |
| 60 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 61 | barrier(); |
| 62 | return likely(arch_spin_value_unlocked(*lp) && |
| 63 | _raw_compare_and_swap(&lp->lock, 0, SPINLOCK_LOCKVAL)); |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 66 | static inline void arch_spin_lock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 68 | if (!arch_spin_trylock_once(lp)) |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 69 | arch_spin_lock_wait(lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 72 | static inline void arch_spin_lock_flags(arch_spinlock_t *lp, |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 73 | unsigned long flags) |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 74 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 75 | if (!arch_spin_trylock_once(lp)) |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 76 | arch_spin_lock_wait_flags(lp, flags); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 79 | static inline int arch_spin_trylock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 81 | if (!arch_spin_trylock_once(lp)) |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 82 | return arch_spin_trylock_retry(lp); |
| 83 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 86 | static inline void arch_spin_unlock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Heiko Carstens | 4423028 | 2014-09-08 08:20:43 +0200 | [diff] [blame] | 88 | typecheck(unsigned int, lp->lock); |
| 89 | asm volatile( |
| 90 | __ASM_BARRIER |
| 91 | "st %1,%0\n" |
| 92 | : "+Q" (lp->lock) |
| 93 | : "d" (0) |
| 94 | : "cc", "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 96 | |
| 97 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
| 98 | { |
| 99 | while (arch_spin_is_locked(lock)) |
| 100 | arch_spin_relax(lock); |
| 101 | } |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Read-write spinlocks, allowing multiple readers |
| 105 | * but only one writer. |
| 106 | * |
| 107 | * NOTE! it is quite common to have readers in interrupts |
| 108 | * but no interrupt writers. For those circumstances we |
| 109 | * can "mix" irq-safe locks - any writer needs to get a |
| 110 | * irq-safe write-lock, but readers can get non-irqsafe |
| 111 | * read-locks. |
| 112 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * read_can_lock - would read_trylock() succeed? |
| 116 | * @lock: the rwlock in question. |
| 117 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 118 | #define arch_read_can_lock(x) ((int)(x)->lock >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * write_can_lock - would write_trylock() succeed? |
| 122 | * @lock: the rwlock in question. |
| 123 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 124 | #define arch_write_can_lock(x) ((x)->lock == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Martin Schwidefsky | 2684e73 | 2014-09-22 14:45:11 +0200 | [diff] [blame] | 126 | extern int _raw_read_trylock_retry(arch_rwlock_t *lp); |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 127 | extern int _raw_write_trylock_retry(arch_rwlock_t *lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Martin Schwidefsky | 2684e73 | 2014-09-22 14:45:11 +0200 | [diff] [blame] | 129 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 130 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
| 131 | |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 132 | static inline int arch_read_trylock_once(arch_rwlock_t *rw) |
| 133 | { |
| 134 | unsigned int old = ACCESS_ONCE(rw->lock); |
| 135 | return likely((int) old >= 0 && |
| 136 | _raw_compare_and_swap(&rw->lock, old, old + 1)); |
| 137 | } |
| 138 | |
| 139 | static inline int arch_write_trylock_once(arch_rwlock_t *rw) |
| 140 | { |
| 141 | unsigned int old = ACCESS_ONCE(rw->lock); |
| 142 | return likely(old == 0 && |
| 143 | _raw_compare_and_swap(&rw->lock, 0, 0x80000000)); |
| 144 | } |
| 145 | |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 146 | #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES |
| 147 | |
| 148 | #define __RAW_OP_OR "lao" |
| 149 | #define __RAW_OP_AND "lan" |
| 150 | #define __RAW_OP_ADD "laa" |
| 151 | |
| 152 | #define __RAW_LOCK(ptr, op_val, op_string) \ |
| 153 | ({ \ |
| 154 | unsigned int old_val; \ |
| 155 | \ |
| 156 | typecheck(unsigned int *, ptr); \ |
| 157 | asm volatile( \ |
| 158 | op_string " %0,%2,%1\n" \ |
| 159 | "bcr 14,0\n" \ |
| 160 | : "=d" (old_val), "+Q" (*ptr) \ |
| 161 | : "d" (op_val) \ |
| 162 | : "cc", "memory"); \ |
| 163 | old_val; \ |
| 164 | }) |
| 165 | |
| 166 | #define __RAW_UNLOCK(ptr, op_val, op_string) \ |
| 167 | ({ \ |
| 168 | unsigned int old_val; \ |
| 169 | \ |
| 170 | typecheck(unsigned int *, ptr); \ |
| 171 | asm volatile( \ |
| 172 | "bcr 14,0\n" \ |
| 173 | op_string " %0,%2,%1\n" \ |
| 174 | : "=d" (old_val), "+Q" (*ptr) \ |
| 175 | : "d" (op_val) \ |
| 176 | : "cc", "memory"); \ |
| 177 | old_val; \ |
| 178 | }) |
| 179 | |
| 180 | extern void _raw_read_lock_wait(arch_rwlock_t *lp); |
| 181 | extern void _raw_write_lock_wait(arch_rwlock_t *lp, unsigned int prev); |
| 182 | |
| 183 | static inline void arch_read_lock(arch_rwlock_t *rw) |
| 184 | { |
| 185 | unsigned int old; |
| 186 | |
| 187 | old = __RAW_LOCK(&rw->lock, 1, __RAW_OP_ADD); |
| 188 | if ((int) old < 0) |
| 189 | _raw_read_lock_wait(rw); |
| 190 | } |
| 191 | |
| 192 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
| 193 | { |
| 194 | __RAW_UNLOCK(&rw->lock, -1, __RAW_OP_ADD); |
| 195 | } |
| 196 | |
| 197 | static inline void arch_write_lock(arch_rwlock_t *rw) |
| 198 | { |
| 199 | unsigned int old; |
| 200 | |
| 201 | old = __RAW_LOCK(&rw->lock, 0x80000000, __RAW_OP_OR); |
| 202 | if (old != 0) |
| 203 | _raw_write_lock_wait(rw, old); |
| 204 | rw->owner = SPINLOCK_LOCKVAL; |
| 205 | } |
| 206 | |
| 207 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
| 208 | { |
| 209 | rw->owner = 0; |
| 210 | __RAW_UNLOCK(&rw->lock, 0x7fffffff, __RAW_OP_AND); |
| 211 | } |
| 212 | |
| 213 | #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 214 | |
| 215 | extern void _raw_read_lock_wait(arch_rwlock_t *lp); |
| 216 | extern void _raw_write_lock_wait(arch_rwlock_t *lp); |
| 217 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 218 | static inline void arch_read_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 220 | if (!arch_read_trylock_once(rw)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 221 | _raw_read_lock_wait(rw); |
| 222 | } |
| 223 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 224 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 225 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 226 | unsigned int old; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 227 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 228 | do { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 229 | old = ACCESS_ONCE(rw->lock); |
| 230 | } while (!_raw_compare_and_swap(&rw->lock, old, old - 1)); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 233 | static inline void arch_write_lock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 234 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 235 | if (!arch_write_trylock_once(rw)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 236 | _raw_write_lock_wait(rw); |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 237 | rw->owner = SPINLOCK_LOCKVAL; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 240 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 241 | { |
Heiko Carstens | 4423028 | 2014-09-08 08:20:43 +0200 | [diff] [blame] | 242 | typecheck(unsigned int, rw->lock); |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 243 | |
| 244 | rw->owner = 0; |
Heiko Carstens | 4423028 | 2014-09-08 08:20:43 +0200 | [diff] [blame] | 245 | asm volatile( |
| 246 | __ASM_BARRIER |
| 247 | "st %1,%0\n" |
| 248 | : "+Q" (rw->lock) |
| 249 | : "d" (0) |
| 250 | : "cc", "memory"); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Martin Schwidefsky | bbae71b | 2014-09-22 16:34:38 +0200 | [diff] [blame] | 253 | #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 254 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 255 | static inline int arch_read_trylock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 256 | { |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 257 | if (!arch_read_trylock_once(rw)) |
| 258 | return _raw_read_trylock_retry(rw); |
| 259 | return 1; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 262 | static inline int arch_write_trylock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 263 | { |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 264 | if (!arch_write_trylock_once(rw) && !_raw_write_trylock_retry(rw)) |
| 265 | return 0; |
| 266 | rw->owner = SPINLOCK_LOCKVAL; |
Martin Schwidefsky | bae8f56 | 2014-05-15 11:00:44 +0200 | [diff] [blame] | 267 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Martin Schwidefsky | d59b93d | 2014-09-19 14:29:31 +0200 | [diff] [blame] | 270 | static inline void arch_read_relax(arch_rwlock_t *rw) |
| 271 | { |
| 272 | arch_lock_relax(rw->owner); |
| 273 | } |
| 274 | |
| 275 | static inline void arch_write_relax(arch_rwlock_t *rw) |
| 276 | { |
| 277 | arch_lock_relax(rw->owner); |
| 278 | } |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | #endif /* __ASM_SPINLOCK_H */ |