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