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 $
26 * $DragonFly: src/sys/platform/pc32/i386/mp_machdep.c,v 1.60 2008/06/07 12:03:52 mneumann Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
35 #include <sys/malloc.h>
36 #include <sys/memrange.h>
37 #include <sys/cons.h> /* cngetc() */
38 #include <sys/machintr.h>
41 #include <vm/vm_param.h>
43 #include <vm/vm_kern.h>
44 #include <vm/vm_extern.h>
46 #include <vm/vm_map.h>
52 #include <machine/smp.h>
53 #include <machine_base/apic/apicreg.h>
54 #include <machine/atomic.h>
55 #include <machine/cpufunc.h>
56 #include <machine_base/apic/mpapic.h>
57 #include <machine/psl.h>
58 #include <machine/segments.h>
59 #include <machine/tss.h>
60 #include <machine/specialreg.h>
61 #include <machine/globaldata.h>
63 #include <machine/md_var.h> /* setidt() */
64 #include <machine_base/icu/icu.h> /* IPIs */
65 #include <machine_base/isa/intr_machdep.h> /* IPIs */
67 #define FIXUP_EXTRA_APIC_INTS 8 /* additional entries we may create */
69 #define WARMBOOT_TARGET 0
70 #define WARMBOOT_OFF (KERNBASE + 0x0467)
71 #define WARMBOOT_SEG (KERNBASE + 0x0469)
73 #define BIOS_BASE (0xf0000)
74 #define BIOS_SIZE (0x10000)
75 #define BIOS_COUNT (BIOS_SIZE/4)
77 #define CMOS_REG (0x70)
78 #define CMOS_DATA (0x71)
79 #define BIOS_RESET (0x0f)
80 #define BIOS_WARM (0x0a)
82 #define PROCENTRY_FLAG_EN 0x01
83 #define PROCENTRY_FLAG_BP 0x02
84 #define IOAPICENTRY_FLAG_EN 0x01
87 /* MP Floating Pointer Structure */
88 typedef struct MPFPS {
101 /* MP Configuration Table Header */
102 typedef struct MPCTH {
104 u_short base_table_length;
108 u_char product_id[12];
109 void *oem_table_pointer;
110 u_short oem_table_size;
113 u_short extended_table_length;
114 u_char extended_table_checksum;
119 typedef struct PROCENTRY {
124 u_long cpu_signature;
125 u_long feature_flags;
130 typedef struct BUSENTRY {
136 typedef struct IOAPICENTRY {
142 } *io_apic_entry_ptr;
144 typedef struct INTENTRY {
154 /* descriptions of MP basetable entries */
155 typedef struct BASETABLE_ENTRY {
164 vm_size_t mp_cth_mapsz;
168 * this code MUST be enabled here and in mpboot.s.
169 * it follows the very early stages of AP boot by placing values in CMOS ram.
170 * it NORMALLY will never be needed and thus the primitive method for enabling.
173 #if defined(CHECK_POINTS)
174 #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA))
175 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
177 #define CHECK_INIT(D); \
178 CHECK_WRITE(0x34, (D)); \
179 CHECK_WRITE(0x35, (D)); \
180 CHECK_WRITE(0x36, (D)); \
181 CHECK_WRITE(0x37, (D)); \
182 CHECK_WRITE(0x38, (D)); \
183 CHECK_WRITE(0x39, (D));
185 #define CHECK_PRINT(S); \
186 kprintf("%s: %d, %d, %d, %d, %d, %d\n", \
195 #else /* CHECK_POINTS */
197 #define CHECK_INIT(D)
198 #define CHECK_PRINT(S)
200 #endif /* CHECK_POINTS */
203 * Values to send to the POST hardware.
205 #define MP_BOOTADDRESS_POST 0x10
206 #define MP_PROBE_POST 0x11
207 #define MPTABLE_PASS1_POST 0x12
209 #define MP_START_POST 0x13
210 #define MP_ENABLE_POST 0x14
211 #define MPTABLE_PASS2_POST 0x15
213 #define START_ALL_APS_POST 0x16
214 #define INSTALL_AP_TRAMP_POST 0x17
215 #define START_AP_POST 0x18
217 #define MP_ANNOUNCE_POST 0x19
219 static int need_hyperthreading_fixup;
220 static u_int logical_cpus;
221 u_int logical_cpus_mask;
223 static int madt_probe_test;
224 TUNABLE_INT("hw.madt_probe_test", &madt_probe_test);
226 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
227 int current_postcode;
229 /** XXX FIXME: what system files declare these??? */
230 extern struct region_descriptor r_gdt, r_idt;
232 int bsp_apic_ready = 0; /* flags useability of BSP apic */
233 int mp_naps; /* # of Applications processors */
234 int mp_nbusses; /* # of busses */
236 int mp_napics; /* # of IO APICs */
238 vm_offset_t cpu_apic_address;
240 vm_offset_t io_apic_address[NAPICID]; /* NAPICID is more than enough */
241 u_int32_t *io_apic_versions;
245 u_int32_t cpu_apic_versions[MAXCPU];
247 extern int64_t tsc_offsets[];
249 extern u_long ebda_addr;
252 struct apic_intmapinfo int_to_apicintpin[APIC_INTMAPSIZE];
256 * APIC ID logical/physical mapping structures.
257 * We oversize these to simplify boot-time config.
259 int cpu_num_to_apic_id[NAPICID];
261 int io_num_to_apic_id[NAPICID];
263 int apic_id_to_logical[NAPICID];
265 /* AP uses this during bootstrap. Do not staticize. */
269 /* Hotwire a 0->4MB V==P mapping */
270 extern pt_entry_t *KPTphys;
273 * SMP page table page. Setup by locore to point to a page table
274 * page from which we allocate per-cpu privatespace areas io_apics,
278 #define IO_MAPPING_START_INDEX \
279 (SMP_MAXCPU * sizeof(struct privatespace) / PAGE_SIZE)
281 extern pt_entry_t *SMPpt;
282 static int SMPpt_alloc_index = IO_MAPPING_START_INDEX;
284 struct pcb stoppcbs[MAXCPU];
287 * Local data and functions.
290 static u_int boot_address;
291 static u_int base_memory;
292 static int mp_finish;
294 static void mp_enable(u_int boot_addr);
296 static int mptable_probe(void);
297 static int mptable_search_sig(u_int32_t target, int count);
298 static void mptable_hyperthread_fixup(u_int id_mask);
299 static void mptable_pass1(struct mptable_pos *);
300 static int mptable_pass2(struct mptable_pos *);
301 static void mptable_default(int type);
302 static void mptable_fix(void);
303 static void mptable_map(struct mptable_pos *, vm_paddr_t);
304 static void mptable_unmap(struct mptable_pos *);
307 static void setup_apic_irq_mapping(void);
308 static int apic_int_is_bus_type(int intr, int bus_type);
310 static int start_all_aps(u_int boot_addr);
311 static void install_ap_tramp(u_int boot_addr);
312 static int start_ap(struct mdglobaldata *gd, u_int boot_addr);
314 static cpumask_t smp_startup_mask = 1; /* which cpus have been started */
315 cpumask_t smp_active_mask = 1; /* which cpus are ready for IPIs etc? */
316 SYSCTL_INT(_machdep, OID_AUTO, smp_active, CTLFLAG_RD, &smp_active_mask, 0, "");
319 * Calculate usable address in base memory for AP trampoline code.
322 mp_bootaddress(u_int basemem)
324 POSTCODE(MP_BOOTADDRESS_POST);
326 base_memory = basemem;
328 boot_address = base_memory & ~0xfff; /* round down to 4k boundary */
329 if ((base_memory - boot_address) < bootMP_size)
330 boot_address -= 4096; /* not enough, lower by 4k */
337 * Look for an Intel MP spec table (ie, SMP capable hardware).
346 * Make sure our SMPpt[] page table is big enough to hold all the
349 KKASSERT(IO_MAPPING_START_INDEX < NPTEPG - 2);
351 POSTCODE(MP_PROBE_POST);
353 /* see if EBDA exists */
354 if (ebda_addr != 0) {
355 /* search first 1K of EBDA */
356 target = (u_int32_t)ebda_addr;
357 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
360 /* last 1K of base memory, effective 'top of base' passed in */
361 target = (u_int32_t)(base_memory - 0x400);
362 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
366 /* search the BIOS */
367 target = (u_int32_t)BIOS_BASE;
368 if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
377 * Startup the SMP processors.
382 POSTCODE(MP_START_POST);
383 mp_enable(boot_address);
388 * Print various information about the SMP system hardware and setup.
395 POSTCODE(MP_ANNOUNCE_POST);
397 kprintf("DragonFly/MP: Multiprocessor motherboard\n");
398 kprintf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
399 kprintf(", version: 0x%08x", cpu_apic_versions[0]);
400 kprintf(", at 0x%08x\n", cpu_apic_address);
401 for (x = 1; x <= mp_naps; ++x) {
402 kprintf(" cpu%d (AP): apic id: %2d", x, CPU_TO_ID(x));
403 kprintf(", version: 0x%08x", cpu_apic_versions[x]);
404 kprintf(", at 0x%08x\n", cpu_apic_address);
408 for (x = 0; x < mp_napics; ++x) {
409 kprintf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x));
410 kprintf(", version: 0x%08x", io_apic_versions[x]);
411 kprintf(", at 0x%08x\n", io_apic_address[x]);
414 kprintf(" Warning: APIC I/O disabled\n");
419 * AP cpu's call this to sync up protected mode.
421 * WARNING! We must ensure that the cpu is sufficiently initialized to
422 * be able to use to the FP for our optimized bzero/bcopy code before
423 * we enter more mainstream C code.
425 * WARNING! %fs is not set up on entry. This routine sets up %fs.
431 int x, myid = bootAP;
433 struct mdglobaldata *md;
434 struct privatespace *ps;
436 ps = &CPU_prvspace[myid];
438 gdt_segs[GPRIV_SEL].ssd_base = (int)ps;
439 gdt_segs[GPROC0_SEL].ssd_base =
440 (int) &ps->mdglobaldata.gd_common_tss;
441 ps->mdglobaldata.mi.gd_prvspace = ps;
443 for (x = 0; x < NGDT; x++) {
444 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd);
447 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
448 r_gdt.rd_base = (int) &gdt[myid * NGDT];
449 lgdt(&r_gdt); /* does magic intra-segment return */
454 mdcpu->gd_currentldt = _default_ldt;
456 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
457 gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
459 md = mdcpu; /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
461 md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
462 md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
463 md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
464 md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL].sd;
465 md->gd_common_tssd = *md->gd_tss_gdt;
469 * Set to a known state:
470 * Set by mpboot.s: CR0_PG, CR0_PE
471 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
474 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
476 pmap_set_opt(); /* PSE/4MB pages, etc */
478 /* set up CPU registers and state */
481 /* set up FPU state on the AP */
482 npxinit(__INITIAL_NPXCW__);
484 /* set up SSE registers */
488 /*******************************************************************
489 * local functions and data
493 * start the SMP system
496 mp_enable(u_int boot_addr)
503 vm_paddr_t mpfps_paddr;
505 POSTCODE(MP_ENABLE_POST);
510 mpfps_paddr = mptable_probe();
513 struct mptable_pos mpt;
515 mptable_map(&mpt, mpfps_paddr);
518 * We can safely map physical memory into SMPpt after
519 * mptable_pass1() completes.
523 if (cpu_apic_address == 0)
524 panic("mp_enable: no local apic (mptable)!\n");
527 * Examine the MP table for needed info
529 x = mptable_pass2(&mpt);
533 /* Local apic is mapped on last page */
534 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
535 pmap_get_pgeflag() | (cpu_apic_address & PG_FRAME));
538 * Can't process default configs till the
539 * CPU APIC is pmapped
544 /* Post scan cleanup */
547 vm_paddr_t madt_paddr;
550 madt_paddr = madt_probe();
552 panic("mp_enable: madt_probe failed\n");
554 cpu_apic_address = madt_pass1(madt_paddr);
555 if (cpu_apic_address == 0)
556 panic("mp_enable: no local apic (madt)!\n");
558 /* Local apic is mapped on last page */
559 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
560 pmap_get_pgeflag() | (cpu_apic_address & PG_FRAME));
562 bsp_apic_id = (lapic.id & 0xff000000) >> 24;
563 if (madt_pass2(madt_paddr, bsp_apic_id))
564 panic("mp_enable: madt_pass2 failed\n");
569 setup_apic_irq_mapping();
571 /* fill the LOGICAL io_apic_versions table */
572 for (apic = 0; apic < mp_napics; ++apic) {
573 ux = io_apic_read(apic, IOAPIC_VER);
574 io_apic_versions[apic] = ux;
575 io_apic_set_id(apic, IO_TO_ID(apic));
578 /* program each IO APIC in the system */
579 for (apic = 0; apic < mp_napics; ++apic)
580 if (io_apic_setup(apic) < 0)
581 panic("IO APIC setup failure");
586 * These are required for SMP operation
589 /* install a 'Spurious INTerrupt' vector */
590 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
591 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
593 /* install an inter-CPU IPI for TLB invalidation */
594 setidt(XINVLTLB_OFFSET, Xinvltlb,
595 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
597 /* install an inter-CPU IPI for IPIQ messaging */
598 setidt(XIPIQ_OFFSET, Xipiq,
599 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
601 /* install a timer vector */
602 setidt(XTIMER_OFFSET, Xtimer,
603 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
605 /* install an inter-CPU IPI for CPU stop/restart */
606 setidt(XCPUSTOP_OFFSET, Xcpustop,
607 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
609 /* start each Application Processor */
610 start_all_aps(boot_addr);
615 * look for the MP spec signature
618 /* string defined by the Intel MP Spec as identifying the MP table */
619 #define MP_SIG 0x5f504d5f /* _MP_ */
620 #define NEXT(X) ((X) += 4)
622 mptable_search_sig(u_int32_t target, int count)
628 KKASSERT(target != 0);
630 map_size = count * sizeof(u_int32_t);
631 addr = pmap_mapdev((vm_paddr_t)target, map_size);
634 for (x = 0; x < count; NEXT(x)) {
635 if (addr[x] == MP_SIG) {
636 /* make array index a byte index */
637 ret = target + (x * sizeof(u_int32_t));
642 pmap_unmapdev((vm_offset_t)addr, map_size);
647 static basetable_entry basetable_entry_types[] =
649 {0, 20, "Processor"},
656 typedef struct BUSDATA {
658 enum busTypes bus_type;
661 typedef struct INTDATA {
671 typedef struct BUSTYPENAME {
676 static bus_type_name bus_type_table[] =
682 {UNKNOWN_BUSTYPE, "---"},
685 {UNKNOWN_BUSTYPE, "---"},
686 {UNKNOWN_BUSTYPE, "---"},
687 {UNKNOWN_BUSTYPE, "---"},
688 {UNKNOWN_BUSTYPE, "---"},
689 {UNKNOWN_BUSTYPE, "---"},
691 {UNKNOWN_BUSTYPE, "---"},
692 {UNKNOWN_BUSTYPE, "---"},
693 {UNKNOWN_BUSTYPE, "---"},
694 {UNKNOWN_BUSTYPE, "---"},
696 {UNKNOWN_BUSTYPE, "---"}
698 /* from MP spec v1.4, table 5-1 */
699 static int default_data[7][5] =
701 /* nbus, id0, type0, id1, type1 */
702 {1, 0, ISA, 255, 255},
703 {1, 0, EISA, 255, 255},
704 {1, 0, EISA, 255, 255},
705 {1, 0, MCA, 255, 255},
707 {2, 0, EISA, 1, PCI},
713 static bus_datum *bus_data;
716 /* the IO INT data, one entry per possible APIC INTerrupt */
717 static io_int *io_apic_ints;
721 static int processor_entry (proc_entry_ptr entry, int cpu);
722 static int bus_entry (bus_entry_ptr entry, int bus);
724 static int io_apic_entry (io_apic_entry_ptr entry, int apic);
725 static int int_entry (int_entry_ptr entry, int intr);
727 static int lookup_bus_type (char *name);
731 * 1st pass on motherboard's Intel MP specification table.
734 * cpu_apic_address (common to all CPUs)
740 * need_hyperthreading_fixup
744 mptable_pass1(struct mptable_pos *mpt)
757 POSTCODE(MPTABLE_PASS1_POST);
760 KKASSERT(fps != NULL);
763 /* clear various tables */
764 for (x = 0; x < NAPICID; ++x) {
765 io_apic_address[x] = ~0; /* IO APIC address table */
769 /* init everything to empty */
778 /* check for use of 'default' configuration */
779 if (fps->mpfb1 != 0) {
780 /* use default addresses */
781 cpu_apic_address = DEFAULT_APIC_BASE;
783 io_apic_address[0] = DEFAULT_IO_APIC_BASE;
786 /* fill in with defaults */
787 mp_naps = 2; /* includes BSP */
788 mp_nbusses = default_data[fps->mpfb1 - 1][0];
797 panic("MP Configuration Table Header MISSING!");
799 cpu_apic_address = (vm_offset_t) cth->apic_address;
801 /* walk the table, recording info of interest */
802 totalSize = cth->base_table_length - sizeof(struct MPCTH);
803 position = (u_char *) cth + sizeof(struct MPCTH);
804 count = cth->entry_count;
807 switch (type = *(u_char *) position) {
808 case 0: /* processor_entry */
809 if (((proc_entry_ptr)position)->cpu_flags
810 & PROCENTRY_FLAG_EN) {
813 ((proc_entry_ptr)position)->apic_id;
816 case 1: /* bus_entry */
819 case 2: /* io_apic_entry */
821 if (((io_apic_entry_ptr)position)->apic_flags
822 & IOAPICENTRY_FLAG_EN)
823 io_apic_address[mp_napics++] =
824 (vm_offset_t)((io_apic_entry_ptr)
825 position)->apic_address;
828 case 3: /* int_entry */
833 case 4: /* int_entry */
836 panic("mpfps Base Table HOSED!");
840 totalSize -= basetable_entry_types[type].length;
841 position = (uint8_t *)position +
842 basetable_entry_types[type].length;
846 /* qualify the numbers */
847 if (mp_naps > MAXCPU) {
848 kprintf("Warning: only using %d of %d available CPUs!\n",
853 /* See if we need to fixup HT logical CPUs. */
854 mptable_hyperthread_fixup(id_mask);
856 --mp_naps; /* subtract the BSP */
861 * 2nd pass on motherboard's Intel MP specification table.
865 * ID_TO_IO(N), phy APIC ID to log CPU/IO table
866 * CPU_TO_ID(N), logical CPU to APIC ID table
867 * IO_TO_ID(N), logical IO to APIC ID table
872 mptable_pass2(struct mptable_pos *mpt)
874 struct PROCENTRY proc;
882 int apic, bus, cpu, intr;
885 POSTCODE(MPTABLE_PASS2_POST);
888 KKASSERT(fps != NULL);
890 /* Initialize fake proc entry for use with HT fixup. */
891 bzero(&proc, sizeof(proc));
893 proc.cpu_flags = PROCENTRY_FLAG_EN;
896 MALLOC(io_apic_versions, u_int32_t *, sizeof(u_int32_t) * mp_napics,
898 MALLOC(ioapic, volatile ioapic_t **, sizeof(ioapic_t *) * mp_napics,
899 M_DEVBUF, M_WAITOK | M_ZERO);
900 MALLOC(io_apic_ints, io_int *, sizeof(io_int) * (nintrs + FIXUP_EXTRA_APIC_INTS),
903 MALLOC(bus_data, bus_datum *, sizeof(bus_datum) * mp_nbusses,
907 for (i = 0; i < mp_napics; i++) {
908 ioapic[i] = permanent_io_mapping(io_apic_address[i]);
912 /* clear various tables */
913 for (x = 0; x < NAPICID; ++x) {
914 CPU_TO_ID(x) = -1; /* logical CPU to APIC ID table */
916 ID_TO_IO(x) = -1; /* phy APIC ID to log CPU/IO table */
917 IO_TO_ID(x) = -1; /* logical IO to APIC ID table */
921 /* clear bus data table */
922 for (x = 0; x < mp_nbusses; ++x)
923 bus_data[x].bus_id = 0xff;
926 /* clear IO APIC INT table */
927 for (x = 0; x < (nintrs + 1); ++x) {
928 io_apic_ints[x].int_type = 0xff;
929 io_apic_ints[x].int_vector = 0xff;
933 /* record whether PIC or virtual-wire mode */
934 machintr_setvar_simple(MACHINTR_VAR_IMCR_PRESENT, fps->mpfb2 & 0x80);
936 /* check for use of 'default' configuration */
938 return fps->mpfb1; /* return default configuration type */
942 panic("MP Configuration Table Header MISSING!");
944 /* walk the table, recording info of interest */
945 totalSize = cth->base_table_length - sizeof(struct MPCTH);
946 position = (u_char *) cth + sizeof(struct MPCTH);
947 count = cth->entry_count;
948 apic = bus = intr = 0;
949 cpu = 1; /* pre-count the BSP */
952 switch (type = *(u_char *) position) {
954 if (processor_entry(position, cpu))
957 if (need_hyperthreading_fixup) {
959 * Create fake mptable processor entries
960 * and feed them to processor_entry() to
961 * enumerate the logical CPUs.
963 proc.apic_id = ((proc_entry_ptr)position)->apic_id;
964 for (i = 1; i < logical_cpus; i++) {
966 processor_entry(&proc, cpu);
967 logical_cpus_mask |= (1 << cpu);
973 if (bus_entry(position, bus))
978 if (io_apic_entry(position, apic))
984 if (int_entry(position, intr))
989 /* int_entry(position); */
992 panic("mpfps Base Table HOSED!");
996 totalSize -= basetable_entry_types[type].length;
997 position = (uint8_t *)position + basetable_entry_types[type].length;
1000 if (CPU_TO_ID(0) < 0)
1001 panic("NO BSP found!");
1003 /* report fact that its NOT a default configuration */
1008 * Check if we should perform a hyperthreading "fix-up" to
1009 * enumerate any logical CPU's that aren't already listed
1012 * XXX: We assume that all of the physical CPUs in the
1013 * system have the same number of logical CPUs.
1015 * XXX: We assume that APIC ID's are allocated such that
1016 * the APIC ID's for a physical processor are aligned
1017 * with the number of logical CPU's in the processor.
1020 mptable_hyperthread_fixup(u_int id_mask)
1022 int i, id, lcpus_max;
1024 if ((cpu_feature & CPUID_HTT) == 0)
1027 lcpus_max = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
1031 if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
1033 * INSTRUCTION SET REFERENCE, A-M (#253666)
1034 * Page 3-181, Table 3-20
1035 * "The nearest power-of-2 integer that is not smaller
1036 * than EBX[23:16] is the number of unique initial APIC
1037 * IDs reserved for addressing different logical
1038 * processors in a physical package."
1040 for (i = 0; ; ++i) {
1041 if ((1 << i) >= lcpus_max) {
1048 if (mp_naps == lcpus_max) {
1049 /* We have nothing to fix */
1051 } else if (mp_naps == 1) {
1052 /* XXX this may be incorrect */
1053 logical_cpus = lcpus_max;
1055 int cur, prev, dist;
1058 * Calculate the distances between two nearest
1059 * APIC IDs. If all such distances are same,
1060 * then it is the number of missing cpus that
1061 * we are going to fill later.
1063 dist = cur = prev = -1;
1064 for (id = 0; id < MAXCPU; ++id) {
1065 if ((id_mask & 1 << id) == 0)
1070 int new_dist = cur - prev;
1076 * Make sure that all distances
1077 * between two nearest APIC IDs
1080 if (dist != new_dist)
1088 /* Must be power of 2 */
1089 if (dist & (dist - 1))
1092 /* Can't exceed CPU package capacity */
1093 if (dist > lcpus_max)
1094 logical_cpus = lcpus_max;
1096 logical_cpus = dist;
1100 * For each APIC ID of a CPU that is set in the mask,
1101 * scan the other candidate APIC ID's for this
1102 * physical processor. If any of those ID's are
1103 * already in the table, then kill the fixup.
1105 for (id = 0; id < MAXCPU; id++) {
1106 if ((id_mask & 1 << id) == 0)
1108 /* First, make sure we are on a logical_cpus boundary. */
1109 if (id % logical_cpus != 0)
1111 for (i = id + 1; i < id + logical_cpus; i++)
1112 if ((id_mask & 1 << i) != 0)
1117 * Ok, the ID's checked out, so enable the fixup. We have to fixup
1118 * mp_naps right now.
1120 need_hyperthreading_fixup = 1;
1121 mp_naps *= logical_cpus;
1125 mptable_map(struct mptable_pos *mpt, vm_paddr_t mpfps_paddr)
1129 vm_size_t cth_mapsz = 0;
1131 fps = pmap_mapdev(mpfps_paddr, sizeof(*fps));
1132 if (fps->pap != 0) {
1134 * Map configuration table header to get
1135 * the base table size
1137 cth = pmap_mapdev(fps->pap, sizeof(*cth));
1138 cth_mapsz = cth->base_table_length;
1139 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
1142 * Map the base table
1144 cth = pmap_mapdev(fps->pap, cth_mapsz);
1149 mpt->mp_cth_mapsz = cth_mapsz;
1153 mptable_unmap(struct mptable_pos *mpt)
1155 if (mpt->mp_cth != NULL) {
1156 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
1158 mpt->mp_cth_mapsz = 0;
1160 if (mpt->mp_fps != NULL) {
1161 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
1169 assign_apic_irq(int apic, int intpin, int irq)
1173 if (int_to_apicintpin[irq].ioapic != -1)
1174 panic("assign_apic_irq: inconsistent table");
1176 int_to_apicintpin[irq].ioapic = apic;
1177 int_to_apicintpin[irq].int_pin = intpin;
1178 int_to_apicintpin[irq].apic_address = ioapic[apic];
1179 int_to_apicintpin[irq].redirindex = IOAPIC_REDTBL + 2 * intpin;
1181 for (x = 0; x < nintrs; x++) {
1182 if ((io_apic_ints[x].int_type == 0 ||
1183 io_apic_ints[x].int_type == 3) &&
1184 io_apic_ints[x].int_vector == 0xff &&
1185 io_apic_ints[x].dst_apic_id == IO_TO_ID(apic) &&
1186 io_apic_ints[x].dst_apic_int == intpin)
1187 io_apic_ints[x].int_vector = irq;
1192 revoke_apic_irq(int irq)
1198 if (int_to_apicintpin[irq].ioapic == -1)
1199 panic("revoke_apic_irq: inconsistent table");
1201 oldapic = int_to_apicintpin[irq].ioapic;
1202 oldintpin = int_to_apicintpin[irq].int_pin;
1204 int_to_apicintpin[irq].ioapic = -1;
1205 int_to_apicintpin[irq].int_pin = 0;
1206 int_to_apicintpin[irq].apic_address = NULL;
1207 int_to_apicintpin[irq].redirindex = 0;
1209 for (x = 0; x < nintrs; x++) {
1210 if ((io_apic_ints[x].int_type == 0 ||
1211 io_apic_ints[x].int_type == 3) &&
1212 io_apic_ints[x].int_vector != 0xff &&
1213 io_apic_ints[x].dst_apic_id == IO_TO_ID(oldapic) &&
1214 io_apic_ints[x].dst_apic_int == oldintpin)
1215 io_apic_ints[x].int_vector = 0xff;
1223 allocate_apic_irq(int intr)
1229 if (io_apic_ints[intr].int_vector != 0xff)
1230 return; /* Interrupt handler already assigned */
1232 if (io_apic_ints[intr].int_type != 0 &&
1233 (io_apic_ints[intr].int_type != 3 ||
1234 (io_apic_ints[intr].dst_apic_id == IO_TO_ID(0) &&
1235 io_apic_ints[intr].dst_apic_int == 0)))
1236 return; /* Not INT or ExtInt on != (0, 0) */
1239 while (irq < APIC_INTMAPSIZE &&
1240 int_to_apicintpin[irq].ioapic != -1)
1243 if (irq >= APIC_INTMAPSIZE)
1244 return; /* No free interrupt handlers */
1246 apic = ID_TO_IO(io_apic_ints[intr].dst_apic_id);
1247 intpin = io_apic_ints[intr].dst_apic_int;
1249 assign_apic_irq(apic, intpin, irq);
1250 io_apic_setup_intpin(apic, intpin);
1255 swap_apic_id(int apic, int oldid, int newid)
1262 return; /* Nothing to do */
1264 kprintf("Changing APIC ID for IO APIC #%d from %d to %d in MP table\n",
1265 apic, oldid, newid);
1267 /* Swap physical APIC IDs in interrupt entries */
1268 for (x = 0; x < nintrs; x++) {
1269 if (io_apic_ints[x].dst_apic_id == oldid)
1270 io_apic_ints[x].dst_apic_id = newid;
1271 else if (io_apic_ints[x].dst_apic_id == newid)
1272 io_apic_ints[x].dst_apic_id = oldid;
1275 /* Swap physical APIC IDs in IO_TO_ID mappings */
1276 for (oapic = 0; oapic < mp_napics; oapic++)
1277 if (IO_TO_ID(oapic) == newid)
1280 if (oapic < mp_napics) {
1281 kprintf("Changing APIC ID for IO APIC #%d from "
1282 "%d to %d in MP table\n",
1283 oapic, newid, oldid);
1284 IO_TO_ID(oapic) = oldid;
1286 IO_TO_ID(apic) = newid;
1291 fix_id_to_io_mapping(void)
1295 for (x = 0; x < NAPICID; x++)
1298 for (x = 0; x <= mp_naps; x++)
1299 if (CPU_TO_ID(x) < NAPICID)
1300 ID_TO_IO(CPU_TO_ID(x)) = x;
1302 for (x = 0; x < mp_napics; x++)
1303 if (IO_TO_ID(x) < NAPICID)
1304 ID_TO_IO(IO_TO_ID(x)) = x;
1309 first_free_apic_id(void)
1313 for (freeid = 0; freeid < NAPICID; freeid++) {
1314 for (x = 0; x <= mp_naps; x++)
1315 if (CPU_TO_ID(x) == freeid)
1319 for (x = 0; x < mp_napics; x++)
1320 if (IO_TO_ID(x) == freeid)
1331 io_apic_id_acceptable(int apic, int id)
1333 int cpu; /* Logical CPU number */
1334 int oapic; /* Logical IO APIC number for other IO APIC */
1337 return 0; /* Out of range */
1339 for (cpu = 0; cpu <= mp_naps; cpu++)
1340 if (CPU_TO_ID(cpu) == id)
1341 return 0; /* Conflict with CPU */
1343 for (oapic = 0; oapic < mp_napics && oapic < apic; oapic++)
1344 if (IO_TO_ID(oapic) == id)
1345 return 0; /* Conflict with other APIC */
1347 return 1; /* ID is acceptable for IO APIC */
1352 io_apic_find_int_entry(int apic, int pin)
1356 /* search each of the possible INTerrupt sources */
1357 for (x = 0; x < nintrs; ++x) {
1358 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1359 (pin == io_apic_ints[x].dst_apic_int))
1360 return (&io_apic_ints[x]);
1368 * parse an Intel MP specification table
1376 int apic; /* IO APIC unit number */
1377 int freeid; /* Free physical APIC ID */
1378 int physid; /* Current physical IO APIC ID */
1381 int bus_0 = 0; /* Stop GCC warning */
1382 int bus_pci = 0; /* Stop GCC warning */
1386 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
1387 * did it wrong. The MP spec says that when more than 1 PCI bus
1388 * exists the BIOS must begin with bus entries for the PCI bus and use
1389 * actual PCI bus numbering. This implies that when only 1 PCI bus
1390 * exists the BIOS can choose to ignore this ordering, and indeed many
1391 * MP motherboards do ignore it. This causes a problem when the PCI
1392 * sub-system makes requests of the MP sub-system based on PCI bus
1393 * numbers. So here we look for the situation and renumber the
1394 * busses and associated INTs in an effort to "make it right".
1397 /* find bus 0, PCI bus, count the number of PCI busses */
1398 for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) {
1399 if (bus_data[x].bus_id == 0) {
1402 if (bus_data[x].bus_type == PCI) {
1408 * bus_0 == slot of bus with ID of 0
1409 * bus_pci == slot of last PCI bus encountered
1412 /* check the 1 PCI bus case for sanity */
1413 /* if it is number 0 all is well */
1414 if (num_pci_bus == 1 &&
1415 bus_data[bus_pci].bus_id != 0) {
1417 /* mis-numbered, swap with whichever bus uses slot 0 */
1419 /* swap the bus entry types */
1420 bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type;
1421 bus_data[bus_0].bus_type = PCI;
1424 /* swap each relavant INTerrupt entry */
1425 id = bus_data[bus_pci].bus_id;
1426 for (x = 0; x < nintrs; ++x) {
1427 if (io_apic_ints[x].src_bus_id == id) {
1428 io_apic_ints[x].src_bus_id = 0;
1430 else if (io_apic_ints[x].src_bus_id == 0) {
1431 io_apic_ints[x].src_bus_id = id;
1438 /* Assign IO APIC IDs.
1440 * First try the existing ID. If a conflict is detected, try
1441 * the ID in the MP table. If a conflict is still detected, find
1444 * We cannot use the ID_TO_IO table before all conflicts has been
1445 * resolved and the table has been corrected.
1447 for (apic = 0; apic < mp_napics; ++apic) { /* For all IO APICs */
1449 /* First try to use the value set by the BIOS */
1450 physid = io_apic_get_id(apic);
1451 if (io_apic_id_acceptable(apic, physid)) {
1452 if (IO_TO_ID(apic) != physid)
1453 swap_apic_id(apic, IO_TO_ID(apic), physid);
1457 /* Then check if the value in the MP table is acceptable */
1458 if (io_apic_id_acceptable(apic, IO_TO_ID(apic)))
1461 /* Last resort, find a free APIC ID and use it */
1462 freeid = first_free_apic_id();
1463 if (freeid >= NAPICID)
1464 panic("No free physical APIC IDs found");
1466 if (io_apic_id_acceptable(apic, freeid)) {
1467 swap_apic_id(apic, IO_TO_ID(apic), freeid);
1470 panic("Free physical APIC ID not usable");
1472 fix_id_to_io_mapping();
1476 /* detect and fix broken Compaq MP table */
1477 if (apic_int_type(0, 0) == -1) {
1478 kprintf("APIC_IO: MP table broken: 8259->APIC entry missing!\n");
1479 io_apic_ints[nintrs].int_type = 3; /* ExtInt */
1480 io_apic_ints[nintrs].int_vector = 0xff; /* Unassigned */
1481 /* XXX fixme, set src bus id etc, but it doesn't seem to hurt */
1482 io_apic_ints[nintrs].dst_apic_id = IO_TO_ID(0);
1483 io_apic_ints[nintrs].dst_apic_int = 0; /* Pin 0 */
1485 } else if (apic_int_type(0, 0) == 0) {
1486 kprintf("APIC_IO: MP table broken: ExtINT entry corrupt!\n");
1487 for (x = 0; x < nintrs; ++x)
1488 if ((0 == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1489 (0 == io_apic_ints[x].dst_apic_int)) {
1490 io_apic_ints[x].int_type = 3;
1491 io_apic_ints[x].int_vector = 0xff;
1497 * Fix missing IRQ 15 when IRQ 14 is an ISA interrupt. IDE
1498 * controllers universally come in pairs. If IRQ 14 is specified
1499 * as an ISA interrupt, then IRQ 15 had better be too.
1501 * [ Shuttle XPC / AMD Athlon X2 ]
1502 * The MPTable is missing an entry for IRQ 15. Note that the
1503 * ACPI table has an entry for both 14 and 15.
1505 if (apic_int_type(0, 14) == 0 && apic_int_type(0, 15) == -1) {
1506 kprintf("APIC_IO: MP table broken: IRQ 15 not ISA when IRQ 14 is!\n");
1507 io14 = io_apic_find_int_entry(0, 14);
1508 io_apic_ints[nintrs] = *io14;
1509 io_apic_ints[nintrs].src_bus_irq = 15;
1510 io_apic_ints[nintrs].dst_apic_int = 15;
1518 /* Assign low level interrupt handlers */
1520 setup_apic_irq_mapping(void)
1526 for (x = 0; x < APIC_INTMAPSIZE; x++) {
1527 int_to_apicintpin[x].ioapic = -1;
1528 int_to_apicintpin[x].int_pin = 0;
1529 int_to_apicintpin[x].apic_address = NULL;
1530 int_to_apicintpin[x].redirindex = 0;
1533 /* First assign ISA/EISA interrupts */
1534 for (x = 0; x < nintrs; x++) {
1535 int_vector = io_apic_ints[x].src_bus_irq;
1536 if (int_vector < APIC_INTMAPSIZE &&
1537 io_apic_ints[x].int_vector == 0xff &&
1538 int_to_apicintpin[int_vector].ioapic == -1 &&
1539 (apic_int_is_bus_type(x, ISA) ||
1540 apic_int_is_bus_type(x, EISA)) &&
1541 io_apic_ints[x].int_type == 0) {
1542 assign_apic_irq(ID_TO_IO(io_apic_ints[x].dst_apic_id),
1543 io_apic_ints[x].dst_apic_int,
1548 /* Assign ExtInt entry if no ISA/EISA interrupt 0 entry */
1549 for (x = 0; x < nintrs; x++) {
1550 if (io_apic_ints[x].dst_apic_int == 0 &&
1551 io_apic_ints[x].dst_apic_id == IO_TO_ID(0) &&
1552 io_apic_ints[x].int_vector == 0xff &&
1553 int_to_apicintpin[0].ioapic == -1 &&
1554 io_apic_ints[x].int_type == 3) {
1555 assign_apic_irq(0, 0, 0);
1559 /* PCI interrupt assignment is deferred */
1565 mp_set_cpuids(int cpu_id, int apic_id)
1567 CPU_TO_ID(cpu_id) = apic_id;
1568 ID_TO_CPU(apic_id) = cpu_id;
1572 processor_entry(proc_entry_ptr entry, int cpu)
1576 /* check for usability */
1577 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
1580 if(entry->apic_id >= NAPICID)
1581 panic("CPU APIC ID out of range (0..%d)", NAPICID - 1);
1582 /* check for BSP flag */
1583 if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
1584 mp_set_cpuids(0, entry->apic_id);
1585 return 0; /* its already been counted */
1588 /* add another AP to list, if less than max number of CPUs */
1589 else if (cpu < MAXCPU) {
1590 mp_set_cpuids(cpu, entry->apic_id);
1599 bus_entry(bus_entry_ptr entry, int bus)
1604 /* encode the name into an index */
1605 for (x = 0; x < 6; ++x) {
1606 if ((c = entry->bus_type[x]) == ' ')
1612 if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE)
1613 panic("unknown bus type: '%s'", name);
1615 bus_data[bus].bus_id = entry->bus_id;
1616 bus_data[bus].bus_type = x;
1624 io_apic_entry(io_apic_entry_ptr entry, int apic)
1626 if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN))
1629 IO_TO_ID(apic) = entry->apic_id;
1630 if (entry->apic_id < NAPICID)
1631 ID_TO_IO(entry->apic_id) = apic;
1639 lookup_bus_type(char *name)
1643 for (x = 0; x < MAX_BUSTYPE; ++x)
1644 if (strcmp(bus_type_table[x].name, name) == 0)
1645 return bus_type_table[x].type;
1647 return UNKNOWN_BUSTYPE;
1653 int_entry(int_entry_ptr entry, int intr)
1657 io_apic_ints[intr].int_type = entry->int_type;
1658 io_apic_ints[intr].int_flags = entry->int_flags;
1659 io_apic_ints[intr].src_bus_id = entry->src_bus_id;
1660 io_apic_ints[intr].src_bus_irq = entry->src_bus_irq;
1661 if (entry->dst_apic_id == 255) {
1662 /* This signal goes to all IO APICS. Select an IO APIC
1663 with sufficient number of interrupt pins */
1664 for (apic = 0; apic < mp_napics; apic++)
1665 if (((io_apic_read(apic, IOAPIC_VER) &
1666 IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) >=
1667 entry->dst_apic_int)
1669 if (apic < mp_napics)
1670 io_apic_ints[intr].dst_apic_id = IO_TO_ID(apic);
1672 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1674 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1675 io_apic_ints[intr].dst_apic_int = entry->dst_apic_int;
1681 apic_int_is_bus_type(int intr, int bus_type)
1685 for (bus = 0; bus < mp_nbusses; ++bus)
1686 if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id)
1687 && ((int) bus_data[bus].bus_type == bus_type))
1694 * Given a traditional ISA INT mask, return an APIC mask.
1697 isa_apic_mask(u_int isa_mask)
1702 #if defined(SKIP_IRQ15_REDIRECT)
1703 if (isa_mask == (1 << 15)) {
1704 kprintf("skipping ISA IRQ15 redirect\n");
1707 #endif /* SKIP_IRQ15_REDIRECT */
1709 isa_irq = ffs(isa_mask); /* find its bit position */
1710 if (isa_irq == 0) /* doesn't exist */
1712 --isa_irq; /* make it zero based */
1714 apic_pin = isa_apic_irq(isa_irq); /* look for APIC connection */
1718 return (1 << apic_pin); /* convert pin# to a mask */
1722 * Determine which APIC pin an ISA/EISA INT is attached to.
1724 #define INTTYPE(I) (io_apic_ints[(I)].int_type)
1725 #define INTPIN(I) (io_apic_ints[(I)].dst_apic_int)
1726 #define INTIRQ(I) (io_apic_ints[(I)].int_vector)
1727 #define INTAPIC(I) (ID_TO_IO(io_apic_ints[(I)].dst_apic_id))
1729 #define SRCBUSIRQ(I) (io_apic_ints[(I)].src_bus_irq)
1731 isa_apic_irq(int isa_irq)
1735 for (intr = 0; intr < nintrs; ++intr) { /* check each record */
1736 if (INTTYPE(intr) == 0) { /* standard INT */
1737 if (SRCBUSIRQ(intr) == isa_irq) {
1738 if (apic_int_is_bus_type(intr, ISA) ||
1739 apic_int_is_bus_type(intr, EISA)) {
1740 if (INTIRQ(intr) == 0xff)
1741 return -1; /* unassigned */
1742 return INTIRQ(intr); /* found */
1747 return -1; /* NOT found */
1752 * Determine which APIC pin a PCI INT is attached to.
1754 #define SRCBUSID(I) (io_apic_ints[(I)].src_bus_id)
1755 #define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
1756 #define SRCBUSLINE(I) (io_apic_ints[(I)].src_bus_irq & 0x03)
1758 pci_apic_irq(int pciBus, int pciDevice, int pciInt)
1762 --pciInt; /* zero based */
1764 for (intr = 0; intr < nintrs; ++intr) { /* check each record */
1765 if ((INTTYPE(intr) == 0) /* standard INT */
1766 && (SRCBUSID(intr) == pciBus)
1767 && (SRCBUSDEVICE(intr) == pciDevice)
1768 && (SRCBUSLINE(intr) == pciInt)) { /* a candidate IRQ */
1769 if (apic_int_is_bus_type(intr, PCI)) {
1770 if (INTIRQ(intr) == 0xff)
1771 allocate_apic_irq(intr);
1772 if (INTIRQ(intr) == 0xff)
1773 return -1; /* unassigned */
1774 return INTIRQ(intr); /* exact match */
1779 return -1; /* NOT found */
1783 next_apic_irq(int irq)
1790 for (intr = 0; intr < nintrs; intr++) {
1791 if (INTIRQ(intr) != irq || INTTYPE(intr) != 0)
1793 bus = SRCBUSID(intr);
1794 bustype = apic_bus_type(bus);
1795 if (bustype != ISA &&
1801 if (intr >= nintrs) {
1804 for (ointr = intr + 1; ointr < nintrs; ointr++) {
1805 if (INTTYPE(ointr) != 0)
1807 if (bus != SRCBUSID(ointr))
1809 if (bustype == PCI) {
1810 if (SRCBUSDEVICE(intr) != SRCBUSDEVICE(ointr))
1812 if (SRCBUSLINE(intr) != SRCBUSLINE(ointr))
1815 if (bustype == ISA || bustype == EISA) {
1816 if (SRCBUSIRQ(intr) != SRCBUSIRQ(ointr))
1819 if (INTPIN(intr) == INTPIN(ointr))
1823 if (ointr >= nintrs) {
1826 return INTIRQ(ointr);
1841 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt.
1844 * Exactly what this means is unclear at this point. It is a solution
1845 * for motherboards that redirect the MBIRQ0 pin. Generically a motherboard
1846 * could route any of the ISA INTs to upper (>15) IRQ values. But most would
1847 * NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an
1851 undirect_isa_irq(int rirq)
1855 kprintf("Freeing redirected ISA irq %d.\n", rirq);
1856 /** FIXME: tickle the MB redirector chip */
1860 kprintf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq);
1867 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt
1870 undirect_pci_irq(int rirq)
1874 kprintf("Freeing redirected PCI irq %d.\n", rirq);
1876 /** FIXME: tickle the MB redirector chip */
1880 kprintf("Freeing (NOT implemented) redirected PCI irq %d.\n",
1888 * given a bus ID, return:
1889 * the bus type if found
1893 apic_bus_type(int id)
1897 for (x = 0; x < mp_nbusses; ++x)
1898 if (bus_data[x].bus_id == id)
1899 return bus_data[x].bus_type;
1907 * given a LOGICAL APIC# and pin#, return:
1908 * the associated src bus ID if found
1912 apic_src_bus_id(int apic, int pin)
1916 /* search each of the possible INTerrupt sources */
1917 for (x = 0; x < nintrs; ++x)
1918 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1919 (pin == io_apic_ints[x].dst_apic_int))
1920 return (io_apic_ints[x].src_bus_id);
1922 return -1; /* NOT found */
1926 * given a LOGICAL APIC# and pin#, return:
1927 * the associated src bus IRQ if found
1931 apic_src_bus_irq(int apic, int pin)
1935 for (x = 0; x < nintrs; x++)
1936 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1937 (pin == io_apic_ints[x].dst_apic_int))
1938 return (io_apic_ints[x].src_bus_irq);
1940 return -1; /* NOT found */
1945 * given a LOGICAL APIC# and pin#, return:
1946 * the associated INTerrupt type if found
1950 apic_int_type(int apic, int pin)
1954 /* search each of the possible INTerrupt sources */
1955 for (x = 0; x < nintrs; ++x) {
1956 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1957 (pin == io_apic_ints[x].dst_apic_int))
1958 return (io_apic_ints[x].int_type);
1960 return -1; /* NOT found */
1964 * Return the IRQ associated with an APIC pin
1967 apic_irq(int apic, int pin)
1972 for (x = 0; x < nintrs; ++x) {
1973 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1974 (pin == io_apic_ints[x].dst_apic_int)) {
1975 res = io_apic_ints[x].int_vector;
1978 if (apic != int_to_apicintpin[res].ioapic)
1979 panic("apic_irq: inconsistent table %d/%d", apic, int_to_apicintpin[res].ioapic);
1980 if (pin != int_to_apicintpin[res].int_pin)
1981 panic("apic_irq inconsistent table (2)");
1990 * given a LOGICAL APIC# and pin#, return:
1991 * the associated trigger mode if found
1995 apic_trigger(int apic, int pin)
1999 /* search each of the possible INTerrupt sources */
2000 for (x = 0; x < nintrs; ++x)
2001 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
2002 (pin == io_apic_ints[x].dst_apic_int))
2003 return ((io_apic_ints[x].int_flags >> 2) & 0x03);
2005 return -1; /* NOT found */
2010 * given a LOGICAL APIC# and pin#, return:
2011 * the associated 'active' level if found
2015 apic_polarity(int apic, int pin)
2019 /* search each of the possible INTerrupt sources */
2020 for (x = 0; x < nintrs; ++x)
2021 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
2022 (pin == io_apic_ints[x].dst_apic_int))
2023 return (io_apic_ints[x].int_flags & 0x03);
2025 return -1; /* NOT found */
2031 * set data according to MP defaults
2032 * FIXME: probably not complete yet...
2035 mptable_default(int type)
2037 int ap_cpu_id, boot_cpu_id;
2038 #if defined(APIC_IO)
2041 #endif /* APIC_IO */
2044 kprintf(" MP default config type: %d\n", type);
2047 kprintf(" bus: ISA, APIC: 82489DX\n");
2050 kprintf(" bus: EISA, APIC: 82489DX\n");
2053 kprintf(" bus: EISA, APIC: 82489DX\n");
2056 kprintf(" bus: MCA, APIC: 82489DX\n");
2059 kprintf(" bus: ISA+PCI, APIC: Integrated\n");
2062 kprintf(" bus: EISA+PCI, APIC: Integrated\n");
2065 kprintf(" bus: MCA+PCI, APIC: Integrated\n");
2068 kprintf(" future type\n");
2074 boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24;
2075 ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0;
2078 CPU_TO_ID(0) = boot_cpu_id;
2079 ID_TO_CPU(boot_cpu_id) = 0;
2081 /* one and only AP */
2082 CPU_TO_ID(1) = ap_cpu_id;
2083 ID_TO_CPU(ap_cpu_id) = 1;
2085 #if defined(APIC_IO)
2086 /* one and only IO APIC */
2087 io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24;
2090 * sanity check, refer to MP spec section 3.6.6, last paragraph
2091 * necessary as some hardware isn't properly setting up the IO APIC
2093 #if defined(REALLY_ANAL_IOAPICID_VALUE)
2094 if (io_apic_id != 2) {
2096 if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) {
2097 #endif /* REALLY_ANAL_IOAPICID_VALUE */
2098 io_apic_set_id(0, 2);
2101 IO_TO_ID(0) = io_apic_id;
2102 ID_TO_IO(io_apic_id) = 0;
2103 #endif /* APIC_IO */
2105 /* fill out bus entries */
2114 bus_data[0].bus_id = default_data[type - 1][1];
2115 bus_data[0].bus_type = default_data[type - 1][2];
2116 bus_data[1].bus_id = default_data[type - 1][3];
2117 bus_data[1].bus_type = default_data[type - 1][4];
2120 /* case 4: case 7: MCA NOT supported */
2121 default: /* illegal/reserved */
2122 panic("BAD default MP config: %d", type);
2126 #if defined(APIC_IO)
2127 /* general cases from MP v1.4, table 5-2 */
2128 for (pin = 0; pin < 16; ++pin) {
2129 io_apic_ints[pin].int_type = 0;
2130 io_apic_ints[pin].int_flags = 0x05; /* edge/active-hi */
2131 io_apic_ints[pin].src_bus_id = 0;
2132 io_apic_ints[pin].src_bus_irq = pin; /* IRQ2 caught below */
2133 io_apic_ints[pin].dst_apic_id = io_apic_id;
2134 io_apic_ints[pin].dst_apic_int = pin; /* 1-to-1 */
2137 /* special cases from MP v1.4, table 5-2 */
2139 io_apic_ints[2].int_type = 0xff; /* N/C */
2140 io_apic_ints[13].int_type = 0xff; /* N/C */
2141 #if !defined(APIC_MIXED_MODE)
2143 panic("sorry, can't support type 2 default yet");
2144 #endif /* APIC_MIXED_MODE */
2147 io_apic_ints[2].src_bus_irq = 0; /* ISA IRQ0 is on APIC INT 2 */
2150 io_apic_ints[0].int_type = 0xff; /* N/C */
2152 io_apic_ints[0].int_type = 3; /* vectored 8259 */
2153 #endif /* APIC_IO */
2157 * Map a physical memory address representing I/O into KVA. The I/O
2158 * block is assumed not to cross a page boundary.
2161 permanent_io_mapping(vm_paddr_t pa)
2167 KKASSERT(pa < 0x100000000LL);
2169 pgeflag = 0; /* not used for SMP yet */
2172 * If the requested physical address has already been incidently
2173 * mapped, just use the existing mapping. Otherwise create a new
2176 for (i = IO_MAPPING_START_INDEX; i < SMPpt_alloc_index; ++i) {
2177 if (((vm_offset_t)SMPpt[i] & PG_FRAME) ==
2178 ((vm_offset_t)pa & PG_FRAME)) {
2182 if (i == SMPpt_alloc_index) {
2183 if (i == NPTEPG - 2) {
2184 panic("permanent_io_mapping: We ran out of space"
2187 SMPpt[i] = (pt_entry_t)(PG_V | PG_RW | pgeflag |
2188 ((vm_offset_t)pa & PG_FRAME));
2189 ++SMPpt_alloc_index;
2191 vaddr = (vm_offset_t)CPU_prvspace + (i * PAGE_SIZE) +
2192 ((vm_offset_t)pa & PAGE_MASK);
2193 return ((void *)vaddr);
2197 * start each AP in our list
2200 start_all_aps(u_int boot_addr)
2204 u_char mpbiosreason;
2205 u_long mpbioswarmvec;
2206 struct mdglobaldata *gd;
2207 struct privatespace *ps;
2211 POSTCODE(START_ALL_APS_POST);
2213 /* Initialize BSP's local APIC */
2214 apic_initialize(TRUE);
2217 /* install the AP 1st level boot code */
2218 install_ap_tramp(boot_addr);
2221 /* save the current value of the warm-start vector */
2222 mpbioswarmvec = *((u_long *) WARMBOOT_OFF);
2223 outb(CMOS_REG, BIOS_RESET);
2224 mpbiosreason = inb(CMOS_DATA);
2226 /* set up temporary P==V mapping for AP boot */
2227 /* XXX this is a hack, we should boot the AP on its own stack/PTD */
2228 kptbase = (uintptr_t)(void *)KPTphys;
2229 for (x = 0; x < NKPT; x++) {
2230 PTD[x] = (pd_entry_t)(PG_V | PG_RW |
2231 ((kptbase + x * PAGE_SIZE) & PG_FRAME));
2236 for (x = 1; x <= mp_naps; ++x) {
2238 /* This is a bit verbose, it will go away soon. */
2240 /* first page of AP's private space */
2241 pg = x * i386_btop(sizeof(struct privatespace));
2243 /* allocate new private data page(s) */
2244 gd = (struct mdglobaldata *)kmem_alloc(&kernel_map,
2245 MDGLOBALDATA_BASEALLOC_SIZE);
2246 /* wire it into the private page table page */
2247 for (i = 0; i < MDGLOBALDATA_BASEALLOC_SIZE; i += PAGE_SIZE) {
2248 SMPpt[pg + i / PAGE_SIZE] = (pt_entry_t)
2249 (PG_V | PG_RW | vtophys_pte((char *)gd + i));
2251 pg += MDGLOBALDATA_BASEALLOC_PAGES;
2253 SMPpt[pg + 0] = 0; /* *gd_CMAP1 */
2254 SMPpt[pg + 1] = 0; /* *gd_CMAP2 */
2255 SMPpt[pg + 2] = 0; /* *gd_CMAP3 */
2256 SMPpt[pg + 3] = 0; /* *gd_PMAP1 */
2258 /* allocate and set up an idle stack data page */
2259 stack = (char *)kmem_alloc(&kernel_map, UPAGES*PAGE_SIZE);
2260 for (i = 0; i < UPAGES; i++) {
2261 SMPpt[pg + 4 + i] = (pt_entry_t)
2262 (PG_V | PG_RW | vtophys_pte(PAGE_SIZE * i + stack));
2265 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
2266 bzero(gd, sizeof(*gd));
2267 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
2269 /* prime data page for it to use */
2270 mi_gdinit(&gd->mi, x);
2272 gd->gd_CMAP1 = &SMPpt[pg + 0];
2273 gd->gd_CMAP2 = &SMPpt[pg + 1];
2274 gd->gd_CMAP3 = &SMPpt[pg + 2];
2275 gd->gd_PMAP1 = &SMPpt[pg + 3];
2276 gd->gd_CADDR1 = ps->CPAGE1;
2277 gd->gd_CADDR2 = ps->CPAGE2;
2278 gd->gd_CADDR3 = ps->CPAGE3;
2279 gd->gd_PADDR1 = (unsigned *)ps->PPAGE1;
2280 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
2281 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
2283 /* setup a vector to our boot code */
2284 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
2285 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
2286 outb(CMOS_REG, BIOS_RESET);
2287 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
2290 * Setup the AP boot stack
2292 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
2295 /* attempt to start the Application Processor */
2296 CHECK_INIT(99); /* setup checkpoints */
2297 if (!start_ap(gd, boot_addr)) {
2298 kprintf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x));
2299 CHECK_PRINT("trace"); /* show checkpoints */
2300 /* better panic as the AP may be running loose */
2301 kprintf("panic y/n? [y] ");
2302 if (cngetc() != 'n')
2305 CHECK_PRINT("trace"); /* show checkpoints */
2307 /* record its version info */
2308 cpu_apic_versions[x] = cpu_apic_versions[0];
2311 /* set ncpus to 1 + highest logical cpu. Not all may have come up */
2314 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
2315 for (shift = 0; (1 << shift) <= ncpus; ++shift)
2318 ncpus2_shift = shift;
2319 ncpus2 = 1 << shift;
2320 ncpus2_mask = ncpus2 - 1;
2322 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
2323 if ((1 << shift) < ncpus)
2325 ncpus_fit = 1 << shift;
2326 ncpus_fit_mask = ncpus_fit - 1;
2328 /* build our map of 'other' CPUs */
2329 mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
2330 mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * ncpus);
2331 bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
2333 /* fill in our (BSP) APIC version */
2334 cpu_apic_versions[0] = lapic.version;
2336 /* restore the warmstart vector */
2337 *(u_long *) WARMBOOT_OFF = mpbioswarmvec;
2338 outb(CMOS_REG, BIOS_RESET);
2339 outb(CMOS_DATA, mpbiosreason);
2342 * NOTE! The idlestack for the BSP was setup by locore. Finish
2343 * up, clean out the P==V mapping we did earlier.
2345 for (x = 0; x < NKPT; x++)
2349 /* number of APs actually started */
2355 * load the 1st level AP boot code into base memory.
2358 /* targets for relocation */
2359 extern void bigJump(void);
2360 extern void bootCodeSeg(void);
2361 extern void bootDataSeg(void);
2362 extern void MPentry(void);
2363 extern u_int MP_GDT;
2364 extern u_int mp_gdtbase;
2367 install_ap_tramp(u_int boot_addr)
2370 int size = *(int *) ((u_long) & bootMP_size);
2371 u_char *src = (u_char *) ((u_long) bootMP);
2372 u_char *dst = (u_char *) boot_addr + KERNBASE;
2373 u_int boot_base = (u_int) bootMP;
2378 POSTCODE(INSTALL_AP_TRAMP_POST);
2380 for (x = 0; x < size; ++x)
2384 * modify addresses in code we just moved to basemem. unfortunately we
2385 * need fairly detailed info about mpboot.s for this to work. changes
2386 * to mpboot.s might require changes here.
2389 /* boot code is located in KERNEL space */
2390 dst = (u_char *) boot_addr + KERNBASE;
2392 /* modify the lgdt arg */
2393 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
2394 *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
2396 /* modify the ljmp target for MPentry() */
2397 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
2398 *dst32 = ((u_int) MPentry - KERNBASE);
2400 /* modify the target for boot code segment */
2401 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
2402 dst8 = (u_int8_t *) (dst16 + 1);
2403 *dst16 = (u_int) boot_addr & 0xffff;
2404 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2406 /* modify the target for boot data segment */
2407 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
2408 dst8 = (u_int8_t *) (dst16 + 1);
2409 *dst16 = (u_int) boot_addr & 0xffff;
2410 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2415 * this function starts the AP (application processor) identified
2416 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
2417 * to accomplish this. This is necessary because of the nuances
2418 * of the different hardware we might encounter. It ain't pretty,
2419 * but it seems to work.
2421 * NOTE: eventually an AP gets to ap_init(), which is called just
2422 * before the AP goes into the LWKT scheduler's idle loop.
2425 start_ap(struct mdglobaldata *gd, u_int boot_addr)
2429 u_long icr_lo, icr_hi;
2431 POSTCODE(START_AP_POST);
2433 /* get the PHYSICAL APIC ID# */
2434 physical_cpu = CPU_TO_ID(gd->mi.gd_cpuid);
2436 /* calculate the vector */
2437 vector = (boot_addr >> 12) & 0xff;
2439 /* Make sure the target cpu sees everything */
2443 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
2444 * and running the target CPU. OR this INIT IPI might be latched (P5
2445 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
2449 /* setup the address for the target AP */
2450 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
2451 icr_hi |= (physical_cpu << 24);
2452 lapic.icr_hi = icr_hi;
2454 /* do an INIT IPI: assert RESET */
2455 icr_lo = lapic.icr_lo & 0xfff00000;
2456 lapic.icr_lo = icr_lo | 0x0000c500;
2458 /* wait for pending status end */
2459 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2462 /* do an INIT IPI: deassert RESET */
2463 lapic.icr_lo = icr_lo | 0x00008500;
2465 /* wait for pending status end */
2466 u_sleep(10000); /* wait ~10mS */
2467 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2471 * next we do a STARTUP IPI: the previous INIT IPI might still be
2472 * latched, (P5 bug) this 1st STARTUP would then terminate
2473 * immediately, and the previously started INIT IPI would continue. OR
2474 * the previous INIT IPI has already run. and this STARTUP IPI will
2475 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
2479 /* do a STARTUP IPI */
2480 lapic.icr_lo = icr_lo | 0x00000600 | vector;
2481 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2483 u_sleep(200); /* wait ~200uS */
2486 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
2487 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
2488 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
2489 * recognized after hardware RESET or INIT IPI.
2492 lapic.icr_lo = icr_lo | 0x00000600 | vector;
2493 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2495 u_sleep(200); /* wait ~200uS */
2497 /* wait for it to start, see ap_init() */
2498 set_apic_timer(5000000);/* == 5 seconds */
2499 while (read_apic_timer()) {
2500 if (smp_startup_mask & (1 << gd->mi.gd_cpuid))
2501 return 1; /* return SUCCESS */
2503 return 0; /* return FAILURE */
2508 * Lazy flush the TLB on all other CPU's. DEPRECATED.
2510 * If for some reason we were unable to start all cpus we cannot safely
2511 * use broadcast IPIs.
2517 if (smp_startup_mask == smp_active_mask) {
2518 all_but_self_ipi(XINVLTLB_OFFSET);
2520 selected_apic_ipi(smp_active_mask, XINVLTLB_OFFSET,
2521 APIC_DELMODE_FIXED);
2527 * When called the executing CPU will send an IPI to all other CPUs
2528 * requesting that they halt execution.
2530 * Usually (but not necessarily) called with 'other_cpus' as its arg.
2532 * - Signals all CPUs in map to stop.
2533 * - Waits for each to stop.
2540 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
2541 * from executing at same time.
2544 stop_cpus(u_int map)
2546 map &= smp_active_mask;
2548 /* send the Xcpustop IPI to all CPUs in map */
2549 selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED);
2551 while ((stopped_cpus & map) != map)
2559 * Called by a CPU to restart stopped CPUs.
2561 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
2563 * - Signals all CPUs in map to restart.
2564 * - Waits for each to restart.
2572 restart_cpus(u_int map)
2574 /* signal other cpus to restart */
2575 started_cpus = map & smp_active_mask;
2577 while ((stopped_cpus & map) != 0) /* wait for each to clear its bit */
2584 * This is called once the mpboot code has gotten us properly relocated
2585 * and the MMU turned on, etc. ap_init() is actually the idle thread,
2586 * and when it returns the scheduler will call the real cpu_idle() main
2587 * loop for the idlethread. Interrupts are disabled on entry and should
2588 * remain disabled at return.
2596 * Adjust smp_startup_mask to signal the BSP that we have started
2597 * up successfully. Note that we do not yet hold the BGL. The BSP
2598 * is waiting for our signal.
2600 * We can't set our bit in smp_active_mask yet because we are holding
2601 * interrupts physically disabled and remote cpus could deadlock
2602 * trying to send us an IPI.
2604 smp_startup_mask |= 1 << mycpu->gd_cpuid;
2608 * Interlock for finalization. Wait until mp_finish is non-zero,
2609 * then get the MP lock.
2611 * Note: We are in a critical section.
2613 * Note: We have to synchronize td_mpcount to our desired MP state
2614 * before calling cpu_try_mplock().
2616 * Note: we are the idle thread, we can only spin.
2618 * Note: The load fence is memory volatile and prevents the compiler
2619 * from improperly caching mp_finish, and the cpu from improperly
2622 while (mp_finish == 0)
2624 ++curthread->td_mpcount;
2625 while (cpu_try_mplock() == 0)
2628 if (cpu_feature & CPUID_TSC) {
2630 * The BSP is constantly updating tsc0_offset, figure out the
2631 * relative difference to synchronize ktrdump.
2633 tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc0_offset;
2636 /* BSP may have changed PTD while we're waiting for the lock */
2639 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2643 /* Build our map of 'other' CPUs. */
2644 mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
2646 kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
2648 /* A quick check from sanity claus */
2649 apic_id = (apic_id_to_logical[(lapic.id & 0x0f000000) >> 24]);
2650 if (mycpu->gd_cpuid != apic_id) {
2651 kprintf("SMP: cpuid = %d\n", mycpu->gd_cpuid);
2652 kprintf("SMP: apic_id = %d\n", apic_id);
2653 kprintf("PTD[MPPTDI] = %p\n", (void *)PTD[MPPTDI]);
2654 panic("cpuid mismatch! boom!!");
2657 /* Initialize AP's local APIC for irq's */
2658 apic_initialize(FALSE);
2660 /* Set memory range attributes for this CPU to match the BSP */
2661 mem_range_AP_init();
2664 * Once we go active we must process any IPIQ messages that may
2665 * have been queued, because no actual IPI will occur until we
2666 * set our bit in the smp_active_mask. If we don't the IPI
2667 * message interlock could be left set which would also prevent
2670 * The idle loop doesn't expect the BGL to be held and while
2671 * lwkt_switch() normally cleans things up this is a special case
2672 * because we returning almost directly into the idle loop.
2674 * The idle thread is never placed on the runq, make sure
2675 * nothing we've done put it there.
2677 KKASSERT(curthread->td_mpcount == 1);
2678 smp_active_mask |= 1 << mycpu->gd_cpuid;
2681 * Enable interrupts here. idle_restore will also do it, but
2682 * doing it here lets us clean up any strays that got posted to
2683 * the CPU during the AP boot while we are still in a critical
2686 __asm __volatile("sti; pause; pause"::);
2687 mdcpu->gd_fpending = 0;
2688 mdcpu->gd_ipending = 0;
2690 initclocks_pcpu(); /* clock interrupts (via IPIs) */
2691 lwkt_process_ipiq();
2694 * Releasing the mp lock lets the BSP finish up the SMP init
2697 KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
2701 * Get SMP fully working before we start initializing devices.
2709 kprintf("Finish MP startup\n");
2710 if (cpu_feature & CPUID_TSC)
2711 tsc0_offset = rdtsc();
2714 while (smp_active_mask != smp_startup_mask) {
2716 if (cpu_feature & CPUID_TSC)
2717 tsc0_offset = rdtsc();
2719 while (try_mplock() == 0)
2722 kprintf("Active CPU Mask: %08x\n", smp_active_mask);
2725 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
2728 cpu_send_ipiq(int dcpu)
2730 if ((1 << dcpu) & smp_active_mask)
2731 single_apic_ipi(dcpu, XIPIQ_OFFSET, APIC_DELMODE_FIXED);
2734 #if 0 /* single_apic_ipi_passive() not working yet */
2736 * Returns 0 on failure, 1 on success
2739 cpu_send_ipiq_passive(int dcpu)
2742 if ((1 << dcpu) & smp_active_mask) {
2743 r = single_apic_ipi_passive(dcpu, XIPIQ_OFFSET,
2744 APIC_DELMODE_FIXED);