intr.h revision 1.31 1 1.31 fvdl /* $NetBSD: intr.h,v 1.31 2002/11/22 21:21:14 fvdl Exp $ */
2 1.1 mycroft
3 1.9 mycroft /*-
4 1.19 thorpej * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 1.9 mycroft * All rights reserved.
6 1.9 mycroft *
7 1.9 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.19 thorpej * by Charles M. Hannum, and by Jason R. Thorpe.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.1 mycroft * must display the following acknowledgement:
20 1.9 mycroft * This product includes software developed by the NetBSD
21 1.9 mycroft * Foundation, Inc. and its contributors.
22 1.9 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.9 mycroft * contributors may be used to endorse or promote products derived
24 1.9 mycroft * from this software without specific prior written permission.
25 1.1 mycroft *
26 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.9 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.9 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.9 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.9 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.9 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.9 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.9 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.9 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.9 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.9 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mycroft */
38 1.1 mycroft
39 1.4 mycroft #ifndef _I386_INTR_H_
40 1.4 mycroft #define _I386_INTR_H_
41 1.4 mycroft
42 1.30 fvdl #include <machine/intrdefs.h>
43 1.30 fvdl
44 1.30 fvdl #ifndef _LOCORE
45 1.30 fvdl #include <machine/cpu.h>
46 1.30 fvdl #include <machine/pic.h>
47 1.30 fvdl
48 1.26 fvdl /*
49 1.30 fvdl * Struct describing an interrupt source for a CPU. struct cpu_info
50 1.30 fvdl * has an array of MAX_INTR_SOURCES of these. The index in the array
51 1.30 fvdl * is equal to the stub number of the stubcode as present in vector.s
52 1.30 fvdl *
53 1.30 fvdl * The primary CPU's array of interrupt sources has its first 16
54 1.30 fvdl * entries reserved for legacy ISA irq handlers. This means that
55 1.30 fvdl * they have a 1:1 mapping for arrayindex:irq_num. This is not
56 1.30 fvdl * true for interrupts that come in through IO APICs, to find
57 1.30 fvdl * their source, go through ci->ci_isources[index].is_pic
58 1.30 fvdl *
59 1.30 fvdl * It's possible to always maintain a 1:1 mapping, but that means
60 1.30 fvdl * limiting the total number of interrupt sources to MAX_INTR_SOURCES
61 1.30 fvdl * (32), instead of 32 per CPU. It also would mean that having multiple
62 1.30 fvdl * IO APICs which deliver interrupts from an equal pin number would
63 1.30 fvdl * overlap if they were to be sent to the same CPU.
64 1.30 fvdl */
65 1.30 fvdl
66 1.30 fvdl struct intrstub {
67 1.30 fvdl void *ist_entry;
68 1.30 fvdl void *ist_recurse;
69 1.30 fvdl void *ist_resume;
70 1.30 fvdl };
71 1.13 mycroft
72 1.30 fvdl struct intrsource {
73 1.30 fvdl int is_maxlevel; /* max. IPL for this source */
74 1.30 fvdl int is_pin; /* IRQ for legacy; pin for IO APIC */
75 1.30 fvdl struct intrhand *is_handlers; /* handler chain */
76 1.30 fvdl struct pic *is_pic; /* originating PIC */
77 1.30 fvdl void *is_recurse; /* entry for spllower */
78 1.30 fvdl void *is_resume; /* entry for doreti */
79 1.30 fvdl struct evcnt is_evcnt; /* interrupt counter */
80 1.30 fvdl char is_evname[32]; /* event counter name */
81 1.30 fvdl int is_flags; /* see below */
82 1.30 fvdl int is_type; /* level, edge */
83 1.30 fvdl int is_idtvec;
84 1.30 fvdl int is_minlevel;
85 1.30 fvdl };
86 1.30 fvdl
87 1.30 fvdl #define IS_LEGACY 0x0001 /* legacy ISA irq source */
88 1.30 fvdl #define IS_IPI 0x0002
89 1.30 fvdl #define IS_LOG 0x0004
90 1.3 mycroft
91 1.3 mycroft
92 1.30 fvdl /*
93 1.30 fvdl * Interrupt handler chains. *_intr_establish() insert a handler into
94 1.30 fvdl * the list. The handler is called with its (single) argument.
95 1.30 fvdl */
96 1.26 fvdl
97 1.30 fvdl struct intrhand {
98 1.30 fvdl int (*ih_fun)(void *);
99 1.30 fvdl void *ih_arg;
100 1.30 fvdl int ih_level;
101 1.30 fvdl struct intrhand *ih_next;
102 1.30 fvdl int ih_pin;
103 1.30 fvdl int ih_slot;
104 1.30 fvdl struct cpu_info *ih_cpu;
105 1.30 fvdl };
106 1.3 mycroft
107 1.30 fvdl #define IMASK(ci,level) (ci)->ci_imask[(level)]
108 1.30 fvdl #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
109 1.26 fvdl
110 1.26 fvdl extern void Xspllower __P((void));
111 1.3 mycroft
112 1.3 mycroft static __inline int splraise __P((int));
113 1.14 cgd static __inline void spllower __P((int));
114 1.3 mycroft static __inline void softintr __P((int));
115 1.3 mycroft
116 1.3 mycroft /*
117 1.30 fvdl * Convert spl level to local APIC level
118 1.30 fvdl */
119 1.30 fvdl #define APIC_LEVEL(l) ((l) << 4)
120 1.30 fvdl
121 1.30 fvdl /*
122 1.23 fvdl * compiler barrier: prevent reordering of instructions.
123 1.23 fvdl * XXX something similar will move to <sys/cdefs.h>
124 1.23 fvdl * or thereabouts.
125 1.23 fvdl * This prevents the compiler from reordering code around
126 1.23 fvdl * this "instruction", acting as a sequence point for code generation.
127 1.23 fvdl */
128 1.23 fvdl
129 1.30 fvdl #define __splbarrier() __asm __volatile("":::"memory")
130 1.23 fvdl
131 1.23 fvdl /*
132 1.3 mycroft * Add a mask to cpl, and return the old value of cpl.
133 1.3 mycroft */
134 1.3 mycroft static __inline int
135 1.30 fvdl splraise(int nlevel)
136 1.3 mycroft {
137 1.30 fvdl int olevel;
138 1.30 fvdl struct cpu_info *ci = curcpu();
139 1.3 mycroft
140 1.30 fvdl olevel = ci->ci_ilevel;
141 1.30 fvdl if (nlevel > olevel)
142 1.30 fvdl ci->ci_ilevel = nlevel;
143 1.23 fvdl __splbarrier();
144 1.30 fvdl return (olevel);
145 1.3 mycroft }
146 1.3 mycroft
147 1.30 fvdl void cpu_Debugger(void);
148 1.30 fvdl void printf(const char *, ...)
149 1.30 fvdl __attribute__((__format__(__printf__,1,2)));
150 1.30 fvdl
151 1.3 mycroft /*
152 1.3 mycroft * Restore a value to cpl (unmasking interrupts). If any unmasked
153 1.3 mycroft * interrupts are pending, call Xspllower() to process them.
154 1.3 mycroft */
155 1.3 mycroft static __inline void
156 1.30 fvdl spllower(int nlevel)
157 1.3 mycroft {
158 1.30 fvdl struct cpu_info *ci = curcpu();
159 1.3 mycroft
160 1.23 fvdl __splbarrier();
161 1.30 fvdl ci->ci_ilevel = nlevel;
162 1.30 fvdl /*
163 1.30 fvdl * Since this should only lower the interrupt level,
164 1.30 fvdl * the XOR below should only show interrupts that
165 1.30 fvdl * are being unmasked.
166 1.30 fvdl */
167 1.30 fvdl if (ci->ci_ipending & IUNMASK(ci,nlevel))
168 1.3 mycroft Xspllower();
169 1.3 mycroft }
170 1.3 mycroft
171 1.3 mycroft /*
172 1.3 mycroft * Hardware interrupt masks
173 1.3 mycroft */
174 1.26 fvdl #define splbio() splraise(IPL_BIO)
175 1.26 fvdl #define splnet() splraise(IPL_NET)
176 1.26 fvdl #define spltty() splraise(IPL_TTY)
177 1.26 fvdl #define splaudio() splraise(IPL_AUDIO)
178 1.26 fvdl #define splclock() splraise(IPL_CLOCK)
179 1.7 mycroft #define splstatclock() splclock()
180 1.26 fvdl #define splserial() splraise(IPL_SERIAL)
181 1.26 fvdl #define splipi() splraise(IPL_IPI)
182 1.26 fvdl
183 1.26 fvdl #define spllpt() spltty()
184 1.8 is
185 1.30 fvdl #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
186 1.24 yamt #define spllpt() spltty()
187 1.3 mycroft
188 1.3 mycroft /*
189 1.3 mycroft * Software interrupt masks
190 1.3 mycroft *
191 1.3 mycroft * NOTE: splsoftclock() is used by hardclock() to lower the priority from
192 1.3 mycroft * clock to softclock before it calls softclock().
193 1.3 mycroft */
194 1.26 fvdl #define spllowersoftclock() spllower(IPL_SOFTCLOCK)
195 1.26 fvdl
196 1.26 fvdl #define splsoftclock() splraise(IPL_SOFTCLOCK)
197 1.26 fvdl #define splsoftnet() splraise(IPL_SOFTNET)
198 1.26 fvdl #define splsoftserial() splraise(IPL_SOFTSERIAL)
199 1.3 mycroft
200 1.3 mycroft /*
201 1.3 mycroft * Miscellaneous
202 1.3 mycroft */
203 1.26 fvdl #define splvm() splraise(IPL_IMP)
204 1.26 fvdl #define splhigh() splraise(IPL_HIGH)
205 1.26 fvdl #define spl0() spllower(IPL_NONE)
206 1.29 fvdl #define splsched() splraise(IPL_SCHED)
207 1.28 fvdl #define spllock() splhigh()
208 1.14 cgd #define splx(x) spllower(x)
209 1.3 mycroft
210 1.3 mycroft /*
211 1.3 mycroft * Software interrupt registration
212 1.3 mycroft *
213 1.3 mycroft * We hand-code this to ensure that it's atomic.
214 1.30 fvdl *
215 1.30 fvdl * XXX always scheduled on the current CPU.
216 1.3 mycroft */
217 1.3 mycroft static __inline void
218 1.30 fvdl softintr(int sir)
219 1.3 mycroft {
220 1.30 fvdl struct cpu_info *ci = curcpu();
221 1.30 fvdl
222 1.30 fvdl __asm __volatile("lock ; orl %1, %0" :
223 1.30 fvdl "=m"(ci->ci_ipending) : "ir" (1 << sir));
224 1.3 mycroft }
225 1.3 mycroft
226 1.30 fvdl /*
227 1.30 fvdl * XXX
228 1.30 fvdl */
229 1.6 mycroft #define setsoftnet() softintr(SIR_NET)
230 1.3 mycroft
231 1.30 fvdl /*
232 1.30 fvdl * Stub declarations.
233 1.30 fvdl */
234 1.26 fvdl
235 1.30 fvdl extern void Xsoftclock(void);
236 1.30 fvdl extern void Xsoftnet(void);
237 1.30 fvdl extern void Xsoftserial(void);
238 1.26 fvdl
239 1.30 fvdl extern struct intrstub i8259_stubs[];
240 1.30 fvdl extern struct intrstub ioapic_stubs[];
241 1.26 fvdl
242 1.26 fvdl struct cpu_info;
243 1.26 fvdl
244 1.30 fvdl #include "ioapic.h"
245 1.30 fvdl
246 1.30 fvdl extern char idt_allocmap[];
247 1.30 fvdl
248 1.30 fvdl void intr_default_setup(void);
249 1.30 fvdl int i386_nmi(void);
250 1.30 fvdl void intr_calculatemasks(struct cpu_info *);
251 1.30 fvdl int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *);
252 1.30 fvdl int intr_allocate_slot(struct pic *, int, int, int, struct cpu_info **, int *,
253 1.30 fvdl int *);
254 1.30 fvdl void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *);
255 1.30 fvdl void intr_disestablish(struct intrhand *);
256 1.30 fvdl void cpu_intr_init(struct cpu_info *);
257 1.30 fvdl #ifdef INTRDEBUG
258 1.30 fvdl void intr_printconfig(void);
259 1.30 fvdl #endif
260 1.30 fvdl
261 1.30 fvdl #if NIOAPIC > 0
262 1.30 fvdl int intr_find_mpmapping(int bus, int pin, int *handle);
263 1.30 fvdl #endif
264 1.30 fvdl
265 1.30 fvdl #ifdef MULTIPROCESSOR
266 1.30 fvdl int i386_send_ipi(struct cpu_info *, int);
267 1.30 fvdl void i386_broadcast_ipi(int);
268 1.30 fvdl void i386_multicast_ipi(int, int);
269 1.30 fvdl void i386_ipi_handler(void);
270 1.30 fvdl void i386_intlock(void);
271 1.30 fvdl void i386_intunlock(void);
272 1.26 fvdl #endif
273 1.26 fvdl
274 1.3 mycroft #endif /* !_LOCORE */
275 1.19 thorpej
276 1.19 thorpej /*
277 1.19 thorpej * Generic software interrupt support.
278 1.19 thorpej */
279 1.19 thorpej
280 1.19 thorpej #define I386_SOFTINTR_SOFTCLOCK 0
281 1.19 thorpej #define I386_SOFTINTR_SOFTNET 1
282 1.19 thorpej #define I386_SOFTINTR_SOFTSERIAL 2
283 1.19 thorpej #define I386_NSOFTINTR 3
284 1.19 thorpej
285 1.19 thorpej #ifndef _LOCORE
286 1.19 thorpej #include <sys/queue.h>
287 1.19 thorpej
288 1.19 thorpej struct i386_soft_intrhand {
289 1.19 thorpej TAILQ_ENTRY(i386_soft_intrhand)
290 1.19 thorpej sih_q;
291 1.19 thorpej struct i386_soft_intr *sih_intrhead;
292 1.19 thorpej void (*sih_fn)(void *);
293 1.19 thorpej void *sih_arg;
294 1.19 thorpej int sih_pending;
295 1.19 thorpej };
296 1.19 thorpej
297 1.19 thorpej struct i386_soft_intr {
298 1.19 thorpej TAILQ_HEAD(, i386_soft_intrhand)
299 1.19 thorpej softintr_q;
300 1.19 thorpej int softintr_ssir;
301 1.26 fvdl struct simplelock softintr_slock;
302 1.19 thorpej };
303 1.19 thorpej
304 1.19 thorpej #define i386_softintr_lock(si, s) \
305 1.19 thorpej do { \
306 1.30 fvdl (s) = splhigh(); \
307 1.26 fvdl simple_lock(&si->softintr_slock); \
308 1.20 lukem } while (/*CONSTCOND*/ 0)
309 1.19 thorpej
310 1.19 thorpej #define i386_softintr_unlock(si, s) \
311 1.19 thorpej do { \
312 1.26 fvdl simple_unlock(&si->softintr_slock); \
313 1.19 thorpej splx((s)); \
314 1.20 lukem } while (/*CONSTCOND*/ 0)
315 1.19 thorpej
316 1.19 thorpej void *softintr_establish(int, void (*)(void *), void *);
317 1.19 thorpej void softintr_disestablish(void *);
318 1.19 thorpej void softintr_init(void);
319 1.19 thorpej void softintr_dispatch(int);
320 1.19 thorpej
321 1.19 thorpej #define softintr_schedule(arg) \
322 1.19 thorpej do { \
323 1.19 thorpej struct i386_soft_intrhand *__sih = (arg); \
324 1.19 thorpej struct i386_soft_intr *__si = __sih->sih_intrhead; \
325 1.19 thorpej int __s; \
326 1.19 thorpej \
327 1.19 thorpej i386_softintr_lock(__si, __s); \
328 1.19 thorpej if (__sih->sih_pending == 0) { \
329 1.19 thorpej TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q); \
330 1.19 thorpej __sih->sih_pending = 1; \
331 1.19 thorpej softintr(__si->softintr_ssir); \
332 1.19 thorpej } \
333 1.19 thorpej i386_softintr_unlock(__si, __s); \
334 1.20 lukem } while (/*CONSTCOND*/ 0)
335 1.19 thorpej #endif /* _LOCORE */
336 1.4 mycroft
337 1.4 mycroft #endif /* !_I386_INTR_H_ */
338