1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_SIMPLE_SPINLOCK_H
3 #define _ASM_POWERPC_SIMPLE_SPINLOCK_H
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 <linux/kcsan-checks.h>
19 #include <asm/paravirt.h>
21 #include <asm/synch.h>
22 #include <asm/ppc-opcode.h>
25 /* use 0x800000yy when locked, where yy == CPU number */
27 #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
29 #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
35 static __always_inline
int arch_spin_value_unlocked(arch_spinlock_t lock
)
37 return lock
.slock
== 0;
40 static inline int arch_spin_is_locked(arch_spinlock_t
*lock
)
42 return !arch_spin_value_unlocked(READ_ONCE(*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
;
52 unsigned int eh
= IS_ENABLED(CONFIG_PPC64
);
56 "1: lwarx %0,0,%2,%[eh]\n\
64 : "r" (token
), "r" (&lock
->slock
), [eh
] "n" (eh
)
70 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
72 return __arch_spin_trylock(lock
) == 0;
76 * On a system with shared processors (that is, where a physical
77 * processor is multiplexed between several virtual processors),
78 * there is no point spinning on a lock if the holder of the lock
79 * isn't currently scheduled on a physical processor. Instead
80 * we detect this situation and ask the hypervisor to give the
81 * rest of our timeslice to the lock holder.
83 * So that we can tell which virtual processor is holding a lock,
84 * we put 0x80000000 | smp_processor_id() in the lock when it is
85 * held. Conveniently, we have a word in the paca that holds this
89 #if defined(CONFIG_PPC_SPLPAR)
90 /* We only yield to the hypervisor if we are in shared processor mode */
91 void splpar_spin_yield(arch_spinlock_t
*lock
);
92 void splpar_rw_yield(arch_rwlock_t
*lock
);
94 static inline void splpar_spin_yield(arch_spinlock_t
*lock
) {}
95 static inline void splpar_rw_yield(arch_rwlock_t
*lock
) {}
98 static inline void spin_yield(arch_spinlock_t
*lock
)
100 if (is_shared_processor())
101 splpar_spin_yield(lock
);
106 static inline void rw_yield(arch_rwlock_t
*lock
)
108 if (is_shared_processor())
109 splpar_rw_yield(lock
);
114 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
117 if (likely(__arch_spin_trylock(lock
) == 0))
121 if (is_shared_processor())
122 splpar_spin_yield(lock
);
123 } while (unlikely(lock
->slock
!= 0));
128 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
131 __asm__
__volatile__("# arch_spin_unlock\n\t"
132 PPC_RELEASE_BARRIER
: : :"memory");
137 * Read-write spinlocks, allowing multiple readers
138 * but only one writer.
140 * NOTE! it is quite common to have readers in interrupts
141 * but no interrupt writers. For those circumstances we
142 * can "mix" irq-safe locks - any writer needs to get a
143 * irq-safe write-lock, but readers can get non-irqsafe
148 #define __DO_SIGN_EXTEND "extsw %0,%0\n"
149 #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */
151 #define __DO_SIGN_EXTEND
152 #define WRLOCK_TOKEN (-1)
156 * This returns the old value in the lock + 1,
157 * so we got a read lock if the return value is > 0.
159 static inline long __arch_read_trylock(arch_rwlock_t
*rw
)
162 unsigned int eh
= IS_ENABLED(CONFIG_PPC64
);
164 __asm__
__volatile__(
165 "1: lwarx %0,0,%1,%[eh]\n"
173 : "r" (&rw
->lock
), [eh
] "n" (eh
)
174 : "cr0", "xer", "memory");
180 * This returns the old value in the lock,
181 * so we got the write lock if the return value is 0.
183 static inline long __arch_write_trylock(arch_rwlock_t
*rw
)
186 unsigned int eh
= IS_ENABLED(CONFIG_PPC64
);
188 token
= WRLOCK_TOKEN
;
189 __asm__
__volatile__(
190 "1: lwarx %0,0,%2,%[eh]\n\
197 : "r" (token
), "r" (&rw
->lock
), [eh
] "n" (eh
)
203 static inline void arch_read_lock(arch_rwlock_t
*rw
)
206 if (likely(__arch_read_trylock(rw
) > 0))
210 if (is_shared_processor())
212 } while (unlikely(rw
->lock
< 0));
217 static inline void arch_write_lock(arch_rwlock_t
*rw
)
220 if (likely(__arch_write_trylock(rw
) == 0))
224 if (is_shared_processor())
226 } while (unlikely(rw
->lock
!= 0));
231 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
233 return __arch_read_trylock(rw
) > 0;
236 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
238 return __arch_write_trylock(rw
) == 0;
241 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
245 __asm__
__volatile__(
254 : "cr0", "xer", "memory");
257 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
259 __asm__
__volatile__("# write_unlock\n\t"
260 PPC_RELEASE_BARRIER
: : :"memory");
264 #define arch_spin_relax(lock) spin_yield(lock)
265 #define arch_read_relax(lock) rw_yield(lock)
266 #define arch_write_relax(lock) rw_yield(lock)
268 #endif /* _ASM_POWERPC_SIMPLE_SPINLOCK_H */