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