xref: /openbsd/sys/arch/i386/i386/autoconf.c (revision 09467b48)
1 /*	$OpenBSD: autoconf.c,v 1.106 2019/12/08 12:25:30 mpi Exp $	*/
2 /*	$NetBSD: autoconf.c,v 1.20 1996/05/03 19:41:56 christos 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  * cpu_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 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/user.h>
50 #include <sys/buf.h>
51 #include <sys/disklabel.h>
52 #include <sys/conf.h>
53 #include <sys/reboot.h>
54 #include <sys/device.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/hibernate.h>
58 
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 
64 #include <uvm/uvm_extern.h>
65 
66 #include <machine/pte.h>
67 #include <machine/cpu.h>
68 #include <machine/gdt.h>
69 #include <machine/biosvar.h>
70 
71 #include <dev/cons.h>
72 
73 #include "ioapic.h"
74 
75 #include "acpi.h"
76 
77 #if NIOAPIC > 0
78 #include <machine/i82093var.h>
79 #endif
80 
81 #if NACPI > 0
82 #include <dev/acpi/acpivar.h>
83 #endif
84 
85 #ifdef MULTIPROCESSOR
86 #include <machine/mpbiosvar.h>
87 #endif
88 
89 /*
90  * The following several variables are related to
91  * the configuration process, and are used in initializing
92  * the machine.
93  */
94 extern dev_t bootdev;
95 
96 /* Support for VIA C3 RNG */
97 extern struct timeout viac3_rnd_tmo;
98 extern int	viac3_rnd_present;
99 void		viac3_rnd(void *);
100 
101 extern struct timeout rdrand_tmo;
102 extern int	has_rdrand;
103 extern int	has_rdseed;
104 void		rdrand(void *);
105 
106 #ifdef CRYPTO
107 void		viac3_crypto_setup(void);
108 extern int	i386_has_xcrypt;
109 #endif
110 
111 void
112 unmap_startup(void)
113 {
114 	extern int kernel_text[], endboot[];
115 	vaddr_t p = (vaddr_t)kernel_text;
116 
117 	do {
118 		pmap_kremove(p, PAGE_SIZE);
119 		p += PAGE_SIZE;
120 	} while (p < (vaddr_t)endboot);
121 }
122 
123 /*
124  * Determine i/o configuration for a machine.
125  */
126 void
127 cpu_configure(void)
128 {
129 	/*
130 	 * Note, on i386, configure is not running under splhigh unlike other
131 	 * architectures.  This fact is used by the pcmcia irq line probing.
132 	 */
133 
134 	gdt_init();		/* XXX - pcibios uses gdt stuff */
135 
136 	/* Set up proc0's TSS */
137 	i386_proc0_tss_init();
138 
139 	pmap_bootstrap_pae();
140 
141 #if defined(MULTIPROCESSOR) || \
142     (NACPI > 0 && !defined(SMALL_KERNEL))
143 	/* install the lowmem ptp after boot args for 1:1 mappings */
144 	pmap_prealloc_lowmem_ptp();
145 #endif
146 
147 #ifdef MULTIPROCESSOR
148 	pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE,		/* virtual */
149 	    (paddr_t)MP_TRAMPOLINE,			/* physical */
150 	    PROT_READ | PROT_WRITE | PROT_EXEC);	/* protection */
151 	pmap_kenter_pa((vaddr_t)MP_TRAMP_DATA,		/* virtual */
152 	    (paddr_t)MP_TRAMP_DATA,			/* physical */
153 	    PROT_READ | PROT_WRITE);			/* protection */
154 #endif
155 
156 	if (config_rootfound("mainbus", NULL) == NULL)
157 		panic("cpu_configure: mainbus not configured");
158 
159 #if NIOAPIC > 0
160 	ioapic_enable();
161 #endif
162 
163 	proc0.p_addr->u_pcb.pcb_cr0 = rcr0();
164 
165 	unmap_startup();
166 
167 #ifdef MULTIPROCESSOR
168 	/* propagate TSS configuration to the idle pcb's. */
169 	cpu_init_idle_pcbs();
170 #endif
171 	spl0();
172 
173 	/*
174 	 * We can not know which is our root disk, defer
175 	 * until we can checksum blocks to figure it out.
176 	 */
177 	cold = 0;
178 
179 
180 	/*
181 	 * At this point the RNG is running, and if FSXR is set we can
182 	 * use it.  Here we setup a periodic timeout to collect the data.
183 	 */
184 	if (viac3_rnd_present) {
185 		timeout_set(&viac3_rnd_tmo, viac3_rnd, &viac3_rnd_tmo);
186 		viac3_rnd(&viac3_rnd_tmo);
187 	}
188 	if (has_rdrand || has_rdseed) {
189 		timeout_set(&rdrand_tmo, rdrand, &rdrand_tmo);
190 		rdrand(&rdrand_tmo);
191 	}
192 
193 #ifdef CRYPTO
194 	/*
195 	 * Also, if the chip has crypto available, enable it.
196 	 */
197 	if (i386_has_xcrypt)
198 		viac3_crypto_setup();
199 #endif
200 }
201 
202 void
203 device_register(struct device *dev, void *aux)
204 {
205 }
206 
207 /*
208  * Now that we are fully operational, we can checksum the
209  * disks, and using some heuristics, hopefully are able to
210  * always determine the correct root disk.
211  */
212 void
213 diskconf(void)
214 {
215 	int majdev, unit, part = 0;
216 	struct device *bootdv = NULL;
217 	dev_t tmpdev;
218 	char buf[128];
219 	extern bios_bootmac_t *bios_bootmac;
220 
221 	dkcsumattach();
222 
223 	if ((bootdev & B_MAGICMASK) == (u_int)B_DEVMAGIC) {
224 		majdev = B_TYPE(bootdev);
225 		unit = B_UNIT(bootdev);
226 		part = B_PARTITION(bootdev);
227 		snprintf(buf, sizeof buf, "%s%d%c", findblkname(majdev),
228 		    unit, part + 'a');
229 		bootdv = parsedisk(buf, strlen(buf), part, &tmpdev);
230 	}
231 
232 	if (bios_bootmac) {
233 		struct ifnet *ifp;
234 
235 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
236 			if (ifp->if_type == IFT_ETHER &&
237 			    memcmp(bios_bootmac->mac,
238 			    ((struct arpcom *)ifp)->ac_enaddr,
239 			    ETHER_ADDR_LEN) == 0)
240 				break;
241 		}
242 		if (ifp) {
243 			printf("PXE boot MAC address %s, interface %s\n",
244 			    ether_sprintf(bios_bootmac->mac), ifp->if_xname);
245 #if defined(NFSCLIENT)
246 			bootdv = parsedisk(ifp->if_xname, strlen(ifp->if_xname),
247 			    0, &tmpdev);
248 			part = 0;
249 #endif
250 		} else
251 			printf("PXE boot MAC address %s, interface %s\n",
252 			    ether_sprintf(bios_bootmac->mac), "unknown");
253 	}
254 
255 	setroot(bootdv, part, RB_USERREQ);
256 	dumpconf();
257 
258 #ifdef HIBERNATE
259 	hibernate_resume();
260 #endif /* HIBERNATE */
261 }
262 
263 struct nam2blk nam2blk[] = {
264 	{ "wd",		0 },
265 	{ "fd",		2 },
266 	{ "sd",		4 },
267 	{ "cd",		6 },
268 	{ "vnd",	14 },
269 	{ "rd",		17 },
270 	{ NULL,		-1 }
271 };
272