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