]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/mp_machdep.c
zfs: merge openzfs/zfs@009d3288d
[FreeBSD/FreeBSD.git] / sys / arm / arm / mp_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Semihalf.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include "opt_ddb.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/pcpu.h>
40 #include <sys/sched.h>
41 #include <sys/smp.h>
42 #include <sys/ktr.h>
43 #include <sys/malloc.h>
44
45 #include <vm/vm.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_kern.h>
48 #include <vm/pmap.h>
49
50 #include <machine/armreg.h>
51 #include <machine/cpu.h>
52 #include <machine/cpufunc.h>
53 #include <machine/debug_monitor.h>
54 #include <machine/smp.h>
55 #include <machine/pcb.h>
56 #include <machine/intr.h>
57 #include <machine/vmparam.h>
58 #ifdef VFP
59 #include <machine/vfp.h>
60 #endif
61 #ifdef CPU_MV_PJ4B
62 #include <arm/mv/mvwin.h>
63 #endif
64
65 /* used to hold the AP's until we are ready to release them */
66 struct mtx ap_boot_mtx;
67
68 /* # of Applications processors */
69 volatile int mp_naps;
70
71 /* Set to 1 once we're ready to let the APs out of the pen. */
72 volatile int aps_ready = 0;
73
74 void set_stackptrs(int cpu);
75
76 /* Temporary variables for init_secondary()  */
77 void *dpcpu[MAXCPU - 1];
78
79 /* Determine if we running MP machine */
80 int
81 cpu_mp_probe(void)
82 {
83
84         KASSERT(mp_ncpus != 0, ("cpu_mp_probe: mp_ncpus is unset"));
85
86         CPU_SETOF(0, &all_cpus);
87
88         return (mp_ncpus > 1);
89 }
90
91 /* Start Application Processor via platform specific function */
92 static int
93 check_ap(void)
94 {
95         uint32_t ms;
96
97         for (ms = 0; ms < 2000; ++ms) {
98                 if ((mp_naps + 1) == mp_ncpus)
99                         return (0);             /* success */
100                 else
101                         DELAY(1000);
102         }
103
104         return (-2);
105 }
106
107 /* Initialize and fire up non-boot processors */
108 void
109 cpu_mp_start(void)
110 {
111         int error, i;
112
113         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
114
115         /* Reserve memory for application processors */
116         for(i = 0; i < (mp_ncpus - 1); i++)
117                 dpcpu[i] = kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
118
119         dcache_wbinv_poc_all();
120
121         /* Initialize boot code and start up processors */
122         platform_mp_start_ap();
123
124         /*  Check if ap's started properly */
125         error = check_ap();
126         if (error)
127                 printf("WARNING: Some AP's failed to start\n");
128         else
129                 for (i = 1; i < mp_ncpus; i++)
130                         CPU_SET(i, &all_cpus);
131 }
132
133 /* Introduce rest of cores to the world */
134 void
135 cpu_mp_announce(void)
136 {
137
138 }
139
140 void
141 init_secondary(int cpu)
142 {
143         struct pcpu *pc;
144         uint32_t loop_counter;
145
146         pmap_set_tex();
147         cpuinfo_reinit_mmu(pmap_kern_ttb);
148         cpu_setup();
149
150         /* Provide stack pointers for other processor modes. */
151         set_stackptrs(cpu);
152
153         enable_interrupts(PSR_A);
154         pc = &__pcpu[cpu];
155
156         /*
157          * pcpu_init() updates queue, so it should not be executed in parallel
158          * on several cores
159          */
160         while(mp_naps < (cpu - 1))
161                 ;
162
163         pcpu_init(pc, cpu, sizeof(struct pcpu));
164         pc->pc_mpidr = cp15_mpidr_get() & 0xFFFFFF;
165         dpcpu_init(dpcpu[cpu - 1], cpu);
166 #if defined(DDB)
167         dbg_monitor_init_secondary();
168 #endif
169         /* Signal our startup to BSP */
170         atomic_add_rel_32(&mp_naps, 1);
171
172         /* Spin until the BSP releases the APs */
173         while (!atomic_load_acq_int(&aps_ready)) {
174 #if __ARM_ARCH >= 7
175                 __asm __volatile("wfe");
176 #endif
177         }
178
179         /* Initialize curthread */
180         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
181         pc->pc_curthread = pc->pc_idlethread;
182         pc->pc_curpcb = pc->pc_idlethread->td_pcb;
183         set_curthread(pc->pc_idlethread);
184         schedinit_ap();
185 #ifdef VFP
186         vfp_init();
187 #endif
188
189         /* Configure the interrupt controller */
190         intr_pic_init_secondary();
191
192         /* Apply possible BP hardening */
193         cpuinfo_init_bp_hardening();
194
195         mtx_lock_spin(&ap_boot_mtx);
196
197         atomic_add_rel_32(&smp_cpus, 1);
198
199         if (smp_cpus == mp_ncpus) {
200                 /* enable IPI's, tlb shootdown, freezes etc */
201                 atomic_store_rel_int(&smp_started, 1);
202         }
203
204         mtx_unlock_spin(&ap_boot_mtx);
205
206         loop_counter = 0;
207         while (smp_started == 0) {
208                 DELAY(100);
209                 loop_counter++;
210                 if (loop_counter == 1000)
211                         CTR0(KTR_SMP, "AP still wait for smp_started");
212         }
213         /* Start per-CPU event timers. */
214         cpu_initclocks_ap();
215
216         CTR0(KTR_SMP, "go into scheduler");
217
218         /* Enter the scheduler */
219         sched_ap_entry();
220
221         panic("scheduler returned us to %s", __func__);
222         /* NOTREACHED */
223 }
224
225 static void
226 ipi_rendezvous(void *dummy __unused)
227 {
228
229         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
230         smp_rendezvous_action();
231 }
232
233 static void
234 ipi_ast(void *dummy __unused)
235 {
236
237         CTR0(KTR_SMP, "IPI_AST");
238 }
239
240 static void
241 ipi_stop(void *dummy __unused)
242 {
243         u_int cpu;
244
245         /*
246          * IPI_STOP_HARD is mapped to IPI_STOP.
247          */
248         CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
249
250         cpu = PCPU_GET(cpuid);
251         savectx(&stoppcbs[cpu]);
252
253         /*
254          * CPUs are stopped when entering the debugger and at
255          * system shutdown, both events which can precede a
256          * panic dump.  For the dump to be correct, all caches
257          * must be flushed and invalidated, but on ARM there's
258          * no way to broadcast a wbinv_all to other cores.
259          * Instead, we have each core do the local wbinv_all as
260          * part of stopping the core.  The core requesting the
261          * stop will do the l2 cache flush after all other cores
262          * have done their l1 flushes and stopped.
263          */
264         dcache_wbinv_poc_all();
265
266         /* Indicate we are stopped */
267         CPU_SET_ATOMIC(cpu, &stopped_cpus);
268
269         /* Wait for restart */
270         while (!CPU_ISSET(cpu, &started_cpus))
271                 cpu_spinwait();
272
273         CPU_CLR_ATOMIC(cpu, &started_cpus);
274         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
275 #ifdef DDB
276         dbg_resume_dbreg();
277 #endif
278         CTR0(KTR_SMP, "IPI_STOP (restart)");
279 }
280
281 static void
282 ipi_preempt(void *arg)
283 {
284
285         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
286         sched_preempt(curthread);
287 }
288
289 static void
290 ipi_hardclock(void *arg)
291 {
292
293         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
294         hardclockintr();
295 }
296
297 static void
298 release_aps(void *dummy __unused)
299 {
300         uint32_t loop_counter;
301
302         if (mp_ncpus == 1)
303                 return;
304
305         intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
306         intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
307         intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
308         intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
309         intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
310
311         atomic_store_rel_int(&aps_ready, 1);
312         /* Wake the other threads up */
313         dsb();
314         sev();
315
316         printf("Release APs\n");
317
318         for (loop_counter = 0; loop_counter < 2000; loop_counter++) {
319                 if (smp_started)
320                         return;
321                 DELAY(1000);
322         }
323         printf("AP's not started\n");
324 }
325
326 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
327
328 struct cpu_group *
329 cpu_topo(void)
330 {
331
332         return (smp_topo_1level(CG_SHARE_L2, mp_ncpus, 0));
333 }
334
335 void
336 cpu_mp_setmaxid(void)
337 {
338
339         platform_mp_setmaxid();
340 }
341
342 /* Sending IPI */
343 void
344 ipi_all_but_self(u_int ipi)
345 {
346         cpuset_t other_cpus;
347
348         other_cpus = all_cpus;
349         CPU_CLR(PCPU_GET(cpuid), &other_cpus);
350         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
351         intr_ipi_send(other_cpus, ipi);
352 }
353
354 void
355 ipi_cpu(int cpu, u_int ipi)
356 {
357         cpuset_t cpus;
358
359         CPU_ZERO(&cpus);
360         CPU_SET(cpu, &cpus);
361
362         CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
363         intr_ipi_send(cpus, ipi);
364 }
365
366 void
367 ipi_selected(cpuset_t cpus, u_int ipi)
368 {
369
370         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
371         intr_ipi_send(cpus, ipi);
372 }