xref: /netbsd-src/sys/arch/powerpc/oea/cpu_subr.c (revision ca453df649ce9db45b64d73678ba06cbccf9aa11)
1 /*	$NetBSD: cpu_subr.c,v 1.70 2011/06/29 06:00:17 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Matt Thomas.
5  * Copyright (c) 2001 Tsubai Masanari.
6  * Copyright (c) 1998, 1999, 2001 Internet Research Institute, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by
20  *	Internet Research Institute, Inc.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: cpu_subr.c,v 1.70 2011/06/29 06:00:17 matt Exp $");
38 
39 #include "opt_ppcparam.h"
40 #include "opt_multiprocessor.h"
41 #include "opt_altivec.h"
42 #include "sysmon_envsys.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/types.h>
48 #include <sys/lwp.h>
49 #include <sys/malloc.h>
50 #include <sys/xcall.h>
51 
52 #include <uvm/uvm.h>
53 
54 #include <powerpc/pcb.h>
55 #include <powerpc/psl.h>
56 #include <powerpc/spr.h>
57 #include <powerpc/oea/hid.h>
58 #include <powerpc/oea/hid_601.h>
59 #include <powerpc/oea/spr.h>
60 #include <powerpc/oea/cpufeat.h>
61 
62 #include <dev/sysmon/sysmonvar.h>
63 
64 static void cpu_enable_l2cr(register_t);
65 static void cpu_enable_l3cr(register_t);
66 static void cpu_config_l2cr(int);
67 static void cpu_config_l3cr(int);
68 static void cpu_probe_speed(struct cpu_info *);
69 static void cpu_idlespin(void);
70 static void cpu_set_dfs_xcall(void *, void *);
71 #if NSYSMON_ENVSYS > 0
72 static void cpu_tau_setup(struct cpu_info *);
73 static void cpu_tau_refresh(struct sysmon_envsys *, envsys_data_t *);
74 #endif
75 
76 int cpu;
77 int ncpus;
78 
79 struct fmttab {
80 	register_t fmt_mask;
81 	register_t fmt_value;
82 	const char *fmt_string;
83 };
84 
85 /*
86  * This should be one per CPU but since we only support it on 750 variants it
87  * doesn't realy matter since none of them supports SMP
88  */
89 envsys_data_t sensor;
90 
91 static const struct fmttab cpu_7450_l2cr_formats[] = {
92 	{ L2CR_L2E, 0, " disabled" },
93 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
94 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
95 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
96 	{ L2CR_L2E, ~0, " 256KB L2 cache" },
97 	{ L2CR_L2PE, 0, " no parity" },
98 	{ L2CR_L2PE, ~0, " parity enabled" },
99 	{ 0, 0, NULL }
100 };
101 
102 static const struct fmttab cpu_7448_l2cr_formats[] = {
103 	{ L2CR_L2E, 0, " disabled" },
104 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
105 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
106 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
107 	{ L2CR_L2E, ~0, " 1MB L2 cache" },
108 	{ L2CR_L2PE, 0, " no parity" },
109 	{ L2CR_L2PE, ~0, " parity enabled" },
110 	{ 0, 0, NULL }
111 };
112 
113 static const struct fmttab cpu_7457_l2cr_formats[] = {
114 	{ L2CR_L2E, 0, " disabled" },
115 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
116 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
117 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
118 	{ L2CR_L2E, ~0, " 512KB L2 cache" },
119 	{ L2CR_L2PE, 0, " no parity" },
120 	{ L2CR_L2PE, ~0, " parity enabled" },
121 	{ 0, 0, NULL }
122 };
123 
124 static const struct fmttab cpu_7450_l3cr_formats[] = {
125 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3DO, " data-only" },
126 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3IO, " instruction-only" },
127 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3DO|L3CR_L3IO, " locked" },
128 	{ L3CR_L3SIZ, L3SIZ_2M, " 2MB" },
129 	{ L3CR_L3SIZ, L3SIZ_1M, " 1MB" },
130 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3PE|L3CR_L3APE, " parity" },
131 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3PE, " data-parity" },
132 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3APE, " address-parity" },
133 	{ L3CR_L3PE|L3CR_L3APE, 0, " no-parity" },
134 	{ L3CR_L3SIZ, ~0, " L3 cache" },
135 	{ L3CR_L3RT, L3RT_MSUG2_DDR, " (DDR SRAM)" },
136 	{ L3CR_L3RT, L3RT_PIPELINE_LATE, " (LW SRAM)" },
137 	{ L3CR_L3RT, L3RT_PB2_SRAM, " (PB2 SRAM)" },
138 	{ L3CR_L3CLK, ~0, " at" },
139 	{ L3CR_L3CLK, L3CLK_20, " 2:1" },
140 	{ L3CR_L3CLK, L3CLK_25, " 2.5:1" },
141 	{ L3CR_L3CLK, L3CLK_30, " 3:1" },
142 	{ L3CR_L3CLK, L3CLK_35, " 3.5:1" },
143 	{ L3CR_L3CLK, L3CLK_40, " 4:1" },
144 	{ L3CR_L3CLK, L3CLK_50, " 5:1" },
145 	{ L3CR_L3CLK, L3CLK_60, " 6:1" },
146 	{ L3CR_L3CLK, ~0, " ratio" },
147 	{ 0, 0, NULL },
148 };
149 
150 static const struct fmttab cpu_ibm750_l2cr_formats[] = {
151 	{ L2CR_L2E, 0, " disabled" },
152 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
153 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
154 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
155 	{ 0, ~0, " 512KB" },
156 	{ L2CR_L2WT, L2CR_L2WT, " WT" },
157 	{ L2CR_L2WT, 0, " WB" },
158 	{ L2CR_L2PE, L2CR_L2PE, " with ECC" },
159 	{ 0, ~0, " L2 cache" },
160 	{ 0, 0, NULL }
161 };
162 
163 static const struct fmttab cpu_l2cr_formats[] = {
164 	{ L2CR_L2E, 0, " disabled" },
165 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
166 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
167 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
168 	{ L2CR_L2PE, L2CR_L2PE, " parity" },
169 	{ L2CR_L2PE, 0, " no-parity" },
170 	{ L2CR_L2SIZ, L2SIZ_2M, " 2MB" },
171 	{ L2CR_L2SIZ, L2SIZ_1M, " 1MB" },
172 	{ L2CR_L2SIZ, L2SIZ_512K, " 512KB" },
173 	{ L2CR_L2SIZ, L2SIZ_256K, " 256KB" },
174 	{ L2CR_L2WT, L2CR_L2WT, " WT" },
175 	{ L2CR_L2WT, 0, " WB" },
176 	{ L2CR_L2E, ~0, " L2 cache" },
177 	{ L2CR_L2RAM, L2RAM_FLOWTHRU_BURST, " (FB SRAM)" },
178 	{ L2CR_L2RAM, L2RAM_PIPELINE_LATE, " (LW SRAM)" },
179 	{ L2CR_L2RAM, L2RAM_PIPELINE_BURST, " (PB SRAM)" },
180 	{ L2CR_L2CLK, ~0, " at" },
181 	{ L2CR_L2CLK, L2CLK_10, " 1:1" },
182 	{ L2CR_L2CLK, L2CLK_15, " 1.5:1" },
183 	{ L2CR_L2CLK, L2CLK_20, " 2:1" },
184 	{ L2CR_L2CLK, L2CLK_25, " 2.5:1" },
185 	{ L2CR_L2CLK, L2CLK_30, " 3:1" },
186 	{ L2CR_L2CLK, L2CLK_35, " 3.5:1" },
187 	{ L2CR_L2CLK, L2CLK_40, " 4:1" },
188 	{ L2CR_L2CLK, ~0, " ratio" },
189 	{ 0, 0, NULL }
190 };
191 
192 static void cpu_fmttab_print(const struct fmttab *, register_t);
193 
194 struct cputab {
195 	const char name[8];
196 	uint16_t version;
197 	uint16_t revfmt;
198 };
199 #define	REVFMT_MAJMIN	1		/* %u.%u */
200 #define	REVFMT_HEX	2		/* 0x%04x */
201 #define	REVFMT_DEC	3		/* %u */
202 static const struct cputab models[] = {
203 	{ "601",	MPC601,		REVFMT_DEC },
204 	{ "602",	MPC602,		REVFMT_DEC },
205 	{ "603",	MPC603,		REVFMT_MAJMIN },
206 	{ "603e",	MPC603e,	REVFMT_MAJMIN },
207 	{ "603ev",	MPC603ev,	REVFMT_MAJMIN },
208 	{ "G2",		MPCG2,		REVFMT_MAJMIN },
209 	{ "604",	MPC604,		REVFMT_MAJMIN },
210 	{ "604e",	MPC604e,	REVFMT_MAJMIN },
211 	{ "604ev",	MPC604ev,	REVFMT_MAJMIN },
212 	{ "620",	MPC620,  	REVFMT_HEX },
213 	{ "750",	MPC750,		REVFMT_MAJMIN },
214 	{ "750FX",	IBM750FX,	REVFMT_MAJMIN },
215 	{ "750GX",	IBM750GX,	REVFMT_MAJMIN },
216 	{ "7400",	MPC7400,	REVFMT_MAJMIN },
217 	{ "7410",	MPC7410,	REVFMT_MAJMIN },
218 	{ "7450",	MPC7450,	REVFMT_MAJMIN },
219 	{ "7455",	MPC7455,	REVFMT_MAJMIN },
220 	{ "7457",	MPC7457,	REVFMT_MAJMIN },
221 	{ "7447A",	MPC7447A,	REVFMT_MAJMIN },
222 	{ "7448",	MPC7448,	REVFMT_MAJMIN },
223 	{ "8240",	MPC8240,	REVFMT_MAJMIN },
224 	{ "8245",	MPC8245,	REVFMT_MAJMIN },
225 	{ "970",	IBM970,		REVFMT_MAJMIN },
226 	{ "970FX",	IBM970FX,	REVFMT_MAJMIN },
227 	{ "970MP",	IBM970MP,	REVFMT_MAJMIN },
228 	{ "POWER3II",   IBMPOWER3II,    REVFMT_MAJMIN },
229 	{ "",		0,		REVFMT_HEX }
230 };
231 
232 #ifdef MULTIPROCESSOR
233 struct cpu_info cpu_info[CPU_MAXNUM] = {
234     [0] = {
235 	.ci_curlwp = &lwp0,
236     },
237 };
238 volatile struct cpu_hatch_data *cpu_hatch_data;
239 volatile int cpu_hatch_stack;
240 extern int ticks_per_intr;
241 #include <powerpc/oea/bat.h>
242 #include <powerpc/pic/picvar.h>
243 #include <powerpc/pic/ipivar.h>
244 extern struct bat battable[];
245 #else
246 struct cpu_info cpu_info[1] = {
247     [0] = {
248 	.ci_curlwp = &lwp0,
249     },
250 };
251 #endif /*MULTIPROCESSOR*/
252 
253 int cpu_altivec;
254 register_t cpu_psluserset;
255 register_t cpu_pslusermod;
256 register_t cpu_pslusermask = 0xffff;
257 char cpu_model[80];
258 
259 /* This is to be called from locore.S, and nowhere else. */
260 
261 void
262 cpu_model_init(void)
263 {
264 	u_int pvr, vers;
265 
266 	pvr = mfpvr();
267 	vers = pvr >> 16;
268 
269 	oeacpufeat = 0;
270 
271 	if ((vers >= IBMRS64II && vers <= IBM970GX) || vers == MPC620 ||
272 		vers == IBMCELL || vers == IBMPOWER6P5)
273 		oeacpufeat |= OEACPU_64 | OEACPU_64_BRIDGE | OEACPU_NOBAT;
274 
275 	else if (vers == MPC601)
276 		oeacpufeat |= OEACPU_601;
277 
278 	else if (MPC745X_P(vers) && vers != MPC7450)
279 		oeacpufeat |= OEACPU_XBSEN | OEACPU_HIGHBAT | OEACPU_HIGHSPRG;
280 
281 	else if (vers == IBM750FX || vers == IBM750GX)
282 		oeacpufeat |= OEACPU_HIGHBAT;
283 }
284 
285 void
286 cpu_fmttab_print(const struct fmttab *fmt, register_t data)
287 {
288 	for (; fmt->fmt_mask != 0 || fmt->fmt_value != 0; fmt++) {
289 		if ((~fmt->fmt_mask & fmt->fmt_value) != 0 ||
290 		    (data & fmt->fmt_mask) == fmt->fmt_value)
291 			aprint_normal("%s", fmt->fmt_string);
292 	}
293 }
294 
295 void
296 cpu_idlespin(void)
297 {
298 	register_t msr;
299 
300 	if (powersave <= 0)
301 		return;
302 
303 	__asm volatile(
304 		"sync;"
305 		"mfmsr	%0;"
306 		"oris	%0,%0,%1@h;"	/* enter power saving mode */
307 		"mtmsr	%0;"
308 		"isync;"
309 	    :	"=r"(msr)
310 	    :	"J"(PSL_POW));
311 }
312 
313 void
314 cpu_probe_cache(void)
315 {
316 	u_int assoc, pvr, vers;
317 
318 	pvr = mfpvr();
319 	vers = pvr >> 16;
320 
321 
322 	/* Presently common across almost all implementations. */
323 	curcpu()->ci_ci.dcache_line_size = 32;
324 	curcpu()->ci_ci.icache_line_size = 32;
325 
326 
327 	switch (vers) {
328 #define	K	*1024
329 	case IBM750FX:
330 	case IBM750GX:
331 	case MPC601:
332 	case MPC750:
333 	case MPC7400:
334 	case MPC7447A:
335 	case MPC7448:
336 	case MPC7450:
337 	case MPC7455:
338 	case MPC7457:
339 		curcpu()->ci_ci.dcache_size = 32 K;
340 		curcpu()->ci_ci.icache_size = 32 K;
341 		assoc = 8;
342 		break;
343 	case MPC603:
344 		curcpu()->ci_ci.dcache_size = 8 K;
345 		curcpu()->ci_ci.icache_size = 8 K;
346 		assoc = 2;
347 		break;
348 	case MPC603e:
349 	case MPC603ev:
350 	case MPC604:
351 	case MPC8240:
352 	case MPC8245:
353 	case MPCG2:
354 		curcpu()->ci_ci.dcache_size = 16 K;
355 		curcpu()->ci_ci.icache_size = 16 K;
356 		assoc = 4;
357 		break;
358 	case MPC604e:
359 	case MPC604ev:
360 		curcpu()->ci_ci.dcache_size = 32 K;
361 		curcpu()->ci_ci.icache_size = 32 K;
362 		assoc = 4;
363 		break;
364 	case IBMPOWER3II:
365 		curcpu()->ci_ci.dcache_size = 64 K;
366 		curcpu()->ci_ci.icache_size = 32 K;
367 		curcpu()->ci_ci.dcache_line_size = 128;
368 		curcpu()->ci_ci.icache_line_size = 128;
369 		assoc = 128; /* not a typo */
370 		break;
371 	case IBM970:
372 	case IBM970FX:
373 	case IBM970MP:
374 		curcpu()->ci_ci.dcache_size = 32 K;
375 		curcpu()->ci_ci.icache_size = 64 K;
376 		curcpu()->ci_ci.dcache_line_size = 128;
377 		curcpu()->ci_ci.icache_line_size = 128;
378 		assoc = 2;
379 		break;
380 
381 	default:
382 		curcpu()->ci_ci.dcache_size = PAGE_SIZE;
383 		curcpu()->ci_ci.icache_size = PAGE_SIZE;
384 		assoc = 1;
385 #undef	K
386 	}
387 
388 	/*
389 	 * Possibly recolor.
390 	 */
391 	uvm_page_recolor(atop(curcpu()->ci_ci.dcache_size / assoc));
392 }
393 
394 struct cpu_info *
395 cpu_attach_common(device_t self, int id)
396 {
397 	struct cpu_info *ci;
398 	u_int pvr, vers;
399 
400 	ci = &cpu_info[id];
401 #ifndef MULTIPROCESSOR
402 	/*
403 	 * If this isn't the primary CPU, print an error message
404 	 * and just bail out.
405 	 */
406 	if (id != 0) {
407 		aprint_normal(": ID %d\n", id);
408 		aprint_normal_dev(self,
409 		    "processor off-line; "
410 		    "multiprocessor support not present in kernel\n");
411 		return (NULL);
412 	}
413 #endif
414 
415 	ci->ci_cpuid = id;
416 	ci->ci_idepth = -1;
417 	ci->ci_dev = self;
418 	ci->ci_idlespin = cpu_idlespin;
419 
420 	pvr = mfpvr();
421 	vers = (pvr >> 16) & 0xffff;
422 
423 	switch (id) {
424 	case 0:
425 		/* load my cpu_number to PIR */
426 		switch (vers) {
427 		case MPC601:
428 		case MPC604:
429 		case MPC604e:
430 		case MPC604ev:
431 		case MPC7400:
432 		case MPC7410:
433 		case MPC7447A:
434 		case MPC7448:
435 		case MPC7450:
436 		case MPC7455:
437 		case MPC7457:
438 			mtspr(SPR_PIR, id);
439 		}
440 		cpu_setup(self, ci);
441 		break;
442 	default:
443 		if (id >= CPU_MAXNUM) {
444 			aprint_normal(": more than %d cpus?\n", CPU_MAXNUM);
445 			panic("cpuattach");
446 		}
447 #ifndef MULTIPROCESSOR
448 		aprint_normal(" not configured\n");
449 		return NULL;
450 #else
451 		mi_cpu_attach(ci);
452 		break;
453 #endif
454 	}
455 	return (ci);
456 }
457 
458 void
459 cpu_setup(device_t self, struct cpu_info *ci)
460 {
461 	u_int hid0, hid0_save, pvr, vers;
462 	const char * const xname = device_xname(self);
463 	const char *bitmask;
464 	char hidbuf[128];
465 	char model[80];
466 
467 	pvr = mfpvr();
468 	vers = (pvr >> 16) & 0xffff;
469 
470 	cpu_identify(model, sizeof(model));
471 	aprint_normal(": %s, ID %d%s\n", model,  cpu_number(),
472 	    cpu_number() == 0 ? " (primary)" : "");
473 
474 	/* set the cpu number */
475 	ci->ci_cpuid = cpu_number();
476 	hid0_save = hid0 = mfspr(SPR_HID0);
477 
478 	cpu_probe_cache();
479 
480 	/*
481 	 * Configure power-saving mode.
482 	 */
483 	switch (vers) {
484 	case MPC604:
485 	case MPC604e:
486 	case MPC604ev:
487 		/*
488 		 * Do not have HID0 support settings, but can support
489 		 * MSR[POW] off
490 		 */
491 		powersave = 1;
492 		break;
493 
494 	case MPC603:
495 	case MPC603e:
496 	case MPC603ev:
497 	case MPC7400:
498 	case MPC7410:
499 	case MPC8240:
500 	case MPC8245:
501 	case MPCG2:
502 		/* Select DOZE mode. */
503 		hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
504 		hid0 |= HID0_DOZE | HID0_DPM;
505 		powersave = 1;
506 		break;
507 
508 	case MPC750:
509 	case IBM750FX:
510 	case IBM750GX:
511 		/* Select NAP mode. */
512 		hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
513 		hid0 |= HID0_NAP | HID0_DPM;
514 		powersave = 1;
515 		break;
516 
517 	case MPC7447A:
518 	case MPC7448:
519 	case MPC7457:
520 	case MPC7455:
521 	case MPC7450:
522 		/* Enable the 7450 branch caches */
523 		hid0 |= HID0_SGE | HID0_BTIC;
524 		hid0 |= HID0_LRSTK | HID0_FOLD | HID0_BHT;
525 		/* Enable more and larger BAT registers */
526 		if (oeacpufeat & OEACPU_XBSEN)
527 			hid0 |= HID0_XBSEN;
528 		if (oeacpufeat & OEACPU_HIGHBAT)
529 			hid0 |= HID0_HIGH_BAT_EN;
530 		/* Disable BTIC on 7450 Rev 2.0 or earlier */
531 		if (vers == MPC7450 && (pvr & 0xFFFF) <= 0x0200)
532 			hid0 &= ~HID0_BTIC;
533 		/* Select NAP mode. */
534 		hid0 &= ~HID0_SLEEP;
535 		hid0 |= HID0_NAP | HID0_DPM;
536 		powersave = 1;
537 		break;
538 
539 	case IBM970:
540 	case IBM970FX:
541 	case IBM970MP:
542 	case IBMPOWER3II:
543 	default:
544 		/* No power-saving mode is available. */ ;
545 	}
546 
547 #ifdef NAPMODE
548 	switch (vers) {
549 	case IBM750FX:
550 	case IBM750GX:
551 	case MPC750:
552 	case MPC7400:
553 		/* Select NAP mode. */
554 		hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
555 		hid0 |= HID0_NAP;
556 		break;
557 	}
558 #endif
559 
560 	switch (vers) {
561 	case IBM750FX:
562 	case IBM750GX:
563 	case MPC750:
564 		hid0 &= ~HID0_DBP;		/* XXX correct? */
565 		hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
566 		break;
567 
568 	case MPC7400:
569 	case MPC7410:
570 		hid0 &= ~HID0_SPD;
571 		hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
572 		hid0 |= HID0_EIEC;
573 		break;
574 	}
575 
576 	if (hid0 != hid0_save) {
577 		mtspr(SPR_HID0, hid0);
578 		__asm volatile("sync;isync");
579 	}
580 
581 
582 	switch (vers) {
583 	case MPC601:
584 		bitmask = HID0_601_BITMASK;
585 		break;
586 	case MPC7450:
587 	case MPC7455:
588 	case MPC7457:
589 		bitmask = HID0_7450_BITMASK;
590 		break;
591 	case IBM970:
592 	case IBM970FX:
593 	case IBM970MP:
594 		bitmask = 0;
595 		break;
596 	default:
597 		bitmask = HID0_BITMASK;
598 		break;
599 	}
600 	snprintb(hidbuf, sizeof hidbuf, bitmask, hid0);
601 	aprint_normal_dev(self, "HID0 %s, powersave: %d\n", hidbuf, powersave);
602 
603 	ci->ci_khz = 0;
604 
605 	/*
606 	 * Display speed and cache configuration.
607 	 */
608 	switch (vers) {
609 	case MPC604:
610 	case MPC604e:
611 	case MPC604ev:
612 	case MPC750:
613 	case IBM750FX:
614 	case IBM750GX:
615 	case MPC7400:
616 	case MPC7410:
617 	case MPC7447A:
618 	case MPC7448:
619 	case MPC7450:
620 	case MPC7455:
621 	case MPC7457:
622 		aprint_normal_dev(self, "");
623 		cpu_probe_speed(ci);
624 		aprint_normal("%u.%02u MHz",
625 			      ci->ci_khz / 1000, (ci->ci_khz / 10) % 100);
626 		switch (vers) {
627 		case MPC7450: /* 7441 does not have L3! */
628 		case MPC7455: /* 7445 does not have L3! */
629 		case MPC7457: /* 7447 does not have L3! */
630 			cpu_config_l3cr(vers);
631 			break;
632 		case IBM750FX:
633 		case IBM750GX:
634 		case MPC750:
635 		case MPC7400:
636 		case MPC7410:
637 		case MPC7447A:
638 		case MPC7448:
639 			cpu_config_l2cr(pvr);
640 			break;
641 		default:
642 			break;
643 		}
644 		aprint_normal("\n");
645 		break;
646 	}
647 
648 #if NSYSMON_ENVSYS > 0
649 	/*
650 	 * Attach MPC750 temperature sensor to the envsys subsystem.
651 	 * XXX the 74xx series also has this sensor, but it is not
652 	 * XXX supported by Motorola and may return values that are off by
653 	 * XXX 35-55 degrees C.
654 	 */
655 	if (vers == MPC750 || vers == IBM750FX || vers == IBM750GX)
656 		cpu_tau_setup(ci);
657 #endif
658 
659 	evcnt_attach_dynamic(&ci->ci_ev_clock, EVCNT_TYPE_INTR,
660 		NULL, xname, "clock");
661 	evcnt_attach_dynamic(&ci->ci_ev_traps, EVCNT_TYPE_TRAP,
662 		NULL, xname, "traps");
663 	evcnt_attach_dynamic(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP,
664 		&ci->ci_ev_traps, xname, "kernel DSI traps");
665 	evcnt_attach_dynamic(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP,
666 		&ci->ci_ev_traps, xname, "user DSI traps");
667 	evcnt_attach_dynamic(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP,
668 		&ci->ci_ev_udsi, xname, "user DSI failures");
669 	evcnt_attach_dynamic(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP,
670 		&ci->ci_ev_traps, xname, "kernel ISI traps");
671 	evcnt_attach_dynamic(&ci->ci_ev_isi, EVCNT_TYPE_TRAP,
672 		&ci->ci_ev_traps, xname, "user ISI traps");
673 	evcnt_attach_dynamic(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
674 		&ci->ci_ev_isi, xname, "user ISI failures");
675 	evcnt_attach_dynamic(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
676 		&ci->ci_ev_traps, xname, "system call traps");
677 	evcnt_attach_dynamic(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
678 		&ci->ci_ev_traps, xname, "PGM traps");
679 	evcnt_attach_dynamic(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
680 		&ci->ci_ev_traps, xname, "FPU unavailable traps");
681 	evcnt_attach_dynamic(&ci->ci_ev_fpusw, EVCNT_TYPE_TRAP,
682 		&ci->ci_ev_fpu, xname, "FPU context switches");
683 	evcnt_attach_dynamic(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
684 		&ci->ci_ev_traps, xname, "user alignment traps");
685 	evcnt_attach_dynamic(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
686 		&ci->ci_ev_ali, xname, "user alignment traps");
687 	evcnt_attach_dynamic(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
688 		&ci->ci_ev_umchk, xname, "user MCHK failures");
689 	evcnt_attach_dynamic(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
690 		&ci->ci_ev_traps, xname, "AltiVec unavailable");
691 #ifdef ALTIVEC
692 	if (cpu_altivec) {
693 		evcnt_attach_dynamic(&ci->ci_ev_vecsw, EVCNT_TYPE_TRAP,
694 		    &ci->ci_ev_vec, xname, "AltiVec context switches");
695 	}
696 #endif
697 	evcnt_attach_dynamic(&ci->ci_ev_ipi, EVCNT_TYPE_INTR,
698 		NULL, xname, "IPIs");
699 }
700 
701 /*
702  * According to a document labeled "PVR Register Settings":
703  ** For integrated microprocessors the PVR register inside the device
704  ** will identify the version of the microprocessor core. You must also
705  ** read the Device ID, PCI register 02, to identify the part and the
706  ** Revision ID, PCI register 08, to identify the revision of the
707  ** integrated microprocessor.
708  * This apparently applies to 8240/8245/8241, PVR 00810101 and 80811014
709  */
710 
711 void
712 cpu_identify(char *str, size_t len)
713 {
714 	u_int pvr, major, minor;
715 	uint16_t vers, rev, revfmt;
716 	const struct cputab *cp;
717 	const char *name;
718 	size_t n;
719 
720 	pvr = mfpvr();
721 	vers = pvr >> 16;
722 	rev = pvr;
723 
724 	switch (vers) {
725 	case MPC7410:
726 		minor = (pvr >> 0) & 0xff;
727 		major = minor <= 4 ? 1 : 2;
728 		break;
729 	case MPCG2: /*XXX see note above */
730 		major = (pvr >> 4) & 0xf;
731 		minor = (pvr >> 0) & 0xf;
732 		break;
733 	default:
734 		major = (pvr >>  8) & 0xf;
735 		minor = (pvr >>  0) & 0xf;
736 	}
737 
738 	for (cp = models; cp->name[0] != '\0'; cp++) {
739 		if (cp->version == vers)
740 			break;
741 	}
742 
743 	if (str == NULL) {
744 		str = cpu_model;
745 		len = sizeof(cpu_model);
746 		cpu = vers;
747 	}
748 
749 	revfmt = cp->revfmt;
750 	name = cp->name;
751 	if (rev == MPC750 && pvr == 15) {
752 		name = "755";
753 		revfmt = REVFMT_HEX;
754 	}
755 
756 	if (cp->name[0] != '\0') {
757 		n = snprintf(str, len, "%s (Revision ", cp->name);
758 	} else {
759 		n = snprintf(str, len, "Version %#x (Revision ", vers);
760 	}
761 	if (len > n) {
762 		switch (revfmt) {
763 		case REVFMT_MAJMIN:
764 			snprintf(str + n, len - n, "%u.%u)", major, minor);
765 			break;
766 		case REVFMT_HEX:
767 			snprintf(str + n, len - n, "0x%04x)", rev);
768 			break;
769 		case REVFMT_DEC:
770 			snprintf(str + n, len - n, "%u)", rev);
771 			break;
772 		}
773 	}
774 }
775 
776 #ifdef L2CR_CONFIG
777 u_int l2cr_config = L2CR_CONFIG;
778 #else
779 u_int l2cr_config = 0;
780 #endif
781 
782 #ifdef L3CR_CONFIG
783 u_int l3cr_config = L3CR_CONFIG;
784 #else
785 u_int l3cr_config = 0;
786 #endif
787 
788 void
789 cpu_enable_l2cr(register_t l2cr)
790 {
791 	register_t msr, x;
792 	uint16_t vers;
793 
794 	vers = mfpvr() >> 16;
795 
796 	/* Disable interrupts and set the cache config bits. */
797 	msr = mfmsr();
798 	mtmsr(msr & ~PSL_EE);
799 #ifdef ALTIVEC
800 	if (cpu_altivec)
801 		__asm volatile("dssall");
802 #endif
803 	__asm volatile("sync");
804 	mtspr(SPR_L2CR, l2cr & ~L2CR_L2E);
805 	__asm volatile("sync");
806 
807 	/* Wait for L2 clock to be stable (640 L2 clocks). */
808 	delay(100);
809 
810 	/* Invalidate all L2 contents. */
811 	if (MPC745X_P(vers)) {
812 		mtspr(SPR_L2CR, l2cr | L2CR_L2I);
813 		do {
814 			x = mfspr(SPR_L2CR);
815 		} while (x & L2CR_L2I);
816 	} else {
817 		mtspr(SPR_L2CR, l2cr | L2CR_L2I);
818 		do {
819 			x = mfspr(SPR_L2CR);
820 		} while (x & L2CR_L2IP);
821 	}
822 	/* Enable L2 cache. */
823 	l2cr |= L2CR_L2E;
824 	mtspr(SPR_L2CR, l2cr);
825 	mtmsr(msr);
826 }
827 
828 void
829 cpu_enable_l3cr(register_t l3cr)
830 {
831 	register_t x;
832 
833 	/* By The Book (numbered steps from section 3.7.1.3 of MPC7450UM) */
834 
835 	/*
836 	 * 1: Set all L3CR bits for final config except L3E, L3I, L3PE, and
837 	 *    L3CLKEN.  (also mask off reserved bits in case they were included
838 	 *    in L3CR_CONFIG)
839 	 */
840 	l3cr &= ~(L3CR_L3E|L3CR_L3I|L3CR_L3PE|L3CR_L3CLKEN|L3CR_RESERVED);
841 	mtspr(SPR_L3CR, l3cr);
842 
843 	/* 2: Set L3CR[5] (otherwise reserved bit) to 1 */
844 	l3cr |= 0x04000000;
845 	mtspr(SPR_L3CR, l3cr);
846 
847 	/* 3: Set L3CLKEN to 1*/
848 	l3cr |= L3CR_L3CLKEN;
849 	mtspr(SPR_L3CR, l3cr);
850 
851 	/* 4/5: Perform a global cache invalidate (ref section 3.7.3.6) */
852 	__asm volatile("dssall;sync");
853 	/* L3 cache is already disabled, no need to clear L3E */
854 	mtspr(SPR_L3CR, l3cr|L3CR_L3I);
855 	do {
856 		x = mfspr(SPR_L3CR);
857 	} while (x & L3CR_L3I);
858 
859 	/* 6: Clear L3CLKEN to 0 */
860 	l3cr &= ~L3CR_L3CLKEN;
861 	mtspr(SPR_L3CR, l3cr);
862 
863 	/* 7: Perform a 'sync' and wait at least 100 CPU cycles */
864 	__asm volatile("sync");
865 	delay(100);
866 
867 	/* 8: Set L3E and L3CLKEN */
868 	l3cr |= (L3CR_L3E|L3CR_L3CLKEN);
869 	mtspr(SPR_L3CR, l3cr);
870 
871 	/* 9: Perform a 'sync' and wait at least 100 CPU cycles */
872 	__asm volatile("sync");
873 	delay(100);
874 }
875 
876 void
877 cpu_config_l2cr(int pvr)
878 {
879 	register_t l2cr;
880 	u_int vers = (pvr >> 16) & 0xffff;
881 
882 	l2cr = mfspr(SPR_L2CR);
883 
884 	/*
885 	 * For MP systems, the firmware may only configure the L2 cache
886 	 * on the first CPU.  In this case, assume that the other CPUs
887 	 * should use the same value for L2CR.
888 	 */
889 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
890 		l2cr_config = l2cr;
891 	}
892 
893 	/*
894 	 * Configure L2 cache if not enabled.
895 	 */
896 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
897 		cpu_enable_l2cr(l2cr_config);
898 		l2cr = mfspr(SPR_L2CR);
899 	}
900 
901 	if ((l2cr & L2CR_L2E) == 0) {
902 		aprint_normal(" L2 cache present but not enabled ");
903 		return;
904 	}
905 	aprint_normal(",");
906 
907 	switch (vers) {
908 	case IBM750FX:
909 	case IBM750GX:
910 		cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
911 		break;
912 	case MPC750:
913 		if ((pvr & 0xffffff00) == 0x00082200 /* IBM750CX */ ||
914 		    (pvr & 0xffffef00) == 0x00082300 /* IBM750CXe */)
915 			cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
916 		else
917 			cpu_fmttab_print(cpu_l2cr_formats, l2cr);
918 		break;
919 	case MPC7447A:
920 	case MPC7457:
921 		cpu_fmttab_print(cpu_7457_l2cr_formats, l2cr);
922 		return;
923 	case MPC7448:
924 		cpu_fmttab_print(cpu_7448_l2cr_formats, l2cr);
925 		return;
926 	case MPC7450:
927 	case MPC7455:
928 		cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
929 		break;
930 	default:
931 		cpu_fmttab_print(cpu_l2cr_formats, l2cr);
932 		break;
933 	}
934 }
935 
936 void
937 cpu_config_l3cr(int vers)
938 {
939 	register_t l2cr;
940 	register_t l3cr;
941 
942 	l2cr = mfspr(SPR_L2CR);
943 
944 	/*
945 	 * For MP systems, the firmware may only configure the L2 cache
946 	 * on the first CPU.  In this case, assume that the other CPUs
947 	 * should use the same value for L2CR.
948 	 */
949 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
950 		l2cr_config = l2cr;
951 	}
952 
953 	/*
954 	 * Configure L2 cache if not enabled.
955 	 */
956 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
957 		cpu_enable_l2cr(l2cr_config);
958 		l2cr = mfspr(SPR_L2CR);
959 	}
960 
961 	aprint_normal(",");
962 	switch (vers) {
963 	case MPC7447A:
964 	case MPC7457:
965 		cpu_fmttab_print(cpu_7457_l2cr_formats, l2cr);
966 		return;
967 	case MPC7448:
968 		cpu_fmttab_print(cpu_7448_l2cr_formats, l2cr);
969 		return;
970 	default:
971 		cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
972 		break;
973 	}
974 
975 	l3cr = mfspr(SPR_L3CR);
976 
977 	/*
978 	 * For MP systems, the firmware may only configure the L3 cache
979 	 * on the first CPU.  In this case, assume that the other CPUs
980 	 * should use the same value for L3CR.
981 	 */
982 	if ((l3cr & L3CR_L3E) != 0 && l3cr_config == 0) {
983 		l3cr_config = l3cr;
984 	}
985 
986 	/*
987 	 * Configure L3 cache if not enabled.
988 	 */
989 	if ((l3cr & L3CR_L3E) == 0 && l3cr_config != 0) {
990 		cpu_enable_l3cr(l3cr_config);
991 		l3cr = mfspr(SPR_L3CR);
992 	}
993 
994 	if (l3cr & L3CR_L3E) {
995 		aprint_normal(",");
996 		cpu_fmttab_print(cpu_7450_l3cr_formats, l3cr);
997 	}
998 }
999 
1000 void
1001 cpu_probe_speed(struct cpu_info *ci)
1002 {
1003 	uint64_t cps;
1004 
1005 	mtspr(SPR_MMCR0, MMCR0_FC);
1006 	mtspr(SPR_PMC1, 0);
1007 	mtspr(SPR_MMCR0, MMCR0_PMC1SEL(PMCN_CYCLES));
1008 	delay(100000);
1009 	cps = (mfspr(SPR_PMC1) * 10) + 4999;
1010 
1011 	mtspr(SPR_MMCR0, MMCR0_FC);
1012 
1013 	ci->ci_khz = (cps * cpu_get_dfs()) / 1000;
1014 }
1015 
1016 /*
1017  * Read the Dynamic Frequency Switching state and return a divisor for
1018  * the maximum frequency.
1019  */
1020 int
1021 cpu_get_dfs(void)
1022 {
1023 	u_int pvr, vers;
1024 
1025 	pvr = mfpvr();
1026 	vers = pvr >> 16;
1027 
1028 	switch (vers) {
1029 	case MPC7448:
1030 		if (mfspr(SPR_HID1) & HID1_DFS4)
1031 			return 4;
1032 	case MPC7447A:
1033 		if (mfspr(SPR_HID1) & HID1_DFS2)
1034 			return 2;
1035 	}
1036 	return 1;
1037 }
1038 
1039 /*
1040  * Set the Dynamic Frequency Switching divisor the same for all cpus.
1041  */
1042 void
1043 cpu_set_dfs(int div)
1044 {
1045 	uint64_t where;
1046 	u_int dfs_mask, pvr, vers;
1047 
1048 	pvr = mfpvr();
1049 	vers = pvr >> 16;
1050 	dfs_mask = 0;
1051 
1052 	switch (vers) {
1053 	case MPC7448:
1054 		dfs_mask |= HID1_DFS4;
1055 	case MPC7447A:
1056 		dfs_mask |= HID1_DFS2;
1057 		break;
1058 	default:
1059 		printf("cpu_set_dfs: DFS not supported\n");
1060 		return;
1061 
1062 	}
1063 
1064 	where = xc_broadcast(0, (xcfunc_t)cpu_set_dfs_xcall, &div, &dfs_mask);
1065 	xc_wait(where);
1066 }
1067 
1068 static void
1069 cpu_set_dfs_xcall(void *arg1, void *arg2)
1070 {
1071 	u_int dfs_mask, hid1, old_hid1;
1072 	int *divisor, s;
1073 
1074 	divisor = arg1;
1075 	dfs_mask = *(u_int *)arg2;
1076 
1077 	s = splhigh();
1078 	hid1 = old_hid1 = mfspr(SPR_HID1);
1079 
1080 	switch (*divisor) {
1081 	case 1:
1082 		hid1 &= ~dfs_mask;
1083 		break;
1084 	case 2:
1085 		hid1 &= ~(dfs_mask & HID1_DFS4);
1086 		hid1 |= dfs_mask & HID1_DFS2;
1087 		break;
1088 	case 4:
1089 		hid1 &= ~(dfs_mask & HID1_DFS2);
1090 		hid1 |= dfs_mask & HID1_DFS4;
1091 		break;
1092 	}
1093 
1094 	if (hid1 != old_hid1) {
1095 		__asm volatile("sync");
1096 		mtspr(SPR_HID1, hid1);
1097 		__asm volatile("sync;isync");
1098 	}
1099 
1100 	splx(s);
1101 }
1102 
1103 #if NSYSMON_ENVSYS > 0
1104 void
1105 cpu_tau_setup(struct cpu_info *ci)
1106 {
1107 	struct sysmon_envsys *sme;
1108 	int error, therm_delay;
1109 
1110 	mtspr(SPR_THRM1, SPR_THRM_VALID);
1111 	mtspr(SPR_THRM2, 0);
1112 
1113 	/*
1114 	 * we need to figure out how much 20+us in units of CPU clock cycles
1115 	 * are
1116 	 */
1117 
1118 	therm_delay = ci->ci_khz / 40;		/* 25us just to be safe */
1119 
1120         mtspr(SPR_THRM3, SPR_THRM_TIMER(therm_delay) | SPR_THRM_ENABLE);
1121 
1122 	sme = sysmon_envsys_create();
1123 
1124 	sensor.units = ENVSYS_STEMP;
1125 	sensor.state = ENVSYS_SINVALID;
1126 	(void)strlcpy(sensor.desc, "CPU Temp", sizeof(sensor.desc));
1127 	if (sysmon_envsys_sensor_attach(sme, &sensor)) {
1128 		sysmon_envsys_destroy(sme);
1129 		return;
1130 	}
1131 
1132 	sme->sme_name = device_xname(ci->ci_dev);
1133 	sme->sme_cookie = ci;
1134 	sme->sme_refresh = cpu_tau_refresh;
1135 
1136 	if ((error = sysmon_envsys_register(sme)) != 0) {
1137 		aprint_error_dev(ci->ci_dev,
1138 		    " unable to register with sysmon (%d)\n", error);
1139 		sysmon_envsys_destroy(sme);
1140 	}
1141 }
1142 
1143 
1144 /* Find the temperature of the CPU. */
1145 void
1146 cpu_tau_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1147 {
1148 	int i, threshold, count;
1149 
1150 	threshold = 64; /* Half of the 7-bit sensor range */
1151 
1152 	/* Successive-approximation code adapted from Motorola
1153 	 * application note AN1800/D, "Programming the Thermal Assist
1154 	 * Unit in the MPC750 Microprocessor".
1155 	 */
1156 	for (i = 5; i >= 0 ; i--) {
1157 		mtspr(SPR_THRM1,
1158 		    SPR_THRM_THRESHOLD(threshold) | SPR_THRM_VALID);
1159 		count = 0;
1160 		while ((count < 100000) &&
1161 		    ((mfspr(SPR_THRM1) & SPR_THRM_TIV) == 0)) {
1162 			count++;
1163 			delay(1);
1164 		}
1165 		if (mfspr(SPR_THRM1) & SPR_THRM_TIN) {
1166 			/* The interrupt bit was set, meaning the
1167 			 * temperature was above the threshold
1168 			 */
1169 			threshold += 1 << i;
1170 		} else {
1171 			/* Temperature was below the threshold */
1172 			threshold -= 1 << i;
1173 		}
1174 
1175 	}
1176 	threshold += 2;
1177 
1178 	/* Convert the temperature in degrees C to microkelvin */
1179 	edata->value_cur = (threshold * 1000000) + 273150000;
1180 	edata->state = ENVSYS_SVALID;
1181 }
1182 #endif /* NSYSMON_ENVSYS > 0 */
1183 
1184 #ifdef MULTIPROCESSOR
1185 extern volatile u_int cpu_spinstart_ack;
1186 
1187 int
1188 cpu_spinup(device_t self, struct cpu_info *ci)
1189 {
1190 	volatile struct cpu_hatch_data hatch_data, *h = &hatch_data;
1191 	struct pglist mlist;
1192 	int i, error, pvr, vers;
1193 	char *hp;
1194 
1195 	pvr = mfpvr();
1196 	vers = pvr >> 16;
1197 	KASSERT(ci != curcpu());
1198 
1199 	/* Now allocate a hatch stack */
1200 	error = uvm_pglistalloc(0x1000, 0x10000, 0x10000000, 16, 0,
1201 	    &mlist, 1, 1);
1202 	if (error) {
1203 		aprint_error(": unable to allocate hatch stack\n");
1204 		return -1;
1205 	}
1206 
1207 	hp = (void *)VM_PAGE_TO_PHYS(TAILQ_FIRST(&mlist));
1208 	memset(hp, 0, 0x1000);
1209 
1210 	/* Initialize secondary cpu's initial lwp to its idlelwp. */
1211 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
1212 	ci->ci_curpcb = lwp_getpcb(ci->ci_curlwp);
1213 	ci->ci_curpm = ci->ci_curpcb->pcb_pm;
1214 
1215 	cpu_hatch_data = h;
1216 	h->hatch_running = 0;
1217 	h->hatch_self = self;
1218 	h->hatch_ci = ci;
1219 	h->hatch_pir = ci->ci_cpuid;
1220 
1221 	cpu_hatch_stack = (uint32_t)hp;
1222 	ci->ci_lasttb = cpu_info[0].ci_lasttb;
1223 
1224 	/* copy special registers */
1225 
1226 	h->hatch_hid0 = mfspr(SPR_HID0);
1227 
1228 	__asm volatile ("mfsdr1 %0" : "=r"(h->hatch_sdr1));
1229 	for (i = 0; i < 16; i++) {
1230 		__asm ("mfsrin %0,%1" : "=r"(h->hatch_sr[i]) :
1231 		       "r"(i << ADDR_SR_SHFT));
1232 	}
1233 	if (oeacpufeat & OEACPU_64)
1234 		h->hatch_asr = mfspr(SPR_ASR);
1235 	else
1236 		h->hatch_asr = 0;
1237 
1238 	/* copy the bat regs */
1239 	__asm volatile ("mfibatu %0,0" : "=r"(h->hatch_batu[0]));
1240 	__asm volatile ("mfibatl %0,0" : "=r"(h->hatch_batl[0]));
1241 	__asm volatile ("mfibatu %0,1" : "=r"(h->hatch_batu[1]));
1242 	__asm volatile ("mfibatl %0,1" : "=r"(h->hatch_batl[1]));
1243 	__asm volatile ("mfibatu %0,2" : "=r"(h->hatch_batu[2]));
1244 	__asm volatile ("mfibatl %0,2" : "=r"(h->hatch_batl[2]));
1245 	__asm volatile ("mfibatu %0,3" : "=r"(h->hatch_batu[3]));
1246 	__asm volatile ("mfibatl %0,3" : "=r"(h->hatch_batl[3]));
1247 	__asm volatile ("sync; isync");
1248 
1249 	if (md_setup_trampoline(h, ci) == -1)
1250 		return -1;
1251 	md_presync_timebase(h);
1252 	md_start_timebase(h);
1253 
1254 	/* wait for secondary printf */
1255 
1256 	delay(200000);
1257 
1258 	if (h->hatch_running < 1) {
1259 		aprint_error("%d:CPU %d didn't start %d\n", cpu_spinstart_ack,
1260 		    ci->ci_cpuid, cpu_spinstart_ack);
1261 		Debugger();
1262 		return -1;
1263 	}
1264 
1265 	/* Register IPI Interrupt */
1266 	if (ipiops.ppc_establish_ipi)
1267 		ipiops.ppc_establish_ipi(IST_LEVEL, IPL_HIGH, NULL);
1268 
1269 	return 0;
1270 }
1271 
1272 static volatile int start_secondary_cpu;
1273 extern void tlbia(void);
1274 
1275 register_t
1276 cpu_hatch(void)
1277 {
1278 	volatile struct cpu_hatch_data *h = cpu_hatch_data;
1279 	struct cpu_info * const ci = h->hatch_ci;
1280 	struct pcb *pcb;
1281 	u_int msr;
1282 	int i;
1283 
1284 	/* Initialize timebase. */
1285 	__asm ("mttbl %0; mttbu %0; mttbl %0" :: "r"(0));
1286 
1287 	/*
1288 	 * Set PIR (Processor Identification Register).  i.e. whoami
1289 	 * Note that PIR is read-only on some CPU versions, so we write to it
1290 	 * only if it has a different value than we need.
1291 	 */
1292 
1293 	msr = mfspr(SPR_PIR);
1294 	if (msr != h->hatch_pir)
1295 		mtspr(SPR_PIR, h->hatch_pir);
1296 
1297 	__asm volatile ("mtsprg0 %0" :: "r"(ci));
1298 	curlwp = ci->ci_curlwp;
1299 	cpu_spinstart_ack = 0;
1300 
1301 	/* Initialize MMU. */
1302 	__asm ("mtibatu 0,%0" :: "r"(h->hatch_batu[0]));
1303 	__asm ("mtibatl 0,%0" :: "r"(h->hatch_batl[0]));
1304 	__asm ("mtibatu 1,%0" :: "r"(h->hatch_batu[1]));
1305 	__asm ("mtibatl 1,%0" :: "r"(h->hatch_batl[1]));
1306 	__asm ("mtibatu 2,%0" :: "r"(h->hatch_batu[2]));
1307 	__asm ("mtibatl 2,%0" :: "r"(h->hatch_batl[2]));
1308 	__asm ("mtibatu 3,%0" :: "r"(h->hatch_batu[3]));
1309 	__asm ("mtibatl 3,%0" :: "r"(h->hatch_batl[3]));
1310 
1311 	mtspr(SPR_HID0, h->hatch_hid0);
1312 
1313 	__asm ("mtibatl 0,%0; mtibatu 0,%1; mtdbatl 0,%0; mtdbatu 0,%1;"
1314 	    :: "r"(battable[0].batl), "r"(battable[0].batu));
1315 
1316 	__asm volatile ("sync");
1317 	for (i = 0; i < 16; i++)
1318 		__asm ("mtsrin %0,%1" :: "r"(h->hatch_sr[i]), "r"(i << ADDR_SR_SHFT));
1319 	__asm volatile ("sync; isync");
1320 
1321 	if (oeacpufeat & OEACPU_64)
1322 		mtspr(SPR_ASR, h->hatch_asr);
1323 
1324 	cpu_spinstart_ack = 1;
1325 	__asm ("ptesync");
1326 	__asm ("mtsdr1 %0" :: "r"(h->hatch_sdr1));
1327 	__asm volatile ("sync; isync");
1328 
1329 	cpu_spinstart_ack = 5;
1330 	for (i = 0; i < 16; i++)
1331 		__asm ("mfsrin %0,%1" : "=r"(h->hatch_sr[i]) :
1332 		       "r"(i << ADDR_SR_SHFT));
1333 
1334 	/* Enable I/D address translations. */
1335 	msr = mfmsr();
1336 	msr |= PSL_IR|PSL_DR|PSL_ME|PSL_RI;
1337 	mtmsr(msr);
1338 	__asm volatile ("sync; isync");
1339 	cpu_spinstart_ack = 2;
1340 
1341 	md_sync_timebase(h);
1342 
1343 	cpu_setup(h->hatch_self, ci);
1344 
1345 	h->hatch_running = 1;
1346 	__asm volatile ("sync; isync");
1347 
1348 	while (start_secondary_cpu == 0)
1349 		;
1350 
1351 	__asm volatile ("sync; isync");
1352 
1353 	aprint_normal("cpu%d started\n", curcpu()->ci_index);
1354 	__asm volatile ("mtdec %0" :: "r"(ticks_per_intr));
1355 
1356 	md_setup_interrupts();
1357 
1358 	ci->ci_ipending = 0;
1359 	ci->ci_cpl = 0;
1360 
1361 	mtmsr(mfmsr() | PSL_EE);
1362 	pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
1363 	return pcb->pcb_sp;
1364 }
1365 
1366 void
1367 cpu_boot_secondary_processors(void)
1368 {
1369 	start_secondary_cpu = 1;
1370 	__asm volatile ("sync");
1371 }
1372 
1373 #endif /*MULTIPROCESSOR*/
1374