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