xref: /netbsd-src/sys/arch/xen/x86/autoconf.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: autoconf.c,v 1.19 2017/07/29 06:29:32 maxv 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.19 2017/07/29 06:29:32 maxv Exp $");
49 
50 #include "opt_xen.h"
51 #include "opt_multiprocessor.h"
52 #include "opt_nfs_boot.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/buf.h>
57 #include <sys/disklabel.h>
58 #include <sys/disk.h>
59 #include <sys/conf.h>
60 #include <sys/device.h>
61 #include <sys/vnode.h>
62 #include <sys/fcntl.h>
63 #include <sys/dkio.h>
64 #include <sys/proc.h>
65 #include <sys/kauth.h>
66 
67 #ifdef NFS_BOOT_BOOTSTATIC
68 #include <net/if.h>
69 #include <net/if_ether.h>
70 #include <netinet/in.h>
71 #include <nfs/rpcv2.h>
72 #include <nfs/nfsproto.h>
73 #include <nfs/nfs.h>
74 #include <nfs/nfsmount.h>
75 #include <nfs/nfsdiskless.h>
76 #include <xen/if_xennetvar.h>
77 #endif
78 
79 #include <machine/pte.h>
80 #include <machine/cpu.h>
81 #include <machine/gdt.h>
82 #include <machine/pcb.h>
83 #include <machine/bootinfo.h>
84 
85 static int is_valid_disk(device_t);
86 
87 struct disklist *x86_alldisks;
88 int x86_ndisks;
89 
90 #include "bios32.h"
91 #if NBIOS32 > 0
92 #include <machine/bios32.h>
93 /* XXX */
94 extern void platform_init(void);
95 #endif
96 
97 #include "opt_pcibios.h"
98 #ifdef PCIBIOS
99 #include <dev/pci/pcireg.h>
100 #include <dev/pci/pcivar.h>
101 #include <i386/pci/pcibios.h>
102 #endif
103 
104 /*
105  * Determine i/o configuration for a machine.
106  */
107 void
108 cpu_configure(void)
109 {
110 	struct pcb *pcb;
111 
112 	startrtclock();
113 
114 #if defined(DOM0OPS)
115 	if (xendomain_is_dom0()) {
116 #if NBIOS32 > 0
117 		bios32_init();
118 		platform_init();
119 		/* identify hypervisor type from SMBIOS */
120 		identify_hypervisor();
121 #endif /* NBIOS32 > 0 */
122 	} else
123 #endif /* DOM0OPS */
124 		vm_guest = VM_GUEST_XEN;
125 #ifdef PCIBIOS
126 	pcibios_init();
127 #endif
128 
129 	if (config_rootfound("mainbus", NULL) == NULL)
130 		panic("configure: mainbus not configured");
131 
132 #ifdef INTRDEBUG
133 	intr_printconfig();
134 #endif
135 
136 	/* resync cr0 after FPU configuration */
137 	pcb = lwp_getpcb(&lwp0);
138 	pcb->pcb_cr0 = rcr0();
139 #ifdef MULTIPROCESSOR
140 	/* propagate this to the idle pcb's. */
141 	cpu_init_idle_lwps();
142 #endif
143 
144 	spl0();
145 }
146 
147 void
148 cpu_rootconf(void)
149 {
150 	cpu_bootconf();
151 
152 	printf("boot device: %s\n",
153 	    booted_device ? device_xname(booted_device) : "<unknown>");
154 	rootconf();
155 }
156 
157 
158 /*
159  * Attempt to find the device from which we were booted.
160  */
161 void
162 cpu_bootconf(void)
163 {
164 	device_t dv;
165 	deviter_t di;
166 	union xen_cmdline_parseinfo xcp;
167 
168 	if (booted_device)
169 		return;
170 
171 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
172 
173 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
174 	     dv != NULL;
175 	     dv = deviter_next(&di)) {
176 		bool is_ifnet, is_disk;
177 		const char *devname;
178 
179 		is_ifnet = (device_class(dv) == DV_IFNET);
180 		is_disk = is_valid_disk(dv);
181 		devname = device_xname(dv);
182 
183 		if (!is_ifnet && !is_disk)
184 			continue;
185 
186 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
187 			booted_device = dv;
188 			break;
189 		}
190 
191 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
192 			continue;
193 
194 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
195 			booted_partition = toupper(
196 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
197 		}
198 
199 		booted_device = dv;
200 		break;
201 	}
202 	deviter_release(&di);
203 }
204 
205 #include "pci.h"
206 
207 #include <dev/isa/isavar.h>
208 #if NPCI > 0
209 #include <dev/pci/pcivar.h>
210 #endif
211 
212 
213 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
214 static int
215 dom0_bootstatic_callback(struct nfs_diskless *nd)
216 {
217 #if 0
218 	struct ifnet *ifp = nd->nd_ifp;
219 #endif
220 	int flags = 0;
221 	union xen_cmdline_parseinfo xcp;
222 	struct sockaddr_in *sin;
223 
224 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
225 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
226 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
227 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
228 
229 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
230 		flags |= NFS_BOOT_HAS_SERVER;
231 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
232 			flags |= NFS_BOOT_HAS_ROOTPATH;
233 	}
234 
235 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
236 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
237 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
238 
239 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
240 	memset((void *)sin, 0, sizeof(*sin));
241 	sin->sin_len = sizeof(*sin);
242 	sin->sin_family = AF_INET;
243 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
244 
245 	if (nd->nd_myip.s_addr)
246 		flags |= NFS_BOOT_HAS_MYIP;
247 	if (nd->nd_gwip.s_addr)
248 		flags |= NFS_BOOT_HAS_GWIP;
249 	if (nd->nd_mask.s_addr)
250 		flags |= NFS_BOOT_HAS_MASK;
251 	if (sin->sin_addr.s_addr)
252 		flags |= NFS_BOOT_HAS_SERVADDR;
253 
254 	return flags;
255 }
256 #endif
257 
258 void
259 device_register(device_t dev, void *aux)
260 {
261 	/*
262 	 * Handle network interfaces here, the attachment information is
263 	 * not available driver independently later.
264 	 * For disks, there is nothing useful available at attach time.
265 	 */
266 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
267 	if (device_class(dev) == DV_IFNET) {
268 		union xen_cmdline_parseinfo xcp;
269 
270 #ifdef NFS_BOOT_BOOTSTATIC
271 #ifdef DOM0OPS
272 		if (xendomain_is_privileged()) {
273 			nfs_bootstatic_callback = dom0_bootstatic_callback;
274 		} else
275 #endif
276 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
277 		nfs_bootstatic_callback = xennet_bootstatic_callback;
278 #endif
279 #endif
280 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
281 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
282 		    sizeof(xcp.xcp_bootdev)) == 0)
283 		{
284 			goto found;
285 		}
286 	}
287 #endif
288 	if (device_class(dev) == DV_IFNET) {
289 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
290 		if (bin == NULL)
291 			return;
292 
293 		/*
294 		 * We don't check the driver name against the device name
295 		 * passed by the boot ROM. The ROM should stay usable
296 		 * if the driver gets obsoleted.
297 		 * The physical attachment information (checked below)
298 		 * must be sufficient to identify the device.
299 		 */
300 
301 		if (bin->bus == BI_BUS_ISA &&
302 		    device_is_a(device_parent(dev), "isa")) {
303 			struct isa_attach_args *iaa = aux;
304 
305 			/* compare IO base address */
306 			/* XXXJRT what about multiple I/O addrs? */
307 			if (iaa->ia_nio > 0 &&
308 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
309 				goto found;
310 		}
311 #if NPCI > 0
312 		if (bin->bus == BI_BUS_PCI &&
313 		    device_is_a(device_parent(dev), "pci")) {
314 			struct pci_attach_args *paa = aux;
315 			int b, d, f;
316 
317 			/*
318 			 * Calculate BIOS representation of:
319 			 *
320 			 *	<bus,device,function>
321 			 *
322 			 * and compare.
323 			 */
324 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
325 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
326 				goto found;
327 		}
328 #endif
329 	}
330 	return;
331 
332 found:
333 	if (booted_device) {
334 		/* XXX should be a "panic()" */
335 		printf("warning: double match for boot device (%s, %s)\n",
336 		    device_xname(booted_device), device_xname(dev));
337 		return;
338 	}
339 	booted_device = dev;
340 }
341 
342 static int
343 is_valid_disk(device_t dv)
344 {
345 
346 	if (device_class(dv) != DV_DISK)
347 		return (0);
348 
349 	return (device_is_a(dv, "dk") ||
350 		device_is_a(dv, "sd") ||
351 		device_is_a(dv, "wd") ||
352 		device_is_a(dv, "ld") ||
353 		device_is_a(dv, "ed") ||
354 		device_is_a(dv, "xbd"));
355 }
356