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