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 #define WARMBOOT_TARGET 0
72 #define WARMBOOT_OFF (KERNBASE + 0x0467)
73 #define WARMBOOT_SEG (KERNBASE + 0x0469)
75 #define BIOS_BASE (0xf0000)
76 #define BIOS_BASE2 (0xe0000)
77 #define BIOS_SIZE (0x10000)
78 #define BIOS_COUNT (BIOS_SIZE/4)
80 #define CMOS_REG (0x70)
81 #define CMOS_DATA (0x71)
82 #define BIOS_RESET (0x0f)
83 #define BIOS_WARM (0x0a)
85 #define PROCENTRY_FLAG_EN 0x01
86 #define PROCENTRY_FLAG_BP 0x02
87 #define IOAPICENTRY_FLAG_EN 0x01
90 /* MP Floating Pointer Structure */
91 typedef struct MPFPS {
104 /* MP Configuration Table Header */
105 typedef struct MPCTH {
107 u_short base_table_length;
111 u_char product_id[12];
112 u_int32_t oem_table_pointer;
113 u_short oem_table_size;
115 u_int32_t apic_address;
116 u_short extended_table_length;
117 u_char extended_table_checksum;
122 typedef struct PROCENTRY {
127 u_int32_t cpu_signature;
128 u_int32_t feature_flags;
133 typedef struct BUSENTRY {
139 typedef struct IOAPICENTRY {
144 u_int32_t apic_address;
145 } *io_apic_entry_ptr;
147 typedef struct INTENTRY {
157 /* descriptions of MP basetable entries */
158 typedef struct BASETABLE_ENTRY {
167 vm_size_t mp_cth_mapsz;
170 #define MPTABLE_POS_USE_DEFAULT(mpt) \
171 ((mpt)->mp_fps->mpfb1 != 0 || (mpt)->mp_cth == NULL)
175 int mb_type; /* MPTABLE_BUS_ */
176 TAILQ_ENTRY(mptable_bus) mb_link;
179 #define MPTABLE_BUS_ISA 0
180 #define MPTABLE_BUS_PCI 1
182 struct mptable_bus_info {
183 TAILQ_HEAD(, mptable_bus) mbi_list;
186 struct mptable_pci_int {
193 TAILQ_ENTRY(mptable_pci_int) mpci_link;
196 struct mptable_ioapic {
202 TAILQ_ENTRY(mptable_ioapic) mio_link;
205 typedef int (*mptable_iter_func)(void *, const void *, int);
208 * this code MUST be enabled here and in mpboot.s.
209 * it follows the very early stages of AP boot by placing values in CMOS ram.
210 * it NORMALLY will never be needed and thus the primitive method for enabling.
213 #if defined(CHECK_POINTS)
214 #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA))
215 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
217 #define CHECK_INIT(D); \
218 CHECK_WRITE(0x34, (D)); \
219 CHECK_WRITE(0x35, (D)); \
220 CHECK_WRITE(0x36, (D)); \
221 CHECK_WRITE(0x37, (D)); \
222 CHECK_WRITE(0x38, (D)); \
223 CHECK_WRITE(0x39, (D));
225 #define CHECK_PRINT(S); \
226 kprintf("%s: %d, %d, %d, %d, %d, %d\n", \
235 #else /* CHECK_POINTS */
237 #define CHECK_INIT(D)
238 #define CHECK_PRINT(S)
240 #endif /* CHECK_POINTS */
243 * Values to send to the POST hardware.
245 #define MP_BOOTADDRESS_POST 0x10
246 #define MP_PROBE_POST 0x11
247 #define MPTABLE_PASS1_POST 0x12
249 #define MP_START_POST 0x13
250 #define MP_ENABLE_POST 0x14
251 #define MPTABLE_PASS2_POST 0x15
253 #define START_ALL_APS_POST 0x16
254 #define INSTALL_AP_TRAMP_POST 0x17
255 #define START_AP_POST 0x18
257 #define MP_ANNOUNCE_POST 0x19
259 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
260 int current_postcode;
262 /** XXX FIXME: what system files declare these??? */
263 extern struct region_descriptor r_gdt, r_idt;
265 int mp_naps; /* # of Applications processors */
268 u_int32_t cpu_apic_versions[NAPICID]; /* populated during mptable scan */
270 extern int64_t tsc_offsets[];
272 extern u_long ebda_addr;
274 #ifdef SMP /* APIC-IO */
275 struct apic_intmapinfo int_to_apicintpin[APIC_INTMAPSIZE];
279 * APIC ID logical/physical mapping structures.
280 * We oversize these to simplify boot-time config.
282 int cpu_num_to_apic_id[NAPICID];
283 int apic_id_to_logical[NAPICID];
285 /* AP uses this during bootstrap. Do not staticize. */
289 struct pcb stoppcbs[MAXCPU];
291 extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
293 static basetable_entry basetable_entry_types[] =
295 {0, 20, "Processor"},
303 * Local data and functions.
306 static u_int boot_address;
307 static u_int base_memory;
308 static int mp_finish;
309 static int mp_finish_lapic;
311 static void mp_enable(u_int boot_addr);
313 static int mptable_iterate_entries(const mpcth_t,
314 mptable_iter_func, void *);
315 static int mptable_search(void);
316 static long mptable_search_sig(u_int32_t target, int count);
317 static int mptable_hyperthread_fixup(cpumask_t, int);
318 static int mptable_map(struct mptable_pos *);
319 static void mptable_unmap(struct mptable_pos *);
320 static void mptable_bus_info_alloc(const mpcth_t,
321 struct mptable_bus_info *);
322 static void mptable_bus_info_free(struct mptable_bus_info *);
324 static int mptable_lapic_probe(struct lapic_enumerator *);
325 static void mptable_lapic_enumerate(struct lapic_enumerator *);
326 static void mptable_lapic_default(void);
328 static int mptable_ioapic_probe(struct ioapic_enumerator *);
329 static void mptable_ioapic_enumerate(struct ioapic_enumerator *);
331 static int start_all_aps(u_int boot_addr);
333 static void install_ap_tramp(u_int boot_addr);
335 static int start_ap(struct mdglobaldata *gd, u_int boot_addr, int smibest);
336 static int smitest(void);
338 static cpumask_t smp_startup_mask = 1; /* which cpus have been started */
339 static cpumask_t smp_lapic_mask = 1; /* which cpus have lapic been inited */
340 cpumask_t smp_active_mask = 1; /* which cpus are ready for IPIs etc? */
341 SYSCTL_INT(_machdep, OID_AUTO, smp_active, CTLFLAG_RD, &smp_active_mask, 0, "");
342 static u_int bootMP_size;
346 static vm_paddr_t mptable_fps_phyaddr;
347 static int mptable_use_default;
348 static TAILQ_HEAD(mptable_pci_int_list, mptable_pci_int) mptable_pci_int_list =
349 TAILQ_HEAD_INITIALIZER(mptable_pci_int_list);
350 static TAILQ_HEAD(mptable_ioapic_list, mptable_ioapic) mptable_ioapic_list =
351 TAILQ_HEAD_INITIALIZER(mptable_ioapic_list);
354 * Calculate usable address in base memory for AP trampoline code.
357 mp_bootaddress(u_int basemem)
359 POSTCODE(MP_BOOTADDRESS_POST);
361 base_memory = basemem;
363 bootMP_size = mptramp_end - mptramp_start;
364 boot_address = trunc_page(basemem * 1024); /* round down to 4k boundary */
365 if (((basemem * 1024) - boot_address) < bootMP_size)
366 boot_address -= PAGE_SIZE; /* not enough, lower by 4k */
367 /* 3 levels of page table pages */
368 mptramp_pagetables = boot_address - (PAGE_SIZE * 3);
370 return mptramp_pagetables;
377 struct mptable_pos mpt;
380 KKASSERT(mptable_fps_phyaddr == 0);
382 mptable_fps_phyaddr = mptable_search();
383 if (mptable_fps_phyaddr == 0)
386 error = mptable_map(&mpt);
388 mptable_fps_phyaddr = 0;
392 if (MPTABLE_POS_USE_DEFAULT(&mpt)) {
393 kprintf("MPTABLE: use default configuration\n");
394 mptable_use_default = 1;
396 if (mpt.mp_fps->mpfb2 & 0x80)
401 SYSINIT(mptable_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, mptable_probe, 0);
404 * Look for an Intel MP spec table (ie, SMP capable hardware).
412 POSTCODE(MP_PROBE_POST);
414 /* see if EBDA exists */
415 if (ebda_addr != 0) {
416 /* search first 1K of EBDA */
417 target = (u_int32_t)ebda_addr;
418 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
421 /* last 1K of base memory, effective 'top of base' passed in */
422 target = (u_int32_t)(base_memory - 0x400);
423 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
427 /* search the BIOS */
428 target = (u_int32_t)BIOS_BASE;
429 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
432 /* search the extended BIOS */
433 target = (u_int32_t)BIOS_BASE2;
434 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
442 mptable_iterate_entries(const mpcth_t cth, mptable_iter_func func, void *arg)
444 int count, total_size;
445 const void *position;
447 KKASSERT(cth->base_table_length >= sizeof(struct MPCTH));
448 total_size = cth->base_table_length - sizeof(struct MPCTH);
449 position = (const uint8_t *)cth + sizeof(struct MPCTH);
450 count = cth->entry_count;
455 KKASSERT(total_size >= 0);
456 if (total_size == 0) {
457 kprintf("invalid base MP table, "
458 "entry count and length mismatch\n");
462 type = *(const uint8_t *)position;
464 case 0: /* processor_entry */
465 case 1: /* bus_entry */
466 case 2: /* io_apic_entry */
467 case 3: /* int_entry */
468 case 4: /* int_entry */
471 kprintf("unknown base MP table entry type %d\n", type);
475 if (total_size < basetable_entry_types[type].length) {
476 kprintf("invalid base MP table length, "
477 "does not contain all entries\n");
480 total_size -= basetable_entry_types[type].length;
482 error = func(arg, position, type);
486 position = (const uint8_t *)position +
487 basetable_entry_types[type].length;
494 * Startup the SMP processors.
499 POSTCODE(MP_START_POST);
500 mp_enable(boot_address);
505 * Print various information about the SMP system hardware and setup.
512 POSTCODE(MP_ANNOUNCE_POST);
514 kprintf("DragonFly/MP: Multiprocessor motherboard\n");
515 kprintf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
516 kprintf(", version: 0x%08x\n", cpu_apic_versions[0]);
517 for (x = 1; x <= mp_naps; ++x) {
518 kprintf(" cpu%d (AP): apic id: %2d", x, CPU_TO_ID(x));
519 kprintf(", version: 0x%08x\n", cpu_apic_versions[x]);
523 kprintf(" Warning: APIC I/O disabled\n");
527 * AP cpu's call this to sync up protected mode.
529 * WARNING! %gs is not set up on entry. This routine sets up %gs.
535 int x, myid = bootAP;
537 struct mdglobaldata *md;
538 struct privatespace *ps;
540 ps = &CPU_prvspace[myid];
542 gdt_segs[GPROC0_SEL].ssd_base =
543 (long) &ps->mdglobaldata.gd_common_tss;
544 ps->mdglobaldata.mi.gd_prvspace = ps;
546 /* We fill the 32-bit segment descriptors */
547 for (x = 0; x < NGDT; x++) {
548 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1))
549 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x]);
551 /* And now a 64-bit one */
552 ssdtosyssd(&gdt_segs[GPROC0_SEL],
553 (struct system_segment_descriptor *)&gdt[myid * NGDT + GPROC0_SEL]);
555 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
556 r_gdt.rd_base = (long) &gdt[myid * NGDT];
557 lgdt(&r_gdt); /* does magic intra-segment return */
559 /* lgdt() destroys the GSBASE value, so we load GSBASE after lgdt() */
560 wrmsr(MSR_FSBASE, 0); /* User value */
561 wrmsr(MSR_GSBASE, (u_int64_t)ps);
562 wrmsr(MSR_KGSBASE, 0); /* XXX User value while we're in the kernel */
568 mdcpu->gd_currentldt = _default_ldt;
571 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
572 gdt[myid * NGDT + GPROC0_SEL].sd_type = SDT_SYSTSS;
574 md = mdcpu; /* loaded through %gs:0 (mdglobaldata.mi.gd_prvspace)*/
576 md->gd_common_tss.tss_rsp0 = 0; /* not used until after switch */
578 md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
580 md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL];
581 md->gd_common_tssd = *md->gd_tss_gdt;
583 /* double fault stack */
584 md->gd_common_tss.tss_ist1 =
585 (long)&md->mi.gd_prvspace->idlestack[
586 sizeof(md->mi.gd_prvspace->idlestack)];
591 * Set to a known state:
592 * Set by mpboot.s: CR0_PG, CR0_PE
593 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
596 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
599 /* Set up the fast syscall stuff */
600 msr = rdmsr(MSR_EFER) | EFER_SCE;
601 wrmsr(MSR_EFER, msr);
602 wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
603 wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
604 msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
605 ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
606 wrmsr(MSR_STAR, msr);
607 wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
609 pmap_set_opt(); /* PSE/4MB pages, etc */
611 /* Initialize the PAT MSR. */
615 /* set up CPU registers and state */
618 /* set up SSE/NX registers */
621 /* set up FPU state on the AP */
622 npxinit(__INITIAL_NPXCW__);
624 /* disable the APIC, just to be SURE */
625 lapic->svr &= ~APIC_SVR_ENABLE;
627 /* data returned to BSP */
628 cpu_apic_versions[0] = lapic->version;
631 /*******************************************************************
632 * local functions and data
636 * start the SMP system
639 mp_enable(u_int boot_addr)
641 POSTCODE(MP_ENABLE_POST);
645 /* Initialize BSP's local APIC */
648 /* start each Application Processor */
649 start_all_aps(boot_addr);
655 MachIntrABI.finalize();
660 * look for the MP spec signature
663 /* string defined by the Intel MP Spec as identifying the MP table */
664 #define MP_SIG 0x5f504d5f /* _MP_ */
665 #define NEXT(X) ((X) += 4)
667 mptable_search_sig(u_int32_t target, int count)
673 KKASSERT(target != 0);
675 map_size = count * sizeof(u_int32_t);
676 addr = pmap_mapdev((vm_paddr_t)target, map_size);
679 for (x = 0; x < count; NEXT(x)) {
680 if (addr[x] == MP_SIG) {
681 /* make array index a byte index */
682 ret = target + (x * sizeof(u_int32_t));
687 pmap_unmapdev((vm_offset_t)addr, map_size);
691 static int processor_entry (const struct PROCENTRY *entry, int cpu);
694 * Check if we should perform a hyperthreading "fix-up" to
695 * enumerate any logical CPU's that aren't already listed
698 * XXX: We assume that all of the physical CPUs in the
699 * system have the same number of logical CPUs.
701 * XXX: We assume that APIC ID's are allocated such that
702 * the APIC ID's for a physical processor are aligned
703 * with the number of logical CPU's in the processor.
706 mptable_hyperthread_fixup(cpumask_t id_mask, int cpu_count)
708 int i, id, lcpus_max, logical_cpus;
710 if ((cpu_feature & CPUID_HTT) == 0)
713 lcpus_max = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
717 if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
719 * INSTRUCTION SET REFERENCE, A-M (#253666)
720 * Page 3-181, Table 3-20
721 * "The nearest power-of-2 integer that is not smaller
722 * than EBX[23:16] is the number of unique initial APIC
723 * IDs reserved for addressing different logical
724 * processors in a physical package."
727 if ((1 << i) >= lcpus_max) {
734 KKASSERT(cpu_count != 0);
735 if (cpu_count == lcpus_max) {
736 /* We have nothing to fix */
738 } else if (cpu_count == 1) {
739 /* XXX this may be incorrect */
740 logical_cpus = lcpus_max;
745 * Calculate the distances between two nearest
746 * APIC IDs. If all such distances are same,
747 * then it is the number of missing cpus that
748 * we are going to fill later.
750 dist = cur = prev = -1;
751 for (id = 0; id < MAXCPU; ++id) {
752 if ((id_mask & CPUMASK(id)) == 0)
757 int new_dist = cur - prev;
763 * Make sure that all distances
764 * between two nearest APIC IDs
767 if (dist != new_dist)
775 /* Must be power of 2 */
776 if (dist & (dist - 1))
779 /* Can't exceed CPU package capacity */
780 if (dist > lcpus_max)
781 logical_cpus = lcpus_max;
787 * For each APIC ID of a CPU that is set in the mask,
788 * scan the other candidate APIC ID's for this
789 * physical processor. If any of those ID's are
790 * already in the table, then kill the fixup.
792 for (id = 0; id < MAXCPU; id++) {
793 if ((id_mask & CPUMASK(id)) == 0)
795 /* First, make sure we are on a logical_cpus boundary. */
796 if (id % logical_cpus != 0)
798 for (i = id + 1; i < id + logical_cpus; i++)
799 if ((id_mask & CPUMASK(i)) != 0)
806 mptable_map(struct mptable_pos *mpt)
810 vm_size_t cth_mapsz = 0;
812 KKASSERT(mptable_fps_phyaddr != 0);
814 bzero(mpt, sizeof(*mpt));
816 fps = pmap_mapdev(mptable_fps_phyaddr, sizeof(*fps));
819 * Map configuration table header to get
820 * the base table size
822 cth = pmap_mapdev(fps->pap, sizeof(*cth));
823 cth_mapsz = cth->base_table_length;
824 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
826 if (cth_mapsz < sizeof(*cth)) {
827 kprintf("invalid base MP table length %d\n",
829 pmap_unmapdev((vm_offset_t)fps, sizeof(*fps));
836 cth = pmap_mapdev(fps->pap, cth_mapsz);
841 mpt->mp_cth_mapsz = cth_mapsz;
847 mptable_unmap(struct mptable_pos *mpt)
849 if (mpt->mp_cth != NULL) {
850 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
852 mpt->mp_cth_mapsz = 0;
854 if (mpt->mp_fps != NULL) {
855 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
861 mp_set_cpuids(int cpu_id, int apic_id)
863 CPU_TO_ID(cpu_id) = apic_id;
864 ID_TO_CPU(apic_id) = cpu_id;
868 processor_entry(const struct PROCENTRY *entry, int cpu)
872 /* check for usability */
873 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
876 /* check for BSP flag */
877 if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
878 mp_set_cpuids(0, entry->apic_id);
879 return 0; /* its already been counted */
882 /* add another AP to list, if less than max number of CPUs */
883 else if (cpu < MAXCPU) {
884 mp_set_cpuids(cpu, entry->apic_id);
892 * Map a physical memory address representing I/O into KVA. The I/O
893 * block is assumed not to cross a page boundary.
896 ioapic_map(vm_paddr_t pa)
898 KKASSERT(pa < 0x100000000LL);
900 return pmap_mapdev_uncacheable(pa, PAGE_SIZE);
904 * start each AP in our list
907 start_all_aps(u_int boot_addr)
909 vm_offset_t va = boot_address + KERNBASE;
910 u_int64_t *pt4, *pt3, *pt2;
917 u_long mpbioswarmvec;
918 struct mdglobaldata *gd;
919 struct privatespace *ps;
921 POSTCODE(START_ALL_APS_POST);
923 /* install the AP 1st level boot code */
924 pmap_kenter(va, boot_address);
925 cpu_invlpg((void *)va); /* JG XXX */
926 bcopy(mptramp_start, (void *)va, bootMP_size);
928 /* Locate the page tables, they'll be below the trampoline */
929 pt4 = (u_int64_t *)(uintptr_t)(mptramp_pagetables + KERNBASE);
930 pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t);
931 pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t);
933 /* Create the initial 1GB replicated page tables */
934 for (i = 0; i < 512; i++) {
935 /* Each slot of the level 4 pages points to the same level 3 page */
936 pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE);
937 pt4[i] |= PG_V | PG_RW | PG_U;
939 /* Each slot of the level 3 pages points to the same level 2 page */
940 pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE));
941 pt3[i] |= PG_V | PG_RW | PG_U;
943 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
944 pt2[i] = i * (2 * 1024 * 1024);
945 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
948 /* save the current value of the warm-start vector */
949 mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
950 outb(CMOS_REG, BIOS_RESET);
951 mpbiosreason = inb(CMOS_DATA);
953 /* setup a vector to our boot code */
954 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
955 *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
956 outb(CMOS_REG, BIOS_RESET);
957 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
960 * If we have a TSC we can figure out the SMI interrupt rate.
961 * The SMI does not necessarily use a constant rate. Spend
962 * up to 250ms trying to figure it out.
965 if (cpu_feature & CPUID_TSC) {
966 set_apic_timer(275000);
967 smilast = read_apic_timer();
968 for (x = 0; x < 20 && read_apic_timer(); ++x) {
969 smicount = smitest();
970 if (smibest == 0 || smilast - smicount < smibest)
971 smibest = smilast - smicount;
974 if (smibest > 250000)
977 smibest = smibest * (int64_t)1000000 /
978 get_apic_timer_frequency();
982 kprintf("SMI Frequency (worst case): %d Hz (%d us)\n",
983 1000000 / smibest, smibest);
986 for (x = 1; x <= mp_naps; ++x) {
988 /* This is a bit verbose, it will go away soon. */
990 /* first page of AP's private space */
991 pg = x * x86_64_btop(sizeof(struct privatespace));
993 /* allocate new private data page(s) */
994 gd = (struct mdglobaldata *)kmem_alloc(&kernel_map,
995 MDGLOBALDATA_BASEALLOC_SIZE);
997 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
998 bzero(gd, sizeof(*gd));
999 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
1001 /* prime data page for it to use */
1002 mi_gdinit(&gd->mi, x);
1004 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
1005 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
1007 /* setup a vector to our boot code */
1008 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
1009 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
1010 outb(CMOS_REG, BIOS_RESET);
1011 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
1014 * Setup the AP boot stack
1016 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
1019 /* attempt to start the Application Processor */
1020 CHECK_INIT(99); /* setup checkpoints */
1021 if (!start_ap(gd, boot_addr, smibest)) {
1022 kprintf("\nAP #%d (PHY# %d) failed!\n",
1024 CHECK_PRINT("trace"); /* show checkpoints */
1025 /* better panic as the AP may be running loose */
1026 kprintf("panic y/n? [y] ");
1027 if (cngetc() != 'n')
1030 CHECK_PRINT("trace"); /* show checkpoints */
1032 /* record its version info */
1033 cpu_apic_versions[x] = cpu_apic_versions[0];
1036 /* set ncpus to 1 + highest logical cpu. Not all may have come up */
1039 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
1040 for (shift = 0; (1 << shift) <= ncpus; ++shift)
1043 ncpus2_shift = shift;
1044 ncpus2 = 1 << shift;
1045 ncpus2_mask = ncpus2 - 1;
1047 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
1048 if ((1 << shift) < ncpus)
1050 ncpus_fit = 1 << shift;
1051 ncpus_fit_mask = ncpus_fit - 1;
1053 /* build our map of 'other' CPUs */
1054 mycpu->gd_other_cpus = smp_startup_mask & ~CPUMASK(mycpu->gd_cpuid);
1055 mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * ncpus);
1056 bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
1058 /* fill in our (BSP) APIC version */
1059 cpu_apic_versions[0] = lapic->version;
1061 /* restore the warmstart vector */
1062 *(u_long *) WARMBOOT_OFF = mpbioswarmvec;
1063 outb(CMOS_REG, BIOS_RESET);
1064 outb(CMOS_DATA, mpbiosreason);
1067 * NOTE! The idlestack for the BSP was setup by locore. Finish
1068 * up, clean out the P==V mapping we did earlier.
1073 * Wait all APs to finish initializing LAPIC
1075 mp_finish_lapic = 1;
1077 kprintf("SMP: Waiting APs LAPIC initialization\n");
1078 if (cpu_feature & CPUID_TSC)
1079 tsc0_offset = rdtsc();
1082 while (smp_lapic_mask != smp_startup_mask) {
1084 if (cpu_feature & CPUID_TSC)
1085 tsc0_offset = rdtsc();
1087 while (try_mplock() == 0)
1090 /* number of APs actually started */
1096 * load the 1st level AP boot code into base memory.
1099 /* targets for relocation */
1100 extern void bigJump(void);
1101 extern void bootCodeSeg(void);
1102 extern void bootDataSeg(void);
1103 extern void MPentry(void);
1104 extern u_int MP_GDT;
1105 extern u_int mp_gdtbase;
1110 install_ap_tramp(u_int boot_addr)
1113 int size = *(int *) ((u_long) & bootMP_size);
1114 u_char *src = (u_char *) ((u_long) bootMP);
1115 u_char *dst = (u_char *) boot_addr + KERNBASE;
1116 u_int boot_base = (u_int) bootMP;
1121 POSTCODE(INSTALL_AP_TRAMP_POST);
1123 for (x = 0; x < size; ++x)
1127 * modify addresses in code we just moved to basemem. unfortunately we
1128 * need fairly detailed info about mpboot.s for this to work. changes
1129 * to mpboot.s might require changes here.
1132 /* boot code is located in KERNEL space */
1133 dst = (u_char *) boot_addr + KERNBASE;
1135 /* modify the lgdt arg */
1136 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
1137 *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
1139 /* modify the ljmp target for MPentry() */
1140 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
1141 *dst32 = ((u_int) MPentry - KERNBASE);
1143 /* modify the target for boot code segment */
1144 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
1145 dst8 = (u_int8_t *) (dst16 + 1);
1146 *dst16 = (u_int) boot_addr & 0xffff;
1147 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
1149 /* modify the target for boot data segment */
1150 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
1151 dst8 = (u_int8_t *) (dst16 + 1);
1152 *dst16 = (u_int) boot_addr & 0xffff;
1153 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
1159 * This function starts the AP (application processor) identified
1160 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
1161 * to accomplish this. This is necessary because of the nuances
1162 * of the different hardware we might encounter. It ain't pretty,
1163 * but it seems to work.
1165 * NOTE: eventually an AP gets to ap_init(), which is called just
1166 * before the AP goes into the LWKT scheduler's idle loop.
1169 start_ap(struct mdglobaldata *gd, u_int boot_addr, int smibest)
1173 u_long icr_lo, icr_hi;
1175 POSTCODE(START_AP_POST);
1177 /* get the PHYSICAL APIC ID# */
1178 physical_cpu = CPU_TO_ID(gd->mi.gd_cpuid);
1180 /* calculate the vector */
1181 vector = (boot_addr >> 12) & 0xff;
1183 /* We don't want anything interfering */
1186 /* Make sure the target cpu sees everything */
1190 * Try to detect when a SMI has occurred, wait up to 200ms.
1192 * If a SMI occurs during an AP reset but before we issue
1193 * the STARTUP command, the AP may brick. To work around
1194 * this problem we hold off doing the AP startup until
1195 * after we have detected the SMI. Hopefully another SMI
1196 * will not occur before we finish the AP startup.
1198 * Retries don't seem to help. SMIs have a window of opportunity
1199 * and if USB->legacy keyboard emulation is enabled in the BIOS
1200 * the interrupt rate can be quite high.
1202 * NOTE: Don't worry about the L1 cache load, it might bloat
1203 * ldelta a little but ndelta will be so huge when the SMI
1204 * occurs the detection logic will still work fine.
1207 set_apic_timer(200000);
1212 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
1213 * and running the target CPU. OR this INIT IPI might be latched (P5
1214 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
1217 * see apic/apicreg.h for icr bit definitions.
1219 * TIME CRITICAL CODE, DO NOT DO ANY KPRINTFS IN THE HOT PATH.
1223 * Setup the address for the target AP. We can setup
1224 * icr_hi once and then just trigger operations with
1227 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1228 icr_hi |= (physical_cpu << 24);
1229 icr_lo = lapic->icr_lo & 0xfff00000;
1230 lapic->icr_hi = icr_hi;
1233 * Do an INIT IPI: assert RESET
1235 * Use edge triggered mode to assert INIT
1237 lapic->icr_lo = icr_lo | 0x00004500;
1238 while (lapic->icr_lo & APIC_DELSTAT_MASK)
1242 * The spec calls for a 10ms delay but we may have to use a
1243 * MUCH lower delay to avoid bricking an AP due to a fast SMI
1244 * interrupt. We have other loops here too and dividing by 2
1245 * doesn't seem to be enough even after subtracting 350us,
1246 * so we divide by 4.
1248 * Our minimum delay is 150uS, maximum is 10ms. If no SMI
1249 * interrupt was detected we use the full 10ms.
1253 else if (smibest < 150 * 4 + 350)
1255 else if ((smibest - 350) / 4 < 10000)
1256 u_sleep((smibest - 350) / 4);
1261 * Do an INIT IPI: deassert RESET
1263 * Use level triggered mode to deassert. It is unclear
1264 * why we need to do this.
1266 lapic->icr_lo = icr_lo | 0x00008500;
1267 while (lapic->icr_lo & APIC_DELSTAT_MASK)
1269 u_sleep(150); /* wait 150us */
1272 * Next we do a STARTUP IPI: the previous INIT IPI might still be
1273 * latched, (P5 bug) this 1st STARTUP would then terminate
1274 * immediately, and the previously started INIT IPI would continue. OR
1275 * the previous INIT IPI has already run. and this STARTUP IPI will
1276 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
1279 lapic->icr_lo = icr_lo | 0x00000600 | vector;
1280 while (lapic->icr_lo & APIC_DELSTAT_MASK)
1282 u_sleep(200); /* wait ~200uS */
1285 * Finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
1286 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
1287 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
1288 * recognized after hardware RESET or INIT IPI.
1290 lapic->icr_lo = icr_lo | 0x00000600 | vector;
1291 while (lapic->icr_lo & APIC_DELSTAT_MASK)
1294 /* Resume normal operation */
1297 /* wait for it to start, see ap_init() */
1298 set_apic_timer(5000000);/* == 5 seconds */
1299 while (read_apic_timer()) {
1300 if (smp_startup_mask & CPUMASK(gd->mi.gd_cpuid))
1301 return 1; /* return SUCCESS */
1304 return 0; /* return FAILURE */
1319 while (read_apic_timer()) {
1321 for (count = 0; count < 100; ++count)
1322 ntsc = rdtsc(); /* force loop to occur */
1324 ndelta = ntsc - ltsc;
1325 if (ldelta > ndelta)
1327 if (ndelta > ldelta * 2)
1330 ldelta = ntsc - ltsc;
1333 return(read_apic_timer());
1337 * Synchronously flush the TLB on all other CPU's. The current cpu's
1338 * TLB is not flushed. If the caller wishes to flush the current cpu's
1339 * TLB the caller must call cpu_invltlb() in addition to smp_invltlb().
1341 * NOTE: If for some reason we were unable to start all cpus we cannot
1342 * safely use broadcast IPIs.
1345 static cpumask_t smp_invltlb_req;
1347 #define SMP_INVLTLB_DEBUG
1353 struct mdglobaldata *md = mdcpu;
1354 #ifdef SMP_INVLTLB_DEBUG
1359 crit_enter_gd(&md->mi);
1360 md->gd_invltlb_ret = 0;
1361 ++md->mi.gd_cnt.v_smpinvltlb;
1362 atomic_set_cpumask(&smp_invltlb_req, md->mi.gd_cpumask);
1363 #ifdef SMP_INVLTLB_DEBUG
1366 if (smp_startup_mask == smp_active_mask) {
1367 all_but_self_ipi(XINVLTLB_OFFSET);
1369 selected_apic_ipi(smp_active_mask & ~md->mi.gd_cpumask,
1370 XINVLTLB_OFFSET, APIC_DELMODE_FIXED);
1373 #ifdef SMP_INVLTLB_DEBUG
1375 kprintf("smp_invltlb: ipi sent\n");
1377 while ((md->gd_invltlb_ret & smp_active_mask & ~md->mi.gd_cpumask) !=
1378 (smp_active_mask & ~md->mi.gd_cpumask)) {
1381 #ifdef SMP_INVLTLB_DEBUG
1383 if (++count == 400000000) {
1384 print_backtrace(-1);
1385 kprintf("smp_invltlb: endless loop %08lx %08lx, "
1386 "rflags %016jx retry",
1387 (long)md->gd_invltlb_ret,
1388 (long)smp_invltlb_req,
1389 (intmax_t)read_rflags());
1390 __asm __volatile ("sti");
1393 lwkt_process_ipiq();
1395 int bcpu = BSFCPUMASK(~md->gd_invltlb_ret &
1396 ~md->mi.gd_cpumask &
1400 kprintf("bcpu %d\n", bcpu);
1401 xgd = globaldata_find(bcpu);
1402 kprintf("thread %p %s\n", xgd->gd_curthread, xgd->gd_curthread->td_comm);
1405 Debugger("giving up");
1411 atomic_clear_cpumask(&smp_invltlb_req, md->mi.gd_cpumask);
1412 crit_exit_gd(&md->mi);
1419 * Called from Xinvltlb assembly with interrupts disabled. We didn't
1420 * bother to bump the critical section count or nested interrupt count
1421 * so only do very low level operations here.
1424 smp_invltlb_intr(void)
1426 struct mdglobaldata *md = mdcpu;
1427 struct mdglobaldata *omd;
1432 mask = smp_invltlb_req;
1435 cpu = BSFCPUMASK(mask);
1436 mask &= ~CPUMASK(cpu);
1437 omd = (struct mdglobaldata *)globaldata_find(cpu);
1438 atomic_set_cpumask(&omd->gd_invltlb_ret, md->mi.gd_cpumask);
1445 * When called the executing CPU will send an IPI to all other CPUs
1446 * requesting that they halt execution.
1448 * Usually (but not necessarily) called with 'other_cpus' as its arg.
1450 * - Signals all CPUs in map to stop.
1451 * - Waits for each to stop.
1458 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
1459 * from executing at same time.
1462 stop_cpus(cpumask_t map)
1464 map &= smp_active_mask;
1466 /* send the Xcpustop IPI to all CPUs in map */
1467 selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED);
1469 while ((stopped_cpus & map) != map)
1477 * Called by a CPU to restart stopped CPUs.
1479 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
1481 * - Signals all CPUs in map to restart.
1482 * - Waits for each to restart.
1490 restart_cpus(cpumask_t map)
1492 /* signal other cpus to restart */
1493 started_cpus = map & smp_active_mask;
1495 while ((stopped_cpus & map) != 0) /* wait for each to clear its bit */
1502 * This is called once the mpboot code has gotten us properly relocated
1503 * and the MMU turned on, etc. ap_init() is actually the idle thread,
1504 * and when it returns the scheduler will call the real cpu_idle() main
1505 * loop for the idlethread. Interrupts are disabled on entry and should
1506 * remain disabled at return.
1514 * Adjust smp_startup_mask to signal the BSP that we have started
1515 * up successfully. Note that we do not yet hold the BGL. The BSP
1516 * is waiting for our signal.
1518 * We can't set our bit in smp_active_mask yet because we are holding
1519 * interrupts physically disabled and remote cpus could deadlock
1520 * trying to send us an IPI.
1522 smp_startup_mask |= CPUMASK(mycpu->gd_cpuid);
1526 * Interlock for LAPIC initialization. Wait until mp_finish_lapic is
1527 * non-zero, then get the MP lock.
1529 * Note: We are in a critical section.
1531 * Note: we are the idle thread, we can only spin.
1533 * Note: The load fence is memory volatile and prevents the compiler
1534 * from improperly caching mp_finish_lapic, and the cpu from improperly
1537 while (mp_finish_lapic == 0)
1539 while (try_mplock() == 0)
1542 if (cpu_feature & CPUID_TSC) {
1544 * The BSP is constantly updating tsc0_offset, figure out
1545 * the relative difference to synchronize ktrdump.
1547 tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc0_offset;
1550 /* BSP may have changed PTD while we're waiting for the lock */
1553 /* Build our map of 'other' CPUs. */
1554 mycpu->gd_other_cpus = smp_startup_mask & ~CPUMASK(mycpu->gd_cpuid);
1556 /* A quick check from sanity claus */
1557 apic_id = (apic_id_to_logical[(lapic->id & 0xff000000) >> 24]);
1558 if (mycpu->gd_cpuid != apic_id) {
1559 kprintf("SMP: cpuid = %d\n", mycpu->gd_cpuid);
1560 kprintf("SMP: apic_id = %d lapicid %d\n",
1561 apic_id, (lapic->id & 0xff000000) >> 24);
1563 kprintf("PTD[MPPTDI] = %p\n", (void *)PTD[MPPTDI]);
1565 panic("cpuid mismatch! boom!!");
1568 /* Initialize AP's local APIC for irq's */
1571 /* LAPIC initialization is done */
1572 smp_lapic_mask |= CPUMASK(mycpu->gd_cpuid);
1575 /* Let BSP move onto the next initialization stage */
1579 * Interlock for finalization. Wait until mp_finish is non-zero,
1580 * then get the MP lock.
1582 * Note: We are in a critical section.
1584 * Note: we are the idle thread, we can only spin.
1586 * Note: The load fence is memory volatile and prevents the compiler
1587 * from improperly caching mp_finish, and the cpu from improperly
1590 while (mp_finish == 0)
1592 while (try_mplock() == 0)
1595 /* BSP may have changed PTD while we're waiting for the lock */
1598 /* Set memory range attributes for this CPU to match the BSP */
1599 mem_range_AP_init();
1602 * Once we go active we must process any IPIQ messages that may
1603 * have been queued, because no actual IPI will occur until we
1604 * set our bit in the smp_active_mask. If we don't the IPI
1605 * message interlock could be left set which would also prevent
1608 * The idle loop doesn't expect the BGL to be held and while
1609 * lwkt_switch() normally cleans things up this is a special case
1610 * because we returning almost directly into the idle loop.
1612 * The idle thread is never placed on the runq, make sure
1613 * nothing we've done put it there.
1615 KKASSERT(get_mplock_count(curthread) == 1);
1616 smp_active_mask |= CPUMASK(mycpu->gd_cpuid);
1619 * Enable interrupts here. idle_restore will also do it, but
1620 * doing it here lets us clean up any strays that got posted to
1621 * the CPU during the AP boot while we are still in a critical
1624 __asm __volatile("sti; pause; pause"::);
1625 bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
1627 initclocks_pcpu(); /* clock interrupts (via IPIs) */
1628 lwkt_process_ipiq();
1631 * Releasing the mp lock lets the BSP finish up the SMP init
1634 KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
1638 * Get SMP fully working before we start initializing devices.
1646 kprintf("Finish MP startup\n");
1648 while (smp_active_mask != smp_startup_mask)
1650 while (try_mplock() == 0)
1653 kprintf("Active CPU Mask: %016jx\n",
1654 (uintmax_t)smp_active_mask);
1658 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
1661 cpu_send_ipiq(int dcpu)
1663 if (CPUMASK(dcpu) & smp_active_mask)
1664 single_apic_ipi(dcpu, XIPIQ_OFFSET, APIC_DELMODE_FIXED);
1667 #if 0 /* single_apic_ipi_passive() not working yet */
1669 * Returns 0 on failure, 1 on success
1672 cpu_send_ipiq_passive(int dcpu)
1675 if (CPUMASK(dcpu) & smp_active_mask) {
1676 r = single_apic_ipi_passive(dcpu, XIPIQ_OFFSET,
1677 APIC_DELMODE_FIXED);
1684 mptable_bus_info_callback(void *xarg, const void *pos, int type)
1686 struct mptable_bus_info *bus_info = xarg;
1687 const struct BUSENTRY *ent;
1688 struct mptable_bus *bus;
1694 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
1695 if (bus->mb_id == ent->bus_id) {
1696 kprintf("mptable_bus_info_alloc: duplicated bus id "
1697 "(%d)\n", bus->mb_id);
1703 if (strncmp(ent->bus_type, "PCI", 3) == 0) {
1704 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
1705 bus->mb_type = MPTABLE_BUS_PCI;
1706 } else if (strncmp(ent->bus_type, "ISA", 3) == 0) {
1707 bus = kmalloc(sizeof(*bus), M_TEMP, M_WAITOK | M_ZERO);
1708 bus->mb_type = MPTABLE_BUS_ISA;
1712 bus->mb_id = ent->bus_id;
1713 TAILQ_INSERT_TAIL(&bus_info->mbi_list, bus, mb_link);
1719 mptable_bus_info_alloc(const mpcth_t cth, struct mptable_bus_info *bus_info)
1723 bzero(bus_info, sizeof(*bus_info));
1724 TAILQ_INIT(&bus_info->mbi_list);
1726 error = mptable_iterate_entries(cth, mptable_bus_info_callback, bus_info);
1728 mptable_bus_info_free(bus_info);
1732 mptable_bus_info_free(struct mptable_bus_info *bus_info)
1734 struct mptable_bus *bus;
1736 while ((bus = TAILQ_FIRST(&bus_info->mbi_list)) != NULL) {
1737 TAILQ_REMOVE(&bus_info->mbi_list, bus, mb_link);
1742 struct mptable_lapic_cbarg1 {
1745 u_int ht_apicid_mask;
1749 mptable_lapic_pass1_callback(void *xarg, const void *pos, int type)
1751 const struct PROCENTRY *ent;
1752 struct mptable_lapic_cbarg1 *arg = xarg;
1758 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
1762 if (ent->apic_id < 32) {
1763 arg->ht_apicid_mask |= 1 << ent->apic_id;
1764 } else if (arg->ht_fixup) {
1765 kprintf("MPTABLE: lapic id > 32, disable HTT fixup\n");
1771 struct mptable_lapic_cbarg2 {
1778 mptable_lapic_pass2_callback(void *xarg, const void *pos, int type)
1780 const struct PROCENTRY *ent;
1781 struct mptable_lapic_cbarg2 *arg = xarg;
1787 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
1788 KKASSERT(!arg->found_bsp);
1792 if (processor_entry(ent, arg->cpu))
1795 if (arg->logical_cpus) {
1796 struct PROCENTRY proc;
1800 * Create fake mptable processor entries
1801 * and feed them to processor_entry() to
1802 * enumerate the logical CPUs.
1804 bzero(&proc, sizeof(proc));
1806 proc.cpu_flags = PROCENTRY_FLAG_EN;
1807 proc.apic_id = ent->apic_id;
1809 for (i = 1; i < arg->logical_cpus; i++) {
1811 processor_entry(&proc, arg->cpu);
1819 mptable_lapic_default(void)
1821 int ap_apicid, bsp_apicid;
1823 mp_naps = 1; /* exclude BSP */
1825 /* Map local apic before the id field is accessed */
1826 lapic_map(DEFAULT_APIC_BASE);
1828 bsp_apicid = APIC_ID(lapic->id);
1829 ap_apicid = (bsp_apicid == 0) ? 1 : 0;
1832 mp_set_cpuids(0, bsp_apicid);
1833 /* one and only AP */
1834 mp_set_cpuids(1, ap_apicid);
1840 * ID_TO_CPU(N), APIC ID to logical CPU table
1841 * CPU_TO_ID(N), logical CPU to APIC ID table
1844 mptable_lapic_enumerate(struct lapic_enumerator *e)
1846 struct mptable_pos mpt;
1847 struct mptable_lapic_cbarg1 arg1;
1848 struct mptable_lapic_cbarg2 arg2;
1850 int error, logical_cpus = 0;
1851 vm_offset_t lapic_addr;
1853 if (mptable_use_default) {
1854 mptable_lapic_default();
1858 error = mptable_map(&mpt);
1860 panic("mptable_lapic_enumerate mptable_map failed\n");
1861 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1865 /* Save local apic address */
1866 lapic_addr = (vm_offset_t)cth->apic_address;
1867 KKASSERT(lapic_addr != 0);
1870 * Find out how many CPUs do we have
1872 bzero(&arg1, sizeof(arg1));
1873 arg1.ht_fixup = 1; /* Apply ht fixup by default */
1875 error = mptable_iterate_entries(cth,
1876 mptable_lapic_pass1_callback, &arg1);
1878 panic("mptable_iterate_entries(lapic_pass1) failed\n");
1879 KKASSERT(arg1.cpu_count != 0);
1881 /* See if we need to fixup HT logical CPUs. */
1882 if (arg1.ht_fixup) {
1883 logical_cpus = mptable_hyperthread_fixup(arg1.ht_apicid_mask,
1885 if (logical_cpus != 0)
1886 arg1.cpu_count *= logical_cpus;
1888 mp_naps = arg1.cpu_count;
1890 /* Qualify the numbers again, after possible HT fixup */
1891 if (mp_naps > MAXCPU) {
1892 kprintf("Warning: only using %d of %d available CPUs!\n",
1898 --mp_naps; /* subtract the BSP */
1901 * Link logical CPU id to local apic id
1903 bzero(&arg2, sizeof(arg2));
1905 arg2.logical_cpus = logical_cpus;
1907 error = mptable_iterate_entries(cth,
1908 mptable_lapic_pass2_callback, &arg2);
1910 panic("mptable_iterate_entries(lapic_pass2) failed\n");
1911 KKASSERT(arg2.found_bsp);
1913 /* Map local apic */
1914 lapic_map(lapic_addr);
1916 mptable_unmap(&mpt);
1919 struct mptable_lapic_probe_cbarg {
1925 mptable_lapic_probe_callback(void *xarg, const void *pos, int type)
1927 const struct PROCENTRY *ent;
1928 struct mptable_lapic_probe_cbarg *arg = xarg;
1934 if ((ent->cpu_flags & PROCENTRY_FLAG_EN) == 0)
1938 if (ent->cpu_flags & PROCENTRY_FLAG_BP) {
1939 if (arg->found_bsp) {
1940 kprintf("more than one BSP in base MP table\n");
1949 mptable_lapic_probe(struct lapic_enumerator *e)
1951 struct mptable_pos mpt;
1952 struct mptable_lapic_probe_cbarg arg;
1956 if (mptable_fps_phyaddr == 0)
1959 if (mptable_use_default)
1962 error = mptable_map(&mpt);
1965 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
1970 if (cth->apic_address == 0)
1973 bzero(&arg, sizeof(arg));
1974 error = mptable_iterate_entries(cth,
1975 mptable_lapic_probe_callback, &arg);
1977 if (arg.cpu_count == 0) {
1978 kprintf("MP table contains no processor entries\n");
1980 } else if (!arg.found_bsp) {
1981 kprintf("MP table does not contains BSP entry\n");
1986 mptable_unmap(&mpt);
1990 static struct lapic_enumerator mptable_lapic_enumerator = {
1991 .lapic_prio = LAPIC_ENUM_PRIO_MPTABLE,
1992 .lapic_probe = mptable_lapic_probe,
1993 .lapic_enumerate = mptable_lapic_enumerate
1997 mptable_lapic_enum_register(void)
1999 lapic_enumerator_register(&mptable_lapic_enumerator);
2001 SYSINIT(mptable_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
2002 mptable_lapic_enum_register, 0);
2005 mptable_ioapic_list_callback(void *xarg, const void *pos, int type)
2007 const struct IOAPICENTRY *ent;
2008 struct mptable_ioapic *nioapic, *ioapic;
2014 if ((ent->apic_flags & IOAPICENTRY_FLAG_EN) == 0)
2017 if (ent->apic_address == 0) {
2018 kprintf("mptable_ioapic_create_list: zero IOAPIC addr\n");
2022 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2023 if (ioapic->mio_apic_id == ent->apic_id) {
2024 kprintf("mptable_ioapic_create_list: duplicated "
2025 "apic id %d\n", ioapic->mio_apic_id);
2028 if (ioapic->mio_addr == ent->apic_address) {
2029 kprintf("mptable_ioapic_create_list: overlapped "
2030 "IOAPIC addr 0x%08x", ioapic->mio_addr);
2035 nioapic = kmalloc(sizeof(*nioapic), M_DEVBUF, M_WAITOK | M_ZERO);
2036 nioapic->mio_apic_id = ent->apic_id;
2037 nioapic->mio_addr = ent->apic_address;
2040 * Create IOAPIC list in ascending order of APIC ID
2042 TAILQ_FOREACH_REVERSE(ioapic, &mptable_ioapic_list,
2043 mptable_ioapic_list, mio_link) {
2044 if (nioapic->mio_apic_id > ioapic->mio_apic_id) {
2045 TAILQ_INSERT_AFTER(&mptable_ioapic_list,
2046 ioapic, nioapic, mio_link);
2051 TAILQ_INSERT_HEAD(&mptable_ioapic_list, nioapic, mio_link);
2057 mptable_ioapic_create_list(void)
2059 struct mptable_ioapic *ioapic;
2060 struct mptable_pos mpt;
2063 if (mptable_fps_phyaddr == 0)
2066 if (mptable_use_default) {
2067 ioapic = kmalloc(sizeof(*ioapic), M_DEVBUF, M_WAITOK | M_ZERO);
2068 ioapic->mio_idx = 0;
2069 ioapic->mio_apic_id = 0; /* NOTE: any value is ok here */
2070 ioapic->mio_addr = 0xfec00000; /* XXX magic number */
2072 TAILQ_INSERT_HEAD(&mptable_ioapic_list, ioapic, mio_link);
2076 error = mptable_map(&mpt);
2078 panic("mptable_ioapic_create_list: mptable_map failed\n");
2079 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
2081 error = mptable_iterate_entries(mpt.mp_cth,
2082 mptable_ioapic_list_callback, NULL);
2084 while ((ioapic = TAILQ_FIRST(&mptable_ioapic_list)) != NULL) {
2085 TAILQ_REMOVE(&mptable_ioapic_list, ioapic, mio_link);
2086 kfree(ioapic, M_DEVBUF);
2092 * Assign index number for each IOAPIC
2095 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2096 ioapic->mio_idx = idx;
2100 mptable_unmap(&mpt);
2102 SYSINIT(mptable_ioapic_list, SI_BOOT2_PRESMP, SI_ORDER_SECOND,
2103 mptable_ioapic_create_list, 0);
2106 mptable_pci_int_callback(void *xarg, const void *pos, int type)
2108 const struct mptable_bus_info *bus_info = xarg;
2109 const struct mptable_ioapic *ioapic;
2110 const struct mptable_bus *bus;
2111 struct mptable_pci_int *pci_int;
2112 const struct INTENTRY *ent;
2113 int pci_pin, pci_dev;
2119 if (ent->int_type != 0)
2122 TAILQ_FOREACH(bus, &bus_info->mbi_list, mb_link) {
2123 if (bus->mb_type == MPTABLE_BUS_PCI &&
2124 bus->mb_id == ent->src_bus_id)
2130 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2131 if (ioapic->mio_apic_id == ent->dst_apic_id)
2134 if (ioapic == NULL) {
2135 kprintf("MPTABLE: warning PCI int dst apic id %d "
2136 "does not exist\n", ent->dst_apic_id);
2140 pci_pin = ent->src_bus_irq & 0x3;
2141 pci_dev = (ent->src_bus_irq >> 2) & 0x1f;
2143 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
2144 if (pci_int->mpci_bus == ent->src_bus_id &&
2145 pci_int->mpci_dev == pci_dev &&
2146 pci_int->mpci_pin == pci_pin) {
2147 if (pci_int->mpci_ioapic_idx == ioapic->mio_idx &&
2148 pci_int->mpci_ioapic_pin == ent->dst_apic_int) {
2149 kprintf("MPTABLE: warning duplicated "
2150 "PCI int entry for "
2151 "bus %d, dev %d, pin %d\n",
2157 kprintf("mptable_pci_int_register: "
2158 "conflict PCI int entry for "
2159 "bus %d, dev %d, pin %d, "
2160 "IOAPIC %d.%d -> %d.%d\n",
2164 pci_int->mpci_ioapic_idx,
2165 pci_int->mpci_ioapic_pin,
2173 pci_int = kmalloc(sizeof(*pci_int), M_DEVBUF, M_WAITOK | M_ZERO);
2175 pci_int->mpci_bus = ent->src_bus_id;
2176 pci_int->mpci_dev = pci_dev;
2177 pci_int->mpci_pin = pci_pin;
2178 pci_int->mpci_ioapic_idx = ioapic->mio_idx;
2179 pci_int->mpci_ioapic_pin = ent->dst_apic_int;
2181 TAILQ_INSERT_TAIL(&mptable_pci_int_list, pci_int, mpci_link);
2187 mptable_pci_int_register(void)
2189 struct mptable_bus_info bus_info;
2190 const struct mptable_bus *bus;
2191 struct mptable_pci_int *pci_int;
2192 struct mptable_pos mpt;
2193 int error, force_pci0, npcibus;
2196 if (mptable_fps_phyaddr == 0)
2199 if (mptable_use_default)
2202 if (TAILQ_EMPTY(&mptable_ioapic_list))
2205 error = mptable_map(&mpt);
2207 panic("mptable_pci_int_register: mptable_map failed\n");
2208 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
2212 mptable_bus_info_alloc(cth, &bus_info);
2213 if (TAILQ_EMPTY(&bus_info.mbi_list))
2218 TAILQ_FOREACH(bus, &bus_info.mbi_list, mb_link) {
2219 if (bus->mb_type == MPTABLE_BUS_PCI)
2223 mptable_bus_info_free(&bus_info);
2225 } else if (npcibus == 1) {
2229 error = mptable_iterate_entries(cth,
2230 mptable_pci_int_callback, &bus_info);
2232 mptable_bus_info_free(&bus_info);
2235 while ((pci_int = TAILQ_FIRST(&mptable_pci_int_list)) != NULL) {
2236 TAILQ_REMOVE(&mptable_pci_int_list, pci_int, mpci_link);
2237 kfree(pci_int, M_DEVBUF);
2243 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link)
2244 pci_int->mpci_bus = 0;
2247 mptable_unmap(&mpt);
2249 SYSINIT(mptable_pci, SI_BOOT2_PRESMP, SI_ORDER_ANY,
2250 mptable_pci_int_register, 0);
2252 struct mptable_ioapic_probe_cbarg {
2253 const struct mptable_bus_info *bus_info;
2257 mptable_ioapic_probe_callback(void *xarg, const void *pos, int type)
2259 struct mptable_ioapic_probe_cbarg *arg = xarg;
2260 const struct mptable_ioapic *ioapic;
2261 const struct mptable_bus *bus;
2262 const struct INTENTRY *ent;
2268 if (ent->int_type != 0)
2271 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
2272 if (bus->mb_type == MPTABLE_BUS_ISA &&
2273 bus->mb_id == ent->src_bus_id)
2279 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2280 if (ioapic->mio_apic_id == ent->dst_apic_id)
2283 if (ioapic == NULL) {
2284 kprintf("MPTABLE: warning ISA int dst apic id %d "
2285 "does not exist\n", ent->dst_apic_id);
2289 /* XXX magic number */
2290 if (ent->src_bus_irq >= 16) {
2291 kprintf("mptable_ioapic_probe: invalid ISA irq (%d)\n",
2299 mptable_ioapic_probe(struct ioapic_enumerator *e)
2301 struct mptable_ioapic_probe_cbarg arg;
2302 struct mptable_bus_info bus_info;
2303 struct mptable_pos mpt;
2307 if (mptable_fps_phyaddr == 0)
2310 if (mptable_use_default)
2313 if (TAILQ_EMPTY(&mptable_ioapic_list))
2316 error = mptable_map(&mpt);
2318 panic("mptable_ioapic_probe: mptable_map failed\n");
2319 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
2323 mptable_bus_info_alloc(cth, &bus_info);
2325 bzero(&arg, sizeof(arg));
2326 arg.bus_info = &bus_info;
2328 error = mptable_iterate_entries(cth,
2329 mptable_ioapic_probe_callback, &arg);
2331 mptable_bus_info_free(&bus_info);
2332 mptable_unmap(&mpt);
2337 struct mptable_ioapic_int_cbarg {
2338 const struct mptable_bus_info *bus_info;
2343 mptable_ioapic_int_callback(void *xarg, const void *pos, int type)
2345 struct mptable_ioapic_int_cbarg *arg = xarg;
2346 const struct mptable_ioapic *ioapic;
2347 const struct mptable_bus *bus;
2348 const struct INTENTRY *ent;
2357 if (ent->int_type != 0)
2360 TAILQ_FOREACH(bus, &arg->bus_info->mbi_list, mb_link) {
2361 if (bus->mb_type == MPTABLE_BUS_ISA &&
2362 bus->mb_id == ent->src_bus_id)
2368 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2369 if (ioapic->mio_apic_id == ent->dst_apic_id)
2372 if (ioapic == NULL) {
2373 kprintf("MPTABLE: warning ISA int dst apic id %d "
2374 "does not exist\n", ent->dst_apic_id);
2378 if (ent->dst_apic_int >= ioapic->mio_npin) {
2379 panic("mptable_ioapic_enumerate: invalid I/O APIC "
2380 "pin %d, should be < %d",
2381 ent->dst_apic_int, ioapic->mio_npin);
2383 gsi = ioapic->mio_gsi_base + ent->dst_apic_int;
2385 if (ent->src_bus_irq != gsi) {
2387 kprintf("MPTABLE: INTSRC irq %d -> GSI %d\n",
2388 ent->src_bus_irq, gsi);
2390 ioapic_intsrc(ent->src_bus_irq, gsi,
2391 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
2397 mptable_ioapic_enumerate(struct ioapic_enumerator *e)
2399 struct mptable_bus_info bus_info;
2400 struct mptable_ioapic *ioapic;
2401 struct mptable_pos mpt;
2405 KKASSERT(mptable_fps_phyaddr != 0);
2406 KKASSERT(!TAILQ_EMPTY(&mptable_ioapic_list));
2408 TAILQ_FOREACH(ioapic, &mptable_ioapic_list, mio_link) {
2409 const struct mptable_ioapic *prev_ioapic;
2413 addr = ioapic_map(ioapic->mio_addr);
2415 ver = ioapic_read(addr, IOAPIC_VER);
2416 ioapic->mio_npin = ((ver & IOART_VER_MAXREDIR)
2417 >> MAXREDIRSHIFT) + 1;
2419 prev_ioapic = TAILQ_PREV(ioapic,
2420 mptable_ioapic_list, mio_link);
2421 if (prev_ioapic == NULL) {
2422 ioapic->mio_gsi_base = 0;
2424 ioapic->mio_gsi_base =
2425 prev_ioapic->mio_gsi_base +
2426 prev_ioapic->mio_npin;
2428 ioapic_add(addr, ioapic->mio_gsi_base, ioapic->mio_npin);
2431 kprintf("MPTABLE: IOAPIC addr 0x%08x, "
2432 "apic id %d, idx %d, gsi base %d, npin %d\n",
2434 ioapic->mio_apic_id,
2436 ioapic->mio_gsi_base,
2441 if (mptable_use_default) {
2443 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (default)\n");
2444 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
2448 error = mptable_map(&mpt);
2450 panic("mptable_ioapic_probe: mptable_map failed\n");
2451 KKASSERT(!MPTABLE_POS_USE_DEFAULT(&mpt));
2455 mptable_bus_info_alloc(cth, &bus_info);
2457 if (TAILQ_EMPTY(&bus_info.mbi_list)) {
2459 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 (no bus)\n");
2460 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH);
2462 struct mptable_ioapic_int_cbarg arg;
2464 bzero(&arg, sizeof(arg));
2465 arg.bus_info = &bus_info;
2467 error = mptable_iterate_entries(cth,
2468 mptable_ioapic_int_callback, &arg);
2470 panic("mptable_ioapic_int failed\n");
2472 if (arg.ioapic_nint == 0) {
2474 kprintf("MPTABLE: INTSRC irq 0 -> GSI 2 "
2477 ioapic_intsrc(0, 2, INTR_TRIGGER_EDGE,
2478 INTR_POLARITY_HIGH);
2482 mptable_bus_info_free(&bus_info);
2484 mptable_unmap(&mpt);
2487 static struct ioapic_enumerator mptable_ioapic_enumerator = {
2488 .ioapic_prio = IOAPIC_ENUM_PRIO_MPTABLE,
2489 .ioapic_probe = mptable_ioapic_probe,
2490 .ioapic_enumerate = mptable_ioapic_enumerate
2494 mptable_ioapic_enum_register(void)
2496 ioapic_enumerator_register(&mptable_ioapic_enumerator);
2498 SYSINIT(mptable_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
2499 mptable_ioapic_enum_register, 0);
2502 mptable_pci_int_dump(void)
2504 const struct mptable_pci_int *pci_int;
2506 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
2507 kprintf("MPTABLE: %d:%d INT%c -> IOAPIC %d.%d\n",
2510 pci_int->mpci_pin + 'A',
2511 pci_int->mpci_ioapic_idx,
2512 pci_int->mpci_ioapic_pin);
2517 mptable_pci_int_route(int bus, int dev, int pin, int intline)
2519 const struct mptable_pci_int *pci_int;
2523 --pin; /* zero based */
2525 TAILQ_FOREACH(pci_int, &mptable_pci_int_list, mpci_link) {
2526 if (pci_int->mpci_bus == bus &&
2527 pci_int->mpci_dev == dev &&
2528 pci_int->mpci_pin == pin)
2531 if (pci_int != NULL) {
2534 gsi = ioapic_gsi(pci_int->mpci_ioapic_idx,
2535 pci_int->mpci_ioapic_pin);
2537 irq = ioapic_abi_find_gsi(gsi,
2538 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
2544 kprintf("MPTABLE: fixed interrupt routing "
2545 "for %d:%d INT%c\n", bus, dev, pin + 'A');
2548 irq = ioapic_abi_find_irq(intline,
2549 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
2552 if (irq >= 0 && bootverbose) {
2553 kprintf("MPTABLE: %d:%d INT%c routed to irq %d\n",
2554 bus, dev, pin + 'A', irq);