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