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