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