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/debug_monitor.h>
55 #include <machine/intr.h>
56 #include <machine/smp.h>
58 #include <machine/vfp.h>
62 #include <dev/ofw/openfirm.h>
63 #include <dev/ofw/ofw_cpu.h>
66 #include <dev/psci/psci.h>
68 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
70 extern struct pcpu __pcpu[];
79 static device_identify_t arm64_cpu_identify;
80 static device_probe_t arm64_cpu_probe;
81 static device_attach_t arm64_cpu_attach;
83 static void ipi_ast(void *);
84 static void ipi_hardclock(void *);
85 static void ipi_preempt(void *);
86 static void ipi_rendezvous(void *);
87 static void ipi_stop(void *);
89 static int ipi_handler(void *arg);
91 struct mtx ap_boot_mtx;
92 struct pcb stoppcbs[MAXCPU];
95 static uint32_t cpu_reg[MAXCPU][2];
97 static device_t cpu_list[MAXCPU];
99 void mpentry(unsigned long cpuid);
100 void init_secondary(uint64_t);
102 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
104 /* Set to 1 once we're ready to let the APs out of the pen. */
105 volatile int aps_ready = 0;
107 /* Temporary variables for init_secondary() */
108 void *dpcpu[MAXCPU - 1];
110 static device_method_t arm64_cpu_methods[] = {
111 /* Device interface */
112 DEVMETHOD(device_identify, arm64_cpu_identify),
113 DEVMETHOD(device_probe, arm64_cpu_probe),
114 DEVMETHOD(device_attach, arm64_cpu_attach),
119 static devclass_t arm64_cpu_devclass;
120 static driver_t arm64_cpu_driver = {
126 DRIVER_MODULE(arm64_cpu, cpu, arm64_cpu_driver, arm64_cpu_devclass, 0, 0);
129 arm64_cpu_identify(driver_t *driver, device_t parent)
132 if (device_find_child(parent, "arm64_cpu", -1) != NULL)
134 if (BUS_ADD_CHILD(parent, 0, "arm64_cpu", -1) == NULL)
135 device_printf(parent, "add child failed\n");
139 arm64_cpu_probe(device_t dev)
143 cpuid = device_get_unit(dev);
144 if (cpuid >= MAXCPU || cpuid > mp_maxid)
152 arm64_cpu_attach(device_t dev)
159 cpuid = device_get_unit(dev);
161 if (cpuid >= MAXCPU || cpuid > mp_maxid)
163 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
165 reg = cpu_get_cpuid(dev, ®_size);
170 device_printf(dev, "register <");
171 for (i = 0; i < reg_size; i++)
172 printf("%s%x", (i == 0) ? "" : " ", reg[i]);
176 /* Set the device to start it later */
177 cpu_list[cpuid] = dev;
183 release_aps(void *dummy __unused)
187 /* Setup the IPI handler */
188 for (i = 0; i < INTR_IPI_COUNT; i++)
189 arm_setup_ipihandler(ipi_handler, i);
191 atomic_store_rel_int(&aps_ready, 1);
192 /* Wake up the other CPUs */
193 __asm __volatile("sev");
195 printf("Release APs\n");
197 for (i = 0; i < 2000; i++) {
199 for (cpu = 0; cpu <= mp_maxid; cpu++) {
202 print_cpu_features(cpu);
209 printf("APs not started\n");
211 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
214 init_secondary(uint64_t cpu)
219 pcpup = &__pcpu[cpu];
221 * Set the pcpu pointer with a backup in tpidr_el1 to be
222 * loaded when entering the kernel from userland.
226 "msr tpidr_el1, %0" :: "r"(pcpup));
228 /* Spin until the BSP releases the APs */
230 __asm __volatile("wfe");
232 /* Initialize curthread */
233 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
234 pcpup->pc_curthread = pcpup->pc_idlethread;
235 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
238 * Identify current CPU. This is necessary to setup
239 * affinity registers and to provide support for
240 * runtime chip identification.
244 /* Configure the interrupt controller */
245 arm_init_secondary();
247 for (i = 0; i < INTR_IPI_COUNT; i++)
250 /* Start per-CPU event timers. */
259 /* Enable interrupts */
262 mtx_lock_spin(&ap_boot_mtx);
264 atomic_add_rel_32(&smp_cpus, 1);
266 if (smp_cpus == mp_ncpus) {
267 /* enable IPI's, tlb shootdown, freezes etc */
268 atomic_store_rel_int(&smp_started, 1);
271 mtx_unlock_spin(&ap_boot_mtx);
273 /* Enter the scheduler */
276 panic("scheduler returned us to init_secondary");
281 ipi_ast(void *dummy __unused)
284 CTR0(KTR_SMP, "IPI_AST");
288 ipi_hardclock(void *dummy __unused)
291 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
296 ipi_preempt(void *dummy __unused)
298 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
299 sched_preempt(curthread);
303 ipi_rendezvous(void *dummy __unused)
306 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
307 smp_rendezvous_action();
311 ipi_stop(void *dummy __unused)
315 CTR0(KTR_SMP, "IPI_STOP");
317 cpu = PCPU_GET(cpuid);
318 savectx(&stoppcbs[cpu]);
320 /* Indicate we are stopped */
321 CPU_SET_ATOMIC(cpu, &stopped_cpus);
323 /* Wait for restart */
324 while (!CPU_ISSET(cpu, &started_cpus))
327 CPU_CLR_ATOMIC(cpu, &started_cpus);
328 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
329 CTR0(KTR_SMP, "IPI_STOP (restart)");
333 ipi_handler(void *arg)
337 arg = (void *)((uintptr_t)arg & ~(1 << 16));
338 KASSERT((uintptr_t)arg < INTR_IPI_COUNT,
339 ("Invalid IPI %ju", (uintptr_t)arg));
341 cpu = PCPU_GET(cpuid);
342 ipi = (uintptr_t)arg;
352 ipi_rendezvous(NULL);
362 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
365 return (FILTER_HANDLED);
372 return (smp_topo_none());
375 /* Determine if we running MP machine */
380 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
386 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
393 /* Check we are able to start this cpu */
397 KASSERT(id < MAXCPU, ("Too mant CPUs"));
399 KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
401 cpu_reg[id][0] = reg[0];
403 cpu_reg[id][1] = reg[1];
406 /* We are already running on cpu 0 */
412 pcpu_init(pcpup, id, sizeof(struct pcpu));
414 dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
416 dpcpu_init(dpcpu[id - 1], id);
419 if (addr_size == 2) {
421 target_cpu |= reg[1];
424 printf("Starting CPU %u (%lx)\n", id, target_cpu);
425 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
427 err = psci_cpu_on(target_cpu, pa, id);
428 if (err != PSCI_RETVAL_SUCCESS) {
429 /* Panic here if INVARIANTS are enabled */
430 KASSERT(0, ("Failed to start CPU %u (%lx)\n", id, target_cpu));
433 kmem_free(kernel_arena, (vm_offset_t)dpcpu[id - 1], DPCPU_SIZE);
434 dpcpu[id - 1] = NULL;
435 /* Notify the user that the CPU failed to start */
436 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
438 CPU_SET(id, &all_cpus);
444 /* Initialize and fire up non-boot processors */
449 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
451 CPU_SET(0, &all_cpus);
453 switch(cpu_enum_method) {
456 ofw_cpu_early_foreach(cpu_init_fdt, true);
464 /* Introduce rest of cores to the world */
466 cpu_mp_announce(void)
471 cpu_mp_setmaxid(void)
476 cores = ofw_cpu_early_foreach(NULL, false);
478 cores = MIN(cores, MAXCPU);
480 printf("Found %d CPUs in the device tree\n", cores);
482 mp_maxid = cores - 1;
483 cpu_enum_method = CPUS_FDT;
489 printf("No CPU data, limiting to 1 core\n");