mp_machdep.c revision 212170
1/*-
2 * Copyright (c) 2008 Marcel Moolenaar
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 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/powerpc/powerpc/mp_machdep.c 212170 2010-09-03 03:56:09Z grehan $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/ktr.h>
34#include <sys/bus.h>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/pcpu.h>
38#include <sys/proc.h>
39#include <sys/sched.h>
40#include <sys/smp.h>
41
42#include <vm/vm.h>
43#include <vm/vm_param.h>
44#include <vm/pmap.h>
45#include <vm/vm_map.h>
46#include <vm/vm_extern.h>
47#include <vm/vm_kern.h>
48
49#include <machine/bus.h>
50#include <machine/cpu.h>
51#include <machine/intr_machdep.h>
52#include <machine/platform.h>
53#include <machine/md_var.h>
54#include <machine/smp.h>
55
56#include "pic_if.h"
57
58extern struct pcpu __pcpu[MAXCPU];
59
60volatile static int ap_awake;
61volatile static u_int ap_letgo;
62volatile static uint32_t ap_decr;
63volatile static u_quad_t ap_timebase;
64static u_int ipi_msg_cnt[32];
65static struct mtx ap_boot_mtx;
66
67void
68machdep_ap_bootstrap(void)
69{
70	/* Set up important bits on the CPU (HID registers, etc.) */
71	cpudep_ap_setup();
72
73	/* Set PIR */
74	PCPU_SET(pir, mfspr(SPR_PIR));
75	PCPU_SET(awake, 1);
76	__asm __volatile("msync; isync");
77
78	while (ap_letgo == 0)
79		;
80
81	/* Initialize DEC and TB, sync with the BSP values */
82	decr_ap_init();
83	mttb(ap_timebase);
84	__asm __volatile("mtdec %0" :: "r"(ap_decr));
85
86	/* Serialize console output and AP count increment */
87	mtx_lock_spin(&ap_boot_mtx);
88	ap_awake++;
89	printf("SMP: AP CPU #%d launched\n", PCPU_GET(cpuid));
90	mtx_unlock_spin(&ap_boot_mtx);
91
92	/* Initialize curthread */
93	PCPU_SET(curthread, PCPU_GET(idlethread));
94	PCPU_SET(curpcb, curthread->td_pcb);
95
96	/* Let the DEC and external interrupts go */
97	mtmsr(mfmsr() | PSL_EE);
98
99	/* Announce ourselves awake, and enter the scheduler */
100	sched_throw(NULL);
101}
102
103struct cpu_group *
104cpu_topo(void)
105{
106
107	return (smp_topo_none());
108}
109
110void
111cpu_mp_setmaxid(void)
112{
113	struct cpuref cpuref;
114	int error;
115
116	mp_ncpus = 0;
117	error = platform_smp_first_cpu(&cpuref);
118	while (!error) {
119		mp_ncpus++;
120		error = platform_smp_next_cpu(&cpuref);
121	}
122	/* Sanity. */
123	if (mp_ncpus == 0)
124		mp_ncpus = 1;
125
126	/*
127	 * Set the largest cpuid we're going to use. This is necessary
128	 * for VM initialization.
129	 */
130	mp_maxid = min(mp_ncpus, MAXCPU) - 1;
131}
132
133int
134cpu_mp_probe(void)
135{
136
137	/*
138	 * We're not going to enable SMP if there's only 1 processor.
139	 */
140	return (mp_ncpus > 1);
141}
142
143void
144cpu_mp_start(void)
145{
146	struct cpuref bsp, cpu;
147	struct pcpu *pc;
148	int error;
149
150	error = platform_smp_get_bsp(&bsp);
151	KASSERT(error == 0, ("Don't know BSP"));
152	KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__));
153
154	error = platform_smp_first_cpu(&cpu);
155	while (!error) {
156		if (cpu.cr_cpuid >= MAXCPU) {
157			printf("SMP: cpu%d: skipped -- ID out of range\n",
158			    cpu.cr_cpuid);
159			goto next;
160		}
161		if (all_cpus & (1 << cpu.cr_cpuid)) {
162			printf("SMP: cpu%d: skipped - duplicate ID\n",
163			    cpu.cr_cpuid);
164			goto next;
165		}
166		if (cpu.cr_cpuid != bsp.cr_cpuid) {
167			void *dpcpu;
168
169			pc = &__pcpu[cpu.cr_cpuid];
170			dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
171			pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
172			dpcpu_init(dpcpu, cpu.cr_cpuid);
173		} else {
174			pc = pcpup;
175			pc->pc_cpuid = bsp.cr_cpuid;
176			pc->pc_bsp = 1;
177		}
178		pc->pc_cpumask = 1 << pc->pc_cpuid;
179		pc->pc_hwref = cpu.cr_hwref;
180		all_cpus |= pc->pc_cpumask;
181next:
182		error = platform_smp_next_cpu(&cpu);
183	}
184}
185
186void
187cpu_mp_announce(void)
188{
189	struct pcpu *pc;
190	int i;
191
192	for (i = 0; i <= mp_maxid; i++) {
193		pc = pcpu_find(i);
194		if (pc == NULL)
195			continue;
196		printf("cpu%d: dev=%x", i, (int)pc->pc_hwref);
197		if (pc->pc_bsp)
198			printf(" (BSP)");
199		printf("\n");
200	}
201}
202
203static void
204cpu_mp_unleash(void *dummy)
205{
206	struct pcpu *pc;
207	int cpus, timeout;
208
209	if (mp_ncpus <= 1)
210		return;
211
212	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
213
214	cpus = 0;
215	smp_cpus = 0;
216	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
217		cpus++;
218		pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
219		if (!pc->pc_bsp) {
220			if (bootverbose)
221				printf("Waking up CPU %d (dev=%x)\n",
222				    pc->pc_cpuid, (int)pc->pc_hwref);
223
224			platform_smp_start_cpu(pc);
225
226			timeout = 2000;	/* wait 2sec for the AP */
227			while (!pc->pc_awake && --timeout > 0)
228				DELAY(1000);
229
230		} else {
231			PCPU_SET(pir, mfspr(SPR_PIR));
232			pc->pc_awake = 1;
233		}
234		if (pc->pc_awake) {
235			if (bootverbose)
236				printf("Adding CPU %d, pir=%x, awake=%x\n",
237				    pc->pc_cpuid, pc->pc_pir, pc->pc_awake);
238			smp_cpus++;
239		} else
240			stopped_cpus |= (1 << pc->pc_cpuid);
241	}
242
243	ap_awake = 1;
244
245	/* Provide our current DEC and TB values for APs */
246	__asm __volatile("mfdec %0" : "=r"(ap_decr));
247	ap_timebase = mftb() + 10;
248	__asm __volatile("msync; isync");
249
250	/* Let APs continue */
251	atomic_store_rel_int(&ap_letgo, 1);
252
253	mttb(ap_timebase);
254
255	while (ap_awake < smp_cpus)
256		;
257
258	if (smp_cpus != cpus || cpus != mp_ncpus) {
259		printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
260		    mp_ncpus, cpus, smp_cpus);
261	}
262
263	/* Let the APs get into the scheduler */
264	DELAY(10000);
265
266	smp_active = 1;
267	smp_started = 1;
268}
269
270SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
271
272int
273powerpc_ipi_handler(void *arg)
274{
275	cpumask_t self;
276	uint32_t ipimask;
277	int msg;
278
279	CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr());
280
281	ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
282	if (ipimask == 0)
283		return (FILTER_STRAY);
284	while ((msg = ffs(ipimask) - 1) != -1) {
285		ipimask &= ~(1u << msg);
286		ipi_msg_cnt[msg]++;
287		switch (msg) {
288		case IPI_AST:
289			CTR1(KTR_SMP, "%s: IPI_AST", __func__);
290			break;
291		case IPI_PREEMPT:
292			CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
293			sched_preempt(curthread);
294			break;
295		case IPI_RENDEZVOUS:
296			CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__);
297			smp_rendezvous_action();
298			break;
299		case IPI_STOP:
300
301			/*
302			 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
303			 * necessary to add such case in the switch.
304			 */
305			CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)",
306			    __func__);
307			self = PCPU_GET(cpumask);
308			savectx(PCPU_GET(curpcb));
309			atomic_set_int(&stopped_cpus, self);
310			while ((started_cpus & self) == 0)
311				cpu_spinwait();
312			atomic_clear_int(&started_cpus, self);
313			atomic_clear_int(&stopped_cpus, self);
314			CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__);
315			break;
316		}
317	}
318
319	return (FILTER_HANDLED);
320}
321
322static void
323ipi_send(struct pcpu *pc, int ipi)
324{
325
326	CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__,
327	    pc, pc->pc_cpuid, ipi);
328
329	atomic_set_32(&pc->pc_ipimask, (1 << ipi));
330	PIC_IPI(root_pic, pc->pc_cpuid);
331
332	CTR1(KTR_SMP, "%s: sent", __func__);
333}
334
335/* Send an IPI to a set of cpus. */
336void
337ipi_selected(cpumask_t cpus, int ipi)
338{
339	struct pcpu *pc;
340
341	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
342		if (cpus & pc->pc_cpumask)
343			ipi_send(pc, ipi);
344	}
345}
346
347/* Send an IPI to a specific CPU. */
348void
349ipi_cpu(int cpu, u_int ipi)
350{
351
352	ipi_send(cpuid_to_pcpu[cpu], ipi);
353}
354
355/* Send an IPI to all CPUs EXCEPT myself. */
356void
357ipi_all_but_self(int ipi)
358{
359	struct pcpu *pc;
360
361	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
362		if (pc != pcpup)
363			ipi_send(pc, ipi);
364	}
365}
366