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