xref: /dragonfly/sys/bus/pci/x86_64/legacy.c (revision 55f88487)
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  * $FreeBSD: src/sys/i386/i386/legacy.c,v 1.63.2.1.4.1 2009/04/15 03:14:26 kensmith Exp $
30  */
31 
32 /*
33  * This code implements a system driver for legacy systems that do not
34  * support ACPI or when ACPI support is not present in the kernel.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44 
45 #include "legacyvar.h"
46 
47 static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device");
48 struct legacy_device {
49 	int			lg_pcibus;
50 };
51 
52 #define DEVTOAT(dev)	((struct legacy_device *)device_get_ivars(dev))
53 
54 static int	legacy_identify(driver_t *, device_t);
55 static int	legacy_probe(device_t);
56 static int	legacy_attach(device_t);
57 static int	legacy_print_child(device_t, device_t);
58 static device_t	legacy_add_child(device_t, device_t, int, const char *, int);
59 static int	legacy_read_ivar(device_t, device_t, int, uintptr_t *);
60 static int	legacy_write_ivar(device_t, device_t, int, uintptr_t);
61 
62 static device_method_t legacy_methods[] = {
63 	/* Device interface */
64 	DEVMETHOD(device_identify,	legacy_identify),
65 	DEVMETHOD(device_probe,		legacy_probe),
66 	DEVMETHOD(device_attach,	legacy_attach),
67 	DEVMETHOD(device_detach,	bus_generic_detach),
68 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
69 	DEVMETHOD(device_suspend,	bus_generic_suspend),
70 	DEVMETHOD(device_resume,	bus_generic_resume),
71 
72 	/* Bus interface */
73 	DEVMETHOD(bus_print_child,	legacy_print_child),
74 	DEVMETHOD(bus_add_child,	legacy_add_child),
75 	DEVMETHOD(bus_read_ivar,	legacy_read_ivar),
76 	DEVMETHOD(bus_write_ivar,	legacy_write_ivar),
77 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
78 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
79 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
80 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
81 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
82 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
83 
84 	DEVMETHOD_END
85 };
86 
87 static driver_t legacy_driver = {
88 	"legacy",
89 	legacy_methods,
90 	1,			/* no softc */
91 };
92 static devclass_t legacy_devclass;
93 
94 DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, NULL, NULL);
95 
96 static int
97 legacy_identify(driver_t *driver, device_t parent)
98 {
99 	/*
100 	 * Basically a static device, there's no point reinstalling it
101 	 * on rescan.
102 	 */
103 	if (device_get_state(parent) == DS_ATTACHED)
104 		return (0);
105 	if (device_get_state(parent) == DS_INPROGRESS)
106 		return (0);
107 
108 	/*
109 	 * Add child device with order of 11 so it gets probed
110 	 * after ACPI (which is at order 10).
111 	 */
112 	if (BUS_ADD_CHILD(parent, parent, 11, "legacy", 0) == NULL)
113 		panic("legacy: could not attach");
114 	return (0);
115 }
116 
117 static int
118 legacy_probe(device_t dev)
119 {
120 #if 0 /* ACPI_ENABLE_PCI */
121 	device_t acpi;
122 
123 	/*
124 	 * Fail to probe if ACPI is ok.
125 	 */
126 	acpi = devclass_get_device(devclass_find("acpi"), 0);
127 	if (acpi != NULL && device_is_alive(acpi))
128 		return (ENXIO);
129 #endif	/* ACPI_ENABLE_PCI */
130 	device_set_desc(dev, "legacy system");
131 	device_quiet(dev);
132 	return (0);
133 }
134 
135 static int
136 legacy_attach(device_t dev)
137 {
138 	device_t child;
139 
140 	/*
141 	 * Let our child drivers identify any child devices that they
142 	 * can find.  Once that is done attach any devices that we
143 	 * found.
144 	 */
145 	bus_generic_probe(dev);
146 	bus_generic_attach(dev);
147 
148 	/*
149 	 * If we didn't see ISA on a pci bridge, create a
150 	 * connection point now so it shows up "on motherboard".
151 	 */
152 	if (!devclass_get_device(devclass_find("isa"), 0)) {
153 		child = BUS_ADD_CHILD(dev, dev, 0, "isa", 0);
154 		if (child == NULL)
155 			panic("legacy_attach isa");
156 		device_probe_and_attach(child);
157 	}
158 
159 	return 0;
160 }
161 
162 static int
163 legacy_print_child(device_t bus, device_t child)
164 {
165 	struct legacy_device *atdev = DEVTOAT(child);
166 	int retval = 0;
167 
168 	retval += bus_print_child_header(bus, child);
169 	if (atdev->lg_pcibus != -1)
170 		retval += kprintf(" pcibus %d", atdev->lg_pcibus);
171 	retval += kprintf(" on motherboard\n");	/* XXX "motherboard", ick */
172 
173 	return (retval);
174 }
175 
176 static device_t
177 legacy_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
178 {
179 	device_t child;
180 	struct legacy_device *atdev;
181 	atdev = kmalloc(sizeof(struct legacy_device), M_LEGACYDEV,
182 	    M_NOWAIT | M_ZERO);
183 	if (atdev == NULL)
184 		return(NULL);
185 	atdev->lg_pcibus = -1;
186 
187 	child = device_add_child_ordered(bus, order, name, unit);
188 	if (child == NULL) {
189 		kfree(atdev, M_LEGACYDEV);
190 	}
191 	else {
192 		/* should we kfree this in legacy_child_detached? */
193 		device_set_ivars(child, atdev);
194 	}
195 	return (child);
196 }
197 
198 static int
199 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
200 {
201 	struct legacy_device *atdev = DEVTOAT(child);
202 
203 	switch (which) {
204 	case LEGACY_IVAR_PCIDOMAIN:
205 		*result = 0;
206 		break;
207 	case LEGACY_IVAR_PCIBUS:
208 		*result = atdev->lg_pcibus;
209 		break;
210 	default:
211 		return ENOENT;
212 	}
213 	return 0;
214 }
215 
216 static int
217 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
218 {
219 	struct legacy_device *atdev = DEVTOAT(child);
220 
221 	switch (which) {
222 	case LEGACY_IVAR_PCIDOMAIN:
223 		return EINVAL;
224 	case LEGACY_IVAR_PCIBUS:
225 		atdev->lg_pcibus = value;
226 		break;
227 	default:
228 		return ENOENT;
229 	}
230 	return 0;
231 }
232 
233 #ifdef notyet
234 /*
235  * Legacy CPU attachment when ACPI is not available.  Drivers like
236  * cpufreq(4) hang off this.
237  */
238 static void	cpu_identify(driver_t *driver, device_t parent);
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_identify,	cpu_identify),
253 	DEVMETHOD(device_probe,		bus_generic_probe),
254 	DEVMETHOD(device_attach,	bus_generic_attach),
255 	DEVMETHOD(device_detach,	bus_generic_detach),
256 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
257 	DEVMETHOD(device_suspend,	bus_generic_suspend),
258 	DEVMETHOD(device_resume,	bus_generic_resume),
259 
260 	/* Bus interface */
261 	DEVMETHOD(bus_add_child,	cpu_add_child),
262 	DEVMETHOD(bus_read_ivar,	cpu_read_ivar),
263 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
264 	DEVMETHOD(bus_get_resource_list, cpu_get_rlist),
265 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
266 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
267 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
268 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
269 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
270 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
271 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
272 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
273 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
274 
275 	DEVMETHOD_END
276 };
277 
278 static driver_t cpu_driver = {
279 	"cpu",
280 	cpu_methods,
281 	1,		/* no softc */
282 };
283 static devclass_t cpu_devclass;
284 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, NULL, NULL);
285 
286 static void
287 cpu_identify(driver_t *driver, device_t parent)
288 {
289 	device_t child;
290 	int i;
291 
292 	/*
293 	 * Attach a cpuX device for each CPU.  We use an order of 150
294 	 * so that these devices are attached after the Host-PCI
295 	 * bridges (which are added at order 100).
296 	 */
297 	for (i = 0; i <= mp_maxid; i++)
298 		if (!CPU_ABSENT(i)) {
299 			child = BUS_ADD_CHILD(parent, 150, "cpu", i);
300 			if (child == NULL)
301 				panic("legacy_attach cpu");
302 		}
303 }
304 
305 static device_t
306 cpu_add_child(device_t bus, int order, const char *name, int unit)
307 {
308 	struct cpu_device *cd;
309 	device_t child;
310 	struct pcpu *pc;
311 
312 	if ((cd = kmalloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
313 		return (NULL);
314 
315 	resource_list_init(&cd->cd_rl);
316 	pc = pcpu_find(device_get_unit(bus));
317 	cd->cd_pcpu = pc;
318 
319 	child = device_add_child_ordered(bus, order, name, unit);
320 	if (child != NULL) {
321 		pc->pc_device = child;
322 		device_set_ivars(child, cd);
323 	} else
324 		kfree(cd, M_DEVBUF);
325 	return (child);
326 }
327 
328 static struct resource_list *
329 cpu_get_rlist(device_t dev, device_t child)
330 {
331 	struct cpu_device *cpdev;
332 
333 	cpdev = device_get_ivars(child);
334 	return (&cpdev->cd_rl);
335 }
336 
337 static int
338 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
339 {
340 	struct cpu_device *cpdev;
341 
342 	if (index != CPU_IVAR_PCPU)
343 		return (ENOENT);
344 	cpdev = device_get_ivars(child);
345 	*result = (uintptr_t)cpdev->cd_pcpu;
346 	return (0);
347 }
348 #endif
349