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