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