autoconf.c revision 1.56 1 /* $NetBSD: autoconf.c,v 1.56 2021/05/22 15:04:33 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)autoconf.c 8.4 (Berkeley) 10/1/93
41 */
42
43 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
44
45 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.56 2021/05/22 15:04:33 thorpej Exp $");
46
47 #include "pci.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/buf.h>
52 #include <sys/disklabel.h>
53 #include <sys/reboot.h>
54 #include <sys/device.h>
55 #include <sys/conf.h>
56 #include <dev/cons.h>
57
58 #include <dev/pci/pcivar.h>
59
60 #include <machine/autoconf.h>
61 #include <machine/alpha.h>
62 #include <machine/cpu.h>
63 #include <machine/prom.h>
64 #include <machine/cpuconf.h>
65 #include <machine/intr.h>
66
67 struct bootdev_data *bootdev_data;
68
69 void parse_prom_bootdev(void);
70 static inline int atoi(const char *);
71
72 /*
73 * cpu_configure:
74 * called at boot time, configure all devices on system
75 */
76 void
77 cpu_configure(void)
78 {
79
80 parse_prom_bootdev();
81
82 /*
83 * Disable interrupts during autoconfiguration. splhigh() won't
84 * work, because it simply _raises_ the IPL, so if machine checks
85 * are disabled, they'll stay disabled. Machine checks are needed
86 * during autoconfig.
87 */
88 (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
89 if (config_rootfound("mainbus", NULL) == NULL)
90 panic("no mainbus found");
91 (void)spl0();
92
93 /*
94 * Note that bootstrapping is finished, and set the HWRPB up
95 * to do restarts.
96 */
97 hwrpb_restart_setup();
98 }
99
100 static void
101 qemu_find_rootdev(void)
102 {
103 char buf[32] = { 0 };
104
105 /*
106 * Check for "rootdev=wd0".
107 */
108 if (! prom_qemu_getenv("rootdev", buf, sizeof(buf))) {
109 /*
110 * Check "root=/dev/wd0a", "root=/dev/hda1", etc.
111 */
112 if (! prom_qemu_getenv("root", buf, sizeof(buf))) {
113 printf("WARNING: no rootdev= or root= arguments "
114 "provided by Qemu\n");
115 return;
116 }
117 }
118
119 if (prom_qemu_getenv("rootdev", buf, sizeof(buf))) {
120 snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
121 "rootdev=%s", buf);
122 booted_device = device_find_by_xname(buf);
123 return;
124 }
125
126 const size_t devlen = strlen("/dev/");
127 const char *cp = buf;
128 char *ecp = &buf[sizeof(buf) - 1];
129
130 /* Find the start of the device xname. */
131 if (strlen(cp) > devlen && strncmp(cp, "/dev/", devlen) == 0) {
132 cp += devlen;
133 }
134
135 /* Now strip any partition letter off the end. */
136 while (ecp != cp) {
137 if (*ecp >= '0' && *ecp <= '9') {
138 break;
139 }
140 *ecp-- = '\0';
141 }
142
143 snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
144 "root=%s", cp);
145 booted_device = device_find_by_xname(cp);
146 }
147
148 void
149 cpu_rootconf(void)
150 {
151
152 if (booted_device == NULL && alpha_is_qemu) {
153 qemu_find_rootdev();
154 }
155
156 if (booted_device == NULL) {
157 printf("WARNING: can't figure what device matches \"%s\"\n",
158 bootinfo.booted_dev);
159 }
160 rootconf();
161 }
162
163 void
164 parse_prom_bootdev(void)
165 {
166 static char hacked_boot_dev[128];
167 static struct bootdev_data bd;
168 char *cp, *scp, *boot_fields[8];
169 int i, done;
170
171 booted_device = NULL;
172 booted_partition = 0;
173 bootdev_data = NULL;
174
175 memcpy(hacked_boot_dev, bootinfo.booted_dev,
176 uimin(sizeof bootinfo.booted_dev, sizeof hacked_boot_dev));
177 #if 0
178 printf("parse_prom_bootdev: boot dev = \"%s\"\n", hacked_boot_dev);
179 #endif
180
181 i = 0;
182 scp = cp = hacked_boot_dev;
183 for (done = 0; !done; cp++) {
184 if (*cp != ' ' && *cp != '\0')
185 continue;
186 if (*cp == '\0')
187 done = 1;
188
189 *cp = '\0';
190 boot_fields[i++] = scp;
191 scp = cp + 1;
192 if (i == 8)
193 done = 1;
194 }
195 if (i != 8)
196 return; /* doesn't look like anything we know! */
197
198 #if 0
199 printf("i = %d, done = %d\n", i, done);
200 for (i--; i >= 0; i--)
201 printf("%d = %s\n", i, boot_fields[i]);
202 #endif
203
204 bd.protocol = boot_fields[0];
205 bd.bus = atoi(boot_fields[1]);
206 bd.slot = atoi(boot_fields[2]);
207 bd.channel = atoi(boot_fields[3]);
208 bd.remote_address = boot_fields[4];
209 bd.unit = atoi(boot_fields[5]);
210 bd.boot_dev_type = atoi(boot_fields[6]);
211 bd.ctrl_dev_type = boot_fields[7];
212
213 #if 0
214 printf("parsed: proto = %s, bus = %d, slot = %d, channel = %d,\n",
215 bd.protocol, bd.bus, bd.slot, bd.channel);
216 printf("\tremote = %s, unit = %d, dev_type = %d, ctrl_type = %s\n",
217 bd.remote_address, bd.unit, bd.boot_dev_type, bd.ctrl_dev_type);
218 #endif
219
220 bootdev_data = &bd;
221 }
222
223 static inline int
224 atoi(const char *s)
225 {
226 return (int)strtoll(s, NULL, 10);
227 }
228
229 void
230 device_register(device_t dev, void *aux)
231 {
232 #if NPCI > 0
233 device_t parent = device_parent(dev);
234
235 if (parent != NULL && device_is_a(parent, "pci"))
236 device_pci_register(dev, aux);
237 #endif
238
239 if (bootdev_data == NULL) {
240 /*
241 * There is no hope.
242 */
243 return;
244 }
245 if (platform.device_register)
246 (*platform.device_register)(dev, aux);
247 }
248