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