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