2 * Copyright (c) 1996, by Steve Passe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: src/sys/i386/i386/mp_machdep.c,v 1.115.2.15 2003/03/14 21:22:35 jhb Exp $
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>
39 #include <sys/mplock2.h>
42 #include <vm/vm_param.h>
44 #include <vm/vm_kern.h>
45 #include <vm/vm_extern.h>
47 #include <vm/vm_map.h>
53 #include <machine/smp.h>
54 #include <machine_base/isa/isa_intr.h>
55 #include <machine_base/apic/apicreg.h>
56 #include <machine_base/apic/apicvar.h>
57 #include <machine/atomic.h>
58 #include <machine/cpufunc.h>
59 #include <machine/cputypes.h>
60 #include <machine_base/apic/lapic.h>
61 #include <machine_base/apic/ioapic.h>
62 #include <machine/psl.h>
63 #include <machine/segments.h>
64 #include <machine/tss.h>
65 #include <machine/specialreg.h>
66 #include <machine/globaldata.h>
67 #include <machine/pmap_inval.h>
68 #include <machine/mptable.h>
70 #include <machine/md_var.h> /* setidt() */
71 #include <machine_base/icu/icu.h> /* IPIs */
72 #include <machine_base/apic/ioapic_abi.h>
73 #include <machine/intr_machdep.h> /* IPIs */
75 extern u_int base_memory;
76 extern u_long ebda_addr;
77 extern int imcr_present;
80 #define BIOS_BASE (0xf0000)
81 #define BIOS_BASE2 (0xe0000)
82 #define BIOS_SIZE (0x10000)
83 #define BIOS_COUNT (BIOS_SIZE/4)
85 #define PROCENTRY_FLAG_EN 0x01
86 #define PROCENTRY_FLAG_BP 0x02
87 #define IOAPICENTRY_FLAG_EN 0x01
90 /* MP Floating Pointer Structure */
91 typedef struct MPFPS {
104 /* MP Configuration Table Header */
105 typedef struct MPCTH {
107 u_short base_table_length;
111 u_char product_id[12];
112 u_int32_t oem_table_pointer;
113 u_short oem_table_size;
115 u_int32_t apic_address;
116 u_short extended_table_length;
117 u_char extended_table_checksum;
122 typedef struct PROCENTRY {
127 u_int32_t cpu_signature;
128 u_int32_t feature_flags;
133 typedef struct BUSENTRY {
139 typedef struct IOAPICENTRY {
144 u_int32_t apic_address;
145 } *io_apic_entry_ptr;
147 typedef struct INTENTRY {
157 /* descriptions of MP basetable entries */
158 typedef struct BASETABLE_ENTRY {
167 vm_size_t mp_cth_mapsz;
170 #define MPTABLE_POS_USE_DEFAULT(mpt) \
171 ((mpt)->mp_fps->mpfb1 != 0 || (mpt)->mp_cth == NULL)
175 int mb_type; /* MPTABLE_BUS_ */
176 TAILQ_ENTRY(mptable_bus) mb_link;
179 #define MPTABLE_BUS_ISA 0
180 #define MPTABLE_BUS_PCI 1
182 struct mptable_bus_info {
183 TAILQ_HEAD(, mptable_bus) mbi_list;
186 struct mptable_pci_int {
193 TAILQ_ENTRY(mptable_pci_int) mpci_link;
196 struct mptable_ioapic {
202 TAILQ_ENTRY(mptable_ioapic) mio_link;
205 typedef int (*mptable_iter_func)(void *, const void *, int);
207 static int mptable_iterate_entries(const mpcth_t,
208 mptable_iter_func, void *);
209 static int mptable_search(void);
210 static long mptable_search_sig(u_int32_t target, int count);
211 static int mptable_hyperthread_fixup(cpumask_t, int);
212 static int mptable_map(struct mptable_pos *);
213 static void mptable_unmap(struct mptable_pos *);
214 static void mptable_bus_info_alloc(const mpcth_t,
215 struct mptable_bus_info *);
216 static void mptable_bus_info_free(struct mptable_bus_info *);
218 static int mptable_lapic_probe(struct lapic_enumerator *);
219 static int mptable_lapic_enumerate(struct lapic_enumerator *);
220 static void mptable_lapic_default(void);
222 static int mptable_ioapic_probe(struct ioapic_enumerator *);
223 static void mptable_ioapic_enumerate(struct ioapic_enumerator *);
225 static basetable_entry basetable_entry_types[] =
227 {0, 20, "Processor"},
234 static vm_paddr_t mptable_fps_phyaddr;
235 static int mptable_use_default;
236 static TAILQ_HEAD(mptable_pci_int_list, mptable_pci_int) mptable_pci_int_list =
237 TAILQ_HEAD_INITIALIZER(mptable_pci_int_list);
238 static TAILQ_HEAD(mptable_ioapic_list, mptable_ioapic) mptable_ioapic_list =
239 TAILQ_HEAD_INITIALIZER(mptable_ioapic_list);
244 struct mptable_pos mpt;
247 KKASSERT(mptable_fps_phyaddr == 0);
249 mptable_fps_phyaddr = mptable_search();
250 if (mptable_fps_phyaddr == 0)
253 error = mptable_map(&mpt);
255 mptable_fps_phyaddr = 0;
259 if (MPTABLE_POS_USE_DEFAULT(&mpt)) {
260 kprintf("MPTABLE: use default configuration\n");
261 mptable_use_default = 1;
263 if (mpt.mp_fps->mpfb2 & 0x80)
268 SYSINIT(mptable_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, mptable_probe, 0);
271 * Look for an Intel MP spec table (ie, SMP capable hardware).
279 /* see if EBDA exists */
280 if (ebda_addr != 0) {
281 /* search first 1K of EBDA */
282 target = (u_int32_t)ebda_addr;
283 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
286 /* last 1K of base memory, effective 'top of base' passed in */
287 target = (u_int32_t)(base_memory - 0x400);
288 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
292 /* search the BIOS */
293 target = (u_int32_t)BIOS_BASE;
294 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
297 /* search the extended BIOS */
298 target = (u_int32_t)BIOS_BASE2;
299 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
307 mptable_iterate_entries(const mpcth_t cth, mptable_iter_func func, void *arg)
309 int count, total_size;
310 const void *position;
312 KKASSERT(cth->base_table_length >= sizeof(struct MPCTH));
313 total_size = cth->base_table_length - sizeof(struct MPCTH);
314 position = (const uint8_t *)cth + sizeof(struct MPCTH);
315 count = cth->entry_count;
320 KKASSERT(total_size >= 0);
321 if (total_size == 0) {
322 kprintf("invalid base MP table, "
323 "entry count and length mismatch\n");
327 type = *(const uint8_t *)position;
329 case 0: /* processor_entry */
330 case 1: /* bus_entry */
331 case 2: /* io_apic_entry */
332 case 3: /* int_entry */
333 case 4: /* int_entry */
336 kprintf("unknown base MP table entry type %d\n", type);
340 if (total_size < basetable_entry_types[type].length) {
341 kprintf("invalid base MP table length, "
342 "does not contain all entries\n");
345 total_size -= basetable_entry_types[type].length;
347 error = func(arg, position, type);
351 position = (const uint8_t *)position +
352 basetable_entry_types[type].length;
358 * look for the MP spec signature
361 /* string defined by the Intel MP Spec as identifying the MP table */
362 #define MP_SIG 0x5f504d5f /* _MP_ */
363 #define NEXT(X) ((X) += 4)
365 mptable_search_sig(u_int32_t target, int count)
371 KKASSERT(target != 0);
373 map_size = count * sizeof(u_int32_t);
374 addr = pmap_mapdev((vm_paddr_t)target, map_size);
377 for (x = 0; x < count; NEXT(x)) {
378 if (addr[x] == MP_SIG) {
379 /* make array index a byte index */
380 ret = target + (x * sizeof(u_int32_t));
385 pmap_unmapdev((vm_offset_t)addr, map_size);
389 static int processor_entry (const struct PROCENTRY *entry, int cpu);
392 * Check if we should perform a hyperthreading "fix-up" to
393 * enumerate any logical CPU's that aren't already listed
396 * XXX: We assume that all of the physical CPUs in the
397 * system have the same number of logical CPUs.
399 * XXX: We assume that APIC ID's are allocated such that
400 * the APIC ID's for a physical processor are aligned
401 * with the number of logical CPU's in the processor.
404 mptable_hyperthread_fixup(cpumask_t id_mask, int cpu_count)
406 int i, id, lcpus_max, logical_cpus;
408 if ((cpu_feature & CPUID_HTT) == 0)
411 lcpus_max = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
415 if (cpu_vendor_id == CPU_VENDOR_INTEL) {
417 * INSTRUCTION SET REFERENCE, A-M (#253666)
418 * Page 3-181, Table 3-20
419 * "The nearest power-of-2 integer that is not smaller
420 * than EBX[23:16] is the number of unique initial APIC
421 * IDs reserved for addressing different logical
422 * processors in a physical package."
425 if ((1 << i) >= lcpus_max) {
432 KKASSERT(cpu_count != 0);
433 if (cpu_count == lcpus_max) {
434 /* We have nothing to fix */
436 } else if (cpu_count == 1) {
437 /* XXX this may be incorrect */
438 logical_cpus = lcpus_max;
443 * Calculate the distances between two nearest
444 * APIC IDs. If all such distances are same,
445 * then it is the number of missing cpus that
446 * we are going to fill later.
448 dist = cur = prev = -1;
449 for (id = 0; id < MAXCPU; ++id) {
450 if ((id_mask & CPUMASK(id)) == 0)
455 int new_dist = cur - prev;
461 * Make sure that all distances
462 * between two nearest APIC IDs
465 if (dist != new_dist)
473 /* Must be power of 2 */
474 if (dist & (dist - 1))
477 /* Can't exceed CPU package capacity */
478 if (dist > lcpus_max)
479 logical_cpus = lcpus_max;
485 * For each APIC ID of a CPU that is set in the mask,
486 * scan the other candidate APIC ID's for this
487 * physical processor. If any of those ID's are
488 * already in the table, then kill the fixup.
490 for (id = 0; id < MAXCPU; id++) {
491 if ((id_mask & CPUMASK(id)) == 0)
493 /* First, make sure we are on a logical_cpus boundary. */
494 if (id % logical_cpus != 0)
496 for (i = id + 1; i < id + logical_cpus; i++)
497 if ((id_mask & CPUMASK(i)) != 0)
504 mptable_map(struct mptable_pos *mpt)
508 vm_size_t cth_mapsz = 0;
510 KKASSERT(mptable_fps_phyaddr != 0);
512 bzero(mpt, sizeof(*mpt));
514 fps = pmap_mapdev(mptable_fps_phyaddr, sizeof(*fps));
517 * Map configuration table header to get
518 * the base table size
520 cth = pmap_mapdev(fps->pap, sizeof(*cth));
521 cth_mapsz = cth->base_table_length;
522 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
524 if (cth_mapsz < sizeof(*cth)) {
525 kprintf("invalid base MP table length %d\n",
527 pmap_unmapdev((vm_offset_t)fps, sizeof(*fps));
534 cth = pmap_mapdev(fps->pap, cth_mapsz);
539 mpt->mp_cth_mapsz = cth_mapsz;
545 mptable_unmap(struct mptable_pos *mpt)
547 if (mpt->mp_cth != NULL) {
548 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
550 mpt->mp_cth_mapsz = 0;
552 if (mpt->mp_fps != NULL) {
553 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
559 processor_entry(const struct PROCENTRY *entry, int cpu)
563 /* check for usability */
564 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
567 /* check for BSP flag */
568 if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
569 lapic_set_cpuid(0, entry->apic_id);
570 return 0; /* its already been counted */
573 /* add another AP to list, if less than max number of CPUs */
574 else if (cpu < MAXCPU) {
575 lapic_set_cpuid(cpu, entry->apic_id);
583 mptable_bus_info_callback(void *xarg, const void *pos, int type)
585 struct mptable_bus_info *bus_info = xarg;
586 const struct BUSENTRY *ent;
587 struct mptable_bus *bus;
593 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
594 if (bus->mb_id == ent->bus_id) {
595 kprintf("mptable_bus_info_alloc: duplicated bus id "
596 "(%d)\n", bus->mb_id);
602 if (strncmp(ent->bus_type, "PCI", 3) == 0) {
603 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
604 bus->mb_type = MPTABLE_BUS_PCI;
605 } else if (strncmp(ent->bus_type, "ISA", 3) == 0) {
606 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
607 bus->mb_type = MPTABLE_BUS_ISA;
611 bus->mb_id = ent->bus_id;
612 TAILQ_INSERT_TAIL(&bus_info->mbi_list, bus, mb_link);
618 mptable_bus_info_alloc(const mpcth_t cth, struct mptable_bus_info *bus_info)
622 bzero(bus_info, sizeof(*bus_info));
623 TAILQ_INIT(&bus_info->mbi_list);
625 error = mptable_iterate_entries(cth, mptable_bus_info_callback, bus_info);
627 mptable_bus_info_free(bus_info);
631 mptable_bus_info_free(struct mptable_bus_info *bus_info)
633 struct mptable_bus *bus;
635 while ((bus = TAILQ_FIRST(&bus_info->mbi_list)) != NULL) {
636 TAILQ_REMOVE(&bus_info->mbi_list, bus, mb_link);
641 struct mptable_lapic_cbarg1 {
644 u_int ht_apicid_mask;
648 mptable_lapic_pass1_callback(void *xarg, const void *pos, int type)
650 const struct PROCENTRY *ent;
651 struct mptable_lapic_cbarg1 *arg = xarg;
657 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
661 if (ent->apic_id < 32) {
662 arg->ht_apicid_mask |= 1 << ent->apic_id;
663 } else if (arg->ht_fixup) {
664 kprintf("MPTABLE: lapic id > 32, disable HTT fixup\n");
670 struct mptable_lapic_cbarg2 {
677 mptable_lapic_pass2_callback(void *xarg, const void *pos, int type)
679 const struct PROCENTRY *ent;
680 struct mptable_lapic_cbarg2 *arg = xarg;
686 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
687 KKASSERT(!arg->found_bsp);
691 if (processor_entry(ent, arg->cpu))
694 if (arg->logical_cpus) {
695 struct PROCENTRY proc;
699 * Create fake mptable processor entries
700 * and feed them to processor_entry() to
701 * enumerate the logical CPUs.
703 bzero(&proc, sizeof(proc));
705 proc.cpu_flags = PROCENTRY_FLAG_EN;
706 proc.apic_id = ent->apic_id;
708 for (i = 1; i < arg->logical_cpus; i++) {
710 processor_entry(&proc, arg->cpu);
718 mptable_lapic_default(void)
720 int ap_apicid, bsp_apicid;
722 naps = 1; /* exclude BSP */
724 /* Map local apic before the id field is accessed */
725 lapic_map(DEFAULT_APIC_BASE);
727 bsp_apicid = APIC_ID(lapic->id);
728 ap_apicid = (bsp_apicid == 0) ? 1 : 0;
731 lapic_set_cpuid(0, bsp_apicid);
732 /* one and only AP */
733 lapic_set_cpuid(1, ap_apicid);
739 * APIC ID <-> CPU ID mappings
742 mptable_lapic_enumerate(struct lapic_enumerator *e)
744 struct mptable_pos mpt;
745 struct mptable_lapic_cbarg1 arg1;
746 struct mptable_lapic_cbarg2 arg2;
748 int error, logical_cpus = 0;
749 vm_paddr_t lapic_addr;
751 if (mptable_use_default) {
752 mptable_lapic_default();
756 error = mptable_map(&mpt);
758 panic("mptable_lapic_enumerate mptable_map failed");
759 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
763 /* Save local apic address */
764 lapic_addr = cth->apic_address;
765 KKASSERT(lapic_addr != 0);
768 * Find out how many CPUs do we have
770 bzero(&arg1, sizeof(arg1));
771 arg1.ht_fixup = 1; /* Apply ht fixup by default */
773 error = mptable_iterate_entries(cth,
774 mptable_lapic_pass1_callback, &arg1);
776 panic("mptable_iterate_entries(lapic_pass1) failed");
777 KKASSERT(arg1.cpu_count != 0);
779 /* See if we need to fixup HT logical CPUs. */
781 logical_cpus = mptable_hyperthread_fixup(arg1.ht_apicid_mask,
783 if (logical_cpus != 0)
784 arg1.cpu_count *= logical_cpus;
786 naps = arg1.cpu_count - 1; /* subtract the BSP */
789 * Link logical CPU id to local apic id
791 bzero(&arg2, sizeof(arg2));
793 arg2.logical_cpus = logical_cpus;
795 error = mptable_iterate_entries(cth,
796 mptable_lapic_pass2_callback, &arg2);
798 panic("mptable_iterate_entries(lapic_pass2) failed");
799 KKASSERT(arg2.found_bsp);
802 lapic_map(lapic_addr);
809 struct mptable_lapic_probe_cbarg {
815 mptable_lapic_probe_callback(void *xarg, const void *pos, int type)
817 const struct PROCENTRY *ent;
818 struct mptable_lapic_probe_cbarg *arg = xarg;
824 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
828 if (ent->apic_id == APICID_MAX) {
829 kprintf("MPTABLE: invalid LAPIC apic id %d\n",
834 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
835 if (arg->found_bsp) {
836 kprintf("more than one BSP in base MP table\n");
845 mptable_lapic_probe(struct lapic_enumerator *e)
847 struct mptable_pos mpt;
848 struct mptable_lapic_probe_cbarg arg;
852 if (mptable_fps_phyaddr == 0)
855 if (mptable_use_default)
858 error = mptable_map(&mpt);
861 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
866 if (cth->apic_address == 0)
869 bzero(&arg, sizeof(arg));
870 error = mptable_iterate_entries(cth,
871 mptable_lapic_probe_callback, &arg);
873 if (arg.cpu_count == 0) {
874 kprintf("MP table contains no processor entries\n");
876 } else if (!arg.found_bsp) {
877 kprintf("MP table does not contains BSP entry\n");
886 static struct lapic_enumerator mptable_lapic_enumerator = {
887 .lapic_prio = LAPIC_ENUM_PRIO_MPTABLE,
888 .lapic_probe = mptable_lapic_probe,
889 .lapic_enumerate = mptable_lapic_enumerate
893 mptable_lapic_enum_register(void)
895 lapic_enumerator_register(&mptable_lapic_enumerator);
897 SYSINIT(mptable_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
898 mptable_lapic_enum_register, 0);
901 mptable_ioapic_list_callback(void *xarg, const void *pos, int type)
903 const struct IOAPICENTRY *ent;
904 struct mptable_ioapic *nioapic, *ioapic;
910 if ((ent->apic_flags & IOAPICENTRY_FLAG_EN) == 0)
913 if (ent->apic_address == 0) {
914 kprintf("mptable_ioapic_create_list: zero IOAPIC addr\n");
917 if (ent->apic_id == APICID_MAX) {
918 kprintf("mptable_ioapic_create_list: "
919 "invalid IOAPIC apic id %d\n", ent->apic_id);
923 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
924 if (ioapic->mio_apic_id == ent->apic_id) {
925 kprintf("mptable_ioapic_create_list: duplicated "
926 "apic id %d\n", ioapic->mio_apic_id);
929 if (ioapic->mio_addr == ent->apic_address) {
930 kprintf("mptable_ioapic_create_list: overlapped "
931 "IOAPIC addr 0x%08x", ioapic->mio_addr);
936 nioapic = kmalloc(sizeof(*nioapic), M_DEVBUF, M_WAITOK | M_ZERO);
937 nioapic->mio_apic_id = ent->apic_id;
938 nioapic->mio_addr = ent->apic_address;
941 * Create IOAPIC list in ascending order of APIC ID
943 TAILQ_FOREACH_REVERSE(ioapic, &mptable_ioapic_list,
944 mptable_ioapic_list, mio_link) {
945 if (nioapic->mio_apic_id > ioapic->mio_apic_id) {
946 TAILQ_INSERT_AFTER(&mptable_ioapic_list,
947 ioapic, nioapic, mio_link);
952 TAILQ_INSERT_HEAD(&mptable_ioapic_list, nioapic, mio_link);
958 mptable_ioapic_create_list(void)
960 struct mptable_ioapic *ioapic;
961 struct mptable_pos mpt;
964 if (mptable_fps_phyaddr == 0)
967 if (mptable_use_default) {
968 ioapic = kmalloc(sizeof(*ioapic), M_DEVBUF, M_WAITOK | M_ZERO);
970 ioapic->mio_apic_id = 0; /* NOTE: any value is ok here */
971 ioapic->mio_addr = 0xfec00000; /* XXX magic number */
973 TAILQ_INSERT_HEAD(&mptable_ioapic_list, ioapic, mio_link);
977 error = mptable_map(&mpt);
979 panic("mptable_ioapic_create_list: mptable_map failed");
980 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
982 error = mptable_iterate_entries(mpt.mp_cth,
983 mptable_ioapic_list_callback, NULL);
985 while ((ioapic = TAILQ_FIRST(&mptable_ioapic_list)) != NULL) {
986 TAILQ_REMOVE(&mptable_ioapic_list, ioapic, mio_link);
987 kfree(ioapic, M_DEVBUF);
993 * Assign index number for each IOAPIC
996 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
997 ioapic->mio_idx = idx;
1001 mptable_unmap(&mpt);
1003 SYSINIT(mptable_ioapic_list, SI_BOOT2_PRESMP, SI_ORDER_SECOND,
1004 mptable_ioapic_create_list, 0);
1007 mptable_pci_int_callback(void *xarg, const void *pos, int type)
1009 const struct mptable_bus_info *bus_info = xarg;
1010 const struct mptable_ioapic *ioapic;
1011 const struct mptable_bus *bus;
1012 struct mptable_pci_int *pci_int;
1013 const struct INTENTRY *ent;
1014 int pci_pin, pci_dev;
1020 if (ent->int_type != 0)
1023 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
1024 if (bus->mb_type == MPTABLE_BUS_PCI &&
1025 bus->mb_id == ent->src_bus_id)
1031 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1032 if (ioapic->mio_apic_id == ent->dst_apic_id)
1035 if (ioapic == NULL) {
1036 kprintf("MPTABLE: warning PCI int dst apic id %d "
1037 "does not exist\n", ent->dst_apic_id);
1041 pci_pin = ent->src_bus_irq & 0x3;
1042 pci_dev = (ent->src_bus_irq >> 2) & 0x1f;
1044 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1045 if (pci_int->mpci_bus == ent->src_bus_id &&
1046 pci_int->mpci_dev == pci_dev &&
1047 pci_int->mpci_pin == pci_pin) {
1048 if (pci_int->mpci_ioapic_idx == ioapic->mio_idx &&
1049 pci_int->mpci_ioapic_pin == ent->dst_apic_int) {
1050 kprintf("MPTABLE: warning duplicated "
1051 "PCI int entry for "
1052 "bus %d, dev %d, pin %d\n",
1058 kprintf("mptable_pci_int_register: "
1059 "conflict PCI int entry for "
1060 "bus %d, dev %d, pin %d, "
1061 "IOAPIC %d.%d -> %d.%d\n",
1065 pci_int->mpci_ioapic_idx,
1066 pci_int->mpci_ioapic_pin,
1074 pci_int = kmalloc(sizeof(*pci_int), M_DEVBUF, M_WAITOK | M_ZERO);
1076 pci_int->mpci_bus = ent->src_bus_id;
1077 pci_int->mpci_dev = pci_dev;
1078 pci_int->mpci_pin = pci_pin;
1079 pci_int->mpci_ioapic_idx = ioapic->mio_idx;
1080 pci_int->mpci_ioapic_pin = ent->dst_apic_int;
1082 TAILQ_INSERT_TAIL(&mptable_pci_int_list, pci_int, mpci_link);
1088 mptable_pci_int_register(void)
1090 struct mptable_bus_info bus_info;
1091 const struct mptable_bus *bus;
1092 struct mptable_pci_int *pci_int;
1093 struct mptable_pos mpt;
1094 int error, force_pci0, npcibus;
1097 if (mptable_fps_phyaddr == 0)
1100 if (mptable_use_default)
1103 if (TAILQ_EMPTY(&mptable_ioapic_list))
1106 error = mptable_map(&mpt);
1108 panic("mptable_pci_int_register: mptable_map failed");
1109 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1113 mptable_bus_info_alloc(cth, &bus_info);
1114 if (TAILQ_EMPTY(&bus_info.mbi_list))
1119 TAILQ_FOREACH(bus, &bus_info.mbi_list, mb_link) {
1120 if (bus->mb_type == MPTABLE_BUS_PCI)
1124 mptable_bus_info_free(&bus_info);
1126 } else if (npcibus == 1) {
1130 error = mptable_iterate_entries(cth,
1131 mptable_pci_int_callback, &bus_info);
1133 mptable_bus_info_free(&bus_info);
1136 while ((pci_int = TAILQ_FIRST(&mptable_pci_int_list)) != NULL) {
1137 TAILQ_REMOVE(&mptable_pci_int_list, pci_int, mpci_link);
1138 kfree(pci_int, M_DEVBUF);
1144 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link)
1145 pci_int->mpci_bus = 0;
1148 mptable_unmap(&mpt);
1150 SYSINIT(mptable_pci, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1151 mptable_pci_int_register, 0);
1153 struct mptable_ioapic_probe_cbarg {
1154 const struct mptable_bus_info *bus_info;
1158 mptable_ioapic_probe_callback(void *xarg, const void *pos, int type)
1160 struct mptable_ioapic_probe_cbarg *arg = xarg;
1161 const struct mptable_ioapic *ioapic;
1162 const struct mptable_bus *bus;
1163 const struct INTENTRY *ent;
1169 if (ent->int_type != 0)
1172 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1173 if (bus->mb_type == MPTABLE_BUS_ISA &&
1174 bus->mb_id == ent->src_bus_id)
1180 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1181 if (ioapic->mio_apic_id == ent->dst_apic_id)
1184 if (ioapic == NULL) {
1185 kprintf("MPTABLE: warning ISA int dst apic id %d "
1186 "does not exist\n", ent->dst_apic_id);
1190 /* XXX magic number */
1191 if (ent->src_bus_irq >= ISA_IRQ_CNT) {
1192 kprintf("mptable_ioapic_probe: invalid ISA irq (%d)\n",
1200 mptable_ioapic_probe(struct ioapic_enumerator *e)
1202 struct mptable_ioapic_probe_cbarg arg;
1203 struct mptable_bus_info bus_info;
1204 struct mptable_pos mpt;
1208 if (mptable_fps_phyaddr == 0)
1211 if (mptable_use_default)
1214 if (TAILQ_EMPTY(&mptable_ioapic_list))
1217 error = mptable_map(&mpt);
1219 panic("mptable_ioapic_probe: mptable_map failed");
1220 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1224 mptable_bus_info_alloc(cth, &bus_info);
1226 bzero(&arg, sizeof(arg));
1227 arg.bus_info = &bus_info;
1229 error = mptable_iterate_entries(cth,
1230 mptable_ioapic_probe_callback, &arg);
1232 mptable_bus_info_free(&bus_info);
1233 mptable_unmap(&mpt);
1238 struct mptable_ioapic_int_cbarg {
1239 const struct mptable_bus_info *bus_info;
1244 mptable_ioapic_int_callback(void *xarg, const void *pos, int type)
1246 struct mptable_ioapic_int_cbarg *arg = xarg;
1247 const struct mptable_ioapic *ioapic;
1248 const struct mptable_bus *bus;
1249 const struct INTENTRY *ent;
1258 if (ent->int_type != 0)
1261 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1262 if (bus->mb_type == MPTABLE_BUS_ISA &&
1263 bus->mb_id == ent->src_bus_id)
1269 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1270 if (ioapic->mio_apic_id == ent->dst_apic_id)
1273 if (ioapic == NULL) {
1274 kprintf("MPTABLE: warning ISA int dst apic id %d "
1275 "does not exist\n", ent->dst_apic_id);
1279 if (ent->dst_apic_int >= ioapic->mio_npin) {
1280 panic("mptable_ioapic_enumerate: invalid I/O APIC "
1281 "pin %d, should be < %d",
1282 ent->dst_apic_int, ioapic->mio_npin);
1284 gsi = ioapic->mio_gsi_base + ent->dst_apic_int;
1286 if (ent->src_bus_irq != gsi) {
1288 kprintf("MPTABLE: INTSRC irq %d -> GSI %d\n",
1289 ent->src_bus_irq, gsi);
1291 ioapic_intsrc(ent->src_bus_irq, gsi,
1292 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1298 mptable_ioapic_enumerate(struct ioapic_enumerator *e)
1300 struct mptable_bus_info bus_info;
1301 struct mptable_ioapic *ioapic;
1302 struct mptable_pos mpt;
1306 KKASSERT(mptable_fps_phyaddr != 0);
1307 KKASSERT(!TAILQ_EMPTY(&mptable_ioapic_list));
1309 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1310 const struct mptable_ioapic *prev_ioapic;
1314 addr = ioapic_map(ioapic->mio_addr);
1316 ver = ioapic_read(addr, IOAPIC_VER);
1317 ioapic->mio_npin = ((ver & IOART_VER_MAXREDIR)
1318 >> MAXREDIRSHIFT) + 1;
1320 prev_ioapic = TAILQ_PREV(ioapic,
1321 mptable_ioapic_list, mio_link);
1322 if (prev_ioapic == NULL) {
1323 ioapic->mio_gsi_base = 0;
1325 ioapic->mio_gsi_base =
1326 prev_ioapic->mio_gsi_base +
1327 prev_ioapic->mio_npin;
1329 ioapic_add(addr, ioapic->mio_gsi_base, ioapic->mio_npin);
1332 kprintf("MPTABLE: IOAPIC addr 0x%08x, "
1333 "apic id %d, idx %d, gsi base %d, npin %d\n",
1335 ioapic->mio_apic_id,
1337 ioapic->mio_gsi_base,
1342 if (mptable_use_default) {
1344 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (default)\n");
1345 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1349 error = mptable_map(&mpt);
1351 panic("mptable_ioapic_probe: mptable_map failed");
1352 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1356 mptable_bus_info_alloc(cth, &bus_info);
1358 if (TAILQ_EMPTY(&bus_info.mbi_list)) {
1360 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (no bus)\n");
1361 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1363 struct mptable_ioapic_int_cbarg arg;
1365 bzero(&arg, sizeof(arg));
1366 arg.bus_info = &bus_info;
1368 error = mptable_iterate_entries(cth,
1369 mptable_ioapic_int_callback, &arg);
1371 panic("mptable_ioapic_int failed");
1373 if (arg.ioapic_nint == 0) {
1375 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 "
1378 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE,
1379 INTR_POLARITY_HIGH);
1383 mptable_bus_info_free(&bus_info);
1385 mptable_unmap(&mpt);
1388 static struct ioapic_enumerator mptable_ioapic_enumerator = {
1389 .ioapic_prio = IOAPIC_ENUM_PRIO_MPTABLE,
1390 .ioapic_probe = mptable_ioapic_probe,
1391 .ioapic_enumerate = mptable_ioapic_enumerate
1395 mptable_ioapic_enum_register(void)
1397 ioapic_enumerator_register(&mptable_ioapic_enumerator);
1399 SYSINIT(mptable_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1400 mptable_ioapic_enum_register, 0);
1403 mptable_pci_int_dump(void)
1405 const struct mptable_pci_int *pci_int;
1410 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1411 kprintf("MPTABLE: %d:%d INT%c -> IOAPIC %d.%d\n",
1414 pci_int->mpci_pin + 'A',
1415 pci_int->mpci_ioapic_idx,
1416 pci_int->mpci_ioapic_pin);
1421 mptable_pci_int_route(int bus, int dev, int pin, int intline)
1423 const struct mptable_pci_int *pci_int;
1427 --pin; /* zero based */
1429 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1430 if (pci_int->mpci_bus == bus &&
1431 pci_int->mpci_dev == dev &&
1432 pci_int->mpci_pin == pin)
1435 if (pci_int != NULL) {
1438 gsi = ioapic_gsi(pci_int->mpci_ioapic_idx,
1439 pci_int->mpci_ioapic_pin);
1441 irq = ioapic_find_legacy_by_gsi(gsi,
1442 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1448 kprintf("MPTABLE: fixed interrupt routing "
1449 "for %d:%d INT%c\n", bus, dev, pin + 'A');
1452 irq = ioapic_find_legacy_by_irq(intline,
1453 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1456 if (irq >= 0 && bootverbose) {
1457 kprintf("MPTABLE: %d:%d INT%c routed to irq %d\n",
1458 bus, dev, pin + 'A', irq);