]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/mp_machdep.c
zfs: merge openzfs/zfs@21bd76613 (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / mp_machdep.c
1 /*-
2  * Copyright (c) 2015-2016 The FreeBSD Foundation
3  *
4  * This software was developed by Andrew Turner under
5  * sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29
30 #include "opt_acpi.h"
31 #include "opt_ddb.h"
32 #include "opt_kstack_pages.h"
33 #include "opt_platform.h"
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/csan.h>
43 #include <sys/domainset.h>
44 #include <sys/kernel.h>
45 #include <sys/ktr.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/pcpu.h>
50 #include <sys/proc.h>
51 #include <sys/sched.h>
52 #include <sys/smp.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_map.h>
59
60 #include <machine/machdep.h>
61 #include <machine/debug_monitor.h>
62 #include <machine/intr.h>
63 #include <machine/smp.h>
64 #ifdef VFP
65 #include <machine/vfp.h>
66 #endif
67
68 #ifdef DEV_ACPI
69 #include <contrib/dev/acpica/include/acpi.h>
70 #include <dev/acpica/acpivar.h>
71 #endif
72
73 #ifdef FDT
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>
78 #endif
79
80 #include <dev/psci/psci.h>
81
82 #include "pic_if.h"
83
84 #define MP_BOOTSTACK_SIZE       (kstack_pages * PAGE_SIZE)
85
86 #define MP_QUIRK_CPULIST        0x01    /* The list of cpus may be wrong, */
87                                         /* don't panic if one fails to start */
88 static uint32_t mp_quirks;
89
90 #ifdef FDT
91 static struct {
92         const char *compat;
93         uint32_t quirks;
94 } fdt_quirks[] = {
95         { "arm,foundation-aarch64",     MP_QUIRK_CPULIST },
96         { "arm,fvp-base",               MP_QUIRK_CPULIST },
97         /* This is incorrect in some DTS files */
98         { "arm,vfp-base",               MP_QUIRK_CPULIST },
99         { NULL, 0 },
100 };
101 #endif
102
103 typedef void intr_ipi_send_t(void *, cpuset_t, u_int);
104 typedef void intr_ipi_handler_t(void *);
105
106 #define INTR_IPI_NAMELEN        (MAXCOMLEN + 1)
107 struct intr_ipi {
108         intr_ipi_handler_t *    ii_handler;
109         void *                  ii_handler_arg;
110         intr_ipi_send_t *       ii_send;
111         void *                  ii_send_arg;
112         char                    ii_name[INTR_IPI_NAMELEN];
113         u_long *                ii_count;
114 };
115
116 static struct intr_ipi ipi_sources[INTR_IPI_COUNT];
117
118 static struct intr_ipi *intr_ipi_lookup(u_int);
119 static void intr_pic_ipi_setup(u_int, const char *, intr_ipi_handler_t *,
120     void *);
121
122 static void ipi_ast(void *);
123 static void ipi_hardclock(void *);
124 static void ipi_preempt(void *);
125 static void ipi_rendezvous(void *);
126 static void ipi_stop(void *);
127
128 struct pcb stoppcbs[MAXCPU];
129
130 #ifdef FDT
131 static u_int fdt_cpuid;
132 #endif
133
134 void mpentry(unsigned long cpuid);
135 void init_secondary(uint64_t);
136
137 /* Synchronize AP startup. */
138 static struct mtx ap_boot_mtx;
139
140 /* Stacks for AP initialization, discarded once idle threads are started. */
141 void *bootstack;
142 static void *bootstacks[MAXCPU];
143
144 /* Count of started APs, used to synchronize access to bootstack. */
145 static volatile int aps_started;
146
147 /* Set to 1 once we're ready to let the APs out of the pen. */
148 static volatile int aps_ready;
149
150 /* Temporary variables for init_secondary()  */
151 void *dpcpu[MAXCPU - 1];
152
153 static bool
154 is_boot_cpu(uint64_t target_cpu)
155 {
156
157         return (PCPU_GET_MPIDR(cpuid_to_pcpu[0]) == (target_cpu & CPU_AFF_MASK));
158 }
159
160 static void
161 release_aps(void *dummy __unused)
162 {
163         int i, started;
164
165         /* Only release CPUs if they exist */
166         if (mp_ncpus == 1)
167                 return;
168
169         intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
170         intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
171         intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
172         intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
173         intr_pic_ipi_setup(IPI_STOP_HARD, "stop hard", ipi_stop, NULL);
174         intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
175
176         atomic_store_rel_int(&aps_ready, 1);
177         /* Wake up the other CPUs */
178         __asm __volatile(
179             "dsb ishst  \n"
180             "sev        \n"
181             ::: "memory");
182
183         printf("Release APs...");
184
185         started = 0;
186         for (i = 0; i < 2000; i++) {
187                 if (atomic_load_acq_int(&smp_started) != 0) {
188                         printf("done\n");
189                         return;
190                 }
191                 /*
192                  * Don't time out while we are making progress. Some large
193                  * systems can take a while to start all CPUs.
194                  */
195                 if (smp_cpus > started) {
196                         i = 0;
197                         started = smp_cpus;
198                 }
199                 DELAY(1000);
200         }
201
202         printf("APs not started\n");
203 }
204 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
205
206 void
207 init_secondary(uint64_t cpu)
208 {
209         struct pcpu *pcpup;
210         pmap_t pmap0;
211         uint64_t mpidr;
212
213         /*
214          * Verify that the value passed in 'cpu' argument (aka context_id) is
215          * valid. Some older U-Boot based PSCI implementations are buggy,
216          * they can pass random value in it.
217          */
218         mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
219         if (cpu >= MAXCPU || cpuid_to_pcpu[cpu] == NULL ||
220             PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) != mpidr) {
221                 for (cpu = 0; cpu < mp_maxid; cpu++)
222                         if (cpuid_to_pcpu[cpu] != NULL &&
223                             PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) == mpidr)
224                                 break;
225                 if ( cpu >= MAXCPU)
226                         panic("MPIDR for this CPU is not in pcpu table");
227         }
228
229         pcpup = cpuid_to_pcpu[cpu];
230         /*
231          * Set the pcpu pointer with a backup in tpidr_el1 to be
232          * loaded when entering the kernel from userland.
233          */
234         __asm __volatile(
235             "mov x18, %0 \n"
236             "msr tpidr_el1, %0" :: "r"(pcpup));
237
238         /*
239          * Identify current CPU. This is necessary to setup
240          * affinity registers and to provide support for
241          * runtime chip identification.
242          *
243          * We need this before signalling the CPU is ready to
244          * let the boot CPU use the results.
245          */
246         pcpup->pc_midr = get_midr();
247         identify_cpu(cpu);
248
249         /* Ensure the stores in identify_cpu have completed */
250         atomic_thread_fence_acq_rel();
251
252         /* Signal the BSP and spin until it has released all APs. */
253         atomic_add_int(&aps_started, 1);
254         while (!atomic_load_int(&aps_ready))
255                 __asm __volatile("wfe");
256
257         /* Initialize curthread */
258         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
259         pcpup->pc_curthread = pcpup->pc_idlethread;
260         schedinit_ap();
261
262         /* Initialize curpmap to match TTBR0's current setting. */
263         pmap0 = vmspace_pmap(&vmspace0);
264         KASSERT(pmap_to_ttbr0(pmap0) == READ_SPECIALREG(ttbr0_el1),
265             ("pmap0 doesn't match cpu %ld's ttbr0", cpu));
266         pcpup->pc_curpmap = pmap0;
267
268         install_cpu_errata();
269
270         intr_pic_init_secondary();
271
272         /* Start per-CPU event timers. */
273         cpu_initclocks_ap();
274
275 #ifdef VFP
276         vfp_init();
277 #endif
278
279         dbg_init();
280         pan_enable();
281
282         mtx_lock_spin(&ap_boot_mtx);
283         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);
287         }
288         mtx_unlock_spin(&ap_boot_mtx);
289
290         kcsan_cpu_init(cpu);
291
292         /* Enter the scheduler */
293         sched_throw(NULL);
294
295         panic("scheduler returned us to init_secondary");
296         /* NOTREACHED */
297 }
298
299 static void
300 smp_after_idle_runnable(void *arg __unused)
301 {
302         int cpu;
303
304         if (mp_ncpus == 1)
305                 return;
306
307         KASSERT(smp_started != 0, ("%s: SMP not started yet", __func__));
308
309         /*
310          * Wait for all APs to handle an interrupt.  After that, we know that
311          * the APs have entered the scheduler at least once, so the boot stacks
312          * are safe to free.
313          */
314         smp_rendezvous(smp_no_rendezvous_barrier, NULL,
315             smp_no_rendezvous_barrier, NULL);
316
317         for (cpu = 1; cpu < mp_ncpus; cpu++) {
318                 if (bootstacks[cpu] != NULL)
319                         kmem_free((vm_offset_t)bootstacks[cpu],
320                             MP_BOOTSTACK_SIZE);
321         }
322 }
323 SYSINIT(smp_after_idle_runnable, SI_SUB_SMP, SI_ORDER_ANY,
324     smp_after_idle_runnable, NULL);
325
326 /*
327  *  Send IPI thru interrupt controller.
328  */
329 static void
330 pic_ipi_send(void *arg, cpuset_t cpus, u_int ipi)
331 {
332
333         KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
334
335         /*
336          * Ensure that this CPU's stores will be visible to IPI
337          * recipients before starting to send the interrupts.
338          */
339         dsb(ishst);
340
341         PIC_IPI_SEND(intr_irq_root_dev, arg, cpus, ipi);
342 }
343
344 /*
345  *  Setup IPI handler on interrupt controller.
346  *
347  *  Not SMP coherent.
348  */
349 static void
350 intr_pic_ipi_setup(u_int ipi, const char *name, intr_ipi_handler_t *hand,
351     void *arg)
352 {
353         struct intr_irqsrc *isrc;
354         struct intr_ipi *ii;
355         int error;
356
357         KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
358         KASSERT(hand != NULL, ("%s: ipi %u no handler", __func__, ipi));
359
360         error = PIC_IPI_SETUP(intr_irq_root_dev, ipi, &isrc);
361         if (error != 0)
362                 return;
363
364         isrc->isrc_handlers++;
365
366         ii = intr_ipi_lookup(ipi);
367         KASSERT(ii->ii_count == NULL, ("%s: ipi %u reused", __func__, ipi));
368
369         ii->ii_handler = hand;
370         ii->ii_handler_arg = arg;
371         ii->ii_send = pic_ipi_send;
372         ii->ii_send_arg = isrc;
373         strlcpy(ii->ii_name, name, INTR_IPI_NAMELEN);
374         ii->ii_count = intr_ipi_setup_counters(name);
375
376         PIC_ENABLE_INTR(intr_irq_root_dev, isrc);
377 }
378
379 static void
380 intr_ipi_send(cpuset_t cpus, u_int ipi)
381 {
382         struct intr_ipi *ii;
383
384         ii = intr_ipi_lookup(ipi);
385         if (ii->ii_count == NULL)
386                 panic("%s: not setup IPI %u", __func__, ipi);
387
388         ii->ii_send(ii->ii_send_arg, cpus, ipi);
389 }
390
391 static void
392 ipi_ast(void *dummy __unused)
393 {
394
395         CTR0(KTR_SMP, "IPI_AST");
396 }
397
398 static void
399 ipi_hardclock(void *dummy __unused)
400 {
401
402         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
403         hardclockintr();
404 }
405
406 static void
407 ipi_preempt(void *dummy __unused)
408 {
409         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
410         sched_preempt(curthread);
411 }
412
413 static void
414 ipi_rendezvous(void *dummy __unused)
415 {
416
417         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
418         smp_rendezvous_action();
419 }
420
421 static void
422 ipi_stop(void *dummy __unused)
423 {
424         u_int cpu;
425
426         CTR0(KTR_SMP, "IPI_STOP");
427
428         cpu = PCPU_GET(cpuid);
429         savectx(&stoppcbs[cpu]);
430
431         /* Indicate we are stopped */
432         CPU_SET_ATOMIC(cpu, &stopped_cpus);
433
434         /* Wait for restart */
435         while (!CPU_ISSET(cpu, &started_cpus))
436                 cpu_spinwait();
437
438 #ifdef DDB
439         dbg_register_sync(NULL);
440 #endif
441
442         CPU_CLR_ATOMIC(cpu, &started_cpus);
443         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
444         CTR0(KTR_SMP, "IPI_STOP (restart)");
445 }
446
447 struct cpu_group *
448 cpu_topo(void)
449 {
450         struct cpu_group *dom, *root;
451         int i;
452
453         root = smp_topo_alloc(1);
454         dom = smp_topo_alloc(vm_ndomains);
455
456         root->cg_parent = NULL;
457         root->cg_child = dom;
458         CPU_COPY(&all_cpus, &root->cg_mask);
459         root->cg_count = mp_ncpus;
460         root->cg_children = vm_ndomains;
461         root->cg_level = CG_SHARE_NONE;
462         root->cg_flags = 0;
463
464         /*
465          * Redundant layers will be collapsed by the caller so we don't need a
466          * special case for a single domain.
467          */
468         for (i = 0; i < vm_ndomains; i++, dom++) {
469                 dom->cg_parent = root;
470                 dom->cg_child = NULL;
471                 CPU_COPY(&cpuset_domain[i], &dom->cg_mask);
472                 dom->cg_count = CPU_COUNT(&dom->cg_mask);
473                 dom->cg_children = 0;
474                 dom->cg_level = CG_SHARE_L3;
475                 dom->cg_flags = 0;
476         }
477
478         return (root);
479 }
480
481 /* Determine if we running MP machine */
482 int
483 cpu_mp_probe(void)
484 {
485
486         /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
487         return (1);
488 }
489
490 static int
491 enable_cpu_psci(uint64_t target_cpu, vm_paddr_t entry, u_int cpuid)
492 {
493         int err;
494
495         err = psci_cpu_on(target_cpu, entry, cpuid);
496         if (err != PSCI_RETVAL_SUCCESS) {
497                 /*
498                  * Panic here if INVARIANTS are enabled and PSCI failed to
499                  * start the requested CPU.  psci_cpu_on() returns PSCI_MISSING
500                  * to indicate we are unable to use it to start the given CPU.
501                  */
502                 KASSERT(err == PSCI_MISSING ||
503                     (mp_quirks & MP_QUIRK_CPULIST) == MP_QUIRK_CPULIST,
504                     ("Failed to start CPU %u (%lx), error %d\n",
505                     cpuid, target_cpu, err));
506                 return (EINVAL);
507         }
508
509         return (0);
510 }
511
512 static int
513 enable_cpu_spin(uint64_t cpu, vm_paddr_t entry, vm_paddr_t release_paddr)
514 {
515         vm_paddr_t *release_addr;
516
517         release_addr = pmap_mapdev(release_paddr, sizeof(*release_addr));
518         if (release_addr == NULL)
519                 return (ENOMEM);
520
521         *release_addr = entry;
522         pmap_unmapdev((vm_offset_t)release_addr, sizeof(*release_addr));
523
524         __asm __volatile(
525             "dsb sy     \n"
526             "sev        \n"
527             ::: "memory");
528
529         return (0);
530 }
531
532 /*
533  * Starts a given CPU. If the CPU is already running, i.e. it is the boot CPU,
534  * do nothing. Returns true if the CPU is present and running.
535  */
536 static bool
537 start_cpu(u_int cpuid, uint64_t target_cpu, int domain, vm_paddr_t release_addr)
538 {
539         struct pcpu *pcpup;
540         vm_offset_t pcpu_mem;
541         vm_size_t size;
542         vm_paddr_t pa;
543         int err, naps;
544
545         /* Check we are able to start this cpu */
546         if (cpuid > mp_maxid)
547                 return (false);
548
549         /* Skip boot CPU */
550         if (is_boot_cpu(target_cpu))
551                 return (true);
552
553         KASSERT(cpuid < MAXCPU, ("Too many CPUs"));
554
555         size = round_page(sizeof(*pcpup) + DPCPU_SIZE);
556         pcpu_mem = kmem_malloc_domainset(DOMAINSET_PREF(domain), size,
557             M_WAITOK | M_ZERO);
558         pmap_disable_promotion(pcpu_mem, size);
559
560         pcpup = (struct pcpu *)pcpu_mem;
561         pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
562         pcpup->pc_mpidr_low = target_cpu & CPU_AFF_MASK;
563         pcpup->pc_mpidr_high = (target_cpu & CPU_AFF_MASK) >> 32;
564
565         dpcpu[cpuid - 1] = (void *)(pcpup + 1);
566         dpcpu_init(dpcpu[cpuid - 1], cpuid);
567
568         bootstacks[cpuid] = (void *)kmem_malloc_domainset(
569             DOMAINSET_PREF(domain), MP_BOOTSTACK_SIZE, M_WAITOK | M_ZERO);
570
571         naps = atomic_load_int(&aps_started);
572         bootstack = (char *)bootstacks[cpuid] + MP_BOOTSTACK_SIZE;
573
574         printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
575         pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
576
577         /*
578          * A limited set of hardware we support can only do spintables and
579          * remain useful, due to lack of EL3.  Thus, we'll usually fall into the
580          * PSCI branch here.
581          */
582         MPASS(release_addr == 0 || !psci_present);
583         if (release_addr != 0)
584                 err = enable_cpu_spin(target_cpu, pa, release_addr);
585         else
586                 err = enable_cpu_psci(target_cpu, pa, cpuid);
587
588         if (err != 0) {
589                 pcpu_destroy(pcpup);
590                 dpcpu[cpuid - 1] = NULL;
591                 kmem_free((vm_offset_t)bootstacks[cpuid], MP_BOOTSTACK_SIZE);
592                 kmem_free(pcpu_mem, size);
593                 bootstacks[cpuid] = NULL;
594                 mp_ncpus--;
595                 return (false);
596         }
597
598         /* Wait for the AP to switch to its boot stack. */
599         while (atomic_load_int(&aps_started) < naps + 1)
600                 cpu_spinwait();
601         CPU_SET(cpuid, &all_cpus);
602
603         return (true);
604 }
605
606 #ifdef DEV_ACPI
607 static void
608 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
609 {
610         ACPI_MADT_GENERIC_INTERRUPT *intr;
611         u_int *cpuid;
612         u_int id;
613         int domain;
614
615         switch(entry->Type) {
616         case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
617                 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
618                 cpuid = arg;
619
620                 if (is_boot_cpu(intr->ArmMpidr))
621                         id = 0;
622                 else
623                         id = *cpuid;
624
625                 domain = 0;
626 #ifdef NUMA
627                 if (vm_ndomains > 1)
628                         domain = acpi_pxm_get_cpu_locality(intr->Uid);
629 #endif
630                 if (start_cpu(id, intr->ArmMpidr, domain, 0)) {
631                         MPASS(cpuid_to_pcpu[id] != NULL);
632                         cpuid_to_pcpu[id]->pc_acpi_id = intr->Uid;
633                         /*
634                          * Don't increment for the boot CPU, its CPU ID is
635                          * reserved.
636                          */
637                         if (!is_boot_cpu(intr->ArmMpidr))
638                                 (*cpuid)++;
639                 }
640
641                 break;
642         default:
643                 break;
644         }
645 }
646
647 static void
648 cpu_init_acpi(void)
649 {
650         ACPI_TABLE_MADT *madt;
651         vm_paddr_t physaddr;
652         u_int cpuid;
653
654         physaddr = acpi_find_table(ACPI_SIG_MADT);
655         if (physaddr == 0)
656                 return;
657
658         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
659         if (madt == NULL) {
660                 printf("Unable to map the MADT, not starting APs\n");
661                 return;
662         }
663         /* Boot CPU is always 0 */
664         cpuid = 1;
665         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
666             madt_handler, &cpuid);
667
668         acpi_unmap_table(madt);
669
670 #if MAXMEMDOM > 1
671         acpi_pxm_set_cpu_locality();
672 #endif
673 }
674 #endif
675
676 #ifdef FDT
677 /*
678  * Failure is indicated by failing to populate *release_addr.
679  */
680 static void
681 populate_release_addr(phandle_t node, vm_paddr_t *release_addr)
682 {
683         pcell_t buf[2];
684
685         if (OF_getencprop(node, "cpu-release-addr", buf, sizeof(buf)) !=
686             sizeof(buf))
687                 return;
688
689         *release_addr = (((uintptr_t)buf[0] << 32) | buf[1]);
690 }
691
692 static boolean_t
693 start_cpu_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
694 {
695         uint64_t target_cpu;
696         vm_paddr_t release_addr;
697         char *enable_method;
698         int domain;
699         int cpuid;
700
701         target_cpu = reg[0];
702         if (addr_size == 2) {
703                 target_cpu <<= 32;
704                 target_cpu |= reg[1];
705         }
706
707         if (is_boot_cpu(target_cpu))
708                 cpuid = 0;
709         else
710                 cpuid = fdt_cpuid;
711
712         /*
713          * If PSCI is present, we'll always use that -- the cpu_on method is
714          * mandated in both v0.1 and v0.2.  We'll check the enable-method if
715          * we don't have PSCI and use spin table if it's provided.
716          */
717         release_addr = 0;
718         if (!psci_present && cpuid != 0) {
719                 if (OF_getprop_alloc(node, "enable-method",
720                     (void **)&enable_method) <= 0)
721                         return (FALSE);
722
723                 if (strcmp(enable_method, "spin-table") != 0) {
724                         OF_prop_free(enable_method);
725                         return (FALSE);
726                 }
727
728                 OF_prop_free(enable_method);
729                 populate_release_addr(node, &release_addr);
730                 if (release_addr == 0) {
731                         printf("Failed to fetch release address for CPU %u",
732                             cpuid);
733                         return (FALSE);
734                 }
735         }
736
737         if (!start_cpu(cpuid, target_cpu, 0, release_addr))
738                 return (FALSE);
739
740         /*
741          * Don't increment for the boot CPU, its CPU ID is reserved.
742          */
743         if (!is_boot_cpu(target_cpu))
744                 fdt_cpuid++;
745
746         /* Try to read the numa node of this cpu */
747         if (vm_ndomains == 1 ||
748             OF_getencprop(node, "numa-node-id", &domain, sizeof(domain)) <= 0)
749                 domain = 0;
750         cpuid_to_pcpu[cpuid]->pc_domain = domain;
751         if (domain < MAXMEMDOM)
752                 CPU_SET(cpuid, &cpuset_domain[domain]);
753         return (TRUE);
754 }
755 static void
756 cpu_init_fdt(void)
757 {
758         phandle_t node;
759         int i;
760
761         node = OF_peer(0);
762         for (i = 0; fdt_quirks[i].compat != NULL; i++) {
763                 if (ofw_bus_node_is_compatible(node,
764                     fdt_quirks[i].compat) != 0) {
765                         mp_quirks = fdt_quirks[i].quirks;
766                 }
767         }
768         fdt_cpuid = 1;
769         ofw_cpu_early_foreach(start_cpu_fdt, true);
770 }
771 #endif
772
773 /* Initialize and fire up non-boot processors */
774 void
775 cpu_mp_start(void)
776 {
777         uint64_t mpidr;
778
779         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
780
781         /* CPU 0 is always boot CPU. */
782         CPU_SET(0, &all_cpus);
783         mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
784         cpuid_to_pcpu[0]->pc_mpidr_low = mpidr;
785         cpuid_to_pcpu[0]->pc_mpidr_high = mpidr >> 32;
786
787         switch(arm64_bus_method) {
788 #ifdef DEV_ACPI
789         case ARM64_BUS_ACPI:
790                 mp_quirks = MP_QUIRK_CPULIST;
791                 cpu_init_acpi();
792                 break;
793 #endif
794 #ifdef FDT
795         case ARM64_BUS_FDT:
796                 cpu_init_fdt();
797                 break;
798 #endif
799         default:
800                 break;
801         }
802 }
803
804 /* Introduce rest of cores to the world */
805 void
806 cpu_mp_announce(void)
807 {
808 }
809
810 #ifdef DEV_ACPI
811 static void
812 cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
813 {
814         ACPI_MADT_GENERIC_INTERRUPT *intr;
815         u_int *cores = arg;
816
817         switch(entry->Type) {
818         case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
819                 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
820                 (*cores)++;
821                 break;
822         default:
823                 break;
824         }
825 }
826
827 static u_int
828 cpu_count_acpi(void)
829 {
830         ACPI_TABLE_MADT *madt;
831         vm_paddr_t physaddr;
832         u_int cores;
833
834         physaddr = acpi_find_table(ACPI_SIG_MADT);
835         if (physaddr == 0)
836                 return (0);
837
838         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
839         if (madt == NULL) {
840                 printf("Unable to map the MADT, not starting APs\n");
841                 return (0);
842         }
843
844         cores = 0;
845         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
846             cpu_count_acpi_handler, &cores);
847
848         acpi_unmap_table(madt);
849
850         return (cores);
851 }
852 #endif
853
854 void
855 cpu_mp_setmaxid(void)
856 {
857         int cores;
858
859         mp_ncpus = 1;
860         mp_maxid = 0;
861
862         switch(arm64_bus_method) {
863 #ifdef DEV_ACPI
864         case ARM64_BUS_ACPI:
865                 cores = cpu_count_acpi();
866                 if (cores > 0) {
867                         cores = MIN(cores, MAXCPU);
868                         if (bootverbose)
869                                 printf("Found %d CPUs in the ACPI tables\n",
870                                     cores);
871                         mp_ncpus = cores;
872                         mp_maxid = cores - 1;
873                 }
874                 break;
875 #endif
876 #ifdef FDT
877         case ARM64_BUS_FDT:
878                 cores = ofw_cpu_early_foreach(NULL, false);
879                 if (cores > 0) {
880                         cores = MIN(cores, MAXCPU);
881                         if (bootverbose)
882                                 printf("Found %d CPUs in the device tree\n",
883                                     cores);
884                         mp_ncpus = cores;
885                         mp_maxid = cores - 1;
886                 }
887                 break;
888 #endif
889         default:
890                 if (bootverbose)
891                         printf("No CPU data, limiting to 1 core\n");
892                 break;
893         }
894
895         if (TUNABLE_INT_FETCH("hw.ncpu", &cores)) {
896                 if (cores > 0 && cores < mp_ncpus) {
897                         mp_ncpus = cores;
898                         mp_maxid = cores - 1;
899                 }
900         }
901 }
902
903 /*
904  *  Lookup IPI source.
905  */
906 static struct intr_ipi *
907 intr_ipi_lookup(u_int ipi)
908 {
909
910         if (ipi >= INTR_IPI_COUNT)
911                 panic("%s: no such IPI %u", __func__, ipi);
912
913         return (&ipi_sources[ipi]);
914 }
915
916 /*
917  *  interrupt controller dispatch function for IPIs. It should
918  *  be called straight from the interrupt controller, when associated
919  *  interrupt source is learned. Or from anybody who has an interrupt
920  *  source mapped.
921  */
922 void
923 intr_ipi_dispatch(u_int ipi, struct trapframe *tf)
924 {
925         void *arg;
926         struct intr_ipi *ii;
927
928         ii = intr_ipi_lookup(ipi);
929         if (ii->ii_count == NULL)
930                 panic("%s: not setup IPI %u", __func__, ipi);
931
932         intr_ipi_increment_count(ii->ii_count, PCPU_GET(cpuid));
933
934         /*
935          * Supply ipi filter with trapframe argument
936          * if none is registered.
937          */
938         arg = ii->ii_handler_arg != NULL ? ii->ii_handler_arg : tf;
939         ii->ii_handler(arg);
940 }
941
942 #ifdef notyet
943 /*
944  *  Map IPI into interrupt controller.
945  *
946  *  Not SMP coherent.
947  */
948 static int
949 ipi_map(struct intr_irqsrc *isrc, u_int ipi)
950 {
951         boolean_t is_percpu;
952         int error;
953
954         if (ipi >= INTR_IPI_COUNT)
955                 panic("%s: no such IPI %u", __func__, ipi);
956
957         KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
958
959         isrc->isrc_type = INTR_ISRCT_NAMESPACE;
960         isrc->isrc_nspc_type = INTR_IRQ_NSPC_IPI;
961         isrc->isrc_nspc_num = ipi_next_num;
962
963         error = PIC_REGISTER(intr_irq_root_dev, isrc, &is_percpu);
964         if (error == 0) {
965                 isrc->isrc_dev = intr_irq_root_dev;
966                 ipi_next_num++;
967         }
968         return (error);
969 }
970
971 /*
972  *  Setup IPI handler to interrupt source.
973  *
974  *  Note that there could be more ways how to send and receive IPIs
975  *  on a platform like fast interrupts for example. In that case,
976  *  one can call this function with ASIF_NOALLOC flag set and then
977  *  call intr_ipi_dispatch() when appropriate.
978  *
979  *  Not SMP coherent.
980  */
981 int
982 intr_ipi_set_handler(u_int ipi, const char *name, intr_ipi_filter_t *filter,
983     void *arg, u_int flags)
984 {
985         struct intr_irqsrc *isrc;
986         int error;
987
988         if (filter == NULL)
989                 return(EINVAL);
990
991         isrc = intr_ipi_lookup(ipi);
992         if (isrc->isrc_ipifilter != NULL)
993                 return (EEXIST);
994
995         if ((flags & AISHF_NOALLOC) == 0) {
996                 error = ipi_map(isrc, ipi);
997                 if (error != 0)
998                         return (error);
999         }
1000
1001         isrc->isrc_ipifilter = filter;
1002         isrc->isrc_arg = arg;
1003         isrc->isrc_handlers = 1;
1004         isrc->isrc_count = intr_ipi_setup_counters(name);
1005         isrc->isrc_index = 0; /* it should not be used in IPI case */
1006
1007         if (isrc->isrc_dev != NULL) {
1008                 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1009                 PIC_ENABLE_SOURCE(isrc->isrc_dev, isrc);
1010         }
1011         return (0);
1012 }
1013 #endif
1014
1015 /* Sending IPI */
1016 void
1017 ipi_all_but_self(u_int ipi)
1018 {
1019         cpuset_t cpus;
1020
1021         cpus = all_cpus;
1022         CPU_CLR(PCPU_GET(cpuid), &cpus);
1023         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
1024         intr_ipi_send(cpus, ipi);
1025 }
1026
1027 void
1028 ipi_cpu(int cpu, u_int ipi)
1029 {
1030         cpuset_t cpus;
1031
1032         CPU_ZERO(&cpus);
1033         CPU_SET(cpu, &cpus);
1034
1035         CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
1036         intr_ipi_send(cpus, ipi);
1037 }
1038
1039 void
1040 ipi_selected(cpuset_t cpus, u_int ipi)
1041 {
1042
1043         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
1044         intr_ipi_send(cpus, ipi);
1045 }