apic: APIC ID should not be 255, which is used for broadcast
[dragonfly.git] / sys / platform / pc32 / i386 / mptable.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
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. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/i386/mp_machdep.c,v 1.115.2.15 2003/03/14 21:22:35 jhb Exp $
26  */
27
28 #include "opt_cpu.h"
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 #include <sys/malloc.h>
35 #include <sys/memrange.h>
36 #include <sys/cons.h>   /* cngetc() */
37 #include <sys/machintr.h>
38
39 #include <vm/vm.h>
40 #include <vm/vm_param.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_kern.h>
43 #include <vm/vm_extern.h>
44 #include <sys/lock.h>
45 #include <vm/vm_map.h>
46 #include <sys/user.h>
47 #ifdef GPROF 
48 #include <sys/gmon.h>
49 #endif
50
51 #include <sys/mplock2.h>
52
53 #include <machine/smp.h>
54 #include <machine_base/isa/isa_intr.h>
55 #include <machine_base/apic/apicreg.h>
56 #include <machine/atomic.h>
57 #include <machine/cpufunc.h>
58 #include <machine/cputypes.h>
59 #include <machine_base/apic/ioapic_abi.h>
60 #include <machine_base/apic/lapic.h>
61 #include <machine_base/apic/ioapic.h>
62 #include <machine_base/apic/apicvar.h>
63 #include <machine/psl.h>
64 #include <machine/segments.h>
65 #include <machine/tss.h>
66 #include <machine/specialreg.h>
67 #include <machine/globaldata.h>
68 #include <machine/pmap_inval.h>
69 #include <machine/mptable.h>
70
71 #include <machine/md_var.h>             /* setidt() */
72 #include <machine_base/icu/icu.h>       /* IPIs */
73 #include <machine/intr_machdep.h>       /* IPIs */
74
75 extern u_long   ebda_addr;
76 extern u_int    base_memory;
77 extern int      imcr_present;
78 extern int      naps;
79
80 #define BIOS_BASE               (0xf0000)
81 #define BIOS_BASE2              (0xe0000)
82 #define BIOS_SIZE               (0x10000)
83 #define BIOS_COUNT              (BIOS_SIZE/4)
84
85 #define PROCENTRY_FLAG_EN       0x01
86 #define PROCENTRY_FLAG_BP       0x02
87 #define IOAPICENTRY_FLAG_EN     0x01
88
89 /* MP Floating Pointer Structure */
90 typedef struct MPFPS {
91         char    signature[4];
92         u_int32_t pap;
93         u_char  length;
94         u_char  spec_rev;
95         u_char  checksum;
96         u_char  mpfb1;
97         u_char  mpfb2;
98         u_char  mpfb3;
99         u_char  mpfb4;
100         u_char  mpfb5;
101 }      *mpfps_t;
102
103 /* MP Configuration Table Header */
104 typedef struct MPCTH {
105         char    signature[4];
106         u_short base_table_length;
107         u_char  spec_rev;
108         u_char  checksum;
109         u_char  oem_id[8];
110         u_char  product_id[12];
111         void   *oem_table_pointer;
112         u_short oem_table_size;
113         u_short entry_count;
114         void   *apic_address;
115         u_short extended_table_length;
116         u_char  extended_table_checksum;
117         u_char  reserved;
118 }      *mpcth_t;
119
120
121 typedef struct PROCENTRY {
122         u_char  type;
123         u_char  apic_id;
124         u_char  apic_version;
125         u_char  cpu_flags;
126         u_long  cpu_signature;
127         u_long  feature_flags;
128         u_long  reserved1;
129         u_long  reserved2;
130 }      *proc_entry_ptr;
131
132 typedef struct BUSENTRY {
133         u_char  type;
134         u_char  bus_id;
135         char    bus_type[6];
136 }      *bus_entry_ptr;
137
138 typedef struct IOAPICENTRY {
139         u_char  type;
140         u_char  apic_id;
141         u_char  apic_version;
142         u_char  apic_flags;
143         void   *apic_address;
144 }      *io_apic_entry_ptr;
145
146 typedef struct INTENTRY {
147         u_char  type;
148         u_char  int_type;
149         u_short int_flags;
150         u_char  src_bus_id;
151         u_char  src_bus_irq;
152         u_char  dst_apic_id;
153         u_char  dst_apic_int;
154 }      *int_entry_ptr;
155
156 /* descriptions of MP basetable entries */
157 typedef struct BASETABLE_ENTRY {
158         u_char  type;
159         u_char  length;
160         char    name[16];
161 }       basetable_entry;
162
163 struct mptable_pos {
164         mpfps_t         mp_fps;
165         mpcth_t         mp_cth;
166         vm_size_t       mp_cth_mapsz;
167 };
168
169 #define MPTABLE_POS_USE_DEFAULT(mpt) \
170         ((mpt)->mp_fps->mpfb1 != 0 || (mpt)->mp_cth == NULL)
171
172 struct mptable_bus {
173         int             mb_id;
174         int             mb_type;        /* MPTABLE_BUS_ */
175         TAILQ_ENTRY(mptable_bus) mb_link;
176 };
177
178 #define MPTABLE_BUS_ISA         0
179 #define MPTABLE_BUS_PCI         1
180
181 struct mptable_bus_info {
182         TAILQ_HEAD(, mptable_bus) mbi_list;
183 };
184
185 struct mptable_pci_int {
186         int             mpci_bus;
187         int             mpci_dev;
188         int             mpci_pin;
189
190         int             mpci_ioapic_idx;
191         int             mpci_ioapic_pin;
192         TAILQ_ENTRY(mptable_pci_int) mpci_link;
193 };
194
195 struct mptable_ioapic {
196         int             mio_idx;
197         int             mio_apic_id;
198         uint32_t        mio_addr;
199         int             mio_gsi_base;
200         int             mio_npin;
201         TAILQ_ENTRY(mptable_ioapic) mio_link;
202 };
203
204 typedef int     (*mptable_iter_func)(void *, const void *, int);
205
206 static int      mptable_iterate_entries(const mpcth_t,
207                     mptable_iter_func, void *);
208 static int      mptable_search(void);
209 static int      mptable_search_sig(u_int32_t target, int count);
210 static int      mptable_hyperthread_fixup(cpumask_t, int);
211 static int      mptable_map(struct mptable_pos *);
212 static void     mptable_unmap(struct mptable_pos *);
213 static void     mptable_bus_info_alloc(const mpcth_t,
214                     struct mptable_bus_info *);
215 static void     mptable_bus_info_free(struct mptable_bus_info *);
216
217 static int      mptable_lapic_probe(struct lapic_enumerator *);
218 static void     mptable_lapic_enumerate(struct lapic_enumerator *);
219 static void     mptable_lapic_default(void);
220
221 static int      mptable_ioapic_probe(struct ioapic_enumerator *);
222 static void     mptable_ioapic_enumerate(struct ioapic_enumerator *);
223
224 static basetable_entry basetable_entry_types[] =
225 {
226         {0, 20, "Processor"},
227         {1, 8, "Bus"},
228         {2, 8, "I/O APIC"},
229         {3, 8, "I/O INT"},
230         {4, 8, "Local INT"}
231 };
232
233 static vm_paddr_t       mptable_fps_phyaddr;
234 static int              mptable_use_default;
235 static TAILQ_HEAD(mptable_pci_int_list, mptable_pci_int) mptable_pci_int_list =
236         TAILQ_HEAD_INITIALIZER(mptable_pci_int_list);
237 static TAILQ_HEAD(mptable_ioapic_list, mptable_ioapic) mptable_ioapic_list =
238         TAILQ_HEAD_INITIALIZER(mptable_ioapic_list);
239
240 static void
241 mptable_probe(void)
242 {
243         struct mptable_pos mpt;
244         int error;
245
246         KKASSERT(mptable_fps_phyaddr == 0);
247
248         mptable_fps_phyaddr = mptable_search();
249         if (mptable_fps_phyaddr == 0)
250                 return;
251
252         error = mptable_map(&mpt);
253         if (error) {
254                 mptable_fps_phyaddr = 0;
255                 return;
256         }
257
258         if (MPTABLE_POS_USE_DEFAULT(&mpt)) {
259                 kprintf("MPTABLE: use default configuration\n");
260                 mptable_use_default = 1;
261         }
262         if (mpt.mp_fps->mpfb2 & 0x80)
263                 imcr_present = 1;
264
265         mptable_unmap(&mpt);
266 }
267 SYSINIT(mptable_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, mptable_probe, 0);
268
269 /*
270  * Look for an Intel MP spec table (ie, SMP capable hardware).
271  */
272 static int
273 mptable_search(void)
274 {
275         int     x;
276         u_int32_t target;
277  
278         /* see if EBDA exists */
279         if (ebda_addr != 0) {
280                 /* search first 1K of EBDA */
281                 target = (u_int32_t)ebda_addr;
282                 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
283                         return x;
284         } else {
285                 /* last 1K of base memory, effective 'top of base' passed in */
286                 target = (u_int32_t)(base_memory - 0x400);
287                 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
288                         return x;
289         }
290
291         /* search the BIOS */
292         target = (u_int32_t)BIOS_BASE;
293         if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
294                 return x;
295
296         /* search the extended BIOS */
297         target = (u_int32_t)BIOS_BASE2;
298         if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
299                 return x;
300
301         /* nothing found */
302         return 0;
303 }
304
305 static int
306 mptable_iterate_entries(const mpcth_t cth, mptable_iter_func func, void *arg)
307 {
308         int count, total_size;
309         const void *position;
310
311         KKASSERT(cth->base_table_length >= sizeof(struct MPCTH));
312         total_size = cth->base_table_length - sizeof(struct MPCTH);
313         position = (const uint8_t *)cth + sizeof(struct MPCTH);
314         count = cth->entry_count;
315
316         while (count--) {
317                 int type, error;
318
319                 KKASSERT(total_size >= 0);
320                 if (total_size == 0) {
321                         kprintf("invalid base MP table, "
322                                 "entry count and length mismatch\n");
323                         return EINVAL;
324                 }
325
326                 type = *(const uint8_t *)position;
327                 switch (type) {
328                 case 0: /* processor_entry */
329                 case 1: /* bus_entry */
330                 case 2: /* io_apic_entry */
331                 case 3: /* int_entry */
332                 case 4: /* int_entry */
333                         break;
334                 default:
335                         kprintf("unknown base MP table entry type %d\n", type);
336                         return EINVAL;
337                 }
338
339                 if (total_size < basetable_entry_types[type].length) {
340                         kprintf("invalid base MP table length, "
341                                 "does not contain all entries\n");
342                         return EINVAL;
343                 }
344                 total_size -= basetable_entry_types[type].length;
345
346                 error = func(arg, position, type);
347                 if (error)
348                         return error;
349
350                 position = (const uint8_t *)position +
351                     basetable_entry_types[type].length;
352         }
353         return 0;
354 }
355
356 /*
357  * look for the MP spec signature
358  */
359
360 /* string defined by the Intel MP Spec as identifying the MP table */
361 #define MP_SIG          0x5f504d5f      /* _MP_ */
362 #define NEXT(X)         ((X) += 4)
363 static int
364 mptable_search_sig(u_int32_t target, int count)
365 {
366         vm_size_t map_size;
367         u_int32_t *addr;
368         int x, ret;
369
370         KKASSERT(target != 0);
371
372         map_size = count * sizeof(u_int32_t);
373         addr = pmap_mapdev((vm_paddr_t)target, map_size);
374
375         ret = 0;
376         for (x = 0; x < count; NEXT(x)) {
377                 if (addr[x] == MP_SIG) {
378                         /* make array index a byte index */
379                         ret = target + (x * sizeof(u_int32_t));
380                         break;
381                 }
382         }
383
384         pmap_unmapdev((vm_offset_t)addr, map_size);
385         return ret;
386 }
387
388 static int processor_entry      (const struct PROCENTRY *entry, int cpu);
389
390 /*
391  * Check if we should perform a hyperthreading "fix-up" to
392  * enumerate any logical CPU's that aren't already listed
393  * in the table.
394  *
395  * XXX: We assume that all of the physical CPUs in the
396  * system have the same number of logical CPUs.
397  *
398  * XXX: We assume that APIC ID's are allocated such that
399  * the APIC ID's for a physical processor are aligned
400  * with the number of logical CPU's in the processor.
401  */
402 static int
403 mptable_hyperthread_fixup(cpumask_t id_mask, int cpu_count)
404 {
405         int i, id, lcpus_max, logical_cpus;
406
407         if ((cpu_feature & CPUID_HTT) == 0)
408                 return 0;
409
410         lcpus_max = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
411         if (lcpus_max <= 1)
412                 return 0;
413
414         if (cpu_vendor_id == CPU_VENDOR_INTEL) {
415                 /*
416                  * INSTRUCTION SET REFERENCE, A-M (#253666)
417                  * Page 3-181, Table 3-20
418                  * "The nearest power-of-2 integer that is not smaller
419                  *  than EBX[23:16] is the number of unique initial APIC
420                  *  IDs reserved for addressing different logical
421                  *  processors in a physical package."
422                  */
423                 for (i = 0; ; ++i) {
424                         if ((1 << i) >= lcpus_max) {
425                                 lcpus_max = 1 << i;
426                                 break;
427                         }
428                 }
429         }
430
431         KKASSERT(cpu_count != 0);
432         if (cpu_count == lcpus_max) {
433                 /* We have nothing to fix */
434                 return 0;
435         } else if (cpu_count == 1) {
436                 /* XXX this may be incorrect */
437                 logical_cpus = lcpus_max;
438         } else {
439                 int cur, prev, dist;
440
441                 /*
442                  * Calculate the distances between two nearest
443                  * APIC IDs.  If all such distances are same,
444                  * then it is the number of missing cpus that
445                  * we are going to fill later.
446                  */
447                 dist = cur = prev = -1;
448                 for (id = 0; id < MAXCPU; ++id) {
449                         if ((id_mask & CPUMASK(id)) == 0)
450                                 continue;
451
452                         cur = id;
453                         if (prev >= 0) {
454                                 int new_dist = cur - prev;
455
456                                 if (dist < 0)
457                                         dist = new_dist;
458
459                                 /*
460                                  * Make sure that all distances
461                                  * between two nearest APIC IDs
462                                  * are same.
463                                  */
464                                 if (dist != new_dist)
465                                         return 0;
466                         }
467                         prev = cur;
468                 }
469                 if (dist == 1)
470                         return 0;
471
472                 /* Must be power of 2 */
473                 if (dist & (dist - 1))
474                         return 0;
475
476                 /* Can't exceed CPU package capacity */
477                 if (dist > lcpus_max)
478                         logical_cpus = lcpus_max;
479                 else
480                         logical_cpus = dist;
481         }
482
483         /*
484          * For each APIC ID of a CPU that is set in the mask,
485          * scan the other candidate APIC ID's for this
486          * physical processor.  If any of those ID's are
487          * already in the table, then kill the fixup.
488          */
489         for (id = 0; id < MAXCPU; id++) {
490                 if ((id_mask & CPUMASK(id)) == 0)
491                         continue;
492                 /* First, make sure we are on a logical_cpus boundary. */
493                 if (id % logical_cpus != 0)
494                         return 0;
495                 for (i = id + 1; i < id + logical_cpus; i++)
496                         if ((id_mask & CPUMASK(i)) != 0)
497                                 return 0;
498         }
499         return logical_cpus;
500 }
501
502 static int
503 mptable_map(struct mptable_pos *mpt)
504 {
505         mpfps_t fps = NULL;
506         mpcth_t cth = NULL;
507         vm_size_t cth_mapsz = 0;
508
509         KKASSERT(mptable_fps_phyaddr != 0);
510
511         bzero(mpt, sizeof(*mpt));
512
513         fps = pmap_mapdev(mptable_fps_phyaddr, sizeof(*fps));
514         if (fps->pap != 0) {
515                 /*
516                  * Map configuration table header to get
517                  * the base table size
518                  */
519                 cth = pmap_mapdev(fps->pap, sizeof(*cth));
520                 cth_mapsz = cth->base_table_length;
521                 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
522
523                 if (cth_mapsz < sizeof(*cth)) {
524                         kprintf("invalid base MP table length %d\n",
525                                 (int)cth_mapsz);
526                         pmap_unmapdev((vm_offset_t)fps, sizeof(*fps));
527                         return EINVAL;
528                 }
529
530                 /*
531                  * Map the base table
532                  */
533                 cth = pmap_mapdev(fps->pap, cth_mapsz);
534         }
535
536         mpt->mp_fps = fps;
537         mpt->mp_cth = cth;
538         mpt->mp_cth_mapsz = cth_mapsz;
539
540         return 0;
541 }
542
543 static void
544 mptable_unmap(struct mptable_pos *mpt)
545 {
546         if (mpt->mp_cth != NULL) {
547                 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
548                 mpt->mp_cth = NULL;
549                 mpt->mp_cth_mapsz = 0;
550         }
551         if (mpt->mp_fps != NULL) {
552                 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
553                 mpt->mp_fps = NULL;
554         }
555 }
556
557 static int
558 processor_entry(const struct PROCENTRY *entry, int cpu)
559 {
560         KKASSERT(cpu > 0);
561
562         /* check for usability */
563         if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
564                 return 0;
565
566         /* check for BSP flag */
567         if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
568                 lapic_set_cpuid(0, entry->apic_id);
569                 return 0;       /* its already been counted */
570         }
571
572         /* add another AP to list, if less than max number of CPUs */
573         else if (cpu < MAXCPU) {
574                 lapic_set_cpuid(cpu, entry->apic_id);
575                 return 1;
576         }
577
578         return 0;
579 }
580
581 static int
582 mptable_bus_info_callback(void *xarg, const void *pos, int type)
583 {
584         struct mptable_bus_info *bus_info = xarg;
585         const struct BUSENTRY *ent;
586         struct mptable_bus *bus;
587
588         if (type != 1)
589                 return 0;
590
591         ent = pos;
592         TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
593                 if (bus->mb_id == ent->bus_id) {
594                         kprintf("mptable_bus_info_alloc: duplicated bus id "
595                                 "(%d)\n", bus->mb_id);
596                         return EINVAL;
597                 }
598         }
599
600         bus = NULL;
601         if (strncmp(ent->bus_type, "PCI", 3) == 0) {
602                 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
603                 bus->mb_type = MPTABLE_BUS_PCI;
604         } else if (strncmp(ent->bus_type, "ISA", 3) == 0) {
605                 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
606                 bus->mb_type = MPTABLE_BUS_ISA;
607         }
608
609         if (bus != NULL) {
610                 bus->mb_id = ent->bus_id;
611                 TAILQ_INSERT_TAIL(&bus_info->mbi_list, bus, mb_link);
612         }
613         return 0;
614 }
615
616 static void
617 mptable_bus_info_alloc(const mpcth_t cth, struct mptable_bus_info *bus_info)
618 {
619         int error;
620
621         bzero(bus_info, sizeof(*bus_info));
622         TAILQ_INIT(&bus_info->mbi_list);
623
624         error = mptable_iterate_entries(cth, mptable_bus_info_callback, bus_info);
625         if (error)
626                 mptable_bus_info_free(bus_info);
627 }
628
629 static void
630 mptable_bus_info_free(struct mptable_bus_info *bus_info)
631 {
632         struct mptable_bus *bus;
633
634         while ((bus = TAILQ_FIRST(&bus_info->mbi_list)) != NULL) {
635                 TAILQ_REMOVE(&bus_info->mbi_list, bus, mb_link);
636                 kfree(bus, M_TEMP);
637         }
638 }
639
640 struct mptable_lapic_cbarg1 {
641         int     cpu_count;
642         int     ht_fixup;
643         u_int   ht_apicid_mask;
644 };
645
646 static int
647 mptable_lapic_pass1_callback(void *xarg, const void *pos, int type)
648 {
649         const struct PROCENTRY *ent;
650         struct mptable_lapic_cbarg1 *arg = xarg;
651
652         if (type != 0)
653                 return 0;
654         ent = pos;
655
656         if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
657                 return 0;
658
659         arg->cpu_count++;
660         if (ent->apic_id < 32) {
661                 arg->ht_apicid_mask |= 1 << ent->apic_id;
662         } else if (arg->ht_fixup) {
663                 kprintf("MPTABLE: lapic id > 32, disable HTT fixup\n");
664                 arg->ht_fixup = 0;
665         }
666         return 0;
667 }
668
669 struct mptable_lapic_cbarg2 {
670         int     cpu;
671         int     logical_cpus;
672         int     found_bsp;
673 };
674
675 static int
676 mptable_lapic_pass2_callback(void *xarg, const void *pos, int type)
677 {
678         const struct PROCENTRY *ent;
679         struct mptable_lapic_cbarg2 *arg = xarg;
680
681         if (type != 0)
682                 return 0;
683         ent = pos;
684
685         if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
686                 KKASSERT(!arg->found_bsp);
687                 arg->found_bsp = 1;
688         }
689
690         if (processor_entry(ent, arg->cpu))
691                 arg->cpu++;
692
693         if (arg->logical_cpus) {
694                 struct PROCENTRY proc;
695                 int i;
696
697                 /*
698                  * Create fake mptable processor entries
699                  * and feed them to processor_entry() to
700                  * enumerate the logical CPUs.
701                  */
702                 bzero(&proc, sizeof(proc));
703                 proc.type = 0;
704                 proc.cpu_flags = PROCENTRY_FLAG_EN;
705                 proc.apic_id = ent->apic_id;
706
707                 for (i = 1; i < arg->logical_cpus; i++) {
708                         proc.apic_id++;
709                         processor_entry(&proc, arg->cpu);
710                         arg->cpu++;
711                 }
712         }
713         return 0;
714 }
715
716 static void
717 mptable_lapic_default(void)
718 {
719         int ap_apicid, bsp_apicid;
720
721         naps = 1; /* exclude BSP */
722
723         /* Map local apic before the id field is accessed */
724         lapic_map(DEFAULT_APIC_BASE);
725
726         bsp_apicid = APIC_ID(lapic->id);
727         ap_apicid = (bsp_apicid == 0) ? 1 : 0;
728
729         /* BSP */
730         lapic_set_cpuid(0, bsp_apicid);
731         /* one and only AP */
732         lapic_set_cpuid(1, ap_apicid);
733 }
734
735 /*
736  * Configure:
737  *     naps
738  *     APIC ID <-> CPU ID mappings
739  */
740 static void
741 mptable_lapic_enumerate(struct lapic_enumerator *e)
742 {
743         struct mptable_pos mpt;
744         struct mptable_lapic_cbarg1 arg1;
745         struct mptable_lapic_cbarg2 arg2;
746         mpcth_t cth;
747         int error, logical_cpus = 0;
748         vm_paddr_t lapic_addr;
749
750         if (mptable_use_default) {
751                 mptable_lapic_default();
752                 return;
753         }
754
755         error = mptable_map(&mpt);
756         if (error)
757                 panic("mptable_lapic_enumerate mptable_map failed");
758         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
759
760         cth = mpt.mp_cth;
761
762         /* Save local apic address */
763         lapic_addr = (vm_paddr_t)(uintptr_t)cth->apic_address;
764         KKASSERT(lapic_addr != 0);
765
766         /*
767          * Find out how many CPUs do we have
768          */
769         bzero(&arg1, sizeof(arg1));
770         arg1.ht_fixup = 1; /* Apply ht fixup by default */
771
772         error = mptable_iterate_entries(cth,
773                     mptable_lapic_pass1_callback, &arg1);
774         if (error)
775                 panic("mptable_iterate_entries(lapic_pass1) failed");
776         KKASSERT(arg1.cpu_count != 0);
777
778         /* See if we need to fixup HT logical CPUs. */
779         if (arg1.ht_fixup) {
780                 logical_cpus = mptable_hyperthread_fixup(arg1.ht_apicid_mask,
781                                                          arg1.cpu_count);
782                 if (logical_cpus != 0)
783                         arg1.cpu_count *= logical_cpus;
784         }
785         naps = arg1.cpu_count - 1;      /* subtract the BSP */
786
787         /*
788          * Link logical CPU id to local apic id
789          */
790         bzero(&arg2, sizeof(arg2));
791         arg2.cpu = 1;
792         arg2.logical_cpus = logical_cpus;
793
794         error = mptable_iterate_entries(cth,
795                     mptable_lapic_pass2_callback, &arg2);
796         if (error)
797                 panic("mptable_iterate_entries(lapic_pass2) failed");
798         KKASSERT(arg2.found_bsp);
799
800         /* Map local apic */
801         lapic_map(lapic_addr);
802
803         mptable_unmap(&mpt);
804 }
805
806 struct mptable_lapic_probe_cbarg {
807         int     cpu_count;
808         int     found_bsp;
809 };
810
811 static int
812 mptable_lapic_probe_callback(void *xarg, const void *pos, int type)
813 {
814         const struct PROCENTRY *ent;
815         struct mptable_lapic_probe_cbarg *arg = xarg;
816
817         if (type != 0)
818                 return 0;
819         ent = pos;
820
821         if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
822                 return 0;
823         arg->cpu_count++;
824
825         if (ent->apic_id == APICID_MAX) {
826                 kprintf("MPTABLE: invalid LAPIC apic id %d\n",
827                     ent->apic_id);
828                 return EINVAL;
829         }
830
831         if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
832                 if (arg->found_bsp) {
833                         kprintf("more than one BSP in base MP table\n");
834                         return EINVAL;
835                 }
836                 arg->found_bsp = 1;
837         }
838         return 0;
839 }
840
841 static int
842 mptable_lapic_probe(struct lapic_enumerator *e)
843 {
844         struct mptable_pos mpt;
845         struct mptable_lapic_probe_cbarg arg;
846         mpcth_t cth;
847         int error;
848
849         if (mptable_fps_phyaddr == 0)
850                 return ENXIO;
851
852         if (mptable_use_default)
853                 return 0;
854
855         error = mptable_map(&mpt);
856         if (error)
857                 return error;
858         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
859
860         error = EINVAL;
861         cth = mpt.mp_cth;
862
863         if (cth->apic_address == NULL)
864                 goto done;
865
866         bzero(&arg, sizeof(arg));
867         error = mptable_iterate_entries(cth,
868                     mptable_lapic_probe_callback, &arg);
869         if (!error) {
870                 if (arg.cpu_count == 0) {
871                         kprintf("MP table contains no processor entries\n");
872                         error = EINVAL;
873                 } else if (!arg.found_bsp) {
874                         kprintf("MP table does not contains BSP entry\n");
875                         error = EINVAL;
876                 }
877         }
878 done:
879         mptable_unmap(&mpt);
880         return error;
881 }
882
883 static struct lapic_enumerator  mptable_lapic_enumerator = {
884         .lapic_prio = LAPIC_ENUM_PRIO_MPTABLE,
885         .lapic_probe = mptable_lapic_probe,
886         .lapic_enumerate = mptable_lapic_enumerate
887 };
888
889 static void
890 mptable_lapic_enum_register(void)
891 {
892         lapic_enumerator_register(&mptable_lapic_enumerator);
893 }
894 SYSINIT(mptable_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
895         mptable_lapic_enum_register, 0);
896
897 static int
898 mptable_ioapic_list_callback(void *xarg, const void *pos, int type)
899 {
900         const struct IOAPICENTRY *ent;
901         struct mptable_ioapic *nioapic, *ioapic;
902
903         if (type != 2)
904                 return 0;
905         ent = pos;
906
907         if ((ent->apic_flags & IOAPICENTRY_FLAG_EN) == 0)
908                 return 0;
909
910         if (ent->apic_address == NULL) {
911                 kprintf("mptable_ioapic_create_list: zero IOAPIC addr\n");
912                 return EINVAL;
913         }
914         if (ent->apic_id == APICID_MAX) {
915                 kprintf("mptable_ioapic_create_list: "
916                     "invalid IOAPIC apic id %d\n", ent->apic_id);
917                 return EINVAL;
918         }
919
920         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
921                 if (ioapic->mio_apic_id == ent->apic_id) {
922                         kprintf("mptable_ioapic_create_list: duplicated "
923                                 "apic id %d\n", ioapic->mio_apic_id);
924                         return EINVAL;
925                 }
926                 if (ioapic->mio_addr == (uint32_t)ent->apic_address) {
927                         kprintf("mptable_ioapic_create_list: overlapped "
928                                 "IOAPIC addr 0x%08x", ioapic->mio_addr);
929                         return EINVAL;
930                 }
931         }
932
933         nioapic = kmalloc(sizeof(*nioapic), M_DEVBUF, M_WAITOK | M_ZERO);
934         nioapic->mio_apic_id = ent->apic_id;
935         nioapic->mio_addr = (uint32_t)ent->apic_address;
936
937         /*
938          * Create IOAPIC list in ascending order of APIC ID
939          */
940         TAILQ_FOREACH_REVERSE(ioapic, &mptable_ioapic_list,
941             mptable_ioapic_list, mio_link) {
942                 if (nioapic->mio_apic_id > ioapic->mio_apic_id) {
943                         TAILQ_INSERT_AFTER(&mptable_ioapic_list,
944                             ioapic, nioapic, mio_link);
945                         break;
946                 }
947         }
948         if (ioapic == NULL)
949                 TAILQ_INSERT_HEAD(&mptable_ioapic_list, nioapic, mio_link);
950
951         return 0;
952 }
953
954 static void
955 mptable_ioapic_create_list(void)
956 {
957         struct mptable_ioapic *ioapic;
958         struct mptable_pos mpt;
959         int idx, error;
960
961         if (mptable_fps_phyaddr == 0)
962                 return;
963
964         if (mptable_use_default) {
965                 ioapic = kmalloc(sizeof(*ioapic), M_DEVBUF, M_WAITOK | M_ZERO);
966                 ioapic->mio_idx = 0;
967                 ioapic->mio_apic_id = 0;        /* NOTE: any value is ok here */
968                 ioapic->mio_addr = 0xfec00000;  /* XXX magic number */
969
970                 TAILQ_INSERT_HEAD(&mptable_ioapic_list, ioapic, mio_link);
971                 return;
972         }
973
974         error = mptable_map(&mpt);
975         if (error)
976                 panic("mptable_ioapic_create_list: mptable_map failed");
977         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
978
979         error = mptable_iterate_entries(mpt.mp_cth,
980                     mptable_ioapic_list_callback, NULL);
981         if (error) {
982                 while ((ioapic = TAILQ_FIRST(&mptable_ioapic_list)) != NULL) {
983                         TAILQ_REMOVE(&mptable_ioapic_list, ioapic, mio_link);
984                         kfree(ioapic, M_DEVBUF);
985                 }
986                 goto done;
987         }
988
989         /*
990          * Assign index number for each IOAPIC
991          */
992         idx = 0;
993         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
994                 ioapic->mio_idx = idx;
995                 ++idx;
996         }
997 done:
998         mptable_unmap(&mpt);
999 }
1000 SYSINIT(mptable_ioapic_list, SI_BOOT2_PRESMP, SI_ORDER_SECOND,
1001         mptable_ioapic_create_list, 0);
1002
1003 static int
1004 mptable_pci_int_callback(void *xarg, const void *pos, int type)
1005 {
1006         const struct mptable_bus_info *bus_info = xarg;
1007         const struct mptable_ioapic *ioapic;
1008         const struct mptable_bus *bus;
1009         struct mptable_pci_int *pci_int;
1010         const struct INTENTRY *ent;
1011         int pci_pin, pci_dev;
1012
1013         if (type != 3)
1014                 return 0;
1015         ent = pos;
1016
1017         if (ent->int_type != 0)
1018                 return 0;
1019
1020         TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
1021                 if (bus->mb_type == MPTABLE_BUS_PCI &&
1022                     bus->mb_id == ent->src_bus_id)
1023                         break;
1024         }
1025         if (bus == NULL)
1026                 return 0;
1027
1028         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1029                 if (ioapic->mio_apic_id == ent->dst_apic_id)
1030                         break;
1031         }
1032         if (ioapic == NULL) {
1033                 kprintf("MPTABLE: warning PCI int dst apic id %d "
1034                         "does not exist\n", ent->dst_apic_id);
1035                 return 0;
1036         }
1037
1038         pci_pin = ent->src_bus_irq & 0x3;
1039         pci_dev = (ent->src_bus_irq >> 2) & 0x1f;
1040
1041         TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1042                 if (pci_int->mpci_bus == ent->src_bus_id &&
1043                     pci_int->mpci_dev == pci_dev &&
1044                     pci_int->mpci_pin == pci_pin) {
1045                         if (pci_int->mpci_ioapic_idx == ioapic->mio_idx &&
1046                             pci_int->mpci_ioapic_pin == ent->dst_apic_int) {
1047                                 kprintf("MPTABLE: warning duplicated "
1048                                         "PCI int entry for "
1049                                         "bus %d, dev %d, pin %d\n",
1050                                         pci_int->mpci_bus,
1051                                         pci_int->mpci_dev,
1052                                         pci_int->mpci_pin);
1053                                 return 0;
1054                         } else {
1055                                 kprintf("mptable_pci_int_register: "
1056                                         "conflict PCI int entry for "
1057                                         "bus %d, dev %d, pin %d, "
1058                                         "IOAPIC %d.%d -> %d.%d\n",
1059                                         pci_int->mpci_bus,
1060                                         pci_int->mpci_dev,
1061                                         pci_int->mpci_pin,
1062                                         pci_int->mpci_ioapic_idx,
1063                                         pci_int->mpci_ioapic_pin,
1064                                         ioapic->mio_idx,
1065                                         ent->dst_apic_int);
1066                                 return EINVAL;
1067                         }
1068                 }
1069         }
1070
1071         pci_int = kmalloc(sizeof(*pci_int), M_DEVBUF, M_WAITOK | M_ZERO);
1072
1073         pci_int->mpci_bus = ent->src_bus_id;
1074         pci_int->mpci_dev = pci_dev;
1075         pci_int->mpci_pin = pci_pin;
1076         pci_int->mpci_ioapic_idx = ioapic->mio_idx;
1077         pci_int->mpci_ioapic_pin = ent->dst_apic_int;
1078
1079         TAILQ_INSERT_TAIL(&mptable_pci_int_list, pci_int, mpci_link);
1080
1081         return 0;
1082 }
1083
1084 static void
1085 mptable_pci_int_register(void)
1086 {
1087         struct mptable_bus_info bus_info;
1088         const struct mptable_bus *bus;
1089         struct mptable_pci_int *pci_int;
1090         struct mptable_pos mpt;
1091         int error, force_pci0, npcibus;
1092         mpcth_t cth;
1093
1094         if (mptable_fps_phyaddr == 0)
1095                 return;
1096
1097         if (mptable_use_default)
1098                 return;
1099
1100         if (TAILQ_EMPTY(&mptable_ioapic_list))
1101                 return;
1102
1103         error = mptable_map(&mpt);
1104         if (error)
1105                 panic("mptable_pci_int_register: mptable_map failed");
1106         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1107
1108         cth = mpt.mp_cth;
1109
1110         mptable_bus_info_alloc(cth, &bus_info);
1111         if (TAILQ_EMPTY(&bus_info.mbi_list))
1112                 goto done;
1113
1114         force_pci0 = 0;
1115         npcibus = 0;
1116         TAILQ_FOREACH(bus, &bus_info.mbi_list, mb_link) {
1117                 if (bus->mb_type == MPTABLE_BUS_PCI)
1118                         ++npcibus;
1119         }
1120         if (npcibus == 0) {
1121                 mptable_bus_info_free(&bus_info);
1122                 goto done;
1123         } else if (npcibus == 1) {
1124                 force_pci0 = 1;
1125         }
1126
1127         error = mptable_iterate_entries(cth,
1128                     mptable_pci_int_callback, &bus_info);
1129
1130         mptable_bus_info_free(&bus_info);
1131
1132         if (error) {
1133                 while ((pci_int = TAILQ_FIRST(&mptable_pci_int_list)) != NULL) {
1134                         TAILQ_REMOVE(&mptable_pci_int_list, pci_int, mpci_link);
1135                         kfree(pci_int, M_DEVBUF);
1136                 }
1137                 goto done;
1138         }
1139
1140         if (force_pci0) {
1141                 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link)
1142                         pci_int->mpci_bus = 0;
1143         }
1144 done:
1145         mptable_unmap(&mpt);
1146 }
1147 SYSINIT(mptable_pci, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1148         mptable_pci_int_register, 0);
1149
1150 struct mptable_ioapic_probe_cbarg {
1151         const struct mptable_bus_info *bus_info;
1152 };
1153
1154 static int
1155 mptable_ioapic_probe_callback(void *xarg, const void *pos, int type)
1156 {
1157         struct mptable_ioapic_probe_cbarg *arg = xarg;
1158         const struct mptable_ioapic *ioapic;
1159         const struct mptable_bus *bus;
1160         const struct INTENTRY *ent;
1161
1162         if (type != 3)
1163                 return 0;
1164         ent = pos;
1165
1166         if (ent->int_type != 0)
1167                 return 0;
1168
1169         TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1170                 if (bus->mb_type == MPTABLE_BUS_ISA &&
1171                     bus->mb_id == ent->src_bus_id)
1172                         break;
1173         }
1174         if (bus == NULL)
1175                 return 0;
1176
1177         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1178                 if (ioapic->mio_apic_id == ent->dst_apic_id)
1179                         break;
1180         }
1181         if (ioapic == NULL) {
1182                 kprintf("MPTABLE: warning ISA int dst apic id %d "
1183                         "does not exist\n", ent->dst_apic_id);
1184                 return 0;
1185         }
1186
1187         /* XXX magic number */
1188         if (ent->src_bus_irq >= ISA_IRQ_CNT) {
1189                 kprintf("mptable_ioapic_probe: invalid ISA irq (%d)\n",
1190                         ent->src_bus_irq);
1191                 return EINVAL;
1192         }
1193         return 0;
1194 }
1195
1196 static int
1197 mptable_ioapic_probe(struct ioapic_enumerator *e)
1198 {
1199         struct mptable_ioapic_probe_cbarg arg;
1200         struct mptable_bus_info bus_info;
1201         struct mptable_pos mpt;
1202         mpcth_t cth;
1203         int error;
1204
1205         if (mptable_fps_phyaddr == 0)
1206                 return ENXIO;
1207
1208         if (mptable_use_default)
1209                 return 0;
1210
1211         if (TAILQ_EMPTY(&mptable_ioapic_list))
1212                 return ENXIO;
1213
1214         error = mptable_map(&mpt);
1215         if (error)
1216                 panic("mptable_ioapic_probe: mptable_map failed");
1217         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1218
1219         cth = mpt.mp_cth;
1220
1221         mptable_bus_info_alloc(cth, &bus_info);
1222
1223         bzero(&arg, sizeof(arg));
1224         arg.bus_info = &bus_info;
1225
1226         error = mptable_iterate_entries(cth,
1227                     mptable_ioapic_probe_callback, &arg);
1228
1229         mptable_bus_info_free(&bus_info);
1230         mptable_unmap(&mpt);
1231
1232         return error;
1233 }
1234
1235 struct mptable_ioapic_int_cbarg {
1236         const struct mptable_bus_info *bus_info;
1237         int     ioapic_nint;
1238 };
1239
1240 static int
1241 mptable_ioapic_int_callback(void *xarg, const void *pos, int type)
1242 {
1243         struct mptable_ioapic_int_cbarg *arg = xarg;
1244         const struct mptable_ioapic *ioapic;
1245         const struct mptable_bus *bus;
1246         const struct INTENTRY *ent;
1247         int gsi;
1248
1249         if (type != 3)
1250                 return 0;
1251
1252         arg->ioapic_nint++;
1253
1254         ent = pos;
1255         if (ent->int_type != 0)
1256                 return 0;
1257
1258         TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1259                 if (bus->mb_type == MPTABLE_BUS_ISA &&
1260                     bus->mb_id == ent->src_bus_id)
1261                         break;
1262         }
1263         if (bus == NULL)
1264                 return 0;
1265
1266         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1267                 if (ioapic->mio_apic_id == ent->dst_apic_id)
1268                         break;
1269         }
1270         if (ioapic == NULL) {
1271                 kprintf("MPTABLE: warning ISA int dst apic id %d "
1272                         "does not exist\n", ent->dst_apic_id);
1273                 return 0;
1274         }
1275
1276         if (ent->dst_apic_int >= ioapic->mio_npin) {
1277                 panic("mptable_ioapic_enumerate: invalid I/O APIC "
1278                       "pin %d, should be < %d",
1279                       ent->dst_apic_int, ioapic->mio_npin);
1280         }
1281         gsi = ioapic->mio_gsi_base + ent->dst_apic_int;
1282
1283         if (ent->src_bus_irq != gsi) {
1284                 if (bootverbose) {
1285                         kprintf("MPTABLE: INTSRC irq %d -> GSI %d\n",
1286                                 ent->src_bus_irq, gsi);
1287                 }
1288                 ioapic_intsrc(ent->src_bus_irq, gsi,
1289                     INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1290         }
1291         return 0;
1292 }
1293
1294 static void
1295 mptable_ioapic_enumerate(struct ioapic_enumerator *e)
1296 {
1297         struct mptable_bus_info bus_info;
1298         struct mptable_ioapic *ioapic;
1299         struct mptable_pos mpt;
1300         mpcth_t cth;
1301         int error;
1302
1303         KKASSERT(mptable_fps_phyaddr != 0);
1304         KKASSERT(!TAILQ_EMPTY(&mptable_ioapic_list));
1305
1306         TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1307                 const struct mptable_ioapic *prev_ioapic;
1308                 uint32_t ver;
1309                 void *addr;
1310
1311                 addr = ioapic_map(ioapic->mio_addr);
1312
1313                 ver = ioapic_read(addr, IOAPIC_VER);
1314                 ioapic->mio_npin = ((ver & IOART_VER_MAXREDIR)
1315                                     >> MAXREDIRSHIFT) + 1;
1316
1317                 prev_ioapic = TAILQ_PREV(ioapic,
1318                                 mptable_ioapic_list, mio_link);
1319                 if (prev_ioapic == NULL) {
1320                         ioapic->mio_gsi_base = 0;
1321                 } else {
1322                         ioapic->mio_gsi_base =
1323                                 prev_ioapic->mio_gsi_base +
1324                                 prev_ioapic->mio_npin;
1325                 }
1326                 ioapic_add(addr, ioapic->mio_gsi_base,
1327                     ioapic->mio_npin);
1328
1329                 if (bootverbose) {
1330                         kprintf("MPTABLE: IOAPIC addr 0x%08x, "
1331                                 "apic id %d, idx %d, gsi base %d, npin %d\n",
1332                                 ioapic->mio_addr,
1333                                 ioapic->mio_apic_id,
1334                                 ioapic->mio_idx,
1335                                 ioapic->mio_gsi_base,
1336                                 ioapic->mio_npin);
1337                 }
1338         }
1339
1340         if (mptable_use_default) {
1341                 if (bootverbose)
1342                         kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (default)\n");
1343                 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1344                 return;
1345         }
1346
1347         error = mptable_map(&mpt);
1348         if (error)
1349                 panic("mptable_ioapic_probe: mptable_map failed");
1350         KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1351
1352         cth = mpt.mp_cth;
1353
1354         mptable_bus_info_alloc(cth, &bus_info);
1355
1356         if (TAILQ_EMPTY(&bus_info.mbi_list)) {
1357                 if (bootverbose)
1358                         kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (no bus)\n");
1359                 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1360         } else {
1361                 struct mptable_ioapic_int_cbarg arg;
1362
1363                 bzero(&arg, sizeof(arg));
1364                 arg.bus_info = &bus_info;
1365
1366                 error = mptable_iterate_entries(cth,
1367                             mptable_ioapic_int_callback, &arg);
1368                 if (error)
1369                         panic("mptable_ioapic_int failed");
1370
1371                 if (arg.ioapic_nint == 0) {
1372                         if (bootverbose) {
1373                                 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 "
1374                                         "(no int)\n");
1375                         }
1376                         ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE,
1377                             INTR_POLARITY_HIGH);
1378                 }
1379         }
1380
1381         mptable_bus_info_free(&bus_info);
1382
1383         mptable_unmap(&mpt);
1384 }
1385
1386 static struct ioapic_enumerator mptable_ioapic_enumerator = {
1387         .ioapic_prio = IOAPIC_ENUM_PRIO_MPTABLE,
1388         .ioapic_probe = mptable_ioapic_probe,
1389         .ioapic_enumerate = mptable_ioapic_enumerate
1390 };
1391
1392 static void
1393 mptable_ioapic_enum_register(void)
1394 {
1395         ioapic_enumerator_register(&mptable_ioapic_enumerator);
1396 }
1397 SYSINIT(mptable_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1398         mptable_ioapic_enum_register, 0);
1399
1400 void
1401 mptable_pci_int_dump(void)
1402 {
1403         const struct mptable_pci_int *pci_int;
1404
1405         if (!bootverbose)
1406                 return;
1407
1408         TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1409                 kprintf("MPTABLE: %d:%d INT%c -> IOAPIC %d.%d\n",
1410                         pci_int->mpci_bus,
1411                         pci_int->mpci_dev,
1412                         pci_int->mpci_pin + 'A',
1413                         pci_int->mpci_ioapic_idx,
1414                         pci_int->mpci_ioapic_pin);
1415         }
1416 }
1417
1418 int
1419 mptable_pci_int_route(int bus, int dev, int pin, int intline)
1420 {
1421         const struct mptable_pci_int *pci_int;
1422         int irq = -1;
1423
1424         KKASSERT(pin >= 1);
1425         --pin;  /* zero based */
1426
1427         TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1428                 if (pci_int->mpci_bus == bus &&
1429                     pci_int->mpci_dev == dev &&
1430                     pci_int->mpci_pin == pin)
1431                         break;
1432         }
1433         if (pci_int != NULL) {
1434                 int gsi;
1435
1436                 gsi = ioapic_gsi(pci_int->mpci_ioapic_idx,
1437                         pci_int->mpci_ioapic_pin);
1438                 if (gsi >= 0) {
1439                         irq = ioapic_find_legacy_by_gsi(gsi,
1440                                 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1441                 }
1442         }
1443
1444         if (irq < 0) {
1445                 if (bootverbose) {
1446                         kprintf("MPTABLE: fixed interrupt routing "
1447                                 "for %d:%d INT%c\n", bus, dev, pin + 'A');
1448                 }
1449
1450                 irq = ioapic_find_legacy_by_irq(intline,
1451                         INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1452         }
1453
1454         if (irq >= 0 && bootverbose) {
1455                 kprintf("MPTABLE: %d:%d INT%c routed to irq %d\n",
1456                         bus, dev, pin + 'A', irq);
1457         }
1458         return irq;
1459 }