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