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/apic/apicreg.h>
55 #include <machine/atomic.h>
56 #include <machine/cpufunc.h>
57 #include <machine_base/apic/lapic.h>
58 #include <machine_base/apic/ioapic.h>
59 #include <machine/psl.h>
60 #include <machine/segments.h>
61 #include <machine/tss.h>
62 #include <machine/specialreg.h>
63 #include <machine/globaldata.h>
64 #include <machine/pmap_inval.h>
66 #include <machine/md_var.h> /* setidt() */
67 #include <machine_base/icu/icu.h> /* IPIs */
68 #include <machine_base/apic/ioapic_abi.h>
69 #include <machine/intr_machdep.h> /* IPIs */
71 extern u_int base_memory;
72 extern u_long ebda_addr;
74 #define BIOS_BASE (0xf0000)
75 #define BIOS_BASE2 (0xe0000)
76 #define BIOS_SIZE (0x10000)
77 #define BIOS_COUNT (BIOS_SIZE/4)
79 #define PROCENTRY_FLAG_EN 0x01
80 #define PROCENTRY_FLAG_BP 0x02
81 #define IOAPICENTRY_FLAG_EN 0x01
84 /* MP Floating Pointer Structure */
85 typedef struct MPFPS {
98 /* MP Configuration Table Header */
99 typedef struct MPCTH {
101 u_short base_table_length;
105 u_char product_id[12];
106 u_int32_t oem_table_pointer;
107 u_short oem_table_size;
109 u_int32_t apic_address;
110 u_short extended_table_length;
111 u_char extended_table_checksum;
116 typedef struct PROCENTRY {
121 u_int32_t cpu_signature;
122 u_int32_t feature_flags;
127 typedef struct BUSENTRY {
133 typedef struct IOAPICENTRY {
138 u_int32_t apic_address;
139 } *io_apic_entry_ptr;
141 typedef struct INTENTRY {
151 /* descriptions of MP basetable entries */
152 typedef struct BASETABLE_ENTRY {
161 vm_size_t mp_cth_mapsz;
164 #define MPTABLE_POS_USE_DEFAULT(mpt) \
165 ((mpt)->mp_fps->mpfb1 != 0 || (mpt)->mp_cth == NULL)
169 int mb_type; /* MPTABLE_BUS_ */
170 TAILQ_ENTRY(mptable_bus) mb_link;
173 #define MPTABLE_BUS_ISA 0
174 #define MPTABLE_BUS_PCI 1
176 struct mptable_bus_info {
177 TAILQ_HEAD(, mptable_bus) mbi_list;
180 struct mptable_pci_int {
187 TAILQ_ENTRY(mptable_pci_int) mpci_link;
190 struct mptable_ioapic {
196 TAILQ_ENTRY(mptable_ioapic) mio_link;
199 typedef int (*mptable_iter_func)(void *, const void *, int);
201 static int mptable_iterate_entries(const mpcth_t,
202 mptable_iter_func, void *);
203 static int mptable_search(void);
204 static long mptable_search_sig(u_int32_t target, int count);
205 static int mptable_hyperthread_fixup(cpumask_t, int);
206 static int mptable_map(struct mptable_pos *);
207 static void mptable_unmap(struct mptable_pos *);
208 static void mptable_bus_info_alloc(const mpcth_t,
209 struct mptable_bus_info *);
210 static void mptable_bus_info_free(struct mptable_bus_info *);
212 static int mptable_lapic_probe(struct lapic_enumerator *);
213 static void mptable_lapic_enumerate(struct lapic_enumerator *);
214 static void mptable_lapic_default(void);
216 static int mptable_ioapic_probe(struct ioapic_enumerator *);
217 static void mptable_ioapic_enumerate(struct ioapic_enumerator *);
219 static basetable_entry basetable_entry_types[] =
221 {0, 20, "Processor"},
228 static vm_paddr_t mptable_fps_phyaddr;
229 static int mptable_use_default;
230 static TAILQ_HEAD(mptable_pci_int_list, mptable_pci_int) mptable_pci_int_list =
231 TAILQ_HEAD_INITIALIZER(mptable_pci_int_list);
232 static TAILQ_HEAD(mptable_ioapic_list, mptable_ioapic) mptable_ioapic_list =
233 TAILQ_HEAD_INITIALIZER(mptable_ioapic_list);
238 struct mptable_pos mpt;
241 KKASSERT(mptable_fps_phyaddr == 0);
243 mptable_fps_phyaddr = mptable_search();
244 if (mptable_fps_phyaddr == 0)
247 error = mptable_map(&mpt);
249 mptable_fps_phyaddr = 0;
253 if (MPTABLE_POS_USE_DEFAULT(&mpt)) {
254 kprintf("MPTABLE: use default configuration\n");
255 mptable_use_default = 1;
257 if (mpt.mp_fps->mpfb2 & 0x80)
262 SYSINIT(mptable_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, mptable_probe, 0);
265 * Look for an Intel MP spec table (ie, SMP capable hardware).
273 /* see if EBDA exists */
274 if (ebda_addr != 0) {
275 /* search first 1K of EBDA */
276 target = (u_int32_t)ebda_addr;
277 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
280 /* last 1K of base memory, effective 'top of base' passed in */
281 target = (u_int32_t)(base_memory - 0x400);
282 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
286 /* search the BIOS */
287 target = (u_int32_t)BIOS_BASE;
288 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
291 /* search the extended BIOS */
292 target = (u_int32_t)BIOS_BASE2;
293 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
301 mptable_iterate_entries(const mpcth_t cth, mptable_iter_func func, void *arg)
303 int count, total_size;
304 const void *position;
306 KKASSERT(cth->base_table_length >= sizeof(struct MPCTH));
307 total_size = cth->base_table_length - sizeof(struct MPCTH);
308 position = (const uint8_t *)cth + sizeof(struct MPCTH);
309 count = cth->entry_count;
314 KKASSERT(total_size >= 0);
315 if (total_size == 0) {
316 kprintf("invalid base MP table, "
317 "entry count and length mismatch\n");
321 type = *(const uint8_t *)position;
323 case 0: /* processor_entry */
324 case 1: /* bus_entry */
325 case 2: /* io_apic_entry */
326 case 3: /* int_entry */
327 case 4: /* int_entry */
330 kprintf("unknown base MP table entry type %d\n", type);
334 if (total_size < basetable_entry_types[type].length) {
335 kprintf("invalid base MP table length, "
336 "does not contain all entries\n");
339 total_size -= basetable_entry_types[type].length;
341 error = func(arg, position, type);
345 position = (const uint8_t *)position +
346 basetable_entry_types[type].length;
352 * look for the MP spec signature
355 /* string defined by the Intel MP Spec as identifying the MP table */
356 #define MP_SIG 0x5f504d5f /* _MP_ */
357 #define NEXT(X) ((X) += 4)
359 mptable_search_sig(u_int32_t target, int count)
365 KKASSERT(target != 0);
367 map_size = count * sizeof(u_int32_t);
368 addr = pmap_mapdev((vm_paddr_t)target, map_size);
371 for (x = 0; x < count; NEXT(x)) {
372 if (addr[x] == MP_SIG) {
373 /* make array index a byte index */
374 ret = target + (x * sizeof(u_int32_t));
379 pmap_unmapdev((vm_offset_t)addr, map_size);
383 static int processor_entry (const struct PROCENTRY *entry, int cpu);
386 * Check if we should perform a hyperthreading "fix-up" to
387 * enumerate any logical CPU's that aren't already listed
390 * XXX: We assume that all of the physical CPUs in the
391 * system have the same number of logical CPUs.
393 * XXX: We assume that APIC ID's are allocated such that
394 * the APIC ID's for a physical processor are aligned
395 * with the number of logical CPU's in the processor.
398 mptable_hyperthread_fixup(cpumask_t id_mask, int cpu_count)
400 int i, id, lcpus_max, logical_cpus;
402 if ((cpu_feature & CPUID_HTT) == 0)
405 lcpus_max = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
409 if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
411 * INSTRUCTION SET REFERENCE, A-M (#253666)
412 * Page 3-181, Table 3-20
413 * "The nearest power-of-2 integer that is not smaller
414 * than EBX[23:16] is the number of unique initial APIC
415 * IDs reserved for addressing different logical
416 * processors in a physical package."
419 if ((1 << i) >= lcpus_max) {
426 KKASSERT(cpu_count != 0);
427 if (cpu_count == lcpus_max) {
428 /* We have nothing to fix */
430 } else if (cpu_count == 1) {
431 /* XXX this may be incorrect */
432 logical_cpus = lcpus_max;
437 * Calculate the distances between two nearest
438 * APIC IDs. If all such distances are same,
439 * then it is the number of missing cpus that
440 * we are going to fill later.
442 dist = cur = prev = -1;
443 for (id = 0; id < MAXCPU; ++id) {
444 if ((id_mask & CPUMASK(id)) == 0)
449 int new_dist = cur - prev;
455 * Make sure that all distances
456 * between two nearest APIC IDs
459 if (dist != new_dist)
467 /* Must be power of 2 */
468 if (dist & (dist - 1))
471 /* Can't exceed CPU package capacity */
472 if (dist > lcpus_max)
473 logical_cpus = lcpus_max;
479 * For each APIC ID of a CPU that is set in the mask,
480 * scan the other candidate APIC ID's for this
481 * physical processor. If any of those ID's are
482 * already in the table, then kill the fixup.
484 for (id = 0; id < MAXCPU; id++) {
485 if ((id_mask & CPUMASK(id)) == 0)
487 /* First, make sure we are on a logical_cpus boundary. */
488 if (id % logical_cpus != 0)
490 for (i = id + 1; i < id + logical_cpus; i++)
491 if ((id_mask & CPUMASK(i)) != 0)
498 mptable_map(struct mptable_pos *mpt)
502 vm_size_t cth_mapsz = 0;
504 KKASSERT(mptable_fps_phyaddr != 0);
506 bzero(mpt, sizeof(*mpt));
508 fps = pmap_mapdev(mptable_fps_phyaddr, sizeof(*fps));
511 * Map configuration table header to get
512 * the base table size
514 cth = pmap_mapdev(fps->pap, sizeof(*cth));
515 cth_mapsz = cth->base_table_length;
516 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
518 if (cth_mapsz < sizeof(*cth)) {
519 kprintf("invalid base MP table length %d\n",
521 pmap_unmapdev((vm_offset_t)fps, sizeof(*fps));
528 cth = pmap_mapdev(fps->pap, cth_mapsz);
533 mpt->mp_cth_mapsz = cth_mapsz;
539 mptable_unmap(struct mptable_pos *mpt)
541 if (mpt->mp_cth != NULL) {
542 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
544 mpt->mp_cth_mapsz = 0;
546 if (mpt->mp_fps != NULL) {
547 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
553 processor_entry(const struct PROCENTRY *entry, int cpu)
557 /* check for usability */
558 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
561 /* check for BSP flag */
562 if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
563 lapic_set_cpuid(0, entry->apic_id);
564 return 0; /* its already been counted */
567 /* add another AP to list, if less than max number of CPUs */
568 else if (cpu < MAXCPU) {
569 lapic_set_cpuid(cpu, entry->apic_id);
577 mptable_bus_info_callback(void *xarg, const void *pos, int type)
579 struct mptable_bus_info *bus_info = xarg;
580 const struct BUSENTRY *ent;
581 struct mptable_bus *bus;
587 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
588 if (bus->mb_id == ent->bus_id) {
589 kprintf("mptable_bus_info_alloc: duplicated bus id "
590 "(%d)\n", bus->mb_id);
596 if (strncmp(ent->bus_type, "PCI", 3) == 0) {
597 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
598 bus->mb_type = MPTABLE_BUS_PCI;
599 } else if (strncmp(ent->bus_type, "ISA", 3) == 0) {
600 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
601 bus->mb_type = MPTABLE_BUS_ISA;
605 bus->mb_id = ent->bus_id;
606 TAILQ_INSERT_TAIL(&bus_info->mbi_list, bus, mb_link);
612 mptable_bus_info_alloc(const mpcth_t cth, struct mptable_bus_info *bus_info)
616 bzero(bus_info, sizeof(*bus_info));
617 TAILQ_INIT(&bus_info->mbi_list);
619 error = mptable_iterate_entries(cth, mptable_bus_info_callback, bus_info);
621 mptable_bus_info_free(bus_info);
625 mptable_bus_info_free(struct mptable_bus_info *bus_info)
627 struct mptable_bus *bus;
629 while ((bus = TAILQ_FIRST(&bus_info->mbi_list)) != NULL) {
630 TAILQ_REMOVE(&bus_info->mbi_list, bus, mb_link);
635 struct mptable_lapic_cbarg1 {
638 u_int ht_apicid_mask;
642 mptable_lapic_pass1_callback(void *xarg, const void *pos, int type)
644 const struct PROCENTRY *ent;
645 struct mptable_lapic_cbarg1 *arg = xarg;
651 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
655 if (ent->apic_id < 32) {
656 arg->ht_apicid_mask |= 1 << ent->apic_id;
657 } else if (arg->ht_fixup) {
658 kprintf("MPTABLE: lapic id > 32, disable HTT fixup\n");
664 struct mptable_lapic_cbarg2 {
671 mptable_lapic_pass2_callback(void *xarg, const void *pos, int type)
673 const struct PROCENTRY *ent;
674 struct mptable_lapic_cbarg2 *arg = xarg;
680 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
681 KKASSERT(!arg->found_bsp);
685 if (processor_entry(ent, arg->cpu))
688 if (arg->logical_cpus) {
689 struct PROCENTRY proc;
693 * Create fake mptable processor entries
694 * and feed them to processor_entry() to
695 * enumerate the logical CPUs.
697 bzero(&proc, sizeof(proc));
699 proc.cpu_flags = PROCENTRY_FLAG_EN;
700 proc.apic_id = ent->apic_id;
702 for (i = 1; i < arg->logical_cpus; i++) {
704 processor_entry(&proc, arg->cpu);
712 mptable_lapic_default(void)
714 int ap_apicid, bsp_apicid;
716 mp_naps = 1; /* exclude BSP */
718 /* Map local apic before the id field is accessed */
719 lapic_map(DEFAULT_APIC_BASE);
721 bsp_apicid = APIC_ID(lapic->id);
722 ap_apicid = (bsp_apicid == 0) ? 1 : 0;
725 lapic_set_cpuid(0, bsp_apicid);
726 /* one and only AP */
727 lapic_set_cpuid(1, ap_apicid);
733 * APIC ID <-> CPU ID mappings
736 mptable_lapic_enumerate(struct lapic_enumerator *e)
738 struct mptable_pos mpt;
739 struct mptable_lapic_cbarg1 arg1;
740 struct mptable_lapic_cbarg2 arg2;
742 int error, logical_cpus = 0;
743 vm_offset_t lapic_addr;
745 if (mptable_use_default) {
746 mptable_lapic_default();
750 error = mptable_map(&mpt);
752 panic("mptable_lapic_enumerate mptable_map failed\n");
753 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
757 /* Save local apic address */
758 lapic_addr = (vm_offset_t)cth->apic_address;
759 KKASSERT(lapic_addr != 0);
762 * Find out how many CPUs do we have
764 bzero(&arg1, sizeof(arg1));
765 arg1.ht_fixup = 1; /* Apply ht fixup by default */
767 error = mptable_iterate_entries(cth,
768 mptable_lapic_pass1_callback, &arg1);
770 panic("mptable_iterate_entries(lapic_pass1) failed\n");
771 KKASSERT(arg1.cpu_count != 0);
773 /* See if we need to fixup HT logical CPUs. */
775 logical_cpus = mptable_hyperthread_fixup(arg1.ht_apicid_mask,
777 if (logical_cpus != 0)
778 arg1.cpu_count *= logical_cpus;
780 mp_naps = arg1.cpu_count - 1; /* subtract the BSP */
783 * Link logical CPU id to local apic id
785 bzero(&arg2, sizeof(arg2));
787 arg2.logical_cpus = logical_cpus;
789 error = mptable_iterate_entries(cth,
790 mptable_lapic_pass2_callback, &arg2);
792 panic("mptable_iterate_entries(lapic_pass2) failed\n");
793 KKASSERT(arg2.found_bsp);
796 lapic_map(lapic_addr);
801 struct mptable_lapic_probe_cbarg {
807 mptable_lapic_probe_callback(void *xarg, const void *pos, int type)
809 const struct PROCENTRY *ent;
810 struct mptable_lapic_probe_cbarg *arg = xarg;
816 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
820 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
821 if (arg->found_bsp) {
822 kprintf("more than one BSP in base MP table\n");
831 mptable_lapic_probe(struct lapic_enumerator *e)
833 struct mptable_pos mpt;
834 struct mptable_lapic_probe_cbarg arg;
838 if (mptable_fps_phyaddr == 0)
841 if (mptable_use_default)
844 error = mptable_map(&mpt);
847 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
852 if (cth->apic_address == 0)
855 bzero(&arg, sizeof(arg));
856 error = mptable_iterate_entries(cth,
857 mptable_lapic_probe_callback, &arg);
859 if (arg.cpu_count == 0) {
860 kprintf("MP table contains no processor entries\n");
862 } else if (!arg.found_bsp) {
863 kprintf("MP table does not contains BSP entry\n");
872 static struct lapic_enumerator mptable_lapic_enumerator = {
873 .lapic_prio = LAPIC_ENUM_PRIO_MPTABLE,
874 .lapic_probe = mptable_lapic_probe,
875 .lapic_enumerate = mptable_lapic_enumerate
879 mptable_lapic_enum_register(void)
881 lapic_enumerator_register(&mptable_lapic_enumerator);
883 SYSINIT(mptable_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
884 mptable_lapic_enum_register, 0);
887 mptable_ioapic_list_callback(void *xarg, const void *pos, int type)
889 const struct IOAPICENTRY *ent;
890 struct mptable_ioapic *nioapic, *ioapic;
896 if ((ent->apic_flags & IOAPICENTRY_FLAG_EN) == 0)
899 if (ent->apic_address == 0) {
900 kprintf("mptable_ioapic_create_list: zero IOAPIC addr\n");
904 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
905 if (ioapic->mio_apic_id == ent->apic_id) {
906 kprintf("mptable_ioapic_create_list: duplicated "
907 "apic id %d\n", ioapic->mio_apic_id);
910 if (ioapic->mio_addr == ent->apic_address) {
911 kprintf("mptable_ioapic_create_list: overlapped "
912 "IOAPIC addr 0x%08x", ioapic->mio_addr);
917 nioapic = kmalloc(sizeof(*nioapic), M_DEVBUF, M_WAITOK | M_ZERO);
918 nioapic->mio_apic_id = ent->apic_id;
919 nioapic->mio_addr = ent->apic_address;
922 * Create IOAPIC list in ascending order of APIC ID
924 TAILQ_FOREACH_REVERSE(ioapic, &mptable_ioapic_list,
925 mptable_ioapic_list, mio_link) {
926 if (nioapic->mio_apic_id > ioapic->mio_apic_id) {
927 TAILQ_INSERT_AFTER(&mptable_ioapic_list,
928 ioapic, nioapic, mio_link);
933 TAILQ_INSERT_HEAD(&mptable_ioapic_list, nioapic, mio_link);
939 mptable_ioapic_create_list(void)
941 struct mptable_ioapic *ioapic;
942 struct mptable_pos mpt;
945 if (mptable_fps_phyaddr == 0)
948 if (mptable_use_default) {
949 ioapic = kmalloc(sizeof(*ioapic), M_DEVBUF, M_WAITOK | M_ZERO);
951 ioapic->mio_apic_id = 0; /* NOTE: any value is ok here */
952 ioapic->mio_addr = 0xfec00000; /* XXX magic number */
954 TAILQ_INSERT_HEAD(&mptable_ioapic_list, ioapic, mio_link);
958 error = mptable_map(&mpt);
960 panic("mptable_ioapic_create_list: mptable_map failed\n");
961 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
963 error = mptable_iterate_entries(mpt.mp_cth,
964 mptable_ioapic_list_callback, NULL);
966 while ((ioapic = TAILQ_FIRST(&mptable_ioapic_list)) != NULL) {
967 TAILQ_REMOVE(&mptable_ioapic_list, ioapic, mio_link);
968 kfree(ioapic, M_DEVBUF);
974 * Assign index number for each IOAPIC
977 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
978 ioapic->mio_idx = idx;
984 SYSINIT(mptable_ioapic_list, SI_BOOT2_PRESMP, SI_ORDER_SECOND,
985 mptable_ioapic_create_list, 0);
988 mptable_pci_int_callback(void *xarg, const void *pos, int type)
990 const struct mptable_bus_info *bus_info = xarg;
991 const struct mptable_ioapic *ioapic;
992 const struct mptable_bus *bus;
993 struct mptable_pci_int *pci_int;
994 const struct INTENTRY *ent;
995 int pci_pin, pci_dev;
1001 if (ent->int_type != 0)
1004 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
1005 if (bus->mb_type == MPTABLE_BUS_PCI &&
1006 bus->mb_id == ent->src_bus_id)
1012 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1013 if (ioapic->mio_apic_id == ent->dst_apic_id)
1016 if (ioapic == NULL) {
1017 kprintf("MPTABLE: warning PCI int dst apic id %d "
1018 "does not exist\n", ent->dst_apic_id);
1022 pci_pin = ent->src_bus_irq & 0x3;
1023 pci_dev = (ent->src_bus_irq >> 2) & 0x1f;
1025 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1026 if (pci_int->mpci_bus == ent->src_bus_id &&
1027 pci_int->mpci_dev == pci_dev &&
1028 pci_int->mpci_pin == pci_pin) {
1029 if (pci_int->mpci_ioapic_idx == ioapic->mio_idx &&
1030 pci_int->mpci_ioapic_pin == ent->dst_apic_int) {
1031 kprintf("MPTABLE: warning duplicated "
1032 "PCI int entry for "
1033 "bus %d, dev %d, pin %d\n",
1039 kprintf("mptable_pci_int_register: "
1040 "conflict PCI int entry for "
1041 "bus %d, dev %d, pin %d, "
1042 "IOAPIC %d.%d -> %d.%d\n",
1046 pci_int->mpci_ioapic_idx,
1047 pci_int->mpci_ioapic_pin,
1055 pci_int = kmalloc(sizeof(*pci_int), M_DEVBUF, M_WAITOK | M_ZERO);
1057 pci_int->mpci_bus = ent->src_bus_id;
1058 pci_int->mpci_dev = pci_dev;
1059 pci_int->mpci_pin = pci_pin;
1060 pci_int->mpci_ioapic_idx = ioapic->mio_idx;
1061 pci_int->mpci_ioapic_pin = ent->dst_apic_int;
1063 TAILQ_INSERT_TAIL(&mptable_pci_int_list, pci_int, mpci_link);
1069 mptable_pci_int_register(void)
1071 struct mptable_bus_info bus_info;
1072 const struct mptable_bus *bus;
1073 struct mptable_pci_int *pci_int;
1074 struct mptable_pos mpt;
1075 int error, force_pci0, npcibus;
1078 if (mptable_fps_phyaddr == 0)
1081 if (mptable_use_default)
1084 if (TAILQ_EMPTY(&mptable_ioapic_list))
1087 error = mptable_map(&mpt);
1089 panic("mptable_pci_int_register: mptable_map failed\n");
1090 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1094 mptable_bus_info_alloc(cth, &bus_info);
1095 if (TAILQ_EMPTY(&bus_info.mbi_list))
1100 TAILQ_FOREACH(bus, &bus_info.mbi_list, mb_link) {
1101 if (bus->mb_type == MPTABLE_BUS_PCI)
1105 mptable_bus_info_free(&bus_info);
1107 } else if (npcibus == 1) {
1111 error = mptable_iterate_entries(cth,
1112 mptable_pci_int_callback, &bus_info);
1114 mptable_bus_info_free(&bus_info);
1117 while ((pci_int = TAILQ_FIRST(&mptable_pci_int_list)) != NULL) {
1118 TAILQ_REMOVE(&mptable_pci_int_list, pci_int, mpci_link);
1119 kfree(pci_int, M_DEVBUF);
1125 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link)
1126 pci_int->mpci_bus = 0;
1129 mptable_unmap(&mpt);
1131 SYSINIT(mptable_pci, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1132 mptable_pci_int_register, 0);
1134 struct mptable_ioapic_probe_cbarg {
1135 const struct mptable_bus_info *bus_info;
1139 mptable_ioapic_probe_callback(void *xarg, const void *pos, int type)
1141 struct mptable_ioapic_probe_cbarg *arg = xarg;
1142 const struct mptable_ioapic *ioapic;
1143 const struct mptable_bus *bus;
1144 const struct INTENTRY *ent;
1150 if (ent->int_type != 0)
1153 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1154 if (bus->mb_type == MPTABLE_BUS_ISA &&
1155 bus->mb_id == ent->src_bus_id)
1161 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1162 if (ioapic->mio_apic_id == ent->dst_apic_id)
1165 if (ioapic == NULL) {
1166 kprintf("MPTABLE: warning ISA int dst apic id %d "
1167 "does not exist\n", ent->dst_apic_id);
1171 /* XXX magic number */
1172 if (ent->src_bus_irq >= 16) {
1173 kprintf("mptable_ioapic_probe: invalid ISA irq (%d)\n",
1181 mptable_ioapic_probe(struct ioapic_enumerator *e)
1183 struct mptable_ioapic_probe_cbarg arg;
1184 struct mptable_bus_info bus_info;
1185 struct mptable_pos mpt;
1189 if (mptable_fps_phyaddr == 0)
1192 if (mptable_use_default)
1195 if (TAILQ_EMPTY(&mptable_ioapic_list))
1198 error = mptable_map(&mpt);
1200 panic("mptable_ioapic_probe: mptable_map failed\n");
1201 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1205 mptable_bus_info_alloc(cth, &bus_info);
1207 bzero(&arg, sizeof(arg));
1208 arg.bus_info = &bus_info;
1210 error = mptable_iterate_entries(cth,
1211 mptable_ioapic_probe_callback, &arg);
1213 mptable_bus_info_free(&bus_info);
1214 mptable_unmap(&mpt);
1219 struct mptable_ioapic_int_cbarg {
1220 const struct mptable_bus_info *bus_info;
1225 mptable_ioapic_int_callback(void *xarg, const void *pos, int type)
1227 struct mptable_ioapic_int_cbarg *arg = xarg;
1228 const struct mptable_ioapic *ioapic;
1229 const struct mptable_bus *bus;
1230 const struct INTENTRY *ent;
1239 if (ent->int_type != 0)
1242 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
1243 if (bus->mb_type == MPTABLE_BUS_ISA &&
1244 bus->mb_id == ent->src_bus_id)
1250 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1251 if (ioapic->mio_apic_id == ent->dst_apic_id)
1254 if (ioapic == NULL) {
1255 kprintf("MPTABLE: warning ISA int dst apic id %d "
1256 "does not exist\n", ent->dst_apic_id);
1260 if (ent->dst_apic_int >= ioapic->mio_npin) {
1261 panic("mptable_ioapic_enumerate: invalid I/O APIC "
1262 "pin %d, should be < %d",
1263 ent->dst_apic_int, ioapic->mio_npin);
1265 gsi = ioapic->mio_gsi_base + ent->dst_apic_int;
1267 if (ent->src_bus_irq != gsi) {
1269 kprintf("MPTABLE: INTSRC irq %d -> GSI %d\n",
1270 ent->src_bus_irq, gsi);
1272 ioapic_intsrc(ent->src_bus_irq, gsi,
1273 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1279 mptable_ioapic_enumerate(struct ioapic_enumerator *e)
1281 struct mptable_bus_info bus_info;
1282 struct mptable_ioapic *ioapic;
1283 struct mptable_pos mpt;
1287 KKASSERT(mptable_fps_phyaddr != 0);
1288 KKASSERT(!TAILQ_EMPTY(&mptable_ioapic_list));
1290 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
1291 const struct mptable_ioapic *prev_ioapic;
1295 addr = ioapic_map(ioapic->mio_addr);
1297 ver = ioapic_read(addr, IOAPIC_VER);
1298 ioapic->mio_npin = ((ver & IOART_VER_MAXREDIR)
1299 >> MAXREDIRSHIFT) + 1;
1301 prev_ioapic = TAILQ_PREV(ioapic,
1302 mptable_ioapic_list, mio_link);
1303 if (prev_ioapic == NULL) {
1304 ioapic->mio_gsi_base = 0;
1306 ioapic->mio_gsi_base =
1307 prev_ioapic->mio_gsi_base +
1308 prev_ioapic->mio_npin;
1310 ioapic_add(addr, ioapic->mio_gsi_base, ioapic->mio_npin);
1313 kprintf("MPTABLE: IOAPIC addr 0x%08x, "
1314 "apic id %d, idx %d, gsi base %d, npin %d\n",
1316 ioapic->mio_apic_id,
1318 ioapic->mio_gsi_base,
1323 if (mptable_use_default) {
1325 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (default)\n");
1326 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1330 error = mptable_map(&mpt);
1332 panic("mptable_ioapic_probe: mptable_map failed\n");
1333 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1337 mptable_bus_info_alloc(cth, &bus_info);
1339 if (TAILQ_EMPTY(&bus_info.mbi_list)) {
1341 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (no bus)\n");
1342 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
1344 struct mptable_ioapic_int_cbarg arg;
1346 bzero(&arg, sizeof(arg));
1347 arg.bus_info = &bus_info;
1349 error = mptable_iterate_entries(cth,
1350 mptable_ioapic_int_callback, &arg);
1352 panic("mptable_ioapic_int failed\n");
1354 if (arg.ioapic_nint == 0) {
1356 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 "
1359 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE,
1360 INTR_POLARITY_HIGH);
1364 mptable_bus_info_free(&bus_info);
1366 mptable_unmap(&mpt);
1369 static struct ioapic_enumerator mptable_ioapic_enumerator = {
1370 .ioapic_prio = IOAPIC_ENUM_PRIO_MPTABLE,
1371 .ioapic_probe = mptable_ioapic_probe,
1372 .ioapic_enumerate = mptable_ioapic_enumerate
1376 mptable_ioapic_enum_register(void)
1378 ioapic_enumerator_register(&mptable_ioapic_enumerator);
1380 SYSINIT(mptable_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
1381 mptable_ioapic_enum_register, 0);
1384 mptable_pci_int_dump(void)
1386 const struct mptable_pci_int *pci_int;
1388 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1389 kprintf("MPTABLE: %d:%d INT%c -> IOAPIC %d.%d\n",
1392 pci_int->mpci_pin + 'A',
1393 pci_int->mpci_ioapic_idx,
1394 pci_int->mpci_ioapic_pin);
1399 mptable_pci_int_route(int bus, int dev, int pin, int intline)
1401 const struct mptable_pci_int *pci_int;
1405 --pin; /* zero based */
1407 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
1408 if (pci_int->mpci_bus == bus &&
1409 pci_int->mpci_dev == dev &&
1410 pci_int->mpci_pin == pin)
1413 if (pci_int != NULL) {
1416 gsi = ioapic_gsi(pci_int->mpci_ioapic_idx,
1417 pci_int->mpci_ioapic_pin);
1419 irq = ioapic_abi_find_gsi(gsi,
1420 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1426 kprintf("MPTABLE: fixed interrupt routing "
1427 "for %d:%d INT%c\n", bus, dev, pin + 'A');
1430 irq = ioapic_abi_find_irq(intline,
1431 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
1434 if (irq >= 0 && bootverbose) {
1435 kprintf("MPTABLE: %d:%d INT%c routed to irq %d\n",
1436 bus, dev, pin + 'A', irq);