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