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