ofwoea_machdep.c revision 1.1.10.3 1 1.1.10.1 bouyer /* $NetBSD: ofwoea_machdep.c,v 1.1.10.3 2007/11/18 19:34:40 bouyer Exp $ */
2 1.1.10.1 bouyer
3 1.1.10.1 bouyer /*-
4 1.1.10.1 bouyer * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.10.1 bouyer * All rights reserved.
6 1.1.10.1 bouyer *
7 1.1.10.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.10.1 bouyer * by Tim Rightnour
9 1.1.10.1 bouyer *
10 1.1.10.1 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.10.1 bouyer * modification, are permitted provided that the following conditions
12 1.1.10.1 bouyer * are met:
13 1.1.10.1 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.10.1 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.10.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.10.1 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.10.1 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.10.1 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.10.1 bouyer * must display the following acknowledgement:
20 1.1.10.1 bouyer * This product includes software developed by the NetBSD
21 1.1.10.1 bouyer * Foundation, Inc. and its contributors.
22 1.1.10.1 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.10.1 bouyer * contributors may be used to endorse or promote products derived
24 1.1.10.1 bouyer * from this software without specific prior written permission.
25 1.1.10.1 bouyer *
26 1.1.10.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.10.1 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.10.1 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.10.1 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.10.1 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.10.1 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.10.1 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.10.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.10.1 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.10.1 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.10.1 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.1.10.1 bouyer */
38 1.1.10.1 bouyer
39 1.1.10.1 bouyer #include <sys/cdefs.h>
40 1.1.10.1 bouyer __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.1.10.3 2007/11/18 19:34:40 bouyer Exp $");
41 1.1.10.1 bouyer
42 1.1.10.1 bouyer
43 1.1.10.1 bouyer #include "opt_compat_netbsd.h"
44 1.1.10.1 bouyer #include "opt_ddb.h"
45 1.1.10.1 bouyer #include "opt_kgdb.h"
46 1.1.10.1 bouyer #include "opt_ipkdb.h"
47 1.1.10.1 bouyer
48 1.1.10.1 bouyer #include <sys/param.h>
49 1.1.10.1 bouyer #include <sys/buf.h>
50 1.1.10.1 bouyer #include <sys/boot_flag.h>
51 1.1.10.1 bouyer #include <sys/extent.h>
52 1.1.10.1 bouyer #include <sys/kernel.h>
53 1.1.10.1 bouyer #include <sys/ksyms.h>
54 1.1.10.1 bouyer #include <uvm/uvm_extern.h>
55 1.1.10.1 bouyer
56 1.1.10.1 bouyer #include <dev/ofw/openfirm.h>
57 1.1.10.1 bouyer #include <machine/pmap.h>
58 1.1.10.1 bouyer #include <machine/powerpc.h>
59 1.1.10.1 bouyer #include <machine/trap.h>
60 1.1.10.3 bouyer #include <machine/vmparam.h>
61 1.1.10.1 bouyer #include <powerpc/bus.h>
62 1.1.10.1 bouyer #include <powerpc/oea/bat.h>
63 1.1.10.1 bouyer #include <powerpc/ofw_bus.h>
64 1.1.10.1 bouyer #include <powerpc/ofw_cons.h>
65 1.1.10.1 bouyer #include <powerpc/spr.h>
66 1.1.10.1 bouyer #include <arch/powerpc/pic/picvar.h>
67 1.1.10.1 bouyer
68 1.1.10.1 bouyer #include "opt_oea.h"
69 1.1.10.1 bouyer
70 1.1.10.1 bouyer #include "ksyms.h"
71 1.1.10.1 bouyer
72 1.1.10.1 bouyer #ifdef DDB
73 1.1.10.1 bouyer #include <machine/db_machdep.h>
74 1.1.10.1 bouyer #include <ddb/db_extern.h>
75 1.1.10.1 bouyer #endif
76 1.1.10.1 bouyer
77 1.1.10.1 bouyer #ifdef KGDB
78 1.1.10.1 bouyer #include <sys/kgdb.h>
79 1.1.10.1 bouyer #endif
80 1.1.10.1 bouyer
81 1.1.10.1 bouyer #ifdef IPKDB
82 1.1.10.1 bouyer #include <ipkdb/ipkdb.h>
83 1.1.10.1 bouyer #endif
84 1.1.10.1 bouyer
85 1.1.10.1 bouyer #include "opt_ofwoea.h"
86 1.1.10.1 bouyer
87 1.1.10.1 bouyer #ifdef OFWOEA_DEBUG
88 1.1.10.1 bouyer #define DPRINTF printf
89 1.1.10.1 bouyer #else
90 1.1.10.2 bouyer #define DPRINTF while (0) printf
91 1.1.10.1 bouyer #endif
92 1.1.10.1 bouyer
93 1.1.10.1 bouyer typedef struct _rangemap {
94 1.1.10.1 bouyer u_int32_t addr;
95 1.1.10.1 bouyer u_int32_t size;
96 1.1.10.1 bouyer int type;
97 1.1.10.1 bouyer } rangemap_t;
98 1.1.10.1 bouyer
99 1.1.10.1 bouyer struct ofw_translations {
100 1.1.10.1 bouyer vaddr_t va;
101 1.1.10.1 bouyer int len;
102 1.1.10.1 bouyer #if defined (PMAC_G5)
103 1.1.10.1 bouyer register64_t pa;
104 1.1.10.1 bouyer #else
105 1.1.10.1 bouyer register_t pa;
106 1.1.10.1 bouyer #endif
107 1.1.10.1 bouyer int mode;
108 1.1.10.1 bouyer }__attribute__((packed));
109 1.1.10.1 bouyer
110 1.1.10.1 bouyer struct pmap ofw_pmap;
111 1.1.10.1 bouyer struct ofw_translations ofmap[32];
112 1.1.10.1 bouyer char bootpath[256];
113 1.1.10.1 bouyer #if NKSYMS || defined(DDB) || defined(LKM)
114 1.1.10.1 bouyer void *startsym, *endsym;
115 1.1.10.1 bouyer #endif
116 1.1.10.1 bouyer #ifdef TIMEBASE_FREQ
117 1.1.10.1 bouyer u_int timebase_freq = TIMEBASE_FREQ;
118 1.1.10.1 bouyer #else
119 1.1.10.1 bouyer u_int timebase_freq = 0;
120 1.1.10.1 bouyer #endif
121 1.1.10.1 bouyer
122 1.1.10.1 bouyer extern int ofmsr;
123 1.1.10.1 bouyer extern int chosen;
124 1.1.10.1 bouyer extern uint32_t ticks_per_sec;
125 1.1.10.1 bouyer extern uint32_t ns_per_tick;
126 1.1.10.1 bouyer extern uint32_t ticks_per_intr;
127 1.1.10.1 bouyer
128 1.1.10.1 bouyer static int save_ofmap(struct ofw_translations *, int);
129 1.1.10.1 bouyer static void restore_ofmap(struct ofw_translations *, int);
130 1.1.10.1 bouyer static void set_timebase(void);
131 1.1.10.1 bouyer
132 1.1.10.1 bouyer void
133 1.1.10.1 bouyer ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
134 1.1.10.1 bouyer {
135 1.1.10.1 bouyer int ofmaplen;
136 1.1.10.1 bouyer #if defined (PPC_OEA64_BRIDGE)
137 1.1.10.1 bouyer register_t scratch;
138 1.1.10.1 bouyer #endif
139 1.1.10.1 bouyer
140 1.1.10.1 bouyer #if defined (PPC_OEA)
141 1.1.10.1 bouyer /* initialze bats */
142 1.1.10.1 bouyer ofwoea_batinit();
143 1.1.10.1 bouyer #elif defined (PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
144 1.1.10.1 bouyer #endif /* PPC_OEA */
145 1.1.10.1 bouyer
146 1.1.10.1 bouyer #if NKSYMS || defined(DDB) || defined(LKM)
147 1.1.10.1 bouyer /* get info of kernel symbol table from bootloader */
148 1.1.10.1 bouyer memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
149 1.1.10.1 bouyer memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
150 1.1.10.1 bouyer sizeof(endsym));
151 1.1.10.1 bouyer if (startsym == NULL || endsym == NULL)
152 1.1.10.1 bouyer startsym = endsym = NULL;
153 1.1.10.1 bouyer #endif
154 1.1.10.1 bouyer
155 1.1.10.1 bouyer ofwoea_consinit();
156 1.1.10.1 bouyer
157 1.1.10.1 bouyer oea_init(pic_ext_intr);
158 1.1.10.1 bouyer
159 1.1.10.1 bouyer ofmaplen = save_ofmap(NULL, 0);
160 1.1.10.1 bouyer if (ofmaplen > 0)
161 1.1.10.1 bouyer save_ofmap(ofmap, ofmaplen);
162 1.1.10.1 bouyer
163 1.1.10.1 bouyer ofmsr &= ~PSL_IP;
164 1.1.10.1 bouyer
165 1.1.10.1 bouyer /* Parse the args string */
166 1.1.10.1 bouyer if (args) {
167 1.1.10.1 bouyer strcpy(bootpath, args);
168 1.1.10.1 bouyer args = bootpath;
169 1.1.10.1 bouyer while (*++args && *args != ' ');
170 1.1.10.1 bouyer if (*args) {
171 1.1.10.1 bouyer *args++ = 0;
172 1.1.10.1 bouyer while (*args)
173 1.1.10.1 bouyer BOOT_FLAG(*args++, boothowto);
174 1.1.10.1 bouyer }
175 1.1.10.1 bouyer }
176 1.1.10.1 bouyer
177 1.1.10.1 bouyer uvm_setpagesize();
178 1.1.10.1 bouyer pmap_bootstrap(startkernel, endkernel);
179 1.1.10.1 bouyer
180 1.1.10.1 bouyer #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
181 1.1.10.1 bouyer #if defined (PMAC_G5)
182 1.1.10.1 bouyer /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
183 1.1.10.1 bouyer pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000, 0x0);
184 1.1.10.1 bouyer #elif defined (MAMBO)
185 1.1.10.1 bouyer /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
186 1.1.10.1 bouyer pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
187 1.1.10.1 bouyer #endif /* PMAC_G5 */
188 1.1.10.1 bouyer
189 1.1.10.1 bouyer /* Now enable translation (and machine checks/recoverable interrupts) */
190 1.1.10.1 bouyer __asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
191 1.1.10.1 bouyer : "=r"(scratch)
192 1.1.10.1 bouyer : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
193 1.1.10.1 bouyer #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
194 1.1.10.1 bouyer
195 1.1.10.1 bouyer restore_ofmap(ofmap, ofmaplen);
196 1.1.10.1 bouyer
197 1.1.10.1 bouyer #if NKSYMS || defined(DDB) || defined(LKM)
198 1.1.10.1 bouyer ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
199 1.1.10.1 bouyer #endif
200 1.1.10.1 bouyer
201 1.1.10.1 bouyer /* CPU clock stuff */
202 1.1.10.1 bouyer set_timebase();
203 1.1.10.1 bouyer }
204 1.1.10.1 bouyer
205 1.1.10.1 bouyer void
206 1.1.10.1 bouyer set_timebase(void)
207 1.1.10.1 bouyer {
208 1.1.10.1 bouyer int qhandle, phandle, msr, scratch;
209 1.1.10.1 bouyer char type[32];
210 1.1.10.1 bouyer
211 1.1.10.1 bouyer if (timebase_freq != 0) {
212 1.1.10.1 bouyer ticks_per_sec = timebase_freq;
213 1.1.10.1 bouyer goto found;
214 1.1.10.1 bouyer }
215 1.1.10.1 bouyer
216 1.1.10.1 bouyer for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
217 1.1.10.1 bouyer if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
218 1.1.10.1 bouyer && strcmp(type, "cpu") == 0
219 1.1.10.1 bouyer && OF_getprop(qhandle, "timebase-frequency",
220 1.1.10.1 bouyer &ticks_per_sec, sizeof ticks_per_sec) > 0) {
221 1.1.10.1 bouyer goto found;
222 1.1.10.1 bouyer }
223 1.1.10.1 bouyer if ((phandle = OF_child(qhandle)))
224 1.1.10.1 bouyer continue;
225 1.1.10.1 bouyer while (qhandle) {
226 1.1.10.1 bouyer if ((phandle = OF_peer(qhandle)))
227 1.1.10.1 bouyer break;
228 1.1.10.1 bouyer qhandle = OF_parent(qhandle);
229 1.1.10.1 bouyer }
230 1.1.10.1 bouyer }
231 1.1.10.1 bouyer panic("no cpu node");
232 1.1.10.1 bouyer
233 1.1.10.1 bouyer found:
234 1.1.10.1 bouyer __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
235 1.1.10.1 bouyer : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
236 1.1.10.1 bouyer ns_per_tick = 1000000000 / ticks_per_sec;
237 1.1.10.1 bouyer ticks_per_intr = ticks_per_sec / hz;
238 1.1.10.1 bouyer cpu_timebase = ticks_per_sec;
239 1.1.10.1 bouyer curcpu()->ci_lasttb = mftbl();
240 1.1.10.1 bouyer mtspr(SPR_DEC, ticks_per_intr);
241 1.1.10.1 bouyer mtmsr(msr);
242 1.1.10.1 bouyer }
243 1.1.10.1 bouyer
244 1.1.10.1 bouyer static int
245 1.1.10.1 bouyer save_ofmap(struct ofw_translations *map, int maxlen)
246 1.1.10.1 bouyer {
247 1.1.10.1 bouyer int mmui, mmu, len;
248 1.1.10.1 bouyer
249 1.1.10.1 bouyer OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
250 1.1.10.1 bouyer mmu = OF_instance_to_package(mmui);
251 1.1.10.1 bouyer
252 1.1.10.1 bouyer if (map) {
253 1.1.10.1 bouyer memset(map, 0, maxlen); /* to be safe */
254 1.1.10.1 bouyer len = OF_getprop(mmu, "translations", map, maxlen);
255 1.1.10.1 bouyer } else
256 1.1.10.1 bouyer len = OF_getproplen(mmu, "translations");
257 1.1.10.1 bouyer
258 1.1.10.1 bouyer if (len < 0)
259 1.1.10.1 bouyer len = 0;
260 1.1.10.1 bouyer return len;
261 1.1.10.1 bouyer }
262 1.1.10.1 bouyer
263 1.1.10.1 bouyer void
264 1.1.10.1 bouyer restore_ofmap(struct ofw_translations *map, int len)
265 1.1.10.1 bouyer {
266 1.1.10.1 bouyer int n = len / sizeof(struct ofw_translations);
267 1.1.10.1 bouyer int i;
268 1.1.10.1 bouyer
269 1.1.10.1 bouyer pmap_pinit(&ofw_pmap);
270 1.1.10.1 bouyer
271 1.1.10.1 bouyer #if defined(PPC_OEA64_BRIDGE)
272 1.1.10.1 bouyer ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
273 1.1.10.1 bouyer #endif
274 1.1.10.1 bouyer ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
275 1.1.10.1 bouyer
276 1.1.10.1 bouyer #ifdef KERNEL2_SR
277 1.1.10.1 bouyer ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
278 1.1.10.1 bouyer #endif
279 1.1.10.1 bouyer
280 1.1.10.1 bouyer for (i = 0; i < n; i++) {
281 1.1.10.1 bouyer #if defined (PMAC_G5)
282 1.1.10.1 bouyer register64_t pa = map[i].pa;
283 1.1.10.1 bouyer #else
284 1.1.10.1 bouyer register_t pa = map[i].pa;
285 1.1.10.1 bouyer #endif
286 1.1.10.1 bouyer vaddr_t va = map[i].va;
287 1.1.10.1 bouyer size_t length = map[i].len;
288 1.1.10.1 bouyer
289 1.1.10.1 bouyer if (va < 0xf0000000) /* XXX */
290 1.1.10.1 bouyer continue;
291 1.1.10.1 bouyer
292 1.1.10.1 bouyer while (length > 0) {
293 1.1.10.1 bouyer pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
294 1.1.10.1 bouyer VM_PROT_ALL|PMAP_WIRED);
295 1.1.10.1 bouyer pa += PAGE_SIZE;
296 1.1.10.1 bouyer va += PAGE_SIZE;
297 1.1.10.1 bouyer length -= PAGE_SIZE;
298 1.1.10.1 bouyer }
299 1.1.10.1 bouyer }
300 1.1.10.1 bouyer pmap_update(&ofw_pmap);
301 1.1.10.1 bouyer }
302 1.1.10.1 bouyer
303 1.1.10.1 bouyer
304 1.1.10.1 bouyer
305 1.1.10.1 bouyer /*
306 1.1.10.1 bouyer * Scan the device tree for ranges, and batmap them.
307 1.1.10.1 bouyer */
308 1.1.10.1 bouyer
309 1.1.10.1 bouyer static u_int16_t
310 1.1.10.1 bouyer ranges_bitmap(int node, u_int16_t bitmap)
311 1.1.10.1 bouyer {
312 1.1.10.2 bouyer int child, mlen, acells, scells, reclen, i, j;
313 1.1.10.2 bouyer u_int32_t addr, len, map[160];
314 1.1.10.1 bouyer
315 1.1.10.1 bouyer for (child = OF_child(node); child; child = OF_peer(child)) {
316 1.1.10.1 bouyer mlen = OF_getprop(child, "ranges", map, sizeof(map));
317 1.1.10.1 bouyer if (mlen == -1)
318 1.1.10.1 bouyer goto noranges;
319 1.1.10.2 bouyer
320 1.1.10.2 bouyer j = OF_getprop(child, "#address-cells", &acells,
321 1.1.10.1 bouyer sizeof(acells));
322 1.1.10.2 bouyer if (j == -1)
323 1.1.10.1 bouyer goto noranges;
324 1.1.10.2 bouyer
325 1.1.10.2 bouyer j = OF_getprop(child, "#size-cells", &scells,
326 1.1.10.1 bouyer sizeof(scells));
327 1.1.10.2 bouyer if (j == -1)
328 1.1.10.1 bouyer goto noranges;
329 1.1.10.1 bouyer
330 1.1.10.1 bouyer reclen = acells + 1 + scells;
331 1.1.10.1 bouyer
332 1.1.10.2 bouyer for (i=0; i < (mlen/4)/reclen; i++) {
333 1.1.10.2 bouyer addr = map[reclen * i + acells];
334 1.1.10.1 bouyer len = map[reclen * i + reclen - 1];
335 1.1.10.1 bouyer for (j = 0; j < len / 0x10000000; j++)
336 1.1.10.1 bouyer bitmap |= 1 << ((addr+j*0x10000000) >>28);
337 1.1.10.1 bouyer bitmap |= 1 << (addr >> 28);
338 1.1.10.1 bouyer }
339 1.1.10.1 bouyer noranges:
340 1.1.10.1 bouyer bitmap |= ranges_bitmap(child, bitmap);
341 1.1.10.1 bouyer continue;
342 1.1.10.1 bouyer }
343 1.1.10.1 bouyer return bitmap;
344 1.1.10.1 bouyer }
345 1.1.10.1 bouyer
346 1.1.10.1 bouyer void
347 1.1.10.1 bouyer ofwoea_batinit(void)
348 1.1.10.1 bouyer {
349 1.1.10.1 bouyer #if 0
350 1.1.10.1 bouyer /* this is what macppc used to do */
351 1.1.10.1 bouyer oea_batinit(0x80000000, BAT_BL_256M, 0xf0000000, BAT_BL_256M,
352 1.1.10.1 bouyer 0x90000000, BAT_BL_256M, 0xa0000000, BAT_BL_256M,
353 1.1.10.1 bouyer 0xb0000000, BAT_BL_256M, 0);
354 1.1.10.1 bouyer #else
355 1.1.10.1 bouyer u_int16_t bitmap;
356 1.1.10.1 bouyer int node, i;
357 1.1.10.1 bouyer
358 1.1.10.1 bouyer node = OF_finddevice("/");
359 1.1.10.1 bouyer bitmap = ranges_bitmap(node, 0);
360 1.1.10.1 bouyer oea_batinit(0);
361 1.1.10.1 bouyer
362 1.1.10.2 bouyer #ifdef macppc
363 1.1.10.1 bouyer /* XXX this is a macppc-specific hack */
364 1.1.10.1 bouyer bitmap = 0x8f00;
365 1.1.10.2 bouyer #endif
366 1.1.10.3 bouyer for (i=1; i < 0x10; i++) {
367 1.1.10.3 bouyer /* skip the three vital SR regions */
368 1.1.10.3 bouyer if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
369 1.1.10.3 bouyer continue;
370 1.1.10.2 bouyer if (bitmap & (1 << i)) {
371 1.1.10.1 bouyer oea_iobat_add(0x10000000 * i, BAT_BL_256M);
372 1.1.10.2 bouyer DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
373 1.1.10.2 bouyer }
374 1.1.10.3 bouyer }
375 1.1.10.1 bouyer #endif
376 1.1.10.1 bouyer }
377 1.1.10.1 bouyer
378 1.1.10.1 bouyer
379 1.1.10.1 bouyer /* we define these partially, as we will fill the rest in later */
380 1.1.10.1 bouyer struct powerpc_bus_space genppc_isa_io_space_tag = {
381 1.1.10.1 bouyer .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
382 1.1.10.1 bouyer .pbs_base = 0x00000000,
383 1.1.10.1 bouyer };
384 1.1.10.1 bouyer
385 1.1.10.1 bouyer struct powerpc_bus_space genppc_isa_mem_space_tag = {
386 1.1.10.1 bouyer .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
387 1.1.10.1 bouyer .pbs_base = 0x00000000,
388 1.1.10.1 bouyer };
389 1.1.10.1 bouyer
390 1.1.10.1 bouyer /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
391 1.1.10.1 bouyer * Increase if necc.
392 1.1.10.1 bouyer */
393 1.1.10.1 bouyer static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
394 1.1.10.1 bouyer __attribute__((aligned(8)));
395 1.1.10.1 bouyer
396 1.1.10.1 bouyer
397 1.1.10.1 bouyer static void
398 1.1.10.1 bouyer find_ranges(int base, rangemap_t *regions, int *cur, int type)
399 1.1.10.1 bouyer {
400 1.1.10.1 bouyer int node, i, len, reclen;
401 1.1.10.1 bouyer u_int32_t acells, scells, map[160];
402 1.1.10.1 bouyer char tmp[32];
403 1.1.10.1 bouyer
404 1.1.10.1 bouyer node = base;
405 1.1.10.1 bouyer if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
406 1.1.10.1 bouyer goto rec;
407 1.1.10.1 bouyer if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
408 1.1.10.1 bouyer strcmp("pci", tmp) != 0)
409 1.1.10.1 bouyer goto rec;
410 1.1.10.1 bouyer if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
411 1.1.10.1 bouyer goto rec;
412 1.1.10.1 bouyer len = OF_getprop(node, "ranges", map, sizeof(map));
413 1.1.10.1 bouyer if (len == -1)
414 1.1.10.1 bouyer goto rec;
415 1.1.10.1 bouyer if (OF_getprop(node, "#address-cells", &acells,
416 1.1.10.1 bouyer sizeof(acells)) != sizeof(acells))
417 1.1.10.1 bouyer acells = 1;
418 1.1.10.1 bouyer if (OF_getprop(node, "#size-cells", &scells,
419 1.1.10.1 bouyer sizeof(scells)) != sizeof(scells))
420 1.1.10.1 bouyer scells = 1;
421 1.1.10.1 bouyer if (type == RANGE_TYPE_ISA)
422 1.1.10.1 bouyer reclen = 6;
423 1.1.10.1 bouyer else
424 1.1.10.1 bouyer reclen = acells + scells + 1;
425 1.1.10.2 bouyer /*
426 1.1.10.2 bouyer * There exist ISA buses with empty ranges properties. This is
427 1.1.10.2 bouyer * known to occur on the Pegasos II machine, and likely others.
428 1.1.10.2 bouyer * According to them, that means that the isa bus is a fake bus, and
429 1.1.10.2 bouyer * the real maps are the PCI maps of the preceeding bus. To deal
430 1.1.10.2 bouyer * with this, we will set cur to -1 and return.
431 1.1.10.2 bouyer */
432 1.1.10.2 bouyer if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
433 1.1.10.2 bouyer *cur = -1;
434 1.1.10.2 bouyer DPRINTF("Found empty range in isa bus\n");
435 1.1.10.2 bouyer return;
436 1.1.10.2 bouyer }
437 1.1.10.2 bouyer
438 1.1.10.2 bouyer DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
439 1.1.10.1 bouyer switch (type) {
440 1.1.10.1 bouyer case RANGE_TYPE_PCI:
441 1.1.10.1 bouyer case RANGE_TYPE_FIRSTPCI:
442 1.1.10.1 bouyer for (i=0; i < len/(4*reclen); i++) {
443 1.1.10.2 bouyer DPRINTF("FOUND PCI RANGE\n");
444 1.1.10.1 bouyer regions[*cur].type = map[i*reclen] >> 24;
445 1.1.10.1 bouyer regions[*cur].addr = map[i*reclen + acells];
446 1.1.10.1 bouyer regions[*cur].size =
447 1.1.10.1 bouyer map[i*reclen + acells + scells];
448 1.1.10.1 bouyer (*cur)++;
449 1.1.10.1 bouyer }
450 1.1.10.1 bouyer break;
451 1.1.10.1 bouyer case RANGE_TYPE_ISA:
452 1.1.10.1 bouyer for (i=0; i < len/(4*reclen); i++) {
453 1.1.10.1 bouyer if (map[i*reclen] == 1)
454 1.1.10.1 bouyer regions[*cur].type = RANGE_IO;
455 1.1.10.1 bouyer else
456 1.1.10.1 bouyer regions[*cur].type = RANGE_MEM;
457 1.1.10.2 bouyer DPRINTF("FOUND ISA RANGE TYPE=%d\n",
458 1.1.10.2 bouyer regions[*cur].type);
459 1.1.10.1 bouyer regions[*cur].size =
460 1.1.10.1 bouyer map[i*reclen + acells + scells];
461 1.1.10.1 bouyer (*cur)++;
462 1.1.10.1 bouyer }
463 1.1.10.1 bouyer break;
464 1.1.10.1 bouyer }
465 1.1.10.2 bouyer DPRINTF("returning with CUR=%d\n", *cur);
466 1.1.10.1 bouyer return;
467 1.1.10.1 bouyer rec:
468 1.1.10.2 bouyer for (node = OF_child(base); node; node = OF_peer(node)) {
469 1.1.10.2 bouyer DPRINTF("RECURSE 1 STEP\n");
470 1.1.10.1 bouyer find_ranges(node, regions, cur, type);
471 1.1.10.2 bouyer if (*cur == -1)
472 1.1.10.2 bouyer return;
473 1.1.10.2 bouyer }
474 1.1.10.1 bouyer }
475 1.1.10.1 bouyer
476 1.1.10.1 bouyer static int
477 1.1.10.1 bouyer find_lowest_range(rangemap_t *ranges, int nrof, int type)
478 1.1.10.1 bouyer {
479 1.1.10.1 bouyer int i, low = 0;
480 1.1.10.1 bouyer u_int32_t addr = 0xffffffff;
481 1.1.10.1 bouyer
482 1.1.10.1 bouyer for (i=0; i < nrof; i++) {
483 1.1.10.1 bouyer if (ranges[i].type == type && ranges[i].addr != 0 &&
484 1.1.10.1 bouyer ranges[i].addr < addr) {
485 1.1.10.1 bouyer low = i;
486 1.1.10.1 bouyer addr = ranges[i].addr;
487 1.1.10.1 bouyer }
488 1.1.10.1 bouyer }
489 1.1.10.1 bouyer if (addr == 0xffffffff)
490 1.1.10.1 bouyer return -1;
491 1.1.10.1 bouyer return low;
492 1.1.10.1 bouyer }
493 1.1.10.1 bouyer
494 1.1.10.1 bouyer /*
495 1.1.10.1 bouyer * Find a region of memory, and create a bus_space_tag for it.
496 1.1.10.1 bouyer * Notes:
497 1.1.10.1 bouyer * For ISA node is ignored.
498 1.1.10.1 bouyer * node is the starting node. if -1, we start at / and map everything.
499 1.1.10.1 bouyer */
500 1.1.10.1 bouyer
501 1.1.10.1 bouyer int
502 1.1.10.1 bouyer ofwoea_map_space(int rangetype, int iomem, int node,
503 1.1.10.1 bouyer struct powerpc_bus_space *tag, const char *name)
504 1.1.10.1 bouyer {
505 1.1.10.1 bouyer int i, cur, range, nrofholes, error;
506 1.1.10.1 bouyer static int exmap=0;
507 1.1.10.1 bouyer u_int32_t addr;
508 1.1.10.1 bouyer rangemap_t region, holes[32], list[32];
509 1.1.10.1 bouyer
510 1.1.10.1 bouyer memset(list, 0, sizeof(list));
511 1.1.10.1 bouyer cur = 0;
512 1.1.10.1 bouyer if (rangetype == RANGE_TYPE_ISA || node == -1)
513 1.1.10.1 bouyer node = OF_finddevice("/");
514 1.1.10.1 bouyer if (rangetype == RANGE_TYPE_ISA) {
515 1.1.10.1 bouyer u_int32_t size = 0;
516 1.1.10.1 bouyer rangemap_t regions[32];
517 1.1.10.1 bouyer
518 1.1.10.2 bouyer DPRINTF("LOOKING FOR FIRSTPCI\n");
519 1.1.10.1 bouyer find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
520 1.1.10.1 bouyer range = 0;
521 1.1.10.2 bouyer DPRINTF("LOOKING FOR ISA\n");
522 1.1.10.1 bouyer find_ranges(node, regions, &range, RANGE_TYPE_ISA);
523 1.1.10.1 bouyer if (range == 0 || cur == 0)
524 1.1.10.1 bouyer return -1; /* no isa stuff found */
525 1.1.10.2 bouyer /*
526 1.1.10.2 bouyer * This may be confusing to some. The ISA ranges property
527 1.1.10.2 bouyer * is supposed to be a set of IO ranges for the ISA bus, but
528 1.1.10.2 bouyer * generally, it's just a set of pci devfunc lists that tell
529 1.1.10.2 bouyer * you to go look at the parent PCI device for the actual
530 1.1.10.2 bouyer * ranges.
531 1.1.10.2 bouyer */
532 1.1.10.2 bouyer if (range == -1) {
533 1.1.10.2 bouyer /* we found a rangeless isa bus */
534 1.1.10.2 bouyer }
535 1.1.10.2 bouyer DPRINTF("found isa stuff\n");
536 1.1.10.1 bouyer for (i=0; i < range; i++)
537 1.1.10.1 bouyer if (regions[i].type == iomem)
538 1.1.10.1 bouyer size = regions[i].size;
539 1.1.10.1 bouyer if (iomem == RANGE_IO) {
540 1.1.10.1 bouyer /* the first io range is the one */
541 1.1.10.1 bouyer for (i=0; i < cur; i++)
542 1.1.10.1 bouyer if (list[i].type == RANGE_IO && size) {
543 1.1.10.2 bouyer DPRINTF("found IO\n");
544 1.1.10.1 bouyer tag->pbs_offset = list[i].addr;
545 1.1.10.1 bouyer tag->pbs_limit = size;
546 1.1.10.1 bouyer error = bus_space_init(tag, name, NULL, 0);
547 1.1.10.1 bouyer return error;
548 1.1.10.1 bouyer }
549 1.1.10.1 bouyer } else {
550 1.1.10.1 bouyer for (i=0; i < cur; i++)
551 1.1.10.1 bouyer if (list[i].type == RANGE_MEM &&
552 1.1.10.1 bouyer list[i].size == size) {
553 1.1.10.2 bouyer DPRINTF("found mem\n");
554 1.1.10.1 bouyer tag->pbs_offset = list[i].addr;
555 1.1.10.1 bouyer tag->pbs_limit = size;
556 1.1.10.1 bouyer error = bus_space_init(tag, name, NULL, 0);
557 1.1.10.1 bouyer return error;
558 1.1.10.1 bouyer }
559 1.1.10.1 bouyer }
560 1.1.10.1 bouyer return -1; /* NO ISA FOUND */
561 1.1.10.1 bouyer }
562 1.1.10.1 bouyer find_ranges(node, list, &cur, rangetype);
563 1.1.10.1 bouyer
564 1.1.10.1 bouyer DPRINTF("cur == %d\n", cur);
565 1.1.10.1 bouyer /* now list should contain a list of memory regions */
566 1.1.10.1 bouyer for (i=0; i < cur; i++)
567 1.1.10.1 bouyer DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
568 1.1.10.1 bouyer list[i].size, list[i].type);
569 1.1.10.1 bouyer
570 1.1.10.1 bouyer addr=0;
571 1.1.10.1 bouyer range = find_lowest_range(list, cur, iomem);
572 1.1.10.1 bouyer i = 0;
573 1.1.10.1 bouyer nrofholes = 0;
574 1.1.10.1 bouyer while (range != -1) {
575 1.1.10.1 bouyer DPRINTF("range==%d\n", range);
576 1.1.10.1 bouyer DPRINTF("i==%d\n", i);
577 1.1.10.1 bouyer if (i == 0) {
578 1.1.10.1 bouyer memcpy(®ion, &list[range], sizeof(rangemap_t));
579 1.1.10.1 bouyer list[range].addr = 0;
580 1.1.10.1 bouyer i++;
581 1.1.10.1 bouyer range = find_lowest_range(list, cur, iomem);
582 1.1.10.1 bouyer continue;
583 1.1.10.1 bouyer }
584 1.1.10.1 bouyer if (region.addr + region.size < list[range].addr) {
585 1.1.10.1 bouyer /* allocate a hole */
586 1.1.10.1 bouyer holes[nrofholes].addr = region.size + region.addr;
587 1.1.10.1 bouyer holes[nrofholes].size = list[range].addr -
588 1.1.10.1 bouyer holes[nrofholes].addr - 1;
589 1.1.10.1 bouyer nrofholes++;
590 1.1.10.1 bouyer }
591 1.1.10.1 bouyer region.size = list[range].size + list[range].addr -
592 1.1.10.1 bouyer region.addr;
593 1.1.10.1 bouyer list[range].addr = 0;
594 1.1.10.1 bouyer range = find_lowest_range(list, cur, iomem);
595 1.1.10.1 bouyer }
596 1.1.10.1 bouyer DPRINTF("RANGE iomem=%d FOUND\n", iomem);
597 1.1.10.1 bouyer DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
598 1.1.10.1 bouyer region.size, region.type);
599 1.1.10.1 bouyer DPRINTF("HOLES FOUND\n");
600 1.1.10.1 bouyer for (i=0; i < nrofholes; i++)
601 1.1.10.1 bouyer DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
602 1.1.10.1 bouyer holes[i].size, holes[i].type);
603 1.1.10.1 bouyer /* AT THIS POINT WE MAP IT */
604 1.1.10.1 bouyer
605 1.1.10.1 bouyer if (rangetype == RANGE_TYPE_PCI) {
606 1.1.10.1 bouyer if (exmap == EXSTORAGE_MAX)
607 1.1.10.1 bouyer panic("Not enough ex_storage space. "
608 1.1.10.1 bouyer "Increase EXSTORAGE_MAX");
609 1.1.10.1 bouyer
610 1.1.10.1 bouyer /* XXX doing this in here might be wrong */
611 1.1.10.1 bouyer if (iomem == 1) {
612 1.1.10.1 bouyer /* we map an IO region */
613 1.1.10.1 bouyer tag->pbs_offset = region.addr;
614 1.1.10.1 bouyer tag->pbs_base = 0;
615 1.1.10.1 bouyer tag->pbs_limit = region.size;
616 1.1.10.1 bouyer } else {
617 1.1.10.1 bouyer /* ... or a memory region */
618 1.1.10.1 bouyer tag->pbs_offset = 0;
619 1.1.10.1 bouyer tag->pbs_base = region.addr;
620 1.1.10.1 bouyer tag->pbs_limit = region.size + region.addr;
621 1.1.10.1 bouyer }
622 1.1.10.1 bouyer
623 1.1.10.1 bouyer error = bus_space_init(tag, name, ex_storage[exmap],
624 1.1.10.1 bouyer sizeof(ex_storage[exmap]));
625 1.1.10.1 bouyer exmap++;
626 1.1.10.1 bouyer if (error)
627 1.1.10.1 bouyer panic("ofwoea_bus_space_init: can't init tag %s", name);
628 1.1.10.1 bouyer for (i=0; i < nrofholes; i++) {
629 1.1.10.1 bouyer error =
630 1.1.10.1 bouyer extent_alloc_region(tag->pbs_extent,
631 1.1.10.1 bouyer holes[i].addr, holes[i].size, EX_NOWAIT);
632 1.1.10.1 bouyer if (error)
633 1.1.10.1 bouyer panic("ofwoea_bus_space_init: can't block out"
634 1.1.10.1 bouyer " reserved space 0x%x-0x%x: error=%d",
635 1.1.10.1 bouyer holes[i].addr, holes[i].addr+holes[i].size,
636 1.1.10.1 bouyer error);
637 1.1.10.1 bouyer }
638 1.1.10.1 bouyer return error;
639 1.1.10.1 bouyer }
640 1.1.10.1 bouyer return -1;
641 1.1.10.1 bouyer }
642 1.1.10.1 bouyer
643 1.1.10.1 bouyer void
644 1.1.10.1 bouyer ofwoea_bus_space_init(void)
645 1.1.10.1 bouyer {
646 1.1.10.1 bouyer int error;
647 1.1.10.1 bouyer
648 1.1.10.1 bouyer error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
649 1.1.10.1 bouyer &genppc_isa_io_space_tag, "isa-ioport");
650 1.1.10.1 bouyer if (error > 0)
651 1.1.10.1 bouyer panic("Could not map ISA IO");
652 1.1.10.2 bouyer
653 1.1.10.1 bouyer error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
654 1.1.10.1 bouyer &genppc_isa_mem_space_tag, "isa-iomem");
655 1.1.10.1 bouyer if (error > 0)
656 1.1.10.1 bouyer panic("Could not map ISA MEM");
657 1.1.10.1 bouyer }
658