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/machdep.h>
56 #include <machine/intr.h>
57 #include <machine/smp.h>
59 #include <machine/vfp.h>
63 #include <contrib/dev/acpica/include/acpi.h>
64 #include <dev/acpica/acpivar.h>
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofw_bus.h>
70 #include <dev/ofw/ofw_bus_subr.h>
71 #include <dev/ofw/ofw_cpu.h>
74 #include <dev/psci/psci.h>
78 #define MP_QUIRK_CPULIST 0x01 /* The list of cpus may be wrong, */
79 /* don't panic if one fails to start */
80 static uint32_t mp_quirks;
87 { "arm,foundation-aarch64", MP_QUIRK_CPULIST },
88 { "arm,fvp-base", MP_QUIRK_CPULIST },
89 /* This is incorrect in some DTS files */
90 { "arm,vfp-base", MP_QUIRK_CPULIST },
95 typedef void intr_ipi_send_t(void *, cpuset_t, u_int);
96 typedef void intr_ipi_handler_t(void *);
98 #define INTR_IPI_NAMELEN (MAXCOMLEN + 1)
100 intr_ipi_handler_t * ii_handler;
101 void * ii_handler_arg;
102 intr_ipi_send_t * ii_send;
104 char ii_name[INTR_IPI_NAMELEN];
108 static struct intr_ipi ipi_sources[INTR_IPI_COUNT];
110 static struct intr_ipi *intr_ipi_lookup(u_int);
111 static void intr_pic_ipi_setup(u_int, const char *, intr_ipi_handler_t *,
114 extern struct pcpu __pcpu[];
116 static device_identify_t arm64_cpu_identify;
117 static device_probe_t arm64_cpu_probe;
118 static device_attach_t arm64_cpu_attach;
120 static void ipi_ast(void *);
121 static void ipi_hardclock(void *);
122 static void ipi_preempt(void *);
123 static void ipi_rendezvous(void *);
124 static void ipi_stop(void *);
126 struct mtx ap_boot_mtx;
127 struct pcb stoppcbs[MAXCPU];
129 static device_t cpu_list[MAXCPU];
132 * Not all systems boot from the first CPU in the device tree. To work around
133 * this we need to find which CPU we have booted from so when we later
134 * enable the secondary CPUs we skip this one.
136 static int cpu0 = -1;
138 void mpentry(unsigned long cpuid);
139 void init_secondary(uint64_t);
141 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
143 /* Set to 1 once we're ready to let the APs out of the pen. */
144 volatile int aps_ready = 0;
146 /* Temporary variables for init_secondary() */
147 void *dpcpu[MAXCPU - 1];
149 static device_method_t arm64_cpu_methods[] = {
150 /* Device interface */
151 DEVMETHOD(device_identify, arm64_cpu_identify),
152 DEVMETHOD(device_probe, arm64_cpu_probe),
153 DEVMETHOD(device_attach, arm64_cpu_attach),
158 static devclass_t arm64_cpu_devclass;
159 static driver_t arm64_cpu_driver = {
165 DRIVER_MODULE(arm64_cpu, cpu, arm64_cpu_driver, arm64_cpu_devclass, 0, 0);
168 arm64_cpu_identify(driver_t *driver, device_t parent)
171 if (device_find_child(parent, "arm64_cpu", -1) != NULL)
173 if (BUS_ADD_CHILD(parent, 0, "arm64_cpu", -1) == NULL)
174 device_printf(parent, "add child failed\n");
178 arm64_cpu_probe(device_t dev)
182 cpuid = device_get_unit(dev);
183 if (cpuid >= MAXCPU || cpuid > mp_maxid)
191 arm64_cpu_attach(device_t dev)
198 cpuid = device_get_unit(dev);
200 if (cpuid >= MAXCPU || cpuid > mp_maxid)
202 KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
204 reg = cpu_get_cpuid(dev, ®_size);
209 device_printf(dev, "register <");
210 for (i = 0; i < reg_size; i++)
211 printf("%s%x", (i == 0) ? "" : " ", reg[i]);
215 /* Set the device to start it later */
216 cpu_list[cpuid] = dev;
222 release_aps(void *dummy __unused)
226 /* Only release CPUs if they exist */
230 intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
231 intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
232 intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
233 intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
234 intr_pic_ipi_setup(IPI_STOP_HARD, "stop hard", ipi_stop, NULL);
235 intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
237 atomic_store_rel_int(&aps_ready, 1);
238 /* Wake up the other CPUs */
239 __asm __volatile("sev");
241 printf("Release APs\n");
243 for (i = 0; i < 2000; i++) {
249 printf("APs not started\n");
251 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
254 init_secondary(uint64_t cpu)
258 pcpup = &__pcpu[cpu];
260 * Set the pcpu pointer with a backup in tpidr_el1 to be
261 * loaded when entering the kernel from userland.
265 "msr tpidr_el1, %0" :: "r"(pcpup));
267 /* Spin until the BSP releases the APs */
269 __asm __volatile("wfe");
271 /* Initialize curthread */
272 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
273 pcpup->pc_curthread = pcpup->pc_idlethread;
274 pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
277 * Identify current CPU. This is necessary to setup
278 * affinity registers and to provide support for
279 * runtime chip identification.
283 intr_pic_init_secondary();
285 /* Start per-CPU event timers. */
295 /* Enable interrupts */
298 mtx_lock_spin(&ap_boot_mtx);
300 atomic_add_rel_32(&smp_cpus, 1);
302 if (smp_cpus == mp_ncpus) {
303 /* enable IPI's, tlb shootdown, freezes etc */
304 atomic_store_rel_int(&smp_started, 1);
307 mtx_unlock_spin(&ap_boot_mtx);
309 /* Enter the scheduler */
312 panic("scheduler returned us to init_secondary");
317 * Send IPI thru interrupt controller.
320 pic_ipi_send(void *arg, cpuset_t cpus, u_int ipi)
323 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
324 PIC_IPI_SEND(intr_irq_root_dev, arg, cpus, ipi);
328 * Setup IPI handler on interrupt controller.
333 intr_pic_ipi_setup(u_int ipi, const char *name, intr_ipi_handler_t *hand,
336 struct intr_irqsrc *isrc;
340 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
341 KASSERT(hand != NULL, ("%s: ipi %u no handler", __func__, ipi));
343 error = PIC_IPI_SETUP(intr_irq_root_dev, ipi, &isrc);
347 isrc->isrc_handlers++;
349 ii = intr_ipi_lookup(ipi);
350 KASSERT(ii->ii_count == NULL, ("%s: ipi %u reused", __func__, ipi));
352 ii->ii_handler = hand;
353 ii->ii_handler_arg = arg;
354 ii->ii_send = pic_ipi_send;
355 ii->ii_send_arg = isrc;
356 strlcpy(ii->ii_name, name, INTR_IPI_NAMELEN);
357 ii->ii_count = intr_ipi_setup_counters(name);
361 intr_ipi_send(cpuset_t cpus, u_int ipi)
365 ii = intr_ipi_lookup(ipi);
366 if (ii->ii_count == NULL)
367 panic("%s: not setup IPI %u", __func__, ipi);
369 ii->ii_send(ii->ii_send_arg, cpus, ipi);
373 ipi_ast(void *dummy __unused)
376 CTR0(KTR_SMP, "IPI_AST");
380 ipi_hardclock(void *dummy __unused)
383 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
388 ipi_preempt(void *dummy __unused)
390 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
391 sched_preempt(curthread);
395 ipi_rendezvous(void *dummy __unused)
398 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
399 smp_rendezvous_action();
403 ipi_stop(void *dummy __unused)
407 CTR0(KTR_SMP, "IPI_STOP");
409 cpu = PCPU_GET(cpuid);
410 savectx(&stoppcbs[cpu]);
412 /* Indicate we are stopped */
413 CPU_SET_ATOMIC(cpu, &stopped_cpus);
415 /* Wait for restart */
416 while (!CPU_ISSET(cpu, &started_cpus))
419 CPU_CLR_ATOMIC(cpu, &started_cpus);
420 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
421 CTR0(KTR_SMP, "IPI_STOP (restart)");
428 return (smp_topo_none());
431 /* Determine if we running MP machine */
436 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
441 start_cpu(u_int id, uint64_t target_cpu)
448 /* Check we are able to start this cpu */
452 KASSERT(id < MAXCPU, ("Too many CPUs"));
454 /* We are already running on cpu 0 */
459 * Rotate the CPU IDs to put the boot CPU as CPU 0. We keep the other
460 * CPUs ordered as the are likely grouped into clusters so it can be
461 * useful to keep that property, e.g. for the GICv3 driver to send
462 * an IPI to all CPUs in the cluster.
466 cpuid += mp_maxid + 1;
469 pcpup = &__pcpu[cpuid];
470 pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
472 dpcpu[cpuid - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
474 dpcpu_init(dpcpu[cpuid - 1], cpuid);
476 printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
477 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
479 err = psci_cpu_on(target_cpu, pa, cpuid);
480 if (err != PSCI_RETVAL_SUCCESS) {
482 * Panic here if INVARIANTS are enabled and PSCI failed to
483 * start the requested CPU. If psci_cpu_on returns PSCI_MISSING
484 * to indicate we are unable to use it to start the given CPU.
486 KASSERT(err == PSCI_MISSING ||
487 (mp_quirks & MP_QUIRK_CPULIST) == MP_QUIRK_CPULIST,
488 ("Failed to start CPU %u (%lx)\n", id, target_cpu));
491 kmem_free(kernel_arena, (vm_offset_t)dpcpu[cpuid - 1],
493 dpcpu[cpuid - 1] = NULL;
496 /* Notify the user that the CPU failed to start */
497 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
499 CPU_SET(cpuid, &all_cpus);
506 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
508 ACPI_MADT_GENERIC_INTERRUPT *intr;
511 switch(entry->Type) {
512 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
513 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
516 start_cpu((*cpuid), intr->ArmMpidr);
527 ACPI_TABLE_MADT *madt;
531 physaddr = acpi_find_table(ACPI_SIG_MADT);
535 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
537 printf("Unable to map the MADT, not starting APs\n");
542 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
543 madt_handler, &cpuid);
545 acpi_unmap_table(madt);
551 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
557 if (addr_size == 2) {
559 target_cpu |= reg[1];
562 if (!start_cpu(id, target_cpu))
565 /* Try to read the numa node of this cpu */
566 if (OF_getencprop(node, "numa-node-id", &domain, sizeof(domain)) > 0) {
567 __pcpu[id].pc_domain = domain;
568 if (domain < MAXMEMDOM)
569 CPU_SET(id, &cpuset_domain[domain]);
576 /* Initialize and fire up non-boot processors */
585 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
587 CPU_SET(0, &all_cpus);
589 switch(arm64_bus_method) {
592 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
599 for (i = 0; fdt_quirks[i].compat != NULL; i++) {
600 if (ofw_bus_node_is_compatible(node,
601 fdt_quirks[i].compat) != 0) {
602 mp_quirks = fdt_quirks[i].quirks;
605 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
606 ofw_cpu_early_foreach(cpu_init_fdt, true);
614 /* Introduce rest of cores to the world */
616 cpu_mp_announce(void)
622 cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
624 ACPI_MADT_GENERIC_INTERRUPT *intr;
628 switch(entry->Type) {
629 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
630 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
632 mpidr_reg = READ_SPECIALREG(mpidr_el1);
633 if ((mpidr_reg & 0xff00fffffful) == intr->ArmMpidr)
646 ACPI_TABLE_MADT *madt;
650 physaddr = acpi_find_table(ACPI_SIG_MADT);
654 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
656 printf("Unable to map the MADT, not starting APs\n");
661 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
662 cpu_count_acpi_handler, &cores);
664 acpi_unmap_table(madt);
672 cpu_find_cpu0_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
674 uint64_t mpidr_fdt, mpidr_reg;
678 if (addr_size == 2) {
683 mpidr_reg = READ_SPECIALREG(mpidr_el1);
685 if ((mpidr_reg & 0xff00fffffful) == mpidr_fdt)
694 cpu_mp_setmaxid(void)
696 #if defined(DEV_ACPI) || defined(FDT)
700 switch(arm64_bus_method) {
703 cores = cpu_count_acpi();
705 cores = MIN(cores, MAXCPU);
707 printf("Found %d CPUs in the ACPI tables\n",
710 mp_maxid = cores - 1;
717 cores = ofw_cpu_early_foreach(cpu_find_cpu0_fdt, false);
719 cores = MIN(cores, MAXCPU);
721 printf("Found %d CPUs in the device tree\n",
724 mp_maxid = cores - 1;
734 printf("No CPU data, limiting to 1 core\n");
742 static struct intr_ipi *
743 intr_ipi_lookup(u_int ipi)
746 if (ipi >= INTR_IPI_COUNT)
747 panic("%s: no such IPI %u", __func__, ipi);
749 return (&ipi_sources[ipi]);
753 * interrupt controller dispatch function for IPIs. It should
754 * be called straight from the interrupt controller, when associated
755 * interrupt source is learned. Or from anybody who has an interrupt
759 intr_ipi_dispatch(u_int ipi, struct trapframe *tf)
764 ii = intr_ipi_lookup(ipi);
765 if (ii->ii_count == NULL)
766 panic("%s: not setup IPI %u", __func__, ipi);
768 intr_ipi_increment_count(ii->ii_count, PCPU_GET(cpuid));
771 * Supply ipi filter with trapframe argument
772 * if none is registered.
774 arg = ii->ii_handler_arg != NULL ? ii->ii_handler_arg : tf;
780 * Map IPI into interrupt controller.
785 ipi_map(struct intr_irqsrc *isrc, u_int ipi)
790 if (ipi >= INTR_IPI_COUNT)
791 panic("%s: no such IPI %u", __func__, ipi);
793 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
795 isrc->isrc_type = INTR_ISRCT_NAMESPACE;
796 isrc->isrc_nspc_type = INTR_IRQ_NSPC_IPI;
797 isrc->isrc_nspc_num = ipi_next_num;
799 error = PIC_REGISTER(intr_irq_root_dev, isrc, &is_percpu);
801 isrc->isrc_dev = intr_irq_root_dev;
808 * Setup IPI handler to interrupt source.
810 * Note that there could be more ways how to send and receive IPIs
811 * on a platform like fast interrupts for example. In that case,
812 * one can call this function with ASIF_NOALLOC flag set and then
813 * call intr_ipi_dispatch() when appropriate.
818 intr_ipi_set_handler(u_int ipi, const char *name, intr_ipi_filter_t *filter,
819 void *arg, u_int flags)
821 struct intr_irqsrc *isrc;
827 isrc = intr_ipi_lookup(ipi);
828 if (isrc->isrc_ipifilter != NULL)
831 if ((flags & AISHF_NOALLOC) == 0) {
832 error = ipi_map(isrc, ipi);
837 isrc->isrc_ipifilter = filter;
838 isrc->isrc_arg = arg;
839 isrc->isrc_handlers = 1;
840 isrc->isrc_count = intr_ipi_setup_counters(name);
841 isrc->isrc_index = 0; /* it should not be used in IPI case */
843 if (isrc->isrc_dev != NULL) {
844 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
845 PIC_ENABLE_SOURCE(isrc->isrc_dev, isrc);
853 ipi_all_but_self(u_int ipi)
858 CPU_CLR(PCPU_GET(cpuid), &cpus);
859 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
860 intr_ipi_send(cpus, ipi);
864 ipi_cpu(int cpu, u_int ipi)
871 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
872 intr_ipi_send(cpus, ipi);
876 ipi_selected(cpuset_t cpus, u_int ipi)
879 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
880 intr_ipi_send(cpus, ipi);