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>
54 #include <machine/smp.h>
59 #define MAX_STRAY_LOG 5
61 typedef void (*mask_fn)(void *);
63 static int intrcnt_index;
64 static struct intsrc *interrupt_sources[NUM_IO_INTS];
65 static struct mtx intr_table_lock;
66 static STAILQ_HEAD(, pic) pics;
69 static void intr_eoi_src(void *arg);
70 static void intr_disab_eoi_src(void *arg);
71 static void intr_event_stray(void *cookie);
75 static int assign_cpu;
77 static void intr_assign_next_cpu(struct intsrc *isrc);
80 static void intr_init(void *__dummy);
81 static int intr_pic_registered(struct pic *pic);
82 static void intrcnt_setname(const char *name, int index);
83 static void intrcnt_updatename(struct intsrc *is);
84 static void intrcnt_register(struct intsrc *is);
87 intr_pic_registered(struct pic *pic)
91 STAILQ_FOREACH(p, &pics, pics) {
99 * Register a new interrupt controller (PIC). This is to support suspend
100 * and resume where we suspend/resume controllers rather than individual
101 * sources. This also allows controllers with no active sources (such as
102 * 8259As in a system using the APICs) to participate in suspend and resume.
105 intr_register_pic(struct pic *pic)
109 mtx_lock_spin(&intr_table_lock);
110 if (intr_pic_registered(pic))
113 STAILQ_INSERT_TAIL(&pics, pic, pics);
116 mtx_unlock_spin(&intr_table_lock);
121 * Register a new interrupt source with the global interrupt system.
122 * The global interrupts need to be disabled when this function is
126 intr_register_source(struct intsrc *isrc)
130 KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
131 vector = isrc->is_pic->pic_vector(isrc);
132 if (interrupt_sources[vector] != NULL)
135 error = intr_event_create(&isrc->is_event, isrc, 0,
136 (mask_fn)isrc->is_pic->pic_enable_source,
137 intr_eoi_src, intr_disab_eoi_src, "irq%d:", vector);
139 error = intr_event_create(&isrc->is_event, isrc, 0,
140 (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
144 mtx_lock_spin(&intr_table_lock);
145 if (interrupt_sources[vector] != NULL) {
146 mtx_unlock_spin(&intr_table_lock);
147 intr_event_destroy(isrc->is_event);
150 intrcnt_register(isrc);
151 interrupt_sources[vector] = isrc;
152 isrc->is_enabled = 0;
153 mtx_unlock_spin(&intr_table_lock);
158 intr_lookup_source(int vector)
161 return (interrupt_sources[vector]);
165 intr_add_handler(const char *name, int vector, driver_filter_t filter,
166 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
171 isrc = intr_lookup_source(vector);
174 error = intr_event_add_handler(isrc->is_event, name, filter, handler,
175 arg, intr_priority(flags), flags, cookiep);
177 intrcnt_updatename(isrc);
178 mtx_lock_spin(&intr_table_lock);
179 if (!isrc->is_enabled) {
180 isrc->is_enabled = 1;
183 intr_assign_next_cpu(isrc);
185 mtx_unlock_spin(&intr_table_lock);
186 isrc->is_pic->pic_enable_intr(isrc);
188 mtx_unlock_spin(&intr_table_lock);
189 isrc->is_pic->pic_enable_source(isrc);
195 intr_remove_handler(void *cookie)
200 isrc = intr_handler_source(cookie);
201 error = intr_event_remove_handler(cookie);
203 intrcnt_updatename(isrc);
208 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
212 isrc = intr_lookup_source(vector);
215 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
220 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
223 struct intr_event *ie;
229 * We count software interrupts when we process them. The
230 * code here follows previous practice, but there's an
231 * argument for counting hardware interrupts when they're
235 PCPU_LAZY_INC(cnt.v_intr);
240 * XXX: We assume that IRQ 0 is only used for the ISA timer
243 vector = isrc->is_pic->pic_vector(isrc);
247 if (intr_event_handle(ie, frame) != 0)
248 intr_event_stray(isrc);
252 intr_event_stray(void *cookie)
258 * For stray interrupts, mask and EOI the source, bump the
259 * stray count, and log the condition.
261 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
262 (*isrc->is_straycount)++;
263 if (*isrc->is_straycount < MAX_STRAY_LOG)
264 log(LOG_ERR, "stray irq%d\n", isrc->is_pic->pic_vector(isrc));
265 else if (*isrc->is_straycount == MAX_STRAY_LOG)
267 "too many stray irq %d's: not logging anymore\n",
268 isrc->is_pic->pic_vector(isrc));
272 intr_eoi_src(void *arg)
277 isrc->is_pic->pic_eoi_source(isrc);
281 intr_disab_eoi_src(void *arg)
286 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
290 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
293 struct intr_event *ie;
294 struct intr_handler *ih;
295 int error, vector, thread;
300 * We count software interrupts when we process them. The
301 * code here follows previous practice, but there's an
302 * argument for counting hardware interrupts when they're
306 PCPU_LAZY_INC(cnt.v_intr);
311 * XXX: We assume that IRQ 0 is only used for the ISA timer
314 vector = isrc->is_pic->pic_vector(isrc);
319 * For stray interrupts, mask and EOI the source, bump the
320 * stray count, and log the condition.
322 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) {
323 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
324 (*isrc->is_straycount)++;
325 if (*isrc->is_straycount < MAX_STRAY_LOG)
326 log(LOG_ERR, "stray irq%d\n", vector);
327 else if (*isrc->is_straycount == MAX_STRAY_LOG)
329 "too many stray irq %d's: not logging anymore\n",
335 * Execute fast interrupt handlers directly.
336 * To support clock handlers, if a handler registers
337 * with a NULL argument, then we pass it a pointer to
338 * a trapframe as its argument.
340 td->td_intr_nesting_level++;
343 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
344 if (ih->ih_filter == NULL) {
348 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
349 ih->ih_filter, ih->ih_argument == NULL ? frame :
350 ih->ih_argument, ih->ih_name);
351 if (ih->ih_argument == NULL)
352 ih->ih_filter(frame);
354 ih->ih_filter(ih->ih_argument);
358 * If there are any threaded handlers that need to run,
359 * mask the source as well as sending it an EOI. Otherwise,
360 * just send it an EOI but leave it unmasked.
363 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
365 isrc->is_pic->pic_eoi_source(isrc);
368 /* Schedule the ithread if needed. */
370 error = intr_event_schedule_thread(ie);
371 KASSERT(error == 0, ("bad stray interrupt"));
373 td->td_intr_nesting_level--;
382 mtx_lock_spin(&intr_table_lock);
383 STAILQ_FOREACH(pic, &pics, pics) {
384 if (pic->pic_resume != NULL)
385 pic->pic_resume(pic);
387 mtx_unlock_spin(&intr_table_lock);
395 mtx_lock_spin(&intr_table_lock);
396 STAILQ_FOREACH(pic, &pics, pics) {
397 if (pic->pic_suspend != NULL)
398 pic->pic_suspend(pic);
400 mtx_unlock_spin(&intr_table_lock);
404 intrcnt_setname(const char *name, int index)
407 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
412 intrcnt_updatename(struct intsrc *is)
415 intrcnt_setname(is->is_event->ie_fullname, is->is_index);
419 intrcnt_register(struct intsrc *is)
421 char straystr[MAXCOMLEN + 1];
423 /* mtx_assert(&intr_table_lock, MA_OWNED); */
424 KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
425 is->is_index = intrcnt_index;
427 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
428 is->is_pic->pic_vector(is));
429 intrcnt_updatename(is);
430 is->is_count = &intrcnt[is->is_index];
431 intrcnt_setname(straystr, is->is_index + 1);
432 is->is_straycount = &intrcnt[is->is_index + 1];
436 intrcnt_add(const char *name, u_long **countp)
439 mtx_lock_spin(&intr_table_lock);
440 *countp = &intrcnt[intrcnt_index];
441 intrcnt_setname(name, intrcnt_index);
443 mtx_unlock_spin(&intr_table_lock);
447 intr_init(void *dummy __unused)
450 intrcnt_setname("???", 0);
453 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
455 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
459 * Dump data about interrupt handlers
461 DB_SHOW_COMMAND(irqs, db_show_irqs)
463 struct intsrc **isrc;
466 if (strcmp(modif, "v") == 0)
470 isrc = interrupt_sources;
471 for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
473 db_dump_intr_event((*isrc)->is_event, verbose);
479 * Support for balancing interrupt sources across CPUs. For now we just
480 * allocate CPUs round-robin.
483 /* The BSP is always a valid target. */
484 static cpumask_t intr_cpus = (1 << 0);
485 static int current_cpu, num_cpus = 1;
488 intr_assign_next_cpu(struct intsrc *isrc)
494 * Assign this source to a local APIC in a round-robin fashion.
497 apic_id = cpu_apic_ids[current_cpu];
498 pic->pic_assign_cpu(isrc, apic_id);
501 if (current_cpu >= num_cpus)
503 } while (!(intr_cpus & (1 << current_cpu)));
507 * Add a CPU to our mask of valid CPUs that can be destinations of
511 intr_add_cpu(u_int cpu)
515 panic("%s: Invalid CPU ID", __func__);
517 printf("INTR: Adding local APIC %d as a target\n",
520 intr_cpus |= (1 << cpu);
525 * Distribute all the interrupt sources among the available CPUs once the
526 * AP's have been launched.
529 intr_shuffle_irqs(void *arg __unused)
534 /* Don't bother on UP. */
538 /* Round-robin assign a CPU to each enabled source. */
539 mtx_lock_spin(&intr_table_lock);
541 for (i = 0; i < NUM_IO_INTS; i++) {
542 isrc = interrupt_sources[i];
543 if (isrc != NULL && isrc->is_enabled)
544 intr_assign_next_cpu(isrc);
546 mtx_unlock_spin(&intr_table_lock);
548 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs, NULL)