2 * Copyright (c) 2009 Neelkanth Natu
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. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/kernel.h>
41 #include <sys/sched.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_kern.h>
49 #include <machine/clock.h>
50 #include <machine/smp.h>
51 #include <machine/hwfunc.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/cache.h>
54 #include <machine/tlb.h>
56 struct pcb stoppcbs[MAXCPU];
59 static struct mtx ap_boot_mtx;
61 static volatile int aps_ready;
62 static volatile int mp_naps;
65 ipi_send(struct pcpu *pc, int ipi)
68 CTR3(KTR_SMP, "%s: cpu=%d, ipi=%x", __func__, pc->pc_cpuid, ipi);
70 atomic_set_32(&pc->pc_pending_ipis, ipi);
71 platform_ipi_send(pc->pc_cpuid);
73 CTR1(KTR_SMP, "%s: sent", __func__);
77 ipi_all_but_self(int ipi)
81 other_cpus = all_cpus;
82 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
83 ipi_selected(other_cpus, ipi);
86 /* Send an IPI to a set of cpus. */
88 ipi_selected(cpuset_t cpus, int ipi)
92 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
93 if (CPU_ISSET(pc->pc_cpuid, &cpus)) {
94 CTR3(KTR_SMP, "%s: pc: %p, ipi: %x\n", __func__, pc,
101 /* Send an IPI to a specific CPU. */
103 ipi_cpu(int cpu, u_int ipi)
106 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x\n", __func__, cpu, ipi);
107 ipi_send(cpuid_to_pcpu[cpu], ipi);
111 * Handle an IPI sent to this processor.
114 mips_ipi_handler(void *arg)
116 u_int cpu, ipi, ipi_bitmap;
119 cpu = PCPU_GET(cpuid);
121 platform_ipi_clear(); /* quiesce the pending ipi interrupt */
123 ipi_bitmap = atomic_readandclear_int(PCPU_PTR(pending_ipis));
125 return (FILTER_STRAY);
127 CTR1(KTR_SMP, "smp_handle_ipi(), ipi_bitmap=%x", ipi_bitmap);
129 while ((bit = ffs(ipi_bitmap))) {
135 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
136 smp_rendezvous_action();
140 CTR0(KTR_SMP, "IPI_AST");
145 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
146 * necessary to add it in the switch.
148 CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
150 savectx(&stoppcbs[cpu]);
153 /* Indicate we are stopped */
154 CPU_SET_ATOMIC(cpu, &stopped_cpus);
156 /* Wait for restart */
157 while (!CPU_ISSET(cpu, &started_cpus))
160 CPU_CLR_ATOMIC(cpu, &started_cpus);
161 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
162 CTR0(KTR_SMP, "IPI_STOP (restart)");
165 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
166 sched_preempt(curthread);
169 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
173 panic("Unknown IPI 0x%0x on cpu %d", ipi, curcpu);
177 return (FILTER_HANDLED);
186 dpcpu = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO);
190 if (platform_start_ap(cpuid) != 0)
191 return (-1); /* could not start AP */
193 for (ms = 0; ms < 5000; ++ms) {
195 return (0); /* success */
200 return (-2); /* timeout initializing AP */
204 cpu_mp_setmaxid(void)
209 platform_cpu_mask(&cpumask);
212 while ((cpu = CPU_FFS(&cpumask)) != 0) {
215 CPU_CLR(cpu, &cpumask);
221 mp_maxid = min(last, MAXCPU) - 1;
225 cpu_mp_announce(void)
233 return (platform_smp_topo());
240 return (mp_ncpus > 1);
249 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
252 platform_cpu_mask(&cpumask);
254 while (!CPU_EMPTY(&cpumask)) {
255 cpuid = CPU_FFS(&cpumask) - 1;
256 CPU_CLR(cpuid, &cpumask);
258 if (cpuid >= MAXCPU) {
259 printf("cpu_mp_start: ignoring AP #%d.\n", cpuid);
263 if (cpuid != platform_processor_id()) {
264 if ((error = start_ap(cpuid)) != 0) {
265 printf("AP #%d failed to start: %d\n", cpuid, error);
269 printf("AP #%d started!\n", cpuid);
271 CPU_SET(cpuid, &all_cpus);
276 smp_init_secondary(u_int32_t cpuid)
281 tlb_invalidate_all();
282 mips_wr_wired(VMWIRED_ENTRIES);
285 * We assume that the L1 cache on the APs is identical to the one
288 mips_dcache_wbinv_all();
289 mips_icache_sync_all();
295 pcpu_init(PCPU_ADDR(cpuid), cpuid, sizeof(struct pcpu));
296 dpcpu_init(dpcpu, cpuid);
298 /* The AP has initialized successfully - allow the BSP to proceed */
301 /* Spin until the BSP is ready to release the APs */
305 /* Initialize curthread. */
306 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
307 PCPU_SET(curthread, PCPU_GET(idlethread));
309 mtx_lock_spin(&ap_boot_mtx);
313 CTR1(KTR_SMP, "SMP: AP CPU #%d launched", PCPU_GET(cpuid));
316 printf("SMP: AP CPU #%d launched.\n", PCPU_GET(cpuid));
318 if (smp_cpus == mp_ncpus) {
319 atomic_store_rel_int(&smp_started, 1);
322 mtx_unlock_spin(&ap_boot_mtx);
324 while (smp_started == 0)
327 /* Start per-CPU event timers. */
330 /* enter the scheduler */
333 panic("scheduler returned us to %s", __func__);
338 release_aps(void *dummy __unused)
348 ipi_irq = platform_ipi_intrnum();
349 cpu_establish_hardintr("ipi", mips_ipi_handler, NULL, NULL, ipi_irq,
350 INTR_TYPE_MISC | INTR_EXCL, NULL);
352 atomic_store_rel_int(&aps_ready, 1);
354 while (smp_started == 0)
358 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);