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