| Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef __ASM_SPINLOCK_H |
| 3 | #define __ASM_SPINLOCK_H |
| 4 | |
| Rolf Eike Beer | 1cab420 | 2012-05-10 22:57:11 +0200 | [diff] [blame] | 5 | #include <asm/barrier.h> |
| 6 | #include <asm/ldcw.h> |
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 7 | #include <asm/processor.h> |
| 8 | #include <asm/spinlock_types.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 10 | static inline int arch_spin_is_locked(arch_spinlock_t *x) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | { |
| 12 | volatile unsigned int *a = __ldcw_align(x); |
| 13 | return *a == 0; |
| 14 | } |
| 15 | |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 16 | #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0) |
| Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 17 | |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 18 | static inline void arch_spin_lock_flags(arch_spinlock_t *x, |
| James Bottomley | 08dc2ca | 2005-11-17 16:35:09 -0500 | [diff] [blame] | 19 | unsigned long flags) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
| 21 | volatile unsigned int *a; |
| 22 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| Will Deacon | a4c1887 | 2017-10-03 19:25:29 +0100 | [diff] [blame] | 33 | #define arch_spin_lock_flags arch_spin_lock_flags |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| John David Anglin | 3b885ac | 2018-08-12 16:31:17 -0400 | [diff] [blame] | 38 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | a = __ldcw_align(x); |
| John David Anglin | 86d4d06 | 2018-11-06 12:00:01 +0100 | [diff] [blame] | 40 | mb(); |
| 41 | *a = 1; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | a = __ldcw_align(x); |
| 50 | ret = __ldcw(a) != 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | return ret; |
| 53 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 56 | * Read-write spinlocks, allowing multiple readers but only one writer. |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 57 | * Linux rwlocks are unfair to writers; they can be starved for an indefinite |
| 58 | * time by readers. With care, they can also be taken in interrupt context. |
| 59 | * |
| 60 | * In the PA-RISC implementation, we have a spinlock and a counter. |
| 61 | * Readers use the lock to serialise their access to the counter (which |
| 62 | * records how many readers currently hold the lock). |
| 63 | * Writers hold the spinlock, preventing any readers or other writers from |
| 64 | * grabbing the rwlock. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 67 | /* Note that we have to ensure interrupts are disabled in case we're |
| 68 | * 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] | 69 | static __inline__ void arch_read_lock(arch_rwlock_t *rw) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 71 | unsigned long flags; |
| 72 | local_irq_save(flags); |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 73 | arch_spin_lock_flags(&rw->lock, flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | rw->counter++; |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 75 | arch_spin_unlock(&rw->lock); |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 76 | local_irq_restore(flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 79 | /* Note that we have to ensure interrupts are disabled in case we're |
| 80 | * 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] | 81 | static __inline__ void arch_read_unlock(arch_rwlock_t *rw) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 83 | unsigned long flags; |
| 84 | local_irq_save(flags); |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 85 | arch_spin_lock_flags(&rw->lock, flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | rw->counter--; |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 87 | arch_spin_unlock(&rw->lock); |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 88 | local_irq_restore(flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 91 | /* Note that we have to ensure interrupts are disabled in case we're |
| 92 | * 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] | 93 | static __inline__ int arch_read_trylock(arch_rwlock_t *rw) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 95 | unsigned long flags; |
| 96 | retry: |
| 97 | local_irq_save(flags); |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 98 | if (arch_spin_trylock(&rw->lock)) { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 99 | rw->counter++; |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 100 | arch_spin_unlock(&rw->lock); |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 101 | local_irq_restore(flags); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | local_irq_restore(flags); |
| 106 | /* If write-locked, we fail to acquire the lock */ |
| 107 | if (rw->counter < 0) |
| 108 | return 0; |
| 109 | |
| 110 | /* Wait until we have a realistic chance at the lock */ |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 111 | while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0) |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 112 | cpu_relax(); |
| 113 | |
| 114 | goto retry; |
| 115 | } |
| 116 | |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 117 | /* Note that we have to ensure interrupts are disabled in case we're |
| 118 | * interrupted by some other code that wants to read_trylock() this lock */ |
| Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 119 | static __inline__ void arch_write_lock(arch_rwlock_t *rw) |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 120 | { |
| 121 | unsigned long flags; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | retry: |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 123 | local_irq_save(flags); |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 124 | arch_spin_lock_flags(&rw->lock, flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 126 | if (rw->counter != 0) { |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 127 | arch_spin_unlock(&rw->lock); |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 128 | local_irq_restore(flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 130 | while (rw->counter != 0) |
| 131 | cpu_relax(); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | goto retry; |
| 134 | } |
| 135 | |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 136 | rw->counter = -1; /* mark as write-locked */ |
| 137 | mb(); |
| 138 | local_irq_restore(flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 141 | static __inline__ void arch_write_unlock(arch_rwlock_t *rw) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | rw->counter = 0; |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 144 | arch_spin_unlock(&rw->lock); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| Matthew Wilcox | 65ee8f0a | 2006-09-08 05:43:44 -0600 | [diff] [blame] | 147 | /* Note that we have to ensure interrupts are disabled in case we're |
| 148 | * interrupted by some other code that wants to read_trylock() this lock */ |
| Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 149 | static __inline__ int arch_write_trylock(arch_rwlock_t *rw) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 151 | unsigned long flags; |
| 152 | int result = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 154 | local_irq_save(flags); |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 155 | if (arch_spin_trylock(&rw->lock)) { |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 156 | if (rw->counter == 0) { |
| 157 | rw->counter = -1; |
| 158 | result = 1; |
| 159 | } else { |
| 160 | /* Read-locked. Oh well. */ |
| Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 161 | arch_spin_unlock(&rw->lock); |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 162 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| Matthew Wilcox | 6e07185 | 2006-09-02 07:54:58 -0600 | [diff] [blame] | 164 | local_irq_restore(flags); |
| 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 | return result; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | #endif /* __ASM_SPINLOCK_H */ |