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