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