]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/mp_machdep.c
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / mp_machdep.c
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * Copyright (c) 2016 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Andrew Turner under
7  * sponsorship from the FreeBSD Foundation.
8  *
9  * Portions of this software were developed by SRI International and the
10  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
11  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
12  *
13  * Portions of this software were developed by the University of Cambridge
14  * Computer Laboratory as part of the CTSRD Project, with support from the
15  * UK Higher Education Innovation Fund (HEIF).
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 #include "opt_kstack_pages.h"
40 #include "opt_platform.h"
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48 #include <sys/cpu.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/sched.h>
55 #include <sys/smp.h>
56
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_kern.h>
61
62 #include <machine/intr.h>
63 #include <machine/smp.h>
64 #include <machine/sbi.h>
65
66 #ifdef FDT
67 #include <dev/ofw/openfirm.h>
68 #include <dev/ofw/ofw_cpu.h>
69 #endif
70
71 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
72
73 extern struct pcpu __pcpu[];
74
75 uint32_t __riscv_boot_ap[MAXCPU];
76
77 static enum {
78         CPUS_UNKNOWN,
79 #ifdef FDT
80         CPUS_FDT,
81 #endif
82 } cpu_enum_method;
83
84 static device_identify_t riscv64_cpu_identify;
85 static device_probe_t riscv64_cpu_probe;
86 static device_attach_t riscv64_cpu_attach;
87
88 static int ipi_handler(void *);
89
90 struct mtx ap_boot_mtx;
91 struct pcb stoppcbs[MAXCPU];
92
93 #ifdef INVARIANTS
94 static uint32_t cpu_reg[MAXCPU][2];
95 #endif
96 static device_t cpu_list[MAXCPU];
97
98 void mpentry(unsigned long cpuid);
99 void init_secondary(uint64_t);
100
101 uint8_t secondary_stacks[MAXCPU - 1][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
102
103 /* Set to 1 once we're ready to let the APs out of the pen. */
104 volatile int aps_ready = 0;
105
106 /* Temporary variables for init_secondary()  */
107 void *dpcpu[MAXCPU - 1];
108
109 static device_method_t riscv64_cpu_methods[] = {
110         /* Device interface */
111         DEVMETHOD(device_identify,      riscv64_cpu_identify),
112         DEVMETHOD(device_probe,         riscv64_cpu_probe),
113         DEVMETHOD(device_attach,        riscv64_cpu_attach),
114
115         DEVMETHOD_END
116 };
117
118 static devclass_t riscv64_cpu_devclass;
119 static driver_t riscv64_cpu_driver = {
120         "riscv64_cpu",
121         riscv64_cpu_methods,
122         0
123 };
124
125 DRIVER_MODULE(riscv64_cpu, cpu, riscv64_cpu_driver, riscv64_cpu_devclass, 0, 0);
126
127 static void
128 riscv64_cpu_identify(driver_t *driver, device_t parent)
129 {
130
131         if (device_find_child(parent, "riscv64_cpu", -1) != NULL)
132                 return;
133         if (BUS_ADD_CHILD(parent, 0, "riscv64_cpu", -1) == NULL)
134                 device_printf(parent, "add child failed\n");
135 }
136
137 static int
138 riscv64_cpu_probe(device_t dev)
139 {
140         u_int cpuid;
141
142         cpuid = device_get_unit(dev);
143         if (cpuid >= MAXCPU || cpuid > mp_maxid)
144                 return (EINVAL);
145
146         device_quiet(dev);
147         return (0);
148 }
149
150 static int
151 riscv64_cpu_attach(device_t dev)
152 {
153         const uint32_t *reg;
154         size_t reg_size;
155         u_int cpuid;
156         int i;
157
158         cpuid = device_get_unit(dev);
159
160         if (cpuid >= MAXCPU || cpuid > mp_maxid)
161                 return (EINVAL);
162         KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
163
164         reg = cpu_get_cpuid(dev, &reg_size);
165         if (reg == NULL)
166                 return (EINVAL);
167
168         if (bootverbose) {
169                 device_printf(dev, "register <");
170                 for (i = 0; i < reg_size; i++)
171                         printf("%s%x", (i == 0) ? "" : " ", reg[i]);
172                 printf(">\n");
173         }
174
175         /* Set the device to start it later */
176         cpu_list[cpuid] = dev;
177
178         return (0);
179 }
180
181 static void
182 release_aps(void *dummy __unused)
183 {
184         int cpu, i;
185
186         if (mp_ncpus == 1)
187                 return;
188
189         /* Setup the IPI handler */
190         riscv_setup_ipihandler(ipi_handler);
191
192         atomic_store_rel_int(&aps_ready, 1);
193
194         printf("Release APs\n");
195
196         for (i = 0; i < 2000; i++) {
197                 if (smp_started) {
198                         for (cpu = 0; cpu <= mp_maxid; cpu++) {
199                                 if (CPU_ABSENT(cpu))
200                                         continue;
201                         }
202                         return;
203                 }
204                 DELAY(1000);
205         }
206
207         printf("APs not started\n");
208 }
209 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
210
211 void
212 init_secondary(uint64_t cpu)
213 {
214         struct pcpu *pcpup;
215
216         /* Setup the pcpu pointer */
217         pcpup = &__pcpu[cpu];
218         __asm __volatile("mv gp, %0" :: "r"(pcpup));
219
220         /* Spin until the BSP releases the APs */
221         while (!aps_ready)
222                 __asm __volatile("wfi");
223
224         /* Initialize curthread */
225         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
226         pcpup->pc_curthread = pcpup->pc_idlethread;
227         pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
228
229         /*
230          * Identify current CPU. This is necessary to setup
231          * affinity registers and to provide support for
232          * runtime chip identification.
233          */
234         identify_cpu();
235
236         /* Enable software interrupts */
237         riscv_unmask_ipi();
238
239         /* Start per-CPU event timers. */
240         cpu_initclocks_ap();
241
242         /* Enable interrupts */
243         intr_enable();
244
245         mtx_lock_spin(&ap_boot_mtx);
246
247         atomic_add_rel_32(&smp_cpus, 1);
248
249         if (smp_cpus == mp_ncpus) {
250                 /* enable IPI's, tlb shootdown, freezes etc */
251                 atomic_store_rel_int(&smp_started, 1);
252         }
253
254         mtx_unlock_spin(&ap_boot_mtx);
255
256         /* Enter the scheduler */
257         sched_throw(NULL);
258
259         panic("scheduler returned us to init_secondary");
260         /* NOTREACHED */
261 }
262
263 static int
264 ipi_handler(void *arg)
265 {
266         u_int ipi_bitmap;
267         u_int cpu, ipi;
268         int bit;
269
270         sbi_clear_ipi();
271
272         cpu = PCPU_GET(cpuid);
273
274         mb();
275
276         ipi_bitmap = atomic_readandclear_int(PCPU_PTR(pending_ipis));
277         if (ipi_bitmap == 0)
278                 return (FILTER_HANDLED);
279
280         while ((bit = ffs(ipi_bitmap))) {
281                 bit = (bit - 1);
282                 ipi = (1 << bit);
283                 ipi_bitmap &= ~ipi;
284
285                 mb();
286
287                 switch (ipi) {
288                 case IPI_AST:
289                         CTR0(KTR_SMP, "IPI_AST");
290                         break;
291                 case IPI_PREEMPT:
292                         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
293                         sched_preempt(curthread);
294                         break;
295                 case IPI_RENDEZVOUS:
296                         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
297                         smp_rendezvous_action();
298                         break;
299                 case IPI_STOP:
300                 case IPI_STOP_HARD:
301                         CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
302                         savectx(&stoppcbs[cpu]);
303
304                         /* Indicate we are stopped */
305                         CPU_SET_ATOMIC(cpu, &stopped_cpus);
306
307                         /* Wait for restart */
308                         while (!CPU_ISSET(cpu, &started_cpus))
309                                 cpu_spinwait();
310
311                         CPU_CLR_ATOMIC(cpu, &started_cpus);
312                         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
313                         CTR0(KTR_SMP, "IPI_STOP (restart)");
314                         break;
315                 case IPI_HARDCLOCK:
316                         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
317                         hardclockintr();
318                         break;
319                 default:
320                         panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
321                 }
322         }
323
324         return (FILTER_HANDLED);
325 }
326
327 struct cpu_group *
328 cpu_topo(void)
329 {
330
331         return (smp_topo_none());
332 }
333
334 /* Determine if we running MP machine */
335 int
336 cpu_mp_probe(void)
337 {
338
339         return (mp_ncpus > 1);
340 }
341
342 #ifdef FDT
343 static boolean_t
344 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
345 {
346         uint64_t target_cpu;
347         struct pcpu *pcpup;
348
349         /* Check we are able to start this cpu */
350         if (id > mp_maxid)
351                 return (0);
352
353         KASSERT(id < MAXCPU, ("Too many CPUs"));
354
355         KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
356 #ifdef INVARIANTS
357         cpu_reg[id][0] = reg[0];
358         if (addr_size == 2)
359                 cpu_reg[id][1] = reg[1];
360 #endif
361
362         target_cpu = reg[0];
363         if (addr_size == 2) {
364                 target_cpu <<= 32;
365                 target_cpu |= reg[1];
366         }
367
368         pcpup = &__pcpu[id];
369
370         /* We are already running on cpu 0 */
371         if (id == 0) {
372                 return (1);
373         }
374
375         pcpu_init(pcpup, id, sizeof(struct pcpu));
376
377         dpcpu[id - 1] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
378             M_WAITOK | M_ZERO);
379         dpcpu_init(dpcpu[id - 1], id);
380
381         printf("Starting CPU %u (%lx)\n", id, target_cpu);
382         __riscv_boot_ap[id] = 1;
383
384         CPU_SET(id, &all_cpus);
385
386         return (1);
387 }
388 #endif
389
390 /* Initialize and fire up non-boot processors */
391 void
392 cpu_mp_start(void)
393 {
394
395         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
396
397         CPU_SET(0, &all_cpus);
398
399         switch(cpu_enum_method) {
400 #ifdef FDT
401         case CPUS_FDT:
402                 ofw_cpu_early_foreach(cpu_init_fdt, true);
403                 break;
404 #endif
405         case CPUS_UNKNOWN:
406                 break;
407         }
408 }
409
410 /* Introduce rest of cores to the world */
411 void
412 cpu_mp_announce(void)
413 {
414 }
415
416 void
417 cpu_mp_setmaxid(void)
418 {
419 #ifdef FDT
420         int cores;
421
422         cores = ofw_cpu_early_foreach(NULL, false);
423         if (cores > 0) {
424                 cores = MIN(cores, MAXCPU);
425                 if (bootverbose)
426                         printf("Found %d CPUs in the device tree\n", cores);
427                 mp_ncpus = cores;
428                 mp_maxid = cores - 1;
429                 cpu_enum_method = CPUS_FDT;
430                 return;
431         }
432 #endif
433
434         if (bootverbose)
435                 printf("No CPU data, limiting to 1 core\n");
436         mp_ncpus = 1;
437         mp_maxid = 0;
438 }