]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/x86/x86/mptable_pci.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / x86 / x86 / mptable_pci.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * Host to PCI and PCI to PCI bridge drivers that use the MP Table to route
32  * interrupts from PCI devices to I/O APICs.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcib_private.h>
48 #include <x86/mptable.h>
49 #include <machine/legacyvar.h>
50 #include <machine/pci_cfgreg.h>
51
52 #include "pcib_if.h"
53
54 /* Host to PCI bridge driver. */
55
56 static int
57 mptable_hostb_probe(device_t dev)
58 {
59
60         if (pci_cfgregopen() == 0)
61                 return (ENXIO);
62         if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
63                 return (ENXIO);
64         device_set_desc(dev, "MPTable Host-PCI bridge");
65         return (0);
66 }
67
68 static int
69 mptable_hostb_attach(device_t dev)
70 {
71
72 #ifdef NEW_PCIB
73         mptable_pci_host_res_init(dev);
74 #endif
75         device_add_child(dev, "pci", pcib_get_bus(dev));
76         return (bus_generic_attach(dev));
77 }
78
79 /* Pass MSI requests up to the nexus. */
80 static int
81 mptable_hostb_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
82     int *irqs)
83 {
84         device_t bus;
85
86         bus = device_get_parent(pcib);
87         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
88             irqs));
89 }
90
91 static int
92 mptable_hostb_alloc_msix(device_t pcib, device_t dev, int *irq)
93 {
94         device_t bus;
95
96         bus = device_get_parent(pcib);
97         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
98 }
99
100 static int
101 mptable_hostb_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
102     uint32_t *data)
103 {
104         device_t bus;
105
106         bus = device_get_parent(pcib);
107         return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
108 }
109
110 #ifdef NEW_PCIB
111 static int
112 mptable_is_isa_range(u_long start, u_long end)
113 {
114
115         if (end >= 0x10000)
116                 return (0);
117         if ((start & 0xfc00) != (end & 0xfc00))
118                 return (0);
119         start &= ~0xfc00;
120         end &= ~0xfc00;
121         return (start >= 0x100 && end <= 0x3ff);
122 }
123
124 static int
125 mptable_is_vga_range(u_long start, u_long end)
126 {
127         if (end >= 0x10000)
128                 return (0);
129         if ((start & 0xfc00) != (end & 0xfc00))
130                 return (0);
131         start &= ~0xfc00;
132         end &= ~0xfc00;
133         return (pci_is_vga_ioport_range(start, end));
134 }
135
136 static struct resource *
137 mptable_hostb_alloc_resource(device_t dev, device_t child, int type, int *rid,
138     u_long start, u_long end, u_long count, u_int flags)
139 {
140         struct mptable_hostb_softc *sc;
141
142         sc = device_get_softc(dev);
143         if (type == SYS_RES_IOPORT && start + count - 1 == end) {
144                 if (mptable_is_isa_range(start, end)) {
145                         switch (sc->sc_decodes_isa_io) {
146                         case -1:
147                                 return (NULL);
148                         case 1:
149                                 return (bus_generic_alloc_resource(dev, child,
150                                     type, rid, start, end, count, flags));
151                         default:
152                                 break;
153                         }
154                 }
155                 if (mptable_is_vga_range(start, end)) {
156                         switch (sc->sc_decodes_vga_io) {
157                         case -1:
158                                 return (NULL);
159                         case 1:
160                                 return (bus_generic_alloc_resource(dev, child,
161                                     type, rid, start, end, count, flags));
162                         default:
163                                 break;
164                         }
165                 }
166         }
167         start = hostb_alloc_start(type, start, end, count);
168         return (pcib_host_res_alloc(&sc->sc_host_res, child, type, rid, start,
169             end, count, flags));
170 }
171
172 static int
173 mptable_hostb_adjust_resource(device_t dev, device_t child, int type,
174     struct resource *r, u_long start, u_long end)
175 {
176         struct mptable_hostb_softc *sc;
177
178         sc = device_get_softc(dev);
179         return (pcib_host_res_adjust(&sc->sc_host_res, child, type, r, start,
180             end));
181 }
182 #endif
183
184 static device_method_t mptable_hostb_methods[] = {
185         /* Device interface */
186         DEVMETHOD(device_probe,         mptable_hostb_probe),
187         DEVMETHOD(device_attach,        mptable_hostb_attach),
188         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
189         DEVMETHOD(device_suspend,       bus_generic_suspend),
190         DEVMETHOD(device_resume,        bus_generic_resume),
191
192         /* Bus interface */
193         DEVMETHOD(bus_print_child,      bus_generic_print_child),
194         DEVMETHOD(bus_read_ivar,        legacy_pcib_read_ivar),
195         DEVMETHOD(bus_write_ivar,       legacy_pcib_write_ivar),
196 #ifdef NEW_PCIB
197         DEVMETHOD(bus_alloc_resource,   mptable_hostb_alloc_resource),
198         DEVMETHOD(bus_adjust_resource,  mptable_hostb_adjust_resource),
199 #else
200         DEVMETHOD(bus_alloc_resource,   legacy_pcib_alloc_resource),
201         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
202 #endif
203         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
204         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
205         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
206         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
207         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
208
209         /* pcib interface */
210         DEVMETHOD(pcib_maxslots,        legacy_pcib_maxslots),
211         DEVMETHOD(pcib_read_config,     legacy_pcib_read_config),
212         DEVMETHOD(pcib_write_config,    legacy_pcib_write_config),
213         DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
214         DEVMETHOD(pcib_alloc_msi,       mptable_hostb_alloc_msi),
215         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
216         DEVMETHOD(pcib_alloc_msix,      mptable_hostb_alloc_msix),
217         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
218         DEVMETHOD(pcib_map_msi,         mptable_hostb_map_msi),
219
220         { 0, 0 }
221 };
222
223 static devclass_t hostb_devclass;
224
225 DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods,
226     sizeof(struct mptable_hostb_softc));
227 DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, 0, 0);
228
229 /* PCI to PCI bridge driver. */
230
231 static int
232 mptable_pcib_probe(device_t dev)
233 {
234         int bus;
235
236         if ((pci_get_class(dev) != PCIC_BRIDGE) ||
237             (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
238                 return (ENXIO);
239         bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
240         if (bus == 0)
241                 return (ENXIO);
242         if (mptable_pci_probe_table(bus) != 0)
243                 return (ENXIO);
244         device_set_desc(dev, "MPTable PCI-PCI bridge");
245         return (-1000);
246 }
247
248 static device_method_t mptable_pcib_pci_methods[] = {
249         /* Device interface */
250         DEVMETHOD(device_probe,         mptable_pcib_probe),
251
252         /* pcib interface */
253         DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
254
255         {0, 0}
256 };
257
258 static devclass_t pcib_devclass;
259
260 DEFINE_CLASS_1(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
261     sizeof(struct pcib_softc), pcib_driver);
262 DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, 0, 0);