^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Out of line spinlock code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 2004, 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/alternative.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int spin_retry = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int __init spin_retry_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (spin_retry < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) spin_retry = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) early_initcall(spin_retry_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * spin_retry= parameter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int __init spin_retry_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) spin_retry = simple_strtoul(str, &str, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __setup("spin_retry=", spin_retry_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct spin_wait {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct spin_wait *next, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) } __aligned(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static DEFINE_PER_CPU_ALIGNED(struct spin_wait, spin_wait[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define _Q_LOCK_CPU_OFFSET 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define _Q_LOCK_STEAL_OFFSET 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define _Q_TAIL_IDX_OFFSET 18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define _Q_TAIL_CPU_OFFSET 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define _Q_LOCK_CPU_MASK 0x0000ffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define _Q_LOCK_STEAL_ADD 0x00010000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define _Q_LOCK_STEAL_MASK 0x00030000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define _Q_TAIL_IDX_MASK 0x000c0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define _Q_TAIL_CPU_MASK 0xfff00000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define _Q_LOCK_MASK (_Q_LOCK_CPU_MASK | _Q_LOCK_STEAL_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void arch_spin_lock_setup(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct spin_wait *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) node = per_cpu_ptr(&spin_wait[0], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) for (ix = 0; ix < 4; ix++, node++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) memset(node, 0, sizeof(*node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) node->node_id = ((cpu + 1) << _Q_TAIL_CPU_OFFSET) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (ix << _Q_TAIL_IDX_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static inline int arch_load_niai4(int *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) asm_inline volatile(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ALTERNATIVE("", ".long 0xb2fa0040", 49) /* NIAI 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) " l %0,%1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) : "=d" (owner) : "Q" (*lock) : "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static inline int arch_cmpxchg_niai8(int *lock, int old, int new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int expected = old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) asm_inline volatile(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ALTERNATIVE("", ".long 0xb2fa0080", 49) /* NIAI 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) " cs %0,%3,%1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) : "=d" (old), "=Q" (*lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) : "0" (old), "d" (new), "Q" (*lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) : "cc", "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return expected == old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline struct spin_wait *arch_spin_decode_tail(int lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int ix, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ix = (lock & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cpu = (lock & _Q_TAIL_CPU_MASK) >> _Q_TAIL_CPU_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return per_cpu_ptr(&spin_wait[ix], cpu - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline int arch_spin_yield_target(int lock, struct spin_wait *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (lock & _Q_LOCK_CPU_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return lock & _Q_LOCK_CPU_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (node == NULL || node->prev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0; /* 0 -> no target cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) while (node->prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) node = node->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return node->node_id >> _Q_TAIL_CPU_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline void arch_spin_lock_queued(arch_spinlock_t *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct spin_wait *node, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int lockval, ix, node_id, tail_id, old, new, owner, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ix = S390_lowcore.spinlock_index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) lockval = SPINLOCK_LOCKVAL; /* cpu + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) node = this_cpu_ptr(&spin_wait[ix]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) node->prev = node->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) node_id = node->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Enqueue the node for this CPU in the spinlock wait queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) old = READ_ONCE(lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if ((old & _Q_LOCK_CPU_MASK) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) (old & _Q_LOCK_STEAL_MASK) != _Q_LOCK_STEAL_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * The lock is free but there may be waiters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * With no waiters simply take the lock, if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * are waiters try to steal the lock. The lock may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * be stolen three times before the next queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * waiter will get the lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) new = (old ? (old + _Q_LOCK_STEAL_ADD) : 0) | lockval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (__atomic_cmpxchg_bool(&lp->lock, old, new))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Got the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* lock passing in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Make the node of this CPU the new tail. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) new = node_id | (old & _Q_LOCK_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (__atomic_cmpxchg_bool(&lp->lock, old, new))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Set the 'next' pointer of the tail node in the queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) tail_id = old & _Q_TAIL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (tail_id != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) node->prev = arch_spin_decode_tail(tail_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) WRITE_ONCE(node->prev->next, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* Pass the virtual CPU to the lock holder if it is not running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) owner = arch_spin_yield_target(old, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (owner && arch_vcpu_is_preempted(owner - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) smp_yield_cpu(owner - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* Spin on the CPU local node->prev pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (tail_id != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) while (READ_ONCE(node->prev) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (count-- >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* Query running state of lock holder again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) owner = arch_spin_yield_target(old, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (owner && arch_vcpu_is_preempted(owner - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) smp_yield_cpu(owner - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Spin on the lock value in the spinlock_t */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) old = READ_ONCE(lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) owner = old & _Q_LOCK_CPU_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) tail_id = old & _Q_TAIL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) new = ((tail_id != node_id) ? tail_id : 0) | lockval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (__atomic_cmpxchg_bool(&lp->lock, old, new))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Got the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (count-- >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(owner - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) smp_yield_cpu(owner - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* Pass lock_spin job to next CPU in the queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (node_id && tail_id != node_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* Wait until the next CPU has set up the 'next' pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) while ((next = READ_ONCE(node->next)) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) next->prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) S390_lowcore.spinlock_index--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static inline void arch_spin_lock_classic(arch_spinlock_t *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int lockval, old, new, owner, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) lockval = SPINLOCK_LOCKVAL; /* cpu + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Pass the virtual CPU to the lock holder if it is not running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) owner = arch_spin_yield_target(READ_ONCE(lp->lock), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (owner && arch_vcpu_is_preempted(owner - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) smp_yield_cpu(owner - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) old = arch_load_niai4(&lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) owner = old & _Q_LOCK_CPU_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* Try to get the lock if it is free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) new = (old & _Q_TAIL_MASK) | lockval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (arch_cmpxchg_niai8(&lp->lock, old, new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* Got the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (count-- >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) count = spin_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(owner - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) smp_yield_cpu(owner - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) void arch_spin_lock_wait(arch_spinlock_t *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (test_cpu_flag(CIF_DEDICATED_CPU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) arch_spin_lock_queued(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) arch_spin_lock_classic(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) EXPORT_SYMBOL(arch_spin_lock_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int arch_spin_trylock_retry(arch_spinlock_t *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int cpu = SPINLOCK_LOCKVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int owner, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) for (count = spin_retry; count > 0; count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) owner = READ_ONCE(lp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* Try to get the lock if it is free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (__atomic_cmpxchg_bool(&lp->lock, 0, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) EXPORT_SYMBOL(arch_spin_trylock_retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) void arch_read_lock_wait(arch_rwlock_t *rw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (unlikely(in_interrupt())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) while (READ_ONCE(rw->cnts) & 0x10000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* Remove this reader again to allow recursive read locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) __atomic_add_const(-1, &rw->cnts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Put the reader into the wait queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) arch_spin_lock(&rw->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Now add this reader to the count value again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) __atomic_add_const(1, &rw->cnts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* Loop until the writer is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) while (READ_ONCE(rw->cnts) & 0x10000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) arch_spin_unlock(&rw->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) EXPORT_SYMBOL(arch_read_lock_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) void arch_write_lock_wait(arch_rwlock_t *rw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Add this CPU to the write waiters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) __atomic_add(0x20000, &rw->cnts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Put the writer into the wait queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) arch_spin_lock(&rw->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) old = READ_ONCE(rw->cnts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if ((old & 0x1ffff) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) __atomic_cmpxchg_bool(&rw->cnts, old, old | 0x10000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* Got the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) arch_spin_unlock(&rw->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) EXPORT_SYMBOL(arch_write_lock_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) void arch_spin_relax(arch_spinlock_t *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) cpu = READ_ONCE(lp->lock) & _Q_LOCK_CPU_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (MACHINE_IS_LPAR && !arch_vcpu_is_preempted(cpu - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) smp_yield_cpu(cpu - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) EXPORT_SYMBOL(arch_spin_relax);