proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / i386 / i386 / nexus.c
1 /*
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/i386/nexus.c,v 1.26.2.10 2003/02/22 13:16:45 imp Exp $
30  * $DragonFly: src/sys/i386/i386/Attic/nexus.c,v 1.2 2003/06/17 04:28:35 dillon Exp $
31  */
32
33 /*
34  * This code implements a `root nexus' for Intel Architecture
35  * machines.  The function of the root nexus is to serve as an
36  * attachment point for both processors and buses, and to manage
37  * resources which are common to all of them.  In particular,
38  * this code implements the core resource managers for interrupt
39  * requests, DMA requests (which rightfully should be a part of the
40  * ISA code but it's easier to do it here for now), I/O port addresses,
41  * and I/O memory address space.
42  */
43
44 #include "mca.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bus.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
53 #include <sys/rman.h>
54
55 #include <machine/vmparam.h>
56 #include <vm/vm.h>
57 #include <vm/pmap.h>
58 #include <machine/pmap.h>
59
60 #include <machine/resource.h>
61 #ifdef APIC_IO
62 #include <machine/smp.h>
63 #include <machine/mpapic.h>
64 #endif
65
66 #ifdef PC98
67 #include <pc98/pc98/pc98.h>
68 #else
69 #include <i386/isa/isa.h>
70 #endif
71 #include <i386/isa/intr_machdep.h>
72
73 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
74 struct nexus_device {
75         struct resource_list    nx_resources;
76 };
77
78 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
79
80 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
81
82 static  int nexus_probe(device_t);
83 static  int nexus_attach(device_t);
84 static  int nexus_print_all_resources(device_t dev);
85 static  int nexus_print_child(device_t, device_t);
86 static device_t nexus_add_child(device_t bus, int order, const char *name,
87                                 int unit);
88 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
89                                               u_long, u_long, u_long, u_int);
90 static  int nexus_activate_resource(device_t, device_t, int, int,
91                                     struct resource *);
92 static  int nexus_deactivate_resource(device_t, device_t, int, int,
93                                       struct resource *);
94 static  int nexus_release_resource(device_t, device_t, int, int,
95                                    struct resource *);
96 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
97                              void (*)(void *), void *, void **);
98 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
99                                 void *);
100 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
101 static  int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
102 static void nexus_delete_resource(device_t, device_t, int, int);
103
104 static device_method_t nexus_methods[] = {
105         /* Device interface */
106         DEVMETHOD(device_probe,         nexus_probe),
107         DEVMETHOD(device_attach,        nexus_attach),
108         DEVMETHOD(device_detach,        bus_generic_detach),
109         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
110         DEVMETHOD(device_suspend,       bus_generic_suspend),
111         DEVMETHOD(device_resume,        bus_generic_resume),
112
113         /* Bus interface */
114         DEVMETHOD(bus_print_child,      nexus_print_child),
115         DEVMETHOD(bus_add_child,        nexus_add_child),
116         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
117         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
118         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
119         DEVMETHOD(bus_release_resource, nexus_release_resource),
120         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
121         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
122         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
123         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
124         DEVMETHOD(bus_set_resource,     nexus_set_resource),
125         DEVMETHOD(bus_get_resource,     nexus_get_resource),
126         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
127
128         { 0, 0 }
129 };
130
131 static driver_t nexus_driver = {
132         "nexus",
133         nexus_methods,
134         1,                      /* no softc */
135 };
136 static devclass_t nexus_devclass;
137
138 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
139
140 static int
141 nexus_probe(device_t dev)
142 {
143
144         device_quiet(dev);      /* suppress attach message for neatness */
145
146         /*
147          * IRQ's are on the mainboard on old systems, but on the ISA part
148          * of PCI->ISA bridges.  There would be multiple sets of IRQs on
149          * multi-ISA-bus systems.  PCI interrupts are routed to the ISA
150          * component, so in a way, PCI can be a partial child of an ISA bus(!).
151          * APIC interrupts are global though.
152          * In the non-APIC case, disallow the use of IRQ 2.
153          */
154         irq_rman.rm_start = 0;
155         irq_rman.rm_type = RMAN_ARRAY;
156         irq_rman.rm_descr = "Interrupt request lines";
157 #ifdef APIC_IO
158         irq_rman.rm_end = APIC_INTMAPSIZE - 1;
159         if (rman_init(&irq_rman)
160             || rman_manage_region(&irq_rman,
161                                   irq_rman.rm_start, irq_rman.rm_end))
162                 panic("nexus_probe irq_rman");
163 #else
164         irq_rman.rm_end = 15;
165 #ifdef PC98
166         if (rman_init(&irq_rman)
167             || rman_manage_region(&irq_rman,
168                                   irq_rman.rm_start, irq_rman.rm_end))
169                 panic("nexus_probe irq_rman");
170 #else
171         if (rman_init(&irq_rman)
172             || rman_manage_region(&irq_rman, irq_rman.rm_start, 1)
173             || rman_manage_region(&irq_rman, 3, irq_rman.rm_end))
174                 panic("nexus_probe irq_rman");
175 #endif /* PC98 */
176 #endif
177
178         /*
179          * ISA DMA on PCI systems is implemented in the ISA part of each
180          * PCI->ISA bridge and the channels can be duplicated if there are
181          * multiple bridges.  (eg: laptops with docking stations)
182          */
183         drq_rman.rm_start = 0;
184 #ifdef PC98
185         drq_rman.rm_end = 3;
186 #else
187         drq_rman.rm_end = 7;
188 #endif
189         drq_rman.rm_type = RMAN_ARRAY;
190         drq_rman.rm_descr = "DMA request lines";
191         /* XXX drq 0 not available on some machines */
192         if (rman_init(&drq_rman)
193             || rman_manage_region(&drq_rman,
194                                   drq_rman.rm_start, drq_rman.rm_end))
195                 panic("nexus_probe drq_rman");
196
197         /*
198          * However, IO ports and Memory truely are global at this level,
199          * as are APIC interrupts (however many IO APICS there turn out
200          * to be on large systems..)
201          */
202         port_rman.rm_start = 0;
203         port_rman.rm_end = 0xffff;
204         port_rman.rm_type = RMAN_ARRAY;
205         port_rman.rm_descr = "I/O ports";
206         if (rman_init(&port_rman)
207             || rman_manage_region(&port_rman, 0, 0xffff))
208                 panic("nexus_probe port_rman");
209
210         mem_rman.rm_start = 0;
211         mem_rman.rm_end = ~0u;
212         mem_rman.rm_type = RMAN_ARRAY;
213         mem_rman.rm_descr = "I/O memory addresses";
214         if (rman_init(&mem_rman)
215             || rman_manage_region(&mem_rman, 0, ~0))
216                 panic("nexus_probe mem_rman");
217
218         return bus_generic_probe(dev);
219 }
220
221 static int
222 nexus_attach(device_t dev)
223 {
224         device_t        child;
225
226         /*
227          * First, deal with the children we know about already
228          */
229         bus_generic_attach(dev);
230         /*
231          * And if we didn't see EISA or ISA on a pci bridge, create some
232          * connection points now so they show up "on motherboard".
233          */
234         if (!devclass_get_device(devclass_find("eisa"), 0)) {
235                 child = BUS_ADD_CHILD(dev, 0, "eisa", 0);
236                 if (child == NULL)
237                         panic("nexus_attach eisa");
238                 device_probe_and_attach(child);
239         }
240 #if NMCA > 0
241         if (!devclass_get_device(devclass_find("mca"), 0)) {
242                 child = BUS_ADD_CHILD(dev, 0, "mca", 0);
243                 if (child == NULL)
244                         panic("nexus_probe mca");
245                 device_probe_and_attach(child);
246         }
247 #endif
248         if (!devclass_get_device(devclass_find("isa"), 0)) {
249                 child = BUS_ADD_CHILD(dev, 0, "isa", 0);
250                 if (child == NULL)
251                         panic("nexus_attach isa");
252                 device_probe_and_attach(child);
253         }
254
255         return 0;
256 }
257
258 static int
259 nexus_print_all_resources(device_t dev)
260 {
261         struct  nexus_device *ndev = DEVTONX(dev);
262         struct resource_list *rl = &ndev->nx_resources;
263         int retval = 0;
264
265         if (SLIST_FIRST(rl))
266                 retval += printf(" at");
267         
268         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
269         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
270         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
271
272         return retval;
273 }
274
275 static int
276 nexus_print_child(device_t bus, device_t child)
277 {
278         int retval = 0;
279
280         retval += bus_print_child_header(bus, child);
281         retval += nexus_print_all_resources(child);
282         retval += printf(" on motherboard\n");
283
284         return (retval);
285 }
286
287 static device_t
288 nexus_add_child(device_t bus, int order, const char *name, int unit)
289 {
290         device_t                child;
291         struct nexus_device     *ndev;
292
293         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
294         if (!ndev)
295                 return(0);
296         resource_list_init(&ndev->nx_resources);
297
298         child = device_add_child_ordered(bus, order, name, unit); 
299
300         /* should we free this in nexus_child_detached? */
301         device_set_ivars(child, ndev);
302
303         return(child);
304 }
305
306 /*
307  * Allocate a resource on behalf of child.  NB: child is usually going to be a
308  * child of one of our descendants, not a direct child of nexus0.
309  * (Exceptions include npx.)
310  */
311 static struct resource *
312 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
313                      u_long start, u_long end, u_long count, u_int flags)
314 {
315         struct nexus_device *ndev = DEVTONX(child);
316         struct  resource *rv;
317         struct resource_list_entry *rle;
318         struct  rman *rm;
319         int needactivate = flags & RF_ACTIVE;
320
321         /*
322          * If this is an allocation of the "default" range for a given RID, and
323          * we know what the resources for this device are (ie. they aren't maintained
324          * by a child bus), then work out the start/end values.
325          */
326         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
327                 if (ndev == NULL)
328                         return(NULL);
329                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
330                 if (rle == NULL)
331                         return(NULL);
332                 start = rle->start;
333                 end = rle->end;
334                 count = rle->count;
335         }
336
337         flags &= ~RF_ACTIVE;
338
339         switch (type) {
340         case SYS_RES_IRQ:
341                 rm = &irq_rman;
342                 break;
343
344         case SYS_RES_DRQ:
345                 rm = &drq_rman;
346                 break;
347
348         case SYS_RES_IOPORT:
349                 rm = &port_rman;
350                 break;
351
352         case SYS_RES_MEMORY:
353                 rm = &mem_rman;
354                 break;
355
356         default:
357                 return 0;
358         }
359
360         rv = rman_reserve_resource(rm, start, end, count, flags, child);
361         if (rv == 0)
362                 return 0;
363
364         if (type == SYS_RES_MEMORY) {
365                 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
366         } else if (type == SYS_RES_IOPORT) {
367                 rman_set_bustag(rv, I386_BUS_SPACE_IO);
368 #ifndef PC98
369                 rman_set_bushandle(rv, rv->r_start);
370 #endif
371         }
372
373 #ifdef PC98
374         if ((type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) &&
375             i386_bus_space_handle_alloc(rv->r_bustag, rv->r_start, count,
376                                         &rv->r_bushandle) != 0) {
377                 rman_release_resource(rv);
378                 return 0;
379         }
380 #endif
381
382         if (needactivate) {
383                 if (bus_activate_resource(child, type, *rid, rv)) {
384 #ifdef PC98
385                         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
386                                 i386_bus_space_handle_free(rv->r_bustag,
387                                   rv->r_bushandle, rv->r_bushandle->bsh_sz);
388                         }
389 #endif
390                         rman_release_resource(rv);
391                         return 0;
392                 }
393         }
394         
395         return rv;
396 }
397
398 static int
399 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
400                         struct resource *r)
401 {
402         /*
403          * If this is a memory resource, map it into the kernel.
404          */
405         if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
406                 caddr_t vaddr = 0;
407
408                 if (r->r_end < 1024 * 1024) {
409                         /*
410                          * The first 1Mb is mapped at KERNBASE.
411                          */
412                         vaddr = (caddr_t)(uintptr_t)(KERNBASE + r->r_start);
413                 } else {
414                         u_int32_t paddr;
415                         u_int32_t psize;
416                         u_int32_t poffs;
417
418                         paddr = r->r_start;
419                         psize = r->r_end - r->r_start;
420
421                         poffs = paddr - trunc_page(paddr);
422                         vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
423                 }
424                 rman_set_virtual(r, vaddr);
425 #ifdef PC98
426                 /* PC-98: the type of bus_space_handle_t is the structure. */
427                 r->r_bushandle->bsh_base = (bus_addr_t) vaddr;
428 #else
429                 /* IBM-PC: the type of bus_space_handle_t is u_int */
430                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
431 #endif
432         }
433         return (rman_activate_resource(r));
434 }
435
436 static int
437 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
438                           struct resource *r)
439 {
440         /*
441          * If this is a memory resource, unmap it.
442          */
443         if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) && (r->r_end >= 1024 * 1024)) {
444                 u_int32_t psize;
445
446                 psize = r->r_end - r->r_start;
447                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
448         }
449                 
450         return (rman_deactivate_resource(r));
451 }
452
453 static int
454 nexus_release_resource(device_t bus, device_t child, int type, int rid,
455                        struct resource *r)
456 {
457         if (r->r_flags & RF_ACTIVE) {
458                 int error = bus_deactivate_resource(child, type, rid, r);
459                 if (error)
460                         return error;
461         }
462 #ifdef PC98
463         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
464                 i386_bus_space_handle_free(r->r_bustag, r->r_bushandle,
465                                            r->r_bushandle->bsh_sz);
466         }
467 #endif
468         return (rman_release_resource(r));
469 }
470
471 /*
472  * Currently this uses the really grody interface from kern/kern_intr.c
473  * (which really doesn't belong in kern/anything.c).  Eventually, all of
474  * the code in kern_intr.c and machdep_intr.c should get moved here, since
475  * this is going to be the official interface.
476  */
477 static int
478 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
479                  int flags, void (*ihand)(void *), void *arg, void **cookiep)
480 {
481         intrmask_t      *mask;
482         driver_t        *driver;
483         int     error, icflags;
484
485         /* somebody tried to setup an irq that failed to allocate! */
486         if (irq == NULL)
487                 panic("nexus_setup_intr: NULL irq resource!");
488
489         *cookiep = 0;
490         if (irq->r_flags & RF_SHAREABLE)
491                 icflags = 0;
492         else
493                 icflags = INTR_EXCL;
494
495         driver = device_get_driver(child);
496         switch (flags & ~INTR_TYPE_FAST) {
497         case INTR_TYPE_TTY:
498                 mask = &tty_imask;
499                 break;
500         case INTR_TYPE_BIO:
501                 mask = &bio_imask;
502                 break;
503         case INTR_TYPE_NET:
504                 mask = &net_imask;
505                 break;
506         case INTR_TYPE_CAM:
507                 mask = &cam_imask;
508                 break;
509         case INTR_TYPE_MISC:
510                 mask = 0;
511                 break;
512         default:
513                 panic("still using grody create_intr interface");
514         }
515         if (flags & INTR_TYPE_FAST)
516                 icflags |= INTR_FAST;
517
518         /*
519          * We depend here on rman_activate_resource() being idempotent.
520          */
521         error = rman_activate_resource(irq);
522         if (error)
523                 return (error);
524
525         *cookiep = inthand_add(device_get_nameunit(child), irq->r_start,
526             ihand, arg, mask, icflags);
527         if (*cookiep == NULL)
528                 error = EINVAL; /* XXX ??? */
529
530         return (error);
531 }
532
533 static int
534 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
535 {
536         return (inthand_remove(ih));
537 }
538
539 static int
540 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
541 {
542         struct nexus_device     *ndev = DEVTONX(child);
543         struct resource_list    *rl = &ndev->nx_resources;
544
545         /* XXX this should return a success/failure indicator */
546         resource_list_add(rl, type, rid, start, start + count - 1, count);
547         return(0);
548 }
549
550 static int
551 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
552 {
553         struct nexus_device     *ndev = DEVTONX(child);
554         struct resource_list    *rl = &ndev->nx_resources;
555         struct resource_list_entry *rle;
556
557         rle = resource_list_find(rl, type, rid);
558         device_printf(child, "type %d  rid %d  startp %p  countp %p - got %p\n",
559                       type, rid, startp, countp, rle);
560         if (!rle)
561                 return(ENOENT);
562         if (startp)
563                 *startp = rle->start;
564         if (countp)
565                 *countp = rle->count;
566         return(0);
567 }
568
569 static void
570 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
571 {
572         struct nexus_device     *ndev = DEVTONX(child);
573         struct resource_list    *rl = &ndev->nx_resources;
574
575         resource_list_delete(rl, type, rid);
576 }