WIP FPC-III support
[linux/fpc-iii.git] / arch / powerpc / include / asm / simple_spinlock.h
blob9c3c305343331f7455fae7767c5a77aef4530dfb
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_SIMPLE_SPINLOCK_H
3 #define _ASM_POWERPC_SIMPLE_SPINLOCK_H
5 /*
6 * Simple spin lock operations.
8 * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
11 * Rework to support virtual processors
13 * Type of int is used as a full 64b word is not necessary.
15 * (the type definitions are in asm/simple_spinlock_types.h)
17 #include <linux/irqflags.h>
18 #include <asm/paravirt.h>
19 #include <asm/paca.h>
20 #include <asm/synch.h>
21 #include <asm/ppc-opcode.h>
23 #ifdef CONFIG_PPC64
24 /* use 0x800000yy when locked, where yy == CPU number */
25 #ifdef __BIG_ENDIAN__
26 #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
27 #else
28 #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
29 #endif
30 #else
31 #define LOCK_TOKEN 1
32 #endif
34 static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
36 return lock.slock == 0;
39 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
41 smp_mb();
42 return !arch_spin_value_unlocked(*lock);
46 * This returns the old value in the lock, so we succeeded
47 * in getting the lock if the return value is 0.
49 static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
51 unsigned long tmp, token;
53 token = LOCK_TOKEN;
54 __asm__ __volatile__(
55 "1: " PPC_LWARX(%0,0,%2,1) "\n\
56 cmpwi 0,%0,0\n\
57 bne- 2f\n\
58 stwcx. %1,0,%2\n\
59 bne- 1b\n"
60 PPC_ACQUIRE_BARRIER
61 "2:"
62 : "=&r" (tmp)
63 : "r" (token), "r" (&lock->slock)
64 : "cr0", "memory");
66 return tmp;
69 static inline int arch_spin_trylock(arch_spinlock_t *lock)
71 return __arch_spin_trylock(lock) == 0;
75 * On a system with shared processors (that is, where a physical
76 * processor is multiplexed between several virtual processors),
77 * there is no point spinning on a lock if the holder of the lock
78 * isn't currently scheduled on a physical processor. Instead
79 * we detect this situation and ask the hypervisor to give the
80 * rest of our timeslice to the lock holder.
82 * So that we can tell which virtual processor is holding a lock,
83 * we put 0x80000000 | smp_processor_id() in the lock when it is
84 * held. Conveniently, we have a word in the paca that holds this
85 * value.
88 #if defined(CONFIG_PPC_SPLPAR)
89 /* We only yield to the hypervisor if we are in shared processor mode */
90 void splpar_spin_yield(arch_spinlock_t *lock);
91 void splpar_rw_yield(arch_rwlock_t *lock);
92 #else /* SPLPAR */
93 static inline void splpar_spin_yield(arch_spinlock_t *lock) {};
94 static inline void splpar_rw_yield(arch_rwlock_t *lock) {};
95 #endif
97 static inline void spin_yield(arch_spinlock_t *lock)
99 if (is_shared_processor())
100 splpar_spin_yield(lock);
101 else
102 barrier();
105 static inline void rw_yield(arch_rwlock_t *lock)
107 if (is_shared_processor())
108 splpar_rw_yield(lock);
109 else
110 barrier();
113 static inline void arch_spin_lock(arch_spinlock_t *lock)
115 while (1) {
116 if (likely(__arch_spin_trylock(lock) == 0))
117 break;
118 do {
119 HMT_low();
120 if (is_shared_processor())
121 splpar_spin_yield(lock);
122 } while (unlikely(lock->slock != 0));
123 HMT_medium();
127 static inline
128 void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
130 unsigned long flags_dis;
132 while (1) {
133 if (likely(__arch_spin_trylock(lock) == 0))
134 break;
135 local_save_flags(flags_dis);
136 local_irq_restore(flags);
137 do {
138 HMT_low();
139 if (is_shared_processor())
140 splpar_spin_yield(lock);
141 } while (unlikely(lock->slock != 0));
142 HMT_medium();
143 local_irq_restore(flags_dis);
146 #define arch_spin_lock_flags arch_spin_lock_flags
148 static inline void arch_spin_unlock(arch_spinlock_t *lock)
150 __asm__ __volatile__("# arch_spin_unlock\n\t"
151 PPC_RELEASE_BARRIER: : :"memory");
152 lock->slock = 0;
156 * Read-write spinlocks, allowing multiple readers
157 * but only one writer.
159 * NOTE! it is quite common to have readers in interrupts
160 * but no interrupt writers. For those circumstances we
161 * can "mix" irq-safe locks - any writer needs to get a
162 * irq-safe write-lock, but readers can get non-irqsafe
163 * read-locks.
166 #ifdef CONFIG_PPC64
167 #define __DO_SIGN_EXTEND "extsw %0,%0\n"
168 #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
169 #else
170 #define __DO_SIGN_EXTEND
171 #define WRLOCK_TOKEN (-1)
172 #endif
175 * This returns the old value in the lock + 1,
176 * so we got a read lock if the return value is > 0.
178 static inline long __arch_read_trylock(arch_rwlock_t *rw)
180 long tmp;
182 __asm__ __volatile__(
183 "1: " PPC_LWARX(%0,0,%1,1) "\n"
184 __DO_SIGN_EXTEND
185 " addic. %0,%0,1\n\
186 ble- 2f\n"
187 " stwcx. %0,0,%1\n\
188 bne- 1b\n"
189 PPC_ACQUIRE_BARRIER
190 "2:" : "=&r" (tmp)
191 : "r" (&rw->lock)
192 : "cr0", "xer", "memory");
194 return tmp;
198 * This returns the old value in the lock,
199 * so we got the write lock if the return value is 0.
201 static inline long __arch_write_trylock(arch_rwlock_t *rw)
203 long tmp, token;
205 token = WRLOCK_TOKEN;
206 __asm__ __volatile__(
207 "1: " PPC_LWARX(%0,0,%2,1) "\n\
208 cmpwi 0,%0,0\n\
209 bne- 2f\n"
210 " stwcx. %1,0,%2\n\
211 bne- 1b\n"
212 PPC_ACQUIRE_BARRIER
213 "2:" : "=&r" (tmp)
214 : "r" (token), "r" (&rw->lock)
215 : "cr0", "memory");
217 return tmp;
220 static inline void arch_read_lock(arch_rwlock_t *rw)
222 while (1) {
223 if (likely(__arch_read_trylock(rw) > 0))
224 break;
225 do {
226 HMT_low();
227 if (is_shared_processor())
228 splpar_rw_yield(rw);
229 } while (unlikely(rw->lock < 0));
230 HMT_medium();
234 static inline void arch_write_lock(arch_rwlock_t *rw)
236 while (1) {
237 if (likely(__arch_write_trylock(rw) == 0))
238 break;
239 do {
240 HMT_low();
241 if (is_shared_processor())
242 splpar_rw_yield(rw);
243 } while (unlikely(rw->lock != 0));
244 HMT_medium();
248 static inline int arch_read_trylock(arch_rwlock_t *rw)
250 return __arch_read_trylock(rw) > 0;
253 static inline int arch_write_trylock(arch_rwlock_t *rw)
255 return __arch_write_trylock(rw) == 0;
258 static inline void arch_read_unlock(arch_rwlock_t *rw)
260 long tmp;
262 __asm__ __volatile__(
263 "# read_unlock\n\t"
264 PPC_RELEASE_BARRIER
265 "1: lwarx %0,0,%1\n\
266 addic %0,%0,-1\n"
267 " stwcx. %0,0,%1\n\
268 bne- 1b"
269 : "=&r"(tmp)
270 : "r"(&rw->lock)
271 : "cr0", "xer", "memory");
274 static inline void arch_write_unlock(arch_rwlock_t *rw)
276 __asm__ __volatile__("# write_unlock\n\t"
277 PPC_RELEASE_BARRIER: : :"memory");
278 rw->lock = 0;
281 #define arch_spin_relax(lock) spin_yield(lock)
282 #define arch_read_relax(lock) rw_yield(lock)
283 #define arch_write_relax(lock) rw_yield(lock)
285 /* See include/linux/spinlock.h */
286 #define smp_mb__after_spinlock() smp_mb()
288 #endif /* _ASM_POWERPC_SIMPLE_SPINLOCK_H */