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