2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 2001 Jake Burkholder.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
58 * form: src/sys/i386/isa/intr_machdep.c,v 1.57 2001/07/20
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/queue.h>
68 #include <sys/errno.h>
69 #include <sys/interrupt.h>
72 #include <sys/mutex.h>
75 #include <sys/vmmeter.h>
77 #include <machine/frame.h>
78 #include <machine/intr_machdep.h>
80 #define MAX_STRAY_LOG 5
82 CTASSERT((1 << IV_SHIFT) == sizeof(struct intr_vector));
84 ih_func_t *intr_handlers[PIL_MAX];
85 uint16_t pil_countp[PIL_MAX];
87 struct intr_vector intr_vectors[IV_MAX];
88 uint16_t intr_countp[IV_MAX];
89 static u_long intr_stray_count[IV_MAX];
91 static char *pil_names[] = {
94 "ithrd", /* PIL_ITHREAD */
95 "rndzvs", /* PIL_RENDEZVOUS */
97 "stop", /* PIL_STOP */
98 "stray", "stray", "stray", "stray", "stray", "stray", "stray",
99 "fast", /* PIL_FAST */
100 "tick", /* PIL_TICK */
103 /* protect the intr_vectors table */
104 static struct mtx intr_table_lock;
106 static void intr_execute_handlers(void *);
107 static void intr_stray_level(struct trapframe *);
108 static void intr_stray_vector(void *);
109 static int intrcnt_setname(const char *, int);
110 static void intrcnt_updatename(int, const char *, int);
116 intrcnt_updatename(int vec, const char *name, int ispil)
118 static int intrcnt_index, stray_pil_index, stray_vec_index;
121 if (intrnames[0] == '\0') {
124 printf("initalizing intr_countp\n");
125 intrcnt_setname("???", intrcnt_index++);
127 stray_vec_index = intrcnt_index++;
128 intrcnt_setname("stray", stray_vec_index);
129 for (name_index = 0; name_index < IV_MAX; name_index++)
130 intr_countp[name_index] = stray_vec_index;
132 stray_pil_index = intrcnt_index++;
133 intrcnt_setname("pil", stray_pil_index);
134 for (name_index = 0; name_index < PIL_MAX; name_index++)
135 pil_countp[name_index] = stray_pil_index;
141 if (!ispil && intr_countp[vec] != stray_vec_index)
142 name_index = intr_countp[vec];
143 else if (ispil && pil_countp[vec] != stray_pil_index)
144 name_index = pil_countp[vec];
146 name_index = intrcnt_index++;
148 if (intrcnt_setname(name, name_index))
152 intr_countp[vec] = name_index;
154 pil_countp[vec] = name_index;
158 intrcnt_setname(const char *name, int index)
161 if (intrnames + (MAXCOMLEN + 1) * index >= eintrnames)
163 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
169 intr_setup(int pri, ih_func_t *ihf, int vec, iv_func_t *ivf, void *iva)
171 char pilname[MAXCOMLEN + 1];
176 intr_vectors[vec].iv_func = ivf;
177 intr_vectors[vec].iv_arg = iva;
178 intr_vectors[vec].iv_pri = pri;
179 intr_vectors[vec].iv_vec = vec;
181 snprintf(pilname, MAXCOMLEN + 1, "pil%d: %s", pri, pil_names[pri]);
182 intrcnt_updatename(pri, pilname, 1);
183 intr_handlers[pri] = ihf;
188 intr_stray_level(struct trapframe *tf)
191 printf("stray level interrupt %ld\n", tf->tf_level);
195 intr_stray_vector(void *cookie)
197 struct intr_vector *iv;
200 if (intr_stray_count[iv->iv_vec] < MAX_STRAY_LOG) {
201 printf("stray vector interrupt %d\n", iv->iv_vec);
202 intr_stray_count[iv->iv_vec]++;
203 if (intr_stray_count[iv->iv_vec] >= MAX_STRAY_LOG)
204 printf("got %d stray interrupt %d's: not logging "
205 "anymore\n", MAX_STRAY_LOG, iv->iv_vec);
214 /* Mark all interrupts as being stray. */
215 for (i = 0; i < PIL_MAX; i++)
216 intr_handlers[i] = intr_stray_level;
217 for (i = 0; i < IV_MAX; i++) {
218 intr_vectors[i].iv_func = intr_stray_vector;
219 intr_vectors[i].iv_arg = &intr_vectors[i];
220 intr_vectors[i].iv_pri = PIL_LOW;
221 intr_vectors[i].iv_vec = i;
223 intr_handlers[PIL_LOW] = intr_fast;
230 mtx_init(&intr_table_lock, "ithread table lock", NULL, MTX_SPIN);
234 intr_execute_handlers(void *cookie)
236 struct intr_vector *iv;
247 ih = TAILQ_FIRST(&ithd->it_handlers);
248 if (ih != NULL && ih->ih_flags & IH_FAST) {
249 /* Execute fast interrupt handlers directly. */
250 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
251 MPASS(ih->ih_flags & IH_FAST &&
252 ih->ih_argument != NULL);
253 CTR3(KTR_INTR, "%s: executing handler %p(%p)",
254 __func__, ih->ih_handler, ih->ih_argument);
255 ih->ih_handler(ih->ih_argument);
260 /* Schedule a heavyweight interrupt process. */
261 error = ithread_schedule(ithd);
263 intr_stray_vector(iv);
267 inthand_add(const char *name, int vec, void (*handler)(void *), void *arg,
268 int flags, void **cookiep)
270 struct intr_vector *iv;
271 struct ithd *ithd; /* descriptor for the IRQ */
276 * Work around a race where more than one CPU may be registering
277 * handlers on the same IRQ at the same time.
279 iv = &intr_vectors[vec];
280 mtx_lock_spin(&intr_table_lock);
282 mtx_unlock_spin(&intr_table_lock);
284 errcode = ithread_create(&ithd, vec, 0, NULL, NULL, "vec%d:",
288 mtx_lock_spin(&intr_table_lock);
289 if (iv->iv_ithd == NULL) {
291 mtx_unlock_spin(&intr_table_lock);
295 mtx_unlock_spin(&intr_table_lock);
296 ithread_destroy(orphan);
300 errcode = ithread_add_handler(ithd, name, handler, arg,
301 ithread_priority(flags), flags, cookiep);
305 intr_setup(flags & INTR_FAST ? PIL_FAST : PIL_ITHREAD, intr_fast, vec,
306 intr_execute_handlers, iv);
308 intr_stray_count[vec] = 0;
310 intrcnt_updatename(vec, ithd->it_td->td_proc->p_comm, 0);
316 inthand_remove(int vec, void *cookie)
318 struct intr_vector *iv;
321 error = ithread_remove_handler(cookie);
324 * XXX: maybe this should be done regardless of whether
325 * ithread_remove_handler() succeeded?
327 iv = &intr_vectors[vec];
328 mtx_lock_spin(&intr_table_lock);
329 if (iv->iv_ithd == NULL)
330 intr_setup(PIL_ITHREAD, intr_fast, vec,
331 intr_stray_vector, iv);
333 intr_setup(PIL_LOW, intr_fast, vec,
334 intr_execute_handlers, iv);
335 mtx_unlock_spin(&intr_table_lock);