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