Home | History | Annotate | Line # | Download | only in oea
cpu_subr.c revision 1.28.16.5
      1 /*	$NetBSD: cpu_subr.c,v 1.28.16.5 2007/10/04 18:23:39 macallan 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.28.16.5 2007/10/04 18:23:39 macallan 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/malloc.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 
     51 #include <powerpc/oea/hid.h>
     52 #include <powerpc/oea/hid_601.h>
     53 #include <powerpc/spr.h>
     54 
     55 #include <dev/sysmon/sysmonvar.h>
     56 
     57 static void cpu_enable_l2cr(register_t);
     58 static void cpu_enable_l3cr(register_t);
     59 static void cpu_config_l2cr(int);
     60 static void cpu_config_l3cr(int);
     61 static void cpu_probe_speed(struct cpu_info *);
     62 static void cpu_idlespin(void);
     63 #if NSYSMON_ENVSYS > 0
     64 static void cpu_tau_setup(struct cpu_info *);
     65 static int cpu_tau_gtredata(struct sysmon_envsys *, envsys_data_t *);
     66 #endif
     67 
     68 int cpu;
     69 int ncpus;
     70 
     71 struct fmttab {
     72 	register_t fmt_mask;
     73 	register_t fmt_value;
     74 	const char *fmt_string;
     75 };
     76 
     77 static const struct fmttab cpu_7450_l2cr_formats[] = {
     78 	{ L2CR_L2E, 0, " disabled" },
     79 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
     80 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
     81 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
     82 	{ L2CR_L2E, ~0, " 256KB L2 cache" },
     83 	{ 0, 0, NULL }
     84 };
     85 
     86 static const struct fmttab cpu_7448_l2cr_formats[] = {
     87 	{ L2CR_L2E, 0, " disabled" },
     88 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
     89 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
     90 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
     91 	{ L2CR_L2E, ~0, " 1MB L2 cache" },
     92 	{ 0, 0, NULL }
     93 };
     94 
     95 static const struct fmttab cpu_7457_l2cr_formats[] = {
     96 	{ L2CR_L2E, 0, " disabled" },
     97 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
     98 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
     99 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
    100 	{ L2CR_L2E, ~0, " 512KB L2 cache" },
    101 	{ 0, 0, NULL }
    102 };
    103 
    104 static const struct fmttab cpu_7450_l3cr_formats[] = {
    105 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3DO, " data-only" },
    106 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3IO, " instruction-only" },
    107 	{ L3CR_L3DO|L3CR_L3IO, L3CR_L3DO|L3CR_L3IO, " locked" },
    108 	{ L3CR_L3SIZ, L3SIZ_2M, " 2MB" },
    109 	{ L3CR_L3SIZ, L3SIZ_1M, " 1MB" },
    110 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3PE|L3CR_L3APE, " parity" },
    111 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3PE, " data-parity" },
    112 	{ L3CR_L3PE|L3CR_L3APE, L3CR_L3APE, " address-parity" },
    113 	{ L3CR_L3PE|L3CR_L3APE, 0, " no-parity" },
    114 	{ L3CR_L3SIZ, ~0, " L3 cache" },
    115 	{ L3CR_L3RT, L3RT_MSUG2_DDR, " (DDR SRAM)" },
    116 	{ L3CR_L3RT, L3RT_PIPELINE_LATE, " (LW SRAM)" },
    117 	{ L3CR_L3RT, L3RT_PB2_SRAM, " (PB2 SRAM)" },
    118 	{ L3CR_L3CLK, ~0, " at" },
    119 	{ L3CR_L3CLK, L3CLK_20, " 2:1" },
    120 	{ L3CR_L3CLK, L3CLK_25, " 2.5:1" },
    121 	{ L3CR_L3CLK, L3CLK_30, " 3:1" },
    122 	{ L3CR_L3CLK, L3CLK_35, " 3.5:1" },
    123 	{ L3CR_L3CLK, L3CLK_40, " 4:1" },
    124 	{ L3CR_L3CLK, L3CLK_50, " 5:1" },
    125 	{ L3CR_L3CLK, L3CLK_60, " 6:1" },
    126 	{ L3CR_L3CLK, ~0, " ratio" },
    127 	{ 0, 0, NULL },
    128 };
    129 
    130 static const struct fmttab cpu_ibm750_l2cr_formats[] = {
    131 	{ L2CR_L2E, 0, " disabled" },
    132 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
    133 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
    134 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
    135 	{ 0, ~0, " 512KB" },
    136 	{ L2CR_L2WT, L2CR_L2WT, " WT" },
    137 	{ L2CR_L2WT, 0, " WB" },
    138 	{ L2CR_L2PE, L2CR_L2PE, " with ECC" },
    139 	{ 0, ~0, " L2 cache" },
    140 	{ 0, 0, NULL }
    141 };
    142 
    143 static const struct fmttab cpu_l2cr_formats[] = {
    144 	{ L2CR_L2E, 0, " disabled" },
    145 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
    146 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
    147 	{ L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
    148 	{ L2CR_L2PE, L2CR_L2PE, " parity" },
    149 	{ L2CR_L2PE, 0, " no-parity" },
    150 	{ L2CR_L2SIZ, L2SIZ_2M, " 2MB" },
    151 	{ L2CR_L2SIZ, L2SIZ_1M, " 1MB" },
    152 	{ L2CR_L2SIZ, L2SIZ_512K, " 512KB" },
    153 	{ L2CR_L2SIZ, L2SIZ_256K, " 256KB" },
    154 	{ L2CR_L2WT, L2CR_L2WT, " WT" },
    155 	{ L2CR_L2WT, 0, " WB" },
    156 	{ L2CR_L2E, ~0, " L2 cache" },
    157 	{ L2CR_L2RAM, L2RAM_FLOWTHRU_BURST, " (FB SRAM)" },
    158 	{ L2CR_L2RAM, L2RAM_PIPELINE_LATE, " (LW SRAM)" },
    159 	{ L2CR_L2RAM, L2RAM_PIPELINE_BURST, " (PB SRAM)" },
    160 	{ L2CR_L2CLK, ~0, " at" },
    161 	{ L2CR_L2CLK, L2CLK_10, " 1:1" },
    162 	{ L2CR_L2CLK, L2CLK_15, " 1.5:1" },
    163 	{ L2CR_L2CLK, L2CLK_20, " 2:1" },
    164 	{ L2CR_L2CLK, L2CLK_25, " 2.5:1" },
    165 	{ L2CR_L2CLK, L2CLK_30, " 3:1" },
    166 	{ L2CR_L2CLK, L2CLK_35, " 3.5:1" },
    167 	{ L2CR_L2CLK, L2CLK_40, " 4:1" },
    168 	{ L2CR_L2CLK, ~0, " ratio" },
    169 	{ 0, 0, NULL }
    170 };
    171 
    172 static void cpu_fmttab_print(const struct fmttab *, register_t);
    173 
    174 struct cputab {
    175 	const char name[8];
    176 	uint16_t version;
    177 	uint16_t revfmt;
    178 };
    179 #define	REVFMT_MAJMIN	1		/* %u.%u */
    180 #define	REVFMT_HEX	2		/* 0x%04x */
    181 #define	REVFMT_DEC	3		/* %u */
    182 static const struct cputab models[] = {
    183 	{ "601",	MPC601,		REVFMT_DEC },
    184 	{ "602",	MPC602,		REVFMT_DEC },
    185 	{ "603",	MPC603,		REVFMT_MAJMIN },
    186 	{ "603e",	MPC603e,	REVFMT_MAJMIN },
    187 	{ "603ev",	MPC603ev,	REVFMT_MAJMIN },
    188 	{ "G2",		MPCG2,		REVFMT_MAJMIN },
    189 	{ "604",	MPC604,		REVFMT_MAJMIN },
    190 	{ "604e",	MPC604e,	REVFMT_MAJMIN },
    191 	{ "604ev",	MPC604ev,	REVFMT_MAJMIN },
    192 	{ "620",	MPC620,  	REVFMT_HEX },
    193 	{ "750",	MPC750,		REVFMT_MAJMIN },
    194 	{ "750FX",	IBM750FX,	REVFMT_MAJMIN },
    195 	{ "7400",	MPC7400,	REVFMT_MAJMIN },
    196 	{ "7410",	MPC7410,	REVFMT_MAJMIN },
    197 	{ "7450",	MPC7450,	REVFMT_MAJMIN },
    198 	{ "7455",	MPC7455,	REVFMT_MAJMIN },
    199 	{ "7457",	MPC7457,	REVFMT_MAJMIN },
    200 	{ "7447A",	MPC7447A,	REVFMT_MAJMIN },
    201 	{ "7448",	MPC7448,	REVFMT_MAJMIN },
    202 	{ "8240",	MPC8240,	REVFMT_MAJMIN },
    203 	{ "8245",	MPC8245,	REVFMT_MAJMIN },
    204 	{ "970",	IBM970,		REVFMT_MAJMIN },
    205 	{ "970FX",	IBM970FX,	REVFMT_MAJMIN },
    206 	{ "",		0,		REVFMT_HEX }
    207 };
    208 
    209 
    210 #ifdef MULTIPROCESSOR
    211 struct cpu_info cpu_info[CPU_MAXNUM];
    212 #else
    213 struct cpu_info cpu_info[1];
    214 #endif
    215 
    216 int cpu_altivec;
    217 int cpu_psluserset, cpu_pslusermod;
    218 char cpu_model[80];
    219 
    220 void
    221 cpu_fmttab_print(const struct fmttab *fmt, register_t data)
    222 {
    223 	for (; fmt->fmt_mask != 0 || fmt->fmt_value != 0; fmt++) {
    224 		if ((~fmt->fmt_mask & fmt->fmt_value) != 0 ||
    225 		    (data & fmt->fmt_mask) == fmt->fmt_value)
    226 			aprint_normal("%s", fmt->fmt_string);
    227 	}
    228 }
    229 
    230 void
    231 cpu_idlespin(void)
    232 {
    233 	register_t msr;
    234 
    235 	if (powersave <= 0)
    236 		return;
    237 
    238 	__asm volatile(
    239 		"sync;"
    240 		"mfmsr	%0;"
    241 		"oris	%0,%0,%1@h;"	/* enter power saving mode */
    242 		"mtmsr	%0;"
    243 		"isync;"
    244 	    :	"=r"(msr)
    245 	    :	"J"(PSL_POW));
    246 }
    247 
    248 void
    249 cpu_probe_cache(void)
    250 {
    251 	u_int assoc, pvr, vers;
    252 
    253 	pvr = mfpvr();
    254 	vers = pvr >> 16;
    255 
    256 
    257 	/* Presently common across almost all implementations. */
    258 	curcpu()->ci_ci.dcache_line_size = CACHELINESIZE;
    259 	curcpu()->ci_ci.icache_line_size = CACHELINESIZE;
    260 
    261 
    262 	switch (vers) {
    263 #define	K	*1024
    264 	case IBM750FX:
    265 	case MPC601:
    266 	case MPC750:
    267 	case MPC7447A:
    268 	case MPC7448:
    269 	case MPC7450:
    270 	case MPC7455:
    271 	case MPC7457:
    272 		curcpu()->ci_ci.dcache_size = 32 K;
    273 		curcpu()->ci_ci.icache_size = 32 K;
    274 		assoc = 8;
    275 		break;
    276 	case MPC603:
    277 		curcpu()->ci_ci.dcache_size = 8 K;
    278 		curcpu()->ci_ci.icache_size = 8 K;
    279 		assoc = 2;
    280 		break;
    281 	case MPC603e:
    282 	case MPC603ev:
    283 	case MPC604:
    284 	case MPC8240:
    285 	case MPC8245:
    286 	case MPCG2:
    287 		curcpu()->ci_ci.dcache_size = 16 K;
    288 		curcpu()->ci_ci.icache_size = 16 K;
    289 		assoc = 4;
    290 		break;
    291 	case MPC604e:
    292 	case MPC604ev:
    293 		curcpu()->ci_ci.dcache_size = 32 K;
    294 		curcpu()->ci_ci.icache_size = 32 K;
    295 		assoc = 4;
    296 		break;
    297 	case IBM970:
    298 	case IBM970FX:
    299 		curcpu()->ci_ci.dcache_size = 32 K;
    300 		curcpu()->ci_ci.icache_size = 64 K;
    301 		curcpu()->ci_ci.dcache_line_size = 128;
    302 		curcpu()->ci_ci.icache_line_size = 128;
    303 		assoc = 2;
    304 		break;
    305 
    306 	default:
    307 		curcpu()->ci_ci.dcache_size = PAGE_SIZE;
    308 		curcpu()->ci_ci.icache_size = PAGE_SIZE;
    309 		assoc = 1;
    310 #undef	K
    311 	}
    312 
    313 	/*
    314 	 * Possibly recolor.
    315 	 */
    316 	uvm_page_recolor(atop(curcpu()->ci_ci.dcache_size / assoc));
    317 }
    318 
    319 struct cpu_info *
    320 cpu_attach_common(struct device *self, int id)
    321 {
    322 	struct cpu_info *ci;
    323 	u_int pvr, vers;
    324 
    325 	ci = &cpu_info[id];
    326 #ifndef MULTIPROCESSOR
    327 	/*
    328 	 * If this isn't the primary CPU, print an error message
    329 	 * and just bail out.
    330 	 */
    331 	if (id != 0) {
    332 		aprint_normal(": ID %d\n", id);
    333 		aprint_normal("%s: processor off-line; multiprocessor support "
    334 		    "not present in kernel\n", self->dv_xname);
    335 		return (NULL);
    336 	}
    337 #endif
    338 
    339 	ci->ci_cpuid = id;
    340 	ci->ci_intrdepth = -1;
    341 	ci->ci_dev = self;
    342 	ci->ci_idlespin = cpu_idlespin;
    343 
    344 	pvr = mfpvr();
    345 	vers = (pvr >> 16) & 0xffff;
    346 
    347 	switch (id) {
    348 	case 0:
    349 		/* load my cpu_number to PIR */
    350 		switch (vers) {
    351 		case MPC601:
    352 		case MPC604:
    353 		case MPC604e:
    354 		case MPC604ev:
    355 		case MPC7400:
    356 		case MPC7410:
    357 		case MPC7447A:
    358 		case MPC7448:
    359 		case MPC7450:
    360 		case MPC7455:
    361 		case MPC7457:
    362 			mtspr(SPR_PIR, id);
    363 		}
    364 		cpu_setup(self, ci);
    365 		break;
    366 	default:
    367 		if (id >= CPU_MAXNUM) {
    368 			aprint_normal(": more than %d cpus?\n", CPU_MAXNUM);
    369 			panic("cpuattach");
    370 		}
    371 #ifndef MULTIPROCESSOR
    372 		aprint_normal(" not configured\n");
    373 		return NULL;
    374 #else
    375 		mi_cpu_attach(ci);
    376 		break;
    377 #endif
    378 	}
    379 	return (ci);
    380 }
    381 
    382 void
    383 cpu_setup(self, ci)
    384 	struct device *self;
    385 	struct cpu_info *ci;
    386 {
    387 	u_int hid0, pvr, vers;
    388 	const char *bitmask;
    389 	char hidbuf[128];
    390 	char model[80];
    391 
    392 	pvr = mfpvr();
    393 	vers = (pvr >> 16) & 0xffff;
    394 
    395 	cpu_identify(model, sizeof(model));
    396 	aprint_normal(": %s, ID %d%s\n", model,  cpu_number(),
    397 	    cpu_number() == 0 ? " (primary)" : "");
    398 
    399 #if defined (PPC_OEA) || defined (PPC_OEA64)
    400 	hid0 = mfspr(SPR_HID0);
    401 #elif defined (PPC_OEA64_BRIDGE)
    402 	hid0 = mfspr(SPR_HID0);
    403 #endif
    404 
    405 	cpu_probe_cache();
    406 
    407 	/*
    408 	 * Configure power-saving mode.
    409 	 */
    410 	switch (vers) {
    411 	case MPC604:
    412 	case MPC604e:
    413 	case MPC604ev:
    414 		/*
    415 		 * Do not have HID0 support settings, but can support
    416 		 * MSR[POW] off
    417 		 */
    418 		powersave = 1;
    419 		break;
    420 
    421 	case MPC603:
    422 	case MPC603e:
    423 	case MPC603ev:
    424 	case MPC750:
    425 	case IBM750FX:
    426 	case MPC7400:
    427 	case MPC7410:
    428 	case MPC8240:
    429 	case MPC8245:
    430 	case MPCG2:
    431 		/* Select DOZE mode. */
    432 		hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
    433 		hid0 |= HID0_DOZE | HID0_DPM;
    434 		powersave = 1;
    435 		break;
    436 
    437 	case MPC7447A:
    438 	case MPC7448:
    439 	case MPC7457:
    440 	case MPC7455:
    441 	case MPC7450:
    442 		/* Enable the 7450 branch caches */
    443 		hid0 |= HID0_SGE | HID0_BTIC;
    444 		hid0 |= HID0_LRSTK | HID0_FOLD | HID0_BHT;
    445 		/* Disable BTIC on 7450 Rev 2.0 or earlier */
    446 		if (vers == MPC7450 && (pvr & 0xFFFF) <= 0x0200)
    447 			hid0 &= ~HID0_BTIC;
    448 		/* Select NAP mode. */
    449 		hid0 &= ~(HID0_HIGH_BAT_EN | HID0_SLEEP);
    450 		hid0 |= HID0_NAP | HID0_DPM /* | HID0_XBSEN */;
    451 		powersave = 1;
    452 		break;
    453 
    454 	case IBM970:
    455 	case IBM970FX:
    456 	default:
    457 		/* No power-saving mode is available. */ ;
    458 	}
    459 
    460 #ifdef NAPMODE
    461 	switch (vers) {
    462 	case IBM750FX:
    463 	case MPC750:
    464 	case MPC7400:
    465 		/* Select NAP mode. */
    466 		hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
    467 		hid0 |= HID0_NAP;
    468 		break;
    469 	}
    470 #endif
    471 
    472 	switch (vers) {
    473 	case IBM750FX:
    474 	case MPC750:
    475 		hid0 &= ~HID0_DBP;		/* XXX correct? */
    476 		hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
    477 		break;
    478 
    479 	case MPC7400:
    480 	case MPC7410:
    481 		hid0 &= ~HID0_SPD;
    482 		hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
    483 		hid0 |= HID0_EIEC;
    484 		break;
    485 	}
    486 
    487 #if defined (PPC_OEA)
    488 	mtspr(SPR_HID0, hid0);
    489 	__asm volatile("sync;isync");
    490 #endif
    491 
    492 	switch (vers) {
    493 	case MPC601:
    494 		bitmask = HID0_601_BITMASK;
    495 		break;
    496 	case MPC7450:
    497 	case MPC7455:
    498 	case MPC7457:
    499 		bitmask = HID0_7450_BITMASK;
    500 		break;
    501 	case IBM970:
    502 	case IBM970FX:
    503 		bitmask = 0;
    504 		break;
    505 	default:
    506 		bitmask = HID0_BITMASK;
    507 		break;
    508 	}
    509 	bitmask_snprintf(hid0, bitmask, hidbuf, sizeof hidbuf);
    510 	aprint_normal("%s: HID0 %s, powersave: %d\n", self->dv_xname, hidbuf, powersave);
    511 
    512 	ci->ci_khz = 0;
    513 
    514 	/*
    515 	 * Display speed and cache configuration.
    516 	 */
    517 	switch (vers) {
    518 	case MPC604:
    519 	case MPC604e:
    520 	case MPC604ev:
    521 	case MPC750:
    522 	case IBM750FX:
    523 	case MPC7400:
    524 	case MPC7410:
    525 	case MPC7447A:
    526 	case MPC7448:
    527 	case MPC7450:
    528 	case MPC7455:
    529 	case MPC7457:
    530 		aprint_normal("%s: ", self->dv_xname);
    531 		cpu_probe_speed(ci);
    532 		aprint_normal("%u.%02u MHz",
    533 			      ci->ci_khz / 1000, (ci->ci_khz / 10) % 100);
    534 
    535 		if (vers == IBM750FX || vers == MPC750 ||
    536 		    vers == MPC7400  || vers == MPC7410 || MPC745X_P(vers)) {
    537 			if (MPC745X_P(vers)) {
    538 				cpu_config_l3cr(vers);
    539 			} else {
    540 				cpu_config_l2cr(pvr);
    541 			}
    542 		}
    543 		aprint_normal("\n");
    544 		break;
    545 	}
    546 
    547 #if NSYSMON_ENVSYS > 0
    548 	/*
    549 	 * Attach MPC750 temperature sensor to the envsys subsystem.
    550 	 * XXX the 74xx series also has this sensor, but it is not
    551 	 * XXX supported by Motorola and may return values that are off by
    552 	 * XXX 35-55 degrees C.
    553 	 */
    554 	if (vers == MPC750 || vers == IBM750FX)
    555 		cpu_tau_setup(ci);
    556 #endif
    557 
    558 	evcnt_attach_dynamic(&ci->ci_ev_clock, EVCNT_TYPE_INTR,
    559 		NULL, self->dv_xname, "clock");
    560 	evcnt_attach_dynamic(&ci->ci_ev_softclock, EVCNT_TYPE_INTR,
    561 		NULL, self->dv_xname, "soft clock");
    562 	evcnt_attach_dynamic(&ci->ci_ev_softnet, EVCNT_TYPE_INTR,
    563 		NULL, self->dv_xname, "soft net");
    564 	evcnt_attach_dynamic(&ci->ci_ev_softserial, EVCNT_TYPE_INTR,
    565 		NULL, self->dv_xname, "soft serial");
    566 	evcnt_attach_dynamic(&ci->ci_ev_traps, EVCNT_TYPE_TRAP,
    567 		NULL, self->dv_xname, "traps");
    568 	evcnt_attach_dynamic(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP,
    569 		&ci->ci_ev_traps, self->dv_xname, "kernel DSI traps");
    570 	evcnt_attach_dynamic(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP,
    571 		&ci->ci_ev_traps, self->dv_xname, "user DSI traps");
    572 	evcnt_attach_dynamic(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP,
    573 		&ci->ci_ev_udsi, self->dv_xname, "user DSI failures");
    574 	evcnt_attach_dynamic(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP,
    575 		&ci->ci_ev_traps, self->dv_xname, "kernel ISI traps");
    576 	evcnt_attach_dynamic(&ci->ci_ev_isi, EVCNT_TYPE_TRAP,
    577 		&ci->ci_ev_traps, self->dv_xname, "user ISI traps");
    578 	evcnt_attach_dynamic(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
    579 		&ci->ci_ev_isi, self->dv_xname, "user ISI failures");
    580 	evcnt_attach_dynamic(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
    581 		&ci->ci_ev_traps, self->dv_xname, "system call traps");
    582 	evcnt_attach_dynamic(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
    583 		&ci->ci_ev_traps, self->dv_xname, "PGM traps");
    584 	evcnt_attach_dynamic(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
    585 		&ci->ci_ev_traps, self->dv_xname, "FPU unavailable traps");
    586 	evcnt_attach_dynamic(&ci->ci_ev_fpusw, EVCNT_TYPE_TRAP,
    587 		&ci->ci_ev_fpu, self->dv_xname, "FPU context switches");
    588 	evcnt_attach_dynamic(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
    589 		&ci->ci_ev_traps, self->dv_xname, "user alignment traps");
    590 	evcnt_attach_dynamic(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
    591 		&ci->ci_ev_ali, self->dv_xname, "user alignment traps");
    592 	evcnt_attach_dynamic(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
    593 		&ci->ci_ev_umchk, self->dv_xname, "user MCHK failures");
    594 	evcnt_attach_dynamic(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
    595 		&ci->ci_ev_traps, self->dv_xname, "AltiVec unavailable");
    596 #ifdef ALTIVEC
    597 	if (cpu_altivec) {
    598 		evcnt_attach_dynamic(&ci->ci_ev_vecsw, EVCNT_TYPE_TRAP,
    599 		    &ci->ci_ev_vec, self->dv_xname, "AltiVec context switches");
    600 	}
    601 #endif
    602 	evcnt_attach_dynamic(&ci->ci_ev_ipi, EVCNT_TYPE_INTR,
    603 		NULL, self->dv_xname, "IPIs");
    604 }
    605 
    606 void
    607 cpu_identify(char *str, size_t len)
    608 {
    609 	u_int pvr, major, minor;
    610 	uint16_t vers, rev, revfmt;
    611 	const struct cputab *cp;
    612 	const char *name;
    613 	size_t n;
    614 
    615 	pvr = mfpvr();
    616 	vers = pvr >> 16;
    617 	rev = pvr;
    618 
    619 	switch (vers) {
    620 	case MPC7410:
    621 		minor = (pvr >> 0) & 0xff;
    622 		major = minor <= 4 ? 1 : 2;
    623 		break;
    624 	default:
    625 		major = (pvr >>  4) & 0xf;
    626 		minor = (pvr >>  0) & 0xf;
    627 	}
    628 
    629 	for (cp = models; cp->name[0] != '\0'; cp++) {
    630 		if (cp->version == vers)
    631 			break;
    632 	}
    633 
    634 	if (str == NULL) {
    635 		str = cpu_model;
    636 		len = sizeof(cpu_model);
    637 		cpu = vers;
    638 	}
    639 
    640 	revfmt = cp->revfmt;
    641 	name = cp->name;
    642 	if (rev == MPC750 && pvr == 15) {
    643 		name = "755";
    644 		revfmt = REVFMT_HEX;
    645 	}
    646 
    647 	if (cp->name[0] != '\0') {
    648 		n = snprintf(str, len, "%s (Revision ", cp->name);
    649 	} else {
    650 		n = snprintf(str, len, "Version %#x (Revision ", vers);
    651 	}
    652 	if (len > n) {
    653 		switch (revfmt) {
    654 		case REVFMT_MAJMIN:
    655 			snprintf(str + n, len - n, "%u.%u)", major, minor);
    656 			break;
    657 		case REVFMT_HEX:
    658 			snprintf(str + n, len - n, "0x%04x)", rev);
    659 			break;
    660 		case REVFMT_DEC:
    661 			snprintf(str + n, len - n, "%u)", rev);
    662 			break;
    663 		}
    664 	}
    665 }
    666 
    667 #ifdef L2CR_CONFIG
    668 u_int l2cr_config = L2CR_CONFIG;
    669 #else
    670 u_int l2cr_config = 0;
    671 #endif
    672 
    673 #ifdef L3CR_CONFIG
    674 u_int l3cr_config = L3CR_CONFIG;
    675 #else
    676 u_int l3cr_config = 0;
    677 #endif
    678 
    679 void
    680 cpu_enable_l2cr(register_t l2cr)
    681 {
    682 	register_t msr, x;
    683 
    684 	/* Disable interrupts and set the cache config bits. */
    685 	msr = mfmsr();
    686 	mtmsr(msr & ~PSL_EE);
    687 #ifdef ALTIVEC
    688 	if (cpu_altivec)
    689 		__asm volatile("dssall");
    690 #endif
    691 	__asm volatile("sync");
    692 	mtspr(SPR_L2CR, l2cr & ~L2CR_L2E);
    693 	__asm volatile("sync");
    694 
    695 	/* Wait for L2 clock to be stable (640 L2 clocks). */
    696 	delay(100);
    697 
    698 	/* Invalidate all L2 contents. */
    699 	mtspr(SPR_L2CR, l2cr | L2CR_L2I);
    700 	do {
    701 		x = mfspr(SPR_L2CR);
    702 	} while (x & L2CR_L2IP);
    703 
    704 	/* Enable L2 cache. */
    705 	l2cr |= L2CR_L2E;
    706 	mtspr(SPR_L2CR, l2cr);
    707 	mtmsr(msr);
    708 }
    709 
    710 void
    711 cpu_enable_l3cr(register_t l3cr)
    712 {
    713 	register_t x;
    714 
    715 	/* By The Book (numbered steps from section 3.7.1.3 of MPC7450UM) */
    716 
    717 	/*
    718 	 * 1: Set all L3CR bits for final config except L3E, L3I, L3PE, and
    719 	 *    L3CLKEN.  (also mask off reserved bits in case they were included
    720 	 *    in L3CR_CONFIG)
    721 	 */
    722 	l3cr &= ~(L3CR_L3E|L3CR_L3I|L3CR_L3PE|L3CR_L3CLKEN|L3CR_RESERVED);
    723 	mtspr(SPR_L3CR, l3cr);
    724 
    725 	/* 2: Set L3CR[5] (otherwise reserved bit) to 1 */
    726 	l3cr |= 0x04000000;
    727 	mtspr(SPR_L3CR, l3cr);
    728 
    729 	/* 3: Set L3CLKEN to 1*/
    730 	l3cr |= L3CR_L3CLKEN;
    731 	mtspr(SPR_L3CR, l3cr);
    732 
    733 	/* 4/5: Perform a global cache invalidate (ref section 3.7.3.6) */
    734 	__asm volatile("dssall;sync");
    735 	/* L3 cache is already disabled, no need to clear L3E */
    736 	mtspr(SPR_L3CR, l3cr|L3CR_L3I);
    737 	do {
    738 		x = mfspr(SPR_L3CR);
    739 	} while (x & L3CR_L3I);
    740 
    741 	/* 6: Clear L3CLKEN to 0 */
    742 	l3cr &= ~L3CR_L3CLKEN;
    743 	mtspr(SPR_L3CR, l3cr);
    744 
    745 	/* 7: Perform a 'sync' and wait at least 100 CPU cycles */
    746 	__asm volatile("sync");
    747 	delay(100);
    748 
    749 	/* 8: Set L3E and L3CLKEN */
    750 	l3cr |= (L3CR_L3E|L3CR_L3CLKEN);
    751 	mtspr(SPR_L3CR, l3cr);
    752 
    753 	/* 9: Perform a 'sync' and wait at least 100 CPU cycles */
    754 	__asm volatile("sync");
    755 	delay(100);
    756 }
    757 
    758 void
    759 cpu_config_l2cr(int pvr)
    760 {
    761 	register_t l2cr;
    762 
    763 	l2cr = mfspr(SPR_L2CR);
    764 
    765 	/*
    766 	 * For MP systems, the firmware may only configure the L2 cache
    767 	 * on the first CPU.  In this case, assume that the other CPUs
    768 	 * should use the same value for L2CR.
    769 	 */
    770 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
    771 		l2cr_config = l2cr;
    772 	}
    773 
    774 	/*
    775 	 * Configure L2 cache if not enabled.
    776 	 */
    777 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
    778 		cpu_enable_l2cr(l2cr_config);
    779 		l2cr = mfspr(SPR_L2CR);
    780 	}
    781 
    782 	if ((l2cr & L2CR_L2E) == 0) {
    783 		aprint_normal(" L2 cache present but not enabled ");
    784 		return;
    785 	}
    786 
    787 	aprint_normal(",");
    788 	if ((pvr >> 16) == IBM750FX ||
    789 	    (pvr & 0xffffff00) == 0x00082200 /* IBM750CX */ ||
    790 	    (pvr & 0xffffef00) == 0x00082300 /* IBM750CXe */) {
    791 		cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
    792 	} else {
    793 		cpu_fmttab_print(cpu_l2cr_formats, l2cr);
    794 	}
    795 }
    796 
    797 void
    798 cpu_config_l3cr(int vers)
    799 {
    800 	register_t l2cr;
    801 	register_t l3cr;
    802 
    803 	l2cr = mfspr(SPR_L2CR);
    804 
    805 	/*
    806 	 * For MP systems, the firmware may only configure the L2 cache
    807 	 * on the first CPU.  In this case, assume that the other CPUs
    808 	 * should use the same value for L2CR.
    809 	 */
    810 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
    811 		l2cr_config = l2cr;
    812 	}
    813 
    814 	/*
    815 	 * Configure L2 cache if not enabled.
    816 	 */
    817 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
    818 		cpu_enable_l2cr(l2cr_config);
    819 		l2cr = mfspr(SPR_L2CR);
    820 	}
    821 
    822 	aprint_normal(",");
    823 	switch (vers) {
    824 	case MPC7447A:
    825 	case MPC7457:
    826 		cpu_fmttab_print(cpu_7457_l2cr_formats, l2cr);
    827 		return;
    828 	case MPC7448:
    829 		cpu_fmttab_print(cpu_7448_l2cr_formats, l2cr);
    830 		return;
    831 	default:
    832 		cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
    833 		break;
    834 	}
    835 
    836 	l3cr = mfspr(SPR_L3CR);
    837 
    838 	/*
    839 	 * For MP systems, the firmware may only configure the L3 cache
    840 	 * on the first CPU.  In this case, assume that the other CPUs
    841 	 * should use the same value for L3CR.
    842 	 */
    843 	if ((l3cr & L3CR_L3E) != 0 && l3cr_config == 0) {
    844 		l3cr_config = l3cr;
    845 	}
    846 
    847 	/*
    848 	 * Configure L3 cache if not enabled.
    849 	 */
    850 	if ((l3cr & L3CR_L3E) == 0 && l3cr_config != 0) {
    851 		cpu_enable_l3cr(l3cr_config);
    852 		l3cr = mfspr(SPR_L3CR);
    853 	}
    854 
    855 	if (l3cr & L3CR_L3E) {
    856 		aprint_normal(",");
    857 		cpu_fmttab_print(cpu_7450_l3cr_formats, l3cr);
    858 	}
    859 }
    860 
    861 void
    862 cpu_probe_speed(struct cpu_info *ci)
    863 {
    864 	uint64_t cps;
    865 
    866 	mtspr(SPR_MMCR0, MMCR0_FC);
    867 	mtspr(SPR_PMC1, 0);
    868 	mtspr(SPR_MMCR0, MMCR0_PMC1SEL(PMCN_CYCLES));
    869 	delay(100000);
    870 	cps = (mfspr(SPR_PMC1) * 10) + 4999;
    871 
    872 	mtspr(SPR_MMCR0, MMCR0_FC);
    873 
    874 	ci->ci_khz = cps / 1000;
    875 }
    876 
    877 #if NSYSMON_ENVSYS > 0
    878 void
    879 cpu_tau_setup(struct cpu_info *ci)
    880 {
    881 	struct {
    882 		struct sysmon_envsys sme;
    883 		envsys_data_t edata;
    884 	} *datap;
    885 	int error;
    886 
    887 	datap = malloc(sizeof(*datap), M_DEVBUF, M_WAITOK | M_ZERO);
    888 
    889 	datap->edata.sensor = 0;
    890 	datap->edata.units = ENVSYS_STEMP;
    891 	datap->edata.state = ENVSYS_SVALID;
    892 	(void)strlcpy(datap->edata.desc, "CPU Temp",
    893 	    sizeof(datap->edata.desc));
    894 
    895 	ci->ci_sysmon_cookie = &datap->sme;
    896 	datap->sme.sme_nsensors = 1;
    897 	datap->sme.sme_sensor_data = &datap->edata;
    898 	datap->sme.sme_name = ci->ci_dev->dv_xname;
    899 	datap->sme.sme_cookie = ci;
    900 	datap->sme.sme_gtredata = cpu_tau_gtredata;
    901 
    902 	if ((error = sysmon_envsys_register(&datap->sme)) != 0)
    903 		aprint_error("%s: unable to register with sysmon (%d)\n",
    904 		    ci->ci_dev->dv_xname, error);
    905 }
    906 
    907 
    908 /* Find the temperature of the CPU. */
    909 int
    910 cpu_tau_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
    911 {
    912 	int i, threshold, count;
    913 
    914 	if (edata->sensor != 0) {
    915 		edata->state = ENVSYS_SINVALID;
    916 		return 0;
    917 	}
    918 
    919 	threshold = 64; /* Half of the 7-bit sensor range */
    920 	mtspr(SPR_THRM1, 0);
    921 	mtspr(SPR_THRM2, 0);
    922 	/* XXX This counter is supposed to be "at least 20 microseonds, in
    923 	 * XXX units of clock cycles". Since we don't have convenient
    924 	 * XXX access to the CPU speed, set it to a conservative value,
    925 	 * XXX that is, assuming a fast (1GHz) G3 CPU (As of February 2002,
    926 	 * XXX the fastest G3 processor is 700MHz) . The cost is that
    927 	 * XXX measuring the temperature takes a bit longer.
    928 	 */
    929         mtspr(SPR_THRM3, SPR_THRM_TIMER(20000) | SPR_THRM_ENABLE);
    930 
    931 	/* Successive-approximation code adapted from Motorola
    932 	 * application note AN1800/D, "Programming the Thermal Assist
    933 	 * Unit in the MPC750 Microprocessor".
    934 	 */
    935 	for (i = 4; i >= 0 ; i--) {
    936 		mtspr(SPR_THRM1,
    937 		    SPR_THRM_THRESHOLD(threshold) | SPR_THRM_VALID);
    938 		count = 0;
    939 		while ((count < 100) &&
    940 		    ((mfspr(SPR_THRM1) & SPR_THRM_TIV) == 0)) {
    941 			count++;
    942 			delay(1);
    943 		}
    944 		if (mfspr(SPR_THRM1) & SPR_THRM_TIN) {
    945 			/* The interrupt bit was set, meaning the
    946 			 * temperature was above the threshold
    947 			 */
    948 			threshold += 2 << i;
    949 		} else {
    950 			/* Temperature was below the threshold */
    951 			threshold -= 2 << i;
    952 		}
    953 	}
    954 	threshold += 2;
    955 
    956 	/* Convert the temperature in degrees C to microkelvin */
    957 	sme->sme_sensor_data->value_cur = (threshold * 1000000) + 273150000;
    958 
    959 	return 0;
    960 }
    961 #endif /* NSYSMON_ENVSYS > 0 */
    962