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