2 * Copyright (c) 2011 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/mutex.h>
36 #include <sys/sched.h>
39 #include <sys/malloc.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
46 #include <machine/cpu.h>
47 #include <machine/smp.h>
48 #include <machine/pcb.h>
49 #include <machine/pte.h>
50 #include <machine/intr.h>
51 #include <machine/vmparam.h>
53 #include <machine/vfp.h>
59 extern struct pcpu __pcpu[];
60 /* used to hold the AP's until we are ready to release them */
61 struct mtx ap_boot_mtx;
62 struct pcb stoppcbs[MAXCPU];
64 /* # of Applications processors */
67 /* Set to 1 once we're ready to let the APs out of the pen. */
68 volatile int aps_ready = 0;
70 static int ipi_handler(void *arg);
71 void set_stackptrs(int cpu);
73 /* Temporary variables for init_secondary() */
74 void *dpcpu[MAXCPU - 1];
76 /* Determine if we running MP machine */
80 CPU_SETOF(0, &all_cpus);
82 return (platform_mp_probe());
85 /* Start Application Processor via platform specific function */
91 for (ms = 0; ms < 2000; ++ms) {
92 if ((mp_naps + 1) == mp_ncpus)
93 return (0); /* success */
101 extern unsigned char _end[];
103 /* Initialize and fire up non-boot processors */
108 vm_offset_t temp_pagetable_va;
109 vm_paddr_t addr, addr_end;
111 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
113 /* Reserve memory for application processors */
114 for(i = 0; i < (mp_ncpus - 1); i++)
115 dpcpu[i] = (void *)kmem_malloc(kernel_arena, DPCPU_SIZE,
117 temp_pagetable_va = (vm_offset_t)contigmalloc(L1_TABLE_SIZE,
118 M_TEMP, 0, 0x0, 0xffffffff, L1_TABLE_SIZE, 0);
120 addr_end = (vm_offset_t)&_end - KERNVIRTADDR + KERNPHYSADDR;
121 addr_end &= ~L1_S_OFFSET;
122 addr_end += L1_S_SIZE;
123 bzero((void *)temp_pagetable_va, L1_TABLE_SIZE);
124 for (addr = KERNPHYSADDR; addr <= addr_end; addr += L1_S_SIZE) {
125 ((int *)(temp_pagetable_va))[addr >> L1_S_SHIFT] =
126 L1_TYPE_S|L1_SHARED|L1_S_C|L1_S_AP(AP_KRW)|L1_S_DOM(PMAP_DOMAIN_KERNEL)|addr;
127 ((int *)(temp_pagetable_va))[(addr -
128 KERNPHYSADDR + KERNVIRTADDR) >> L1_S_SHIFT] =
129 L1_TYPE_S|L1_SHARED|L1_S_C|L1_S_AP(AP_KRW)|L1_S_DOM(PMAP_DOMAIN_KERNEL)|addr;
132 #if defined(CPU_MV_PJ4B)
133 /* Add ARMADAXP registers required for snoop filter initialization */
134 ((int *)(temp_pagetable_va))[0xf1000000 >> L1_S_SHIFT] =
135 L1_TYPE_S|L1_SHARED|L1_S_B|L1_S_AP(AP_KRW)|0xd0000000;
138 temp_pagetable = (void*)(vtophys(temp_pagetable_va));
139 cpu_idcache_wbinv_all();
140 cpu_l2cache_wbinv_all();
142 /* Initialize boot code and start up processors */
143 platform_mp_start_ap();
145 /* Check if ap's started properly */
148 printf("WARNING: Some AP's failed to start\n");
150 for (i = 1; i < mp_ncpus; i++)
151 CPU_SET(i, &all_cpus);
153 contigfree((void *)temp_pagetable_va, L1_TABLE_SIZE, M_TEMP);
156 /* Introduce rest of cores to the world */
158 cpu_mp_announce(void)
163 extern vm_paddr_t pmap_pa;
165 init_secondary(int cpu)
168 uint32_t loop_counter;
169 int start = 0, end = 0;
179 * pcpu_init() updates queue, so it should not be executed in parallel
182 while(mp_naps < (cpu - 1))
185 pcpu_init(pc, cpu, sizeof(struct pcpu));
186 dpcpu_init(dpcpu[cpu - 1], cpu);
188 /* Provide stack pointers for other processor modes. */
191 /* Signal our startup to BSP */
192 atomic_add_rel_32(&mp_naps, 1);
194 /* Spin until the BSP releases the APs */
198 /* Initialize curthread */
199 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
200 pc->pc_curthread = pc->pc_idlethread;
201 pc->pc_curpcb = pc->pc_idlethread->td_pcb;
208 mtx_lock_spin(&ap_boot_mtx);
210 atomic_add_rel_32(&smp_cpus, 1);
212 if (smp_cpus == mp_ncpus) {
213 /* enable IPI's, tlb shootdown, freezes etc */
214 atomic_store_rel_int(&smp_started, 1);
218 mtx_unlock_spin(&ap_boot_mtx);
222 start = IPI_IRQ_START;
230 for (int i = start; i <= end; i++)
232 enable_interrupts(I32_bit);
235 while (smp_started == 0) {
238 if (loop_counter == 1000)
239 CTR0(KTR_SMP, "AP still wait for smp_started");
241 /* Start per-CPU event timers. */
244 CTR0(KTR_SMP, "go into scheduler");
245 platform_mp_init_secondary();
247 /* Enter the scheduler */
250 panic("scheduler returned us to %s", __func__);
255 ipi_handler(void *arg)
259 cpu = PCPU_GET(cpuid);
261 ipi = pic_ipi_get((int)arg);
263 while ((ipi != 0x3ff)) {
266 CTR0(KTR_SMP, "IPI_RENDEZVOUS");
267 smp_rendezvous_action();
271 CTR0(KTR_SMP, "IPI_AST");
277 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
278 * necessary to add it in the switch.
280 CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
282 savectx(&stoppcbs[cpu]);
284 /* Indicate we are stopped */
285 CPU_SET_ATOMIC(cpu, &stopped_cpus);
287 /* Wait for restart */
288 while (!CPU_ISSET(cpu, &started_cpus))
291 CPU_CLR_ATOMIC(cpu, &started_cpus);
292 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
293 CTR0(KTR_SMP, "IPI_STOP (restart)");
296 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
297 sched_preempt(curthread);
300 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
304 CTR1(KTR_SMP, "%s: IPI_TLB", __func__);
305 cpufuncs.cf_tlb_flushID();
308 panic("Unknown IPI 0x%0x on cpu %d", ipi, curcpu);
312 ipi = pic_ipi_get(-1);
315 return (FILTER_HANDLED);
319 release_aps(void *dummy __unused)
321 uint32_t loop_counter;
322 int start = 0, end = 0;
327 start = IPI_IRQ_START;
335 for (int i = start; i <= end; i++) {
340 * Use 0xdeadbeef as the argument value for irq 0,
341 * if we used 0, the intr code will give the trap frame
344 arm_setup_irqhandler("ipi", ipi_handler, NULL, (void *)i, i,
345 INTR_TYPE_MISC | INTR_EXCL, NULL);
350 atomic_store_rel_int(&aps_ready, 1);
352 printf("Release APs\n");
354 for (loop_counter = 0; loop_counter < 2000; loop_counter++) {
359 printf("AP's not started\n");
362 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
368 return (smp_topo_1level(CG_SHARE_L2, 1, 0));
372 cpu_mp_setmaxid(void)
375 platform_mp_setmaxid();
380 ipi_all_but_self(u_int ipi)
384 other_cpus = all_cpus;
385 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
386 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
387 platform_ipi_send(other_cpus, ipi);
391 ipi_cpu(int cpu, u_int ipi)
398 CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
399 platform_ipi_send(cpus, ipi);
403 ipi_selected(cpuset_t cpus, u_int ipi)
406 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
407 platform_ipi_send(cpus, ipi);
411 tlb_broadcast(int ipi)
415 ipi_all_but_self(ipi);