2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from BSDI: locore.s,v 1.36.2.15 1999/08/23 22:34:41 cp Exp
31 * Copyright (c) 2002 Jake Burkholder.
32 * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
60 #include <sys/param.h>
61 #include <sys/systm.h>
64 #include <sys/kernel.h>
66 #include <sys/mutex.h>
69 #include <sys/sched.h>
73 #include <vm/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_map.h>
79 #include <dev/ofw/openfirm.h>
81 #include <machine/asi.h>
82 #include <machine/atomic.h>
83 #include <machine/bus.h>
84 #include <machine/cpu.h>
85 #include <machine/md_var.h>
86 #include <machine/metadata.h>
87 #include <machine/ofw_machdep.h>
88 #include <machine/pcb.h>
89 #include <machine/smp.h>
90 #include <machine/tick.h>
91 #include <machine/tlb.h>
92 #include <machine/tte.h>
93 #include <machine/ver.h>
95 #define SUNW_STARTCPU "SUNW,start-cpu"
96 #define SUNW_STOPSELF "SUNW,stop-self"
98 static ih_func_t cpu_ipi_ast;
99 static ih_func_t cpu_ipi_preempt;
100 static ih_func_t cpu_ipi_stop;
103 * Argument area used to pass data to non-boot processors as they start up.
104 * This must be statically initialized with a known invalid CPU module ID,
105 * since the other processors will use it before the boot CPU enters the
108 struct cpu_start_args cpu_start_args = { 0, -1, -1, 0, 0, 0 };
109 struct ipi_cache_args ipi_cache_args;
110 struct ipi_tlb_args ipi_tlb_args;
111 struct pcb stoppcbs[MAXCPU];
115 cpu_ipi_selected_t *cpu_ipi_selected;
117 static vm_offset_t mp_tramp;
118 static u_int cpuid_to_mid[MAXCPU];
120 static volatile u_int shutdown_cpus;
122 static void cpu_mp_unleash(void *v);
123 static void spitfire_ipi_send(u_int mid, u_long d0, u_long d1, u_long d2);
124 static void sun4u_startcpu(phandle_t cpu, void *func, u_long arg);
126 static cpu_ipi_selected_t cheetah_ipi_selected;
127 static cpu_ipi_selected_t spitfire_ipi_selected;
129 SYSINIT(cpu_mp_unleash, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
131 CTASSERT(MAXCPU <= IDR_CHEETAH_MAX_BN_PAIRS);
132 CTASSERT(MAXCPU <= sizeof(u_int) * NBBY);
133 CTASSERT(MAXCPU <= sizeof(int) * NBBY);
141 mp_tramp = (vm_offset_t)OF_claim(NULL, PAGE_SIZE, PAGE_SIZE);
142 if (mp_tramp == (vm_offset_t)-1)
143 panic("%s", __func__);
144 bcopy(mp_tramp_code, (void *)mp_tramp, mp_tramp_code_len);
145 *(vm_offset_t *)(mp_tramp + mp_tramp_tlb_slots) = kernel_tlb_slots;
146 *(vm_offset_t *)(mp_tramp + mp_tramp_func) = (vm_offset_t)mp_startup;
147 tp = (struct tte *)(mp_tramp + mp_tramp_code_len);
148 for (i = 0; i < kernel_tlb_slots; i++) {
149 tp[i].tte_vpn = TV_VPN(kernel_tlbs[i].te_va, TS_4M);
150 tp[i].tte_data = TD_V | TD_4M | TD_PA(kernel_tlbs[i].te_pa) |
151 TD_L | TD_CP | TD_CV | TD_P | TD_W;
153 for (i = 0; i < PAGE_SIZE; i += sizeof(vm_offset_t))
157 * On UP systems cpu_ipi_selected() can be called while
158 * cpu_mp_start() wasn't so initialize these here.
160 if (cpu_impl == CPU_IMPL_ULTRASPARCIIIi ||
161 cpu_impl == CPU_IMPL_ULTRASPARCIIIip)
163 if (cpu_impl >= CPU_IMPL_ULTRASPARCIII)
164 cpu_ipi_selected = cheetah_ipi_selected;
166 cpu_ipi_selected = spitfire_ipi_selected;
170 * Probe for other CPUs.
173 cpu_mp_setmaxid(void)
179 all_cpus = 1 << curcpu;
183 for (child = OF_child(OF_peer(0)); child != 0; child = OF_peer(child))
184 if (OF_getprop(child, "device_type", buf, sizeof(buf)) > 0 &&
185 strcmp(buf, "cpu") == 0)
194 return (mp_maxid > 0);
198 sun4u_startcpu(phandle_t cpu, void *func, u_long arg)
208 (cell_t)SUNW_STARTCPU,
213 args.func = (cell_t)func;
214 args.arg = (cell_t)arg;
219 * Fire up any non-boot processors.
225 volatile struct cpu_start_args *csa;
234 mtx_init(&ipi_mtx, "ipi", NULL, MTX_SPIN);
236 intr_setup(PIL_AST, cpu_ipi_ast, -1, NULL, NULL);
237 intr_setup(PIL_RENDEZVOUS, (ih_func_t *)smp_rendezvous_action,
239 intr_setup(PIL_STOP, cpu_ipi_stop, -1, NULL, NULL);
240 intr_setup(PIL_PREEMPT, cpu_ipi_preempt, -1, NULL, NULL);
242 cpuid_to_mid[curcpu] = PCPU_GET(mid);
244 csa = &cpu_start_args;
245 for (child = OF_child(OF_peer(0)); child != 0 && mp_ncpus <= MAXCPU;
246 child = OF_peer(child)) {
247 if (OF_getprop(child, "device_type", buf, sizeof(buf)) <= 0 ||
248 strcmp(buf, "cpu") != 0)
250 if (OF_getprop(child, cpu_impl < CPU_IMPL_ULTRASPARCIII ?
251 "upa-portid" : "portid", &mid, sizeof(mid)) <= 0)
252 panic("%s: can't get module ID", __func__);
253 if (mid == PCPU_GET(mid))
255 if (OF_getprop(child, "clock-frequency", &clock,
257 panic("%s: can't get clock", __func__);
258 if (clock != PCPU_GET(clock))
259 hardclock_use_stick = 1;
262 sun4u_startcpu(child, (void *)mp_tramp, 0);
264 while (csa->csa_state != CPU_TICKSYNC)
267 csa->csa_tick = rd(tick);
268 if (cpu_impl >= CPU_IMPL_ULTRASPARCIII) {
269 while (csa->csa_state != CPU_STICKSYNC)
272 csa->csa_stick = rdstick();
274 while (csa->csa_state != CPU_INIT)
276 csa->csa_tick = csa->csa_stick = 0;
280 cpuid_to_mid[cpuid] = mid;
281 cpu_identify(csa->csa_ver, clock, cpuid);
283 va = kmem_alloc(kernel_map, PCPU_PAGES * PAGE_SIZE);
284 pc = (struct pcpu *)(va + (PCPU_PAGES * PAGE_SIZE)) - 1;
285 pcpu_init(pc, cpuid, sizeof(*pc));
287 pc->pc_clock = clock;
293 all_cpus |= 1 << cpuid;
296 KASSERT(!isjbus || mp_ncpus <= IDR_JALAPENO_MAX_BN_PAIRS,
297 ("%s: can only IPI a maximum of %d JBus-CPUs",
298 __func__, IDR_JALAPENO_MAX_BN_PAIRS));
299 PCPU_SET(other_cpus, all_cpus & ~(1 << curcpu));
304 cpu_mp_announce(void)
310 cpu_mp_unleash(void *v)
312 volatile struct cpu_start_args *csa;
321 ctx_min = TLB_CTX_USER_MIN;
322 ctx_inc = (TLB_CTX_USER_MAX - 1) / mp_ncpus;
323 csa = &cpu_start_args;
324 csa->csa_count = mp_ncpus;
325 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
326 pc->pc_tlb_ctx = ctx_min;
327 pc->pc_tlb_ctx_min = ctx_min;
328 pc->pc_tlb_ctx_max = ctx_min + ctx_inc;
331 if (pc->pc_cpuid == curcpu)
333 KASSERT(pc->pc_idlethread != NULL,
334 ("%s: idlethread", __func__));
335 pc->pc_curthread = pc->pc_idlethread;
336 pc->pc_curpcb = pc->pc_curthread->td_pcb;
337 for (i = 0; i < PCPU_PAGES; i++) {
338 va = pc->pc_addr + i * PAGE_SIZE;
339 pa = pmap_kextract(va);
341 panic("%s: pmap_kextract", __func__);
342 csa->csa_ttes[i].tte_vpn = TV_VPN(va, TS_8K);
343 csa->csa_ttes[i].tte_data = TD_V | TD_8K | TD_PA(pa) |
344 TD_L | TD_CP | TD_CV | TD_P | TD_W;
347 csa->csa_pcpu = pc->pc_addr;
348 csa->csa_mid = pc->pc_mid;
350 while (csa->csa_state != CPU_BOOTSTRAP)
361 cpu_mp_bootstrap(struct pcpu *pc)
363 volatile struct cpu_start_args *csa;
365 csa = &cpu_start_args;
366 if (cpu_impl >= CPU_IMPL_ULTRASPARCIII)
371 * Flush all non-locked TLB entries possibly left over by the
374 tlb_flush_nonlocked();
379 KASSERT(curthread != NULL, ("%s: curthread", __func__));
380 PCPU_SET(other_cpus, all_cpus & ~(1 << curcpu));
381 printf("SMP: AP CPU #%d Launched!\n", curcpu);
385 csa->csa_state = CPU_BOOTSTRAP;
386 while (csa->csa_count != 0)
389 /* Ok, now enter the scheduler. */
394 cpu_mp_shutdown(void)
399 shutdown_cpus = PCPU_GET(other_cpus);
400 if (stopped_cpus != PCPU_GET(other_cpus)) /* XXX */
401 stop_cpus(stopped_cpus ^ PCPU_GET(other_cpus));
403 while (shutdown_cpus != 0) {
405 printf("timeout shutting down CPUs.\n");
413 cpu_ipi_ast(struct trapframe *tf)
419 cpu_ipi_stop(struct trapframe *tf)
422 CTR2(KTR_SMP, "%s: stopped %d", __func__, curcpu);
423 savectx(&stoppcbs[curcpu]);
424 atomic_set_acq_int(&stopped_cpus, PCPU_GET(cpumask));
425 while ((started_cpus & PCPU_GET(cpumask)) == 0) {
426 if ((shutdown_cpus & PCPU_GET(cpumask)) != 0) {
427 atomic_clear_int(&shutdown_cpus, PCPU_GET(cpumask));
428 (void)intr_disable();
433 atomic_clear_rel_int(&started_cpus, PCPU_GET(cpumask));
434 atomic_clear_rel_int(&stopped_cpus, PCPU_GET(cpumask));
435 CTR2(KTR_SMP, "%s: restarted %d", __func__, curcpu);
439 cpu_ipi_preempt(struct trapframe *tf)
445 if (td->td_critnest > 1)
446 td->td_owepreempt = 1;
448 mi_switch(SW_INVOL | SW_PREEMPT, NULL);
453 spitfire_ipi_selected(u_int cpus, u_long d0, u_long d1, u_long d2)
457 KASSERT((cpus & (1 << curcpu)) == 0,
458 ("%s: CPU can't IPI itself", __func__));
462 spitfire_ipi_send(cpuid_to_mid[cpu], d0, d1, d2);
467 spitfire_ipi_send(u_int mid, u_long d0, u_long d1, u_long d2)
473 KASSERT((ldxa(0, ASI_INTR_DISPATCH_STATUS) & IDR_BUSY) == 0,
474 ("%s: outstanding dispatch", __func__));
475 for (i = 0; i < IPI_RETRIES; i++) {
477 stxa(AA_SDB_INTR_D0, ASI_SDB_INTR_W, d0);
478 stxa(AA_SDB_INTR_D1, ASI_SDB_INTR_W, d1);
479 stxa(AA_SDB_INTR_D2, ASI_SDB_INTR_W, d2);
481 stxa(AA_INTR_SEND | (mid << IDC_ITID_SHIFT),
484 * Workaround for SpitFire erratum #54; do a dummy read
485 * from a SDB internal register before the MEMBAR #Sync
486 * for the write to ASI_SDB_INTR_W (requiring another
487 * MEMBAR #Sync in order to make sure the write has
488 * occurred before the load).
491 (void)ldxa(AA_SDB_CNTL_HIGH, ASI_SDB_CONTROL_R);
493 while (((ids = ldxa(0, ASI_INTR_DISPATCH_STATUS)) &
497 if ((ids & (IDR_BUSY | IDR_NACK)) == 0)
500 * Leave interrupts enabled for a bit before retrying
501 * in order to avoid deadlocks if the other CPU is also
502 * trying to send an IPI.
506 if (kdb_active != 0 || panicstr != NULL)
507 printf("%s: couldn't send IPI to module 0x%u\n",
510 panic("%s: couldn't send IPI to module 0x%u",
515 cheetah_ipi_selected(u_int cpus, u_long d0, u_long d1, u_long d2)
523 KASSERT((cpus & (1 << curcpu)) == 0,
524 ("%s: CPU can't IPI itself", __func__));
525 KASSERT((ldxa(0, ASI_INTR_DISPATCH_STATUS) &
526 IDR_CHEETAH_ALL_BUSY) == 0,
527 ("%s: outstanding dispatch", __func__));
531 for (i = 0; i < IPI_RETRIES * mp_ncpus; i++) {
533 stxa(AA_SDB_INTR_D0, ASI_SDB_INTR_W, d0);
534 stxa(AA_SDB_INTR_D1, ASI_SDB_INTR_W, d1);
535 stxa(AA_SDB_INTR_D2, ASI_SDB_INTR_W, d2);
538 for (cpu = 0; cpu < mp_ncpus; cpu++) {
539 if ((cpus & (1 << cpu)) != 0) {
541 (cpuid_to_mid[cpu] << IDC_ITID_SHIFT) |
542 (isjbus ? 0 : bnp << IDC_BN_SHIFT),
548 while (((ids = ldxa(0, ASI_INTR_DISPATCH_STATUS)) &
549 IDR_CHEETAH_ALL_BUSY) != 0)
552 if ((ids & (IDR_CHEETAH_ALL_BUSY | IDR_CHEETAH_ALL_NACK)) == 0)
555 for (cpu = 0; cpu < mp_ncpus; cpu++) {
556 if ((cpus & (1 << cpu)) != 0) {
557 if ((ids & (IDR_NACK << (isjbus ?
558 (2 * cpuid_to_mid[cpu]) :
565 * On at least Fire V880 we may receive IDR_NACKs for
566 * CPUs we actually haven't tried to send an IPI to,
567 * but which apparently can be safely ignored.
572 * Leave interrupts enabled for a bit before retrying
573 * in order to avoid deadlocks if the other CPUs are
574 * also trying to send IPIs.
578 if (kdb_active != 0 || panicstr != NULL)
579 printf("%s: couldn't send IPI (cpus=0x%u ids=0x%lu)\n",
580 __func__, cpus, ids);
582 panic("%s: couldn't send IPI (cpus=0x%u ids=0x%lu)",
583 __func__, cpus, ids);