ofwoea_machdep.c revision 1.3 1 /* $NetBSD: ofwoea_machdep.c,v 1.3 2007/10/25 16:55:50 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.3 2007/10/25 16:55:50 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, len, acells, scells, reclen, i, addr, j;
312 u_int32_t 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 len = OF_getprop(child, "#address-cells", &acells,
319 sizeof(acells));
320 if (len == -1)
321 goto noranges;
322 len = OF_getprop(child, "#size-cells", &scells,
323 sizeof(scells));
324 if (len == -1)
325 goto noranges;
326
327 reclen = acells + 1 + scells;
328
329 for (i=0; i < reclen/(mlen/4); i++) {
330 addr = map[reclen * i + acells + 1];
331 len = map[reclen * i + reclen - 1];
332 for (j = 0; j < len / 0x10000000; j++)
333 bitmap |= 1 << ((addr+j*0x10000000) >>28);
334 bitmap |= 1 << (addr >> 28);
335 }
336
337 noranges:
338 bitmap |= ranges_bitmap(child, bitmap);
339 continue;
340 }
341 return bitmap;
342 }
343
344 void
345 ofwoea_batinit(void)
346 {
347 #if 0
348 /* this is what macppc used to do */
349 oea_batinit(0x80000000, BAT_BL_256M, 0xf0000000, BAT_BL_256M,
350 0x90000000, BAT_BL_256M, 0xa0000000, BAT_BL_256M,
351 0xb0000000, BAT_BL_256M, 0);
352 #else
353 u_int16_t bitmap;
354 int node, i;
355
356 node = OF_finddevice("/");
357 bitmap = ranges_bitmap(node, 0);
358 oea_batinit(0);
359
360 /* XXX this is a macppc-specific hack */
361 bitmap = 0x8f00;
362 for (i=1; i < 0x10; i++)
363 if (bitmap & (1 << i))
364 oea_iobat_add(0x10000000 * i, BAT_BL_256M);
365 #endif
366 }
367
368
369 /* we define these partially, as we will fill the rest in later */
370 struct powerpc_bus_space genppc_isa_io_space_tag = {
371 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
372 .pbs_base = 0x00000000,
373 };
374
375 struct powerpc_bus_space genppc_isa_mem_space_tag = {
376 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
377 .pbs_base = 0x00000000,
378 };
379
380 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
381 * Increase if necc.
382 */
383 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
384 __attribute__((aligned(8)));
385
386
387 static void
388 find_ranges(int base, rangemap_t *regions, int *cur, int type)
389 {
390 int node, i, len, reclen;
391 u_int32_t acells, scells, map[160];
392 char tmp[32];
393
394 node = base;
395 if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
396 goto rec;
397 if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
398 strcmp("pci", tmp) != 0)
399 goto rec;
400 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
401 goto rec;
402 len = OF_getprop(node, "ranges", map, sizeof(map));
403 if (len == -1)
404 goto rec;
405 if (OF_getprop(node, "#address-cells", &acells,
406 sizeof(acells)) != sizeof(acells))
407 acells = 1;
408 if (OF_getprop(node, "#size-cells", &scells,
409 sizeof(scells)) != sizeof(scells))
410 scells = 1;
411 if (type == RANGE_TYPE_ISA)
412 reclen = 6;
413 else
414 reclen = acells + scells + 1;
415 /*
416 * There exist ISA buses with empty ranges properties. This is
417 * known to occur on the Pegasos II machine, and likely others.
418 * According to them, that means that the isa bus is a fake bus, and
419 * the real maps are the PCI maps of the preceeding bus. To deal
420 * with this, we will set cur to -1 and return.
421 */
422 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
423 *cur = -1;
424 DPRINTF("Found empty range in isa bus\n");
425 return;
426 }
427
428 DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
429 switch (type) {
430 case RANGE_TYPE_PCI:
431 case RANGE_TYPE_FIRSTPCI:
432 for (i=0; i < len/(4*reclen); i++) {
433 DPRINTF("FOUND PCI RANGE\n");
434 regions[*cur].type = map[i*reclen] >> 24;
435 regions[*cur].addr = map[i*reclen + acells];
436 regions[*cur].size =
437 map[i*reclen + acells + scells];
438 (*cur)++;
439 }
440 break;
441 case RANGE_TYPE_ISA:
442 for (i=0; i < len/(4*reclen); i++) {
443 if (map[i*reclen] == 1)
444 regions[*cur].type = RANGE_IO;
445 else
446 regions[*cur].type = RANGE_MEM;
447 DPRINTF("FOUND ISA RANGE TYPE=%d\n",
448 regions[*cur].type);
449 regions[*cur].size =
450 map[i*reclen + acells + scells];
451 (*cur)++;
452 }
453 break;
454 }
455 DPRINTF("returning with CUR=%d\n", *cur);
456 return;
457 rec:
458 for (node = OF_child(base); node; node = OF_peer(node)) {
459 DPRINTF("RECURSE 1 STEP\n");
460 find_ranges(node, regions, cur, type);
461 if (*cur == -1)
462 return;
463 }
464 }
465
466 static int
467 find_lowest_range(rangemap_t *ranges, int nrof, int type)
468 {
469 int i, low = 0;
470 u_int32_t addr = 0xffffffff;
471
472 for (i=0; i < nrof; i++) {
473 if (ranges[i].type == type && ranges[i].addr != 0 &&
474 ranges[i].addr < addr) {
475 low = i;
476 addr = ranges[i].addr;
477 }
478 }
479 if (addr == 0xffffffff)
480 return -1;
481 return low;
482 }
483
484 /*
485 * Find a region of memory, and create a bus_space_tag for it.
486 * Notes:
487 * For ISA node is ignored.
488 * node is the starting node. if -1, we start at / and map everything.
489 */
490
491 int
492 ofwoea_map_space(int rangetype, int iomem, int node,
493 struct powerpc_bus_space *tag, const char *name)
494 {
495 int i, cur, range, nrofholes, error;
496 static int exmap=0;
497 u_int32_t addr;
498 rangemap_t region, holes[32], list[32];
499
500 memset(list, 0, sizeof(list));
501 cur = 0;
502 if (rangetype == RANGE_TYPE_ISA || node == -1)
503 node = OF_finddevice("/");
504 if (rangetype == RANGE_TYPE_ISA) {
505 u_int32_t size = 0;
506 rangemap_t regions[32];
507
508 DPRINTF("LOOKING FOR FIRSTPCI\n");
509 find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
510 range = 0;
511 DPRINTF("LOOKING FOR ISA\n");
512 find_ranges(node, regions, &range, RANGE_TYPE_ISA);
513 if (range == 0 || cur == 0)
514 return -1; /* no isa stuff found */
515 /*
516 * This may be confusing to some. The ISA ranges property
517 * is supposed to be a set of IO ranges for the ISA bus, but
518 * generally, it's just a set of pci devfunc lists that tell
519 * you to go look at the parent PCI device for the actual
520 * ranges.
521 */
522 if (range == -1) {
523 /* we found a rangeless isa bus */
524 }
525 DPRINTF("found isa stuff\n");
526 for (i=0; i < range; i++)
527 if (regions[i].type == iomem)
528 size = regions[i].size;
529 if (iomem == RANGE_IO) {
530 /* the first io range is the one */
531 for (i=0; i < cur; i++)
532 if (list[i].type == RANGE_IO && size) {
533 DPRINTF("found IO\n");
534 tag->pbs_offset = list[i].addr;
535 tag->pbs_limit = size;
536 error = bus_space_init(tag, name, NULL, 0);
537 return error;
538 }
539 } else {
540 for (i=0; i < cur; i++)
541 if (list[i].type == RANGE_MEM &&
542 list[i].size == size) {
543 DPRINTF("found mem\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 }
550 return -1; /* NO ISA FOUND */
551 }
552 find_ranges(node, list, &cur, rangetype);
553
554 DPRINTF("cur == %d\n", cur);
555 /* now list should contain a list of memory regions */
556 for (i=0; i < cur; i++)
557 DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
558 list[i].size, list[i].type);
559
560 addr=0;
561 range = find_lowest_range(list, cur, iomem);
562 i = 0;
563 nrofholes = 0;
564 while (range != -1) {
565 DPRINTF("range==%d\n", range);
566 DPRINTF("i==%d\n", i);
567 if (i == 0) {
568 memcpy(®ion, &list[range], sizeof(rangemap_t));
569 list[range].addr = 0;
570 i++;
571 range = find_lowest_range(list, cur, iomem);
572 continue;
573 }
574 if (region.addr + region.size < list[range].addr) {
575 /* allocate a hole */
576 holes[nrofholes].addr = region.size + region.addr;
577 holes[nrofholes].size = list[range].addr -
578 holes[nrofholes].addr - 1;
579 nrofholes++;
580 }
581 region.size = list[range].size + list[range].addr -
582 region.addr;
583 list[range].addr = 0;
584 range = find_lowest_range(list, cur, iomem);
585 }
586 DPRINTF("RANGE iomem=%d FOUND\n", iomem);
587 DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
588 region.size, region.type);
589 DPRINTF("HOLES FOUND\n");
590 for (i=0; i < nrofholes; i++)
591 DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
592 holes[i].size, holes[i].type);
593 /* AT THIS POINT WE MAP IT */
594
595 if (rangetype == RANGE_TYPE_PCI) {
596 if (exmap == EXSTORAGE_MAX)
597 panic("Not enough ex_storage space. "
598 "Increase EXSTORAGE_MAX");
599
600 /* XXX doing this in here might be wrong */
601 if (iomem == 1) {
602 /* we map an IO region */
603 tag->pbs_offset = region.addr;
604 tag->pbs_base = 0;
605 tag->pbs_limit = region.size;
606 } else {
607 /* ... or a memory region */
608 tag->pbs_offset = 0;
609 tag->pbs_base = region.addr;
610 tag->pbs_limit = region.size + region.addr;
611 }
612
613 error = bus_space_init(tag, name, ex_storage[exmap],
614 sizeof(ex_storage[exmap]));
615 exmap++;
616 if (error)
617 panic("ofwoea_bus_space_init: can't init tag %s", name);
618 for (i=0; i < nrofholes; i++) {
619 error =
620 extent_alloc_region(tag->pbs_extent,
621 holes[i].addr, holes[i].size, EX_NOWAIT);
622 if (error)
623 panic("ofwoea_bus_space_init: can't block out"
624 " reserved space 0x%x-0x%x: error=%d",
625 holes[i].addr, holes[i].addr+holes[i].size,
626 error);
627 }
628 return error;
629 }
630 return -1;
631 }
632
633 void
634 ofwoea_bus_space_init(void)
635 {
636 int error;
637
638 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
639 &genppc_isa_io_space_tag, "isa-ioport");
640 if (error > 0)
641 panic("Could not map ISA IO");
642
643 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
644 &genppc_isa_mem_space_tag, "isa-iomem");
645 if (error > 0)
646 panic("Could not map ISA MEM");
647 }
648