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