xref: /freebsd/sys/powerpc/powerpc/mp_machdep.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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$");
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 
58 extern struct pcpu __pcpu[MAXCPU];
59 
60 volatile static int ap_awake;
61 volatile static u_int ap_letgo;
62 volatile static u_quad_t ap_timebase;
63 static u_int ipi_msg_cnt[32];
64 static struct mtx ap_boot_mtx;
65 
66 void
67 machdep_ap_bootstrap(void)
68 {
69 	/* Set up important bits on the CPU (HID registers, etc.) */
70 	cpudep_ap_setup();
71 
72 	/* Set PIR */
73 	PCPU_SET(pir, mfspr(SPR_PIR));
74 	PCPU_SET(awake, 1);
75 	__asm __volatile("msync; isync");
76 
77 	while (ap_letgo == 0)
78 		;
79 
80 	/* Initialize DEC and TB, sync with the BSP values */
81 	mttb(ap_timebase);
82 	decr_ap_init();
83 
84 	/* Serialize console output and AP count increment */
85 	mtx_lock_spin(&ap_boot_mtx);
86 	ap_awake++;
87 	printf("SMP: AP CPU #%d launched\n", PCPU_GET(cpuid));
88 	mtx_unlock_spin(&ap_boot_mtx);
89 
90 	/* Initialize curthread */
91 	PCPU_SET(curthread, PCPU_GET(idlethread));
92 	PCPU_SET(curpcb, curthread->td_pcb);
93 
94 	/* Start per-CPU event timers. */
95 	cpu_initclocks_ap();
96 
97 	/* Announce ourselves awake, and enter the scheduler */
98 	sched_throw(NULL);
99 }
100 
101 struct cpu_group *
102 cpu_topo(void)
103 {
104 
105 	return (smp_topo_none());
106 }
107 
108 void
109 cpu_mp_setmaxid(void)
110 {
111 	struct cpuref cpuref;
112 	int error;
113 
114 	mp_ncpus = 0;
115 	error = platform_smp_first_cpu(&cpuref);
116 	while (!error) {
117 		mp_ncpus++;
118 		error = platform_smp_next_cpu(&cpuref);
119 	}
120 	/* Sanity. */
121 	if (mp_ncpus == 0)
122 		mp_ncpus = 1;
123 
124 	/*
125 	 * Set the largest cpuid we're going to use. This is necessary
126 	 * for VM initialization.
127 	 */
128 	mp_maxid = min(mp_ncpus, MAXCPU) - 1;
129 }
130 
131 int
132 cpu_mp_probe(void)
133 {
134 
135 	/*
136 	 * We're not going to enable SMP if there's only 1 processor.
137 	 */
138 	return (mp_ncpus > 1);
139 }
140 
141 void
142 cpu_mp_start(void)
143 {
144 	struct cpuref bsp, cpu;
145 	struct pcpu *pc;
146 	int error;
147 
148 	error = platform_smp_get_bsp(&bsp);
149 	KASSERT(error == 0, ("Don't know BSP"));
150 	KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__));
151 
152 	error = platform_smp_first_cpu(&cpu);
153 	while (!error) {
154 		if (cpu.cr_cpuid >= MAXCPU) {
155 			printf("SMP: cpu%d: skipped -- ID out of range\n",
156 			    cpu.cr_cpuid);
157 			goto next;
158 		}
159 		if (all_cpus & (1 << cpu.cr_cpuid)) {
160 			printf("SMP: cpu%d: skipped - duplicate ID\n",
161 			    cpu.cr_cpuid);
162 			goto next;
163 		}
164 		if (cpu.cr_cpuid != bsp.cr_cpuid) {
165 			void *dpcpu;
166 
167 			pc = &__pcpu[cpu.cr_cpuid];
168 			dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
169 			pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
170 			dpcpu_init(dpcpu, cpu.cr_cpuid);
171 		} else {
172 			pc = pcpup;
173 			pc->pc_cpuid = bsp.cr_cpuid;
174 			pc->pc_bsp = 1;
175 		}
176 		pc->pc_cpumask = 1 << pc->pc_cpuid;
177 		pc->pc_hwref = cpu.cr_hwref;
178 		all_cpus |= pc->pc_cpumask;
179 next:
180 		error = platform_smp_next_cpu(&cpu);
181 	}
182 }
183 
184 void
185 cpu_mp_announce(void)
186 {
187 	struct pcpu *pc;
188 	int i;
189 
190 	for (i = 0; i <= mp_maxid; i++) {
191 		pc = pcpu_find(i);
192 		if (pc == NULL)
193 			continue;
194 		printf("cpu%d: dev=%x", i, (int)pc->pc_hwref);
195 		if (pc->pc_bsp)
196 			printf(" (BSP)");
197 		printf("\n");
198 	}
199 }
200 
201 static void
202 cpu_mp_unleash(void *dummy)
203 {
204 	struct pcpu *pc;
205 	int cpus, timeout;
206 
207 	if (mp_ncpus <= 1)
208 		return;
209 
210 	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
211 
212 	cpus = 0;
213 	smp_cpus = 0;
214 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
215 		cpus++;
216 		pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
217 		if (!pc->pc_bsp) {
218 			if (bootverbose)
219 				printf("Waking up CPU %d (dev=%x)\n",
220 				    pc->pc_cpuid, (int)pc->pc_hwref);
221 
222 			platform_smp_start_cpu(pc);
223 
224 			timeout = 2000;	/* wait 2sec for the AP */
225 			while (!pc->pc_awake && --timeout > 0)
226 				DELAY(1000);
227 
228 		} else {
229 			PCPU_SET(pir, mfspr(SPR_PIR));
230 			pc->pc_awake = 1;
231 		}
232 		if (pc->pc_awake) {
233 			if (bootverbose)
234 				printf("Adding CPU %d, pir=%x, awake=%x\n",
235 				    pc->pc_cpuid, pc->pc_pir, pc->pc_awake);
236 			smp_cpus++;
237 		} else
238 			stopped_cpus |= (1 << pc->pc_cpuid);
239 	}
240 
241 	ap_awake = 1;
242 
243 	/* Provide our current DEC and TB values for APs */
244 	ap_timebase = mftb() + 10;
245 	__asm __volatile("msync; isync");
246 
247 	/* Let APs continue */
248 	atomic_store_rel_int(&ap_letgo, 1);
249 
250 	mttb(ap_timebase);
251 
252 	while (ap_awake < smp_cpus)
253 		;
254 
255 	if (smp_cpus != cpus || cpus != mp_ncpus) {
256 		printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
257 		    mp_ncpus, cpus, smp_cpus);
258 	}
259 
260 	/* Let the APs get into the scheduler */
261 	DELAY(10000);
262 
263 	smp_active = 1;
264 	smp_started = 1;
265 }
266 
267 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
268 
269 int
270 powerpc_ipi_handler(void *arg)
271 {
272 	cpumask_t self;
273 	uint32_t ipimask;
274 	int msg;
275 
276 	CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr());
277 
278 	ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
279 	if (ipimask == 0)
280 		return (FILTER_STRAY);
281 	while ((msg = ffs(ipimask) - 1) != -1) {
282 		ipimask &= ~(1u << msg);
283 		ipi_msg_cnt[msg]++;
284 		switch (msg) {
285 		case IPI_AST:
286 			CTR1(KTR_SMP, "%s: IPI_AST", __func__);
287 			break;
288 		case IPI_PREEMPT:
289 			CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
290 			sched_preempt(curthread);
291 			break;
292 		case IPI_RENDEZVOUS:
293 			CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__);
294 			smp_rendezvous_action();
295 			break;
296 		case IPI_STOP:
297 
298 			/*
299 			 * IPI_STOP_HARD is mapped to IPI_STOP so it is not
300 			 * necessary to add such case in the switch.
301 			 */
302 			CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)",
303 			    __func__);
304 			self = PCPU_GET(cpumask);
305 			savectx(PCPU_GET(curpcb));
306 			atomic_set_int(&stopped_cpus, self);
307 			while ((started_cpus & self) == 0)
308 				cpu_spinwait();
309 			atomic_clear_int(&started_cpus, self);
310 			atomic_clear_int(&stopped_cpus, self);
311 			CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__);
312 			break;
313 		case IPI_HARDCLOCK:
314 			CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
315 			hardclockintr();
316 			break;
317 		}
318 	}
319 
320 	return (FILTER_HANDLED);
321 }
322 
323 static void
324 ipi_send(struct pcpu *pc, int ipi)
325 {
326 
327 	CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__,
328 	    pc, pc->pc_cpuid, ipi);
329 
330 	atomic_set_32(&pc->pc_ipimask, (1 << ipi));
331 	PIC_IPI(root_pic, pc->pc_cpuid);
332 
333 	CTR1(KTR_SMP, "%s: sent", __func__);
334 }
335 
336 /* Send an IPI to a set of cpus. */
337 void
338 ipi_selected(cpumask_t cpus, int ipi)
339 {
340 	struct pcpu *pc;
341 
342 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
343 		if (cpus & pc->pc_cpumask)
344 			ipi_send(pc, ipi);
345 	}
346 }
347 
348 /* Send an IPI to a specific CPU. */
349 void
350 ipi_cpu(int cpu, u_int ipi)
351 {
352 
353 	ipi_send(cpuid_to_pcpu[cpu], ipi);
354 }
355 
356 /* Send an IPI to all CPUs EXCEPT myself. */
357 void
358 ipi_all_but_self(int ipi)
359 {
360 	struct pcpu *pc;
361 
362 	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
363 		if (pc != pcpup)
364 			ipi_send(pc, ipi);
365 	}
366 }
367