]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/intr_machdep.c
This commit was generated by cvs2svn to compensate for changes in r177576,
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / 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 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
37  * that source.
38  */
39
40 #include "opt_atpic.h"
41 #include "opt_ddb.h"
42
43 #include <sys/param.h>
44 #include <sys/bus.h>
45 #include <sys/interrupt.h>
46 #include <sys/ktr.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/smp.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54 #include <sys/sx.h>
55 #include <machine/clock.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/smp.h>
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61
62 #ifndef DEV_ATPIC
63 #include <machine/segments.h>
64 #include <machine/frame.h>
65 #include <dev/ic/i8259.h>
66 #include <amd64/isa/icu.h>
67 #include <amd64/isa/isa.h>
68 #endif
69
70 #define MAX_STRAY_LOG   5
71
72 typedef void (*mask_fn)(void *);
73
74 static int intrcnt_index;
75 static struct intsrc *interrupt_sources[NUM_IO_INTS];
76 static struct sx intr_table_lock;
77 static struct mtx intrcnt_lock;
78 static STAILQ_HEAD(, pic) pics;
79
80 #ifdef SMP
81 static int assign_cpu;
82
83 static void     intr_assign_next_cpu(struct intsrc *isrc);
84 #endif
85
86 static int      intr_assign_cpu(void *arg, u_char cpu);
87 static void     intr_disable_src(void *arg);
88 #ifdef INTR_FILTER
89 static void     intr_event_stray(void *cookie);
90 #endif
91 static void     intr_init(void *__dummy);
92 static int      intr_pic_registered(struct pic *pic);
93 static void     intrcnt_setname(const char *name, int index);
94 static void     intrcnt_updatename(struct intsrc *is);
95 static void     intrcnt_register(struct intsrc *is);
96
97 static int
98 intr_pic_registered(struct pic *pic)
99 {
100         struct pic *p;
101
102         STAILQ_FOREACH(p, &pics, pics) {
103                 if (p == pic)
104                         return (1);
105         }
106         return (0);
107 }
108
109 /*
110  * Register a new interrupt controller (PIC).  This is to support suspend
111  * and resume where we suspend/resume controllers rather than individual
112  * sources.  This also allows controllers with no active sources (such as
113  * 8259As in a system using the APICs) to participate in suspend and resume.
114  */
115 int
116 intr_register_pic(struct pic *pic)
117 {
118         int error;
119
120         sx_xlock(&intr_table_lock);
121         if (intr_pic_registered(pic))
122                 error = EBUSY;
123         else {
124                 STAILQ_INSERT_TAIL(&pics, pic, pics);
125                 error = 0;
126         }
127         sx_xunlock(&intr_table_lock);
128         return (error);
129 }
130
131 /*
132  * Register a new interrupt source with the global interrupt system.
133  * The global interrupts need to be disabled when this function is
134  * called.
135  */
136 int
137 intr_register_source(struct intsrc *isrc)
138 {
139         int error, vector;
140
141         KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
142         vector = isrc->is_pic->pic_vector(isrc);
143         if (interrupt_sources[vector] != NULL)
144                 return (EEXIST);
145         error = intr_event_create(&isrc->is_event, isrc, 0,
146             intr_disable_src, (mask_fn)isrc->is_pic->pic_enable_source,
147             (mask_fn)isrc->is_pic->pic_eoi_source, intr_assign_cpu, "irq%d:",
148             vector);
149         if (error)
150                 return (error);
151         sx_xlock(&intr_table_lock);
152         if (interrupt_sources[vector] != NULL) {
153                 sx_xunlock(&intr_table_lock);
154                 intr_event_destroy(isrc->is_event);
155                 return (EEXIST);
156         }
157         intrcnt_register(isrc);
158         interrupt_sources[vector] = isrc;
159         isrc->is_handlers = 0;
160         sx_xunlock(&intr_table_lock);
161         return (0);
162 }
163
164 struct intsrc *
165 intr_lookup_source(int vector)
166 {
167
168         return (interrupt_sources[vector]);
169 }
170
171 int
172 intr_add_handler(const char *name, int vector, driver_filter_t filter, 
173     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)    
174 {
175         struct intsrc *isrc;
176         int error;
177
178         isrc = intr_lookup_source(vector);
179         if (isrc == NULL)
180                 return (EINVAL);
181         error = intr_event_add_handler(isrc->is_event, name, filter, handler,
182             arg, intr_priority(flags), flags, cookiep);
183         if (error == 0) {
184                 sx_xlock(&intr_table_lock);
185                 intrcnt_updatename(isrc);
186                 isrc->is_handlers++;
187                 if (isrc->is_handlers == 1) {
188 #ifdef SMP
189                         if (assign_cpu)
190                                 intr_assign_next_cpu(isrc);
191 #endif
192                         isrc->is_pic->pic_enable_intr(isrc);
193                         isrc->is_pic->pic_enable_source(isrc);
194                 }
195                 sx_xunlock(&intr_table_lock);
196         }
197         return (error);
198 }
199
200 int
201 intr_remove_handler(void *cookie)
202 {
203         struct intsrc *isrc;
204         int error;
205
206         isrc = intr_handler_source(cookie);
207         error = intr_event_remove_handler(cookie);
208         if (error == 0) {
209                 sx_xlock(&intr_table_lock);
210                 isrc->is_handlers--;
211                 if (isrc->is_handlers == 0) {
212                         isrc->is_pic->pic_disable_source(isrc, PIC_NO_EOI);
213                         isrc->is_pic->pic_disable_intr(isrc);
214                 }
215                 intrcnt_updatename(isrc);
216                 sx_xunlock(&intr_table_lock);
217         }
218         return (error);
219 }
220
221 int
222 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
223 {
224         struct intsrc *isrc;
225
226         isrc = intr_lookup_source(vector);
227         if (isrc == NULL)
228                 return (EINVAL);
229         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
230 }
231
232 static void
233 intr_disable_src(void *arg)
234 {
235         struct intsrc *isrc;
236
237         isrc = arg;
238         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
239 }
240
241 #ifdef INTR_FILTER
242 void
243 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
244 {
245         struct thread *td;
246         struct intr_event *ie;
247         int vector;
248
249         td = curthread;
250
251         /*
252          * We count software interrupts when we process them.  The
253          * code here follows previous practice, but there's an
254          * argument for counting hardware interrupts when they're
255          * processed too.
256          */
257         (*isrc->is_count)++;
258         PCPU_INC(cnt.v_intr);
259
260         ie = isrc->is_event;
261
262         /*
263          * XXX: We assume that IRQ 0 is only used for the ISA timer
264          * device (clk).
265          */
266         vector = isrc->is_pic->pic_vector(isrc);
267         if (vector == 0)
268                 clkintr_pending = 1;
269
270         if (intr_event_handle(ie, frame) != 0)
271                 intr_event_stray(isrc);
272 }
273
274 static void
275 intr_event_stray(void *cookie)
276 {
277         struct intsrc *isrc;
278
279         isrc = cookie;
280         /*
281          * For stray interrupts, mask and EOI the source, bump the
282          * stray count, and log the condition.
283          */
284         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
285         (*isrc->is_straycount)++;
286         if (*isrc->is_straycount < MAX_STRAY_LOG)
287                 log(LOG_ERR, "stray irq%d\n", isrc->is_pic->pic_vector(isrc));
288         else if (*isrc->is_straycount == MAX_STRAY_LOG)
289                 log(LOG_CRIT,
290                     "too many stray irq %d's: not logging anymore\n",
291                     isrc->is_pic->pic_vector(isrc));
292 }
293 #else
294 void
295 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
296 {
297         struct thread *td;
298         struct intr_event *ie;
299         struct intr_handler *ih;
300         int error, vector, thread, ret;
301
302         td = curthread;
303
304         /*
305          * We count software interrupts when we process them.  The
306          * code here follows previous practice, but there's an
307          * argument for counting hardware interrupts when they're
308          * processed too.
309          */
310         (*isrc->is_count)++;
311         PCPU_INC(cnt.v_intr);
312
313         ie = isrc->is_event;
314
315         /*
316          * XXX: We assume that IRQ 0 is only used for the ISA timer
317          * device (clk).
318          */
319         vector = isrc->is_pic->pic_vector(isrc);
320         if (vector == 0)
321                 clkintr_pending = 1;
322
323         /*
324          * For stray interrupts, mask and EOI the source, bump the
325          * stray count, and log the condition.
326          */
327         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) {
328                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
329                 (*isrc->is_straycount)++;
330                 if (*isrc->is_straycount < MAX_STRAY_LOG)
331                         log(LOG_ERR, "stray irq%d\n", vector);
332                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
333                         log(LOG_CRIT,
334                             "too many stray irq %d's: not logging anymore\n",
335                             vector);
336                 return;
337         }
338
339         /*
340          * Execute fast interrupt handlers directly.
341          * To support clock handlers, if a handler registers
342          * with a NULL argument, then we pass it a pointer to
343          * a trapframe as its argument.
344          */
345         td->td_intr_nesting_level++;
346         ret = 0;
347         thread = 0;
348         critical_enter();
349         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
350                 if (ih->ih_filter == NULL) {
351                         thread = 1;
352                         continue;
353                 }
354                 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
355                     ih->ih_filter, ih->ih_argument == NULL ? frame :
356                     ih->ih_argument, ih->ih_name);
357                 if (ih->ih_argument == NULL)
358                         ret = ih->ih_filter(frame);
359                 else
360                         ret = ih->ih_filter(ih->ih_argument);
361                 /*
362                  * Wrapper handler special case: see
363                  * i386/intr_machdep.c::intr_execute_handlers()
364                  */
365                 if (!thread) {
366                         if (ret == FILTER_SCHEDULE_THREAD)
367                                 thread = 1;
368                 }
369         }
370
371         /*
372          * If there are any threaded handlers that need to run,
373          * mask the source as well as sending it an EOI.  Otherwise,
374          * just send it an EOI but leave it unmasked.
375          */
376         if (thread)
377                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
378         else
379                 isrc->is_pic->pic_eoi_source(isrc);
380
381         /* Schedule the ithread if needed. */
382         if (thread) {
383                 error = intr_event_schedule_thread(ie);
384                 KASSERT(error == 0, ("bad stray interrupt"));
385         }
386         critical_exit();
387         td->td_intr_nesting_level--;
388 }
389 #endif
390
391 void
392 intr_resume(void)
393 {
394         struct pic *pic;
395
396 #ifndef DEV_ATPIC
397         atpic_reset();
398 #endif
399         sx_xlock(&intr_table_lock);
400         STAILQ_FOREACH(pic, &pics, pics) {
401                 if (pic->pic_resume != NULL)
402                         pic->pic_resume(pic);
403         }
404         sx_xunlock(&intr_table_lock);
405 }
406
407 void
408 intr_suspend(void)
409 {
410         struct pic *pic;
411
412         sx_xlock(&intr_table_lock);
413         STAILQ_FOREACH(pic, &pics, pics) {
414                 if (pic->pic_suspend != NULL)
415                         pic->pic_suspend(pic);
416         }
417         sx_xunlock(&intr_table_lock);
418 }
419
420 static int
421 intr_assign_cpu(void *arg, u_char cpu)
422 {
423 #ifdef SMP
424         struct intsrc *isrc;    
425
426         /*
427          * Don't do anything during early boot.  We will pick up the
428          * assignment once the APs are started.
429          */
430         if (assign_cpu && cpu != NOCPU) {
431                 isrc = arg;
432                 sx_xlock(&intr_table_lock);
433                 isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[cpu]);
434                 sx_xunlock(&intr_table_lock);
435         }
436         return (0);
437 #else
438         return (EOPNOTSUPP);
439 #endif
440 }
441
442 static void
443 intrcnt_setname(const char *name, int index)
444 {
445
446         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
447             MAXCOMLEN, name);
448 }
449
450 static void
451 intrcnt_updatename(struct intsrc *is)
452 {
453
454         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
455 }
456
457 static void
458 intrcnt_register(struct intsrc *is)
459 {
460         char straystr[MAXCOMLEN + 1];
461
462         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
463         mtx_lock_spin(&intrcnt_lock);
464         is->is_index = intrcnt_index;
465         intrcnt_index += 2;
466         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
467             is->is_pic->pic_vector(is));
468         intrcnt_updatename(is);
469         is->is_count = &intrcnt[is->is_index];
470         intrcnt_setname(straystr, is->is_index + 1);
471         is->is_straycount = &intrcnt[is->is_index + 1];
472         mtx_unlock_spin(&intrcnt_lock);
473 }
474
475 void
476 intrcnt_add(const char *name, u_long **countp)
477 {
478
479         mtx_lock_spin(&intrcnt_lock);
480         *countp = &intrcnt[intrcnt_index];
481         intrcnt_setname(name, intrcnt_index);
482         intrcnt_index++;
483         mtx_unlock_spin(&intrcnt_lock);
484 }
485
486 static void
487 intr_init(void *dummy __unused)
488 {
489
490         intrcnt_setname("???", 0);
491         intrcnt_index = 1;
492         STAILQ_INIT(&pics);
493         sx_init(&intr_table_lock, "intr sources");
494         mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
495 }
496 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
497
498 #ifndef DEV_ATPIC
499 /* Initialize the two 8259A's to a known-good shutdown state. */
500 void
501 atpic_reset(void)
502 {
503
504         outb(IO_ICU1, ICW1_RESET | ICW1_IC4);
505         outb(IO_ICU1 + ICU_IMR_OFFSET, IDT_IO_INTS);
506         outb(IO_ICU1 + ICU_IMR_OFFSET, 1 << 2);
507         outb(IO_ICU1 + ICU_IMR_OFFSET, ICW4_8086);
508         outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff);
509         outb(IO_ICU1, OCW3_SEL | OCW3_RR);
510
511         outb(IO_ICU2, ICW1_RESET | ICW1_IC4);
512         outb(IO_ICU2 + ICU_IMR_OFFSET, IDT_IO_INTS + 8);
513         outb(IO_ICU2 + ICU_IMR_OFFSET, 2);
514         outb(IO_ICU2 + ICU_IMR_OFFSET, ICW4_8086);
515         outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff);
516         outb(IO_ICU2, OCW3_SEL | OCW3_RR);
517 }
518 #endif
519
520 #ifdef DDB
521 /*
522  * Dump data about interrupt handlers
523  */
524 DB_SHOW_COMMAND(irqs, db_show_irqs)
525 {
526         struct intsrc **isrc;
527         int i, verbose;
528
529         if (strcmp(modif, "v") == 0)
530                 verbose = 1;
531         else
532                 verbose = 0;
533         isrc = interrupt_sources;
534         for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
535                 if (*isrc != NULL)
536                         db_dump_intr_event((*isrc)->is_event, verbose);
537 }
538 #endif
539
540 #ifdef SMP
541 /*
542  * Support for balancing interrupt sources across CPUs.  For now we just
543  * allocate CPUs round-robin.
544  */
545
546 /* The BSP is always a valid target. */
547 static cpumask_t intr_cpus = (1 << 0);
548 static int current_cpu;
549
550 static void
551 intr_assign_next_cpu(struct intsrc *isrc)
552 {
553
554         /*
555          * Assign this source to a local APIC in a round-robin fashion.
556          */
557         isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[current_cpu]);
558         do {
559                 current_cpu++;
560                 if (current_cpu > mp_maxid)
561                         current_cpu = 0;
562         } while (!(intr_cpus & (1 << current_cpu)));
563 }
564
565 /* Attempt to bind the specified IRQ to the specified CPU. */
566 int
567 intr_bind(u_int vector, u_char cpu)
568 {
569         struct intsrc *isrc;
570
571         isrc = intr_lookup_source(vector);
572         if (isrc == NULL)
573                 return (EINVAL);
574         return (intr_event_bind(isrc->is_event, cpu));
575 }
576
577 /*
578  * Add a CPU to our mask of valid CPUs that can be destinations of
579  * interrupts.
580  */
581 void
582 intr_add_cpu(u_int cpu)
583 {
584
585         if (cpu >= MAXCPU)
586                 panic("%s: Invalid CPU ID", __func__);
587         if (bootverbose)
588                 printf("INTR: Adding local APIC %d as a target\n",
589                     cpu_apic_ids[cpu]);
590
591         intr_cpus |= (1 << cpu);
592 }
593
594 /*
595  * Distribute all the interrupt sources among the available CPUs once the
596  * AP's have been launched.
597  */
598 static void
599 intr_shuffle_irqs(void *arg __unused)
600 {
601         struct intsrc *isrc;
602         int i;
603
604         /* Don't bother on UP. */
605         if (mp_ncpus == 1)
606                 return;
607
608         /* Round-robin assign a CPU to each enabled source. */
609         sx_xlock(&intr_table_lock);
610         assign_cpu = 1;
611         for (i = 0; i < NUM_IO_INTS; i++) {
612                 isrc = interrupt_sources[i];
613                 if (isrc != NULL && isrc->is_handlers > 0) {
614                         /*
615                          * If this event is already bound to a CPU,
616                          * then assign the source to that CPU instead
617                          * of picking one via round-robin.
618                          */
619                         if (isrc->is_event->ie_cpu != NOCPU)
620                                 isrc->is_pic->pic_assign_cpu(isrc,
621                                     cpu_apic_ids[isrc->is_event->ie_cpu]);
622                         else
623                                 intr_assign_next_cpu(isrc);
624                 }
625         }
626         sx_xunlock(&intr_table_lock);
627 }
628 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,
629     NULL);
630 #endif