2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3 * Copyright (c) 1996, by Steve Passe
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the developer may NOT be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
37 #include <vm/vm_param.h>
40 #include <machine/apicreg.h>
41 #include <machine/frame.h>
42 #include <machine/intr_machdep.h>
43 #include <machine/apicvar.h>
44 #include <machine/md_var.h>
45 #include <machine/mptable.h>
46 #include <machine/specialreg.h>
48 #include <dev/pci/pcivar.h>
50 /* string defined by the Intel MP Spec as identifying the MP table */
51 #define MP_SIG 0x5f504d5f /* _MP_ */
53 #define MAX_LAPIC_ID 63 /* Max local APIC ID for HTT fixup */
55 #define BIOS_BASE (0xf0000)
56 #define BIOS_SIZE (0x10000)
57 #define BIOS_COUNT (BIOS_SIZE/4)
59 typedef void mptable_entry_handler(u_char *entry, void *arg);
61 static basetable_entry basetable_entry_types[] =
70 typedef struct BUSDATA {
72 enum busTypes bus_type;
75 typedef struct INTDATA {
85 typedef struct BUSTYPENAME {
90 /* From MP spec v1.4, table 4-8. */
91 static bus_type_name bus_type_table[] =
93 {UNKNOWN_BUSTYPE, "CBUS "},
94 {UNKNOWN_BUSTYPE, "CBUSII"},
96 {UNKNOWN_BUSTYPE, "FUTURE"},
97 {UNKNOWN_BUSTYPE, "INTERN"},
99 {UNKNOWN_BUSTYPE, "MBI "},
100 {UNKNOWN_BUSTYPE, "MBII "},
102 {UNKNOWN_BUSTYPE, "MPI "},
103 {UNKNOWN_BUSTYPE, "MPSA "},
104 {UNKNOWN_BUSTYPE, "NUBUS "},
106 {UNKNOWN_BUSTYPE, "PCMCIA"},
107 {UNKNOWN_BUSTYPE, "TC "},
108 {UNKNOWN_BUSTYPE, "VL "},
109 {UNKNOWN_BUSTYPE, "VME "},
110 {UNKNOWN_BUSTYPE, "XPRESS"}
113 /* From MP spec v1.4, table 5-1. */
114 static int default_data[7][5] =
116 /* nbus, id0, type0, id1, type1 */
117 {1, 0, ISA, 255, NOBUS},
118 {1, 0, EISA, 255, NOBUS},
119 {1, 0, EISA, 255, NOBUS},
120 {1, 0, MCA, 255, NOBUS},
122 {2, 0, EISA, 1, PCI},
126 struct pci_probe_table_args {
131 struct pci_route_interrupt_args {
132 u_char bus; /* Source bus. */
133 u_char irq; /* Source slot:pin. */
134 int vector; /* Return value. */
137 static mpfps_t mpfps;
139 static void *ioapics[MAX_APIC_ID + 1];
140 static bus_datum *busses;
141 static int mptable_nioapics, mptable_nbusses, mptable_maxbusid;
142 static int pci0 = -1;
144 static MALLOC_DEFINE(M_MPTABLE, "mptable", "MP Table Items");
146 static enum intr_polarity conforming_polarity(u_char src_bus,
148 static enum intr_trigger conforming_trigger(u_char src_bus, u_char src_bus_irq);
149 static enum intr_polarity intentry_polarity(int_entry_ptr intr);
150 static enum intr_trigger intentry_trigger(int_entry_ptr intr);
151 static int lookup_bus_type(char *name);
152 static void mptable_count_items(void);
153 static void mptable_count_items_handler(u_char *entry, void *arg);
154 #ifdef MPTABLE_FORCE_HTT
155 static void mptable_hyperthread_fixup(u_long id_mask);
157 static void mptable_parse_apics_and_busses(void);
158 static void mptable_parse_apics_and_busses_handler(u_char *entry,
160 static void mptable_parse_default_config_ints(void);
161 static void mptable_parse_ints(void);
162 static void mptable_parse_ints_handler(u_char *entry, void *arg);
163 static void mptable_parse_io_int(int_entry_ptr intr);
164 static void mptable_parse_local_int(int_entry_ptr intr);
165 static void mptable_pci_probe_table_handler(u_char *entry, void *arg);
166 static void mptable_pci_route_interrupt_handler(u_char *entry, void *arg);
167 static void mptable_pci_setup(void);
168 static int mptable_probe(void);
169 static int mptable_probe_cpus(void);
170 static void mptable_probe_cpus_handler(u_char *entry, void *arg __unused);
171 static void mptable_register(void *dummy);
172 static int mptable_setup_local(void);
173 static int mptable_setup_io(void);
174 static void mptable_walk_table(mptable_entry_handler *handler, void *arg);
175 static int search_for_sig(u_int32_t target, int count);
177 static struct apic_enumerator mptable_enumerator = {
186 * look for the MP spec signature
190 search_for_sig(u_int32_t target, int count)
193 u_int32_t *addr = (u_int32_t *) (KERNBASE + target);
195 for (x = 0; x < count; x += 4)
196 if (addr[x] == MP_SIG)
197 /* make array index a byte index */
198 return (target + (x * sizeof(u_int32_t)));
203 lookup_bus_type(char *name)
207 for (x = 0; x < MAX_BUSTYPE; ++x)
208 if (strncmp(bus_type_table[x].name, name, 6) == 0)
209 return (bus_type_table[x].type);
211 return (UNKNOWN_BUSTYPE);
215 * Look for an Intel MP spec table (ie, SMP capable hardware).
224 /* see if EBDA exists */
225 segment = (u_int32_t) *(u_short *)(KERNBASE + 0x40e);
227 /* search first 1K of EBDA */
228 target = (u_int32_t) (segment << 4);
229 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
232 /* last 1K of base memory, effective 'top of base' passed in */
233 target = (u_int32_t) ((basemem * 1024) - 0x400);
234 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
238 /* search the BIOS */
239 target = (u_int32_t) BIOS_BASE;
240 if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
247 mpfps = (mpfps_t)(KERNBASE + x);
249 /* Map in the configuration table if it exists. */
250 if (mpfps->config_type != 0) {
253 "MP Table version 1.%d found using Default Configuration %d\n",
254 mpfps->spec_rev, mpfps->config_type);
255 if (mpfps->config_type != 5 && mpfps->config_type != 6) {
257 "MP Table Default Configuration %d is unsupported\n",
263 if ((uintptr_t)mpfps->pap >= 1024 * 1024) {
264 printf("%s: Unable to map MP Configuration Table\n",
268 mpct = (mpcth_t)(KERNBASE + (uintptr_t)mpfps->pap);
269 if (mpct->base_table_length + (uintptr_t)mpfps->pap >=
271 printf("%s: Unable to map end of MP Config Table\n",
275 if (mpct->signature[0] != 'P' || mpct->signature[1] != 'C' ||
276 mpct->signature[2] != 'M' || mpct->signature[3] != 'P') {
277 printf("%s: MP Config Table has bad signature: %c%c%c%c\n",
278 __func__, mpct->signature[0], mpct->signature[1],
279 mpct->signature[2], mpct->signature[3]);
284 "MP Configuration Table version 1.%d found at %p\n",
285 mpct->spec_rev, mpct);
292 * Run through the MP table enumerating CPUs.
295 mptable_probe_cpus(void)
299 /* Is this a pre-defined config? */
300 if (mpfps->config_type != 0) {
305 mptable_walk_table(mptable_probe_cpus_handler, &cpu_mask);
306 #ifdef MPTABLE_FORCE_HTT
307 mptable_hyperthread_fixup(cpu_mask);
314 * Initialize the local APIC on the BSP.
317 mptable_setup_local(void)
321 /* Is this a pre-defined config? */
322 printf("MPTable: <");
323 if (mpfps->config_type != 0) {
324 addr = DEFAULT_APIC_BASE;
325 printf("Default Configuration %d", mpfps->config_type);
327 addr = mpct->apic_address;
328 printf("%.*s %.*s", (int)sizeof(mpct->oem_id), mpct->oem_id,
329 (int)sizeof(mpct->product_id), mpct->product_id);
337 * Run through the MP table enumerating I/O APICs.
340 mptable_setup_io(void)
345 /* First, we count individual items and allocate arrays. */
346 mptable_count_items();
347 busses = malloc((mptable_maxbusid + 1) * sizeof(bus_datum), M_MPTABLE,
349 for (i = 0; i <= mptable_maxbusid; i++)
350 busses[i].bus_type = NOBUS;
352 /* Second, we run through adding I/O APIC's and busses. */
353 mptable_parse_apics_and_busses();
355 /* Third, we run through the table tweaking interrupt sources. */
356 mptable_parse_ints();
358 /* Fourth, we register all the I/O APIC's. */
359 for (i = 0; i <= MAX_APIC_ID; i++)
360 if (ioapics[i] != NULL)
361 ioapic_register(ioapics[i]);
363 /* Fifth, we setup data structures to handle PCI interrupt routing. */
366 /* Finally, we throw the switch to enable the I/O APIC's. */
367 if (mpfps->mpfb2 & MPFB2_IMCR_PRESENT) {
368 outb(0x22, 0x70); /* select IMCR */
369 byte = inb(0x23); /* current contents */
370 byte |= 0x01; /* mask external INTR */
371 outb(0x23, byte); /* disconnect 8259s/NMI */
378 mptable_register(void *dummy __unused)
381 apic_register_enumerator(&mptable_enumerator);
383 SYSINIT(mptable_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST,
384 mptable_register, NULL);
387 * Call the handler routine for each entry in the MP config table.
390 mptable_walk_table(mptable_entry_handler *handler, void *arg)
395 entry = (u_char *)(mpct + 1);
396 for (i = 0; i < mpct->entry_count; i++) {
398 case MPCT_ENTRY_PROCESSOR:
399 case MPCT_ENTRY_IOAPIC:
402 case MPCT_ENTRY_LOCAL_INT:
405 panic("%s: Unknown MP Config Entry %d\n", __func__,
409 entry += basetable_entry_types[*entry].length;
414 mptable_probe_cpus_handler(u_char *entry, void *arg)
420 case MPCT_ENTRY_PROCESSOR:
421 proc = (proc_entry_ptr)entry;
422 if (proc->cpu_flags & PROCENTRY_FLAG_EN) {
423 lapic_create(proc->apic_id, proc->cpu_flags &
425 if (proc->apic_id < MAX_LAPIC_ID) {
426 cpu_mask = (u_long *)arg;
427 *cpu_mask |= (1ul << proc->apic_id);
435 mptable_count_items_handler(u_char *entry, void *arg __unused)
437 io_apic_entry_ptr apic;
442 bus = (bus_entry_ptr)entry;
444 if (bus->bus_id > mptable_maxbusid)
445 mptable_maxbusid = bus->bus_id;
447 case MPCT_ENTRY_IOAPIC:
448 apic = (io_apic_entry_ptr)entry;
449 if (apic->apic_flags & IOAPICENTRY_FLAG_EN)
456 * Count items in the table.
459 mptable_count_items(void)
462 /* Is this a pre-defined config? */
463 if (mpfps->config_type != 0) {
464 mptable_nioapics = 1;
465 switch (mpfps->config_type) {
478 panic("Unknown pre-defined MP Table config type %d",
481 mptable_maxbusid = mptable_nbusses - 1;
483 mptable_walk_table(mptable_count_items_handler, NULL);
487 * Add a bus or I/O APIC from an entry in the table.
490 mptable_parse_apics_and_busses_handler(u_char *entry, void *arg __unused)
492 io_apic_entry_ptr apic;
494 enum busTypes bus_type;
500 bus = (bus_entry_ptr)entry;
501 bus_type = lookup_bus_type(bus->bus_type);
502 if (bus_type == UNKNOWN_BUSTYPE) {
503 printf("MPTable: Unknown bus %d type \"", bus->bus_id);
504 for (i = 0; i < 6; i++)
505 printf("%c", bus->bus_type[i]);
508 busses[bus->bus_id].bus_id = bus->bus_id;
509 busses[bus->bus_id].bus_type = bus_type;
511 case MPCT_ENTRY_IOAPIC:
512 apic = (io_apic_entry_ptr)entry;
513 if (!(apic->apic_flags & IOAPICENTRY_FLAG_EN))
515 if (apic->apic_id > MAX_APIC_ID)
516 panic("%s: I/O APIC ID %d too high", __func__,
518 if (ioapics[apic->apic_id] != NULL)
519 panic("%s: Double APIC ID %d", __func__,
521 ioapics[apic->apic_id] = ioapic_create(apic->apic_address,
530 * Enumerate I/O APIC's and busses.
533 mptable_parse_apics_and_busses(void)
536 /* Is this a pre-defined config? */
537 if (mpfps->config_type != 0) {
538 ioapics[2] = ioapic_create(DEFAULT_IO_APIC_BASE, 2, 0);
539 busses[0].bus_id = 0;
540 busses[0].bus_type = default_data[mpfps->config_type - 1][2];
541 if (mptable_nbusses > 1) {
542 busses[1].bus_id = 1;
544 default_data[mpfps->config_type - 1][4];
547 mptable_walk_table(mptable_parse_apics_and_busses_handler,
552 * Determine conforming polarity for a given bus type.
554 static enum intr_polarity
555 conforming_polarity(u_char src_bus, u_char src_bus_irq)
558 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
559 switch (busses[src_bus].bus_type) {
562 return (INTR_POLARITY_HIGH);
564 return (INTR_POLARITY_LOW);
566 panic("%s: unknown bus type %d", __func__,
567 busses[src_bus].bus_type);
572 * Determine conforming trigger for a given bus type.
574 static enum intr_trigger
575 conforming_trigger(u_char src_bus, u_char src_bus_irq)
578 KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
579 switch (busses[src_bus].bus_type) {
582 return (elcr_read_trigger(src_bus_irq));
584 return (INTR_TRIGGER_EDGE);
586 return (INTR_TRIGGER_LEVEL);
588 KASSERT(src_bus_irq < 16, ("Invalid EISA IRQ %d", src_bus_irq));
589 KASSERT(elcr_found, ("Missing ELCR"));
590 return (elcr_read_trigger(src_bus_irq));
592 panic("%s: unknown bus type %d", __func__,
593 busses[src_bus].bus_type);
597 static enum intr_polarity
598 intentry_polarity(int_entry_ptr intr)
601 switch (intr->int_flags & INTENTRY_FLAGS_POLARITY) {
602 case INTENTRY_FLAGS_POLARITY_CONFORM:
603 return (conforming_polarity(intr->src_bus_id,
605 case INTENTRY_FLAGS_POLARITY_ACTIVEHI:
606 return (INTR_POLARITY_HIGH);
607 case INTENTRY_FLAGS_POLARITY_ACTIVELO:
608 return (INTR_POLARITY_LOW);
610 panic("Bogus interrupt flags");
614 static enum intr_trigger
615 intentry_trigger(int_entry_ptr intr)
618 switch (intr->int_flags & INTENTRY_FLAGS_TRIGGER) {
619 case INTENTRY_FLAGS_TRIGGER_CONFORM:
620 return (conforming_trigger(intr->src_bus_id,
622 case INTENTRY_FLAGS_TRIGGER_EDGE:
623 return (INTR_TRIGGER_EDGE);
624 case INTENTRY_FLAGS_TRIGGER_LEVEL:
625 return (INTR_TRIGGER_LEVEL);
627 panic("Bogus interrupt flags");
632 * Parse an interrupt entry for an I/O interrupt routed to a pin on an I/O APIC.
635 mptable_parse_io_int(int_entry_ptr intr)
640 apic_id = intr->dst_apic_id;
641 if (intr->dst_apic_id == 0xff) {
643 * An APIC ID of 0xff means that the interrupt is connected
644 * to the specified pin on all I/O APICs in the system. If
645 * there is only one I/O APIC, then use that APIC to route
646 * the interrupts. If there is more than one I/O APIC, then
649 if (mptable_nioapics == 1) {
651 while (ioapics[apic_id] == NULL)
655 "MPTable: Ignoring global interrupt entry for pin %d\n",
660 if (apic_id > MAX_APIC_ID) {
661 printf("MPTable: Ignoring interrupt entry for ioapic%d\n",
665 ioapic = ioapics[apic_id];
666 if (ioapic == NULL) {
668 "MPTable: Ignoring interrupt entry for missing ioapic%d\n",
672 pin = intr->dst_apic_int;
673 switch (intr->int_type) {
674 case INTENTRY_TYPE_INT:
675 switch (busses[intr->src_bus_id].bus_type) {
677 panic("interrupt from missing bus");
680 if (busses[intr->src_bus_id].bus_type == ISA)
681 ioapic_set_bus(ioapic, pin, APIC_BUS_ISA);
683 ioapic_set_bus(ioapic, pin, APIC_BUS_EISA);
684 if (intr->src_bus_irq == pin)
686 ioapic_remap_vector(ioapic, pin, intr->src_bus_irq);
687 if (ioapic_get_vector(ioapic, intr->src_bus_irq) ==
689 ioapic_disable_pin(ioapic, intr->src_bus_irq);
692 ioapic_set_bus(ioapic, pin, APIC_BUS_PCI);
695 ioapic_set_bus(ioapic, pin, APIC_BUS_UNKNOWN);
699 case INTENTRY_TYPE_NMI:
700 ioapic_set_nmi(ioapic, pin);
702 case INTENTRY_TYPE_SMI:
703 ioapic_set_smi(ioapic, pin);
705 case INTENTRY_TYPE_EXTINT:
706 ioapic_set_extint(ioapic, pin);
709 panic("%s: invalid interrupt entry type %d\n", __func__,
712 if (intr->int_type == INTENTRY_TYPE_INT ||
713 (intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
714 INTENTRY_FLAGS_TRIGGER_CONFORM)
715 ioapic_set_triggermode(ioapic, pin, intentry_trigger(intr));
716 if (intr->int_type == INTENTRY_TYPE_INT ||
717 (intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
718 INTENTRY_FLAGS_POLARITY_CONFORM)
719 ioapic_set_polarity(ioapic, pin, intentry_polarity(intr));
723 * Parse an interrupt entry for a local APIC LVT pin.
726 mptable_parse_local_int(int_entry_ptr intr)
730 if (intr->dst_apic_id == 0xff)
731 apic_id = APIC_ID_ALL;
733 apic_id = intr->dst_apic_id;
734 if (intr->dst_apic_int == 0)
738 switch (intr->int_type) {
739 case INTENTRY_TYPE_INT:
742 "MPTable: Ignoring vectored local interrupt for LINTIN%d vector %d\n",
743 intr->dst_apic_int, intr->src_bus_irq);
746 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_FIXED);
749 case INTENTRY_TYPE_NMI:
750 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
752 case INTENTRY_TYPE_SMI:
753 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_SMI);
755 case INTENTRY_TYPE_EXTINT:
756 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_EXTINT);
759 panic("%s: invalid interrupt entry type %d\n", __func__,
762 if ((intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
763 INTENTRY_FLAGS_TRIGGER_CONFORM)
764 lapic_set_lvt_triggermode(apic_id, pin,
765 intentry_trigger(intr));
766 if ((intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
767 INTENTRY_FLAGS_POLARITY_CONFORM)
768 lapic_set_lvt_polarity(apic_id, pin, intentry_polarity(intr));
772 * Parse interrupt entries.
775 mptable_parse_ints_handler(u_char *entry, void *arg __unused)
779 intr = (int_entry_ptr)entry;
782 mptable_parse_io_int(intr);
784 case MPCT_ENTRY_LOCAL_INT:
785 mptable_parse_local_int(intr);
791 * Configure interrupt pins for a default configuration. For details see
792 * Table 5-2 in Section 5 of the MP Table specification.
795 mptable_parse_default_config_ints(void)
797 struct INTENTRY entry;
801 * All default configs route IRQs from bus 0 to the first 16 pins
802 * of the first I/O APIC with an APIC ID of 2.
804 entry.type = MPCT_ENTRY_INT;
805 entry.int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
806 INTENTRY_FLAGS_TRIGGER_CONFORM;
807 entry.src_bus_id = 0;
808 entry.dst_apic_id = 2;
810 /* Run through all 16 pins. */
811 for (pin = 0; pin < 16; pin++) {
812 entry.dst_apic_int = pin;
815 /* Pin 0 is an ExtINT pin. */
816 entry.int_type = INTENTRY_TYPE_EXTINT;
819 /* IRQ 0 is routed to pin 2. */
820 entry.int_type = INTENTRY_TYPE_INT;
821 entry.src_bus_irq = 0;
824 /* All other pins are identity mapped. */
825 entry.int_type = INTENTRY_TYPE_INT;
826 entry.src_bus_irq = pin;
829 mptable_parse_io_int(&entry);
832 /* Certain configs disable certain pins. */
833 if (mpfps->config_type == 7)
834 ioapic_disable_pin(ioapics[2], 0);
835 if (mpfps->config_type == 2) {
836 ioapic_disable_pin(ioapics[2], 2);
837 ioapic_disable_pin(ioapics[2], 13);
842 * Configure the interrupt pins
845 mptable_parse_ints(void)
848 /* Is this a pre-defined config? */
849 if (mpfps->config_type != 0) {
850 /* Configure LINT pins. */
851 lapic_set_lvt_mode(APIC_ID_ALL, LVT_LINT0, APIC_LVT_DM_EXTINT);
852 lapic_set_lvt_mode(APIC_ID_ALL, LVT_LINT1, APIC_LVT_DM_NMI);
854 /* Configure I/O APIC pins. */
855 mptable_parse_default_config_ints();
857 mptable_walk_table(mptable_parse_ints_handler, NULL);
860 #ifdef MPTABLE_FORCE_HTT
862 * Perform a hyperthreading "fix-up" to enumerate any logical CPU's
863 * that aren't already listed in the table.
865 * XXX: We assume that all of the physical CPUs in the
866 * system have the same number of logical CPUs.
868 * XXX: We assume that APIC ID's are allocated such that
869 * the APIC ID's for a physical processor are aligned
870 * with the number of logical CPU's in the processor.
873 mptable_hyperthread_fixup(u_long id_mask)
875 u_int i, id, logical_cpus;
877 /* Nothing to do if there is no HTT support. */
878 if ((cpu_feature & CPUID_HTT) == 0)
880 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
881 if (logical_cpus <= 1)
885 * For each APIC ID of a CPU that is set in the mask,
886 * scan the other candidate APIC ID's for this
887 * physical processor. If any of those ID's are
888 * already in the table, then kill the fixup.
890 for (id = 0; id <= MAX_LAPIC_ID; id++) {
891 if ((id_mask & 1 << id) == 0)
893 /* First, make sure we are on a logical_cpus boundary. */
894 if (id % logical_cpus != 0)
896 for (i = id + 1; i < id + logical_cpus; i++)
897 if ((id_mask & 1 << i) != 0)
902 * Ok, the ID's checked out, so perform the fixup by
903 * adding the logical CPUs.
905 while ((id = ffsl(id_mask)) != 0) {
907 for (i = id + 1; i < id + logical_cpus; i++) {
910 "MPTable: Adding logical CPU %d from main CPU %d\n",
914 id_mask &= ~(1 << id);
917 #endif /* MPTABLE_FORCE_HTT */
920 * Support code for routing PCI interrupts using the MP Table.
923 mptable_pci_setup(void)
928 * Find the first pci bus and call it 0. Panic if pci0 is not
929 * bus zero and there are multiple PCI busses.
931 for (i = 0; i <= mptable_maxbusid; i++)
932 if (busses[i].bus_type == PCI) {
937 "MPTable contains multiple PCI busses but no PCI bus 0");
942 mptable_pci_probe_table_handler(u_char *entry, void *arg)
944 struct pci_probe_table_args *args;
947 if (*entry != MPCT_ENTRY_INT)
949 intr = (int_entry_ptr)entry;
950 args = (struct pci_probe_table_args *)arg;
951 KASSERT(args->bus <= mptable_maxbusid,
952 ("bus %d is too big", args->bus));
953 KASSERT(busses[args->bus].bus_type == PCI, ("probing for non-PCI bus"));
954 if (intr->src_bus_id == args->bus)
959 mptable_pci_probe_table(int bus)
961 struct pci_probe_table_args args;
965 if (mpct == NULL || pci0 == -1 || pci0 + bus > mptable_maxbusid)
967 if (busses[pci0 + bus].bus_type != PCI)
969 args.bus = pci0 + bus;
971 mptable_walk_table(mptable_pci_probe_table_handler, &args);
978 mptable_pci_route_interrupt_handler(u_char *entry, void *arg)
980 struct pci_route_interrupt_args *args;
984 if (*entry != MPCT_ENTRY_INT)
986 intr = (int_entry_ptr)entry;
987 args = (struct pci_route_interrupt_args *)arg;
988 if (intr->src_bus_id != args->bus || intr->src_bus_irq != args->irq)
991 /* Make sure the APIC maps to a known APIC. */
992 KASSERT(ioapics[intr->dst_apic_id] != NULL,
993 ("No I/O APIC %d to route interrupt to", intr->dst_apic_id));
996 * Look up the vector for this APIC / pin combination. If we
997 * have previously matched an entry for this PCI IRQ but it
998 * has the same vector as this entry, just return. Otherwise,
999 * we use the vector for this APIC / pin combination.
1001 vector = ioapic_get_vector(ioapics[intr->dst_apic_id],
1002 intr->dst_apic_int);
1003 if (args->vector == vector)
1005 KASSERT(args->vector == -1,
1006 ("Multiple IRQs for PCI interrupt %d.%d.INT%c: %d and %d\n",
1007 args->bus, args->irq >> 2, 'A' + (args->irq & 0x3), args->vector,
1009 args->vector = vector;
1013 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
1015 struct pci_route_interrupt_args args;
1018 /* Like ACPI, pin numbers are 0-3, not 1-4. */
1020 KASSERT(pci0 != -1, ("do not know how to route PCI interrupts"));
1021 args.bus = pci_get_bus(dev) + pci0;
1022 slot = pci_get_slot(dev);
1025 * PCI interrupt entries in the MP Table encode both the slot and
1026 * pin into the IRQ with the pin being the two least significant
1027 * bits, the slot being the next five bits, and the most significant
1028 * bit being reserved.
1030 args.irq = slot << 2 | pin;
1032 mptable_walk_table(mptable_pci_route_interrupt_handler, &args);
1033 if (args.vector < 0) {
1034 device_printf(pcib, "unable to route slot %d INT%c\n", slot,
1036 return (PCI_INVALID_IRQ);
1039 device_printf(pcib, "slot %d INT%c routed to irq %d\n", slot,
1040 'A' + pin, args.vector);
1041 return (args.vector);