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