]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/i386/i386/legacy.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / i386 / 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  */
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 "opt_mca.h"
51 #ifdef DEV_MCA
52 #include <i386/bios/mca_machdep.h>
53 #endif
54
55 #include <machine/clock.h>
56 #include <machine/legacyvar.h>
57 #include <machine/resource.h>
58
59 static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device");
60 struct legacy_device {
61         int     lg_pcibus;
62         int     lg_pcislot;
63         int     lg_pcifunc;
64 };
65
66 #define DEVTOAT(dev)    ((struct legacy_device *)device_get_ivars(dev))
67
68 static  int legacy_probe(device_t);
69 static  int legacy_attach(device_t);
70 static  int legacy_print_child(device_t, device_t);
71 static device_t legacy_add_child(device_t bus, u_int order, const char *name,
72                                 int unit);
73 static  int legacy_read_ivar(device_t, device_t, int, uintptr_t *);
74 static  int legacy_write_ivar(device_t, device_t, int, uintptr_t);
75
76 static device_method_t legacy_methods[] = {
77         /* Device interface */
78         DEVMETHOD(device_probe,         legacy_probe),
79         DEVMETHOD(device_attach,        legacy_attach),
80         DEVMETHOD(device_detach,        bus_generic_detach),
81         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
82         DEVMETHOD(device_suspend,       bus_generic_suspend),
83         DEVMETHOD(device_resume,        bus_generic_resume),
84
85         /* Bus interface */
86         DEVMETHOD(bus_print_child,      legacy_print_child),
87         DEVMETHOD(bus_add_child,        legacy_add_child),
88         DEVMETHOD(bus_read_ivar,        legacy_read_ivar),
89         DEVMETHOD(bus_write_ivar,       legacy_write_ivar),
90         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
91         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
92         DEVMETHOD(bus_release_resource, bus_generic_release_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 int
111 legacy_probe(device_t dev)
112 {
113
114         device_set_desc(dev, "legacy system");
115         device_quiet(dev);
116         return (0);
117 }
118
119 static int
120 legacy_attach(device_t dev)
121 {
122         device_t child;
123
124         /*
125          * Let our child drivers identify any child devices that they
126          * can find.  Once that is done attach any devices that we
127          * found.
128          */
129         bus_generic_probe(dev);
130         bus_generic_attach(dev);
131
132 #ifndef PC98
133         /*
134          * If we didn't see EISA or ISA on a pci bridge, create some
135          * connection points now so they show up "on motherboard".
136          */
137         if (!devclass_get_device(devclass_find("eisa"), 0)) {
138                 child = BUS_ADD_CHILD(dev, 0, "eisa", 0);
139                 if (child == NULL)
140                         panic("legacy_attach eisa");
141                 device_probe_and_attach(child);
142         }
143 #endif
144 #ifdef DEV_MCA
145         if (MCA_system && !devclass_get_device(devclass_find("mca"), 0)) {
146                 child = BUS_ADD_CHILD(dev, 0, "mca", 0);
147                 if (child == 0)
148                         panic("legacy_probe mca");
149                 device_probe_and_attach(child);
150         }
151 #endif
152         if (!devclass_get_device(devclass_find("isa"), 0)) {
153                 child = BUS_ADD_CHILD(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 += printf(" pcibus %d", atdev->lg_pcibus);
171         retval += printf(" on motherboard\n");  /* XXX "motherboard", ick */
172
173         return (retval);
174 }
175
176 static device_t
177 legacy_add_child(device_t bus, u_int order, const char *name, int unit)
178 {
179         device_t child;
180         struct legacy_device *atdev;
181
182         atdev = malloc(sizeof(struct legacy_device), M_LEGACYDEV,
183             M_NOWAIT | M_ZERO);
184         if (atdev == NULL)
185                 return(NULL);
186         atdev->lg_pcibus = -1;
187         atdev->lg_pcislot = -1;
188         atdev->lg_pcifunc = -1;
189
190         child = device_add_child_ordered(bus, order, name, unit);
191         if (child == NULL)
192                 free(atdev, M_LEGACYDEV);
193         else
194                 /* should we free this in legacy_child_detached? */
195                 device_set_ivars(child, atdev);
196
197         return (child);
198 }
199
200 static int
201 legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
202 {
203         struct legacy_device *atdev = DEVTOAT(child);
204
205         switch (which) {
206         case LEGACY_IVAR_PCIDOMAIN:
207                 *result = 0;
208                 break;
209         case LEGACY_IVAR_PCIBUS:
210                 *result = atdev->lg_pcibus;
211                 break;
212         case LEGACY_IVAR_PCISLOT:
213                 *result = atdev->lg_pcislot;
214                 break;
215         case LEGACY_IVAR_PCIFUNC:
216                 *result = atdev->lg_pcifunc;
217                 break;
218         default:
219                 return ENOENT;
220         }
221         return 0;
222 }
223         
224
225 static int
226 legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
227 {
228         struct legacy_device *atdev = DEVTOAT(child);
229
230         switch (which) {
231         case LEGACY_IVAR_PCIDOMAIN:
232                 return EINVAL;
233         case LEGACY_IVAR_PCIBUS:
234                 atdev->lg_pcibus = value;
235                 break;
236         case LEGACY_IVAR_PCISLOT:
237                 atdev->lg_pcislot = value;
238                 break;
239         case LEGACY_IVAR_PCIFUNC:
240                 atdev->lg_pcifunc = value;
241                 break;
242         default:
243                 return ENOENT;
244         }
245         return 0;
246 }
247
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, u_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_get_resource_list, cpu_get_rlist),
278         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
279         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
280         DEVMETHOD(bus_alloc_resource,   bus_generic_rl_alloc_resource),
281         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
282         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
283         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
284         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
285         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
286
287         DEVMETHOD_END
288 };
289
290 static driver_t cpu_driver = {
291         "cpu",
292         cpu_methods,
293         1,              /* no softc */
294 };
295 static devclass_t cpu_devclass;
296 DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0);
297
298 static void
299 cpu_identify(driver_t *driver, device_t parent)
300 {
301         device_t child;
302         int i;
303
304         /*
305          * Attach a cpuX device for each CPU.  We use an order of 150
306          * so that these devices are attached after the Host-PCI
307          * bridges (which are added at order 100).
308          */
309         CPU_FOREACH(i) {
310                 child = BUS_ADD_CHILD(parent, 150, "cpu", i);
311                 if (child == NULL)
312                         panic("legacy_attach cpu");
313         }
314 }
315
316 static device_t
317 cpu_add_child(device_t bus, u_int order, const char *name, int unit)
318 {
319         struct cpu_device *cd;
320         device_t child;
321         struct pcpu *pc;
322
323         if ((cd = malloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
324                 return (NULL);
325
326         resource_list_init(&cd->cd_rl);
327         pc = pcpu_find(device_get_unit(bus));
328         cd->cd_pcpu = pc;
329
330         child = device_add_child_ordered(bus, order, name, unit);
331         if (child != NULL) {
332                 pc->pc_device = child;
333                 device_set_ivars(child, cd);
334         } else
335                 free(cd, M_DEVBUF);
336         return (child);
337 }
338
339 static struct resource_list *
340 cpu_get_rlist(device_t dev, device_t child)
341 {
342         struct cpu_device *cpdev;
343
344         cpdev = device_get_ivars(child);
345         return (&cpdev->cd_rl);
346 }
347
348 static int
349 cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
350 {
351         struct cpu_device *cpdev;
352
353         switch (index) {
354         case CPU_IVAR_PCPU:
355                 cpdev = device_get_ivars(child);
356                 *result = (uintptr_t)cpdev->cd_pcpu;
357                 break;
358         case CPU_IVAR_NOMINAL_MHZ:
359                 if (tsc_is_invariant) {
360                         *result = (uintptr_t)(atomic_load_acq_64(&tsc_freq) /
361                             1000000);
362                         break;
363                 }
364                 /* FALLTHROUGH */
365         default:
366                 return (ENOENT);
367         }
368         return (0);
369 }