mp_machdep.c revision 183083
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 183083 2008-09-16 16:33:36Z marcel $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34#include <sys/pcpu.h>
35#include <sys/proc.h>
36#include <sys/sched.h>
37#include <sys/smp.h>
38
39#include <machine/bus.h>
40#include <machine/cpu.h>
41#include <machine/intr_machdep.h>
42#include <machine/smp.h>
43
44#include "pic_if.h"
45
46extern struct pcpu __pcpu[MAXCPU];
47
48volatile static int ap_awake;
49volatile static u_int ap_state;
50volatile static uint32_t ap_decr;
51volatile static uint32_t ap_tbl;
52
53int mp_ipi_test = 0;
54
55void
56machdep_ap_bootstrap(void)
57{
58
59	pcpup->pc_awake = 1;
60
61	while (ap_state == 0)
62		;
63
64	mtspr(SPR_TBL, 0);
65	mtspr(SPR_TBU, 0);
66	mtspr(SPR_TBL, ap_tbl);
67	__asm __volatile("mtdec %0" :: "r"(ap_decr));
68
69	ap_awake++;
70
71	/* Initialize curthread. */
72	PCPU_SET(curthread, PCPU_GET(idlethread));
73	PCPU_SET(curpcb, curthread->td_pcb);
74
75	mtmsr(mfmsr() | PSL_EE);
76	sched_throw(NULL);
77}
78
79struct cpu_group *
80cpu_topo(void)
81{
82
83	return (smp_topo_none());
84}
85
86void
87cpu_mp_setmaxid(void)
88{
89	struct cpuref cpuref;
90	int error;
91
92	mp_ncpus = 0;
93	error = powerpc_smp_first_cpu(&cpuref);
94	while (!error) {
95		mp_ncpus++;
96		error = powerpc_smp_next_cpu(&cpuref);
97	}
98	/* Sanity. */
99	if (mp_ncpus == 0)
100		mp_ncpus = 1;
101
102	/*
103	 * Set the largest cpuid we're going to use. This is necessary
104	 * for VM initialization.
105	 */
106	mp_maxid = min(mp_ncpus, MAXCPU) - 1;
107}
108
109int
110cpu_mp_probe(void)
111{
112
113	/*
114	 * We're not going to enable SMP if there's only 1 processor.
115	 */
116	return (mp_ncpus > 1);
117}
118
119void
120cpu_mp_start(void)
121{
122	struct cpuref bsp, cpu;
123	struct pcpu *pc;
124	int error;
125
126	error = powerpc_smp_get_bsp(&bsp);
127	KASSERT(error == 0, ("Don't know BSP"));
128	KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__));
129
130	error = powerpc_smp_first_cpu(&cpu);
131	while (!error) {
132		if (cpu.cr_cpuid >= MAXCPU) {
133			printf("SMP: cpu%d: skipped -- ID out of range\n",
134			    cpu.cr_cpuid);
135			goto next;
136		}
137		if (all_cpus & (1 << cpu.cr_cpuid)) {
138			printf("SMP: cpu%d: skipped - duplicate ID\n",
139			    cpu.cr_cpuid);
140			goto next;
141		}
142		if (cpu.cr_cpuid != bsp.cr_cpuid) {
143			pc = &__pcpu[cpu.cr_cpuid];
144			pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
145		} else {
146			pc = pcpup;
147			pc->pc_cpuid = bsp.cr_cpuid;
148			pc->pc_bsp = 1;
149		}
150		pc->pc_cpumask = 1 << pc->pc_cpuid;
151		pc->pc_hwref = cpu.cr_hwref;
152		all_cpus |= pc->pc_cpumask;
153
154 next:
155		error = powerpc_smp_next_cpu(&cpu);
156	}
157}
158
159void
160cpu_mp_announce(void)
161{
162	struct pcpu *pc;
163	int i;
164
165	for (i = 0; i <= mp_maxid; i++) {
166		pc = pcpu_find(i);
167		if (pc == NULL)
168			continue;
169		printf("cpu%d: dev=%x", i, pc->pc_hwref);
170		if (pc->pc_bsp)
171			printf(" (BSP)");
172		printf("\n");
173	}
174}
175
176static void
177cpu_mp_unleash(void *dummy)
178{
179	struct pcpu *pc;
180	int cpus;
181
182	if (mp_ncpus <= 1)
183		return;
184
185	if (mp_ipi_test != 1) {
186		printf("SMP: ERROR: sending of a test IPI failed\n");
187		return;
188	}
189
190	cpus = 0;
191	smp_cpus = 0;
192	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
193		cpus++;
194		pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
195		if (!pc->pc_bsp) {
196			printf("Waking up CPU %d (dev=%x)\n", pc->pc_cpuid,
197			    pc->pc_hwref);
198			powerpc_smp_start_cpu(pc);
199		} else {
200			__asm __volatile("mfspr %0,1023" : "=r"(pc->pc_pir));
201			pc->pc_awake = 1;
202		}
203		if (pc->pc_awake)
204			smp_cpus++;
205	}
206
207	ap_awake = 1;
208
209	__asm __volatile("mftb %0" : "=r"(ap_tbl));
210	ap_tbl += 10;
211	__asm __volatile("mfdec %0" : "=r"(ap_decr));
212	ap_state++;
213	powerpc_sync();
214
215	mtspr(SPR_TBL, 0);
216	mtspr(SPR_TBU, 0);
217	mtspr(SPR_TBL, ap_tbl);
218
219	while (ap_awake < smp_cpus)
220		;
221
222	if (smp_cpus != cpus || cpus != mp_ncpus) {
223		printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
224			mp_ncpus, cpus, smp_cpus);
225	}
226
227	smp_active = 1;
228	smp_started = 1;
229}
230
231SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
232
233static u_int ipi_msg_cnt[32];
234
235int
236powerpc_ipi_handler(void *arg)
237{
238	cpumask_t self;
239	uint32_t ipimask;
240	int msg;
241
242	ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
243	if (ipimask == 0)
244		return (FILTER_STRAY);
245	while ((msg = ffs(ipimask) - 1) != -1) {
246		ipimask &= ~(1u << msg);
247		ipi_msg_cnt[msg]++;
248		switch (msg) {
249		case IPI_AST:
250			break;
251		case IPI_PREEMPT:
252			sched_preempt(curthread);
253			break;
254		case IPI_RENDEZVOUS:
255			smp_rendezvous_action();
256			break;
257		case IPI_STOP:
258			self = PCPU_GET(cpumask);
259			savectx(PCPU_GET(curpcb));
260			atomic_set_int(&stopped_cpus, self);
261			while ((started_cpus & self) == 0)
262				cpu_spinwait();
263			atomic_clear_int(&started_cpus, self);
264			atomic_clear_int(&stopped_cpus, self);
265			break;
266		case IPI_PPC_TEST:
267			mp_ipi_test++;
268			break;
269		}
270	}
271
272	return (FILTER_HANDLED);
273}
274
275static void
276ipi_send(struct pcpu *pc, int ipi)
277{
278
279	atomic_set_32(&pc->pc_ipimask, (1 << ipi));
280	PIC_IPI(pic, pc->pc_cpuid);
281}
282
283/* Send an IPI to a set of cpus. */
284void
285ipi_selected(cpumask_t cpus, int ipi)
286{
287	struct pcpu *pc;
288
289	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
290		if (cpus & pc->pc_cpumask)
291			ipi_send(pc, ipi);
292	}
293}
294
295/* Send an IPI to all CPUs, including myself. */
296void
297ipi_all(int ipi)
298{
299	struct pcpu *pc;
300
301	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
302		ipi_send(pc, ipi);
303	}
304}
305
306/* Send an IPI to all CPUs EXCEPT myself. */
307void
308ipi_all_but_self(int ipi)
309{
310	struct pcpu *pc;
311
312	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
313		if (pc != pcpup)
314			ipi_send(pc, ipi);
315	}
316}
317
318/* Send an IPI to myself. */
319void
320ipi_self(int ipi)
321{
322
323	ipi_send(pcpup, ipi);
324}
325