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