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