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