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