1b2b3ffcdSSimon Schubert /*-
2b2b3ffcdSSimon Schubert * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3b2b3ffcdSSimon Schubert * All rights reserved.
4b2b3ffcdSSimon Schubert *
5b2b3ffcdSSimon Schubert * Redistribution and use in source and binary forms, with or without
6b2b3ffcdSSimon Schubert * modification, are permitted provided that the following conditions
7b2b3ffcdSSimon Schubert * are met:
8b2b3ffcdSSimon Schubert * 1. Redistributions of source code must retain the above copyright
9b2b3ffcdSSimon Schubert * notice, this list of conditions and the following disclaimer.
10b2b3ffcdSSimon Schubert * 2. Redistributions in binary form must reproduce the above copyright
11b2b3ffcdSSimon Schubert * notice, this list of conditions and the following disclaimer in the
12b2b3ffcdSSimon Schubert * documentation and/or other materials provided with the distribution.
13b2b3ffcdSSimon Schubert * 3. Neither the name of the author nor the names of any co-contributors
14b2b3ffcdSSimon Schubert * may be used to endorse or promote products derived from this software
15b2b3ffcdSSimon Schubert * without specific prior written permission.
16b2b3ffcdSSimon Schubert *
17b2b3ffcdSSimon Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18b2b3ffcdSSimon Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b2b3ffcdSSimon Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b2b3ffcdSSimon Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21b2b3ffcdSSimon Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b2b3ffcdSSimon Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b2b3ffcdSSimon Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b2b3ffcdSSimon Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b2b3ffcdSSimon Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b2b3ffcdSSimon Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b2b3ffcdSSimon Schubert * SUCH DAMAGE.
28b2b3ffcdSSimon Schubert *
29b2b3ffcdSSimon Schubert * $FreeBSD @169221
30b2b3ffcdSSimon Schubert */
31b2b3ffcdSSimon Schubert
32b2b3ffcdSSimon Schubert /*
33b2b3ffcdSSimon Schubert * Host to PCI and PCI to PCI bridge drivers that use the MP Table to route
34b2b3ffcdSSimon Schubert * interrupts from PCI devices to I/O APICs.
35b2b3ffcdSSimon Schubert */
36b2b3ffcdSSimon Schubert
37b2b3ffcdSSimon Schubert #include <sys/param.h>
38b2b3ffcdSSimon Schubert #include <sys/systm.h>
39b2b3ffcdSSimon Schubert #include <sys/bus.h>
40b2b3ffcdSSimon Schubert #include <sys/kernel.h>
41b2b3ffcdSSimon Schubert #include <sys/module.h>
42b2b3ffcdSSimon Schubert
43b2b3ffcdSSimon Schubert #include <bus/pci/pcireg.h>
44b2b3ffcdSSimon Schubert #include <bus/pci/pcivar.h>
45b2b3ffcdSSimon Schubert #include <bus/pci/pcib_private.h>
46b2b3ffcdSSimon Schubert
4755e07399SSepherosa Ziehau #include <machine/mptable.h>
48ed4d621dSSepherosa Ziehau #include <machine_base/apic/ioapic.h>
49b2b3ffcdSSimon Schubert
50b2b3ffcdSSimon Schubert #include "legacyvar.h"
51b2b3ffcdSSimon Schubert #include "pci_cfgreg.h"
52b2b3ffcdSSimon Schubert
53b2b3ffcdSSimon Schubert #include "pcib_if.h"
54b2b3ffcdSSimon Schubert
55b2b3ffcdSSimon Schubert static int
mptable_pci_route_interrupt(device_t pcib,device_t dev,int pin)56b2b3ffcdSSimon Schubert mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
57b2b3ffcdSSimon Schubert {
587a603b36SSepherosa Ziehau int line, bus, slot, irq;
59b2b3ffcdSSimon Schubert
60b2b3ffcdSSimon Schubert bus = pci_get_bus(dev);
61b2b3ffcdSSimon Schubert slot = pci_get_slot(dev);
627a603b36SSepherosa Ziehau irq = pci_get_irq(dev);
63b2b3ffcdSSimon Schubert
64779d3e7aSSepherosa Ziehau line = mptable_pci_int_route(bus, slot, pin, -1);
65779d3e7aSSepherosa Ziehau if (line >= 0)
66779d3e7aSSepherosa Ziehau goto done;
67779d3e7aSSepherosa Ziehau
68779d3e7aSSepherosa Ziehau /*
69779d3e7aSSepherosa Ziehau * MPTABLE does not provide interrupt routing
70779d3e7aSSepherosa Ziehau * for bus:slot:pin, ask parent about it then,
71779d3e7aSSepherosa Ziehau * i.e. PCI-PCI bridge interrupt pin swizzle.
72779d3e7aSSepherosa Ziehau */
73779d3e7aSSepherosa Ziehau line = pcib_route_interrupt(pcib, dev, pin);
74779d3e7aSSepherosa Ziehau if (line != PCI_INVALID_IRQ)
75779d3e7aSSepherosa Ziehau return line;
76779d3e7aSSepherosa Ziehau
77779d3e7aSSepherosa Ziehau /*
78779d3e7aSSepherosa Ziehau * No luck, try using the intline ...
79779d3e7aSSepherosa Ziehau * XXX this probably is broken
80779d3e7aSSepherosa Ziehau */
81e90e7ac4SSepherosa Ziehau line = mptable_pci_int_route(bus, slot, pin, irq);
82e90e7ac4SSepherosa Ziehau if (line >= 0)
83e90e7ac4SSepherosa Ziehau goto done;
84e90e7ac4SSepherosa Ziehau
85e90e7ac4SSepherosa Ziehau kprintf("MPTABLE: Unable to route for bus %d slot %d INT%c\n",
86b2b3ffcdSSimon Schubert bus, slot, 'A' + pin - 1);
87b2b3ffcdSSimon Schubert return PCI_INVALID_IRQ;
886c90c9f7SSepherosa Ziehau
896c90c9f7SSepherosa Ziehau done:
906c90c9f7SSepherosa Ziehau BUS_CONFIG_INTR(dev, dev, line, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
916c90c9f7SSepherosa Ziehau return line;
92b2b3ffcdSSimon Schubert }
93b2b3ffcdSSimon Schubert
94b2b3ffcdSSimon Schubert /* Host to PCI bridge driver. */
95b2b3ffcdSSimon Schubert
96b2b3ffcdSSimon Schubert static int
mptable_hostb_probe(device_t dev)97b2b3ffcdSSimon Schubert mptable_hostb_probe(device_t dev)
98b2b3ffcdSSimon Schubert {
99f45bfca0SSepherosa Ziehau if (!ioapic_enable)
100ca540e3cSSascha Wildner return (ENXIO);
101b2b3ffcdSSimon Schubert
102b2b3ffcdSSimon Schubert if (pci_cfgregopen() == 0)
103b2b3ffcdSSimon Schubert return (ENXIO);
104b2b3ffcdSSimon Schubert #ifdef notyet
105b2b3ffcdSSimon Schubert if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
106b2b3ffcdSSimon Schubert return (ENXIO);
107b2b3ffcdSSimon Schubert #endif
108e90e7ac4SSepherosa Ziehau
109e90e7ac4SSepherosa Ziehau device_set_desc(dev, "MPTABLE Host-PCI bridge");
110b2b3ffcdSSimon Schubert return (0);
111b2b3ffcdSSimon Schubert }
112b2b3ffcdSSimon Schubert
113b2b3ffcdSSimon Schubert static int
mptable_hostb_attach(device_t dev)114b2b3ffcdSSimon Schubert mptable_hostb_attach(device_t dev)
115b2b3ffcdSSimon Schubert {
116b2b3ffcdSSimon Schubert
117b2b3ffcdSSimon Schubert device_add_child(dev, "pci", pcib_get_bus(dev));
118b2b3ffcdSSimon Schubert return (bus_generic_attach(dev));
119b2b3ffcdSSimon Schubert }
120b2b3ffcdSSimon Schubert
121b2b3ffcdSSimon Schubert /* Pass MSI requests up to the nexus. */
122b2b3ffcdSSimon Schubert static int
mptable_hostb_alloc_msi(device_t pcib,device_t dev,int count,int maxcount,int * irqs,int cpuid)123b2b3ffcdSSimon Schubert mptable_hostb_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
124803a9933SSepherosa Ziehau int *irqs, int cpuid)
125b2b3ffcdSSimon Schubert {
126b2b3ffcdSSimon Schubert device_t bus;
127b2b3ffcdSSimon Schubert
128b2b3ffcdSSimon Schubert bus = device_get_parent(pcib);
129b2b3ffcdSSimon Schubert return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
130803a9933SSepherosa Ziehau irqs, cpuid));
131b2b3ffcdSSimon Schubert }
132b2b3ffcdSSimon Schubert
133b2b3ffcdSSimon Schubert static int
mptable_hostb_alloc_msix(device_t pcib,device_t dev,int * irq,int cpuid)1342a1f96b9SSepherosa Ziehau mptable_hostb_alloc_msix(device_t pcib, device_t dev, int *irq, int cpuid)
135b2b3ffcdSSimon Schubert {
136b2b3ffcdSSimon Schubert device_t bus;
137b2b3ffcdSSimon Schubert
138b2b3ffcdSSimon Schubert bus = device_get_parent(pcib);
1392a1f96b9SSepherosa Ziehau return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq, cpuid));
140b2b3ffcdSSimon Schubert }
141b2b3ffcdSSimon Schubert
142b2b3ffcdSSimon Schubert static int
mptable_hostb_map_msi(device_t pcib,device_t dev,int irq,uint64_t * addr,uint32_t * data,int cpuid)143b2b3ffcdSSimon Schubert mptable_hostb_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1440af900e1SSepherosa Ziehau uint32_t *data, int cpuid)
145b2b3ffcdSSimon Schubert {
146b2b3ffcdSSimon Schubert device_t bus;
147b2b3ffcdSSimon Schubert
148b2b3ffcdSSimon Schubert bus = device_get_parent(pcib);
1490af900e1SSepherosa Ziehau return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data,
1500af900e1SSepherosa Ziehau cpuid));
151b2b3ffcdSSimon Schubert }
152b2b3ffcdSSimon Schubert
153b2b3ffcdSSimon Schubert static device_method_t mptable_hostb_methods[] = {
154b2b3ffcdSSimon Schubert /* Device interface */
155b2b3ffcdSSimon Schubert DEVMETHOD(device_probe, mptable_hostb_probe),
156b2b3ffcdSSimon Schubert DEVMETHOD(device_attach, mptable_hostb_attach),
157b2b3ffcdSSimon Schubert DEVMETHOD(device_shutdown, bus_generic_shutdown),
158b2b3ffcdSSimon Schubert DEVMETHOD(device_suspend, bus_generic_suspend),
159b2b3ffcdSSimon Schubert DEVMETHOD(device_resume, bus_generic_resume),
160b2b3ffcdSSimon Schubert
161b2b3ffcdSSimon Schubert /* Bus interface */
162b2b3ffcdSSimon Schubert DEVMETHOD(bus_print_child, bus_generic_print_child),
163b2b3ffcdSSimon Schubert DEVMETHOD(bus_read_ivar, legacy_pcib_read_ivar),
164b2b3ffcdSSimon Schubert DEVMETHOD(bus_write_ivar, legacy_pcib_write_ivar),
165b2b3ffcdSSimon Schubert DEVMETHOD(bus_alloc_resource, legacy_pcib_alloc_resource),
166b2b3ffcdSSimon Schubert DEVMETHOD(bus_release_resource, bus_generic_release_resource),
167b2b3ffcdSSimon Schubert DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
168b2b3ffcdSSimon Schubert DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
169b2b3ffcdSSimon Schubert DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
170b2b3ffcdSSimon Schubert DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
171b2b3ffcdSSimon Schubert
172b2b3ffcdSSimon Schubert /* pcib interface */
173b2b3ffcdSSimon Schubert DEVMETHOD(pcib_maxslots, legacy_pcib_maxslots),
174b2b3ffcdSSimon Schubert DEVMETHOD(pcib_read_config, legacy_pcib_read_config),
175b2b3ffcdSSimon Schubert DEVMETHOD(pcib_write_config, legacy_pcib_write_config),
176b2b3ffcdSSimon Schubert DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
177b2b3ffcdSSimon Schubert DEVMETHOD(pcib_alloc_msi, mptable_hostb_alloc_msi),
178b2b3ffcdSSimon Schubert DEVMETHOD(pcib_release_msi, pcib_release_msi),
179b2b3ffcdSSimon Schubert DEVMETHOD(pcib_alloc_msix, mptable_hostb_alloc_msix),
180b2b3ffcdSSimon Schubert DEVMETHOD(pcib_release_msix, pcib_release_msix),
181b2b3ffcdSSimon Schubert DEVMETHOD(pcib_map_msi, mptable_hostb_map_msi),
182b2b3ffcdSSimon Schubert
183*d3c9c58eSSascha Wildner DEVMETHOD_END
184b2b3ffcdSSimon Schubert };
185b2b3ffcdSSimon Schubert
186b2b3ffcdSSimon Schubert static devclass_t hostb_devclass;
187b2b3ffcdSSimon Schubert
188b2b3ffcdSSimon Schubert DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods, 1);
189aa2b9d05SSascha Wildner DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, NULL, NULL);
190b2b3ffcdSSimon Schubert
191b2b3ffcdSSimon Schubert /* PCI to PCI bridge driver. */
192b2b3ffcdSSimon Schubert
193b2b3ffcdSSimon Schubert static int
mptable_pcib_probe(device_t dev)194b2b3ffcdSSimon Schubert mptable_pcib_probe(device_t dev)
195b2b3ffcdSSimon Schubert {
196b2b3ffcdSSimon Schubert int bus;
197b2b3ffcdSSimon Schubert
198f45bfca0SSepherosa Ziehau if (!ioapic_enable)
199ca540e3cSSascha Wildner return (ENXIO);
200ca540e3cSSascha Wildner
201b2b3ffcdSSimon Schubert if ((pci_get_class(dev) != PCIC_BRIDGE) ||
202b2b3ffcdSSimon Schubert (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
203b2b3ffcdSSimon Schubert return (ENXIO);
204b2b3ffcdSSimon Schubert bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
205b2b3ffcdSSimon Schubert if (bus == 0)
206b2b3ffcdSSimon Schubert return (ENXIO);
207b2b3ffcdSSimon Schubert #ifdef notyet
208b2b3ffcdSSimon Schubert if (mptable_pci_probe_table(bus) != 0)
209b2b3ffcdSSimon Schubert return (ENXIO);
210b2b3ffcdSSimon Schubert #endif
211e90e7ac4SSepherosa Ziehau device_set_desc(dev, "MPTABLE PCI-PCI bridge");
212b2b3ffcdSSimon Schubert return (-500);
213b2b3ffcdSSimon Schubert }
214b2b3ffcdSSimon Schubert
215b2b3ffcdSSimon Schubert static device_method_t mptable_pcib_pci_methods[] = {
216b2b3ffcdSSimon Schubert /* Device interface */
217b2b3ffcdSSimon Schubert DEVMETHOD(device_probe, mptable_pcib_probe),
218b2b3ffcdSSimon Schubert DEVMETHOD(device_attach, pcib_attach),
219b2b3ffcdSSimon Schubert DEVMETHOD(device_shutdown, bus_generic_shutdown),
220b2b3ffcdSSimon Schubert DEVMETHOD(device_suspend, bus_generic_suspend),
221b2b3ffcdSSimon Schubert DEVMETHOD(device_resume, bus_generic_resume),
222b2b3ffcdSSimon Schubert
223b2b3ffcdSSimon Schubert /* Bus interface */
224b2b3ffcdSSimon Schubert DEVMETHOD(bus_print_child, bus_generic_print_child),
225b2b3ffcdSSimon Schubert DEVMETHOD(bus_read_ivar, pcib_read_ivar),
226b2b3ffcdSSimon Schubert DEVMETHOD(bus_write_ivar, pcib_write_ivar),
227b2b3ffcdSSimon Schubert DEVMETHOD(bus_alloc_resource, pcib_alloc_resource),
228b2b3ffcdSSimon Schubert DEVMETHOD(bus_release_resource, bus_generic_release_resource),
229b2b3ffcdSSimon Schubert DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
230b2b3ffcdSSimon Schubert DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
231b2b3ffcdSSimon Schubert DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
232b2b3ffcdSSimon Schubert DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
233b2b3ffcdSSimon Schubert
234b2b3ffcdSSimon Schubert /* pcib interface */
235b2b3ffcdSSimon Schubert DEVMETHOD(pcib_maxslots, pcib_maxslots),
236b2b3ffcdSSimon Schubert DEVMETHOD(pcib_read_config, pcib_read_config),
237b2b3ffcdSSimon Schubert DEVMETHOD(pcib_write_config, pcib_write_config),
238b2b3ffcdSSimon Schubert DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
239b2b3ffcdSSimon Schubert DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi),
240b2b3ffcdSSimon Schubert DEVMETHOD(pcib_release_msi, pcib_release_msi),
241b2b3ffcdSSimon Schubert DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix),
242b2b3ffcdSSimon Schubert DEVMETHOD(pcib_release_msix, pcib_release_msix),
243b2b3ffcdSSimon Schubert DEVMETHOD(pcib_map_msi, pcib_map_msi),
244b2b3ffcdSSimon Schubert
245*d3c9c58eSSascha Wildner DEVMETHOD_END
246b2b3ffcdSSimon Schubert };
247b2b3ffcdSSimon Schubert
248b2b3ffcdSSimon Schubert static devclass_t pcib_devclass;
249b2b3ffcdSSimon Schubert
250b2b3ffcdSSimon Schubert DEFINE_CLASS_0(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
251b2b3ffcdSSimon Schubert sizeof(struct pcib_softc));
252aa2b9d05SSascha Wildner DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, NULL, NULL);
253