ofwoea_machdep.c revision 1.47 1 /* $NetBSD: ofwoea_machdep.c,v 1.47 2020/02/28 22:14:10 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.47 2020/02/28 22:14:10 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_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,2", 12) == 0 ||
179 strncmp(model_name, "PowerMac12,1", 12) == 0)
180 OF_quiesce();
181
182 /* switch CPUs to full speed */
183 if (strncmp(model_name, "PowerMac7,", 10) == 0) {
184 int clock_ih = OF_open("/u3/i2c/i2c-hwclock");
185 if (clock_ih != 0) {
186 OF_call_method_1("slew-high", clock_ih, 0);
187 }
188 }
189
190 /* Initialize bus_space */
191 ofwoea_bus_space_init();
192
193 ofwoea_consinit();
194
195 #if defined(MULTIPROCESSOR) && defined(ofppc)
196 for (i=1; i < CPU_MAXNUM; i++) {
197 snprintf(cpupath, sizeof(cpupath), "/cpus/@%x", i);
198 node = OF_finddevice(cpupath);
199 if (node <= 0)
200 continue;
201 aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
202 OF_start_cpu(node, (u_int)cpu_spinstart, i);
203 for (l=0; l < 100000000; l++) {
204 if (cpu_spinstart_ack == i) {
205 aprint_verbose("CPU %d spun up.\n", i);
206 break;
207 }
208 __asm volatile ("sync");
209 }
210 }
211 #endif
212
213 oea_init(pic_ext_intr);
214
215 ofmaplen = save_ofmap(NULL, 0);
216 if (ofmaplen > 0)
217 save_ofmap(ofmap, ofmaplen);
218
219 /*
220 * XXX
221 * we need to do this here instead of earlier on in ofwinit() for some reason
222 * At least some versions of Apple OF 2.0.1 hang if we do this earlier
223 */
224 ofwmsr &= ~PSL_IP;
225
226 /* Parse the args string */
227 if (args) {
228 strcpy(bootpath, args);
229 args = bootpath;
230 while (*++args && *args != ' ');
231 if (*args) {
232 *args++ = 0;
233 while (*args)
234 BOOT_FLAG(*args++, boothowto);
235 }
236 } else {
237 int chs = OF_finddevice("/chosen");
238 int len;
239
240 len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
241 if (len > -1)
242 bootpath[len] = 0;
243 }
244
245 uvm_md_init();
246
247 pmap_bootstrap(startkernel, endkernel);
248
249 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
250 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
251 #if defined (PMAC_G5)
252 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
253 if (oeacpufeat & OEACPU_64_BRIDGE) {
254 vaddr_t va;
255 paddr_t pa;
256 int i;
257
258 pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
259 round_page(MSGBUFSIZE), 0x0);
260
261 /* Map OFW code+data */
262
263 for (i = 0; i < ofmaplen / sizeof(struct ofw_translations); i++) {
264 if (ofmap[i].va < 0xff800000)
265 continue;
266
267 for (va = ofmap[i].va, pa = ofmap[i].pa;
268 va < ofmap[i].va + ofmap[i].len;
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(ofmap, ofmaplen);
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 static int
376 save_ofmap(struct ofw_translations *map, int maxlen)
377 {
378 int mmui, mmu, len;
379
380 OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
381 mmu = OF_instance_to_package(mmui);
382
383 if (map) {
384 memset(map, 0, maxlen); /* to be safe */
385 len = OF_getprop(mmu, "translations", map, maxlen);
386 } else
387 len = OF_getproplen(mmu, "translations");
388
389 if (len < 0)
390 len = 0;
391 return len;
392 }
393
394
395 /* The PMAC_G5 code here needs to be replaced by code that looks for the
396 size_cells and does the right thing automatically.
397 */
398 void
399 restore_ofmap(struct ofw_translations *map, int len)
400 {
401 int n = len / sizeof(struct ofw_translations);
402 int i;
403
404 pmap_pinit(&ofw_pmap);
405
406 #ifndef _LP64
407 ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
408 ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
409
410 #ifdef KERNEL2_SR
411 ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
412 #endif
413 #endif
414
415 for (i = 0; i < n; i++) {
416 #if defined (PMAC_G5)
417 register64_t pa = map[i].pa;
418 #else
419 register_t pa = map[i].pa;
420 #endif
421 vaddr_t va = map[i].va;
422 size_t length = map[i].len;
423
424 if (va < 0xf0000000) /* XXX */
425 continue;
426
427 while (length > 0) {
428 pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
429 VM_PROT_ALL|PMAP_WIRED);
430 pa += PAGE_SIZE;
431 va += PAGE_SIZE;
432 length -= PAGE_SIZE;
433 }
434 }
435 pmap_update(&ofw_pmap);
436 }
437
438
439
440 /*
441 * Scan the device tree for ranges, and return them as bitmap 0..15
442 */
443 #if !defined(macppc) && defined(PPC_OEA)
444 static u_int16_t
445 ranges_bitmap(int node, u_int16_t bitmap)
446 {
447 int child, mlen, acells, scells, reclen, i, j;
448 u_int32_t addr, len, map[160];
449
450 for (child = OF_child(node); child; child = OF_peer(child)) {
451 mlen = OF_getprop(child, "ranges", map, sizeof(map));
452 if (mlen == -1)
453 goto noranges;
454
455 j = OF_getprop(child, "#address-cells", &acells,
456 sizeof(acells));
457 if (j == -1)
458 goto noranges;
459
460 j = OF_getprop(child, "#size-cells", &scells,
461 sizeof(scells));
462 if (j == -1)
463 goto noranges;
464
465 #ifdef ofppc
466 reclen = acells + modeldata.ranges_offset + scells;
467 #else
468 reclen = acells + 1 + scells;
469 #endif
470
471 for (i=0; i < (mlen/4)/reclen; i++) {
472 addr = map[reclen * i + acells];
473 len = map[reclen * i + reclen - 1];
474 for (j = 0; j < len / 0x10000000; j++)
475 bitmap |= 1 << ((addr+j*0x10000000) >>28);
476 bitmap |= 1 << (addr >> 28);
477 }
478 noranges:
479 bitmap |= ranges_bitmap(child, bitmap);
480 continue;
481 }
482 return bitmap;
483 }
484 #endif /* !macppc && PPC_OEA */
485
486 void
487 ofwoea_batinit(void)
488 {
489 #if defined (PPC_OEA)
490
491 #ifdef macppc
492 /*
493 * cover PCI and register space but not the firmware ROM
494 */
495 #ifdef PPC_OEA601
496
497 /*
498 * use segment registers for the 601
499 */
500 if ((mfpvr() >> 16 ) == MPC601)
501 oea_batinit(
502 0x80000000, BAT_BL_256M,
503 0x90000000, BAT_BL_256M,
504 0xa0000000, BAT_BL_256M,
505 0xb0000000, BAT_BL_256M,
506 0xf0000000, BAT_BL_256M,
507 0);
508 else
509 #endif
510 /*
511 * map to bats
512 */
513 oea_batinit(0x80000000, BAT_BL_1G,
514 0xf0000000, BAT_BL_128M,
515 0xf8000000, BAT_BL_64M,
516 0xfe000000, BAT_BL_8M, /* Grackle IO */
517 0);
518 #else
519 uint16_t bitmap;
520 int node, i;
521
522 node = OF_finddevice("/");
523
524 bitmap = ranges_bitmap(node, 0);
525 oea_batinit(0);
526
527 for (i=1; i < 0x10; i++) {
528 /* skip the three vital SR regions */
529 if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
530 continue;
531 if (bitmap & (1 << i)) {
532 oea_iobat_add(0x10000000 * i, BAT_BL_256M);
533 DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
534 }
535 }
536 #endif
537 #endif /* OEA */
538 }
539
540
541 /* we define these partially, as we will fill the rest in later */
542 struct powerpc_bus_space genppc_isa_io_space_tag = {
543 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
544 .pbs_base = 0x00000000,
545 };
546
547 struct powerpc_bus_space genppc_isa_mem_space_tag = {
548 .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
549 .pbs_base = 0x00000000,
550 };
551
552 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
553 * Increase if necc.
554 */
555 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
556 __attribute__((aligned(8)));
557
558
559 static void
560 find_ranges(int base, rangemap_t *regions, int *cur, int type)
561 {
562 int node, i, len, reclen;
563 u_int32_t parent_acells, acells, scells, map[160];
564 char tmp[32];
565
566 node = base;
567 if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
568 goto rec;
569 if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
570 strcmp("pci", tmp) != 0)
571 goto rec;
572 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
573 goto rec;
574 if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
575 len = OF_getprop(node, "reg", map, sizeof(map));
576 acells = 1;
577 scells = 1;
578 } else {
579 len = OF_getprop(node, "ranges", map, sizeof(map));
580 }
581 if (len == -1)
582 goto rec;
583 if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
584 sizeof(parent_acells)) != sizeof(parent_acells))
585 parent_acells = 1;
586 if (OF_getprop(node, "#address-cells", &acells,
587 sizeof(acells)) != sizeof(acells))
588 acells = 3;
589 if (OF_getprop(node, "#size-cells", &scells,
590 sizeof(scells)) != sizeof(scells))
591 scells = 2;
592 #ifdef ofppc
593 if (modeldata.ranges_offset == 0)
594 scells -= 1;
595 #endif
596 if (type == RANGE_TYPE_ISA)
597 reclen = 6;
598 else
599 reclen = parent_acells + acells + scells;
600 /*
601 * There exist ISA buses with empty ranges properties. This is
602 * known to occur on the Pegasos II machine, and likely others.
603 * According to them, that means that the isa bus is a fake bus, and
604 * the real maps are the PCI maps of the preceeding bus. To deal
605 * with this, we will set cur to -1 and return.
606 */
607 if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
608 *cur = -1;
609 DPRINTF("Found empty range in isa bus\n");
610 return;
611 }
612
613 DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
614 switch (type) {
615 case RANGE_TYPE_PCI:
616 case RANGE_TYPE_FIRSTPCI:
617 for (i=0; i < len/(4*reclen); i++) {
618 DPRINTF("FOUND PCI RANGE\n");
619 regions[*cur].size =
620 map[i*reclen + parent_acells + acells + scells - 1];
621 /* skip ranges of size==0 */
622 if (regions[*cur].size == 0)
623 continue;
624 regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
625 regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
626 (*cur)++;
627 }
628 break;
629 case RANGE_TYPE_ISA:
630 for (i=0; i < len/(4*reclen); i++) {
631 if (map[i*reclen] == 1)
632 regions[*cur].type = RANGE_IO;
633 else
634 regions[*cur].type = RANGE_MEM;
635 DPRINTF("FOUND ISA RANGE TYPE=%d\n",
636 regions[*cur].type);
637 regions[*cur].size =
638 map[i*reclen + acells + scells];
639 (*cur)++;
640 }
641 break;
642 case RANGE_TYPE_MACIO:
643 regions[*cur].type = RANGE_MEM;
644 if (len == 8) {
645 regions[*cur].size = map[1];
646 regions[*cur].addr = map[0];
647 } else {
648 regions[*cur].size = map[2];
649 regions[*cur].addr = map[1];
650 }
651 (*cur)++;
652 break;
653 }
654 DPRINTF("returning with CUR=%d\n", *cur);
655 return;
656 rec:
657 for (node = OF_child(base); node; node = OF_peer(node)) {
658 DPRINTF("RECURSE 1 STEP\n");
659 find_ranges(node, regions, cur, type);
660 if (*cur == -1)
661 return;
662 }
663 }
664
665 static int
666 find_lowest_range(rangemap_t *ranges, int nrof, int type)
667 {
668 int i, low = 0;
669 u_int32_t addr = 0xffffffff;
670
671 for (i=0; i < nrof; i++) {
672 if (ranges[i].type == type && ranges[i].addr != 0 &&
673 ranges[i].addr < addr) {
674 low = i;
675 addr = ranges[i].addr;
676 }
677 }
678 if (addr == 0xffffffff)
679 return -1;
680 return low;
681 }
682
683 /*
684 * Find a region of memory, and create a bus_space_tag for it.
685 * Notes:
686 * For ISA node is ignored.
687 * node is the starting node. if -1, we start at / and map everything.
688 */
689
690 int
691 ofwoea_map_space(int rangetype, int iomem, int node,
692 struct powerpc_bus_space *tag, const char *name)
693 {
694 int i, cur, range, nrofholes, error;
695 static int exmap=0;
696 rangemap_t region, holes[32], list[32];
697
698 memset(list, 0, sizeof(list));
699 memset(®ion, 0, sizeof(region));
700 cur = 0;
701 if (rangetype == RANGE_TYPE_ISA || node == -1)
702 node = OF_finddevice("/");
703 if (rangetype == RANGE_TYPE_ISA) {
704 u_int32_t size = 0;
705 rangemap_t regions[32];
706
707 DPRINTF("LOOKING FOR FIRSTPCI\n");
708 find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
709 range = 0;
710 DPRINTF("LOOKING FOR ISA\n");
711 find_ranges(node, regions, &range, RANGE_TYPE_ISA);
712 if (range == 0 || cur == 0)
713 return -1; /* no isa stuff found */
714 /*
715 * This may be confusing to some. The ISA ranges property
716 * is supposed to be a set of IO ranges for the ISA bus, but
717 * generally, it's just a set of pci devfunc lists that tell
718 * you to go look at the parent PCI device for the actual
719 * ranges.
720 */
721 if (range == -1) {
722 /* we found a rangeless isa bus */
723 if (iomem == RANGE_IO)
724 size = 0x10000;
725 else
726 size = 0x1000000;
727 }
728 DPRINTF("found isa stuff\n");
729 for (i=0; i < range; i++)
730 if (regions[i].type == iomem)
731 size = regions[i].size;
732 if (iomem == RANGE_IO) {
733 /* the first io range is the one */
734 for (i=0; i < cur; i++)
735 if (list[i].type == RANGE_IO && size) {
736 DPRINTF("found IO\n");
737 tag->pbs_offset = list[i].addr;
738 tag->pbs_limit = size;
739 error = bus_space_init(tag, name,
740 ex_storage[exmap],
741 sizeof(ex_storage[exmap]));
742 exmap++;
743 return error;
744 }
745 } else {
746 for (i=0; i < cur; i++)
747 if (list[i].type == RANGE_MEM &&
748 list[i].size == size) {
749 DPRINTF("found mem\n");
750 tag->pbs_offset = list[i].addr;
751 tag->pbs_limit = size;
752 error = bus_space_init(tag, name,
753 ex_storage[exmap],
754 sizeof(ex_storage[exmap]));
755 exmap++;
756 return error;
757 }
758 }
759 return -1; /* NO ISA FOUND */
760 }
761 find_ranges(node, list, &cur, rangetype);
762
763 DPRINTF("cur == %d\n", cur);
764 /* now list should contain a list of memory regions */
765 for (i=0; i < cur; i++)
766 DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
767 list[i].size, list[i].type);
768
769 range = find_lowest_range(list, cur, iomem);
770 i = 0;
771 nrofholes = 0;
772 while (range != -1) {
773 DPRINTF("range==%d\n", range);
774 DPRINTF("i==%d\n", i);
775 if (i == 0) {
776 memcpy(®ion, &list[range], sizeof(rangemap_t));
777 list[range].addr = 0;
778 i++;
779 range = find_lowest_range(list, cur, iomem);
780 continue;
781 }
782 if (region.addr + region.size < list[range].addr) {
783 /* allocate a hole */
784 holes[nrofholes].type = iomem;
785 holes[nrofholes].addr = region.size + region.addr;
786 holes[nrofholes].size = list[range].addr -
787 holes[nrofholes].addr - 1;
788 nrofholes++;
789 }
790 region.size = list[range].size + list[range].addr -
791 region.addr;
792 list[range].addr = 0;
793 range = find_lowest_range(list, cur, iomem);
794 }
795 DPRINTF("RANGE iomem=%d FOUND\n", iomem);
796 DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
797 region.size, region.type);
798 DPRINTF("HOLES FOUND\n");
799 for (i=0; i < nrofholes; i++)
800 DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
801 holes[i].size, holes[i].type);
802 /* AT THIS POINT WE MAP IT */
803
804 if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
805 if (exmap == EXSTORAGE_MAX)
806 panic("Not enough ex_storage space. "
807 "Increase EXSTORAGE_MAX");
808
809 /* XXX doing this in here might be wrong */
810 if (iomem == 1) {
811 /* we map an IO region */
812 tag->pbs_offset = region.addr;
813 tag->pbs_base = 0;
814 tag->pbs_limit = region.size;
815 } else {
816 /* ... or a memory region */
817 tag->pbs_offset = 0;
818 tag->pbs_base = region.addr;
819 tag->pbs_limit = region.size + region.addr;
820 }
821
822 error = bus_space_init(tag, name, ex_storage[exmap],
823 sizeof(ex_storage[exmap]));
824 exmap++;
825 if (error)
826 panic("ofwoea_bus_space_init: can't init tag %s", name);
827 for (i=0; i < nrofholes; i++) {
828 if (holes[i].type == RANGE_IO) {
829 error = extent_alloc_region(tag->pbs_extent,
830 holes[i].addr - tag->pbs_offset,
831 holes[i].size, EX_NOWAIT);
832 } else {
833 error = extent_alloc_region(tag->pbs_extent,
834 holes[i].addr, holes[i].size, EX_NOWAIT);
835 }
836 if (error)
837 panic("ofwoea_bus_space_init: can't block out"
838 " reserved space 0x%x-0x%x: error=%d",
839 holes[i].addr, holes[i].addr+holes[i].size,
840 error);
841 }
842 return error;
843 }
844 return -1;
845 }
846
847 void
848 ofwoea_bus_space_init(void)
849 {
850 int error;
851
852 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
853 &genppc_isa_io_space_tag, "isa-ioport");
854 if (error > 0)
855 panic("Could not map ISA IO");
856
857 error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
858 &genppc_isa_mem_space_tag, "isa-iomem");
859 if (error > 0)
860 panic("Could not map ISA MEM");
861 }
862