]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/io_apic.c
This commit was generated by cvs2svn to compensate for changes in r159985,
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / io_apic.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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_atpic.h"
34 #include "opt_isa.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47
48 #include <machine/apicreg.h>
49 #include <machine/frame.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/apicvar.h>
52 #include <machine/segments.h>
53
54 #define IOAPIC_ISA_INTS         16
55 #define IOAPIC_MEM_REGION       32
56 #define IOAPIC_REDTBL_LO(i)     (IOAPIC_REDTBL + (i) * 2)
57 #define IOAPIC_REDTBL_HI(i)     (IOAPIC_REDTBL_LO(i) + 1)
58
59 #define IRQ_EXTINT              (NUM_IO_INTS + 1)
60 #define IRQ_NMI                 (NUM_IO_INTS + 2)
61 #define IRQ_SMI                 (NUM_IO_INTS + 3)
62 #define IRQ_DISABLED            (NUM_IO_INTS + 4)
63
64 #define TODO            printf("%s: not implemented!\n", __func__)
65
66 static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
67
68 /*
69  * I/O APIC interrupt source driver.  Each pin is assigned an IRQ cookie
70  * as laid out in the ACPI System Interrupt number model where each I/O
71  * APIC has a contiguous chunk of the System Interrupt address space.
72  * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
73  * IRQs behave as PCI IRQs by default.  We also assume that the pin for
74  * IRQ 0 is actually an ExtINT pin.  The apic enumerators override the
75  * configuration of individual pins as indicated by their tables.
76  *
77  * Documentation for the I/O APIC: "82093AA I/O Advanced Programmable
78  * Interrupt Controller (IOAPIC)", May 1996, Intel Corp.
79  * ftp://download.intel.com/design/chipsets/datashts/29056601.pdf
80  */
81
82 struct ioapic_intsrc {
83         struct intsrc io_intsrc;
84         u_int io_irq;
85         u_int io_intpin:8;
86         u_int io_vector:8;
87         u_int io_cpu:8;
88         u_int io_activehi:1;
89         u_int io_edgetrigger:1;
90         u_int io_masked:1;
91         int io_bus:4;
92         uint32_t io_lowreg;
93 };
94
95 struct ioapic {
96         struct pic io_pic;
97         u_int io_id:8;                  /* logical ID */
98         u_int io_apic_id:4;
99         u_int io_intbase:8;             /* System Interrupt base */
100         u_int io_numintr:8;
101         volatile ioapic_t *io_addr;     /* XXX: should use bus_space */
102         STAILQ_ENTRY(ioapic) io_next;
103         struct ioapic_intsrc io_pins[0];
104 };
105
106 static u_int    ioapic_read(volatile ioapic_t *apic, int reg);
107 static void     ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
108 static const char *ioapic_bus_string(int bus_type);
109 static void     ioapic_print_irq(struct ioapic_intsrc *intpin);
110 static void     ioapic_enable_source(struct intsrc *isrc);
111 static void     ioapic_disable_source(struct intsrc *isrc, int eoi);
112 static void     ioapic_eoi_source(struct intsrc *isrc);
113 static void     ioapic_enable_intr(struct intsrc *isrc);
114 static int      ioapic_vector(struct intsrc *isrc);
115 static int      ioapic_source_pending(struct intsrc *isrc);
116 static int      ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
117                     enum intr_polarity pol);
118 static void     ioapic_suspend(struct intsrc *isrc);
119 static void     ioapic_resume(struct intsrc *isrc);
120 static void     ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
121 static void     ioapic_program_intpin(struct ioapic_intsrc *intpin);
122
123 static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
124 struct pic ioapic_template = { ioapic_enable_source, ioapic_disable_source,
125                                ioapic_eoi_source, ioapic_enable_intr,
126                                ioapic_vector, ioapic_source_pending,
127                                ioapic_suspend, ioapic_resume,
128                                ioapic_config_intr, ioapic_assign_cpu };
129
130 static int next_ioapic_base;
131 static u_int next_id;
132
133 SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
134 static int enable_extint;
135 SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
136     "Enable the ExtINT pin in the first I/O APIC");
137 TUNABLE_INT("hw.apic.enable_extint", &enable_extint);
138
139 static __inline void
140 _ioapic_eoi_source(struct intsrc *isrc)
141 {
142         lapic_eoi();
143 }
144
145 static u_int
146 ioapic_read(volatile ioapic_t *apic, int reg)
147 {
148
149         mtx_assert(&icu_lock, MA_OWNED);
150         apic->ioregsel = reg;
151         return (apic->iowin);
152 }
153
154 static void
155 ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
156 {
157
158         mtx_assert(&icu_lock, MA_OWNED);
159         apic->ioregsel = reg;
160         apic->iowin = val;
161 }
162
163 static const char *
164 ioapic_bus_string(int bus_type)
165 {
166
167         switch (bus_type) {
168         case APIC_BUS_ISA:
169                 return ("ISA");
170         case APIC_BUS_EISA:
171                 return ("EISA");
172         case APIC_BUS_PCI:
173                 return ("PCI");
174         default:
175                 return ("unknown");
176         }
177 }
178
179 static void
180 ioapic_print_irq(struct ioapic_intsrc *intpin)
181 {
182
183         switch (intpin->io_irq) {
184         case IRQ_DISABLED:
185                 printf("disabled");
186                 break;
187         case IRQ_EXTINT:
188                 printf("ExtINT");
189                 break;
190         case IRQ_NMI:
191                 printf("NMI");
192                 break;
193         case IRQ_SMI:
194                 printf("SMI");
195                 break;
196         default:
197                 printf("%s IRQ %u", ioapic_bus_string(intpin->io_bus),
198                     intpin->io_irq);
199         }
200 }
201
202 static void
203 ioapic_enable_source(struct intsrc *isrc)
204 {
205         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
206         struct ioapic *io = (struct ioapic *)isrc->is_pic;
207         uint32_t flags;
208
209         mtx_lock_spin(&icu_lock);
210         if (intpin->io_masked) {
211                 flags = intpin->io_lowreg & ~IOART_INTMASK;
212                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
213                     flags);
214                 intpin->io_masked = 0;
215         }
216         mtx_unlock_spin(&icu_lock);
217 }
218
219 static void
220 ioapic_disable_source(struct intsrc *isrc, int eoi)
221 {
222         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
223         struct ioapic *io = (struct ioapic *)isrc->is_pic;
224         uint32_t flags;
225
226         mtx_lock_spin(&icu_lock);
227         if (!intpin->io_masked && !intpin->io_edgetrigger) {
228                 flags = intpin->io_lowreg | IOART_INTMSET;
229                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
230                     flags);
231                 intpin->io_masked = 1;
232         }
233
234         if (eoi == PIC_EOI)
235                 _ioapic_eoi_source(isrc);
236
237         mtx_unlock_spin(&icu_lock);
238 }
239
240 static void
241 ioapic_eoi_source(struct intsrc *isrc)
242 {
243
244         _ioapic_eoi_source(isrc);
245 }
246
247 /*
248  * Completely program an intpin based on the data in its interrupt source
249  * structure.
250  */
251 static void
252 ioapic_program_intpin(struct ioapic_intsrc *intpin)
253 {
254         struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
255         uint32_t low, high, value;
256
257         /*
258          * If a pin is completely invalid or if it is valid but hasn't
259          * been enabled yet, just ensure that the pin is masked.
260          */
261         if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq < NUM_IO_INTS &&
262             intpin->io_vector == 0)) {
263                 mtx_lock_spin(&icu_lock);
264                 low = ioapic_read(io->io_addr,
265                     IOAPIC_REDTBL_LO(intpin->io_intpin));
266                 if ((low & IOART_INTMASK) == IOART_INTMCLR)
267                         ioapic_write(io->io_addr,
268                             IOAPIC_REDTBL_LO(intpin->io_intpin),
269                             low | IOART_INTMSET);
270                 mtx_unlock_spin(&icu_lock);
271                 return;
272         }
273
274         /* Set the destination. */
275         low = IOART_DESTPHY;
276         high = intpin->io_cpu << APIC_ID_SHIFT;
277
278         /* Program the rest of the low word. */
279         if (intpin->io_edgetrigger)
280                 low |= IOART_TRGREDG;
281         else
282                 low |= IOART_TRGRLVL;
283         if (intpin->io_activehi)
284                 low |= IOART_INTAHI;
285         else
286                 low |= IOART_INTALO;
287         if (intpin->io_masked)
288                 low |= IOART_INTMSET;
289         switch (intpin->io_irq) {
290         case IRQ_EXTINT:
291                 KASSERT(intpin->io_edgetrigger,
292                     ("ExtINT not edge triggered"));
293                 low |= IOART_DELEXINT;
294                 break;
295         case IRQ_NMI:
296                 KASSERT(intpin->io_edgetrigger,
297                     ("NMI not edge triggered"));
298                 low |= IOART_DELNMI;
299                 break;
300         case IRQ_SMI:
301                 KASSERT(intpin->io_edgetrigger,
302                     ("SMI not edge triggered"));
303                 low |= IOART_DELSMI;
304                 break;
305         default:
306                 KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
307                     intpin->io_irq));
308                 low |= IOART_DELFIXED | intpin->io_vector;
309         }
310
311         /* Write the values to the APIC. */
312         mtx_lock_spin(&icu_lock);
313         intpin->io_lowreg = low;
314         ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
315         value = ioapic_read(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin));
316         value &= ~IOART_DEST;
317         value |= high;
318         ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), value);
319         mtx_unlock_spin(&icu_lock);
320 }
321
322 static void
323 ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
324 {
325         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
326         struct ioapic *io = (struct ioapic *)isrc->is_pic;
327
328         intpin->io_cpu = apic_id;
329         if (bootverbose) {
330                 printf("ioapic%u: Assigning ", io->io_id);
331                 ioapic_print_irq(intpin);
332                 printf(" to local APIC %u\n", intpin->io_cpu);
333         }
334         ioapic_program_intpin(intpin);
335 }
336
337 static void
338 ioapic_enable_intr(struct intsrc *isrc)
339 {
340         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
341         struct ioapic *io = (struct ioapic *)isrc->is_pic;
342
343         if (intpin->io_vector == 0) {
344                 /*
345                  * Allocate an APIC vector for this interrupt pin.  Once
346                  * we have a vector we program the interrupt pin.
347                  */
348                 intpin->io_vector = apic_alloc_vector(intpin->io_irq);
349                 if (bootverbose) {
350                         printf("ioapic%u: routing intpin %u (", io->io_id,
351                             intpin->io_intpin);
352                         ioapic_print_irq(intpin);
353                         printf(") to vector %u\n", intpin->io_vector);
354                 }
355                 ioapic_program_intpin(intpin);
356                 apic_enable_vector(intpin->io_vector);
357         }
358 }
359
360 static int
361 ioapic_vector(struct intsrc *isrc)
362 {
363         struct ioapic_intsrc *pin;
364
365         pin = (struct ioapic_intsrc *)isrc;
366         return (pin->io_irq);
367 }
368
369 static int
370 ioapic_source_pending(struct intsrc *isrc)
371 {
372         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
373
374         if (intpin->io_vector == 0)
375                 return 0;
376         return (lapic_intr_pending(intpin->io_vector));
377 }
378
379 static int
380 ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
381     enum intr_polarity pol)
382 {
383         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
384         struct ioapic *io = (struct ioapic *)isrc->is_pic;
385         int changed;
386
387         KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
388             ("%s: Conforming trigger or polarity\n", __func__));
389
390         /*
391          * EISA interrupts always use active high polarity, so don't allow
392          * them to be set to active low.
393          *
394          * XXX: Should we write to the ELCR if the trigger mode changes for
395          * an EISA IRQ or an ISA IRQ with the ELCR present?
396          */
397         if (intpin->io_bus == APIC_BUS_EISA)
398                 pol = INTR_POLARITY_HIGH;
399         changed = 0;
400         if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
401                 if (bootverbose)
402                         printf("ioapic%u: Changing trigger for pin %u to %s\n",
403                             io->io_id, intpin->io_intpin,
404                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
405                 intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
406                 changed++;
407         }
408         if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
409                 if (bootverbose)
410                         printf("ioapic%u: Changing polarity for pin %u to %s\n",
411                             io->io_id, intpin->io_intpin,
412                             pol == INTR_POLARITY_HIGH ? "high" : "low");
413                 intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
414                 changed++;
415         }
416         if (changed)
417                 ioapic_program_intpin(intpin);
418         return (0);
419 }
420
421 static void
422 ioapic_suspend(struct intsrc *isrc)
423 {
424
425         TODO;
426 }
427
428 static void
429 ioapic_resume(struct intsrc *isrc)
430 {
431
432         ioapic_program_intpin((struct ioapic_intsrc *)isrc);
433 }
434
435 /*
436  * Create a plain I/O APIC object.
437  */
438 void *
439 ioapic_create(uintptr_t addr, int32_t apic_id, int intbase)
440 {
441         struct ioapic *io;
442         struct ioapic_intsrc *intpin;
443         volatile ioapic_t *apic;
444         u_int numintr, i;
445         uint32_t value;
446
447         /* Map the register window so we can access the device. */
448         apic = pmap_mapdev(addr, IOAPIC_MEM_REGION);
449         mtx_lock_spin(&icu_lock);
450         value = ioapic_read(apic, IOAPIC_VER);
451         mtx_unlock_spin(&icu_lock);
452
453         /* If it's version register doesn't seem to work, punt. */
454         if (value == 0xffffffff) {
455                 pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
456                 return (NULL);
457         }
458
459         /* Determine the number of vectors and set the APIC ID. */
460         numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
461         io = malloc(sizeof(struct ioapic) +
462             numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
463         io->io_pic = ioapic_template;
464         mtx_lock_spin(&icu_lock);
465         io->io_id = next_id++;
466         io->io_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT; 
467         if (apic_id != -1 && io->io_apic_id != apic_id) {
468                 ioapic_write(apic, IOAPIC_ID, apic_id << APIC_ID_SHIFT);
469                 mtx_unlock_spin(&icu_lock);
470                 io->io_apic_id = apic_id;
471                 printf("ioapic%u: Changing APIC ID to %d\n", io->io_id,
472                     apic_id);
473         } else
474                 mtx_unlock_spin(&icu_lock);
475         if (intbase == -1) {
476                 intbase = next_ioapic_base;
477                 printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
478                     intbase);
479         } else if (intbase != next_ioapic_base)
480                 printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
481                     io->io_id, intbase, next_ioapic_base);
482         io->io_intbase = intbase;
483         next_ioapic_base = intbase + numintr;
484         io->io_numintr = numintr;
485         io->io_addr = apic;
486
487         /*
488          * Initialize pins.  Start off with interrupts disabled.  Default
489          * to active-hi and edge-triggered for ISA interrupts and active-lo
490          * and level-triggered for all others.
491          */
492         bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
493         mtx_lock_spin(&icu_lock);
494         for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
495                 intpin->io_intsrc.is_pic = (struct pic *)io;
496                 intpin->io_intpin = i;
497                 intpin->io_irq = intbase + i;
498
499                 /*
500                  * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
501                  * Assume that pins 1-15 are ISA interrupts and that all
502                  * other pins are PCI interrupts.
503                  */
504                 if (intpin->io_irq == 0)
505                         ioapic_set_extint(io, i);
506                 else if (intpin->io_irq < IOAPIC_ISA_INTS) {
507                         intpin->io_bus = APIC_BUS_ISA;
508                         intpin->io_activehi = 1;
509                         intpin->io_edgetrigger = 1;
510                         intpin->io_masked = 1;
511                 } else {
512                         intpin->io_bus = APIC_BUS_PCI;
513                         intpin->io_activehi = 0;
514                         intpin->io_edgetrigger = 0;
515                         intpin->io_masked = 1;
516                 }
517
518                 /*
519                  * Route interrupts to the BSP by default.  Interrupts may
520                  * be routed to other CPUs later after they are enabled.
521                  */
522                 intpin->io_cpu = PCPU_GET(apic_id);
523                 if (bootverbose && intpin->io_irq != IRQ_DISABLED) {
524                         printf("ioapic%u: intpin %d -> ",  io->io_id, i);
525                         ioapic_print_irq(intpin);
526                         printf(" (%s, %s)\n", intpin->io_edgetrigger ?
527                             "edge" : "level", intpin->io_activehi ? "high" :
528                             "low");
529                 }
530                 value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
531                 ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
532         }
533         mtx_unlock_spin(&icu_lock);
534
535         return (io);
536 }
537
538 int
539 ioapic_get_vector(void *cookie, u_int pin)
540 {
541         struct ioapic *io;
542
543         io = (struct ioapic *)cookie;
544         if (pin >= io->io_numintr)
545                 return (-1);
546         return (io->io_pins[pin].io_irq);
547 }
548
549 int
550 ioapic_disable_pin(void *cookie, u_int pin)
551 {
552         struct ioapic *io;
553
554         io = (struct ioapic *)cookie;
555         if (pin >= io->io_numintr)
556                 return (EINVAL);
557         if (io->io_pins[pin].io_irq == IRQ_DISABLED)
558                 return (EINVAL);
559         io->io_pins[pin].io_irq = IRQ_DISABLED;
560         if (bootverbose)
561                 printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
562         return (0);
563 }
564
565 int
566 ioapic_remap_vector(void *cookie, u_int pin, int vector)
567 {
568         struct ioapic *io;
569
570         io = (struct ioapic *)cookie;
571         if (pin >= io->io_numintr || vector < 0)
572                 return (EINVAL);
573         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
574                 return (EINVAL);
575         io->io_pins[pin].io_irq = vector;
576         if (bootverbose)
577                 printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
578                     vector, pin);
579         return (0);
580 }
581
582 int
583 ioapic_set_bus(void *cookie, u_int pin, int bus_type)
584 {
585         struct ioapic *io;
586
587         if (bus_type < 0 || bus_type > APIC_BUS_MAX)
588                 return (EINVAL);
589         io = (struct ioapic *)cookie;
590         if (pin >= io->io_numintr)
591                 return (EINVAL);
592         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
593                 return (EINVAL);
594         io->io_pins[pin].io_bus = bus_type;
595         if (bootverbose)
596                 printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
597                     ioapic_bus_string(bus_type));
598         return (0);
599 }
600
601 int
602 ioapic_set_nmi(void *cookie, u_int pin)
603 {
604         struct ioapic *io;
605
606         io = (struct ioapic *)cookie;
607         if (pin >= io->io_numintr)
608                 return (EINVAL);
609         if (io->io_pins[pin].io_irq == IRQ_NMI)
610                 return (0);
611         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
612                 return (EINVAL);
613         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
614         io->io_pins[pin].io_irq = IRQ_NMI;
615         io->io_pins[pin].io_masked = 0;
616         io->io_pins[pin].io_edgetrigger = 1;
617         io->io_pins[pin].io_activehi = 1;
618         if (bootverbose)
619                 printf("ioapic%u: Routing NMI -> intpin %d\n",
620                     io->io_id, pin);
621         return (0);
622 }
623
624 int
625 ioapic_set_smi(void *cookie, u_int pin)
626 {
627         struct ioapic *io;
628
629         io = (struct ioapic *)cookie;
630         if (pin >= io->io_numintr)
631                 return (EINVAL);
632         if (io->io_pins[pin].io_irq == IRQ_SMI)
633                 return (0);
634         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
635                 return (EINVAL);
636         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
637         io->io_pins[pin].io_irq = IRQ_SMI;
638         io->io_pins[pin].io_masked = 0;
639         io->io_pins[pin].io_edgetrigger = 1;
640         io->io_pins[pin].io_activehi = 1;
641         if (bootverbose)
642                 printf("ioapic%u: Routing SMI -> intpin %d\n",
643                     io->io_id, pin);
644         return (0);
645 }
646
647 int
648 ioapic_set_extint(void *cookie, u_int pin)
649 {
650         struct ioapic *io;
651
652         io = (struct ioapic *)cookie;
653         if (pin >= io->io_numintr)
654                 return (EINVAL);
655         if (io->io_pins[pin].io_irq == IRQ_EXTINT)
656                 return (0);
657         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
658                 return (EINVAL);
659         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
660         io->io_pins[pin].io_irq = IRQ_EXTINT;
661         if (enable_extint)
662                 io->io_pins[pin].io_masked = 0;
663         else
664                 io->io_pins[pin].io_masked = 1;
665         io->io_pins[pin].io_edgetrigger = 1;
666         io->io_pins[pin].io_activehi = 1;
667         if (bootverbose)
668                 printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
669                     io->io_id, pin);
670         return (0);
671 }
672
673 int
674 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
675 {
676         struct ioapic *io;
677
678         io = (struct ioapic *)cookie;
679         if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
680                 return (EINVAL);
681         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
682                 return (EINVAL);
683         io->io_pins[pin].io_activehi = (pol == INTR_POLARITY_HIGH);
684         if (bootverbose)
685                 printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
686                     pol == INTR_POLARITY_HIGH ? "high" : "low");
687         return (0);
688 }
689
690 int
691 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
692 {
693         struct ioapic *io;
694
695         io = (struct ioapic *)cookie;
696         if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
697                 return (EINVAL);
698         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
699                 return (EINVAL);
700         io->io_pins[pin].io_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
701         if (bootverbose)
702                 printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
703                     trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
704         return (0);
705 }
706
707 /*
708  * Register a complete I/O APIC object with the interrupt subsystem.
709  */
710 void
711 ioapic_register(void *cookie)
712 {
713         struct ioapic_intsrc *pin;
714         struct ioapic *io;
715         volatile ioapic_t *apic;
716         uint32_t flags;
717         int i;
718
719         io = (struct ioapic *)cookie;
720         apic = io->io_addr;
721         mtx_lock_spin(&icu_lock);
722         flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
723         STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
724         mtx_unlock_spin(&icu_lock);
725         printf("ioapic%u <Version %u.%u> irqs %u-%u on motherboard\n",
726             io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
727             io->io_intbase + io->io_numintr - 1);
728
729         /* Register valid pins as interrupt sources. */
730         for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
731                 if (pin->io_irq < NUM_IO_INTS)
732                         intr_register_source(&pin->io_intsrc);
733 }