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