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