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