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