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