]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/mp_machdep.c
Import Zstandard 1.2.0
[FreeBSD/FreeBSD.git] / sys / arm / arm / mp_machdep.c
1 /*-
2  * Copyright (c) 2011 Semihalf.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include "opt_ddb.h"
27 #include "opt_smp.h"
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/pcpu.h>
39 #include <sys/sched.h>
40 #include <sys/smp.h>
41 #include <sys/ktr.h>
42 #include <sys/malloc.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_kern.h>
47 #include <vm/pmap.h>
48
49 #include <machine/armreg.h>
50 #include <machine/cpu.h>
51 #include <machine/cpufunc.h>
52 #include <machine/debug_monitor.h>
53 #include <machine/smp.h>
54 #include <machine/pcb.h>
55 #include <machine/physmem.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 extern struct pcpu __pcpu[];
66 /* used to hold the AP's until we are ready to release them */
67 struct mtx ap_boot_mtx;
68 struct pcb stoppcbs[MAXCPU];
69
70 /* # of Applications processors */
71 volatile int mp_naps;
72
73 /* Set to 1 once we're ready to let the APs out of the pen. */
74 volatile int aps_ready = 0;
75
76 #ifndef INTRNG
77 static int ipi_handler(void *arg);
78 #endif
79 void set_stackptrs(int cpu);
80
81 /* Temporary variables for init_secondary()  */
82 void *dpcpu[MAXCPU - 1];
83
84 /* Determine if we running MP machine */
85 int
86 cpu_mp_probe(void)
87 {
88
89         KASSERT(mp_ncpus != 0, ("cpu_mp_probe: mp_ncpus is unset"));
90
91         CPU_SETOF(0, &all_cpus);
92
93         return (mp_ncpus > 1);
94 }
95
96 /* Start Application Processor via platform specific function */
97 static int
98 check_ap(void)
99 {
100         uint32_t ms;
101
102         for (ms = 0; ms < 2000; ++ms) {
103                 if ((mp_naps + 1) == mp_ncpus)
104                         return (0);             /* success */
105                 else
106                         DELAY(1000);
107         }
108
109         return (-2);
110 }
111
112 extern unsigned char _end[];
113
114 /* Initialize and fire up non-boot processors */
115 void
116 cpu_mp_start(void)
117 {
118         int error, i;
119
120         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
121
122         /* Reserve memory for application processors */
123         for(i = 0; i < (mp_ncpus - 1); i++)
124                 dpcpu[i] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
125                     M_WAITOK | M_ZERO);
126
127         dcache_wbinv_poc_all();
128
129         /* Initialize boot code and start up processors */
130         platform_mp_start_ap();
131
132         /*  Check if ap's started properly */
133         error = check_ap();
134         if (error)
135                 printf("WARNING: Some AP's failed to start\n");
136         else
137                 for (i = 1; i < mp_ncpus; i++)
138                         CPU_SET(i, &all_cpus);
139 }
140
141 /* Introduce rest of cores to the world */
142 void
143 cpu_mp_announce(void)
144 {
145
146 }
147
148 extern vm_paddr_t pmap_pa;
149 void
150 init_secondary(int cpu)
151 {
152         struct pcpu *pc;
153         uint32_t loop_counter;
154 #ifndef INTRNG
155         int start = 0, end = 0;
156 #endif
157         uint32_t actlr_mask, actlr_set;
158
159         pmap_set_tex();
160         cpuinfo_get_actlr_modifier(&actlr_mask, &actlr_set);
161         reinit_mmu(pmap_kern_ttb, actlr_mask, actlr_set);
162         cpu_setup();
163
164         /* Provide stack pointers for other processor modes. */
165         set_stackptrs(cpu);
166
167         enable_interrupts(PSR_A);
168         pc = &__pcpu[cpu];
169
170         /*
171          * pcpu_init() updates queue, so it should not be executed in parallel
172          * on several cores
173          */
174         while(mp_naps < (cpu - 1))
175                 ;
176
177         pcpu_init(pc, cpu, sizeof(struct pcpu));
178         dpcpu_init(dpcpu[cpu - 1], cpu);
179 #if __ARM_ARCH >= 6 && defined(DDB)
180         dbg_monitor_init_secondary();
181 #endif
182         /* Signal our startup to BSP */
183         atomic_add_rel_32(&mp_naps, 1);
184
185         /* Spin until the BSP releases the APs */
186         while (!atomic_load_acq_int(&aps_ready)) {
187 #if __ARM_ARCH >= 7
188                 __asm __volatile("wfe");
189 #endif
190         }
191
192         /* Initialize curthread */
193         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
194         pc->pc_curthread = pc->pc_idlethread;
195         pc->pc_curpcb = pc->pc_idlethread->td_pcb;
196         set_curthread(pc->pc_idlethread);
197 #ifdef VFP
198         vfp_init();
199 #endif
200
201         /* Configure the interrupt controller */
202         intr_pic_init_secondary();
203
204         mtx_lock_spin(&ap_boot_mtx);
205
206         atomic_add_rel_32(&smp_cpus, 1);
207
208         if (smp_cpus == mp_ncpus) {
209                 /* enable IPI's, tlb shootdown, freezes etc */
210                 atomic_store_rel_int(&smp_started, 1);
211         }
212
213         mtx_unlock_spin(&ap_boot_mtx);
214
215 #ifndef INTRNG
216         /* Enable ipi */
217 #ifdef IPI_IRQ_START
218         start = IPI_IRQ_START;
219 #ifdef IPI_IRQ_END
220         end = IPI_IRQ_END;
221 #else
222         end = IPI_IRQ_START;
223 #endif
224 #endif
225
226         for (int i = start; i <= end; i++)
227                 arm_unmask_irq(i);
228 #endif /* INTRNG */
229         enable_interrupts(PSR_I);
230
231         loop_counter = 0;
232         while (smp_started == 0) {
233                 DELAY(100);
234                 loop_counter++;
235                 if (loop_counter == 1000)
236                         CTR0(KTR_SMP, "AP still wait for smp_started");
237         }
238         /* Start per-CPU event timers. */
239         cpu_initclocks_ap();
240
241         CTR0(KTR_SMP, "go into scheduler");
242
243         /* Enter the scheduler */
244         sched_throw(NULL);
245
246         panic("scheduler returned us to %s", __func__);
247         /* NOTREACHED */
248 }
249
250 #ifdef INTRNG
251 static void
252 ipi_rendezvous(void *dummy __unused)
253 {
254
255         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
256         smp_rendezvous_action();
257 }
258
259 static void
260 ipi_ast(void *dummy __unused)
261 {
262
263         CTR0(KTR_SMP, "IPI_AST");
264 }
265
266 static void
267 ipi_stop(void *dummy __unused)
268 {
269         u_int cpu;
270
271         /*
272          * IPI_STOP_HARD is mapped to IPI_STOP.
273          */
274         CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
275
276         cpu = PCPU_GET(cpuid);
277         savectx(&stoppcbs[cpu]);
278
279         /*
280          * CPUs are stopped when entering the debugger and at
281          * system shutdown, both events which can precede a
282          * panic dump.  For the dump to be correct, all caches
283          * must be flushed and invalidated, but on ARM there's
284          * no way to broadcast a wbinv_all to other cores.
285          * Instead, we have each core do the local wbinv_all as
286          * part of stopping the core.  The core requesting the
287          * stop will do the l2 cache flush after all other cores
288          * have done their l1 flushes and stopped.
289          */
290         dcache_wbinv_poc_all();
291
292         /* Indicate we are stopped */
293         CPU_SET_ATOMIC(cpu, &stopped_cpus);
294
295         /* Wait for restart */
296         while (!CPU_ISSET(cpu, &started_cpus))
297                 cpu_spinwait();
298
299         CPU_CLR_ATOMIC(cpu, &started_cpus);
300         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
301 #ifdef DDB
302         dbg_resume_dbreg();
303 #endif
304         CTR0(KTR_SMP, "IPI_STOP (restart)");
305 }
306
307 static void
308 ipi_preempt(void *arg)
309 {
310         struct trapframe *oldframe;
311         struct thread *td;
312
313         critical_enter();
314         td = curthread;
315         td->td_intr_nesting_level++;
316         oldframe = td->td_intr_frame;
317         td->td_intr_frame = (struct trapframe *)arg;
318
319         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
320         sched_preempt(td);
321
322         td->td_intr_frame = oldframe;
323         td->td_intr_nesting_level--;
324         critical_exit();
325 }
326
327 static void
328 ipi_hardclock(void *arg)
329 {
330         struct trapframe *oldframe;
331         struct thread *td;
332
333         critical_enter();
334         td = curthread;
335         td->td_intr_nesting_level++;
336         oldframe = td->td_intr_frame;
337         td->td_intr_frame = (struct trapframe *)arg;
338
339         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
340         hardclockintr();
341
342         td->td_intr_frame = oldframe;
343         td->td_intr_nesting_level--;
344         critical_exit();
345 }
346
347 #else
348 static int
349 ipi_handler(void *arg)
350 {
351         u_int   cpu, ipi;
352
353         cpu = PCPU_GET(cpuid);
354
355         ipi = pic_ipi_read((int)arg);
356
357         while ((ipi != 0x3ff)) {
358                 switch (ipi) {
359                 case IPI_RENDEZVOUS:
360                         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
361                         smp_rendezvous_action();
362                         break;
363
364                 case IPI_AST:
365                         CTR0(KTR_SMP, "IPI_AST");
366                         break;
367
368                 case IPI_STOP:
369                         /*
370                          * IPI_STOP_HARD is mapped to IPI_STOP so it is not
371                          * necessary to add it in the switch.
372                          */
373                         CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
374
375                         savectx(&stoppcbs[cpu]);
376
377                         /*
378                          * CPUs are stopped when entering the debugger and at
379                          * system shutdown, both events which can precede a
380                          * panic dump.  For the dump to be correct, all caches
381                          * must be flushed and invalidated, but on ARM there's
382                          * no way to broadcast a wbinv_all to other cores.
383                          * Instead, we have each core do the local wbinv_all as
384                          * part of stopping the core.  The core requesting the
385                          * stop will do the l2 cache flush after all other cores
386                          * have done their l1 flushes and stopped.
387                          */
388                         dcache_wbinv_poc_all();
389
390                         /* Indicate we are stopped */
391                         CPU_SET_ATOMIC(cpu, &stopped_cpus);
392
393                         /* Wait for restart */
394                         while (!CPU_ISSET(cpu, &started_cpus))
395                                 cpu_spinwait();
396
397                         CPU_CLR_ATOMIC(cpu, &started_cpus);
398                         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
399 #ifdef DDB
400                         dbg_resume_dbreg();
401 #endif
402                         CTR0(KTR_SMP, "IPI_STOP (restart)");
403                         break;
404                 case IPI_PREEMPT:
405                         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
406                         sched_preempt(curthread);
407                         break;
408                 case IPI_HARDCLOCK:
409                         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
410                         hardclockintr();
411                         break;
412                 default:
413                         panic("Unknown IPI 0x%0x on cpu %d", ipi, curcpu);
414                 }
415
416                 pic_ipi_clear(ipi);
417                 ipi = pic_ipi_read(-1);
418         }
419
420         return (FILTER_HANDLED);
421 }
422 #endif
423
424 static void
425 release_aps(void *dummy __unused)
426 {
427         uint32_t loop_counter;
428 #ifndef INTRNG
429         int start = 0, end = 0;
430 #endif
431
432         if (mp_ncpus == 1)
433                 return;
434
435 #ifdef INTRNG
436         intr_pic_ipi_setup(IPI_RENDEZVOUS, "rendezvous", ipi_rendezvous, NULL);
437         intr_pic_ipi_setup(IPI_AST, "ast", ipi_ast, NULL);
438         intr_pic_ipi_setup(IPI_STOP, "stop", ipi_stop, NULL);
439         intr_pic_ipi_setup(IPI_PREEMPT, "preempt", ipi_preempt, NULL);
440         intr_pic_ipi_setup(IPI_HARDCLOCK, "hardclock", ipi_hardclock, NULL);
441 #else
442 #ifdef IPI_IRQ_START
443         start = IPI_IRQ_START;
444 #ifdef IPI_IRQ_END
445         end = IPI_IRQ_END;
446 #else
447         end = IPI_IRQ_START;
448 #endif
449 #endif
450
451         for (int i = start; i <= end; i++) {
452                 /*
453                  * IPI handler
454                  */
455                 /*
456                  * Use 0xdeadbeef as the argument value for irq 0,
457                  * if we used 0, the intr code will give the trap frame
458                  * pointer instead.
459                  */
460                 arm_setup_irqhandler("ipi", ipi_handler, NULL, (void *)i, i,
461                     INTR_TYPE_MISC | INTR_EXCL, NULL);
462
463                 /* Enable ipi */
464                 arm_unmask_irq(i);
465         }
466 #endif
467         atomic_store_rel_int(&aps_ready, 1);
468         /* Wake the other threads up */
469         dsb();
470         sev();
471
472         printf("Release APs\n");
473
474         for (loop_counter = 0; loop_counter < 2000; loop_counter++) {
475                 if (smp_started)
476                         return;
477                 DELAY(1000);
478         }
479         printf("AP's not started\n");
480 }
481
482 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
483
484 struct cpu_group *
485 cpu_topo(void)
486 {
487
488         return (smp_topo_1level(CG_SHARE_L2, mp_ncpus, 0));
489 }
490
491 void
492 cpu_mp_setmaxid(void)
493 {
494
495         platform_mp_setmaxid();
496 }
497
498 /* Sending IPI */
499 void
500 ipi_all_but_self(u_int ipi)
501 {
502         cpuset_t other_cpus;
503
504         other_cpus = all_cpus;
505         CPU_CLR(PCPU_GET(cpuid), &other_cpus);
506         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
507 #ifdef INTRNG
508         intr_ipi_send(other_cpus, ipi);
509 #else
510         pic_ipi_send(other_cpus, ipi);
511 #endif
512 }
513
514 void
515 ipi_cpu(int cpu, u_int ipi)
516 {
517         cpuset_t cpus;
518
519         CPU_ZERO(&cpus);
520         CPU_SET(cpu, &cpus);
521
522         CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
523 #ifdef INTRNG
524         intr_ipi_send(cpus, ipi);
525 #else
526         pic_ipi_send(cpus, ipi);
527 #endif
528 }
529
530 void
531 ipi_selected(cpuset_t cpus, u_int ipi)
532 {
533
534         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
535 #ifdef INTRNG
536         intr_ipi_send(cpus, ipi);
537 #else
538         pic_ipi_send(cpus, ipi);
539 #endif
540 }