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