xref: /freebsd-src/sys/x86/x86/mptable_pci.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Host to PCI and PCI to PCI bridge drivers that use the MP Table to route
29  * interrupts from PCI devices to I/O APICs.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcib_private.h>
45 #include <x86/mptable.h>
46 #include <x86/legacyvar.h>
47 #include <machine/pci_cfgreg.h>
48 
49 #include "pcib_if.h"
50 
51 /* Host to PCI bridge driver. */
52 
53 static int
54 mptable_hostb_probe(device_t dev)
55 {
56 
57 	if (pci_cfgregopen() == 0)
58 		return (ENXIO);
59 	if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
60 		return (ENXIO);
61 	device_set_desc(dev, "MPTable Host-PCI bridge");
62 	return (0);
63 }
64 
65 static int
66 mptable_hostb_attach(device_t dev)
67 {
68 
69 #ifdef NEW_PCIB
70 	mptable_pci_host_res_init(dev);
71 #endif
72 	device_add_child(dev, "pci", -1);
73 	return (bus_generic_attach(dev));
74 }
75 
76 #ifdef NEW_PCIB
77 static int
78 mptable_is_isa_range(rman_res_t start, rman_res_t end)
79 {
80 
81 	if (end >= 0x10000)
82 		return (0);
83 	if ((start & 0xfc00) != (end & 0xfc00))
84 		return (0);
85 	start &= ~0xfc00;
86 	end &= ~0xfc00;
87 	return (start >= 0x100 && end <= 0x3ff);
88 }
89 
90 static int
91 mptable_is_vga_range(rman_res_t start, rman_res_t end)
92 {
93 	if (end >= 0x10000)
94 		return (0);
95 	if ((start & 0xfc00) != (end & 0xfc00))
96 		return (0);
97 	start &= ~0xfc00;
98 	end &= ~0xfc00;
99 	return (pci_is_vga_ioport_range(start, end));
100 }
101 
102 static struct resource *
103 mptable_hostb_alloc_resource(device_t dev, device_t child, int type, int *rid,
104     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
105 {
106 	struct mptable_hostb_softc *sc;
107 
108 #ifdef PCI_RES_BUS
109 	if (type == PCI_RES_BUS)
110 		return (pci_domain_alloc_bus(0, child, rid, start, end, count,
111 		    flags));
112 #endif
113 	sc = device_get_softc(dev);
114 	if (type == SYS_RES_IOPORT && start + count - 1 == end) {
115 		if (mptable_is_isa_range(start, end)) {
116 			switch (sc->sc_decodes_isa_io) {
117 			case -1:
118 				return (NULL);
119 			case 1:
120 				return (bus_generic_alloc_resource(dev, child,
121 				    type, rid, start, end, count, flags));
122 			default:
123 				break;
124 			}
125 		}
126 		if (mptable_is_vga_range(start, end)) {
127 			switch (sc->sc_decodes_vga_io) {
128 			case -1:
129 				return (NULL);
130 			case 1:
131 				return (bus_generic_alloc_resource(dev, child,
132 				    type, rid, start, end, count, flags));
133 			default:
134 				break;
135 			}
136 		}
137 	}
138 	start = hostb_alloc_start(type, start, end, count);
139 	return (pcib_host_res_alloc(&sc->sc_host_res, child, type, rid, start,
140 	    end, count, flags));
141 }
142 
143 static int
144 mptable_hostb_adjust_resource(device_t dev, device_t child, int type,
145     struct resource *r, rman_res_t start, rman_res_t end)
146 {
147 	struct mptable_hostb_softc *sc;
148 
149 #ifdef PCI_RES_BUS
150 	if (type == PCI_RES_BUS)
151 		return (pci_domain_adjust_bus(0, child, r, start, end));
152 #endif
153 	sc = device_get_softc(dev);
154 	return (pcib_host_res_adjust(&sc->sc_host_res, child, type, r, start,
155 	    end));
156 }
157 #endif
158 
159 static device_method_t mptable_hostb_methods[] = {
160 	/* Device interface */
161 	DEVMETHOD(device_probe,		mptable_hostb_probe),
162 	DEVMETHOD(device_attach,	mptable_hostb_attach),
163 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
164 	DEVMETHOD(device_suspend,	bus_generic_suspend),
165 	DEVMETHOD(device_resume,	bus_generic_resume),
166 
167 	/* Bus interface */
168 	DEVMETHOD(bus_read_ivar,	legacy_pcib_read_ivar),
169 	DEVMETHOD(bus_write_ivar,	legacy_pcib_write_ivar),
170 #ifdef NEW_PCIB
171 	DEVMETHOD(bus_alloc_resource,	mptable_hostb_alloc_resource),
172 	DEVMETHOD(bus_adjust_resource,	mptable_hostb_adjust_resource),
173 #else
174 	DEVMETHOD(bus_alloc_resource,	legacy_pcib_alloc_resource),
175 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
176 #endif
177 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
178 	DEVMETHOD(bus_release_resource,	legacy_pcib_release_resource),
179 #else
180 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
181 #endif
182 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
183 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
184 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
185 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
186 
187 	/* pcib interface */
188 	DEVMETHOD(pcib_maxslots,	legacy_pcib_maxslots),
189 	DEVMETHOD(pcib_read_config,	legacy_pcib_read_config),
190 	DEVMETHOD(pcib_write_config,	legacy_pcib_write_config),
191 	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
192 	DEVMETHOD(pcib_alloc_msi,	legacy_pcib_alloc_msi),
193 	DEVMETHOD(pcib_release_msi,	pcib_release_msi),
194 	DEVMETHOD(pcib_alloc_msix,	legacy_pcib_alloc_msix),
195 	DEVMETHOD(pcib_release_msix,	pcib_release_msix),
196 	DEVMETHOD(pcib_map_msi,		legacy_pcib_map_msi),
197 
198 	DEVMETHOD_END
199 };
200 
201 static devclass_t hostb_devclass;
202 
203 DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods,
204     sizeof(struct mptable_hostb_softc));
205 DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, 0, 0);
206 
207 /* PCI to PCI bridge driver. */
208 
209 static int
210 mptable_pcib_probe(device_t dev)
211 {
212 	int bus;
213 
214 	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
215 	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
216 		return (ENXIO);
217 	bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
218 	if (bus == 0)
219 		return (ENXIO);
220 	if (mptable_pci_probe_table(bus) != 0)
221 		return (ENXIO);
222 	device_set_desc(dev, "MPTable PCI-PCI bridge");
223 	return (-1000);
224 }
225 
226 static device_method_t mptable_pcib_pci_methods[] = {
227 	/* Device interface */
228 	DEVMETHOD(device_probe,		mptable_pcib_probe),
229 
230 	/* pcib interface */
231 	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
232 
233 	{0, 0}
234 };
235 
236 static devclass_t pcib_devclass;
237 
238 DEFINE_CLASS_1(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
239     sizeof(struct pcib_softc), pcib_driver);
240 DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, 0, 0);
241