Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / alpha / common / main.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998 Doug Rabson <dfr@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/boot/alpha/common/main.c,v 1.13 2000/10/25 23:36:01 dfr Exp $
28  * $DragonFly: src/sys/boot/alpha/common/Attic/main.c,v 1.3 2003/11/10 06:08:29 dillon Exp $
29  */
30
31
32 #include <stand.h>
33 #include <string.h>
34
35 #include <sys/param.h>
36 #include <machine/rpb.h>
37 #include <machine/prom.h>
38 #include "bootstrap.h"
39 #include "libalpha/libalpha.h"
40
41 extern  char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
42
43 struct alpha_devdesc    currdev;        /* our current device */
44 struct arch_switch      archsw;         /* MI/MD interface boundary */
45
46 extern char end[];
47 extern void OSFpal(void);
48 extern void halt(void);
49
50 unsigned long
51 memsize()
52 {
53     struct rpb *hwrpb = (struct rpb *)HWRPB_ADDR;
54     struct mddt *mddtp;
55     struct mddt_cluster *memc;
56     int i;
57     unsigned long total = 0;
58
59     mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
60     for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
61         memc = &mddtp->mddt_clusters[i];
62         total += memc->mddt_pg_cnt << PAGE_SHIFT;
63     }
64     return total;
65 }
66
67 /* #define      XTRA_PAGES      32 */
68 #define XTRA_PAGES      64
69
70 void
71 extend_heap(void)
72 {
73     struct rpb *hwrpb = (struct rpb *)HWRPB_ADDR;
74     struct mddt *mddtp;
75     struct mddt_cluster *memc = 0;
76     int i;
77     unsigned long startpfn;
78     vm_offset_t startva;
79     vm_offset_t startpte;
80
81     /*
82      * Find the last usable memory cluster and add some of its pages
83      * to our address space.  The 256k allowed by the firmware isn't quite
84      * adequate for our needs.
85      */
86     mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
87     for (i = mddtp->mddt_cluster_cnt - 1; i >= 0; i--) {
88         memc = &mddtp->mddt_clusters[i];
89         if (!(memc->mddt_usage & (MDDT_NONVOLATILE | MDDT_PALCODE)))
90             break;
91     }
92
93     /*
94      * We want to extend the heap from 256k up to XTRA_PAGES more pages.
95      * We take pages from the end of the last usable memory region,
96      * taking care to avoid the memory used by the kernel's message
97      * buffer.  We allow 4 pages for the message buffer.
98      */
99     startpfn = memc->mddt_pfn + memc->mddt_pg_cnt - 4 - XTRA_PAGES;
100     startva = 0x20040000;
101     startpte = 0x40000000
102         + (((startva >> 23) & 0x3ff) << PAGE_SHIFT)
103         + (((startva >> 13) & 0x3ff) << 3);
104
105     for (i = 0; i < XTRA_PAGES; i++) {
106         u_int64_t pte;
107         pte = ((startpfn + i) << 32) | 0x1101;
108         *(u_int64_t *) (startpte + 8 * i) = pte;
109     }
110 }
111
112 int
113 main(void)
114 {
115     int         i;
116     char        bootfile[128];
117     
118     /* 
119      * Initialise the heap as early as possible.  Once this is done,
120      * alloc() is usable. The stack is buried inside us, so this is
121      * safe.
122      */
123     setheap((void *)end, (void *)(0x20040000 + XTRA_PAGES * 8192));
124
125 #ifdef  LOADER
126     /*
127      * If this is the two stage disk loader, add the memory used by
128      * the first stage to the heap.
129      */
130     free_region((void *)PRIMARY_LOAD_ADDRESS,
131                 (void *)SECONDARY_LOAD_ADDRESS);
132 #endif
133
134     /* 
135      * XXX Chicken-and-egg problem; we want to have console output
136      * early, but some console attributes may depend on reading from
137      * eg. the boot device, which we can't do yet.  We can use
138      * printf() etc. once this is done.
139      */
140     cons_probe();
141
142     /* switch to OSF pal code. */
143     OSFpal();
144
145     /*
146      * Initialise the block cache
147      */
148     bcache_init(32, 512);       /* 16k XXX tune this */
149
150     /*
151      * March through the device switch probing for things.
152      */
153     for (i = 0; devsw[i] != NULL; i++)
154         if (devsw[i]->dv_init != NULL)
155             (devsw[i]->dv_init)();
156
157     printf("\n");
158     printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
159     printf("(%s, %s)\n", bootprog_maker, bootprog_date);
160     printf("Memory: %ld k\n", memsize() / 1024);
161     
162     /* We're booting from an SRM disk, try to spiff this */
163     currdev.d_dev = devsw[0];                           /* XXX presumes that biosdisk is first in devsw */
164     currdev.d_type = currdev.d_dev->dv_type;
165     currdev.d_kind.srmdisk.unit = 0;
166     currdev.d_kind.srmdisk.slice = -1;                  /* XXX should be able to detect this, default to autoprobe */
167     currdev.d_kind.srmdisk.partition = 0;               /* default to 'a' */
168
169     /* Create alpha-specific variables */
170     prom_getenv(PROM_E_BOOTED_FILE, bootfile, sizeof(bootfile));
171     if (bootfile[0])
172         setenv("bootfile", bootfile, 1);
173     env_setenv("currdev", EV_VOLATILE, alpha_fmtdev(&currdev), alpha_setcurrdev, env_nounset);
174     env_setenv("loaddev", EV_VOLATILE,  alpha_fmtdev(&currdev), env_noset, env_nounset);
175     setenv("LINES", "24", 1);                           /* optional */
176     
177     archsw.arch_autoload = alpha_autoload;
178     archsw.arch_getdev = alpha_getdev;
179     archsw.arch_copyin = alpha_copyin;
180     archsw.arch_copyout = alpha_copyout;
181     archsw.arch_readin = alpha_readin;
182
183     /*
184      * SRM firmware takes *ages* to open the disk device.  We hold it
185      * open until the closeall() when we exec the kernel.  Note that
186      * we must close it eventually since otherwise the firmware leaves
187      * the ncr hardware in a broken state (at least it does on my EB164).
188      */
189     open("/boot", O_RDONLY);
190
191     interact();                 /* doesn't return */
192
193     return 0;
194 }
195
196 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
197
198 static int
199 command_reboot(int argc, char *argv[])
200 {
201
202     printf("Rebooting...\n");
203     delay(1000000);
204     reboot();
205     /* Note: we shouldn't get to this point! */
206     panic("Reboot failed!");
207     exit(0);
208 }
209
210 COMMAND_SET(halt, "halt", "halt the system", command_halt);
211
212 static int
213 command_halt(int argc, char *argv[])
214 {
215     halt();    /* never returns */
216     return(CMD_OK);
217 }
218
219 #if 0
220
221 COMMAND_SET(stack, "stack", "show stack usage", command_stack);
222
223 static int
224 command_stack(int argc, char *argv[])
225 {
226     char        *cp;
227
228     for (cp = &stackbase; cp < &stacktop; cp++)
229         if (*cp != 0)
230             break;
231     
232     printf("%d bytes of stack used\n", &stacktop - cp);
233     return(CMD_OK);
234 }
235
236 #endif
237
238 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
239
240 static int
241 command_heap(int argc, char *argv[])
242 {
243     printf("heap base at %p, top at %p, used %ld\n", end, sbrk(0), sbrk(0) - end);
244     return(CMD_OK);
245 }