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