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