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