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