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: releng/5.2/sys/amd64/amd64/legacy.c 122940 2003-11-21 03:02:00Z peter $");
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/kernel.h>
42 #include <sys/malloc.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45
46 #include <machine/legacyvar.h>
47 #include <machine/resource.h>
48
49 static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device");
50 struct legacy_device {
51 struct resource_list lg_resources;
52 int lg_pcibus;
53 };
54
55 #define DEVTOAT(dev) ((struct legacy_device *)device_get_ivars(dev))
56
57 static void legacy_identify(driver_t *driver, device_t parent);
58 static int legacy_probe(device_t);
59 static int legacy_attach(device_t);
60 static int legacy_print_child(device_t, device_t);
61 static device_t legacy_add_child(device_t bus, int order, const char *name,
62 int unit);
63 static struct resource *legacy_alloc_resource(device_t, device_t, int, int *,
64 u_long, u_long, u_long, u_int);
65 static int legacy_read_ivar(device_t, device_t, int, uintptr_t *);
66 static int legacy_write_ivar(device_t, device_t, int, uintptr_t);
67 static int legacy_release_resource(device_t, device_t, int, int,
68 struct resource *);
69 static int legacy_set_resource(device_t, device_t, int, int, u_long, u_long);
70 static int legacy_get_resource(device_t, device_t, int, int, u_long *, u_long *);
71 static void legacy_delete_resource(device_t, device_t, int, int);
72
73 static device_method_t legacy_methods[] = {
74 /* Device interface */
75 DEVMETHOD(device_identify, legacy_identify),
76 DEVMETHOD(device_probe, legacy_probe),
77 DEVMETHOD(device_attach, legacy_attach),
78 DEVMETHOD(device_detach, bus_generic_detach),
79 DEVMETHOD(device_shutdown, bus_generic_shutdown),
80 DEVMETHOD(device_suspend, bus_generic_suspend),
81 DEVMETHOD(device_resume, bus_generic_resume),
82
83 /* Bus interface */
84 DEVMETHOD(bus_print_child, legacy_print_child),
85 DEVMETHOD(bus_add_child, legacy_add_child),
86 DEVMETHOD(bus_read_ivar, legacy_read_ivar),
87 DEVMETHOD(bus_write_ivar, legacy_write_ivar),
88 DEVMETHOD(bus_set_resource, legacy_set_resource),
89 DEVMETHOD(bus_get_resource, legacy_get_resource),
90 DEVMETHOD(bus_alloc_resource, legacy_alloc_resource),
91 DEVMETHOD(bus_release_resource, legacy_release_resource),
92 DEVMETHOD(bus_delete_resource, legacy_delete_resource),
93 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
94 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
95 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
96 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
97
98 { 0, 0 }
99 };
100
101 static driver_t legacy_driver = {
102 "legacy",
103 legacy_methods,
104 1, /* no softc */
105 };
106 static devclass_t legacy_devclass;
107
108 DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, 0, 0);
109
110 static void
111 legacy_identify(driver_t *driver, device_t parent)
112 {
113
114 /*
115 * Add child device with order of 1 so it gets probed
116 * after ACPI (which is at order 0.
117 */
118 if (BUS_ADD_CHILD(parent, 1, "legacy", 0) == NULL)
119 panic("legacy: could not attach");
120 }
121
122 static int
123 legacy_probe(device_t dev)
124 {
125 device_t acpi;
126
127 /*
128 * Fail to probe if ACPI is ok.
129 */
130 acpi = devclass_get_device(devclass_find("acpi"), 0);
131 if (acpi != NULL && device_is_alive(acpi))
132 return (ENXIO);
133 device_set_desc(dev, "legacy system");
134 device_quiet(dev);
135 return (0);
136 }
137
138 static int
139 legacy_attach(device_t dev)
140 {
141 device_t child;
142
143 /*
144 * First, let our child driver's identify any child devices that
145 * they can find. Once that is done attach any devices that we
146 * found.
147 */
148 bus_generic_probe(dev);
149 bus_generic_attach(dev);
150
151 /*
152 * If we didn't see ISA on a pci bridge, create some
153 * connection points now so it shows up "on motherboard".
154 */
155 if (!devclass_get_device(devclass_find("isa"), 0)) {
156 child = BUS_ADD_CHILD(dev, 0, "isa", 0);
157 if (child == NULL)
158 panic("legacy_attach isa");
159 device_probe_and_attach(child);
160 }
161
162 return 0;
163 }
164
165 static int
166 legacy_print_all_resources(device_t dev)
167 {
168 struct legacy_device *atdev = DEVTOAT(dev);
169 struct resource_list *rl = &atdev->lg_resources;
170 int retval = 0;
171
172 if (SLIST_FIRST(rl) || atdev->lg_pcibus != -1)
173 retval += printf(" at");
174
175 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
176 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
177 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
178
179 return retval;
180 }
181
182 static int
183 legacy_print_child(device_t bus, device_t child)
184 {
185 struct legacy_device *atdev = DEVTOAT(child);
186 int retval = 0;
187
188 retval += bus_print_child_header(bus, child);
189 retval += legacy_print_all_resources(child);
190 if (atdev->lg_pcibus != -1)
191 retval += printf(" pcibus %d", atdev->lg_pcibus);
192 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
193
194 return (retval);
195 }
196
197 static device_t
198 legacy_add_child(device_t bus, int order, const char *name, int unit)
199 {
200 device_t child;
201 struct legacy_device *atdev;
202
203 atdev = malloc(sizeof(struct legacy_device), M_LEGACYDEV,
204 M_NOWAIT | M_ZERO);
205 if (!atdev)
206 return(0);
207 resource_list_init(&atdev->lg_resources);
208 atdev->lg_pcibus = -1;
209
210 child = device_add_child_ordered(bus, order, name, unit);
211
212 /* should we free this in legacy_child_detached? */
213 device_set_ivars(child, atdev);
214
215 return(child);
216 }
217
218 static int
219 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
220 {
221 struct legacy_device *atdev = DEVTOAT(child);
222
223 switch (which) {
224 case LEGACY_IVAR_PCIBUS:
225 *result = atdev->lg_pcibus;
226 break;
227 default:
228 return ENOENT;
229 }
230 return 0;
231 }
232
233
234 static int
235 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
236 {
237 struct legacy_device *atdev = DEVTOAT(child);
238
239 switch (which) {
240 case LEGACY_IVAR_PCIBUS:
241 atdev->lg_pcibus = value;
242 break;
243 default:
244 return ENOENT;
245 }
246 return 0;
247 }
248
249
250 static struct resource *
251 legacy_alloc_resource(device_t bus, device_t child, int type, int *rid,
252 u_long start, u_long end, u_long count, u_int flags)
253 {
254 struct legacy_device *atdev = DEVTOAT(child);
255 struct resource_list *rl = &atdev->lg_resources;
256
257 return (resource_list_alloc(rl, bus, child, type, rid, start, end,
258 count, flags));
259 }
260
261 static int
262 legacy_release_resource(device_t bus, device_t child, int type, int rid,
263 struct resource *r)
264 {
265 struct legacy_device *atdev = DEVTOAT(child);
266 struct resource_list *rl = &atdev->lg_resources;
267
268 return (resource_list_release(rl, bus, child, type, rid, r));
269 }
270
271 static int
272 legacy_set_resource(device_t dev, device_t child, int type, int rid,
273 u_long start, u_long count)
274 {
275 struct legacy_device *atdev = DEVTOAT(child);
276 struct resource_list *rl = &atdev->lg_resources;
277
278 resource_list_add(rl, type, rid, start, start + count - 1, count);
279 return(0);
280 }
281
282 static int
283 legacy_get_resource(device_t dev, device_t child, int type, int rid,
284 u_long *startp, u_long *countp)
285 {
286 struct legacy_device *atdev = DEVTOAT(child);
287 struct resource_list *rl = &atdev->lg_resources;
288 struct resource_list_entry *rle;
289
290 rle = resource_list_find(rl, type, rid);
291 if (!rle)
292 return(ENOENT);
293 if (startp)
294 *startp = rle->start;
295 if (countp)
296 *countp = rle->count;
297 return(0);
298 }
299
300 static void
301 legacy_delete_resource(device_t dev, device_t child, int type, int rid)
302 {
303 struct legacy_device *atdev = DEVTOAT(child);
304 struct resource_list *rl = &atdev->lg_resources;
305
306 resource_list_delete(rl, type, rid);
307 }
Cache object: 48b6de66f773b3c4f658b7033b6c1eec
|