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