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