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