Home | History | Annotate | Line # | Download | only in oea
cpu_subr.c revision 1.9
      1 /*	$NetBSD: cpu_subr.c,v 1.9 2003/07/15 02:54:45 lukem 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.9 2003/07/15 02:54:45 lukem 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_isi, EVCNT_TYPE_TRAP,
    459 		&ci->ci_ev_traps, self->dv_xname, "user ISI traps");
    460 	evcnt_attach_dynamic(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
    461 		&ci->ci_ev_isi, self->dv_xname, "user ISI failures");
    462 	evcnt_attach_dynamic(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
    463 		&ci->ci_ev_traps, self->dv_xname, "system call traps");
    464 	evcnt_attach_dynamic(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
    465 		&ci->ci_ev_traps, self->dv_xname, "PGM traps");
    466 	evcnt_attach_dynamic(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
    467 		&ci->ci_ev_traps, self->dv_xname, "FPU unavailable traps");
    468 	evcnt_attach_dynamic(&ci->ci_ev_fpusw, EVCNT_TYPE_TRAP,
    469 		&ci->ci_ev_fpu, self->dv_xname, "FPU context switches");
    470 	evcnt_attach_dynamic(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
    471 		&ci->ci_ev_traps, self->dv_xname, "user alignment traps");
    472 	evcnt_attach_dynamic(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
    473 		&ci->ci_ev_ali, self->dv_xname, "user alignment traps");
    474 	evcnt_attach_dynamic(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
    475 		&ci->ci_ev_umchk, self->dv_xname, "user MCHK failures");
    476 	evcnt_attach_dynamic(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
    477 		&ci->ci_ev_traps, self->dv_xname, "AltiVec unavailable");
    478 #ifdef ALTIVEC
    479 	if (cpu_altivec) {
    480 		evcnt_attach_dynamic(&ci->ci_ev_vecsw, EVCNT_TYPE_TRAP,
    481 		    &ci->ci_ev_vec, self->dv_xname, "AltiVec context switches");
    482 	}
    483 #endif
    484 }
    485 
    486 void
    487 cpu_identify(char *str, size_t len)
    488 {
    489 	u_int pvr, maj, min;
    490 	uint16_t vers, rev, revfmt;
    491 	const struct cputab *cp;
    492 	const char *name;
    493 	size_t n;
    494 
    495 	pvr = mfpvr();
    496 	vers = pvr >> 16;
    497 	rev = pvr;
    498 	switch (vers) {
    499 	case MPC7410:
    500 		min = (pvr >> 0) & 0xff;
    501 		maj = min <= 4 ? 1 : 2;
    502 		break;
    503 	default:
    504 		maj = (pvr >>  8) & 0xf;
    505 		min = (pvr >>  0) & 0xf;
    506 	}
    507 
    508 	for (cp = models; cp->name[0] != '\0'; cp++) {
    509 		if (cp->version == vers)
    510 			break;
    511 	}
    512 
    513 	if (str == NULL) {
    514 		str = cpu_model;
    515 		len = sizeof(cpu_model);
    516 		cpu = vers;
    517 	}
    518 
    519 	revfmt = cp->revfmt;
    520 	name = cp->name;
    521 	if (rev == MPC750 && pvr == 15) {
    522 		name = "755";
    523 		revfmt = REVFMT_HEX;
    524 	}
    525 
    526 	if (cp->name[0] != '\0') {
    527 		n = snprintf(str, len, "%s (Revision ", cp->name);
    528 	} else {
    529 		n = snprintf(str, len, "Version %#x (Revision ", vers);
    530 	}
    531 	if (len > n) {
    532 		switch (revfmt) {
    533 		case REVFMT_MAJMIN:
    534 			snprintf(str + n, len - n, "%u.%u)", maj, min);
    535 			break;
    536 		case REVFMT_HEX:
    537 			snprintf(str + n, len - n, "0x%04x)", rev);
    538 			break;
    539 		case REVFMT_DEC:
    540 			snprintf(str + n, len - n, "%u)", rev);
    541 			break;
    542 		}
    543 	}
    544 }
    545 
    546 #ifdef L2CR_CONFIG
    547 u_int l2cr_config = L2CR_CONFIG;
    548 #else
    549 u_int l2cr_config = 0;
    550 #endif
    551 
    552 #ifdef L3CR_CONFIG
    553 u_int l3cr_config = L3CR_CONFIG;
    554 #else
    555 u_int l3cr_config = 0;
    556 #endif
    557 
    558 void
    559 cpu_enable_l2cr(register_t l2cr)
    560 {
    561 	register_t msr, x;
    562 
    563 	/* Disable interrupts and set the cache config bits. */
    564 	msr = mfmsr();
    565 	mtmsr(msr & ~PSL_EE);
    566 #ifdef ALTIVEC
    567 	if (cpu_altivec)
    568 		__asm __volatile("dssall");
    569 #endif
    570 	__asm __volatile("sync");
    571 	mtspr(SPR_L2CR, l2cr & ~L2CR_L2E);
    572 	__asm __volatile("sync");
    573 
    574 	/* Wait for L2 clock to be stable (640 L2 clocks). */
    575 	delay(100);
    576 
    577 	/* Invalidate all L2 contents. */
    578 	mtspr(SPR_L2CR, l2cr | L2CR_L2I);
    579 	do {
    580 		x = mfspr(SPR_L2CR);
    581 	} while (x & L2CR_L2IP);
    582 
    583 	/* Enable L2 cache. */
    584 	l2cr |= L2CR_L2E;
    585 	mtspr(SPR_L2CR, l2cr);
    586 	mtmsr(msr);
    587 }
    588 
    589 void
    590 cpu_enable_l3cr(register_t l3cr)
    591 {
    592 	register_t x;
    593 
    594 	/* By The Book (numbered steps from section 3.7.1.3 of MPC7450UM) */
    595 
    596 	/*
    597 	 * 1: Set all L3CR bits for final config except L3E, L3I, L3PE, and
    598 	 *    L3CLKEN.  (also mask off reserved bits in case they were included
    599 	 *    in L3CR_CONFIG)
    600 	 */
    601 	l3cr &= ~(L3CR_L3E|L3CR_L3I|L3CR_L3PE|L3CR_L3CLKEN|L3CR_RESERVED);
    602 	mtspr(SPR_L3CR, l3cr);
    603 
    604 	/* 2: Set L3CR[5] (otherwise reserved bit) to 1 */
    605 	l3cr |= 0x04000000;
    606 	mtspr(SPR_L3CR, l3cr);
    607 
    608 	/* 3: Set L3CLKEN to 1*/
    609 	l3cr |= L3CR_L3CLKEN;
    610 	mtspr(SPR_L3CR, l3cr);
    611 
    612 	/* 4/5: Perform a global cache invalidate (ref section 3.7.3.6) */
    613 	__asm __volatile("dssall;sync");
    614 	/* L3 cache is already disabled, no need to clear L3E */
    615 	mtspr(SPR_L3CR, l3cr|L3CR_L3I);
    616 	do {
    617 		x = mfspr(SPR_L3CR);
    618 	} while (x & L3CR_L3I);
    619 
    620 	/* 6: Clear L3CLKEN to 0 */
    621 	l3cr &= ~L3CR_L3CLKEN;
    622 	mtspr(SPR_L3CR, l3cr);
    623 
    624 	/* 7: Perform a 'sync' and wait at least 100 CPU cycles */
    625 	__asm __volatile("sync");
    626 	delay(100);
    627 
    628 	/* 8: Set L3E and L3CLKEN */
    629 	l3cr |= (L3CR_L3E|L3CR_L3CLKEN);
    630 	mtspr(SPR_L3CR, l3cr);
    631 
    632 	/* 9: Perform a 'sync' and wait at least 100 CPU cycles */
    633 	__asm __volatile("sync");
    634 	delay(100);
    635 }
    636 
    637 void
    638 cpu_config_l2cr(int pvr)
    639 {
    640 	register_t l2cr;
    641 
    642 	l2cr = mfspr(SPR_L2CR);
    643 
    644 	/*
    645 	 * For MP systems, the firmware may only configure the L2 cache
    646 	 * on the first CPU.  In this case, assume that the other CPUs
    647 	 * should use the same value for L2CR.
    648 	 */
    649 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
    650 		l2cr_config = l2cr;
    651 	}
    652 
    653 	/*
    654 	 * Configure L2 cache if not enabled.
    655 	 */
    656 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
    657 		cpu_enable_l2cr(l2cr_config);
    658 		l2cr = mfspr(SPR_L2CR);
    659 	}
    660 
    661 	if ((l2cr & L2CR_L2E) == 0)
    662 		return;
    663 
    664 	aprint_normal(",");
    665 	if ((pvr >> 16) == IBM750FX ||
    666 	    (pvr & 0xffffff00) == 0x00082200 /* IBM750CX */ ||
    667 	    (pvr & 0xffffef00) == 0x00082300 /* IBM750CXe */) {
    668 		cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
    669 	} else {
    670 		cpu_fmttab_print(cpu_l2cr_formats, l2cr);
    671 	}
    672 }
    673 
    674 void
    675 cpu_config_l3cr(int vers)
    676 {
    677 	register_t l2cr;
    678 	register_t l3cr;
    679 
    680 	l2cr = mfspr(SPR_L2CR);
    681 
    682 	/*
    683 	 * For MP systems, the firmware may only configure the L2 cache
    684 	 * on the first CPU.  In this case, assume that the other CPUs
    685 	 * should use the same value for L2CR.
    686 	 */
    687 	if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
    688 		l2cr_config = l2cr;
    689 	}
    690 
    691 	/*
    692 	 * Configure L2 cache if not enabled.
    693 	 */
    694 	if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
    695 		cpu_enable_l2cr(l2cr_config);
    696 		l2cr = mfspr(SPR_L2CR);
    697 	}
    698 
    699 	aprint_normal(",");
    700 	cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
    701 
    702 	l3cr = mfspr(SPR_L3CR);
    703 
    704 	/*
    705 	 * For MP systems, the firmware may only configure the L3 cache
    706 	 * on the first CPU.  In this case, assume that the other CPUs
    707 	 * should use the same value for L3CR.
    708 	 */
    709 	if ((l3cr & L3CR_L3E) != 0 && l3cr_config == 0) {
    710 		l3cr_config = l3cr;
    711 	}
    712 
    713 	/*
    714 	 * Configure L3 cache if not enabled.
    715 	 */
    716 	if ((l3cr & L3CR_L3E) == 0 && l3cr_config != 0) {
    717 		cpu_enable_l3cr(l3cr_config);
    718 		l3cr = mfspr(SPR_L3CR);
    719 	}
    720 
    721 	if (l3cr & L3CR_L3E) {
    722 		aprint_normal(",");
    723 		cpu_fmttab_print(cpu_7450_l3cr_formats, l3cr);
    724 	}
    725 }
    726 
    727 void
    728 cpu_print_speed(void)
    729 {
    730 	uint64_t cps;
    731 
    732 	mtspr(SPR_MMCR0, MMCR0_FC);
    733 	mtspr(SPR_PMC1, 0);
    734 	mtspr(SPR_MMCR0, MMCR0_PMC1SEL(PMCN_CYCLES));
    735 	delay(100000);
    736 	cps = (mfspr(SPR_PMC1) * 10) + 4999;
    737 
    738 	aprint_normal("%lld.%02lld MHz", cps / 1000000, (cps / 10000) % 100);
    739 }
    740 
    741 #if NSYSMON_ENVSYS > 0
    742 const struct envsys_range cpu_tau_ranges[] = {
    743 	{ 0, 0, ENVSYS_STEMP}
    744 };
    745 
    746 struct envsys_basic_info cpu_tau_info[] = {
    747 	{ 0, ENVSYS_STEMP, "CPU temp", 0, 0, ENVSYS_FVALID}
    748 };
    749 
    750 void
    751 cpu_tau_setup(struct cpu_info *ci)
    752 {
    753 	struct sysmon_envsys *sme;
    754 	int error;
    755 
    756 	sme = &ci->ci_sysmon;
    757 	sme->sme_nsensors = 1;
    758 	sme->sme_envsys_version = 1000;
    759 	sme->sme_ranges = cpu_tau_ranges;
    760 	sme->sme_sensor_info = cpu_tau_info;
    761 	sme->sme_sensor_data = &ci->ci_tau_info;
    762 
    763 	sme->sme_sensor_data->sensor = 0;
    764 	sme->sme_sensor_data->warnflags = ENVSYS_WARN_OK;
    765 	sme->sme_sensor_data->validflags = ENVSYS_FVALID|ENVSYS_FCURVALID;
    766 	sme->sme_cookie = ci;
    767 	sme->sme_gtredata = cpu_tau_gtredata;
    768 	sme->sme_streinfo = cpu_tau_streinfo;
    769 
    770 	if ((error = sysmon_envsys_register(sme)) != 0)
    771 		aprint_error("%s: unable to register with sysmon (%d)\n",
    772 		    ci->ci_dev->dv_xname, error);
    773 }
    774 
    775 
    776 /* Find the temperature of the CPU. */
    777 int
    778 cpu_tau_gtredata(sme, tred)
    779 	 struct sysmon_envsys *sme;
    780 	 struct envsys_tre_data *tred;
    781 {
    782 	struct cpu_info *ci;
    783 	int i, threshold, count;
    784 
    785 	if (tred->sensor != 0) {
    786 		tred->validflags = 0;
    787 		return 0;
    788 	}
    789 
    790 	threshold = 64; /* Half of the 7-bit sensor range */
    791 	mtspr(SPR_THRM1, 0);
    792 	mtspr(SPR_THRM2, 0);
    793 	/* XXX This counter is supposed to be "at least 20 microseonds, in
    794 	 * XXX units of clock cycles". Since we don't have convenient
    795 	 * XXX access to the CPU speed, set it to a conservative value,
    796 	 * XXX that is, assuming a fast (1GHz) G3 CPU (As of February 2002,
    797 	 * XXX the fastest G3 processor is 700MHz) . The cost is that
    798 	 * XXX measuring the temperature takes a bit longer.
    799 	 */
    800         mtspr(SPR_THRM3, SPR_THRM_TIMER(20000) | SPR_THRM_ENABLE);
    801 
    802 	/* Successive-approximation code adapted from Motorola
    803 	 * application note AN1800/D, "Programming the Thermal Assist
    804 	 * Unit in the MPC750 Microprocessor".
    805 	 */
    806 	for (i = 4; i >= 0 ; i--) {
    807 		mtspr(SPR_THRM1,
    808 		    SPR_THRM_THRESHOLD(threshold) | SPR_THRM_VALID);
    809 		count = 0;
    810 		while ((count < 100) &&
    811 		    ((mfspr(SPR_THRM1) & SPR_THRM_TIV) == 0)) {
    812 			count++;
    813 			delay(1);
    814 		}
    815 		if (mfspr(SPR_THRM1) & SPR_THRM_TIN) {
    816 			/* The interrupt bit was set, meaning the
    817 			 * temperature was above the threshold
    818 			 */
    819 			threshold += 2 << i;
    820 		} else {
    821 			/* Temperature was below the threshold */
    822 			threshold -= 2 << i;
    823 		}
    824 	}
    825 	threshold += 2;
    826 
    827 	ci = (struct cpu_info *)sme->sme_cookie;
    828 	/* Convert the temperature in degrees C to microkelvin */
    829 	ci->ci_tau_info.cur.data_us = (threshold * 1000000) + 273150000;
    830 
    831 	*tred = ci->ci_tau_info;
    832 
    833 	return 0;
    834 }
    835 
    836 int
    837 cpu_tau_streinfo(sme, binfo)
    838 	 struct sysmon_envsys *sme;
    839 	 struct envsys_basic_info *binfo;
    840 {
    841 
    842 	/* There is nothing to set here. */
    843 	return (EINVAL);
    844 }
    845 #endif /* NSYSMON_ENVSYS > 0 */
    846