mptable_pci.c revision 260973
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: head/sys/x86/x86/mptable_pci.c 260973 2014-01-21 03:14:19Z jhb $");
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 <x86/legacyvar.h>
50#include <machine/pci_cfgreg.h>
51
52#include "pcib_if.h"
53
54/* Host to PCI bridge driver. */
55
56static int
57mptable_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
68static int
69mptable_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#ifdef NEW_PCIB
80static int
81mptable_is_isa_range(u_long start, u_long end)
82{
83
84	if (end >= 0x10000)
85		return (0);
86	if ((start & 0xfc00) != (end & 0xfc00))
87		return (0);
88	start &= ~0xfc00;
89	end &= ~0xfc00;
90	return (start >= 0x100 && end <= 0x3ff);
91}
92
93static int
94mptable_is_vga_range(u_long start, u_long end)
95{
96	if (end >= 0x10000)
97		return (0);
98	if ((start & 0xfc00) != (end & 0xfc00))
99		return (0);
100	start &= ~0xfc00;
101	end &= ~0xfc00;
102	return (pci_is_vga_ioport_range(start, end));
103}
104
105static struct resource *
106mptable_hostb_alloc_resource(device_t dev, device_t child, int type, int *rid,
107    u_long start, u_long end, u_long count, u_int flags)
108{
109	struct mptable_hostb_softc *sc;
110
111	sc = device_get_softc(dev);
112	if (type == SYS_RES_IOPORT && start + count - 1 == end) {
113		if (mptable_is_isa_range(start, end)) {
114			switch (sc->sc_decodes_isa_io) {
115			case -1:
116				return (NULL);
117			case 1:
118				return (bus_generic_alloc_resource(dev, child,
119				    type, rid, start, end, count, flags));
120			default:
121				break;
122			}
123		}
124		if (mptable_is_vga_range(start, end)) {
125			switch (sc->sc_decodes_vga_io) {
126			case -1:
127				return (NULL);
128			case 1:
129				return (bus_generic_alloc_resource(dev, child,
130				    type, rid, start, end, count, flags));
131			default:
132				break;
133			}
134		}
135	}
136	start = hostb_alloc_start(type, start, end, count);
137	return (pcib_host_res_alloc(&sc->sc_host_res, child, type, rid, start,
138	    end, count, flags));
139}
140
141static int
142mptable_hostb_adjust_resource(device_t dev, device_t child, int type,
143    struct resource *r, u_long start, u_long end)
144{
145	struct mptable_hostb_softc *sc;
146
147	sc = device_get_softc(dev);
148	return (pcib_host_res_adjust(&sc->sc_host_res, child, type, r, start,
149	    end));
150}
151#endif
152
153static device_method_t mptable_hostb_methods[] = {
154	/* Device interface */
155	DEVMETHOD(device_probe,		mptable_hostb_probe),
156	DEVMETHOD(device_attach,	mptable_hostb_attach),
157	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
158	DEVMETHOD(device_suspend,	bus_generic_suspend),
159	DEVMETHOD(device_resume,	bus_generic_resume),
160
161	/* Bus interface */
162	DEVMETHOD(bus_read_ivar,	legacy_pcib_read_ivar),
163	DEVMETHOD(bus_write_ivar,	legacy_pcib_write_ivar),
164#ifdef NEW_PCIB
165	DEVMETHOD(bus_alloc_resource,	mptable_hostb_alloc_resource),
166	DEVMETHOD(bus_adjust_resource,	mptable_hostb_adjust_resource),
167#else
168	DEVMETHOD(bus_alloc_resource,	legacy_pcib_alloc_resource),
169	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
170#endif
171	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
172	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
173	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
174	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
175	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
176
177	/* pcib interface */
178	DEVMETHOD(pcib_maxslots,	legacy_pcib_maxslots),
179	DEVMETHOD(pcib_read_config,	legacy_pcib_read_config),
180	DEVMETHOD(pcib_write_config,	legacy_pcib_write_config),
181	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
182	DEVMETHOD(pcib_alloc_msi,	legacy_pcib_alloc_msi),
183	DEVMETHOD(pcib_release_msi,	pcib_release_msi),
184	DEVMETHOD(pcib_alloc_msix,	legacy_pcib_alloc_msix),
185	DEVMETHOD(pcib_release_msix,	pcib_release_msix),
186	DEVMETHOD(pcib_map_msi,		legacy_pcib_map_msi),
187
188	DEVMETHOD_END
189};
190
191static devclass_t hostb_devclass;
192
193DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods,
194    sizeof(struct mptable_hostb_softc));
195DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, 0, 0);
196
197/* PCI to PCI bridge driver. */
198
199static int
200mptable_pcib_probe(device_t dev)
201{
202	int bus;
203
204	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
205	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
206		return (ENXIO);
207	bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
208	if (bus == 0)
209		return (ENXIO);
210	if (mptable_pci_probe_table(bus) != 0)
211		return (ENXIO);
212	device_set_desc(dev, "MPTable PCI-PCI bridge");
213	return (-1000);
214}
215
216static device_method_t mptable_pcib_pci_methods[] = {
217	/* Device interface */
218	DEVMETHOD(device_probe,		mptable_pcib_probe),
219
220	/* pcib interface */
221	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
222
223	{0, 0}
224};
225
226static devclass_t pcib_devclass;
227
228DEFINE_CLASS_1(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
229    sizeof(struct pcib_softc), pcib_driver);
230DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, 0, 0);
231