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