2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Machine dependent interrupt code for i386. For the i386, we have to
34 * deal with different PICs. Thus, we use the passed in vector to lookup
35 * an interrupt source associated with that vector. The interrupt source
36 * describes which PIC the source belongs to and includes methods to handle
42 #include <sys/param.h>
44 #include <sys/interrupt.h>
47 #include <sys/kernel.h>
48 #include <sys/mutex.h>
50 #include <sys/syslog.h>
51 #include <sys/systm.h>
52 #include <machine/clock.h>
53 #include <machine/intr_machdep.h>
58 #define MAX_STRAY_LOG 5
60 typedef void (*mask_fn)(void *);
62 static int intrcnt_index;
63 static struct intsrc *interrupt_sources[NUM_IO_INTS];
64 static struct mtx intr_table_lock;
66 static void intr_init(void *__dummy);
67 static void intrcnt_setname(const char *name, int index);
68 static void intrcnt_updatename(struct intsrc *is);
69 static void intrcnt_register(struct intsrc *is);
72 * Register a new interrupt source with the global interrupt system.
73 * The global interrupts need to be disabled when this function is
77 intr_register_source(struct intsrc *isrc)
81 vector = isrc->is_pic->pic_vector(isrc);
82 if (interrupt_sources[vector] != NULL)
84 error = intr_event_create(&isrc->is_event, isrc, 0,
85 (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
88 mtx_lock_spin(&intr_table_lock);
89 if (interrupt_sources[vector] != NULL) {
90 mtx_unlock_spin(&intr_table_lock);
91 intr_event_destroy(isrc->is_event);
94 intrcnt_register(isrc);
95 interrupt_sources[vector] = isrc;
96 mtx_unlock_spin(&intr_table_lock);
101 intr_lookup_source(int vector)
104 return (interrupt_sources[vector]);
108 intr_add_handler(const char *name, int vector, driver_intr_t handler,
109 void *arg, enum intr_type flags, void **cookiep)
114 isrc = intr_lookup_source(vector);
117 error = intr_event_add_handler(isrc->is_event, name, handler, arg,
118 intr_priority(flags), flags, cookiep);
120 intrcnt_updatename(isrc);
121 isrc->is_pic->pic_enable_intr(isrc);
122 isrc->is_pic->pic_enable_source(isrc);
128 intr_remove_handler(void *cookie)
132 error = intr_event_remove_handler(cookie);
135 intrcnt_updatename(/* XXX */);
141 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
145 isrc = intr_lookup_source(vector);
148 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
152 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
155 struct intr_event *ie;
156 struct intr_handler *ih;
157 int error, vector, thread;
162 * We count software interrupts when we process them. The
163 * code here follows previous practice, but there's an
164 * argument for counting hardware interrupts when they're
168 PCPU_LAZY_INC(cnt.v_intr);
173 * XXX: We assume that IRQ 0 is only used for the ISA timer
176 vector = isrc->is_pic->pic_vector(isrc);
181 * For stray interrupts, mask and EOI the source, bump the
182 * stray count, and log the condition.
184 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) {
185 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
186 (*isrc->is_straycount)++;
187 if (*isrc->is_straycount < MAX_STRAY_LOG)
188 log(LOG_ERR, "stray irq%d\n", vector);
189 else if (*isrc->is_straycount == MAX_STRAY_LOG)
191 "too many stray irq %d's: not logging anymore\n",
197 * Execute fast interrupt handlers directly.
198 * To support clock handlers, if a handler registers
199 * with a NULL argument, then we pass it a pointer to
200 * a trapframe as its argument.
202 td->td_intr_nesting_level++;
205 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
206 if (!(ih->ih_flags & IH_FAST)) {
210 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
211 ih->ih_handler, ih->ih_argument == NULL ? frame :
212 ih->ih_argument, ih->ih_name);
213 if (ih->ih_argument == NULL)
214 ih->ih_handler(frame);
216 ih->ih_handler(ih->ih_argument);
220 * If there are any threaded handlers that need to run,
221 * mask the source as well as sending it an EOI. Otherwise,
222 * just send it an EOI but leave it unmasked.
225 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
227 isrc->is_pic->pic_eoi_source(isrc);
230 /* Schedule the ithread if needed. */
232 error = intr_event_schedule_thread(ie);
233 KASSERT(error == 0, ("bad stray interrupt"));
235 td->td_intr_nesting_level--;
241 struct intsrc **isrc;
244 mtx_lock_spin(&intr_table_lock);
245 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
246 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
247 (*isrc)->is_pic->pic_resume(*isrc);
248 mtx_unlock_spin(&intr_table_lock);
254 struct intsrc **isrc;
257 mtx_lock_spin(&intr_table_lock);
258 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
259 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
260 (*isrc)->is_pic->pic_suspend(*isrc);
261 mtx_unlock_spin(&intr_table_lock);
265 intrcnt_setname(const char *name, int index)
268 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
273 intrcnt_updatename(struct intsrc *is)
276 intrcnt_setname(is->is_event->ie_fullname, is->is_index);
280 intrcnt_register(struct intsrc *is)
282 char straystr[MAXCOMLEN + 1];
284 /* mtx_assert(&intr_table_lock, MA_OWNED); */
285 KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
286 is->is_index = intrcnt_index;
288 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
289 is->is_pic->pic_vector(is));
290 intrcnt_updatename(is);
291 is->is_count = &intrcnt[is->is_index];
292 intrcnt_setname(straystr, is->is_index + 1);
293 is->is_straycount = &intrcnt[is->is_index + 1];
297 intrcnt_add(const char *name, u_long **countp)
300 mtx_lock_spin(&intr_table_lock);
301 *countp = &intrcnt[intrcnt_index];
302 intrcnt_setname(name, intrcnt_index);
304 mtx_unlock_spin(&intr_table_lock);
308 intr_init(void *dummy __unused)
311 intrcnt_setname("???", 0);
313 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
315 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
319 * Dump data about interrupt handlers
321 DB_SHOW_COMMAND(irqs, db_show_irqs)
323 struct intsrc **isrc;
324 int i, quit, verbose;
327 if (strcmp(modif, "v") == 0)
331 isrc = interrupt_sources;
332 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
333 for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
335 db_dump_intr_event((*isrc)->is_event, verbose);