2 * Copyright (c) 2015-2016 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
32 #include "opt_kstack_pages.h"
33 #include "opt_platform.h"
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/sched.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_kern.h>
55 #include <machine/debug_monitor.h>
56 #include <machine/machdep.h>
57 #include <machine/intr.h>
58 #include <machine/smp.h>
60 #include <machine/vfp.h>
64 #include <contrib/dev/acpica/include/acpi.h>
65 #include <dev/acpica/acpivar.h>
69 #include <dev/ofw/openfirm.h>
70 #include <dev/ofw/ofw_cpu.h>
73 #include <dev/psci/psci.h>
77 typedef void intr_ipi_send_t(void *, cpuset_t, u_int);
78 typedef void intr_ipi_handler_t(void *);
80 #define INTR_IPI_NAMELEN (MAXCOMLEN + 1)
82 intr_ipi_handler_t * ii_handler;
83 void * ii_handler_arg;
84 intr_ipi_send_t * ii_send;
86 char ii_name[INTR_IPI_NAMELEN];
90 static struct intr_ipi ipi_sources[INTR_IPI_COUNT];
92 static struct intr_ipi *intr_ipi_lookup(u_int);
93 static void intr_pic_ipi_setup(u_int, const char *, intr_ipi_handler_t *,
96 extern struct pcpu __pcpu[];
98 static device_identify_t arm64_cpu_identify;
99 static device_probe_t arm64_cpu_probe;
100 static device_attach_t arm64_cpu_attach;
102 static void ipi_ast(void *);
103 static void ipi_hardclock(void *);
104 static void ipi_preempt(void *);
105 static void ipi_rendezvous(void *);
106 static void ipi_stop(void *);
108 struct mtx ap_boot_mtx;
109 struct pcb stoppcbs[MAXCPU];
111 static device_t cpu_list[MAXCPU];
114 * Not all systems boot from the first CPU in the device tree. To work around
115 * this we need to find which CPU we have booted from so when we later
116 * enable the secondary CPUs we skip this one.
118 static int cpu0 = -1;
120 void mpentry(unsigned long cpuid);
121 void init_secondary(uint64_t);
123 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
125 /* Set to 1 once we're ready to let the APs out of the pen. */
126 volatile int aps_ready = 0;
128 /* Temporary variables for init_secondary() */
129 void *dpcpu[MAXCPU - 1];
131 static device_method_t arm64_cpu_methods[] = {
132 /* Device interface */
133 DEVMETHOD(device_identify, arm64_cpu_identify),
134 DEVMETHOD(device_probe, arm64_cpu_probe),
135 DEVMETHOD(device_attach, arm64_cpu_attach),
140 static devclass_t arm64_cpu_devclass;
141 static driver_t arm64_cpu_driver = {
147 DRIVER_MODULE(arm64_cpu, cpu, arm64_cpu_driver, arm64_cpu_devclass, 0, 0);
150 arm64_cpu_identify(driver_t *driver, device_t parent)
153 if (device_find_child(parent, "arm64_cpu", -1) != NULL)
155 if (BUS_ADD_CHILD(parent, 0, "arm64_cpu", -1) == NULL)
156 device_printf(parent, "add child failed\n");
160 arm64_cpu_probe(device_t dev)
164 cpuid = device_get_unit(dev);
165 if (cpuid >= MAXCPU || cpuid > mp_maxid)
173 arm64_cpu_attach(device_t dev)
180 cpuid = device_get_unit(dev);
182 if (cpuid >= MAXCPU || cpuid > mp_maxid)
184 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
186 reg = cpu_get_cpuid(dev, ®_size);
191 device_printf(dev, "register <");
192 for (i = 0; i < reg_size; i++)
193 printf("%s%x", (i == 0) ? "" : " ", reg[i]);
197 /* Set the device to start it later */
198 cpu_list[cpuid] = dev;
204 release_aps(void *dummy __unused)
208 /* Only release CPUs if they exist */
212 intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
213 intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
214 intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
215 intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
216 intr_pic_ipi_setup(IPI_STOP_HARD, "stop hard", ipi_stop, NULL);
217 intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
219 atomic_store_rel_int(&aps_ready, 1);
220 /* Wake up the other CPUs */
221 __asm __volatile("sev");
223 printf("Release APs\n");
225 for (i = 0; i < 2000; i++) {
231 printf("APs not started\n");
233 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
236 init_secondary(uint64_t cpu)
240 pcpup = &__pcpu[cpu];
242 * Set the pcpu pointer with a backup in tpidr_el1 to be
243 * loaded when entering the kernel from userland.
247 "msr tpidr_el1, %0" :: "r"(pcpup));
249 /* Spin until the BSP releases the APs */
251 __asm __volatile("wfe");
253 /* Initialize curthread */
254 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
255 pcpup->pc_curthread = pcpup->pc_idlethread;
256 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
259 * Identify current CPU. This is necessary to setup
260 * affinity registers and to provide support for
261 * runtime chip identification.
265 intr_pic_init_secondary();
267 /* Start per-CPU event timers. */
277 /* Enable interrupts */
280 mtx_lock_spin(&ap_boot_mtx);
282 atomic_add_rel_32(&smp_cpus, 1);
284 if (smp_cpus == mp_ncpus) {
285 /* enable IPI's, tlb shootdown, freezes etc */
286 atomic_store_rel_int(&smp_started, 1);
289 mtx_unlock_spin(&ap_boot_mtx);
291 /* Enter the scheduler */
294 panic("scheduler returned us to init_secondary");
299 * Send IPI thru interrupt controller.
302 pic_ipi_send(void *arg, cpuset_t cpus, u_int ipi)
305 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
306 PIC_IPI_SEND(intr_irq_root_dev, arg, cpus, ipi);
310 * Setup IPI handler on interrupt controller.
315 intr_pic_ipi_setup(u_int ipi, const char *name, intr_ipi_handler_t *hand,
318 struct intr_irqsrc *isrc;
322 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
323 KASSERT(hand != NULL, ("%s: ipi %u no handler", __func__, ipi));
325 error = PIC_IPI_SETUP(intr_irq_root_dev, ipi, &isrc);
329 isrc->isrc_handlers++;
331 ii = intr_ipi_lookup(ipi);
332 KASSERT(ii->ii_count == NULL, ("%s: ipi %u reused", __func__, ipi));
334 ii->ii_handler = hand;
335 ii->ii_handler_arg = arg;
336 ii->ii_send = pic_ipi_send;
337 ii->ii_send_arg = isrc;
338 strlcpy(ii->ii_name, name, INTR_IPI_NAMELEN);
339 ii->ii_count = intr_ipi_setup_counters(name);
343 intr_ipi_send(cpuset_t cpus, u_int ipi)
347 ii = intr_ipi_lookup(ipi);
348 if (ii->ii_count == NULL)
349 panic("%s: not setup IPI %u", __func__, ipi);
351 ii->ii_send(ii->ii_send_arg, cpus, ipi);
355 ipi_ast(void *dummy __unused)
358 CTR0(KTR_SMP, "IPI_AST");
362 ipi_hardclock(void *dummy __unused)
365 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
370 ipi_preempt(void *dummy __unused)
372 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
373 sched_preempt(curthread);
377 ipi_rendezvous(void *dummy __unused)
380 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
381 smp_rendezvous_action();
385 ipi_stop(void *dummy __unused)
389 CTR0(KTR_SMP, "IPI_STOP");
391 cpu = PCPU_GET(cpuid);
392 savectx(&stoppcbs[cpu]);
394 /* Indicate we are stopped */
395 CPU_SET_ATOMIC(cpu, &stopped_cpus);
397 /* Wait for restart */
398 while (!CPU_ISSET(cpu, &started_cpus))
401 CPU_CLR_ATOMIC(cpu, &started_cpus);
402 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
403 CTR0(KTR_SMP, "IPI_STOP (restart)");
410 return (smp_topo_none());
413 /* Determine if we running MP machine */
418 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
423 start_cpu(u_int id, uint64_t target_cpu)
430 /* Check we are able to start this cpu */
434 KASSERT(id < MAXCPU, ("Too many CPUs"));
436 /* We are already running on cpu 0 */
441 * Rotate the CPU IDs to put the boot CPU as CPU 0. We keep the other
442 * CPUs ordered as the are likely grouped into clusters so it can be
443 * useful to keep that property, e.g. for the GICv3 driver to send
444 * an IPI to all CPUs in the cluster.
448 cpuid += mp_maxid + 1;
451 pcpup = &__pcpu[cpuid];
452 pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
454 dpcpu[cpuid - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
456 dpcpu_init(dpcpu[cpuid - 1], cpuid);
458 printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
459 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
461 err = psci_cpu_on(target_cpu, pa, cpuid);
462 if (err != PSCI_RETVAL_SUCCESS) {
464 * Panic here if INVARIANTS are enabled and PSCI failed to
465 * start the requested CPU. If psci_cpu_on returns PSCI_MISSING
466 * to indicate we are unable to use it to start the given CPU.
468 KASSERT(err == PSCI_MISSING,
469 ("Failed to start CPU %u (%lx)\n", id, target_cpu));
472 kmem_free(kernel_arena, (vm_offset_t)dpcpu[cpuid - 1],
474 dpcpu[cpuid - 1] = NULL;
475 /* Notify the user that the CPU failed to start */
476 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
478 CPU_SET(cpuid, &all_cpus);
485 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
487 ACPI_MADT_GENERIC_INTERRUPT *intr;
490 switch(entry->Type) {
491 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
492 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
495 start_cpu((*cpuid), intr->ArmMpidr);
506 ACPI_TABLE_MADT *madt;
510 physaddr = acpi_find_table(ACPI_SIG_MADT);
514 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
516 printf("Unable to map the MADT, not starting APs\n");
521 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
522 madt_handler, &cpuid);
524 acpi_unmap_table(madt);
530 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
535 if (addr_size == 2) {
537 target_cpu |= reg[1];
540 return (start_cpu(id, target_cpu) ? TRUE : FALSE);
544 /* Initialize and fire up non-boot processors */
549 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
551 CPU_SET(0, &all_cpus);
553 switch(arm64_bus_method) {
556 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
562 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
563 ofw_cpu_early_foreach(cpu_init_fdt, true);
571 /* Introduce rest of cores to the world */
573 cpu_mp_announce(void)
579 cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
581 ACPI_MADT_GENERIC_INTERRUPT *intr;
585 switch(entry->Type) {
586 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
587 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
589 mpidr_reg = READ_SPECIALREG(mpidr_el1);
590 if ((mpidr_reg & 0xff00fffffful) == intr->ArmMpidr)
603 ACPI_TABLE_MADT *madt;
607 physaddr = acpi_find_table(ACPI_SIG_MADT);
611 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
613 printf("Unable to map the MADT, not starting APs\n");
618 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
619 cpu_count_acpi_handler, &cores);
621 acpi_unmap_table(madt);
629 cpu_find_cpu0_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
631 uint64_t mpidr_fdt, mpidr_reg;
635 if (addr_size == 2) {
640 mpidr_reg = READ_SPECIALREG(mpidr_el1);
642 if ((mpidr_reg & 0xff00fffffful) == mpidr_fdt)
651 cpu_mp_setmaxid(void)
653 #if defined(DEV_ACPI) || defined(FDT)
657 switch(arm64_bus_method) {
660 cores = cpu_count_acpi();
662 cores = MIN(cores, MAXCPU);
664 printf("Found %d CPUs in the ACPI tables\n",
667 mp_maxid = cores - 1;
674 cores = ofw_cpu_early_foreach(cpu_find_cpu0_fdt, false);
676 cores = MIN(cores, MAXCPU);
678 printf("Found %d CPUs in the device tree\n",
681 mp_maxid = cores - 1;
691 printf("No CPU data, limiting to 1 core\n");
699 static struct intr_ipi *
700 intr_ipi_lookup(u_int ipi)
703 if (ipi >= INTR_IPI_COUNT)
704 panic("%s: no such IPI %u", __func__, ipi);
706 return (&ipi_sources[ipi]);
710 * interrupt controller dispatch function for IPIs. It should
711 * be called straight from the interrupt controller, when associated
712 * interrupt source is learned. Or from anybody who has an interrupt
716 intr_ipi_dispatch(u_int ipi, struct trapframe *tf)
721 ii = intr_ipi_lookup(ipi);
722 if (ii->ii_count == NULL)
723 panic("%s: not setup IPI %u", __func__, ipi);
725 intr_ipi_increment_count(ii->ii_count, PCPU_GET(cpuid));
728 * Supply ipi filter with trapframe argument
729 * if none is registered.
731 arg = ii->ii_handler_arg != NULL ? ii->ii_handler_arg : tf;
737 * Map IPI into interrupt controller.
742 ipi_map(struct intr_irqsrc *isrc, u_int ipi)
747 if (ipi >= INTR_IPI_COUNT)
748 panic("%s: no such IPI %u", __func__, ipi);
750 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
752 isrc->isrc_type = INTR_ISRCT_NAMESPACE;
753 isrc->isrc_nspc_type = INTR_IRQ_NSPC_IPI;
754 isrc->isrc_nspc_num = ipi_next_num;
756 error = PIC_REGISTER(intr_irq_root_dev, isrc, &is_percpu);
758 isrc->isrc_dev = intr_irq_root_dev;
765 * Setup IPI handler to interrupt source.
767 * Note that there could be more ways how to send and receive IPIs
768 * on a platform like fast interrupts for example. In that case,
769 * one can call this function with ASIF_NOALLOC flag set and then
770 * call intr_ipi_dispatch() when appropriate.
775 intr_ipi_set_handler(u_int ipi, const char *name, intr_ipi_filter_t *filter,
776 void *arg, u_int flags)
778 struct intr_irqsrc *isrc;
784 isrc = intr_ipi_lookup(ipi);
785 if (isrc->isrc_ipifilter != NULL)
788 if ((flags & AISHF_NOALLOC) == 0) {
789 error = ipi_map(isrc, ipi);
794 isrc->isrc_ipifilter = filter;
795 isrc->isrc_arg = arg;
796 isrc->isrc_handlers = 1;
797 isrc->isrc_count = intr_ipi_setup_counters(name);
798 isrc->isrc_index = 0; /* it should not be used in IPI case */
800 if (isrc->isrc_dev != NULL) {
801 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
802 PIC_ENABLE_SOURCE(isrc->isrc_dev, isrc);
810 ipi_all_but_self(u_int ipi)
815 CPU_CLR(PCPU_GET(cpuid), &cpus);
816 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
817 intr_ipi_send(cpus, ipi);
821 ipi_cpu(int cpu, u_int ipi)
828 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
829 intr_ipi_send(cpus, ipi);
833 ipi_selected(cpuset_t cpus, u_int ipi)
836 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
837 intr_ipi_send(cpus, ipi);