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