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