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