2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1996, by Steve Passe
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the developer may NOT be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include "opt_kstack_pages.h"
36 #include "opt_sched.h"
41 #error How did you get here?
45 #error The apic device is required for SMP, add "device apic" to your config file.
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/cons.h> /* cngetc() */
53 #include <sys/cpuset.h>
55 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/memrange.h>
60 #include <sys/mutex.h>
63 #include <sys/sched.h>
65 #include <sys/sysctl.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
73 #include <x86/apicreg.h>
74 #include <machine/clock.h>
75 #include <machine/cpu.h>
76 #include <machine/cputypes.h>
78 #include <machine/md_var.h>
79 #include <machine/pcb.h>
80 #include <machine/psl.h>
81 #include <machine/smp.h>
82 #include <machine/specialreg.h>
83 #include <x86/ucode.h>
86 #include <contrib/dev/acpica/include/acpi.h>
87 #include <dev/acpica/acpivar.h>
90 #define WARMBOOT_TARGET 0
91 #define WARMBOOT_OFF (PMAP_MAP_LOW + 0x0467)
92 #define WARMBOOT_SEG (PMAP_MAP_LOW + 0x0469)
94 #define CMOS_REG (0x70)
95 #define CMOS_DATA (0x71)
96 #define BIOS_RESET (0x0f)
97 #define BIOS_WARM (0x0a)
100 * this code MUST be enabled here and in mpboot.s.
101 * it follows the very early stages of AP boot by placing values in CMOS ram.
102 * it NORMALLY will never be needed and thus the primitive method for enabling.
107 #if defined(CHECK_POINTS)
108 #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA))
109 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
111 #define CHECK_INIT(D); \
112 CHECK_WRITE(0x34, (D)); \
113 CHECK_WRITE(0x35, (D)); \
114 CHECK_WRITE(0x36, (D)); \
115 CHECK_WRITE(0x37, (D)); \
116 CHECK_WRITE(0x38, (D)); \
117 CHECK_WRITE(0x39, (D));
119 #define CHECK_PRINT(S); \
120 printf("%s: %d, %d, %d, %d, %d, %d\n", \
129 #else /* CHECK_POINTS */
131 #define CHECK_INIT(D)
132 #define CHECK_PRINT(S)
133 #define CHECK_WRITE(A, D)
135 #endif /* CHECK_POINTS */
138 * Local data and functions.
141 static void install_ap_tramp(void);
142 static int start_all_aps(void);
143 static int start_ap(int apic_id);
145 static char *ap_copyout_buf;
146 static char *ap_tramp_stack_base;
148 unsigned int boot_address;
150 #define MiB(v) (v ## ULL << 20)
152 /* Allocate memory for the AP trampoline. */
154 alloc_ap_trampoline(vm_paddr_t *physmap, unsigned int *physmap_idx)
160 for (i = *physmap_idx; i <= *physmap_idx; i -= 2) {
162 * Find a memory region big enough and below the 1MB boundary
163 * for the trampoline code.
164 * NB: needs to be page aligned.
166 if (physmap[i] >= MiB(1) ||
167 (trunc_page(physmap[i + 1]) - round_page(physmap[i])) <
168 round_page(bootMP_size))
173 * Try to steal from the end of the region to mimic previous
174 * behaviour, else fallback to steal from the start.
176 if (physmap[i + 1] < MiB(1)) {
177 boot_address = trunc_page(physmap[i + 1]);
178 if ((physmap[i + 1] - boot_address) < bootMP_size)
179 boot_address -= round_page(bootMP_size);
180 physmap[i + 1] = boot_address;
182 boot_address = round_page(physmap[i]);
183 physmap[i] = boot_address + round_page(bootMP_size);
185 if (physmap[i] == physmap[i + 1] && *physmap_idx != 0) {
186 memmove(&physmap[i], &physmap[i + 2],
187 sizeof(*physmap) * (*physmap_idx - i + 2));
194 boot_address = basemem * 1024 - bootMP_size;
197 "Cannot find enough space for the boot trampoline, placing it at %#x",
203 * Initialize the IPI handlers and start up the AP's.
210 /* Initialize the logical ID to APIC ID table. */
211 for (i = 0; i < MAXCPU; i++) {
212 cpu_apic_ids[i] = -1;
215 /* Install an inter-CPU IPI for TLB invalidation */
216 setidt(IPI_INVLTLB, IDTVEC(invltlb),
217 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
218 setidt(IPI_INVLPG, IDTVEC(invlpg),
219 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
220 setidt(IPI_INVLRNG, IDTVEC(invlrng),
221 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
223 /* Install an inter-CPU IPI for cache invalidation. */
224 setidt(IPI_INVLCACHE, IDTVEC(invlcache),
225 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
227 /* Install an inter-CPU IPI for all-CPU rendezvous */
228 setidt(IPI_RENDEZVOUS, IDTVEC(rendezvous),
229 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
231 /* Install generic inter-CPU IPI handler */
232 setidt(IPI_BITMAP_VECTOR, IDTVEC(ipi_intr_bitmap_handler),
233 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
235 /* Install an inter-CPU IPI for CPU stop/restart */
236 setidt(IPI_STOP, IDTVEC(cpustop),
237 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
239 /* Install an inter-CPU IPI for CPU suspend/resume */
240 setidt(IPI_SUSPEND, IDTVEC(cpususpend),
241 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
243 /* Install an IPI for calling delayed SWI */
244 setidt(IPI_SWI, IDTVEC(ipi_swi),
245 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
247 /* Set boot_cpu_id if needed. */
248 if (boot_cpu_id == -1) {
249 boot_cpu_id = PCPU_GET(apic_id);
250 cpu_info[boot_cpu_id].cpu_bsp = 1;
252 KASSERT(boot_cpu_id == PCPU_GET(apic_id),
253 ("BSP's APIC ID doesn't match boot_cpu_id"));
255 /* Probe logical/physical core configuration. */
260 /* Start each Application Processor */
263 set_interrupt_apic_ids();
265 #if defined(DEV_ACPI) && MAXMEMDOM > 1
266 acpi_pxm_set_cpu_locality();
271 * AP CPU's call this to initialize themselves.
277 struct i386tss *common_tssp;
278 struct region_descriptor r_gdt, r_idt;
279 int gsel_tss, myid, x;
282 /* bootAP is set in start_ap() to our ID. */
285 /* Update microcode before doing anything else. */
288 /* Get per-cpu data */
291 /* prime data page for it to use */
292 pcpu_init(pc, myid, sizeof(struct pcpu));
293 dpcpu_init(dpcpu, myid);
294 pc->pc_apic_id = cpu_apic_ids[myid];
295 pc->pc_prvspace = pc;
296 pc->pc_curthread = 0;
297 pc->pc_common_tssp = common_tssp = &(__pcpu[0].pc_common_tssp)[myid];
301 gdt_segs[GPRIV_SEL].ssd_base = (int)pc;
302 gdt_segs[GPROC0_SEL].ssd_base = (int)common_tssp;
303 gdt_segs[GLDT_SEL].ssd_base = (int)ldt;
305 for (x = 0; x < NGDT; x++) {
306 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd);
309 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
310 r_gdt.rd_base = (int) &gdt[myid * NGDT];
311 lgdt(&r_gdt); /* does magic intra-segment return */
313 r_idt.rd_limit = sizeof(struct gate_descriptor) * NIDT - 1;
314 r_idt.rd_base = (int)idt;
318 PCPU_SET(currentldt, _default_ldt);
320 PCPU_SET(trampstk, (uintptr_t)ap_tramp_stack_base + TRAMP_STACK_SZ -
323 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
324 gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
325 common_tssp->tss_esp0 = PCPU_GET(trampstk);
326 common_tssp->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
327 common_tssp->tss_ioopt = sizeof(struct i386tss) << 16;
328 PCPU_SET(tss_gdt, &gdt[myid * NGDT + GPROC0_SEL].sd);
329 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
332 PCPU_SET(fsgs_gdt, &gdt[myid * NGDT + GUFS_SEL].sd);
333 PCPU_SET(copyout_buf, ap_copyout_buf);
336 * Set to a known state:
337 * Set by mpboot.s: CR0_PG, CR0_PE
338 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
341 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
343 CHECK_WRITE(0x38, 5);
345 /* signal our startup to the BSP. */
347 CHECK_WRITE(0x39, 6);
349 /* Spin until the BSP releases the AP's. */
350 while (atomic_load_acq_int(&aps_ready) == 0)
353 /* BSP may have changed PTD while we were waiting */
356 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
360 init_secondary_tail();
364 * start each AP in our list
366 #define TMPMAP_START 1
371 u_int32_t mpbioswarmvec;
374 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
376 pmap_remap_lower(true);
378 /* install the AP 1st level boot code */
381 /* save the current value of the warm-start vector */
382 mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF);
383 outb(CMOS_REG, BIOS_RESET);
384 mpbiosreason = inb(CMOS_DATA);
386 /* take advantage of the P==V mapping for PTD[0] for AP boot */
389 for (cpu = 1; cpu < mp_ncpus; cpu++) {
390 apic_id = cpu_apic_ids[cpu];
392 /* allocate and set up a boot stack data page */
393 bootstacks[cpu] = kmem_malloc(kstack_pages * PAGE_SIZE,
395 dpcpu = kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
396 /* setup a vector to our boot code */
397 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
398 *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4);
399 outb(CMOS_REG, BIOS_RESET);
400 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */
402 bootSTK = (char *)bootstacks[cpu] + kstack_pages *
406 ap_tramp_stack_base = pmap_trm_alloc(TRAMP_STACK_SZ, M_NOWAIT);
407 ap_copyout_buf = pmap_trm_alloc(TRAMP_COPYOUT_SZ, M_NOWAIT);
409 /* attempt to start the Application Processor */
410 CHECK_INIT(99); /* setup checkpoints */
411 if (!start_ap(apic_id)) {
412 printf("AP #%d (PHY# %d) failed!\n", cpu, apic_id);
413 CHECK_PRINT("trace"); /* show checkpoints */
414 /* better panic as the AP may be running loose */
415 printf("panic y/n? [y] ");
419 CHECK_PRINT("trace"); /* show checkpoints */
421 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */
424 pmap_remap_lower(false);
426 /* restore the warmstart vector */
427 *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec;
429 outb(CMOS_REG, BIOS_RESET);
430 outb(CMOS_DATA, mpbiosreason);
432 /* number of APs actually started */
437 * load the 1st level AP boot code into base memory.
440 /* targets for relocation */
441 extern void bigJump(void);
442 extern void bootCodeSeg(void);
443 extern void bootDataSeg(void);
444 extern void MPentry(void);
446 extern u_int mp_gdtbase;
449 install_ap_tramp(void)
452 int size = *(int *) ((u_long) & bootMP_size);
453 vm_offset_t va = boot_address;
454 u_char *src = (u_char *) ((u_long) bootMP);
455 u_char *dst = (u_char *) va;
456 u_int boot_base = (u_int) bootMP;
461 KASSERT (size <= PAGE_SIZE,
462 ("'size' do not fit into PAGE_SIZE, as expected."));
463 pmap_kenter(va, boot_address);
464 pmap_invalidate_page (kernel_pmap, va);
465 for (x = 0; x < size; ++x)
469 * modify addresses in code we just moved to basemem. unfortunately we
470 * need fairly detailed info about mpboot.s for this to work. changes
471 * to mpboot.s might require changes here.
474 /* boot code is located in KERNEL space */
477 /* modify the lgdt arg */
478 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
479 *dst32 = boot_address + ((u_int) & MP_GDT - boot_base);
481 /* modify the ljmp target for MPentry() */
482 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
483 *dst32 = (u_int)MPentry;
485 /* modify the target for boot code segment */
486 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
487 dst8 = (u_int8_t *) (dst16 + 1);
488 *dst16 = (u_int) boot_address & 0xffff;
489 *dst8 = ((u_int) boot_address >> 16) & 0xff;
491 /* modify the target for boot data segment */
492 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
493 dst8 = (u_int8_t *) (dst16 + 1);
494 *dst16 = (u_int) boot_address & 0xffff;
495 *dst8 = ((u_int) boot_address >> 16) & 0xff;
499 * This function starts the AP (application processor) identified
500 * by the APIC ID 'physicalCpu'. It does quite a "song and dance"
501 * to accomplish this. This is necessary because of the nuances
502 * of the different hardware we might encounter. It isn't pretty,
503 * but it seems to work.
506 start_ap(int apic_id)
511 /* calculate the vector */
512 vector = (boot_address >> 12) & 0xff;
514 /* used as a watchpoint to signal AP startup */
517 ipi_startup(apic_id, vector);
519 /* Wait up to 5 seconds for it to start. */
520 for (ms = 0; ms < 5000; ms++) {
522 return 1; /* return SUCCESS */
525 return 0; /* return FAILURE */
529 * Flush the TLB on other CPU's
532 /* Variables needed for SMP tlb shootdown. */
533 vm_offset_t smp_tlb_addr1, smp_tlb_addr2;
535 volatile uint32_t smp_tlb_generation;
538 * Used by pmap to request cache or TLB invalidation on local and
539 * remote processors. Mask provides the set of remote CPUs which are
540 * to be signalled with the invalidation IPI. Vector specifies which
541 * invalidation IPI is used. As an optimization, the curcpu_cb
542 * callback is invoked on the calling CPU while waiting for remote
543 * CPUs to complete the operation.
545 * The callback function is called unconditionally on the caller's
546 * underlying processor, even when this processor is not set in the
547 * mask. So, the callback function must be prepared to handle such
548 * spurious invocations.
551 smp_targeted_tlb_shootdown(cpuset_t mask, u_int vector, pmap_t pmap,
552 vm_offset_t addr1, vm_offset_t addr2, smp_invl_cb_t curcpu_cb)
555 volatile uint32_t *p_cpudone;
560 * It is not necessary to signal other CPUs while booting or
561 * when in the debugger.
563 if (kdb_active || KERNEL_PANICKED() || !smp_started) {
564 curcpu_cb(pmap, addr1, addr2);
571 * Check for other cpus. Return if none.
573 if (CPU_ISFULLSET(&mask)) {
577 CPU_CLR(PCPU_GET(cpuid), &mask);
578 if (CPU_EMPTY(&mask))
582 KASSERT((read_eflags() & PSL_I) != 0,
583 ("smp_targeted_tlb_shootdown: interrupts disabled"));
584 mtx_lock_spin(&smp_ipi_mtx);
585 smp_tlb_addr1 = addr1;
586 smp_tlb_addr2 = addr2;
588 generation = ++smp_tlb_generation;
589 if (CPU_ISFULLSET(&mask)) {
590 ipi_all_but_self(vector);
591 other_cpus = all_cpus;
592 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
595 ipi_selected(mask, vector);
597 curcpu_cb(pmap, addr1, addr2);
598 CPU_FOREACH_ISSET(cpu, &other_cpus) {
599 p_cpudone = &cpuid_to_pcpu[cpu]->pc_smp_tlb_done;
600 while (*p_cpudone != generation)
603 mtx_unlock_spin(&smp_ipi_mtx);
608 curcpu_cb(pmap, addr1, addr2);
613 smp_masked_invltlb(cpuset_t mask, pmap_t pmap, smp_invl_cb_t curcpu_cb)
615 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, pmap, 0, 0, curcpu_cb);
616 #ifdef COUNT_XINVLTLB_HITS
622 smp_masked_invlpg(cpuset_t mask, vm_offset_t addr, pmap_t pmap,
623 smp_invl_cb_t curcpu_cb)
625 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, pmap, addr, 0, curcpu_cb);
626 #ifdef COUNT_XINVLTLB_HITS
632 smp_masked_invlpg_range(cpuset_t mask, vm_offset_t addr1, vm_offset_t addr2,
633 pmap_t pmap, smp_invl_cb_t curcpu_cb)
635 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, pmap, addr1, addr2,
637 #ifdef COUNT_XINVLTLB_HITS
639 ipi_range_size += (addr2 - addr1) / PAGE_SIZE;
644 smp_cache_flush(smp_invl_cb_t curcpu_cb)
646 smp_targeted_tlb_shootdown(all_cpus, IPI_INVLCACHE, NULL, 0, 0,
651 * Handlers for TLB related IPIs
654 invltlb_handler(void)
659 #ifdef COUNT_XINVLTLB_HITS
660 xhits_gbl[PCPU_GET(cpuid)]++;
661 #endif /* COUNT_XINVLTLB_HITS */
663 (*ipi_invltlb_counts[PCPU_GET(cpuid)])++;
664 #endif /* COUNT_IPIS */
667 * Reading the generation here allows greater parallelism
668 * since invalidating the TLB is a serializing operation.
670 generation = smp_tlb_generation;
671 if (smp_tlb_pmap == kernel_pmap)
673 PCPU_SET(smp_tlb_done, generation);
682 #ifdef COUNT_XINVLTLB_HITS
683 xhits_pg[PCPU_GET(cpuid)]++;
684 #endif /* COUNT_XINVLTLB_HITS */
686 (*ipi_invlpg_counts[PCPU_GET(cpuid)])++;
687 #endif /* COUNT_IPIS */
689 generation = smp_tlb_generation; /* Overlap with serialization */
690 if (smp_tlb_pmap == kernel_pmap)
691 invlpg(smp_tlb_addr1);
692 PCPU_SET(smp_tlb_done, generation);
696 invlrng_handler(void)
698 vm_offset_t addr, addr2;
702 #ifdef COUNT_XINVLTLB_HITS
703 xhits_rng[PCPU_GET(cpuid)]++;
704 #endif /* COUNT_XINVLTLB_HITS */
706 (*ipi_invlrng_counts[PCPU_GET(cpuid)])++;
707 #endif /* COUNT_IPIS */
709 addr = smp_tlb_addr1;
710 addr2 = smp_tlb_addr2;
711 generation = smp_tlb_generation; /* Overlap with serialization */
712 if (smp_tlb_pmap == kernel_pmap) {
716 } while (addr < addr2);
719 PCPU_SET(smp_tlb_done, generation);
723 invlcache_handler(void)
729 (*ipi_invlcache_counts[PCPU_GET(cpuid)])++;
730 #endif /* COUNT_IPIS */
733 * Reading the generation here allows greater parallelism
734 * since wbinvd is a serializing instruction. Without the
735 * temporary, we'd wait for wbinvd to complete, then the read
736 * would execute, then the dependent write, which must then
737 * complete before return from interrupt.
739 generation = smp_tlb_generation;
741 PCPU_SET(smp_tlb_done, generation);