WARNS6 cleanups
[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.10 2005/11/04 08:57:22 dillon 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 <machine/bus.h>
45 #include <sys/rman.h>
46 #include <machine/resource.h>
47 #include <machine/smp.h>
48 #include <sys/interrupt.h>
49
50 #include <sys/pciio.h>
51 #include "pcireg.h"
52 #include "pcivar.h"
53
54 struct pci_compat_driver {
55         driver_t driver;
56         struct pci_device *dvp;
57 };
58
59 /* ------------------------------------------------------------------------- */
60
61 u_long
62 pci_conf_read(pcici_t cfg, u_long reg)
63 {
64         return (pci_read_config(cfg->dev, reg, 4));
65 }
66
67 void
68 pci_conf_write(pcici_t cfg, u_long reg, u_long data)
69 {
70         pci_write_config(cfg->dev, reg, data, 4);
71 }
72
73 int
74 pci_map_port(pcici_t cfg, u_long reg, pci_port_t* pa)
75 {
76         int rid;
77         struct resource *res;
78
79         rid = reg;
80         res = bus_alloc_resource(cfg->dev, SYS_RES_IOPORT, &rid,
81                                  0, ~0, 1, RF_ACTIVE);
82         if (res) {
83                 *pa = rman_get_start(res);
84                 return (1);
85         }
86         return (0);
87 }
88
89 int
90 pci_map_mem(pcici_t cfg, u_long reg, vm_offset_t* va, vm_offset_t* pa)
91 {
92         int rid;
93         struct resource *res;
94
95         rid = reg;
96         res = bus_alloc_resource(cfg->dev, SYS_RES_MEMORY, &rid,
97                                  0, ~0, 1, RF_ACTIVE);
98         if (res) {
99                 *pa = rman_get_start(res);
100                 *va = (vm_offset_t) rman_get_virtual(res);
101                 return (1);
102         }
103         return (0);
104 }
105
106 int
107 pci_map_int(pcici_t cfg, pci_inthand_t *handler, void *arg)
108 {
109         return (pci_map_int_right(cfg, handler, arg, 0));
110 }
111
112 int
113 pci_map_int_right(pcici_t cfg, pci_inthand_t *handler, void *arg, u_int intflags)
114 {
115         int error;
116 #ifdef APIC_IO
117         int nextpin, muxcnt;
118 #endif
119         if (cfg->intpin != 0) {
120                 int irq = cfg->intline;
121                 int rid = 0;
122                 struct resource *res;
123                 int flags = 0;
124                 int resflags = RF_SHAREABLE|RF_ACTIVE;
125                 void *ih;
126
127                 if (intflags & INTR_FAST)
128                         flags |= INTR_FAST;
129                 if (intflags & INTR_EXCL)
130                         resflags &= ~RF_SHAREABLE;
131
132                 res = bus_alloc_resource(cfg->dev, SYS_RES_IRQ, &rid,
133                                          irq, irq, 1, resflags);
134                 if (!res) {
135                         printf("pci_map_int: can't allocate interrupt\n");
136                         return 0;
137                 }
138
139                 /*
140                  * This is ugly. Translate the mask into an interrupt type.
141                  */
142
143                 error = BUS_SETUP_INTR(device_get_parent(cfg->dev), cfg->dev,
144                                        res, flags, handler, arg, &ih, NULL);
145                 if (error != 0)
146                         return 0;
147
148 #ifdef NEW_BUS_PCI
149                 /*
150                  * XXX this apic stuff looks totally busted.  It should
151                  * move to the nexus code which actually registers the
152                  * interrupt.
153                  */
154 #endif
155
156 #ifdef APIC_IO
157                 nextpin = next_apic_irq(irq);
158                 
159                 if (nextpin < 0)
160                         return 1;
161
162                 /* 
163                  * Attempt handling of some broken mp tables.
164                  *
165                  * It's OK to yell (since the mp tables are broken).
166                  * 
167                  * Hanging in the boot is not OK
168                  */
169
170                 muxcnt = 2;
171                 nextpin = next_apic_irq(nextpin);
172                 while (muxcnt < 5 && nextpin >= 0) {
173                         muxcnt++;
174                         nextpin = next_apic_irq(nextpin);
175                 }
176                 if (muxcnt >= 5) {
177                         printf("bogus MP table, more than 4 IO APIC pins connected to the same PCI device or ISA/EISA interrupt\n");
178                         return 0;
179                 }
180                 
181                 printf("bogus MP table, %d IO APIC pins connected to the same PCI device or ISA/EISA interrupt\n", muxcnt);
182
183                 nextpin = next_apic_irq(irq);
184                 while (nextpin >= 0) {
185                         rid = 0;
186                         res = bus_alloc_resource(cfg->dev, SYS_RES_IRQ, &rid,
187                                                  nextpin, nextpin, 1,
188                                                  resflags);
189                         if (!res) {
190                                 printf("pci_map_int: can't allocate extra interrupt\n");
191                                 return 0;
192                         }
193                         error = BUS_SETUP_INTR(device_get_parent(cfg->dev),
194                                                cfg->dev, res, flags,
195                                                handler, arg, &ih, NULL);
196                         if (error != 0) {
197                                 printf("pci_map_int: BUS_SETUP_INTR failed\n");
198                                 return 0;
199                         }
200                         printf("Registered extra interrupt handler for int %d (in addition to int %d)\n", nextpin, irq);
201                         nextpin = next_apic_irq(nextpin);
202                 }
203 #endif
204         }
205         return (1);
206 }
207
208 int
209 pci_unmap_int(pcici_t cfg)
210 {
211         return (0); /* not supported, yet, since cfg doesn't know about idesc */
212 }
213
214 pcici_t
215 pci_get_parent_from_tag(pcici_t tag)
216 {
217         return (pcici_t)pci_devlist_get_parent(tag);
218 }
219
220 int
221 pci_get_bus_from_tag(pcici_t tag)
222 {
223         return tag->bus;
224 }
225
226 /*
227  * A simple driver to wrap the old pci driver mechanism for back-compat.
228  */
229
230 static int
231 pci_compat_probe(device_t dev)
232 {
233         struct pci_device *dvp;
234         struct pci_devinfo *dinfo;
235         pcicfgregs *cfg;
236         const char *name;
237         int error;
238         
239         dinfo = device_get_ivars(dev);
240         cfg = &dinfo->cfg;
241         dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
242
243         /*
244          * Do the wrapped probe.
245          */
246         error = ENXIO;
247         if (dvp && dvp->pd_probe) {
248                 name = dvp->pd_probe(cfg, (cfg->device << 16) + cfg->vendor);
249                 if (name) {
250                         device_set_desc_copy(dev, name);
251                         /* Allow newbus drivers to match "better" */
252                         error = -200;
253                 }
254         }
255
256         return error;
257 }
258
259 static int
260 pci_compat_attach(device_t dev)
261 {
262         struct pci_device *dvp;
263         struct pci_devinfo *dinfo;
264         pcicfgregs *cfg;
265         int unit;
266
267         dinfo = device_get_ivars(dev);
268         cfg = &dinfo->cfg;
269         dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
270
271         unit = device_get_unit(dev);
272         if (unit > *dvp->pd_count)
273                 *dvp->pd_count = unit;
274         if (dvp->pd_attach)
275                 dvp->pd_attach(cfg, unit);
276         device_printf(dev, "driver is using old-style compatability shims\n");
277         return 0;
278 }
279
280 static device_method_t pci_compat_methods[] = {
281         /* Device interface */
282         DEVMETHOD(device_probe,         pci_compat_probe),
283         DEVMETHOD(device_attach,        pci_compat_attach),
284
285         { 0, 0 }
286 };
287
288 /*
289  * Create a new style driver around each old pci driver.
290  */
291 int
292 compat_pci_handler(module_t mod, int type, void *data)
293 {
294         struct pci_device *dvp = (struct pci_device *)data;
295         struct pci_compat_driver *driver;
296         devclass_t pci_devclass = devclass_find("pci");
297
298         switch (type) {
299         case MOD_LOAD:
300                 driver = malloc(sizeof(struct pci_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
301                 driver->driver.name = dvp->pd_name;
302                 driver->driver.methods = pci_compat_methods;
303                 driver->driver.size = sizeof(struct pci_devinfo *);
304                 driver->dvp = dvp;
305                 devclass_add_driver(pci_devclass, (driver_t *)driver);
306                 break;
307         case MOD_UNLOAD:
308                 printf("%s: module unload not supported!\n", dvp->pd_name);
309                 return EOPNOTSUPP;
310         default:
311                 break;
312         }
313         return 0;
314 }