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 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/cpu.h>
40 #include <sys/csan.h>
41 #include <sys/domainset.h>
42 #include <sys/kernel.h>
43 #include <sys/ktr.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/pcpu.h>
48 #include <sys/proc.h>
49 #include <sys/sched.h>
50 #include <sys/smp.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_map.h>
57
58 #include <machine/machdep.h>
59 #include <machine/cpu.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 #define MP_BOOTSTACK_SIZE (kstack_pages * PAGE_SIZE)
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 static void ipi_ast(void *);
101 static void ipi_hardclock(void *);
102 static void ipi_preempt(void *);
103 static void ipi_rendezvous(void *);
104 static void ipi_stop(void *);
105
106 #ifdef FDT
107 static u_int fdt_cpuid;
108 #endif
109
110 void mpentry(unsigned long cpuid);
111 void init_secondary(uint64_t);
112
113 /* Synchronize AP startup. */
114 static struct mtx ap_boot_mtx;
115
116 /* Used to initialize the PCPU ahead of calling init_secondary(). */
117 void *bootpcpu;
118
119 /* Stacks for AP initialization, discarded once idle threads are started. */
120 void *bootstack;
121 static void *bootstacks[MAXCPU];
122
123 /* Count of started APs, used to synchronize access to bootstack. */
124 static volatile int aps_started;
125
126 /* Set to 1 once we're ready to let the APs out of the pen. */
127 static volatile int aps_ready;
128
129 /* Temporary variables for init_secondary() */
130 static void *dpcpu[MAXCPU - 1];
131
132 static bool
is_boot_cpu(uint64_t target_cpu)133 is_boot_cpu(uint64_t target_cpu)
134 {
135
136 return (PCPU_GET_MPIDR(cpuid_to_pcpu[0]) == (target_cpu & CPU_AFF_MASK));
137 }
138
139 static void
release_aps(void * dummy __unused)140 release_aps(void *dummy __unused)
141 {
142 int i, started;
143
144 /* Only release CPUs if they exist */
145 if (mp_ncpus == 1)
146 return;
147
148 intr_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
149 intr_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
150 intr_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
151 intr_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
152 intr_ipi_setup(IPI_STOP_HARD, "stop hard", ipi_stop, NULL);
153 intr_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
154
155 atomic_store_rel_int(&aps_ready, 1);
156 /* Wake up the other CPUs */
157 __asm __volatile(
158 "dsb ishst \n"
159 "sev \n"
160 ::: "memory");
161
162 printf("Release APs...");
163
164 started = 0;
165 for (i = 0; i < 2000; i++) {
166 if (atomic_load_acq_int(&smp_started) != 0) {
167 printf("done\n");
168 return;
169 }
170 /*
171 * Don't time out while we are making progress. Some large
172 * systems can take a while to start all CPUs.
173 */
174 if (smp_cpus > started) {
175 i = 0;
176 started = smp_cpus;
177 }
178 DELAY(1000);
179 }
180
181 printf("APs not started\n");
182 }
183 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
184
185 void
init_secondary(uint64_t cpu)186 init_secondary(uint64_t cpu)
187 {
188 struct pcpu *pcpup;
189 pmap_t pmap0;
190 uint64_t mpidr;
191
192 ptrauth_mp_start(cpu);
193
194 /*
195 * Verify that the value passed in 'cpu' argument (aka context_id) is
196 * valid. Some older U-Boot based PSCI implementations are buggy,
197 * they can pass random value in it.
198 */
199 mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
200 if (cpu >= MAXCPU || cpuid_to_pcpu[cpu] == NULL ||
201 PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) != mpidr) {
202 for (cpu = 0; cpu < mp_maxid; cpu++)
203 if (cpuid_to_pcpu[cpu] != NULL &&
204 PCPU_GET_MPIDR(cpuid_to_pcpu[cpu]) == mpidr)
205 break;
206 if ( cpu >= MAXCPU)
207 panic("MPIDR for this CPU is not in pcpu table");
208 }
209
210 /*
211 * Identify current CPU. This is necessary to setup
212 * affinity registers and to provide support for
213 * runtime chip identification.
214 *
215 * We need this before signalling the CPU is ready to
216 * let the boot CPU use the results.
217 */
218 pcpup = cpuid_to_pcpu[cpu];
219 pcpup->pc_midr = get_midr();
220 identify_cpu(cpu);
221
222 /* Ensure the stores in identify_cpu have completed */
223 atomic_thread_fence_acq_rel();
224
225 /* Signal the BSP and spin until it has released all APs. */
226 atomic_add_int(&aps_started, 1);
227 while (!atomic_load_int(&aps_ready))
228 __asm __volatile("wfe");
229
230 /* Initialize curthread */
231 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
232 pcpup->pc_curthread = pcpup->pc_idlethread;
233 schedinit_ap();
234
235 /* Initialize curpmap to match TTBR0's current setting. */
236 pmap0 = vmspace_pmap(&vmspace0);
237 KASSERT(pmap_to_ttbr0(pmap0) == READ_SPECIALREG(ttbr0_el1),
238 ("pmap0 doesn't match cpu %ld's ttbr0", cpu));
239 pcpup->pc_curpmap = pmap0;
240
241 install_cpu_errata();
242
243 intr_pic_init_secondary();
244
245 /* Start per-CPU event timers. */
246 cpu_initclocks_ap();
247
248 #ifdef VFP
249 vfp_init_secondary();
250 #endif
251
252 dbg_init();
253 pan_enable();
254
255 mtx_lock_spin(&ap_boot_mtx);
256 atomic_add_rel_32(&smp_cpus, 1);
257 if (smp_cpus == mp_ncpus) {
258 /* enable IPI's, tlb shootdown, freezes etc */
259 atomic_store_rel_int(&smp_started, 1);
260 }
261 mtx_unlock_spin(&ap_boot_mtx);
262
263 kcsan_cpu_init(cpu);
264
265 /* Enter the scheduler */
266 sched_ap_entry();
267
268 panic("scheduler returned us to init_secondary");
269 /* NOTREACHED */
270 }
271
272 static void
smp_after_idle_runnable(void * arg __unused)273 smp_after_idle_runnable(void *arg __unused)
274 {
275 int cpu;
276
277 if (mp_ncpus == 1)
278 return;
279
280 KASSERT(smp_started != 0, ("%s: SMP not started yet", __func__));
281
282 /*
283 * Wait for all APs to handle an interrupt. After that, we know that
284 * the APs have entered the scheduler at least once, so the boot stacks
285 * are safe to free.
286 */
287 smp_rendezvous(smp_no_rendezvous_barrier, NULL,
288 smp_no_rendezvous_barrier, NULL);
289
290 for (cpu = 1; cpu < mp_ncpus; cpu++) {
291 if (bootstacks[cpu] != NULL)
292 kmem_free(bootstacks[cpu], MP_BOOTSTACK_SIZE);
293 }
294 }
295 SYSINIT(smp_after_idle_runnable, SI_SUB_SMP, SI_ORDER_ANY,
296 smp_after_idle_runnable, NULL);
297
298 static void
ipi_ast(void * dummy __unused)299 ipi_ast(void *dummy __unused)
300 {
301
302 CTR0(KTR_SMP, "IPI_AST");
303 }
304
305 static void
ipi_hardclock(void * dummy __unused)306 ipi_hardclock(void *dummy __unused)
307 {
308
309 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
310 hardclockintr();
311 }
312
313 static void
ipi_preempt(void * dummy __unused)314 ipi_preempt(void *dummy __unused)
315 {
316 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
317 sched_preempt(curthread);
318 }
319
320 static void
ipi_rendezvous(void * dummy __unused)321 ipi_rendezvous(void *dummy __unused)
322 {
323
324 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
325 smp_rendezvous_action();
326 }
327
328 static void
ipi_stop(void * dummy __unused)329 ipi_stop(void *dummy __unused)
330 {
331 u_int cpu;
332
333 CTR0(KTR_SMP, "IPI_STOP");
334
335 cpu = PCPU_GET(cpuid);
336 savectx(&stoppcbs[cpu]);
337
338 /* Indicate we are stopped */
339 CPU_SET_ATOMIC(cpu, &stopped_cpus);
340
341 /* Wait for restart */
342 while (!CPU_ISSET(cpu, &started_cpus))
343 cpu_spinwait();
344
345 #ifdef DDB
346 dbg_register_sync(NULL);
347 #endif
348
349 CPU_CLR_ATOMIC(cpu, &started_cpus);
350 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
351 CTR0(KTR_SMP, "IPI_STOP (restart)");
352 }
353
354 struct cpu_group *
cpu_topo(void)355 cpu_topo(void)
356 {
357 struct cpu_group *dom, *root;
358 int i;
359
360 root = smp_topo_alloc(1);
361 dom = smp_topo_alloc(vm_ndomains);
362
363 root->cg_parent = NULL;
364 root->cg_child = dom;
365 CPU_COPY(&all_cpus, &root->cg_mask);
366 root->cg_count = mp_ncpus;
367 root->cg_children = vm_ndomains;
368 root->cg_level = CG_SHARE_NONE;
369 root->cg_flags = 0;
370
371 /*
372 * Redundant layers will be collapsed by the caller so we don't need a
373 * special case for a single domain.
374 */
375 for (i = 0; i < vm_ndomains; i++, dom++) {
376 dom->cg_parent = root;
377 dom->cg_child = NULL;
378 CPU_COPY(&cpuset_domain[i], &dom->cg_mask);
379 dom->cg_count = CPU_COUNT(&dom->cg_mask);
380 dom->cg_children = 0;
381 dom->cg_level = CG_SHARE_L3;
382 dom->cg_flags = 0;
383 }
384
385 return (root);
386 }
387
388 /* Determine if we running MP machine */
389 int
cpu_mp_probe(void)390 cpu_mp_probe(void)
391 {
392
393 /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
394 return (1);
395 }
396
397 static int
enable_cpu_psci(uint64_t target_cpu,vm_paddr_t entry,u_int cpuid)398 enable_cpu_psci(uint64_t target_cpu, vm_paddr_t entry, u_int cpuid)
399 {
400 int err;
401
402 err = psci_cpu_on(target_cpu, entry, cpuid);
403 if (err != PSCI_RETVAL_SUCCESS) {
404 /*
405 * Panic here if INVARIANTS are enabled and PSCI failed to
406 * start the requested CPU. psci_cpu_on() returns PSCI_MISSING
407 * to indicate we are unable to use it to start the given CPU.
408 */
409 KASSERT(err == PSCI_MISSING ||
410 (mp_quirks & MP_QUIRK_CPULIST) == MP_QUIRK_CPULIST,
411 ("Failed to start CPU %u (%lx), error %d\n",
412 cpuid, target_cpu, err));
413 return (EINVAL);
414 }
415
416 return (0);
417 }
418
419 static int
enable_cpu_spin(uint64_t cpu,vm_paddr_t entry,vm_paddr_t release_paddr)420 enable_cpu_spin(uint64_t cpu, vm_paddr_t entry, vm_paddr_t release_paddr)
421 {
422 vm_paddr_t *release_addr;
423
424 release_addr = pmap_mapdev(release_paddr, sizeof(*release_addr));
425 if (release_addr == NULL)
426 return (ENOMEM);
427
428 *release_addr = entry;
429 pmap_unmapdev(release_addr, sizeof(*release_addr));
430
431 __asm __volatile(
432 "dsb sy \n"
433 "sev \n"
434 ::: "memory");
435
436 return (0);
437 }
438
439 /*
440 * Starts a given CPU. If the CPU is already running, i.e. it is the boot CPU,
441 * do nothing. Returns true if the CPU is present and running.
442 */
443 static bool
start_cpu(u_int cpuid,uint64_t target_cpu,int domain,vm_paddr_t release_addr)444 start_cpu(u_int cpuid, uint64_t target_cpu, int domain, vm_paddr_t release_addr)
445 {
446 struct pcpu *pcpup;
447 vm_size_t size;
448 vm_paddr_t pa;
449 int err, naps;
450
451 /* Check we are able to start this cpu */
452 if (cpuid > mp_maxid)
453 return (false);
454
455 /* Skip boot CPU */
456 if (is_boot_cpu(target_cpu))
457 return (true);
458
459 KASSERT(cpuid < MAXCPU, ("Too many CPUs"));
460
461 size = round_page(sizeof(*pcpup) + DPCPU_SIZE);
462 pcpup = kmem_malloc_domainset(DOMAINSET_PREF(domain), size,
463 M_WAITOK | M_ZERO);
464 pmap_disable_promotion((vm_offset_t)pcpup, size);
465 pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
466 pcpup->pc_mpidr = target_cpu & CPU_AFF_MASK;
467 bootpcpu = pcpup;
468
469 dpcpu[cpuid - 1] = (void *)(pcpup + 1);
470 dpcpu_init(dpcpu[cpuid - 1], cpuid);
471
472 bootstacks[cpuid] = kmem_malloc_domainset(DOMAINSET_PREF(domain),
473 MP_BOOTSTACK_SIZE, M_WAITOK | M_ZERO);
474
475 naps = atomic_load_int(&aps_started);
476 bootstack = (char *)bootstacks[cpuid] + MP_BOOTSTACK_SIZE;
477
478 printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
479 pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
480
481 /*
482 * A limited set of hardware we support can only do spintables and
483 * remain useful, due to lack of EL3. Thus, we'll usually fall into the
484 * PSCI branch here.
485 */
486 MPASS(release_addr == 0 || !psci_present);
487 if (release_addr != 0)
488 err = enable_cpu_spin(target_cpu, pa, release_addr);
489 else
490 err = enable_cpu_psci(target_cpu, pa, cpuid);
491
492 if (err != 0) {
493 pcpu_destroy(pcpup);
494 dpcpu[cpuid - 1] = NULL;
495 kmem_free(bootstacks[cpuid], MP_BOOTSTACK_SIZE);
496 kmem_free(pcpup, size);
497 bootstacks[cpuid] = NULL;
498 mp_ncpus--;
499 return (false);
500 }
501
502 /* Wait for the AP to switch to its boot stack. */
503 while (atomic_load_int(&aps_started) < naps + 1)
504 cpu_spinwait();
505 CPU_SET(cpuid, &all_cpus);
506
507 return (true);
508 }
509
510 #ifdef DEV_ACPI
511 static void
madt_handler(ACPI_SUBTABLE_HEADER * entry,void * arg)512 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
513 {
514 ACPI_MADT_GENERIC_INTERRUPT *intr;
515 u_int *cpuid;
516 u_int id;
517 int domain;
518
519 switch(entry->Type) {
520 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
521 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
522 cpuid = arg;
523
524 if (is_boot_cpu(intr->ArmMpidr))
525 id = 0;
526 else
527 id = *cpuid;
528
529 domain = 0;
530 #ifdef NUMA
531 if (vm_ndomains > 1)
532 domain = acpi_pxm_get_cpu_locality(intr->Uid);
533 #endif
534 if (start_cpu(id, intr->ArmMpidr, domain, 0)) {
535 MPASS(cpuid_to_pcpu[id] != NULL);
536 cpuid_to_pcpu[id]->pc_acpi_id = intr->Uid;
537 /*
538 * Don't increment for the boot CPU, its CPU ID is
539 * reserved.
540 */
541 if (!is_boot_cpu(intr->ArmMpidr))
542 (*cpuid)++;
543 }
544
545 break;
546 default:
547 break;
548 }
549 }
550
551 static void
cpu_init_acpi(void)552 cpu_init_acpi(void)
553 {
554 ACPI_TABLE_MADT *madt;
555 vm_paddr_t physaddr;
556 u_int cpuid;
557
558 physaddr = acpi_find_table(ACPI_SIG_MADT);
559 if (physaddr == 0)
560 return;
561
562 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
563 if (madt == NULL) {
564 printf("Unable to map the MADT, not starting APs\n");
565 return;
566 }
567 /* Boot CPU is always 0 */
568 cpuid = 1;
569 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
570 madt_handler, &cpuid);
571
572 acpi_unmap_table(madt);
573
574 #if MAXMEMDOM > 1
575 acpi_pxm_set_cpu_locality();
576 #endif
577 }
578 #endif
579
580 #ifdef FDT
581 /*
582 * Failure is indicated by failing to populate *release_addr.
583 */
584 static void
populate_release_addr(phandle_t node,vm_paddr_t * release_addr)585 populate_release_addr(phandle_t node, vm_paddr_t *release_addr)
586 {
587 pcell_t buf[2];
588
589 if (OF_getencprop(node, "cpu-release-addr", buf, sizeof(buf)) !=
590 sizeof(buf))
591 return;
592
593 *release_addr = (((uintptr_t)buf[0] << 32) | buf[1]);
594 }
595
596 static bool
start_cpu_fdt(u_int id,phandle_t node,u_int addr_size,pcell_t * reg)597 start_cpu_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
598 {
599 uint64_t target_cpu;
600 vm_paddr_t release_addr;
601 char *enable_method;
602 int domain;
603 int cpuid;
604
605 target_cpu = reg[0];
606 if (addr_size == 2) {
607 target_cpu <<= 32;
608 target_cpu |= reg[1];
609 }
610
611 if (is_boot_cpu(target_cpu))
612 cpuid = 0;
613 else
614 cpuid = fdt_cpuid;
615
616 /*
617 * If PSCI is present, we'll always use that -- the cpu_on method is
618 * mandated in both v0.1 and v0.2. We'll check the enable-method if
619 * we don't have PSCI and use spin table if it's provided.
620 */
621 release_addr = 0;
622 if (!psci_present && cpuid != 0) {
623 if (OF_getprop_alloc(node, "enable-method",
624 (void **)&enable_method) <= 0)
625 return (false);
626
627 if (strcmp(enable_method, "spin-table") != 0) {
628 OF_prop_free(enable_method);
629 return (false);
630 }
631
632 OF_prop_free(enable_method);
633 populate_release_addr(node, &release_addr);
634 if (release_addr == 0) {
635 printf("Failed to fetch release address for CPU %u",
636 cpuid);
637 return (false);
638 }
639 }
640
641 if (!start_cpu(cpuid, target_cpu, 0, release_addr))
642 return (false);
643
644 /*
645 * Don't increment for the boot CPU, its CPU ID is reserved.
646 */
647 if (!is_boot_cpu(target_cpu))
648 fdt_cpuid++;
649
650 /* Try to read the numa node of this cpu */
651 if (vm_ndomains == 1 ||
652 OF_getencprop(node, "numa-node-id", &domain, sizeof(domain)) <= 0)
653 domain = 0;
654 cpuid_to_pcpu[cpuid]->pc_domain = domain;
655 if (domain < MAXMEMDOM)
656 CPU_SET(cpuid, &cpuset_domain[domain]);
657 return (true);
658 }
659 static void
cpu_init_fdt(void)660 cpu_init_fdt(void)
661 {
662 phandle_t node;
663 int i;
664
665 node = OF_peer(0);
666 for (i = 0; fdt_quirks[i].compat != NULL; i++) {
667 if (ofw_bus_node_is_compatible(node,
668 fdt_quirks[i].compat) != 0) {
669 mp_quirks = fdt_quirks[i].quirks;
670 }
671 }
672 fdt_cpuid = 1;
673 ofw_cpu_early_foreach(start_cpu_fdt, true);
674 }
675 #endif
676
677 /* Initialize and fire up non-boot processors */
678 void
cpu_mp_start(void)679 cpu_mp_start(void)
680 {
681 uint64_t mpidr;
682
683 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
684
685 /* CPU 0 is always boot CPU. */
686 CPU_SET(0, &all_cpus);
687 mpidr = READ_SPECIALREG(mpidr_el1) & CPU_AFF_MASK;
688 cpuid_to_pcpu[0]->pc_mpidr = mpidr;
689
690 cpu_desc_init();
691
692 switch(arm64_bus_method) {
693 #ifdef DEV_ACPI
694 case ARM64_BUS_ACPI:
695 mp_quirks = MP_QUIRK_CPULIST;
696 cpu_init_acpi();
697 break;
698 #endif
699 #ifdef FDT
700 case ARM64_BUS_FDT:
701 cpu_init_fdt();
702 break;
703 #endif
704 default:
705 break;
706 }
707 }
708
709 /* Introduce rest of cores to the world */
710 void
cpu_mp_announce(void)711 cpu_mp_announce(void)
712 {
713 }
714
715 #ifdef DEV_ACPI
716 static void
cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER * entry,void * arg)717 cpu_count_acpi_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
718 {
719 u_int *cores = arg;
720
721 switch(entry->Type) {
722 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
723 (*cores)++;
724 break;
725 default:
726 break;
727 }
728 }
729
730 static u_int
cpu_count_acpi(void)731 cpu_count_acpi(void)
732 {
733 ACPI_TABLE_MADT *madt;
734 vm_paddr_t physaddr;
735 u_int cores;
736
737 physaddr = acpi_find_table(ACPI_SIG_MADT);
738 if (physaddr == 0)
739 return (0);
740
741 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
742 if (madt == NULL) {
743 printf("Unable to map the MADT, not starting APs\n");
744 return (0);
745 }
746
747 cores = 0;
748 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
749 cpu_count_acpi_handler, &cores);
750
751 acpi_unmap_table(madt);
752
753 return (cores);
754 }
755 #endif
756
757 void
cpu_mp_setmaxid(void)758 cpu_mp_setmaxid(void)
759 {
760 int cores;
761
762 mp_ncpus = 1;
763 mp_maxid = 0;
764
765 switch(arm64_bus_method) {
766 #ifdef DEV_ACPI
767 case ARM64_BUS_ACPI:
768 cores = cpu_count_acpi();
769 if (cores > 0) {
770 cores = MIN(cores, MAXCPU);
771 if (bootverbose)
772 printf("Found %d CPUs in the ACPI tables\n",
773 cores);
774 mp_ncpus = cores;
775 mp_maxid = cores - 1;
776 }
777 break;
778 #endif
779 #ifdef FDT
780 case ARM64_BUS_FDT:
781 cores = ofw_cpu_early_foreach(NULL, false);
782 if (cores > 0) {
783 cores = MIN(cores, MAXCPU);
784 if (bootverbose)
785 printf("Found %d CPUs in the device tree\n",
786 cores);
787 mp_ncpus = cores;
788 mp_maxid = cores - 1;
789 }
790 break;
791 #endif
792 default:
793 if (bootverbose)
794 printf("No CPU data, limiting to 1 core\n");
795 break;
796 }
797
798 if (TUNABLE_INT_FETCH("hw.ncpu", &cores)) {
799 if (cores > 0 && cores < mp_ncpus) {
800 mp_ncpus = cores;
801 mp_maxid = cores - 1;
802 }
803 }
804 }
805
806 /* Sending IPI */
807 void
ipi_all_but_self(u_int ipi)808 ipi_all_but_self(u_int ipi)
809 {
810 cpuset_t cpus;
811
812 cpus = all_cpus;
813 CPU_CLR(PCPU_GET(cpuid), &cpus);
814 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
815 intr_ipi_send(cpus, ipi);
816 }
817
818 void
ipi_cpu(int cpu,u_int ipi)819 ipi_cpu(int cpu, u_int ipi)
820 {
821 cpuset_t cpus;
822
823 CPU_ZERO(&cpus);
824 CPU_SET(cpu, &cpus);
825
826 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
827 intr_ipi_send(cpus, ipi);
828 }
829
830 void
ipi_selected(cpuset_t cpus,u_int ipi)831 ipi_selected(cpuset_t cpus, u_int ipi)
832 {
833
834 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
835 intr_ipi_send(cpus, ipi);
836 }
837