mp_machdep.c revision 183030
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 183030 2008-09-15 01:03:16Z 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;
51
52int mp_ipi_test = 0;
53
54void
55machdep_ap_bootstrap(volatile uint32_t *trcp)
56{
57
58	trcp[0] = 0x3000;
59	trcp[1] = (uint32_t)&machdep_ap_bootstrap;
60
61	// __asm __volatile("mtspr 1023,%0" :: "r"(PCPU_GET(cpuid)));
62	__asm __volatile("mfspr %0,1023" : "=r"(pcpup->pc_pir));
63	pcpup->pc_awake = 1;
64
65	while (ap_state == 0)
66		;
67
68	__asm __volatile("mtdec %0" :: "r"(ap_decr));
69
70	ap_awake++;
71
72	/* Initialize curthread. */
73	PCPU_SET(curthread, PCPU_GET(idlethread));
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	__asm __volatile("mfdec %0" : "=r"(ap_decr));
209	ap_state++;
210
211	while (ap_awake < smp_cpus)
212		;
213
214	if (smp_cpus != cpus || cpus != mp_ncpus) {
215		printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
216			mp_ncpus, cpus, smp_cpus);
217	}
218
219	smp_active = 1;
220	smp_started = 1;
221}
222
223SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
224
225static u_int ipi_msg_cnt[32];
226
227int
228powerpc_ipi_handler(void *arg)
229{
230	cpumask_t self;
231	uint32_t ipimask;
232	int msg;
233
234	ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
235	if (ipimask == 0)
236		return (FILTER_STRAY);
237	while ((msg = ffs(ipimask) - 1) != -1) {
238		ipimask &= ~(1u << msg);
239		ipi_msg_cnt[msg]++;
240		switch (msg) {
241		case IPI_AST:
242			break;
243		case IPI_PREEMPT:
244			sched_preempt(curthread);
245			break;
246		case IPI_RENDEZVOUS:
247			smp_rendezvous_action();
248			break;
249		case IPI_STOP:
250			self = PCPU_GET(cpumask);
251			savectx(PCPU_GET(curpcb));
252			atomic_set_int(&stopped_cpus, self);
253			while ((started_cpus & self) == 0)
254				cpu_spinwait();
255			atomic_clear_int(&started_cpus, self);
256			atomic_clear_int(&stopped_cpus, self);
257			break;
258		case IPI_PPC_TEST:
259			mp_ipi_test++;
260			break;
261		}
262	}
263
264	return (FILTER_HANDLED);
265}
266
267static void
268ipi_send(struct pcpu *pc, int ipi)
269{
270
271	atomic_set_32(&pc->pc_ipimask, (1 << ipi));
272	PIC_IPI(pic, pc->pc_cpuid);
273}
274
275/* Send an IPI to a set of cpus. */
276void
277ipi_selected(cpumask_t cpus, int ipi)
278{
279	struct pcpu *pc;
280
281	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
282		if (cpus & pc->pc_cpumask)
283			ipi_send(pc, ipi);
284	}
285}
286
287/* Send an IPI to all CPUs, including myself. */
288void
289ipi_all(int ipi)
290{
291	struct pcpu *pc;
292
293	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
294		ipi_send(pc, ipi);
295	}
296}
297
298/* Send an IPI to all CPUs EXCEPT myself. */
299void
300ipi_all_but_self(int ipi)
301{
302	struct pcpu *pc;
303
304	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
305		if (pc != pcpup)
306			ipi_send(pc, ipi);
307	}
308}
309
310/* Send an IPI to myself. */
311void
312ipi_self(int ipi)
313{
314
315	ipi_send(pcpup, ipi);
316}
317