ofwoea_machdep.c revision 1.44.2.1 1 /* $NetBSD: ofwoea_machdep.c,v 1.44.2.1 2018/07/28 04:37:39 pgoyette 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.44.2.1 2018/07/28 04:37:39 pgoyette Exp $");
34
35 #include "opt_ppcarch.h"
36 #include "opt_compat_netbsd.h"
37 #include "opt_ddb.h"
38 #include "opt_kgdb.h"
39 #include "opt_modular.h"
40
41 #include "wsdisplay.h"
42
43 #include <sys/param.h>
44 #include <sys/buf.h>
45 #include <sys/boot_flag.h>
46 #include <sys/extent.h>
47 #include <sys/kernel.h>
48 #include <sys/ksyms.h>
49 #include <uvm/uvm_extern.h>
50
51 #include <dev/ofw/openfirm.h>
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/rasops/rasops.h>
55 #include <dev/wscons/wsdisplay_vconsvar.h>
56 #include <machine/pmap.h>
57 #include <machine/powerpc.h>
58 #include <machine/trap.h>
59 #include <machine/vmparam.h>
60 #include <machine/autoconf.h>
61 #include <sys/bus.h>
62 #include <powerpc/oea/bat.h>
63 #include <powerpc/oea/ofw_rasconsvar.h>
64 #include <powerpc/oea/cpufeat.h>
65 #include <powerpc/include/oea/spr.h>
66 #include <powerpc/ofw_cons.h>
67 #include <powerpc/spr.h>
68 #include <powerpc/pic/picvar.h>
69
70 #include "opt_oea.h"
71
72 #include "ksyms.h"
73
74 #ifdef DDB
75 #include <machine/db_machdep.h>
76 #include <ddb/db_extern.h>
77 #endif
78
79 #ifdef KGDB
80 #include <sys/kgdb.h>
81 #endif
82
83 #include "opt_ofwoea.h"
84
85 #ifdef ofppc
86 extern struct model_data modeldata;
87 #endif
88
89 #ifdef OFWOEA_DEBUG
90 #define DPRINTF printf
91 #else
92 #define DPRINTF while (0) printf
93 #endif
94
95 typedef struct _rangemap {
96 u_int32_t addr;
97 u_int32_t size;
98 int type;
99 } rangemap_t;
100
101 struct ofw_translations {
102 vaddr_t va;
103 int len;
104 #if defined (PMAC_G5)
105 register64_t pa;
106 #else
107 register_t pa;
108 #endif
109 int mode;
110 }__attribute__((packed));
111
112 struct pmap ofw_pmap;
113 struct ofw_translations ofmap[32];
114 char bootpath[256];
115 char model_name[64];
116 #if NKSYMS || defined(DDB) || defined(MODULAR)
117 void *startsym, *endsym;
118 #endif
119
120 #if PPC_OEA601
121 #define TIMEBASE_FREQ (1000000000) /* RTC register */
122 #endif
123
124 #ifdef TIMEBASE_FREQ
125 u_int timebase_freq = TIMEBASE_FREQ;
126 #else
127 u_int timebase_freq = 0;
128 #endif
129
130 extern int ofwmsr;
131 extern int chosen;
132 extern uint32_t ticks_per_sec;
133 extern uint32_t ns_per_tick;
134 extern uint32_t ticks_per_intr;
135
136 static int save_ofmap(struct ofw_translations *, int);
137 static void restore_ofmap(struct ofw_translations *, int);
138 static void set_timebase(void);
139
140 extern void cpu_spinstart(u_int);
141 extern volatile u_int cpu_spinstart_ack;
142
143 void
144 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
145 {
146 int ofmaplen, node, l;
147 register_t scratch;
148
149 #if defined(MULTIPROCESSOR) && defined(ofppc)
150 char cpupath[32];
151 int i;
152 #endif
153
154 /* initialze bats */
155 if ((oeacpufeat & OEACPU_NOBAT) == 0)
156 ofwoea_batinit();
157
158 #if NKSYMS || defined(DDB) || defined(MODULAR)
159 /* get info of kernel symbol table from bootloader */
160 memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
161 memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
162 sizeof(endsym));
163 if (startsym == NULL || endsym == NULL)
164 startsym = endsym = NULL;
165 #endif
166
167 /* get model name and perform model-specific actions */
168 memset(model_name, 0, sizeof(model_name));
169 node = OF_finddevice("/");
170 if (node != -1) {
171 l = OF_getprop(node, "model", model_name, sizeof(model_name));
172 if (l == -1)
173 OF_getprop(node, "name", model_name,
174 sizeof(model_name));
175 model_init();
176 }
177
178 if (strncmp(model_name, "PowerMac11,", 11) == 0 ||
179 strncmp(model_name, "PowerMac7,", 10) == 0)
180 OF_quiesce();
181
182 /* Initialize bus_space */
183 ofwoea_bus_space_init();
184
185 ofwoea_consinit();
186
187 #if defined(MULTIPROCESSOR) && defined(ofppc)
188 for (i=1; i < CPU_MAXNUM; i++) {
189 snprintf(cpupath, sizeof(cpupath), "/cpus/@%x", i);
190 node = OF_finddevice(cpupath);
191 if (node <= 0)
192 continue;
193 aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
194 OF_start_cpu(node, (u_int)cpu_spinstart, i);
195 for (l=0; l < 100000000; l++) {
196 if (cpu_spinstart_ack == i) {
197 aprint_verbose("CPU %d spun up.\n", i);
198 break;
199 }
200 __asm volatile ("sync");
201 }
202 }
203 #endif
204
205 oea_init(pic_ext_intr);
206
207 ofmaplen = save_ofmap(NULL, 0);
208 if (ofmaplen > 0)
209 save_ofmap(ofmap, ofmaplen);
210
211 /*
212 * XXX
213 * we need to do this here instead of earlier on in ofwinit() for some reason
214 * At least some versions of Apple OF 2.0.1 hang if we do this earlier
215 */
216 ofwmsr &= ~PSL_IP;
217
218 /* Parse the args string */
219 if (args) {
220 strcpy(bootpath, args);
221 args = bootpath;
222 while (*++args && *args != ' ');
223 if (*args) {
224 *args++ = 0;
225 while (*args)
226 BOOT_FLAG(*args++, boothowto);
227 }
228 } else {
229 int chs = OF_finddevice("/chosen");
230 int len;
231
232 len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
233 if (len > -1)
234 bootpath[len] = 0;
235 }
236
237 uvm_md_init();
238
239 pmap_bootstrap(startkernel, endkernel);
240
241 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
242 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
243 #if defined (PMAC_G5)
244 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
245 if (oeacpufeat & OEACPU_64_BRIDGE) {
246 vaddr_t va;
247 paddr_t pa;
248 int i;
249
250 pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
251 round_page(MSGBUFSIZE), 0x0);
252
253 /* Map OFW code+data */
254
255 for (i = 0; i < ofmaplen / sizeof(struct ofw_translations); i++) {
256 if (ofmap[i].va < 0xff800000)
257 continue;
258
259 for (va = ofmap[i].va, pa = ofmap[i].pa;
260 va < ofmap[i].va + ofmap[i].len;
261 va += PAGE_SIZE, pa += PAGE_SIZE) {
262 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
263 VM_PROT_ALL | PMAP_WIRED);
264 }
265 }
266
267 #if NWSDISPLAY > 0
268 /* Map video frame buffer */
269
270 struct rasops_info *ri = &rascons_console_screen.scr_ri;
271
272 if (ri->ri_bits != NULL) {
273 for (va = (vaddr_t) ri->ri_bits;
274 va < round_page((vaddr_t) ri->ri_bits +
275 ri->ri_height * ri->ri_stride);
276 va += PAGE_SIZE) {
277 pmap_enter(pmap_kernel(), va, va,
278 VM_PROT_READ | VM_PROT_WRITE,
279 PMAP_NOCACHE | PMAP_WIRED);
280 }
281 }
282 #endif
283 }
284 #elif defined (MAMBO)
285 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
286 if (oeacpufeat & OEACPU_64_BRIDGE)
287 pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
288 #endif /* PMAC_G5 */
289 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
290
291 /* Now enable translation (and machine checks/recoverable interrupts) */
292 __asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
293 : "=r"(scratch)
294 : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
295
296 restore_ofmap(ofmap, ofmaplen);
297
298 rascons_finalize();
299
300 #if NKSYMS || defined(DDB) || defined(MODULAR)
301 ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
302 #endif
303
304 /* CPU clock stuff */
305 set_timebase();
306
307 #ifdef DDB
308 if (boothowto & RB_KDB)
309 Debugger();
310 #endif
311 }
312
313 void
314 set_timebase(void)
315 {
316 int qhandle, phandle, msr, scratch, node;
317 char type[32];
318
319 if (timebase_freq != 0) {
320 ticks_per_sec = timebase_freq;
321 goto found;
322 }
323
324 node = OF_finddevice("/cpus/@0");
325 if (node != -1 &&
326 OF_getprop(node, "timebase-frequency", &ticks_per_sec,
327 sizeof ticks_per_sec) > 0) {
328 goto found;
329 }
330
331 node = OF_finddevice("/");
332 for (qhandle = node; qhandle; qhandle = phandle) {
333 if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
334 && strcmp(type, "cpu") == 0
335 && OF_getprop(qhandle, "timebase-frequency",
336 &ticks_per_sec, sizeof ticks_per_sec) > 0) {
337 goto found;
338 }
339 if ((phandle = OF_child(qhandle)))
340 continue;
341 while (qhandle) {
342 if ((phandle = OF_peer(qhandle)))
343 break;
344 qhandle = OF_parent(qhandle);
345 }
346 }
347 panic("no cpu node");
348
349 found:
350 __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
351 : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
352 ns_per_tick = 1000000000 / ticks_per_sec;
353 ticks_per_intr = ticks_per_sec / hz;
354 cpu_timebase = ticks_per_sec;
355
356 #ifdef PPC_OEA601
357 if ((mfpvr() >> 16) == MPC601)
358 curcpu()->ci_lasttb = rtc_nanosecs();
359 else
360 #endif
361 curcpu()->ci_lasttb = mftbl();
362
363 mtspr(SPR_DEC, ticks_per_intr);
364 mtmsr(msr);
365 }
366
367 static int
368 save_ofmap(struct ofw_translations *map, int maxlen)
369 {
370 int mmui, mmu, len;
371
372 OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
373 mmu = OF_instance_to_package(mmui);
374
375 if (map) {
376 memset(map, 0, maxlen); /* to be safe */
377 len = OF_getprop(mmu, "translations", map, maxlen);
378 } else
379 len = OF_getproplen(mmu, "translations");
380
381 if (len < 0)
382 len = 0;
383 return len;
384 }
385
386
387 /* The PMAC_G5 code here needs to be replaced by code that looks for the
388 size_cells and does the right thing automatically.
389 */
390 void
391 restore_ofmap(struct ofw_translations *map, int len)
392 {
393 int n = len / sizeof(struct ofw_translations);
394 int i;
395
396 pmap_pinit(&ofw_pmap);
397
398 #ifndef _LP64
399 ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
400 ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
401
402 #ifdef KERNEL2_SR
403 ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
404 #endif
405 #endif
406
407 for (i = 0; i < n; i++) {
408 #if defined (PMAC_G5)
409 register64_t pa = map[i].pa;
410 #else
411 register_t pa = map[i].pa;
412 #endif
413 vaddr_t va = map[i].va;
414 size_t length = map[i].len;
415
416 if (va < 0xf0000000) /* XXX */
417 continue;
418
419 while (length > 0) {
420 pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
421 VM_PROT_ALL|PMAP_WIRED);
422 pa += PAGE_SIZE;
423 va += PAGE_SIZE;
424 length -= PAGE_SIZE;
425 }
426 }
427 pmap_update(&ofw_pmap);
428 }
429
430
431
432 /*
433 * Scan the device tree for ranges, and return them as bitmap 0..15
434 */
435 #if !defined(macppc) && defined(PPC_OEA)
436 static u_int16_t
437 ranges_bitmap(int node, u_int16_t bitmap)
438 {
439 int child, mlen, acells, scells, reclen, i, j;
440 u_int32_t addr, len, map[160];
441
442 for (child = OF_child(node); child; child = OF_peer(child)) {
443 mlen = OF_getprop(child, "ranges", map, sizeof(map));
444 if (mlen == -1)
445 goto noranges;
446
447 j = OF_getprop(child, "#address-cells", &acells,
448 sizeof(acells));
449 if (j == -1)
450 goto noranges;
451
452 j = OF_getprop(child, "#size-cells", &scells,
453 sizeof(scells));
454 if (j == -1)
455 goto noranges;
456
457 #ifdef ofppc
458 reclen = acells + modeldata.ranges_offset + scells;
459 #else
460 reclen = acells + 1 + scells;
461 #endif
462
463 for (i=0; i < (mlen/4)/reclen; i++) {
464 addr = map[reclen * i + acells];
465 len = map[reclen * i + reclen - 1];
466 for (j = 0; j < len / 0x10000000; j++)
467 bitmap |= 1 << ((addr+j*0x10000000) >>28);
468 bitmap |= 1 << (addr >> 28);
469 }
470 noranges:
471 bitmap |= ranges_bitmap(child, bitmap);
472 continue;
473 }
474 return bitmap;
475 }
476 #endif /* !macppc && PPC_OEA */
477
478 void
479 ofwoea_batinit(void)
480 {
481 #if defined (PPC_OEA)
482
483 #ifdef macppc
484 /*
485 * cover PCI and register space but not the firmware ROM
486 */
487 #ifdef PPC_OEA601
488
489 /*
490 * use segment registers for the 601
491 */
492 if ((mfpvr() >> 16 ) == MPC601)
493 oea_batinit(
494 0x80000000, BAT_BL_256M,
495 0x90000000, BAT_BL_256M,
496 0xa0000000, BAT_BL_256M,
497 0xb0000000, BAT_BL_256M,
498 0xf0000000, BAT_BL_256M,
499 0);
500 else
501 #endif
502 /*
503 * map to bats
504 */
505 oea_batinit(0x80000000, BAT_BL_1G,
506 0xf0000000, BAT_BL_128M,
507 0xf8000000, BAT_BL_64M,
508 0xfe000000, BAT_BL_8M, /* Grackle IO */
509 0);
510 #else
511 uint16_t bitmap;
512 int node, i;
513
514 node = OF_finddevice("/");
515
516 bitmap = ranges_bitmap(node, 0);
517 oea_batinit(0);
518
519 for (i=1; i < 0x10; i++) {
520 /* skip the three vital SR regions */
521 if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
522 continue;
523 if (bitmap & (1 << i)) {
524 oea_iobat_add(0x10000000 * i, BAT_BL_256M);
525 DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
526 }
527 }
528 #endif
529 #endif /* OEA */
530 }
531
532
533 /* we define these partially, as we will fill the rest in later */
534 struct powerpc_bus_space genppc_isa_io_space_tag = {
535 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
536 .pbs_base = 0x00000000,
537 };
538
539 struct powerpc_bus_space genppc_isa_mem_space_tag = {
540 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
541 .pbs_base = 0x00000000,
542 };
543
544 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
545 * Increase if necc.
546 */
547 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
548 __attribute__((aligned(8)));
549
550
551 static void
552 find_ranges(int base, rangemap_t *regions, int *cur, int type)
553 {
554 int node, i, len, reclen;
555 u_int32_t parent_acells, acells, scells, map[160];
556 char tmp[32];
557
558 node = base;
559 if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
560 goto rec;
561 if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
562 strcmp("pci", tmp) != 0)
563 goto rec;
564 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
565 goto rec;
566 if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
567 len = OF_getprop(node, "reg", map, sizeof(map));
568 acells = 1;
569 scells = 1;
570 } else {
571 len = OF_getprop(node, "ranges", map, sizeof(map));
572 }
573 if (len == -1)
574 goto rec;
575 if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
576 sizeof(parent_acells)) != sizeof(parent_acells))
577 parent_acells = 1;
578 if (OF_getprop(node, "#address-cells", &acells,
579 sizeof(acells)) != sizeof(acells))
580 acells = 3;
581 if (OF_getprop(node, "#size-cells", &scells,
582 sizeof(scells)) != sizeof(scells))
583 scells = 2;
584 #ifdef ofppc
585 if (modeldata.ranges_offset == 0)
586 scells -= 1;
587 #endif
588 if (type == RANGE_TYPE_ISA)
589 reclen = 6;
590 else
591 reclen = parent_acells + acells + scells;
592 /*
593 * There exist ISA buses with empty ranges properties. This is
594 * known to occur on the Pegasos II machine, and likely others.
595 * According to them, that means that the isa bus is a fake bus, and
596 * the real maps are the PCI maps of the preceeding bus. To deal
597 * with this, we will set cur to -1 and return.
598 */
599 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
600 *cur = -1;
601 DPRINTF("Found empty range in isa bus\n");
602 return;
603 }
604
605 DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
606 switch (type) {
607 case RANGE_TYPE_PCI:
608 case RANGE_TYPE_FIRSTPCI:
609 for (i=0; i < len/(4*reclen); i++) {
610 DPRINTF("FOUND PCI RANGE\n");
611 regions[*cur].size =
612 map[i*reclen + parent_acells + acells + scells - 1];
613 /* skip ranges of size==0 */
614 if (regions[*cur].size == 0)
615 continue;
616 regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
617 regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
618 (*cur)++;
619 }
620 break;
621 case RANGE_TYPE_ISA:
622 for (i=0; i < len/(4*reclen); i++) {
623 if (map[i*reclen] == 1)
624 regions[*cur].type = RANGE_IO;
625 else
626 regions[*cur].type = RANGE_MEM;
627 DPRINTF("FOUND ISA RANGE TYPE=%d\n",
628 regions[*cur].type);
629 regions[*cur].size =
630 map[i*reclen + acells + scells];
631 (*cur)++;
632 }
633 break;
634 case RANGE_TYPE_MACIO:
635 regions[*cur].type = RANGE_MEM;
636 if (len == 8) {
637 regions[*cur].size = map[1];
638 regions[*cur].addr = map[0];
639 } else {
640 regions[*cur].size = map[2];
641 regions[*cur].addr = map[1];
642 }
643 (*cur)++;
644 break;
645 }
646 DPRINTF("returning with CUR=%d\n", *cur);
647 return;
648 rec:
649 for (node = OF_child(base); node; node = OF_peer(node)) {
650 DPRINTF("RECURSE 1 STEP\n");
651 find_ranges(node, regions, cur, type);
652 if (*cur == -1)
653 return;
654 }
655 }
656
657 static int
658 find_lowest_range(rangemap_t *ranges, int nrof, int type)
659 {
660 int i, low = 0;
661 u_int32_t addr = 0xffffffff;
662
663 for (i=0; i < nrof; i++) {
664 if (ranges[i].type == type && ranges[i].addr != 0 &&
665 ranges[i].addr < addr) {
666 low = i;
667 addr = ranges[i].addr;
668 }
669 }
670 if (addr == 0xffffffff)
671 return -1;
672 return low;
673 }
674
675 /*
676 * Find a region of memory, and create a bus_space_tag for it.
677 * Notes:
678 * For ISA node is ignored.
679 * node is the starting node. if -1, we start at / and map everything.
680 */
681
682 int
683 ofwoea_map_space(int rangetype, int iomem, int node,
684 struct powerpc_bus_space *tag, const char *name)
685 {
686 int i, cur, range, nrofholes, error;
687 static int exmap=0;
688 rangemap_t region, holes[32], list[32];
689
690 memset(list, 0, sizeof(list));
691 memset(®ion, 0, sizeof(region));
692 cur = 0;
693 if (rangetype == RANGE_TYPE_ISA || node == -1)
694 node = OF_finddevice("/");
695 if (rangetype == RANGE_TYPE_ISA) {
696 u_int32_t size = 0;
697 rangemap_t regions[32];
698
699 DPRINTF("LOOKING FOR FIRSTPCI\n");
700 find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
701 range = 0;
702 DPRINTF("LOOKING FOR ISA\n");
703 find_ranges(node, regions, &range, RANGE_TYPE_ISA);
704 if (range == 0 || cur == 0)
705 return -1; /* no isa stuff found */
706 /*
707 * This may be confusing to some. The ISA ranges property
708 * is supposed to be a set of IO ranges for the ISA bus, but
709 * generally, it's just a set of pci devfunc lists that tell
710 * you to go look at the parent PCI device for the actual
711 * ranges.
712 */
713 if (range == -1) {
714 /* we found a rangeless isa bus */
715 if (iomem == RANGE_IO)
716 size = 0x10000;
717 else
718 size = 0x1000000;
719 }
720 DPRINTF("found isa stuff\n");
721 for (i=0; i < range; i++)
722 if (regions[i].type == iomem)
723 size = regions[i].size;
724 if (iomem == RANGE_IO) {
725 /* the first io range is the one */
726 for (i=0; i < cur; i++)
727 if (list[i].type == RANGE_IO && size) {
728 DPRINTF("found IO\n");
729 tag->pbs_offset = list[i].addr;
730 tag->pbs_limit = size;
731 error = bus_space_init(tag, name,
732 ex_storage[exmap],
733 sizeof(ex_storage[exmap]));
734 exmap++;
735 return error;
736 }
737 } else {
738 for (i=0; i < cur; i++)
739 if (list[i].type == RANGE_MEM &&
740 list[i].size == size) {
741 DPRINTF("found mem\n");
742 tag->pbs_offset = list[i].addr;
743 tag->pbs_limit = size;
744 error = bus_space_init(tag, name,
745 ex_storage[exmap],
746 sizeof(ex_storage[exmap]));
747 exmap++;
748 return error;
749 }
750 }
751 return -1; /* NO ISA FOUND */
752 }
753 find_ranges(node, list, &cur, rangetype);
754
755 DPRINTF("cur == %d\n", cur);
756 /* now list should contain a list of memory regions */
757 for (i=0; i < cur; i++)
758 DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
759 list[i].size, list[i].type);
760
761 range = find_lowest_range(list, cur, iomem);
762 i = 0;
763 nrofholes = 0;
764 while (range != -1) {
765 DPRINTF("range==%d\n", range);
766 DPRINTF("i==%d\n", i);
767 if (i == 0) {
768 memcpy(®ion, &list[range], sizeof(rangemap_t));
769 list[range].addr = 0;
770 i++;
771 range = find_lowest_range(list, cur, iomem);
772 continue;
773 }
774 if (region.addr + region.size < list[range].addr) {
775 /* allocate a hole */
776 holes[nrofholes].type = iomem;
777 holes[nrofholes].addr = region.size + region.addr;
778 holes[nrofholes].size = list[range].addr -
779 holes[nrofholes].addr - 1;
780 nrofholes++;
781 }
782 region.size = list[range].size + list[range].addr -
783 region.addr;
784 list[range].addr = 0;
785 range = find_lowest_range(list, cur, iomem);
786 }
787 DPRINTF("RANGE iomem=%d FOUND\n", iomem);
788 DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
789 region.size, region.type);
790 DPRINTF("HOLES FOUND\n");
791 for (i=0; i < nrofholes; i++)
792 DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
793 holes[i].size, holes[i].type);
794 /* AT THIS POINT WE MAP IT */
795
796 if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
797 if (exmap == EXSTORAGE_MAX)
798 panic("Not enough ex_storage space. "
799 "Increase EXSTORAGE_MAX");
800
801 /* XXX doing this in here might be wrong */
802 if (iomem == 1) {
803 /* we map an IO region */
804 tag->pbs_offset = region.addr;
805 tag->pbs_base = 0;
806 tag->pbs_limit = region.size;
807 } else {
808 /* ... or a memory region */
809 tag->pbs_offset = 0;
810 tag->pbs_base = region.addr;
811 tag->pbs_limit = region.size + region.addr;
812 }
813
814 error = bus_space_init(tag, name, ex_storage[exmap],
815 sizeof(ex_storage[exmap]));
816 exmap++;
817 if (error)
818 panic("ofwoea_bus_space_init: can't init tag %s", name);
819 for (i=0; i < nrofholes; i++) {
820 if (holes[i].type == RANGE_IO) {
821 error = extent_alloc_region(tag->pbs_extent,
822 holes[i].addr - tag->pbs_offset,
823 holes[i].size, EX_NOWAIT);
824 } else {
825 error = extent_alloc_region(tag->pbs_extent,
826 holes[i].addr, holes[i].size, EX_NOWAIT);
827 }
828 if (error)
829 panic("ofwoea_bus_space_init: can't block out"
830 " reserved space 0x%x-0x%x: error=%d",
831 holes[i].addr, holes[i].addr+holes[i].size,
832 error);
833 }
834 return error;
835 }
836 return -1;
837 }
838
839 void
840 ofwoea_bus_space_init(void)
841 {
842 int error;
843
844 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
845 &genppc_isa_io_space_tag, "isa-ioport");
846 if (error > 0)
847 panic("Could not map ISA IO");
848
849 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
850 &genppc_isa_mem_space_tag, "isa-iomem");
851 if (error > 0)
852 panic("Could not map ISA MEM");
853 }
854