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