]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/mp_machdep.c
Merge OpenSSL 1.0.2e.
[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 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                         return;
193                 DELAY(1000);
194         }
195
196         printf("APs not started\n");
197 }
198 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
199
200 void
201 init_secondary(uint64_t cpu)
202 {
203         struct pcpu *pcpup;
204         int i;
205
206         pcpup = &__pcpu[cpu];
207         /*
208          * Set the pcpu pointer with a backup in tpidr_el1 to be
209          * loaded when entering the kernel from userland.
210          */
211         __asm __volatile(
212             "mov x18, %0 \n"
213             "msr tpidr_el1, %0" :: "r"(pcpup));
214
215         /* Spin until the BSP releases the APs */
216         while (!aps_ready)
217                 __asm __volatile("wfe");
218
219         /* Initialize curthread */
220         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
221         pcpup->pc_curthread = pcpup->pc_idlethread;
222         pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
223
224         /*
225          * Identify current CPU. This is necessary to setup
226          * affinity registers and to provide support for
227          * runtime chip identification.
228          */
229         identify_cpu();
230
231         /* Configure the interrupt controller */
232         arm_init_secondary();
233
234         for (i = 0; i < COUNT_IPI; i++)
235                 arm_unmask_ipi(i);
236
237         /* Start per-CPU event timers. */
238         cpu_initclocks_ap();
239
240 #ifdef VFP
241         vfp_init();
242 #endif
243
244         /* Enable interrupts */
245         intr_enable();
246
247         mtx_lock_spin(&ap_boot_mtx);
248
249         atomic_add_rel_32(&smp_cpus, 1);
250
251         if (smp_cpus == mp_ncpus) {
252                 /* enable IPI's, tlb shootdown, freezes etc */
253                 atomic_store_rel_int(&smp_started, 1);
254         }
255
256         mtx_unlock_spin(&ap_boot_mtx);
257
258         /* Enter the scheduler */
259         sched_throw(NULL);
260
261         panic("scheduler returned us to init_secondary");
262         /* NOTREACHED */
263 }
264
265 static int
266 ipi_handler(void *arg)
267 {
268         u_int cpu, ipi;
269
270         arg = (void *)((uintptr_t)arg & ~(1 << 16));
271         KASSERT((uintptr_t)arg < COUNT_IPI,
272             ("Invalid IPI %ju", (uintptr_t)arg));
273
274         cpu = PCPU_GET(cpuid);
275         ipi = (uintptr_t)arg;
276
277         switch(ipi) {
278         case IPI_AST:
279                 CTR0(KTR_SMP, "IPI_AST");
280                 break;
281         case IPI_PREEMPT:
282                 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
283                 sched_preempt(curthread);
284                 break;
285         case IPI_RENDEZVOUS:
286                 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
287                 smp_rendezvous_action();
288                 break;
289         case IPI_STOP:
290         case IPI_STOP_HARD:
291                 CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
292                 savectx(&stoppcbs[cpu]);
293
294                 /* Indicate we are stopped */
295                 CPU_SET_ATOMIC(cpu, &stopped_cpus);
296
297                 /* Wait for restart */
298                 while (!CPU_ISSET(cpu, &started_cpus))
299                         cpu_spinwait();
300
301                 CPU_CLR_ATOMIC(cpu, &started_cpus);
302                 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
303                 CTR0(KTR_SMP, "IPI_STOP (restart)");
304                 break;
305         case IPI_HARDCLOCK:
306                 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
307                 hardclockintr();
308                 break;
309         default:
310                 panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
311         }
312
313         return (FILTER_HANDLED);
314 }
315
316 struct cpu_group *
317 cpu_topo(void)
318 {
319
320         return (smp_topo_none());
321 }
322
323 /* Determine if we running MP machine */
324 int
325 cpu_mp_probe(void)
326 {
327
328         /* ARM64TODO: Read the u bit of mpidr_el1 to determine this */
329         return (1);
330 }
331
332 #ifdef FDT
333 static boolean_t
334 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
335 {
336         uint64_t target_cpu;
337         struct pcpu *pcpup;
338         vm_paddr_t pa;
339         int err;
340
341         /* Check we are able to start this cpu */
342         if (id > mp_maxid)
343                 return (0);
344
345         KASSERT(id < MAXCPU, ("Too mant CPUs"));
346
347         KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
348 #ifdef INVARIANTS
349         cpu_reg[id][0] = reg[0];
350         if (addr_size == 2)
351                 cpu_reg[id][1] = reg[1];
352 #endif
353
354         /* We are already running on cpu 0 */
355         if (id == 0)
356                 return (1);
357
358
359         pcpup = &__pcpu[id];
360         pcpu_init(pcpup, id, sizeof(struct pcpu));
361
362         dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
363             M_WAITOK | M_ZERO);
364         dpcpu_init(dpcpu[id - 1], id);
365
366         target_cpu = reg[0];
367         if (addr_size == 2) {
368                 target_cpu <<= 32;
369                 target_cpu |= reg[1];
370         }
371
372         printf("Starting CPU %u (%lx)\n", id, target_cpu);
373         pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
374
375         err = psci_cpu_on(target_cpu, pa, id);
376         if (err != PSCI_RETVAL_SUCCESS) {
377                 /* Panic here if INVARIANTS are enabled */
378                 KASSERT(0, ("Failed to start CPU %u (%lx)\n", id, target_cpu));
379
380                 pcpu_destroy(pcpup);
381                 kmem_free(kernel_arena, (vm_offset_t)dpcpu[id - 1], DPCPU_SIZE);
382                 dpcpu[id - 1] = NULL;
383                 /* Notify the user that the CPU failed to start */
384                 printf("Failed to start CPU %u (%lx)\n", id, target_cpu);
385         } else
386                 CPU_SET(id, &all_cpus);
387
388         return (1);
389 }
390 #endif
391
392 /* Initialize and fire up non-boot processors */
393 void
394 cpu_mp_start(void)
395 {
396
397         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
398
399         CPU_SET(0, &all_cpus);
400
401         switch(cpu_enum_method) {
402 #ifdef FDT
403         case CPUS_FDT:
404                 ofw_cpu_early_foreach(cpu_init_fdt, true);
405                 break;
406 #endif
407         case CPUS_UNKNOWN:
408                 break;
409         }
410 }
411
412 /* Introduce rest of cores to the world */
413 void
414 cpu_mp_announce(void)
415 {
416 }
417
418 void
419 cpu_mp_setmaxid(void)
420 {
421 #ifdef FDT
422         int cores;
423
424         cores = ofw_cpu_early_foreach(NULL, false);
425         if (cores > 0) {
426                 cores = MIN(cores, MAXCPU);
427                 if (bootverbose)
428                         printf("Found %d CPUs in the device tree\n", cores);
429                 mp_ncpus = cores;
430                 mp_maxid = cores - 1;
431                 cpu_enum_method = CPUS_FDT;
432                 return;
433         }
434 #endif
435
436         if (bootverbose)
437                 printf("No CPU data, limiting to 1 core\n");
438         mp_ncpus = 1;
439         mp_maxid = 0;
440 }