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/i386/i386/Attic/mp_machdep.c,v 1.9 2003/07/06 21:23:48 dillon Exp $
31 #include "opt_user_ldt.h"
34 #include <machine/smptests.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
44 #include <sys/memrange.h>
46 #include <sys/dkstat.h>
48 #include <sys/cons.h> /* cngetc() */
51 #include <vm/vm_param.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_extern.h>
56 #include <vm/vm_map.h>
62 #include <machine/smp.h>
63 #include <machine/apic.h>
64 #include <machine/atomic.h>
65 #include <machine/cpufunc.h>
66 #include <machine/mpapic.h>
67 #include <machine/psl.h>
68 #include <machine/segments.h>
69 #include <machine/smptests.h> /** TEST_DEFAULT_CONFIG, TEST_TEST1 */
70 #include <machine/tss.h>
71 #include <machine/specialreg.h>
72 #include <machine/globaldata.h>
75 #include <machine/md_var.h> /* setidt() */
76 #include <i386/isa/icu.h> /* IPIs */
77 #include <i386/isa/intr_machdep.h> /* IPIs */
80 #if defined(TEST_DEFAULT_CONFIG)
81 #define MPFPS_MPFB1 TEST_DEFAULT_CONFIG
83 #define MPFPS_MPFB1 mpfps->mpfb1
84 #endif /* TEST_DEFAULT_CONFIG */
86 #define WARMBOOT_TARGET 0
87 #define WARMBOOT_OFF (KERNBASE + 0x0467)
88 #define WARMBOOT_SEG (KERNBASE + 0x0469)
91 #define BIOS_BASE (0xe8000)
92 #define BIOS_SIZE (0x18000)
94 #define BIOS_BASE (0xf0000)
95 #define BIOS_SIZE (0x10000)
97 #define BIOS_COUNT (BIOS_SIZE/4)
99 #define CMOS_REG (0x70)
100 #define CMOS_DATA (0x71)
101 #define BIOS_RESET (0x0f)
102 #define BIOS_WARM (0x0a)
104 #define PROCENTRY_FLAG_EN 0x01
105 #define PROCENTRY_FLAG_BP 0x02
106 #define IOAPICENTRY_FLAG_EN 0x01
109 /* MP Floating Pointer Structure */
110 typedef struct MPFPS {
123 /* MP Configuration Table Header */
124 typedef struct MPCTH {
126 u_short base_table_length;
130 u_char product_id[12];
131 void *oem_table_pointer;
132 u_short oem_table_size;
135 u_short extended_table_length;
136 u_char extended_table_checksum;
141 typedef struct PROCENTRY {
146 u_long cpu_signature;
147 u_long feature_flags;
152 typedef struct BUSENTRY {
158 typedef struct IOAPICENTRY {
164 } *io_apic_entry_ptr;
166 typedef struct INTENTRY {
176 /* descriptions of MP basetable entries */
177 typedef struct BASETABLE_ENTRY {
184 * this code MUST be enabled here and in mpboot.s.
185 * it follows the very early stages of AP boot by placing values in CMOS ram.
186 * it NORMALLY will never be needed and thus the primitive method for enabling.
191 #if defined(CHECK_POINTS) && !defined(PC98)
192 #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA))
193 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
195 #define CHECK_INIT(D); \
196 CHECK_WRITE(0x34, (D)); \
197 CHECK_WRITE(0x35, (D)); \
198 CHECK_WRITE(0x36, (D)); \
199 CHECK_WRITE(0x37, (D)); \
200 CHECK_WRITE(0x38, (D)); \
201 CHECK_WRITE(0x39, (D));
203 #define CHECK_PRINT(S); \
204 printf("%s: %d, %d, %d, %d, %d, %d\n", \
213 #else /* CHECK_POINTS */
215 #define CHECK_INIT(D)
216 #define CHECK_PRINT(S)
218 #endif /* CHECK_POINTS */
221 * Values to send to the POST hardware.
223 #define MP_BOOTADDRESS_POST 0x10
224 #define MP_PROBE_POST 0x11
225 #define MPTABLE_PASS1_POST 0x12
227 #define MP_START_POST 0x13
228 #define MP_ENABLE_POST 0x14
229 #define MPTABLE_PASS2_POST 0x15
231 #define START_ALL_APS_POST 0x16
232 #define INSTALL_AP_TRAMP_POST 0x17
233 #define START_AP_POST 0x18
235 #define MP_ANNOUNCE_POST 0x19
238 static int need_hyperthreading_fixup;
239 static u_int logical_cpus;
242 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
243 int current_postcode;
245 /** XXX FIXME: what system files declare these??? */
246 extern struct region_descriptor r_gdt, r_idt;
248 int bsp_apic_ready = 0; /* flags useability of BSP apic */
249 int mp_naps; /* # of Applications processors */
250 int mp_nbusses; /* # of busses */
251 int mp_napics; /* # of IO APICs */
252 int boot_cpu_id; /* designated BSP */
253 vm_offset_t cpu_apic_address;
254 vm_offset_t io_apic_address[NAPICID]; /* NAPICID is more than enough */
257 u_int32_t cpu_apic_versions[MAXCPU];
258 u_int32_t *io_apic_versions;
260 struct apic_intmapinfo int_to_apicintpin[APIC_INTMAPSIZE];
262 #ifdef APIC_INTR_REORDER
264 volatile int *location;
266 } apic_isrbit_location[32];
271 * APIC ID logical/physical mapping structures.
272 * We oversize these to simplify boot-time config.
274 int cpu_num_to_apic_id[NAPICID];
275 int io_num_to_apic_id[NAPICID];
276 int apic_id_to_logical[NAPICID];
279 /* Bitmap of all available CPUs */
282 /* AP uses this during bootstrap. Do not staticize. */
286 /* Hotwire a 0->4MB V==P mapping */
287 extern pt_entry_t *KPTphys;
289 /* SMP page table page */
290 extern pt_entry_t *SMPpt;
292 struct pcb stoppcbs[MAXCPU];
294 int smp_started; /* has the system started? */
297 * Local data and functions.
300 static int mp_capable;
301 static u_int boot_address;
302 static u_int base_memory;
304 static int picmode; /* 0: virtual wire mode, 1: PIC mode */
305 static mpfps_t mpfps;
306 static int search_for_sig(u_int32_t target, int count);
307 static void mp_enable(u_int boot_addr);
310 static void mptable_hyperthread_fixup(u_int id_mask);
312 static void mptable_pass1(void);
313 static int mptable_pass2(void);
314 static void default_mp_table(int type);
315 static void fix_mp_table(void);
316 static void setup_apic_irq_mapping(void);
317 static int start_all_aps(u_int boot_addr);
318 static void install_ap_tramp(u_int boot_addr);
319 static int start_ap(int logicalCpu, u_int boot_addr);
320 static int apic_int_is_bus_type(int intr, int bus_type);
323 * Calculate usable address in base memory for AP trampoline code.
326 mp_bootaddress(u_int basemem)
328 POSTCODE(MP_BOOTADDRESS_POST);
330 base_memory = basemem * 1024; /* convert to bytes */
332 boot_address = base_memory & ~0xfff; /* round down to 4k boundary */
333 if ((base_memory - boot_address) < bootMP_size)
334 boot_address -= 4096; /* not enough, lower by 4k */
341 * Look for an Intel MP spec table (ie, SMP capable hardware).
350 POSTCODE(MP_PROBE_POST);
352 /* see if EBDA exists */
353 if ((segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) != 0) {
354 /* search first 1K of EBDA */
355 target = (u_int32_t) (segment << 4);
356 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
359 /* last 1K of base memory, effective 'top of base' passed in */
360 target = (u_int32_t) (base_memory - 0x400);
361 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
365 /* search the BIOS */
366 target = (u_int32_t) BIOS_BASE;
367 if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
376 /* calculate needed resources */
380 /* flag fact that we are running multiple processors */
387 * Startup the SMP processors.
392 POSTCODE(MP_START_POST);
394 /* look for MP capable motherboard */
396 mp_enable(boot_address);
398 panic("MP hardware not found!");
403 * Print various information about the SMP system hardware and setup.
410 POSTCODE(MP_ANNOUNCE_POST);
412 printf("FreeBSD/SMP: Multiprocessor motherboard\n");
413 printf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
414 printf(", version: 0x%08x", cpu_apic_versions[0]);
415 printf(", at 0x%08x\n", cpu_apic_address);
416 for (x = 1; x <= mp_naps; ++x) {
417 printf(" cpu%d (AP): apic id: %2d", x, CPU_TO_ID(x));
418 printf(", version: 0x%08x", cpu_apic_versions[x]);
419 printf(", at 0x%08x\n", cpu_apic_address);
423 for (x = 0; x < mp_napics; ++x) {
424 printf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x));
425 printf(", version: 0x%08x", io_apic_versions[x]);
426 printf(", at 0x%08x\n", io_apic_address[x]);
429 printf(" Warning: APIC I/O disabled\n");
434 * AP cpu's call this to sync up protected mode.
440 int x, myid = bootAP;
442 struct mdglobaldata *md;
444 gdt_segs[GPRIV_SEL].ssd_base = (int) &CPU_prvspace[myid];
445 gdt_segs[GPROC0_SEL].ssd_base =
446 (int) &CPU_prvspace[myid].mdglobaldata.gd_common_tss;
447 CPU_prvspace[myid].mdglobaldata.mi.gd_prvspace = &CPU_prvspace[myid];
449 for (x = 0; x < NGDT; x++) {
450 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd);
453 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
454 r_gdt.rd_base = (int) &gdt[myid * NGDT];
455 lgdt(&r_gdt); /* does magic intra-segment return */
461 currentldt = _default_ldt;
464 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
465 gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
469 md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
470 md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
471 md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
472 md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL].sd;
473 md->gd_common_tssd = *md->gd_tss_gdt;
477 * Set to a known state:
478 * Set by mpboot.s: CR0_PG, CR0_PE
479 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
482 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
491 * Final configuration of the BSP's local APIC:
492 * - disable 'pic mode'.
493 * - disable 'virtual wire mode'.
497 bsp_apic_configure(void)
502 /* leave 'pic mode' if necessary */
504 outb(0x22, 0x70); /* select IMCR */
505 byte = inb(0x23); /* current contents */
506 byte |= 0x01; /* mask external INTR */
507 outb(0x23, byte); /* disconnect 8259s/NMI */
510 /* mask lint0 (the 8259 'virtual wire' connection) */
511 temp = lapic.lvt_lint0;
512 temp |= APIC_LVT_M; /* set the mask */
513 lapic.lvt_lint0 = temp;
515 /* setup lint1 to handle NMI */
516 temp = lapic.lvt_lint1;
517 temp &= ~APIC_LVT_M; /* clear the mask */
518 lapic.lvt_lint1 = temp;
521 apic_dump("bsp_apic_configure()");
526 /*******************************************************************
527 * local functions and data
531 * start the SMP system
534 mp_enable(u_int boot_addr)
542 POSTCODE(MP_ENABLE_POST);
544 /* turn on 4MB of V == P addressing so we can get to MP table */
545 *(int *)PTD = PG_V | PG_RW | ((uintptr_t)(void *)KPTphys & PG_FRAME);
548 /* examine the MP table for needed info, uses physical addresses */
554 /* can't process default configs till the CPU APIC is pmapped */
558 /* post scan cleanup */
560 setup_apic_irq_mapping();
564 /* fill the LOGICAL io_apic_versions table */
565 for (apic = 0; apic < mp_napics; ++apic) {
566 ux = io_apic_read(apic, IOAPIC_VER);
567 io_apic_versions[apic] = ux;
568 io_apic_set_id(apic, IO_TO_ID(apic));
571 /* program each IO APIC in the system */
572 for (apic = 0; apic < mp_napics; ++apic)
573 if (io_apic_setup(apic) < 0)
574 panic("IO APIC setup failure");
576 /* install a 'Spurious INTerrupt' vector */
577 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
578 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
580 /* install an inter-CPU IPI for TLB invalidation */
581 setidt(XINVLTLB_OFFSET, Xinvltlb,
582 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
586 /* install an inter-CPU IPI for reading processor state */
587 setidt(XCPUCHECKSTATE_OFFSET, Xcpucheckstate,
588 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
592 /* install an inter-CPU IPI for all-CPU rendezvous */
593 setidt(XRENDEZVOUS_OFFSET, Xrendezvous,
594 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
596 /* install an inter-CPU IPI for forcing an additional software trap */
597 setidt(XCPUAST_OFFSET, Xcpuast,
598 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
600 /* install an inter-CPU IPI for interrupt forwarding */
601 setidt(XFORWARD_IRQ_OFFSET, Xforward_irq,
602 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
604 /* install an inter-CPU IPI for CPU stop/restart */
605 setidt(XCPUSTOP_OFFSET, Xcpustop,
606 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
608 #if defined(TEST_TEST1)
609 /* install a "fake hardware INTerrupt" vector */
610 setidt(XTEST1_OFFSET, Xtest1,
611 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
612 #endif /** TEST_TEST1 */
616 /* start each Application Processor */
617 start_all_aps(boot_addr);
622 * look for the MP spec signature
625 /* string defined by the Intel MP Spec as identifying the MP table */
626 #define MP_SIG 0x5f504d5f /* _MP_ */
627 #define NEXT(X) ((X) += 4)
629 search_for_sig(u_int32_t target, int count)
632 u_int32_t *addr = (u_int32_t *) (KERNBASE + target);
634 for (x = 0; x < count; NEXT(x))
635 if (addr[x] == MP_SIG)
636 /* make array index a byte index */
637 return (target + (x * sizeof(u_int32_t)));
643 static basetable_entry basetable_entry_types[] =
645 {0, 20, "Processor"},
652 typedef struct BUSDATA {
654 enum busTypes bus_type;
657 typedef struct INTDATA {
667 typedef struct BUSTYPENAME {
672 static bus_type_name bus_type_table[] =
678 {UNKNOWN_BUSTYPE, "---"},
681 {UNKNOWN_BUSTYPE, "---"},
682 {UNKNOWN_BUSTYPE, "---"},
683 {UNKNOWN_BUSTYPE, "---"},
684 {UNKNOWN_BUSTYPE, "---"},
685 {UNKNOWN_BUSTYPE, "---"},
687 {UNKNOWN_BUSTYPE, "---"},
688 {UNKNOWN_BUSTYPE, "---"},
689 {UNKNOWN_BUSTYPE, "---"},
690 {UNKNOWN_BUSTYPE, "---"},
692 {UNKNOWN_BUSTYPE, "---"}
694 /* from MP spec v1.4, table 5-1 */
695 static int default_data[7][5] =
697 /* nbus, id0, type0, id1, type1 */
698 {1, 0, ISA, 255, 255},
699 {1, 0, EISA, 255, 255},
700 {1, 0, EISA, 255, 255},
701 {1, 0, MCA, 255, 255},
703 {2, 0, EISA, 1, PCI},
709 static bus_datum *bus_data;
711 /* the IO INT data, one entry per possible APIC INTerrupt */
712 static io_int *io_apic_ints;
716 static int processor_entry __P((proc_entry_ptr entry, int cpu));
717 static int bus_entry __P((bus_entry_ptr entry, int bus));
718 static int io_apic_entry __P((io_apic_entry_ptr entry, int apic));
719 static int int_entry __P((int_entry_ptr entry, int intr));
720 static int lookup_bus_type __P((char *name));
724 * 1st pass on motherboard's Intel MP specification table.
730 * cpu_apic_address (common to all CPUs)
750 POSTCODE(MPTABLE_PASS1_POST);
752 /* clear various tables */
753 for (x = 0; x < NAPICID; ++x) {
754 io_apic_address[x] = ~0; /* IO APIC address table */
757 /* init everything to empty */
766 /* check for use of 'default' configuration */
767 if (MPFPS_MPFB1 != 0) {
768 /* use default addresses */
769 cpu_apic_address = DEFAULT_APIC_BASE;
770 io_apic_address[0] = DEFAULT_IO_APIC_BASE;
772 /* fill in with defaults */
773 mp_naps = 2; /* includes BSP */
774 mp_nbusses = default_data[MPFPS_MPFB1 - 1][0];
781 if ((cth = mpfps->pap) == 0)
782 panic("MP Configuration Table Header MISSING!");
784 cpu_apic_address = (vm_offset_t) cth->apic_address;
786 /* walk the table, recording info of interest */
787 totalSize = cth->base_table_length - sizeof(struct MPCTH);
788 position = (u_char *) cth + sizeof(struct MPCTH);
789 count = cth->entry_count;
792 switch (type = *(u_char *) position) {
793 case 0: /* processor_entry */
794 if (((proc_entry_ptr)position)->cpu_flags
795 & PROCENTRY_FLAG_EN) {
799 ((proc_entry_ptr)position)->apic_id;
803 case 1: /* bus_entry */
806 case 2: /* io_apic_entry */
807 if (((io_apic_entry_ptr)position)->apic_flags
808 & IOAPICENTRY_FLAG_EN)
809 io_apic_address[mp_napics++] =
810 (vm_offset_t)((io_apic_entry_ptr)
811 position)->apic_address;
813 case 3: /* int_entry */
816 case 4: /* int_entry */
819 panic("mpfps Base Table HOSED!");
823 totalSize -= basetable_entry_types[type].length;
824 (u_char*)position += basetable_entry_types[type].length;
828 /* qualify the numbers */
829 if (mp_naps > MAXCPU) {
830 printf("Warning: only using %d of %d available CPUs!\n",
836 /* See if we need to fixup HT logical CPUs. */
837 mptable_hyperthread_fixup(id_mask);
842 * This is also used as a counter while starting the APs.
846 --mp_naps; /* subtract the BSP */
851 * 2nd pass on motherboard's Intel MP specification table.
855 * ID_TO_IO(N), phy APIC ID to log CPU/IO table
856 * CPU_TO_ID(N), logical CPU to APIC ID table
857 * IO_TO_ID(N), logical IO to APIC ID table
865 struct PROCENTRY proc;
873 int apic, bus, cpu, intr;
877 POSTCODE(MPTABLE_PASS2_POST);
880 /* Initialize fake proc entry for use with HT fixup. */
881 bzero(&proc, sizeof(proc));
883 proc.cpu_flags = PROCENTRY_FLAG_EN;
886 pgeflag = 0; /* XXX - Not used under SMP yet. */
888 MALLOC(io_apic_versions, u_int32_t *, sizeof(u_int32_t) * mp_napics,
890 MALLOC(ioapic, volatile ioapic_t **, sizeof(ioapic_t *) * mp_napics,
892 MALLOC(io_apic_ints, io_int *, sizeof(io_int) * (nintrs + 1),
894 MALLOC(bus_data, bus_datum *, sizeof(bus_datum) * mp_nbusses,
897 bzero(ioapic, sizeof(ioapic_t *) * mp_napics);
899 for (i = 0; i < mp_napics; i++) {
900 for (j = 0; j < mp_napics; j++) {
901 /* same page frame as a previous IO apic? */
902 if (((vm_offset_t)SMPpt[NPTEPG-2-j] & PG_FRAME) ==
903 (io_apic_address[i] & PG_FRAME)) {
904 ioapic[i] = (ioapic_t *)((u_int)CPU_prvspace
905 + (NPTEPG-2-j) * PAGE_SIZE
906 + (io_apic_address[i] & PAGE_MASK));
909 /* use this slot if available */
910 if (((vm_offset_t)SMPpt[NPTEPG-2-j] & PG_FRAME) == 0) {
911 SMPpt[NPTEPG-2-j] = (pt_entry_t)(PG_V | PG_RW |
912 pgeflag | (io_apic_address[i] & PG_FRAME));
913 ioapic[i] = (ioapic_t *)((u_int)CPU_prvspace
914 + (NPTEPG-2-j) * PAGE_SIZE
915 + (io_apic_address[i] & PAGE_MASK));
921 /* clear various tables */
922 for (x = 0; x < NAPICID; ++x) {
923 ID_TO_IO(x) = -1; /* phy APIC ID to log CPU/IO table */
924 CPU_TO_ID(x) = -1; /* logical CPU to APIC ID table */
925 IO_TO_ID(x) = -1; /* logical IO to APIC ID table */
928 /* clear bus data table */
929 for (x = 0; x < mp_nbusses; ++x)
930 bus_data[x].bus_id = 0xff;
932 /* clear IO APIC INT table */
933 for (x = 0; x < (nintrs + 1); ++x) {
934 io_apic_ints[x].int_type = 0xff;
935 io_apic_ints[x].int_vector = 0xff;
938 /* setup the cpu/apic mapping arrays */
941 /* record whether PIC or virtual-wire mode */
942 picmode = (mpfps->mpfb2 & 0x80) ? 1 : 0;
944 /* check for use of 'default' configuration */
945 if (MPFPS_MPFB1 != 0)
946 return MPFPS_MPFB1; /* return default configuration type */
948 if ((cth = mpfps->pap) == 0)
949 panic("MP Configuration Table Header MISSING!");
951 /* walk the table, recording info of interest */
952 totalSize = cth->base_table_length - sizeof(struct MPCTH);
953 position = (u_char *) cth + sizeof(struct MPCTH);
954 count = cth->entry_count;
955 apic = bus = intr = 0;
956 cpu = 1; /* pre-count the BSP */
959 switch (type = *(u_char *) position) {
961 if (processor_entry(position, cpu))
965 if (need_hyperthreading_fixup) {
967 * Create fake mptable processor entries
968 * and feed them to processor_entry() to
969 * enumerate the logical CPUs.
971 proc.apic_id = ((proc_entry_ptr)position)->apic_id;
972 for (i = 1; i < logical_cpus; i++) {
974 (void)processor_entry(&proc, cpu);
981 if (bus_entry(position, bus))
985 if (io_apic_entry(position, apic))
989 if (int_entry(position, intr))
993 /* int_entry(position); */
996 panic("mpfps Base Table HOSED!");
1000 totalSize -= basetable_entry_types[type].length;
1001 (u_char *) position += basetable_entry_types[type].length;
1004 if (boot_cpu_id == -1)
1005 panic("NO BSP found!");
1007 /* report fact that its NOT a default configuration */
1013 * Check if we should perform a hyperthreading "fix-up" to
1014 * enumerate any logical CPU's that aren't already listed
1017 * XXX: We assume that all of the physical CPUs in the
1018 * system have the same number of logical CPUs.
1020 * XXX: We assume that APIC ID's are allocated such that
1021 * the APIC ID's for a physical processor are aligned
1022 * with the number of logical CPU's in the processor.
1025 mptable_hyperthread_fixup(u_int id_mask)
1029 /* Nothing to do if there is no HTT support. */
1030 if ((cpu_feature & CPUID_HTT) == 0)
1032 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
1033 if (logical_cpus <= 1)
1037 * For each APIC ID of a CPU that is set in the mask,
1038 * scan the other candidate APIC ID's for this
1039 * physical processor. If any of those ID's are
1040 * already in the table, then kill the fixup.
1042 for (id = 0; id <= MAXCPU; id++) {
1043 if ((id_mask & 1 << id) == 0)
1045 /* First, make sure we are on a logical_cpus boundary. */
1046 if (id % logical_cpus != 0)
1048 for (i = id + 1; i < id + logical_cpus; i++)
1049 if ((id_mask & 1 << i) != 0)
1054 * Ok, the ID's checked out, so enable the fixup. We have to fixup
1055 * mp_naps right now.
1057 need_hyperthreading_fixup = 1;
1058 mp_naps *= logical_cpus;
1063 assign_apic_irq(int apic, int intpin, int irq)
1067 if (int_to_apicintpin[irq].ioapic != -1)
1068 panic("assign_apic_irq: inconsistent table");
1070 int_to_apicintpin[irq].ioapic = apic;
1071 int_to_apicintpin[irq].int_pin = intpin;
1072 int_to_apicintpin[irq].apic_address = ioapic[apic];
1073 int_to_apicintpin[irq].redirindex = IOAPIC_REDTBL + 2 * intpin;
1075 for (x = 0; x < nintrs; x++) {
1076 if ((io_apic_ints[x].int_type == 0 ||
1077 io_apic_ints[x].int_type == 3) &&
1078 io_apic_ints[x].int_vector == 0xff &&
1079 io_apic_ints[x].dst_apic_id == IO_TO_ID(apic) &&
1080 io_apic_ints[x].dst_apic_int == intpin)
1081 io_apic_ints[x].int_vector = irq;
1086 revoke_apic_irq(int irq)
1092 if (int_to_apicintpin[irq].ioapic == -1)
1093 panic("revoke_apic_irq: inconsistent table");
1095 oldapic = int_to_apicintpin[irq].ioapic;
1096 oldintpin = int_to_apicintpin[irq].int_pin;
1098 int_to_apicintpin[irq].ioapic = -1;
1099 int_to_apicintpin[irq].int_pin = 0;
1100 int_to_apicintpin[irq].apic_address = NULL;
1101 int_to_apicintpin[irq].redirindex = 0;
1103 for (x = 0; x < nintrs; x++) {
1104 if ((io_apic_ints[x].int_type == 0 ||
1105 io_apic_ints[x].int_type == 3) &&
1106 io_apic_ints[x].int_vector != 0xff &&
1107 io_apic_ints[x].dst_apic_id == IO_TO_ID(oldapic) &&
1108 io_apic_ints[x].dst_apic_int == oldintpin)
1109 io_apic_ints[x].int_vector = 0xff;
1115 allocate_apic_irq(int intr)
1121 if (io_apic_ints[intr].int_vector != 0xff)
1122 return; /* Interrupt handler already assigned */
1124 if (io_apic_ints[intr].int_type != 0 &&
1125 (io_apic_ints[intr].int_type != 3 ||
1126 (io_apic_ints[intr].dst_apic_id == IO_TO_ID(0) &&
1127 io_apic_ints[intr].dst_apic_int == 0)))
1128 return; /* Not INT or ExtInt on != (0, 0) */
1131 while (irq < APIC_INTMAPSIZE &&
1132 int_to_apicintpin[irq].ioapic != -1)
1135 if (irq >= APIC_INTMAPSIZE)
1136 return; /* No free interrupt handlers */
1138 apic = ID_TO_IO(io_apic_ints[intr].dst_apic_id);
1139 intpin = io_apic_ints[intr].dst_apic_int;
1141 assign_apic_irq(apic, intpin, irq);
1142 io_apic_setup_intpin(apic, intpin);
1147 swap_apic_id(int apic, int oldid, int newid)
1154 return; /* Nothing to do */
1156 printf("Changing APIC ID for IO APIC #%d from %d to %d in MP table\n",
1157 apic, oldid, newid);
1159 /* Swap physical APIC IDs in interrupt entries */
1160 for (x = 0; x < nintrs; x++) {
1161 if (io_apic_ints[x].dst_apic_id == oldid)
1162 io_apic_ints[x].dst_apic_id = newid;
1163 else if (io_apic_ints[x].dst_apic_id == newid)
1164 io_apic_ints[x].dst_apic_id = oldid;
1167 /* Swap physical APIC IDs in IO_TO_ID mappings */
1168 for (oapic = 0; oapic < mp_napics; oapic++)
1169 if (IO_TO_ID(oapic) == newid)
1172 if (oapic < mp_napics) {
1173 printf("Changing APIC ID for IO APIC #%d from "
1174 "%d to %d in MP table\n",
1175 oapic, newid, oldid);
1176 IO_TO_ID(oapic) = oldid;
1178 IO_TO_ID(apic) = newid;
1183 fix_id_to_io_mapping(void)
1187 for (x = 0; x < NAPICID; x++)
1190 for (x = 0; x <= mp_naps; x++)
1191 if (CPU_TO_ID(x) < NAPICID)
1192 ID_TO_IO(CPU_TO_ID(x)) = x;
1194 for (x = 0; x < mp_napics; x++)
1195 if (IO_TO_ID(x) < NAPICID)
1196 ID_TO_IO(IO_TO_ID(x)) = x;
1201 first_free_apic_id(void)
1205 for (freeid = 0; freeid < NAPICID; freeid++) {
1206 for (x = 0; x <= mp_naps; x++)
1207 if (CPU_TO_ID(x) == freeid)
1211 for (x = 0; x < mp_napics; x++)
1212 if (IO_TO_ID(x) == freeid)
1223 io_apic_id_acceptable(int apic, int id)
1225 int cpu; /* Logical CPU number */
1226 int oapic; /* Logical IO APIC number for other IO APIC */
1229 return 0; /* Out of range */
1231 for (cpu = 0; cpu <= mp_naps; cpu++)
1232 if (CPU_TO_ID(cpu) == id)
1233 return 0; /* Conflict with CPU */
1235 for (oapic = 0; oapic < mp_napics && oapic < apic; oapic++)
1236 if (IO_TO_ID(oapic) == id)
1237 return 0; /* Conflict with other APIC */
1239 return 1; /* ID is acceptable for IO APIC */
1244 * parse an Intel MP specification table
1251 int bus_0 = 0; /* Stop GCC warning */
1252 int bus_pci = 0; /* Stop GCC warning */
1254 int apic; /* IO APIC unit number */
1255 int freeid; /* Free physical APIC ID */
1256 int physid; /* Current physical IO APIC ID */
1259 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
1260 * did it wrong. The MP spec says that when more than 1 PCI bus
1261 * exists the BIOS must begin with bus entries for the PCI bus and use
1262 * actual PCI bus numbering. This implies that when only 1 PCI bus
1263 * exists the BIOS can choose to ignore this ordering, and indeed many
1264 * MP motherboards do ignore it. This causes a problem when the PCI
1265 * sub-system makes requests of the MP sub-system based on PCI bus
1266 * numbers. So here we look for the situation and renumber the
1267 * busses and associated INTs in an effort to "make it right".
1270 /* find bus 0, PCI bus, count the number of PCI busses */
1271 for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) {
1272 if (bus_data[x].bus_id == 0) {
1275 if (bus_data[x].bus_type == PCI) {
1281 * bus_0 == slot of bus with ID of 0
1282 * bus_pci == slot of last PCI bus encountered
1285 /* check the 1 PCI bus case for sanity */
1286 /* if it is number 0 all is well */
1287 if (num_pci_bus == 1 &&
1288 bus_data[bus_pci].bus_id != 0) {
1290 /* mis-numbered, swap with whichever bus uses slot 0 */
1292 /* swap the bus entry types */
1293 bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type;
1294 bus_data[bus_0].bus_type = PCI;
1296 /* swap each relavant INTerrupt entry */
1297 id = bus_data[bus_pci].bus_id;
1298 for (x = 0; x < nintrs; ++x) {
1299 if (io_apic_ints[x].src_bus_id == id) {
1300 io_apic_ints[x].src_bus_id = 0;
1302 else if (io_apic_ints[x].src_bus_id == 0) {
1303 io_apic_ints[x].src_bus_id = id;
1308 /* Assign IO APIC IDs.
1310 * First try the existing ID. If a conflict is detected, try
1311 * the ID in the MP table. If a conflict is still detected, find
1314 * We cannot use the ID_TO_IO table before all conflicts has been
1315 * resolved and the table has been corrected.
1317 for (apic = 0; apic < mp_napics; ++apic) { /* For all IO APICs */
1319 /* First try to use the value set by the BIOS */
1320 physid = io_apic_get_id(apic);
1321 if (io_apic_id_acceptable(apic, physid)) {
1322 if (IO_TO_ID(apic) != physid)
1323 swap_apic_id(apic, IO_TO_ID(apic), physid);
1327 /* Then check if the value in the MP table is acceptable */
1328 if (io_apic_id_acceptable(apic, IO_TO_ID(apic)))
1331 /* Last resort, find a free APIC ID and use it */
1332 freeid = first_free_apic_id();
1333 if (freeid >= NAPICID)
1334 panic("No free physical APIC IDs found");
1336 if (io_apic_id_acceptable(apic, freeid)) {
1337 swap_apic_id(apic, IO_TO_ID(apic), freeid);
1340 panic("Free physical APIC ID not usable");
1342 fix_id_to_io_mapping();
1344 /* detect and fix broken Compaq MP table */
1345 if (apic_int_type(0, 0) == -1) {
1346 printf("APIC_IO: MP table broken: 8259->APIC entry missing!\n");
1347 io_apic_ints[nintrs].int_type = 3; /* ExtInt */
1348 io_apic_ints[nintrs].int_vector = 0xff; /* Unassigned */
1349 /* XXX fixme, set src bus id etc, but it doesn't seem to hurt */
1350 io_apic_ints[nintrs].dst_apic_id = IO_TO_ID(0);
1351 io_apic_ints[nintrs].dst_apic_int = 0; /* Pin 0 */
1357 /* Assign low level interrupt handlers */
1359 setup_apic_irq_mapping(void)
1365 for (x = 0; x < APIC_INTMAPSIZE; x++) {
1366 int_to_apicintpin[x].ioapic = -1;
1367 int_to_apicintpin[x].int_pin = 0;
1368 int_to_apicintpin[x].apic_address = NULL;
1369 int_to_apicintpin[x].redirindex = 0;
1372 /* First assign ISA/EISA interrupts */
1373 for (x = 0; x < nintrs; x++) {
1374 int_vector = io_apic_ints[x].src_bus_irq;
1375 if (int_vector < APIC_INTMAPSIZE &&
1376 io_apic_ints[x].int_vector == 0xff &&
1377 int_to_apicintpin[int_vector].ioapic == -1 &&
1378 (apic_int_is_bus_type(x, ISA) ||
1379 apic_int_is_bus_type(x, EISA)) &&
1380 io_apic_ints[x].int_type == 0) {
1381 assign_apic_irq(ID_TO_IO(io_apic_ints[x].dst_apic_id),
1382 io_apic_ints[x].dst_apic_int,
1387 /* Assign ExtInt entry if no ISA/EISA interrupt 0 entry */
1388 for (x = 0; x < nintrs; x++) {
1389 if (io_apic_ints[x].dst_apic_int == 0 &&
1390 io_apic_ints[x].dst_apic_id == IO_TO_ID(0) &&
1391 io_apic_ints[x].int_vector == 0xff &&
1392 int_to_apicintpin[0].ioapic == -1 &&
1393 io_apic_ints[x].int_type == 3) {
1394 assign_apic_irq(0, 0, 0);
1398 /* PCI interrupt assignment is deferred */
1403 processor_entry(proc_entry_ptr entry, int cpu)
1405 /* check for usability */
1406 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
1409 if(entry->apic_id >= NAPICID)
1410 panic("CPU APIC ID out of range (0..%d)", NAPICID - 1);
1411 /* check for BSP flag */
1412 if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
1413 boot_cpu_id = entry->apic_id;
1414 CPU_TO_ID(0) = entry->apic_id;
1415 ID_TO_CPU(entry->apic_id) = 0;
1416 return 0; /* its already been counted */
1419 /* add another AP to list, if less than max number of CPUs */
1420 else if (cpu < MAXCPU) {
1421 CPU_TO_ID(cpu) = entry->apic_id;
1422 ID_TO_CPU(entry->apic_id) = cpu;
1431 bus_entry(bus_entry_ptr entry, int bus)
1436 /* encode the name into an index */
1437 for (x = 0; x < 6; ++x) {
1438 if ((c = entry->bus_type[x]) == ' ')
1444 if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE)
1445 panic("unknown bus type: '%s'", name);
1447 bus_data[bus].bus_id = entry->bus_id;
1448 bus_data[bus].bus_type = x;
1455 io_apic_entry(io_apic_entry_ptr entry, int apic)
1457 if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN))
1460 IO_TO_ID(apic) = entry->apic_id;
1461 if (entry->apic_id < NAPICID)
1462 ID_TO_IO(entry->apic_id) = apic;
1469 lookup_bus_type(char *name)
1473 for (x = 0; x < MAX_BUSTYPE; ++x)
1474 if (strcmp(bus_type_table[x].name, name) == 0)
1475 return bus_type_table[x].type;
1477 return UNKNOWN_BUSTYPE;
1482 int_entry(int_entry_ptr entry, int intr)
1486 io_apic_ints[intr].int_type = entry->int_type;
1487 io_apic_ints[intr].int_flags = entry->int_flags;
1488 io_apic_ints[intr].src_bus_id = entry->src_bus_id;
1489 io_apic_ints[intr].src_bus_irq = entry->src_bus_irq;
1490 if (entry->dst_apic_id == 255) {
1491 /* This signal goes to all IO APICS. Select an IO APIC
1492 with sufficient number of interrupt pins */
1493 for (apic = 0; apic < mp_napics; apic++)
1494 if (((io_apic_read(apic, IOAPIC_VER) &
1495 IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) >=
1496 entry->dst_apic_int)
1498 if (apic < mp_napics)
1499 io_apic_ints[intr].dst_apic_id = IO_TO_ID(apic);
1501 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1503 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1504 io_apic_ints[intr].dst_apic_int = entry->dst_apic_int;
1511 apic_int_is_bus_type(int intr, int bus_type)
1515 for (bus = 0; bus < mp_nbusses; ++bus)
1516 if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id)
1517 && ((int) bus_data[bus].bus_type == bus_type))
1525 * Given a traditional ISA INT mask, return an APIC mask.
1528 isa_apic_mask(u_int isa_mask)
1533 #if defined(SKIP_IRQ15_REDIRECT)
1534 if (isa_mask == (1 << 15)) {
1535 printf("skipping ISA IRQ15 redirect\n");
1538 #endif /* SKIP_IRQ15_REDIRECT */
1540 isa_irq = ffs(isa_mask); /* find its bit position */
1541 if (isa_irq == 0) /* doesn't exist */
1543 --isa_irq; /* make it zero based */
1545 apic_pin = isa_apic_irq(isa_irq); /* look for APIC connection */
1549 return (1 << apic_pin); /* convert pin# to a mask */
1554 * Determine which APIC pin an ISA/EISA INT is attached to.
1556 #define INTTYPE(I) (io_apic_ints[(I)].int_type)
1557 #define INTPIN(I) (io_apic_ints[(I)].dst_apic_int)
1558 #define INTIRQ(I) (io_apic_ints[(I)].int_vector)
1559 #define INTAPIC(I) (ID_TO_IO(io_apic_ints[(I)].dst_apic_id))
1561 #define SRCBUSIRQ(I) (io_apic_ints[(I)].src_bus_irq)
1563 isa_apic_irq(int isa_irq)
1567 for (intr = 0; intr < nintrs; ++intr) { /* check each record */
1568 if (INTTYPE(intr) == 0) { /* standard INT */
1569 if (SRCBUSIRQ(intr) == isa_irq) {
1570 if (apic_int_is_bus_type(intr, ISA) ||
1571 apic_int_is_bus_type(intr, EISA)) {
1572 if (INTIRQ(intr) == 0xff)
1573 return -1; /* unassigned */
1574 return INTIRQ(intr); /* found */
1579 return -1; /* NOT found */
1584 * Determine which APIC pin a PCI INT is attached to.
1586 #define SRCBUSID(I) (io_apic_ints[(I)].src_bus_id)
1587 #define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
1588 #define SRCBUSLINE(I) (io_apic_ints[(I)].src_bus_irq & 0x03)
1590 pci_apic_irq(int pciBus, int pciDevice, int pciInt)
1594 --pciInt; /* zero based */
1596 for (intr = 0; intr < nintrs; ++intr) /* check each record */
1597 if ((INTTYPE(intr) == 0) /* standard INT */
1598 && (SRCBUSID(intr) == pciBus)
1599 && (SRCBUSDEVICE(intr) == pciDevice)
1600 && (SRCBUSLINE(intr) == pciInt)) /* a candidate IRQ */
1601 if (apic_int_is_bus_type(intr, PCI)) {
1602 if (INTIRQ(intr) == 0xff)
1603 allocate_apic_irq(intr);
1604 if (INTIRQ(intr) == 0xff)
1605 return -1; /* unassigned */
1606 return INTIRQ(intr); /* exact match */
1609 return -1; /* NOT found */
1613 next_apic_irq(int irq)
1620 for (intr = 0; intr < nintrs; intr++) {
1621 if (INTIRQ(intr) != irq || INTTYPE(intr) != 0)
1623 bus = SRCBUSID(intr);
1624 bustype = apic_bus_type(bus);
1625 if (bustype != ISA &&
1631 if (intr >= nintrs) {
1634 for (ointr = intr + 1; ointr < nintrs; ointr++) {
1635 if (INTTYPE(ointr) != 0)
1637 if (bus != SRCBUSID(ointr))
1639 if (bustype == PCI) {
1640 if (SRCBUSDEVICE(intr) != SRCBUSDEVICE(ointr))
1642 if (SRCBUSLINE(intr) != SRCBUSLINE(ointr))
1645 if (bustype == ISA || bustype == EISA) {
1646 if (SRCBUSIRQ(intr) != SRCBUSIRQ(ointr))
1649 if (INTPIN(intr) == INTPIN(ointr))
1653 if (ointr >= nintrs) {
1656 return INTIRQ(ointr);
1670 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt.
1673 * Exactly what this means is unclear at this point. It is a solution
1674 * for motherboards that redirect the MBIRQ0 pin. Generically a motherboard
1675 * could route any of the ISA INTs to upper (>15) IRQ values. But most would
1676 * NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an
1680 undirect_isa_irq(int rirq)
1684 printf("Freeing redirected ISA irq %d.\n", rirq);
1685 /** FIXME: tickle the MB redirector chip */
1689 printf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq);
1696 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt
1699 undirect_pci_irq(int rirq)
1703 printf("Freeing redirected PCI irq %d.\n", rirq);
1705 /** FIXME: tickle the MB redirector chip */
1709 printf("Freeing (NOT implemented) redirected PCI irq %d.\n",
1717 * given a bus ID, return:
1718 * the bus type if found
1722 apic_bus_type(int id)
1726 for (x = 0; x < mp_nbusses; ++x)
1727 if (bus_data[x].bus_id == id)
1728 return bus_data[x].bus_type;
1735 * given a LOGICAL APIC# and pin#, return:
1736 * the associated src bus ID if found
1740 apic_src_bus_id(int apic, int pin)
1744 /* search each of the possible INTerrupt sources */
1745 for (x = 0; x < nintrs; ++x)
1746 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1747 (pin == io_apic_ints[x].dst_apic_int))
1748 return (io_apic_ints[x].src_bus_id);
1750 return -1; /* NOT found */
1755 * given a LOGICAL APIC# and pin#, return:
1756 * the associated src bus IRQ if found
1760 apic_src_bus_irq(int apic, int pin)
1764 for (x = 0; x < nintrs; x++)
1765 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1766 (pin == io_apic_ints[x].dst_apic_int))
1767 return (io_apic_ints[x].src_bus_irq);
1769 return -1; /* NOT found */
1774 * given a LOGICAL APIC# and pin#, return:
1775 * the associated INTerrupt type if found
1779 apic_int_type(int apic, int pin)
1783 /* search each of the possible INTerrupt sources */
1784 for (x = 0; x < nintrs; ++x)
1785 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1786 (pin == io_apic_ints[x].dst_apic_int))
1787 return (io_apic_ints[x].int_type);
1789 return -1; /* NOT found */
1793 apic_irq(int apic, int pin)
1798 for (x = 0; x < nintrs; ++x)
1799 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1800 (pin == io_apic_ints[x].dst_apic_int)) {
1801 res = io_apic_ints[x].int_vector;
1804 if (apic != int_to_apicintpin[res].ioapic)
1805 panic("apic_irq: inconsistent table");
1806 if (pin != int_to_apicintpin[res].int_pin)
1807 panic("apic_irq inconsistent table (2)");
1815 * given a LOGICAL APIC# and pin#, return:
1816 * the associated trigger mode if found
1820 apic_trigger(int apic, int pin)
1824 /* search each of the possible INTerrupt sources */
1825 for (x = 0; x < nintrs; ++x)
1826 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1827 (pin == io_apic_ints[x].dst_apic_int))
1828 return ((io_apic_ints[x].int_flags >> 2) & 0x03);
1830 return -1; /* NOT found */
1835 * given a LOGICAL APIC# and pin#, return:
1836 * the associated 'active' level if found
1840 apic_polarity(int apic, int pin)
1844 /* search each of the possible INTerrupt sources */
1845 for (x = 0; x < nintrs; ++x)
1846 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1847 (pin == io_apic_ints[x].dst_apic_int))
1848 return (io_apic_ints[x].int_flags & 0x03);
1850 return -1; /* NOT found */
1855 * set data according to MP defaults
1856 * FIXME: probably not complete yet...
1859 default_mp_table(int type)
1862 #if defined(APIC_IO)
1865 #endif /* APIC_IO */
1868 printf(" MP default config type: %d\n", type);
1871 printf(" bus: ISA, APIC: 82489DX\n");
1874 printf(" bus: EISA, APIC: 82489DX\n");
1877 printf(" bus: EISA, APIC: 82489DX\n");
1880 printf(" bus: MCA, APIC: 82489DX\n");
1883 printf(" bus: ISA+PCI, APIC: Integrated\n");
1886 printf(" bus: EISA+PCI, APIC: Integrated\n");
1889 printf(" bus: MCA+PCI, APIC: Integrated\n");
1892 printf(" future type\n");
1898 boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24;
1899 ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0;
1902 CPU_TO_ID(0) = boot_cpu_id;
1903 ID_TO_CPU(boot_cpu_id) = 0;
1905 /* one and only AP */
1906 CPU_TO_ID(1) = ap_cpu_id;
1907 ID_TO_CPU(ap_cpu_id) = 1;
1909 #if defined(APIC_IO)
1910 /* one and only IO APIC */
1911 io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24;
1914 * sanity check, refer to MP spec section 3.6.6, last paragraph
1915 * necessary as some hardware isn't properly setting up the IO APIC
1917 #if defined(REALLY_ANAL_IOAPICID_VALUE)
1918 if (io_apic_id != 2) {
1920 if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) {
1921 #endif /* REALLY_ANAL_IOAPICID_VALUE */
1922 io_apic_set_id(0, 2);
1925 IO_TO_ID(0) = io_apic_id;
1926 ID_TO_IO(io_apic_id) = 0;
1927 #endif /* APIC_IO */
1929 /* fill out bus entries */
1938 bus_data[0].bus_id = default_data[type - 1][1];
1939 bus_data[0].bus_type = default_data[type - 1][2];
1940 bus_data[1].bus_id = default_data[type - 1][3];
1941 bus_data[1].bus_type = default_data[type - 1][4];
1944 /* case 4: case 7: MCA NOT supported */
1945 default: /* illegal/reserved */
1946 panic("BAD default MP config: %d", type);
1950 #if defined(APIC_IO)
1951 /* general cases from MP v1.4, table 5-2 */
1952 for (pin = 0; pin < 16; ++pin) {
1953 io_apic_ints[pin].int_type = 0;
1954 io_apic_ints[pin].int_flags = 0x05; /* edge/active-hi */
1955 io_apic_ints[pin].src_bus_id = 0;
1956 io_apic_ints[pin].src_bus_irq = pin; /* IRQ2 caught below */
1957 io_apic_ints[pin].dst_apic_id = io_apic_id;
1958 io_apic_ints[pin].dst_apic_int = pin; /* 1-to-1 */
1961 /* special cases from MP v1.4, table 5-2 */
1963 io_apic_ints[2].int_type = 0xff; /* N/C */
1964 io_apic_ints[13].int_type = 0xff; /* N/C */
1965 #if !defined(APIC_MIXED_MODE)
1967 panic("sorry, can't support type 2 default yet");
1968 #endif /* APIC_MIXED_MODE */
1971 io_apic_ints[2].src_bus_irq = 0; /* ISA IRQ0 is on APIC INT 2 */
1974 io_apic_ints[0].int_type = 0xff; /* N/C */
1976 io_apic_ints[0].int_type = 3; /* vectored 8259 */
1977 #endif /* APIC_IO */
1981 * start each AP in our list
1984 start_all_aps(u_int boot_addr)
1987 u_char mpbiosreason;
1988 u_long mpbioswarmvec;
1989 struct mdglobaldata *gd;
1993 POSTCODE(START_ALL_APS_POST);
1995 /* initialize BSP's local APIC */
1999 /* install the AP 1st level boot code */
2000 install_ap_tramp(boot_addr);
2003 /* save the current value of the warm-start vector */
2004 mpbioswarmvec = *((u_long *) WARMBOOT_OFF);
2006 outb(CMOS_REG, BIOS_RESET);
2007 mpbiosreason = inb(CMOS_DATA);
2010 /* record BSP in CPU map */
2013 /* set up temporary P==V mapping for AP boot */
2014 /* XXX this is a hack, we should boot the AP on its own stack/PTD */
2015 kptbase = (uintptr_t)(void *)KPTphys;
2016 for (x = 0; x < NKPT; x++)
2017 PTD[x] = (pd_entry_t)(PG_V | PG_RW |
2018 ((kptbase + x * PAGE_SIZE) & PG_FRAME));
2022 for (x = 1; x <= mp_naps; ++x) {
2024 /* This is a bit verbose, it will go away soon. */
2026 /* first page of AP's private space */
2027 pg = x * i386_btop(sizeof(struct privatespace));
2029 /* allocate a new private data page */
2030 gd = (struct mdglobaldata *)kmem_alloc(kernel_map, PAGE_SIZE);
2032 /* wire it into the private page table page */
2033 SMPpt[pg] = (pt_entry_t)(PG_V | PG_RW | vtophys(gd));
2035 /* allocate and set up an idle stack data page */
2036 stack = (char *)kmem_alloc(kernel_map, UPAGES*PAGE_SIZE);
2037 for (i = 0; i < UPAGES; i++) {
2038 SMPpt[pg + 5 + i] = (pt_entry_t)
2039 (PG_V | PG_RW | vtophys(PAGE_SIZE * i + stack));
2042 SMPpt[pg + 1] = 0; /* *gd_CMAP1 */
2043 SMPpt[pg + 2] = 0; /* *gd_CMAP2 */
2044 SMPpt[pg + 3] = 0; /* *gd_CMAP3 */
2045 SMPpt[pg + 4] = 0; /* *gd_PMAP1 */
2047 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
2048 bzero(gd, sizeof(*gd));
2049 gd->mi.gd_prvspace = &CPU_prvspace[x];
2051 /* prime data page for it to use */
2052 mi_gdinit(&gd->mi, x);
2054 gd->gd_cpu_lockid = x << 24;
2055 gd->gd_CMAP1 = &SMPpt[pg + 1];
2056 gd->gd_CMAP2 = &SMPpt[pg + 2];
2057 gd->gd_CMAP3 = &SMPpt[pg + 3];
2058 gd->gd_PMAP1 = &SMPpt[pg + 4];
2059 gd->gd_CADDR1 = CPU_prvspace[x].CPAGE1;
2060 gd->gd_CADDR2 = CPU_prvspace[x].CPAGE2;
2061 gd->gd_CADDR3 = CPU_prvspace[x].CPAGE3;
2062 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[x].PPAGE1;
2064 /* setup a vector to our boot code */
2065 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
2066 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
2068 outb(CMOS_REG, BIOS_RESET);
2069 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
2073 * Setup the AP boot stack
2075 bootSTK = &CPU_prvspace[x].idlestack[UPAGES*PAGE_SIZE/2];
2078 /* attempt to start the Application Processor */
2079 CHECK_INIT(99); /* setup checkpoints */
2080 if (!start_ap(x, boot_addr)) {
2081 printf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x));
2082 CHECK_PRINT("trace"); /* show checkpoints */
2083 /* better panic as the AP may be running loose */
2084 printf("panic y/n? [y] ");
2085 if (cngetc() != 'n')
2088 CHECK_PRINT("trace"); /* show checkpoints */
2090 /* record its version info */
2091 cpu_apic_versions[x] = cpu_apic_versions[0];
2093 all_cpus |= (1 << x); /* record AP in CPU map */
2096 /* build our map of 'other' CPUs */
2097 mycpu->gd_other_cpus = all_cpus & ~(1 << mycpu->gd_cpuid);
2099 /* fill in our (BSP) APIC version */
2100 cpu_apic_versions[0] = lapic.version;
2102 /* restore the warmstart vector */
2103 *(u_long *) WARMBOOT_OFF = mpbioswarmvec;
2105 outb(CMOS_REG, BIOS_RESET);
2106 outb(CMOS_DATA, mpbiosreason);
2110 * NOTE! The idlestack for the BSP was setup by locore. Finish
2111 * up, clean out the P==V mapping we did earlier.
2113 for (x = 0; x < NKPT; x++)
2117 /* number of APs actually started */
2123 * load the 1st level AP boot code into base memory.
2126 /* targets for relocation */
2127 extern void bigJump(void);
2128 extern void bootCodeSeg(void);
2129 extern void bootDataSeg(void);
2130 extern void MPentry(void);
2131 extern u_int MP_GDT;
2132 extern u_int mp_gdtbase;
2135 install_ap_tramp(u_int boot_addr)
2138 int size = *(int *) ((u_long) & bootMP_size);
2139 u_char *src = (u_char *) ((u_long) bootMP);
2140 u_char *dst = (u_char *) boot_addr + KERNBASE;
2141 u_int boot_base = (u_int) bootMP;
2146 POSTCODE(INSTALL_AP_TRAMP_POST);
2148 for (x = 0; x < size; ++x)
2152 * modify addresses in code we just moved to basemem. unfortunately we
2153 * need fairly detailed info about mpboot.s for this to work. changes
2154 * to mpboot.s might require changes here.
2157 /* boot code is located in KERNEL space */
2158 dst = (u_char *) boot_addr + KERNBASE;
2160 /* modify the lgdt arg */
2161 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
2162 *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
2164 /* modify the ljmp target for MPentry() */
2165 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
2166 *dst32 = ((u_int) MPentry - KERNBASE);
2168 /* modify the target for boot code segment */
2169 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
2170 dst8 = (u_int8_t *) (dst16 + 1);
2171 *dst16 = (u_int) boot_addr & 0xffff;
2172 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2174 /* modify the target for boot data segment */
2175 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
2176 dst8 = (u_int8_t *) (dst16 + 1);
2177 *dst16 = (u_int) boot_addr & 0xffff;
2178 *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2183 * this function starts the AP (application processor) identified
2184 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
2185 * to accomplish this. This is necessary because of the nuances
2186 * of the different hardware we might encounter. It ain't pretty,
2187 * but it seems to work.
2190 start_ap(int logical_cpu, u_int boot_addr)
2195 u_long icr_lo, icr_hi;
2197 POSTCODE(START_AP_POST);
2199 /* get the PHYSICAL APIC ID# */
2200 physical_cpu = CPU_TO_ID(logical_cpu);
2202 /* calculate the vector */
2203 vector = (boot_addr >> 12) & 0xff;
2205 /* used as a watchpoint to signal AP startup */
2208 /* Make sure the target cpu sees everything */
2212 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
2213 * and running the target CPU. OR this INIT IPI might be latched (P5
2214 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
2218 /* setup the address for the target AP */
2219 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
2220 icr_hi |= (physical_cpu << 24);
2221 lapic.icr_hi = icr_hi;
2223 /* do an INIT IPI: assert RESET */
2224 icr_lo = lapic.icr_lo & 0xfff00000;
2225 lapic.icr_lo = icr_lo | 0x0000c500;
2227 /* wait for pending status end */
2228 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2231 /* do an INIT IPI: deassert RESET */
2232 lapic.icr_lo = icr_lo | 0x00008500;
2234 /* wait for pending status end */
2235 u_sleep(10000); /* wait ~10mS */
2236 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2240 * next we do a STARTUP IPI: the previous INIT IPI might still be
2241 * latched, (P5 bug) this 1st STARTUP would then terminate
2242 * immediately, and the previously started INIT IPI would continue. OR
2243 * the previous INIT IPI has already run. and this STARTUP IPI will
2244 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
2248 /* do a STARTUP IPI */
2249 lapic.icr_lo = icr_lo | 0x00000600 | vector;
2250 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2252 u_sleep(200); /* wait ~200uS */
2255 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
2256 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
2257 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
2258 * recognized after hardware RESET or INIT IPI.
2261 lapic.icr_lo = icr_lo | 0x00000600 | vector;
2262 while (lapic.icr_lo & APIC_DELSTAT_MASK)
2264 u_sleep(200); /* wait ~200uS */
2266 /* wait for it to start, see ap_init() */
2267 set_apic_timer(5000000);/* == 5 seconds */
2268 while (read_apic_timer()) {
2270 return 1; /* return SUCCESS */
2272 return 0; /* return FAILURE */
2277 * Flush the TLB on all other CPU's
2279 * XXX: Needs to handshake and wait for completion before proceding.
2284 #if defined(APIC_IO)
2285 if (smp_started && invltlb_ok)
2286 all_but_self_ipi(XINVLTLB_OFFSET);
2287 #endif /* APIC_IO */
2291 * When called the executing CPU will send an IPI to all other CPUs
2292 * requesting that they halt execution.
2294 * Usually (but not necessarily) called with 'other_cpus' as its arg.
2296 * - Signals all CPUs in map to stop.
2297 * - Waits for each to stop.
2304 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
2305 * from executing at same time.
2308 stop_cpus(u_int map)
2313 /* send the Xcpustop IPI to all CPUs in map */
2314 selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED);
2316 while ((stopped_cpus & map) != map)
2324 * Called by a CPU to restart stopped CPUs.
2326 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
2328 * - Signals all CPUs in map to restart.
2329 * - Waits for each to restart.
2337 restart_cpus(u_int map)
2342 started_cpus = map; /* signal other cpus to restart */
2344 while ((stopped_cpus & map) != 0) /* wait for each to clear its bit */
2350 int smp_active = 0; /* are the APs allowed to run? */
2351 SYSCTL_INT(_machdep, OID_AUTO, smp_active, CTLFLAG_RW, &smp_active, 0, "");
2353 /* XXX maybe should be hw.ncpu */
2354 static int smp_cpus = 1; /* how many cpu's running */
2355 SYSCTL_INT(_machdep, OID_AUTO, smp_cpus, CTLFLAG_RD, &smp_cpus, 0, "");
2357 int invltlb_ok = 0; /* throttle smp_invltlb() till safe */
2358 SYSCTL_INT(_machdep, OID_AUTO, invltlb_ok, CTLFLAG_RW, &invltlb_ok, 0, "");
2360 /* Warning: Do not staticize. Used from swtch.s */
2361 int do_page_zero_idle = 1; /* bzero pages for fun and profit in idleloop */
2362 SYSCTL_INT(_machdep, OID_AUTO, do_page_zero_idle, CTLFLAG_RW,
2363 &do_page_zero_idle, 0, "");
2365 /* Is forwarding of a interrupt to the CPU holding the ISR lock enabled ? */
2366 int forward_irq_enabled = 1;
2367 SYSCTL_INT(_machdep, OID_AUTO, forward_irq_enabled, CTLFLAG_RW,
2368 &forward_irq_enabled, 0, "");
2370 /* Enable forwarding of a signal to a process running on a different CPU */
2371 static int forward_signal_enabled = 1;
2372 SYSCTL_INT(_machdep, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
2373 &forward_signal_enabled, 0, "");
2375 /* Enable forwarding of roundrobin to all other cpus */
2376 static int forward_roundrobin_enabled = 1;
2377 SYSCTL_INT(_machdep, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
2378 &forward_roundrobin_enabled, 0, "");
2381 * This is called once the mpboot code has gotten us properly relocated
2382 * and the MMU turned on, etc. ap_init() is actually the idle thread,
2383 * and when it returns the scheduler will call the real cpu_idle() main
2384 * loop for the idlethread. Interrupts are disabled on entry and should
2385 * remain disabled at return.
2394 * Signal the BSP that we have started up successfully by incrementing
2395 * ncpus. Note that we do not hold the BGL yet. The BSP is waiting
2401 * Get the MP lock so we can finish initializing.
2403 while (cpu_try_mplock() == 0)
2406 /* BSP may have changed PTD while we're waiting for the lock */
2409 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2413 /* Build our map of 'other' CPUs. */
2414 mycpu->gd_other_cpus = all_cpus & ~(1 << mycpu->gd_cpuid);
2416 printf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
2418 /* set up CPU registers and state */
2421 /* set up FPU state on the AP */
2422 npxinit(__INITIAL_NPXCW__);
2424 /* set up SSE registers */
2427 /* A quick check from sanity claus */
2428 apic_id = (apic_id_to_logical[(lapic.id & 0x0f000000) >> 24]);
2429 if (mycpu->gd_cpuid != apic_id) {
2430 printf("SMP: cpuid = %d\n", mycpu->gd_cpuid);
2431 printf("SMP: apic_id = %d\n", apic_id);
2432 printf("PTD[MPPTDI] = %p\n", (void *)PTD[MPPTDI]);
2433 panic("cpuid mismatch! boom!!");
2436 /* Init local apic for irq's */
2439 /* Set memory range attributes for this CPU to match the BSP */
2440 mem_range_AP_init();
2443 * Since we have the BGL if smp_cpus matches ncpus then we are
2444 * the last AP to get to this point and we can enable IPI's,
2445 * tlb shootdowns, freezes, and so forth.
2448 if (smp_cpus == ncpus) {
2450 smp_started = 1; /* enable IPI's, tlb shootdown, freezes etc */
2451 smp_active = 1; /* historic */
2455 * The idle loop doesn't expect the BGL to be held and while
2456 * lwkt_switch() normally cleans things up this is a special case
2457 * because we returning almost directly into the idle loop.
2464 #define CHECKSTATE_USER 0
2465 #define CHECKSTATE_SYS 1
2466 #define CHECKSTATE_INTR 2
2468 /* Do not staticize. Used from apic_vector.s */
2469 struct thread *checkstate_curtd[MAXCPU];
2470 int checkstate_cpustate[MAXCPU];
2471 u_long checkstate_pc[MAXCPU];
2473 #define PC_TO_INDEX(pc, prof) \
2474 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
2475 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
2479 addupc_intr_forwarded(struct proc *p, int id, int *astmap)
2485 pc = checkstate_pc[id];
2486 prof = &p->p_stats->p_prof;
2487 if (pc >= prof->pr_off &&
2488 (i = PC_TO_INDEX(pc, prof)) < prof->pr_size) {
2489 if ((p->p_flag & P_OWEUPC) == 0) {
2492 p->p_flag |= P_OWEUPC;
2494 *astmap |= (1 << id);
2500 forwarded_statclock(int id, int pscnt, int *astmap)
2503 struct pstats *pstats;
2510 register struct gmonparam *g;
2514 t = checkstate_curtd[id];
2515 cpustate = checkstate_cpustate[id];
2518 case CHECKSTATE_USER:
2519 if (td->td_proc && td->td_proc->p_flag & P_PROFIL)
2520 addupc_intr_forwarded(td->td_proc, id, astmap);
2524 if (p->p_nice > NZERO)
2529 case CHECKSTATE_SYS:
2532 * Kernel statistics are just like addupc_intr, only easier.
2535 if (g->state == GMON_PROF_ON) {
2536 i = checkstate_pc[id] - g->lowpc;
2537 if (i < g->textsize) {
2538 i /= HISTFRACTION * sizeof(*g->kcount);
2553 case CHECKSTATE_INTR:
2557 * Kernel statistics are just like addupc_intr, only easier.
2560 if (g->state == GMON_PROF_ON) {
2561 i = checkstate_pc[id] - g->lowpc;
2562 if (i < g->textsize) {
2563 i /= HISTFRACTION * sizeof(*g->kcount);
2577 /* Update resource usage integrals and maximums. */
2578 if ((pstats = p->p_stats) != NULL &&
2579 (ru = &pstats->p_ru) != NULL &&
2580 (vm = p->p_vmspace) != NULL) {
2581 ru->ru_ixrss += pgtok(vm->vm_tsize);
2582 ru->ru_idrss += pgtok(vm->vm_dsize);
2583 ru->ru_isrss += pgtok(vm->vm_ssize);
2584 rss = pgtok(vmspace_resident_count(vm));
2585 if (ru->ru_maxrss < rss)
2586 ru->ru_maxrss = rss;
2593 forward_statclock(int pscnt)
2599 /* Kludge. We don't yet have separate locks for the interrupts
2600 * and the kernel. This means that we cannot let the other processors
2601 * handle complex interrupts while inhibiting them from entering
2602 * the kernel in a non-interrupt context.
2604 * What we can do, without changing the locking mechanisms yet,
2605 * is letting the other processors handle a very simple interrupt
2606 * (wich determines the processor states), and do the main
2610 if (!smp_started || !invltlb_ok || cold || panicstr)
2613 printf("forward_statclock\n");
2614 /* Step 1: Probe state (user, cpu, interrupt, spinlock, idle ) */
2616 map = mycpu->gd_other_cpus & ~stopped_cpus ;
2617 checkstate_probed_cpus = 0;
2619 selected_apic_ipi(map,
2620 XCPUCHECKSTATE_OFFSET, APIC_DELMODE_FIXED);
2623 while (checkstate_probed_cpus != map) {
2627 #ifdef BETTER_CLOCK_DIAGNOSTIC
2628 printf("forward_statclock: checkstate %x\n",
2629 checkstate_probed_cpus);
2636 * Step 2: walk through other processors processes, update ticks and
2641 for (id = 0; id < ncpus; id++) {
2642 if (id == mycpu->gd_cpuid)
2644 if (((1 << id) & checkstate_probed_cpus) == 0)
2646 forwarded_statclock(id, pscnt, &map);
2649 checkstate_need_ast |= map;
2650 selected_apic_ipi(map, XCPUAST_OFFSET, APIC_DELMODE_FIXED);
2652 while ((checkstate_need_ast & map) != 0) {
2656 #ifdef BETTER_CLOCK_DIAGNOSTIC
2657 printf("forward_statclock: dropped ast 0x%x\n",
2658 checkstate_need_ast & map);
2667 forward_hardclock(int pscnt)
2673 struct pstats *pstats;
2677 /* Kludge. We don't yet have separate locks for the interrupts
2678 * and the kernel. This means that we cannot let the other processors
2679 * handle complex interrupts while inhibiting them from entering
2680 * the kernel in a non-interrupt context.
2682 * What we can do, without changing the locking mechanisms yet,
2683 * is letting the other processors handle a very simple interrupt
2684 * (wich determines the processor states), and do the main
2688 if (!smp_started || !invltlb_ok || cold || panicstr)
2691 /* Step 1: Probe state (user, cpu, interrupt, spinlock, idle) */
2693 map = mycpu->gd_other_cpus & ~stopped_cpus ;
2694 checkstate_probed_cpus = 0;
2696 selected_apic_ipi(map,
2697 XCPUCHECKSTATE_OFFSET, APIC_DELMODE_FIXED);
2700 while (checkstate_probed_cpus != map) {
2704 #ifdef BETTER_CLOCK_DIAGNOSTIC
2705 printf("forward_hardclock: checkstate %x\n",
2706 checkstate_probed_cpus);
2713 * Step 2: walk through other processors processes, update virtual
2714 * timer and profiling timer. If stathz == 0, also update ticks and
2719 for (id = 0; id < ncpus; id++) {
2720 if (id == mycpu->gd_cpuid)
2722 if (((1 << id) & checkstate_probed_cpus) == 0)
2724 printf("forward_hardclock\n");
2726 p = checkstate_curproc[id];
2728 pstats = p->p_stats;
2729 if (checkstate_cpustate[id] == CHECKSTATE_USER &&
2730 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
2731 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
2732 psignal(p, SIGVTALRM);
2735 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
2736 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
2737 psignal(p, SIGPROF);
2742 forwarded_statclock( id, pscnt, &map);
2747 checkstate_need_ast |= map;
2748 selected_apic_ipi(map, XCPUAST_OFFSET, APIC_DELMODE_FIXED);
2750 while ((checkstate_need_ast & map) != 0) {
2754 #ifdef BETTER_CLOCK_DIAGNOSTIC
2755 printf("forward_hardclock: dropped ast 0x%x\n",
2756 checkstate_need_ast & map);
2764 #endif /* BETTER_CLOCK */
2767 forward_signal(struct proc *p)
2769 /* YYY forward_signal */
2775 /* Kludge. We don't yet have separate locks for the interrupts
2776 * and the kernel. This means that we cannot let the other processors
2777 * handle complex interrupts while inhibiting them from entering
2778 * the kernel in a non-interrupt context.
2780 * What we can do, without changing the locking mechanisms yet,
2781 * is letting the other processors handle a very simple interrupt
2782 * (wich determines the processor states), and do the main
2786 if (!smp_started || !invltlb_ok || cold || panicstr)
2788 if (!forward_signal_enabled)
2791 if (p->p_stat != SRUN)
2797 checkstate_need_ast |= map;
2798 selected_apic_ipi(map, XCPUAST_OFFSET, APIC_DELMODE_FIXED);
2800 while ((checkstate_need_ast & map) != 0) {
2805 printf("forward_signal: dropped ast 0x%x\n",
2806 checkstate_need_ast & map);
2811 if (id == p->p_oncpu)
2818 forward_roundrobin(void)
2820 /* YYY forward_roundrobin */
2825 if (!smp_started || !invltlb_ok || cold || panicstr)
2827 if (!forward_roundrobin_enabled)
2829 resched_cpus |= mycpu->gd_other_cpus;
2830 map = mycpu->gd_other_cpus & ~stopped_cpus ;
2832 selected_apic_ipi(map, XCPUAST_OFFSET, APIC_DELMODE_FIXED);
2834 (void) all_but_self_ipi(XCPUAST_OFFSET);
2837 while ((checkstate_need_ast & map) != 0) {
2842 printf("forward_roundrobin: dropped ast 0x%x\n",
2843 checkstate_need_ast & map);
2851 #ifdef APIC_INTR_REORDER
2853 * Maintain mapping from softintr vector to isr bit in local apic.
2856 set_lapic_isrloc(int intr, int vector)
2858 if (intr < 0 || intr > 32)
2859 panic("set_apic_isrloc: bad intr argument: %d",intr);
2860 if (vector < ICU_OFFSET || vector > 255)
2861 panic("set_apic_isrloc: bad vector argument: %d",vector);
2862 apic_isrbit_location[intr].location = &lapic.isr0 + ((vector>>5)<<2);
2863 apic_isrbit_location[intr].bit = (1<<(vector & 31));
2868 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
2869 * (if specified), rendezvous, execute the action function (if specified),
2870 * rendezvous again, execute the teardown function (if specified), and then
2873 * Note that the supplied external functions _must_ be reentrant and aware
2874 * that they are running in parallel and in an unknown lock context.
2876 static void (*smp_rv_setup_func)(void *arg);
2877 static void (*smp_rv_action_func)(void *arg);
2878 static void (*smp_rv_teardown_func)(void *arg);
2879 static void *smp_rv_func_arg;
2880 static volatile int smp_rv_waiters[2];
2883 smp_rendezvous_action(void)
2885 /* setup function */
2886 if (smp_rv_setup_func != NULL)
2887 smp_rv_setup_func(smp_rv_func_arg);
2888 /* spin on entry rendezvous */
2889 atomic_add_int(&smp_rv_waiters[0], 1);
2890 while (smp_rv_waiters[0] < ncpus)
2892 /* action function */
2893 if (smp_rv_action_func != NULL)
2894 smp_rv_action_func(smp_rv_func_arg);
2895 /* spin on exit rendezvous */
2896 atomic_add_int(&smp_rv_waiters[1], 1);
2897 while (smp_rv_waiters[1] < ncpus)
2899 /* teardown function */
2900 if (smp_rv_teardown_func != NULL)
2901 smp_rv_teardown_func(smp_rv_func_arg);
2905 smp_rendezvous(void (* setup_func)(void *),
2906 void (* action_func)(void *),
2907 void (* teardown_func)(void *),
2910 /* obtain rendezvous lock. This disables interrupts */
2911 spin_lock(&smp_rv_spinlock); /* XXX sleep here? NOWAIT flag? */
2913 /* set static function pointers */
2914 smp_rv_setup_func = setup_func;
2915 smp_rv_action_func = action_func;
2916 smp_rv_teardown_func = teardown_func;
2917 smp_rv_func_arg = arg;
2918 smp_rv_waiters[0] = 0;
2919 smp_rv_waiters[1] = 0;
2921 /* signal other processors, which will enter the IPI with interrupts off */
2922 all_but_self_ipi(XRENDEZVOUS_OFFSET);
2924 /* call executor function */
2925 smp_rendezvous_action();
2928 spin_unlock(&smp_rv_spinlock);