2 * Copyright (c) 2001-2005 Marcel Moolenaar
3 * Copyright (c) 2000 Doug Rabson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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$");
31 #include "opt_kstack_pages.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
38 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/kernel.h>
44 #include <sys/sched.h>
46 #include <sys/sysctl.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56 #include <machine/fpu.h>
57 #include <machine/intr.h>
58 #include <machine/mca.h>
59 #include <machine/md_var.h>
60 #include <machine/pal.h>
61 #include <machine/pcb.h>
62 #include <machine/pmap.h>
63 #include <machine/sal.h>
64 #include <machine/smp.h>
65 #include <i386/include/specialreg.h>
67 MALLOC_DEFINE(M_SMP, "SMP", "SMP related allocations");
69 void ia64_ap_startup(void);
71 #define LID_SAPIC_ID(x) ((int)((x) >> 24) & 0xff)
72 #define LID_SAPIC_EID(x) ((int)((x) >> 16) & 0xff)
73 #define LID_SAPIC_SET(id,eid) (((id & 0xff) << 8 | (eid & 0xff)) << 16);
74 #define LID_SAPIC_MASK 0xffff0000UL
76 /* Variables used by os_boot_rendez and ia64_ap_startup */
79 volatile int ap_delay;
80 volatile int ap_awake;
83 static void cpu_mp_unleash(void *);
89 return smp_topo_none();
93 ia64_store_mca_state(void* arg)
95 unsigned int ncpu = (unsigned int)(uintptr_t)arg;
98 /* ia64_mca_save_state() is CPU-sensitive, so bind ourself to our target CPU */
101 sched_bind(td, ncpu);
105 * Get and save the CPU specific MCA records. Should we get the
106 * MCA state for each processor, or just the CMC state?
108 ia64_mca_save_state(SAL_INFO_MCA);
109 ia64_mca_save_state(SAL_INFO_CMC);
115 ia64_ap_startup(void)
117 volatile struct ia64_interrupt_block *ib = IA64_INTERRUPT_BLOCK;
122 ia64_set_k4((intptr_t)pcpup);
124 vhpt = PCPU_GET(md.vhpt);
126 ia64_set_pta(vhpt + (1 << 8) + (pmap_vhpt_log2size << 2) + 1);
135 ia64_set_fpsr(IA64_FPSR_DEFAULT);
137 /* Wait until it's time for us to be unleashed */
141 /* Initialize curthread. */
142 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
143 PCPU_SET(curthread, PCPU_GET(idlethread));
145 atomic_add_int(&ap_awake, 1);
149 CTR1(KTR_SMP, "SMP: cpu%d launched", PCPU_GET(cpuid));
151 /* Acknowledge and EOI all interrupts. */
152 vector = ia64_get_ivr();
153 while (vector != 15) {
156 vector = (int)ib->ib_inta;
159 vector = ia64_get_ivr();
163 /* kick off the clock on this AP */
175 cpu_mp_setmaxid(void)
179 * Count the number of processors in the system by walking the ACPI
180 * tables. Note that we record the actual number of processors, even
181 * if this is larger than MAXCPU. We only activate MAXCPU processors.
183 mp_ncpus = ia64_count_cpus();
186 * Set the largest cpuid we're going to use. This is necessary for
189 mp_maxid = min(mp_ncpus, MAXCPU) - 1;
197 * If there's only 1 processor, or we don't have a wake-up vector,
198 * we're not going to enable SMP. Note that no wake-up vector can
199 * also mean that the wake-up mechanism is not supported. In this
200 * case we can have multiple processors, but we simply can't wake
203 return (mp_ncpus > 1 && ipi_vector[IPI_AP_WAKEUP] != 0);
207 cpu_mp_add(u_int acpiid, u_int apicid, u_int apiceid)
214 lid = LID_SAPIC_SET(apicid, apiceid);
215 cpuid = ((ia64_get_lid() & LID_SAPIC_MASK) == lid) ? 0 : smp_cpus++;
217 KASSERT((all_cpus & (1UL << cpuid)) == 0,
218 ("%s: cpu%d already in CPU map", __func__, acpiid));
221 pc = (struct pcpu *)malloc(sizeof(*pc), M_SMP, M_WAITOK);
222 pcpu_init(pc, cpuid, sizeof(*pc));
223 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
224 dpcpu_init(dpcpu, cpuid);
228 pc->pc_acpi_id = acpiid;
230 all_cpus |= (1UL << cpuid);
239 for (i = 0; i <= mp_maxid; i++) {
242 printf("cpu%d: ACPI Id=%x, SAPIC Id=%x, SAPIC Eid=%x",
243 i, pc->pc_acpi_id, LID_SAPIC_ID(pc->pc_md.lid),
244 LID_SAPIC_EID(pc->pc_md.lid));
260 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
261 pc->pc_md.current_pmap = kernel_pmap;
262 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
263 if (pc->pc_cpuid > 0) {
265 pc->pc_md.vhpt = pmap_alloc_vhpt();
266 if (pc->pc_md.vhpt == 0) {
267 printf("SMP: WARNING: unable to allocate VHPT"
268 " for cpu%d", pc->pc_cpuid);
271 ap_stack = malloc(KSTACK_PAGES * PAGE_SIZE, M_SMP,
277 printf("SMP: waking up cpu%d\n", pc->pc_cpuid);
279 ipi_send(pc, IPI_AP_WAKEUP);
283 } while (--ap_delay > 0);
284 pc->pc_md.awake = ap_awake;
287 printf("SMP: WARNING: cpu%d did not wake up\n",
295 cpu_mp_unleash(void *dummy)
305 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
307 if (pc->pc_md.awake) {
308 kproc_create(ia64_store_mca_state,
309 (void*)((uintptr_t)pc->pc_cpuid), NULL, 0, 0,
310 "mca %u", pc->pc_cpuid);
318 while (ap_awake != smp_cpus)
321 if (smp_cpus != cpus || cpus != mp_ncpus) {
322 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
323 mp_ncpus, cpus, smp_cpus);
331 * send an IPI to a set of cpus.
334 ipi_selected(cpumask_t cpus, int ipi)
338 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
339 if (cpus & pc->pc_cpumask)
345 * send an IPI to all CPUs EXCEPT myself.
348 ipi_all_but_self(int ipi)
352 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
359 * Send an IPI to the specified processor. The lid parameter holds the
360 * cr.lid (CR64) contents of the target processor. Only the id and eid
361 * fields are used here.
364 ipi_send(struct pcpu *cpu, int ipi)
366 volatile uint64_t *pipi;
369 pipi = (void *)IA64_PHYS_TO_RR6(ia64_lapic_address |
370 ((cpu->pc_md.lid & LID_SAPIC_MASK) >> 12));
371 vector = (uint64_t)(ipi_vector[ipi] & 0xff);
372 KASSERT(vector != 0, ("IPI %d is not assigned a vector", ipi));
374 CTR3(KTR_SMP, "ipi_send(%p, %ld), cpuid=%d", pipi, vector,
378 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);