xref: /netbsd-src/sys/arch/x86/x86/x86_softintr.c (revision ff4bde105648fdd295a51ae5a400c4e26627a7b1)
1 /*	$NetBSD: x86_softintr.c,v 1.4 2022/09/07 00:40:19 knakahara Exp $	*/
2 
3 /*
4  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran, and by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright 2002 (c) Wasabi Systems, Inc.
34  * All rights reserved.
35  *
36  * Written by Frank van der Linden for Wasabi Systems, Inc.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed for the NetBSD Project by
49  *      Wasabi Systems, Inc.
50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51  *    or promote products derived from this software without specific prior
52  *    written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 /*-
68  * Copyright (c) 1991 The Regents of the University of California.
69  * All rights reserved.
70  *
71  * This code is derived from software contributed to Berkeley by
72  * William Jolitz.
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions
76  * are met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  * 3. Neither the name of the University nor the names of its contributors
83  *    may be used to endorse or promote products derived from this software
84  *    without specific prior written permission.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
87  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
90  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
92  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
93  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
94  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
95  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96  * SUCH DAMAGE.
97  *
98  *	@(#)isa.c	7.2 (Berkeley) 5/13/91
99  */
100 
101 /*-
102  * Copyright (c) 1993, 1994 Charles Hannum.
103  *
104  * Redistribution and use in source and binary forms, with or without
105  * modification, are permitted provided that the following conditions
106  * are met:
107  * 1. Redistributions of source code must retain the above copyright
108  *    notice, this list of conditions and the following disclaimer.
109  * 2. Redistributions in binary form must reproduce the above copyright
110  *    notice, this list of conditions and the following disclaimer in the
111  *    documentation and/or other materials provided with the distribution.
112  * 3. All advertising materials mentioning features or use of this software
113  *    must display the following acknowledgement:
114  *	This product includes software developed by the University of
115  *	California, Berkeley and its contributors.
116  * 4. Neither the name of the University nor the names of its contributors
117  *    may be used to endorse or promote products derived from this software
118  *    without specific prior written permission.
119  *
120  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
121  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
122  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
123  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
124  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
125  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
126  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
127  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
128  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
129  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
130  * SUCH DAMAGE.
131  *
132  *	@(#)isa.c	7.2 (Berkeley) 5/13/91
133  */
134 
135 #include <sys/cdefs.h>
136 __KERNEL_RCSID(0, "$NetBSD: x86_softintr.c,v 1.4 2022/09/07 00:40:19 knakahara Exp $");
137 
138 #include <sys/kmem.h>
139 #include <sys/proc.h>
140 #include <sys/intr.h>
141 
142 struct pic softintr_pic = {
143 	.pic_name = "softintr_fakepic",
144 	.pic_type = PIC_SOFT,
145 	.pic_vecbase = 0,
146 	.pic_apicid = 0,
147 	.pic_lock = __SIMPLELOCK_UNLOCKED,
148 };
149 
150 /*
151  * Recalculate the interrupt masks from scratch.
152  * During early boot, anything goes and we are always called on the BP.
153  * When the system is up and running:
154  *
155  * => called with ci == curcpu()
156  * => cpu_lock held by the initiator
157  * => interrupts disabled on-chip (PSL_I)
158  *
159  * Do not call printf(), kmem_free() or other "heavyweight" routines
160  * from here.  This routine must be quick and must not block.
161  */
162 void
x86_intr_calculatemasks(struct cpu_info * ci)163 x86_intr_calculatemasks(struct cpu_info *ci)
164 {
165 	uint64_t unusedirqs, intrlevel[MAX_INTR_SOURCES];
166 	int irq, level;
167 	struct intrhand *q;
168 
169 	/* First, figure out which levels each IRQ uses. */
170 	unusedirqs = UINT64_MAX;
171 	for (irq = 0; irq < MAX_INTR_SOURCES; irq++) {
172 		int levels = 0;
173 
174 		if (ci->ci_isources[irq] == NULL) {
175 			intrlevel[irq] = 0;
176 			continue;
177 		}
178 		for (q = ci->ci_isources[irq]->is_handlers; q; q = q->ih_next)
179 			levels |= 1 << q->ih_level;
180 		intrlevel[irq] = levels;
181 		if (levels)
182 			unusedirqs &= ~(1ULL << irq);
183 	}
184 
185 	/* Then figure out which IRQs use each level. */
186 	for (level = 0; level < NIPL; level++) {
187 		uint64_t irqs = 0;
188 		for (irq = 0; irq < MAX_INTR_SOURCES; irq++)
189 			if (intrlevel[irq] & (1ULL << level))
190 				irqs |= 1ULL << irq;
191 		ci->ci_imask[level] = irqs | unusedirqs;
192 	}
193 
194 	for (level = 0; level<(NIPL-1); level++)
195 		ci->ci_imask[level+1] |= ci->ci_imask[level];
196 
197 	for (irq = 0; irq < MAX_INTR_SOURCES; irq++) {
198 		int maxlevel = IPL_NONE;
199 		int minlevel = IPL_HIGH;
200 
201 		if (ci->ci_isources[irq] == NULL)
202 			continue;
203 		for (q = ci->ci_isources[irq]->is_handlers; q;
204 		     q = q->ih_next) {
205 			if (q->ih_level < minlevel)
206 				minlevel = q->ih_level;
207 			if (q->ih_level > maxlevel)
208 				maxlevel = q->ih_level;
209 		}
210 		ci->ci_isources[irq]->is_maxlevel = maxlevel;
211 		ci->ci_isources[irq]->is_minlevel = minlevel;
212 	}
213 
214 	for (level = 0; level < NIPL; level++)
215 		ci->ci_iunmask[level] = ~ci->ci_imask[level];
216 }
217 
218 
219 
220 #if defined(__HAVE_PREEMPTION)
221 struct intrhand fake_preempt_intrhand;
222 
223 void
x86_init_preempt(struct cpu_info * ci)224 x86_init_preempt(struct cpu_info *ci)
225 {
226 	struct intrsource *isp;
227 
228 	isp = kmem_zalloc(sizeof(*isp), KM_SLEEP);
229 	isp->is_recurse = Xrecurse_preempt;
230 	isp->is_resume = Xresume_preempt;
231 	fake_preempt_intrhand.ih_pic = &softintr_pic;
232 	fake_preempt_intrhand.ih_level = IPL_PREEMPT;
233 	isp->is_handlers = &fake_preempt_intrhand;
234 	isp->is_pic = &softintr_pic;
235 	ci->ci_isources[SIR_PREEMPT] = isp;
236 }
237 #endif
238 
239 #if defined(__HAVE_FAST_SOFTINTS)
240 struct intrhand fake_softclock_intrhand;
241 struct intrhand fake_softnet_intrhand;
242 struct intrhand fake_softserial_intrhand;
243 struct intrhand fake_softbio_intrhand;
244 
245 void
softint_init_md(lwp_t * l,u_int level,uintptr_t * machdep)246 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
247 {
248 	struct intrsource *isp;
249 	struct cpu_info *ci;
250 	u_int sir;
251 
252 	ci = l->l_cpu;
253 
254 	isp = kmem_zalloc(sizeof(*isp), KM_SLEEP);
255 	isp->is_recurse = Xsoftintr;
256 	isp->is_resume = Xsoftintr;
257 	isp->is_pic = &softintr_pic;
258 
259 	switch (level) {
260 	case SOFTINT_BIO:
261 		sir = SIR_BIO;
262 		fake_softbio_intrhand.ih_pic = &softintr_pic;
263 		fake_softbio_intrhand.ih_level = IPL_SOFTBIO;
264 		isp->is_handlers = &fake_softbio_intrhand;
265 		break;
266 	case SOFTINT_NET:
267 		sir = SIR_NET;
268 		fake_softnet_intrhand.ih_pic = &softintr_pic;
269 		fake_softnet_intrhand.ih_level = IPL_SOFTNET;
270 		isp->is_handlers = &fake_softnet_intrhand;
271 		break;
272 	case SOFTINT_SERIAL:
273 		sir = SIR_SERIAL;
274 		fake_softserial_intrhand.ih_pic = &softintr_pic;
275 		fake_softserial_intrhand.ih_level = IPL_SOFTSERIAL;
276 		isp->is_handlers = &fake_softserial_intrhand;
277 		break;
278 	case SOFTINT_CLOCK:
279 		sir = SIR_CLOCK;
280 		fake_softclock_intrhand.ih_pic = &softintr_pic;
281 		fake_softclock_intrhand.ih_level = IPL_SOFTCLOCK;
282 		isp->is_handlers = &fake_softclock_intrhand;
283 		break;
284 	default:
285 		panic("softint_init_md");
286 	}
287 
288 	KASSERT(ci->ci_isources[sir] == NULL);
289 
290 	*machdep = (1 << sir);
291 	ci->ci_isources[sir] = isp;
292 	ci->ci_isources[sir]->is_lwp = l;
293 
294 	x86_intr_calculatemasks(ci);
295 }
296 #endif /* __HAVE_FAST_SOFTINTS */
297