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