Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * include/asm-s390/spinlock.h |
| 3 | * |
| 4 | * S390 version |
| 5 | * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 6 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 7 | * |
| 8 | * Derived from "include/asm-i386/spinlock.h" |
| 9 | */ |
| 10 | |
| 11 | #ifndef __ASM_SPINLOCK_H |
| 12 | #define __ASM_SPINLOCK_H |
| 13 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 14 | #include <linux/smp.h> |
| 15 | |
Martin Schwidefsky | 42e47ee | 2006-10-04 20:02:12 +0200 | [diff] [blame] | 16 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2) |
| 17 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 18 | static inline int |
| 19 | _raw_compare_and_swap(volatile unsigned int *lock, |
| 20 | unsigned int old, unsigned int new) |
| 21 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 22 | asm volatile( |
| 23 | " cs %0,%3,%1" |
| 24 | : "=d" (old), "=Q" (*lock) |
| 25 | : "0" (old), "d" (new), "Q" (*lock) |
| 26 | : "cc", "memory" ); |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 27 | return old; |
| 28 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 30 | #else /* __GNUC__ */ |
| 31 | |
| 32 | static inline int |
| 33 | _raw_compare_and_swap(volatile unsigned int *lock, |
| 34 | unsigned int old, unsigned int new) |
| 35 | { |
| 36 | asm volatile( |
| 37 | " cs %0,%3,0(%4)" |
| 38 | : "=d" (old), "=m" (*lock) |
| 39 | : "0" (old), "d" (new), "a" (lock), "m" (*lock) |
| 40 | : "cc", "memory" ); |
| 41 | return old; |
| 42 | } |
| 43 | |
| 44 | #endif /* __GNUC__ */ |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 48 | * on the local processor, one does not. |
| 49 | * |
| 50 | * We make no fairness assumptions. They have a cost. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 51 | * |
| 52 | * (the type definitions are in asm/spinlock_types.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | */ |
| 54 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 55 | #define __raw_spin_is_locked(x) ((x)->owner_cpu != 0) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 56 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
| 57 | #define __raw_spin_unlock_wait(lock) \ |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 58 | do { while (__raw_spin_is_locked(lock)) \ |
| 59 | _raw_spin_relax(lock); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 61 | extern void _raw_spin_lock_wait(raw_spinlock_t *, unsigned int pc); |
| 62 | extern int _raw_spin_trylock_retry(raw_spinlock_t *, unsigned int pc); |
| 63 | extern void _raw_spin_relax(raw_spinlock_t *lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 65 | static inline void __raw_spin_lock(raw_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Heiko Carstens | 9513e5e | 2005-09-03 15:58:05 -0700 | [diff] [blame] | 67 | unsigned long pc = 1 | (unsigned long) __builtin_return_address(0); |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 68 | int old; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 69 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 70 | old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id()); |
| 71 | if (likely(old == 0)) { |
| 72 | lp->owner_pc = pc; |
| 73 | return; |
| 74 | } |
| 75 | _raw_spin_lock_wait(lp, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 78 | static inline int __raw_spin_trylock(raw_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Heiko Carstens | 9513e5e | 2005-09-03 15:58:05 -0700 | [diff] [blame] | 80 | unsigned long pc = 1 | (unsigned long) __builtin_return_address(0); |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 81 | int old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 83 | old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id()); |
| 84 | if (likely(old == 0)) { |
| 85 | lp->owner_pc = pc; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 86 | return 1; |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 87 | } |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 88 | return _raw_spin_trylock_retry(lp, pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 91 | static inline void __raw_spin_unlock(raw_spinlock_t *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Martin Schwidefsky | 3c1fcfe | 2006-09-30 23:27:45 -0700 | [diff] [blame] | 93 | lp->owner_pc = 0; |
| 94 | _raw_compare_and_swap(&lp->owner_cpu, lp->owner_cpu, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Read-write spinlocks, allowing multiple readers |
| 99 | * but only one writer. |
| 100 | * |
| 101 | * NOTE! it is quite common to have readers in interrupts |
| 102 | * but no interrupt writers. For those circumstances we |
| 103 | * can "mix" irq-safe locks - any writer needs to get a |
| 104 | * irq-safe write-lock, but readers can get non-irqsafe |
| 105 | * read-locks. |
| 106 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * read_can_lock - would read_trylock() succeed? |
| 110 | * @lock: the rwlock in question. |
| 111 | */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 112 | #define __raw_read_can_lock(x) ((int)(x)->lock >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * write_can_lock - would write_trylock() succeed? |
| 116 | * @lock: the rwlock in question. |
| 117 | */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 118 | #define __raw_write_can_lock(x) ((x)->lock == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 120 | extern void _raw_read_lock_wait(raw_rwlock_t *lp); |
| 121 | extern int _raw_read_trylock_retry(raw_rwlock_t *lp); |
| 122 | extern void _raw_write_lock_wait(raw_rwlock_t *lp); |
| 123 | extern int _raw_write_trylock_retry(raw_rwlock_t *lp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 125 | static inline void __raw_read_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 127 | unsigned int old; |
| 128 | old = rw->lock & 0x7fffffffU; |
| 129 | if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old) |
| 130 | _raw_read_lock_wait(rw); |
| 131 | } |
| 132 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 133 | static inline void __raw_read_unlock(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 134 | { |
| 135 | unsigned int old, cmp; |
| 136 | |
| 137 | old = rw->lock; |
| 138 | do { |
| 139 | cmp = old; |
| 140 | old = _raw_compare_and_swap(&rw->lock, old, old - 1); |
| 141 | } while (cmp != old); |
| 142 | } |
| 143 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 144 | static inline void __raw_write_lock(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 145 | { |
| 146 | if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0)) |
| 147 | _raw_write_lock_wait(rw); |
| 148 | } |
| 149 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 150 | static inline void __raw_write_unlock(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 151 | { |
| 152 | _raw_compare_and_swap(&rw->lock, 0x80000000, 0); |
| 153 | } |
| 154 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 155 | static inline int __raw_read_trylock(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 156 | { |
| 157 | unsigned int old; |
| 158 | old = rw->lock & 0x7fffffffU; |
| 159 | if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old)) |
| 160 | return 1; |
| 161 | return _raw_read_trylock_retry(rw); |
| 162 | } |
| 163 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 164 | static inline int __raw_write_trylock(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 165 | { |
| 166 | if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)) |
| 167 | return 1; |
| 168 | return _raw_write_trylock_retry(rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 171 | #define _raw_read_relax(lock) cpu_relax() |
| 172 | #define _raw_write_relax(lock) cpu_relax() |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | #endif /* __ASM_SPINLOCK_H */ |