2 * Copyright (c) 2015 The FreeBSD Foundation
5 * This software was developed by Andrew Turner under
6 * sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "opt_kstack_pages.h"
32 #include "opt_platform.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
46 #include <sys/sched.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
54 #include <machine/intr.h>
55 #include <machine/smp.h>
57 #include <machine/vfp.h>
61 #include <dev/ofw/openfirm.h>
62 #include <dev/ofw/ofw_cpu.h>
65 #include <dev/psci/psci.h>
67 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
69 extern struct pcpu __pcpu[];
78 static device_identify_t arm64_cpu_identify;
79 static device_probe_t arm64_cpu_probe;
80 static device_attach_t arm64_cpu_attach;
82 static int ipi_handler(void *arg);
84 struct mtx ap_boot_mtx;
85 struct pcb stoppcbs[MAXCPU];
88 static uint32_t cpu_reg[MAXCPU][2];
90 static device_t cpu_list[MAXCPU];
92 void mpentry(unsigned long cpuid);
93 void init_secondary(uint64_t);
95 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
97 /* Set to 1 once we're ready to let the APs out of the pen. */
98 volatile int aps_ready = 0;
100 /* Temporary variables for init_secondary() */
101 void *dpcpu[MAXCPU - 1];
103 static device_method_t arm64_cpu_methods[] = {
104 /* Device interface */
105 DEVMETHOD(device_identify, arm64_cpu_identify),
106 DEVMETHOD(device_probe, arm64_cpu_probe),
107 DEVMETHOD(device_attach, arm64_cpu_attach),
112 static devclass_t arm64_cpu_devclass;
113 static driver_t arm64_cpu_driver = {
119 DRIVER_MODULE(arm64_cpu, cpu, arm64_cpu_driver, arm64_cpu_devclass, 0, 0);
122 arm64_cpu_identify(driver_t *driver, device_t parent)
125 if (device_find_child(parent, "arm64_cpu", -1) != NULL)
127 if (BUS_ADD_CHILD(parent, 0, "arm64_cpu", -1) == NULL)
128 device_printf(parent, "add child failed\n");
132 arm64_cpu_probe(device_t dev)
136 cpuid = device_get_unit(dev);
137 if (cpuid >= MAXCPU || cpuid > mp_maxid)
145 arm64_cpu_attach(device_t dev)
152 cpuid = device_get_unit(dev);
154 if (cpuid >= MAXCPU || cpuid > mp_maxid)
156 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
158 reg = cpu_get_cpuid(dev, ®_size);
163 device_printf(dev, "register <");
164 for (i = 0; i < reg_size; i++)
165 printf("%s%x", (i == 0) ? "" : " ", reg[i]);
169 /* Set the device to start it later */
170 cpu_list[cpuid] = dev;
176 release_aps(void *dummy __unused)
180 /* Setup the IPI handler */
181 for (i = 0; i < COUNT_IPI; i++)
182 arm_setup_ipihandler(ipi_handler, i);
184 atomic_store_rel_int(&aps_ready, 1);
185 /* Wake up the other CPUs */
186 __asm __volatile("sev");
188 printf("Release APs\n");
190 for (i = 0; i < 2000; i++) {
196 printf("APs not started\n");
198 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
201 init_secondary(uint64_t cpu)
206 pcpup = &__pcpu[cpu];
208 * Set the pcpu pointer with a backup in tpidr_el1 to be
209 * loaded when entering the kernel from userland.
213 "msr tpidr_el1, %0" :: "r"(pcpup));
215 /* Spin until the BSP releases the APs */
217 __asm __volatile("wfe");
219 /* Initialize curthread */
220 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
221 pcpup->pc_curthread = pcpup->pc_idlethread;
222 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
225 * Identify current CPU. This is necessary to setup
226 * affinity registers and to provide support for
227 * runtime chip identification.
231 /* Configure the interrupt controller */
232 arm_init_secondary();
234 for (i = 0; i < COUNT_IPI; i++)
237 /* Start per-CPU event timers. */
244 /* Enable interrupts */
247 mtx_lock_spin(&ap_boot_mtx);
249 atomic_add_rel_32(&smp_cpus, 1);
251 if (smp_cpus == mp_ncpus) {
252 /* enable IPI's, tlb shootdown, freezes etc */
253 atomic_store_rel_int(&smp_started, 1);
256 mtx_unlock_spin(&ap_boot_mtx);
258 /* Enter the scheduler */
261 panic("scheduler returned us to init_secondary");
266 ipi_handler(void *arg)
270 arg = (void *)((uintptr_t)arg & ~(1 << 16));
271 KASSERT((uintptr_t)arg < COUNT_IPI,
272 ("Invalid IPI %ju", (uintptr_t)arg));
274 cpu = PCPU_GET(cpuid);
275 ipi = (uintptr_t)arg;
279 CTR0(KTR_SMP, "IPI_AST");
282 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
283 sched_preempt(curthread);
286 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
287 smp_rendezvous_action();
291 CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
292 savectx(&stoppcbs[cpu]);
294 /* Indicate we are stopped */
295 CPU_SET_ATOMIC(cpu, &stopped_cpus);
297 /* Wait for restart */
298 while (!CPU_ISSET(cpu, &started_cpus))
301 CPU_CLR_ATOMIC(cpu, &started_cpus);
302 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
303 CTR0(KTR_SMP, "IPI_STOP (restart)");
306 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
310 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
313 return (FILTER_HANDLED);
320 return (smp_topo_none());
323 /* Determine if we running MP machine */
328 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
334 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
341 /* Check we are able to start this cpu */
345 KASSERT(id < MAXCPU, ("Too mant CPUs"));
347 KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
349 cpu_reg[id][0] = reg[0];
351 cpu_reg[id][1] = reg[1];
354 /* We are already running on cpu 0 */
360 pcpu_init(pcpup, id, sizeof(struct pcpu));
362 dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
364 dpcpu_init(dpcpu[id - 1], id);
367 if (addr_size == 2) {
369 target_cpu |= reg[1];
372 printf("Starting CPU %u (%lx)\n", id, target_cpu);
373 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
375 err = psci_cpu_on(target_cpu, pa, id);
376 if (err != PSCI_RETVAL_SUCCESS) {
377 /* Panic here if INVARIANTS are enabled */
378 KASSERT(0, ("Failed to start CPU %u (%lx)\n", id, target_cpu));
381 kmem_free(kernel_arena, (vm_offset_t)dpcpu[id - 1], DPCPU_SIZE);
382 dpcpu[id - 1] = NULL;
383 /* Notify the user that the CPU failed to start */
384 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
386 CPU_SET(id, &all_cpus);
392 /* Initialize and fire up non-boot processors */
397 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
399 CPU_SET(0, &all_cpus);
401 switch(cpu_enum_method) {
404 ofw_cpu_early_foreach(cpu_init_fdt, true);
412 /* Introduce rest of cores to the world */
414 cpu_mp_announce(void)
419 cpu_mp_setmaxid(void)
424 cores = ofw_cpu_early_foreach(NULL, false);
426 cores = MIN(cores, MAXCPU);
428 printf("Found %d CPUs in the device tree\n", cores);
430 mp_maxid = cores - 1;
431 cpu_enum_method = CPUS_FDT;
437 printf("No CPU data, limiting to 1 core\n");