pci: Move legacyvar.h to bus/pci/i386
[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 "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 int      legacy_identify(driver_t *, device_t);
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, device_t, int, const char *, 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
68 static device_method_t legacy_methods[] = {
69         /* Device interface */
70         DEVMETHOD(device_identify,      legacy_identify),
71         DEVMETHOD(device_probe,         legacy_probe),
72         DEVMETHOD(device_attach,        legacy_attach),
73         DEVMETHOD(device_detach,        bus_generic_detach),
74         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
75         DEVMETHOD(device_suspend,       bus_generic_suspend),
76         DEVMETHOD(device_resume,        bus_generic_resume),
77
78         /* Bus interface */
79         DEVMETHOD(bus_print_child,      legacy_print_child),
80         DEVMETHOD(bus_add_child,        legacy_add_child),
81         DEVMETHOD(bus_read_ivar,        legacy_read_ivar),
82         DEVMETHOD(bus_write_ivar,       legacy_write_ivar),
83         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
84         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
85         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
86         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
87         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
88         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
89
90         { 0, 0 }
91 };
92
93 static driver_t legacy_driver = {
94         "legacy",
95         legacy_methods,
96         1,                      /* no softc */
97 };
98 static devclass_t legacy_devclass;
99
100 DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, 0, 0);
101
102 static int
103 legacy_identify(driver_t *driver, device_t parent)
104 {
105         /*
106          * Basically a static device, there's no point reinstalling it
107          * on rescan.
108          */
109         if (device_get_state(parent) == DS_ATTACHED)
110                 return (0);
111         if (device_get_state(parent) == DS_INPROGRESS)
112                 return (0);
113
114         /*
115          * Add child device with order of 11 so it gets probed
116          * after ACPI (which is at order 10).
117          */
118         if (BUS_ADD_CHILD(parent, parent, 11, "legacy", 0) == NULL)
119                 panic("legacy: could not attach");
120         return (0);
121 }
122
123 static int
124 legacy_probe(device_t dev)
125 {
126 #ifdef ACPI_ENABLE_PCI
127         device_t acpi;
128
129         /*
130          * Fail to probe if ACPI is ok.
131          */
132         acpi = devclass_get_device(devclass_find("acpi"), 0);
133         if (acpi != NULL && device_is_alive(acpi))
134                 return (ENXIO);
135 #endif  /* ACPI_ENABLE_PCI */
136         device_set_desc(dev, "legacy system");
137         device_quiet(dev);
138         return (0);
139 }
140
141 static int
142 legacy_attach(device_t dev)
143 {
144         device_t child;
145
146         /*
147          * Let our child drivers identify any child devices that they
148          * 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 EISA or ISA on a pci bridge, create some
156          * connection points now so they show up "on motherboard".
157          */
158         if (!devclass_get_device(devclass_find("eisa"), 0)) {
159                 child = BUS_ADD_CHILD(dev, dev, 0, "eisa", 0);
160                 if (child == NULL)
161                         panic("legacy_attach eisa");
162                 device_probe_and_attach(child);
163         }
164 #ifdef DEV_MCA
165         if (MCA_system && !devclass_get_device(devclass_find("mca"), 0)) {
166                 child = BUS_ADD_CHILD(dev, dev, 0, "mca", 0);
167                 if (child == 0)
168                         panic("legacy_probe mca");
169                 device_probe_and_attach(child);
170         }
171 #endif
172         if (!devclass_get_device(devclass_find("isa"), 0)) {
173                 child = BUS_ADD_CHILD(dev, dev, 0, "isa", 0);
174                 if (child == NULL)
175                         panic("legacy_attach isa");
176                 device_probe_and_attach(child);
177         }
178
179         return 0;
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         if (atdev->lg_pcibus != -1)
190                 retval += kprintf(" pcibus %d", atdev->lg_pcibus);
191         retval += kprintf(" on motherboard\n"); /* XXX "motherboard", ick */
192
193         return (retval);
194 }
195
196 static device_t
197 legacy_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
198 {
199         device_t child;
200         struct legacy_device *atdev;
201         atdev = kmalloc(sizeof(struct legacy_device), M_LEGACYDEV,
202             M_NOWAIT | M_ZERO);
203         if (atdev == NULL)
204                 return(NULL);
205         atdev->lg_pcibus = -1;
206
207         child = device_add_child_ordered(bus, order, name, unit);
208         if (child == NULL) {
209                 kfree(atdev, M_LEGACYDEV);
210         }
211         else {
212                 /* should we kfree 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_PCIDOMAIN:
225                 *result = 0;
226                 break;
227         case LEGACY_IVAR_PCIBUS:
228                 *result = atdev->lg_pcibus;
229                 break;
230         default:
231                 return ENOENT;
232         }
233         return 0;
234 }
235
236 static int
237 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
238 {
239         struct legacy_device *atdev = DEVTOAT(child);
240
241         switch (which) {
242         case LEGACY_IVAR_PCIDOMAIN:
243                 return EINVAL;
244         case LEGACY_IVAR_PCIBUS:
245                 atdev->lg_pcibus = value;
246                 break;
247         default:
248                 return ENOENT;
249         }
250         return 0;
251 }
252
253 #ifdef notyet
254 /*
255  * Legacy CPU attachment when ACPI is not available.  Drivers like
256  * cpufreq(4) hang off this.
257  */
258 static void     cpu_identify(driver_t *driver, device_t parent);
259 static int      cpu_read_ivar(device_t dev, device_t child, int index,
260                     uintptr_t *result);
261 static device_t cpu_add_child(device_t bus, int order, const char *name,
262                     int unit);
263 static struct resource_list *cpu_get_rlist(device_t dev, device_t child);
264
265 struct cpu_device {
266         struct resource_list cd_rl;
267         struct pcpu *cd_pcpu;
268 };
269
270 static device_method_t cpu_methods[] = {
271         /* Device interface */
272         DEVMETHOD(device_identify,      cpu_identify),
273         DEVMETHOD(device_probe,         bus_generic_probe),
274         DEVMETHOD(device_attach,        bus_generic_attach),
275         DEVMETHOD(device_detach,        bus_generic_detach),
276         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
277         DEVMETHOD(device_suspend,       bus_generic_suspend),
278         DEVMETHOD(device_resume,        bus_generic_resume),
279
280         /* Bus interface */
281         DEVMETHOD(bus_add_child,        cpu_add_child),
282         DEVMETHOD(bus_read_ivar,        cpu_read_ivar),
283         DEVMETHOD(bus_print_child,      bus_generic_print_child),
284         DEVMETHOD(bus_get_resource_list, cpu_get_rlist),
285         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
286         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
287         DEVMETHOD(bus_alloc_resource,   bus_generic_rl_alloc_resource),
288         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
289         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
290         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
291         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
292         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
293         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
294
295         { 0, 0 }
296 };
297
298 static driver_t cpu_driver = {
299         "cpu",
300         cpu_methods,
301         1,              /* no softc */
302 };
303 static devclass_t cpu_devclass;
304 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0);
305
306 static void
307 cpu_identify(driver_t *driver, device_t parent)
308 {
309         device_t child;
310         int i;
311
312         /*
313          * Attach a cpuX device for each CPU.  We use an order of 150
314          * so that these devices are attached after the Host-PCI
315          * bridges (which are added at order 100).
316          */
317         for (i = 0; i <= mp_maxid; i++)
318                 if (!CPU_ABSENT(i)) {
319                         child = BUS_ADD_CHILD(parent, 150, "cpu", i);
320                         if (child == NULL)
321                                 panic("legacy_attach cpu");
322                 }
323 }
324
325 static device_t
326 cpu_add_child(device_t bus, int order, const char *name, int unit)
327 {
328         struct cpu_device *cd;
329         device_t child;
330         struct pcpu *pc;
331
332         if ((cd = kmalloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
333                 return (NULL);
334
335         resource_list_init(&cd->cd_rl);
336         pc = pcpu_find(device_get_unit(bus));
337         cd->cd_pcpu = pc;
338
339         child = device_add_child_ordered(bus, order, name, unit);
340         if (child != NULL) {
341                 pc->pc_device = child;
342                 device_set_ivars(child, cd);
343         } else
344                 kfree(cd, M_DEVBUF);
345         return (child);
346 }
347
348 static struct resource_list *
349 cpu_get_rlist(device_t dev, device_t child)
350 {
351         struct cpu_device *cpdev;
352
353         cpdev = device_get_ivars(child);
354         return (&cpdev->cd_rl);
355 }
356
357 static int
358 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
359 {
360         struct cpu_device *cpdev;
361
362         if (index != CPU_IVAR_PCPU)
363                 return (ENOENT);
364         cpdev = device_get_ivars(child);
365         *result = (uintptr_t)cpdev->cd_pcpu;
366         return (0);
367 }
368 #endif