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