xref: /freebsd/sys/x86/x86/mptable_pci.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
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/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcib_private.h>
43 #include <x86/mptable.h>
44 #include <x86/legacyvar.h>
45 #include <machine/pci_cfgreg.h>
46 
47 #include "pcib_if.h"
48 
49 /* Host to PCI bridge driver. */
50 
51 static int
mptable_hostb_probe(device_t dev)52 mptable_hostb_probe(device_t dev)
53 {
54 
55 	if (pci_cfgregopen() == 0)
56 		return (ENXIO);
57 	if (mptable_pci_probe_table(legacy_get_pcibus(dev)) != 0)
58 		return (ENXIO);
59 	device_set_desc(dev, "MPTable Host-PCI bridge");
60 	return (0);
61 }
62 
63 static int
mptable_hostb_attach(device_t dev)64 mptable_hostb_attach(device_t dev)
65 {
66 
67 	mptable_pci_host_res_init(dev);
68 	device_add_child(dev, "pci", DEVICE_UNIT_ANY);
69 	bus_attach_children(dev);
70 	return (0);
71 }
72 
73 static int
mptable_is_isa_range(rman_res_t start,rman_res_t end)74 mptable_is_isa_range(rman_res_t start, rman_res_t end)
75 {
76 
77 	if (end >= 0x10000)
78 		return (0);
79 	if ((start & 0xfc00) != (end & 0xfc00))
80 		return (0);
81 	start &= ~0xfc00;
82 	end &= ~0xfc00;
83 	return (start >= 0x100 && end <= 0x3ff);
84 }
85 
86 static int
mptable_is_vga_range(rman_res_t start,rman_res_t end)87 mptable_is_vga_range(rman_res_t start, rman_res_t end)
88 {
89 	if (end >= 0x10000)
90 		return (0);
91 	if ((start & 0xfc00) != (end & 0xfc00))
92 		return (0);
93 	start &= ~0xfc00;
94 	end &= ~0xfc00;
95 	return (pci_is_vga_ioport_range(start, end));
96 }
97 
98 static struct resource *
mptable_hostb_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)99 mptable_hostb_alloc_resource(device_t dev, device_t child, int type, int *rid,
100     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
101 {
102 	struct mptable_hostb_softc *sc;
103 
104 	if (type == PCI_RES_BUS)
105 		return (pci_domain_alloc_bus(0, child, rid, start, end, count,
106 		    flags));
107 	sc = device_get_softc(dev);
108 	if (type == SYS_RES_IOPORT && start + count - 1 == end) {
109 		if (mptable_is_isa_range(start, end)) {
110 			switch (sc->sc_decodes_isa_io) {
111 			case -1:
112 				return (NULL);
113 			case 1:
114 				return (bus_generic_alloc_resource(dev, child,
115 				    type, rid, start, end, count, flags));
116 			default:
117 				break;
118 			}
119 		}
120 		if (mptable_is_vga_range(start, end)) {
121 			switch (sc->sc_decodes_vga_io) {
122 			case -1:
123 				return (NULL);
124 			case 1:
125 				return (bus_generic_alloc_resource(dev, child,
126 				    type, rid, start, end, count, flags));
127 			default:
128 				break;
129 			}
130 		}
131 	}
132 	start = hostb_alloc_start(type, start, end, count);
133 	return (pcib_host_res_alloc(&sc->sc_host_res, child, type, rid, start,
134 	    end, count, flags));
135 }
136 
137 static int
mptable_hostb_adjust_resource(device_t dev,device_t child,struct resource * r,rman_res_t start,rman_res_t end)138 mptable_hostb_adjust_resource(device_t dev, device_t child,
139     struct resource *r, rman_res_t start, rman_res_t end)
140 {
141 	struct mptable_hostb_softc *sc;
142 
143 	if (rman_get_type(r) == PCI_RES_BUS)
144 		return (pci_domain_adjust_bus(0, child, r, start, end));
145 	sc = device_get_softc(dev);
146 	return (pcib_host_res_adjust(&sc->sc_host_res, child, r, start, end));
147 }
148 
149 static device_method_t mptable_hostb_methods[] = {
150 	/* Device interface */
151 	DEVMETHOD(device_probe,		mptable_hostb_probe),
152 	DEVMETHOD(device_attach,	mptable_hostb_attach),
153 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
154 	DEVMETHOD(device_suspend,	bus_generic_suspend),
155 	DEVMETHOD(device_resume,	bus_generic_resume),
156 
157 	/* Bus interface */
158 	DEVMETHOD(bus_read_ivar,	legacy_pcib_read_ivar),
159 	DEVMETHOD(bus_write_ivar,	legacy_pcib_write_ivar),
160 	DEVMETHOD(bus_alloc_resource,	mptable_hostb_alloc_resource),
161 	DEVMETHOD(bus_adjust_resource,	mptable_hostb_adjust_resource),
162 	DEVMETHOD(bus_release_resource,	legacy_pcib_release_resource),
163 	DEVMETHOD(bus_activate_resource, legacy_pcib_activate_resource),
164 	DEVMETHOD(bus_deactivate_resource, legacy_pcib_deactivate_resource),
165 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
166 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
167 
168 	/* pcib interface */
169 	DEVMETHOD(pcib_maxslots,	legacy_pcib_maxslots),
170 	DEVMETHOD(pcib_read_config,	legacy_pcib_read_config),
171 	DEVMETHOD(pcib_write_config,	legacy_pcib_write_config),
172 	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
173 	DEVMETHOD(pcib_alloc_msi,	legacy_pcib_alloc_msi),
174 	DEVMETHOD(pcib_release_msi,	pcib_release_msi),
175 	DEVMETHOD(pcib_alloc_msix,	legacy_pcib_alloc_msix),
176 	DEVMETHOD(pcib_release_msix,	pcib_release_msix),
177 	DEVMETHOD(pcib_map_msi,		legacy_pcib_map_msi),
178 
179 	DEVMETHOD_END
180 };
181 
182 DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods,
183     sizeof(struct mptable_hostb_softc));
184 DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, 0, 0);
185 
186 /* PCI to PCI bridge driver. */
187 
188 static int
mptable_pcib_probe(device_t dev)189 mptable_pcib_probe(device_t dev)
190 {
191 	int bus;
192 
193 	if ((pci_get_class(dev) != PCIC_BRIDGE) ||
194 	    (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
195 		return (ENXIO);
196 	bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
197 	if (bus == 0)
198 		return (ENXIO);
199 	if (mptable_pci_probe_table(bus) != 0)
200 		return (ENXIO);
201 	device_set_desc(dev, "MPTable PCI-PCI bridge");
202 	return (-1000);
203 }
204 
205 static device_method_t mptable_pcib_pci_methods[] = {
206 	/* Device interface */
207 	DEVMETHOD(device_probe,		mptable_pcib_probe),
208 
209 	/* pcib interface */
210 	DEVMETHOD(pcib_route_interrupt,	mptable_pci_route_interrupt),
211 	{0, 0}
212 };
213 
214 DEFINE_CLASS_1(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
215     sizeof(struct pcib_softc), pcib_driver);
216 DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, 0, 0);
217