mp_machdep.c revision 282780
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: head/sys/arm/arm/mp_machdep.c 282780 2015-05-11 19:55:01Z alc $");
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
66extern struct pcpu __pcpu[];
67/* used to hold the AP's until we are ready to release them */
68struct mtx ap_boot_mtx;
69struct pcb stoppcbs[MAXCPU];
70
71/* # of Applications processors */
72volatile int mp_naps;
73
74/* Set to 1 once we're ready to let the APs out of the pen. */
75volatile int aps_ready = 0;
76
77static int ipi_handler(void *arg);
78void set_stackptrs(int cpu);
79
80/* Temporary variables for init_secondary()  */
81void *dpcpu[MAXCPU - 1];
82
83/* Determine if we running MP machine */
84int
85cpu_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 */
93static int
94check_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
108extern unsigned char _end[];
109
110/* Initialize and fire up non-boot processors */
111void
112cpu_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 */
141void
142cpu_mp_announce(void)
143{
144
145}
146
147extern vm_paddr_t pmap_pa;
148void
149init_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 (!aps_ready)
189		;
190
191	/* Initialize curthread */
192	KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
193	pc->pc_curthread = pc->pc_idlethread;
194	pc->pc_curpcb = pc->pc_idlethread->td_pcb;
195	set_curthread(pc->pc_idlethread);
196#ifdef VFP
197	pc->pc_cpu = cpu;
198
199	vfp_init();
200#endif
201
202	mtx_lock_spin(&ap_boot_mtx);
203
204	atomic_add_rel_32(&smp_cpus, 1);
205
206	if (smp_cpus == mp_ncpus) {
207		/* enable IPI's, tlb shootdown, freezes etc */
208		atomic_store_rel_int(&smp_started, 1);
209	}
210
211	mtx_unlock_spin(&ap_boot_mtx);
212
213	/* Enable ipi */
214#ifdef IPI_IRQ_START
215	start = IPI_IRQ_START;
216#ifdef IPI_IRQ_END
217  	end = IPI_IRQ_END;
218#else
219	end = IPI_IRQ_START;
220#endif
221#endif
222
223	for (int i = start; i <= end; i++)
224		arm_unmask_irq(i);
225	enable_interrupts(PSR_I);
226
227	loop_counter = 0;
228	while (smp_started == 0) {
229		DELAY(100);
230		loop_counter++;
231		if (loop_counter == 1000)
232			CTR0(KTR_SMP, "AP still wait for smp_started");
233	}
234	/* Start per-CPU event timers. */
235	cpu_initclocks_ap();
236
237	CTR0(KTR_SMP, "go into scheduler");
238	platform_mp_init_secondary();
239
240	/* Enter the scheduler */
241	sched_throw(NULL);
242
243	panic("scheduler returned us to %s", __func__);
244	/* NOTREACHED */
245}
246
247static int
248ipi_handler(void *arg)
249{
250	u_int	cpu, ipi;
251
252	cpu = PCPU_GET(cpuid);
253
254	ipi = pic_ipi_read((int)arg);
255
256	while ((ipi != 0x3ff)) {
257		switch (ipi) {
258		case IPI_RENDEZVOUS:
259			CTR0(KTR_SMP, "IPI_RENDEZVOUS");
260			smp_rendezvous_action();
261			break;
262
263		case IPI_AST:
264			CTR0(KTR_SMP, "IPI_AST");
265			break;
266
267		case IPI_STOP:
268			/*
269			 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
270			 * necessary to add it in the switch.
271			 */
272			CTR0(KTR_SMP, "IPI_STOP or IPI_STOP_HARD");
273
274			savectx(&stoppcbs[cpu]);
275
276			/*
277			 * CPUs are stopped when entering the debugger and at
278			 * system shutdown, both events which can precede a
279			 * panic dump.  For the dump to be correct, all caches
280			 * must be flushed and invalidated, but on ARM there's
281			 * no way to broadcast a wbinv_all to other cores.
282			 * Instead, we have each core do the local wbinv_all as
283			 * part of stopping the core.  The core requesting the
284			 * stop will do the l2 cache flush after all other cores
285			 * have done their l1 flushes and stopped.
286			 */
287			cpu_idcache_wbinv_all();
288
289			/* Indicate we are stopped */
290			CPU_SET_ATOMIC(cpu, &stopped_cpus);
291
292			/* Wait for restart */
293			while (!CPU_ISSET(cpu, &started_cpus))
294				cpu_spinwait();
295
296			CPU_CLR_ATOMIC(cpu, &started_cpus);
297			CPU_CLR_ATOMIC(cpu, &stopped_cpus);
298			CTR0(KTR_SMP, "IPI_STOP (restart)");
299			break;
300		case IPI_PREEMPT:
301			CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
302			sched_preempt(curthread);
303			break;
304		case IPI_HARDCLOCK:
305			CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
306			hardclockintr();
307			break;
308		case IPI_TLB:
309			CTR1(KTR_SMP, "%s: IPI_TLB", __func__);
310			cpufuncs.cf_tlb_flushID();
311			break;
312		default:
313			panic("Unknown IPI 0x%0x on cpu %d", ipi, curcpu);
314		}
315
316		pic_ipi_clear(ipi);
317		ipi = pic_ipi_read(-1);
318	}
319
320	return (FILTER_HANDLED);
321}
322
323static void
324release_aps(void *dummy __unused)
325{
326	uint32_t loop_counter;
327	int start = 0, end = 0;
328
329	if (mp_ncpus == 1)
330		return;
331#ifdef IPI_IRQ_START
332	start = IPI_IRQ_START;
333#ifdef IPI_IRQ_END
334	end = IPI_IRQ_END;
335#else
336	end = IPI_IRQ_START;
337#endif
338#endif
339
340	for (int i = start; i <= end; i++) {
341		/*
342		 * IPI handler
343		 */
344		/*
345		 * Use 0xdeadbeef as the argument value for irq 0,
346		 * if we used 0, the intr code will give the trap frame
347		 * pointer instead.
348		 */
349		arm_setup_irqhandler("ipi", ipi_handler, NULL, (void *)i, i,
350		    INTR_TYPE_MISC | INTR_EXCL, NULL);
351
352		/* Enable ipi */
353		arm_unmask_irq(i);
354	}
355	atomic_store_rel_int(&aps_ready, 1);
356
357	printf("Release APs\n");
358
359	for (loop_counter = 0; loop_counter < 2000; loop_counter++) {
360		if (smp_started)
361			return;
362		DELAY(1000);
363	}
364	printf("AP's not started\n");
365}
366
367SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
368
369struct cpu_group *
370cpu_topo(void)
371{
372
373	return (smp_topo_1level(CG_SHARE_L2, mp_ncpus, 0));
374}
375
376void
377cpu_mp_setmaxid(void)
378{
379
380	platform_mp_setmaxid();
381}
382
383/* Sending IPI */
384void
385ipi_all_but_self(u_int ipi)
386{
387	cpuset_t other_cpus;
388
389	other_cpus = all_cpus;
390	CPU_CLR(PCPU_GET(cpuid), &other_cpus);
391	CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
392	platform_ipi_send(other_cpus, ipi);
393}
394
395void
396ipi_cpu(int cpu, u_int ipi)
397{
398	cpuset_t cpus;
399
400	CPU_ZERO(&cpus);
401	CPU_SET(cpu, &cpus);
402
403	CTR3(KTR_SMP, "%s: cpu: %d, ipi: %x", __func__, cpu, ipi);
404	platform_ipi_send(cpus, ipi);
405}
406
407void
408ipi_selected(cpuset_t cpus, u_int ipi)
409{
410
411	CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
412	platform_ipi_send(cpus, ipi);
413}
414
415void
416tlb_broadcast(int ipi)
417{
418
419	if (smp_started)
420		ipi_all_but_self(ipi);
421}
422