ofw.c revision 1.60 1 /* $NetBSD: ofw.c,v 1.60 2012/09/22 00:33:41 matt Exp $ */
2
3 /*
4 * Copyright 1997
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as
15 * they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Digital Equipment Corporation. Neither the "Digital Equipment
19 * Corporation" name nor any trademark or logo of Digital Equipment
20 * Corporation may be used to endorse or promote products derived
21 * from this software without the prior written permission of
22 * Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage.
34 */
35
36 /*
37 * Routines for interfacing between NetBSD and OFW.
38 *
39 * Parts of this could be moved to an MI file in time. -JJK
40 *
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ofw.c,v 1.60 2012/09/22 00:33:41 matt Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/device.h>
49 #include <sys/kernel.h>
50 #include <sys/reboot.h>
51 #include <sys/mbuf.h>
52
53 #include <uvm/uvm.h>
54
55 #include <dev/cons.h>
56
57 #define _ARM32_BUS_DMA_PRIVATE
58 #include <sys/bus.h>
59 #include <machine/frame.h>
60 #include <machine/bootconfig.h>
61 #include <machine/cpu.h>
62 #include <machine/intr.h>
63 #include <machine/irqhandler.h>
64
65 #include <dev/ofw/openfirm.h>
66 #include <machine/ofw.h>
67
68 #include <netinet/in.h>
69
70 #if BOOT_FW_DHCP
71 #include <nfs/bootdata.h>
72 #endif
73
74 #ifdef SHARK
75 #include "machine/pio.h"
76 #include "machine/isa_machdep.h"
77 #endif
78
79 #include "isadma.h"
80 #include "igsfb_ofbus.h"
81 #include "chipsfb_ofbus.h"
82 #include "vga_ofbus.h"
83
84 #define IO_VIRT_BASE (OFW_VIRT_BASE + OFW_VIRT_SIZE)
85 #define IO_VIRT_SIZE 0x01000000
86
87 #define KERNEL_IMG_PTS 2
88 #define KERNEL_VMDATA_PTS (KERNEL_VM_SIZE >> (L1_S_SHIFT + 2))
89 #define KERNEL_OFW_PTS 4
90 #define KERNEL_IO_PTS 4
91
92 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
93 /*
94 * The range 0xf1000000 - 0xf6ffffff is available for kernel VM space
95 * OFW sits at 0xf7000000
96 */
97 #define KERNEL_VM_SIZE 0x06000000
98
99 /*
100 * Imported variables
101 */
102 extern BootConfig bootconfig; /* temporary, I hope */
103
104 #ifdef DIAGNOSTIC
105 /* NOTE: These variables will be removed, well some of them */
106 extern u_int current_mask;
107 #endif
108
109 extern int ofw_handleticks;
110
111
112 /*
113 * Imported routines
114 */
115 extern void dump_spl_masks(void);
116 extern void dumpsys(void);
117 extern void dotickgrovelling(vaddr_t);
118
119 #define WriteWord(a, b) \
120 *((volatile unsigned int *)(a)) = (b)
121
122 #define ReadWord(a) \
123 (*((volatile unsigned int *)(a)))
124
125
126 /*
127 * Exported variables
128 */
129 /* These should all be in a meminfo structure. */
130 paddr_t physical_start;
131 paddr_t physical_freestart;
132 paddr_t physical_freeend;
133 paddr_t physical_end;
134 u_int free_pages;
135
136 paddr_t msgbufphys;
137
138 /* for storage allocation, used to be local to ofw_construct_proc0_addrspace */
139 static vaddr_t virt_freeptr;
140
141 int ofw_callbacks = 0; /* debugging counter */
142
143 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
144 int console_ihandle = 0;
145 static void reset_screen(void);
146 #endif
147
148 /**************************************************************/
149
150
151 /*
152 * Declarations and definitions private to this module
153 *
154 */
155
156 struct mem_region {
157 paddr_t start;
158 psize_t size;
159 };
160
161 struct mem_translation {
162 vaddr_t virt;
163 vsize_t size;
164 paddr_t phys;
165 unsigned int mode;
166 };
167
168 struct isa_range {
169 paddr_t isa_phys_hi;
170 paddr_t isa_phys_lo;
171 paddr_t parent_phys_start;
172 psize_t isa_size;
173 };
174
175 struct vl_range {
176 paddr_t vl_phys_hi;
177 paddr_t vl_phys_lo;
178 paddr_t parent_phys_start;
179 psize_t vl_size;
180 };
181
182 struct vl_isa_range {
183 paddr_t isa_phys_hi;
184 paddr_t isa_phys_lo;
185 paddr_t parent_phys_hi;
186 paddr_t parent_phys_lo;
187 psize_t isa_size;
188 };
189
190 struct dma_range {
191 paddr_t start;
192 psize_t size;
193 };
194
195 struct ofw_cbargs {
196 char *name;
197 int nargs;
198 int nreturns;
199 int args_n_results[12];
200 };
201
202
203 /* Memory info */
204 static int nOFphysmem;
205 static struct mem_region *OFphysmem;
206 static int nOFphysavail;
207 static struct mem_region *OFphysavail;
208 static int nOFtranslations;
209 static struct mem_translation *OFtranslations;
210 static int nOFdmaranges;
211 static struct dma_range *OFdmaranges;
212
213 /* The OFW client services handle. */
214 /* Initialized by ofw_init(). */
215 static ofw_handle_t ofw_client_services_handle;
216
217
218 static void ofw_callbackhandler(void *);
219 static void ofw_construct_proc0_addrspace(void);
220 static void ofw_getphysmeminfo(void);
221 static void ofw_getvirttranslations(void);
222 static void *ofw_malloc(vsize_t size);
223 static void ofw_claimpages(vaddr_t *, pv_addr_t *, vsize_t);
224 static void ofw_discardmappings(vaddr_t, vaddr_t, vsize_t);
225 static int ofw_mem_ihandle(void);
226 static int ofw_mmu_ihandle(void);
227 static paddr_t ofw_claimphys(paddr_t, psize_t, paddr_t);
228 #if 0
229 static paddr_t ofw_releasephys(paddr_t, psize_t);
230 #endif
231 static vaddr_t ofw_claimvirt(vaddr_t, vsize_t, vaddr_t);
232 static void ofw_settranslation(vaddr_t, paddr_t, vsize_t, int);
233 static void ofw_initallocator(void);
234 static void ofw_configisaonly(paddr_t *, paddr_t *);
235 static void ofw_configvl(int, paddr_t *, paddr_t *);
236 static vaddr_t ofw_valloc(vsize_t, vaddr_t);
237
238
239 /*
240 * DHCP hooks. For a first cut, we look to see if there is a DHCP
241 * packet that was saved by the firmware. If not, we proceed as before,
242 * getting hand-configured data from NVRAM. If there is one, we get the
243 * packet, and extract the data from it. For now, we hand that data up
244 * in the boot_args string as before.
245 */
246
247
248 /**************************************************************/
249
250
251 /*
252 *
253 * Support routines for xxx_machdep.c
254 *
255 * The intent is that all OFW-based configurations use the
256 * exported routines in this file to do their business. If
257 * they need to override some function they are free to do so.
258 *
259 * The exported routines are:
260 *
261 * openfirmware
262 * ofw_init
263 * ofw_boot
264 * ofw_getbootinfo
265 * ofw_configmem
266 * ofw_configisa
267 * ofw_configisadma
268 * ofw_gettranslation
269 * ofw_map
270 * ofw_getcleaninfo
271 */
272
273
274 int
275 openfirmware(void *args)
276 {
277 int ofw_result;
278 u_int saved_irq_state;
279
280 /* OFW is not re-entrant, so we wrap a mutex around the call. */
281 saved_irq_state = disable_interrupts(I32_bit);
282 ofw_result = ofw_client_services_handle(args);
283 (void)restore_interrupts(saved_irq_state);
284
285 return(ofw_result);
286 }
287
288
289 void
290 ofw_init(ofw_handle_t ofw_handle)
291 {
292 ofw_client_services_handle = ofw_handle;
293
294 /* Everything we allocate in the remainder of this block is
295 * constrained to be in the "kernel-static" portion of the
296 * virtual address space (i.e., 0xF0000000 - 0xF1000000).
297 * This is because all such objects are expected to be in
298 * that range by NetBSD, or the objects will be re-mapped
299 * after the page-table-switch to other specific locations.
300 * In the latter case, it's simplest if our pre-switch handles
301 * on those objects are in regions that are already "well-
302 * known." (Otherwise, the cloning of the OFW-managed address-
303 * space becomes more awkward.) To minimize the number of L2
304 * page tables that we use, we are further restricting the
305 * remaining allocations in this block to the bottom quarter of
306 * the legal range. OFW will have loaded the kernel text+data+bss
307 * starting at the bottom of the range, and we will allocate
308 * objects from the top, moving downwards. The two sub-regions
309 * will collide if their total sizes hit 8MB. The current total
310 * is <1.5MB, but INSTALL kernels are > 4MB, so hence the 8MB
311 * limit. The variable virt-freeptr represents the next free va
312 * (moving downwards).
313 */
314 virt_freeptr = KERNEL_BASE + (0x00400000 * KERNEL_IMG_PTS);
315 }
316
317
318 void
319 ofw_boot(int howto, char *bootstr)
320 {
321
322 #ifdef DIAGNOSTIC
323 printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
324 printf("current_mask=%08x\n", current_mask);
325
326 printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
327 irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
328 irqmasks[IPL_VM]);
329 printf("ipl_clock=%08x ipl_none=%08x\n",
330 irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
331
332 dump_spl_masks();
333 #endif
334
335 /*
336 * If we are still cold then hit the air brakes
337 * and crash to earth fast
338 */
339 if (cold) {
340 doshutdownhooks();
341 pmf_system_shutdown(boothowto);
342 printf("Halted while still in the ICE age.\n");
343 printf("The operating system has halted.\n");
344 goto ofw_exit;
345 /*NOTREACHED*/
346 }
347
348 /*
349 * If RB_NOSYNC was not specified sync the discs.
350 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
351 * It looks like syslogd is getting woken up only to find that it cannot
352 * page part of the binary in as the filesystem has been unmounted.
353 */
354 if (!(howto & RB_NOSYNC))
355 bootsync();
356
357 /* Say NO to interrupts */
358 splhigh();
359
360 /* Do a dump if requested. */
361 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
362 dumpsys();
363
364 /* Run any shutdown hooks */
365 doshutdownhooks();
366
367 pmf_system_shutdown(boothowto);
368
369 /* Make sure IRQ's are disabled */
370 IRQdisable;
371
372 if (howto & RB_HALT) {
373 printf("The operating system has halted.\n");
374 goto ofw_exit;
375 }
376
377 /* Tell the user we are booting */
378 printf("rebooting...\n");
379
380 /* Jump into the OFW boot routine. */
381 {
382 static char str[256];
383 char *ap = str, *ap1 = ap;
384
385 if (bootstr && *bootstr) {
386 if (strlen(bootstr) > sizeof str - 5)
387 printf("boot string too large, ignored\n");
388 else {
389 strcpy(str, bootstr);
390 ap1 = ap = str + strlen(str);
391 *ap++ = ' ';
392 }
393 }
394 *ap++ = '-';
395 if (howto & RB_SINGLE)
396 *ap++ = 's';
397 if (howto & RB_KDB)
398 *ap++ = 'd';
399 *ap++ = 0;
400 if (ap[-2] == '-')
401 *ap1 = 0;
402 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
403 reset_screen();
404 #endif
405 OF_boot(str);
406 /*NOTREACHED*/
407 }
408
409 ofw_exit:
410 printf("Calling OF_exit...\n");
411 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
412 reset_screen();
413 #endif
414 OF_exit();
415 /*NOTREACHED*/
416 }
417
418
419 #if BOOT_FW_DHCP
420
421 extern char *ip2dotted(struct in_addr);
422
423 /*
424 * Get DHCP data from OFW
425 */
426
427 void
428 get_fw_dhcp_data(struct bootdata *bdp)
429 {
430 int chosen;
431 int dhcplen;
432
433 memset((char *)bdp, 0, sizeof(*bdp));
434 if ((chosen = OF_finddevice("/chosen")) == -1)
435 panic("no /chosen from OFW");
436 if ((dhcplen = OF_getproplen(chosen, "bootp-response")) > 0) {
437 u_char *cp;
438 int dhcp_type = 0;
439 char *ip;
440
441 /*
442 * OFW saved a DHCP (or BOOTP) packet for us.
443 */
444 if (dhcplen > sizeof(bdp->dhcp_packet))
445 panic("DHCP packet too large");
446 OF_getprop(chosen, "bootp-response", &bdp->dhcp_packet,
447 sizeof(bdp->dhcp_packet));
448 SANITY(bdp->dhcp_packet.op == BOOTREPLY, "bogus DHCP packet");
449 /*
450 * Collect the interesting data from DHCP into
451 * the bootdata structure.
452 */
453 bdp->ip_address = bdp->dhcp_packet.yiaddr;
454 ip = ip2dotted(bdp->ip_address);
455 if (memcmp(bdp->dhcp_packet.options, DHCP_OPTIONS_COOKIE, 4) == 0)
456 parse_dhcp_options(&bdp->dhcp_packet,
457 bdp->dhcp_packet.options + 4,
458 &bdp->dhcp_packet.options[dhcplen
459 - DHCP_FIXED_NON_UDP], bdp, ip);
460 if (bdp->root_ip.s_addr == 0)
461 bdp->root_ip = bdp->dhcp_packet.siaddr;
462 if (bdp->swap_ip.s_addr == 0)
463 bdp->swap_ip = bdp->dhcp_packet.siaddr;
464 }
465 /*
466 * If the DHCP packet did not contain all the necessary data,
467 * look in NVRAM for the missing parts.
468 */
469 {
470 int options;
471 int proplen;
472 #define BOOTJUNKV_SIZE 256
473 char bootjunkv[BOOTJUNKV_SIZE]; /* minimize stack usage */
474
475
476 if ((options = OF_finddevice("/options")) == -1)
477 panic("can't find /options");
478 if (bdp->ip_address.s_addr == 0 &&
479 (proplen = OF_getprop(options, "ipaddr",
480 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
481 bootjunkv[proplen] = '\0';
482 if (dotted2ip(bootjunkv, &bdp->ip_address.s_addr) == 0)
483 bdp->ip_address.s_addr = 0;
484 }
485 if (bdp->ip_mask.s_addr == 0 &&
486 (proplen = OF_getprop(options, "netmask",
487 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
488 bootjunkv[proplen] = '\0';
489 if (dotted2ip(bootjunkv, &bdp->ip_mask.s_addr) == 0)
490 bdp->ip_mask.s_addr = 0;
491 }
492 if (bdp->hostname[0] == '\0' &&
493 (proplen = OF_getprop(options, "hostname",
494 bdp->hostname, sizeof(bdp->hostname) - 1)) > 0) {
495 bdp->hostname[proplen] = '\0';
496 }
497 if (bdp->root[0] == '\0' &&
498 (proplen = OF_getprop(options, "rootfs",
499 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
500 bootjunkv[proplen] = '\0';
501 parse_server_path(bootjunkv, &bdp->root_ip, bdp->root);
502 }
503 if (bdp->swap[0] == '\0' &&
504 (proplen = OF_getprop(options, "swapfs",
505 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
506 bootjunkv[proplen] = '\0';
507 parse_server_path(bootjunkv, &bdp->swap_ip, bdp->swap);
508 }
509 }
510 }
511
512 #endif /* BOOT_FW_DHCP */
513
514 void
515 ofw_getbootinfo(char **bp_pp, char **ba_pp)
516 {
517 int chosen;
518 int bp_len;
519 int ba_len;
520 char *bootpathv;
521 char *bootargsv;
522
523 /* Read the bootpath and bootargs out of OFW. */
524 /* XXX is bootpath still interesting? --emg */
525 if ((chosen = OF_finddevice("/chosen")) == -1)
526 panic("no /chosen from OFW");
527 bp_len = OF_getproplen(chosen, "bootpath");
528 ba_len = OF_getproplen(chosen, "bootargs");
529 if (bp_len < 0 || ba_len < 0)
530 panic("can't get boot data from OFW");
531
532 bootpathv = (char *)ofw_malloc(bp_len);
533 bootargsv = (char *)ofw_malloc(ba_len);
534
535 if (bp_len)
536 OF_getprop(chosen, "bootpath", bootpathv, bp_len);
537 else
538 bootpathv[0] = '\0';
539
540 if (ba_len)
541 OF_getprop(chosen, "bootargs", bootargsv, ba_len);
542 else
543 bootargsv[0] = '\0';
544
545 *bp_pp = bootpathv;
546 *ba_pp = bootargsv;
547 #ifdef DIAGNOSTIC
548 printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv, bootargsv);
549 #endif
550 }
551
552 paddr_t
553 ofw_getcleaninfo(void)
554 {
555 int cpu;
556 vaddr_t vclean;
557 paddr_t pclean;
558
559 if ((cpu = OF_finddevice("/cpu")) == -1)
560 panic("no /cpu from OFW");
561
562 if ((OF_getprop(cpu, "d-cache-flush-address", &vclean,
563 sizeof(vclean))) != sizeof(vclean)) {
564 #ifdef DEBUG
565 printf("no OFW d-cache-flush-address property\n");
566 #endif
567 return -1;
568 }
569
570 if ((pclean = ofw_gettranslation(
571 of_decode_int((unsigned char *)&vclean))) == -1)
572 panic("OFW failed to translate cache flush address");
573
574 return pclean;
575 }
576
577 void
578 ofw_configisa(paddr_t *pio, paddr_t *pmem)
579 {
580 int vl;
581
582 if ((vl = OF_finddevice("/vlbus")) == -1) /* old style OFW dev info tree */
583 ofw_configisaonly(pio, pmem);
584 else /* old style OFW dev info tree */
585 ofw_configvl(vl, pio, pmem);
586 }
587
588 static void
589 ofw_configisaonly(paddr_t *pio, paddr_t *pmem)
590 {
591 int isa;
592 int rangeidx;
593 int size;
594 paddr_t hi, start;
595 struct isa_range ranges[2];
596
597 if ((isa = OF_finddevice("/isa")) == -1)
598 panic("OFW has no /isa device node");
599
600 /* expect to find two isa ranges: IO/data and memory/data */
601 if ((size = OF_getprop(isa, "ranges", ranges, sizeof(ranges)))
602 != sizeof(ranges))
603 panic("unexpected size of OFW /isa ranges property: %d", size);
604
605 *pio = *pmem = -1;
606
607 for (rangeidx = 0; rangeidx < 2; ++rangeidx) {
608 hi = of_decode_int((unsigned char *)
609 &ranges[rangeidx].isa_phys_hi);
610 start = of_decode_int((unsigned char *)
611 &ranges[rangeidx].parent_phys_start);
612
613 if (hi & 1) { /* then I/O space */
614 *pio = start;
615 } else {
616 *pmem = start;
617 }
618 } /* END for */
619
620 if ((*pio == -1) || (*pmem == -1))
621 panic("bad OFW /isa ranges property");
622
623 }
624
625 static void
626 ofw_configvl(int vl, paddr_t *pio, paddr_t *pmem)
627 {
628 int isa;
629 int ir, vr;
630 int size;
631 paddr_t hi, start;
632 struct vl_isa_range isa_ranges[2];
633 struct vl_range vl_ranges[2];
634
635 if ((isa = OF_finddevice("/vlbus/isa")) == -1)
636 panic("OFW has no /vlbus/isa device node");
637
638 /* expect to find two isa ranges: IO/data and memory/data */
639 if ((size = OF_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges)))
640 != sizeof(isa_ranges))
641 panic("unexpected size of OFW /vlbus/isa ranges property: %d",
642 size);
643
644 /* expect to find two vl ranges: IO/data and memory/data */
645 if ((size = OF_getprop(vl, "ranges", vl_ranges, sizeof(vl_ranges)))
646 != sizeof(vl_ranges))
647 panic("unexpected size of OFW /vlbus ranges property: %d", size);
648
649 *pio = -1;
650 *pmem = -1;
651
652 for (ir = 0; ir < 2; ++ir) {
653 for (vr = 0; vr < 2; ++vr) {
654 if ((isa_ranges[ir].parent_phys_hi
655 == vl_ranges[vr].vl_phys_hi) &&
656 (isa_ranges[ir].parent_phys_lo
657 == vl_ranges[vr].vl_phys_lo)) {
658 hi = of_decode_int((unsigned char *)
659 &isa_ranges[ir].isa_phys_hi);
660 start = of_decode_int((unsigned char *)
661 &vl_ranges[vr].parent_phys_start);
662
663 if (hi & 1) { /* then I/O space */
664 *pio = start;
665 } else {
666 *pmem = start;
667 }
668 } /* END if */
669 } /* END for */
670 } /* END for */
671
672 if ((*pio == -1) || (*pmem == -1))
673 panic("bad OFW /isa ranges property");
674 }
675
676 #if NISADMA > 0
677 struct arm32_dma_range *shark_isa_dma_ranges;
678 int shark_isa_dma_nranges;
679 #endif
680
681 void
682 ofw_configisadma(paddr_t *pdma)
683 {
684 int root;
685 int rangeidx;
686 int size;
687 struct dma_range *dr;
688
689 if ((root = OF_finddevice("/")) == -1 ||
690 (size = OF_getproplen(root, "dma-ranges")) <= 0 ||
691 (OFdmaranges = (struct dma_range *)ofw_malloc(size)) == 0 ||
692 OF_getprop(root, "dma-ranges", OFdmaranges, size) != size)
693 panic("bad / dma-ranges property");
694
695 nOFdmaranges = size / sizeof(struct dma_range);
696
697 #if NISADMA > 0
698 /* Allocate storage for non-OFW representation of the range. */
699 shark_isa_dma_ranges = ofw_malloc(nOFdmaranges *
700 sizeof(*shark_isa_dma_ranges));
701 if (shark_isa_dma_ranges == NULL)
702 panic("unable to allocate shark_isa_dma_ranges");
703 shark_isa_dma_nranges = nOFdmaranges;
704 #endif
705
706 for (rangeidx = 0, dr = OFdmaranges; rangeidx < nOFdmaranges;
707 ++rangeidx, ++dr) {
708 dr->start = of_decode_int((unsigned char *)&dr->start);
709 dr->size = of_decode_int((unsigned char *)&dr->size);
710 #if NISADMA > 0
711 shark_isa_dma_ranges[rangeidx].dr_sysbase = dr->start;
712 shark_isa_dma_ranges[rangeidx].dr_busbase = dr->start;
713 shark_isa_dma_ranges[rangeidx].dr_len = dr->size;
714 #endif
715 }
716
717 #ifdef DEBUG
718 printf("DMA ranges size = %d\n", size);
719
720 for (rangeidx = 0; rangeidx < nOFdmaranges; ++rangeidx) {
721 printf("%08lx %08lx\n",
722 (u_long)OFdmaranges[rangeidx].start,
723 (u_long)OFdmaranges[rangeidx].size);
724 }
725 #endif
726 }
727
728 /*
729 * Memory configuration:
730 *
731 * We start off running in the environment provided by OFW.
732 * This has the MMU turned on, the kernel code and data
733 * mapped-in at KERNEL_BASE (0xF0000000), OFW's text and
734 * data mapped-in at OFW_VIRT_BASE (0xF7000000), and (possibly)
735 * page0 mapped-in at 0x0.
736 *
737 * The strategy is to set-up the address space for proc0 --
738 * including the allocation of space for new page tables -- while
739 * memory is still managed by OFW. We then effectively create a
740 * copy of the address space by dumping all of OFW's translations
741 * and poking them into the new page tables. We then notify OFW
742 * that we are assuming control of memory-management by installing
743 * our callback-handler, and switch to the NetBSD-managed page
744 * tables with the cpu_setttb() call.
745 *
746 * This scheme may cause some amount of memory to be wasted within
747 * OFW as dead page tables, but it shouldn't be more than about
748 * 20-30KB. (It's also possible that OFW will re-use the space.)
749 */
750 void
751 ofw_configmem(void)
752 {
753 int i;
754
755 /* Set-up proc0 address space. */
756 ofw_construct_proc0_addrspace();
757
758 /*
759 * Get a dump of OFW's picture of physical memory.
760 * This is used below to initialize a load of variables used by pmap.
761 * We get it now rather than later because we are about to
762 * tell OFW to stop managing memory.
763 */
764 ofw_getphysmeminfo();
765
766 /* We are about to take control of memory-management from OFW.
767 * Establish callbacks for OFW to use for its future memory needs.
768 * This is required for us to keep using OFW services.
769 */
770
771 /* First initialize our callback memory allocator. */
772 ofw_initallocator();
773
774 OF_set_callback(ofw_callbackhandler);
775
776 /* Switch to the proc0 pagetables. */
777 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
778 cpu_setttb(kernel_l1pt.pv_pa, true);
779 cpu_tlb_flushID();
780 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
781
782 /*
783 * Moved from cpu_startup() as data_abort_handler() references
784 * this during uvm init
785 */
786 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
787
788 /* Set-up the various globals which describe physical memory for pmap. */
789 {
790 struct mem_region *mp;
791 int totalcnt;
792 int availcnt;
793
794 /* physmem, physical_start, physical_end */
795 physmem = 0;
796 for (totalcnt = 0, mp = OFphysmem; totalcnt < nOFphysmem;
797 totalcnt++, mp++) {
798 #ifdef OLDPRINTFS
799 printf("physmem: %x, %x\n", mp->start, mp->size);
800 #endif
801 physmem += btoc(mp->size);
802 }
803 physical_start = OFphysmem[0].start;
804 mp--;
805 physical_end = mp->start + mp->size;
806
807 /* free_pages, physical_freestart, physical_freeend */
808 free_pages = 0;
809 for (availcnt = 0, mp = OFphysavail; availcnt < nOFphysavail;
810 availcnt++, mp++) {
811 #ifdef OLDPRINTFS
812 printf("physavail: %x, %x\n", mp->start, mp->size);
813 #endif
814 free_pages += btoc(mp->size);
815 }
816 physical_freestart = OFphysavail[0].start;
817 mp--;
818 physical_freeend = mp->start + mp->size;
819 #ifdef OLDPRINTFS
820 printf("pmap_bootstrap: physmem = %x, free_pages = %x\n",
821 physmem, free_pages);
822 #endif
823
824 /*
825 * This is a hack to work with the existing pmap code.
826 * That code depends on a RiscPC BootConfig structure
827 * containing, among other things, an array describing
828 * the regions of physical memory. So, for now, we need
829 * to stuff our OFW-derived physical memory info into a
830 * "fake" BootConfig structure.
831 *
832 * An added twist is that we initialize the BootConfig
833 * structure with our "available" physical memory regions
834 * rather than the "total" physical memory regions. Why?
835 * Because:
836 *
837 * (a) the VM code requires that the "free" pages it is
838 * initialized with have consecutive indices. This
839 * allows it to use more efficient data structures
840 * (presumably).
841 * (b) the current pmap routines which report the initial
842 * set of free page indices (pmap_next_page) and
843 * which map addresses to indices (pmap_page_index)
844 * assume that the free pages are consecutive across
845 * memory region boundaries.
846 *
847 * This means that memory which is "stolen" at startup time
848 * (say, for page descriptors) MUST come from either the
849 * bottom of the first region or the top of the last.
850 *
851 * This requirement doesn't mesh well with OFW (or at least
852 * our use of it). We can get around it for the time being
853 * by pretending that our "available" region array describes
854 * all of our physical memory. This may cause some important
855 * information to be excluded from a dump file, but so far
856 * I haven't come across any other negative effects.
857 *
858 * In the long-run we should fix the index
859 * generation/translation code in the pmap module.
860 */
861
862 if (DRAM_BLOCKS < (availcnt + 1))
863 panic("more ofw memory regions than bootconfig blocks");
864
865 for (i = 0, mp = OFphysavail; i < nOFphysavail; i++, mp++) {
866 bootconfig.dram[i].address = mp->start;
867 bootconfig.dram[i].pages = btoc(mp->size);
868 }
869 bootconfig.dramblocks = availcnt;
870 }
871
872 /* Load memory into UVM. */
873 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
874
875 /* XXX Please kill this code dead. */
876 for (i = 0; i < bootconfig.dramblocks; i++) {
877 paddr_t start = (paddr_t)bootconfig.dram[i].address;
878 paddr_t end = start + (bootconfig.dram[i].pages * PAGE_SIZE);
879 #if NISADMA > 0
880 paddr_t istart, isize;
881 #endif
882
883 if (start < physical_freestart)
884 start = physical_freestart;
885 if (end > physical_freeend)
886 end = physical_freeend;
887
888 #if 0
889 printf("%d: %lx -> %lx\n", loop, start, end - 1);
890 #endif
891
892 #if NISADMA > 0
893 if (arm32_dma_range_intersect(shark_isa_dma_ranges,
894 shark_isa_dma_nranges,
895 start, end - start,
896 &istart, &isize)) {
897 /*
898 * Place the pages that intersect with the
899 * ISA DMA range onto the ISA DMA free list.
900 */
901 #if 0
902 printf(" ISADMA 0x%lx -> 0x%lx\n", istart,
903 istart + isize - 1);
904 #endif
905 uvm_page_physload(atop(istart),
906 atop(istart + isize), atop(istart),
907 atop(istart + isize), VM_FREELIST_ISADMA);
908
909 /*
910 * Load the pieces that come before the
911 * intersection onto the default free list.
912 */
913 if (start < istart) {
914 #if 0
915 printf(" BEFORE 0x%lx -> 0x%lx\n",
916 start, istart - 1);
917 #endif
918 uvm_page_physload(atop(start),
919 atop(istart), atop(start),
920 atop(istart), VM_FREELIST_DEFAULT);
921 }
922
923 /*
924 * Load the pieces that come after the
925 * intersection onto the default free list.
926 */
927 if ((istart + isize) < end) {
928 #if 0
929 printf(" AFTER 0x%lx -> 0x%lx\n",
930 (istart + isize), end - 1);
931 #endif
932 uvm_page_physload(atop(istart + isize),
933 atop(end), atop(istart + isize),
934 atop(end), VM_FREELIST_DEFAULT);
935 }
936 } else {
937 uvm_page_physload(atop(start), atop(end),
938 atop(start), atop(end), VM_FREELIST_DEFAULT);
939 }
940 #else /* NISADMA > 0 */
941 uvm_page_physload(atop(start), atop(end),
942 atop(start), atop(end), VM_FREELIST_DEFAULT);
943 #endif /* NISADMA > 0 */
944 }
945
946 /* Initialize pmap module. */
947 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
948 }
949
950
951 /*
952 ************************************************************
953
954 Routines private to this module
955
956 ************************************************************
957 */
958
959 /* N.B. Not supposed to call printf in callback-handler! Could deadlock! */
960 static void
961 ofw_callbackhandler(void *v)
962 {
963 struct ofw_cbargs *args = v;
964 char *name = args->name;
965 int nargs = args->nargs;
966 int nreturns = args->nreturns;
967 int *args_n_results = args->args_n_results;
968
969 ofw_callbacks++;
970
971 #if defined(OFWGENCFG)
972 /* Check this first, so that we don't waste IRQ time parsing. */
973 if (strcmp(name, "tick") == 0) {
974 vaddr_t frame;
975
976 /* Check format. */
977 if (nargs != 1 || nreturns < 1) {
978 args_n_results[nargs] = -1;
979 args->nreturns = 1;
980 return;
981 }
982 args_n_results[nargs] = 0; /* properly formatted request */
983
984 /*
985 * Note that we are running in the IRQ frame, with interrupts
986 * disabled.
987 *
988 * We need to do two things here:
989 * - copy a few words out of the input frame into a global
990 * area, for later use by our real tick-handling code
991 * - patch a few words in the frame so that when OFW returns
992 * from the interrupt it will resume with our handler
993 * rather than the code that was actually interrupted.
994 * Our handler will resume when it finishes with the code
995 * that was actually interrupted.
996 *
997 * It's simplest to do this in assembler, since it requires
998 * switching frames and grovelling about with registers.
999 */
1000 frame = (vaddr_t)args_n_results[0];
1001 if (ofw_handleticks)
1002 dotickgrovelling(frame);
1003 args_n_results[nargs + 1] = frame;
1004 args->nreturns = 1;
1005 } else
1006 #endif
1007
1008 if (strcmp(name, "map") == 0) {
1009 vaddr_t va;
1010 paddr_t pa;
1011 vsize_t size;
1012 int mode;
1013 int ap_bits;
1014 int dom_bits;
1015 int cb_bits;
1016
1017 /* Check format. */
1018 if (nargs != 4 || nreturns < 2) {
1019 args_n_results[nargs] = -1;
1020 args->nreturns = 1;
1021 return;
1022 }
1023 args_n_results[nargs] = 0; /* properly formatted request */
1024
1025 pa = (paddr_t)args_n_results[0];
1026 va = (vaddr_t)args_n_results[1];
1027 size = (vsize_t)args_n_results[2];
1028 mode = args_n_results[3];
1029 ap_bits = (mode & 0x00000C00);
1030 dom_bits = (mode & 0x000001E0);
1031 cb_bits = (mode & 0x000000C0);
1032
1033 /* Sanity checks. */
1034 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
1035 (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
1036 (pa & PGOFSET) != 0 || (size & PGOFSET) != 0 ||
1037 size == 0 || (dom_bits >> 5) != 0) {
1038 args_n_results[nargs + 1] = -1;
1039 args->nreturns = 1;
1040 return;
1041 }
1042
1043 /* Write-back anything stuck in the cache. */
1044 cpu_idcache_wbinv_all();
1045
1046 /* Install new mappings. */
1047 {
1048 pt_entry_t *pte = vtopte(va);
1049 int npages = size >> PGSHIFT;
1050
1051 ap_bits >>= 10;
1052 for (; npages > 0; pte++, pa += PAGE_SIZE, npages--)
1053 *pte = (pa | L2_AP(ap_bits) | L2_TYPE_S |
1054 cb_bits);
1055 PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
1056 }
1057
1058 /* Clean out tlb. */
1059 tlb_flush();
1060
1061 args_n_results[nargs + 1] = 0;
1062 args->nreturns = 2;
1063 } else if (strcmp(name, "unmap") == 0) {
1064 vaddr_t va;
1065 vsize_t size;
1066
1067 /* Check format. */
1068 if (nargs != 2 || nreturns < 1) {
1069 args_n_results[nargs] = -1;
1070 args->nreturns = 1;
1071 return;
1072 }
1073 args_n_results[nargs] = 0; /* properly formatted request */
1074
1075 va = (vaddr_t)args_n_results[0];
1076 size = (vsize_t)args_n_results[1];
1077
1078 /* Sanity checks. */
1079 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
1080 (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
1081 (size & PGOFSET) != 0 || size == 0) {
1082 args_n_results[nargs + 1] = -1;
1083 args->nreturns = 1;
1084 return;
1085 }
1086
1087 /* Write-back anything stuck in the cache. */
1088 cpu_idcache_wbinv_all();
1089
1090 /* Zero the mappings. */
1091 {
1092 pt_entry_t *pte = vtopte(va);
1093 int npages = size >> PGSHIFT;
1094
1095 for (; npages > 0; pte++, npages--)
1096 *pte = 0;
1097 PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
1098 }
1099
1100 /* Clean out tlb. */
1101 tlb_flush();
1102
1103 args->nreturns = 1;
1104 } else if (strcmp(name, "translate") == 0) {
1105 vaddr_t va;
1106 paddr_t pa;
1107 int mode;
1108 pt_entry_t pte;
1109
1110 /* Check format. */
1111 if (nargs != 1 || nreturns < 4) {
1112 args_n_results[nargs] = -1;
1113 args->nreturns = 1;
1114 return;
1115 }
1116 args_n_results[nargs] = 0; /* properly formatted request */
1117
1118 va = (vaddr_t)args_n_results[0];
1119
1120 /* Sanity checks.
1121 * For now, I am only willing to translate va's in the
1122 * "ofw range." Eventually, I may be more generous. -JJK
1123 */
1124 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
1125 va >= (OFW_VIRT_BASE + OFW_VIRT_SIZE)) {
1126 args_n_results[nargs + 1] = -1;
1127 args->nreturns = 1;
1128 return;
1129 }
1130
1131 /* Lookup mapping. */
1132 pte = *vtopte(va);
1133 if (pte == 0) {
1134 /* No mapping. */
1135 args_n_results[nargs + 1] = -1;
1136 args->nreturns = 2;
1137 } else {
1138 /* Existing mapping. */
1139 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
1140 mode = (pte & 0x0C00) | (0 << 5) | (pte & 0x000C); /* AP | DOM | CB */
1141
1142 args_n_results[nargs + 1] = 0;
1143 args_n_results[nargs + 2] = pa;
1144 args_n_results[nargs + 3] = mode;
1145 args->nreturns = 4;
1146 }
1147 } else if (strcmp(name, "claim-phys") == 0) {
1148 struct pglist alloclist;
1149 paddr_t low, high, align;
1150 psize_t size;
1151
1152 /*
1153 * XXX
1154 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
1155 * XXX
1156 */
1157
1158 /* Check format. */
1159 if (nargs != 4 || nreturns < 3) {
1160 args_n_results[nargs] = -1;
1161 args->nreturns = 1;
1162 return;
1163 }
1164 args_n_results[nargs] = 0; /* properly formatted request */
1165
1166 low = args_n_results[0];
1167 size = args_n_results[2];
1168 align = args_n_results[3];
1169 high = args_n_results[1] + size;
1170
1171 #if 0
1172 printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n",
1173 low, size, align, high);
1174 align = size;
1175 printf("forcing align to be 0x%x\n", align);
1176 #endif
1177
1178 args_n_results[nargs + 1] =
1179 uvm_pglistalloc(size, low, high, align, 0, &alloclist, 1, 0);
1180 #if 0
1181 printf(" -> 0x%lx", args_n_results[nargs + 1]);
1182 #endif
1183 if (args_n_results[nargs + 1] != 0) {
1184 #if 0
1185 printf("(failed)\n");
1186 #endif
1187 args_n_results[nargs + 1] = -1;
1188 args->nreturns = 2;
1189 return;
1190 }
1191 args_n_results[nargs + 2] = VM_PAGE_TO_PHYS(alloclist.tqh_first);
1192 #if 0
1193 printf("(succeeded: pa = 0x%lx)\n", args_n_results[nargs + 2]);
1194 #endif
1195 args->nreturns = 3;
1196
1197 } else if (strcmp(name, "release-phys") == 0) {
1198 printf("unimplemented ofw callback - %s\n", name);
1199 args_n_results[nargs] = -1;
1200 args->nreturns = 1;
1201 } else if (strcmp(name, "claim-virt") == 0) {
1202 vaddr_t va;
1203 vsize_t size;
1204 vaddr_t align;
1205
1206 /* XXX - notyet */
1207 /* printf("unimplemented ofw callback - %s\n", name);*/
1208 args_n_results[nargs] = -1;
1209 args->nreturns = 1;
1210 return;
1211
1212 /* Check format. */
1213 if (nargs != 2 || nreturns < 3) {
1214 args_n_results[nargs] = -1;
1215 args->nreturns = 1;
1216 return;
1217 }
1218 args_n_results[nargs] = 0; /* properly formatted request */
1219
1220 /* Allocate size bytes with specified alignment. */
1221 size = (vsize_t)args_n_results[0];
1222 align = (vaddr_t)args_n_results[1];
1223 if (align % PAGE_SIZE != 0) {
1224 args_n_results[nargs + 1] = -1;
1225 args->nreturns = 2;
1226 return;
1227 }
1228
1229 if (va == 0) {
1230 /* Couldn't allocate. */
1231 args_n_results[nargs + 1] = -1;
1232 args->nreturns = 2;
1233 } else {
1234 /* Successful allocation. */
1235 args_n_results[nargs + 1] = 0;
1236 args_n_results[nargs + 2] = va;
1237 args->nreturns = 3;
1238 }
1239 } else if (strcmp(name, "release-virt") == 0) {
1240 vaddr_t va;
1241 vsize_t size;
1242
1243 /* XXX - notyet */
1244 printf("unimplemented ofw callback - %s\n", name);
1245 args_n_results[nargs] = -1;
1246 args->nreturns = 1;
1247 return;
1248
1249 /* Check format. */
1250 if (nargs != 2 || nreturns < 1) {
1251 args_n_results[nargs] = -1;
1252 args->nreturns = 1;
1253 return;
1254 }
1255 args_n_results[nargs] = 0; /* properly formatted request */
1256
1257 /* Release bytes. */
1258 va = (vaddr_t)args_n_results[0];
1259 size = (vsize_t)args_n_results[1];
1260
1261 args->nreturns = 1;
1262 } else {
1263 args_n_results[nargs] = -1;
1264 args->nreturns = 1;
1265 }
1266 }
1267
1268 static void
1269 ofw_construct_proc0_addrspace(void)
1270 {
1271 int i, oft;
1272 static pv_addr_t proc0_pt_sys;
1273 static pv_addr_t proc0_pt_kernel[KERNEL_IMG_PTS];
1274 static pv_addr_t proc0_pt_vmdata[KERNEL_VMDATA_PTS];
1275 static pv_addr_t proc0_pt_ofw[KERNEL_OFW_PTS];
1276 static pv_addr_t proc0_pt_io[KERNEL_IO_PTS];
1277 static pv_addr_t msgbuf;
1278 vaddr_t L1pagetable;
1279 struct mem_translation *tp;
1280
1281 /* Set-up the system page. */
1282 KASSERT(vector_page == 0); /* XXX for now */
1283 systempage.pv_va = ofw_claimvirt(vector_page, PAGE_SIZE, 0);
1284 if (systempage.pv_va == -1) {
1285 /* Something was already mapped to vector_page's VA. */
1286 systempage.pv_va = vector_page;
1287 systempage.pv_pa = ofw_gettranslation(vector_page);
1288 if (systempage.pv_pa == -1)
1289 panic("bogus result from gettranslation(vector_page)");
1290 } else {
1291 /* We were just allocated the page-length range at VA 0. */
1292 if (systempage.pv_va != vector_page)
1293 panic("bogus result from claimvirt(vector_page, PAGE_SIZE, 0)");
1294
1295 /* Now allocate a physical page, and establish the mapping. */
1296 systempage.pv_pa = ofw_claimphys(0, PAGE_SIZE, PAGE_SIZE);
1297 if (systempage.pv_pa == -1)
1298 panic("bogus result from claimphys(0, PAGE_SIZE, PAGE_SIZE)");
1299 ofw_settranslation(systempage.pv_va, systempage.pv_pa,
1300 PAGE_SIZE, -1); /* XXX - mode? -JJK */
1301
1302 /* Zero the memory. */
1303 memset((char *)systempage.pv_va, 0, PAGE_SIZE);
1304 }
1305
1306 /* Allocate/initialize space for the proc0, NetBSD-managed */
1307 /* page tables that we will be switching to soon. */
1308 ofw_claimpages(&virt_freeptr, &kernel_l1pt, L1_TABLE_SIZE);
1309 ofw_claimpages(&virt_freeptr, &proc0_pt_sys, L2_TABLE_SIZE);
1310 for (i = 0; i < KERNEL_IMG_PTS; i++)
1311 ofw_claimpages(&virt_freeptr, &proc0_pt_kernel[i], L2_TABLE_SIZE);
1312 for (i = 0; i < KERNEL_VMDATA_PTS; i++)
1313 ofw_claimpages(&virt_freeptr, &proc0_pt_vmdata[i], L2_TABLE_SIZE);
1314 for (i = 0; i < KERNEL_OFW_PTS; i++)
1315 ofw_claimpages(&virt_freeptr, &proc0_pt_ofw[i], L2_TABLE_SIZE);
1316 for (i = 0; i < KERNEL_IO_PTS; i++)
1317 ofw_claimpages(&virt_freeptr, &proc0_pt_io[i], L2_TABLE_SIZE);
1318
1319 /* Allocate/initialize space for stacks. */
1320 #ifndef OFWGENCFG
1321 ofw_claimpages(&virt_freeptr, &irqstack, PAGE_SIZE);
1322 #endif
1323 ofw_claimpages(&virt_freeptr, &undstack, PAGE_SIZE);
1324 ofw_claimpages(&virt_freeptr, &abtstack, PAGE_SIZE);
1325 ofw_claimpages(&virt_freeptr, &kernelstack, UPAGES * PAGE_SIZE);
1326
1327 /* Allocate/initialize space for msgbuf area. */
1328 ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE);
1329 msgbufphys = msgbuf.pv_pa;
1330
1331 /* Construct the proc0 L1 pagetable. */
1332 L1pagetable = kernel_l1pt.pv_va;
1333
1334 pmap_link_l2pt(L1pagetable, 0x0, &proc0_pt_sys);
1335 for (i = 0; i < KERNEL_IMG_PTS; i++)
1336 pmap_link_l2pt(L1pagetable, KERNEL_BASE + i * 0x00400000,
1337 &proc0_pt_kernel[i]);
1338 for (i = 0; i < KERNEL_VMDATA_PTS; i++)
1339 pmap_link_l2pt(L1pagetable, KERNEL_VM_BASE + i * 0x00400000,
1340 &proc0_pt_vmdata[i]);
1341 for (i = 0; i < KERNEL_OFW_PTS; i++)
1342 pmap_link_l2pt(L1pagetable, OFW_VIRT_BASE + i * 0x00400000,
1343 &proc0_pt_ofw[i]);
1344 for (i = 0; i < KERNEL_IO_PTS; i++)
1345 pmap_link_l2pt(L1pagetable, IO_VIRT_BASE + i * 0x00400000,
1346 &proc0_pt_io[i]);
1347
1348 /*
1349 * OK, we're done allocating.
1350 * Get a dump of OFW's translations, and make the appropriate
1351 * entries in the L2 pagetables that we just allocated.
1352 */
1353
1354 ofw_getvirttranslations();
1355
1356 for (oft = 0, tp = OFtranslations; oft < nOFtranslations;
1357 oft++, tp++) {
1358
1359 vaddr_t va;
1360 paddr_t pa;
1361 int npages = tp->size / PAGE_SIZE;
1362
1363 /* Size must be an integral number of pages. */
1364 if (npages == 0 || tp->size % PAGE_SIZE != 0)
1365 panic("illegal ofw translation (size)");
1366
1367 /* Make an entry for each page in the appropriate table. */
1368 for (va = tp->virt, pa = tp->phys; npages > 0;
1369 va += PAGE_SIZE, pa += PAGE_SIZE, npages--) {
1370 /*
1371 * Map the top bits to the appropriate L2 pagetable.
1372 * The only allowable regions are page0, the
1373 * kernel-static area, and the ofw area.
1374 */
1375 switch (va >> (L1_S_SHIFT + 2)) {
1376 case 0:
1377 /* page0 */
1378 break;
1379
1380 #if KERNEL_IMG_PTS != 2
1381 #error "Update ofw translation range list"
1382 #endif
1383 case ( KERNEL_BASE >> (L1_S_SHIFT + 2)):
1384 case ((KERNEL_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
1385 /* kernel static area */
1386 break;
1387
1388 case ( OFW_VIRT_BASE >> (L1_S_SHIFT + 2)):
1389 case ((OFW_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
1390 case ((OFW_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
1391 case ((OFW_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
1392 /* ofw area */
1393 break;
1394
1395 case ( IO_VIRT_BASE >> (L1_S_SHIFT + 2)):
1396 case ((IO_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
1397 case ((IO_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
1398 case ((IO_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
1399 /* io area */
1400 break;
1401
1402 default:
1403 /* illegal */
1404 panic("illegal ofw translation (addr) %#lx",
1405 va);
1406 }
1407
1408 /* Make the entry. */
1409 pmap_map_entry(L1pagetable, va, pa,
1410 VM_PROT_READ|VM_PROT_WRITE,
1411 (tp->mode & 0xC) == 0xC ? PTE_CACHE
1412 : PTE_NOCACHE);
1413 }
1414 }
1415
1416 /*
1417 * We don't actually want some of the mappings that we just
1418 * set up to appear in proc0's address space. In particular,
1419 * we don't want aliases to physical addresses that the kernel
1420 * has-mapped/will-map elsewhere.
1421 */
1422 ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va,
1423 msgbuf.pv_va, MSGBUFSIZE);
1424
1425 /* update the top of the kernel VM */
1426 pmap_curmaxkvaddr =
1427 KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000);
1428
1429 /*
1430 * gross hack for the sake of not thrashing the TLB and making
1431 * cache flush more efficient: blast l1 ptes for sections.
1432 */
1433 for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) {
1434 vaddr_t va = tp->virt;
1435 paddr_t pa = tp->phys;
1436
1437 if (((va | pa) & L1_S_OFFSET) == 0) {
1438 int nsections = tp->size / L1_S_SIZE;
1439
1440 while (nsections--) {
1441 /* XXXJRT prot?? */
1442 pmap_map_section(L1pagetable, va, pa,
1443 VM_PROT_READ|VM_PROT_WRITE,
1444 (tp->mode & 0xC) == 0xC ? PTE_CACHE
1445 : PTE_NOCACHE);
1446 va += L1_S_SIZE;
1447 pa += L1_S_SIZE;
1448 }
1449 }
1450 }
1451 }
1452
1453
1454 static void
1455 ofw_getphysmeminfo(void)
1456 {
1457 int phandle;
1458 int mem_len;
1459 int avail_len;
1460 int i;
1461
1462 if ((phandle = OF_finddevice("/memory")) == -1 ||
1463 (mem_len = OF_getproplen(phandle, "reg")) <= 0 ||
1464 (OFphysmem = (struct mem_region *)ofw_malloc(mem_len)) == 0 ||
1465 OF_getprop(phandle, "reg", OFphysmem, mem_len) != mem_len ||
1466 (avail_len = OF_getproplen(phandle, "available")) <= 0 ||
1467 (OFphysavail = (struct mem_region *)ofw_malloc(avail_len)) == 0 ||
1468 OF_getprop(phandle, "available", OFphysavail, avail_len)
1469 != avail_len)
1470 panic("can't get physmeminfo from OFW");
1471
1472 nOFphysmem = mem_len / sizeof(struct mem_region);
1473 nOFphysavail = avail_len / sizeof(struct mem_region);
1474
1475 /*
1476 * Sort the blocks in each array into ascending address order.
1477 * Also, page-align all blocks.
1478 */
1479 for (i = 0; i < 2; i++) {
1480 struct mem_region *tmp = (i == 0) ? OFphysmem : OFphysavail;
1481 struct mem_region *mp;
1482 int cnt = (i == 0) ? nOFphysmem : nOFphysavail;
1483 int j;
1484
1485 #ifdef OLDPRINTFS
1486 printf("ofw_getphysmeminfo: %d blocks\n", cnt);
1487 #endif
1488
1489 /* XXX - Convert all the values to host order. -JJK */
1490 for (j = 0, mp = tmp; j < cnt; j++, mp++) {
1491 mp->start = of_decode_int((unsigned char *)&mp->start);
1492 mp->size = of_decode_int((unsigned char *)&mp->size);
1493 }
1494
1495 for (j = 0, mp = tmp; j < cnt; j++, mp++) {
1496 u_int s, sz;
1497 struct mem_region *mp1;
1498
1499 /* Page-align start of the block. */
1500 s = mp->start % PAGE_SIZE;
1501 if (s != 0) {
1502 s = (PAGE_SIZE - s);
1503
1504 if (mp->size >= s) {
1505 mp->start += s;
1506 mp->size -= s;
1507 }
1508 }
1509
1510 /* Page-align the size. */
1511 mp->size -= mp->size % PAGE_SIZE;
1512
1513 /* Handle empty block. */
1514 if (mp->size == 0) {
1515 memmove(mp, mp + 1, (cnt - (mp - tmp))
1516 * sizeof(struct mem_region));
1517 cnt--;
1518 mp--;
1519 continue;
1520 }
1521
1522 /* Bubble sort. */
1523 s = mp->start;
1524 sz = mp->size;
1525 for (mp1 = tmp; mp1 < mp; mp1++)
1526 if (s < mp1->start)
1527 break;
1528 if (mp1 < mp) {
1529 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
1530 mp1->start = s;
1531 mp1->size = sz;
1532 }
1533 }
1534
1535 #ifdef OLDPRINTFS
1536 for (mp = tmp; mp->size; mp++) {
1537 printf("%x, %x\n", mp->start, mp->size);
1538 }
1539 #endif
1540 }
1541 }
1542
1543
1544 static void
1545 ofw_getvirttranslations(void)
1546 {
1547 int mmu_phandle;
1548 int mmu_ihandle;
1549 int trans_len;
1550 int over, len;
1551 int i;
1552 struct mem_translation *tp;
1553
1554 mmu_ihandle = ofw_mmu_ihandle();
1555
1556 /* overallocate to avoid increases during allocation */
1557 over = 4 * sizeof(struct mem_translation);
1558 if ((mmu_phandle = OF_instance_to_package(mmu_ihandle)) == -1 ||
1559 (len = OF_getproplen(mmu_phandle, "translations")) <= 0 ||
1560 (OFtranslations = ofw_malloc(len + over)) == 0 ||
1561 (trans_len = OF_getprop(mmu_phandle, "translations",
1562 OFtranslations, len + over)) > (len + over))
1563 panic("can't get virttranslations from OFW");
1564
1565 /* XXX - Convert all the values to host order. -JJK */
1566 nOFtranslations = trans_len / sizeof(struct mem_translation);
1567 #ifdef OLDPRINTFS
1568 printf("ofw_getvirtmeminfo: %d blocks\n", nOFtranslations);
1569 #endif
1570 for (i = 0, tp = OFtranslations; i < nOFtranslations; i++, tp++) {
1571 tp->virt = of_decode_int((unsigned char *)&tp->virt);
1572 tp->size = of_decode_int((unsigned char *)&tp->size);
1573 tp->phys = of_decode_int((unsigned char *)&tp->phys);
1574 tp->mode = of_decode_int((unsigned char *)&tp->mode);
1575 }
1576 }
1577
1578 /*
1579 * ofw_valloc: allocate blocks of VM for IO and other special purposes
1580 */
1581 typedef struct _vfree {
1582 struct _vfree *pNext;
1583 vaddr_t start;
1584 vsize_t size;
1585 } VFREE, *PVFREE;
1586
1587 static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE };
1588
1589 static PVFREE vflist = &vfinitial;
1590
1591 static vaddr_t
1592 ofw_valloc(vsize_t size, vaddr_t align)
1593 {
1594 PVFREE *ppvf;
1595 PVFREE pNew;
1596 vaddr_t new;
1597 vaddr_t lead;
1598
1599 for (ppvf = &vflist; *ppvf; ppvf = &((*ppvf)->pNext)) {
1600 if (align == 0) {
1601 new = (*ppvf)->start;
1602 lead = 0;
1603 } else {
1604 new = ((*ppvf)->start + (align - 1)) & ~(align - 1);
1605 lead = new - (*ppvf)->start;
1606 }
1607
1608 if (((*ppvf)->size - lead) >= size) {
1609 if (lead == 0) {
1610 /* using whole block */
1611 if (size == (*ppvf)->size) {
1612 /* splice out of list */
1613 (*ppvf) = (*ppvf)->pNext;
1614 } else { /* tail of block is free */
1615 (*ppvf)->start = new + size;
1616 (*ppvf)->size -= size;
1617 }
1618 } else {
1619 vsize_t tail = ((*ppvf)->start
1620 + (*ppvf)->size) - (new + size);
1621 /* free space at beginning */
1622 (*ppvf)->size = lead;
1623
1624 if (tail != 0) {
1625 /* free space at tail */
1626 pNew = ofw_malloc(sizeof(VFREE));
1627 pNew->pNext = (*ppvf)->pNext;
1628 (*ppvf)->pNext = pNew;
1629 pNew->start = new + size;
1630 pNew->size = tail;
1631 }
1632 }
1633 return new;
1634 } /* END if */
1635 } /* END for */
1636
1637 return -1;
1638 }
1639
1640 vaddr_t
1641 ofw_map(paddr_t pa, vsize_t size, int cb_bits)
1642 {
1643 vaddr_t va;
1644
1645 if ((va = ofw_valloc(size, size)) == -1)
1646 panic("cannot alloc virtual memory for %#lx", pa);
1647
1648 ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */
1649
1650 ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits);
1651
1652 return va;
1653 }
1654
1655 static int
1656 ofw_mem_ihandle(void)
1657 {
1658 static int mem_ihandle = 0;
1659 int chosen;
1660
1661 if (mem_ihandle != 0)
1662 return(mem_ihandle);
1663
1664 if ((chosen = OF_finddevice("/chosen")) == -1 ||
1665 OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0)
1666 panic("ofw_mem_ihandle");
1667
1668 mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle);
1669
1670 return(mem_ihandle);
1671 }
1672
1673
1674 static int
1675 ofw_mmu_ihandle(void)
1676 {
1677 static int mmu_ihandle = 0;
1678 int chosen;
1679
1680 if (mmu_ihandle != 0)
1681 return(mmu_ihandle);
1682
1683 if ((chosen = OF_finddevice("/chosen")) == -1 ||
1684 OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0)
1685 panic("ofw_mmu_ihandle");
1686
1687 mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle);
1688
1689 return(mmu_ihandle);
1690 }
1691
1692
1693 /* Return -1 on failure. */
1694 static paddr_t
1695 ofw_claimphys(paddr_t pa, psize_t size, paddr_t align)
1696 {
1697 int mem_ihandle = ofw_mem_ihandle();
1698
1699 /* printf("ofw_claimphys (%x, %x, %x) --> ", pa, size, align);*/
1700 if (align == 0) {
1701 /* Allocate at specified base; alignment is ignored. */
1702 pa = OF_call_method_1("claim", mem_ihandle, 3, pa, size, align);
1703 } else {
1704 /* Allocate anywhere, with specified alignment. */
1705 pa = OF_call_method_1("claim", mem_ihandle, 2, size, align);
1706 }
1707
1708 /* printf("%x\n", pa);*/
1709 return(pa);
1710 }
1711
1712
1713 #if 0
1714 /* Return -1 on failure. */
1715 static paddr_t
1716 ofw_releasephys(paddr_t pa, psize_t size)
1717 {
1718 int mem_ihandle = ofw_mem_ihandle();
1719
1720 /* printf("ofw_releasephys (%x, %x)\n", pa, size);*/
1721
1722 return (OF_call_method_1("release", mem_ihandle, 2, pa, size));
1723 }
1724 #endif
1725
1726 /* Return -1 on failure. */
1727 static vaddr_t
1728 ofw_claimvirt(vaddr_t va, vsize_t size, vaddr_t align)
1729 {
1730 int mmu_ihandle = ofw_mmu_ihandle();
1731
1732 /*printf("ofw_claimvirt (%x, %x, %x) --> ", va, size, align);*/
1733 if (align == 0) {
1734 /* Allocate at specified base; alignment is ignored. */
1735 va = OF_call_method_1("claim", mmu_ihandle, 3, va, size, align);
1736 } else {
1737 /* Allocate anywhere, with specified alignment. */
1738 va = OF_call_method_1("claim", mmu_ihandle, 2, size, align);
1739 }
1740
1741 /*printf("%x\n", va);*/
1742 return(va);
1743 }
1744
1745 /* Return -1 if no mapping. */
1746 paddr_t
1747 ofw_gettranslation(vaddr_t va)
1748 {
1749 int mmu_ihandle = ofw_mmu_ihandle();
1750 paddr_t pa;
1751 int mode;
1752 int exists;
1753
1754 #ifdef OFW_DEBUG
1755 printf("ofw_gettranslation (%x) --> ", (uint32_t)va);
1756 #endif
1757 exists = 0; /* gets set to true if translation exists */
1758 if (OF_call_method("translate", mmu_ihandle, 1, 3, va, &pa, &mode,
1759 &exists) != 0)
1760 return(-1);
1761
1762 #ifdef OFW_DEBUG
1763 printf("%d %x\n", exists, (uint32_t)pa);
1764 #endif
1765 return(exists ? pa : -1);
1766 }
1767
1768
1769 static void
1770 ofw_settranslation(vaddr_t va, paddr_t pa, vsize_t size, int mode)
1771 {
1772 int mmu_ihandle = ofw_mmu_ihandle();
1773
1774 #ifdef OFW_DEBUG
1775 printf("ofw_settranslation (%x, %x, %x, %x) --> void", (uint32_t)va,
1776 (uint32_t)pa, (uint32_t)size, (uint32_t)mode);
1777 #endif
1778 if (OF_call_method("map", mmu_ihandle, 4, 0, pa, va, size, mode) != 0)
1779 panic("ofw_settranslation failed");
1780 }
1781
1782 /*
1783 * Allocation routine used before the kernel takes over memory.
1784 * Use this for efficient storage for things that aren't rounded to
1785 * page size.
1786 *
1787 * The point here is not necessarily to be very efficient (even though
1788 * that's sort of nice), but to do proper dynamic allocation to avoid
1789 * size-limitation errors.
1790 *
1791 */
1792
1793 typedef struct _leftover {
1794 struct _leftover *pNext;
1795 vsize_t size;
1796 } LEFTOVER, *PLEFTOVER;
1797
1798 /* leftover bits of pages. first word is pointer to next.
1799 second word is size of leftover */
1800 static PLEFTOVER leftovers = NULL;
1801
1802 static void *
1803 ofw_malloc(vsize_t size)
1804 {
1805 PLEFTOVER *ppLeftover;
1806 PLEFTOVER pLeft;
1807 pv_addr_t new;
1808 vsize_t newSize, claim_size;
1809
1810 /* round and set minimum size */
1811 size = max(sizeof(LEFTOVER),
1812 ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1)));
1813
1814 for (ppLeftover = &leftovers; *ppLeftover;
1815 ppLeftover = &((*ppLeftover)->pNext))
1816 if ((*ppLeftover)->size >= size)
1817 break;
1818
1819 if (*ppLeftover) { /* have a leftover of the right size */
1820 /* remember the leftover */
1821 new.pv_va = (vaddr_t)*ppLeftover;
1822 if ((*ppLeftover)->size < (size + sizeof(LEFTOVER))) {
1823 /* splice out of chain */
1824 *ppLeftover = (*ppLeftover)->pNext;
1825 } else {
1826 /* remember the next pointer */
1827 pLeft = (*ppLeftover)->pNext;
1828 newSize = (*ppLeftover)->size - size; /* reduce size */
1829 /* move pointer */
1830 *ppLeftover = (PLEFTOVER)(((vaddr_t)*ppLeftover)
1831 + size);
1832 (*ppLeftover)->pNext = pLeft;
1833 (*ppLeftover)->size = newSize;
1834 }
1835 } else {
1836 claim_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1837 ofw_claimpages(&virt_freeptr, &new, claim_size);
1838 if ((size + sizeof(LEFTOVER)) <= claim_size) {
1839 pLeft = (PLEFTOVER)(new.pv_va + size);
1840 pLeft->pNext = leftovers;
1841 pLeft->size = claim_size - size;
1842 leftovers = pLeft;
1843 }
1844 }
1845
1846 return (void *)(new.pv_va);
1847 }
1848
1849 /*
1850 * Here is a really, really sleazy free. It's not used right now,
1851 * because it's not worth the extra complexity for just a few bytes.
1852 *
1853 */
1854 #if 0
1855 static void
1856 ofw_free(vaddr_t addr, vsize_t size)
1857 {
1858 PLEFTOVER pLeftover = (PLEFTOVER)addr;
1859
1860 /* splice right into list without checks or compaction */
1861 pLeftover->pNext = leftovers;
1862 pLeftover->size = size;
1863 leftovers = pLeftover;
1864 }
1865 #endif
1866
1867 /*
1868 * Allocate and zero round(size)/PAGE_SIZE pages of memory.
1869 * We guarantee that the allocated memory will be
1870 * aligned to a boundary equal to the smallest power of
1871 * 2 greater than or equal to size.
1872 * free_pp is an IN/OUT parameter which points to the
1873 * last allocated virtual address in an allocate-downwards
1874 * stack. pv_p is an OUT parameter which contains the
1875 * virtual and physical base addresses of the allocated
1876 * memory.
1877 */
1878 static void
1879 ofw_claimpages(vaddr_t *free_pp, pv_addr_t *pv_p, vsize_t size)
1880 {
1881 /* round-up to page boundary */
1882 vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1883 vsize_t aligned_size;
1884 vaddr_t va;
1885 paddr_t pa;
1886
1887 if (alloc_size == 0)
1888 panic("ofw_claimpages zero");
1889
1890 for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1)
1891 ;
1892
1893 /* The only way to provide the alignment guarantees is to
1894 * allocate the virtual and physical ranges separately,
1895 * then do an explicit map call.
1896 */
1897 va = (*free_pp & ~(aligned_size - 1)) - aligned_size;
1898 if (ofw_claimvirt(va, alloc_size, 0) != va)
1899 panic("ofw_claimpages va alloc");
1900 pa = ofw_claimphys(0, alloc_size, aligned_size);
1901 if (pa == -1)
1902 panic("ofw_claimpages pa alloc");
1903 /* XXX - what mode? -JJK */
1904 ofw_settranslation(va, pa, alloc_size, -1);
1905
1906 /* The memory's mapped-in now, so we can zero it. */
1907 memset((char *)va, 0, alloc_size);
1908
1909 /* Set OUT parameters. */
1910 *free_pp = va;
1911 pv_p->pv_va = va;
1912 pv_p->pv_pa = pa;
1913 }
1914
1915
1916 static void
1917 ofw_discardmappings(vaddr_t L2pagetable, vaddr_t va, vsize_t size)
1918 {
1919 /* round-up to page boundary */
1920 vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1921 int npages = alloc_size / PAGE_SIZE;
1922
1923 if (npages == 0)
1924 panic("ofw_discardmappings zero");
1925
1926 /* Discard each mapping. */
1927 for (; npages > 0; va += PAGE_SIZE, npages--) {
1928 /* Sanity. The current entry should be non-null. */
1929 if (ReadWord(L2pagetable + ((va >> 10) & 0x00000FFC)) == 0)
1930 panic("ofw_discardmappings zero entry");
1931
1932 /* Clear the entry. */
1933 WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0);
1934 }
1935 }
1936
1937
1938 static void
1939 ofw_initallocator(void)
1940 {
1941
1942 }
1943
1944 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
1945 static void
1946 reset_screen(void)
1947 {
1948
1949 if ((console_ihandle == 0) || (console_ihandle == -1))
1950 return;
1951
1952 OF_call_method("install", console_ihandle, 0, 0);
1953 }
1954 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */
1955