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