1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef __ASM_SPINLOCK_H
3 #define __ASM_SPINLOCK_H
7 * Simple spin lock operations.
9 * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
10 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
11 * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
12 * Rework to support virtual processors
14 * Type of int is used as a full 64b word is not necessary.
16 * (the type definitions are in asm/spinlock_types.h)
18 #include <linux/irqflags.h>
21 #include <asm/hvcall.h>
23 #include <asm/synch.h>
24 #include <asm/ppc-opcode.h>
25 #include <asm/asm-405.h>
28 /* use 0x800000yy when locked, where yy == CPU number */
30 #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
32 #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
38 #ifdef CONFIG_PPC_PSERIES
39 #define vcpu_is_preempted vcpu_is_preempted
40 static inline bool vcpu_is_preempted(int cpu)
42 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
44 return !!(be32_to_cpu(lppaca_of(cpu).yield_count) & 1);
48 static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
50 return lock.slock == 0;
53 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
56 return !arch_spin_value_unlocked(*lock);
60 * This returns the old value in the lock, so we succeeded
61 * in getting the lock if the return value is 0.
63 static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
65 unsigned long tmp, token;
69 "1: " PPC_LWARX(%0,0,%2,1) "\n\
77 : "r" (token), "r" (&lock->slock)
83 static inline int arch_spin_trylock(arch_spinlock_t *lock)
85 return __arch_spin_trylock(lock) == 0;
89 * On a system with shared processors (that is, where a physical
90 * processor is multiplexed between several virtual processors),
91 * there is no point spinning on a lock if the holder of the lock
92 * isn't currently scheduled on a physical processor. Instead
93 * we detect this situation and ask the hypervisor to give the
94 * rest of our timeslice to the lock holder.
96 * So that we can tell which virtual processor is holding a lock,
97 * we put 0x80000000 | smp_processor_id() in the lock when it is
98 * held. Conveniently, we have a word in the paca that holds this
102 #if defined(CONFIG_PPC_SPLPAR)
103 /* We only yield to the hypervisor if we are in shared processor mode */
104 void splpar_spin_yield(arch_spinlock_t *lock);
105 void splpar_rw_yield(arch_rwlock_t *lock);
107 static inline void splpar_spin_yield(arch_spinlock_t *lock) {};
108 static inline void splpar_rw_yield(arch_rwlock_t *lock) {};
111 static inline bool is_shared_processor(void)
114 * LPPACA is only available on Pseries so guard anything LPPACA related to
115 * allow other platforms (which include this common header) to compile.
117 #ifdef CONFIG_PPC_PSERIES
118 return (IS_ENABLED(CONFIG_PPC_SPLPAR) &&
119 lppaca_shared_proc(local_paca->lppaca_ptr));
125 static inline void spin_yield(arch_spinlock_t *lock)
127 if (is_shared_processor())
128 splpar_spin_yield(lock);
133 static inline void rw_yield(arch_rwlock_t *lock)
135 if (is_shared_processor())
136 splpar_rw_yield(lock);
141 static inline void arch_spin_lock(arch_spinlock_t *lock)
144 if (likely(__arch_spin_trylock(lock) == 0))
148 if (is_shared_processor())
149 splpar_spin_yield(lock);
150 } while (unlikely(lock->slock != 0));
156 void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
158 unsigned long flags_dis;
161 if (likely(__arch_spin_trylock(lock) == 0))
163 local_save_flags(flags_dis);
164 local_irq_restore(flags);
167 if (is_shared_processor())
168 splpar_spin_yield(lock);
169 } while (unlikely(lock->slock != 0));
171 local_irq_restore(flags_dis);
174 #define arch_spin_lock_flags arch_spin_lock_flags
176 static inline void arch_spin_unlock(arch_spinlock_t *lock)
178 __asm__ __volatile__("# arch_spin_unlock\n\t"
179 PPC_RELEASE_BARRIER: : :"memory");
184 * Read-write spinlocks, allowing multiple readers
185 * but only one writer.
187 * NOTE! it is quite common to have readers in interrupts
188 * but no interrupt writers. For those circumstances we
189 * can "mix" irq-safe locks - any writer needs to get a
190 * irq-safe write-lock, but readers can get non-irqsafe
195 #define __DO_SIGN_EXTEND "extsw %0,%0\n"
196 #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
198 #define __DO_SIGN_EXTEND
199 #define WRLOCK_TOKEN (-1)
203 * This returns the old value in the lock + 1,
204 * so we got a read lock if the return value is > 0.
206 static inline long __arch_read_trylock(arch_rwlock_t *rw)
210 __asm__ __volatile__(
211 "1: " PPC_LWARX(%0,0,%1,1) "\n"
221 : "cr0", "xer", "memory");
227 * This returns the old value in the lock,
228 * so we got the write lock if the return value is 0.
230 static inline long __arch_write_trylock(arch_rwlock_t *rw)
234 token = WRLOCK_TOKEN;
235 __asm__ __volatile__(
236 "1: " PPC_LWARX(%0,0,%2,1) "\n\
244 : "r" (token), "r" (&rw->lock)
250 static inline void arch_read_lock(arch_rwlock_t *rw)
253 if (likely(__arch_read_trylock(rw) > 0))
257 if (is_shared_processor())
259 } while (unlikely(rw->lock < 0));
264 static inline void arch_write_lock(arch_rwlock_t *rw)
267 if (likely(__arch_write_trylock(rw) == 0))
271 if (is_shared_processor())
273 } while (unlikely(rw->lock != 0));
278 static inline int arch_read_trylock(arch_rwlock_t *rw)
280 return __arch_read_trylock(rw) > 0;
283 static inline int arch_write_trylock(arch_rwlock_t *rw)
285 return __arch_write_trylock(rw) == 0;
288 static inline void arch_read_unlock(arch_rwlock_t *rw)
292 __asm__ __volatile__(
302 : "cr0", "xer", "memory");
305 static inline void arch_write_unlock(arch_rwlock_t *rw)
307 __asm__ __volatile__("# write_unlock\n\t"
308 PPC_RELEASE_BARRIER: : :"memory");
312 #define arch_spin_relax(lock) spin_yield(lock)
313 #define arch_read_relax(lock) rw_yield(lock)
314 #define arch_write_relax(lock) rw_yield(lock)
316 /* See include/linux/spinlock.h */
317 #define smp_mb__after_spinlock() smp_mb()
319 #endif /* __KERNEL__ */
320 #endif /* __ASM_SPINLOCK_H */