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