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