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>
46 #include <sys/kernel.h>
48 #include <sys/mutex.h>
51 #include <sys/syslog.h>
52 #include <sys/systm.h>
54 #include <machine/clock.h>
55 #include <machine/intr_machdep.h>
56 #include <machine/smp.h>
61 #define MAX_STRAY_LOG 5
63 typedef void (*mask_fn)(void *);
65 static int intrcnt_index;
66 static struct intsrc *interrupt_sources[NUM_IO_INTS];
67 static struct sx intr_table_lock;
68 static struct mtx intrcnt_lock;
69 static STAILQ_HEAD(, pic) pics;
72 static int assign_cpu;
74 static void intr_assign_next_cpu(struct intsrc *isrc);
77 static int intr_assign_cpu(void *arg, u_char cpu);
78 static void intr_disable_src(void *arg);
79 static void intr_init(void *__dummy);
80 static int intr_pic_registered(struct pic *pic);
81 static void intrcnt_setname(const char *name, int index);
82 static void intrcnt_updatename(struct intsrc *is);
83 static void intrcnt_register(struct intsrc *is);
86 intr_pic_registered(struct pic *pic)
90 STAILQ_FOREACH(p, &pics, pics) {
98 * Register a new interrupt controller (PIC). This is to support suspend
99 * and resume where we suspend/resume controllers rather than individual
100 * sources. This also allows controllers with no active sources (such as
101 * 8259As in a system using the APICs) to participate in suspend and resume.
104 intr_register_pic(struct pic *pic)
108 sx_xlock(&intr_table_lock);
109 if (intr_pic_registered(pic))
112 STAILQ_INSERT_TAIL(&pics, pic, pics);
115 sx_xunlock(&intr_table_lock);
120 * Register a new interrupt source with the global interrupt system.
121 * The global interrupts need to be disabled when this function is
125 intr_register_source(struct intsrc *isrc)
129 KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
130 vector = isrc->is_pic->pic_vector(isrc);
131 if (interrupt_sources[vector] != NULL)
133 error = intr_event_create(&isrc->is_event, isrc, 0, vector,
134 intr_disable_src, (mask_fn)isrc->is_pic->pic_enable_source,
135 (mask_fn)isrc->is_pic->pic_eoi_source, intr_assign_cpu, "irq%d:",
139 sx_xlock(&intr_table_lock);
140 if (interrupt_sources[vector] != NULL) {
141 sx_xunlock(&intr_table_lock);
142 intr_event_destroy(isrc->is_event);
145 intrcnt_register(isrc);
146 interrupt_sources[vector] = isrc;
147 isrc->is_handlers = 0;
148 sx_xunlock(&intr_table_lock);
153 intr_lookup_source(int vector)
156 return (interrupt_sources[vector]);
160 intr_add_handler(const char *name, int vector, driver_filter_t filter,
161 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
166 isrc = intr_lookup_source(vector);
169 error = intr_event_add_handler(isrc->is_event, name, filter, handler,
170 arg, intr_priority(flags), flags, cookiep);
172 sx_xlock(&intr_table_lock);
173 intrcnt_updatename(isrc);
175 if (isrc->is_handlers == 1) {
178 intr_assign_next_cpu(isrc);
180 isrc->is_pic->pic_enable_intr(isrc);
181 isrc->is_pic->pic_enable_source(isrc);
183 sx_xunlock(&intr_table_lock);
189 intr_remove_handler(void *cookie)
194 isrc = intr_handler_source(cookie);
195 error = intr_event_remove_handler(cookie);
197 sx_xlock(&intr_table_lock);
199 if (isrc->is_handlers == 0) {
200 isrc->is_pic->pic_disable_source(isrc, PIC_NO_EOI);
201 isrc->is_pic->pic_disable_intr(isrc);
203 intrcnt_updatename(isrc);
204 sx_xunlock(&intr_table_lock);
210 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
214 isrc = intr_lookup_source(vector);
217 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
221 intr_disable_src(void *arg)
226 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
230 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
232 struct intr_event *ie;
239 * We count software interrupts when we process them. The
240 * code here follows previous practice, but there's an
241 * argument for counting hardware interrupts when they're
245 PCPU_INC(cnt.v_intr);
250 * XXX: We assume that IRQ 0 is only used for the ISA timer
253 vector = isrc->is_pic->pic_vector(isrc);
258 * For stray interrupts, mask and EOI the source, bump the
259 * stray count, and log the condition.
261 if (intr_event_handle(ie, frame) != 0) {
262 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
263 (*isrc->is_straycount)++;
264 if (*isrc->is_straycount < MAX_STRAY_LOG)
265 log(LOG_ERR, "stray irq%d\n", vector);
266 else if (*isrc->is_straycount == MAX_STRAY_LOG)
268 "too many stray irq %d's: not logging anymore\n",
278 sx_xlock(&intr_table_lock);
279 STAILQ_FOREACH(pic, &pics, pics) {
280 if (pic->pic_resume != NULL)
281 pic->pic_resume(pic);
283 sx_xunlock(&intr_table_lock);
291 sx_xlock(&intr_table_lock);
292 STAILQ_FOREACH(pic, &pics, pics) {
293 if (pic->pic_suspend != NULL)
294 pic->pic_suspend(pic);
296 sx_xunlock(&intr_table_lock);
300 intr_assign_cpu(void *arg, u_char cpu)
306 * Don't do anything during early boot. We will pick up the
307 * assignment once the APs are started.
309 if (assign_cpu && cpu != NOCPU) {
311 sx_xlock(&intr_table_lock);
312 isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[cpu]);
313 sx_xunlock(&intr_table_lock);
322 intrcnt_setname(const char *name, int index)
325 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
330 intrcnt_updatename(struct intsrc *is)
333 intrcnt_setname(is->is_event->ie_fullname, is->is_index);
337 intrcnt_register(struct intsrc *is)
339 char straystr[MAXCOMLEN + 1];
341 KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
342 mtx_lock_spin(&intrcnt_lock);
343 is->is_index = intrcnt_index;
345 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
346 is->is_pic->pic_vector(is));
347 intrcnt_updatename(is);
348 is->is_count = &intrcnt[is->is_index];
349 intrcnt_setname(straystr, is->is_index + 1);
350 is->is_straycount = &intrcnt[is->is_index + 1];
351 mtx_unlock_spin(&intrcnt_lock);
355 intrcnt_add(const char *name, u_long **countp)
358 mtx_lock_spin(&intrcnt_lock);
359 *countp = &intrcnt[intrcnt_index];
360 intrcnt_setname(name, intrcnt_index);
362 mtx_unlock_spin(&intrcnt_lock);
366 intr_init(void *dummy __unused)
369 intrcnt_setname("???", 0);
372 sx_init(&intr_table_lock, "intr sources");
373 mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
375 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
379 * Dump data about interrupt handlers
381 DB_SHOW_COMMAND(irqs, db_show_irqs)
383 struct intsrc **isrc;
386 if (strcmp(modif, "v") == 0)
390 isrc = interrupt_sources;
391 for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
393 db_dump_intr_event((*isrc)->is_event, verbose);
399 * Support for balancing interrupt sources across CPUs. For now we just
400 * allocate CPUs round-robin.
403 /* The BSP is always a valid target. */
404 static cpumask_t intr_cpus = (1 << 0);
405 static int current_cpu;
408 intr_assign_next_cpu(struct intsrc *isrc)
412 * Assign this source to a local APIC in a round-robin fashion.
414 isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[current_cpu]);
417 if (current_cpu > mp_maxid)
419 } while (!(intr_cpus & (1 << current_cpu)));
422 /* Attempt to bind the specified IRQ to the specified CPU. */
424 intr_bind(u_int vector, u_char cpu)
428 isrc = intr_lookup_source(vector);
431 return (intr_event_bind(isrc->is_event, cpu));
435 * Add a CPU to our mask of valid CPUs that can be destinations of
439 intr_add_cpu(u_int cpu)
443 panic("%s: Invalid CPU ID", __func__);
445 printf("INTR: Adding local APIC %d as a target\n",
448 intr_cpus |= (1 << cpu);
452 * Distribute all the interrupt sources among the available CPUs once the
453 * AP's have been launched.
456 intr_shuffle_irqs(void *arg __unused)
468 /* Don't bother on UP. */
472 /* Round-robin assign a CPU to each enabled source. */
473 sx_xlock(&intr_table_lock);
475 for (i = 0; i < NUM_IO_INTS; i++) {
476 isrc = interrupt_sources[i];
477 if (isrc != NULL && isrc->is_handlers > 0) {
479 * If this event is already bound to a CPU,
480 * then assign the source to that CPU instead
481 * of picking one via round-robin.
483 if (isrc->is_event->ie_cpu != NOCPU)
484 isrc->is_pic->pic_assign_cpu(isrc,
485 cpu_apic_ids[isrc->is_event->ie_cpu]);
487 intr_assign_next_cpu(isrc);
490 sx_xunlock(&intr_table_lock);
492 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,