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)
144 arm64_cpu_attach(device_t dev)
151 cpuid = device_get_unit(dev);
153 if (cpuid >= MAXCPU || cpuid > mp_maxid)
155 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
157 reg = cpu_get_cpuid(dev, ®_size);
161 device_printf(dev, "Found register:");
162 for (i = 0; i < reg_size; i++)
163 printf(" %x", reg[i]);
166 /* Set the device to start it later */
167 cpu_list[cpuid] = dev;
173 release_aps(void *dummy __unused)
177 /* Setup the IPI handler */
178 for (i = 0; i < COUNT_IPI; i++)
179 arm_setup_ipihandler(ipi_handler, i);
181 atomic_store_rel_int(&aps_ready, 1);
182 /* Wake up the other CPUs */
183 __asm __volatile("sev");
185 printf("Release APs\n");
187 for (i = 0; i < 2000; i++) {
193 printf("AP's not started\n");
195 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
198 init_secondary(uint64_t cpu)
203 pcpup = &__pcpu[cpu];
205 * Set the pcpu pointer with a backup in tpidr_el1 to be
206 * loaded when entering the kernel from userland.
210 "msr tpidr_el1, %0" :: "r"(pcpup));
212 /* Spin until the BSP releases the APs */
214 __asm __volatile("wfe");
216 /* Initialize curthread */
217 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
218 pcpup->pc_curthread = pcpup->pc_idlethread;
219 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
222 * Identify current CPU. This is necessary to setup
223 * affinity registers and to provide support for
224 * runtime chip identification.
228 /* Configure the interrupt controller */
229 arm_init_secondary();
231 for (i = 0; i < COUNT_IPI; i++)
234 /* Start per-CPU event timers. */
241 /* Enable interrupts */
244 mtx_lock_spin(&ap_boot_mtx);
246 atomic_add_rel_32(&smp_cpus, 1);
248 if (smp_cpus == mp_ncpus) {
249 /* enable IPI's, tlb shootdown, freezes etc */
250 atomic_store_rel_int(&smp_started, 1);
253 mtx_unlock_spin(&ap_boot_mtx);
255 /* Enter the scheduler */
258 panic("scheduler returned us to init_secondary");
263 ipi_handler(void *arg)
267 arg = (void *)((uintptr_t)arg & ~(1 << 16));
268 KASSERT((uintptr_t)arg < COUNT_IPI,
269 ("Invalid IPI %ju", (uintptr_t)arg));
271 cpu = PCPU_GET(cpuid);
272 ipi = (uintptr_t)arg;
276 CTR0(KTR_SMP, "IPI_AST");
279 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
280 sched_preempt(curthread);
283 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
284 smp_rendezvous_action();
288 CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
289 savectx(&stoppcbs[cpu]);
291 /* Indicate we are stopped */
292 CPU_SET_ATOMIC(cpu, &stopped_cpus);
294 /* Wait for restart */
295 while (!CPU_ISSET(cpu, &started_cpus))
298 CPU_CLR_ATOMIC(cpu, &started_cpus);
299 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
300 CTR0(KTR_SMP, "IPI_STOP (restart)");
303 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
307 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
310 return (FILTER_HANDLED);
317 return (smp_topo_none());
320 /* Determine if we running MP machine */
325 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
331 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
338 /* Check we are able to start this cpu */
342 KASSERT(id < MAXCPU, ("Too mant CPUs"));
344 KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
346 cpu_reg[id][0] = reg[0];
348 cpu_reg[id][1] = reg[1];
351 /* We are already running on cpu 0 */
355 CPU_SET(id, &all_cpus);
358 pcpu_init(pcpup, id, sizeof(struct pcpu));
360 dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
362 dpcpu_init(dpcpu[id - 1], id);
365 if (addr_size == 2) {
367 target_cpu |= reg[1];
370 printf("Starting CPU %u (%lx)\n", id, target_cpu);
371 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
373 err = psci_cpu_on(target_cpu, pa, id);
374 if (err != PSCI_RETVAL_SUCCESS)
375 printf("Failed to start CPU %u\n", id);
381 /* Initialize and fire up non-boot processors */
386 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
388 CPU_SET(0, &all_cpus);
390 switch(cpu_enum_method) {
393 ofw_cpu_early_foreach(cpu_init_fdt, true);
401 /* Introduce rest of cores to the world */
403 cpu_mp_announce(void)
408 cpu_mp_setmaxid(void)
413 cores = ofw_cpu_early_foreach(NULL, false);
415 cores = MIN(cores, MAXCPU);
417 printf("Found %d CPUs in the device tree\n", cores);
419 mp_maxid = cores - 1;
420 cpu_enum_method = CPUS_FDT;
426 printf("No CPU data, limiting to 1 core\n");