2 * Copyright (c) 2015 The FreeBSD Foundation
3 * Copyright (c) 2016 Ruslan Bukin <br@bsdpad.com>
6 * Portions of this software were developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
9 * Portions of this software were developed by SRI International and the
10 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
11 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
13 * Portions of this software were developed by the University of Cambridge
14 * Computer Laboratory as part of the CTSRD Project, with support from the
15 * UK Higher Education Innovation Fund (HEIF).
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "opt_kstack_pages.h"
40 #include "opt_platform.h"
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
54 #include <sys/sched.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
63 #include <machine/intr.h>
64 #include <machine/smp.h>
65 #include <machine/sbi.h>
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofw_cpu.h>
72 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
74 extern struct pcpu __pcpu[];
76 uint32_t __riscv_boot_ap[MAXCPU];
85 static device_identify_t riscv64_cpu_identify;
86 static device_probe_t riscv64_cpu_probe;
87 static device_attach_t riscv64_cpu_attach;
89 static int ipi_handler(void *);
91 struct mtx ap_boot_mtx;
92 struct pcb stoppcbs[MAXCPU];
94 extern uint32_t boot_hart;
95 extern cpuset_t all_harts;
98 static uint32_t cpu_reg[MAXCPU][2];
100 static device_t cpu_list[MAXCPU];
102 void mpentry(unsigned long cpuid);
103 void init_secondary(uint64_t);
105 uint8_t secondary_stacks[MAXCPU][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
107 /* Set to 1 once we're ready to let the APs out of the pen. */
108 volatile int aps_ready = 0;
110 /* Temporary variables for init_secondary() */
111 void *dpcpu[MAXCPU - 1];
113 static device_method_t riscv64_cpu_methods[] = {
114 /* Device interface */
115 DEVMETHOD(device_identify, riscv64_cpu_identify),
116 DEVMETHOD(device_probe, riscv64_cpu_probe),
117 DEVMETHOD(device_attach, riscv64_cpu_attach),
122 static devclass_t riscv64_cpu_devclass;
123 static driver_t riscv64_cpu_driver = {
129 DRIVER_MODULE(riscv64_cpu, cpu, riscv64_cpu_driver, riscv64_cpu_devclass, 0, 0);
132 riscv64_cpu_identify(driver_t *driver, device_t parent)
135 if (device_find_child(parent, "riscv64_cpu", -1) != NULL)
137 if (BUS_ADD_CHILD(parent, 0, "riscv64_cpu", -1) == NULL)
138 device_printf(parent, "add child failed\n");
142 riscv64_cpu_probe(device_t dev)
146 cpuid = device_get_unit(dev);
147 if (cpuid >= MAXCPU || cpuid > mp_maxid)
155 riscv64_cpu_attach(device_t dev)
162 cpuid = device_get_unit(dev);
164 if (cpuid >= MAXCPU || cpuid > mp_maxid)
166 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
168 reg = cpu_get_cpuid(dev, ®_size);
173 device_printf(dev, "register <");
174 for (i = 0; i < reg_size; i++)
175 printf("%s%x", (i == 0) ? "" : " ", reg[i]);
179 /* Set the device to start it later */
180 cpu_list[cpuid] = dev;
186 release_aps(void *dummy __unused)
194 /* Setup the IPI handler */
195 riscv_setup_ipihandler(ipi_handler);
197 atomic_store_rel_int(&aps_ready, 1);
199 /* Wake up the other CPUs */
201 CPU_CLR(boot_hart, &mask);
203 printf("Release APs\n");
205 sbi_send_ipi(mask.__bits);
207 for (i = 0; i < 2000; i++) {
209 for (cpu = 0; cpu <= mp_maxid; cpu++) {
218 printf("APs not started\n");
220 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
223 init_secondary(uint64_t hart)
228 /* Renumber this cpu */
230 if (cpuid < boot_hart)
231 cpuid += mp_maxid + 1;
234 /* Setup the pcpu pointer */
235 pcpup = &__pcpu[cpuid];
236 __asm __volatile("mv gp, %0" :: "r"(pcpup));
238 /* Workaround: make sure wfi doesn't halt the hart */
239 csr_set(sie, SIE_SSIE);
240 csr_set(sip, SIE_SSIE);
242 /* Spin until the BSP releases the APs */
244 __asm __volatile("wfi");
246 /* Initialize curthread */
247 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
248 pcpup->pc_curthread = pcpup->pc_idlethread;
249 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
252 * Identify current CPU. This is necessary to setup
253 * affinity registers and to provide support for
254 * runtime chip identification.
258 /* Enable software interrupts */
261 /* Start per-CPU event timers. */
264 /* Enable external (PLIC) interrupts */
265 csr_set(sie, SIE_SEIE);
267 /* Activate process 0's pmap. */
268 pmap_activate_boot(vmspace_pmap(proc0.p_vmspace));
270 mtx_lock_spin(&ap_boot_mtx);
272 atomic_add_rel_32(&smp_cpus, 1);
274 if (smp_cpus == mp_ncpus) {
275 /* enable IPI's, tlb shootdown, freezes etc */
276 atomic_store_rel_int(&smp_started, 1);
279 mtx_unlock_spin(&ap_boot_mtx);
281 /* Enter the scheduler */
284 panic("scheduler returned us to init_secondary");
289 ipi_handler(void *arg)
297 cpu = PCPU_GET(cpuid);
301 ipi_bitmap = atomic_readandclear_int(PCPU_PTR(pending_ipis));
303 return (FILTER_HANDLED);
305 while ((bit = ffs(ipi_bitmap))) {
314 CTR0(KTR_SMP, "IPI_AST");
317 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
318 sched_preempt(curthread);
321 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
322 smp_rendezvous_action();
326 CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
327 savectx(&stoppcbs[cpu]);
329 /* Indicate we are stopped */
330 CPU_SET_ATOMIC(cpu, &stopped_cpus);
332 /* Wait for restart */
333 while (!CPU_ISSET(cpu, &started_cpus))
336 CPU_CLR_ATOMIC(cpu, &started_cpus);
337 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
338 CTR0(KTR_SMP, "IPI_STOP (restart)");
341 * The kernel debugger might have set a breakpoint,
342 * so flush the instruction cache.
347 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
351 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
355 return (FILTER_HANDLED);
362 return (smp_topo_none());
365 /* Determine if we running MP machine */
370 return (mp_ncpus > 1);
375 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
381 /* Check if this hart supports MMU. */
382 if (OF_getproplen(node, "mmu-type") < 0)
385 KASSERT(id < MAXCPU, ("Too many CPUs"));
387 KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
389 cpu_reg[id][0] = reg[0];
391 cpu_reg[id][1] = reg[1];
395 if (addr_size == 2) {
400 KASSERT(hart < MAXCPU, ("Too many harts."));
402 /* We are already running on this cpu */
403 if (hart == boot_hart)
407 * Rotate the CPU IDs to put the boot CPU as CPU 0.
408 * We keep the other CPUs ordered.
411 if (cpuid < boot_hart)
412 cpuid += mp_maxid + 1;
415 /* Check if we are able to start this cpu */
416 if (cpuid > mp_maxid)
419 pcpup = &__pcpu[cpuid];
420 pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
421 pcpup->pc_hart = hart;
423 dpcpu[cpuid - 1] = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
424 dpcpu_init(dpcpu[cpuid - 1], cpuid);
426 printf("Starting CPU %u (hart %lx)\n", cpuid, hart);
427 __riscv_boot_ap[hart] = 1;
429 CPU_SET(cpuid, &all_cpus);
430 CPU_SET(hart, &all_harts);
436 /* Initialize and fire up non-boot processors */
441 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
443 CPU_SET(0, &all_cpus);
444 CPU_SET(boot_hart, &all_harts);
446 switch(cpu_enum_method) {
449 ofw_cpu_early_foreach(cpu_init_fdt, true);
457 /* Introduce rest of cores to the world */
459 cpu_mp_announce(void)
464 cpu_check_mmu(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
467 /* Check if this hart supports MMU. */
468 if (OF_getproplen(node, "mmu-type") < 0)
475 cpu_mp_setmaxid(void)
480 cores = ofw_cpu_early_foreach(cpu_check_mmu, true);
482 cores = MIN(cores, MAXCPU);
484 printf("Found %d CPUs in the device tree\n", cores);
486 mp_maxid = cores - 1;
487 cpu_enum_method = CPUS_FDT;
493 printf("No CPU data, limiting to 1 core\n");