pci: Don't test whether acpi module is loaded; acpi-pci is not ready to go yet
[dragonfly.git] / sys / bus / pci / i386 / legacy.c
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  * __FBSDID("$FreeBSD: src/sys/i386/i386/legacy.c,v 1.63.2.1.4.1 2009/04/15 03:14:26 kensmith Exp $");
29  */
30
31 #include <sys/cdefs.h>
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 <sys/module.h>
44 #include <sys/rman.h>
45
46 #undef DEV_MCA
47 #ifdef DEV_MCA
48 #include <i386/bios/mca_machdep.h>
49 #endif
50
51 #include <machine/legacyvar.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, device_t parent, 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 11 so it gets probed
109          * after ACPI (which is at order 10).
110          */
111         if (BUS_ADD_CHILD(parent, parent, 11, "legacy", 0) == NULL)
112                 panic("legacy: could not attach");
113 }
114
115 static int
116 legacy_probe(device_t dev)
117 {
118 #ifdef ACPI_ENABLE_PCI
119         device_t acpi;
120
121         /*
122          * Fail to probe if ACPI is ok.
123          */
124         acpi = devclass_get_device(devclass_find("acpi"), 0);
125         if (acpi != NULL && device_is_alive(acpi))
126                 return (ENXIO);
127 #endif  /* ACPI_ENABLE_PCI */
128         device_set_desc(dev, "legacy system");
129         device_quiet(dev);
130         return (0);
131 }
132
133 static int
134 legacy_attach(device_t dev)
135 {
136         device_t child;
137
138         /*
139          * Let our child drivers identify any child devices that they
140          * can find.  Once that is done attach any devices that we
141          * found.
142          */
143         bus_generic_probe(dev);
144         bus_generic_attach(dev);
145
146 #ifndef PC98
147         /*
148          * If we didn't see EISA or ISA on a pci bridge, create some
149          * connection points now so they show up "on motherboard".
150          */
151         if (!devclass_get_device(devclass_find("eisa"), 0)) {
152                 child = BUS_ADD_CHILD(dev, dev, 0, "eisa", 0);
153                 if (child == NULL)
154                         panic("legacy_attach eisa");
155                 device_probe_and_attach(child);
156         }
157 #endif
158 #ifdef DEV_MCA
159         if (MCA_system && !devclass_get_device(devclass_find("mca"), 0)) {
160                 child = BUS_ADD_CHILD(dev, dev, 0, "mca", 0);
161                 if (child == 0)
162                         panic("legacy_probe mca");
163                 device_probe_and_attach(child);
164         }
165 #endif
166         if (!devclass_get_device(devclass_find("isa"), 0)) {
167                 child = BUS_ADD_CHILD(dev, dev, 0, "isa", 0);
168                 if (child == NULL)
169                         panic("legacy_attach isa");
170                 device_probe_and_attach(child);
171         }
172
173         return 0;
174 }
175
176 static int
177 legacy_print_child(device_t bus, device_t child)
178 {
179         struct legacy_device *atdev = DEVTOAT(child);
180         int retval = 0;
181
182         retval += bus_print_child_header(bus, child);
183         if (atdev->lg_pcibus != -1)
184                 retval += kprintf(" pcibus %d", atdev->lg_pcibus);
185         retval += kprintf(" on motherboard\n"); /* XXX "motherboard", ick */
186
187         return (retval);
188 }
189
190 static device_t
191 legacy_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
192 {
193         device_t child;
194         struct legacy_device *atdev;
195         atdev = kmalloc(sizeof(struct legacy_device), M_LEGACYDEV,
196             M_NOWAIT | M_ZERO);
197         if (atdev == NULL)
198                 return(NULL);
199         atdev->lg_pcibus = -1;
200
201         child = device_add_child_ordered(bus, order, name, unit);
202         if (child == NULL) {
203                 kfree(atdev, M_LEGACYDEV);
204         }
205         else {
206                 /* should we kfree this in legacy_child_detached? */
207                 device_set_ivars(child, atdev);
208         }
209         return (child);
210 }
211
212 static int
213 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
214 {
215         struct legacy_device *atdev = DEVTOAT(child);
216
217         switch (which) {
218         case LEGACY_IVAR_PCIDOMAIN:
219                 *result = 0;
220                 break;
221         case LEGACY_IVAR_PCIBUS:
222                 *result = atdev->lg_pcibus;
223                 break;
224         default:
225                 return ENOENT;
226         }
227         return 0;
228 }
229
230 static int
231 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
232 {
233         struct legacy_device *atdev = DEVTOAT(child);
234
235         switch (which) {
236         case LEGACY_IVAR_PCIDOMAIN:
237                 return EINVAL;
238         case LEGACY_IVAR_PCIBUS:
239                 atdev->lg_pcibus = value;
240                 break;
241         default:
242                 return ENOENT;
243         }
244         return 0;
245 }
246
247 #ifdef notyet
248 /*
249  * Legacy CPU attachment when ACPI is not available.  Drivers like
250  * cpufreq(4) hang off this.
251  */
252 static void     cpu_identify(driver_t *driver, device_t parent);
253 static int      cpu_read_ivar(device_t dev, device_t child, int index,
254                     uintptr_t *result);
255 static device_t cpu_add_child(device_t bus, int order, const char *name,
256                     int unit);
257 static struct resource_list *cpu_get_rlist(device_t dev, device_t child);
258
259 struct cpu_device {
260         struct resource_list cd_rl;
261         struct pcpu *cd_pcpu;
262 };
263
264 static device_method_t cpu_methods[] = {
265         /* Device interface */
266         DEVMETHOD(device_identify,      cpu_identify),
267         DEVMETHOD(device_probe,         bus_generic_probe),
268         DEVMETHOD(device_attach,        bus_generic_attach),
269         DEVMETHOD(device_detach,        bus_generic_detach),
270         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
271         DEVMETHOD(device_suspend,       bus_generic_suspend),
272         DEVMETHOD(device_resume,        bus_generic_resume),
273
274         /* Bus interface */
275         DEVMETHOD(bus_add_child,        cpu_add_child),
276         DEVMETHOD(bus_read_ivar,        cpu_read_ivar),
277         DEVMETHOD(bus_print_child,      bus_generic_print_child),
278         DEVMETHOD(bus_get_resource_list, cpu_get_rlist),
279         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
280         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
281         DEVMETHOD(bus_alloc_resource,   bus_generic_rl_alloc_resource),
282         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
283         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
284         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
285         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
286         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
287         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
288
289         { 0, 0 }
290 };
291
292 static driver_t cpu_driver = {
293         "cpu",
294         cpu_methods,
295         1,              /* no softc */
296 };
297 static devclass_t cpu_devclass;
298 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0);
299
300 static void
301 cpu_identify(driver_t *driver, device_t parent)
302 {
303         device_t child;
304         int i;
305
306         /*
307          * Attach a cpuX device for each CPU.  We use an order of 150
308          * so that these devices are attached after the Host-PCI
309          * bridges (which are added at order 100).
310          */
311         for (i = 0; i <= mp_maxid; i++)
312                 if (!CPU_ABSENT(i)) {
313                         child = BUS_ADD_CHILD(parent, 150, "cpu", i);
314                         if (child == NULL)
315                                 panic("legacy_attach cpu");
316                 }
317 }
318
319 static device_t
320 cpu_add_child(device_t bus, int order, const char *name, int unit)
321 {
322         struct cpu_device *cd;
323         device_t child;
324         struct pcpu *pc;
325
326         if ((cd = kmalloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
327                 return (NULL);
328
329         resource_list_init(&cd->cd_rl);
330         pc = pcpu_find(device_get_unit(bus));
331         cd->cd_pcpu = pc;
332
333         child = device_add_child_ordered(bus, order, name, unit);
334         if (child != NULL) {
335                 pc->pc_device = child;
336                 device_set_ivars(child, cd);
337         } else
338                 kfree(cd, M_DEVBUF);
339         return (child);
340 }
341
342 static struct resource_list *
343 cpu_get_rlist(device_t dev, device_t child)
344 {
345         struct cpu_device *cpdev;
346
347         cpdev = device_get_ivars(child);
348         return (&cpdev->cd_rl);
349 }
350
351 static int
352 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
353 {
354         struct cpu_device *cpdev;
355
356         if (index != CPU_IVAR_PCPU)
357                 return (ENOENT);
358         cpdev = device_get_ivars(child);
359         *result = (uintptr_t)cpdev->cd_pcpu;
360         return (0);
361 }
362 #endif