Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SPINLOCK_H |
| 2 | #define __ASM_SPINLOCK_H |
| 3 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 4 | #include <asm/processor.h> |
| 5 | #include <asm/spinlock_types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 7 | static inline int arch_spin_is_locked(arch_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | { |
| 9 | volatile unsigned int *a = __ldcw_align(x); |
| 10 | return *a == 0; |
| 11 | } |
| 12 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 13 | #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0) |
| 14 | #define arch_spin_unlock_wait(x) \ |
| 15 | do { cpu_relax(); } while (arch_spin_is_locked(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 17 | static inline void arch_spin_lock_flags(arch_spinlock_t *x, |
James Bottomley | 08dc2ca | 2005-11-17 16:35:09 -0500 | [diff] [blame] | 18 | unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
| 20 | volatile unsigned int *a; |
| 21 | |
| 22 | mb(); |
| 23 | a = __ldcw_align(x); |
| 24 | while (__ldcw(a) == 0) |
James Bottomley | 08dc2ca | 2005-11-17 16:35:09 -0500 | [diff] [blame] | 25 | while (*a == 0) |
| 26 | if (flags & PSW_SM_I) { |
| 27 | local_irq_enable(); |
| 28 | cpu_relax(); |
| 29 | local_irq_disable(); |
| 30 | } else |
| 31 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | mb(); |
| 33 | } |
| 34 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 35 | static inline void arch_spin_unlock(arch_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | volatile unsigned int *a; |
| 38 | mb(); |
| 39 | a = __ldcw_align(x); |
| 40 | *a = 1; |
| 41 | mb(); |
| 42 | } |
| 43 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 44 | static inline int arch_spin_trylock(arch_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | volatile unsigned int *a; |
| 47 | int ret; |
| 48 | |
| 49 | mb(); |
| 50 | a = __ldcw_align(x); |
| 51 | ret = __ldcw(a) != 0; |
| 52 | mb(); |
| 53 | |
| 54 | return ret; |
| 55 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 58 | * Read-write spinlocks, allowing multiple readers but only one writer. |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 59 | * Linux rwlocks are unfair to writers; they can be starved for an indefinite |
| 60 | * time by readers. With care, they can also be taken in interrupt context. |
| 61 | * |
| 62 | * In the PA-RISC implementation, we have a spinlock and a counter. |
| 63 | * Readers use the lock to serialise their access to the counter (which |
| 64 | * records how many readers currently hold the lock). |
| 65 | * Writers hold the spinlock, preventing any readers or other writers from |
| 66 | * grabbing the rwlock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 69 | /* Note that we have to ensure interrupts are disabled in case we're |
| 70 | * interrupted by some other code that wants to grab the same read lock */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 71 | static __inline__ void arch_read_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 73 | unsigned long flags; |
| 74 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 75 | arch_spin_lock_flags(&rw->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | rw->counter++; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 77 | arch_spin_unlock(&rw->lock); |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 78 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 81 | /* Note that we have to ensure interrupts are disabled in case we're |
| 82 | * interrupted by some other code that wants to grab the same read lock */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 83 | static __inline__ void arch_read_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 85 | unsigned long flags; |
| 86 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 87 | arch_spin_lock_flags(&rw->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | rw->counter--; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 89 | arch_spin_unlock(&rw->lock); |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 90 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 93 | /* Note that we have to ensure interrupts are disabled in case we're |
| 94 | * interrupted by some other code that wants to grab the same read lock */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 95 | static __inline__ int arch_read_trylock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 97 | unsigned long flags; |
| 98 | retry: |
| 99 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 100 | if (arch_spin_trylock(&rw->lock)) { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 101 | rw->counter++; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 102 | arch_spin_unlock(&rw->lock); |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 103 | local_irq_restore(flags); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | local_irq_restore(flags); |
| 108 | /* If write-locked, we fail to acquire the lock */ |
| 109 | if (rw->counter < 0) |
| 110 | return 0; |
| 111 | |
| 112 | /* Wait until we have a realistic chance at the lock */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 113 | while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0) |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 114 | cpu_relax(); |
| 115 | |
| 116 | goto retry; |
| 117 | } |
| 118 | |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 119 | /* Note that we have to ensure interrupts are disabled in case we're |
| 120 | * interrupted by some other code that wants to read_trylock() this lock */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 121 | static __inline__ void arch_write_lock(arch_rwlock_t *rw) |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 122 | { |
| 123 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | retry: |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 125 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 126 | arch_spin_lock_flags(&rw->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 128 | if (rw->counter != 0) { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 129 | arch_spin_unlock(&rw->lock); |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 130 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 132 | while (rw->counter != 0) |
| 133 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | goto retry; |
| 136 | } |
| 137 | |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 138 | rw->counter = -1; /* mark as write-locked */ |
| 139 | mb(); |
| 140 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 143 | static __inline__ void arch_write_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | rw->counter = 0; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 146 | arch_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 149 | /* Note that we have to ensure interrupts are disabled in case we're |
| 150 | * interrupted by some other code that wants to read_trylock() this lock */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 151 | static __inline__ int arch_write_trylock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 153 | unsigned long flags; |
| 154 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 156 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 157 | if (arch_spin_trylock(&rw->lock)) { |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 158 | if (rw->counter == 0) { |
| 159 | rw->counter = -1; |
| 160 | result = 1; |
| 161 | } else { |
| 162 | /* Read-locked. Oh well. */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 163 | arch_spin_unlock(&rw->lock); |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 166 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 168 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Kyle McMartin | bc8846c | 2006-03-24 21:22:02 -0700 | [diff] [blame] | 171 | /* |
| 172 | * read_can_lock - would read_trylock() succeed? |
| 173 | * @lock: the rwlock in question. |
| 174 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 175 | static __inline__ int arch_read_can_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Kyle McMartin | bc8846c | 2006-03-24 21:22:02 -0700 | [diff] [blame] | 177 | return rw->counter >= 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Kyle McMartin | bc8846c | 2006-03-24 21:22:02 -0700 | [diff] [blame] | 180 | /* |
| 181 | * write_can_lock - would write_trylock() succeed? |
| 182 | * @lock: the rwlock in question. |
| 183 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 184 | static __inline__ int arch_write_can_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Kyle McMartin | bc8846c | 2006-03-24 21:22:02 -0700 | [diff] [blame] | 186 | return !rw->counter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 189 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 190 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
Robin Holt | f5f7eac | 2009-04-02 16:59:46 -0700 | [diff] [blame] | 191 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 192 | #define arch_spin_relax(lock) cpu_relax() |
| 193 | #define arch_read_relax(lock) cpu_relax() |
| 194 | #define arch_write_relax(lock) cpu_relax() |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | #endif /* __ASM_SPINLOCK_H */ |