cpu_subr.c revision 1.42 1 /* $NetBSD: cpu_subr.c,v 1.42 2008/02/05 18:10:47 garbled 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.42 2008/02/05 18:10:47 garbled 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/types.h>
48 #include <sys/lwp.h>
49 #include <sys/user.h>
50 #include <sys/malloc.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #include <powerpc/oea/hid.h>
55 #include <powerpc/oea/hid_601.h>
56 #include <powerpc/spr.h>
57 #include <powerpc/oea/cpufeat.h>
58
59 #include <dev/sysmon/sysmonvar.h>
60
61 static void cpu_enable_l2cr(register_t);
62 static void cpu_enable_l3cr(register_t);
63 static void cpu_config_l2cr(int);
64 static void cpu_config_l3cr(int);
65 static void cpu_probe_speed(struct cpu_info *);
66 static void cpu_idlespin(void);
67 #if NSYSMON_ENVSYS > 0
68 static void cpu_tau_setup(struct cpu_info *);
69 static void cpu_tau_refresh(struct sysmon_envsys *, envsys_data_t *);
70 #endif
71
72 int cpu;
73 int ncpus;
74
75 struct fmttab {
76 register_t fmt_mask;
77 register_t fmt_value;
78 const char *fmt_string;
79 };
80
81 static const struct fmttab cpu_7450_l2cr_formats[] = {
82 { L2CR_L2E, 0, " disabled" },
83 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
84 { L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
85 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
86 { L2CR_L2E, ~0, " 256KB L2 cache" },
87 { L2CR_L2PE, 0, " no parity" },
88 { L2CR_L2PE, ~0, " parity enabled" },
89 { 0, 0, NULL }
90 };
91
92 static const struct fmttab cpu_7448_l2cr_formats[] = {
93 { L2CR_L2E, 0, " disabled" },
94 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
95 { L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
96 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
97 { L2CR_L2E, ~0, " 1MB L2 cache" },
98 { L2CR_L2PE, 0, " no parity" },
99 { L2CR_L2PE, ~0, " parity enabled" },
100 { 0, 0, NULL }
101 };
102
103 static const struct fmttab cpu_7457_l2cr_formats[] = {
104 { L2CR_L2E, 0, " disabled" },
105 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
106 { L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
107 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
108 { L2CR_L2E, ~0, " 512KB L2 cache" },
109 { L2CR_L2PE, 0, " no parity" },
110 { L2CR_L2PE, ~0, " parity enabled" },
111 { 0, 0, NULL }
112 };
113
114 static const struct fmttab cpu_7450_l3cr_formats[] = {
115 { L3CR_L3DO|L3CR_L3IO, L3CR_L3DO, " data-only" },
116 { L3CR_L3DO|L3CR_L3IO, L3CR_L3IO, " instruction-only" },
117 { L3CR_L3DO|L3CR_L3IO, L3CR_L3DO|L3CR_L3IO, " locked" },
118 { L3CR_L3SIZ, L3SIZ_2M, " 2MB" },
119 { L3CR_L3SIZ, L3SIZ_1M, " 1MB" },
120 { L3CR_L3PE|L3CR_L3APE, L3CR_L3PE|L3CR_L3APE, " parity" },
121 { L3CR_L3PE|L3CR_L3APE, L3CR_L3PE, " data-parity" },
122 { L3CR_L3PE|L3CR_L3APE, L3CR_L3APE, " address-parity" },
123 { L3CR_L3PE|L3CR_L3APE, 0, " no-parity" },
124 { L3CR_L3SIZ, ~0, " L3 cache" },
125 { L3CR_L3RT, L3RT_MSUG2_DDR, " (DDR SRAM)" },
126 { L3CR_L3RT, L3RT_PIPELINE_LATE, " (LW SRAM)" },
127 { L3CR_L3RT, L3RT_PB2_SRAM, " (PB2 SRAM)" },
128 { L3CR_L3CLK, ~0, " at" },
129 { L3CR_L3CLK, L3CLK_20, " 2:1" },
130 { L3CR_L3CLK, L3CLK_25, " 2.5:1" },
131 { L3CR_L3CLK, L3CLK_30, " 3:1" },
132 { L3CR_L3CLK, L3CLK_35, " 3.5:1" },
133 { L3CR_L3CLK, L3CLK_40, " 4:1" },
134 { L3CR_L3CLK, L3CLK_50, " 5:1" },
135 { L3CR_L3CLK, L3CLK_60, " 6:1" },
136 { L3CR_L3CLK, ~0, " ratio" },
137 { 0, 0, NULL },
138 };
139
140 static const struct fmttab cpu_ibm750_l2cr_formats[] = {
141 { L2CR_L2E, 0, " disabled" },
142 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
143 { L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
144 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
145 { 0, ~0, " 512KB" },
146 { L2CR_L2WT, L2CR_L2WT, " WT" },
147 { L2CR_L2WT, 0, " WB" },
148 { L2CR_L2PE, L2CR_L2PE, " with ECC" },
149 { 0, ~0, " L2 cache" },
150 { 0, 0, NULL }
151 };
152
153 static const struct fmttab cpu_l2cr_formats[] = {
154 { L2CR_L2E, 0, " disabled" },
155 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO, " data-only" },
156 { L2CR_L2DO|L2CR_L2IO, L2CR_L2IO, " instruction-only" },
157 { L2CR_L2DO|L2CR_L2IO, L2CR_L2DO|L2CR_L2IO, " locked" },
158 { L2CR_L2PE, L2CR_L2PE, " parity" },
159 { L2CR_L2PE, 0, " no-parity" },
160 { L2CR_L2SIZ, L2SIZ_2M, " 2MB" },
161 { L2CR_L2SIZ, L2SIZ_1M, " 1MB" },
162 { L2CR_L2SIZ, L2SIZ_512K, " 512KB" },
163 { L2CR_L2SIZ, L2SIZ_256K, " 256KB" },
164 { L2CR_L2WT, L2CR_L2WT, " WT" },
165 { L2CR_L2WT, 0, " WB" },
166 { L2CR_L2E, ~0, " L2 cache" },
167 { L2CR_L2RAM, L2RAM_FLOWTHRU_BURST, " (FB SRAM)" },
168 { L2CR_L2RAM, L2RAM_PIPELINE_LATE, " (LW SRAM)" },
169 { L2CR_L2RAM, L2RAM_PIPELINE_BURST, " (PB SRAM)" },
170 { L2CR_L2CLK, ~0, " at" },
171 { L2CR_L2CLK, L2CLK_10, " 1:1" },
172 { L2CR_L2CLK, L2CLK_15, " 1.5:1" },
173 { L2CR_L2CLK, L2CLK_20, " 2:1" },
174 { L2CR_L2CLK, L2CLK_25, " 2.5:1" },
175 { L2CR_L2CLK, L2CLK_30, " 3:1" },
176 { L2CR_L2CLK, L2CLK_35, " 3.5:1" },
177 { L2CR_L2CLK, L2CLK_40, " 4:1" },
178 { L2CR_L2CLK, ~0, " ratio" },
179 { 0, 0, NULL }
180 };
181
182 static void cpu_fmttab_print(const struct fmttab *, register_t);
183
184 struct cputab {
185 const char name[8];
186 uint16_t version;
187 uint16_t revfmt;
188 };
189 #define REVFMT_MAJMIN 1 /* %u.%u */
190 #define REVFMT_HEX 2 /* 0x%04x */
191 #define REVFMT_DEC 3 /* %u */
192 static const struct cputab models[] = {
193 { "601", MPC601, REVFMT_DEC },
194 { "602", MPC602, REVFMT_DEC },
195 { "603", MPC603, REVFMT_MAJMIN },
196 { "603e", MPC603e, REVFMT_MAJMIN },
197 { "603ev", MPC603ev, REVFMT_MAJMIN },
198 { "G2", MPCG2, REVFMT_MAJMIN },
199 { "604", MPC604, REVFMT_MAJMIN },
200 { "604e", MPC604e, REVFMT_MAJMIN },
201 { "604ev", MPC604ev, REVFMT_MAJMIN },
202 { "620", MPC620, REVFMT_HEX },
203 { "750", MPC750, REVFMT_MAJMIN },
204 { "750FX", IBM750FX, REVFMT_MAJMIN },
205 { "7400", MPC7400, REVFMT_MAJMIN },
206 { "7410", MPC7410, REVFMT_MAJMIN },
207 { "7450", MPC7450, REVFMT_MAJMIN },
208 { "7455", MPC7455, REVFMT_MAJMIN },
209 { "7457", MPC7457, REVFMT_MAJMIN },
210 { "7447A", MPC7447A, REVFMT_MAJMIN },
211 { "7448", MPC7448, REVFMT_MAJMIN },
212 { "8240", MPC8240, REVFMT_MAJMIN },
213 { "8245", MPC8245, REVFMT_MAJMIN },
214 { "970", IBM970, REVFMT_MAJMIN },
215 { "970FX", IBM970FX, REVFMT_MAJMIN },
216 { "POWER3II", IBMPOWER3II, REVFMT_MAJMIN },
217 { "", 0, REVFMT_HEX }
218 };
219
220 #ifdef MULTIPROCESSOR
221 struct cpu_info cpu_info[CPU_MAXNUM] = { { .ci_curlwp = &lwp0, }, };
222 volatile struct cpu_hatch_data *cpu_hatch_data;
223 volatile int cpu_hatch_stack;
224 extern int ticks_per_intr;
225 #include <powerpc/oea/bat.h>
226 #include <arch/powerpc/pic/picvar.h>
227 #include <arch/powerpc/pic/ipivar.h>
228 extern struct bat battable[];
229 #else
230 struct cpu_info cpu_info[1] = { { .ci_curlwp = &lwp0, }, };
231 #endif /*MULTIPROCESSOR*/
232
233 int cpu_altivec;
234 int cpu_psluserset, cpu_pslusermod;
235 char cpu_model[80];
236
237 /* This is to be called from locore.S, and nowhere else. */
238
239 extern unsigned long oeacpufeat;
240
241 void
242 cpu_model_init(void)
243 {
244 u_int pvr, vers;
245
246 pvr = mfpvr();
247 vers = pvr >> 16;
248
249 oeacpufeat = 0;
250
251 if ((vers >= IBMRS64II && vers <= IBM970GX) || vers == MPC620 ||
252 vers == IBMCELL || vers == IBMPOWER6P5)
253 oeacpufeat |= OEACPU_64 | OEACPU_64_BRIDGE | OEACPU_NOBAT;
254
255 if (vers == MPC601)
256 oeacpufeat |= OEACPU_601;
257 }
258
259 void
260 cpu_fmttab_print(const struct fmttab *fmt, register_t data)
261 {
262 for (; fmt->fmt_mask != 0 || fmt->fmt_value != 0; fmt++) {
263 if ((~fmt->fmt_mask & fmt->fmt_value) != 0 ||
264 (data & fmt->fmt_mask) == fmt->fmt_value)
265 aprint_normal("%s", fmt->fmt_string);
266 }
267 }
268
269 void
270 cpu_idlespin(void)
271 {
272 register_t msr;
273
274 if (powersave <= 0)
275 return;
276
277 __asm volatile(
278 "sync;"
279 "mfmsr %0;"
280 "oris %0,%0,%1@h;" /* enter power saving mode */
281 "mtmsr %0;"
282 "isync;"
283 : "=r"(msr)
284 : "J"(PSL_POW));
285 }
286
287 void
288 cpu_probe_cache(void)
289 {
290 u_int assoc, pvr, vers;
291
292 pvr = mfpvr();
293 vers = pvr >> 16;
294
295
296 /* Presently common across almost all implementations. */
297 curcpu()->ci_ci.dcache_line_size = CACHELINESIZE;
298 curcpu()->ci_ci.icache_line_size = CACHELINESIZE;
299
300
301 switch (vers) {
302 #define K *1024
303 case IBM750FX:
304 case MPC601:
305 case MPC750:
306 case MPC7447A:
307 case MPC7448:
308 case MPC7450:
309 case MPC7455:
310 case MPC7457:
311 curcpu()->ci_ci.dcache_size = 32 K;
312 curcpu()->ci_ci.icache_size = 32 K;
313 assoc = 8;
314 break;
315 case MPC603:
316 curcpu()->ci_ci.dcache_size = 8 K;
317 curcpu()->ci_ci.icache_size = 8 K;
318 assoc = 2;
319 break;
320 case MPC603e:
321 case MPC603ev:
322 case MPC604:
323 case MPC8240:
324 case MPC8245:
325 case MPCG2:
326 curcpu()->ci_ci.dcache_size = 16 K;
327 curcpu()->ci_ci.icache_size = 16 K;
328 assoc = 4;
329 break;
330 case MPC604e:
331 case MPC604ev:
332 curcpu()->ci_ci.dcache_size = 32 K;
333 curcpu()->ci_ci.icache_size = 32 K;
334 assoc = 4;
335 break;
336 case IBMPOWER3II:
337 curcpu()->ci_ci.dcache_size = 64 K;
338 curcpu()->ci_ci.icache_size = 32 K;
339 curcpu()->ci_ci.dcache_line_size = 128;
340 curcpu()->ci_ci.icache_line_size = 128;
341 assoc = 128; /* not a typo */
342 break;
343 case IBM970:
344 case IBM970FX:
345 curcpu()->ci_ci.dcache_size = 32 K;
346 curcpu()->ci_ci.icache_size = 64 K;
347 curcpu()->ci_ci.dcache_line_size = 128;
348 curcpu()->ci_ci.icache_line_size = 128;
349 assoc = 2;
350 break;
351
352 default:
353 curcpu()->ci_ci.dcache_size = PAGE_SIZE;
354 curcpu()->ci_ci.icache_size = PAGE_SIZE;
355 assoc = 1;
356 #undef K
357 }
358
359 /*
360 * Possibly recolor.
361 */
362 uvm_page_recolor(atop(curcpu()->ci_ci.dcache_size / assoc));
363 }
364
365 struct cpu_info *
366 cpu_attach_common(struct device *self, int id)
367 {
368 struct cpu_info *ci;
369 u_int pvr, vers;
370
371 ci = &cpu_info[id];
372 #ifndef MULTIPROCESSOR
373 /*
374 * If this isn't the primary CPU, print an error message
375 * and just bail out.
376 */
377 if (id != 0) {
378 aprint_normal(": ID %d\n", id);
379 aprint_normal("%s: processor off-line; multiprocessor support "
380 "not present in kernel\n", self->dv_xname);
381 return (NULL);
382 }
383 #endif
384
385 ci->ci_cpuid = id;
386 ci->ci_intrdepth = -1;
387 ci->ci_dev = self;
388 ci->ci_idlespin = cpu_idlespin;
389
390 pvr = mfpvr();
391 vers = (pvr >> 16) & 0xffff;
392
393 switch (id) {
394 case 0:
395 /* load my cpu_number to PIR */
396 switch (vers) {
397 case MPC601:
398 case MPC604:
399 case MPC604e:
400 case MPC604ev:
401 case MPC7400:
402 case MPC7410:
403 case MPC7447A:
404 case MPC7448:
405 case MPC7450:
406 case MPC7455:
407 case MPC7457:
408 mtspr(SPR_PIR, id);
409 }
410 cpu_setup(self, ci);
411 break;
412 default:
413 if (id >= CPU_MAXNUM) {
414 aprint_normal(": more than %d cpus?\n", CPU_MAXNUM);
415 panic("cpuattach");
416 }
417 #ifndef MULTIPROCESSOR
418 aprint_normal(" not configured\n");
419 return NULL;
420 #else
421 mi_cpu_attach(ci);
422 break;
423 #endif
424 }
425 return (ci);
426 }
427
428 void
429 cpu_setup(self, ci)
430 struct device *self;
431 struct cpu_info *ci;
432 {
433 u_int hid0, hid0_save, pvr, vers;
434 const char *bitmask;
435 char hidbuf[128];
436 char model[80];
437
438 pvr = mfpvr();
439 vers = (pvr >> 16) & 0xffff;
440
441 cpu_identify(model, sizeof(model));
442 aprint_normal(": %s, ID %d%s\n", model, cpu_number(),
443 cpu_number() == 0 ? " (primary)" : "");
444
445 hid0_save = hid0 = mfspr(SPR_HID0);
446
447 cpu_probe_cache();
448
449 /*
450 * Configure power-saving mode.
451 */
452 switch (vers) {
453 case MPC604:
454 case MPC604e:
455 case MPC604ev:
456 /*
457 * Do not have HID0 support settings, but can support
458 * MSR[POW] off
459 */
460 powersave = 1;
461 break;
462
463 case MPC603:
464 case MPC603e:
465 case MPC603ev:
466 case MPC750:
467 case IBM750FX:
468 case MPC7400:
469 case MPC7410:
470 case MPC8240:
471 case MPC8245:
472 case MPCG2:
473 /* Select DOZE mode. */
474 hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
475 hid0 |= HID0_DOZE | HID0_DPM;
476 powersave = 1;
477 break;
478
479 case MPC7447A:
480 case MPC7448:
481 case MPC7457:
482 case MPC7455:
483 case MPC7450:
484 /* Enable the 7450 branch caches */
485 hid0 |= HID0_SGE | HID0_BTIC;
486 hid0 |= HID0_LRSTK | HID0_FOLD | HID0_BHT;
487 /* Disable BTIC on 7450 Rev 2.0 or earlier */
488 if (vers == MPC7450 && (pvr & 0xFFFF) <= 0x0200)
489 hid0 &= ~HID0_BTIC;
490 /* Select NAP mode. */
491 hid0 &= ~(HID0_HIGH_BAT_EN | HID0_SLEEP);
492 hid0 |= HID0_NAP | HID0_DPM /* | HID0_XBSEN */;
493 powersave = 1;
494 break;
495
496 case IBM970:
497 case IBM970FX:
498 case IBMPOWER3II:
499 default:
500 /* No power-saving mode is available. */ ;
501 }
502
503 #ifdef NAPMODE
504 switch (vers) {
505 case IBM750FX:
506 case MPC750:
507 case MPC7400:
508 /* Select NAP mode. */
509 hid0 &= ~(HID0_DOZE | HID0_NAP | HID0_SLEEP);
510 hid0 |= HID0_NAP;
511 break;
512 }
513 #endif
514
515 switch (vers) {
516 case IBM750FX:
517 case MPC750:
518 hid0 &= ~HID0_DBP; /* XXX correct? */
519 hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
520 break;
521
522 case MPC7400:
523 case MPC7410:
524 hid0 &= ~HID0_SPD;
525 hid0 |= HID0_EMCP | HID0_BTIC | HID0_SGE | HID0_BHT;
526 hid0 |= HID0_EIEC;
527 break;
528 }
529
530 if (hid0 != hid0_save) {
531 mtspr(SPR_HID0, hid0);
532 __asm volatile("sync;isync");
533 }
534
535
536 switch (vers) {
537 case MPC601:
538 bitmask = HID0_601_BITMASK;
539 break;
540 case MPC7450:
541 case MPC7455:
542 case MPC7457:
543 bitmask = HID0_7450_BITMASK;
544 break;
545 case IBM970:
546 case IBM970FX:
547 bitmask = 0;
548 break;
549 default:
550 bitmask = HID0_BITMASK;
551 break;
552 }
553 bitmask_snprintf(hid0, bitmask, hidbuf, sizeof hidbuf);
554 aprint_normal("%s: HID0 %s, powersave: %d\n", self->dv_xname, hidbuf,
555 powersave);
556
557 ci->ci_khz = 0;
558
559 /*
560 * Display speed and cache configuration.
561 */
562 switch (vers) {
563 case MPC604:
564 case MPC604e:
565 case MPC604ev:
566 case MPC750:
567 case IBM750FX:
568 case MPC7400:
569 case MPC7410:
570 case MPC7447A:
571 case MPC7448:
572 case MPC7450:
573 case MPC7455:
574 case MPC7457:
575 aprint_normal("%s: ", self->dv_xname);
576 cpu_probe_speed(ci);
577 aprint_normal("%u.%02u MHz",
578 ci->ci_khz / 1000, (ci->ci_khz / 10) % 100);
579 switch (vers) {
580 case MPC7450: /* 7441 does not have L3! */
581 case MPC7455: /* 7445 does not have L3! */
582 case MPC7457: /* 7447 does not have L3! */
583 cpu_config_l3cr(vers);
584 break;
585 case IBM750FX:
586 case MPC750:
587 case MPC7400:
588 case MPC7410:
589 case MPC7447A:
590 case MPC7448:
591 cpu_config_l2cr(pvr);
592 break;
593 default:
594 break;
595 }
596 aprint_normal("\n");
597 break;
598 }
599
600 #if NSYSMON_ENVSYS > 0
601 /*
602 * Attach MPC750 temperature sensor to the envsys subsystem.
603 * XXX the 74xx series also has this sensor, but it is not
604 * XXX supported by Motorola and may return values that are off by
605 * XXX 35-55 degrees C.
606 */
607 if (vers == MPC750 || vers == IBM750FX)
608 cpu_tau_setup(ci);
609 #endif
610
611 evcnt_attach_dynamic(&ci->ci_ev_clock, EVCNT_TYPE_INTR,
612 NULL, self->dv_xname, "clock");
613 evcnt_attach_dynamic(&ci->ci_ev_softclock, EVCNT_TYPE_INTR,
614 NULL, self->dv_xname, "soft clock");
615 evcnt_attach_dynamic(&ci->ci_ev_softnet, EVCNT_TYPE_INTR,
616 NULL, self->dv_xname, "soft net");
617 evcnt_attach_dynamic(&ci->ci_ev_softserial, EVCNT_TYPE_INTR,
618 NULL, self->dv_xname, "soft serial");
619 evcnt_attach_dynamic(&ci->ci_ev_traps, EVCNT_TYPE_TRAP,
620 NULL, self->dv_xname, "traps");
621 evcnt_attach_dynamic(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP,
622 &ci->ci_ev_traps, self->dv_xname, "kernel DSI traps");
623 evcnt_attach_dynamic(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP,
624 &ci->ci_ev_traps, self->dv_xname, "user DSI traps");
625 evcnt_attach_dynamic(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP,
626 &ci->ci_ev_udsi, self->dv_xname, "user DSI failures");
627 evcnt_attach_dynamic(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP,
628 &ci->ci_ev_traps, self->dv_xname, "kernel ISI traps");
629 evcnt_attach_dynamic(&ci->ci_ev_isi, EVCNT_TYPE_TRAP,
630 &ci->ci_ev_traps, self->dv_xname, "user ISI traps");
631 evcnt_attach_dynamic(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
632 &ci->ci_ev_isi, self->dv_xname, "user ISI failures");
633 evcnt_attach_dynamic(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
634 &ci->ci_ev_traps, self->dv_xname, "system call traps");
635 evcnt_attach_dynamic(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
636 &ci->ci_ev_traps, self->dv_xname, "PGM traps");
637 evcnt_attach_dynamic(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
638 &ci->ci_ev_traps, self->dv_xname, "FPU unavailable traps");
639 evcnt_attach_dynamic(&ci->ci_ev_fpusw, EVCNT_TYPE_TRAP,
640 &ci->ci_ev_fpu, self->dv_xname, "FPU context switches");
641 evcnt_attach_dynamic(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
642 &ci->ci_ev_traps, self->dv_xname, "user alignment traps");
643 evcnt_attach_dynamic(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
644 &ci->ci_ev_ali, self->dv_xname, "user alignment traps");
645 evcnt_attach_dynamic(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
646 &ci->ci_ev_umchk, self->dv_xname, "user MCHK failures");
647 evcnt_attach_dynamic(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
648 &ci->ci_ev_traps, self->dv_xname, "AltiVec unavailable");
649 #ifdef ALTIVEC
650 if (cpu_altivec) {
651 evcnt_attach_dynamic(&ci->ci_ev_vecsw, EVCNT_TYPE_TRAP,
652 &ci->ci_ev_vec, self->dv_xname, "AltiVec context switches");
653 }
654 #endif
655 evcnt_attach_dynamic(&ci->ci_ev_ipi, EVCNT_TYPE_INTR,
656 NULL, self->dv_xname, "IPIs");
657 }
658
659 /*
660 * According to a document labeled "PVR Register Settings":
661 ** For integrated microprocessors the PVR register inside the device
662 ** will identify the version of the microprocessor core. You must also
663 ** read the Device ID, PCI register 02, to identify the part and the
664 ** Revision ID, PCI register 08, to identify the revision of the
665 ** integrated microprocessor.
666 * This apparently applies to 8240/8245/8241, PVR 00810101 and 80811014
667 */
668
669 void
670 cpu_identify(char *str, size_t len)
671 {
672 u_int pvr, major, minor;
673 uint16_t vers, rev, revfmt;
674 const struct cputab *cp;
675 const char *name;
676 size_t n;
677
678 pvr = mfpvr();
679 vers = pvr >> 16;
680 rev = pvr;
681
682 switch (vers) {
683 case MPC7410:
684 minor = (pvr >> 0) & 0xff;
685 major = minor <= 4 ? 1 : 2;
686 break;
687 case MPCG2: /*XXX see note above */
688 major = (pvr >> 4) & 0xf;
689 minor = (pvr >> 0) & 0xf;
690 break;
691 default:
692 major = (pvr >> 8) & 0xf;
693 minor = (pvr >> 0) & 0xf;
694 }
695
696 for (cp = models; cp->name[0] != '\0'; cp++) {
697 if (cp->version == vers)
698 break;
699 }
700
701 if (str == NULL) {
702 str = cpu_model;
703 len = sizeof(cpu_model);
704 cpu = vers;
705 }
706
707 revfmt = cp->revfmt;
708 name = cp->name;
709 if (rev == MPC750 && pvr == 15) {
710 name = "755";
711 revfmt = REVFMT_HEX;
712 }
713
714 if (cp->name[0] != '\0') {
715 n = snprintf(str, len, "%s (Revision ", cp->name);
716 } else {
717 n = snprintf(str, len, "Version %#x (Revision ", vers);
718 }
719 if (len > n) {
720 switch (revfmt) {
721 case REVFMT_MAJMIN:
722 snprintf(str + n, len - n, "%u.%u)", major, minor);
723 break;
724 case REVFMT_HEX:
725 snprintf(str + n, len - n, "0x%04x)", rev);
726 break;
727 case REVFMT_DEC:
728 snprintf(str + n, len - n, "%u)", rev);
729 break;
730 }
731 }
732 }
733
734 #ifdef L2CR_CONFIG
735 u_int l2cr_config = L2CR_CONFIG;
736 #else
737 u_int l2cr_config = 0;
738 #endif
739
740 #ifdef L3CR_CONFIG
741 u_int l3cr_config = L3CR_CONFIG;
742 #else
743 u_int l3cr_config = 0;
744 #endif
745
746 void
747 cpu_enable_l2cr(register_t l2cr)
748 {
749 register_t msr, x;
750 uint16_t vers;
751
752 vers = mfpvr() >> 16;
753
754 /* Disable interrupts and set the cache config bits. */
755 msr = mfmsr();
756 mtmsr(msr & ~PSL_EE);
757 #ifdef ALTIVEC
758 if (cpu_altivec)
759 __asm volatile("dssall");
760 #endif
761 __asm volatile("sync");
762 mtspr(SPR_L2CR, l2cr & ~L2CR_L2E);
763 __asm volatile("sync");
764
765 /* Wait for L2 clock to be stable (640 L2 clocks). */
766 delay(100);
767
768 /* Invalidate all L2 contents. */
769 if (MPC745X_P(vers)) {
770 mtspr(SPR_L2CR, l2cr | L2CR_L2I);
771 do {
772 x = mfspr(SPR_L2CR);
773 } while (x & L2CR_L2I);
774 } else {
775 mtspr(SPR_L2CR, l2cr | L2CR_L2I);
776 do {
777 x = mfspr(SPR_L2CR);
778 } while (x & L2CR_L2IP);
779 }
780 /* Enable L2 cache. */
781 l2cr |= L2CR_L2E;
782 mtspr(SPR_L2CR, l2cr);
783 mtmsr(msr);
784 }
785
786 void
787 cpu_enable_l3cr(register_t l3cr)
788 {
789 register_t x;
790
791 /* By The Book (numbered steps from section 3.7.1.3 of MPC7450UM) */
792
793 /*
794 * 1: Set all L3CR bits for final config except L3E, L3I, L3PE, and
795 * L3CLKEN. (also mask off reserved bits in case they were included
796 * in L3CR_CONFIG)
797 */
798 l3cr &= ~(L3CR_L3E|L3CR_L3I|L3CR_L3PE|L3CR_L3CLKEN|L3CR_RESERVED);
799 mtspr(SPR_L3CR, l3cr);
800
801 /* 2: Set L3CR[5] (otherwise reserved bit) to 1 */
802 l3cr |= 0x04000000;
803 mtspr(SPR_L3CR, l3cr);
804
805 /* 3: Set L3CLKEN to 1*/
806 l3cr |= L3CR_L3CLKEN;
807 mtspr(SPR_L3CR, l3cr);
808
809 /* 4/5: Perform a global cache invalidate (ref section 3.7.3.6) */
810 __asm volatile("dssall;sync");
811 /* L3 cache is already disabled, no need to clear L3E */
812 mtspr(SPR_L3CR, l3cr|L3CR_L3I);
813 do {
814 x = mfspr(SPR_L3CR);
815 } while (x & L3CR_L3I);
816
817 /* 6: Clear L3CLKEN to 0 */
818 l3cr &= ~L3CR_L3CLKEN;
819 mtspr(SPR_L3CR, l3cr);
820
821 /* 7: Perform a 'sync' and wait at least 100 CPU cycles */
822 __asm volatile("sync");
823 delay(100);
824
825 /* 8: Set L3E and L3CLKEN */
826 l3cr |= (L3CR_L3E|L3CR_L3CLKEN);
827 mtspr(SPR_L3CR, l3cr);
828
829 /* 9: Perform a 'sync' and wait at least 100 CPU cycles */
830 __asm volatile("sync");
831 delay(100);
832 }
833
834 void
835 cpu_config_l2cr(int pvr)
836 {
837 register_t l2cr;
838 u_int vers = (pvr >> 16) & 0xffff;
839
840 l2cr = mfspr(SPR_L2CR);
841
842 /*
843 * For MP systems, the firmware may only configure the L2 cache
844 * on the first CPU. In this case, assume that the other CPUs
845 * should use the same value for L2CR.
846 */
847 if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
848 l2cr_config = l2cr;
849 }
850
851 /*
852 * Configure L2 cache if not enabled.
853 */
854 if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
855 cpu_enable_l2cr(l2cr_config);
856 l2cr = mfspr(SPR_L2CR);
857 }
858
859 if ((l2cr & L2CR_L2E) == 0) {
860 aprint_normal(" L2 cache present but not enabled ");
861 return;
862 }
863 aprint_normal(",");
864
865 switch (vers) {
866 case IBM750FX:
867 cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
868 break;
869 case MPC750:
870 if ((pvr & 0xffffff00) == 0x00082200 /* IBM750CX */ ||
871 (pvr & 0xffffef00) == 0x00082300 /* IBM750CXe */)
872 cpu_fmttab_print(cpu_ibm750_l2cr_formats, l2cr);
873 else
874 cpu_fmttab_print(cpu_l2cr_formats, l2cr);
875 break;
876 case MPC7447A:
877 case MPC7457:
878 cpu_fmttab_print(cpu_7457_l2cr_formats, l2cr);
879 return;
880 case MPC7448:
881 cpu_fmttab_print(cpu_7448_l2cr_formats, l2cr);
882 return;
883 case MPC7450:
884 case MPC7455:
885 cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
886 break;
887 default:
888 cpu_fmttab_print(cpu_l2cr_formats, l2cr);
889 break;
890 }
891 }
892
893 void
894 cpu_config_l3cr(int vers)
895 {
896 register_t l2cr;
897 register_t l3cr;
898
899 l2cr = mfspr(SPR_L2CR);
900
901 /*
902 * For MP systems, the firmware may only configure the L2 cache
903 * on the first CPU. In this case, assume that the other CPUs
904 * should use the same value for L2CR.
905 */
906 if ((l2cr & L2CR_L2E) != 0 && l2cr_config == 0) {
907 l2cr_config = l2cr;
908 }
909
910 /*
911 * Configure L2 cache if not enabled.
912 */
913 if ((l2cr & L2CR_L2E) == 0 && l2cr_config != 0) {
914 cpu_enable_l2cr(l2cr_config);
915 l2cr = mfspr(SPR_L2CR);
916 }
917
918 aprint_normal(",");
919 switch (vers) {
920 case MPC7447A:
921 case MPC7457:
922 cpu_fmttab_print(cpu_7457_l2cr_formats, l2cr);
923 return;
924 case MPC7448:
925 cpu_fmttab_print(cpu_7448_l2cr_formats, l2cr);
926 return;
927 default:
928 cpu_fmttab_print(cpu_7450_l2cr_formats, l2cr);
929 break;
930 }
931
932 l3cr = mfspr(SPR_L3CR);
933
934 /*
935 * For MP systems, the firmware may only configure the L3 cache
936 * on the first CPU. In this case, assume that the other CPUs
937 * should use the same value for L3CR.
938 */
939 if ((l3cr & L3CR_L3E) != 0 && l3cr_config == 0) {
940 l3cr_config = l3cr;
941 }
942
943 /*
944 * Configure L3 cache if not enabled.
945 */
946 if ((l3cr & L3CR_L3E) == 0 && l3cr_config != 0) {
947 cpu_enable_l3cr(l3cr_config);
948 l3cr = mfspr(SPR_L3CR);
949 }
950
951 if (l3cr & L3CR_L3E) {
952 aprint_normal(",");
953 cpu_fmttab_print(cpu_7450_l3cr_formats, l3cr);
954 }
955 }
956
957 void
958 cpu_probe_speed(struct cpu_info *ci)
959 {
960 uint64_t cps;
961
962 mtspr(SPR_MMCR0, MMCR0_FC);
963 mtspr(SPR_PMC1, 0);
964 mtspr(SPR_MMCR0, MMCR0_PMC1SEL(PMCN_CYCLES));
965 delay(100000);
966 cps = (mfspr(SPR_PMC1) * 10) + 4999;
967
968 mtspr(SPR_MMCR0, MMCR0_FC);
969
970 ci->ci_khz = cps / 1000;
971 }
972
973 #if NSYSMON_ENVSYS > 0
974 void
975 cpu_tau_setup(struct cpu_info *ci)
976 {
977 struct sysmon_envsys *sme;
978 envsys_data_t sensor;
979 int error;
980
981 sme = sysmon_envsys_create();
982
983 sensor.state = ENVSYS_SVALID;
984 sensor.units = ENVSYS_STEMP;
985 (void)strlcpy(sensor.desc, "CPU Temp", sizeof(sensor.desc));
986 if (sysmon_envsys_sensor_attach(sme, &sensor)) {
987 sysmon_envsys_destroy(sme);
988 return;
989 }
990
991 sme->sme_name = ci->ci_dev->dv_xname;
992 sme->sme_cookie = ci;
993 sme->sme_refresh = cpu_tau_refresh;
994
995 if ((error = sysmon_envsys_register(sme)) != 0) {
996 aprint_error("%s: unable to register with sysmon (%d)\n",
997 ci->ci_dev->dv_xname, error);
998 sysmon_envsys_destroy(sme);
999 }
1000 }
1001
1002
1003 /* Find the temperature of the CPU. */
1004 void
1005 cpu_tau_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1006 {
1007 int i, threshold, count;
1008
1009 threshold = 64; /* Half of the 7-bit sensor range */
1010 mtspr(SPR_THRM1, 0);
1011 mtspr(SPR_THRM2, 0);
1012 /* XXX This counter is supposed to be "at least 20 microseonds, in
1013 * XXX units of clock cycles". Since we don't have convenient
1014 * XXX access to the CPU speed, set it to a conservative value,
1015 * XXX that is, assuming a fast (1GHz) G3 CPU (As of February 2002,
1016 * XXX the fastest G3 processor is 700MHz) . The cost is that
1017 * XXX measuring the temperature takes a bit longer.
1018 */
1019 mtspr(SPR_THRM3, SPR_THRM_TIMER(20000) | SPR_THRM_ENABLE);
1020
1021 /* Successive-approximation code adapted from Motorola
1022 * application note AN1800/D, "Programming the Thermal Assist
1023 * Unit in the MPC750 Microprocessor".
1024 */
1025 for (i = 4; i >= 0 ; i--) {
1026 mtspr(SPR_THRM1,
1027 SPR_THRM_THRESHOLD(threshold) | SPR_THRM_VALID);
1028 count = 0;
1029 while ((count < 100) &&
1030 ((mfspr(SPR_THRM1) & SPR_THRM_TIV) == 0)) {
1031 count++;
1032 delay(1);
1033 }
1034 if (mfspr(SPR_THRM1) & SPR_THRM_TIN) {
1035 /* The interrupt bit was set, meaning the
1036 * temperature was above the threshold
1037 */
1038 threshold += 2 << i;
1039 } else {
1040 /* Temperature was below the threshold */
1041 threshold -= 2 << i;
1042 }
1043 }
1044 threshold += 2;
1045
1046 /* Convert the temperature in degrees C to microkelvin */
1047 edata->value_cur = (threshold * 1000000) + 273150000;
1048 }
1049 #endif /* NSYSMON_ENVSYS > 0 */
1050
1051 #ifdef MULTIPROCESSOR
1052 int
1053 cpu_spinup(struct device *self, struct cpu_info *ci)
1054 {
1055 volatile struct cpu_hatch_data hatch_data, *h = &hatch_data;
1056 struct pglist mlist;
1057 int i, error, pvr, vers;
1058 char *cp;
1059
1060 pvr = mfpvr();
1061 vers = pvr >> 16;
1062 KASSERT(ci != curcpu());
1063
1064 /*
1065 * Allocate some contiguous pages for the intteup PCB and stack
1066 * from the lowest 256MB (because bat0 always maps it va == pa).
1067 */
1068 error = uvm_pglistalloc(INTSTK, 0x0, 0x10000000, 0, 0, &mlist, 1, 1);
1069 if (error) {
1070 aprint_error(": unable to allocate idle stack\n");
1071 return -1;
1072 }
1073
1074 KASSERT(ci != &cpu_info[0]);
1075
1076 cp = (void *)VM_PAGE_TO_PHYS(TAILQ_FIRST(&mlist));
1077 memset(cp, 0, INTSTK);
1078
1079 ci->ci_intstk = cp;
1080
1081 /* Initialize secondary cpu's initial lwp to its idlelwp. */
1082 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
1083 ci->ci_curpcb = &ci->ci_curlwp->l_addr->u_pcb;
1084 ci->ci_curpm = ci->ci_curpcb->pcb_pm;
1085
1086 cpu_hatch_data = h;
1087 h->running = 0;
1088 h->self = self;
1089 h->ci = ci;
1090 h->pir = ci->ci_cpuid;
1091 cpu_hatch_stack = (uint32_t)cp + INTSTK - sizeof(struct trapframe);
1092 ci->ci_lasttb = cpu_info[0].ci_lasttb;
1093
1094 /* copy special registers */
1095 h->hid0 = mfspr(SPR_HID0);
1096 __asm volatile ("mfsdr1 %0" : "=r"(h->sdr1));
1097 for (i = 0; i < 16; i++)
1098 __asm ("mfsrin %0,%1" : "=r"(h->sr[i]) :
1099 "r"(i << ADDR_SR_SHFT));
1100 /* copy the bat regs */
1101 __asm volatile ("mfibatu %0,0" : "=r"(h->batu[0]));
1102 __asm volatile ("mfibatl %0,0" : "=r"(h->batl[0]));
1103 __asm volatile ("mfibatu %0,1" : "=r"(h->batu[1]));
1104 __asm volatile ("mfibatl %0,1" : "=r"(h->batl[1]));
1105 __asm volatile ("mfibatu %0,2" : "=r"(h->batu[2]));
1106 __asm volatile ("mfibatl %0,2" : "=r"(h->batl[2]));
1107 __asm volatile ("mfibatu %0,3" : "=r"(h->batu[3]));
1108 __asm volatile ("mfibatl %0,3" : "=r"(h->batl[3]));
1109 __asm volatile ("sync; isync");
1110
1111 if (md_setup_trampoline(h, ci) == -1)
1112 return -1;
1113 md_presync_timebase(h);
1114 md_start_timebase(h);
1115
1116 /* wait for secondary printf */
1117 delay(200000);
1118
1119 if (h->running == 0) {
1120 aprint_error(":CPU %d didn't start\n", ci->ci_cpuid);
1121 return -1;
1122 }
1123
1124 /* Register IPI Interrupt */
1125 ipiops.ppc_establish_ipi(IST_LEVEL, IPL_HIGH, NULL);
1126
1127 return 0;
1128 }
1129
1130 static volatile int start_secondary_cpu;
1131
1132 void
1133 cpu_hatch()
1134 {
1135 volatile struct cpu_hatch_data *h = cpu_hatch_data;
1136 struct cpu_info * const ci = h->ci;
1137 u_int msr;
1138 int i;
1139
1140 /* Initialize timebase. */
1141 __asm ("mttbl %0; mttbu %0; mttbl %0" :: "r"(0));
1142
1143 /* Set PIR (Processor Identification Register). i.e. whoami */
1144 mtspr(SPR_PIR, h->pir);
1145 __asm volatile ("mtsprg 0,%0" :: "r"(ci));
1146
1147 /* Initialize MMU. */
1148 __asm ("mtibatu 0,%0" :: "r"(h->batu[0]));
1149 __asm ("mtibatl 0,%0" :: "r"(h->batl[0]));
1150 __asm ("mtibatu 1,%0" :: "r"(h->batu[1]));
1151 __asm ("mtibatl 1,%0" :: "r"(h->batl[1]));
1152 __asm ("mtibatu 2,%0" :: "r"(h->batu[2]));
1153 __asm ("mtibatl 2,%0" :: "r"(h->batl[2]));
1154 __asm ("mtibatu 3,%0" :: "r"(h->batu[3]));
1155 __asm ("mtibatl 3,%0" :: "r"(h->batl[3]));
1156
1157 mtspr(SPR_HID0, h->hid0);
1158
1159 __asm ("mtibatl 0,%0; mtibatu 0,%1; mtdbatl 0,%0; mtdbatu 0,%1;"
1160 :: "r"(battable[0].batl), "r"(battable[0].batu));
1161
1162 for (i = 0; i < 16; i++)
1163 __asm ("mtsrin %0,%1" :: "r"(h->sr[i]), "r"(i << ADDR_SR_SHFT));
1164
1165 __asm ("mtsdr1 %0" :: "r"(h->sdr1));
1166 __asm volatile ("isync");
1167
1168 /* Enable I/D address translations. */
1169 __asm volatile ("mfmsr %0" : "=r"(msr));
1170 msr |= PSL_IR|PSL_DR|PSL_ME|PSL_RI;
1171 __asm volatile ("mtmsr %0" :: "r"(msr));
1172 __asm volatile ("sync; isync");
1173
1174 md_sync_timebase(h);
1175
1176 cpu_setup(h->self, ci);
1177
1178 h->running = 1;
1179 __asm volatile ("sync; isync");
1180
1181 while (start_secondary_cpu == 0)
1182 ;
1183
1184 __asm volatile ("sync; isync");
1185
1186 aprint_normal("cpu%d: started\n", cpu_number());
1187 __asm volatile ("mtdec %0" :: "r"(ticks_per_intr));
1188
1189 md_setup_interrupts();
1190
1191 ci->ci_ipending = 0;
1192 ci->ci_cpl = 0;
1193
1194 mtmsr(mfmsr() | PSL_EE);
1195 }
1196
1197 void
1198 cpu_boot_secondary_processors()
1199 {
1200 start_secondary_cpu = 1;
1201 __asm volatile ("sync");
1202 }
1203
1204 #endif /*MULTIPROCESSOR*/
1205