]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/mp_machdep.c
Update llvm/clang to r242221.
[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/armreg.h>
47 #include <machine/cpu.h>
48 #include <machine/cpufunc.h>
49 #include <machine/smp.h>
50 #include <machine/pcb.h>
51 #include <machine/pmap.h>
52 #include <machine/pte.h>
53 #include <machine/physmem.h>
54 #include <machine/intr.h>
55 #include <machine/vmparam.h>
56 #ifdef VFP
57 #include <machine/vfp.h>
58 #endif
59 #ifdef CPU_MV_PJ4B
60 #include <arm/mv/mvwin.h>
61 #include <dev/fdt/fdt_common.h>
62 #endif
63
64 #include "opt_smp.h"
65
66 extern struct pcpu __pcpu[];
67 /* used to hold the AP's until we are ready to release them */
68 struct mtx ap_boot_mtx;
69 struct pcb stoppcbs[MAXCPU];
70
71 /* # of Applications processors */
72 volatile int mp_naps;
73
74 /* Set to 1 once we're ready to let the APs out of the pen. */
75 volatile int aps_ready = 0;
76
77 static int ipi_handler(void *arg);
78 void set_stackptrs(int cpu);
79
80 /* Temporary variables for init_secondary()  */
81 void *dpcpu[MAXCPU - 1];
82
83 /* Determine if we running MP machine */
84 int
85 cpu_mp_probe(void)
86 {
87         CPU_SETOF(0, &all_cpus);
88
89         return (platform_mp_probe());
90 }
91
92 /* Start Application Processor via platform specific function */
93 static int
94 check_ap(void)
95 {
96         uint32_t ms;
97
98         for (ms = 0; ms < 2000; ++ms) {
99                 if ((mp_naps + 1) == mp_ncpus)
100                         return (0);             /* success */
101                 else
102                         DELAY(1000);
103         }
104
105         return (-2);
106 }
107
108 extern unsigned char _end[];
109
110 /* Initialize and fire up non-boot processors */
111 void
112 cpu_mp_start(void)
113 {
114         int error, i;
115
116         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
117
118         /* Reserve memory for application processors */
119         for(i = 0; i < (mp_ncpus - 1); i++)
120                 dpcpu[i] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
121                     M_WAITOK | M_ZERO);
122
123         cpu_idcache_wbinv_all();
124         cpu_l2cache_wbinv_all();
125         cpu_idcache_wbinv_all();
126
127         /* Initialize boot code and start up processors */
128         platform_mp_start_ap();
129
130         /*  Check if ap's started properly */
131         error = check_ap();
132         if (error)
133                 printf("WARNING: Some AP's failed to start\n");
134         else
135                 for (i = 1; i < mp_ncpus; i++)
136                         CPU_SET(i, &all_cpus);
137
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         int start = 0, end = 0;
154
155 #ifdef ARM_NEW_PMAP
156         pmap_set_tex();
157         reinit_mmu(pmap_kern_ttb, (1<<6) | (1<< 0), (1<<6) | (1<< 0));
158         cpu_setup();
159
160         /* Provide stack pointers for other processor modes. */
161         set_stackptrs(cpu);
162
163         enable_interrupts(PSR_A);
164 #else /* ARM_NEW_PMAP */
165         cpu_setup();
166         setttb(pmap_pa);
167         cpu_tlb_flushID();
168 #endif /* ARM_NEW_PMAP */
169         pc = &__pcpu[cpu];
170
171         /*
172          * pcpu_init() updates queue, so it should not be executed in parallel
173          * on several cores
174          */
175         while(mp_naps < (cpu - 1))
176                 ;
177
178         pcpu_init(pc, cpu, sizeof(struct pcpu));
179         dpcpu_init(dpcpu[cpu - 1], cpu);
180 #ifndef ARM_NEW_PMAP
181         /* Provide stack pointers for other processor modes. */
182         set_stackptrs(cpu);
183 #endif
184         /* Signal our startup to BSP */
185         atomic_add_rel_32(&mp_naps, 1);
186
187         /* Spin until the BSP releases the APs */
188         while (!atomic_load_acq_int(&aps_ready)) {
189 #if __ARM_ARCH >= 7
190                 __asm __volatile("wfe");
191 #endif
192         }
193
194         /* Initialize curthread */
195         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
196         pc->pc_curthread = pc->pc_idlethread;
197         pc->pc_curpcb = pc->pc_idlethread->td_pcb;
198         set_curthread(pc->pc_idlethread);
199 #ifdef VFP
200         vfp_init();
201 #endif
202
203         mtx_lock_spin(&ap_boot_mtx);
204
205         atomic_add_rel_32(&smp_cpus, 1);
206
207         if (smp_cpus == mp_ncpus) {
208                 /* enable IPI's, tlb shootdown, freezes etc */
209                 atomic_store_rel_int(&smp_started, 1);
210         }
211
212         mtx_unlock_spin(&ap_boot_mtx);
213
214         /* Enable ipi */
215 #ifdef IPI_IRQ_START
216         start = IPI_IRQ_START;
217 #ifdef IPI_IRQ_END
218         end = IPI_IRQ_END;
219 #else
220         end = IPI_IRQ_START;
221 #endif
222 #endif
223
224         for (int i = start; i <= end; i++)
225                 arm_unmask_irq(i);
226         enable_interrupts(PSR_I);
227
228         loop_counter = 0;
229         while (smp_started == 0) {
230                 DELAY(100);
231                 loop_counter++;
232                 if (loop_counter == 1000)
233                         CTR0(KTR_SMP, "AP still wait for smp_started");
234         }
235         /* Start per-CPU event timers. */
236         cpu_initclocks_ap();
237
238         CTR0(KTR_SMP, "go into scheduler");
239         platform_mp_init_secondary();
240
241         /* Enter the scheduler */
242         sched_throw(NULL);
243
244         panic("scheduler returned us to %s", __func__);
245         /* NOTREACHED */
246 }
247
248 static int
249 ipi_handler(void *arg)
250 {
251         u_int   cpu, ipi;
252
253         cpu = PCPU_GET(cpuid);
254
255         ipi = pic_ipi_read((int)arg);
256
257         while ((ipi != 0x3ff)) {
258                 switch (ipi) {
259                 case IPI_RENDEZVOUS:
260                         CTR0(KTR_SMP, "IPI_RENDEZVOUS");
261                         smp_rendezvous_action();
262                         break;
263
264                 case IPI_AST:
265                         CTR0(KTR_SMP, "IPI_AST");
266                         break;
267
268                 case IPI_STOP:
269                         /*
270                          * IPI_STOP_HARD is mapped to IPI_STOP so it is not
271                          * necessary to add it in the switch.
272                          */
273                         CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
274
275                         savectx(&stoppcbs[cpu]);
276
277                         /*
278                          * CPUs are stopped when entering the debugger and at
279                          * system shutdown, both events which can precede a
280                          * panic dump.  For the dump to be correct, all caches
281                          * must be flushed and invalidated, but on ARM there's
282                          * no way to broadcast a wbinv_all to other cores.
283                          * Instead, we have each core do the local wbinv_all as
284                          * part of stopping the core.  The core requesting the
285                          * stop will do the l2 cache flush after all other cores
286                          * have done their l1 flushes and stopped.
287                          */
288                         cpu_idcache_wbinv_all();
289
290                         /* Indicate we are stopped */
291                         CPU_SET_ATOMIC(cpu, &stopped_cpus);
292
293                         /* Wait for restart */
294                         while (!CPU_ISSET(cpu, &started_cpus))
295                                 cpu_spinwait();
296
297                         CPU_CLR_ATOMIC(cpu, &started_cpus);
298                         CPU_CLR_ATOMIC(cpu, &stopped_cpus);
299                         CTR0(KTR_SMP, "IPI_STOP (restart)");
300                         break;
301                 case IPI_PREEMPT:
302                         CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
303                         sched_preempt(curthread);
304                         break;
305                 case IPI_HARDCLOCK:
306                         CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
307                         hardclockintr();
308                         break;
309                 case IPI_TLB:
310                         CTR1(KTR_SMP, "%s: IPI_TLB", __func__);
311                         cpufuncs.cf_tlb_flushID();
312                         break;
313                 default:
314                         panic("Unknown IPI 0x%0x on cpu %d", ipi, curcpu);
315                 }
316
317                 pic_ipi_clear(ipi);
318                 ipi = pic_ipi_read(-1);
319         }
320
321         return (FILTER_HANDLED);
322 }
323
324 static void
325 release_aps(void *dummy __unused)
326 {
327         uint32_t loop_counter;
328         int start = 0, end = 0;
329
330         if (mp_ncpus == 1)
331                 return;
332 #ifdef IPI_IRQ_START
333         start = IPI_IRQ_START;
334 #ifdef IPI_IRQ_END
335         end = IPI_IRQ_END;
336 #else
337         end = IPI_IRQ_START;
338 #endif
339 #endif
340
341         for (int i = start; i <= end; i++) {
342                 /*
343                  * IPI handler
344                  */
345                 /*
346                  * Use 0xdeadbeef as the argument value for irq 0,
347                  * if we used 0, the intr code will give the trap frame
348                  * pointer instead.
349                  */
350                 arm_setup_irqhandler("ipi", ipi_handler, NULL, (void *)i, i,
351                     INTR_TYPE_MISC | INTR_EXCL, NULL);
352
353                 /* Enable ipi */
354                 arm_unmask_irq(i);
355         }
356         atomic_store_rel_int(&aps_ready, 1);
357         /* Wake the other threads up */
358 #if __ARM_ARCH >= 7
359         armv7_sev();
360 #endif
361
362         printf("Release APs\n");
363
364         for (loop_counter = 0; loop_counter < 2000; loop_counter++) {
365                 if (smp_started)
366                         return;
367                 DELAY(1000);
368         }
369         printf("AP's not started\n");
370 }
371
372 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
373
374 struct cpu_group *
375 cpu_topo(void)
376 {
377
378         return (smp_topo_1level(CG_SHARE_L2, mp_ncpus, 0));
379 }
380
381 void
382 cpu_mp_setmaxid(void)
383 {
384
385         platform_mp_setmaxid();
386 }
387
388 /* Sending IPI */
389 void
390 ipi_all_but_self(u_int ipi)
391 {
392         cpuset_t other_cpus;
393
394         other_cpus = all_cpus;
395         CPU_CLR(PCPU_GET(cpuid), &other_cpus);
396         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
397         platform_ipi_send(other_cpus, ipi);
398 }
399
400 void
401 ipi_cpu(int cpu, u_int ipi)
402 {
403         cpuset_t cpus;
404
405         CPU_ZERO(&cpus);
406         CPU_SET(cpu, &cpus);
407
408         CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
409         platform_ipi_send(cpus, ipi);
410 }
411
412 void
413 ipi_selected(cpuset_t cpus, u_int ipi)
414 {
415
416         CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
417         platform_ipi_send(cpus, ipi);
418 }
419
420 void
421 tlb_broadcast(int ipi)
422 {
423
424         if (smp_started)
425                 ipi_all_but_self(ipi);
426 }