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