intr.h revision 1.17.2.7 1 1.17.2.5 nathanw /* $NetBSD: intr.h,v 1.17.2.7 2002/12/11 06:01:00 thorpej Exp $ */
2 1.1 mycroft
3 1.9 mycroft /*-
4 1.17.2.1 nathanw * 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.17.2.1 nathanw * 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.17.2.7 thorpej #include <machine/intrdefs.h>
43 1.17.2.7 thorpej
44 1.17.2.7 thorpej #ifndef _LOCORE
45 1.17.2.7 thorpej #include <machine/cpu.h>
46 1.17.2.7 thorpej #include <machine/pic.h>
47 1.17.2.7 thorpej
48 1.17.2.5 nathanw /*
49 1.17.2.7 thorpej * Struct describing an interrupt source for a CPU. struct cpu_info
50 1.17.2.7 thorpej * has an array of MAX_INTR_SOURCES of these. The index in the array
51 1.17.2.7 thorpej * is equal to the stub number of the stubcode as present in vector.s
52 1.17.2.5 nathanw *
53 1.17.2.7 thorpej * The primary CPU's array of interrupt sources has its first 16
54 1.17.2.7 thorpej * entries reserved for legacy ISA irq handlers. This means that
55 1.17.2.7 thorpej * they have a 1:1 mapping for arrayindex:irq_num. This is not
56 1.17.2.7 thorpej * true for interrupts that come in through IO APICs, to find
57 1.17.2.7 thorpej * their source, go through ci->ci_isources[index].is_pic
58 1.17.2.5 nathanw *
59 1.17.2.7 thorpej * It's possible to always maintain a 1:1 mapping, but that means
60 1.17.2.7 thorpej * limiting the total number of interrupt sources to MAX_INTR_SOURCES
61 1.17.2.7 thorpej * (32), instead of 32 per CPU. It also would mean that having multiple
62 1.17.2.7 thorpej * IO APICs which deliver interrupts from an equal pin number would
63 1.17.2.7 thorpej * overlap if they were to be sent to the same CPU.
64 1.17.2.5 nathanw */
65 1.13 mycroft
66 1.17.2.7 thorpej struct intrstub {
67 1.17.2.7 thorpej void *ist_entry;
68 1.17.2.7 thorpej void *ist_recurse;
69 1.17.2.7 thorpej void *ist_resume;
70 1.17.2.7 thorpej };
71 1.3 mycroft
72 1.17.2.7 thorpej struct intrsource {
73 1.17.2.7 thorpej int is_maxlevel; /* max. IPL for this source */
74 1.17.2.7 thorpej int is_pin; /* IRQ for legacy; pin for IO APIC */
75 1.17.2.7 thorpej struct intrhand *is_handlers; /* handler chain */
76 1.17.2.7 thorpej struct pic *is_pic; /* originating PIC */
77 1.17.2.7 thorpej void *is_recurse; /* entry for spllower */
78 1.17.2.7 thorpej void *is_resume; /* entry for doreti */
79 1.17.2.7 thorpej struct evcnt is_evcnt; /* interrupt counter */
80 1.17.2.7 thorpej char is_evname[32]; /* event counter name */
81 1.17.2.7 thorpej int is_flags; /* see below */
82 1.17.2.7 thorpej int is_type; /* level, edge */
83 1.17.2.7 thorpej int is_idtvec;
84 1.17.2.7 thorpej int is_minlevel;
85 1.17.2.7 thorpej };
86 1.3 mycroft
87 1.17.2.7 thorpej #define IS_LEGACY 0x0001 /* legacy ISA irq source */
88 1.17.2.7 thorpej #define IS_IPI 0x0002
89 1.17.2.7 thorpej #define IS_LOG 0x0004
90 1.17.2.5 nathanw
91 1.3 mycroft
92 1.17.2.7 thorpej /*
93 1.17.2.7 thorpej * Interrupt handler chains. *_intr_establish() insert a handler into
94 1.17.2.7 thorpej * the list. The handler is called with its (single) argument.
95 1.17.2.7 thorpej */
96 1.17.2.7 thorpej
97 1.17.2.7 thorpej struct intrhand {
98 1.17.2.7 thorpej int (*ih_fun)(void *);
99 1.17.2.7 thorpej void *ih_arg;
100 1.17.2.7 thorpej int ih_level;
101 1.17.2.7 thorpej struct intrhand *ih_next;
102 1.17.2.7 thorpej int ih_pin;
103 1.17.2.7 thorpej int ih_slot;
104 1.17.2.7 thorpej struct cpu_info *ih_cpu;
105 1.17.2.7 thorpej };
106 1.17.2.7 thorpej
107 1.17.2.7 thorpej #define IMASK(ci,level) (ci)->ci_imask[(level)]
108 1.17.2.7 thorpej #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
109 1.17.2.5 nathanw
110 1.17.2.5 nathanw 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.17.2.7 thorpej * Convert spl level to local APIC level
118 1.17.2.7 thorpej */
119 1.17.2.7 thorpej #define APIC_LEVEL(l) ((l) << 4)
120 1.17.2.7 thorpej
121 1.17.2.7 thorpej /*
122 1.17.2.3 nathanw * compiler barrier: prevent reordering of instructions.
123 1.17.2.3 nathanw * XXX something similar will move to <sys/cdefs.h>
124 1.17.2.3 nathanw * or thereabouts.
125 1.17.2.3 nathanw * This prevents the compiler from reordering code around
126 1.17.2.3 nathanw * this "instruction", acting as a sequence point for code generation.
127 1.17.2.3 nathanw */
128 1.17.2.3 nathanw
129 1.17.2.7 thorpej #define __splbarrier() __asm __volatile("":::"memory")
130 1.17.2.3 nathanw
131 1.17.2.3 nathanw /*
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.17.2.7 thorpej splraise(int nlevel)
136 1.3 mycroft {
137 1.17.2.7 thorpej int olevel;
138 1.17.2.7 thorpej struct cpu_info *ci = curcpu();
139 1.3 mycroft
140 1.17.2.7 thorpej olevel = ci->ci_ilevel;
141 1.17.2.7 thorpej if (nlevel > olevel)
142 1.17.2.7 thorpej ci->ci_ilevel = nlevel;
143 1.17.2.3 nathanw __splbarrier();
144 1.17.2.7 thorpej return (olevel);
145 1.3 mycroft }
146 1.3 mycroft
147 1.17.2.7 thorpej void cpu_Debugger(void);
148 1.17.2.7 thorpej void printf(const char *, ...)
149 1.17.2.7 thorpej __attribute__((__format__(__printf__,1,2)));
150 1.17.2.7 thorpej
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.17.2.7 thorpej spllower(int nlevel)
157 1.3 mycroft {
158 1.17.2.7 thorpej struct cpu_info *ci = curcpu();
159 1.3 mycroft
160 1.17.2.3 nathanw __splbarrier();
161 1.17.2.7 thorpej ci->ci_ilevel = nlevel;
162 1.17.2.7 thorpej /*
163 1.17.2.7 thorpej * Since this should only lower the interrupt level,
164 1.17.2.7 thorpej * the XOR below should only show interrupts that
165 1.17.2.7 thorpej * are being unmasked.
166 1.17.2.7 thorpej */
167 1.17.2.7 thorpej 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.17.2.5 nathanw #define splbio() splraise(IPL_BIO)
175 1.17.2.5 nathanw #define splnet() splraise(IPL_NET)
176 1.17.2.5 nathanw #define spltty() splraise(IPL_TTY)
177 1.17.2.5 nathanw #define splaudio() splraise(IPL_AUDIO)
178 1.17.2.5 nathanw #define splclock() splraise(IPL_CLOCK)
179 1.7 mycroft #define splstatclock() splclock()
180 1.17.2.5 nathanw #define splserial() splraise(IPL_SERIAL)
181 1.17.2.5 nathanw #define splipi() splraise(IPL_IPI)
182 1.17.2.5 nathanw
183 1.17.2.5 nathanw #define spllpt() spltty()
184 1.8 is
185 1.17.2.7 thorpej #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
186 1.17.2.3 nathanw #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.17.2.5 nathanw #define spllowersoftclock() spllower(IPL_SOFTCLOCK)
195 1.17.2.5 nathanw
196 1.17.2.5 nathanw #define splsoftclock() splraise(IPL_SOFTCLOCK)
197 1.17.2.5 nathanw #define splsoftnet() splraise(IPL_SOFTNET)
198 1.17.2.5 nathanw #define splsoftserial() splraise(IPL_SOFTSERIAL)
199 1.3 mycroft
200 1.3 mycroft /*
201 1.3 mycroft * Miscellaneous
202 1.3 mycroft */
203 1.17.2.5 nathanw #define splvm() splraise(IPL_IMP)
204 1.17.2.5 nathanw #define splhigh() splraise(IPL_HIGH)
205 1.17.2.5 nathanw #define spl0() spllower(IPL_NONE)
206 1.17.2.6 nathanw #define splsched() splraise(IPL_SCHED)
207 1.17.2.5 nathanw #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.17.2.7 thorpej *
215 1.17.2.7 thorpej * XXX always scheduled on the current CPU.
216 1.3 mycroft */
217 1.3 mycroft static __inline void
218 1.17.2.7 thorpej softintr(int sir)
219 1.3 mycroft {
220 1.17.2.7 thorpej struct cpu_info *ci = curcpu();
221 1.17.2.7 thorpej
222 1.17.2.7 thorpej __asm __volatile("lock ; orl %1, %0" :
223 1.17.2.7 thorpej "=m"(ci->ci_ipending) : "ir" (1 << sir));
224 1.3 mycroft }
225 1.3 mycroft
226 1.17.2.7 thorpej /*
227 1.17.2.7 thorpej * XXX
228 1.17.2.7 thorpej */
229 1.6 mycroft #define setsoftnet() softintr(SIR_NET)
230 1.3 mycroft
231 1.17.2.7 thorpej /*
232 1.17.2.7 thorpej * Stub declarations.
233 1.17.2.7 thorpej */
234 1.17.2.5 nathanw
235 1.17.2.7 thorpej extern void Xsoftclock(void);
236 1.17.2.7 thorpej extern void Xsoftnet(void);
237 1.17.2.7 thorpej extern void Xsoftserial(void);
238 1.17.2.5 nathanw
239 1.17.2.7 thorpej extern struct intrstub i8259_stubs[];
240 1.17.2.7 thorpej extern struct intrstub ioapic_stubs[];
241 1.17.2.5 nathanw
242 1.17.2.5 nathanw struct cpu_info;
243 1.17.2.5 nathanw
244 1.17.2.7 thorpej extern char idt_allocmap[];
245 1.17.2.7 thorpej
246 1.17.2.7 thorpej void intr_default_setup(void);
247 1.17.2.7 thorpej int i386_nmi(void);
248 1.17.2.7 thorpej void intr_calculatemasks(struct cpu_info *);
249 1.17.2.7 thorpej int intr_allocate_slot_cpu(struct cpu_info *, struct pic *, int, int *);
250 1.17.2.7 thorpej int intr_allocate_slot(struct pic *, int, int, int, struct cpu_info **, int *,
251 1.17.2.7 thorpej int *);
252 1.17.2.7 thorpej void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *);
253 1.17.2.7 thorpej void intr_disestablish(struct intrhand *);
254 1.17.2.7 thorpej void cpu_intr_init(struct cpu_info *);
255 1.17.2.7 thorpej int intr_find_mpmapping(int bus, int pin, int *handle);
256 1.17.2.7 thorpej #ifdef INTRDEBUG
257 1.17.2.7 thorpej void intr_printconfig(void);
258 1.17.2.7 thorpej #endif
259 1.17.2.7 thorpej
260 1.17.2.7 thorpej #ifdef MULTIPROCESSOR
261 1.17.2.7 thorpej int i386_send_ipi(struct cpu_info *, int);
262 1.17.2.7 thorpej void i386_broadcast_ipi(int);
263 1.17.2.7 thorpej void i386_multicast_ipi(int, int);
264 1.17.2.7 thorpej void i386_ipi_handler(void);
265 1.17.2.7 thorpej void i386_intlock(struct intrframe);
266 1.17.2.7 thorpej void i386_intunlock(struct intrframe);
267 1.17.2.7 thorpej void i386_softintlock(void);
268 1.17.2.7 thorpej void i386_softintunlock(void);
269 1.17.2.5 nathanw #endif
270 1.17.2.5 nathanw
271 1.3 mycroft #endif /* !_LOCORE */
272 1.17.2.1 nathanw
273 1.17.2.1 nathanw /*
274 1.17.2.1 nathanw * Generic software interrupt support.
275 1.17.2.1 nathanw */
276 1.17.2.1 nathanw
277 1.17.2.1 nathanw #define I386_SOFTINTR_SOFTCLOCK 0
278 1.17.2.1 nathanw #define I386_SOFTINTR_SOFTNET 1
279 1.17.2.1 nathanw #define I386_SOFTINTR_SOFTSERIAL 2
280 1.17.2.1 nathanw #define I386_NSOFTINTR 3
281 1.17.2.1 nathanw
282 1.17.2.1 nathanw #ifndef _LOCORE
283 1.17.2.1 nathanw #include <sys/queue.h>
284 1.17.2.1 nathanw
285 1.17.2.1 nathanw struct i386_soft_intrhand {
286 1.17.2.1 nathanw TAILQ_ENTRY(i386_soft_intrhand)
287 1.17.2.1 nathanw sih_q;
288 1.17.2.1 nathanw struct i386_soft_intr *sih_intrhead;
289 1.17.2.1 nathanw void (*sih_fn)(void *);
290 1.17.2.1 nathanw void *sih_arg;
291 1.17.2.1 nathanw int sih_pending;
292 1.17.2.1 nathanw };
293 1.17.2.1 nathanw
294 1.17.2.1 nathanw struct i386_soft_intr {
295 1.17.2.1 nathanw TAILQ_HEAD(, i386_soft_intrhand)
296 1.17.2.1 nathanw softintr_q;
297 1.17.2.1 nathanw int softintr_ssir;
298 1.17.2.5 nathanw struct simplelock softintr_slock;
299 1.17.2.1 nathanw };
300 1.17.2.1 nathanw
301 1.17.2.1 nathanw #define i386_softintr_lock(si, s) \
302 1.17.2.1 nathanw do { \
303 1.17.2.7 thorpej (s) = splhigh(); \
304 1.17.2.5 nathanw simple_lock(&si->softintr_slock); \
305 1.17.2.1 nathanw } while (/*CONSTCOND*/ 0)
306 1.17.2.1 nathanw
307 1.17.2.1 nathanw #define i386_softintr_unlock(si, s) \
308 1.17.2.1 nathanw do { \
309 1.17.2.5 nathanw simple_unlock(&si->softintr_slock); \
310 1.17.2.1 nathanw splx((s)); \
311 1.17.2.1 nathanw } while (/*CONSTCOND*/ 0)
312 1.17.2.1 nathanw
313 1.17.2.1 nathanw void *softintr_establish(int, void (*)(void *), void *);
314 1.17.2.1 nathanw void softintr_disestablish(void *);
315 1.17.2.1 nathanw void softintr_init(void);
316 1.17.2.1 nathanw void softintr_dispatch(int);
317 1.17.2.1 nathanw
318 1.17.2.1 nathanw #define softintr_schedule(arg) \
319 1.17.2.1 nathanw do { \
320 1.17.2.1 nathanw struct i386_soft_intrhand *__sih = (arg); \
321 1.17.2.1 nathanw struct i386_soft_intr *__si = __sih->sih_intrhead; \
322 1.17.2.1 nathanw int __s; \
323 1.17.2.1 nathanw \
324 1.17.2.1 nathanw i386_softintr_lock(__si, __s); \
325 1.17.2.1 nathanw if (__sih->sih_pending == 0) { \
326 1.17.2.1 nathanw TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q); \
327 1.17.2.1 nathanw __sih->sih_pending = 1; \
328 1.17.2.1 nathanw softintr(__si->softintr_ssir); \
329 1.17.2.1 nathanw } \
330 1.17.2.1 nathanw i386_softintr_unlock(__si, __s); \
331 1.17.2.1 nathanw } while (/*CONSTCOND*/ 0)
332 1.17.2.1 nathanw #endif /* _LOCORE */
333 1.4 mycroft
334 1.4 mycroft #endif /* !_I386_INTR_H_ */
335