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