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 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 21 | unsigned int old_expected = old; |
| 22 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 23 | asm volatile( |
| 24 | " cs %0,%3,%1" |
| 25 | : "=d" (old), "=Q" (*lock) |
| 26 | : "0" (old), "d" (new), "Q" (*lock) |
| 27 | : "cc", "memory" ); |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 28 | return old == old_expected; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 29 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 33 | * on the local processor, one does not. |
| 34 | * |
| 35 | * We make no fairness assumptions. They have a cost. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 36 | * |
| 37 | * (the type definitions are in asm/spinlock_types.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 40 | void arch_spin_lock_wait(arch_spinlock_t *); |
| 41 | int arch_spin_trylock_retry(arch_spinlock_t *); |
| 42 | void arch_spin_relax(arch_spinlock_t *); |
| 43 | void arch_spin_lock_wait_flags(arch_spinlock_t *, unsigned long flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame^] | 45 | static inline u32 arch_spin_lockval(int cpu) |
| 46 | { |
| 47 | return ~cpu; |
| 48 | } |
| 49 | |
Heiko Carstens | efc1d23 | 2013-09-05 13:26:17 +0200 | [diff] [blame] | 50 | static inline int arch_spin_value_unlocked(arch_spinlock_t lock) |
| 51 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 52 | return lock.lock == 0; |
| 53 | } |
| 54 | |
| 55 | static inline int arch_spin_is_locked(arch_spinlock_t *lp) |
| 56 | { |
| 57 | return ACCESS_ONCE(lp->lock) != 0; |
| 58 | } |
| 59 | |
| 60 | static inline int arch_spin_trylock_once(arch_spinlock_t *lp) |
| 61 | { |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame^] | 62 | return _raw_compare_and_swap(&lp->lock, 0, SPINLOCK_LOCKVAL); |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline int arch_spin_tryrelease_once(arch_spinlock_t *lp) |
| 66 | { |
Philipp Hachtmann | 6c8cd5b | 2014-04-07 18:25:23 +0200 | [diff] [blame^] | 67 | return _raw_compare_and_swap(&lp->lock, SPINLOCK_LOCKVAL, 0); |
Heiko Carstens | efc1d23 | 2013-09-05 13:26:17 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 70 | static inline void arch_spin_lock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 72 | if (unlikely(!arch_spin_trylock_once(lp))) |
| 73 | arch_spin_lock_wait(lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 76 | static inline void arch_spin_lock_flags(arch_spinlock_t *lp, |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 77 | unsigned long flags) |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 78 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 79 | if (unlikely(!arch_spin_trylock_once(lp))) |
| 80 | arch_spin_lock_wait_flags(lp, flags); |
Hisashi Hifumi | 894cdde | 2008-01-26 14:11:28 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 83 | static inline int arch_spin_trylock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 85 | if (unlikely(!arch_spin_trylock_once(lp))) |
| 86 | return arch_spin_trylock_retry(lp); |
| 87 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 90 | static inline void arch_spin_unlock(arch_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 92 | arch_spin_tryrelease_once(lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 94 | |
| 95 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
| 96 | { |
| 97 | while (arch_spin_is_locked(lock)) |
| 98 | arch_spin_relax(lock); |
| 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Read-write spinlocks, allowing multiple readers |
| 103 | * but only one writer. |
| 104 | * |
| 105 | * NOTE! it is quite common to have readers in interrupts |
| 106 | * but no interrupt writers. For those circumstances we |
| 107 | * can "mix" irq-safe locks - any writer needs to get a |
| 108 | * irq-safe write-lock, but readers can get non-irqsafe |
| 109 | * read-locks. |
| 110 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * read_can_lock - would read_trylock() succeed? |
| 114 | * @lock: the rwlock in question. |
| 115 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 116 | #define arch_read_can_lock(x) ((int)(x)->lock >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * write_can_lock - would write_trylock() succeed? |
| 120 | * @lock: the rwlock in question. |
| 121 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 122 | #define arch_write_can_lock(x) ((x)->lock == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Thomas Gleixner | fb3a6bb | 2009-12-03 20:01:19 +0100 | [diff] [blame] | 124 | extern void _raw_read_lock_wait(arch_rwlock_t *lp); |
| 125 | extern void _raw_read_lock_wait_flags(arch_rwlock_t *lp, unsigned long flags); |
| 126 | extern int _raw_read_trylock_retry(arch_rwlock_t *lp); |
| 127 | extern void _raw_write_lock_wait(arch_rwlock_t *lp); |
| 128 | extern void _raw_write_lock_wait_flags(arch_rwlock_t *lp, unsigned long flags); |
| 129 | extern int _raw_write_trylock_retry(arch_rwlock_t *lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 131 | static inline void arch_read_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 133 | unsigned int old; |
| 134 | old = rw->lock & 0x7fffffffU; |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 135 | if (!_raw_compare_and_swap(&rw->lock, old, old + 1)) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 136 | _raw_read_lock_wait(rw); |
| 137 | } |
| 138 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 139 | static inline void arch_read_lock_flags(arch_rwlock_t *rw, unsigned long flags) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 140 | { |
| 141 | unsigned int old; |
| 142 | old = rw->lock & 0x7fffffffU; |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 143 | if (!_raw_compare_and_swap(&rw->lock, old, old + 1)) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 144 | _raw_read_lock_wait_flags(rw, flags); |
| 145 | } |
| 146 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 147 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 148 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 149 | unsigned int old; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 150 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 151 | do { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 152 | old = ACCESS_ONCE(rw->lock); |
| 153 | } while (!_raw_compare_and_swap(&rw->lock, old, old - 1)); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 156 | static inline void arch_write_lock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 157 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 158 | if (unlikely(!_raw_compare_and_swap(&rw->lock, 0, 0x80000000))) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 159 | _raw_write_lock_wait(rw); |
| 160 | } |
| 161 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 162 | static inline void arch_write_lock_flags(arch_rwlock_t *rw, unsigned long flags) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 163 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 164 | if (unlikely(!_raw_compare_and_swap(&rw->lock, 0, 0x80000000))) |
Heiko Carstens | ce58ae6 | 2009-06-12 10:26:22 +0200 | [diff] [blame] | 165 | _raw_write_lock_wait_flags(rw, flags); |
| 166 | } |
| 167 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 168 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 169 | { |
| 170 | _raw_compare_and_swap(&rw->lock, 0x80000000, 0); |
| 171 | } |
| 172 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 173 | static inline int arch_read_trylock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 174 | { |
| 175 | unsigned int old; |
| 176 | old = rw->lock & 0x7fffffffU; |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 177 | if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1))) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 178 | return 1; |
| 179 | return _raw_read_trylock_retry(rw); |
| 180 | } |
| 181 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 182 | static inline int arch_write_trylock(arch_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 183 | { |
Philipp Hachtmann | 5b3f683 | 2014-04-07 18:25:23 +0200 | [diff] [blame] | 184 | if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000))) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 185 | return 1; |
| 186 | return _raw_write_trylock_retry(rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 189 | #define arch_read_relax(lock) cpu_relax() |
| 190 | #define arch_write_relax(lock) cpu_relax() |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #endif /* __ASM_SPINLOCK_H */ |