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/jump_label.h>
19 #include <linux/irqflags.h>
22 #include <asm/hvcall.h>
24 #include <asm/synch.h>
25 #include <asm/ppc-opcode.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 DECLARE_STATIC_KEY_FALSE(shared_processor);
41 #define vcpu_is_preempted vcpu_is_preempted
42 static inline bool vcpu_is_preempted(int cpu)
44 if (!static_branch_unlikely(&shared_processor))
46 return !!(be32_to_cpu(lppaca_of(cpu).yield_count) & 1);
50 static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
52 return lock.slock == 0;
55 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
58 return !arch_spin_value_unlocked(*lock);
62 * This returns the old value in the lock, so we succeeded
63 * in getting the lock if the return value is 0.
65 static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock)
67 unsigned long tmp, token;
71 "1: " PPC_LWARX(%0,0,%2,1) "\n\
79 : "r" (token), "r" (&lock->slock)
85 static inline int arch_spin_trylock(arch_spinlock_t *lock)
87 return __arch_spin_trylock(lock) == 0;
91 * On a system with shared processors (that is, where a physical
92 * processor is multiplexed between several virtual processors),
93 * there is no point spinning on a lock if the holder of the lock
94 * isn't currently scheduled on a physical processor. Instead
95 * we detect this situation and ask the hypervisor to give the
96 * rest of our timeslice to the lock holder.
98 * So that we can tell which virtual processor is holding a lock,
99 * we put 0x80000000 | smp_processor_id() in the lock when it is
100 * held. Conveniently, we have a word in the paca that holds this
104 #if defined(CONFIG_PPC_SPLPAR)
105 /* We only yield to the hypervisor if we are in shared processor mode */
106 void splpar_spin_yield(arch_spinlock_t *lock);
107 void splpar_rw_yield(arch_rwlock_t *lock);
109 static inline void splpar_spin_yield(arch_spinlock_t *lock) {};
110 static inline void splpar_rw_yield(arch_rwlock_t *lock) {};
113 static inline bool is_shared_processor(void)
115 #ifdef CONFIG_PPC_SPLPAR
116 return static_branch_unlikely(&shared_processor);
122 static inline void spin_yield(arch_spinlock_t *lock)
124 if (is_shared_processor())
125 splpar_spin_yield(lock);
130 static inline void rw_yield(arch_rwlock_t *lock)
132 if (is_shared_processor())
133 splpar_rw_yield(lock);
138 static inline void arch_spin_lock(arch_spinlock_t *lock)
141 if (likely(__arch_spin_trylock(lock) == 0))
145 if (is_shared_processor())
146 splpar_spin_yield(lock);
147 } while (unlikely(lock->slock != 0));
153 void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
155 unsigned long flags_dis;
158 if (likely(__arch_spin_trylock(lock) == 0))
160 local_save_flags(flags_dis);
161 local_irq_restore(flags);
164 if (is_shared_processor())
165 splpar_spin_yield(lock);
166 } while (unlikely(lock->slock != 0));
168 local_irq_restore(flags_dis);
171 #define arch_spin_lock_flags arch_spin_lock_flags
173 static inline void arch_spin_unlock(arch_spinlock_t *lock)
175 __asm__ __volatile__("# arch_spin_unlock\n\t"
176 PPC_RELEASE_BARRIER: : :"memory");
181 * Read-write spinlocks, allowing multiple readers
182 * but only one writer.
184 * NOTE! it is quite common to have readers in interrupts
185 * but no interrupt writers. For those circumstances we
186 * can "mix" irq-safe locks - any writer needs to get a
187 * irq-safe write-lock, but readers can get non-irqsafe
192 #define __DO_SIGN_EXTEND "extsw %0,%0\n"
193 #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
195 #define __DO_SIGN_EXTEND
196 #define WRLOCK_TOKEN (-1)
200 * This returns the old value in the lock + 1,
201 * so we got a read lock if the return value is > 0.
203 static inline long __arch_read_trylock(arch_rwlock_t *rw)
207 __asm__ __volatile__(
208 "1: " PPC_LWARX(%0,0,%1,1) "\n"
217 : "cr0", "xer", "memory");
223 * This returns the old value in the lock,
224 * so we got the write lock if the return value is 0.
226 static inline long __arch_write_trylock(arch_rwlock_t *rw)
230 token = WRLOCK_TOKEN;
231 __asm__ __volatile__(
232 "1: " PPC_LWARX(%0,0,%2,1) "\n\
239 : "r" (token), "r" (&rw->lock)
245 static inline void arch_read_lock(arch_rwlock_t *rw)
248 if (likely(__arch_read_trylock(rw) > 0))
252 if (is_shared_processor())
254 } while (unlikely(rw->lock < 0));
259 static inline void arch_write_lock(arch_rwlock_t *rw)
262 if (likely(__arch_write_trylock(rw) == 0))
266 if (is_shared_processor())
268 } while (unlikely(rw->lock != 0));
273 static inline int arch_read_trylock(arch_rwlock_t *rw)
275 return __arch_read_trylock(rw) > 0;
278 static inline int arch_write_trylock(arch_rwlock_t *rw)
280 return __arch_write_trylock(rw) == 0;
283 static inline void arch_read_unlock(arch_rwlock_t *rw)
287 __asm__ __volatile__(
296 : "cr0", "xer", "memory");
299 static inline void arch_write_unlock(arch_rwlock_t *rw)
301 __asm__ __volatile__("# write_unlock\n\t"
302 PPC_RELEASE_BARRIER: : :"memory");
306 #define arch_spin_relax(lock) spin_yield(lock)
307 #define arch_read_relax(lock) rw_yield(lock)
308 #define arch_write_relax(lock) rw_yield(lock)
310 /* See include/linux/spinlock.h */
311 #define smp_mb__after_spinlock() smp_mb()
313 #endif /* __KERNEL__ */
314 #endif /* __ASM_SPINLOCK_H */