2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996, by Steve Passe
6 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the developer may NOT be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_mptable_force_htt.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
45 #include <vm/vm_param.h>
48 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcib_private.h>
52 #include <x86/apicreg.h>
53 #include <x86/mptable.h>
54 #include <machine/frame.h>
55 #include <machine/intr_machdep.h>
56 #include <x86/apicvar.h>
57 #include <machine/md_var.h>
58 #include <machine/pc/bios.h>
60 #include <machine/resource.h>
62 #include <machine/specialreg.h>
64 /* string defined by the Intel MP Spec as identifying the MP table */
65 #define MP_SIG 0x5f504d5f /* _MP_ */
68 #define MAX_LAPIC_ID 63 /* Max local APIC ID for HTT fixup */
70 #define MAX_LAPIC_ID 31 /* Max local APIC ID for HTT fixup */
73 #define BIOS_BASE (0xf0000)
74 #define BIOS_SIZE (0x10000)
75 #define BIOS_COUNT (BIOS_SIZE/4)
77 typedef void mptable_entry_handler(u_char *entry, void *arg);
78 typedef void mptable_extended_entry_handler(ext_entry_ptr entry, void *arg);
80 /* descriptions of MP table entries */
81 typedef struct BASETABLE_ENTRY {
87 static basetable_entry basetable_entry_types[] =
96 typedef struct BUSDATA {
98 enum busTypes bus_type;
101 typedef struct INTDATA {
111 typedef struct BUSTYPENAME {
116 /* From MP spec v1.4, table 4-8. */
117 static bus_type_name bus_type_table[] =
119 {UNKNOWN_BUSTYPE, "CBUS "},
120 {UNKNOWN_BUSTYPE, "CBUSII"},
122 {UNKNOWN_BUSTYPE, "FUTURE"},
123 {UNKNOWN_BUSTYPE, "INTERN"},
125 {UNKNOWN_BUSTYPE, "MBI "},
126 {UNKNOWN_BUSTYPE, "MBII "},
128 {UNKNOWN_BUSTYPE, "MPI "},
129 {UNKNOWN_BUSTYPE, "MPSA "},
130 {UNKNOWN_BUSTYPE, "NUBUS "},
132 {UNKNOWN_BUSTYPE, "PCMCIA"},
133 {UNKNOWN_BUSTYPE, "TC "},
134 {UNKNOWN_BUSTYPE, "VL "},
135 {UNKNOWN_BUSTYPE, "VME "},
136 {UNKNOWN_BUSTYPE, "XPRESS"}
139 /* From MP spec v1.4, table 5-1. */
140 static int default_data[7][5] =
142 /* nbus, id0, type0, id1, type1 */
143 {1, 0, ISA, 255, NOBUS},
144 {1, 0, EISA, 255, NOBUS},
145 {1, 0, EISA, 255, NOBUS},
146 {1, 0, MCA, 255, NOBUS},
148 {2, 0, EISA, 1, PCI},
152 struct pci_probe_table_args {
157 struct pci_route_interrupt_args {
158 u_char bus; /* Source bus. */
159 u_char irq; /* Source slot:pin. */
160 int vector; /* Return value. */
163 static mpfps_t mpfps;
165 static ext_entry_ptr mpet;
166 static void *ioapics[IOAPIC_MAX_ID + 1];
167 static bus_datum *busses;
168 static int mptable_nioapics, mptable_nbusses, mptable_maxbusid;
169 static int pci0 = -1;
171 static MALLOC_DEFINE(M_MPTABLE, "mptable", "MP Table Items");
173 static enum intr_polarity conforming_polarity(u_char src_bus,
175 static enum intr_trigger conforming_trigger(u_char src_bus, u_char src_bus_irq);
176 static enum intr_polarity intentry_polarity(int_entry_ptr intr);
177 static enum intr_trigger intentry_trigger(int_entry_ptr intr);
178 static int lookup_bus_type(char *name);
179 static void mptable_count_items(void);
180 static void mptable_count_items_handler(u_char *entry, void *arg);
181 #ifdef MPTABLE_FORCE_HTT
182 static void mptable_hyperthread_fixup(u_int id_mask);
184 static void mptable_parse_apics_and_busses(void);
185 static void mptable_parse_apics_and_busses_handler(u_char *entry,
187 static void mptable_parse_default_config_ints(void);
188 static void mptable_parse_ints(void);
189 static void mptable_parse_ints_handler(u_char *entry, void *arg);
190 static void mptable_parse_io_int(int_entry_ptr intr);
191 static void mptable_parse_local_int(int_entry_ptr intr);
192 static void mptable_pci_probe_table_handler(u_char *entry, void *arg);
193 static void mptable_pci_route_interrupt_handler(u_char *entry, void *arg);
194 static void mptable_pci_setup(void);
195 static int mptable_probe(void);
196 static int mptable_probe_cpus(void);
197 static void mptable_probe_cpus_handler(u_char *entry, void *arg __unused);
198 static void mptable_setup_cpus_handler(u_char *entry, void *arg __unused);
199 static void mptable_register(void *dummy);
200 static int mptable_setup_local(void);
201 static int mptable_setup_io(void);
203 static void mptable_walk_extended_table(
204 mptable_extended_entry_handler *handler, void *arg);
206 static void mptable_walk_table(mptable_entry_handler *handler, void *arg);
207 static int search_for_sig(u_int32_t target, int count);
209 static struct apic_enumerator mptable_enumerator = {
210 .apic_name = "MPTable",
211 .apic_probe = mptable_probe,
212 .apic_probe_cpus = mptable_probe_cpus,
213 .apic_setup_local = mptable_setup_local,
214 .apic_setup_io = mptable_setup_io
218 * look for the MP spec signature
222 search_for_sig(u_int32_t target, int count)
227 addr = (u_int32_t *)BIOS_PADDRTOVADDR(target);
228 for (x = 0; x < count; x += 4)
229 if (addr[x] == MP_SIG)
230 /* make array index a byte index */
231 return (target + (x * sizeof(u_int32_t)));
236 lookup_bus_type(char *name)
240 for (x = 0; x < MAX_BUSTYPE; ++x)
241 if (strncmp(bus_type_table[x].name, name, 6) == 0)
242 return (bus_type_table[x].type);
244 return (UNKNOWN_BUSTYPE);
248 * Look for an Intel MP spec table (ie, SMP capable hardware).
257 /* see if EBDA exists */
258 if ((segment = *(u_short *)BIOS_PADDRTOVADDR(0x40e)) != 0) {
259 /* search first 1K of EBDA */
260 target = (u_int32_t) (segment << 4);
261 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
264 /* last 1K of base memory, effective 'top of base' passed in */
265 target = (u_int32_t) ((basemem * 1024) - 0x400);
266 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
270 /* search the BIOS */
271 target = (u_int32_t) BIOS_BASE;
272 if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
279 mpfps = (mpfps_t)BIOS_PADDRTOVADDR(x);
281 /* Map in the configuration table if it exists. */
282 if (mpfps->config_type != 0) {
285 "MP Table version 1.%d found using Default Configuration %d\n",
286 mpfps->spec_rev, mpfps->config_type);
287 if (mpfps->config_type != 5 && mpfps->config_type != 6) {
289 "MP Table Default Configuration %d is unsupported\n",
295 if ((uintptr_t)mpfps->pap >= 1024 * 1024) {
296 printf("%s: Unable to map MP Configuration Table\n",
300 mpct = (mpcth_t)BIOS_PADDRTOVADDR((uintptr_t)mpfps->pap);
301 if (mpct->base_table_length + (uintptr_t)mpfps->pap >=
303 printf("%s: Unable to map end of MP Config Table\n",
307 if (mpct->extended_table_length != 0 &&
308 mpct->extended_table_length + mpct->base_table_length +
309 (uintptr_t)mpfps->pap < 1024 * 1024)
310 mpet = (ext_entry_ptr)((char *)mpct +
311 mpct->base_table_length);
312 if (mpct->signature[0] != 'P' || mpct->signature[1] != 'C' ||
313 mpct->signature[2] != 'M' || mpct->signature[3] != 'P') {
314 printf("%s: MP Config Table has bad signature: %c%c%c%c\n",
315 __func__, mpct->signature[0], mpct->signature[1],
316 mpct->signature[2], mpct->signature[3]);
321 "MP Configuration Table version 1.%d found at %p\n",
322 mpct->spec_rev, mpct);
329 * Run through the MP table enumerating CPUs.
332 mptable_probe_cpus(void)
336 /* Is this a pre-defined config? */
337 if (mpfps->config_type != 0) {
344 mptable_walk_table(mptable_probe_cpus_handler, &cpu_mask);
350 * Initialize the local APIC on the BSP.
353 mptable_setup_local(void)
358 /* Is this a pre-defined config? */
359 printf("MPTable: <");
360 if (mpfps->config_type != 0) {
363 addr = DEFAULT_APIC_BASE;
364 printf("Default Configuration %d", mpfps->config_type);
368 mptable_walk_table(mptable_setup_cpus_handler, &cpu_mask);
369 #ifdef MPTABLE_FORCE_HTT
370 mptable_hyperthread_fixup(cpu_mask);
372 addr = mpct->apic_address;
373 printf("%.*s %.*s", (int)sizeof(mpct->oem_id), mpct->oem_id,
374 (int)sizeof(mpct->product_id), mpct->product_id);
382 * Run through the MP table enumerating I/O APICs.
385 mptable_setup_io(void)
390 /* First, we count individual items and allocate arrays. */
391 mptable_count_items();
392 busses = malloc((mptable_maxbusid + 1) * sizeof(bus_datum), M_MPTABLE,
394 for (i = 0; i <= mptable_maxbusid; i++)
395 busses[i].bus_type = NOBUS;
397 /* Second, we run through adding I/O APIC's and buses. */
398 mptable_parse_apics_and_busses();
400 /* Third, we run through the table tweaking interrupt sources. */
401 mptable_parse_ints();
403 /* Fourth, we register all the I/O APIC's. */
404 for (i = 0; i <= IOAPIC_MAX_ID; i++)
405 if (ioapics[i] != NULL)
406 ioapic_register(ioapics[i]);
408 /* Fifth, we setup data structures to handle PCI interrupt routing. */
411 /* Finally, we throw the switch to enable the I/O APIC's. */
412 if (mpfps->mpfb2 & MPFB2_IMCR_PRESENT) {
413 outb(0x22, 0x70); /* select IMCR */
414 byte = inb(0x23); /* current contents */
415 byte |= 0x01; /* mask external INTR */
416 outb(0x23, byte); /* disconnect 8259s/NMI */
423 mptable_register(void *dummy __unused)
426 apic_register_enumerator(&mptable_enumerator);
428 SYSINIT(mptable_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, mptable_register,
432 * Call the handler routine for each entry in the MP config base table.
435 mptable_walk_table(mptable_entry_handler *handler, void *arg)
440 entry = (u_char *)(mpct + 1);
441 for (i = 0; i < mpct->entry_count; i++) {
443 case MPCT_ENTRY_PROCESSOR:
444 case MPCT_ENTRY_IOAPIC:
447 case MPCT_ENTRY_LOCAL_INT:
450 panic("%s: Unknown MP Config Entry %d\n", __func__,
454 entry += basetable_entry_types[*entry].length;
460 * Call the handler routine for each entry in the MP config extended
464 mptable_walk_extended_table(mptable_extended_entry_handler *handler, void *arg)
466 ext_entry_ptr end, entry;
471 end = (ext_entry_ptr)((char *)mpet + mpct->extended_table_length);
472 while (entry < end) {
474 entry = (ext_entry_ptr)((char *)entry + entry->length);
480 mptable_probe_cpus_handler(u_char *entry, void *arg)
485 case MPCT_ENTRY_PROCESSOR:
486 proc = (proc_entry_ptr)entry;
487 if (proc->cpu_flags & PROCENTRY_FLAG_EN &&
488 proc->apic_id < MAX_LAPIC_ID && mp_ncpus < MAXCPU) {
491 mp_maxid = mp_ncpus - 1;
493 max_apic_id = max(max_apic_id, proc->apic_id);
500 mptable_setup_cpus_handler(u_char *entry, void *arg)
506 case MPCT_ENTRY_PROCESSOR:
507 proc = (proc_entry_ptr)entry;
508 if (proc->cpu_flags & PROCENTRY_FLAG_EN) {
509 lapic_create(proc->apic_id, proc->cpu_flags &
511 if (proc->apic_id < MAX_LAPIC_ID) {
512 cpu_mask = (u_int *)arg;
513 *cpu_mask |= (1ul << proc->apic_id);
521 mptable_count_items_handler(u_char *entry, void *arg __unused)
523 io_apic_entry_ptr apic;
528 bus = (bus_entry_ptr)entry;
530 if (bus->bus_id > mptable_maxbusid)
531 mptable_maxbusid = bus->bus_id;
533 case MPCT_ENTRY_IOAPIC:
534 apic = (io_apic_entry_ptr)entry;
535 if (apic->apic_flags & IOAPICENTRY_FLAG_EN)
542 * Count items in the table.
545 mptable_count_items(void)
548 /* Is this a pre-defined config? */
549 if (mpfps->config_type != 0) {
550 mptable_nioapics = 1;
551 switch (mpfps->config_type) {
564 panic("Unknown pre-defined MP Table config type %d",
567 mptable_maxbusid = mptable_nbusses - 1;
569 mptable_walk_table(mptable_count_items_handler, NULL);
573 * Add a bus or I/O APIC from an entry in the table.
576 mptable_parse_apics_and_busses_handler(u_char *entry, void *arg __unused)
578 io_apic_entry_ptr apic;
580 enum busTypes bus_type;
585 bus = (bus_entry_ptr)entry;
586 bus_type = lookup_bus_type(bus->bus_type);
587 if (bus_type == UNKNOWN_BUSTYPE) {
588 printf("MPTable: Unknown bus %d type \"", bus->bus_id);
589 for (i = 0; i < 6; i++)
590 printf("%c", bus->bus_type[i]);
593 busses[bus->bus_id].bus_id = bus->bus_id;
594 busses[bus->bus_id].bus_type = bus_type;
596 case MPCT_ENTRY_IOAPIC:
597 apic = (io_apic_entry_ptr)entry;
598 if (!(apic->apic_flags & IOAPICENTRY_FLAG_EN))
600 if (apic->apic_id > IOAPIC_MAX_ID)
601 panic("%s: I/O APIC ID %d too high", __func__,
603 if (ioapics[apic->apic_id] != NULL)
604 panic("%s: Double APIC ID %d", __func__,
606 ioapics[apic->apic_id] = ioapic_create(apic->apic_address,
615 * Enumerate I/O APIC's and buses.
618 mptable_parse_apics_and_busses(void)
621 /* Is this a pre-defined config? */
622 if (mpfps->config_type != 0) {
623 ioapics[2] = ioapic_create(DEFAULT_IO_APIC_BASE, 2, 0);
624 busses[0].bus_id = 0;
625 busses[0].bus_type = default_data[mpfps->config_type - 1][2];
626 if (mptable_nbusses > 1) {
627 busses[1].bus_id = 1;
629 default_data[mpfps->config_type - 1][4];
632 mptable_walk_table(mptable_parse_apics_and_busses_handler,
637 * Determine conforming polarity for a given bus type.
639 static enum intr_polarity
640 conforming_polarity(u_char src_bus, u_char src_bus_irq)
643 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
644 switch (busses[src_bus].bus_type) {
647 return (INTR_POLARITY_HIGH);
649 return (INTR_POLARITY_LOW);
651 panic("%s: unknown bus type %d", __func__,
652 busses[src_bus].bus_type);
657 * Determine conforming trigger for a given bus type.
659 static enum intr_trigger
660 conforming_trigger(u_char src_bus, u_char src_bus_irq)
663 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
664 switch (busses[src_bus].bus_type) {
667 return (elcr_read_trigger(src_bus_irq));
669 return (INTR_TRIGGER_EDGE);
671 return (INTR_TRIGGER_LEVEL);
674 KASSERT(src_bus_irq < 16, ("Invalid EISA IRQ %d", src_bus_irq));
675 KASSERT(elcr_found, ("Missing ELCR"));
676 return (elcr_read_trigger(src_bus_irq));
679 panic("%s: unknown bus type %d", __func__,
680 busses[src_bus].bus_type);
684 static enum intr_polarity
685 intentry_polarity(int_entry_ptr intr)
688 switch (intr->int_flags & INTENTRY_FLAGS_POLARITY) {
689 case INTENTRY_FLAGS_POLARITY_CONFORM:
690 return (conforming_polarity(intr->src_bus_id,
692 case INTENTRY_FLAGS_POLARITY_ACTIVEHI:
693 return (INTR_POLARITY_HIGH);
694 case INTENTRY_FLAGS_POLARITY_ACTIVELO:
695 return (INTR_POLARITY_LOW);
697 panic("Bogus interrupt flags");
701 static enum intr_trigger
702 intentry_trigger(int_entry_ptr intr)
705 switch (intr->int_flags & INTENTRY_FLAGS_TRIGGER) {
706 case INTENTRY_FLAGS_TRIGGER_CONFORM:
707 return (conforming_trigger(intr->src_bus_id,
709 case INTENTRY_FLAGS_TRIGGER_EDGE:
710 return (INTR_TRIGGER_EDGE);
711 case INTENTRY_FLAGS_TRIGGER_LEVEL:
712 return (INTR_TRIGGER_LEVEL);
714 panic("Bogus interrupt flags");
719 * Parse an interrupt entry for an I/O interrupt routed to a pin on an I/O APIC.
722 mptable_parse_io_int(int_entry_ptr intr)
727 apic_id = intr->dst_apic_id;
728 if (intr->dst_apic_id == 0xff) {
730 * An APIC ID of 0xff means that the interrupt is connected
731 * to the specified pin on all I/O APICs in the system. If
732 * there is only one I/O APIC, then use that APIC to route
733 * the interrupts. If there is more than one I/O APIC, then
736 if (mptable_nioapics == 1) {
738 while (ioapics[apic_id] == NULL)
742 "MPTable: Ignoring global interrupt entry for pin %d\n",
747 if (apic_id > IOAPIC_MAX_ID) {
748 printf("MPTable: Ignoring interrupt entry for ioapic%d\n",
752 ioapic = ioapics[apic_id];
753 if (ioapic == NULL) {
755 "MPTable: Ignoring interrupt entry for missing ioapic%d\n",
759 pin = intr->dst_apic_int;
760 switch (intr->int_type) {
761 case INTENTRY_TYPE_INT:
762 switch (busses[intr->src_bus_id].bus_type) {
764 panic("interrupt from missing bus");
767 if (busses[intr->src_bus_id].bus_type == ISA)
768 ioapic_set_bus(ioapic, pin, APIC_BUS_ISA);
770 ioapic_set_bus(ioapic, pin, APIC_BUS_EISA);
771 if (intr->src_bus_irq == pin)
773 ioapic_remap_vector(ioapic, pin, intr->src_bus_irq);
774 if (ioapic_get_vector(ioapic, intr->src_bus_irq) ==
776 ioapic_disable_pin(ioapic, intr->src_bus_irq);
779 ioapic_set_bus(ioapic, pin, APIC_BUS_PCI);
782 ioapic_set_bus(ioapic, pin, APIC_BUS_UNKNOWN);
786 case INTENTRY_TYPE_NMI:
787 ioapic_set_nmi(ioapic, pin);
789 case INTENTRY_TYPE_SMI:
790 ioapic_set_smi(ioapic, pin);
792 case INTENTRY_TYPE_EXTINT:
793 ioapic_set_extint(ioapic, pin);
796 panic("%s: invalid interrupt entry type %d\n", __func__,
799 if (intr->int_type == INTENTRY_TYPE_INT ||
800 (intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
801 INTENTRY_FLAGS_TRIGGER_CONFORM)
802 ioapic_set_triggermode(ioapic, pin, intentry_trigger(intr));
803 if (intr->int_type == INTENTRY_TYPE_INT ||
804 (intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
805 INTENTRY_FLAGS_POLARITY_CONFORM)
806 ioapic_set_polarity(ioapic, pin, intentry_polarity(intr));
810 * Parse an interrupt entry for a local APIC LVT pin.
813 mptable_parse_local_int(int_entry_ptr intr)
817 if (intr->dst_apic_id == 0xff)
818 apic_id = APIC_ID_ALL;
820 apic_id = intr->dst_apic_id;
821 if (intr->dst_apic_int == 0)
822 pin = APIC_LVT_LINT0;
824 pin = APIC_LVT_LINT1;
825 switch (intr->int_type) {
826 case INTENTRY_TYPE_INT:
829 "MPTable: Ignoring vectored local interrupt for LINTIN%d vector %d\n",
830 intr->dst_apic_int, intr->src_bus_irq);
833 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_FIXED);
836 case INTENTRY_TYPE_NMI:
837 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
839 case INTENTRY_TYPE_SMI:
840 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_SMI);
842 case INTENTRY_TYPE_EXTINT:
843 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_EXTINT);
846 panic("%s: invalid interrupt entry type %d\n", __func__,
849 if ((intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
850 INTENTRY_FLAGS_TRIGGER_CONFORM)
851 lapic_set_lvt_triggermode(apic_id, pin,
852 intentry_trigger(intr));
853 if ((intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
854 INTENTRY_FLAGS_POLARITY_CONFORM)
855 lapic_set_lvt_polarity(apic_id, pin, intentry_polarity(intr));
859 * Parse interrupt entries.
862 mptable_parse_ints_handler(u_char *entry, void *arg __unused)
866 intr = (int_entry_ptr)entry;
869 mptable_parse_io_int(intr);
871 case MPCT_ENTRY_LOCAL_INT:
872 mptable_parse_local_int(intr);
878 * Configure interrupt pins for a default configuration. For details see
879 * Table 5-2 in Section 5 of the MP Table specification.
882 mptable_parse_default_config_ints(void)
884 struct INTENTRY entry;
888 * All default configs route IRQs from bus 0 to the first 16 pins
889 * of the first I/O APIC with an APIC ID of 2.
891 entry.type = MPCT_ENTRY_INT;
892 entry.int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
893 INTENTRY_FLAGS_TRIGGER_CONFORM;
894 entry.src_bus_id = 0;
895 entry.dst_apic_id = 2;
897 /* Run through all 16 pins. */
898 for (pin = 0; pin < 16; pin++) {
899 entry.dst_apic_int = pin;
902 /* Pin 0 is an ExtINT pin. */
903 entry.int_type = INTENTRY_TYPE_EXTINT;
906 /* IRQ 0 is routed to pin 2. */
907 entry.int_type = INTENTRY_TYPE_INT;
908 entry.src_bus_irq = 0;
911 /* All other pins are identity mapped. */
912 entry.int_type = INTENTRY_TYPE_INT;
913 entry.src_bus_irq = pin;
916 mptable_parse_io_int(&entry);
919 /* Certain configs disable certain pins. */
920 if (mpfps->config_type == 7)
921 ioapic_disable_pin(ioapics[2], 0);
922 if (mpfps->config_type == 2) {
923 ioapic_disable_pin(ioapics[2], 2);
924 ioapic_disable_pin(ioapics[2], 13);
929 * Configure the interrupt pins
932 mptable_parse_ints(void)
935 /* Is this a pre-defined config? */
936 if (mpfps->config_type != 0) {
937 /* Configure LINT pins. */
938 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT0,
940 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT1, APIC_LVT_DM_NMI);
942 /* Configure I/O APIC pins. */
943 mptable_parse_default_config_ints();
945 mptable_walk_table(mptable_parse_ints_handler, NULL);
948 #ifdef MPTABLE_FORCE_HTT
950 * Perform a hyperthreading "fix-up" to enumerate any logical CPU's
951 * that aren't already listed in the table.
953 * XXX: We assume that all of the physical CPUs in the
954 * system have the same number of logical CPUs.
956 * XXX: We assume that APIC ID's are allocated such that
957 * the APIC ID's for a physical processor are aligned
958 * with the number of logical CPU's in the processor.
961 mptable_hyperthread_fixup(u_int id_mask)
963 u_int i, id, logical_cpus;
965 /* Nothing to do if there is no HTT support. */
966 if ((cpu_feature & CPUID_HTT) == 0)
968 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
969 if (logical_cpus <= 1)
973 * For each APIC ID of a CPU that is set in the mask,
974 * scan the other candidate APIC ID's for this
975 * physical processor. If any of those ID's are
976 * already in the table, then kill the fixup.
978 for (id = 0; id <= MAX_LAPIC_ID; id++) {
979 if ((id_mask & 1 << id) == 0)
981 /* First, make sure we are on a logical_cpus boundary. */
982 if (id % logical_cpus != 0)
984 for (i = id + 1; i < id + logical_cpus; i++)
985 if ((id_mask & 1 << i) != 0)
990 * Ok, the ID's checked out, so perform the fixup by
991 * adding the logical CPUs.
993 while ((id = ffs(id_mask)) != 0) {
995 for (i = id + 1; i < id + logical_cpus; i++) {
998 "MPTable: Adding logical CPU %d from main CPU %d\n",
1002 id_mask &= ~(1 << id);
1005 #endif /* MPTABLE_FORCE_HTT */
1008 * Support code for routing PCI interrupts using the MP Table.
1011 mptable_pci_setup(void)
1016 * Find the first pci bus and call it 0. Panic if pci0 is not
1017 * bus zero and there are multiple PCI buses.
1019 for (i = 0; i <= mptable_maxbusid; i++)
1020 if (busses[i].bus_type == PCI) {
1025 "MPTable contains multiple PCI buses but no PCI bus 0");
1030 mptable_pci_probe_table_handler(u_char *entry, void *arg)
1032 struct pci_probe_table_args *args;
1035 if (*entry != MPCT_ENTRY_INT)
1037 intr = (int_entry_ptr)entry;
1038 args = (struct pci_probe_table_args *)arg;
1039 KASSERT(args->bus <= mptable_maxbusid,
1040 ("bus %d is too big", args->bus));
1041 KASSERT(busses[args->bus].bus_type == PCI, ("probing for non-PCI bus"));
1042 if (intr->src_bus_id == args->bus)
1047 mptable_pci_probe_table(int bus)
1049 struct pci_probe_table_args args;
1053 if (mpct == NULL || pci0 == -1 || pci0 + bus > mptable_maxbusid)
1055 if (busses[pci0 + bus].bus_type != PCI)
1057 args.bus = pci0 + bus;
1059 mptable_walk_table(mptable_pci_probe_table_handler, &args);
1060 if (args.found == 0)
1066 mptable_pci_route_interrupt_handler(u_char *entry, void *arg)
1068 struct pci_route_interrupt_args *args;
1072 if (*entry != MPCT_ENTRY_INT)
1074 intr = (int_entry_ptr)entry;
1075 args = (struct pci_route_interrupt_args *)arg;
1076 if (intr->src_bus_id != args->bus || intr->src_bus_irq != args->irq)
1079 /* Make sure the APIC maps to a known APIC. */
1080 KASSERT(ioapics[intr->dst_apic_id] != NULL,
1081 ("No I/O APIC %d to route interrupt to", intr->dst_apic_id));
1084 * Look up the vector for this APIC / pin combination. If we
1085 * have previously matched an entry for this PCI IRQ but it
1086 * has the same vector as this entry, just return. Otherwise,
1087 * we use the vector for this APIC / pin combination.
1089 vector = ioapic_get_vector(ioapics[intr->dst_apic_id],
1090 intr->dst_apic_int);
1091 if (args->vector == vector)
1093 KASSERT(args->vector == -1,
1094 ("Multiple IRQs for PCI interrupt %d.%d.INT%c: %d and %d\n",
1095 args->bus, args->irq >> 2, 'A' + (args->irq & 0x3), args->vector,
1097 args->vector = vector;
1101 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
1103 struct pci_route_interrupt_args args;
1106 /* Like ACPI, pin numbers are 0-3, not 1-4. */
1108 KASSERT(pci0 != -1, ("do not know how to route PCI interrupts"));
1109 args.bus = pci_get_bus(dev) + pci0;
1110 slot = pci_get_slot(dev);
1113 * PCI interrupt entries in the MP Table encode both the slot and
1114 * pin into the IRQ with the pin being the two least significant
1115 * bits, the slot being the next five bits, and the most significant
1116 * bit being reserved.
1118 args.irq = slot << 2 | pin;
1120 mptable_walk_table(mptable_pci_route_interrupt_handler, &args);
1121 if (args.vector < 0) {
1122 device_printf(pcib, "unable to route slot %d INT%c\n", slot,
1124 return (PCI_INVALID_IRQ);
1127 device_printf(pcib, "slot %d INT%c routed to irq %d\n", slot,
1128 'A' + pin, args.vector);
1129 return (args.vector);
1133 struct host_res_args {
1134 struct mptable_hostb_softc *sc;
1140 * Initialize a Host-PCI bridge so it can restrict resource allocation
1141 * requests to the resources it actually decodes according to MP
1142 * config table extended entries.
1145 mptable_host_res_handler(ext_entry_ptr entry, void *arg)
1147 struct host_res_args *args;
1148 cbasm_entry_ptr cbasm;
1151 uint64_t start, end;
1152 int error, *flagp, flags, type;
1155 switch (entry->type) {
1156 case MPCT_EXTENTRY_SAS:
1157 sas = (sas_entry_ptr)entry;
1158 if (sas->bus_id != args->bus)
1160 switch (sas->address_type) {
1161 case SASENTRY_TYPE_IO:
1162 type = SYS_RES_IOPORT;
1165 case SASENTRY_TYPE_MEMORY:
1166 type = SYS_RES_MEMORY;
1169 case SASENTRY_TYPE_PREFETCH:
1170 type = SYS_RES_MEMORY;
1171 flags = RF_PREFETCHABLE;
1175 "MPTable: Unknown systems address space type for bus %u: %d\n",
1176 sas->bus_id, sas->address_type);
1179 start = sas->address_base;
1180 end = sas->address_base + sas->address_length - 1;
1182 if (start > ULONG_MAX) {
1183 device_printf(args->dev,
1184 "Ignoring %d range above 4GB (%#jx-%#jx)\n",
1185 type, (uintmax_t)start, (uintmax_t)end);
1188 if (end > ULONG_MAX) {
1189 device_printf(args->dev,
1190 "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
1191 type, (uintmax_t)start, (uintmax_t)end);
1195 error = pcib_host_res_decodes(&args->sc->sc_host_res, type,
1198 panic("Failed to manage %d range (%#jx-%#jx): %d",
1199 type, (uintmax_t)start, (uintmax_t)end, error);
1201 case MPCT_EXTENTRY_CBASM:
1202 cbasm = (cbasm_entry_ptr)entry;
1203 if (cbasm->bus_id != args->bus)
1205 switch (cbasm->predefined_range) {
1206 case CBASMENTRY_RANGE_ISA_IO:
1207 flagp = &args->sc->sc_decodes_isa_io;
1210 case CBASMENTRY_RANGE_VGA_IO:
1211 flagp = &args->sc->sc_decodes_vga_io;
1216 "MPTable: Unknown compatiblity address space range for bus %u: %d\n",
1217 cbasm->bus_id, cbasm->predefined_range);
1222 "MPTable: Duplicate compatibility %s range for bus %u\n",
1223 name, cbasm->bus_id);
1224 switch (cbasm->address_mod) {
1225 case CBASMENTRY_ADDRESS_MOD_ADD:
1228 device_printf(args->dev, "decoding %s ports\n",
1231 case CBASMENTRY_ADDRESS_MOD_SUBTRACT:
1234 device_printf(args->dev,
1235 "not decoding %s ports\n", name);
1239 "MPTable: Unknown compatibility address space modifier: %u\n",
1240 cbasm->address_mod);
1248 mptable_pci_host_res_init(device_t pcib)
1250 struct host_res_args args;
1252 KASSERT(pci0 != -1, ("do not know how to map PCI bus IDs"));
1253 args.bus = pci_get_bus(pcib) + pci0;
1255 args.sc = device_get_softc(pcib);
1256 if (pcib_host_res_init(pcib, &args.sc->sc_host_res) != 0)
1257 panic("failed to init hostb resources");
1258 mptable_walk_extended_table(mptable_host_res_handler, &args);