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 * $FreeBSD @169221
30 */
31
32 /*
33 * Host to PCI and PCI to PCI bridge drivers that use the MP Table to route
34 * interrupts from PCI devices to I/O APICs.
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42
43 #include <bus/pci/pcireg.h>
44 #include <bus/pci/pcivar.h>
45 #include <bus/pci/pcib_private.h>
46
47 #include <machine/mptable.h>
48 #include <machine_base/apic/ioapic.h>
49
50 #include "legacyvar.h"
51 #include "pci_cfgreg.h"
52
53 #include "pcib_if.h"
54
55 static int
mptable_pci_route_interrupt(device_t pcib,device_t dev,int pin)56 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
57 {
58 int line, bus, slot, irq;
59
60 bus = pci_get_bus(dev);
61 slot = pci_get_slot(dev);
62 irq = pci_get_irq(dev);
63
64 line = mptable_pci_int_route(bus, slot, pin, -1);
65 if (line >= 0)
66 goto done;
67
68 /*
69 * MPTABLE does not provide interrupt routing
70 * for bus:slot:pin, ask parent about it then,
71 * i.e. PCI-PCI bridge interrupt pin swizzle.
72 */
73 line = pcib_route_interrupt(pcib, dev, pin);
74 if (line != PCI_INVALID_IRQ)
75 return line;
76
77 /*
78 * No luck, try using the intline ...
79 * XXX this probably is broken
80 */
81 line = mptable_pci_int_route(bus, slot, pin, irq);
82 if (line >= 0)
83 goto done;
84
85 kprintf("MPTABLE: Unable to route for bus %d slot %d INT%c\n",
86 bus, slot, 'A' + pin - 1);
87 return PCI_INVALID_IRQ;
88
89 done:
90 BUS_CONFIG_INTR(dev, dev, line, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
91 return line;
92 }
93
94 /* Host to PCI bridge driver. */
95
96 static int
mptable_hostb_probe(device_t dev)97 mptable_hostb_probe(device_t dev)
98 {
99 if (!ioapic_enable)
100 return (ENXIO);
101
102 if (pci_cfgregopen() == 0)
103 return (ENXIO);
104 #ifdef notyet
105 if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
106 return (ENXIO);
107 #endif
108
109 device_set_desc(dev, "MPTABLE Host-PCI bridge");
110 return (0);
111 }
112
113 static int
mptable_hostb_attach(device_t dev)114 mptable_hostb_attach(device_t dev)
115 {
116
117 device_add_child(dev, "pci", pcib_get_bus(dev));
118 return (bus_generic_attach(dev));
119 }
120
121 /* Pass MSI requests up to the nexus. */
122 static int
mptable_hostb_alloc_msi(device_t pcib,device_t dev,int count,int maxcount,int * irqs,int cpuid)123 mptable_hostb_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
124 int *irqs, int cpuid)
125 {
126 device_t bus;
127
128 bus = device_get_parent(pcib);
129 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
130 irqs, cpuid));
131 }
132
133 static int
mptable_hostb_alloc_msix(device_t pcib,device_t dev,int * irq,int cpuid)134 mptable_hostb_alloc_msix(device_t pcib, device_t dev, int *irq, int cpuid)
135 {
136 device_t bus;
137
138 bus = device_get_parent(pcib);
139 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq, cpuid));
140 }
141
142 static int
mptable_hostb_map_msi(device_t pcib,device_t dev,int irq,uint64_t * addr,uint32_t * data,int cpuid)143 mptable_hostb_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
144 uint32_t *data, int cpuid)
145 {
146 device_t bus;
147
148 bus = device_get_parent(pcib);
149 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data,
150 cpuid));
151 }
152
153 static 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_print_child, bus_generic_print_child),
163 DEVMETHOD(bus_read_ivar, legacy_pcib_read_ivar),
164 DEVMETHOD(bus_write_ivar, legacy_pcib_write_ivar),
165 DEVMETHOD(bus_alloc_resource, legacy_pcib_alloc_resource),
166 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
167 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
168 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
169 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
170 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
171
172 /* pcib interface */
173 DEVMETHOD(pcib_maxslots, legacy_pcib_maxslots),
174 DEVMETHOD(pcib_read_config, legacy_pcib_read_config),
175 DEVMETHOD(pcib_write_config, legacy_pcib_write_config),
176 DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
177 DEVMETHOD(pcib_alloc_msi, mptable_hostb_alloc_msi),
178 DEVMETHOD(pcib_release_msi, pcib_release_msi),
179 DEVMETHOD(pcib_alloc_msix, mptable_hostb_alloc_msix),
180 DEVMETHOD(pcib_release_msix, pcib_release_msix),
181 DEVMETHOD(pcib_map_msi, mptable_hostb_map_msi),
182
183 DEVMETHOD_END
184 };
185
186 static devclass_t hostb_devclass;
187
188 DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods, 1);
189 DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, NULL, NULL);
190
191 /* PCI to PCI bridge driver. */
192
193 static int
mptable_pcib_probe(device_t dev)194 mptable_pcib_probe(device_t dev)
195 {
196 int bus;
197
198 if (!ioapic_enable)
199 return (ENXIO);
200
201 if ((pci_get_class(dev) != PCIC_BRIDGE) ||
202 (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
203 return (ENXIO);
204 bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
205 if (bus == 0)
206 return (ENXIO);
207 #ifdef notyet
208 if (mptable_pci_probe_table(bus) != 0)
209 return (ENXIO);
210 #endif
211 device_set_desc(dev, "MPTABLE PCI-PCI bridge");
212 return (-500);
213 }
214
215 static device_method_t mptable_pcib_pci_methods[] = {
216 /* Device interface */
217 DEVMETHOD(device_probe, mptable_pcib_probe),
218 DEVMETHOD(device_attach, pcib_attach),
219 DEVMETHOD(device_shutdown, bus_generic_shutdown),
220 DEVMETHOD(device_suspend, bus_generic_suspend),
221 DEVMETHOD(device_resume, bus_generic_resume),
222
223 /* Bus interface */
224 DEVMETHOD(bus_print_child, bus_generic_print_child),
225 DEVMETHOD(bus_read_ivar, pcib_read_ivar),
226 DEVMETHOD(bus_write_ivar, pcib_write_ivar),
227 DEVMETHOD(bus_alloc_resource, pcib_alloc_resource),
228 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
229 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
230 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
231 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
232 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
233
234 /* pcib interface */
235 DEVMETHOD(pcib_maxslots, pcib_maxslots),
236 DEVMETHOD(pcib_read_config, pcib_read_config),
237 DEVMETHOD(pcib_write_config, pcib_write_config),
238 DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
239 DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi),
240 DEVMETHOD(pcib_release_msi, pcib_release_msi),
241 DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix),
242 DEVMETHOD(pcib_release_msix, pcib_release_msix),
243 DEVMETHOD(pcib_map_msi, pcib_map_msi),
244
245 DEVMETHOD_END
246 };
247
248 static devclass_t pcib_devclass;
249
250 DEFINE_CLASS_0(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
251 sizeof(struct pcib_softc));
252 DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, NULL, NULL);
253