Merge branch 'vendor/FILE'
[dragonfly.git] / sys / bus / pci / i386 / mptable_pci.c
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/smp.h>
48
49 #include "legacyvar.h"
50 #include "pci_cfgreg.h"
51
52 #include "pcib_if.h"
53
54 static int
55 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
56 {
57         int line, bus, slot, irq;
58
59         bus = pci_get_bus(dev);
60         slot = pci_get_slot(dev);
61         irq = pci_get_irq(dev);
62
63         line = mptable_pci_int_route(bus, slot, pin, irq);
64         if (line >= 0)
65                 goto done;
66
67         kprintf("MPTABLE: Unable to route for bus %d slot %d INT%c\n",
68                 bus, slot, 'A' + pin - 1);
69         return PCI_INVALID_IRQ;
70
71 done:
72         BUS_CONFIG_INTR(dev, dev, line, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
73         return line;
74 }
75
76 /* Host to PCI bridge driver. */
77
78 static int
79 mptable_hostb_probe(device_t dev)
80 {
81         if (!apic_io_enable)
82                 return (ENXIO);
83
84         if (pci_cfgregopen() == 0)
85                 return (ENXIO);
86 #ifdef notyet
87         if (mptable_pci_probe_table(pcib_get_bus(dev)) != 0)
88                 return (ENXIO);
89 #endif
90
91         if (bootverbose)
92                 mptable_pci_int_dump();
93
94         device_set_desc(dev, "MPTABLE Host-PCI bridge");
95         return (0);
96 }
97
98 static int
99 mptable_hostb_attach(device_t dev)
100 {
101
102         device_add_child(dev, "pci", pcib_get_bus(dev));
103         return (bus_generic_attach(dev));
104 }
105
106 /* Pass MSI requests up to the nexus. */
107 static int
108 mptable_hostb_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
109     int *irqs)
110 {
111         device_t bus;
112
113         bus = device_get_parent(pcib);
114         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
115             irqs));
116 }
117
118 static int
119 mptable_hostb_alloc_msix(device_t pcib, device_t dev, int *irq)
120 {
121         device_t bus;
122
123         bus = device_get_parent(pcib);
124         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
125 }
126
127 static int
128 mptable_hostb_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
129     uint32_t *data)
130 {
131         device_t bus;
132
133         bus = device_get_parent(pcib);
134         return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
135 }
136
137 static device_method_t mptable_hostb_methods[] = {
138         /* Device interface */
139         DEVMETHOD(device_probe,         mptable_hostb_probe),
140         DEVMETHOD(device_attach,        mptable_hostb_attach),
141         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
142         DEVMETHOD(device_suspend,       bus_generic_suspend),
143         DEVMETHOD(device_resume,        bus_generic_resume),
144
145         /* Bus interface */
146         DEVMETHOD(bus_print_child,      bus_generic_print_child),
147         DEVMETHOD(bus_read_ivar,        legacy_pcib_read_ivar),
148         DEVMETHOD(bus_write_ivar,       legacy_pcib_write_ivar),
149         DEVMETHOD(bus_alloc_resource,   legacy_pcib_alloc_resource),
150         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
151         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
152         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
153         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
154         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
155
156         /* pcib interface */
157         DEVMETHOD(pcib_maxslots,        legacy_pcib_maxslots),
158         DEVMETHOD(pcib_read_config,     legacy_pcib_read_config),
159         DEVMETHOD(pcib_write_config,    legacy_pcib_write_config),
160         DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
161         DEVMETHOD(pcib_alloc_msi,       mptable_hostb_alloc_msi),
162         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
163         DEVMETHOD(pcib_alloc_msix,      mptable_hostb_alloc_msix),
164         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
165         DEVMETHOD(pcib_map_msi,         mptable_hostb_map_msi),
166
167         { 0, 0 }
168 };
169
170 static devclass_t hostb_devclass;
171
172 DEFINE_CLASS_0(pcib, mptable_hostb_driver, mptable_hostb_methods, 1);
173 DRIVER_MODULE(mptable_pcib, legacy, mptable_hostb_driver, hostb_devclass, 0, 0);
174
175 /* PCI to PCI bridge driver. */
176
177 static int
178 mptable_pcib_probe(device_t dev)
179 {
180         int bus;
181
182         if (!apic_io_enable)
183                 return (ENXIO);
184
185         if ((pci_get_class(dev) != PCIC_BRIDGE) ||
186             (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
187                 return (ENXIO);
188         bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
189         if (bus == 0)
190                 return (ENXIO);
191 #ifdef notyet
192         if (mptable_pci_probe_table(bus) != 0)
193                 return (ENXIO);
194 #endif
195         device_set_desc(dev, "MPTABLE PCI-PCI bridge");
196         return (-500);
197 }
198
199 static device_method_t mptable_pcib_pci_methods[] = {
200         /* Device interface */
201         DEVMETHOD(device_probe,         mptable_pcib_probe),
202         DEVMETHOD(device_attach,        pcib_attach),
203         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
204         DEVMETHOD(device_suspend,       bus_generic_suspend),
205         DEVMETHOD(device_resume,        bus_generic_resume),
206
207         /* Bus interface */
208         DEVMETHOD(bus_print_child,      bus_generic_print_child),
209         DEVMETHOD(bus_read_ivar,        pcib_read_ivar),
210         DEVMETHOD(bus_write_ivar,       pcib_write_ivar),
211         DEVMETHOD(bus_alloc_resource,   pcib_alloc_resource),
212         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
213         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
214         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
215         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
216         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
217
218         /* pcib interface */
219         DEVMETHOD(pcib_maxslots,        pcib_maxslots),
220         DEVMETHOD(pcib_read_config,     pcib_read_config),
221         DEVMETHOD(pcib_write_config,    pcib_write_config),
222         DEVMETHOD(pcib_route_interrupt, mptable_pci_route_interrupt),
223         DEVMETHOD(pcib_alloc_msi,       pcib_alloc_msi),
224         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
225         DEVMETHOD(pcib_alloc_msix,      pcib_alloc_msix),
226         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
227         DEVMETHOD(pcib_map_msi,         pcib_map_msi),
228
229         {0, 0}
230 };
231
232 static devclass_t pcib_devclass;
233
234 DEFINE_CLASS_0(pcib, mptable_pcib_driver, mptable_pcib_pci_methods,
235     sizeof(struct pcib_softc));
236 DRIVER_MODULE(mptable_pcib, pci, mptable_pcib_driver, pcib_devclass, 0, 0);