xref: /netbsd-src/sys/arch/xen/x86/autoconf.c (revision 4a96dd4fc2b02adf9af17780aaa72447a29ac609)
1 /*	$NetBSD: autoconf.c,v 1.26 2023/10/17 12:07:42 bouyer 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.26 2023/10/17 12:07:42 bouyer 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 #include <x86/autoconf.h>
86 
87 struct disklist *x86_alldisks;
88 int x86_ndisks;
89 int x86_found_console;
90 
91 #include "bios32.h"
92 #if NBIOS32 > 0
93 #include <machine/bios32.h>
94 /* XXX */
95 extern void platform_init(void);
96 #endif
97 
98 #include "opt_pcibios.h"
99 #ifdef PCIBIOS
100 #include <dev/pci/pcireg.h>
101 #include <dev/pci/pcivar.h>
102 #include <i386/pci/pcibios.h>
103 #endif
104 
105 #ifdef DEBUG_GEOM
106 #define DPRINTF(a) printf a
107 #else
108 #define DPRINTF(a)
109 #endif
110 
111 /*
112  * Determine i/o configuration for a machine.
113  */
114 void
cpu_configure(void)115 cpu_configure(void)
116 {
117 	struct pcb *pcb;
118 
119 	xen_startrtclock();
120 
121 #if defined(DOM0OPS)
122 	if (xendomain_is_dom0()) {
123 #if NBIOS32 > 0
124 		bios32_init();
125 		platform_init();
126 		/* identify hypervisor type from SMBIOS */
127 		identify_hypervisor();
128 #endif /* NBIOS32 > 0 */
129 	}
130 #endif /* DOM0OPS */
131 #ifdef PCIBIOS
132 	pcibios_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 #if NIOAPIC > 0
143 	ioapic_enable();
144 #endif
145 	/* resync cr0 after FPU configuration */
146 	pcb = lwp_getpcb(&lwp0);
147 	pcb->pcb_cr0 = rcr0();
148 #ifdef MULTIPROCESSOR
149 	/* propagate this to the idle pcb's. */
150 	cpu_init_idle_lwps();
151 #endif
152 
153 	spl0();
154 }
155 
156 void
cpu_rootconf(void)157 cpu_rootconf(void)
158 {
159 	cpu_bootconf();
160 
161 	printf("boot device: %s\n",
162 	    booted_device ? device_xname(booted_device) :
163 	    bootspec ? bootspec : "<unknown>");
164 	rootconf();
165 }
166 
167 
168 /*
169  * Attempt to find the device from which we were booted.
170  */
171 void
cpu_bootconf(void)172 cpu_bootconf(void)
173 {
174 	xen_bootconf();
175 }
176 #include "pci.h"
177 
178 #include <dev/isa/isavar.h>
179 #if NPCI > 0
180 #include <dev/pci/pcivar.h>
181 #endif
182 
183 
184 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
185 static int
dom0_bootstatic_callback(struct nfs_diskless * nd)186 dom0_bootstatic_callback(struct nfs_diskless *nd)
187 {
188 #if 0
189 	struct ifnet *ifp = nd->nd_ifp;
190 #endif
191 	int flags = 0;
192 	union xen_cmdline_parseinfo xcp;
193 	struct sockaddr_in *sin;
194 
195 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
196 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
197 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
198 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
199 
200 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
201 		flags |= NFS_BOOT_HAS_SERVER;
202 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
203 			flags |= NFS_BOOT_HAS_ROOTPATH;
204 	}
205 
206 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
207 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
208 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
209 
210 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
211 	memset((void *)sin, 0, sizeof(*sin));
212 	sin->sin_len = sizeof(*sin);
213 	sin->sin_family = AF_INET;
214 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
215 
216 	if (nd->nd_myip.s_addr)
217 		flags |= NFS_BOOT_HAS_MYIP;
218 	if (nd->nd_gwip.s_addr)
219 		flags |= NFS_BOOT_HAS_GWIP;
220 	if (nd->nd_mask.s_addr)
221 		flags |= NFS_BOOT_HAS_MASK;
222 	if (sin->sin_addr.s_addr)
223 		flags |= NFS_BOOT_HAS_SERVADDR;
224 
225 	return flags;
226 }
227 #endif
228 
229 void
device_register(device_t dev,void * aux)230 device_register(device_t dev, void *aux)
231 {
232 	device_t isaboot, pciboot;
233 	device_t found = NULL;
234 
235 	isaboot = device_isa_register(dev, aux);
236 	pciboot = device_pci_register(dev, aux);
237 
238 	/*
239 	 * Handle network interfaces here, the attachment information is
240 	 * not available driver independently later.
241 	 * For disks, there is nothing useful available at attach time.
242 	 */
243 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
244 	if (device_class(dev) == DV_IFNET) {
245 		union xen_cmdline_parseinfo xcp;
246 
247 #ifdef NFS_BOOT_BOOTSTATIC
248 #ifdef DOM0OPS
249 		if (xendomain_is_privileged()) {
250 			nfs_bootstatic_callback = dom0_bootstatic_callback;
251 		} else
252 #endif
253 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
254 		nfs_bootstatic_callback = xennet_bootstatic_callback;
255 #endif
256 #endif
257 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
258 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
259 		    sizeof(xcp.xcp_bootdev)) == 0)
260 		{
261 			found = dev;
262 		}
263 	}
264 #endif
265 	if (found == NULL) {
266 		if (isaboot != NULL)
267 			found = isaboot;
268 		else if (pciboot != NULL)
269 			found = pciboot;
270 	}
271 
272 	if (found) {
273 		if (booted_device) {
274 			/* XXX should be a "panic()" */
275 			printf("warning: double match for boot device (%s, %s)\n",
276 			    device_xname(booted_device), device_xname(dev));
277 			return;
278 		}
279 		booted_device = found;
280 		booted_method = "device/register";
281 	}
282 }
283