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/cputypes.h>
57 #include <machine_base/apic/mpapic.h>
58 #include <machine/psl.h>
59 #include <machine/segments.h>
60 #include <machine/tss.h>
61 #include <machine/specialreg.h>
62 #include <machine/globaldata.h>
64 #include <machine/md_var.h> /* setidt() */
65 #include <machine_base/icu/icu.h> /* IPIs */
66 #include <machine_base/isa/intr_machdep.h> /* IPIs */
68 #define FIXUP_EXTRA_APIC_INTS 8 /* additional entries we may create */
70 #define WARMBOOT_TARGET 0
71 #define WARMBOOT_OFF (KERNBASE + 0x0467)
72 #define WARMBOOT_SEG (KERNBASE + 0x0469)
74 #define BIOS_BASE (0xf0000)
75 #define BIOS_BASE2 (0xe0000)
76 #define BIOS_SIZE (0x10000)
77 #define BIOS_COUNT (BIOS_SIZE/4)
79 #define CMOS_REG (0x70)
80 #define CMOS_DATA (0x71)
81 #define BIOS_RESET (0x0f)
82 #define BIOS_WARM (0x0a)
84 #define PROCENTRY_FLAG_EN 0x01
85 #define PROCENTRY_FLAG_BP 0x02
86 #define IOAPICENTRY_FLAG_EN 0x01
89 /* MP Floating Pointer Structure */
90 typedef struct MPFPS
{
103 /* MP Configuration Table Header */
104 typedef struct MPCTH
{
106 u_short base_table_length
;
110 u_char product_id
[12];
111 void *oem_table_pointer
;
112 u_short oem_table_size
;
115 u_short extended_table_length
;
116 u_char extended_table_checksum
;
121 typedef struct PROCENTRY
{
126 u_long cpu_signature
;
127 u_long feature_flags
;
132 typedef struct BUSENTRY
{
138 typedef struct IOAPICENTRY
{
144 } *io_apic_entry_ptr
;
146 typedef struct INTENTRY
{
156 /* descriptions of MP basetable entries */
157 typedef struct BASETABLE_ENTRY
{
166 vm_size_t mp_cth_mapsz
;
169 typedef int (*mptable_iter_func
)(void *, const void *, int);
172 * this code MUST be enabled here and in mpboot.s.
173 * it follows the very early stages of AP boot by placing values in CMOS ram.
174 * it NORMALLY will never be needed and thus the primitive method for enabling.
177 #if defined(CHECK_POINTS)
178 #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA))
179 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
181 #define CHECK_INIT(D); \
182 CHECK_WRITE(0x34, (D)); \
183 CHECK_WRITE(0x35, (D)); \
184 CHECK_WRITE(0x36, (D)); \
185 CHECK_WRITE(0x37, (D)); \
186 CHECK_WRITE(0x38, (D)); \
187 CHECK_WRITE(0x39, (D));
189 #define CHECK_PRINT(S); \
190 kprintf("%s: %d, %d, %d, %d, %d, %d\n", \
199 #else /* CHECK_POINTS */
201 #define CHECK_INIT(D)
202 #define CHECK_PRINT(S)
204 #endif /* CHECK_POINTS */
207 * Values to send to the POST hardware.
209 #define MP_BOOTADDRESS_POST 0x10
210 #define MP_PROBE_POST 0x11
211 #define MPTABLE_PASS1_POST 0x12
213 #define MP_START_POST 0x13
214 #define MP_ENABLE_POST 0x14
215 #define MPTABLE_PASS2_POST 0x15
217 #define START_ALL_APS_POST 0x16
218 #define INSTALL_AP_TRAMP_POST 0x17
219 #define START_AP_POST 0x18
221 #define MP_ANNOUNCE_POST 0x19
223 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
224 int current_postcode
;
226 /** XXX FIXME: what system files declare these??? */
227 extern struct region_descriptor r_gdt
, r_idt
;
229 int mp_naps
; /* # of Applications processors */
231 static int mp_nbusses
; /* # of busses */
232 int mp_napics
; /* # of IO APICs */
235 vm_offset_t io_apic_address
[NAPICID
]; /* NAPICID is more than enough */
236 u_int32_t
*io_apic_versions
;
240 u_int32_t cpu_apic_versions
[MAXCPU
];
242 extern int64_t tsc_offsets
[];
244 extern u_long ebda_addr
;
247 struct apic_intmapinfo int_to_apicintpin
[APIC_INTMAPSIZE
];
251 * APIC ID logical/physical mapping structures.
252 * We oversize these to simplify boot-time config.
254 int cpu_num_to_apic_id
[NAPICID
];
256 int io_num_to_apic_id
[NAPICID
];
258 int apic_id_to_logical
[NAPICID
];
260 /* AP uses this during bootstrap. Do not staticize. */
264 /* Hotwire a 0->4MB V==P mapping */
265 extern pt_entry_t
*KPTphys
;
268 * SMP page table page. Setup by locore to point to a page table
269 * page from which we allocate per-cpu privatespace areas io_apics,
273 #define IO_MAPPING_START_INDEX \
274 (SMP_MAXCPU * sizeof(struct privatespace) / PAGE_SIZE)
276 extern pt_entry_t
*SMPpt
;
277 static int SMPpt_alloc_index
= IO_MAPPING_START_INDEX
;
279 struct pcb stoppcbs
[MAXCPU
];
281 static basetable_entry basetable_entry_types
[] =
283 {0, 20, "Processor"},
291 * Local data and functions.
294 static u_int boot_address
;
295 static u_int base_memory
;
296 static int mp_finish
;
298 static void mp_enable(u_int boot_addr
);
300 static int mptable_iterate_entries(const mpcth_t
,
301 mptable_iter_func
, void *);
302 static int mptable_probe(void);
303 static int mptable_search(void);
304 static int mptable_check(vm_paddr_t
);
305 static int mptable_search_sig(u_int32_t target
, int count
);
306 static int mptable_hyperthread_fixup(u_int
, int);
308 static void mptable_pass1(struct mptable_pos
*);
309 static void mptable_pass2(struct mptable_pos
*);
310 static void mptable_default(int type
);
311 static void mptable_fix(void);
313 static int mptable_map(struct mptable_pos
*, vm_paddr_t
);
314 static void mptable_unmap(struct mptable_pos
*);
315 static void mptable_imcr(struct mptable_pos
*);
317 static int mptable_lapic_probe(struct lapic_enumerator
*);
318 static void mptable_lapic_enumerate(struct lapic_enumerator
*);
319 static void mptable_lapic_default(void);
322 static void setup_apic_irq_mapping(void);
323 static int apic_int_is_bus_type(int intr
, int bus_type
);
325 static int start_all_aps(u_int boot_addr
);
326 static void install_ap_tramp(u_int boot_addr
);
327 static int start_ap(struct mdglobaldata
*gd
, u_int boot_addr
);
329 static cpumask_t smp_startup_mask
= 1; /* which cpus have been started */
330 cpumask_t smp_active_mask
= 1; /* which cpus are ready for IPIs etc? */
331 SYSCTL_INT(_machdep
, OID_AUTO
, smp_active
, CTLFLAG_RD
, &smp_active_mask
, 0, "");
334 * Calculate usable address in base memory for AP trampoline code.
337 mp_bootaddress(u_int basemem
)
339 POSTCODE(MP_BOOTADDRESS_POST
);
341 base_memory
= basemem
;
343 boot_address
= base_memory
& ~0xfff; /* round down to 4k boundary */
344 if ((base_memory
- boot_address
) < bootMP_size
)
345 boot_address
-= 4096; /* not enough, lower by 4k */
356 mpfps_paddr
= mptable_search();
357 if (mptable_check(mpfps_paddr
))
364 * Look for an Intel MP spec table (ie, SMP capable hardware).
373 * Make sure our SMPpt[] page table is big enough to hold all the
376 KKASSERT(IO_MAPPING_START_INDEX
< NPTEPG
- 2);
378 POSTCODE(MP_PROBE_POST
);
380 /* see if EBDA exists */
381 if (ebda_addr
!= 0) {
382 /* search first 1K of EBDA */
383 target
= (u_int32_t
)ebda_addr
;
384 if ((x
= mptable_search_sig(target
, 1024 / 4)) > 0)
387 /* last 1K of base memory, effective 'top of base' passed in */
388 target
= (u_int32_t
)(base_memory
- 0x400);
389 if ((x
= mptable_search_sig(target
, 1024 / 4)) > 0)
393 /* search the BIOS */
394 target
= (u_int32_t
)BIOS_BASE
;
395 if ((x
= mptable_search_sig(target
, BIOS_COUNT
)) > 0)
398 /* search the extended BIOS */
399 target
= (u_int32_t
)BIOS_BASE2
;
400 if ((x
= mptable_search_sig(target
, BIOS_COUNT
)) > 0)
407 struct mptable_check_cbarg
{
413 mptable_check_callback(void *xarg
, const void *pos
, int type
)
415 const struct PROCENTRY
*ent
;
416 struct mptable_check_cbarg
*arg
= xarg
;
422 if ((ent
->cpu_flags
& PROCENTRY_FLAG_EN
) == 0)
426 if (ent
->cpu_flags
& PROCENTRY_FLAG_BP
) {
427 if (arg
->found_bsp
) {
428 kprintf("more than one BSP in base MP table\n");
437 mptable_check(vm_paddr_t mpfps_paddr
)
439 struct mptable_pos mpt
;
440 struct mptable_check_cbarg arg
;
444 if (mpfps_paddr
== 0)
447 error
= mptable_map(&mpt
, mpfps_paddr
);
451 if (mpt
.mp_fps
->mpfb1
!= 0)
459 if (cth
->apic_address
== 0)
462 bzero(&arg
, sizeof(arg
));
463 error
= mptable_iterate_entries(cth
, mptable_check_callback
, &arg
);
465 if (arg
.cpu_count
== 0) {
466 kprintf("MP table contains no processor entries\n");
468 } else if (!arg
.found_bsp
) {
469 kprintf("MP table does not contains BSP entry\n");
479 mptable_iterate_entries(const mpcth_t cth
, mptable_iter_func func
, void *arg
)
481 int count
, total_size
;
482 const void *position
;
484 KKASSERT(cth
->base_table_length
>= sizeof(struct MPCTH
));
485 total_size
= cth
->base_table_length
- sizeof(struct MPCTH
);
486 position
= (const uint8_t *)cth
+ sizeof(struct MPCTH
);
487 count
= cth
->entry_count
;
492 KKASSERT(total_size
>= 0);
493 if (total_size
== 0) {
494 kprintf("invalid base MP table, "
495 "entry count and length mismatch\n");
499 type
= *(const uint8_t *)position
;
501 case 0: /* processor_entry */
502 case 1: /* bus_entry */
503 case 2: /* io_apic_entry */
504 case 3: /* int_entry */
505 case 4: /* int_entry */
508 kprintf("unknown base MP table entry type %d\n", type
);
512 if (total_size
< basetable_entry_types
[type
].length
) {
513 kprintf("invalid base MP table length, "
514 "does not contain all entries\n");
517 total_size
-= basetable_entry_types
[type
].length
;
519 error
= func(arg
, position
, type
);
523 position
= (const uint8_t *)position
+
524 basetable_entry_types
[type
].length
;
531 * Startup the SMP processors.
536 POSTCODE(MP_START_POST
);
537 mp_enable(boot_address
);
542 * Print various information about the SMP system hardware and setup.
549 POSTCODE(MP_ANNOUNCE_POST
);
551 kprintf("DragonFly/MP: Multiprocessor motherboard\n");
552 kprintf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
553 kprintf(", version: 0x%08x\n", cpu_apic_versions
[0]);
554 for (x
= 1; x
<= mp_naps
; ++x
) {
555 kprintf(" cpu%d (AP): apic id: %2d", x
, CPU_TO_ID(x
));
556 kprintf(", version: 0x%08x\n", cpu_apic_versions
[x
]);
560 for (x
= 0; x
< mp_napics
; ++x
) {
561 kprintf(" io%d (APIC): apic id: %2d", x
, IO_TO_ID(x
));
562 kprintf(", version: 0x%08x", io_apic_versions
[x
]);
563 kprintf(", at 0x%08lx\n", io_apic_address
[x
]);
566 kprintf(" Warning: APIC I/O disabled\n");
571 * AP cpu's call this to sync up protected mode.
573 * WARNING! We must ensure that the cpu is sufficiently initialized to
574 * be able to use to the FP for our optimized bzero/bcopy code before
575 * we enter more mainstream C code.
577 * WARNING! %fs is not set up on entry. This routine sets up %fs.
583 int x
, myid
= bootAP
;
585 struct mdglobaldata
*md
;
586 struct privatespace
*ps
;
588 ps
= &CPU_prvspace
[myid
];
590 gdt_segs
[GPRIV_SEL
].ssd_base
= (int)ps
;
591 gdt_segs
[GPROC0_SEL
].ssd_base
=
592 (int) &ps
->mdglobaldata
.gd_common_tss
;
593 ps
->mdglobaldata
.mi
.gd_prvspace
= ps
;
595 for (x
= 0; x
< NGDT
; x
++) {
596 ssdtosd(&gdt_segs
[x
], &gdt
[myid
* NGDT
+ x
].sd
);
599 r_gdt
.rd_limit
= NGDT
* sizeof(gdt
[0]) - 1;
600 r_gdt
.rd_base
= (int) &gdt
[myid
* NGDT
];
601 lgdt(&r_gdt
); /* does magic intra-segment return */
606 mdcpu
->gd_currentldt
= _default_ldt
;
608 gsel_tss
= GSEL(GPROC0_SEL
, SEL_KPL
);
609 gdt
[myid
* NGDT
+ GPROC0_SEL
].sd
.sd_type
= SDT_SYS386TSS
;
611 md
= mdcpu
; /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
613 md
->gd_common_tss
.tss_esp0
= 0; /* not used until after switch */
614 md
->gd_common_tss
.tss_ss0
= GSEL(GDATA_SEL
, SEL_KPL
);
615 md
->gd_common_tss
.tss_ioopt
= (sizeof md
->gd_common_tss
) << 16;
616 md
->gd_tss_gdt
= &gdt
[myid
* NGDT
+ GPROC0_SEL
].sd
;
617 md
->gd_common_tssd
= *md
->gd_tss_gdt
;
621 * Set to a known state:
622 * Set by mpboot.s: CR0_PG, CR0_PE
623 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
626 cr0
&= ~(CR0_CD
| CR0_NW
| CR0_EM
);
628 pmap_set_opt(); /* PSE/4MB pages, etc */
630 /* set up CPU registers and state */
633 /* set up FPU state on the AP */
634 npxinit(__INITIAL_NPXCW__
);
636 /* set up SSE registers */
640 /*******************************************************************
641 * local functions and data
645 * start the SMP system
648 mp_enable(u_int boot_addr
)
654 vm_paddr_t mpfps_paddr
;
655 struct mptable_pos mpt
;
657 POSTCODE(MP_ENABLE_POST
);
661 mpfps_paddr
= mptable_probe();
663 mptable_map(&mpt
, mpfps_paddr
);
670 panic("no MP table, disable APIC_IO!\n");
672 mptable_map(&mpt
, mpfps_paddr
);
675 * Examine the MP table for needed info
682 /* Post scan cleanup */
685 setup_apic_irq_mapping();
687 /* fill the LOGICAL io_apic_versions table */
688 for (apic
= 0; apic
< mp_napics
; ++apic
) {
689 ux
= io_apic_read(apic
, IOAPIC_VER
);
690 io_apic_versions
[apic
] = ux
;
691 io_apic_set_id(apic
, IO_TO_ID(apic
));
694 /* program each IO APIC in the system */
695 for (apic
= 0; apic
< mp_napics
; ++apic
)
696 if (io_apic_setup(apic
) < 0)
697 panic("IO APIC setup failure");
702 * These are required for SMP operation
705 /* install a 'Spurious INTerrupt' vector */
706 setidt(XSPURIOUSINT_OFFSET
, Xspuriousint
,
707 SDT_SYS386IGT
, SEL_KPL
, GSEL(GCODE_SEL
, SEL_KPL
));
709 /* install an inter-CPU IPI for TLB invalidation */
710 setidt(XINVLTLB_OFFSET
, Xinvltlb
,
711 SDT_SYS386IGT
, SEL_KPL
, GSEL(GCODE_SEL
, SEL_KPL
));
713 /* install an inter-CPU IPI for IPIQ messaging */
714 setidt(XIPIQ_OFFSET
, Xipiq
,
715 SDT_SYS386IGT
, SEL_KPL
, GSEL(GCODE_SEL
, SEL_KPL
));
717 /* install a timer vector */
718 setidt(XTIMER_OFFSET
, Xtimer
,
719 SDT_SYS386IGT
, SEL_KPL
, GSEL(GCODE_SEL
, SEL_KPL
));
721 /* install an inter-CPU IPI for CPU stop/restart */
722 setidt(XCPUSTOP_OFFSET
, Xcpustop
,
723 SDT_SYS386IGT
, SEL_KPL
, GSEL(GCODE_SEL
, SEL_KPL
));
725 /* start each Application Processor */
726 start_all_aps(boot_addr
);
731 * look for the MP spec signature
734 /* string defined by the Intel MP Spec as identifying the MP table */
735 #define MP_SIG 0x5f504d5f /* _MP_ */
736 #define NEXT(X) ((X) += 4)
738 mptable_search_sig(u_int32_t target
, int count
)
744 KKASSERT(target
!= 0);
746 map_size
= count
* sizeof(u_int32_t
);
747 addr
= pmap_mapdev((vm_paddr_t
)target
, map_size
);
750 for (x
= 0; x
< count
; NEXT(x
)) {
751 if (addr
[x
] == MP_SIG
) {
752 /* make array index a byte index */
753 ret
= target
+ (x
* sizeof(u_int32_t
));
758 pmap_unmapdev((vm_offset_t
)addr
, map_size
);
763 typedef struct BUSDATA
{
765 enum busTypes bus_type
;
768 typedef struct INTDATA
{
778 typedef struct BUSTYPENAME
{
785 static bus_type_name bus_type_table
[] =
791 {UNKNOWN_BUSTYPE
, "---"},
794 {UNKNOWN_BUSTYPE
, "---"},
795 {UNKNOWN_BUSTYPE
, "---"},
796 {UNKNOWN_BUSTYPE
, "---"},
797 {UNKNOWN_BUSTYPE
, "---"},
798 {UNKNOWN_BUSTYPE
, "---"},
800 {UNKNOWN_BUSTYPE
, "---"},
801 {UNKNOWN_BUSTYPE
, "---"},
802 {UNKNOWN_BUSTYPE
, "---"},
803 {UNKNOWN_BUSTYPE
, "---"},
805 {UNKNOWN_BUSTYPE
, "---"}
807 /* from MP spec v1.4, table 5-1 */
808 static int default_data
[7][5] =
810 /* nbus, id0, type0, id1, type1 */
811 {1, 0, ISA
, 255, 255},
812 {1, 0, EISA
, 255, 255},
813 {1, 0, EISA
, 255, 255},
814 {1, 0, MCA
, 255, 255},
816 {2, 0, EISA
, 1, PCI
},
822 static bus_datum
*bus_data
;
824 /* the IO INT data, one entry per possible APIC INTerrupt */
825 static io_int
*io_apic_ints
;
830 static int processor_entry (const struct PROCENTRY
*entry
, int cpu
);
832 static int bus_entry (const struct BUSENTRY
*entry
, int bus
);
833 static int io_apic_entry (const struct IOAPICENTRY
*entry
, int apic
);
834 static int int_entry (const struct INTENTRY
*entry
, int intr
);
835 static int lookup_bus_type (char *name
);
841 mptable_ioapic_pass1_callback(void *xarg
, const void *pos
, int type
)
843 const struct IOAPICENTRY
*ioapic_ent
;
846 case 1: /* bus_entry */
850 case 2: /* io_apic_entry */
852 if (ioapic_ent
->apic_flags
& IOAPICENTRY_FLAG_EN
) {
853 io_apic_address
[mp_napics
++] =
854 (vm_offset_t
)ioapic_ent
->apic_address
;
858 case 3: /* int_entry */
866 * 1st pass on motherboard's Intel MP specification table.
875 mptable_pass1(struct mptable_pos
*mpt
)
880 POSTCODE(MPTABLE_PASS1_POST
);
883 KKASSERT(fps
!= NULL
);
885 /* clear various tables */
886 for (x
= 0; x
< NAPICID
; ++x
)
887 io_apic_address
[x
] = ~0; /* IO APIC address table */
893 /* check for use of 'default' configuration */
894 if (fps
->mpfb1
!= 0) {
895 io_apic_address
[0] = DEFAULT_IO_APIC_BASE
;
896 mp_nbusses
= default_data
[fps
->mpfb1
- 1][0];
902 error
= mptable_iterate_entries(mpt
->mp_cth
,
903 mptable_ioapic_pass1_callback
, NULL
);
905 panic("mptable_iterate_entries(ioapic_pass1) failed\n");
909 struct mptable_ioapic2_cbarg
{
916 mptable_ioapic_pass2_callback(void *xarg
, const void *pos
, int type
)
918 struct mptable_ioapic2_cbarg
*arg
= xarg
;
922 if (bus_entry(pos
, arg
->bus
))
927 if (io_apic_entry(pos
, arg
->apic
))
932 if (int_entry(pos
, arg
->intr
))
940 * 2nd pass on motherboard's Intel MP specification table.
943 * ID_TO_IO(N), phy APIC ID to log CPU/IO table
944 * IO_TO_ID(N), logical IO to APIC ID table
949 mptable_pass2(struct mptable_pos
*mpt
)
951 struct mptable_ioapic2_cbarg arg
;
955 POSTCODE(MPTABLE_PASS2_POST
);
958 KKASSERT(fps
!= NULL
);
960 MALLOC(io_apic_versions
, u_int32_t
*, sizeof(u_int32_t
) * mp_napics
,
962 MALLOC(ioapic
, volatile ioapic_t
**, sizeof(ioapic_t
*) * mp_napics
,
963 M_DEVBUF
, M_WAITOK
| M_ZERO
);
964 MALLOC(io_apic_ints
, io_int
*, sizeof(io_int
) * (nintrs
+ FIXUP_EXTRA_APIC_INTS
),
966 MALLOC(bus_data
, bus_datum
*, sizeof(bus_datum
) * mp_nbusses
,
969 for (x
= 0; x
< mp_napics
; x
++)
970 ioapic
[x
] = permanent_io_mapping(io_apic_address
[x
]);
972 /* clear various tables */
973 for (x
= 0; x
< NAPICID
; ++x
) {
974 ID_TO_IO(x
) = -1; /* phy APIC ID to log CPU/IO table */
975 IO_TO_ID(x
) = -1; /* logical IO to APIC ID table */
978 /* clear bus data table */
979 for (x
= 0; x
< mp_nbusses
; ++x
)
980 bus_data
[x
].bus_id
= 0xff;
982 /* clear IO APIC INT table */
983 for (x
= 0; x
< (nintrs
+ 1); ++x
) {
984 io_apic_ints
[x
].int_type
= 0xff;
985 io_apic_ints
[x
].int_vector
= 0xff;
988 /* check for use of 'default' configuration */
989 if (fps
->mpfb1
!= 0) {
990 mptable_default(fps
->mpfb1
);
994 bzero(&arg
, sizeof(arg
));
995 error
= mptable_iterate_entries(mpt
->mp_cth
,
996 mptable_ioapic_pass2_callback
, &arg
);
998 panic("mptable_iterate_entries(ioapic_pass2) failed\n");
1001 #endif /* APIC_IO */
1004 * Check if we should perform a hyperthreading "fix-up" to
1005 * enumerate any logical CPU's that aren't already listed
1008 * XXX: We assume that all of the physical CPUs in the
1009 * system have the same number of logical CPUs.
1011 * XXX: We assume that APIC ID's are allocated such that
1012 * the APIC ID's for a physical processor are aligned
1013 * with the number of logical CPU's in the processor.
1016 mptable_hyperthread_fixup(u_int id_mask
, int cpu_count
)
1018 int i
, id
, lcpus_max
, logical_cpus
;
1020 if ((cpu_feature
& CPUID_HTT
) == 0)
1023 lcpus_max
= (cpu_procinfo
& CPUID_HTT_CORES
) >> 16;
1027 if (cpu_vendor_id
== CPU_VENDOR_INTEL
) {
1029 * INSTRUCTION SET REFERENCE, A-M (#253666)
1030 * Page 3-181, Table 3-20
1031 * "The nearest power-of-2 integer that is not smaller
1032 * than EBX[23:16] is the number of unique initial APIC
1033 * IDs reserved for addressing different logical
1034 * processors in a physical package."
1036 for (i
= 0; ; ++i
) {
1037 if ((1 << i
) >= lcpus_max
) {
1044 KKASSERT(cpu_count
!= 0);
1045 if (cpu_count
== lcpus_max
) {
1046 /* We have nothing to fix */
1048 } else if (cpu_count
== 1) {
1049 /* XXX this may be incorrect */
1050 logical_cpus
= lcpus_max
;
1052 int cur
, prev
, dist
;
1055 * Calculate the distances between two nearest
1056 * APIC IDs. If all such distances are same,
1057 * then it is the number of missing cpus that
1058 * we are going to fill later.
1060 dist
= cur
= prev
= -1;
1061 for (id
= 0; id
< MAXCPU
; ++id
) {
1062 if ((id_mask
& 1 << id
) == 0)
1067 int new_dist
= cur
- prev
;
1073 * Make sure that all distances
1074 * between two nearest APIC IDs
1077 if (dist
!= new_dist
)
1085 /* Must be power of 2 */
1086 if (dist
& (dist
- 1))
1089 /* Can't exceed CPU package capacity */
1090 if (dist
> lcpus_max
)
1091 logical_cpus
= lcpus_max
;
1093 logical_cpus
= dist
;
1097 * For each APIC ID of a CPU that is set in the mask,
1098 * scan the other candidate APIC ID's for this
1099 * physical processor. If any of those ID's are
1100 * already in the table, then kill the fixup.
1102 for (id
= 0; id
< MAXCPU
; id
++) {
1103 if ((id_mask
& 1 << id
) == 0)
1105 /* First, make sure we are on a logical_cpus boundary. */
1106 if (id
% logical_cpus
!= 0)
1108 for (i
= id
+ 1; i
< id
+ logical_cpus
; i
++)
1109 if ((id_mask
& 1 << i
) != 0)
1112 return logical_cpus
;
1116 mptable_map(struct mptable_pos
*mpt
, vm_paddr_t mpfps_paddr
)
1120 vm_size_t cth_mapsz
= 0;
1122 bzero(mpt
, sizeof(*mpt
));
1124 fps
= pmap_mapdev(mpfps_paddr
, sizeof(*fps
));
1125 if (fps
->pap
!= 0) {
1127 * Map configuration table header to get
1128 * the base table size
1130 cth
= pmap_mapdev(fps
->pap
, sizeof(*cth
));
1131 cth_mapsz
= cth
->base_table_length
;
1132 pmap_unmapdev((vm_offset_t
)cth
, sizeof(*cth
));
1134 if (cth_mapsz
< sizeof(*cth
)) {
1135 kprintf("invalid base MP table length %d\n",
1137 pmap_unmapdev((vm_offset_t
)fps
, sizeof(*fps
));
1142 * Map the base table
1144 cth
= pmap_mapdev(fps
->pap
, cth_mapsz
);
1149 mpt
->mp_cth_mapsz
= cth_mapsz
;
1155 mptable_unmap(struct mptable_pos
*mpt
)
1157 if (mpt
->mp_cth
!= NULL
) {
1158 pmap_unmapdev((vm_offset_t
)mpt
->mp_cth
, mpt
->mp_cth_mapsz
);
1160 mpt
->mp_cth_mapsz
= 0;
1162 if (mpt
->mp_fps
!= NULL
) {
1163 pmap_unmapdev((vm_offset_t
)mpt
->mp_fps
, sizeof(*mpt
->mp_fps
));
1171 assign_apic_irq(int apic
, int intpin
, int irq
)
1175 if (int_to_apicintpin
[irq
].ioapic
!= -1)
1176 panic("assign_apic_irq: inconsistent table");
1178 int_to_apicintpin
[irq
].ioapic
= apic
;
1179 int_to_apicintpin
[irq
].int_pin
= intpin
;
1180 int_to_apicintpin
[irq
].apic_address
= ioapic
[apic
];
1181 int_to_apicintpin
[irq
].redirindex
= IOAPIC_REDTBL
+ 2 * intpin
;
1183 for (x
= 0; x
< nintrs
; x
++) {
1184 if ((io_apic_ints
[x
].int_type
== 0 ||
1185 io_apic_ints
[x
].int_type
== 3) &&
1186 io_apic_ints
[x
].int_vector
== 0xff &&
1187 io_apic_ints
[x
].dst_apic_id
== IO_TO_ID(apic
) &&
1188 io_apic_ints
[x
].dst_apic_int
== intpin
)
1189 io_apic_ints
[x
].int_vector
= irq
;
1194 revoke_apic_irq(int irq
)
1200 if (int_to_apicintpin
[irq
].ioapic
== -1)
1201 panic("revoke_apic_irq: inconsistent table");
1203 oldapic
= int_to_apicintpin
[irq
].ioapic
;
1204 oldintpin
= int_to_apicintpin
[irq
].int_pin
;
1206 int_to_apicintpin
[irq
].ioapic
= -1;
1207 int_to_apicintpin
[irq
].int_pin
= 0;
1208 int_to_apicintpin
[irq
].apic_address
= NULL
;
1209 int_to_apicintpin
[irq
].redirindex
= 0;
1211 for (x
= 0; x
< nintrs
; x
++) {
1212 if ((io_apic_ints
[x
].int_type
== 0 ||
1213 io_apic_ints
[x
].int_type
== 3) &&
1214 io_apic_ints
[x
].int_vector
!= 0xff &&
1215 io_apic_ints
[x
].dst_apic_id
== IO_TO_ID(oldapic
) &&
1216 io_apic_ints
[x
].dst_apic_int
== oldintpin
)
1217 io_apic_ints
[x
].int_vector
= 0xff;
1225 allocate_apic_irq(int intr
)
1231 if (io_apic_ints
[intr
].int_vector
!= 0xff)
1232 return; /* Interrupt handler already assigned */
1234 if (io_apic_ints
[intr
].int_type
!= 0 &&
1235 (io_apic_ints
[intr
].int_type
!= 3 ||
1236 (io_apic_ints
[intr
].dst_apic_id
== IO_TO_ID(0) &&
1237 io_apic_ints
[intr
].dst_apic_int
== 0)))
1238 return; /* Not INT or ExtInt on != (0, 0) */
1241 while (irq
< APIC_INTMAPSIZE
&&
1242 int_to_apicintpin
[irq
].ioapic
!= -1)
1245 if (irq
>= APIC_INTMAPSIZE
)
1246 return; /* No free interrupt handlers */
1248 apic
= ID_TO_IO(io_apic_ints
[intr
].dst_apic_id
);
1249 intpin
= io_apic_ints
[intr
].dst_apic_int
;
1251 assign_apic_irq(apic
, intpin
, irq
);
1252 io_apic_setup_intpin(apic
, intpin
);
1257 swap_apic_id(int apic
, int oldid
, int newid
)
1264 return; /* Nothing to do */
1266 kprintf("Changing APIC ID for IO APIC #%d from %d to %d in MP table\n",
1267 apic
, oldid
, newid
);
1269 /* Swap physical APIC IDs in interrupt entries */
1270 for (x
= 0; x
< nintrs
; x
++) {
1271 if (io_apic_ints
[x
].dst_apic_id
== oldid
)
1272 io_apic_ints
[x
].dst_apic_id
= newid
;
1273 else if (io_apic_ints
[x
].dst_apic_id
== newid
)
1274 io_apic_ints
[x
].dst_apic_id
= oldid
;
1277 /* Swap physical APIC IDs in IO_TO_ID mappings */
1278 for (oapic
= 0; oapic
< mp_napics
; oapic
++)
1279 if (IO_TO_ID(oapic
) == newid
)
1282 if (oapic
< mp_napics
) {
1283 kprintf("Changing APIC ID for IO APIC #%d from "
1284 "%d to %d in MP table\n",
1285 oapic
, newid
, oldid
);
1286 IO_TO_ID(oapic
) = oldid
;
1288 IO_TO_ID(apic
) = newid
;
1293 fix_id_to_io_mapping(void)
1297 for (x
= 0; x
< NAPICID
; x
++)
1300 for (x
= 0; x
<= mp_naps
; x
++)
1301 if (CPU_TO_ID(x
) < NAPICID
)
1302 ID_TO_IO(CPU_TO_ID(x
)) = x
;
1304 for (x
= 0; x
< mp_napics
; x
++)
1305 if (IO_TO_ID(x
) < NAPICID
)
1306 ID_TO_IO(IO_TO_ID(x
)) = x
;
1311 first_free_apic_id(void)
1315 for (freeid
= 0; freeid
< NAPICID
; freeid
++) {
1316 for (x
= 0; x
<= mp_naps
; x
++)
1317 if (CPU_TO_ID(x
) == freeid
)
1321 for (x
= 0; x
< mp_napics
; x
++)
1322 if (IO_TO_ID(x
) == freeid
)
1333 io_apic_id_acceptable(int apic
, int id
)
1335 int cpu
; /* Logical CPU number */
1336 int oapic
; /* Logical IO APIC number for other IO APIC */
1339 return 0; /* Out of range */
1341 for (cpu
= 0; cpu
<= mp_naps
; cpu
++)
1342 if (CPU_TO_ID(cpu
) == id
)
1343 return 0; /* Conflict with CPU */
1345 for (oapic
= 0; oapic
< mp_napics
&& oapic
< apic
; oapic
++)
1346 if (IO_TO_ID(oapic
) == id
)
1347 return 0; /* Conflict with other APIC */
1349 return 1; /* ID is acceptable for IO APIC */
1354 io_apic_find_int_entry(int apic
, int pin
)
1358 /* search each of the possible INTerrupt sources */
1359 for (x
= 0; x
< nintrs
; ++x
) {
1360 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1361 (pin
== io_apic_ints
[x
].dst_apic_int
))
1362 return (&io_apic_ints
[x
]);
1368 * parse an Intel MP specification table
1375 int apic
; /* IO APIC unit number */
1376 int freeid
; /* Free physical APIC ID */
1377 int physid
; /* Current physical IO APIC ID */
1379 int bus_0
= 0; /* Stop GCC warning */
1380 int bus_pci
= 0; /* Stop GCC warning */
1384 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
1385 * did it wrong. The MP spec says that when more than 1 PCI bus
1386 * exists the BIOS must begin with bus entries for the PCI bus and use
1387 * actual PCI bus numbering. This implies that when only 1 PCI bus
1388 * exists the BIOS can choose to ignore this ordering, and indeed many
1389 * MP motherboards do ignore it. This causes a problem when the PCI
1390 * sub-system makes requests of the MP sub-system based on PCI bus
1391 * numbers. So here we look for the situation and renumber the
1392 * busses and associated INTs in an effort to "make it right".
1395 /* find bus 0, PCI bus, count the number of PCI busses */
1396 for (num_pci_bus
= 0, x
= 0; x
< mp_nbusses
; ++x
) {
1397 if (bus_data
[x
].bus_id
== 0) {
1400 if (bus_data
[x
].bus_type
== PCI
) {
1406 * bus_0 == slot of bus with ID of 0
1407 * bus_pci == slot of last PCI bus encountered
1410 /* check the 1 PCI bus case for sanity */
1411 /* if it is number 0 all is well */
1412 if (num_pci_bus
== 1 &&
1413 bus_data
[bus_pci
].bus_id
!= 0) {
1415 /* mis-numbered, swap with whichever bus uses slot 0 */
1417 /* swap the bus entry types */
1418 bus_data
[bus_pci
].bus_type
= bus_data
[bus_0
].bus_type
;
1419 bus_data
[bus_0
].bus_type
= PCI
;
1421 /* swap each relavant INTerrupt entry */
1422 id
= bus_data
[bus_pci
].bus_id
;
1423 for (x
= 0; x
< nintrs
; ++x
) {
1424 if (io_apic_ints
[x
].src_bus_id
== id
) {
1425 io_apic_ints
[x
].src_bus_id
= 0;
1427 else if (io_apic_ints
[x
].src_bus_id
== 0) {
1428 io_apic_ints
[x
].src_bus_id
= id
;
1433 /* Assign IO APIC IDs.
1435 * First try the existing ID. If a conflict is detected, try
1436 * the ID in the MP table. If a conflict is still detected, find
1439 * We cannot use the ID_TO_IO table before all conflicts has been
1440 * resolved and the table has been corrected.
1442 for (apic
= 0; apic
< mp_napics
; ++apic
) { /* For all IO APICs */
1444 /* First try to use the value set by the BIOS */
1445 physid
= io_apic_get_id(apic
);
1446 if (io_apic_id_acceptable(apic
, physid
)) {
1447 if (IO_TO_ID(apic
) != physid
)
1448 swap_apic_id(apic
, IO_TO_ID(apic
), physid
);
1452 /* Then check if the value in the MP table is acceptable */
1453 if (io_apic_id_acceptable(apic
, IO_TO_ID(apic
)))
1456 /* Last resort, find a free APIC ID and use it */
1457 freeid
= first_free_apic_id();
1458 if (freeid
>= NAPICID
)
1459 panic("No free physical APIC IDs found");
1461 if (io_apic_id_acceptable(apic
, freeid
)) {
1462 swap_apic_id(apic
, IO_TO_ID(apic
), freeid
);
1465 panic("Free physical APIC ID not usable");
1467 fix_id_to_io_mapping();
1469 /* detect and fix broken Compaq MP table */
1470 if (apic_int_type(0, 0) == -1) {
1471 kprintf("APIC_IO: MP table broken: 8259->APIC entry missing!\n");
1472 io_apic_ints
[nintrs
].int_type
= 3; /* ExtInt */
1473 io_apic_ints
[nintrs
].int_vector
= 0xff; /* Unassigned */
1474 /* XXX fixme, set src bus id etc, but it doesn't seem to hurt */
1475 io_apic_ints
[nintrs
].dst_apic_id
= IO_TO_ID(0);
1476 io_apic_ints
[nintrs
].dst_apic_int
= 0; /* Pin 0 */
1478 } else if (apic_int_type(0, 0) == 0) {
1479 kprintf("APIC_IO: MP table broken: ExtINT entry corrupt!\n");
1480 for (x
= 0; x
< nintrs
; ++x
)
1481 if ((0 == ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1482 (0 == io_apic_ints
[x
].dst_apic_int
)) {
1483 io_apic_ints
[x
].int_type
= 3;
1484 io_apic_ints
[x
].int_vector
= 0xff;
1490 * Fix missing IRQ 15 when IRQ 14 is an ISA interrupt. IDE
1491 * controllers universally come in pairs. If IRQ 14 is specified
1492 * as an ISA interrupt, then IRQ 15 had better be too.
1494 * [ Shuttle XPC / AMD Athlon X2 ]
1495 * The MPTable is missing an entry for IRQ 15. Note that the
1496 * ACPI table has an entry for both 14 and 15.
1498 if (apic_int_type(0, 14) == 0 && apic_int_type(0, 15) == -1) {
1499 kprintf("APIC_IO: MP table broken: IRQ 15 not ISA when IRQ 14 is!\n");
1500 io14
= io_apic_find_int_entry(0, 14);
1501 io_apic_ints
[nintrs
] = *io14
;
1502 io_apic_ints
[nintrs
].src_bus_irq
= 15;
1503 io_apic_ints
[nintrs
].dst_apic_int
= 15;
1508 /* Assign low level interrupt handlers */
1510 setup_apic_irq_mapping(void)
1516 for (x
= 0; x
< APIC_INTMAPSIZE
; x
++) {
1517 int_to_apicintpin
[x
].ioapic
= -1;
1518 int_to_apicintpin
[x
].int_pin
= 0;
1519 int_to_apicintpin
[x
].apic_address
= NULL
;
1520 int_to_apicintpin
[x
].redirindex
= 0;
1522 /* Default to masked */
1523 int_to_apicintpin
[x
].flags
= IOAPIC_IM_FLAG_MASKED
;
1526 /* First assign ISA/EISA interrupts */
1527 for (x
= 0; x
< nintrs
; x
++) {
1528 int_vector
= io_apic_ints
[x
].src_bus_irq
;
1529 if (int_vector
< APIC_INTMAPSIZE
&&
1530 io_apic_ints
[x
].int_vector
== 0xff &&
1531 int_to_apicintpin
[int_vector
].ioapic
== -1 &&
1532 (apic_int_is_bus_type(x
, ISA
) ||
1533 apic_int_is_bus_type(x
, EISA
)) &&
1534 io_apic_ints
[x
].int_type
== 0) {
1535 assign_apic_irq(ID_TO_IO(io_apic_ints
[x
].dst_apic_id
),
1536 io_apic_ints
[x
].dst_apic_int
,
1541 /* Assign ExtInt entry if no ISA/EISA interrupt 0 entry */
1542 for (x
= 0; x
< nintrs
; x
++) {
1543 if (io_apic_ints
[x
].dst_apic_int
== 0 &&
1544 io_apic_ints
[x
].dst_apic_id
== IO_TO_ID(0) &&
1545 io_apic_ints
[x
].int_vector
== 0xff &&
1546 int_to_apicintpin
[0].ioapic
== -1 &&
1547 io_apic_ints
[x
].int_type
== 3) {
1548 assign_apic_irq(0, 0, 0);
1552 /* PCI interrupt assignment is deferred */
1558 mp_set_cpuids(int cpu_id
, int apic_id
)
1560 CPU_TO_ID(cpu_id
) = apic_id
;
1561 ID_TO_CPU(apic_id
) = cpu_id
;
1565 processor_entry(const struct PROCENTRY
*entry
, int cpu
)
1569 /* check for usability */
1570 if (!(entry
->cpu_flags
& PROCENTRY_FLAG_EN
))
1573 /* check for BSP flag */
1574 if (entry
->cpu_flags
& PROCENTRY_FLAG_BP
) {
1575 mp_set_cpuids(0, entry
->apic_id
);
1576 return 0; /* its already been counted */
1579 /* add another AP to list, if less than max number of CPUs */
1580 else if (cpu
< MAXCPU
) {
1581 mp_set_cpuids(cpu
, entry
->apic_id
);
1591 bus_entry(const struct BUSENTRY
*entry
, int bus
)
1596 /* encode the name into an index */
1597 for (x
= 0; x
< 6; ++x
) {
1598 if ((c
= entry
->bus_type
[x
]) == ' ')
1604 if ((x
= lookup_bus_type(name
)) == UNKNOWN_BUSTYPE
)
1605 panic("unknown bus type: '%s'", name
);
1607 bus_data
[bus
].bus_id
= entry
->bus_id
;
1608 bus_data
[bus
].bus_type
= x
;
1614 io_apic_entry(const struct IOAPICENTRY
*entry
, int apic
)
1616 if (!(entry
->apic_flags
& IOAPICENTRY_FLAG_EN
))
1619 IO_TO_ID(apic
) = entry
->apic_id
;
1620 ID_TO_IO(entry
->apic_id
) = apic
;
1626 lookup_bus_type(char *name
)
1630 for (x
= 0; x
< MAX_BUSTYPE
; ++x
)
1631 if (strcmp(bus_type_table
[x
].name
, name
) == 0)
1632 return bus_type_table
[x
].type
;
1634 return UNKNOWN_BUSTYPE
;
1638 int_entry(const struct INTENTRY
*entry
, int intr
)
1642 io_apic_ints
[intr
].int_type
= entry
->int_type
;
1643 io_apic_ints
[intr
].int_flags
= entry
->int_flags
;
1644 io_apic_ints
[intr
].src_bus_id
= entry
->src_bus_id
;
1645 io_apic_ints
[intr
].src_bus_irq
= entry
->src_bus_irq
;
1646 if (entry
->dst_apic_id
== 255) {
1647 /* This signal goes to all IO APICS. Select an IO APIC
1648 with sufficient number of interrupt pins */
1649 for (apic
= 0; apic
< mp_napics
; apic
++)
1650 if (((io_apic_read(apic
, IOAPIC_VER
) &
1651 IOART_VER_MAXREDIR
) >> MAXREDIRSHIFT
) >=
1652 entry
->dst_apic_int
)
1654 if (apic
< mp_napics
)
1655 io_apic_ints
[intr
].dst_apic_id
= IO_TO_ID(apic
);
1657 io_apic_ints
[intr
].dst_apic_id
= entry
->dst_apic_id
;
1659 io_apic_ints
[intr
].dst_apic_id
= entry
->dst_apic_id
;
1660 io_apic_ints
[intr
].dst_apic_int
= entry
->dst_apic_int
;
1666 apic_int_is_bus_type(int intr
, int bus_type
)
1670 for (bus
= 0; bus
< mp_nbusses
; ++bus
)
1671 if ((bus_data
[bus
].bus_id
== io_apic_ints
[intr
].src_bus_id
)
1672 && ((int) bus_data
[bus
].bus_type
== bus_type
))
1679 * Given a traditional ISA INT mask, return an APIC mask.
1682 isa_apic_mask(u_int isa_mask
)
1687 #if defined(SKIP_IRQ15_REDIRECT)
1688 if (isa_mask
== (1 << 15)) {
1689 kprintf("skipping ISA IRQ15 redirect\n");
1692 #endif /* SKIP_IRQ15_REDIRECT */
1694 isa_irq
= ffs(isa_mask
); /* find its bit position */
1695 if (isa_irq
== 0) /* doesn't exist */
1697 --isa_irq
; /* make it zero based */
1699 apic_pin
= isa_apic_irq(isa_irq
); /* look for APIC connection */
1703 return (1 << apic_pin
); /* convert pin# to a mask */
1707 * Determine which APIC pin an ISA/EISA INT is attached to.
1709 #define INTTYPE(I) (io_apic_ints[(I)].int_type)
1710 #define INTPIN(I) (io_apic_ints[(I)].dst_apic_int)
1711 #define INTIRQ(I) (io_apic_ints[(I)].int_vector)
1712 #define INTAPIC(I) (ID_TO_IO(io_apic_ints[(I)].dst_apic_id))
1714 #define SRCBUSIRQ(I) (io_apic_ints[(I)].src_bus_irq)
1716 isa_apic_irq(int isa_irq
)
1720 for (intr
= 0; intr
< nintrs
; ++intr
) { /* check each record */
1721 if (INTTYPE(intr
) == 0) { /* standard INT */
1722 if (SRCBUSIRQ(intr
) == isa_irq
) {
1723 if (apic_int_is_bus_type(intr
, ISA
) ||
1724 apic_int_is_bus_type(intr
, EISA
)) {
1725 if (INTIRQ(intr
) == 0xff)
1726 return -1; /* unassigned */
1727 return INTIRQ(intr
); /* found */
1732 return -1; /* NOT found */
1737 * Determine which APIC pin a PCI INT is attached to.
1739 #define SRCBUSID(I) (io_apic_ints[(I)].src_bus_id)
1740 #define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
1741 #define SRCBUSLINE(I) (io_apic_ints[(I)].src_bus_irq & 0x03)
1743 pci_apic_irq(int pciBus
, int pciDevice
, int pciInt
)
1747 --pciInt
; /* zero based */
1749 for (intr
= 0; intr
< nintrs
; ++intr
) { /* check each record */
1750 if ((INTTYPE(intr
) == 0) /* standard INT */
1751 && (SRCBUSID(intr
) == pciBus
)
1752 && (SRCBUSDEVICE(intr
) == pciDevice
)
1753 && (SRCBUSLINE(intr
) == pciInt
)) { /* a candidate IRQ */
1754 if (apic_int_is_bus_type(intr
, PCI
)) {
1755 if (INTIRQ(intr
) == 0xff)
1756 allocate_apic_irq(intr
);
1757 if (INTIRQ(intr
) == 0xff)
1758 return -1; /* unassigned */
1759 return INTIRQ(intr
); /* exact match */
1764 return -1; /* NOT found */
1768 next_apic_irq(int irq
)
1775 for (intr
= 0; intr
< nintrs
; intr
++) {
1776 if (INTIRQ(intr
) != irq
|| INTTYPE(intr
) != 0)
1778 bus
= SRCBUSID(intr
);
1779 bustype
= apic_bus_type(bus
);
1780 if (bustype
!= ISA
&&
1786 if (intr
>= nintrs
) {
1789 for (ointr
= intr
+ 1; ointr
< nintrs
; ointr
++) {
1790 if (INTTYPE(ointr
) != 0)
1792 if (bus
!= SRCBUSID(ointr
))
1794 if (bustype
== PCI
) {
1795 if (SRCBUSDEVICE(intr
) != SRCBUSDEVICE(ointr
))
1797 if (SRCBUSLINE(intr
) != SRCBUSLINE(ointr
))
1800 if (bustype
== ISA
|| bustype
== EISA
) {
1801 if (SRCBUSIRQ(intr
) != SRCBUSIRQ(ointr
))
1804 if (INTPIN(intr
) == INTPIN(ointr
))
1808 if (ointr
>= nintrs
) {
1811 return INTIRQ(ointr
);
1826 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt.
1829 * Exactly what this means is unclear at this point. It is a solution
1830 * for motherboards that redirect the MBIRQ0 pin. Generically a motherboard
1831 * could route any of the ISA INTs to upper (>15) IRQ values. But most would
1832 * NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an
1836 undirect_isa_irq(int rirq
)
1840 kprintf("Freeing redirected ISA irq %d.\n", rirq
);
1841 /** FIXME: tickle the MB redirector chip */
1845 kprintf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq
);
1852 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt
1855 undirect_pci_irq(int rirq
)
1859 kprintf("Freeing redirected PCI irq %d.\n", rirq
);
1861 /** FIXME: tickle the MB redirector chip */
1865 kprintf("Freeing (NOT implemented) redirected PCI irq %d.\n",
1875 * given a bus ID, return:
1876 * the bus type if found
1880 apic_bus_type(int id
)
1884 for (x
= 0; x
< mp_nbusses
; ++x
)
1885 if (bus_data
[x
].bus_id
== id
)
1886 return bus_data
[x
].bus_type
;
1892 * given a LOGICAL APIC# and pin#, return:
1893 * the associated src bus ID if found
1897 apic_src_bus_id(int apic
, int pin
)
1901 /* search each of the possible INTerrupt sources */
1902 for (x
= 0; x
< nintrs
; ++x
)
1903 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1904 (pin
== io_apic_ints
[x
].dst_apic_int
))
1905 return (io_apic_ints
[x
].src_bus_id
);
1907 return -1; /* NOT found */
1911 * given a LOGICAL APIC# and pin#, return:
1912 * the associated src bus IRQ if found
1916 apic_src_bus_irq(int apic
, int pin
)
1920 for (x
= 0; x
< nintrs
; x
++)
1921 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1922 (pin
== io_apic_ints
[x
].dst_apic_int
))
1923 return (io_apic_ints
[x
].src_bus_irq
);
1925 return -1; /* NOT found */
1930 * given a LOGICAL APIC# and pin#, return:
1931 * the associated INTerrupt type if found
1935 apic_int_type(int apic
, int pin
)
1939 /* search each of the possible INTerrupt sources */
1940 for (x
= 0; x
< nintrs
; ++x
) {
1941 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1942 (pin
== io_apic_ints
[x
].dst_apic_int
))
1943 return (io_apic_ints
[x
].int_type
);
1945 return -1; /* NOT found */
1949 * Return the IRQ associated with an APIC pin
1952 apic_irq(int apic
, int pin
)
1957 for (x
= 0; x
< nintrs
; ++x
) {
1958 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1959 (pin
== io_apic_ints
[x
].dst_apic_int
)) {
1960 res
= io_apic_ints
[x
].int_vector
;
1963 if (apic
!= int_to_apicintpin
[res
].ioapic
)
1964 panic("apic_irq: inconsistent table %d/%d", apic
, int_to_apicintpin
[res
].ioapic
);
1965 if (pin
!= int_to_apicintpin
[res
].int_pin
)
1966 panic("apic_irq inconsistent table (2)");
1975 * given a LOGICAL APIC# and pin#, return:
1976 * the associated trigger mode if found
1980 apic_trigger(int apic
, int pin
)
1984 /* search each of the possible INTerrupt sources */
1985 for (x
= 0; x
< nintrs
; ++x
)
1986 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
1987 (pin
== io_apic_ints
[x
].dst_apic_int
))
1988 return ((io_apic_ints
[x
].int_flags
>> 2) & 0x03);
1990 return -1; /* NOT found */
1995 * given a LOGICAL APIC# and pin#, return:
1996 * the associated 'active' level if found
2000 apic_polarity(int apic
, int pin
)
2004 /* search each of the possible INTerrupt sources */
2005 for (x
= 0; x
< nintrs
; ++x
)
2006 if ((apic
== ID_TO_IO(io_apic_ints
[x
].dst_apic_id
)) &&
2007 (pin
== io_apic_ints
[x
].dst_apic_int
))
2008 return (io_apic_ints
[x
].int_flags
& 0x03);
2010 return -1; /* NOT found */
2014 * set data according to MP defaults
2015 * FIXME: probably not complete yet...
2018 mptable_default(int type
)
2024 kprintf(" MP default config type: %d\n", type
);
2027 kprintf(" bus: ISA, APIC: 82489DX\n");
2030 kprintf(" bus: EISA, APIC: 82489DX\n");
2033 kprintf(" bus: EISA, APIC: 82489DX\n");
2036 kprintf(" bus: MCA, APIC: 82489DX\n");
2039 kprintf(" bus: ISA+PCI, APIC: Integrated\n");
2042 kprintf(" bus: EISA+PCI, APIC: Integrated\n");
2045 kprintf(" bus: MCA+PCI, APIC: Integrated\n");
2048 kprintf(" future type\n");
2054 /* one and only IO APIC */
2055 io_apic_id
= (io_apic_read(0, IOAPIC_ID
) & APIC_ID_MASK
) >> 24;
2058 * sanity check, refer to MP spec section 3.6.6, last paragraph
2059 * necessary as some hardware isn't properly setting up the IO APIC
2061 #if defined(REALLY_ANAL_IOAPICID_VALUE)
2062 if (io_apic_id
!= 2) {
2064 if ((io_apic_id
== 0) || (io_apic_id
== 1) || (io_apic_id
== 15)) {
2065 #endif /* REALLY_ANAL_IOAPICID_VALUE */
2066 io_apic_set_id(0, 2);
2069 IO_TO_ID(0) = io_apic_id
;
2070 ID_TO_IO(io_apic_id
) = 0;
2072 /* fill out bus entries */
2081 bus_data
[0].bus_id
= default_data
[type
- 1][1];
2082 bus_data
[0].bus_type
= default_data
[type
- 1][2];
2083 bus_data
[1].bus_id
= default_data
[type
- 1][3];
2084 bus_data
[1].bus_type
= default_data
[type
- 1][4];
2087 /* case 4: case 7: MCA NOT supported */
2088 default: /* illegal/reserved */
2089 panic("BAD default MP config: %d", type
);
2093 /* general cases from MP v1.4, table 5-2 */
2094 for (pin
= 0; pin
< 16; ++pin
) {
2095 io_apic_ints
[pin
].int_type
= 0;
2096 io_apic_ints
[pin
].int_flags
= 0x05; /* edge/active-hi */
2097 io_apic_ints
[pin
].src_bus_id
= 0;
2098 io_apic_ints
[pin
].src_bus_irq
= pin
; /* IRQ2 caught below */
2099 io_apic_ints
[pin
].dst_apic_id
= io_apic_id
;
2100 io_apic_ints
[pin
].dst_apic_int
= pin
; /* 1-to-1 */
2103 /* special cases from MP v1.4, table 5-2 */
2105 io_apic_ints
[2].int_type
= 0xff; /* N/C */
2106 io_apic_ints
[13].int_type
= 0xff; /* N/C */
2107 #if !defined(APIC_MIXED_MODE)
2109 panic("sorry, can't support type 2 default yet");
2110 #endif /* APIC_MIXED_MODE */
2113 io_apic_ints
[2].src_bus_irq
= 0; /* ISA IRQ0 is on APIC INT 2 */
2116 io_apic_ints
[0].int_type
= 0xff; /* N/C */
2118 io_apic_ints
[0].int_type
= 3; /* vectored 8259 */
2121 #endif /* APIC_IO */
2124 * Map a physical memory address representing I/O into KVA. The I/O
2125 * block is assumed not to cross a page boundary.
2128 permanent_io_mapping(vm_paddr_t pa
)
2134 KKASSERT(pa
< 0x100000000LL
);
2136 pgeflag
= 0; /* not used for SMP yet */
2139 * If the requested physical address has already been incidently
2140 * mapped, just use the existing mapping. Otherwise create a new
2143 for (i
= IO_MAPPING_START_INDEX
; i
< SMPpt_alloc_index
; ++i
) {
2144 if (((vm_offset_t
)SMPpt
[i
] & PG_FRAME
) ==
2145 ((vm_offset_t
)pa
& PG_FRAME
)) {
2149 if (i
== SMPpt_alloc_index
) {
2150 if (i
== NPTEPG
- 2) {
2151 panic("permanent_io_mapping: We ran out of space"
2154 SMPpt
[i
] = (pt_entry_t
)(PG_V
| PG_RW
| PG_N
| pgeflag
|
2155 ((vm_offset_t
)pa
& PG_FRAME
));
2156 ++SMPpt_alloc_index
;
2158 vaddr
= (vm_offset_t
)CPU_prvspace
+ (i
* PAGE_SIZE
) +
2159 ((vm_offset_t
)pa
& PAGE_MASK
);
2160 return ((void *)vaddr
);
2164 * start each AP in our list
2167 start_all_aps(u_int boot_addr
)
2171 u_char mpbiosreason
;
2172 u_long mpbioswarmvec
;
2173 struct mdglobaldata
*gd
;
2174 struct privatespace
*ps
;
2178 POSTCODE(START_ALL_APS_POST
);
2180 /* Initialize BSP's local APIC */
2181 apic_initialize(TRUE
);
2183 /* install the AP 1st level boot code */
2184 install_ap_tramp(boot_addr
);
2187 /* save the current value of the warm-start vector */
2188 mpbioswarmvec
= *((u_long
*) WARMBOOT_OFF
);
2189 outb(CMOS_REG
, BIOS_RESET
);
2190 mpbiosreason
= inb(CMOS_DATA
);
2192 /* set up temporary P==V mapping for AP boot */
2193 /* XXX this is a hack, we should boot the AP on its own stack/PTD */
2194 kptbase
= (uintptr_t)(void *)KPTphys
;
2195 for (x
= 0; x
< NKPT
; x
++) {
2196 PTD
[x
] = (pd_entry_t
)(PG_V
| PG_RW
|
2197 ((kptbase
+ x
* PAGE_SIZE
) & PG_FRAME
));
2202 for (x
= 1; x
<= mp_naps
; ++x
) {
2204 /* This is a bit verbose, it will go away soon. */
2206 /* first page of AP's private space */
2207 pg
= x
* i386_btop(sizeof(struct privatespace
));
2209 /* allocate new private data page(s) */
2210 gd
= (struct mdglobaldata
*)kmem_alloc(&kernel_map
,
2211 MDGLOBALDATA_BASEALLOC_SIZE
);
2212 /* wire it into the private page table page */
2213 for (i
= 0; i
< MDGLOBALDATA_BASEALLOC_SIZE
; i
+= PAGE_SIZE
) {
2214 SMPpt
[pg
+ i
/ PAGE_SIZE
] = (pt_entry_t
)
2215 (PG_V
| PG_RW
| vtophys_pte((char *)gd
+ i
));
2217 pg
+= MDGLOBALDATA_BASEALLOC_PAGES
;
2219 SMPpt
[pg
+ 0] = 0; /* *gd_CMAP1 */
2220 SMPpt
[pg
+ 1] = 0; /* *gd_CMAP2 */
2221 SMPpt
[pg
+ 2] = 0; /* *gd_CMAP3 */
2222 SMPpt
[pg
+ 3] = 0; /* *gd_PMAP1 */
2224 /* allocate and set up an idle stack data page */
2225 stack
= (char *)kmem_alloc(&kernel_map
, UPAGES
*PAGE_SIZE
);
2226 for (i
= 0; i
< UPAGES
; i
++) {
2227 SMPpt
[pg
+ 4 + i
] = (pt_entry_t
)
2228 (PG_V
| PG_RW
| vtophys_pte(PAGE_SIZE
* i
+ stack
));
2231 gd
= &CPU_prvspace
[x
].mdglobaldata
; /* official location */
2232 bzero(gd
, sizeof(*gd
));
2233 gd
->mi
.gd_prvspace
= ps
= &CPU_prvspace
[x
];
2235 /* prime data page for it to use */
2236 mi_gdinit(&gd
->mi
, x
);
2238 gd
->gd_CMAP1
= &SMPpt
[pg
+ 0];
2239 gd
->gd_CMAP2
= &SMPpt
[pg
+ 1];
2240 gd
->gd_CMAP3
= &SMPpt
[pg
+ 2];
2241 gd
->gd_PMAP1
= &SMPpt
[pg
+ 3];
2242 gd
->gd_CADDR1
= ps
->CPAGE1
;
2243 gd
->gd_CADDR2
= ps
->CPAGE2
;
2244 gd
->gd_CADDR3
= ps
->CPAGE3
;
2245 gd
->gd_PADDR1
= (unsigned *)ps
->PPAGE1
;
2246 gd
->mi
.gd_ipiq
= (void *)kmem_alloc(&kernel_map
, sizeof(lwkt_ipiq
) * (mp_naps
+ 1));
2247 bzero(gd
->mi
.gd_ipiq
, sizeof(lwkt_ipiq
) * (mp_naps
+ 1));
2249 /* setup a vector to our boot code */
2250 *((volatile u_short
*) WARMBOOT_OFF
) = WARMBOOT_TARGET
;
2251 *((volatile u_short
*) WARMBOOT_SEG
) = (boot_addr
>> 4);
2252 outb(CMOS_REG
, BIOS_RESET
);
2253 outb(CMOS_DATA
, BIOS_WARM
); /* 'warm-start' */
2256 * Setup the AP boot stack
2258 bootSTK
= &ps
->idlestack
[UPAGES
*PAGE_SIZE
/2];
2261 /* attempt to start the Application Processor */
2262 CHECK_INIT(99); /* setup checkpoints */
2263 if (!start_ap(gd
, boot_addr
)) {
2264 kprintf("AP #%d (PHY# %d) failed!\n", x
, CPU_TO_ID(x
));
2265 CHECK_PRINT("trace"); /* show checkpoints */
2266 /* better panic as the AP may be running loose */
2267 kprintf("panic y/n? [y] ");
2268 if (cngetc() != 'n')
2271 CHECK_PRINT("trace"); /* show checkpoints */
2273 /* record its version info */
2274 cpu_apic_versions
[x
] = cpu_apic_versions
[0];
2277 /* set ncpus to 1 + highest logical cpu. Not all may have come up */
2280 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
2281 for (shift
= 0; (1 << shift
) <= ncpus
; ++shift
)
2284 ncpus2_shift
= shift
;
2285 ncpus2
= 1 << shift
;
2286 ncpus2_mask
= ncpus2
- 1;
2288 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
2289 if ((1 << shift
) < ncpus
)
2291 ncpus_fit
= 1 << shift
;
2292 ncpus_fit_mask
= ncpus_fit
- 1;
2294 /* build our map of 'other' CPUs */
2295 mycpu
->gd_other_cpus
= smp_startup_mask
& ~(1 << mycpu
->gd_cpuid
);
2296 mycpu
->gd_ipiq
= (void *)kmem_alloc(&kernel_map
, sizeof(lwkt_ipiq
) * ncpus
);
2297 bzero(mycpu
->gd_ipiq
, sizeof(lwkt_ipiq
) * ncpus
);
2299 /* fill in our (BSP) APIC version */
2300 cpu_apic_versions
[0] = lapic
.version
;
2302 /* restore the warmstart vector */
2303 *(u_long
*) WARMBOOT_OFF
= mpbioswarmvec
;
2304 outb(CMOS_REG
, BIOS_RESET
);
2305 outb(CMOS_DATA
, mpbiosreason
);
2308 * NOTE! The idlestack for the BSP was setup by locore. Finish
2309 * up, clean out the P==V mapping we did earlier.
2311 for (x
= 0; x
< NKPT
; x
++)
2315 /* number of APs actually started */
2321 * load the 1st level AP boot code into base memory.
2324 /* targets for relocation */
2325 extern void bigJump(void);
2326 extern void bootCodeSeg(void);
2327 extern void bootDataSeg(void);
2328 extern void MPentry(void);
2329 extern u_int MP_GDT
;
2330 extern u_int mp_gdtbase
;
2333 install_ap_tramp(u_int boot_addr
)
2336 int size
= *(int *) ((u_long
) & bootMP_size
);
2337 u_char
*src
= (u_char
*) ((u_long
) bootMP
);
2338 u_char
*dst
= (u_char
*) boot_addr
+ KERNBASE
;
2339 u_int boot_base
= (u_int
) bootMP
;
2344 POSTCODE(INSTALL_AP_TRAMP_POST
);
2346 for (x
= 0; x
< size
; ++x
)
2350 * modify addresses in code we just moved to basemem. unfortunately we
2351 * need fairly detailed info about mpboot.s for this to work. changes
2352 * to mpboot.s might require changes here.
2355 /* boot code is located in KERNEL space */
2356 dst
= (u_char
*) boot_addr
+ KERNBASE
;
2358 /* modify the lgdt arg */
2359 dst32
= (u_int32_t
*) (dst
+ ((u_int
) & mp_gdtbase
- boot_base
));
2360 *dst32
= boot_addr
+ ((u_int
) & MP_GDT
- boot_base
);
2362 /* modify the ljmp target for MPentry() */
2363 dst32
= (u_int32_t
*) (dst
+ ((u_int
) bigJump
- boot_base
) + 1);
2364 *dst32
= ((u_int
) MPentry
- KERNBASE
);
2366 /* modify the target for boot code segment */
2367 dst16
= (u_int16_t
*) (dst
+ ((u_int
) bootCodeSeg
- boot_base
));
2368 dst8
= (u_int8_t
*) (dst16
+ 1);
2369 *dst16
= (u_int
) boot_addr
& 0xffff;
2370 *dst8
= ((u_int
) boot_addr
>> 16) & 0xff;
2372 /* modify the target for boot data segment */
2373 dst16
= (u_int16_t
*) (dst
+ ((u_int
) bootDataSeg
- boot_base
));
2374 dst8
= (u_int8_t
*) (dst16
+ 1);
2375 *dst16
= (u_int
) boot_addr
& 0xffff;
2376 *dst8
= ((u_int
) boot_addr
>> 16) & 0xff;
2381 * this function starts the AP (application processor) identified
2382 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
2383 * to accomplish this. This is necessary because of the nuances
2384 * of the different hardware we might encounter. It ain't pretty,
2385 * but it seems to work.
2387 * NOTE: eventually an AP gets to ap_init(), which is called just
2388 * before the AP goes into the LWKT scheduler's idle loop.
2391 start_ap(struct mdglobaldata
*gd
, u_int boot_addr
)
2395 u_long icr_lo
, icr_hi
;
2397 POSTCODE(START_AP_POST
);
2399 /* get the PHYSICAL APIC ID# */
2400 physical_cpu
= CPU_TO_ID(gd
->mi
.gd_cpuid
);
2402 /* calculate the vector */
2403 vector
= (boot_addr
>> 12) & 0xff;
2405 /* Make sure the target cpu sees everything */
2409 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
2410 * and running the target CPU. OR this INIT IPI might be latched (P5
2411 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
2415 /* setup the address for the target AP */
2416 icr_hi
= lapic
.icr_hi
& ~APIC_ID_MASK
;
2417 icr_hi
|= (physical_cpu
<< 24);
2418 lapic
.icr_hi
= icr_hi
;
2420 /* do an INIT IPI: assert RESET */
2421 icr_lo
= lapic
.icr_lo
& 0xfff00000;
2422 lapic
.icr_lo
= icr_lo
| 0x0000c500;
2424 /* wait for pending status end */
2425 while (lapic
.icr_lo
& APIC_DELSTAT_MASK
)
2428 /* do an INIT IPI: deassert RESET */
2429 lapic
.icr_lo
= icr_lo
| 0x00008500;
2431 /* wait for pending status end */
2432 u_sleep(10000); /* wait ~10mS */
2433 while (lapic
.icr_lo
& APIC_DELSTAT_MASK
)
2437 * next we do a STARTUP IPI: the previous INIT IPI might still be
2438 * latched, (P5 bug) this 1st STARTUP would then terminate
2439 * immediately, and the previously started INIT IPI would continue. OR
2440 * the previous INIT IPI has already run. and this STARTUP IPI will
2441 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
2445 /* do a STARTUP IPI */
2446 lapic
.icr_lo
= icr_lo
| 0x00000600 | vector
;
2447 while (lapic
.icr_lo
& APIC_DELSTAT_MASK
)
2449 u_sleep(200); /* wait ~200uS */
2452 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
2453 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
2454 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
2455 * recognized after hardware RESET or INIT IPI.
2458 lapic
.icr_lo
= icr_lo
| 0x00000600 | vector
;
2459 while (lapic
.icr_lo
& APIC_DELSTAT_MASK
)
2461 u_sleep(200); /* wait ~200uS */
2463 /* wait for it to start, see ap_init() */
2464 set_apic_timer(5000000);/* == 5 seconds */
2465 while (read_apic_timer()) {
2466 if (smp_startup_mask
& (1 << gd
->mi
.gd_cpuid
))
2467 return 1; /* return SUCCESS */
2469 return 0; /* return FAILURE */
2474 * Lazy flush the TLB on all other CPU's. DEPRECATED.
2476 * If for some reason we were unable to start all cpus we cannot safely
2477 * use broadcast IPIs.
2483 if (smp_startup_mask
== smp_active_mask
) {
2484 all_but_self_ipi(XINVLTLB_OFFSET
);
2486 selected_apic_ipi(smp_active_mask
, XINVLTLB_OFFSET
,
2487 APIC_DELMODE_FIXED
);
2493 * When called the executing CPU will send an IPI to all other CPUs
2494 * requesting that they halt execution.
2496 * Usually (but not necessarily) called with 'other_cpus' as its arg.
2498 * - Signals all CPUs in map to stop.
2499 * - Waits for each to stop.
2506 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
2507 * from executing at same time.
2510 stop_cpus(u_int map
)
2512 map
&= smp_active_mask
;
2514 /* send the Xcpustop IPI to all CPUs in map */
2515 selected_apic_ipi(map
, XCPUSTOP_OFFSET
, APIC_DELMODE_FIXED
);
2517 while ((stopped_cpus
& map
) != map
)
2525 * Called by a CPU to restart stopped CPUs.
2527 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
2529 * - Signals all CPUs in map to restart.
2530 * - Waits for each to restart.
2538 restart_cpus(u_int map
)
2540 /* signal other cpus to restart */
2541 started_cpus
= map
& smp_active_mask
;
2543 while ((stopped_cpus
& map
) != 0) /* wait for each to clear its bit */
2550 * This is called once the mpboot code has gotten us properly relocated
2551 * and the MMU turned on, etc. ap_init() is actually the idle thread,
2552 * and when it returns the scheduler will call the real cpu_idle() main
2553 * loop for the idlethread. Interrupts are disabled on entry and should
2554 * remain disabled at return.
2562 * Adjust smp_startup_mask to signal the BSP that we have started
2563 * up successfully. Note that we do not yet hold the BGL. The BSP
2564 * is waiting for our signal.
2566 * We can't set our bit in smp_active_mask yet because we are holding
2567 * interrupts physically disabled and remote cpus could deadlock
2568 * trying to send us an IPI.
2570 smp_startup_mask
|= 1 << mycpu
->gd_cpuid
;
2574 * Interlock for finalization. Wait until mp_finish is non-zero,
2575 * then get the MP lock.
2577 * Note: We are in a critical section.
2579 * Note: We have to synchronize td_mpcount to our desired MP state
2580 * before calling cpu_try_mplock().
2582 * Note: we are the idle thread, we can only spin.
2584 * Note: The load fence is memory volatile and prevents the compiler
2585 * from improperly caching mp_finish, and the cpu from improperly
2588 while (mp_finish
== 0)
2590 ++curthread
->td_mpcount
;
2591 while (cpu_try_mplock() == 0)
2594 if (cpu_feature
& CPUID_TSC
) {
2596 * The BSP is constantly updating tsc0_offset, figure out the
2597 * relative difference to synchronize ktrdump.
2599 tsc_offsets
[mycpu
->gd_cpuid
] = rdtsc() - tsc0_offset
;
2602 /* BSP may have changed PTD while we're waiting for the lock */
2605 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2609 /* Build our map of 'other' CPUs. */
2610 mycpu
->gd_other_cpus
= smp_startup_mask
& ~(1 << mycpu
->gd_cpuid
);
2612 kprintf("SMP: AP CPU #%d Launched!\n", mycpu
->gd_cpuid
);
2614 /* A quick check from sanity claus */
2615 apic_id
= (apic_id_to_logical
[(lapic
.id
& 0x0f000000) >> 24]);
2616 if (mycpu
->gd_cpuid
!= apic_id
) {
2617 kprintf("SMP: cpuid = %d\n", mycpu
->gd_cpuid
);
2618 kprintf("SMP: apic_id = %d\n", apic_id
);
2619 kprintf("PTD[MPPTDI] = %p\n", (void *)PTD
[MPPTDI
]);
2620 panic("cpuid mismatch! boom!!");
2623 /* Initialize AP's local APIC for irq's */
2624 apic_initialize(FALSE
);
2626 /* Set memory range attributes for this CPU to match the BSP */
2627 mem_range_AP_init();
2630 * Once we go active we must process any IPIQ messages that may
2631 * have been queued, because no actual IPI will occur until we
2632 * set our bit in the smp_active_mask. If we don't the IPI
2633 * message interlock could be left set which would also prevent
2636 * The idle loop doesn't expect the BGL to be held and while
2637 * lwkt_switch() normally cleans things up this is a special case
2638 * because we returning almost directly into the idle loop.
2640 * The idle thread is never placed on the runq, make sure
2641 * nothing we've done put it there.
2643 KKASSERT(curthread
->td_mpcount
== 1);
2644 smp_active_mask
|= 1 << mycpu
->gd_cpuid
;
2647 * Enable interrupts here. idle_restore will also do it, but
2648 * doing it here lets us clean up any strays that got posted to
2649 * the CPU during the AP boot while we are still in a critical
2652 __asm
__volatile("sti; pause; pause"::);
2653 mdcpu
->gd_fpending
= 0;
2655 initclocks_pcpu(); /* clock interrupts (via IPIs) */
2656 lwkt_process_ipiq();
2659 * Releasing the mp lock lets the BSP finish up the SMP init
2662 KKASSERT((curthread
->td_flags
& TDF_RUNQ
) == 0);
2666 * Get SMP fully working before we start initializing devices.
2674 kprintf("Finish MP startup\n");
2675 if (cpu_feature
& CPUID_TSC
)
2676 tsc0_offset
= rdtsc();
2679 while (smp_active_mask
!= smp_startup_mask
) {
2681 if (cpu_feature
& CPUID_TSC
)
2682 tsc0_offset
= rdtsc();
2684 while (try_mplock() == 0)
2687 kprintf("Active CPU Mask: %08x\n", smp_active_mask
);
2690 SYSINIT(finishsmp
, SI_BOOT2_FINISH_SMP
, SI_ORDER_FIRST
, ap_finish
, NULL
)
2693 cpu_send_ipiq(int dcpu
)
2695 if ((1 << dcpu
) & smp_active_mask
)
2696 single_apic_ipi(dcpu
, XIPIQ_OFFSET
, APIC_DELMODE_FIXED
);
2699 #if 0 /* single_apic_ipi_passive() not working yet */
2701 * Returns 0 on failure, 1 on success
2704 cpu_send_ipiq_passive(int dcpu
)
2707 if ((1 << dcpu
) & smp_active_mask
) {
2708 r
= single_apic_ipi_passive(dcpu
, XIPIQ_OFFSET
,
2709 APIC_DELMODE_FIXED
);
2715 struct mptable_lapic_cbarg1
{
2718 u_int ht_apicid_mask
;
2722 mptable_lapic_pass1_callback(void *xarg
, const void *pos
, int type
)
2724 const struct PROCENTRY
*ent
;
2725 struct mptable_lapic_cbarg1
*arg
= xarg
;
2731 if ((ent
->cpu_flags
& PROCENTRY_FLAG_EN
) == 0)
2735 if (ent
->apic_id
< 32) {
2736 arg
->ht_apicid_mask
|= 1 << ent
->apic_id
;
2737 } else if (arg
->ht_fixup
) {
2738 kprintf("MPTABLE: lapic id > 32, disable HTT fixup\n");
2744 struct mptable_lapic_cbarg2
{
2751 mptable_lapic_pass2_callback(void *xarg
, const void *pos
, int type
)
2753 const struct PROCENTRY
*ent
;
2754 struct mptable_lapic_cbarg2
*arg
= xarg
;
2760 if (ent
->cpu_flags
& PROCENTRY_FLAG_BP
) {
2761 KKASSERT(!arg
->found_bsp
);
2765 if (processor_entry(ent
, arg
->cpu
))
2768 if (arg
->logical_cpus
) {
2769 struct PROCENTRY proc
;
2773 * Create fake mptable processor entries
2774 * and feed them to processor_entry() to
2775 * enumerate the logical CPUs.
2777 bzero(&proc
, sizeof(proc
));
2779 proc
.cpu_flags
= PROCENTRY_FLAG_EN
;
2780 proc
.apic_id
= ent
->apic_id
;
2782 for (i
= 1; i
< arg
->logical_cpus
; i
++) {
2784 processor_entry(&proc
, arg
->cpu
);
2792 mptable_imcr(struct mptable_pos
*mpt
)
2794 /* record whether PIC or virtual-wire mode */
2795 machintr_setvar_simple(MACHINTR_VAR_IMCR_PRESENT
,
2796 mpt
->mp_fps
->mpfb2
& 0x80);
2799 struct mptable_lapic_enumerator
{
2800 struct lapic_enumerator enumerator
;
2801 vm_paddr_t mpfps_paddr
;
2805 mptable_lapic_default(void)
2807 int ap_apicid
, bsp_apicid
;
2809 mp_naps
= 1; /* exclude BSP */
2811 /* Map local apic before the id field is accessed */
2812 lapic_map(DEFAULT_APIC_BASE
);
2814 bsp_apicid
= APIC_ID(lapic
.id
);
2815 ap_apicid
= (bsp_apicid
== 0) ? 1 : 0;
2818 mp_set_cpuids(0, bsp_apicid
);
2819 /* one and only AP */
2820 mp_set_cpuids(1, ap_apicid
);
2826 * ID_TO_CPU(N), APIC ID to logical CPU table
2827 * CPU_TO_ID(N), logical CPU to APIC ID table
2830 mptable_lapic_enumerate(struct lapic_enumerator
*e
)
2832 struct mptable_pos mpt
;
2833 struct mptable_lapic_cbarg1 arg1
;
2834 struct mptable_lapic_cbarg2 arg2
;
2836 int error
, logical_cpus
= 0;
2837 vm_offset_t lapic_addr
;
2838 vm_paddr_t mpfps_paddr
;
2840 mpfps_paddr
= ((struct mptable_lapic_enumerator
*)e
)->mpfps_paddr
;
2841 KKASSERT(mpfps_paddr
!= 0);
2843 error
= mptable_map(&mpt
, mpfps_paddr
);
2845 panic("mptable_lapic_enumerate mptable_map failed\n");
2847 KKASSERT(mpt
.mp_fps
!= NULL
);
2850 * Check for use of 'default' configuration
2852 if (mpt
.mp_fps
->mpfb1
!= 0) {
2853 mptable_lapic_default();
2854 mptable_unmap(&mpt
);
2859 KKASSERT(cth
!= NULL
);
2861 /* Save local apic address */
2862 lapic_addr
= (vm_offset_t
)cth
->apic_address
;
2863 KKASSERT(lapic_addr
!= 0);
2866 * Find out how many CPUs do we have
2868 bzero(&arg1
, sizeof(arg1
));
2869 arg1
.ht_fixup
= 1; /* Apply ht fixup by default */
2871 error
= mptable_iterate_entries(cth
,
2872 mptable_lapic_pass1_callback
, &arg1
);
2874 panic("mptable_iterate_entries(lapic_pass1) failed\n");
2875 KKASSERT(arg1
.cpu_count
!= 0);
2877 /* See if we need to fixup HT logical CPUs. */
2878 if (arg1
.ht_fixup
) {
2879 logical_cpus
= mptable_hyperthread_fixup(arg1
.ht_apicid_mask
,
2881 if (logical_cpus
!= 0)
2882 arg1
.cpu_count
*= logical_cpus
;
2884 mp_naps
= arg1
.cpu_count
;
2886 /* Qualify the numbers again, after possible HT fixup */
2887 if (mp_naps
> MAXCPU
) {
2888 kprintf("Warning: only using %d of %d available CPUs!\n",
2893 --mp_naps
; /* subtract the BSP */
2896 * Link logical CPU id to local apic id
2898 bzero(&arg2
, sizeof(arg2
));
2900 arg2
.logical_cpus
= logical_cpus
;
2902 error
= mptable_iterate_entries(cth
,
2903 mptable_lapic_pass2_callback
, &arg2
);
2905 panic("mptable_iterate_entries(lapic_pass2) failed\n");
2906 KKASSERT(arg2
.found_bsp
);
2908 /* Map local apic */
2909 lapic_map(lapic_addr
);
2911 mptable_unmap(&mpt
);
2915 mptable_lapic_probe(struct lapic_enumerator
*e
)
2917 vm_paddr_t mpfps_paddr
;
2919 mpfps_paddr
= mptable_probe();
2920 if (mpfps_paddr
== 0)
2923 ((struct mptable_lapic_enumerator
*)e
)->mpfps_paddr
= mpfps_paddr
;
2927 static struct mptable_lapic_enumerator mptable_lapic_enumerator
= {
2929 .lapic_prio
= LAPIC_ENUM_PRIO_MPTABLE
,
2930 .lapic_probe
= mptable_lapic_probe
,
2931 .lapic_enumerate
= mptable_lapic_enumerate
2936 mptable_apic_register(void)
2938 lapic_enumerator_register(&mptable_lapic_enumerator
.enumerator
);
2940 SYSINIT(madt
, SI_BOOT2_PRESMP
, SI_ORDER_ANY
, mptable_apic_register
, 0);