2 * Copyright (c) 2008 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * 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 ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
37 #include <sys/sched.h>
41 #include <vm/vm_param.h>
43 #include <vm/vm_map.h>
44 #include <vm/vm_extern.h>
45 #include <vm/vm_kern.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/platform.h>
51 #include <machine/md_var.h>
52 #include <machine/smp.h>
56 extern struct pcpu __pcpu[MAXCPU];
58 volatile static int ap_awake;
59 volatile static u_int ap_letgo;
60 volatile static uint32_t ap_decr;
61 volatile static u_quad_t ap_timebase;
62 static u_int ipi_msg_cnt[32];
65 machdep_ap_bootstrap(void)
68 PCPU_SET(pir, mfspr(SPR_PIR));
70 __asm __volatile("msync; isync");
75 /* Initialize DEC and TB, sync with the BSP values */
78 __asm __volatile("mtdec %0" :: "r"(ap_decr));
80 atomic_add_int(&ap_awake, 1);
81 CTR1(KTR_SMP, "SMP: AP CPU%d launched", PCPU_GET(cpuid));
83 /* Initialize curthread */
84 PCPU_SET(curthread, PCPU_GET(idlethread));
85 PCPU_SET(curpcb, curthread->td_pcb);
87 /* Let the DEC and external interrupts go */
88 mtmsr(mfmsr() | PSL_EE);
96 return (smp_topo_none());
100 cpu_mp_setmaxid(void)
102 struct cpuref cpuref;
106 error = platform_smp_first_cpu(&cpuref);
109 error = platform_smp_next_cpu(&cpuref);
116 * Set the largest cpuid we're going to use. This is necessary
117 * for VM initialization.
119 mp_maxid = min(mp_ncpus, MAXCPU) - 1;
127 * We're not going to enable SMP if there's only 1 processor.
129 return (mp_ncpus > 1);
135 struct cpuref bsp, cpu;
139 error = platform_smp_get_bsp(&bsp);
140 KASSERT(error == 0, ("Don't know BSP"));
141 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__));
143 error = platform_smp_first_cpu(&cpu);
145 if (cpu.cr_cpuid >= MAXCPU) {
146 printf("SMP: cpu%d: skipped -- ID out of range\n",
150 if (all_cpus & (1 << cpu.cr_cpuid)) {
151 printf("SMP: cpu%d: skipped - duplicate ID\n",
155 if (cpu.cr_cpuid != bsp.cr_cpuid) {
158 pc = &__pcpu[cpu.cr_cpuid];
159 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
160 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
161 dpcpu_init(dpcpu, cpu.cr_cpuid);
164 pc->pc_cpuid = bsp.cr_cpuid;
167 pc->pc_cpumask = 1 << pc->pc_cpuid;
168 pc->pc_hwref = cpu.cr_hwref;
169 all_cpus |= pc->pc_cpumask;
171 error = platform_smp_next_cpu(&cpu);
176 cpu_mp_announce(void)
181 for (i = 0; i <= mp_maxid; i++) {
185 printf("cpu%d: dev=%x", i, pc->pc_hwref);
193 cpu_mp_unleash(void *dummy)
203 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
205 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
208 printf("Waking up CPU %d (dev=%x)\n",
209 pc->pc_cpuid, pc->pc_hwref);
211 platform_smp_start_cpu(pc);
213 timeout = 2000; /* wait 2sec for the AP */
214 while (!pc->pc_awake && --timeout > 0)
218 PCPU_SET(pir, mfspr(SPR_PIR));
223 printf("Adding CPU %d, pir=%x, awake=%x\n",
224 pc->pc_cpuid, pc->pc_pir, pc->pc_awake);
227 stopped_cpus |= (1 << pc->pc_cpuid);
232 /* Provide our current DEC and TB values for APs */
233 __asm __volatile("mfdec %0" : "=r"(ap_decr));
234 ap_timebase = mftb() + 10;
235 __asm __volatile("msync; isync");
237 /* Let APs continue */
238 atomic_store_rel_int(&ap_letgo, 1);
242 while (ap_awake < smp_cpus)
245 if (smp_cpus != cpus || cpus != mp_ncpus) {
246 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
247 mp_ncpus, cpus, smp_cpus);
254 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
257 powerpc_ipi_handler(void *arg)
263 CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr());
265 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
267 return (FILTER_STRAY);
268 while ((msg = ffs(ipimask) - 1) != -1) {
269 ipimask &= ~(1u << msg);
273 CTR1(KTR_SMP, "%s: IPI_AST", __func__);
276 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
277 sched_preempt(curthread);
280 CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__);
281 smp_rendezvous_action();
286 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
287 * necessary to add such case in the switch.
289 CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)",
291 self = PCPU_GET(cpumask);
292 savectx(PCPU_GET(curpcb));
293 atomic_set_int(&stopped_cpus, self);
294 while ((started_cpus & self) == 0)
296 atomic_clear_int(&started_cpus, self);
297 atomic_clear_int(&stopped_cpus, self);
298 CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__);
303 return (FILTER_HANDLED);
307 ipi_send(struct pcpu *pc, int ipi)
310 CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__,
311 pc, pc->pc_cpuid, ipi);
313 atomic_set_32(&pc->pc_ipimask, (1 << ipi));
314 PIC_IPI(pic, pc->pc_cpuid);
316 CTR1(KTR_SMP, "%s: sent", __func__);
319 /* Send an IPI to a set of cpus. */
321 ipi_selected(cpumask_t cpus, int ipi)
325 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
326 if (cpus & pc->pc_cpumask)
331 /* Send an IPI to all CPUs EXCEPT myself. */
333 ipi_all_but_self(int ipi)
337 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {