1/*	$NetBSD: lock.h,v 1.28 2008/02/23 05:48:13 matt Exp $	*/
2
3/*
4 * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *     This product includes software developed at Ludd, University of Lule}.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef _VAX_LOCK_H_
34#define _VAX_LOCK_H_
35
36#include <sys/param.h>
37
38#ifdef _KERNEL
39#ifdef _KERNEL_OPT
40#include "opt_multiprocessor.h"
41#include <machine/intr.h>
42#endif
43#include <machine/cpu.h>
44#endif
45
46static __inline int
47__SIMPLELOCK_LOCKED_P(__cpu_simple_lock_t *__ptr)
48{
49	return *__ptr == __SIMPLELOCK_LOCKED;
50}
51
52static __inline int
53__SIMPLELOCK_UNLOCKED_P(__cpu_simple_lock_t *__ptr)
54{
55	return *__ptr == __SIMPLELOCK_UNLOCKED;
56}
57
58static __inline void
59__cpu_simple_lock_clear(__cpu_simple_lock_t *__ptr)
60{
61	*__ptr = __SIMPLELOCK_UNLOCKED;
62}
63
64static __inline void
65__cpu_simple_lock_set(__cpu_simple_lock_t *__ptr)
66{
67	*__ptr = __SIMPLELOCK_LOCKED;
68}
69
70static __inline void __cpu_simple_lock_init(__cpu_simple_lock_t *);
71static __inline void
72__cpu_simple_lock_init(__cpu_simple_lock_t *__alp)
73{
74#ifdef _HARDKERNEL
75	__asm __volatile ("movl %0,%%r1;jsb Sunlock"
76		: /* No output */
77		: "g"(__alp)
78		: "r1","cc","memory");
79#else
80	__asm __volatile ("bbcci $0,%0,1f;1:"
81		: /* No output */
82		: "m"(*__alp)
83		: "cc");
84#endif
85}
86
87static __inline int __cpu_simple_lock_try(__cpu_simple_lock_t *);
88static __inline int
89__cpu_simple_lock_try(__cpu_simple_lock_t *__alp)
90{
91	int ret;
92
93#ifdef _HARDKERNEL
94	__asm __volatile ("movl %1,%%r1;jsb Slocktry;movl %%r0,%0"
95		: "=&r"(ret)
96		: "g"(__alp)
97		: "r0","r1","cc","memory");
98#else
99	__asm __volatile ("clrl %0;bbssi $0,%1,1f;incl %0;1:"
100		: "=&r"(ret)
101		: "m"(*__alp)
102		: "cc");
103#endif
104
105	return ret;
106}
107
108static __inline void __cpu_simple_lock(__cpu_simple_lock_t *);
109static __inline void
110__cpu_simple_lock(__cpu_simple_lock_t *__alp)
111{
112#if defined(_HARDKERNEL) && defined(MULTIPROCESSOR)
113	struct cpu_info * const __ci = curcpu();
114
115	while (__cpu_simple_lock_try(__alp) == 0) {
116#define	VAX_LOCK_CHECKS ((1 << IPI_SEND_CNCHAR) | (1 << IPI_DDB))
117		if (__ci->ci_ipimsgs & VAX_LOCK_CHECKS) {
118			cpu_handle_ipi();
119		}
120	}
121#else /* _HARDKERNEL && MULTIPROCESSOR */
122	__asm __volatile ("1:bbssi $0,%0,1b"
123		: /* No outputs */
124		: "m"(*__alp)
125		: "cc");
126#endif /* _HARDKERNEL && MULTIPROCESSOR */
127}
128
129static __inline void __cpu_simple_unlock(__cpu_simple_lock_t *);
130static __inline void
131__cpu_simple_unlock(__cpu_simple_lock_t *__alp)
132{
133#ifdef _HARDKERNEL
134	__asm __volatile ("movl %0,%%r1;jsb Sunlock"
135		: /* No output */
136		: "g"(__alp)
137		: "r1","cc","memory");
138#else
139	__asm __volatile ("bbcci $0,%0,1f;1:"
140		: /* No output */
141		: "m"(*__alp)
142		: "cc");
143#endif
144}
145
146#if defined(MULTIPROCESSOR)
147/*
148 * On the Vax, interprocessor interrupts can come in at device priority
149 * level or lower. This can cause some problems while waiting for r/w
150 * spinlocks from a high'ish priority level: IPIs that come in will not
151 * be processed. This can lead to deadlock.
152 *
153 * This hook allows IPIs to be processed while a spinlock's interlock
154 * is released.
155 */
156#define SPINLOCK_SPIN_HOOK						\
157do {									\
158	struct cpu_info * const __ci = curcpu();			\
159									\
160	if (__ci->ci_ipimsgs != 0) {					\
161		/* printf("CPU %lu has IPIs pending\n",			\
162		    __ci->ci_cpuid); */					\
163		cpu_handle_ipi();					\
164	}								\
165} while (/*CONSTCOND*/0)
166#endif /* MULTIPROCESSOR */
167
168static __inline void mb_read(void);
169static __inline void
170mb_read(void)
171{
172}
173
174static __inline void mb_write(void);
175static __inline void
176mb_write(void)
177{
178}
179#endif /* _VAX_LOCK_H_ */
180