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 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
60 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/mutex.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_map.h>
77 #include <dev/ofw/openfirm.h>
79 #include <machine/asi.h>
80 #include <machine/atomic.h>
81 #include <machine/bus.h>
82 #include <machine/md_var.h>
83 #include <machine/metadata.h>
84 #include <machine/ofw_machdep.h>
85 #include <machine/pcb.h>
86 #include <machine/smp.h>
87 #include <machine/tick.h>
88 #include <machine/tlb.h>
89 #include <machine/tte.h>
91 static ih_func_t cpu_ipi_ast;
92 static ih_func_t cpu_ipi_stop;
95 * Argument area used to pass data to non-boot processors as they start up.
96 * This must be statically initialized with a known invalid upa module id,
97 * since the other processors will use it before the boot cpu enters the
100 struct cpu_start_args cpu_start_args = { 0, -1, -1, 0, 0 };
101 struct ipi_cache_args ipi_cache_args;
102 struct ipi_tlb_args ipi_tlb_args;
103 struct pcb stoppcbs[MAXCPU];
107 vm_offset_t mp_tramp;
111 static volatile u_int shutdown_cpus;
113 void cpu_mp_unleash(void *);
114 SYSINIT(cpu_mp_unleash, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
123 v = OF_claim(NULL, PAGE_SIZE, PAGE_SIZE);
125 panic("mp_tramp_alloc");
126 bcopy(mp_tramp_code, v, mp_tramp_code_len);
127 *(u_long *)(v + mp_tramp_tlb_slots) = kernel_tlb_slots;
128 *(u_long *)(v + mp_tramp_func) = (u_long)mp_startup;
129 tp = (struct tte *)(v + mp_tramp_code_len);
130 for (i = 0; i < kernel_tlb_slots; i++) {
131 tp[i].tte_vpn = TV_VPN(kernel_tlbs[i].te_va, TS_4M);
132 tp[i].tte_data = TD_V | TD_4M | TD_PA(kernel_tlbs[i].te_pa) |
133 TD_L | TD_CP | TD_CV | TD_P | TD_W;
135 for (i = 0; i < PAGE_SIZE; i += sizeof(long))
137 return (vm_offset_t)v;
141 * Probe for other cpus.
144 cpu_mp_setmaxid(void)
151 all_cpus = 1 << PCPU_GET(cpuid);
152 mp_boot_mid = PCPU_GET(mid);
157 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
158 if (OF_getprop(child, "device_type", buf, sizeof(buf)) > 0 &&
159 strcmp(buf, "cpu") == 0)
169 return (mp_maxid > 0);
173 sun4u_startcpu(phandle_t cpu, void *func, u_long arg)
183 (cell_t)"SUNW,start-cpu",
192 args.func = (cell_t)func;
193 args.arg = (cell_t)arg;
198 * Stop the calling CPU.
208 (cell_t)"SUNW,stop-self",
213 openfirmware_exit(&args);
214 panic("sun4u_stopself: failed.");
218 * Fire up any non-boot processors.
223 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);
242 csa = &cpu_start_args;
243 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
244 if (OF_getprop(child, "device_type", buf, sizeof(buf)) <= 0 ||
245 strcmp(buf, "cpu") != 0)
247 if (OF_getprop(child, "upa-portid", &mid, sizeof(mid)) <= 0 &&
248 OF_getprop(child, "portid", &mid, sizeof(mid)) <= 0)
249 panic("cpu_mp_start: can't get module id");
250 if (mid == mp_boot_mid)
252 if (OF_getprop(child, "clock-frequency", &clock,
254 panic("cpu_mp_start: can't get clock");
257 sun4u_startcpu(child, (void *)mp_tramp, 0);
259 while (csa->csa_state != CPU_CLKSYNC)
262 csa->csa_tick = rd(tick);
263 while (csa->csa_state != CPU_INIT)
269 cpu_identify(csa->csa_ver, clock, cpuid);
271 va = kmem_alloc(kernel_map, PCPU_PAGES * PAGE_SIZE);
272 pc = (struct pcpu *)(va + (PCPU_PAGES * PAGE_SIZE)) - 1;
273 pcpu_init(pc, cpuid, sizeof(*pc));
278 all_cpus |= 1 << cpuid;
280 PCPU_SET(other_cpus, all_cpus & ~(1 << PCPU_GET(cpuid)));
285 cpu_mp_announce(void)
290 cpu_mp_unleash(void *v)
292 volatile struct cpu_start_args *csa;
301 ctx_min = TLB_CTX_USER_MIN;
302 ctx_inc = (TLB_CTX_USER_MAX - 1) / mp_ncpus;
303 csa = &cpu_start_args;
304 csa->csa_count = mp_ncpus;
305 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
306 pc->pc_tlb_ctx = ctx_min;
307 pc->pc_tlb_ctx_min = ctx_min;
308 pc->pc_tlb_ctx_max = ctx_min + ctx_inc;
311 if (pc->pc_cpuid == PCPU_GET(cpuid))
313 KASSERT(pc->pc_idlethread != NULL,
314 ("cpu_mp_unleash: idlethread"));
315 pc->pc_curthread = pc->pc_idlethread;
316 pc->pc_curpcb = pc->pc_curthread->td_pcb;
317 for (i = 0; i < PCPU_PAGES; i++) {
318 va = pc->pc_addr + i * PAGE_SIZE;
319 pa = pmap_kextract(va);
321 panic("cpu_mp_unleash: pmap_kextract\n");
322 csa->csa_ttes[i].tte_vpn = TV_VPN(va, TS_8K);
323 csa->csa_ttes[i].tte_data = TD_V | TD_8K | TD_PA(pa) |
324 TD_L | TD_CP | TD_CV | TD_P | TD_W;
327 csa->csa_pcpu = pc->pc_addr;
328 csa->csa_mid = pc->pc_mid;
330 while (csa->csa_state != CPU_BOOTSTRAP)
341 cpu_mp_bootstrap(struct pcpu *pc)
343 volatile struct cpu_start_args *csa;
345 csa = &cpu_start_args;
351 KASSERT(curthread != NULL, ("cpu_mp_bootstrap: curthread"));
352 PCPU_SET(other_cpus, all_cpus & ~(1 << PCPU_GET(cpuid)));
353 printf("SMP: AP CPU #%d Launched!\n", PCPU_GET(cpuid));
357 csa->csa_state = CPU_BOOTSTRAP;
358 while (csa->csa_count != 0)
361 /* ok, now grab sched_lock and enter the scheduler */
362 mtx_lock_spin(&sched_lock);
364 PCPU_SET(switchtime, cpu_ticks());
365 PCPU_SET(switchticks, ticks);
366 cpu_throw(NULL, choosethread()); /* doesn't return */
370 cpu_mp_shutdown(void)
375 shutdown_cpus = PCPU_GET(other_cpus);
376 if (stopped_cpus != PCPU_GET(other_cpus)) /* XXX */
377 stop_cpus(stopped_cpus ^ PCPU_GET(other_cpus));
379 while (shutdown_cpus != 0) {
381 printf("timeout shutting down CPUs.\n");
385 /* XXX: delay a bit to allow the CPUs to actually enter the PROM. */
391 cpu_ipi_ast(struct trapframe *tf)
396 cpu_ipi_stop(struct trapframe *tf)
399 CTR1(KTR_SMP, "cpu_ipi_stop: stopped %d", PCPU_GET(cpuid));
400 savectx(&stoppcbs[PCPU_GET(cpuid)]);
401 atomic_set_acq_int(&stopped_cpus, PCPU_GET(cpumask));
402 while ((started_cpus & PCPU_GET(cpumask)) == 0) {
403 if ((shutdown_cpus & PCPU_GET(cpumask)) != 0) {
404 atomic_clear_int(&shutdown_cpus, PCPU_GET(cpumask));
408 atomic_clear_rel_int(&started_cpus, PCPU_GET(cpumask));
409 atomic_clear_rel_int(&stopped_cpus, PCPU_GET(cpumask));
410 CTR1(KTR_SMP, "cpu_ipi_stop: restarted %d", PCPU_GET(cpuid));
414 cpu_ipi_selected(u_int cpus, u_long d0, u_long d1, u_long d2)
423 cpu_ipi_send(pc->pc_mid, d0, d1, d2);
428 cpu_ipi_send(u_int mid, u_long d0, u_long d1, u_long d2)
433 KASSERT((ldxa(0, ASI_INTR_DISPATCH_STATUS) & IDR_BUSY) == 0,
434 ("cpu_ipi_send: outstanding dispatch"));
435 for (i = 0; i < IPI_RETRIES; i++) {
437 stxa(AA_SDB_INTR_D0, ASI_SDB_INTR_W, d0);
438 stxa(AA_SDB_INTR_D1, ASI_SDB_INTR_W, d1);
439 stxa(AA_SDB_INTR_D2, ASI_SDB_INTR_W, d2);
440 stxa(AA_INTR_SEND | (mid << 14), ASI_SDB_INTR_W, 0);
442 while (ldxa(0, ASI_INTR_DISPATCH_STATUS) & IDR_BUSY)
445 if ((ldxa(0, ASI_INTR_DISPATCH_STATUS) & IDR_NACK) == 0)
453 printf("cpu_ipi_send: couldn't send ipi to module %u\n", mid);
455 panic("cpu_ipi_send: couldn't send ipi");
459 ipi_selected(u_int cpus, u_int ipi)
461 cpu_ipi_selected(cpus, 0, (u_long)tl_ipi_level, ipi);
471 ipi_all_but_self(u_int ipi)
473 cpu_ipi_selected(PCPU_GET(other_cpus), 0, (u_long)tl_ipi_level, ipi);