]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/intr_machdep.c
Bring in the reminaing bits to make interrupt filtering work:
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / intr_machdep.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 /*
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
37  * that source.
38  */
39
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/interrupt.h>
45 #include <sys/lock.h>
46 #include <sys/ktr.h>
47 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.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>
55 #ifdef DDB
56 #include <ddb/ddb.h>
57 #endif
58
59 #define MAX_STRAY_LOG   5
60
61 typedef void (*mask_fn)(void *);
62
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;
67
68 #ifdef INTR_FILTER
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);
72 #endif
73
74 #ifdef SMP
75 static int assign_cpu;
76
77 static void     intr_assign_next_cpu(struct intsrc *isrc);
78 #endif
79
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);
85
86 static int
87 intr_pic_registered(struct pic *pic)
88 {
89         struct pic *p;
90
91         STAILQ_FOREACH(p, &pics, pics) {
92                 if (p == pic)
93                         return (1);
94         }
95         return (0);
96 }
97
98 /*
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.
103  */
104 int
105 intr_register_pic(struct pic *pic)
106 {
107         int error;
108
109         mtx_lock_spin(&intr_table_lock);
110         if (intr_pic_registered(pic))
111                 error = EBUSY;
112         else {
113                 STAILQ_INSERT_TAIL(&pics, pic, pics);
114                 error = 0;
115         }
116         mtx_unlock_spin(&intr_table_lock);
117         return (error);
118 }
119
120 /*
121  * Register a new interrupt source with the global interrupt system.
122  * The global interrupts need to be disabled when this function is
123  * called.
124  */
125 int
126 intr_register_source(struct intsrc *isrc)
127 {
128         int error, vector;
129
130         KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
131         vector = isrc->is_pic->pic_vector(isrc);
132         if (interrupt_sources[vector] != NULL)
133                 return (EEXIST);
134 #ifdef INTR_FILTER
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);
138 #else
139         error = intr_event_create(&isrc->is_event, isrc, 0,
140             (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
141 #endif
142         if (error)
143                 return (error);
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);
148                 return (EEXIST);
149         }
150         intrcnt_register(isrc);
151         interrupt_sources[vector] = isrc;
152         isrc->is_enabled = 0;
153         mtx_unlock_spin(&intr_table_lock);
154         return (0);
155 }
156
157 struct intsrc *
158 intr_lookup_source(int vector)
159 {
160
161         return (interrupt_sources[vector]);
162 }
163
164 int
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)
167 {
168         struct intsrc *isrc;
169         int error;
170
171         isrc = intr_lookup_source(vector);
172         if (isrc == NULL)
173                 return (EINVAL);
174         error = intr_event_add_handler(isrc->is_event, name, filter, handler,
175             arg, intr_priority(flags), flags, cookiep);
176         if (error == 0) {
177                 intrcnt_updatename(isrc);
178                 mtx_lock_spin(&intr_table_lock);
179                 if (!isrc->is_enabled) {
180                         isrc->is_enabled = 1;
181 #ifdef SMP
182                         if (assign_cpu)
183                                 intr_assign_next_cpu(isrc);
184 #endif
185                         mtx_unlock_spin(&intr_table_lock);
186                         isrc->is_pic->pic_enable_intr(isrc);
187                 } else
188                         mtx_unlock_spin(&intr_table_lock);
189                 isrc->is_pic->pic_enable_source(isrc);
190         }
191         return (error);
192 }
193
194 int
195 intr_remove_handler(void *cookie)
196 {
197         struct intsrc *isrc;
198         int error;
199
200         isrc = intr_handler_source(cookie);
201         error = intr_event_remove_handler(cookie);
202         if (error == 0)
203                 intrcnt_updatename(isrc);
204         return (error);
205 }
206
207 int
208 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
209 {
210         struct intsrc *isrc;
211
212         isrc = intr_lookup_source(vector);
213         if (isrc == NULL)
214                 return (EINVAL);
215         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
216 }
217
218 #ifdef INTR_FILTER
219 void
220 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
221 {
222         struct thread *td;
223         struct intr_event *ie;
224         int vector;
225
226         td = curthread;
227
228         /*
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
232          * processed too.
233          */
234         (*isrc->is_count)++;
235         PCPU_LAZY_INC(cnt.v_intr);
236
237         ie = isrc->is_event;
238
239         /*
240          * XXX: We assume that IRQ 0 is only used for the ISA timer
241          * device (clk).
242          */
243         vector = isrc->is_pic->pic_vector(isrc);
244         if (vector == 0)
245                 clkintr_pending = 1;
246
247         if (intr_event_handle(ie, frame) != 0)
248                 intr_event_stray(isrc);         
249 }
250
251 static void
252 intr_event_stray(void *cookie)
253 {
254         struct intsrc *isrc;
255
256         isrc = cookie;
257         /*
258          * For stray interrupts, mask and EOI the source, bump the
259          * stray count, and log the condition.
260          */
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)
266                 log(LOG_CRIT,
267                     "too many stray irq %d's: not logging anymore\n",
268                     isrc->is_pic->pic_vector(isrc));
269 }
270
271 static void
272 intr_eoi_src(void *arg)
273 {
274         struct intsrc *isrc;
275
276         isrc = arg;
277         isrc->is_pic->pic_eoi_source(isrc);
278 }
279
280 static void
281 intr_disab_eoi_src(void *arg)
282 {
283         struct intsrc *isrc;
284
285         isrc = arg;
286         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
287 }
288 #else
289 void
290 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
291 {
292         struct thread *td;
293         struct intr_event *ie;
294         struct intr_handler *ih;
295         int error, vector, thread;
296
297         td = curthread;
298
299         /*
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
303          * processed too.
304          */
305         (*isrc->is_count)++;
306         PCPU_LAZY_INC(cnt.v_intr);
307
308         ie = isrc->is_event;
309
310         /*
311          * XXX: We assume that IRQ 0 is only used for the ISA timer
312          * device (clk).
313          */
314         vector = isrc->is_pic->pic_vector(isrc);
315         if (vector == 0)
316                 clkintr_pending = 1;
317
318         /*
319          * For stray interrupts, mask and EOI the source, bump the
320          * stray count, and log the condition.
321          */
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)
328                         log(LOG_CRIT,
329                             "too many stray irq %d's: not logging anymore\n",
330                             vector);
331                 return;
332         }
333
334         /*
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.
339          */
340         td->td_intr_nesting_level++;
341         thread = 0;
342         critical_enter();
343         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
344                 if (ih->ih_filter == NULL) {
345                         thread = 1;
346                         continue;
347                 }
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);
353                 else
354                         ih->ih_filter(ih->ih_argument);
355         }
356
357         /*
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.
361          */
362         if (thread)
363                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
364         else
365                 isrc->is_pic->pic_eoi_source(isrc);
366         critical_exit();
367
368         /* Schedule the ithread if needed. */
369         if (thread) {
370                 error = intr_event_schedule_thread(ie);
371                 KASSERT(error == 0, ("bad stray interrupt"));
372         }
373         td->td_intr_nesting_level--;
374 }
375 #endif
376
377 void
378 intr_resume(void)
379 {
380         struct pic *pic;
381
382         mtx_lock_spin(&intr_table_lock);
383         STAILQ_FOREACH(pic, &pics, pics) {
384                 if (pic->pic_resume != NULL)
385                         pic->pic_resume(pic);
386         }
387         mtx_unlock_spin(&intr_table_lock);
388 }
389
390 void
391 intr_suspend(void)
392 {
393         struct pic *pic;
394
395         mtx_lock_spin(&intr_table_lock);
396         STAILQ_FOREACH(pic, &pics, pics) {
397                 if (pic->pic_suspend != NULL)
398                         pic->pic_suspend(pic);
399         }
400         mtx_unlock_spin(&intr_table_lock);
401 }
402
403 static void
404 intrcnt_setname(const char *name, int index)
405 {
406
407         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
408             MAXCOMLEN, name);
409 }
410
411 static void
412 intrcnt_updatename(struct intsrc *is)
413 {
414
415         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
416 }
417
418 static void
419 intrcnt_register(struct intsrc *is)
420 {
421         char straystr[MAXCOMLEN + 1];
422
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;
426         intrcnt_index += 2;
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];
433 }
434
435 void
436 intrcnt_add(const char *name, u_long **countp)
437 {
438
439         mtx_lock_spin(&intr_table_lock);
440         *countp = &intrcnt[intrcnt_index];
441         intrcnt_setname(name, intrcnt_index);
442         intrcnt_index++;
443         mtx_unlock_spin(&intr_table_lock);
444 }
445
446 static void
447 intr_init(void *dummy __unused)
448 {
449
450         intrcnt_setname("???", 0);
451         intrcnt_index = 1;
452         STAILQ_INIT(&pics);
453         mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
454 }
455 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
456
457 #ifdef DDB
458 /*
459  * Dump data about interrupt handlers
460  */
461 DB_SHOW_COMMAND(irqs, db_show_irqs)
462 {
463         struct intsrc **isrc;
464         int i, verbose;
465
466         if (strcmp(modif, "v") == 0)
467                 verbose = 1;
468         else
469                 verbose = 0;
470         isrc = interrupt_sources;
471         for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
472                 if (*isrc != NULL)
473                         db_dump_intr_event((*isrc)->is_event, verbose);
474 }
475 #endif
476
477 #ifdef SMP
478 /*
479  * Support for balancing interrupt sources across CPUs.  For now we just
480  * allocate CPUs round-robin.
481  */
482
483 /* The BSP is always a valid target. */
484 static cpumask_t intr_cpus = (1 << 0);
485 static int current_cpu, num_cpus = 1;
486
487 static void
488 intr_assign_next_cpu(struct intsrc *isrc)
489 {
490         struct pic *pic;
491         u_int apic_id;
492
493         /*
494          * Assign this source to a local APIC in a round-robin fashion.
495          */
496         pic = isrc->is_pic;
497         apic_id = cpu_apic_ids[current_cpu];
498         pic->pic_assign_cpu(isrc, apic_id);
499         do {
500                 current_cpu++;
501                 if (current_cpu >= num_cpus)
502                         current_cpu = 0;
503         } while (!(intr_cpus & (1 << current_cpu)));
504 }
505
506 /*
507  * Add a CPU to our mask of valid CPUs that can be destinations of
508  * interrupts.
509  */
510 void
511 intr_add_cpu(u_int cpu)
512 {
513
514         if (cpu >= MAXCPU)
515                 panic("%s: Invalid CPU ID", __func__);
516         if (bootverbose)
517                 printf("INTR: Adding local APIC %d as a target\n",
518                     cpu_apic_ids[cpu]);
519
520         intr_cpus |= (1 << cpu);
521         num_cpus++;
522 }
523
524 /*
525  * Distribute all the interrupt sources among the available CPUs once the
526  * AP's have been launched.
527  */
528 static void
529 intr_shuffle_irqs(void *arg __unused)
530 {
531         struct intsrc *isrc;
532         int i;
533
534         /* Don't bother on UP. */
535         if (num_cpus <= 1)
536                 return;
537
538         /* Round-robin assign a CPU to each enabled source. */
539         mtx_lock_spin(&intr_table_lock);
540         assign_cpu = 1;
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);
545         }
546         mtx_unlock_spin(&intr_table_lock);
547 }
548 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs, NULL)
549 #endif