mptable_pci.c revision 233676
1193323Sed/*-
2193323Sed * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed * 3. Neither the name of the author nor the names of any co-contributors
14193323Sed *    may be used to endorse or promote products derived from this software
15252723Sdim *    without specific prior written permission.
16252723Sdim *
17193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18226890Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23226890Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24226890Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25235633Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26235633Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27235633Sdim * SUCH DAMAGE.
28235633Sdim */
29224145Sdim
30245431Sdim/*
31199481Srdivacky * Host to PCI and PCI to PCI bridge drivers that use the MP Table to route
32193323Sed * interrupts from PCI devices to I/O APICs.
33218893Sdim */
34208599Srdivacky
35252723Sdim#include <sys/cdefs.h>
36263509Sdim__FBSDID("$FreeBSD: head/sys/x86/x86/mptable_pci.c 233676 2012-03-29 19:03:22Z jhb $");
37193323Sed
38193323Sed#include <sys/param.h>
39235633Sdim#include <sys/systm.h>
40235633Sdim#include <sys/bus.h>
41235633Sdim#include <sys/kernel.h>
42235633Sdim#include <sys/module.h>
43235633Sdim#include <sys/rman.h>
44235633Sdim
45235633Sdim#include <dev/pci/pcireg.h>
46235633Sdim#include <dev/pci/pcivar.h>
47235633Sdim#include <dev/pci/pcib_private.h>
48235633Sdim#include <x86/mptable.h>
49235633Sdim#include <machine/legacyvar.h>
50252723Sdim#include <machine/pci_cfgreg.h>
51235633Sdim
52235633Sdim#include "pcib_if.h"
53235633Sdim
54235633Sdim/* Host to PCI bridge driver. */
55235633Sdim
56235633Sdimstatic int
57235633Sdimmptable_hostb_probe(device_t dev)
58235633Sdim{
59252723Sdim
60252723Sdim	if (pci_cfgregopen() == 0)
61252723Sdim		return (ENXIO);
62252723Sdim	if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
63252723Sdim		return (ENXIO);
64235633Sdim	device_set_desc(dev, "MPTable Host-PCI bridge");
65245431Sdim	return (0);
66193323Sed}
67193323Sed
68193323Sedstatic int
69198090Srdivackymptable_hostb_attach(device_t dev)
70198090Srdivacky{
71198090Srdivacky
72193323Sed#ifdef NEW_PCIB
73263509Sdim	mptable_pci_host_res_init(dev);
74263509Sdim#endif
75263509Sdim	device_add_child(dev, "pci", pcib_get_bus(dev));
76263509Sdim	return (bus_generic_attach(dev));
77263509Sdim}
78263509Sdim
79263509Sdim/* Pass MSI requests up to the nexus. */
80263509Sdimstatic int
81mptable_hostb_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
82    int *irqs)
83{
84	device_t bus;
85
86	bus = device_get_parent(pcib);
87	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
88	    irqs));
89}
90
91static int
92mptable_hostb_alloc_msix(device_t pcib, device_t dev, int *irq)
93{
94	device_t bus;
95
96	bus = device_get_parent(pcib);
97	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
98}
99
100#ifdef NEW_PCIB
101static int
102mptable_is_isa_range(u_long start, u_long end)
103{
104
105	if (end >= 0x10000)
106		return (0);
107	if ((start & 0xfc00) != (end & 0xfc00))
108		return (0);
109	start &= ~0xfc00;
110	end &= ~0xfc00;
111	return (start >= 0x100 && end <= 0x3ff);
112}
113
114static int
115mptable_is_vga_range(u_long start, u_long end)
116{
117	if (end >= 0x10000)
118		return (0);
119	if ((start & 0xfc00) != (end & 0xfc00))
120		return (0);
121	start &= ~0xfc00;
122	end &= ~0xfc00;
123	return (pci_is_vga_ioport_range(start, end));
124}
125
126static struct resource *
127mptable_hostb_alloc_resource(device_t dev, device_t child, int type, int *rid,
128    u_long start, u_long end, u_long count, u_int flags)
129{
130	struct mptable_hostb_softc *sc;
131
132	sc = device_get_softc(dev);
133	if (type == SYS_RES_IOPORT && start + count - 1 == end) {
134		if (mptable_is_isa_range(start, end)) {
135			switch (sc->sc_decodes_isa_io) {
136			case -1:
137				return (NULL);
138			case 1:
139				return (bus_generic_alloc_resource(dev, child,
140				    type, rid, start, end, count, flags));
141			default:
142				break;
143			}
144		}
145		if (mptable_is_vga_range(start, end)) {
146			switch (sc->sc_decodes_vga_io) {
147			case -1:
148				return (NULL);
149			case 1:
150				return (bus_generic_alloc_resource(dev, child,
151				    type, rid, start, end, count, flags));
152			default:
153				break;
154			}
155		}
156	}
157	start = hostb_alloc_start(type, start, end, count);
158	return (pcib_host_res_alloc(&sc->sc_host_res, child, type, rid, start,
159	    end, count, flags));
160}
161
162static int
163mptable_hostb_adjust_resource(device_t dev, device_t child, int type,
164    struct resource *r, u_long start, u_long end)
165{
166	struct mptable_hostb_softc *sc;
167
168	sc = device_get_softc(dev);
169	return (pcib_host_res_adjust(&sc->sc_host_res, child, type, r, start,
170	    end));
171}
172#endif
173
174static device_method_t mptable_hostb_methods[] = {
175	/* Device interface */
176	DEVMETHOD(device_probe,		mptable_hostb_probe),
177	DEVMETHOD(device_attach,	mptable_hostb_attach),
178	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
179	DEVMETHOD(device_suspend,	bus_generic_suspend),
180	DEVMETHOD(device_resume,	bus_generic_resume),
181
182	/* Bus interface */
183	DEVMETHOD(bus_read_ivar,	legacy_pcib_read_ivar),
184	DEVMETHOD(bus_write_ivar,	legacy_pcib_write_ivar),
185#ifdef NEW_PCIB
186	DEVMETHOD(bus_alloc_resource,	mptable_hostb_alloc_resource),
187	DEVMETHOD(bus_adjust_resource,	mptable_hostb_adjust_resource),
188#else
189	DEVMETHOD(bus_alloc_resource,	legacy_pcib_alloc_resource),
190	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
191#endif
192	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
193	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
194	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
195	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
196	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
197
198	/* pcib interface */
199	DEVMETHOD(pcib_maxslots,	legacy_pcib_maxslots),
200	DEVMETHOD(pcib_read_config,	legacy_pcib_read_config),
201	DEVMETHOD(pcib_write_config,	legacy_pcib_write_config),
202	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
203	DEVMETHOD(pcib_alloc_msi,	mptable_hostb_alloc_msi),
204	DEVMETHOD(pcib_release_msi,	pcib_release_msi),
205	DEVMETHOD(pcib_alloc_msix,	mptable_hostb_alloc_msix),
206	DEVMETHOD(pcib_release_msix,	pcib_release_msix),
207	DEVMETHOD(pcib_map_msi,		legacy_pcib_map_msi),
208
209	DEVMETHOD_END
210};
211
212static devclass_t hostb_devclass;
213
214DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods,
215    sizeof(struct mptable_hostb_softc));
216DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, 0, 0);
217
218/* PCI to PCI bridge driver. */
219
220static int
221mptable_pcib_probe(device_t dev)
222{
223	int bus;
224
225	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
226	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
227		return (ENXIO);
228	bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
229	if (bus == 0)
230		return (ENXIO);
231	if (mptable_pci_probe_table(bus) != 0)
232		return (ENXIO);
233	device_set_desc(dev, "MPTable PCI-PCI bridge");
234	return (-1000);
235}
236
237static device_method_t mptable_pcib_pci_methods[] = {
238	/* Device interface */
239	DEVMETHOD(device_probe,		mptable_pcib_probe),
240
241	/* pcib interface */
242	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
243
244	{0, 0}
245};
246
247static devclass_t pcib_devclass;
248
249DEFINE_CLASS_1(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
250    sizeof(struct pcib_softc), pcib_driver);
251DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, 0, 0);
252