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 amd64. For amd64, 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)(uintptr_t vector);
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 = ithread_create(&isrc->is_ithread, (uintptr_t)isrc, 0,
85 (mask_fn)isrc->is_pic->pic_disable_source,
86 (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
89 mtx_lock_spin(&intr_table_lock);
90 if (interrupt_sources[vector] != NULL) {
91 mtx_unlock_spin(&intr_table_lock);
92 ithread_destroy(isrc->is_ithread);
95 intrcnt_register(isrc);
96 interrupt_sources[vector] = isrc;
97 mtx_unlock_spin(&intr_table_lock);
102 intr_lookup_source(int vector)
105 return (interrupt_sources[vector]);
109 intr_add_handler(const char *name, int vector, driver_intr_t handler,
110 void *arg, enum intr_type flags, void **cookiep)
115 isrc = intr_lookup_source(vector);
118 error = ithread_add_handler(isrc->is_ithread, name, handler, arg,
119 ithread_priority(flags), flags, cookiep);
121 intrcnt_updatename(isrc);
122 isrc->is_pic->pic_enable_intr(isrc);
123 isrc->is_pic->pic_enable_source(isrc);
129 intr_remove_handler(void *cookie)
133 error = ithread_remove_handler(cookie);
136 intrcnt_updatename(/* XXX */);
142 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
146 isrc = intr_lookup_source(vector);
149 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
153 intr_execute_handlers(struct intsrc *isrc, struct intrframe *iframe)
161 td->td_intr_nesting_level++;
164 * We count software interrupts when we process them. The
165 * code here follows previous practice, but there's an
166 * argument for counting hardware interrupts when they're
170 PCPU_LAZY_INC(cnt.v_intr);
172 it = isrc->is_ithread;
176 ih = TAILQ_FIRST(&it->it_handlers);
179 * XXX: We assume that IRQ 0 is only used for the ISA timer
182 vector = isrc->is_pic->pic_vector(isrc);
186 if (ih != NULL && ih->ih_flags & IH_FAST) {
188 * Execute fast interrupt handlers directly.
189 * To support clock handlers, if a handler registers
190 * with a NULL argument, then we pass it a pointer to
191 * a trapframe as its argument.
194 TAILQ_FOREACH(ih, &it->it_handlers, ih_next) {
195 MPASS(ih->ih_flags & IH_FAST);
196 CTR3(KTR_INTR, "%s: executing handler %p(%p)",
197 __func__, ih->ih_handler,
198 ih->ih_argument == NULL ? iframe :
200 if (ih->ih_argument == NULL)
201 ih->ih_handler(iframe);
203 ih->ih_handler(ih->ih_argument);
205 isrc->is_pic->pic_eoi_source(isrc);
210 * For stray and threaded interrupts, we mask and EOI the
213 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
217 error = ithread_schedule(it);
219 if (error == EINVAL) {
220 (*isrc->is_straycount)++;
221 if (*isrc->is_straycount < MAX_STRAY_LOG)
222 log(LOG_ERR, "stray irq%d\n", vector);
223 else if (*isrc->is_straycount == MAX_STRAY_LOG)
225 "too many stray irq %d's: not logging anymore\n",
228 td->td_intr_nesting_level--;
234 struct intsrc **isrc;
237 mtx_lock_spin(&intr_table_lock);
238 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
239 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
240 (*isrc)->is_pic->pic_resume(*isrc);
241 mtx_unlock_spin(&intr_table_lock);
247 struct intsrc **isrc;
250 mtx_lock_spin(&intr_table_lock);
251 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
252 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
253 (*isrc)->is_pic->pic_suspend(*isrc);
254 mtx_unlock_spin(&intr_table_lock);
258 intrcnt_setname(const char *name, int index)
261 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
266 intrcnt_updatename(struct intsrc *is)
269 intrcnt_setname(is->is_ithread->it_td->td_proc->p_comm, is->is_index);
273 intrcnt_register(struct intsrc *is)
275 char straystr[MAXCOMLEN + 1];
277 /* mtx_assert(&intr_table_lock, MA_OWNED); */
278 KASSERT(is->is_ithread != NULL, ("%s: isrc with no ithread", __func__));
279 is->is_index = intrcnt_index;
281 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
282 is->is_pic->pic_vector(is));
283 intrcnt_updatename(is);
284 is->is_count = &intrcnt[is->is_index];
285 intrcnt_setname(straystr, is->is_index + 1);
286 is->is_straycount = &intrcnt[is->is_index + 1];
290 intrcnt_add(const char *name, u_long **countp)
293 mtx_lock_spin(&intr_table_lock);
294 *countp = &intrcnt[intrcnt_index];
295 intrcnt_setname(name, intrcnt_index);
297 mtx_unlock_spin(&intr_table_lock);
301 intr_init(void *dummy __unused)
304 intrcnt_setname("???", 0);
306 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
308 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
312 * Dump data about interrupt handlers
314 DB_SHOW_COMMAND(irqs, db_show_irqs)
316 struct intsrc **isrc;
317 int i, quit, verbose;
320 if (strcmp(modif, "v") == 0)
324 isrc = interrupt_sources;
325 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
326 for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
328 db_dump_ithread((*isrc)->is_ithread, verbose);