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