resource: Per-CPU hardware resources support, step 3 of many
[dragonfly.git] / sys / bus / isa / i386 / isa_compat.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/isa/isa_compat.c,v 1.18.2.1 2001/05/17 23:05:06 imp Exp $
27  * $DragonFly: src/sys/bus/isa/i386/isa_compat.c,v 1.12 2006/12/22 23:12:16 swildner Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/rman.h>
37 #include <sys/machintr.h>
38
39 #include <machine/vmparam.h>
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <machine/pmap.h>
43 #include <machine/md_var.h>
44
45 #include "../isavar.h"
46 #include "isa_compat.h"
47 #include "isa_device.h"
48
49 struct isa_compat_resources {
50     struct resource *ports;
51     struct resource *memory;
52     struct resource *drq;
53     struct resource *irq;
54 };
55
56 struct isa_compat_driver {
57         driver_t driver;
58         struct old_isa_driver *op;
59 };
60
61 int
62 isa_compat_nextid(void)
63 {
64         static int id = 2;      /* id_id of -1, 0 and 1 are "used" */
65
66         return id++;
67 }
68
69 static void
70 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
71 {
72         int rid;
73         u_long start, count;
74
75         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
76                              &start, &count) == 0) {
77                 rid = 0;
78                 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
79                                                 &rid, 0ul, ~0ul, 1,
80                                                 RF_ACTIVE);
81                 if (res->ports == NULL && bootverbose)
82                         kprintf("isa_compat: didn't get ports for %s\n",
83                                device_get_name(dev));
84         } else
85                 res->ports = 0;
86
87         if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
88                              &start, &count) == 0
89             && start != 0) {
90                 rid = 0;
91                 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
92                                                  &rid, 0ul, ~0ul, 1,
93                                                  RF_ACTIVE);
94                 if (res->memory == NULL && bootverbose)
95                         kprintf("isa_compat: didn't get memory for %s\n",
96                                device_get_name(dev));
97         } else
98                 res->memory = 0;
99
100         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
101                              &start, &count) == 0) {
102                 rid = 0;
103                 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
104                                               &rid, 0ul, ~0ul, 1,
105                                               RF_ACTIVE);
106                 if (res->drq == NULL && bootverbose)
107                         kprintf("isa_compat: didn't get drq for %s\n",
108                                device_get_name(dev));
109         } else
110                 res->drq = 0;
111
112         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
113                              &start, &count) == 0) {
114                 rid = 0;
115                 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
116                                               &rid, 0ul, ~0ul, 1,
117                                               RF_SHAREABLE | RF_ACTIVE);
118                 if (res->irq == NULL && bootverbose)
119                         kprintf("isa_compat: didn't get irq for %s\n",
120                                device_get_name(dev));
121         } else
122                 res->irq = 0;
123 }
124
125 static void
126 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
127 {
128         if (res->ports) {
129                 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
130                 res->ports = 0;
131         }
132         if (res->memory) {
133                 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
134                 res->memory = 0;
135         }
136         if (res->drq) {
137                 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
138                 res->drq = 0;
139         }
140         if (res->irq) {
141                 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
142                 res->irq = 0;
143         }
144 }
145
146 #define irqmask(x)      ((x) < 0 ? 0 : (1 << (x)))
147
148 static int
149 isa_compat_probe(device_t dev)
150 {
151         struct isa_device *dvp = device_get_softc(dev);
152         struct isa_compat_resources res;
153         struct old_isa_driver *op;
154         u_long start, count;
155
156         /* No pnp support */
157         if (isa_get_vendorid(dev))
158                 return (ENXIO);
159
160         bzero(&res, sizeof(res));
161         /*
162          * Fill in the isa_device fields.
163          */
164         op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
165         dvp->id_id = isa_compat_nextid();
166         dvp->id_driver = op->driver;
167         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
168                              &start, &count) == 0)
169                 dvp->id_iobase = start;
170         else
171                 dvp->id_iobase = -1;
172         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
173                              &start, &count) == 0)
174                 dvp->id_irq = irqmask(start);
175         else
176                 dvp->id_irq = 0;
177         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
178                              &start, &count) == 0)
179                 dvp->id_drq = start;
180         else
181                 dvp->id_drq = -1;
182         if (bus_get_resource(dev, SYS_RES_MEMORY,
183                              0, &start, &count) == 0) {
184                 dvp->id_maddr = (void *)(uintptr_t)start;
185                 dvp->id_msize = count;
186         } else {
187                 dvp->id_maddr = NULL;
188                 dvp->id_msize = 0;
189         }
190         dvp->id_unit = device_get_unit(dev);
191         dvp->id_flags = device_get_flags(dev);
192         dvp->id_enabled = device_is_enabled(dev);       /* XXX unused */
193         dvp->id_device = dev;
194
195         /*
196          * Do the wrapped probe.
197          */
198         if (dvp->id_driver->probe) {
199                 int portsize;
200                 void *maddr;
201                 struct isa_device old;
202
203                 isa_compat_alloc_resources(dev, &res);
204                 if (res.memory)
205                         maddr = rman_get_virtual(res.memory);
206                 else
207                         maddr = 0;
208                 dvp->id_maddr = maddr;
209                 old = *dvp;
210                 portsize = dvp->id_driver->probe(dvp);
211                 isa_compat_release_resources(dev, &res);
212                 if (portsize != 0) {
213                         if (portsize > 0 || dvp->id_iobase != old.id_iobase) {
214                                 bus_set_resource(dev, SYS_RES_IOPORT, 0,
215                                     dvp->id_iobase, portsize, -1);
216                         }
217                         if (dvp->id_irq != old.id_irq) {
218                                 int intr = ffs(dvp->id_irq) - 1;
219
220                                 bus_set_resource(dev, SYS_RES_IRQ, 0, intr, 1,
221                                     machintr_intr_cpuid(intr));
222                         }
223                         if (dvp->id_drq != old.id_drq) {
224                                 bus_set_resource(dev, SYS_RES_DRQ, 0,
225                                     dvp->id_drq, 1, -1);
226                         }
227                         if (dvp->id_maddr != old.id_maddr
228                             || dvp->id_msize != old.id_msize) {
229                                 maddr = dvp->id_maddr;
230                                 if (maddr != NULL) {
231                                         bus_set_resource(dev, SYS_RES_MEMORY, 0,
232                                             kvtop(maddr), dvp->id_msize, -1);
233                                 } else {
234                                         bus_delete_resource(dev,
235                                             SYS_RES_MEMORY, 0);
236                                 }
237                         }
238                         return 0;
239                 }
240         }
241         return ENXIO;
242 }
243
244 static int
245 isa_compat_attach(device_t dev)
246 {
247         struct isa_device *dvp = device_get_softc(dev);
248         struct isa_compat_resources res;
249         int error;
250
251         bzero(&res, sizeof(res));
252         isa_compat_alloc_resources(dev, &res);
253         if (dvp->id_driver->attach)
254                 dvp->id_driver->attach(dvp);
255         if (res.irq && dvp->id_irq && dvp->id_intr) {
256                 struct old_isa_driver *op;
257                 void *ih;
258
259                 op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
260                 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
261                                        res.irq, op->type,
262                                        dvp->id_intr,
263                                        (void *)(uintptr_t)dvp->id_unit,
264                                        &ih, NULL);
265                 if (error)
266                         kprintf("isa_compat_attach: failed to setup intr: %d\n",
267                                error);
268         }
269         device_printf(dev, "driver is using old-style compatibility shims\n");
270         return 0;
271 }
272
273 static device_method_t isa_compat_methods[] = {
274         /* Device interface */
275         DEVMETHOD(device_probe,         isa_compat_probe),
276         DEVMETHOD(device_attach,        isa_compat_attach),
277
278         { 0, 0 }
279 };
280
281 /*
282  * Create a new style driver around each old isa driver.
283  */
284 void
285 isa_wrap_old_drivers(void)
286 {
287         int i;
288         struct old_isa_driver *op;
289         devclass_t isa_devclass = devclass_find("isa");
290         struct isa_compat_driver *driver;
291
292         for (i = 0, op = &old_drivers[0]; i < old_drivers_count; i++, op++) {
293                 driver = kmalloc(sizeof(struct isa_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
294                 driver->driver.name = op->driver->name;
295                 driver->driver.methods = isa_compat_methods;
296                 driver->driver.size = sizeof(struct isa_device);
297                 driver->op = op;
298                 if (op->driver->sensitive_hw)
299                         resource_set_int(op->driver->name, -1, "sensitive", 1);
300                 devclass_add_driver(isa_devclass, (driver_t *)driver);
301         }
302 }