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;
67 static int assign_cpu;
69 static void intr_assign_next_cpu(struct intsrc *isrc);
72 static void intr_init(void *__dummy);
73 static void intrcnt_setname(const char *name, int index);
74 static void intrcnt_updatename(struct intsrc *is);
75 static void intrcnt_register(struct intsrc *is);
78 * Register a new interrupt source with the global interrupt system.
79 * The global interrupts need to be disabled when this function is
83 intr_register_source(struct intsrc *isrc)
87 vector = isrc->is_pic->pic_vector(isrc);
88 if (interrupt_sources[vector] != NULL)
90 error = intr_event_create(&isrc->is_event, isrc, 0,
91 (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
94 mtx_lock_spin(&intr_table_lock);
95 if (interrupt_sources[vector] != NULL) {
96 mtx_unlock_spin(&intr_table_lock);
97 intr_event_destroy(isrc->is_event);
100 intrcnt_register(isrc);
101 interrupt_sources[vector] = isrc;
102 isrc->is_enabled = 0;
103 mtx_unlock_spin(&intr_table_lock);
108 intr_lookup_source(int vector)
111 return (interrupt_sources[vector]);
115 intr_add_handler(const char *name, int vector, driver_intr_t handler,
116 void *arg, enum intr_type flags, void **cookiep)
121 isrc = intr_lookup_source(vector);
124 error = intr_event_add_handler(isrc->is_event, name, handler, arg,
125 intr_priority(flags), flags, cookiep);
127 intrcnt_updatename(isrc);
128 mtx_lock_spin(&intr_table_lock);
129 if (!isrc->is_enabled) {
130 isrc->is_enabled = 1;
133 intr_assign_next_cpu(isrc);
135 mtx_unlock_spin(&intr_table_lock);
136 isrc->is_pic->pic_enable_intr(isrc);
138 mtx_unlock_spin(&intr_table_lock);
139 isrc->is_pic->pic_enable_source(isrc);
145 intr_remove_handler(void *cookie)
149 error = intr_event_remove_handler(cookie);
152 intrcnt_updatename(/* XXX */);
158 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
162 isrc = intr_lookup_source(vector);
165 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
169 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
172 struct intr_event *ie;
173 struct intr_handler *ih;
174 int error, vector, thread;
179 * We count software interrupts when we process them. The
180 * code here follows previous practice, but there's an
181 * argument for counting hardware interrupts when they're
185 PCPU_LAZY_INC(cnt.v_intr);
190 * XXX: We assume that IRQ 0 is only used for the ISA timer
193 vector = isrc->is_pic->pic_vector(isrc);
198 * For stray interrupts, mask and EOI the source, bump the
199 * stray count, and log the condition.
201 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) {
202 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
203 (*isrc->is_straycount)++;
204 if (*isrc->is_straycount < MAX_STRAY_LOG)
205 log(LOG_ERR, "stray irq%d\n", vector);
206 else if (*isrc->is_straycount == MAX_STRAY_LOG)
208 "too many stray irq %d's: not logging anymore\n",
214 * Execute fast interrupt handlers directly.
215 * To support clock handlers, if a handler registers
216 * with a NULL argument, then we pass it a pointer to
217 * a trapframe as its argument.
219 td->td_intr_nesting_level++;
222 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
223 if (!(ih->ih_flags & IH_FAST)) {
227 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
228 ih->ih_handler, ih->ih_argument == NULL ? frame :
229 ih->ih_argument, ih->ih_name);
230 if (ih->ih_argument == NULL)
231 ih->ih_handler(frame);
233 ih->ih_handler(ih->ih_argument);
237 * If there are any threaded handlers that need to run,
238 * mask the source as well as sending it an EOI. Otherwise,
239 * just send it an EOI but leave it unmasked.
242 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
244 isrc->is_pic->pic_eoi_source(isrc);
247 /* Schedule the ithread if needed. */
249 error = intr_event_schedule_thread(ie);
250 KASSERT(error == 0, ("bad stray interrupt"));
252 td->td_intr_nesting_level--;
258 struct intsrc **isrc;
261 mtx_lock_spin(&intr_table_lock);
262 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
263 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
264 (*isrc)->is_pic->pic_resume(*isrc);
265 mtx_unlock_spin(&intr_table_lock);
271 struct intsrc **isrc;
274 mtx_lock_spin(&intr_table_lock);
275 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
276 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
277 (*isrc)->is_pic->pic_suspend(*isrc);
278 mtx_unlock_spin(&intr_table_lock);
282 intrcnt_setname(const char *name, int index)
285 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
290 intrcnt_updatename(struct intsrc *is)
293 intrcnt_setname(is->is_event->ie_fullname, is->is_index);
297 intrcnt_register(struct intsrc *is)
299 char straystr[MAXCOMLEN + 1];
301 /* mtx_assert(&intr_table_lock, MA_OWNED); */
302 KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
303 is->is_index = intrcnt_index;
305 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
306 is->is_pic->pic_vector(is));
307 intrcnt_updatename(is);
308 is->is_count = &intrcnt[is->is_index];
309 intrcnt_setname(straystr, is->is_index + 1);
310 is->is_straycount = &intrcnt[is->is_index + 1];
314 intrcnt_add(const char *name, u_long **countp)
317 mtx_lock_spin(&intr_table_lock);
318 *countp = &intrcnt[intrcnt_index];
319 intrcnt_setname(name, intrcnt_index);
321 mtx_unlock_spin(&intr_table_lock);
325 intr_init(void *dummy __unused)
328 intrcnt_setname("???", 0);
330 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
332 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
336 * Dump data about interrupt handlers
338 DB_SHOW_COMMAND(irqs, db_show_irqs)
340 struct intsrc **isrc;
343 if (strcmp(modif, "v") == 0)
347 isrc = interrupt_sources;
348 for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
350 db_dump_intr_event((*isrc)->is_event, verbose);
356 * Support for balancing interrupt sources across CPUs. For now we just
357 * allocate CPUs round-robin.
360 static u_int cpu_apic_ids[MAXCPU];
361 static int current_cpu, num_cpus;
364 intr_assign_next_cpu(struct intsrc *isrc)
370 * Assign this source to a local APIC in a round-robin fashion.
373 apic_id = cpu_apic_ids[current_cpu];
375 if (current_cpu >= num_cpus)
378 printf("INTR: Assigning IRQ %d", pic->pic_vector(isrc));
379 printf(" to local APIC %u\n", apic_id);
381 pic->pic_assign_cpu(isrc, apic_id);
385 * Add a local APIC ID to our list of valid local APIC IDs that can
386 * be destinations of interrupts.
389 intr_add_cpu(u_int apic_id)
393 printf("INTR: Adding local APIC %d as a target\n", apic_id);
394 if (num_cpus >= MAXCPU)
395 panic("WARNING: Local APIC IDs exhausted!");
396 cpu_apic_ids[num_cpus] = apic_id;
401 * Distribute all the interrupt sources among the available CPUs once the
402 * AP's have been launched.
405 intr_shuffle_irqs(void *arg __unused)
410 /* Don't bother on UP. */
414 /* Round-robin assign each enabled source a CPU. */
415 mtx_lock_spin(&intr_table_lock);
417 for (i = 0; i < NUM_IO_INTS; i++) {
418 isrc = interrupt_sources[i];
419 if (isrc != NULL && isrc->is_enabled)
420 intr_assign_next_cpu(isrc);
422 mtx_unlock_spin(&intr_table_lock);
424 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs, NULL)