ofwoea_machdep.c revision 1.55 1 1.55 thorpej /* $NetBSD: ofwoea_machdep.c,v 1.55 2021/02/27 01:22:18 thorpej Exp $ */
2 1.2 garbled
3 1.2 garbled /*-
4 1.2 garbled * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.2 garbled * All rights reserved.
6 1.2 garbled *
7 1.2 garbled * This code is derived from software contributed to The NetBSD Foundation
8 1.2 garbled * by Tim Rightnour
9 1.2 garbled *
10 1.2 garbled * Redistribution and use in source and binary forms, with or without
11 1.2 garbled * modification, are permitted provided that the following conditions
12 1.2 garbled * are met:
13 1.2 garbled * 1. Redistributions of source code must retain the above copyright
14 1.2 garbled * notice, this list of conditions and the following disclaimer.
15 1.2 garbled * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 garbled * notice, this list of conditions and the following disclaimer in the
17 1.2 garbled * documentation and/or other materials provided with the distribution.
18 1.2 garbled *
19 1.2 garbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 garbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 garbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 garbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 garbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 garbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 garbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 garbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 garbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 garbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 garbled * POSSIBILITY OF SUCH DAMAGE.
30 1.2 garbled */
31 1.2 garbled
32 1.2 garbled #include <sys/cdefs.h>
33 1.55 thorpej __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.55 2021/02/27 01:22:18 thorpej Exp $");
34 1.2 garbled
35 1.48 rin #include "ksyms.h"
36 1.48 rin #include "wsdisplay.h"
37 1.48 rin
38 1.48 rin #ifdef _KERNEL_OPT
39 1.48 rin #include "opt_ddb.h"
40 1.2 garbled #include "opt_kgdb.h"
41 1.17 apb #include "opt_modular.h"
42 1.49 rin #include "opt_multiprocessor.h"
43 1.48 rin #include "opt_oea.h"
44 1.48 rin #include "opt_ofwoea.h"
45 1.48 rin #include "opt_ppcarch.h"
46 1.48 rin #endif
47 1.32 macallan
48 1.2 garbled #include <sys/param.h>
49 1.2 garbled #include <sys/buf.h>
50 1.2 garbled #include <sys/boot_flag.h>
51 1.2 garbled #include <sys/extent.h>
52 1.2 garbled #include <sys/kernel.h>
53 1.2 garbled #include <sys/ksyms.h>
54 1.2 garbled #include <uvm/uvm_extern.h>
55 1.2 garbled
56 1.2 garbled #include <dev/ofw/openfirm.h>
57 1.32 macallan #include <dev/wscons/wsconsio.h>
58 1.32 macallan #include <dev/wscons/wsdisplayvar.h>
59 1.32 macallan #include <dev/rasops/rasops.h>
60 1.32 macallan #include <dev/wscons/wsdisplay_vconsvar.h>
61 1.2 garbled #include <machine/pmap.h>
62 1.2 garbled #include <machine/powerpc.h>
63 1.2 garbled #include <machine/trap.h>
64 1.5 garbled #include <machine/vmparam.h>
65 1.6 garbled #include <machine/autoconf.h>
66 1.24 dyoung #include <sys/bus.h>
67 1.2 garbled #include <powerpc/oea/bat.h>
68 1.32 macallan #include <powerpc/oea/ofw_rasconsvar.h>
69 1.10 garbled #include <powerpc/oea/cpufeat.h>
70 1.37 macallan #include <powerpc/include/oea/spr.h>
71 1.2 garbled #include <powerpc/ofw_cons.h>
72 1.52 thorpej #include <powerpc/ofw_machdep.h>
73 1.2 garbled #include <powerpc/spr.h>
74 1.22 matt #include <powerpc/pic/picvar.h>
75 1.2 garbled
76 1.2 garbled #ifdef DDB
77 1.2 garbled #include <machine/db_machdep.h>
78 1.2 garbled #include <ddb/db_extern.h>
79 1.2 garbled #endif
80 1.2 garbled
81 1.2 garbled #ifdef KGDB
82 1.2 garbled #include <sys/kgdb.h>
83 1.2 garbled #endif
84 1.2 garbled
85 1.11 garbled #ifdef ofppc
86 1.11 garbled extern struct model_data modeldata;
87 1.11 garbled #endif
88 1.11 garbled
89 1.2 garbled #ifdef OFWOEA_DEBUG
90 1.2 garbled #define DPRINTF printf
91 1.2 garbled #else
92 1.3 garbled #define DPRINTF while (0) printf
93 1.2 garbled #endif
94 1.2 garbled
95 1.2 garbled typedef struct _rangemap {
96 1.2 garbled u_int32_t addr;
97 1.2 garbled u_int32_t size;
98 1.2 garbled int type;
99 1.2 garbled } rangemap_t;
100 1.2 garbled
101 1.52 thorpej struct OF_translation ofw_translations[OFW_MAX_TRANSLATIONS];
102 1.2 garbled
103 1.54 thorpej /*
104 1.54 thorpej * Data structures holding OpenFirmware's translations when running
105 1.54 thorpej * in virtual-mode.
106 1.54 thorpej *
107 1.54 thorpej * When we call into OpenFirmware, we point the calling CPU's
108 1.54 thorpej * cpu_info::ci_battable at ofw_battable[]. For now, this table
109 1.54 thorpej * is empty, which will ensure that any DSI exceptions that occur
110 1.54 thorpej * during the firmware call will not erroneously load kernel BAT
111 1.54 thorpej * mappings that could clobber the firmware's translations.
112 1.54 thorpej */
113 1.2 garbled struct pmap ofw_pmap;
114 1.54 thorpej struct bat ofw_battable[BAT_VA2IDX(0xffffffff)+1];
115 1.54 thorpej
116 1.2 garbled char bootpath[256];
117 1.6 garbled char model_name[64];
118 1.15 ad #if NKSYMS || defined(DDB) || defined(MODULAR)
119 1.2 garbled void *startsym, *endsym;
120 1.2 garbled #endif
121 1.37 macallan
122 1.37 macallan #if PPC_OEA601
123 1.37 macallan #define TIMEBASE_FREQ (1000000000) /* RTC register */
124 1.37 macallan #endif
125 1.37 macallan
126 1.2 garbled #ifdef TIMEBASE_FREQ
127 1.2 garbled u_int timebase_freq = TIMEBASE_FREQ;
128 1.2 garbled #else
129 1.2 garbled u_int timebase_freq = 0;
130 1.2 garbled #endif
131 1.2 garbled
132 1.50 rin int ofw_quiesce;
133 1.50 rin
134 1.25 macallan extern int ofwmsr;
135 1.2 garbled extern uint32_t ticks_per_sec;
136 1.2 garbled extern uint32_t ns_per_tick;
137 1.2 garbled extern uint32_t ticks_per_intr;
138 1.2 garbled
139 1.52 thorpej static void restore_ofmap(void);
140 1.2 garbled static void set_timebase(void);
141 1.2 garbled
142 1.13 garbled extern void cpu_spinstart(u_int);
143 1.31 kiyohara extern volatile u_int cpu_spinstart_ack;
144 1.13 garbled
145 1.2 garbled void
146 1.2 garbled ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
147 1.2 garbled {
148 1.52 thorpej int node, l;
149 1.2 garbled register_t scratch;
150 1.2 garbled
151 1.13 garbled #if defined(MULTIPROCESSOR) && defined(ofppc)
152 1.13 garbled char cpupath[32];
153 1.13 garbled int i;
154 1.13 garbled #endif
155 1.13 garbled
156 1.2 garbled /* initialze bats */
157 1.10 garbled if ((oeacpufeat & OEACPU_NOBAT) == 0)
158 1.10 garbled ofwoea_batinit();
159 1.2 garbled
160 1.15 ad #if NKSYMS || defined(DDB) || defined(MODULAR)
161 1.2 garbled /* get info of kernel symbol table from bootloader */
162 1.2 garbled memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
163 1.2 garbled memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
164 1.2 garbled sizeof(endsym));
165 1.2 garbled if (startsym == NULL || endsym == NULL)
166 1.2 garbled startsym = endsym = NULL;
167 1.2 garbled #endif
168 1.2 garbled
169 1.6 garbled /* get model name and perform model-specific actions */
170 1.6 garbled memset(model_name, 0, sizeof(model_name));
171 1.6 garbled node = OF_finddevice("/");
172 1.30 jmmv if (node != -1) {
173 1.11 garbled l = OF_getprop(node, "model", model_name, sizeof(model_name));
174 1.11 garbled if (l == -1)
175 1.11 garbled OF_getprop(node, "name", model_name,
176 1.11 garbled sizeof(model_name));
177 1.6 garbled model_init();
178 1.6 garbled }
179 1.27 macallan
180 1.47 macallan if (strncmp(model_name, "PowerMac11,2", 12) == 0 ||
181 1.47 macallan strncmp(model_name, "PowerMac12,1", 12) == 0)
182 1.50 rin ofw_quiesce = 1;
183 1.33 macallan
184 1.46 macallan /* switch CPUs to full speed */
185 1.46 macallan if (strncmp(model_name, "PowerMac7,", 10) == 0) {
186 1.46 macallan int clock_ih = OF_open("/u3/i2c/i2c-hwclock");
187 1.46 macallan if (clock_ih != 0) {
188 1.46 macallan OF_call_method_1("slew-high", clock_ih, 0);
189 1.46 macallan }
190 1.46 macallan }
191 1.46 macallan
192 1.20 kiyohara /* Initialize bus_space */
193 1.20 kiyohara ofwoea_bus_space_init();
194 1.6 garbled
195 1.2 garbled ofwoea_consinit();
196 1.2 garbled
197 1.50 rin if (ofw_quiesce)
198 1.50 rin OF_quiesce();
199 1.50 rin
200 1.13 garbled #if defined(MULTIPROCESSOR) && defined(ofppc)
201 1.13 garbled for (i=1; i < CPU_MAXNUM; i++) {
202 1.38 matt snprintf(cpupath, sizeof(cpupath), "/cpus/@%x", i);
203 1.13 garbled node = OF_finddevice(cpupath);
204 1.13 garbled if (node <= 0)
205 1.13 garbled continue;
206 1.13 garbled aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
207 1.13 garbled OF_start_cpu(node, (u_int)cpu_spinstart, i);
208 1.13 garbled for (l=0; l < 100000000; l++) {
209 1.13 garbled if (cpu_spinstart_ack == i) {
210 1.13 garbled aprint_verbose("CPU %d spun up.\n", i);
211 1.13 garbled break;
212 1.13 garbled }
213 1.13 garbled __asm volatile ("sync");
214 1.13 garbled }
215 1.13 garbled }
216 1.13 garbled #endif
217 1.13 garbled
218 1.2 garbled /* Parse the args string */
219 1.2 garbled if (args) {
220 1.2 garbled strcpy(bootpath, args);
221 1.2 garbled args = bootpath;
222 1.2 garbled while (*++args && *args != ' ');
223 1.2 garbled if (*args) {
224 1.2 garbled *args++ = 0;
225 1.2 garbled while (*args)
226 1.2 garbled BOOT_FLAG(*args++, boothowto);
227 1.2 garbled }
228 1.44 macallan } else {
229 1.44 macallan int chs = OF_finddevice("/chosen");
230 1.44 macallan int len;
231 1.44 macallan
232 1.44 macallan len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
233 1.44 macallan if (len > -1)
234 1.44 macallan bootpath[len] = 0;
235 1.2 garbled }
236 1.2 garbled
237 1.55 thorpej oea_init(pic_ext_intr);
238 1.55 thorpej
239 1.55 thorpej /*
240 1.55 thorpej * Now that we've installed our own exception vectors,
241 1.55 thorpej * ensure that exceptions that happen while running
242 1.55 thorpej * firmware code fall into ours.
243 1.55 thorpej */
244 1.55 thorpej ofwmsr &= ~PSL_IP;
245 1.55 thorpej
246 1.39 cherry uvm_md_init();
247 1.10 garbled
248 1.2 garbled pmap_bootstrap(startkernel, endkernel);
249 1.2 garbled
250 1.13 garbled /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
251 1.2 garbled #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
252 1.2 garbled #if defined (PMAC_G5)
253 1.2 garbled /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
254 1.32 macallan if (oeacpufeat & OEACPU_64_BRIDGE) {
255 1.32 macallan vaddr_t va;
256 1.32 macallan paddr_t pa;
257 1.53 thorpej vsize_t size;
258 1.32 macallan int i;
259 1.32 macallan
260 1.33 macallan pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
261 1.33 macallan round_page(MSGBUFSIZE), 0x0);
262 1.32 macallan
263 1.32 macallan /* Map OFW code+data */
264 1.32 macallan
265 1.53 thorpej for (i = 0; i < __arraycount(ofw_translations); i++) {
266 1.53 thorpej va = ofw_translations[i].virt;
267 1.53 thorpej size = ofw_translations[i].size;
268 1.53 thorpej pa = ofw_translations[i].phys;
269 1.53 thorpej /* XXX mode */
270 1.53 thorpej
271 1.53 thorpej if (size == 0) {
272 1.53 thorpej /* No more, all done! */
273 1.53 thorpej break;
274 1.53 thorpej }
275 1.53 thorpej
276 1.53 thorpej if (va < 0xff800000)
277 1.32 macallan continue;
278 1.32 macallan
279 1.53 thorpej
280 1.53 thorpej for (; va < (ofw_translations[i].virt + size);
281 1.32 macallan va += PAGE_SIZE, pa += PAGE_SIZE) {
282 1.32 macallan pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
283 1.32 macallan VM_PROT_ALL | PMAP_WIRED);
284 1.32 macallan }
285 1.32 macallan }
286 1.32 macallan
287 1.32 macallan #if NWSDISPLAY > 0
288 1.32 macallan /* Map video frame buffer */
289 1.32 macallan
290 1.32 macallan struct rasops_info *ri = &rascons_console_screen.scr_ri;
291 1.32 macallan
292 1.32 macallan if (ri->ri_bits != NULL) {
293 1.32 macallan for (va = (vaddr_t) ri->ri_bits;
294 1.32 macallan va < round_page((vaddr_t) ri->ri_bits +
295 1.32 macallan ri->ri_height * ri->ri_stride);
296 1.32 macallan va += PAGE_SIZE) {
297 1.32 macallan pmap_enter(pmap_kernel(), va, va,
298 1.32 macallan VM_PROT_READ | VM_PROT_WRITE,
299 1.32 macallan PMAP_NOCACHE | PMAP_WIRED);
300 1.32 macallan }
301 1.32 macallan }
302 1.32 macallan #endif
303 1.32 macallan }
304 1.2 garbled #elif defined (MAMBO)
305 1.2 garbled /* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
306 1.10 garbled if (oeacpufeat & OEACPU_64_BRIDGE)
307 1.10 garbled pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
308 1.2 garbled #endif /* PMAC_G5 */
309 1.10 garbled #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
310 1.2 garbled
311 1.2 garbled /* Now enable translation (and machine checks/recoverable interrupts) */
312 1.2 garbled __asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
313 1.2 garbled : "=r"(scratch)
314 1.2 garbled : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
315 1.2 garbled
316 1.52 thorpej restore_ofmap();
317 1.2 garbled
318 1.43 macallan rascons_finalize();
319 1.43 macallan
320 1.15 ad #if NKSYMS || defined(DDB) || defined(MODULAR)
321 1.36 matt ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
322 1.2 garbled #endif
323 1.2 garbled
324 1.2 garbled /* CPU clock stuff */
325 1.2 garbled set_timebase();
326 1.26 phx
327 1.26 phx #ifdef DDB
328 1.26 phx if (boothowto & RB_KDB)
329 1.26 phx Debugger();
330 1.26 phx #endif
331 1.2 garbled }
332 1.2 garbled
333 1.2 garbled void
334 1.2 garbled set_timebase(void)
335 1.2 garbled {
336 1.41 macallan int qhandle, phandle, msr, scratch, node;
337 1.2 garbled char type[32];
338 1.2 garbled
339 1.2 garbled if (timebase_freq != 0) {
340 1.2 garbled ticks_per_sec = timebase_freq;
341 1.2 garbled goto found;
342 1.2 garbled }
343 1.2 garbled
344 1.41 macallan node = OF_finddevice("/cpus/@0");
345 1.42 mrg if (node != -1 &&
346 1.42 mrg OF_getprop(node, "timebase-frequency", &ticks_per_sec,
347 1.42 mrg sizeof ticks_per_sec) > 0) {
348 1.41 macallan goto found;
349 1.41 macallan }
350 1.41 macallan
351 1.41 macallan node = OF_finddevice("/");
352 1.41 macallan for (qhandle = node; qhandle; qhandle = phandle) {
353 1.2 garbled if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
354 1.2 garbled && strcmp(type, "cpu") == 0
355 1.2 garbled && OF_getprop(qhandle, "timebase-frequency",
356 1.2 garbled &ticks_per_sec, sizeof ticks_per_sec) > 0) {
357 1.2 garbled goto found;
358 1.2 garbled }
359 1.2 garbled if ((phandle = OF_child(qhandle)))
360 1.2 garbled continue;
361 1.2 garbled while (qhandle) {
362 1.2 garbled if ((phandle = OF_peer(qhandle)))
363 1.2 garbled break;
364 1.2 garbled qhandle = OF_parent(qhandle);
365 1.2 garbled }
366 1.2 garbled }
367 1.2 garbled panic("no cpu node");
368 1.2 garbled
369 1.2 garbled found:
370 1.2 garbled __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
371 1.2 garbled : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
372 1.2 garbled ns_per_tick = 1000000000 / ticks_per_sec;
373 1.2 garbled ticks_per_intr = ticks_per_sec / hz;
374 1.2 garbled cpu_timebase = ticks_per_sec;
375 1.37 macallan
376 1.37 macallan #ifdef PPC_OEA601
377 1.37 macallan if ((mfpvr() >> 16) == MPC601)
378 1.37 macallan curcpu()->ci_lasttb = rtc_nanosecs();
379 1.37 macallan else
380 1.37 macallan #endif
381 1.2 garbled curcpu()->ci_lasttb = mftbl();
382 1.37 macallan
383 1.2 garbled mtspr(SPR_DEC, ticks_per_intr);
384 1.2 garbled mtmsr(msr);
385 1.2 garbled }
386 1.2 garbled
387 1.2 garbled void
388 1.52 thorpej restore_ofmap(void)
389 1.2 garbled {
390 1.52 thorpej vaddr_t va, size;
391 1.52 thorpej paddr_t pa;
392 1.2 garbled int i;
393 1.2 garbled
394 1.2 garbled pmap_pinit(&ofw_pmap);
395 1.2 garbled
396 1.36 matt #ifndef _LP64
397 1.32 macallan ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
398 1.32 macallan ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
399 1.2 garbled
400 1.2 garbled #ifdef KERNEL2_SR
401 1.32 macallan ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
402 1.2 garbled #endif
403 1.36 matt #endif
404 1.2 garbled
405 1.52 thorpej for (i = 0; i < __arraycount(ofw_translations); i++) {
406 1.52 thorpej va = ofw_translations[i].virt;
407 1.52 thorpej size = ofw_translations[i].size;
408 1.52 thorpej pa = ofw_translations[i].phys;
409 1.52 thorpej /* XXX mode */
410 1.52 thorpej
411 1.52 thorpej if (size == 0) {
412 1.52 thorpej /* No more, all done! */
413 1.52 thorpej break;
414 1.52 thorpej }
415 1.2 garbled
416 1.52 thorpej if (va < 0xf0000000) /* XXX */
417 1.2 garbled continue;
418 1.2 garbled
419 1.52 thorpej while (size > 0) {
420 1.52 thorpej pmap_enter(&ofw_pmap, va, pa, VM_PROT_ALL,
421 1.2 garbled VM_PROT_ALL|PMAP_WIRED);
422 1.2 garbled pa += PAGE_SIZE;
423 1.2 garbled va += PAGE_SIZE;
424 1.52 thorpej size -= PAGE_SIZE;
425 1.2 garbled }
426 1.2 garbled }
427 1.2 garbled pmap_update(&ofw_pmap);
428 1.19 kiyohara }
429 1.2 garbled
430 1.2 garbled
431 1.2 garbled
432 1.2 garbled /*
433 1.18 phx * Scan the device tree for ranges, and return them as bitmap 0..15
434 1.2 garbled */
435 1.36 matt #if !defined(macppc) && defined(PPC_OEA)
436 1.2 garbled static u_int16_t
437 1.2 garbled ranges_bitmap(int node, u_int16_t bitmap)
438 1.2 garbled {
439 1.4 garbled int child, mlen, acells, scells, reclen, i, j;
440 1.4 garbled u_int32_t addr, len, map[160];
441 1.2 garbled
442 1.2 garbled for (child = OF_child(node); child; child = OF_peer(child)) {
443 1.2 garbled mlen = OF_getprop(child, "ranges", map, sizeof(map));
444 1.2 garbled if (mlen == -1)
445 1.2 garbled goto noranges;
446 1.4 garbled
447 1.4 garbled j = OF_getprop(child, "#address-cells", &acells,
448 1.2 garbled sizeof(acells));
449 1.4 garbled if (j == -1)
450 1.2 garbled goto noranges;
451 1.4 garbled
452 1.4 garbled j = OF_getprop(child, "#size-cells", &scells,
453 1.2 garbled sizeof(scells));
454 1.4 garbled if (j == -1)
455 1.2 garbled goto noranges;
456 1.2 garbled
457 1.18 phx #ifdef ofppc
458 1.18 phx reclen = acells + modeldata.ranges_offset + scells;
459 1.18 phx #else
460 1.2 garbled reclen = acells + 1 + scells;
461 1.18 phx #endif
462 1.2 garbled
463 1.4 garbled for (i=0; i < (mlen/4)/reclen; i++) {
464 1.4 garbled addr = map[reclen * i + acells];
465 1.2 garbled len = map[reclen * i + reclen - 1];
466 1.2 garbled for (j = 0; j < len / 0x10000000; j++)
467 1.2 garbled bitmap |= 1 << ((addr+j*0x10000000) >>28);
468 1.2 garbled bitmap |= 1 << (addr >> 28);
469 1.2 garbled }
470 1.2 garbled noranges:
471 1.2 garbled bitmap |= ranges_bitmap(child, bitmap);
472 1.2 garbled continue;
473 1.2 garbled }
474 1.2 garbled return bitmap;
475 1.2 garbled }
476 1.36 matt #endif /* !macppc && PPC_OEA */
477 1.2 garbled
478 1.2 garbled void
479 1.2 garbled ofwoea_batinit(void)
480 1.2 garbled {
481 1.10 garbled #if defined (PPC_OEA)
482 1.27 macallan
483 1.27 macallan #ifdef macppc
484 1.27 macallan /*
485 1.27 macallan * cover PCI and register space but not the firmware ROM
486 1.27 macallan */
487 1.37 macallan #ifdef PPC_OEA601
488 1.37 macallan
489 1.37 macallan /*
490 1.37 macallan * use segment registers for the 601
491 1.37 macallan */
492 1.37 macallan if ((mfpvr() >> 16 ) == MPC601)
493 1.37 macallan oea_batinit(
494 1.37 macallan 0x80000000, BAT_BL_256M,
495 1.37 macallan 0x90000000, BAT_BL_256M,
496 1.37 macallan 0xa0000000, BAT_BL_256M,
497 1.37 macallan 0xb0000000, BAT_BL_256M,
498 1.37 macallan 0xf0000000, BAT_BL_256M,
499 1.37 macallan 0);
500 1.37 macallan else
501 1.51 thorpej #endif /* PPC_OEA601 */
502 1.37 macallan /*
503 1.37 macallan * map to bats
504 1.37 macallan */
505 1.28 matt oea_batinit(0x80000000, BAT_BL_1G,
506 1.27 macallan 0xf0000000, BAT_BL_128M,
507 1.27 macallan 0xf8000000, BAT_BL_64M,
508 1.29 macallan 0xfe000000, BAT_BL_8M, /* Grackle IO */
509 1.27 macallan 0);
510 1.51 thorpej #else /* ! macppc */
511 1.28 matt uint16_t bitmap;
512 1.2 garbled int node, i;
513 1.2 garbled
514 1.2 garbled node = OF_finddevice("/");
515 1.27 macallan
516 1.2 garbled bitmap = ranges_bitmap(node, 0);
517 1.2 garbled oea_batinit(0);
518 1.19 kiyohara
519 1.5 garbled for (i=1; i < 0x10; i++) {
520 1.5 garbled /* skip the three vital SR regions */
521 1.5 garbled if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
522 1.5 garbled continue;
523 1.4 garbled if (bitmap & (1 << i)) {
524 1.2 garbled oea_iobat_add(0x10000000 * i, BAT_BL_256M);
525 1.4 garbled DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
526 1.4 garbled }
527 1.5 garbled }
528 1.51 thorpej #endif /* macppc */
529 1.10 garbled #endif /* OEA */
530 1.2 garbled }
531 1.2 garbled
532 1.2 garbled
533 1.2 garbled /* we define these partially, as we will fill the rest in later */
534 1.2 garbled struct powerpc_bus_space genppc_isa_io_space_tag = {
535 1.2 garbled .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
536 1.2 garbled .pbs_base = 0x00000000,
537 1.2 garbled };
538 1.2 garbled
539 1.2 garbled struct powerpc_bus_space genppc_isa_mem_space_tag = {
540 1.2 garbled .pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
541 1.2 garbled .pbs_base = 0x00000000,
542 1.2 garbled };
543 1.2 garbled
544 1.2 garbled /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
545 1.2 garbled * Increase if necc.
546 1.2 garbled */
547 1.2 garbled static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
548 1.2 garbled __attribute__((aligned(8)));
549 1.2 garbled
550 1.2 garbled
551 1.2 garbled static void
552 1.2 garbled find_ranges(int base, rangemap_t *regions, int *cur, int type)
553 1.2 garbled {
554 1.2 garbled int node, i, len, reclen;
555 1.32 macallan u_int32_t parent_acells, acells, scells, map[160];
556 1.2 garbled char tmp[32];
557 1.2 garbled
558 1.2 garbled node = base;
559 1.2 garbled if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
560 1.2 garbled goto rec;
561 1.2 garbled if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
562 1.2 garbled strcmp("pci", tmp) != 0)
563 1.2 garbled goto rec;
564 1.2 garbled if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
565 1.2 garbled goto rec;
566 1.40 macallan if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
567 1.40 macallan len = OF_getprop(node, "reg", map, sizeof(map));
568 1.40 macallan acells = 1;
569 1.40 macallan scells = 1;
570 1.40 macallan } else {
571 1.40 macallan len = OF_getprop(node, "ranges", map, sizeof(map));
572 1.40 macallan }
573 1.2 garbled if (len == -1)
574 1.2 garbled goto rec;
575 1.32 macallan if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
576 1.32 macallan sizeof(parent_acells)) != sizeof(parent_acells))
577 1.32 macallan parent_acells = 1;
578 1.2 garbled if (OF_getprop(node, "#address-cells", &acells,
579 1.2 garbled sizeof(acells)) != sizeof(acells))
580 1.32 macallan acells = 3;
581 1.2 garbled if (OF_getprop(node, "#size-cells", &scells,
582 1.2 garbled sizeof(scells)) != sizeof(scells))
583 1.32 macallan scells = 2;
584 1.11 garbled #ifdef ofppc
585 1.11 garbled if (modeldata.ranges_offset == 0)
586 1.11 garbled scells -= 1;
587 1.11 garbled #endif
588 1.2 garbled if (type == RANGE_TYPE_ISA)
589 1.2 garbled reclen = 6;
590 1.2 garbled else
591 1.32 macallan reclen = parent_acells + acells + scells;
592 1.3 garbled /*
593 1.3 garbled * There exist ISA buses with empty ranges properties. This is
594 1.3 garbled * known to occur on the Pegasos II machine, and likely others.
595 1.3 garbled * According to them, that means that the isa bus is a fake bus, and
596 1.3 garbled * the real maps are the PCI maps of the preceeding bus. To deal
597 1.3 garbled * with this, we will set cur to -1 and return.
598 1.3 garbled */
599 1.3 garbled if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
600 1.3 garbled *cur = -1;
601 1.3 garbled DPRINTF("Found empty range in isa bus\n");
602 1.3 garbled return;
603 1.3 garbled }
604 1.3 garbled
605 1.3 garbled DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
606 1.2 garbled switch (type) {
607 1.2 garbled case RANGE_TYPE_PCI:
608 1.2 garbled case RANGE_TYPE_FIRSTPCI:
609 1.2 garbled for (i=0; i < len/(4*reclen); i++) {
610 1.3 garbled DPRINTF("FOUND PCI RANGE\n");
611 1.8 garbled regions[*cur].size =
612 1.32 macallan map[i*reclen + parent_acells + acells + scells - 1];
613 1.8 garbled /* skip ranges of size==0 */
614 1.8 garbled if (regions[*cur].size == 0)
615 1.8 garbled continue;
616 1.32 macallan regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
617 1.32 macallan regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
618 1.2 garbled (*cur)++;
619 1.2 garbled }
620 1.2 garbled break;
621 1.2 garbled case RANGE_TYPE_ISA:
622 1.2 garbled for (i=0; i < len/(4*reclen); i++) {
623 1.2 garbled if (map[i*reclen] == 1)
624 1.2 garbled regions[*cur].type = RANGE_IO;
625 1.2 garbled else
626 1.2 garbled regions[*cur].type = RANGE_MEM;
627 1.3 garbled DPRINTF("FOUND ISA RANGE TYPE=%d\n",
628 1.3 garbled regions[*cur].type);
629 1.2 garbled regions[*cur].size =
630 1.2 garbled map[i*reclen + acells + scells];
631 1.2 garbled (*cur)++;
632 1.2 garbled }
633 1.2 garbled break;
634 1.40 macallan case RANGE_TYPE_MACIO:
635 1.40 macallan regions[*cur].type = RANGE_MEM;
636 1.41 macallan if (len == 8) {
637 1.41 macallan regions[*cur].size = map[1];
638 1.41 macallan regions[*cur].addr = map[0];
639 1.41 macallan } else {
640 1.41 macallan regions[*cur].size = map[2];
641 1.41 macallan regions[*cur].addr = map[1];
642 1.41 macallan }
643 1.40 macallan (*cur)++;
644 1.40 macallan break;
645 1.2 garbled }
646 1.3 garbled DPRINTF("returning with CUR=%d\n", *cur);
647 1.2 garbled return;
648 1.2 garbled rec:
649 1.3 garbled for (node = OF_child(base); node; node = OF_peer(node)) {
650 1.3 garbled DPRINTF("RECURSE 1 STEP\n");
651 1.2 garbled find_ranges(node, regions, cur, type);
652 1.3 garbled if (*cur == -1)
653 1.3 garbled return;
654 1.3 garbled }
655 1.2 garbled }
656 1.2 garbled
657 1.2 garbled static int
658 1.2 garbled find_lowest_range(rangemap_t *ranges, int nrof, int type)
659 1.2 garbled {
660 1.2 garbled int i, low = 0;
661 1.2 garbled u_int32_t addr = 0xffffffff;
662 1.2 garbled
663 1.2 garbled for (i=0; i < nrof; i++) {
664 1.2 garbled if (ranges[i].type == type && ranges[i].addr != 0 &&
665 1.2 garbled ranges[i].addr < addr) {
666 1.2 garbled low = i;
667 1.2 garbled addr = ranges[i].addr;
668 1.2 garbled }
669 1.2 garbled }
670 1.2 garbled if (addr == 0xffffffff)
671 1.2 garbled return -1;
672 1.2 garbled return low;
673 1.2 garbled }
674 1.2 garbled
675 1.2 garbled /*
676 1.2 garbled * Find a region of memory, and create a bus_space_tag for it.
677 1.2 garbled * Notes:
678 1.2 garbled * For ISA node is ignored.
679 1.2 garbled * node is the starting node. if -1, we start at / and map everything.
680 1.2 garbled */
681 1.2 garbled
682 1.2 garbled int
683 1.2 garbled ofwoea_map_space(int rangetype, int iomem, int node,
684 1.2 garbled struct powerpc_bus_space *tag, const char *name)
685 1.2 garbled {
686 1.2 garbled int i, cur, range, nrofholes, error;
687 1.2 garbled static int exmap=0;
688 1.2 garbled rangemap_t region, holes[32], list[32];
689 1.2 garbled
690 1.2 garbled memset(list, 0, sizeof(list));
691 1.32 macallan memset(®ion, 0, sizeof(region));
692 1.2 garbled cur = 0;
693 1.2 garbled if (rangetype == RANGE_TYPE_ISA || node == -1)
694 1.2 garbled node = OF_finddevice("/");
695 1.2 garbled if (rangetype == RANGE_TYPE_ISA) {
696 1.2 garbled u_int32_t size = 0;
697 1.2 garbled rangemap_t regions[32];
698 1.2 garbled
699 1.3 garbled DPRINTF("LOOKING FOR FIRSTPCI\n");
700 1.2 garbled find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
701 1.2 garbled range = 0;
702 1.3 garbled DPRINTF("LOOKING FOR ISA\n");
703 1.2 garbled find_ranges(node, regions, &range, RANGE_TYPE_ISA);
704 1.2 garbled if (range == 0 || cur == 0)
705 1.2 garbled return -1; /* no isa stuff found */
706 1.3 garbled /*
707 1.3 garbled * This may be confusing to some. The ISA ranges property
708 1.3 garbled * is supposed to be a set of IO ranges for the ISA bus, but
709 1.3 garbled * generally, it's just a set of pci devfunc lists that tell
710 1.3 garbled * you to go look at the parent PCI device for the actual
711 1.3 garbled * ranges.
712 1.3 garbled */
713 1.3 garbled if (range == -1) {
714 1.3 garbled /* we found a rangeless isa bus */
715 1.20 kiyohara if (iomem == RANGE_IO)
716 1.20 kiyohara size = 0x10000;
717 1.20 kiyohara else
718 1.20 kiyohara size = 0x1000000;
719 1.3 garbled }
720 1.3 garbled DPRINTF("found isa stuff\n");
721 1.2 garbled for (i=0; i < range; i++)
722 1.2 garbled if (regions[i].type == iomem)
723 1.2 garbled size = regions[i].size;
724 1.2 garbled if (iomem == RANGE_IO) {
725 1.2 garbled /* the first io range is the one */
726 1.2 garbled for (i=0; i < cur; i++)
727 1.2 garbled if (list[i].type == RANGE_IO && size) {
728 1.3 garbled DPRINTF("found IO\n");
729 1.2 garbled tag->pbs_offset = list[i].addr;
730 1.2 garbled tag->pbs_limit = size;
731 1.20 kiyohara error = bus_space_init(tag, name,
732 1.20 kiyohara ex_storage[exmap],
733 1.20 kiyohara sizeof(ex_storage[exmap]));
734 1.20 kiyohara exmap++;
735 1.2 garbled return error;
736 1.2 garbled }
737 1.2 garbled } else {
738 1.2 garbled for (i=0; i < cur; i++)
739 1.2 garbled if (list[i].type == RANGE_MEM &&
740 1.2 garbled list[i].size == size) {
741 1.3 garbled DPRINTF("found mem\n");
742 1.2 garbled tag->pbs_offset = list[i].addr;
743 1.2 garbled tag->pbs_limit = size;
744 1.20 kiyohara error = bus_space_init(tag, name,
745 1.20 kiyohara ex_storage[exmap],
746 1.20 kiyohara sizeof(ex_storage[exmap]));
747 1.20 kiyohara exmap++;
748 1.2 garbled return error;
749 1.2 garbled }
750 1.2 garbled }
751 1.2 garbled return -1; /* NO ISA FOUND */
752 1.2 garbled }
753 1.2 garbled find_ranges(node, list, &cur, rangetype);
754 1.2 garbled
755 1.2 garbled DPRINTF("cur == %d\n", cur);
756 1.2 garbled /* now list should contain a list of memory regions */
757 1.2 garbled for (i=0; i < cur; i++)
758 1.2 garbled DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
759 1.2 garbled list[i].size, list[i].type);
760 1.2 garbled
761 1.2 garbled range = find_lowest_range(list, cur, iomem);
762 1.2 garbled i = 0;
763 1.2 garbled nrofholes = 0;
764 1.2 garbled while (range != -1) {
765 1.2 garbled DPRINTF("range==%d\n", range);
766 1.2 garbled DPRINTF("i==%d\n", i);
767 1.2 garbled if (i == 0) {
768 1.2 garbled memcpy(®ion, &list[range], sizeof(rangemap_t));
769 1.2 garbled list[range].addr = 0;
770 1.2 garbled i++;
771 1.2 garbled range = find_lowest_range(list, cur, iomem);
772 1.2 garbled continue;
773 1.2 garbled }
774 1.2 garbled if (region.addr + region.size < list[range].addr) {
775 1.2 garbled /* allocate a hole */
776 1.11 garbled holes[nrofholes].type = iomem;
777 1.2 garbled holes[nrofholes].addr = region.size + region.addr;
778 1.2 garbled holes[nrofholes].size = list[range].addr -
779 1.2 garbled holes[nrofholes].addr - 1;
780 1.2 garbled nrofholes++;
781 1.2 garbled }
782 1.2 garbled region.size = list[range].size + list[range].addr -
783 1.2 garbled region.addr;
784 1.2 garbled list[range].addr = 0;
785 1.2 garbled range = find_lowest_range(list, cur, iomem);
786 1.2 garbled }
787 1.2 garbled DPRINTF("RANGE iomem=%d FOUND\n", iomem);
788 1.2 garbled DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
789 1.2 garbled region.size, region.type);
790 1.2 garbled DPRINTF("HOLES FOUND\n");
791 1.2 garbled for (i=0; i < nrofholes; i++)
792 1.2 garbled DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
793 1.2 garbled holes[i].size, holes[i].type);
794 1.2 garbled /* AT THIS POINT WE MAP IT */
795 1.2 garbled
796 1.40 macallan if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
797 1.2 garbled if (exmap == EXSTORAGE_MAX)
798 1.2 garbled panic("Not enough ex_storage space. "
799 1.2 garbled "Increase EXSTORAGE_MAX");
800 1.2 garbled
801 1.2 garbled /* XXX doing this in here might be wrong */
802 1.2 garbled if (iomem == 1) {
803 1.2 garbled /* we map an IO region */
804 1.2 garbled tag->pbs_offset = region.addr;
805 1.2 garbled tag->pbs_base = 0;
806 1.2 garbled tag->pbs_limit = region.size;
807 1.2 garbled } else {
808 1.2 garbled /* ... or a memory region */
809 1.2 garbled tag->pbs_offset = 0;
810 1.2 garbled tag->pbs_base = region.addr;
811 1.2 garbled tag->pbs_limit = region.size + region.addr;
812 1.19 kiyohara }
813 1.2 garbled
814 1.2 garbled error = bus_space_init(tag, name, ex_storage[exmap],
815 1.2 garbled sizeof(ex_storage[exmap]));
816 1.2 garbled exmap++;
817 1.2 garbled if (error)
818 1.2 garbled panic("ofwoea_bus_space_init: can't init tag %s", name);
819 1.2 garbled for (i=0; i < nrofholes; i++) {
820 1.11 garbled if (holes[i].type == RANGE_IO) {
821 1.11 garbled error = extent_alloc_region(tag->pbs_extent,
822 1.11 garbled holes[i].addr - tag->pbs_offset,
823 1.11 garbled holes[i].size, EX_NOWAIT);
824 1.11 garbled } else {
825 1.11 garbled error = extent_alloc_region(tag->pbs_extent,
826 1.11 garbled holes[i].addr, holes[i].size, EX_NOWAIT);
827 1.11 garbled }
828 1.2 garbled if (error)
829 1.2 garbled panic("ofwoea_bus_space_init: can't block out"
830 1.2 garbled " reserved space 0x%x-0x%x: error=%d",
831 1.2 garbled holes[i].addr, holes[i].addr+holes[i].size,
832 1.2 garbled error);
833 1.2 garbled }
834 1.2 garbled return error;
835 1.2 garbled }
836 1.2 garbled return -1;
837 1.2 garbled }
838 1.2 garbled
839 1.2 garbled void
840 1.2 garbled ofwoea_bus_space_init(void)
841 1.2 garbled {
842 1.2 garbled int error;
843 1.2 garbled
844 1.2 garbled error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
845 1.2 garbled &genppc_isa_io_space_tag, "isa-ioport");
846 1.2 garbled if (error > 0)
847 1.2 garbled panic("Could not map ISA IO");
848 1.3 garbled
849 1.2 garbled error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
850 1.2 garbled &genppc_isa_mem_space_tag, "isa-iomem");
851 1.2 garbled if (error > 0)
852 1.2 garbled panic("Could not map ISA MEM");
853 1.2 garbled }
854