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