]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/mp_machdep.c
MFC r348785:
[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 #include <vm/vm_map.h>
62
63 #include <machine/intr.h>
64 #include <machine/smp.h>
65 #include <machine/sbi.h>
66
67 #ifdef FDT
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofw_cpu.h>
70 #endif
71
72 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
73
74 extern struct pcpu __pcpu[];
75
76 uint32_t __riscv_boot_ap[MAXCPU];
77
78 static enum {
79         CPUS_UNKNOWN,
80 #ifdef FDT
81         CPUS_FDT,
82 #endif
83 } cpu_enum_method;
84
85 static device_identify_t riscv64_cpu_identify;
86 static device_probe_t riscv64_cpu_probe;
87 static device_attach_t riscv64_cpu_attach;
88
89 static int ipi_handler(void *);
90
91 struct mtx ap_boot_mtx;
92 struct pcb stoppcbs[MAXCPU];
93
94 extern uint32_t boot_hart;
95 extern cpuset_t all_harts;
96
97 #ifdef INVARIANTS
98 static uint32_t cpu_reg[MAXCPU][2];
99 #endif
100 static device_t cpu_list[MAXCPU];
101
102 void mpentry(unsigned long cpuid);
103 void init_secondary(uint64_t);
104
105 uint8_t secondary_stacks[MAXCPU][PAGE_SIZE * KSTACK_PAGES] __aligned(16);
106
107 /* Set to 1 once we're ready to let the APs out of the pen. */
108 volatile int aps_ready = 0;
109
110 /* Temporary variables for init_secondary()  */
111 void *dpcpu[MAXCPU - 1];
112
113 static device_method_t riscv64_cpu_methods[] = {
114         /* Device interface */
115         DEVMETHOD(device_identify,      riscv64_cpu_identify),
116         DEVMETHOD(device_probe,         riscv64_cpu_probe),
117         DEVMETHOD(device_attach,        riscv64_cpu_attach),
118
119         DEVMETHOD_END
120 };
121
122 static devclass_t riscv64_cpu_devclass;
123 static driver_t riscv64_cpu_driver = {
124         "riscv64_cpu",
125         riscv64_cpu_methods,
126         0
127 };
128
129 DRIVER_MODULE(riscv64_cpu, cpu, riscv64_cpu_driver, riscv64_cpu_devclass, 0, 0);
130
131 static void
132 riscv64_cpu_identify(driver_t *driver, device_t parent)
133 {
134
135         if (device_find_child(parent, "riscv64_cpu", -1) != NULL)
136                 return;
137         if (BUS_ADD_CHILD(parent, 0, "riscv64_cpu", -1) == NULL)
138                 device_printf(parent, "add child failed\n");
139 }
140
141 static int
142 riscv64_cpu_probe(device_t dev)
143 {
144         u_int cpuid;
145
146         cpuid = device_get_unit(dev);
147         if (cpuid >= MAXCPU || cpuid > mp_maxid)
148                 return (EINVAL);
149
150         device_quiet(dev);
151         return (0);
152 }
153
154 static int
155 riscv64_cpu_attach(device_t dev)
156 {
157         const uint32_t *reg;
158         size_t reg_size;
159         u_int cpuid;
160         int i;
161
162         cpuid = device_get_unit(dev);
163
164         if (cpuid >= MAXCPU || cpuid > mp_maxid)
165                 return (EINVAL);
166         KASSERT(cpu_list[cpuid] == NULL, ("Already have cpu %u", cpuid));
167
168         reg = cpu_get_cpuid(dev, &reg_size);
169         if (reg == NULL)
170                 return (EINVAL);
171
172         if (bootverbose) {
173                 device_printf(dev, "register <");
174                 for (i = 0; i < reg_size; i++)
175                         printf("%s%x", (i == 0) ? "" : " ", reg[i]);
176                 printf(">\n");
177         }
178
179         /* Set the device to start it later */
180         cpu_list[cpuid] = dev;
181
182         return (0);
183 }
184
185 static void
186 release_aps(void *dummy __unused)
187 {
188         cpuset_t mask;
189         int cpu, i;
190
191         if (mp_ncpus == 1)
192                 return;
193
194         /* Setup the IPI handler */
195         riscv_setup_ipihandler(ipi_handler);
196
197         atomic_store_rel_int(&aps_ready, 1);
198
199         /* Wake up the other CPUs */
200         mask = all_harts;
201         CPU_CLR(boot_hart, &mask);
202
203         printf("Release APs\n");
204
205         sbi_send_ipi(mask.__bits);
206
207         for (i = 0; i < 2000; i++) {
208                 if (smp_started) {
209                         for (cpu = 0; cpu <= mp_maxid; cpu++) {
210                                 if (CPU_ABSENT(cpu))
211                                         continue;
212                         }
213                         return;
214                 }
215                 DELAY(1000);
216         }
217
218         printf("APs not started\n");
219 }
220 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
221
222 void
223 init_secondary(uint64_t hart)
224 {
225         struct pcpu *pcpup;
226         u_int cpuid;
227
228         /* Renumber this cpu */
229         cpuid = hart;
230         if (cpuid < boot_hart)
231                 cpuid += mp_maxid + 1;
232         cpuid -= boot_hart;
233
234         /* Setup the pcpu pointer */
235         pcpup = &__pcpu[cpuid];
236         __asm __volatile("mv gp, %0" :: "r"(pcpup));
237
238         /* Workaround: make sure wfi doesn't halt the hart */
239         csr_set(sie, SIE_SSIE);
240         csr_set(sip, SIE_SSIE);
241
242         /* Spin until the BSP releases the APs */
243         while (!aps_ready)
244                 __asm __volatile("wfi");
245
246         /* Initialize curthread */
247         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
248         pcpup->pc_curthread = pcpup->pc_idlethread;
249         pcpup->pc_curpcb = pcpup->pc_idlethread->td_pcb;
250
251         /*
252          * Identify current CPU. This is necessary to setup
253          * affinity registers and to provide support for
254          * runtime chip identification.
255          */
256         identify_cpu();
257
258         /* Enable software interrupts */
259         riscv_unmask_ipi();
260
261         /* Start per-CPU event timers. */
262         cpu_initclocks_ap();
263
264         /* Enable external (PLIC) interrupts */
265         csr_set(sie, SIE_SEIE);
266
267         /* Activate process 0's pmap. */
268         pmap_activate_boot(vmspace_pmap(proc0.p_vmspace));
269
270         mtx_lock_spin(&ap_boot_mtx);
271
272         atomic_add_rel_32(&smp_cpus, 1);
273
274         if (smp_cpus == mp_ncpus) {
275                 /* enable IPI's, tlb shootdown, freezes etc */
276                 atomic_store_rel_int(&smp_started, 1);
277         }
278
279         mtx_unlock_spin(&ap_boot_mtx);
280
281         /* Enter the scheduler */
282         sched_throw(NULL);
283
284         panic("scheduler returned us to init_secondary");
285         /* NOTREACHED */
286 }
287
288 static int
289 ipi_handler(void *arg)
290 {
291         u_int ipi_bitmap;
292         u_int cpu, ipi;
293         int bit;
294
295         sbi_clear_ipi();
296
297         cpu = PCPU_GET(cpuid);
298
299         mb();
300
301         ipi_bitmap = atomic_readandclear_int(PCPU_PTR(pending_ipis));
302         if (ipi_bitmap == 0)
303                 return (FILTER_HANDLED);
304
305         while ((bit = ffs(ipi_bitmap))) {
306                 bit = (bit - 1);
307                 ipi = (1 << bit);
308                 ipi_bitmap &= ~ipi;
309
310                 mb();
311
312                 switch (ipi) {
313                 case IPI_AST:
314                         CTR0(KTR_SMP, "IPI_AST");
315                         break;
316                 case IPI_PREEMPT:
317                         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
318                         sched_preempt(curthread);
319                         break;
320                 case IPI_RENDEZVOUS:
321                         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
322                         smp_rendezvous_action();
323                         break;
324                 case IPI_STOP:
325                 case IPI_STOP_HARD:
326                         CTR0(KTR_SMP, (ipi == IPI_STOP) ? "IPI_STOP" : "IPI_STOP_HARD");
327                         savectx(&stoppcbs[cpu]);
328
329                         /* Indicate we are stopped */
330                         CPU_SET_ATOMIC(cpu, &stopped_cpus);
331
332                         /* Wait for restart */
333                         while (!CPU_ISSET(cpu, &started_cpus))
334                                 cpu_spinwait();
335
336                         CPU_CLR_ATOMIC(cpu, &started_cpus);
337                         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
338                         CTR0(KTR_SMP, "IPI_STOP (restart)");
339
340                         /*
341                          * The kernel debugger might have set a breakpoint,
342                          * so flush the instruction cache.
343                          */
344                         fence_i();
345                         break;
346                 case IPI_HARDCLOCK:
347                         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
348                         hardclockintr();
349                         break;
350                 default:
351                         panic("Unknown IPI %#0x on cpu %d", ipi, curcpu);
352                 }
353         }
354
355         return (FILTER_HANDLED);
356 }
357
358 struct cpu_group *
359 cpu_topo(void)
360 {
361
362         return (smp_topo_none());
363 }
364
365 /* Determine if we running MP machine */
366 int
367 cpu_mp_probe(void)
368 {
369
370         return (mp_ncpus > 1);
371 }
372
373 #ifdef FDT
374 static boolean_t
375 cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
376 {
377         struct pcpu *pcpup;
378         uint64_t hart;
379         u_int cpuid;
380
381         /* Check if this hart supports MMU. */
382         if (OF_getproplen(node, "mmu-type") < 0)
383                 return (0);
384
385         KASSERT(id < MAXCPU, ("Too many CPUs"));
386
387         KASSERT(addr_size == 1 || addr_size == 2, ("Invalid register size"));
388 #ifdef INVARIANTS
389         cpu_reg[id][0] = reg[0];
390         if (addr_size == 2)
391                 cpu_reg[id][1] = reg[1];
392 #endif
393
394         hart = reg[0];
395         if (addr_size == 2) {
396                 hart <<= 32;
397                 hart |= reg[1];
398         }
399
400         KASSERT(hart < MAXCPU, ("Too many harts."));
401
402         /* We are already running on this cpu */
403         if (hart == boot_hart)
404                 return (1);
405
406         /*
407          * Rotate the CPU IDs to put the boot CPU as CPU 0.
408          * We keep the other CPUs ordered.
409          */
410         cpuid = hart;
411         if (cpuid < boot_hart)
412                 cpuid += mp_maxid + 1;
413         cpuid -= boot_hart;
414
415         /* Check if we are able to start this cpu */
416         if (cpuid > mp_maxid)
417                 return (0);
418
419         pcpup = &__pcpu[cpuid];
420         pcpu_init(pcpup, cpuid, sizeof(struct pcpu));
421         pcpup->pc_hart = hart;
422
423         dpcpu[cpuid - 1] = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
424         dpcpu_init(dpcpu[cpuid - 1], cpuid);
425
426         printf("Starting CPU %u (hart %lx)\n", cpuid, hart);
427         __riscv_boot_ap[hart] = 1;
428
429         CPU_SET(cpuid, &all_cpus);
430         CPU_SET(hart, &all_harts);
431
432         return (1);
433 }
434 #endif
435
436 /* Initialize and fire up non-boot processors */
437 void
438 cpu_mp_start(void)
439 {
440
441         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
442
443         CPU_SET(0, &all_cpus);
444         CPU_SET(boot_hart, &all_harts);
445
446         switch(cpu_enum_method) {
447 #ifdef FDT
448         case CPUS_FDT:
449                 ofw_cpu_early_foreach(cpu_init_fdt, true);
450                 break;
451 #endif
452         case CPUS_UNKNOWN:
453                 break;
454         }
455 }
456
457 /* Introduce rest of cores to the world */
458 void
459 cpu_mp_announce(void)
460 {
461 }
462
463 static boolean_t
464 cpu_check_mmu(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
465 {
466
467         /* Check if this hart supports MMU. */
468         if (OF_getproplen(node, "mmu-type") < 0)
469                 return (0);
470
471         return (1);
472 }
473
474 void
475 cpu_mp_setmaxid(void)
476 {
477 #ifdef FDT
478         int cores;
479
480         cores = ofw_cpu_early_foreach(cpu_check_mmu, true);
481         if (cores > 0) {
482                 cores = MIN(cores, MAXCPU);
483                 if (bootverbose)
484                         printf("Found %d CPUs in the device tree\n", cores);
485                 mp_ncpus = cores;
486                 mp_maxid = cores - 1;
487                 cpu_enum_method = CPUS_FDT;
488                 return;
489         }
490 #endif
491
492         if (bootverbose)
493                 printf("No CPU data, limiting to 1 core\n");
494         mp_ncpus = 1;
495         mp_maxid = 0;
496 }