2 * Copyright 1998 Massachusetts Institute of Technology
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
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
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.3 2003/08/07 21:17:22 dillon Exp $
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.
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
55 #include <machine/vmparam.h>
58 #include <machine/pmap.h>
60 #include <machine/resource.h>
62 #include <machine/smp.h>
63 #include <machine/mpapic.h>
67 #include <pc98/pc98/pc98.h>
69 #include <bus/isa/i386/isa.h>
71 #include <i386/isa/intr_machdep.h>
73 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
75 struct resource_list nx_resources;
78 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
80 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
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,
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,
92 static int nexus_deactivate_resource(device_t, device_t, int, int,
94 static int nexus_release_resource(device_t, device_t, int, int,
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 *,
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);
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),
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),
131 static driver_t nexus_driver = {
136 static devclass_t nexus_devclass;
138 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
141 nexus_probe(device_t dev)
144 device_quiet(dev); /* suppress attach message for neatness */
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.
154 irq_rman.rm_start = 0;
155 irq_rman.rm_type = RMAN_ARRAY;
156 irq_rman.rm_descr = "Interrupt request lines";
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");
164 irq_rman.rm_end = 15;
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");
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");
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)
183 drq_rman.rm_start = 0;
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");
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..)
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");
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");
218 return bus_generic_probe(dev);
222 nexus_attach(device_t dev)
227 * First, deal with the children we know about already
229 bus_generic_attach(dev);
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".
234 if (!devclass_get_device(devclass_find("eisa"), 0)) {
235 child = BUS_ADD_CHILD(dev, 0, "eisa", 0);
237 panic("nexus_attach eisa");
238 device_probe_and_attach(child);
241 if (!devclass_get_device(devclass_find("mca"), 0)) {
242 child = BUS_ADD_CHILD(dev, 0, "mca", 0);
244 panic("nexus_probe mca");
245 device_probe_and_attach(child);
248 if (!devclass_get_device(devclass_find("isa"), 0)) {
249 child = BUS_ADD_CHILD(dev, 0, "isa", 0);
251 panic("nexus_attach isa");
252 device_probe_and_attach(child);
259 nexus_print_all_resources(device_t dev)
261 struct nexus_device *ndev = DEVTONX(dev);
262 struct resource_list *rl = &ndev->nx_resources;
266 retval += printf(" at");
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");
276 nexus_print_child(device_t bus, device_t child)
280 retval += bus_print_child_header(bus, child);
281 retval += nexus_print_all_resources(child);
282 retval += printf(" on motherboard\n");
288 nexus_add_child(device_t bus, int order, const char *name, int unit)
291 struct nexus_device *ndev;
293 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
296 resource_list_init(&ndev->nx_resources);
298 child = device_add_child_ordered(bus, order, name, unit);
300 /* should we free this in nexus_child_detached? */
301 device_set_ivars(child, ndev);
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.)
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)
315 struct nexus_device *ndev = DEVTONX(child);
317 struct resource_list_entry *rle;
319 int needactivate = flags & RF_ACTIVE;
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.
326 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
329 rle = resource_list_find(&ndev->nx_resources, type, *rid);
360 rv = rman_reserve_resource(rm, start, end, count, flags, child);
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);
369 rman_set_bushandle(rv, rv->r_start);
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);
383 if (bus_activate_resource(child, type, *rid, rv)) {
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);
390 rman_release_resource(rv);
399 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
403 * If this is a memory resource, map it into the kernel.
405 if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
408 if (r->r_end < 1024 * 1024) {
410 * The first 1Mb is mapped at KERNBASE.
412 vaddr = (caddr_t)(uintptr_t)(KERNBASE + r->r_start);
419 psize = r->r_end - r->r_start;
421 poffs = paddr - trunc_page(paddr);
422 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
424 rman_set_virtual(r, vaddr);
426 /* PC-98: the type of bus_space_handle_t is the structure. */
427 r->r_bushandle->bsh_base = (bus_addr_t) vaddr;
429 /* IBM-PC: the type of bus_space_handle_t is u_int */
430 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
433 return (rman_activate_resource(r));
437 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
441 * If this is a memory resource, unmap it.
443 if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) && (r->r_end >= 1024 * 1024)) {
446 psize = r->r_end - r->r_start;
447 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
450 return (rman_deactivate_resource(r));
454 nexus_release_resource(device_t bus, device_t child, int type, int rid,
457 if (r->r_flags & RF_ACTIVE) {
458 int error = bus_deactivate_resource(child, type, rid, r);
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);
468 return (rman_release_resource(r));
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.
478 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
479 int flags, void (*ihand)(void *), void *arg, void **cookiep)
485 /* somebody tried to setup an irq that failed to allocate! */
487 panic("nexus_setup_intr: NULL irq resource!");
490 if (irq->r_flags & RF_SHAREABLE)
495 driver = device_get_driver(child);
496 switch (flags & ~INTR_TYPE_FAST) {
513 panic("still using grody create_intr interface");
515 if (flags & INTR_TYPE_FAST)
516 icflags |= INTR_FAST;
519 * We depend here on rman_activate_resource() being idempotent.
521 error = rman_activate_resource(irq);
525 *cookiep = inthand_add(device_get_nameunit(child), irq->r_start,
526 ihand, arg, mask, icflags);
527 if (*cookiep == NULL)
528 error = EINVAL; /* XXX ??? */
534 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
536 return (inthand_remove(ih));
540 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
542 struct nexus_device *ndev = DEVTONX(child);
543 struct resource_list *rl = &ndev->nx_resources;
545 /* XXX this should return a success/failure indicator */
546 resource_list_add(rl, type, rid, start, start + count - 1, count);
551 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
553 struct nexus_device *ndev = DEVTONX(child);
554 struct resource_list *rl = &ndev->nx_resources;
555 struct resource_list_entry *rle;
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);
563 *startp = rle->start;
565 *countp = rle->count;
570 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
572 struct nexus_device *ndev = DEVTONX(child);
573 struct resource_list *rl = &ndev->nx_resources;
575 resource_list_delete(rl, type, rid);