lock.h revision 1.31
1/* $NetBSD: lock.h,v 1.31 2021/04/03 14:56:13 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Machine-dependent spin lock operations.
35 */
36
37#ifndef _ALPHA_LOCK_H_
38#define	_ALPHA_LOCK_H_
39
40#ifdef _KERNEL_OPT
41#include "opt_multiprocessor.h"
42#endif
43
44static __inline int
45__SIMPLELOCK_LOCKED_P(const __cpu_simple_lock_t *__ptr)
46{
47	return *__ptr == __SIMPLELOCK_LOCKED;
48}
49
50static __inline int
51__SIMPLELOCK_UNLOCKED_P(const __cpu_simple_lock_t *__ptr)
52{
53	return *__ptr == __SIMPLELOCK_UNLOCKED;
54}
55
56static __inline void
57__cpu_simple_lock_clear(__cpu_simple_lock_t *__ptr)
58{
59	*__ptr = __SIMPLELOCK_UNLOCKED;
60}
61
62static __inline void
63__cpu_simple_lock_set(__cpu_simple_lock_t *__ptr)
64{
65	*__ptr = __SIMPLELOCK_LOCKED;
66}
67
68static __inline void
69__cpu_simple_lock_init(__cpu_simple_lock_t *alp)
70{
71
72	__asm volatile(
73		"# BEGIN __cpu_simple_lock_init\n"
74		"	stl	$31, %0		\n"
75		"	mb			\n"
76		"	# END __cpu_simple_lock_init"
77		: "=m" (*alp));
78}
79
80static __inline void
81__cpu_simple_lock(__cpu_simple_lock_t *alp)
82{
83	unsigned long t0;
84
85	/*
86	 * Note, if we detect that the lock is held when
87	 * we do the initial load-locked, we spin using
88	 * a non-locked load to save the coherency logic
89	 * some work.
90	 */
91
92	__asm volatile(
93		"# BEGIN __cpu_simple_lock\n"
94		"1:	ldl_l	%0, %3		\n"
95		"	bne	%0, 2f		\n"
96		"	bis	$31, %2, %0	\n"
97		"	stl_c	%0, %1		\n"
98		"	beq	%0, 3f		\n"
99		"	mb			\n"
100		"	br	4f		\n"
101		"2:	ldl	%0, %3		\n"
102		"	beq	%0, 1b		\n"
103		"	br	2b		\n"
104		"3:	br	1b		\n"
105		"4:				\n"
106		"	# END __cpu_simple_lock\n"
107		: "=&r" (t0), "=m" (*alp)
108		: "i" (__SIMPLELOCK_LOCKED), "m" (*alp)
109		: "memory");
110}
111
112static __inline int
113__cpu_simple_lock_try(__cpu_simple_lock_t *alp)
114{
115	unsigned long t0, v0;
116
117	__asm volatile(
118		"# BEGIN __cpu_simple_lock_try\n"
119		"1:	ldl_l	%0, %4		\n"
120		"	bne	%0, 2f		\n"
121		"	bis	$31, %3, %0	\n"
122		"	stl_c	%0, %2		\n"
123		"	beq	%0, 3f		\n"
124		"	mb			\n"
125		"	bis	$31, 1, %1	\n"
126		"	br	4f		\n"
127		"2:	bis	$31, $31, %1	\n"
128		"	br	4f		\n"
129		"3:	br	1b		\n"
130		"4:				\n"
131		"	# END __cpu_simple_lock_try"
132		: "=&r" (t0), "=r" (v0), "=m" (*alp)
133		: "i" (__SIMPLELOCK_LOCKED), "m" (*alp)
134		: "memory");
135
136	return (v0 != 0);
137}
138
139static __inline void
140__cpu_simple_unlock(__cpu_simple_lock_t *alp)
141{
142
143	__asm volatile(
144		"# BEGIN __cpu_simple_unlock\n"
145		"	mb			\n"
146		"	stl	$31, %0		\n"
147		"	# END __cpu_simple_unlock"
148		: "=m" (*alp));
149}
150
151#if defined(MULTIPROCESSOR)
152/*
153 * On the Alpha, interprocessor interrupts come in at device priority
154 * level (ALPHA_PSL_IPL_CLOCK).  This can cause some problems while
155 * waiting for spin locks from a high'ish priority level (like spin
156 * mutexes used by the scheduler): IPIs that come in will not be
157 * processed. This can lead to deadlock.
158 *
159 * This hook allows IPIs to be processed while spinning.  Note we only
160 * do the special thing if IPIs are blocked (current IPL >= IPL_CLOCK).
161 * IPIs will be processed in the normal fashion otherwise, and checking
162 * this way ensures that preemption is disabled (i.e. curcpu() is stable).
163 */
164#define	SPINLOCK_SPIN_HOOK						\
165do {									\
166	unsigned long _ipl_ = alpha_pal_rdps() & ALPHA_PSL_IPL_MASK;	\
167									\
168	if (_ipl_ >= ALPHA_PSL_IPL_CLOCK) {				\
169		struct cpu_info *__ci = curcpu();			\
170		if (atomic_load_relaxed(&__ci->ci_ipis) != 0) {		\
171			alpha_ipi_process(__ci, NULL);			\
172		}							\
173	}								\
174} while (/*CONSTCOND*/0)
175#define	SPINLOCK_BACKOFF_HOOK	(void)nullop((void *)0)
176#endif /* MULTIPROCESSOR */
177
178#endif /* _ALPHA_LOCK_H_ */
179