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