1 /*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34 * This code implements a system driver for legacy systems that do not
35 * support ACPI or when ACPI support is not present in the kernel.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <machine/bus.h>
46 #include <sys/pcpu.h>
47 #include <sys/rman.h>
48 #include <sys/smp.h>
49
50 #include <machine/legacyvar.h>
51 #include <machine/resource.h>
52
53 static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device");
54 struct legacy_device {
55 int lg_pcibus;
56 };
57
58 #define DEVTOAT(dev) ((struct legacy_device *)device_get_ivars(dev))
59
60 static void legacy_identify(driver_t *driver, device_t parent);
61 static int legacy_probe(device_t);
62 static int legacy_attach(device_t);
63 static int legacy_print_child(device_t, device_t);
64 static device_t legacy_add_child(device_t bus, int order, const char *name,
65 int unit);
66 static int legacy_read_ivar(device_t, device_t, int, uintptr_t *);
67 static int legacy_write_ivar(device_t, device_t, int, uintptr_t);
68
69 static device_method_t legacy_methods[] = {
70 /* Device interface */
71 DEVMETHOD(device_identify, legacy_identify),
72 DEVMETHOD(device_probe, legacy_probe),
73 DEVMETHOD(device_attach, legacy_attach),
74 DEVMETHOD(device_detach, bus_generic_detach),
75 DEVMETHOD(device_shutdown, bus_generic_shutdown),
76 DEVMETHOD(device_suspend, bus_generic_suspend),
77 DEVMETHOD(device_resume, bus_generic_resume),
78
79 /* Bus interface */
80 DEVMETHOD(bus_print_child, legacy_print_child),
81 DEVMETHOD(bus_add_child, legacy_add_child),
82 DEVMETHOD(bus_read_ivar, legacy_read_ivar),
83 DEVMETHOD(bus_write_ivar, legacy_write_ivar),
84 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
85 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
86 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
87 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
88 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
89 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
90
91 { 0, 0 }
92 };
93
94 static driver_t legacy_driver = {
95 "legacy",
96 legacy_methods,
97 1, /* no softc */
98 };
99 static devclass_t legacy_devclass;
100
101 DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, 0, 0);
102
103 static void
104 legacy_identify(driver_t *driver, device_t parent)
105 {
106
107 /*
108 * Add child device with order of 1 so it gets probed
109 * after ACPI (which is at order 0.
110 */
111 if (BUS_ADD_CHILD(parent, 1, "legacy", 0) == NULL)
112 panic("legacy: could not attach");
113 }
114
115 static int
116 legacy_probe(device_t dev)
117 {
118 device_t acpi;
119
120 /*
121 * Fail to probe if ACPI is ok.
122 */
123 acpi = devclass_get_device(devclass_find("acpi"), 0);
124 if (acpi != NULL && device_is_alive(acpi))
125 return (ENXIO);
126 device_set_desc(dev, "legacy system");
127 device_quiet(dev);
128 return (0);
129 }
130
131 static int
132 legacy_attach(device_t dev)
133 {
134 device_t child;
135 int i;
136
137 /* First, attach the CPU pseudo-driver. */
138 for (i = 0; i <= mp_maxid; i++)
139 if (!CPU_ABSENT(i)) {
140 child = BUS_ADD_CHILD(dev, 0, "cpu", i);
141 if (child == NULL)
142 panic("legacy_attach cpu");
143 device_probe_and_attach(child);
144 }
145
146 /*
147 * Second, let our child driver's identify any child devices that
148 * they can find. Once that is done attach any devices that we
149 * found.
150 */
151 bus_generic_probe(dev);
152 bus_generic_attach(dev);
153
154 /*
155 * If we didn't see ISA on a pci bridge, create some
156 * connection points now so it shows up "on motherboard".
157 */
158 if (!devclass_get_device(devclass_find("isa"), 0)) {
159 child = BUS_ADD_CHILD(dev, 0, "isa", 0);
160 if (child == NULL)
161 panic("legacy_attach isa");
162 device_probe_and_attach(child);
163 }
164
165 return 0;
166 }
167
168 static int
169 legacy_print_child(device_t bus, device_t child)
170 {
171 struct legacy_device *atdev = DEVTOAT(child);
172 int retval = 0;
173
174 retval += bus_print_child_header(bus, child);
175 if (atdev->lg_pcibus != -1)
176 retval += printf(" pcibus %d", atdev->lg_pcibus);
177 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
178
179 return (retval);
180 }
181
182 static device_t
183 legacy_add_child(device_t bus, int order, const char *name, int unit)
184 {
185 device_t child;
186 struct legacy_device *atdev;
187
188 atdev = malloc(sizeof(struct legacy_device), M_LEGACYDEV,
189 M_NOWAIT | M_ZERO);
190 if (atdev == NULL)
191 return(NULL);
192 atdev->lg_pcibus = -1;
193
194 child = device_add_child_ordered(bus, order, name, unit);
195 if (child == NULL)
196 free(atdev, M_LEGACYDEV);
197 else
198 /* should we free this in legacy_child_detached? */
199 device_set_ivars(child, atdev);
200
201 return (child);
202 }
203
204 static int
205 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
206 {
207 struct legacy_device *atdev = DEVTOAT(child);
208
209 switch (which) {
210 case LEGACY_IVAR_PCIBUS:
211 *result = atdev->lg_pcibus;
212 break;
213 default:
214 return ENOENT;
215 }
216 return 0;
217 }
218
219
220 static int
221 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
222 {
223 struct legacy_device *atdev = DEVTOAT(child);
224
225 switch (which) {
226 case LEGACY_IVAR_PCIBUS:
227 atdev->lg_pcibus = value;
228 break;
229 default:
230 return ENOENT;
231 }
232 return 0;
233 }
234
235 /*
236 * Legacy CPU attachment when ACPI is not available. Drivers like
237 * cpufreq(4) hang off this.
238 */
239 static int cpu_read_ivar(device_t dev, device_t child, int index,
240 uintptr_t *result);
241 static device_t cpu_add_child(device_t bus, int order, const char *name,
242 int unit);
243 static struct resource_list *cpu_get_rlist(device_t dev, device_t child);
244
245 struct cpu_device {
246 struct resource_list cd_rl;
247 struct pcpu *cd_pcpu;
248 };
249
250 static device_method_t cpu_methods[] = {
251 /* Device interface */
252 DEVMETHOD(device_probe, bus_generic_probe),
253 DEVMETHOD(device_attach, bus_generic_attach),
254 DEVMETHOD(device_detach, bus_generic_detach),
255 DEVMETHOD(device_shutdown, bus_generic_shutdown),
256 DEVMETHOD(device_suspend, bus_generic_suspend),
257 DEVMETHOD(device_resume, bus_generic_resume),
258
259 /* Bus interface */
260 DEVMETHOD(bus_add_child, cpu_add_child),
261 DEVMETHOD(bus_read_ivar, cpu_read_ivar),
262 DEVMETHOD(bus_print_child, bus_generic_print_child),
263 DEVMETHOD(bus_get_resource_list, cpu_get_rlist),
264 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
265 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
266 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
267 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
268 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
269 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
270 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
271 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
272 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
273
274 { 0, 0 }
275 };
276
277 static driver_t cpu_driver = {
278 "cpu",
279 cpu_methods,
280 1, /* no softc */
281 };
282 static devclass_t cpu_devclass;
283 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0);
284
285 static device_t
286 cpu_add_child(device_t bus, int order, const char *name, int unit)
287 {
288 struct cpu_device *cd;
289 device_t child;
290 struct pcpu *pc;
291
292 if ((cd = malloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
293 return (NULL);
294
295 resource_list_init(&cd->cd_rl);
296 pc = pcpu_find(device_get_unit(bus));
297 cd->cd_pcpu = pc;
298
299 child = device_add_child_ordered(bus, order, name, unit);
300 if (child != NULL) {
301 pc->pc_device = child;
302 device_set_ivars(child, cd);
303 } else
304 free(cd, M_DEVBUF);
305 return (child);
306 }
307
308 static struct resource_list *
309 cpu_get_rlist(device_t dev, device_t child)
310 {
311 struct cpu_device *cpdev;
312
313 cpdev = device_get_ivars(child);
314 return (&cpdev->cd_rl);
315 }
316
317 static int
318 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
319 {
320 struct cpu_device *cpdev;
321
322 if (index != CPU_IVAR_PCPU)
323 return (ENOENT);
324 cpdev = device_get_ivars(child);
325 *result = (uintptr_t)cpdev->cd_pcpu;
326 return (0);
327 }
Cache object: eda7e5ab21be3ca60f86b6a36fa5f16f
|