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
33 #include "opt_kstack_pages.h"
34 #include "opt_platform.h"
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
51 #include <sys/sched.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_map.h>
60 #include <machine/machdep.h>
61 #include <machine/debug_monitor.h>
62 #include <machine/intr.h>
63 #include <machine/smp.h>
65 #include <machine/vfp.h>
69 #include <contrib/dev/acpica/include/acpi.h>
70 #include <dev/acpica/acpivar.h>
74 #include <dev/ofw/openfirm.h>
75 #include <dev/ofw/ofw_bus.h>
76 #include <dev/ofw/ofw_bus_subr.h>
77 #include <dev/ofw/ofw_cpu.h>
80 #include <dev/psci/psci.h>
84 #define MP_QUIRK_CPULIST 0x01 /* The list of cpus may be wrong, */
85 /* don't panic if one fails to start */
86 static uint32_t mp_quirks;
93 { "arm,foundation-aarch64", MP_QUIRK_CPULIST },
94 { "arm,fvp-base", MP_QUIRK_CPULIST },
95 /* This is incorrect in some DTS files */
96 { "arm,vfp-base", MP_QUIRK_CPULIST },
101 typedef void intr_ipi_send_t(void *, cpuset_t, u_int);
102 typedef void intr_ipi_handler_t(void *);
104 #define INTR_IPI_NAMELEN (MAXCOMLEN + 1)
106 intr_ipi_handler_t * ii_handler;
107 void * ii_handler_arg;
108 intr_ipi_send_t * ii_send;
110 char ii_name[INTR_IPI_NAMELEN];
114 static struct intr_ipi ipi_sources[INTR_IPI_COUNT];
116 static struct intr_ipi *intr_ipi_lookup(u_int);
117 static void intr_pic_ipi_setup(u_int, const char *, intr_ipi_handler_t *,
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 pcb stoppcbs[MAXCPU];
129 * Not all systems boot from the first CPU in the device tree. To work around
130 * this we need to find which CPU we have booted from so when we later
131 * enable the secondary CPUs we skip this one.
133 static int cpu0 = -1;
135 void mpentry(unsigned long cpuid);
136 void init_secondary(uint64_t);
138 /* Synchronize AP startup. */
139 static struct mtx ap_boot_mtx;
141 /* Stacks for AP initialization, discarded once idle threads are started. */
143 static void *bootstacks[MAXCPU];
145 /* Count of started APs, used to synchronize access to bootstack. */
146 static volatile int aps_started;
148 /* Set to 1 once we're ready to let the APs out of the pen. */
149 static volatile int aps_ready;
151 /* Temporary variables for init_secondary() */
152 void *dpcpu[MAXCPU - 1];
155 release_aps(void *dummy __unused)
159 /* Only release CPUs if they exist */
163 intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
164 intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
165 intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
166 intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
167 intr_pic_ipi_setup(IPI_STOP_HARD, "stop hard", ipi_stop, NULL);
168 intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
170 atomic_store_rel_int(&aps_ready, 1);
171 /* Wake up the other CPUs */
177 printf("Release APs...");
180 for (i = 0; i < 2000; i++) {
186 * Don't time out while we are making progress. Some large
187 * systems can take a while to start all CPUs.
189 if (smp_cpus > started) {
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));
216 * Identify current CPU. This is necessary to setup
217 * affinity registers and to provide support for
218 * runtime chip identification.
220 * We need this before signalling the CPU is ready to
221 * let the boot CPU use the results.
225 /* Ensure the stores in identify_cpu have completed */
226 atomic_thread_fence_acq_rel();
228 /* Signal the BSP and spin until it has released all APs. */
229 atomic_add_int(&aps_started, 1);
230 while (!atomic_load_int(&aps_ready))
231 __asm __volatile("wfe");
233 pcpup->pc_midr = get_midr();
235 /* Initialize curthread */
236 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
237 pcpup->pc_curthread = pcpup->pc_idlethread;
239 /* Initialize curpmap to match TTBR0's current setting. */
240 pmap0 = vmspace_pmap(&vmspace0);
241 KASSERT(pmap_to_ttbr0(pmap0) == READ_SPECIALREG(ttbr0_el1),
242 ("pmap0 doesn't match cpu %ld's ttbr0", cpu));
243 pcpup->pc_curpmap = pmap0;
245 install_cpu_errata();
247 intr_pic_init_secondary();
249 /* Start per-CPU event timers. */
259 mtx_lock_spin(&ap_boot_mtx);
260 atomic_add_rel_32(&smp_cpus, 1);
261 if (smp_cpus == mp_ncpus) {
262 /* enable IPI's, tlb shootdown, freezes etc */
263 atomic_store_rel_int(&smp_started, 1);
265 mtx_unlock_spin(&ap_boot_mtx);
270 * Assert that smp_after_idle_runnable condition is reasonable.
272 MPASS(PCPU_GET(curpcb) == NULL);
274 /* Enter the scheduler */
277 panic("scheduler returned us to init_secondary");
282 smp_after_idle_runnable(void *arg __unused)
287 for (cpu = 1; cpu < mp_ncpus; cpu++) {
288 if (bootstacks[cpu] != NULL) {
290 while (atomic_load_ptr(&pc->pc_curpcb) == NULL)
292 kmem_free((vm_offset_t)bootstacks[cpu], PAGE_SIZE);
296 SYSINIT(smp_after_idle_runnable, SI_SUB_SMP, SI_ORDER_ANY,
297 smp_after_idle_runnable, NULL);
300 * Send IPI thru interrupt controller.
303 pic_ipi_send(void *arg, cpuset_t cpus, u_int ipi)
306 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
309 * Ensure that this CPU's stores will be visible to IPI
310 * recipients before starting to send the interrupts.
314 PIC_IPI_SEND(intr_irq_root_dev, arg, cpus, ipi);
318 * Setup IPI handler on interrupt controller.
323 intr_pic_ipi_setup(u_int ipi, const char *name, intr_ipi_handler_t *hand,
326 struct intr_irqsrc *isrc;
330 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
331 KASSERT(hand != NULL, ("%s: ipi %u no handler", __func__, ipi));
333 error = PIC_IPI_SETUP(intr_irq_root_dev, ipi, &isrc);
337 isrc->isrc_handlers++;
339 ii = intr_ipi_lookup(ipi);
340 KASSERT(ii->ii_count == NULL, ("%s: ipi %u reused", __func__, ipi));
342 ii->ii_handler = hand;
343 ii->ii_handler_arg = arg;
344 ii->ii_send = pic_ipi_send;
345 ii->ii_send_arg = isrc;
346 strlcpy(ii->ii_name, name, INTR_IPI_NAMELEN);
347 ii->ii_count = intr_ipi_setup_counters(name);
351 intr_ipi_send(cpuset_t cpus, u_int ipi)
355 ii = intr_ipi_lookup(ipi);
356 if (ii->ii_count == NULL)
357 panic("%s: not setup IPI %u", __func__, ipi);
359 ii->ii_send(ii->ii_send_arg, cpus, ipi);
363 ipi_ast(void *dummy __unused)
366 CTR0(KTR_SMP, "IPI_AST");
370 ipi_hardclock(void *dummy __unused)
373 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
378 ipi_preempt(void *dummy __unused)
380 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
381 sched_preempt(curthread);
385 ipi_rendezvous(void *dummy __unused)
388 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
389 smp_rendezvous_action();
393 ipi_stop(void *dummy __unused)
397 CTR0(KTR_SMP, "IPI_STOP");
399 cpu = PCPU_GET(cpuid);
400 savectx(&stoppcbs[cpu]);
402 /* Indicate we are stopped */
403 CPU_SET_ATOMIC(cpu, &stopped_cpus);
405 /* Wait for restart */
406 while (!CPU_ISSET(cpu, &started_cpus))
410 dbg_register_sync(NULL);
413 CPU_CLR_ATOMIC(cpu, &started_cpus);
414 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
415 CTR0(KTR_SMP, "IPI_STOP (restart)");
422 return (smp_topo_none());
425 /* Determine if we running MP machine */
430 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
435 start_cpu(u_int id, uint64_t target_cpu)
442 /* Check we are able to start this cpu */
446 KASSERT(id < MAXCPU, ("Too many CPUs"));
448 /* We are already running on cpu 0 */
453 * Rotate the CPU IDs to put the boot CPU as CPU 0. We keep the other
454 * CPUs ordered as they are likely grouped into clusters so it can be
455 * useful to keep that property, e.g. for the GICv3 driver to send
456 * an IPI to all CPUs in the cluster.
460 cpuid += mp_maxid + 1;
463 pcpup = &__pcpu[cpuid];
464 pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
466 dpcpu[cpuid - 1] = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
467 dpcpu_init(dpcpu[cpuid - 1], cpuid);
469 bootstacks[cpuid] = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
471 naps = atomic_load_int(&aps_started);
472 bootstack = (char *)bootstacks[cpuid] + PAGE_SIZE;
474 printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
475 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
476 err = psci_cpu_on(target_cpu, pa, cpuid);
477 if (err != PSCI_RETVAL_SUCCESS) {
479 * Panic here if INVARIANTS are enabled and PSCI failed to
480 * start the requested CPU. psci_cpu_on() returns PSCI_MISSING
481 * to indicate we are unable to use it to start the given CPU.
483 KASSERT(err == PSCI_MISSING ||
484 (mp_quirks & MP_QUIRK_CPULIST) == MP_QUIRK_CPULIST,
485 ("Failed to start CPU %u (%lx), error %d\n",
486 id, target_cpu, err));
489 kmem_free((vm_offset_t)dpcpu[cpuid - 1], DPCPU_SIZE);
490 dpcpu[cpuid - 1] = NULL;
491 kmem_free((vm_offset_t)bootstacks[cpuid], PAGE_SIZE);
492 bootstacks[cpuid] = NULL;
495 /* Notify the user that the CPU failed to start */
496 printf("Failed to start CPU %u (%lx), error %d\n",
497 id, target_cpu, err);
499 /* Wait for the AP to switch to its boot stack. */
500 while (atomic_load_int(&aps_started) < naps + 1)
502 CPU_SET(cpuid, &all_cpus);
510 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
512 ACPI_MADT_GENERIC_INTERRUPT *intr;
516 switch(entry->Type) {
517 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
518 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
521 start_cpu(id, intr->ArmMpidr);
522 __pcpu[id].pc_acpi_id = intr->Uid;
533 ACPI_TABLE_MADT *madt;
537 physaddr = acpi_find_table(ACPI_SIG_MADT);
541 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
543 printf("Unable to map the MADT, not starting APs\n");
548 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
549 madt_handler, &cpuid);
551 acpi_unmap_table(madt);
554 acpi_pxm_set_cpu_locality();
561 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
567 if (addr_size == 2) {
569 target_cpu |= reg[1];
572 if (!start_cpu(id, target_cpu))
575 /* Try to read the numa node of this cpu */
576 if (vm_ndomains == 1 ||
577 OF_getencprop(node, "numa-node-id", &domain, sizeof(domain)) <= 0)
579 __pcpu[id].pc_domain = domain;
580 if (domain < MAXMEMDOM)
581 CPU_SET(id, &cpuset_domain[domain]);
587 /* Initialize and fire up non-boot processors */
596 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
598 CPU_SET(0, &all_cpus);
600 switch(arm64_bus_method) {
603 mp_quirks = MP_QUIRK_CPULIST;
604 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
611 for (i = 0; fdt_quirks[i].compat != NULL; i++) {
612 if (ofw_bus_node_is_compatible(node,
613 fdt_quirks[i].compat) != 0) {
614 mp_quirks = fdt_quirks[i].quirks;
617 KASSERT(cpu0 >= 0, ("Current CPU was not found"));
618 ofw_cpu_early_foreach(cpu_init_fdt, true);
626 /* Introduce rest of cores to the world */
628 cpu_mp_announce(void)
634 cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
636 ACPI_MADT_GENERIC_INTERRUPT *intr;
640 switch(entry->Type) {
641 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
642 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
644 mpidr_reg = READ_SPECIALREG(mpidr_el1);
645 if ((mpidr_reg & 0xff00fffffful) == intr->ArmMpidr)
658 ACPI_TABLE_MADT *madt;
662 physaddr = acpi_find_table(ACPI_SIG_MADT);
666 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
668 printf("Unable to map the MADT, not starting APs\n");
673 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
674 cpu_count_acpi_handler, &cores);
676 acpi_unmap_table(madt);
684 cpu_find_cpu0_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
686 uint64_t mpidr_fdt, mpidr_reg;
690 if (addr_size == 2) {
695 mpidr_reg = READ_SPECIALREG(mpidr_el1);
697 if ((mpidr_reg & 0xff00fffffful) == mpidr_fdt)
706 cpu_mp_setmaxid(void)
713 switch(arm64_bus_method) {
716 cores = cpu_count_acpi();
718 cores = MIN(cores, MAXCPU);
720 printf("Found %d CPUs in the ACPI tables\n",
723 mp_maxid = cores - 1;
729 cores = ofw_cpu_early_foreach(cpu_find_cpu0_fdt, false);
731 cores = MIN(cores, MAXCPU);
733 printf("Found %d CPUs in the device tree\n",
736 mp_maxid = cores - 1;
742 printf("No CPU data, limiting to 1 core\n");
746 if (TUNABLE_INT_FETCH("hw.ncpu", &cores)) {
747 if (cores > 0 && cores < mp_ncpus) {
749 mp_maxid = cores - 1;
757 static struct intr_ipi *
758 intr_ipi_lookup(u_int ipi)
761 if (ipi >= INTR_IPI_COUNT)
762 panic("%s: no such IPI %u", __func__, ipi);
764 return (&ipi_sources[ipi]);
768 * interrupt controller dispatch function for IPIs. It should
769 * be called straight from the interrupt controller, when associated
770 * interrupt source is learned. Or from anybody who has an interrupt
774 intr_ipi_dispatch(u_int ipi, struct trapframe *tf)
779 ii = intr_ipi_lookup(ipi);
780 if (ii->ii_count == NULL)
781 panic("%s: not setup IPI %u", __func__, ipi);
783 intr_ipi_increment_count(ii->ii_count, PCPU_GET(cpuid));
786 * Supply ipi filter with trapframe argument
787 * if none is registered.
789 arg = ii->ii_handler_arg != NULL ? ii->ii_handler_arg : tf;
795 * Map IPI into interrupt controller.
800 ipi_map(struct intr_irqsrc *isrc, u_int ipi)
805 if (ipi >= INTR_IPI_COUNT)
806 panic("%s: no such IPI %u", __func__, ipi);
808 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
810 isrc->isrc_type = INTR_ISRCT_NAMESPACE;
811 isrc->isrc_nspc_type = INTR_IRQ_NSPC_IPI;
812 isrc->isrc_nspc_num = ipi_next_num;
814 error = PIC_REGISTER(intr_irq_root_dev, isrc, &is_percpu);
816 isrc->isrc_dev = intr_irq_root_dev;
823 * Setup IPI handler to interrupt source.
825 * Note that there could be more ways how to send and receive IPIs
826 * on a platform like fast interrupts for example. In that case,
827 * one can call this function with ASIF_NOALLOC flag set and then
828 * call intr_ipi_dispatch() when appropriate.
833 intr_ipi_set_handler(u_int ipi, const char *name, intr_ipi_filter_t *filter,
834 void *arg, u_int flags)
836 struct intr_irqsrc *isrc;
842 isrc = intr_ipi_lookup(ipi);
843 if (isrc->isrc_ipifilter != NULL)
846 if ((flags & AISHF_NOALLOC) == 0) {
847 error = ipi_map(isrc, ipi);
852 isrc->isrc_ipifilter = filter;
853 isrc->isrc_arg = arg;
854 isrc->isrc_handlers = 1;
855 isrc->isrc_count = intr_ipi_setup_counters(name);
856 isrc->isrc_index = 0; /* it should not be used in IPI case */
858 if (isrc->isrc_dev != NULL) {
859 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
860 PIC_ENABLE_SOURCE(isrc->isrc_dev, isrc);
868 ipi_all_but_self(u_int ipi)
873 CPU_CLR(PCPU_GET(cpuid), &cpus);
874 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
875 intr_ipi_send(cpus, ipi);
879 ipi_cpu(int cpu, u_int ipi)
886 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
887 intr_ipi_send(cpus, ipi);
891 ipi_selected(cpuset_t cpus, u_int ipi)
894 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
895 intr_ipi_send(cpus, ipi);