xref: /openbsd/sys/arch/amd64/amd64/autoconf.c (revision a14dd3eb)
1 /*	$OpenBSD: autoconf.c,v 1.56 2024/05/14 01:42:07 guenther Exp $	*/
2 /*	$NetBSD: autoconf.c,v 1.1 2003/04/26 18:39:26 fvdl 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 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/reboot.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/timeout.h>
54 #include <sys/hibernate.h>
55 #include <uvm/uvm_extern.h>
56 
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/cpufunc.h>
64 #include <machine/biosvar.h>
65 
66 #include "ioapic.h"
67 #include "lapic.h"
68 
69 #if NIOAPIC > 0
70 #include <machine/i82093var.h>
71 #endif
72 
73 #if NLAPIC > 0
74 #include <machine/i82489var.h>
75 #endif
76 
77 int	cold = 1;	/* if 1, still working on cold-start */
78 extern dev_t bootdev;
79 
80 /* Support for VIA C3 RNG */
81 extern struct timeout viac3_rnd_tmo;
82 extern int	viac3_rnd_present;
83 void		viac3_rnd(void *);
84 
85 extern struct timeout rdrand_tmo;
86 extern int	has_rdrand;
87 extern int	has_rdseed;
88 void		rdrand(void *);
89 
90 #ifdef CRYPTO
91 void		viac3_crypto_setup(void);
92 extern int	amd64_has_xcrypt;
93 
94 void		pclmul_setup(void);
95 void		aesni_setup(void);
96 #endif
97 
98 void
unmap_startup(void)99 unmap_startup(void)
100 {
101 	extern int kernel_text[], endboot[];
102 	vaddr_t p = (vaddr_t)kernel_text;
103 
104 	do {
105 		pmap_kremove(p, PAGE_SIZE);
106 		p += PAGE_SIZE;
107 	} while (p < (vaddr_t)endboot);
108 }
109 
110 /*
111  * Determine i/o configuration for a machine.
112  */
113 void
cpu_configure(void)114 cpu_configure(void)
115 {
116 	x86_64_proc0_tss_ldt_init();
117 
118 	pmap_randomize();
119 	map_tramps();
120 
121 	if (config_rootfound("mainbus", NULL) == NULL)
122 		panic("configure: mainbus not configured");
123 
124 	intr_printconfig();
125 
126 #if NIOAPIC > 0
127 	lapic_set_lvt();
128 	ioapic_enable();
129 #endif
130 
131 	unmap_startup();
132 
133 	lcr8(0);
134 	spl0();
135 	cold = 0;
136 
137 	/*
138 	 * At this point the RNG is running, and if FSXR is set we can
139 	 * use it.  Here we setup a periodic timeout to collect the data.
140 	 */
141 	if (viac3_rnd_present) {
142 		timeout_set(&viac3_rnd_tmo, viac3_rnd, &viac3_rnd_tmo);
143 		viac3_rnd(&viac3_rnd_tmo);
144 	}
145 	timeout_set(&rdrand_tmo, rdrand, &rdrand_tmo);
146 	rdrand(&rdrand_tmo);
147 #ifdef CRYPTO
148 	/*
149 	 * Also, if the chip has crypto available, enable it.
150 	 */
151 	if (amd64_has_xcrypt)
152 		viac3_crypto_setup();
153 
154 	if (cpu_ecxfeature & CPUIDECX_PCLMUL)
155 		pclmul_setup();
156 
157 	if (cpu_ecxfeature & CPUIDECX_AES)
158 		aesni_setup();
159 #endif
160 }
161 
162 void
device_register(struct device * dev,void * aux)163 device_register(struct device *dev, void *aux)
164 {
165 }
166 
167 /*
168  * Now that we are fully operational, we can checksum the
169  * disks, and using some heuristics, hopefully are able to
170  * always determine the correct root disk.
171  */
172 void
diskconf(void)173 diskconf(void)
174 {
175 	int majdev, unit, part = 0;
176 	struct device *bootdv = NULL;
177 	dev_t tmpdev;
178 	char buf[128];
179 	extern bios_bootmac_t *bios_bootmac;
180 
181 	dkcsumattach();
182 
183 	if ((bootdev & B_MAGICMASK) == (u_int)B_DEVMAGIC) {
184 		majdev = B_TYPE(bootdev);
185 		unit = B_UNIT(bootdev);
186 		part = B_PARTITION(bootdev);
187 		snprintf(buf, sizeof buf, "%s%d%c", findblkname(majdev),
188 		    unit, part + 'a');
189 		bootdv = parsedisk(buf, strlen(buf), part, &tmpdev);
190 	}
191 
192 	if (bios_bootmac) {
193 		struct ifnet *ifp;
194 
195 		TAILQ_FOREACH(ifp, &ifnetlist, if_list) {
196 			if (ifp->if_type == IFT_ETHER &&
197 			    memcmp(bios_bootmac->mac,
198 			    ((struct arpcom *)ifp)->ac_enaddr,
199 			    ETHER_ADDR_LEN) == 0)
200 				break;
201 		}
202 		if (ifp) {
203 			printf("PXE boot MAC address %s, interface %s\n",
204 			    ether_sprintf(bios_bootmac->mac), ifp->if_xname);
205 #if defined(NFSCLIENT)
206 			bootdv = parsedisk(ifp->if_xname, strlen(ifp->if_xname),
207 			    0, &tmpdev);
208 			part = 0;
209 #endif
210 		} else
211 			printf("PXE boot MAC address %s, interface %s\n",
212 			    ether_sprintf(bios_bootmac->mac), "unknown");
213 	}
214 
215 	setroot(bootdv, part, RB_USERREQ);
216 	dumpconf();
217 
218 #ifdef HIBERNATE
219 	hibernate_resume();
220 #endif /* HIBERNATE */
221 }
222 
223 const struct nam2blk nam2blk[] = {
224 	{ "wd",		0 },
225 	{ "fd",		2 },
226 	{ "sd",		4 },
227 	{ "cd",		6 },
228 	{ "vnd",	14 },
229 	{ "rd",		17 },
230 	{ NULL,		-1 }
231 };
232