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