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>
36 #include <sys/sched.h>
39 #include <machine/bus.h>
40 #include <machine/cpu.h>
41 #include <machine/intr_machdep.h>
42 #include <machine/smp.h>
46 extern struct pcpu __pcpu[MAXCPU];
48 volatile static int ap_awake;
49 volatile static u_int ap_state;
50 volatile static uint32_t ap_decr;
51 volatile static uint32_t ap_tbl;
54 machdep_ap_bootstrap(void)
64 mtspr(SPR_TBL, ap_tbl);
65 __asm __volatile("mtdec %0" :: "r"(ap_decr));
69 /* Initialize curthread. */
70 PCPU_SET(curthread, PCPU_GET(idlethread));
71 PCPU_SET(curpcb, curthread->td_pcb);
73 mtmsr(mfmsr() | PSL_EE);
81 return (smp_topo_none());
91 error = powerpc_smp_first_cpu(&cpuref);
94 error = powerpc_smp_next_cpu(&cpuref);
101 * Set the largest cpuid we're going to use. This is necessary
102 * for VM initialization.
104 mp_maxid = min(mp_ncpus, MAXCPU) - 1;
112 * We're not going to enable SMP if there's only 1 processor.
114 return (mp_ncpus > 1);
120 struct cpuref bsp, cpu;
124 error = powerpc_smp_get_bsp(&bsp);
125 KASSERT(error == 0, ("Don't know BSP"));
126 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__));
128 error = powerpc_smp_first_cpu(&cpu);
130 if (cpu.cr_cpuid >= MAXCPU) {
131 printf("SMP: cpu%d: skipped -- ID out of range\n",
135 if (all_cpus & (1 << cpu.cr_cpuid)) {
136 printf("SMP: cpu%d: skipped - duplicate ID\n",
140 if (cpu.cr_cpuid != bsp.cr_cpuid) {
141 pc = &__pcpu[cpu.cr_cpuid];
142 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
145 pc->pc_cpuid = bsp.cr_cpuid;
148 pc->pc_cpumask = 1 << pc->pc_cpuid;
149 pc->pc_hwref = cpu.cr_hwref;
150 all_cpus |= pc->pc_cpumask;
153 error = powerpc_smp_next_cpu(&cpu);
158 cpu_mp_announce(void)
163 for (i = 0; i <= mp_maxid; i++) {
167 printf("cpu%d: dev=%x", i, pc->pc_hwref);
175 cpu_mp_unleash(void *dummy)
185 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
187 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
189 printf("Waking up CPU %d (dev=%x)\n", pc->pc_cpuid,
191 powerpc_smp_start_cpu(pc);
193 __asm __volatile("mfspr %0,1023" : "=r"(pc->pc_pir));
202 __asm __volatile("mftb %0" : "=r"(ap_tbl));
204 __asm __volatile("mfdec %0" : "=r"(ap_decr));
210 mtspr(SPR_TBL, ap_tbl);
212 while (ap_awake < smp_cpus)
215 if (smp_cpus != cpus || cpus != mp_ncpus) {
216 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
217 mp_ncpus, cpus, smp_cpus);
224 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
226 static u_int ipi_msg_cnt[32];
229 powerpc_ipi_handler(void *arg)
235 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
237 return (FILTER_STRAY);
238 while ((msg = ffs(ipimask) - 1) != -1) {
239 ipimask &= ~(1u << msg);
245 sched_preempt(curthread);
248 smp_rendezvous_action();
251 self = PCPU_GET(cpumask);
252 savectx(PCPU_GET(curpcb));
253 atomic_set_int(&stopped_cpus, self);
254 while ((started_cpus & self) == 0)
256 atomic_clear_int(&started_cpus, self);
257 atomic_clear_int(&stopped_cpus, self);
262 return (FILTER_HANDLED);
266 ipi_send(struct pcpu *pc, int ipi)
269 atomic_set_32(&pc->pc_ipimask, (1 << ipi));
270 PIC_IPI(pic, pc->pc_cpuid);
273 /* Send an IPI to a set of cpus. */
275 ipi_selected(cpumask_t cpus, int ipi)
279 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
280 if (cpus & pc->pc_cpumask)
285 /* Send an IPI to all CPUs EXCEPT myself. */
287 ipi_all_but_self(int ipi)
291 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {