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);
501 mptable_setup_cpus_handler(u_char *entry, void *arg)
507 case MPCT_ENTRY_PROCESSOR:
508 proc = (proc_entry_ptr)entry;
509 if (proc->cpu_flags & PROCENTRY_FLAG_EN) {
510 lapic_create(proc->apic_id, proc->cpu_flags &
512 if (proc->apic_id < MAX_LAPIC_ID) {
513 cpu_mask = (u_int *)arg;
514 *cpu_mask |= (1ul << proc->apic_id);
522 mptable_count_items_handler(u_char *entry, void *arg __unused)
524 io_apic_entry_ptr apic;
529 bus = (bus_entry_ptr)entry;
531 if (bus->bus_id > mptable_maxbusid)
532 mptable_maxbusid = bus->bus_id;
534 case MPCT_ENTRY_IOAPIC:
535 apic = (io_apic_entry_ptr)entry;
536 if (apic->apic_flags & IOAPICENTRY_FLAG_EN)
543 * Count items in the table.
546 mptable_count_items(void)
549 /* Is this a pre-defined config? */
550 if (mpfps->config_type != 0) {
551 mptable_nioapics = 1;
552 switch (mpfps->config_type) {
565 panic("Unknown pre-defined MP Table config type %d",
568 mptable_maxbusid = mptable_nbusses - 1;
570 mptable_walk_table(mptable_count_items_handler, NULL);
574 * Add a bus or I/O APIC from an entry in the table.
577 mptable_parse_apics_and_busses_handler(u_char *entry, void *arg __unused)
579 io_apic_entry_ptr apic;
581 enum busTypes bus_type;
587 bus = (bus_entry_ptr)entry;
588 bus_type = lookup_bus_type(bus->bus_type);
589 if (bus_type == UNKNOWN_BUSTYPE) {
590 printf("MPTable: Unknown bus %d type \"", bus->bus_id);
591 for (i = 0; i < 6; i++)
592 printf("%c", bus->bus_type[i]);
595 busses[bus->bus_id].bus_id = bus->bus_id;
596 busses[bus->bus_id].bus_type = bus_type;
598 case MPCT_ENTRY_IOAPIC:
599 apic = (io_apic_entry_ptr)entry;
600 if (!(apic->apic_flags & IOAPICENTRY_FLAG_EN))
602 if (apic->apic_id > IOAPIC_MAX_ID)
603 panic("%s: I/O APIC ID %d too high", __func__,
605 if (ioapics[apic->apic_id] != NULL)
606 panic("%s: Double APIC ID %d", __func__,
608 ioapics[apic->apic_id] = ioapic_create(apic->apic_address,
617 * Enumerate I/O APIC's and buses.
620 mptable_parse_apics_and_busses(void)
623 /* Is this a pre-defined config? */
624 if (mpfps->config_type != 0) {
625 ioapics[2] = ioapic_create(DEFAULT_IO_APIC_BASE, 2, 0);
626 busses[0].bus_id = 0;
627 busses[0].bus_type = default_data[mpfps->config_type - 1][2];
628 if (mptable_nbusses > 1) {
629 busses[1].bus_id = 1;
631 default_data[mpfps->config_type - 1][4];
634 mptable_walk_table(mptable_parse_apics_and_busses_handler,
639 * Determine conforming polarity for a given bus type.
641 static enum intr_polarity
642 conforming_polarity(u_char src_bus, u_char src_bus_irq)
645 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
646 switch (busses[src_bus].bus_type) {
649 return (INTR_POLARITY_HIGH);
651 return (INTR_POLARITY_LOW);
653 panic("%s: unknown bus type %d", __func__,
654 busses[src_bus].bus_type);
659 * Determine conforming trigger for a given bus type.
661 static enum intr_trigger
662 conforming_trigger(u_char src_bus, u_char src_bus_irq)
665 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
666 switch (busses[src_bus].bus_type) {
669 return (elcr_read_trigger(src_bus_irq));
671 return (INTR_TRIGGER_EDGE);
673 return (INTR_TRIGGER_LEVEL);
676 KASSERT(src_bus_irq < 16, ("Invalid EISA IRQ %d", src_bus_irq));
677 KASSERT(elcr_found, ("Missing ELCR"));
678 return (elcr_read_trigger(src_bus_irq));
681 panic("%s: unknown bus type %d", __func__,
682 busses[src_bus].bus_type);
686 static enum intr_polarity
687 intentry_polarity(int_entry_ptr intr)
690 switch (intr->int_flags & INTENTRY_FLAGS_POLARITY) {
691 case INTENTRY_FLAGS_POLARITY_CONFORM:
692 return (conforming_polarity(intr->src_bus_id,
694 case INTENTRY_FLAGS_POLARITY_ACTIVEHI:
695 return (INTR_POLARITY_HIGH);
696 case INTENTRY_FLAGS_POLARITY_ACTIVELO:
697 return (INTR_POLARITY_LOW);
699 panic("Bogus interrupt flags");
703 static enum intr_trigger
704 intentry_trigger(int_entry_ptr intr)
707 switch (intr->int_flags & INTENTRY_FLAGS_TRIGGER) {
708 case INTENTRY_FLAGS_TRIGGER_CONFORM:
709 return (conforming_trigger(intr->src_bus_id,
711 case INTENTRY_FLAGS_TRIGGER_EDGE:
712 return (INTR_TRIGGER_EDGE);
713 case INTENTRY_FLAGS_TRIGGER_LEVEL:
714 return (INTR_TRIGGER_LEVEL);
716 panic("Bogus interrupt flags");
721 * Parse an interrupt entry for an I/O interrupt routed to a pin on an I/O APIC.
724 mptable_parse_io_int(int_entry_ptr intr)
729 apic_id = intr->dst_apic_id;
730 if (intr->dst_apic_id == 0xff) {
732 * An APIC ID of 0xff means that the interrupt is connected
733 * to the specified pin on all I/O APICs in the system. If
734 * there is only one I/O APIC, then use that APIC to route
735 * the interrupts. If there is more than one I/O APIC, then
738 if (mptable_nioapics == 1) {
740 while (ioapics[apic_id] == NULL)
744 "MPTable: Ignoring global interrupt entry for pin %d\n",
749 if (apic_id > IOAPIC_MAX_ID) {
750 printf("MPTable: Ignoring interrupt entry for ioapic%d\n",
754 ioapic = ioapics[apic_id];
755 if (ioapic == NULL) {
757 "MPTable: Ignoring interrupt entry for missing ioapic%d\n",
761 pin = intr->dst_apic_int;
762 switch (intr->int_type) {
763 case INTENTRY_TYPE_INT:
764 switch (busses[intr->src_bus_id].bus_type) {
766 panic("interrupt from missing bus");
769 if (busses[intr->src_bus_id].bus_type == ISA)
770 ioapic_set_bus(ioapic, pin, APIC_BUS_ISA);
772 ioapic_set_bus(ioapic, pin, APIC_BUS_EISA);
773 if (intr->src_bus_irq == pin)
775 ioapic_remap_vector(ioapic, pin, intr->src_bus_irq);
776 if (ioapic_get_vector(ioapic, intr->src_bus_irq) ==
778 ioapic_disable_pin(ioapic, intr->src_bus_irq);
781 ioapic_set_bus(ioapic, pin, APIC_BUS_PCI);
784 ioapic_set_bus(ioapic, pin, APIC_BUS_UNKNOWN);
788 case INTENTRY_TYPE_NMI:
789 ioapic_set_nmi(ioapic, pin);
791 case INTENTRY_TYPE_SMI:
792 ioapic_set_smi(ioapic, pin);
794 case INTENTRY_TYPE_EXTINT:
795 ioapic_set_extint(ioapic, pin);
798 panic("%s: invalid interrupt entry type %d\n", __func__,
801 if (intr->int_type == INTENTRY_TYPE_INT ||
802 (intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
803 INTENTRY_FLAGS_TRIGGER_CONFORM)
804 ioapic_set_triggermode(ioapic, pin, intentry_trigger(intr));
805 if (intr->int_type == INTENTRY_TYPE_INT ||
806 (intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
807 INTENTRY_FLAGS_POLARITY_CONFORM)
808 ioapic_set_polarity(ioapic, pin, intentry_polarity(intr));
812 * Parse an interrupt entry for a local APIC LVT pin.
815 mptable_parse_local_int(int_entry_ptr intr)
819 if (intr->dst_apic_id == 0xff)
820 apic_id = APIC_ID_ALL;
822 apic_id = intr->dst_apic_id;
823 if (intr->dst_apic_int == 0)
824 pin = APIC_LVT_LINT0;
826 pin = APIC_LVT_LINT1;
827 switch (intr->int_type) {
828 case INTENTRY_TYPE_INT:
831 "MPTable: Ignoring vectored local interrupt for LINTIN%d vector %d\n",
832 intr->dst_apic_int, intr->src_bus_irq);
835 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_FIXED);
838 case INTENTRY_TYPE_NMI:
839 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
841 case INTENTRY_TYPE_SMI:
842 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_SMI);
844 case INTENTRY_TYPE_EXTINT:
845 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_EXTINT);
848 panic("%s: invalid interrupt entry type %d\n", __func__,
851 if ((intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
852 INTENTRY_FLAGS_TRIGGER_CONFORM)
853 lapic_set_lvt_triggermode(apic_id, pin,
854 intentry_trigger(intr));
855 if ((intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
856 INTENTRY_FLAGS_POLARITY_CONFORM)
857 lapic_set_lvt_polarity(apic_id, pin, intentry_polarity(intr));
861 * Parse interrupt entries.
864 mptable_parse_ints_handler(u_char *entry, void *arg __unused)
868 intr = (int_entry_ptr)entry;
871 mptable_parse_io_int(intr);
873 case MPCT_ENTRY_LOCAL_INT:
874 mptable_parse_local_int(intr);
880 * Configure interrupt pins for a default configuration. For details see
881 * Table 5-2 in Section 5 of the MP Table specification.
884 mptable_parse_default_config_ints(void)
886 struct INTENTRY entry;
890 * All default configs route IRQs from bus 0 to the first 16 pins
891 * of the first I/O APIC with an APIC ID of 2.
893 entry.type = MPCT_ENTRY_INT;
894 entry.int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
895 INTENTRY_FLAGS_TRIGGER_CONFORM;
896 entry.src_bus_id = 0;
897 entry.dst_apic_id = 2;
899 /* Run through all 16 pins. */
900 for (pin = 0; pin < 16; pin++) {
901 entry.dst_apic_int = pin;
904 /* Pin 0 is an ExtINT pin. */
905 entry.int_type = INTENTRY_TYPE_EXTINT;
908 /* IRQ 0 is routed to pin 2. */
909 entry.int_type = INTENTRY_TYPE_INT;
910 entry.src_bus_irq = 0;
913 /* All other pins are identity mapped. */
914 entry.int_type = INTENTRY_TYPE_INT;
915 entry.src_bus_irq = pin;
918 mptable_parse_io_int(&entry);
921 /* Certain configs disable certain pins. */
922 if (mpfps->config_type == 7)
923 ioapic_disable_pin(ioapics[2], 0);
924 if (mpfps->config_type == 2) {
925 ioapic_disable_pin(ioapics[2], 2);
926 ioapic_disable_pin(ioapics[2], 13);
931 * Configure the interrupt pins
934 mptable_parse_ints(void)
937 /* Is this a pre-defined config? */
938 if (mpfps->config_type != 0) {
939 /* Configure LINT pins. */
940 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT0,
942 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT1, APIC_LVT_DM_NMI);
944 /* Configure I/O APIC pins. */
945 mptable_parse_default_config_ints();
947 mptable_walk_table(mptable_parse_ints_handler, NULL);
950 #ifdef MPTABLE_FORCE_HTT
952 * Perform a hyperthreading "fix-up" to enumerate any logical CPU's
953 * that aren't already listed in the table.
955 * XXX: We assume that all of the physical CPUs in the
956 * system have the same number of logical CPUs.
958 * XXX: We assume that APIC ID's are allocated such that
959 * the APIC ID's for a physical processor are aligned
960 * with the number of logical CPU's in the processor.
963 mptable_hyperthread_fixup(u_int id_mask)
965 u_int i, id, logical_cpus;
967 /* Nothing to do if there is no HTT support. */
968 if ((cpu_feature & CPUID_HTT) == 0)
970 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
971 if (logical_cpus <= 1)
975 * For each APIC ID of a CPU that is set in the mask,
976 * scan the other candidate APIC ID's for this
977 * physical processor. If any of those ID's are
978 * already in the table, then kill the fixup.
980 for (id = 0; id <= MAX_LAPIC_ID; id++) {
981 if ((id_mask & 1 << id) == 0)
983 /* First, make sure we are on a logical_cpus boundary. */
984 if (id % logical_cpus != 0)
986 for (i = id + 1; i < id + logical_cpus; i++)
987 if ((id_mask & 1 << i) != 0)
992 * Ok, the ID's checked out, so perform the fixup by
993 * adding the logical CPUs.
995 while ((id = ffs(id_mask)) != 0) {
997 for (i = id + 1; i < id + logical_cpus; i++) {
1000 "MPTable: Adding logical CPU %d from main CPU %d\n",
1004 id_mask &= ~(1 << id);
1007 #endif /* MPTABLE_FORCE_HTT */
1010 * Support code for routing PCI interrupts using the MP Table.
1013 mptable_pci_setup(void)
1018 * Find the first pci bus and call it 0. Panic if pci0 is not
1019 * bus zero and there are multiple PCI buses.
1021 for (i = 0; i <= mptable_maxbusid; i++)
1022 if (busses[i].bus_type == PCI) {
1027 "MPTable contains multiple PCI buses but no PCI bus 0");
1032 mptable_pci_probe_table_handler(u_char *entry, void *arg)
1034 struct pci_probe_table_args *args;
1037 if (*entry != MPCT_ENTRY_INT)
1039 intr = (int_entry_ptr)entry;
1040 args = (struct pci_probe_table_args *)arg;
1041 KASSERT(args->bus <= mptable_maxbusid,
1042 ("bus %d is too big", args->bus));
1043 KASSERT(busses[args->bus].bus_type == PCI, ("probing for non-PCI bus"));
1044 if (intr->src_bus_id == args->bus)
1049 mptable_pci_probe_table(int bus)
1051 struct pci_probe_table_args args;
1055 if (mpct == NULL || pci0 == -1 || pci0 + bus > mptable_maxbusid)
1057 if (busses[pci0 + bus].bus_type != PCI)
1059 args.bus = pci0 + bus;
1061 mptable_walk_table(mptable_pci_probe_table_handler, &args);
1062 if (args.found == 0)
1068 mptable_pci_route_interrupt_handler(u_char *entry, void *arg)
1070 struct pci_route_interrupt_args *args;
1074 if (*entry != MPCT_ENTRY_INT)
1076 intr = (int_entry_ptr)entry;
1077 args = (struct pci_route_interrupt_args *)arg;
1078 if (intr->src_bus_id != args->bus || intr->src_bus_irq != args->irq)
1081 /* Make sure the APIC maps to a known APIC. */
1082 KASSERT(ioapics[intr->dst_apic_id] != NULL,
1083 ("No I/O APIC %d to route interrupt to", intr->dst_apic_id));
1086 * Look up the vector for this APIC / pin combination. If we
1087 * have previously matched an entry for this PCI IRQ but it
1088 * has the same vector as this entry, just return. Otherwise,
1089 * we use the vector for this APIC / pin combination.
1091 vector = ioapic_get_vector(ioapics[intr->dst_apic_id],
1092 intr->dst_apic_int);
1093 if (args->vector == vector)
1095 KASSERT(args->vector == -1,
1096 ("Multiple IRQs for PCI interrupt %d.%d.INT%c: %d and %d\n",
1097 args->bus, args->irq >> 2, 'A' + (args->irq & 0x3), args->vector,
1099 args->vector = vector;
1103 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
1105 struct pci_route_interrupt_args args;
1108 /* Like ACPI, pin numbers are 0-3, not 1-4. */
1110 KASSERT(pci0 != -1, ("do not know how to route PCI interrupts"));
1111 args.bus = pci_get_bus(dev) + pci0;
1112 slot = pci_get_slot(dev);
1115 * PCI interrupt entries in the MP Table encode both the slot and
1116 * pin into the IRQ with the pin being the two least significant
1117 * bits, the slot being the next five bits, and the most significant
1118 * bit being reserved.
1120 args.irq = slot << 2 | pin;
1122 mptable_walk_table(mptable_pci_route_interrupt_handler, &args);
1123 if (args.vector < 0) {
1124 device_printf(pcib, "unable to route slot %d INT%c\n", slot,
1126 return (PCI_INVALID_IRQ);
1129 device_printf(pcib, "slot %d INT%c routed to irq %d\n", slot,
1130 'A' + pin, args.vector);
1131 return (args.vector);
1135 struct host_res_args {
1136 struct mptable_hostb_softc *sc;
1142 * Initialize a Host-PCI bridge so it can restrict resource allocation
1143 * requests to the resources it actually decodes according to MP
1144 * config table extended entries.
1147 mptable_host_res_handler(ext_entry_ptr entry, void *arg)
1149 struct host_res_args *args;
1150 cbasm_entry_ptr cbasm;
1153 uint64_t start, end;
1154 int error, *flagp, flags, type;
1157 switch (entry->type) {
1158 case MPCT_EXTENTRY_SAS:
1159 sas = (sas_entry_ptr)entry;
1160 if (sas->bus_id != args->bus)
1162 switch (sas->address_type) {
1163 case SASENTRY_TYPE_IO:
1164 type = SYS_RES_IOPORT;
1167 case SASENTRY_TYPE_MEMORY:
1168 type = SYS_RES_MEMORY;
1171 case SASENTRY_TYPE_PREFETCH:
1172 type = SYS_RES_MEMORY;
1173 flags = RF_PREFETCHABLE;
1177 "MPTable: Unknown systems address space type for bus %u: %d\n",
1178 sas->bus_id, sas->address_type);
1181 start = sas->address_base;
1182 end = sas->address_base + sas->address_length - 1;
1184 if (start > ULONG_MAX) {
1185 device_printf(args->dev,
1186 "Ignoring %d range above 4GB (%#jx-%#jx)\n",
1187 type, (uintmax_t)start, (uintmax_t)end);
1190 if (end > ULONG_MAX) {
1191 device_printf(args->dev,
1192 "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
1193 type, (uintmax_t)start, (uintmax_t)end);
1197 error = pcib_host_res_decodes(&args->sc->sc_host_res, type,
1200 panic("Failed to manage %d range (%#jx-%#jx): %d",
1201 type, (uintmax_t)start, (uintmax_t)end, error);
1203 case MPCT_EXTENTRY_CBASM:
1204 cbasm = (cbasm_entry_ptr)entry;
1205 if (cbasm->bus_id != args->bus)
1207 switch (cbasm->predefined_range) {
1208 case CBASMENTRY_RANGE_ISA_IO:
1209 flagp = &args->sc->sc_decodes_isa_io;
1212 case CBASMENTRY_RANGE_VGA_IO:
1213 flagp = &args->sc->sc_decodes_vga_io;
1218 "MPTable: Unknown compatiblity address space range for bus %u: %d\n",
1219 cbasm->bus_id, cbasm->predefined_range);
1224 "MPTable: Duplicate compatibility %s range for bus %u\n",
1225 name, cbasm->bus_id);
1226 switch (cbasm->address_mod) {
1227 case CBASMENTRY_ADDRESS_MOD_ADD:
1230 device_printf(args->dev, "decoding %s ports\n",
1233 case CBASMENTRY_ADDRESS_MOD_SUBTRACT:
1236 device_printf(args->dev,
1237 "not decoding %s ports\n", name);
1241 "MPTable: Unknown compatibility address space modifier: %u\n",
1242 cbasm->address_mod);
1250 mptable_pci_host_res_init(device_t pcib)
1252 struct host_res_args args;
1254 KASSERT(pci0 != -1, ("do not know how to map PCI bus IDs"));
1255 args.bus = pci_get_bus(pcib) + pci0;
1257 args.sc = device_get_softc(pcib);
1258 if (pcib_host_res_init(pcib, &args.sc->sc_host_res) != 0)
1259 panic("failed to init hostb resources");
1260 mptable_walk_extended_table(mptable_host_res_handler, &args);