ofwoea_machdep.c revision 1.7 1 /* $NetBSD: ofwoea_machdep.c,v 1.7 2008/01/09 21:32:38 garbled Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.7 2008/01/09 21:32:38 garbled Exp $");
41
42
43 #include "opt_compat_netbsd.h"
44 #include "opt_ddb.h"
45 #include "opt_kgdb.h"
46 #include "opt_ipkdb.h"
47
48 #include <sys/param.h>
49 #include <sys/buf.h>
50 #include <sys/boot_flag.h>
51 #include <sys/extent.h>
52 #include <sys/kernel.h>
53 #include <sys/ksyms.h>
54 #include <uvm/uvm_extern.h>
55
56 #include <dev/ofw/openfirm.h>
57 #include <machine/pmap.h>
58 #include <machine/powerpc.h>
59 #include <machine/trap.h>
60 #include <machine/vmparam.h>
61 #include <machine/autoconf.h>
62 #include <powerpc/bus.h>
63 #include <powerpc/oea/bat.h>
64 #include <powerpc/ofw_bus.h>
65 #include <powerpc/ofw_cons.h>
66 #include <powerpc/spr.h>
67 #include <arch/powerpc/pic/picvar.h>
68
69 #include "opt_oea.h"
70
71 #include "ksyms.h"
72
73 #ifdef DDB
74 #include <machine/db_machdep.h>
75 #include <ddb/db_extern.h>
76 #endif
77
78 #ifdef KGDB
79 #include <sys/kgdb.h>
80 #endif
81
82 #ifdef IPKDB
83 #include <ipkdb/ipkdb.h>
84 #endif
85
86 #include "opt_ofwoea.h"
87
88 #ifdef OFWOEA_DEBUG
89 #define DPRINTF printf
90 #else
91 #define DPRINTF while (0) printf
92 #endif
93
94 typedef struct _rangemap {
95 u_int32_t addr;
96 u_int32_t size;
97 int type;
98 } rangemap_t;
99
100 struct ofw_translations {
101 vaddr_t va;
102 int len;
103 #if defined (PMAC_G5)
104 register64_t pa;
105 #else
106 register_t pa;
107 #endif
108 int mode;
109 }__attribute__((packed));
110
111 struct pmap ofw_pmap;
112 struct ofw_translations ofmap[32];
113 char bootpath[256];
114 char model_name[64];
115 #if NKSYMS || defined(DDB) || defined(LKM)
116 void *startsym, *endsym;
117 #endif
118 #ifdef TIMEBASE_FREQ
119 u_int timebase_freq = TIMEBASE_FREQ;
120 #else
121 u_int timebase_freq = 0;
122 #endif
123
124 extern int ofmsr;
125 extern int chosen;
126 extern uint32_t ticks_per_sec;
127 extern uint32_t ns_per_tick;
128 extern uint32_t ticks_per_intr;
129
130 static int save_ofmap(struct ofw_translations *, int);
131 static void restore_ofmap(struct ofw_translations *, int);
132 static void set_timebase(void);
133
134 void
135 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
136 {
137 int ofmaplen, node;
138 #if defined (PPC_OEA64_BRIDGE)
139 register_t scratch;
140 #endif
141
142 #if defined (PPC_OEA)
143 /* initialze bats */
144 ofwoea_batinit();
145 #elif defined (PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
146 #endif /* PPC_OEA */
147
148 #if NKSYMS || defined(DDB) || defined(LKM)
149 /* get info of kernel symbol table from bootloader */
150 memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
151 memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
152 sizeof(endsym));
153 if (startsym == NULL || endsym == NULL)
154 startsym = endsym = NULL;
155 #endif
156
157 /* get model name and perform model-specific actions */
158 memset(model_name, 0, sizeof(model_name));
159 node = OF_finddevice("/");
160 if (node >= 0) {
161 OF_getprop(node, "model", model_name, sizeof(model_name));
162 model_init();
163 }
164
165 ofwoea_consinit();
166
167 oea_init(pic_ext_intr);
168
169 ofmaplen = save_ofmap(NULL, 0);
170 if (ofmaplen > 0)
171 save_ofmap(ofmap, ofmaplen);
172
173 ofmsr &= ~PSL_IP;
174
175 /* Parse the args string */
176 if (args) {
177 strcpy(bootpath, args);
178 args = bootpath;
179 while (*++args && *args != ' ');
180 if (*args) {
181 *args++ = 0;
182 while (*args)
183 BOOT_FLAG(*args++, boothowto);
184 }
185 }
186
187 uvm_setpagesize();
188 pmap_bootstrap(startkernel, endkernel);
189
190 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
191 #if defined (PMAC_G5)
192 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
193 pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000, 0x0);
194 #elif defined (MAMBO)
195 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
196 pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
197 #endif /* PMAC_G5 */
198
199 /* Now enable translation (and machine checks/recoverable interrupts) */
200 __asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
201 : "=r"(scratch)
202 : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
203 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
204
205 restore_ofmap(ofmap, ofmaplen);
206
207 #if NKSYMS || defined(DDB) || defined(LKM)
208 ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
209 #endif
210
211 /* CPU clock stuff */
212 set_timebase();
213 }
214
215 void
216 set_timebase(void)
217 {
218 int qhandle, phandle, msr, scratch;
219 char type[32];
220
221 if (timebase_freq != 0) {
222 ticks_per_sec = timebase_freq;
223 goto found;
224 }
225
226 for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
227 if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
228 && strcmp(type, "cpu") == 0
229 && OF_getprop(qhandle, "timebase-frequency",
230 &ticks_per_sec, sizeof ticks_per_sec) > 0) {
231 goto found;
232 }
233 if ((phandle = OF_child(qhandle)))
234 continue;
235 while (qhandle) {
236 if ((phandle = OF_peer(qhandle)))
237 break;
238 qhandle = OF_parent(qhandle);
239 }
240 }
241 panic("no cpu node");
242
243 found:
244 __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
245 : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
246 ns_per_tick = 1000000000 / ticks_per_sec;
247 ticks_per_intr = ticks_per_sec / hz;
248 cpu_timebase = ticks_per_sec;
249 curcpu()->ci_lasttb = mftbl();
250 mtspr(SPR_DEC, ticks_per_intr);
251 mtmsr(msr);
252 }
253
254 static int
255 save_ofmap(struct ofw_translations *map, int maxlen)
256 {
257 int mmui, mmu, len;
258
259 OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
260 mmu = OF_instance_to_package(mmui);
261
262 if (map) {
263 memset(map, 0, maxlen); /* to be safe */
264 len = OF_getprop(mmu, "translations", map, maxlen);
265 } else
266 len = OF_getproplen(mmu, "translations");
267
268 if (len < 0)
269 len = 0;
270 return len;
271 }
272
273 void
274 restore_ofmap(struct ofw_translations *map, int len)
275 {
276 int n = len / sizeof(struct ofw_translations);
277 int i;
278
279 pmap_pinit(&ofw_pmap);
280
281 #if defined(PPC_OEA64_BRIDGE)
282 ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
283 #endif
284 ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
285
286 #ifdef KERNEL2_SR
287 ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
288 #endif
289
290 for (i = 0; i < n; i++) {
291 #if defined (PMAC_G5)
292 register64_t pa = map[i].pa;
293 #else
294 register_t pa = map[i].pa;
295 #endif
296 vaddr_t va = map[i].va;
297 size_t length = map[i].len;
298
299 if (va < 0xf0000000) /* XXX */
300 continue;
301
302 while (length > 0) {
303 pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
304 VM_PROT_ALL|PMAP_WIRED);
305 pa += PAGE_SIZE;
306 va += PAGE_SIZE;
307 length -= PAGE_SIZE;
308 }
309 }
310 pmap_update(&ofw_pmap);
311 }
312
313
314
315 /*
316 * Scan the device tree for ranges, and batmap them.
317 */
318
319 static u_int16_t
320 ranges_bitmap(int node, u_int16_t bitmap)
321 {
322 int child, mlen, acells, scells, reclen, i, j;
323 u_int32_t addr, len, map[160];
324
325 for (child = OF_child(node); child; child = OF_peer(child)) {
326 mlen = OF_getprop(child, "ranges", map, sizeof(map));
327 if (mlen == -1)
328 goto noranges;
329
330 j = OF_getprop(child, "#address-cells", &acells,
331 sizeof(acells));
332 if (j == -1)
333 goto noranges;
334
335 j = OF_getprop(child, "#size-cells", &scells,
336 sizeof(scells));
337 if (j == -1)
338 goto noranges;
339
340 reclen = acells + 1 + scells;
341
342 for (i=0; i < (mlen/4)/reclen; i++) {
343 addr = map[reclen * i + acells];
344 len = map[reclen * i + reclen - 1];
345 for (j = 0; j < len / 0x10000000; j++)
346 bitmap |= 1 << ((addr+j*0x10000000) >>28);
347 bitmap |= 1 << (addr >> 28);
348 }
349 noranges:
350 bitmap |= ranges_bitmap(child, bitmap);
351 continue;
352 }
353 return bitmap;
354 }
355
356 void
357 ofwoea_batinit(void)
358 {
359 #ifdef PPC_OEA
360 u_int16_t bitmap;
361 int node, i;
362
363 node = OF_finddevice("/");
364 bitmap = ranges_bitmap(node, 0);
365 oea_batinit(0);
366
367 #ifdef macppc
368 /* XXX this is a macppc-specific hack */
369 bitmap = 0x8f00;
370 #endif
371 for (i=1; i < 0x10; i++) {
372 /* skip the three vital SR regions */
373 if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
374 continue;
375 if (bitmap & (1 << i)) {
376 oea_iobat_add(0x10000000 * i, BAT_BL_256M);
377 DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
378 }
379 }
380 #endif
381 }
382
383
384 /* we define these partially, as we will fill the rest in later */
385 struct powerpc_bus_space genppc_isa_io_space_tag = {
386 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
387 .pbs_base = 0x00000000,
388 };
389
390 struct powerpc_bus_space genppc_isa_mem_space_tag = {
391 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
392 .pbs_base = 0x00000000,
393 };
394
395 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
396 * Increase if necc.
397 */
398 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
399 __attribute__((aligned(8)));
400
401
402 static void
403 find_ranges(int base, rangemap_t *regions, int *cur, int type)
404 {
405 int node, i, len, reclen;
406 u_int32_t acells, scells, map[160];
407 char tmp[32];
408
409 node = base;
410 if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
411 goto rec;
412 if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
413 strcmp("pci", tmp) != 0)
414 goto rec;
415 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
416 goto rec;
417 len = OF_getprop(node, "ranges", map, sizeof(map));
418 if (len == -1)
419 goto rec;
420 if (OF_getprop(node, "#address-cells", &acells,
421 sizeof(acells)) != sizeof(acells))
422 acells = 1;
423 if (OF_getprop(node, "#size-cells", &scells,
424 sizeof(scells)) != sizeof(scells))
425 scells = 1;
426 if (type == RANGE_TYPE_ISA)
427 reclen = 6;
428 else
429 reclen = acells + scells + 1;
430 /*
431 * There exist ISA buses with empty ranges properties. This is
432 * known to occur on the Pegasos II machine, and likely others.
433 * According to them, that means that the isa bus is a fake bus, and
434 * the real maps are the PCI maps of the preceeding bus. To deal
435 * with this, we will set cur to -1 and return.
436 */
437 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
438 *cur = -1;
439 DPRINTF("Found empty range in isa bus\n");
440 return;
441 }
442
443 DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
444 switch (type) {
445 case RANGE_TYPE_PCI:
446 case RANGE_TYPE_FIRSTPCI:
447 for (i=0; i < len/(4*reclen); i++) {
448 DPRINTF("FOUND PCI RANGE\n");
449 regions[*cur].type = map[i*reclen] >> 24;
450 regions[*cur].addr = map[i*reclen + acells];
451 regions[*cur].size =
452 map[i*reclen + acells + scells];
453 (*cur)++;
454 }
455 break;
456 case RANGE_TYPE_ISA:
457 for (i=0; i < len/(4*reclen); i++) {
458 if (map[i*reclen] == 1)
459 regions[*cur].type = RANGE_IO;
460 else
461 regions[*cur].type = RANGE_MEM;
462 DPRINTF("FOUND ISA RANGE TYPE=%d\n",
463 regions[*cur].type);
464 regions[*cur].size =
465 map[i*reclen + acells + scells];
466 (*cur)++;
467 }
468 break;
469 }
470 DPRINTF("returning with CUR=%d\n", *cur);
471 return;
472 rec:
473 for (node = OF_child(base); node; node = OF_peer(node)) {
474 DPRINTF("RECURSE 1 STEP\n");
475 find_ranges(node, regions, cur, type);
476 if (*cur == -1)
477 return;
478 }
479 }
480
481 static int
482 find_lowest_range(rangemap_t *ranges, int nrof, int type)
483 {
484 int i, low = 0;
485 u_int32_t addr = 0xffffffff;
486
487 for (i=0; i < nrof; i++) {
488 if (ranges[i].type == type && ranges[i].addr != 0 &&
489 ranges[i].addr < addr) {
490 low = i;
491 addr = ranges[i].addr;
492 }
493 }
494 if (addr == 0xffffffff)
495 return -1;
496 return low;
497 }
498
499 /*
500 * Find a region of memory, and create a bus_space_tag for it.
501 * Notes:
502 * For ISA node is ignored.
503 * node is the starting node. if -1, we start at / and map everything.
504 */
505
506 int
507 ofwoea_map_space(int rangetype, int iomem, int node,
508 struct powerpc_bus_space *tag, const char *name)
509 {
510 int i, cur, range, nrofholes, error;
511 static int exmap=0;
512 u_int32_t addr;
513 rangemap_t region, holes[32], list[32];
514
515 memset(list, 0, sizeof(list));
516 cur = 0;
517 if (rangetype == RANGE_TYPE_ISA || node == -1)
518 node = OF_finddevice("/");
519 if (rangetype == RANGE_TYPE_ISA) {
520 u_int32_t size = 0;
521 rangemap_t regions[32];
522
523 DPRINTF("LOOKING FOR FIRSTPCI\n");
524 find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
525 range = 0;
526 DPRINTF("LOOKING FOR ISA\n");
527 find_ranges(node, regions, &range, RANGE_TYPE_ISA);
528 if (range == 0 || cur == 0)
529 return -1; /* no isa stuff found */
530 /*
531 * This may be confusing to some. The ISA ranges property
532 * is supposed to be a set of IO ranges for the ISA bus, but
533 * generally, it's just a set of pci devfunc lists that tell
534 * you to go look at the parent PCI device for the actual
535 * ranges.
536 */
537 if (range == -1) {
538 /* we found a rangeless isa bus */
539 }
540 DPRINTF("found isa stuff\n");
541 for (i=0; i < range; i++)
542 if (regions[i].type == iomem)
543 size = regions[i].size;
544 if (iomem == RANGE_IO) {
545 /* the first io range is the one */
546 for (i=0; i < cur; i++)
547 if (list[i].type == RANGE_IO && size) {
548 DPRINTF("found IO\n");
549 tag->pbs_offset = list[i].addr;
550 tag->pbs_limit = size;
551 error = bus_space_init(tag, name, NULL, 0);
552 return error;
553 }
554 } else {
555 for (i=0; i < cur; i++)
556 if (list[i].type == RANGE_MEM &&
557 list[i].size == size) {
558 DPRINTF("found mem\n");
559 tag->pbs_offset = list[i].addr;
560 tag->pbs_limit = size;
561 error = bus_space_init(tag, name, NULL, 0);
562 return error;
563 }
564 }
565 return -1; /* NO ISA FOUND */
566 }
567 find_ranges(node, list, &cur, rangetype);
568
569 DPRINTF("cur == %d\n", cur);
570 /* now list should contain a list of memory regions */
571 for (i=0; i < cur; i++)
572 DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
573 list[i].size, list[i].type);
574
575 addr=0;
576 range = find_lowest_range(list, cur, iomem);
577 i = 0;
578 nrofholes = 0;
579 while (range != -1) {
580 DPRINTF("range==%d\n", range);
581 DPRINTF("i==%d\n", i);
582 if (i == 0) {
583 memcpy(®ion, &list[range], sizeof(rangemap_t));
584 list[range].addr = 0;
585 i++;
586 range = find_lowest_range(list, cur, iomem);
587 continue;
588 }
589 if (region.addr + region.size < list[range].addr) {
590 /* allocate a hole */
591 holes[nrofholes].addr = region.size + region.addr;
592 holes[nrofholes].size = list[range].addr -
593 holes[nrofholes].addr - 1;
594 nrofholes++;
595 }
596 region.size = list[range].size + list[range].addr -
597 region.addr;
598 list[range].addr = 0;
599 range = find_lowest_range(list, cur, iomem);
600 }
601 DPRINTF("RANGE iomem=%d FOUND\n", iomem);
602 DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
603 region.size, region.type);
604 DPRINTF("HOLES FOUND\n");
605 for (i=0; i < nrofholes; i++)
606 DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
607 holes[i].size, holes[i].type);
608 /* AT THIS POINT WE MAP IT */
609
610 if (rangetype == RANGE_TYPE_PCI) {
611 if (exmap == EXSTORAGE_MAX)
612 panic("Not enough ex_storage space. "
613 "Increase EXSTORAGE_MAX");
614
615 /* XXX doing this in here might be wrong */
616 if (iomem == 1) {
617 /* we map an IO region */
618 tag->pbs_offset = region.addr;
619 tag->pbs_base = 0;
620 tag->pbs_limit = region.size;
621 } else {
622 /* ... or a memory region */
623 tag->pbs_offset = 0;
624 tag->pbs_base = region.addr;
625 tag->pbs_limit = region.size + region.addr;
626 }
627
628 error = bus_space_init(tag, name, ex_storage[exmap],
629 sizeof(ex_storage[exmap]));
630 exmap++;
631 if (error)
632 panic("ofwoea_bus_space_init: can't init tag %s", name);
633 for (i=0; i < nrofholes; i++) {
634 error =
635 extent_alloc_region(tag->pbs_extent,
636 holes[i].addr, holes[i].size, EX_NOWAIT);
637 if (error)
638 panic("ofwoea_bus_space_init: can't block out"
639 " reserved space 0x%x-0x%x: error=%d",
640 holes[i].addr, holes[i].addr+holes[i].size,
641 error);
642 }
643 return error;
644 }
645 return -1;
646 }
647
648 void
649 ofwoea_bus_space_init(void)
650 {
651 int error;
652
653 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
654 &genppc_isa_io_space_tag, "isa-ioport");
655 if (error > 0)
656 panic("Could not map ISA IO");
657
658 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
659 &genppc_isa_mem_space_tag, "isa-iomem");
660 if (error > 0)
661 panic("Could not map ISA MEM");
662 }
663