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