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