]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/io_apic.c
Use uintmax_t (typedef'd to rman_res_t type) for rman ranges.
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_acpi.h"
31 #include "opt_isa.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48
49 #include <x86/apicreg.h>
50 #include <machine/frame.h>
51 #include <machine/intr_machdep.h>
52 #include <x86/apicvar.h>
53 #include <machine/resource.h>
54 #include <machine/segments.h>
55 #include <x86/iommu/iommu_intrmap.h>
56
57 #define IOAPIC_ISA_INTS         16
58 #define IOAPIC_MEM_REGION       32
59 #define IOAPIC_REDTBL_LO(i)     (IOAPIC_REDTBL + (i) * 2)
60 #define IOAPIC_REDTBL_HI(i)     (IOAPIC_REDTBL_LO(i) + 1)
61
62 static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
63
64 /*
65  * I/O APIC interrupt source driver.  Each pin is assigned an IRQ cookie
66  * as laid out in the ACPI System Interrupt number model where each I/O
67  * APIC has a contiguous chunk of the System Interrupt address space.
68  * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
69  * IRQs behave as PCI IRQs by default.  We also assume that the pin for
70  * IRQ 0 is actually an ExtINT pin.  The apic enumerators override the
71  * configuration of individual pins as indicated by their tables.
72  *
73  * Documentation for the I/O APIC: "82093AA I/O Advanced Programmable
74  * Interrupt Controller (IOAPIC)", May 1996, Intel Corp.
75  * ftp://download.intel.com/design/chipsets/datashts/29056601.pdf
76  */
77
78 struct ioapic_intsrc {
79         struct intsrc io_intsrc;
80         u_int io_irq;
81         u_int io_intpin:8;
82         u_int io_vector:8;
83         u_int io_cpu;
84         u_int io_activehi:1;
85         u_int io_edgetrigger:1;
86         u_int io_masked:1;
87         int io_bus:4;
88         uint32_t io_lowreg;
89         u_int io_remap_cookie;
90 };
91
92 struct ioapic {
93         struct pic io_pic;
94         u_int io_id:8;                  /* logical ID */
95         u_int io_apic_id:4;
96         u_int io_intbase:8;             /* System Interrupt base */
97         u_int io_numintr:8;
98         u_int io_haseoi:1;
99         volatile ioapic_t *io_addr;     /* XXX: should use bus_space */
100         vm_paddr_t io_paddr;
101         STAILQ_ENTRY(ioapic) io_next;
102         struct ioapic_intsrc io_pins[0];
103 };
104
105 static u_int    ioapic_read(volatile ioapic_t *apic, int reg);
106 static void     ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
107 static const char *ioapic_bus_string(int bus_type);
108 static void     ioapic_print_irq(struct ioapic_intsrc *intpin);
109 static void     ioapic_enable_source(struct intsrc *isrc);
110 static void     ioapic_disable_source(struct intsrc *isrc, int eoi);
111 static void     ioapic_eoi_source(struct intsrc *isrc);
112 static void     ioapic_enable_intr(struct intsrc *isrc);
113 static void     ioapic_disable_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_resume(struct pic *pic, bool suspend_cancelled);
119 static int      ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
120 static void     ioapic_program_intpin(struct ioapic_intsrc *intpin);
121 static void     ioapic_reprogram_intpin(struct intsrc *isrc);
122
123 static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
124 struct pic ioapic_template = {
125         .pic_enable_source = ioapic_enable_source,
126         .pic_disable_source = ioapic_disable_source,
127         .pic_eoi_source = ioapic_eoi_source,
128         .pic_enable_intr = ioapic_enable_intr,
129         .pic_disable_intr = ioapic_disable_intr,
130         .pic_vector = ioapic_vector,
131         .pic_source_pending = ioapic_source_pending,
132         .pic_suspend = NULL,
133         .pic_resume = ioapic_resume,
134         .pic_config_intr = ioapic_config_intr,
135         .pic_assign_cpu = ioapic_assign_cpu,
136         .pic_reprogram_pin = ioapic_reprogram_intpin,
137 };
138
139 static int next_ioapic_base;
140 static u_int next_id;
141
142 static int enable_extint;
143 SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
144     "Enable the ExtINT pin in the first I/O APIC");
145
146 static void
147 _ioapic_eoi_source(struct intsrc *isrc, int locked)
148 {
149         struct ioapic_intsrc *src;
150         struct ioapic *io;
151         volatile uint32_t *apic_eoi;
152         uint32_t low1;
153
154         lapic_eoi();
155         if (!lapic_eoi_suppression)
156                 return;
157         src = (struct ioapic_intsrc *)isrc;
158         if (src->io_edgetrigger)
159                 return;
160         io = (struct ioapic *)isrc->is_pic;
161
162         /*
163          * Handle targeted EOI for level-triggered pins, if broadcast
164          * EOI suppression is supported by LAPICs.
165          */
166         if (io->io_haseoi) {
167                 /*
168                  * If IOAPIC has EOI Register, simply write vector
169                  * number into the reg.
170                  */
171                 apic_eoi = (volatile uint32_t *)((volatile char *)
172                     io->io_addr + IOAPIC_EOIR);
173                 *apic_eoi = src->io_vector;
174         } else {
175                 /*
176                  * Otherwise, if IO-APIC is too old to provide EOIR,
177                  * do what Intel did for the Linux kernel. Temporary
178                  * switch the pin to edge-trigger and back, masking
179                  * the pin during the trick.
180                  */
181                 if (!locked)
182                         mtx_lock_spin(&icu_lock);
183                 low1 = src->io_lowreg;
184                 low1 &= ~IOART_TRGRLVL;
185                 low1 |= IOART_TRGREDG | IOART_INTMSET;
186                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
187                     low1);
188                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
189                     src->io_lowreg);
190                 if (!locked)
191                         mtx_unlock_spin(&icu_lock);
192         }
193 }
194
195 static u_int
196 ioapic_read(volatile ioapic_t *apic, int reg)
197 {
198
199         mtx_assert(&icu_lock, MA_OWNED);
200         apic->ioregsel = reg;
201         return (apic->iowin);
202 }
203
204 static void
205 ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
206 {
207
208         mtx_assert(&icu_lock, MA_OWNED);
209         apic->ioregsel = reg;
210         apic->iowin = val;
211 }
212
213 static const char *
214 ioapic_bus_string(int bus_type)
215 {
216
217         switch (bus_type) {
218         case APIC_BUS_ISA:
219                 return ("ISA");
220         case APIC_BUS_EISA:
221                 return ("EISA");
222         case APIC_BUS_PCI:
223                 return ("PCI");
224         default:
225                 return ("unknown");
226         }
227 }
228
229 static void
230 ioapic_print_irq(struct ioapic_intsrc *intpin)
231 {
232
233         switch (intpin->io_irq) {
234         case IRQ_DISABLED:
235                 printf("disabled");
236                 break;
237         case IRQ_EXTINT:
238                 printf("ExtINT");
239                 break;
240         case IRQ_NMI:
241                 printf("NMI");
242                 break;
243         case IRQ_SMI:
244                 printf("SMI");
245                 break;
246         default:
247                 printf("%s IRQ %u", ioapic_bus_string(intpin->io_bus),
248                     intpin->io_irq);
249         }
250 }
251
252 static void
253 ioapic_enable_source(struct intsrc *isrc)
254 {
255         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
256         struct ioapic *io = (struct ioapic *)isrc->is_pic;
257         uint32_t flags;
258
259         mtx_lock_spin(&icu_lock);
260         if (intpin->io_masked) {
261                 flags = intpin->io_lowreg & ~IOART_INTMASK;
262                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
263                     flags);
264                 intpin->io_masked = 0;
265         }
266         mtx_unlock_spin(&icu_lock);
267 }
268
269 static void
270 ioapic_disable_source(struct intsrc *isrc, int eoi)
271 {
272         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
273         struct ioapic *io = (struct ioapic *)isrc->is_pic;
274         uint32_t flags;
275
276         mtx_lock_spin(&icu_lock);
277         if (!intpin->io_masked && !intpin->io_edgetrigger) {
278                 flags = intpin->io_lowreg | IOART_INTMSET;
279                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
280                     flags);
281                 intpin->io_masked = 1;
282         }
283
284         if (eoi == PIC_EOI)
285                 _ioapic_eoi_source(isrc, 1);
286
287         mtx_unlock_spin(&icu_lock);
288 }
289
290 static void
291 ioapic_eoi_source(struct intsrc *isrc)
292 {
293
294         _ioapic_eoi_source(isrc, 0);
295 }
296
297 /*
298  * Completely program an intpin based on the data in its interrupt source
299  * structure.
300  */
301 static void
302 ioapic_program_intpin(struct ioapic_intsrc *intpin)
303 {
304         struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
305         uint32_t low, high, value;
306 #ifdef ACPI_DMAR
307         int error;
308 #endif
309
310         /*
311          * If a pin is completely invalid or if it is valid but hasn't
312          * been enabled yet, just ensure that the pin is masked.
313          */
314         mtx_assert(&icu_lock, MA_OWNED);
315         if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq < NUM_IO_INTS &&
316             intpin->io_vector == 0)) {
317                 low = ioapic_read(io->io_addr,
318                     IOAPIC_REDTBL_LO(intpin->io_intpin));
319                 if ((low & IOART_INTMASK) == IOART_INTMCLR)
320                         ioapic_write(io->io_addr,
321                             IOAPIC_REDTBL_LO(intpin->io_intpin),
322                             low | IOART_INTMSET);
323 #ifdef ACPI_DMAR
324                 mtx_unlock_spin(&icu_lock);
325                 iommu_unmap_ioapic_intr(io->io_apic_id,
326                     &intpin->io_remap_cookie);
327                 mtx_lock_spin(&icu_lock);
328 #endif
329                 return;
330         }
331
332 #ifdef ACPI_DMAR
333         mtx_unlock_spin(&icu_lock);
334         error = iommu_map_ioapic_intr(io->io_apic_id,
335             intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
336             intpin->io_activehi, intpin->io_irq, &intpin->io_remap_cookie,
337             &high, &low);
338         mtx_lock_spin(&icu_lock);
339         if (error == 0) {
340                 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin),
341                     high);
342                 intpin->io_lowreg = low;
343                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
344                     low);
345                 return;
346         } else if (error != EOPNOTSUPP) {
347                 return;
348         }
349 #endif
350
351         /* Set the destination. */
352         low = IOART_DESTPHY;
353         high = intpin->io_cpu << APIC_ID_SHIFT;
354
355         /* Program the rest of the low word. */
356         if (intpin->io_edgetrigger)
357                 low |= IOART_TRGREDG;
358         else
359                 low |= IOART_TRGRLVL;
360         if (intpin->io_activehi)
361                 low |= IOART_INTAHI;
362         else
363                 low |= IOART_INTALO;
364         if (intpin->io_masked)
365                 low |= IOART_INTMSET;
366         switch (intpin->io_irq) {
367         case IRQ_EXTINT:
368                 KASSERT(intpin->io_edgetrigger,
369                     ("ExtINT not edge triggered"));
370                 low |= IOART_DELEXINT;
371                 break;
372         case IRQ_NMI:
373                 KASSERT(intpin->io_edgetrigger,
374                     ("NMI not edge triggered"));
375                 low |= IOART_DELNMI;
376                 break;
377         case IRQ_SMI:
378                 KASSERT(intpin->io_edgetrigger,
379                     ("SMI not edge triggered"));
380                 low |= IOART_DELSMI;
381                 break;
382         default:
383                 KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
384                     intpin->io_irq));
385                 low |= IOART_DELFIXED | intpin->io_vector;
386         }
387
388         /* Write the values to the APIC. */
389         value = ioapic_read(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin));
390         value &= ~IOART_DEST;
391         value |= high;
392         ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), value);
393         intpin->io_lowreg = low;
394         ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
395 }
396
397 static void
398 ioapic_reprogram_intpin(struct intsrc *isrc)
399 {
400
401         mtx_lock_spin(&icu_lock);
402         ioapic_program_intpin((struct ioapic_intsrc *)isrc);
403         mtx_unlock_spin(&icu_lock);
404 }
405
406 static int
407 ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
408 {
409         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
410         struct ioapic *io = (struct ioapic *)isrc->is_pic;
411         u_int old_vector, new_vector;
412         u_int old_id;
413
414         /*
415          * keep 1st core as the destination for NMI
416          */
417         if (intpin->io_irq == IRQ_NMI)
418                 apic_id = 0;
419
420         /*
421          * Set us up to free the old irq.
422          */
423         old_vector = intpin->io_vector;
424         old_id = intpin->io_cpu;
425         if (old_vector && apic_id == old_id)
426                 return (0);
427
428         /*
429          * Allocate an APIC vector for this interrupt pin.  Once
430          * we have a vector we program the interrupt pin.
431          */
432         new_vector = apic_alloc_vector(apic_id, intpin->io_irq);
433         if (new_vector == 0)
434                 return (ENOSPC);
435
436         /*
437          * Mask the old intpin if it is enabled while it is migrated.
438          *
439          * At least some level-triggered interrupts seem to need the
440          * extra DELAY() to avoid being stuck in a non-EOI'd state.
441          */
442         mtx_lock_spin(&icu_lock);
443         if (!intpin->io_masked && !intpin->io_edgetrigger) {
444                 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
445                     intpin->io_lowreg | IOART_INTMSET);
446                 mtx_unlock_spin(&icu_lock);
447                 DELAY(100);
448                 mtx_lock_spin(&icu_lock);
449         }
450
451         intpin->io_cpu = apic_id;
452         intpin->io_vector = new_vector;
453         if (isrc->is_handlers > 0)
454                 apic_enable_vector(intpin->io_cpu, intpin->io_vector);
455         if (bootverbose) {
456                 printf("ioapic%u: routing intpin %u (", io->io_id,
457                     intpin->io_intpin);
458                 ioapic_print_irq(intpin);
459                 printf(") to lapic %u vector %u\n", intpin->io_cpu,
460                     intpin->io_vector);
461         }
462         ioapic_program_intpin(intpin);
463         mtx_unlock_spin(&icu_lock);
464
465         /*
466          * Free the old vector after the new one is established.  This is done
467          * to prevent races where we could miss an interrupt.
468          */
469         if (old_vector) {
470                 if (isrc->is_handlers > 0)
471                         apic_disable_vector(old_id, old_vector);
472                 apic_free_vector(old_id, old_vector, intpin->io_irq);
473         }
474         return (0);
475 }
476
477 static void
478 ioapic_enable_intr(struct intsrc *isrc)
479 {
480         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
481
482         if (intpin->io_vector == 0)
483                 if (ioapic_assign_cpu(isrc, intr_next_cpu()) != 0)
484                         panic("Couldn't find an APIC vector for IRQ %d",
485                             intpin->io_irq);
486         apic_enable_vector(intpin->io_cpu, intpin->io_vector);
487 }
488
489
490 static void
491 ioapic_disable_intr(struct intsrc *isrc)
492 {
493         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
494         u_int vector;
495
496         if (intpin->io_vector != 0) {
497                 /* Mask this interrupt pin and free its APIC vector. */
498                 vector = intpin->io_vector;
499                 apic_disable_vector(intpin->io_cpu, vector);
500                 mtx_lock_spin(&icu_lock);
501                 intpin->io_masked = 1;
502                 intpin->io_vector = 0;
503                 ioapic_program_intpin(intpin);
504                 mtx_unlock_spin(&icu_lock);
505                 apic_free_vector(intpin->io_cpu, vector, intpin->io_irq);
506         }
507 }
508
509 static int
510 ioapic_vector(struct intsrc *isrc)
511 {
512         struct ioapic_intsrc *pin;
513
514         pin = (struct ioapic_intsrc *)isrc;
515         return (pin->io_irq);
516 }
517
518 static int
519 ioapic_source_pending(struct intsrc *isrc)
520 {
521         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
522
523         if (intpin->io_vector == 0)
524                 return 0;
525         return (lapic_intr_pending(intpin->io_vector));
526 }
527
528 static int
529 ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
530     enum intr_polarity pol)
531 {
532         struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
533         struct ioapic *io = (struct ioapic *)isrc->is_pic;
534         int changed;
535
536         KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
537             ("%s: Conforming trigger or polarity\n", __func__));
538
539         /*
540          * EISA interrupts always use active high polarity, so don't allow
541          * them to be set to active low.
542          *
543          * XXX: Should we write to the ELCR if the trigger mode changes for
544          * an EISA IRQ or an ISA IRQ with the ELCR present?
545          */
546         mtx_lock_spin(&icu_lock);
547         if (intpin->io_bus == APIC_BUS_EISA)
548                 pol = INTR_POLARITY_HIGH;
549         changed = 0;
550         if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
551                 if (bootverbose)
552                         printf("ioapic%u: Changing trigger for pin %u to %s\n",
553                             io->io_id, intpin->io_intpin,
554                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
555                 intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
556                 changed++;
557         }
558         if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
559                 if (bootverbose)
560                         printf("ioapic%u: Changing polarity for pin %u to %s\n",
561                             io->io_id, intpin->io_intpin,
562                             pol == INTR_POLARITY_HIGH ? "high" : "low");
563                 intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
564                 changed++;
565         }
566         if (changed)
567                 ioapic_program_intpin(intpin);
568         mtx_unlock_spin(&icu_lock);
569         return (0);
570 }
571
572 static void
573 ioapic_resume(struct pic *pic, bool suspend_cancelled)
574 {
575         struct ioapic *io = (struct ioapic *)pic;
576         int i;
577
578         mtx_lock_spin(&icu_lock);
579         for (i = 0; i < io->io_numintr; i++)
580                 ioapic_program_intpin(&io->io_pins[i]);
581         mtx_unlock_spin(&icu_lock);
582 }
583
584 /*
585  * Create a plain I/O APIC object.
586  */
587 void *
588 ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase)
589 {
590         struct ioapic *io;
591         struct ioapic_intsrc *intpin;
592         volatile ioapic_t *apic;
593         u_int numintr, i;
594         uint32_t value;
595
596         /* Map the register window so we can access the device. */
597         apic = pmap_mapdev(addr, IOAPIC_MEM_REGION);
598         mtx_lock_spin(&icu_lock);
599         value = ioapic_read(apic, IOAPIC_VER);
600         mtx_unlock_spin(&icu_lock);
601
602         /* If it's version register doesn't seem to work, punt. */
603         if (value == 0xffffffff) {
604                 pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
605                 return (NULL);
606         }
607
608         /* Determine the number of vectors and set the APIC ID. */
609         numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
610         io = malloc(sizeof(struct ioapic) +
611             numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
612         io->io_pic = ioapic_template;
613         mtx_lock_spin(&icu_lock);
614         io->io_id = next_id++;
615         io->io_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
616         if (apic_id != -1 && io->io_apic_id != apic_id) {
617                 ioapic_write(apic, IOAPIC_ID, apic_id << APIC_ID_SHIFT);
618                 mtx_unlock_spin(&icu_lock);
619                 io->io_apic_id = apic_id;
620                 printf("ioapic%u: Changing APIC ID to %d\n", io->io_id,
621                     apic_id);
622         } else
623                 mtx_unlock_spin(&icu_lock);
624         if (intbase == -1) {
625                 intbase = next_ioapic_base;
626                 printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
627                     intbase);
628         } else if (intbase != next_ioapic_base && bootverbose)
629                 printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
630                     io->io_id, intbase, next_ioapic_base);
631         io->io_intbase = intbase;
632         next_ioapic_base = intbase + numintr;
633         io->io_numintr = numintr;
634         io->io_addr = apic;
635         io->io_paddr = addr;
636
637         if (bootverbose) {
638                 printf("ioapic%u: ver 0x%02x maxredir 0x%02x\n", io->io_id,
639                     (value & IOART_VER_VERSION), (value & IOART_VER_MAXREDIR)
640                     >> MAXREDIRSHIFT);
641         }
642         /*
643          * The  summary information about IO-APIC versions is taken from
644          * the Linux kernel source:
645          *     0Xh     82489DX
646          *     1Xh     I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
647          *     2Xh     I/O(x)APIC which is PCI 2.2 Compliant
648          *     30h-FFh Reserved
649          * IO-APICs with version >= 0x20 have working EOIR register.
650          */
651         io->io_haseoi = (value & IOART_VER_VERSION) >= 0x20;
652
653         /*
654          * Initialize pins.  Start off with interrupts disabled.  Default
655          * to active-hi and edge-triggered for ISA interrupts and active-lo
656          * and level-triggered for all others.
657          */
658         bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
659         mtx_lock_spin(&icu_lock);
660         for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
661                 intpin->io_intsrc.is_pic = (struct pic *)io;
662                 intpin->io_intpin = i;
663                 intpin->io_irq = intbase + i;
664
665                 /*
666                  * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
667                  * Assume that pins 1-15 are ISA interrupts and that all
668                  * other pins are PCI interrupts.
669                  */
670                 if (intpin->io_irq == 0)
671                         ioapic_set_extint(io, i);
672                 else if (intpin->io_irq < IOAPIC_ISA_INTS) {
673                         intpin->io_bus = APIC_BUS_ISA;
674                         intpin->io_activehi = 1;
675                         intpin->io_edgetrigger = 1;
676                         intpin->io_masked = 1;
677                 } else {
678                         intpin->io_bus = APIC_BUS_PCI;
679                         intpin->io_activehi = 0;
680                         intpin->io_edgetrigger = 0;
681                         intpin->io_masked = 1;
682                 }
683
684                 /*
685                  * Route interrupts to the BSP by default.  Interrupts may
686                  * be routed to other CPUs later after they are enabled.
687                  */
688                 intpin->io_cpu = PCPU_GET(apic_id);
689                 value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
690                 ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
691 #ifdef ACPI_DMAR
692                 /* dummy, but sets cookie */
693                 mtx_unlock_spin(&icu_lock);
694                 iommu_map_ioapic_intr(io->io_apic_id,
695                     intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
696                     intpin->io_activehi, intpin->io_irq,
697                     &intpin->io_remap_cookie, NULL, NULL);
698                 mtx_lock_spin(&icu_lock);
699 #endif
700         }
701         mtx_unlock_spin(&icu_lock);
702
703         return (io);
704 }
705
706 int
707 ioapic_get_vector(void *cookie, u_int pin)
708 {
709         struct ioapic *io;
710
711         io = (struct ioapic *)cookie;
712         if (pin >= io->io_numintr)
713                 return (-1);
714         return (io->io_pins[pin].io_irq);
715 }
716
717 int
718 ioapic_disable_pin(void *cookie, u_int pin)
719 {
720         struct ioapic *io;
721
722         io = (struct ioapic *)cookie;
723         if (pin >= io->io_numintr)
724                 return (EINVAL);
725         if (io->io_pins[pin].io_irq == IRQ_DISABLED)
726                 return (EINVAL);
727         io->io_pins[pin].io_irq = IRQ_DISABLED;
728         if (bootverbose)
729                 printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
730         return (0);
731 }
732
733 int
734 ioapic_remap_vector(void *cookie, u_int pin, int vector)
735 {
736         struct ioapic *io;
737
738         io = (struct ioapic *)cookie;
739         if (pin >= io->io_numintr || vector < 0)
740                 return (EINVAL);
741         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
742                 return (EINVAL);
743         io->io_pins[pin].io_irq = vector;
744         if (bootverbose)
745                 printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
746                     vector, pin);
747         return (0);
748 }
749
750 int
751 ioapic_set_bus(void *cookie, u_int pin, int bus_type)
752 {
753         struct ioapic *io;
754
755         if (bus_type < 0 || bus_type > APIC_BUS_MAX)
756                 return (EINVAL);
757         io = (struct ioapic *)cookie;
758         if (pin >= io->io_numintr)
759                 return (EINVAL);
760         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
761                 return (EINVAL);
762         if (io->io_pins[pin].io_bus == bus_type)
763                 return (0);
764         io->io_pins[pin].io_bus = bus_type;
765         if (bootverbose)
766                 printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
767                     ioapic_bus_string(bus_type));
768         return (0);
769 }
770
771 int
772 ioapic_set_nmi(void *cookie, u_int pin)
773 {
774         struct ioapic *io;
775
776         io = (struct ioapic *)cookie;
777         if (pin >= io->io_numintr)
778                 return (EINVAL);
779         if (io->io_pins[pin].io_irq == IRQ_NMI)
780                 return (0);
781         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
782                 return (EINVAL);
783         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
784         io->io_pins[pin].io_irq = IRQ_NMI;
785         io->io_pins[pin].io_masked = 0;
786         io->io_pins[pin].io_edgetrigger = 1;
787         io->io_pins[pin].io_activehi = 1;
788         if (bootverbose)
789                 printf("ioapic%u: Routing NMI -> intpin %d\n",
790                     io->io_id, pin);
791         return (0);
792 }
793
794 int
795 ioapic_set_smi(void *cookie, u_int pin)
796 {
797         struct ioapic *io;
798
799         io = (struct ioapic *)cookie;
800         if (pin >= io->io_numintr)
801                 return (EINVAL);
802         if (io->io_pins[pin].io_irq == IRQ_SMI)
803                 return (0);
804         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
805                 return (EINVAL);
806         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
807         io->io_pins[pin].io_irq = IRQ_SMI;
808         io->io_pins[pin].io_masked = 0;
809         io->io_pins[pin].io_edgetrigger = 1;
810         io->io_pins[pin].io_activehi = 1;
811         if (bootverbose)
812                 printf("ioapic%u: Routing SMI -> intpin %d\n",
813                     io->io_id, pin);
814         return (0);
815 }
816
817 int
818 ioapic_set_extint(void *cookie, u_int pin)
819 {
820         struct ioapic *io;
821
822         io = (struct ioapic *)cookie;
823         if (pin >= io->io_numintr)
824                 return (EINVAL);
825         if (io->io_pins[pin].io_irq == IRQ_EXTINT)
826                 return (0);
827         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
828                 return (EINVAL);
829         io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
830         io->io_pins[pin].io_irq = IRQ_EXTINT;
831         if (enable_extint)
832                 io->io_pins[pin].io_masked = 0;
833         else
834                 io->io_pins[pin].io_masked = 1;
835         io->io_pins[pin].io_edgetrigger = 1;
836         io->io_pins[pin].io_activehi = 1;
837         if (bootverbose)
838                 printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
839                     io->io_id, pin);
840         return (0);
841 }
842
843 int
844 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
845 {
846         struct ioapic *io;
847         int activehi;
848
849         io = (struct ioapic *)cookie;
850         if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
851                 return (EINVAL);
852         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
853                 return (EINVAL);
854         activehi = (pol == INTR_POLARITY_HIGH);
855         if (io->io_pins[pin].io_activehi == activehi)
856                 return (0);
857         io->io_pins[pin].io_activehi = activehi;
858         if (bootverbose)
859                 printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
860                     pol == INTR_POLARITY_HIGH ? "high" : "low");
861         return (0);
862 }
863
864 int
865 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
866 {
867         struct ioapic *io;
868         int edgetrigger;
869
870         io = (struct ioapic *)cookie;
871         if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
872                 return (EINVAL);
873         if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
874                 return (EINVAL);
875         edgetrigger = (trigger == INTR_TRIGGER_EDGE);
876         if (io->io_pins[pin].io_edgetrigger == edgetrigger)
877                 return (0);
878         io->io_pins[pin].io_edgetrigger = edgetrigger;
879         if (bootverbose)
880                 printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
881                     trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
882         return (0);
883 }
884
885 /*
886  * Register a complete I/O APIC object with the interrupt subsystem.
887  */
888 void
889 ioapic_register(void *cookie)
890 {
891         struct ioapic_intsrc *pin;
892         struct ioapic *io;
893         volatile ioapic_t *apic;
894         uint32_t flags;
895         int i;
896
897         io = (struct ioapic *)cookie;
898         apic = io->io_addr;
899         mtx_lock_spin(&icu_lock);
900         flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
901         STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
902         mtx_unlock_spin(&icu_lock);
903         printf("ioapic%u <Version %u.%u> irqs %u-%u on motherboard\n",
904             io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
905             io->io_intbase + io->io_numintr - 1);
906
907         /* Register valid pins as interrupt sources. */
908         intr_register_pic(&io->io_pic);
909         for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
910                 if (pin->io_irq < NUM_IO_INTS)
911                         intr_register_source(&pin->io_intsrc);
912 }
913
914 /* A simple new-bus driver to consume PCI I/O APIC devices. */
915 static int
916 ioapic_pci_probe(device_t dev)
917 {
918
919         if (pci_get_class(dev) == PCIC_BASEPERIPH &&
920             pci_get_subclass(dev) == PCIS_BASEPERIPH_PIC) {
921                 switch (pci_get_progif(dev)) {
922                 case PCIP_BASEPERIPH_PIC_IO_APIC:
923                         device_set_desc(dev, "IO APIC");
924                         break;
925                 case PCIP_BASEPERIPH_PIC_IOX_APIC:
926                         device_set_desc(dev, "IO(x) APIC");
927                         break;
928                 default:
929                         return (ENXIO);
930                 }
931                 device_quiet(dev);
932                 return (-10000);
933         }
934         return (ENXIO);
935 }
936
937 static int
938 ioapic_pci_attach(device_t dev)
939 {
940
941         return (0);
942 }
943
944 static device_method_t ioapic_pci_methods[] = {
945         /* Device interface */
946         DEVMETHOD(device_probe,         ioapic_pci_probe),
947         DEVMETHOD(device_attach,        ioapic_pci_attach),
948
949         { 0, 0 }
950 };
951
952 DEFINE_CLASS_0(ioapic, ioapic_pci_driver, ioapic_pci_methods, 0);
953
954 static devclass_t ioapic_devclass;
955 DRIVER_MODULE(ioapic, pci, ioapic_pci_driver, ioapic_devclass, 0, 0);
956
957 /*
958  * A new-bus driver to consume the memory resources associated with
959  * the APICs in the system.  On some systems ACPI or PnPBIOS system
960  * resource devices may already claim these resources.  To keep from
961  * breaking those devices, we attach ourself to the nexus device after
962  * legacy0 and acpi0 and ignore any allocation failures.
963  */
964 static void
965 apic_identify(driver_t *driver, device_t parent)
966 {
967
968         /*
969          * Add at order 12.  acpi0 is probed at order 10 and legacy0
970          * is probed at order 11.
971          */
972         if (lapic_paddr != 0)
973                 BUS_ADD_CHILD(parent, 12, "apic", 0);
974 }
975
976 static int
977 apic_probe(device_t dev)
978 {
979
980         device_set_desc(dev, "APIC resources");
981         device_quiet(dev);
982         return (0);
983 }
984
985 static void
986 apic_add_resource(device_t dev, int rid, vm_paddr_t base, size_t length)
987 {
988         int error;
989
990         error = bus_set_resource(dev, SYS_RES_MEMORY, rid, base, length);
991         if (error)
992                 panic("apic_add_resource: resource %d failed set with %d", rid,
993                     error);
994         bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
995 }
996
997 static int
998 apic_attach(device_t dev)
999 {
1000         struct ioapic *io;
1001         int i;
1002
1003         /* Reserve the local APIC. */
1004         apic_add_resource(dev, 0, lapic_paddr, LAPIC_MEM_REGION);
1005         i = 1;
1006         STAILQ_FOREACH(io, &ioapic_list, io_next) {
1007                 apic_add_resource(dev, i, io->io_paddr, IOAPIC_MEM_REGION);
1008                 i++;
1009         }
1010         return (0);
1011 }
1012
1013 static device_method_t apic_methods[] = {
1014         /* Device interface */
1015         DEVMETHOD(device_identify,      apic_identify),
1016         DEVMETHOD(device_probe,         apic_probe),
1017         DEVMETHOD(device_attach,        apic_attach),
1018
1019         { 0, 0 }
1020 };
1021
1022 DEFINE_CLASS_0(apic, apic_driver, apic_methods, 0);
1023
1024 static devclass_t apic_devclass;
1025 DRIVER_MODULE(apic, nexus, apic_driver, apic_devclass, 0, 0);
1026
1027 #include "opt_ddb.h"
1028
1029 #ifdef DDB
1030 #include <ddb/ddb.h>
1031
1032 static const char *
1033 ioapic_delivery_mode(uint32_t mode)
1034 {
1035
1036         switch (mode) {
1037         case IOART_DELFIXED:
1038                 return ("fixed");
1039         case IOART_DELLOPRI:
1040                 return ("lowestpri");
1041         case IOART_DELSMI:
1042                 return ("SMI");
1043         case IOART_DELRSV1:
1044                 return ("rsrvd1");
1045         case IOART_DELNMI:
1046                 return ("NMI");
1047         case IOART_DELINIT:
1048                 return ("INIT");
1049         case IOART_DELRSV2:
1050                 return ("rsrvd2");
1051         case IOART_DELEXINT:
1052                 return ("ExtINT");
1053         default:
1054                 return ("");
1055         }
1056 }
1057
1058 static u_int
1059 db_ioapic_read(volatile ioapic_t *apic, int reg)
1060 {
1061
1062         apic->ioregsel = reg;
1063         return (apic->iowin);
1064 }
1065
1066 static void
1067 db_show_ioapic_one(volatile ioapic_t *io_addr)
1068 {
1069         uint32_t r, lo, hi;
1070         int mre, i;
1071
1072         r = db_ioapic_read(io_addr, IOAPIC_VER);
1073         mre = (r & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT;
1074         db_printf("Id 0x%08x Ver 0x%02x MRE %d\n",
1075             db_ioapic_read(io_addr, IOAPIC_ID), r & IOART_VER_VERSION, mre);
1076         for (i = 0; i < mre; i++) {
1077                 lo = db_ioapic_read(io_addr, IOAPIC_REDTBL_LO(i));
1078                 hi = db_ioapic_read(io_addr, IOAPIC_REDTBL_HI(i));
1079                 db_printf("  pin %d Dest %s/%x %smasked Trig %s RemoteIRR %d "
1080                     "Polarity %s Status %s DeliveryMode %s Vec %d\n", i,
1081                     (lo & IOART_DESTMOD) == IOART_DESTLOG ? "log" : "phy",
1082                     (hi & IOART_DEST) >> 24,
1083                     (lo & IOART_INTMASK) == IOART_INTMSET ? "" : "not",
1084                     (lo & IOART_TRGRMOD) == IOART_TRGRLVL ? "lvl" : "edge",
1085                     (lo & IOART_REM_IRR) == IOART_REM_IRR ? 1 : 0,
1086                     (lo & IOART_INTPOL) == IOART_INTALO ? "low" : "high",
1087                     (lo & IOART_DELIVS) == IOART_DELIVS ? "pend" : "idle",
1088                     ioapic_delivery_mode(lo & IOART_DELMOD),
1089                     (lo & IOART_INTVEC));
1090           }
1091 }
1092
1093 DB_SHOW_COMMAND(ioapic, db_show_ioapic)
1094 {
1095         struct ioapic *ioapic;
1096         int idx, i;
1097
1098         if (!have_addr) {
1099                 db_printf("usage: show ioapic index\n");
1100                 return;
1101         }
1102
1103         idx = (int)addr;
1104         i = 0;
1105         STAILQ_FOREACH(ioapic, &ioapic_list, io_next) {
1106                 if (idx == i) {
1107                         db_show_ioapic_one(ioapic->io_addr);
1108                         break;
1109                 }
1110                 i++;
1111         }
1112 }
1113
1114 DB_SHOW_ALL_COMMAND(ioapics, db_show_all_ioapics)
1115 {
1116         struct ioapic *ioapic;
1117
1118         STAILQ_FOREACH(ioapic, &ioapic_list, io_next)
1119                 db_show_ioapic_one(ioapic->io_addr);
1120 }
1121 #endif