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