c66eb09736425ebe582db03d74d254cbe80e1b2c
[dragonfly.git] / sys / bus / pci / pci_compat.c
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/pci/pci_compat.c,v 1.35.2.1 2001/10/14 21:14:14 luigi Exp $
27  * $DragonFly: src/sys/bus/pci/pci_compat.c,v 1.14 2007/05/13 18:33:56 swildner Exp $
28  *
29  */
30
31 #include "opt_bus.h"
32
33 /* for compatibility to FreeBSD-2.2 and 3.x versions of PCI code */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42
43 #include <sys/bus.h>
44 #include <sys/rman.h>
45 #include <machine/smp.h>
46 #include <sys/interrupt.h>
47
48 #include <sys/pciio.h>
49 #include "pcireg.h"
50 #include "pcivar.h"
51 #include <bus/pci/pci_cfgreg.h>
52
53 struct pci_compat_driver {
54         driver_t driver;
55         struct pci_device *dvp;
56 };
57
58 /* ------------------------------------------------------------------------- */
59
60 u_long
61 pci_conf_read(pcici_t cfg, u_long reg)
62 {
63         return (pci_read_config(cfg->dev, reg, 4));
64 }
65
66 void
67 pci_conf_write(pcici_t cfg, u_long reg, u_long data)
68 {
69         pci_write_config(cfg->dev, reg, data, 4);
70 }
71
72 void
73 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
74 {
75         pci_cfgregwrite(cfg->bus, cfg->slot, cfg->func, reg, data, bytes);
76 }
77
78 int
79 pci_map_port(pcici_t cfg, u_long reg, pci_port_t* pa)
80 {
81         int rid;
82         struct resource *res;
83
84         rid = reg;
85         res = bus_alloc_resource(cfg->dev, SYS_RES_IOPORT, &rid,
86                                  0, ~0, 1, RF_ACTIVE);
87         if (res) {
88                 *pa = rman_get_start(res);
89                 return (1);
90         }
91         return (0);
92 }
93
94 int
95 pci_map_mem(pcici_t cfg, u_long reg, vm_offset_t* va, vm_offset_t* pa)
96 {
97         int rid;
98         struct resource *res;
99
100         rid = reg;
101         res = bus_alloc_resource(cfg->dev, SYS_RES_MEMORY, &rid,
102                                  0, ~0, 1, RF_ACTIVE);
103         if (res) {
104                 *pa = rman_get_start(res);
105                 *va = (vm_offset_t) rman_get_virtual(res);
106                 return (1);
107         }
108         return (0);
109 }
110
111 int
112 pci_map_int(pcici_t cfg, pci_inthand_t *handler, void *arg)
113 {
114         return (pci_map_int_right(cfg, handler, arg, 0));
115 }
116
117 int
118 pci_map_int_right(pcici_t cfg, pci_inthand_t *handler, void *arg, u_int intflags)
119 {
120         int error;
121 #ifdef APIC_IO
122         int nextpin, muxcnt;
123 #endif
124         if (cfg->intpin != 0) {
125                 int irq = cfg->intline;
126                 int rid = 0;
127                 struct resource *res;
128                 int flags = 0;
129                 int resflags = RF_SHAREABLE|RF_ACTIVE;
130                 void *ih;
131
132                 if (intflags & INTR_FAST)
133                         flags |= INTR_FAST;
134                 if (intflags & INTR_EXCL)
135                         resflags &= ~RF_SHAREABLE;
136
137                 res = bus_alloc_resource(cfg->dev, SYS_RES_IRQ, &rid,
138                                          irq, irq, 1, resflags);
139                 if (!res) {
140                         kprintf("pci_map_int: can't allocate interrupt\n");
141                         return 0;
142                 }
143
144                 /*
145                  * This is ugly. Translate the mask into an interrupt type.
146                  */
147
148                 error = BUS_SETUP_INTR(device_get_parent(cfg->dev), cfg->dev,
149                                        res, flags, handler, arg, &ih, NULL);
150                 if (error != 0)
151                         return 0;
152
153 #ifdef NEW_BUS_PCI
154                 /*
155                  * XXX this apic stuff looks totally busted.  It should
156                  * move to the nexus code which actually registers the
157                  * interrupt.
158                  */
159 #endif
160
161 #ifdef APIC_IO
162                 nextpin = next_apic_irq(irq);
163                 
164                 if (nextpin < 0)
165                         return 1;
166
167                 /* 
168                  * Attempt handling of some broken mp tables.
169                  *
170                  * It's OK to yell (since the mp tables are broken).
171                  * 
172                  * Hanging in the boot is not OK
173                  */
174
175                 muxcnt = 2;
176                 nextpin = next_apic_irq(nextpin);
177                 while (muxcnt < 5 && nextpin >= 0) {
178                         muxcnt++;
179                         nextpin = next_apic_irq(nextpin);
180                 }
181                 if (muxcnt >= 5) {
182                         kprintf("bogus MP table, more than 4 IO APIC pins connected to the same PCI device or ISA/EISA interrupt\n");
183                         return 0;
184                 }
185                 
186                 kprintf("bogus MP table, %d IO APIC pins connected to the same PCI device or ISA/EISA interrupt\n", muxcnt);
187
188                 nextpin = next_apic_irq(irq);
189                 while (nextpin >= 0) {
190                         rid = 0;
191                         res = bus_alloc_resource(cfg->dev, SYS_RES_IRQ, &rid,
192                                                  nextpin, nextpin, 1,
193                                                  resflags);
194                         if (!res) {
195                                 kprintf("pci_map_int: can't allocate extra interrupt\n");
196                                 return 0;
197                         }
198                         error = BUS_SETUP_INTR(device_get_parent(cfg->dev),
199                                                cfg->dev, res, flags,
200                                                handler, arg, &ih, NULL);
201                         if (error != 0) {
202                                 kprintf("pci_map_int: BUS_SETUP_INTR failed\n");
203                                 return 0;
204                         }
205                         kprintf("Registered extra interrupt handler for int %d (in addition to int %d)\n", nextpin, irq);
206                         nextpin = next_apic_irq(nextpin);
207                 }
208 #endif
209         }
210         return (1);
211 }
212
213 int
214 pci_unmap_int(pcici_t cfg)
215 {
216         return (0); /* not supported, yet, since cfg doesn't know about idesc */
217 }
218
219 pcici_t
220 pci_get_parent_from_tag(pcici_t tag)
221 {
222         return (pcici_t)pci_devlist_get_parent(tag);
223 }
224
225 int
226 pci_get_bus_from_tag(pcici_t tag)
227 {
228         return tag->bus;
229 }
230
231 /*
232  * A simple driver to wrap the old pci driver mechanism for back-compat.
233  */
234
235 static int
236 pci_compat_probe(device_t dev)
237 {
238         struct pci_device *dvp;
239         struct pci_devinfo *dinfo;
240         pcicfgregs *cfg;
241         const char *name;
242         int error;
243         
244         dinfo = device_get_ivars(dev);
245         cfg = &dinfo->cfg;
246         dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
247
248         /*
249          * Do the wrapped probe.
250          */
251         error = ENXIO;
252         if (dvp && dvp->pd_probe) {
253                 name = dvp->pd_probe(cfg, (cfg->device << 16) + cfg->vendor);
254                 if (name) {
255                         device_set_desc_copy(dev, name);
256                         /* Allow newbus drivers to match "better" */
257                         error = -200;
258                 }
259         }
260
261         return error;
262 }
263
264 static int
265 pci_compat_attach(device_t dev)
266 {
267         struct pci_device *dvp;
268         struct pci_devinfo *dinfo;
269         pcicfgregs *cfg;
270         int unit;
271
272         dinfo = device_get_ivars(dev);
273         cfg = &dinfo->cfg;
274         dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
275
276         unit = device_get_unit(dev);
277         if (unit > *dvp->pd_count)
278                 *dvp->pd_count = unit;
279         if (dvp->pd_attach)
280                 dvp->pd_attach(cfg, unit);
281         device_printf(dev, "driver is using old-style compatibility shims\n");
282         return 0;
283 }
284
285 static device_method_t pci_compat_methods[] = {
286         /* Device interface */
287         DEVMETHOD(device_probe,         pci_compat_probe),
288         DEVMETHOD(device_attach,        pci_compat_attach),
289
290         { 0, 0 }
291 };
292
293 /*
294  * Create a new style driver around each old pci driver.
295  */
296 int
297 compat_pci_handler(module_t mod, int type, void *data)
298 {
299         struct pci_device *dvp = (struct pci_device *)data;
300         struct pci_compat_driver *driver;
301         devclass_t pci_devclass = devclass_find("pci");
302
303         switch (type) {
304         case MOD_LOAD:
305                 driver = kmalloc(sizeof(struct pci_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
306                 driver->driver.name = dvp->pd_name;
307                 driver->driver.methods = pci_compat_methods;
308                 driver->driver.size = sizeof(struct pci_devinfo *);
309                 driver->dvp = dvp;
310                 devclass_add_driver(pci_devclass, (driver_t *)driver);
311                 break;
312         case MOD_UNLOAD:
313                 kprintf("%s: module unload not supported!\n", dvp->pd_name);
314                 return EOPNOTSUPP;
315         default:
316                 break;
317         }
318         return 0;
319 }