1 /* $NetBSD: ofwoea_machdep.c,v 1.16 2008/11/30 18:21:35 martin Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.16 2008/11/30 18:21:35 martin Exp $");
35 #include "opt_ppcarch.h"
36 #include "opt_compat_netbsd.h"
39 #include "opt_ipkdb.h"
40 #include "opt_modular.h"
42 #include <sys/param.h>
44 #include <sys/boot_flag.h>
45 #include <sys/extent.h>
46 #include <sys/kernel.h>
47 #include <sys/ksyms.h>
48 #include <uvm/uvm_extern.h>
50 #include <dev/ofw/openfirm.h>
51 #include <machine/pmap.h>
52 #include <machine/powerpc.h>
53 #include <machine/trap.h>
54 #include <machine/vmparam.h>
55 #include <machine/autoconf.h>
56 #include <powerpc/bus.h>
57 #include <powerpc/oea/bat.h>
58 #include <powerpc/oea/cpufeat.h>
59 #include <powerpc/ofw_bus.h>
60 #include <powerpc/ofw_cons.h>
61 #include <powerpc/spr.h>
62 #include <arch/powerpc/pic/picvar.h>
69 #include <machine/db_machdep.h>
70 #include <ddb/db_extern.h>
78 #include <ipkdb/ipkdb.h>
81 #include "opt_ofwoea.h"
84 extern struct model_data modeldata
;
88 #define DPRINTF printf
90 #define DPRINTF while (0) printf
93 typedef struct _rangemap
{
99 struct ofw_translations
{
102 #if defined (PMAC_G5)
108 }__attribute__((packed
));
110 struct pmap ofw_pmap
;
111 struct ofw_translations ofmap
[32];
114 #if NKSYMS || defined(DDB) || defined(MODULAR)
115 void *startsym
, *endsym
;
118 u_int timebase_freq
= TIMEBASE_FREQ
;
120 u_int timebase_freq
= 0;
125 extern uint32_t ticks_per_sec
;
126 extern uint32_t ns_per_tick
;
127 extern uint32_t ticks_per_intr
;
129 static int save_ofmap(struct ofw_translations
*, int);
130 static void restore_ofmap(struct ofw_translations
*, int);
131 static void set_timebase(void);
133 extern void cpu_spinstart(u_int
);
134 volatile u_int cpu_spinstart_ack
, cpu_spinstart_cpunum
;
137 ofwoea_initppc(u_int startkernel
, u_int endkernel
, char *args
)
139 int ofmaplen
, node
, l
;
142 #if defined(MULTIPROCESSOR) && defined(ofppc)
148 if ((oeacpufeat
& OEACPU_NOBAT
) == 0)
151 #if NKSYMS || defined(DDB) || defined(MODULAR)
152 /* get info of kernel symbol table from bootloader */
153 memcpy(&startsym
, args
+ strlen(args
) + 1, sizeof(startsym
));
154 memcpy(&endsym
, args
+ strlen(args
) + 1 + sizeof(startsym
),
156 if (startsym
== NULL
|| endsym
== NULL
)
157 startsym
= endsym
= NULL
;
160 /* get model name and perform model-specific actions */
161 memset(model_name
, 0, sizeof(model_name
));
162 node
= OF_finddevice("/");
164 l
= OF_getprop(node
, "model", model_name
, sizeof(model_name
));
166 OF_getprop(node
, "name", model_name
,
173 #if defined(MULTIPROCESSOR) && defined(ofppc)
174 for (i
=1; i
< CPU_MAXNUM
; i
++) {
175 sprintf(cpupath
, "/cpus/@%x", i
);
176 node
= OF_finddevice(cpupath
);
179 aprint_verbose("Starting up CPU %d %s\n", i
, cpupath
);
180 OF_start_cpu(node
, (u_int
)cpu_spinstart
, i
);
181 for (l
=0; l
< 100000000; l
++) {
182 if (cpu_spinstart_ack
== i
) {
183 aprint_verbose("CPU %d spun up.\n", i
);
186 __asm
volatile ("sync");
191 oea_init(pic_ext_intr
);
193 ofmaplen
= save_ofmap(NULL
, 0);
195 save_ofmap(ofmap
, ofmaplen
);
199 /* Parse the args string */
201 strcpy(bootpath
, args
);
203 while (*++args
&& *args
!= ' ');
207 BOOT_FLAG(*args
++, boothowto
);
213 #if defined (PPC_OEA64_BRIDGE) && defined (PPC_OEA)
214 if (oeacpufeat
& OEACPU_64_BRIDGE
)
215 pmap_setup64bridge();
219 pmap_bootstrap(startkernel
, endkernel
);
221 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
222 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
223 #if defined (PMAC_G5)
224 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
225 if (oeacpufeat
& OEACPU_64_BRIDGE
)
226 pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000,
228 #elif defined (MAMBO)
229 /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
230 if (oeacpufeat
& OEACPU_64_BRIDGE
)
231 pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
233 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
235 /* Now enable translation (and machine checks/recoverable interrupts) */
236 __asm
__volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
238 : "K"(PSL_IR
|PSL_DR
|PSL_ME
|PSL_RI
));
240 restore_ofmap(ofmap
, ofmaplen
);
242 #if NKSYMS || defined(DDB) || defined(MODULAR)
243 ksyms_addsyms_elf((int)((u_int
)endsym
- (u_int
)startsym
), startsym
, endsym
);
246 /* CPU clock stuff */
253 int qhandle
, phandle
, msr
, scratch
;
256 if (timebase_freq
!= 0) {
257 ticks_per_sec
= timebase_freq
;
261 for (qhandle
= OF_peer(0); qhandle
; qhandle
= phandle
) {
262 if (OF_getprop(qhandle
, "device_type", type
, sizeof type
) > 0
263 && strcmp(type
, "cpu") == 0
264 && OF_getprop(qhandle
, "timebase-frequency",
265 &ticks_per_sec
, sizeof ticks_per_sec
) > 0) {
268 if ((phandle
= OF_child(qhandle
)))
271 if ((phandle
= OF_peer(qhandle
)))
273 qhandle
= OF_parent(qhandle
);
276 panic("no cpu node");
279 __asm
volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
280 : "=r"(msr
), "=r"(scratch
) : "K"((u_short
)~PSL_EE
));
281 ns_per_tick
= 1000000000 / ticks_per_sec
;
282 ticks_per_intr
= ticks_per_sec
/ hz
;
283 cpu_timebase
= ticks_per_sec
;
284 curcpu()->ci_lasttb
= mftbl();
285 mtspr(SPR_DEC
, ticks_per_intr
);
290 save_ofmap(struct ofw_translations
*map
, int maxlen
)
294 OF_getprop(chosen
, "mmu", &mmui
, sizeof mmui
);
295 mmu
= OF_instance_to_package(mmui
);
298 memset(map
, 0, maxlen
); /* to be safe */
299 len
= OF_getprop(mmu
, "translations", map
, maxlen
);
301 len
= OF_getproplen(mmu
, "translations");
309 /* The PMAC_G5 code here needs to be replaced by code that looks for the
310 size_cells and does the right thing automatically.
313 restore_ofmap(struct ofw_translations
*map
, int len
)
315 int n
= len
/ sizeof(struct ofw_translations
);
318 pmap_pinit(&ofw_pmap
);
320 ofw_pmap
.pm_sr
[KERNEL_SR
] = KERNEL_SEGMENT
;
323 ofw_pmap
.pm_sr
[KERNEL2_SR
] = KERNEL2_SEGMENT
;
326 for (i
= 0; i
< n
; i
++) {
327 #if defined (PMAC_G5)
328 register64_t pa
= map
[i
].pa
;
330 register_t pa
= map
[i
].pa
;
332 vaddr_t va
= map
[i
].va
;
333 size_t length
= map
[i
].len
;
335 if (va
< 0xf0000000) /* XXX */
339 pmap_enter(&ofw_pmap
, va
, (paddr_t
)pa
, VM_PROT_ALL
,
340 VM_PROT_ALL
|PMAP_WIRED
);
346 pmap_update(&ofw_pmap
);
352 * Scan the device tree for ranges, and batmap them.
356 ranges_bitmap(int node
, u_int16_t bitmap
)
358 int child
, mlen
, acells
, scells
, reclen
, i
, j
;
359 u_int32_t addr
, len
, map
[160];
361 for (child
= OF_child(node
); child
; child
= OF_peer(child
)) {
362 mlen
= OF_getprop(child
, "ranges", map
, sizeof(map
));
366 j
= OF_getprop(child
, "#address-cells", &acells
,
371 j
= OF_getprop(child
, "#size-cells", &scells
,
376 reclen
= acells
+ 1 + scells
;
378 for (i
=0; i
< (mlen
/4)/reclen
; i
++) {
379 addr
= map
[reclen
* i
+ acells
];
380 len
= map
[reclen
* i
+ reclen
- 1];
381 for (j
= 0; j
< len
/ 0x10000000; j
++)
382 bitmap
|= 1 << ((addr
+j
*0x10000000) >>28);
383 bitmap
|= 1 << (addr
>> 28);
386 bitmap
|= ranges_bitmap(child
, bitmap
);
395 #if defined (PPC_OEA)
399 node
= OF_finddevice("/");
400 bitmap
= ranges_bitmap(node
, 0);
404 /* XXX this is a macppc-specific hack */
407 for (i
=1; i
< 0x10; i
++) {
408 /* skip the three vital SR regions */
409 if (i
== USER_SR
|| i
== KERNEL_SR
|| i
== KERNEL2_SR
)
411 if (bitmap
& (1 << i
)) {
412 oea_iobat_add(0x10000000 * i
, BAT_BL_256M
);
413 DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i
);
420 /* we define these partially, as we will fill the rest in later */
421 struct powerpc_bus_space genppc_isa_io_space_tag
= {
422 .pbs_flags
= _BUS_SPACE_LITTLE_ENDIAN
|_BUS_SPACE_IO_TYPE
,
423 .pbs_base
= 0x00000000,
426 struct powerpc_bus_space genppc_isa_mem_space_tag
= {
427 .pbs_flags
= _BUS_SPACE_LITTLE_ENDIAN
|_BUS_SPACE_MEM_TYPE
,
428 .pbs_base
= 0x00000000,
431 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
434 static char ex_storage
[EXSTORAGE_MAX
][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES
)]
435 __attribute__((aligned(8)));
439 find_ranges(int base
, rangemap_t
*regions
, int *cur
, int type
)
441 int node
, i
, len
, reclen
;
442 u_int32_t acells
, scells
, map
[160];
446 if (OF_getprop(node
, "device_type", tmp
, sizeof(tmp
)) == -1)
448 if ((type
== RANGE_TYPE_PCI
|| type
== RANGE_TYPE_FIRSTPCI
) &&
449 strcmp("pci", tmp
) != 0)
451 if (type
== RANGE_TYPE_ISA
&& strcmp("isa", tmp
) != 0)
453 len
= OF_getprop(node
, "ranges", map
, sizeof(map
));
456 if (OF_getprop(node
, "#address-cells", &acells
,
457 sizeof(acells
)) != sizeof(acells
))
459 if (OF_getprop(node
, "#size-cells", &scells
,
460 sizeof(scells
)) != sizeof(scells
))
463 if (modeldata
.ranges_offset
== 0)
466 if (type
== RANGE_TYPE_ISA
)
469 reclen
= acells
+ scells
+ 1;
471 * There exist ISA buses with empty ranges properties. This is
472 * known to occur on the Pegasos II machine, and likely others.
473 * According to them, that means that the isa bus is a fake bus, and
474 * the real maps are the PCI maps of the preceeding bus. To deal
475 * with this, we will set cur to -1 and return.
477 if (type
== RANGE_TYPE_ISA
&& strcmp("isa", tmp
) == 0 && len
== 0) {
479 DPRINTF("Found empty range in isa bus\n");
483 DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen
, *cur
, len
);
486 case RANGE_TYPE_FIRSTPCI
:
487 for (i
=0; i
< len
/(4*reclen
); i
++) {
488 DPRINTF("FOUND PCI RANGE\n");
490 map
[i
*reclen
+ acells
+ scells
];
491 /* skip ranges of size==0 */
492 if (regions
[*cur
].size
== 0)
494 regions
[*cur
].type
= map
[i
*reclen
] >> 24;
495 regions
[*cur
].addr
= map
[i
*reclen
+ acells
];
500 for (i
=0; i
< len
/(4*reclen
); i
++) {
501 if (map
[i
*reclen
] == 1)
502 regions
[*cur
].type
= RANGE_IO
;
504 regions
[*cur
].type
= RANGE_MEM
;
505 DPRINTF("FOUND ISA RANGE TYPE=%d\n",
508 map
[i
*reclen
+ acells
+ scells
];
513 DPRINTF("returning with CUR=%d\n", *cur
);
516 for (node
= OF_child(base
); node
; node
= OF_peer(node
)) {
517 DPRINTF("RECURSE 1 STEP\n");
518 find_ranges(node
, regions
, cur
, type
);
525 find_lowest_range(rangemap_t
*ranges
, int nrof
, int type
)
528 u_int32_t addr
= 0xffffffff;
530 for (i
=0; i
< nrof
; i
++) {
531 if (ranges
[i
].type
== type
&& ranges
[i
].addr
!= 0 &&
532 ranges
[i
].addr
< addr
) {
534 addr
= ranges
[i
].addr
;
537 if (addr
== 0xffffffff)
543 * Find a region of memory, and create a bus_space_tag for it.
545 * For ISA node is ignored.
546 * node is the starting node. if -1, we start at / and map everything.
550 ofwoea_map_space(int rangetype
, int iomem
, int node
,
551 struct powerpc_bus_space
*tag
, const char *name
)
553 int i
, cur
, range
, nrofholes
, error
;
556 rangemap_t region
, holes
[32], list
[32];
558 memset(list
, 0, sizeof(list
));
560 if (rangetype
== RANGE_TYPE_ISA
|| node
== -1)
561 node
= OF_finddevice("/");
562 if (rangetype
== RANGE_TYPE_ISA
) {
564 rangemap_t regions
[32];
566 DPRINTF("LOOKING FOR FIRSTPCI\n");
567 find_ranges(node
, list
, &cur
, RANGE_TYPE_FIRSTPCI
);
569 DPRINTF("LOOKING FOR ISA\n");
570 find_ranges(node
, regions
, &range
, RANGE_TYPE_ISA
);
571 if (range
== 0 || cur
== 0)
572 return -1; /* no isa stuff found */
574 * This may be confusing to some. The ISA ranges property
575 * is supposed to be a set of IO ranges for the ISA bus, but
576 * generally, it's just a set of pci devfunc lists that tell
577 * you to go look at the parent PCI device for the actual
581 /* we found a rangeless isa bus */
583 DPRINTF("found isa stuff\n");
584 for (i
=0; i
< range
; i
++)
585 if (regions
[i
].type
== iomem
)
586 size
= regions
[i
].size
;
587 if (iomem
== RANGE_IO
) {
588 /* the first io range is the one */
589 for (i
=0; i
< cur
; i
++)
590 if (list
[i
].type
== RANGE_IO
&& size
) {
591 DPRINTF("found IO\n");
592 tag
->pbs_offset
= list
[i
].addr
;
593 tag
->pbs_limit
= size
;
594 error
= bus_space_init(tag
, name
, NULL
, 0);
598 for (i
=0; i
< cur
; i
++)
599 if (list
[i
].type
== RANGE_MEM
&&
600 list
[i
].size
== size
) {
601 DPRINTF("found mem\n");
602 tag
->pbs_offset
= list
[i
].addr
;
603 tag
->pbs_limit
= size
;
604 error
= bus_space_init(tag
, name
, NULL
, 0);
608 return -1; /* NO ISA FOUND */
610 find_ranges(node
, list
, &cur
, rangetype
);
612 DPRINTF("cur == %d\n", cur
);
613 /* now list should contain a list of memory regions */
614 for (i
=0; i
< cur
; i
++)
615 DPRINTF("addr=0x%x size=0x%x type=%d\n", list
[i
].addr
,
616 list
[i
].size
, list
[i
].type
);
619 range
= find_lowest_range(list
, cur
, iomem
);
622 while (range
!= -1) {
623 DPRINTF("range==%d\n", range
);
624 DPRINTF("i==%d\n", i
);
626 memcpy(®ion
, &list
[range
], sizeof(rangemap_t
));
627 list
[range
].addr
= 0;
629 range
= find_lowest_range(list
, cur
, iomem
);
632 if (region
.addr
+ region
.size
< list
[range
].addr
) {
633 /* allocate a hole */
634 holes
[nrofholes
].type
= iomem
;
635 holes
[nrofholes
].addr
= region
.size
+ region
.addr
;
636 holes
[nrofholes
].size
= list
[range
].addr
-
637 holes
[nrofholes
].addr
- 1;
640 region
.size
= list
[range
].size
+ list
[range
].addr
-
642 list
[range
].addr
= 0;
643 range
= find_lowest_range(list
, cur
, iomem
);
645 DPRINTF("RANGE iomem=%d FOUND\n", iomem
);
646 DPRINTF("addr=0x%x size=0x%x type=%d\n", region
.addr
,
647 region
.size
, region
.type
);
648 DPRINTF("HOLES FOUND\n");
649 for (i
=0; i
< nrofholes
; i
++)
650 DPRINTF("addr=0x%x size=0x%x type=%d\n", holes
[i
].addr
,
651 holes
[i
].size
, holes
[i
].type
);
652 /* AT THIS POINT WE MAP IT */
654 if (rangetype
== RANGE_TYPE_PCI
) {
655 if (exmap
== EXSTORAGE_MAX
)
656 panic("Not enough ex_storage space. "
657 "Increase EXSTORAGE_MAX");
659 /* XXX doing this in here might be wrong */
661 /* we map an IO region */
662 tag
->pbs_offset
= region
.addr
;
664 tag
->pbs_limit
= region
.size
;
666 /* ... or a memory region */
668 tag
->pbs_base
= region
.addr
;
669 tag
->pbs_limit
= region
.size
+ region
.addr
;
672 error
= bus_space_init(tag
, name
, ex_storage
[exmap
],
673 sizeof(ex_storage
[exmap
]));
676 panic("ofwoea_bus_space_init: can't init tag %s", name
);
677 for (i
=0; i
< nrofholes
; i
++) {
678 if (holes
[i
].type
== RANGE_IO
) {
679 error
= extent_alloc_region(tag
->pbs_extent
,
680 holes
[i
].addr
- tag
->pbs_offset
,
681 holes
[i
].size
, EX_NOWAIT
);
683 error
= extent_alloc_region(tag
->pbs_extent
,
684 holes
[i
].addr
, holes
[i
].size
, EX_NOWAIT
);
687 panic("ofwoea_bus_space_init: can't block out"
688 " reserved space 0x%x-0x%x: error=%d",
689 holes
[i
].addr
, holes
[i
].addr
+holes
[i
].size
,
698 ofwoea_bus_space_init(void)
702 error
= ofwoea_map_space(RANGE_TYPE_ISA
, RANGE_IO
, -1,
703 &genppc_isa_io_space_tag
, "isa-ioport");
705 panic("Could not map ISA IO");
707 error
= ofwoea_map_space(RANGE_TYPE_ISA
, RANGE_MEM
, -1,
708 &genppc_isa_mem_space_tag
, "isa-iomem");
710 panic("Could not map ISA MEM");