xref: /netbsd-src/sys/arch/xen/x86/autoconf.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: autoconf.c,v 1.10 2009/07/29 12:02:07 cegger Exp $	*/
2 /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
3 
4 /*-
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)autoconf.c	7.1 (Berkeley) 5/9/91
36  */
37 
38 /*
39  * Setup the system to run on the current machine.
40  *
41  * Configure() is called at boot time and initializes the vba
42  * device tables and the memory controller monitoring.  Available
43  * devices are determined (from possibilities mentioned in ioconf.c),
44  * and the drivers are initialized.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.10 2009/07/29 12:02:07 cegger Exp $");
49 
50 #include "opt_xen.h"
51 #include "opt_compat_oldboot.h"
52 #include "opt_multiprocessor.h"
53 #include "opt_nfs_boot.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/buf.h>
58 #include <sys/disklabel.h>
59 #include <sys/disk.h>
60 #include <sys/conf.h>
61 #ifdef COMPAT_OLDBOOT
62 #include <sys/reboot.h>
63 #endif
64 #include <sys/device.h>
65 #include <sys/vnode.h>
66 #include <sys/fcntl.h>
67 #include <sys/dkio.h>
68 #include <sys/proc.h>
69 #include <sys/user.h>
70 #include <sys/kauth.h>
71 
72 #ifdef NFS_BOOT_BOOTSTATIC
73 #include <net/if.h>
74 #include <net/if_ether.h>
75 #include <netinet/in.h>
76 #include <nfs/rpcv2.h>
77 #include <nfs/nfsproto.h>
78 #include <nfs/nfs.h>
79 #include <nfs/nfsmount.h>
80 #include <nfs/nfsdiskless.h>
81 #include <xen/if_xennetvar.h>
82 #endif
83 
84 #include <machine/pte.h>
85 #include <machine/cpu.h>
86 #include <machine/gdt.h>
87 #include <machine/pcb.h>
88 #include <machine/bootinfo.h>
89 
90 static void findroot(void);
91 static int is_valid_disk(device_t);
92 static void handle_wedges(device_t, int);
93 
94 struct disklist *x86_alldisks;
95 int x86_ndisks;
96 
97 #include "bios32.h"
98 #if NBIOS32 > 0
99 #include <machine/bios32.h>
100 #endif
101 
102 #include "opt_pcibios.h"
103 #ifdef PCIBIOS
104 #include <dev/pci/pcireg.h>
105 #include <dev/pci/pcivar.h>
106 #include <i386/pci/pcibios.h>
107 #endif
108 
109 #include "opt_kvm86.h"
110 #ifdef KVM86
111 #include <machine/kvm86.h>
112 #endif
113 
114 /*
115  * Determine i/o configuration for a machine.
116  */
117 void
118 cpu_configure(void)
119 {
120 
121 	startrtclock();
122 
123 #if NBIOS32 > 0 && defined(DOM0OPS)
124 	if (xendomain_is_dom0())
125 		bios32_init();
126 #endif /* NBIOS32 > 0 && DOM0OPS */
127 #ifdef PCIBIOS
128 	pcibios_init();
129 #endif
130 
131 #ifdef KVM86
132 	kvm86_init();
133 #endif
134 
135 	if (config_rootfound("mainbus", NULL) == NULL)
136 		panic("configure: mainbus not configured");
137 
138 #ifdef INTRDEBUG
139 	intr_printconfig();
140 #endif
141 
142 	/* resync cr0 after FPU configuration */
143 	lwp0.l_addr->u_pcb.pcb_cr0 = rcr0();
144 #ifdef MULTIPROCESSOR
145 	/* propagate this to the idle pcb's. */
146 	cpu_init_idle_lwps();
147 #endif
148 
149 	spl0();
150 }
151 
152 void
153 cpu_rootconf(void)
154 {
155 	findroot();
156 
157 	if (booted_wedge) {
158 		KASSERT(booted_device != NULL);
159 		printf("boot device: %s (%s)\n",
160 		    device_xname(booted_wedge), device_xname(booted_device));
161 		setroot(booted_wedge, 0);
162 	} else {
163 		printf("boot device: %s\n",
164 		    booted_device ? device_xname(booted_device) : "<unknown>");
165 		setroot(booted_device, booted_partition);
166 	}
167 }
168 
169 
170 /*
171  * Attempt to find the device from which we were booted.
172  * If we can do so, and not instructed not to do so,
173  * change rootdev to correspond to the load device.
174  */
175 void
176 findroot(void)
177 {
178 	device_t dv;
179 	union xen_cmdline_parseinfo xcp;
180 
181 	if (booted_device)
182 		return;
183 
184 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
185 
186 	TAILQ_FOREACH(dv, &alldevs, dv_list) {
187 		bool is_ifnet, is_disk;
188 		const char *devname;
189 
190 		is_ifnet = (device_class(dv) == DV_IFNET);
191 		is_disk = is_valid_disk(dv);
192 		devname = device_xname(dv);
193 
194 		if (!is_ifnet && !is_disk)
195 			continue;
196 
197 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
198 			handle_wedges(dv, 0);
199 			break;
200 		}
201 
202 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
203 			continue;
204 
205 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
206 			booted_partition = toupper(
207 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
208 		}
209 
210 		booted_device = dv;
211 		break;
212 	}
213 }
214 
215 #include "pci.h"
216 
217 #include <dev/isa/isavar.h>
218 #if NPCI > 0
219 #include <dev/pci/pcivar.h>
220 #endif
221 
222 
223 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
224 static int
225 dom0_bootstatic_callback(struct nfs_diskless *nd)
226 {
227 #if 0
228 	struct ifnet *ifp = nd->nd_ifp;
229 #endif
230 	int flags = 0;
231 	union xen_cmdline_parseinfo xcp;
232 	struct sockaddr_in *sin;
233 
234 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
235 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
236 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
237 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
238 
239 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
240 		flags |= NFS_BOOT_HAS_SERVER;
241 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
242 			flags |= NFS_BOOT_HAS_ROOTPATH;
243 	}
244 
245 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
246 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
247 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
248 
249 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
250 	memset((void *)sin, 0, sizeof(*sin));
251 	sin->sin_len = sizeof(*sin);
252 	sin->sin_family = AF_INET;
253 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
254 
255 	if (nd->nd_myip.s_addr)
256 		flags |= NFS_BOOT_HAS_MYIP;
257 	if (nd->nd_gwip.s_addr)
258 		flags |= NFS_BOOT_HAS_GWIP;
259 	if (nd->nd_mask.s_addr)
260 		flags |= NFS_BOOT_HAS_MASK;
261 	if (sin->sin_addr.s_addr)
262 		flags |= NFS_BOOT_HAS_SERVADDR;
263 
264 	return flags;
265 }
266 #endif
267 
268 void
269 device_register(device_t dev, void *aux)
270 {
271 	/*
272 	 * Handle network interfaces here, the attachment information is
273 	 * not available driver independently later.
274 	 * For disks, there is nothing useful available at attach time.
275 	 */
276 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
277 	if (device_class(dev) == DV_IFNET) {
278 		union xen_cmdline_parseinfo xcp;
279 
280 #ifdef NFS_BOOT_BOOTSTATIC
281 #ifdef DOM0OPS
282 		if (xendomain_is_privileged()) {
283 			nfs_bootstatic_callback = dom0_bootstatic_callback;
284 		} else
285 #endif
286 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
287 		nfs_bootstatic_callback = xennet_bootstatic_callback;
288 #endif
289 #endif
290 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
291 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
292 		    sizeof(xcp.xcp_bootdev)) == 0)
293 		{
294 			goto found;
295 		}
296 	}
297 #endif
298 	if (device_class(dev) == DV_IFNET) {
299 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
300 		if (bin == NULL)
301 			return;
302 
303 		/*
304 		 * We don't check the driver name against the device name
305 		 * passed by the boot ROM. The ROM should stay usable
306 		 * if the driver gets obsoleted.
307 		 * The physical attachment information (checked below)
308 		 * must be sufficient to identify the device.
309 		 */
310 
311 		if (bin->bus == BI_BUS_ISA &&
312 		    device_is_a(device_parent(dev), "isa")) {
313 			struct isa_attach_args *iaa = aux;
314 
315 			/* compare IO base address */
316 			/* XXXJRT what about multiple I/O addrs? */
317 			if (iaa->ia_nio > 0 &&
318 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
319 				goto found;
320 		}
321 #if NPCI > 0
322 		if (bin->bus == BI_BUS_PCI &&
323 		    device_is_a(device_parent(dev), "pci")) {
324 			struct pci_attach_args *paa = aux;
325 			int b, d, f;
326 
327 			/*
328 			 * Calculate BIOS representation of:
329 			 *
330 			 *	<bus,device,function>
331 			 *
332 			 * and compare.
333 			 */
334 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
335 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
336 				goto found;
337 		}
338 #endif
339 	}
340 	return;
341 
342 found:
343 	if (booted_device) {
344 		/* XXX should be a "panic()" */
345 		printf("warning: double match for boot device (%s, %s)\n",
346 		    device_xname(booted_device), device_xname(dev));
347 		return;
348 	}
349 	booted_device = dev;
350 }
351 
352 static void
353 handle_wedges(device_t dv, int par)
354 {
355 	if (config_handle_wedges(dv, par) == 0)
356 		return;
357 	booted_device = dv;
358 	booted_partition = par;
359 }
360 
361 static int
362 is_valid_disk(device_t dv)
363 {
364 
365 	if (device_class(dv) != DV_DISK)
366 		return (0);
367 
368 	return (device_is_a(dv, "dk") ||
369 		device_is_a(dv, "sd") ||
370 		device_is_a(dv, "wd") ||
371 		device_is_a(dv, "ld") ||
372 		device_is_a(dv, "ed") ||
373 		device_is_a(dv, "xbd"));
374 }
375