]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/mp_machdep.c
Merge ^/head r292951 through r293015.
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / mp_machdep.c
1 /*-
2  * Copyright (c) 2015 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/intr.h>
55 #include <machine/smp.h>
56 #ifdef VFP
57 #include <machine/vfp.h>
58 #endif
59
60 #ifdef FDT
61 #include <dev/ofw/openfirm.h>
62 #include <dev/ofw/ofw_cpu.h>
63 #endif
64
65 #include <dev/psci/psci.h>
66
67 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
68
69 extern struct pcpu __pcpu[];
70
71 static enum {
72         CPUS_UNKNOWN,
73 #ifdef FDT
74         CPUS_FDT,
75 #endif
76 } cpu_enum_method;
77
78 static device_identify_t arm64_cpu_identify;
79 static device_probe_t arm64_cpu_probe;
80 static device_attach_t arm64_cpu_attach;
81
82 static int ipi_handler(void *arg);
83
84 struct mtx ap_boot_mtx;
85 struct pcb stoppcbs[MAXCPU];
86
87 #ifdef INVARIANTS
88 static uint32_t cpu_reg[MAXCPU][2];
89 #endif
90 static device_t cpu_list[MAXCPU];
91
92 void mpentry(unsigned long cpuid);
93 void init_secondary(uint64_t);
94
95 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
96
97 /* Set to 1 once we're ready to let the APs out of the pen. */
98 volatile int aps_ready = 0;
99
100 /* Temporary variables for init_secondary()  */
101 void *dpcpu[MAXCPU - 1];
102
103 static device_method_t arm64_cpu_methods[] = {
104         /* Device interface */
105         DEVMETHOD(device_identify,      arm64_cpu_identify),
106         DEVMETHOD(device_probe,         arm64_cpu_probe),
107         DEVMETHOD(device_attach,        arm64_cpu_attach),
108
109         DEVMETHOD_END
110 };
111
112 static devclass_t arm64_cpu_devclass;
113 static driver_t arm64_cpu_driver = {
114         "arm64_cpu",
115         arm64_cpu_methods,
116         0
117 };
118
119 DRIVER_MODULE(arm64_cpu, cpu, arm64_cpu_driver, arm64_cpu_devclass, 0, 0);
120
121 static void
122 arm64_cpu_identify(driver_t *driver, device_t parent)
123 {
124
125         if (device_find_child(parent, "arm64_cpu", -1) != NULL)
126                 return;
127         if (BUS_ADD_CHILD(parent, 0, "arm64_cpu", -1) == NULL)
128                 device_printf(parent, "add child failed\n");
129 }
130
131 static int
132 arm64_cpu_probe(device_t dev)
133 {
134         u_int cpuid;
135
136         cpuid = device_get_unit(dev);
137         if (cpuid >= MAXCPU || cpuid > mp_maxid)
138                 return (EINVAL);
139
140         device_quiet(dev);
141         return (0);
142 }
143
144 static int
145 arm64_cpu_attach(device_t dev)
146 {
147         const uint32_t *reg;
148         size_t reg_size;
149         u_int cpuid;
150         int i;
151
152         cpuid = device_get_unit(dev);
153
154         if (cpuid >= MAXCPU || cpuid > mp_maxid)
155                 return (EINVAL);
156         KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
157
158         reg = cpu_get_cpuid(dev, &reg_size);
159         if (reg == NULL)
160                 return (EINVAL);
161
162         if (bootverbose) {
163                 device_printf(dev, "register <");
164                 for (i = 0; i < reg_size; i++)
165                         printf("%s%x", (i == 0) ? "" : " ", reg[i]);
166                 printf(">\n");
167         }
168
169         /* Set the device to start it later */
170         cpu_list[cpuid] = dev;
171
172         return (0);
173 }
174
175 static void
176 release_aps(void *dummy __unused)
177 {
178         int cpu, i;
179
180         /* Setup the IPI handler */
181         for (i = 0; i < COUNT_IPI; i++)
182                 arm_setup_ipihandler(ipi_handler, i);
183
184         atomic_store_rel_int(&aps_ready, 1);
185         /* Wake up the other CPUs */
186         __asm __volatile("sev");
187
188         printf("Release APs\n");
189
190         for (i = 0; i < 2000; i++) {
191                 if (smp_started) {
192                         for (cpu = 0; cpu <= mp_maxid; cpu++) {
193                                 if (CPU_ABSENT(cpu))
194                                         continue;
195                                 print_cpu_features(cpu);
196                         }
197                         return;
198                 }
199                 DELAY(1000);
200         }
201
202         printf("APs not started\n");
203 }
204 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
205
206 void
207 init_secondary(uint64_t cpu)
208 {
209         struct pcpu *pcpup;
210         int i;
211
212         pcpup = &__pcpu[cpu];
213         /*
214          * Set the pcpu pointer with a backup in tpidr_el1 to be
215          * loaded when entering the kernel from userland.
216          */
217         __asm __volatile(
218             "mov x18, %0 \n"
219             "msr tpidr_el1, %0" :: "r"(pcpup));
220
221         /* Spin until the BSP releases the APs */
222         while (!aps_ready)
223                 __asm __volatile("wfe");
224
225         /* Initialize curthread */
226         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
227         pcpup->pc_curthread = pcpup->pc_idlethread;
228         pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
229
230         /*
231          * Identify current CPU. This is necessary to setup
232          * affinity registers and to provide support for
233          * runtime chip identification.
234          */
235         identify_cpu();
236
237         /* Configure the interrupt controller */
238         arm_init_secondary();
239
240         for (i = 0; i < COUNT_IPI; i++)
241                 arm_unmask_ipi(i);
242
243         /* Start per-CPU event timers. */
244         cpu_initclocks_ap();
245
246 #ifdef VFP
247         vfp_init();
248 #endif
249
250         /* Enable interrupts */
251         intr_enable();
252
253         mtx_lock_spin(&ap_boot_mtx);
254
255         atomic_add_rel_32(&smp_cpus, 1);
256
257         if (smp_cpus == mp_ncpus) {
258                 /* enable IPI's, tlb shootdown, freezes etc */
259                 atomic_store_rel_int(&smp_started, 1);
260         }
261
262         mtx_unlock_spin(&ap_boot_mtx);
263
264         /* Enter the scheduler */
265         sched_throw(NULL);
266
267         panic("scheduler returned us to init_secondary");
268         /* NOTREACHED */
269 }
270
271 static int
272 ipi_handler(void *arg)
273 {
274         u_int cpu, ipi;
275
276         arg = (void *)((uintptr_t)arg & ~(1 << 16));
277         KASSERT((uintptr_t)arg < COUNT_IPI,
278             ("Invalid IPI %ju", (uintptr_t)arg));
279
280         cpu = PCPU_GET(cpuid);
281         ipi = (uintptr_t)arg;
282
283         switch(ipi) {
284         case IPI_AST:
285                 CTR0(KTR_SMP, "IPI_AST");
286                 break;
287         case IPI_PREEMPT:
288                 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
289                 sched_preempt(curthread);
290                 break;
291         case IPI_RENDEZVOUS:
292                 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
293                 smp_rendezvous_action();
294                 break;
295         case IPI_STOP:
296         case IPI_STOP_HARD:
297                 CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
298                 savectx(&stoppcbs[cpu]);
299
300                 /* Indicate we are stopped */
301                 CPU_SET_ATOMIC(cpu, &stopped_cpus);
302
303                 /* Wait for restart */
304                 while (!CPU_ISSET(cpu, &started_cpus))
305                         cpu_spinwait();
306
307                 CPU_CLR_ATOMIC(cpu, &started_cpus);
308                 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
309                 CTR0(KTR_SMP, "IPI_STOP (restart)");
310                 break;
311         case IPI_HARDCLOCK:
312                 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
313                 hardclockintr();
314                 break;
315         default:
316                 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
317         }
318
319         return (FILTER_HANDLED);
320 }
321
322 struct cpu_group *
323 cpu_topo(void)
324 {
325
326         return (smp_topo_none());
327 }
328
329 /* Determine if we running MP machine */
330 int
331 cpu_mp_probe(void)
332 {
333
334         /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
335         return (1);
336 }
337
338 #ifdef FDT
339 static boolean_t
340 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
341 {
342         uint64_t target_cpu;
343         struct pcpu *pcpup;
344         vm_paddr_t pa;
345         int err;
346
347         /* Check we are able to start this cpu */
348         if (id > mp_maxid)
349                 return (0);
350
351         KASSERT(id < MAXCPU, ("Too mant CPUs"));
352
353         KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
354 #ifdef INVARIANTS
355         cpu_reg[id][0] = reg[0];
356         if (addr_size == 2)
357                 cpu_reg[id][1] = reg[1];
358 #endif
359
360         /* We are already running on cpu 0 */
361         if (id == 0)
362                 return (1);
363
364
365         pcpup = &__pcpu[id];
366         pcpu_init(pcpup, id, sizeof(struct pcpu));
367
368         dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
369             M_WAITOK | M_ZERO);
370         dpcpu_init(dpcpu[id - 1], id);
371
372         target_cpu = reg[0];
373         if (addr_size == 2) {
374                 target_cpu <<= 32;
375                 target_cpu |= reg[1];
376         }
377
378         printf("Starting CPU %u (%lx)\n", id, target_cpu);
379         pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
380
381         err = psci_cpu_on(target_cpu, pa, id);
382         if (err != PSCI_RETVAL_SUCCESS) {
383                 /* Panic here if INVARIANTS are enabled */
384                 KASSERT(0, ("Failed to start CPU %u (%lx)\n", id, target_cpu));
385
386                 pcpu_destroy(pcpup);
387                 kmem_free(kernel_arena, (vm_offset_t)dpcpu[id - 1], DPCPU_SIZE);
388                 dpcpu[id - 1] = NULL;
389                 /* Notify the user that the CPU failed to start */
390                 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
391         } else
392                 CPU_SET(id, &all_cpus);
393
394         return (1);
395 }
396 #endif
397
398 /* Initialize and fire up non-boot processors */
399 void
400 cpu_mp_start(void)
401 {
402
403         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
404
405         CPU_SET(0, &all_cpus);
406
407         switch(cpu_enum_method) {
408 #ifdef FDT
409         case CPUS_FDT:
410                 ofw_cpu_early_foreach(cpu_init_fdt, true);
411                 break;
412 #endif
413         case CPUS_UNKNOWN:
414                 break;
415         }
416 }
417
418 /* Introduce rest of cores to the world */
419 void
420 cpu_mp_announce(void)
421 {
422 }
423
424 void
425 cpu_mp_setmaxid(void)
426 {
427 #ifdef FDT
428         int cores;
429
430         cores = ofw_cpu_early_foreach(NULL, false);
431         if (cores > 0) {
432                 cores = MIN(cores, MAXCPU);
433                 if (bootverbose)
434                         printf("Found %d CPUs in the device tree\n", cores);
435                 mp_ncpus = cores;
436                 mp_maxid = cores - 1;
437                 cpu_enum_method = CPUS_FDT;
438                 return;
439         }
440 #endif
441
442         if (bootverbose)
443                 printf("No CPU data, limiting to 1 core\n");
444         mp_ncpus = 1;
445         mp_maxid = 0;
446 }