kernel - Add support for MosChip PCIe serial com, and console support
[dragonfly.git] / sys / platform / vkernel64 / x86_64 / autoconf.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      from: @(#)autoconf.c    7.1 (Berkeley) 5/9/91
34  * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
35  */
36
37 /*
38  * Setup the system to run on the current machine.
39  *
40  * Configure() is called at boot time and initializes the vba
41  * device tables and the memory controller monitoring.  Available
42  * devices are determined (from possibilities mentioned in ioconf.c),
43  * and the drivers are initialized.
44  */
45 #include "opt_bootp.h"
46 #include "opt_cd9660.h"
47 #include "opt_nfs.h"
48 #include "opt_nfsroot.h"
49 #include "opt_rootdevname.h"
50
51 #include "use_isa.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/bootmaj.h>
56 #include <sys/bus.h>
57 #include <sys/buf.h>
58 #include <sys/conf.h>
59 #include <sys/diskslice.h>
60 #include <sys/reboot.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/cons.h>
65 #include <sys/thread.h>
66 #include <sys/device.h>
67 #include <sys/machintr.h>
68
69 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_pager.h>
72
73 #if 0
74 #include <machine/pcb.h>
75 #include <machine/pcb_ext.h>
76 #endif
77 #include <machine/smp.h>
78 #include <machine/globaldata.h>
79 #include <machine/md_var.h>
80
81 #if NISA > 0
82 #include <bus/isa/isavar.h>
83
84 device_t isa_bus_device = NULL;
85 #endif
86
87 static void cpu_startup (void *);
88 static void configure_first (void *);
89 static void configure (void *);
90 static void configure_final (void *);
91
92 #if defined(NFS) && defined(NFS_ROOT)
93 #if !defined(BOOTP_NFSROOT)
94 static void     pxe_setup_nfsdiskless(void);
95 #endif
96 #endif
97
98 SYSINIT(cpu, SI_BOOT2_START_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
99 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
100 /* SI_ORDER_SECOND is hookable */
101 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
102 /* SI_ORDER_MIDDLE is hookable */
103 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
104
105 cdev_t  rootdev = NULL;
106 cdev_t  dumpdev = NULL;
107
108 /*
109  * nfsroot.iosize may be set in loader.conf, 32768 is recommended to
110  * be able to max-out a GigE link if the server supports it.  Many servers
111  * do not so the default is 8192.
112  *
113  * nfsroot.rahead defaults to something reasonable, can be overridden.
114  */
115 static int nfsroot_iosize = 8192;
116 TUNABLE_INT("nfsroot.iosize", &nfsroot_iosize);
117 static int nfsroot_rahead = 4;
118 TUNABLE_INT("nfsroot.rahead", &nfsroot_rahead);
119
120 /*
121  *
122  */
123 static void
124 cpu_startup(void *dummy)
125 {
126         vm_offset_t buffer_sva;
127         vm_offset_t buffer_eva;
128         vm_offset_t pager_sva;
129         vm_offset_t pager_eva;
130
131         kprintf("%s", version);
132         kprintf("real memory = %ju (%juK bytes)\n",
133             (uintmax_t)ptoa(Maxmem), (uintmax_t)(ptoa(Maxmem) / 1024));
134
135         if (nbuf == 0) {
136                 int factor = 4 * NBUFCALCSIZE / 1024;
137                 int kbytes = Maxmem * (PAGE_SIZE / 1024);
138
139                 nbuf = 50;
140                 if (kbytes > 4096)
141                         nbuf += min((kbytes - 4096) / factor, 65536 / factor);
142                 if (kbytes > 65536)
143                         nbuf += (kbytes - 65536) * 2 / (factor * 5);
144                 if (maxbcache && nbuf > maxbcache / NBUFCALCSIZE)
145                         nbuf = maxbcache / NBUFCALCSIZE;
146         }
147         if (nbuf > (virtual_end - virtual_start) / (MAXBSIZE * 2)) {
148                 nbuf = (virtual_end - virtual_start) / (MAXBSIZE * 2);
149                 kprintf("Warning: nbufs capped at %ld\n", nbuf);
150         }
151
152         nswbuf_mem = lmax(lmin(nbuf / 32, 32), 4);
153 #ifdef NSWBUF_MIN
154         if (nswbuf_mem < NSWBUF_MIN)
155                 nswbuf_mem = NSWBUF_MIN;
156 #endif
157         nswbuf_kva = lmax(lmin(nbuf / 4, 256), 16);
158 #ifdef NSWBUF_MIN
159         if (nswbuf_kva < NSWBUF_MIN)
160                 nswbuf_kva = NSWBUF_MIN;
161 #endif
162
163         /*
164          * Allocate memory for the buffer cache
165          */
166         buf = (void *)kmem_alloc(&kernel_map,
167                                  nbuf * sizeof(struct buf),
168                                  VM_SUBSYS_BUF);
169         swbuf_mem = (void *)kmem_alloc(&kernel_map,
170                                        nswbuf_mem * sizeof(struct buf),
171                                        VM_SUBSYS_BUF);
172         swbuf_kva = (void *)kmem_alloc(&kernel_map,
173                                        nswbuf_kva * sizeof(struct buf),
174                                        VM_SUBSYS_BUF);
175
176         kmem_suballoc(&kernel_map, &clean_map, &clean_sva, &clean_eva,
177                       (nbuf * MAXBSIZE * 2) +
178                       (nswbuf_mem + nswbuf_kva) *MAXPHYS +
179                       pager_map_size);
180         kmem_suballoc(&clean_map, &buffer_map, &buffer_sva, &buffer_eva,
181                       (nbuf * MAXBSIZE * 2));
182         buffer_map.system_map = 1;
183         kmem_suballoc(&clean_map, &pager_map, &pager_sva, &pager_eva,
184                       (nswbuf_mem + nswbuf_kva) *MAXPHYS +
185                       pager_map_size);
186         pager_map.system_map = 1;
187         kprintf("avail memory = %lu (%luK bytes)\n", ptoa(vmstats.v_free_count),
188                 ptoa(vmstats.v_free_count) / 1024);
189         mp_start();
190         mp_announce();
191         cpu_setregs();
192 }
193
194 /*
195  * Determine i/o configuration for a machine.
196  */
197 static void
198 configure_first(void *dummy)
199 {
200 }
201
202 static void
203 configure(void *dummy)
204 {
205         /*
206          * Final interrupt support acviation, then enable hardware interrupts.
207          */
208         MachIntrABI.finalize();
209         cpu_enable_intr();
210
211         /*
212          * This will configure all devices, generally starting with the
213          * nexus (pc64/x86_64/nexus.c).  The nexus ISA code explicitly
214          * dummies up the attach in order to delay legacy initialization
215          * until after all other busses/subsystems have had a chance
216          * at those resources.
217          */
218         root_bus_configure();
219
220 #if NISA > 0
221         /*
222          * Explicitly probe and attach ISA last.  The isa bus saves
223          * it's device node at attach time for us here.
224          */
225         if (isa_bus_device)
226                 isa_probe_children(isa_bus_device);
227 #endif
228
229         /*
230          * Allow lowering of the ipl to the lowest kernel level if we
231          * panic (or call tsleep() before clearing `cold').  No level is
232          * completely safe (since a panic may occur in a critical region
233          * at splhigh()), but we want at least bio interrupts to work.
234          */
235         safepri = TDPRI_KERN_USER;
236 }
237
238 /*
239  * Finalize configure.  Reprobe for the console, in case it was one
240  * of the devices which attached, then finish console initialization.
241  */
242 static void
243 configure_final(void *dummy)
244 {
245         cninit();
246         cninit_finish();
247
248         if (bootverbose)
249                 kprintf("Device configuration finished.\n");
250 }
251
252 #ifdef BOOTP
253 void bootpc_init(void);
254 #endif
255 /*
256  * Do legacy root filesystem discovery.
257  */
258 void
259 cpu_rootconf(void)
260 {
261 #ifdef BOOTP
262         bootpc_init();
263 #endif
264 #if defined(NFS) && defined(NFS_ROOT)
265 #if !defined(BOOTP_NFSROOT)
266         pxe_setup_nfsdiskless();
267         if (nfs_diskless_valid)
268 #endif
269                 rootdevnames[0] = "nfs:";
270 #endif
271 }
272 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL);
273
274 #if defined(NFS) && defined(NFS_ROOT)
275 #if !defined(BOOTP_NFSROOT)
276
277 #include <sys/socket.h>
278 #include <net/if.h>
279 #include <net/if_dl.h>
280 #include <net/if_types.h>
281 #include <net/if_var.h>
282 #include <net/ethernet.h>
283 #include <netinet/in.h>
284 #include <vfs/nfs/rpcv2.h>
285 #include <vfs/nfs/nfsproto.h>
286 #include <vfs/nfs/nfs.h>
287 #include <vfs/nfs/nfsdiskless.h>
288
289 extern struct nfs_diskless      nfs_diskless;
290
291 /*
292  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
293  * exist the sockaddr will remain zerod out (callers typically just check
294  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
295  */
296 static int
297 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
298 {
299         u_int32_t       a[4];
300         char            *cp;
301
302         bzero(sa, sizeof(*sa));
303
304         if ((cp = kgetenv(ev)) == NULL)
305                 return(1);
306         if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
307                 return(1);
308         if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
309                 return(1);
310         /* XXX is this ordering correct? */
311         sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
312         sa->sin_len = sizeof(*sa);
313         sa->sin_family = AF_INET;
314         return(0);
315 }
316
317 static int
318 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
319 {
320         char            *cp;
321         u_int32_t       a[6];
322
323         bzero(sa, sizeof(*sa));
324         sa->sdl_len = sizeof(*sa);
325         sa->sdl_family = AF_LINK;
326         sa->sdl_type = IFT_ETHER;
327         sa->sdl_alen = ETHER_ADDR_LEN;
328         if ((cp = kgetenv(ev)) == NULL)
329                 return(1);
330         if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
331                 return(1);
332         sa->sdl_data[0] = a[0];
333         sa->sdl_data[1] = a[1];
334         sa->sdl_data[2] = a[2];
335         sa->sdl_data[3] = a[3];
336         sa->sdl_data[4] = a[4];
337         sa->sdl_data[5] = a[5];
338         return(0);
339 }
340
341 static int
342 decode_nfshandle(char *ev, u_char *fh)
343 {
344         u_char  *cp;
345         int     len, val;
346
347         if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
348                 return(0);
349         len = 0;
350         cp++;
351         for (;;) {
352                 if (*cp == 'X')
353                         return(len);
354                 if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
355                         return(0);
356                 *(fh++) = val;
357                 len++;
358                 cp += 2;
359                 if (len > NFSX_V2FH)
360                     return(0);
361         }
362 }
363
364 /*
365  * Populate the essential fields in the nfsv3_diskless structure.
366  *
367  * The loader is expected to export the following environment variables:
368  *
369  * boot.netif.ip                IP address on boot interface
370  * boot.netif.netmask           netmask on boot interface
371  * boot.netif.gateway           default gateway (optional)
372  * boot.netif.hwaddr            hardware address of boot interface
373  * boot.netif.name              name of boot interface (instead of hw addr)
374  * boot.nfsroot.server          IP address of root filesystem server
375  * boot.nfsroot.path            path of the root filesystem on server
376  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
377  */
378 static void
379 pxe_setup_nfsdiskless(void)
380 {
381         struct nfs_diskless     *nd = &nfs_diskless;
382         struct ifnet            *ifp;
383         struct ifaddr           *ifa;
384         struct sockaddr_dl      *sdl, ourdl;
385         struct sockaddr_in      myaddr, netmask;
386         char                    *cp;
387
388         /* set up interface */
389         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
390                 return;
391         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
392                 kprintf("PXE: no netmask\n");
393                 return;
394         }
395         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
396         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
397         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
398                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
399         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
400
401         if ((cp = kgetenv("boot.netif.name")) != NULL) {
402                 ifnet_lock();
403                 ifp = ifunit(cp);
404                 if (ifp) {
405                         strlcpy(nd->myif.ifra_name, ifp->if_xname,
406                             sizeof(nd->myif.ifra_name));
407                         ifnet_unlock();
408                         goto match_done;
409                 }
410                 ifnet_unlock();
411                 kprintf("PXE: cannot find interface %s\n", cp);
412                 return;
413         }
414
415         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
416                 kprintf("PXE: no hardware address\n");
417                 return;
418         }
419         ifa = NULL;
420         ifnet_lock();
421         TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
422                 struct ifaddr_container *ifac;
423
424                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
425                         ifa = ifac->ifa;
426
427                         if ((ifa->ifa_addr->sa_family == AF_LINK) &&
428                             (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
429                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
430                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
431                                     !bcmp(sdl->sdl_data + sdl->sdl_nlen,
432                                           ourdl.sdl_data + ourdl.sdl_nlen,
433                                           sdl->sdl_alen)) {
434                                         strlcpy(nd->myif.ifra_name,
435                                             ifp->if_xname,
436                                             sizeof(nd->myif.ifra_name));
437                                         ifnet_unlock();
438                                         goto match_done;
439                                 }
440                         }
441                 }
442         }
443         ifnet_unlock();
444         kprintf("PXE: no interface\n");
445         return; /* no matching interface */
446 match_done:
447         /* set up gateway */
448         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
449
450         /* XXX set up swap? */
451
452         /* set up root mount */
453         nd->root_args.rsize = nfsroot_iosize;
454         nd->root_args.wsize = nfsroot_iosize;
455         nd->root_args.sotype = SOCK_STREAM;
456         nd->root_args.readahead = nfsroot_rahead;
457         nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT |
458                               NFSMNT_READAHEAD;
459         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
460                 kprintf("PXE: no server\n");
461                 return;
462         }
463         nd->root_saddr.sin_port = htons(NFS_PORT);
464
465         /*
466          * A tftp-only loader may pass NFS path information without a
467          * root handle.  Generate a warning but continue configuring.
468          */
469         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
470                 kprintf("PXE: Warning, no NFS handle passed from loader\n");
471         }
472         if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
473                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
474
475         nfs_diskless_valid = 1;
476 }
477
478 #endif
479 #endif