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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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>
53 #include <machine/intr_machdep.h>
55 #include <sys/interrupt.h>
57 #include <machine/vmparam.h>
60 #include <machine/pmap.h>
62 #include <machine/resource.h>
65 #include <isa/isavar.h>
66 #include <amd64/isa/isa.h>
68 #include <sys/rtprio.h>
70 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
72 struct resource_list nx_resources;
75 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
77 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
79 static int nexus_probe(device_t);
80 static int nexus_attach(device_t);
81 static int nexus_print_all_resources(device_t dev);
82 static int nexus_print_child(device_t, device_t);
83 static device_t nexus_add_child(device_t bus, int order, const char *name,
85 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
86 u_long, u_long, u_long, u_int);
87 static int nexus_config_intr(device_t, int, enum intr_trigger,
89 static int nexus_activate_resource(device_t, device_t, int, int,
91 static int nexus_deactivate_resource(device_t, device_t, int, int,
93 static int nexus_release_resource(device_t, device_t, int, int,
95 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
96 void (*)(void *), void *, void **);
97 static int nexus_teardown_intr(device_t, device_t, struct resource *,
99 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
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_alloc_resource, nexus_alloc_resource),
117 DEVMETHOD(bus_release_resource, nexus_release_resource),
118 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
119 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
120 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
121 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
122 DEVMETHOD(bus_config_intr, nexus_config_intr),
123 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
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)
145 device_quiet(dev); /* suppress attach message for neatness */
150 * - IRQ resource creation should be moved to the PIC/APIC driver.
151 * - DRQ resource creation should be moved to the DMAC driver.
152 * - The above should be sorted to probe earlier than any child busses.
154 * - Leave I/O and memory creation here, as child probes may need them.
155 * (especially eg. ACPI)
159 * IRQ's are on the mainboard on old systems, but on the ISA part
160 * of PCI->ISA bridges. There would be multiple sets of IRQs on
161 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
162 * component, so in a way, PCI can be a partial child of an ISA bus(!).
163 * APIC interrupts are global though.
165 irq_rman.rm_start = 0;
166 irq_rman.rm_type = RMAN_ARRAY;
167 irq_rman.rm_descr = "Interrupt request lines";
168 irq_rman.rm_end = NUM_IO_INTS - 1;
169 if (rman_init(&irq_rman))
170 panic("nexus_probe irq_rman");
173 * We search for regions of existing IRQs and add those to the IRQ
177 for (irq = 0; irq < NUM_IO_INTS; irq++)
178 if (intr_lookup_source(irq) != NULL) {
181 } else if (last != -1) {
182 if (rman_manage_region(&irq_rman, last, irq - 1) != 0)
183 panic("nexus_probe irq_rman add");
186 if (last != -1 && rman_manage_region(&irq_rman, last, irq - 1) != 0)
187 panic("nexus_probe irq_rman add");
190 * ISA DMA on PCI systems is implemented in the ISA part of each
191 * PCI->ISA bridge and the channels can be duplicated if there are
192 * multiple bridges. (eg: laptops with docking stations)
194 drq_rman.rm_start = 0;
196 drq_rman.rm_type = RMAN_ARRAY;
197 drq_rman.rm_descr = "DMA request lines";
198 /* XXX drq 0 not available on some machines */
199 if (rman_init(&drq_rman)
200 || rman_manage_region(&drq_rman,
201 drq_rman.rm_start, drq_rman.rm_end))
202 panic("nexus_probe drq_rman");
205 * However, IO ports and Memory truely are global at this level,
206 * as are APIC interrupts (however many IO APICS there turn out
207 * to be on large systems..)
209 port_rman.rm_start = 0;
210 port_rman.rm_end = 0xffff;
211 port_rman.rm_type = RMAN_ARRAY;
212 port_rman.rm_descr = "I/O ports";
213 if (rman_init(&port_rman)
214 || rman_manage_region(&port_rman, 0, 0xffff))
215 panic("nexus_probe port_rman");
217 mem_rman.rm_start = 0;
218 mem_rman.rm_end = ~0u;
219 mem_rman.rm_type = RMAN_ARRAY;
220 mem_rman.rm_descr = "I/O memory addresses";
221 if (rman_init(&mem_rman)
222 || rman_manage_region(&mem_rman, 0, ~0))
223 panic("nexus_probe mem_rman");
229 nexus_attach(device_t dev)
232 bus_generic_probe(dev);
233 bus_generic_attach(dev);
238 nexus_print_all_resources(device_t dev)
240 struct nexus_device *ndev = DEVTONX(dev);
241 struct resource_list *rl = &ndev->nx_resources;
244 if (STAILQ_FIRST(rl))
245 retval += printf(" at");
247 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
248 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
249 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
255 nexus_print_child(device_t bus, device_t child)
259 retval += bus_print_child_header(bus, child);
260 retval += nexus_print_all_resources(child);
261 if (device_get_flags(child))
262 retval += printf(" flags %#x", device_get_flags(child));
263 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
269 nexus_add_child(device_t bus, int order, const char *name, int unit)
272 struct nexus_device *ndev;
274 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
277 resource_list_init(&ndev->nx_resources);
279 child = device_add_child_ordered(bus, order, name, unit);
281 /* should we free this in nexus_child_detached? */
282 device_set_ivars(child, ndev);
288 * Allocate a resource on behalf of child. NB: child is usually going to be a
289 * child of one of our descendants, not a direct child of nexus0.
291 static struct resource *
292 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
293 u_long start, u_long end, u_long count, u_int flags)
295 struct nexus_device *ndev = DEVTONX(child);
297 struct resource_list_entry *rle;
299 int needactivate = flags & RF_ACTIVE;
302 * If this is an allocation of the "default" range for a given RID, and
303 * we know what the resources for this device are (ie. they aren't maintained
304 * by a child bus), then work out the start/end values.
306 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
309 rle = resource_list_find(&ndev->nx_resources, type, *rid);
340 rv = rman_reserve_resource(rm, start, end, count, flags, child);
344 if (type == SYS_RES_MEMORY) {
345 rman_set_bustag(rv, AMD64_BUS_SPACE_MEM);
346 } else if (type == SYS_RES_IOPORT) {
347 rman_set_bustag(rv, AMD64_BUS_SPACE_IO);
348 rman_set_bushandle(rv, rman_get_start(rv));
352 if (bus_activate_resource(child, type, *rid, rv)) {
353 rman_release_resource(rv);
362 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
367 * If this is a memory resource, map it into the kernel.
369 if (rman_get_bustag(r) == AMD64_BUS_SPACE_MEM) {
372 if (rman_get_end(r) < 1024 * 1024) {
374 * The first 1Mb is mapped at KERNBASE.
376 vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
382 paddr = rman_get_start(r);
383 psize = rman_get_size(r);
385 poffs = paddr - trunc_page(paddr);
386 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
388 rman_set_virtual(r, vaddr);
389 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
391 return (rman_activate_resource(r));
395 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
399 * If this is a memory resource, unmap it.
401 if ((rman_get_bustag(r) == AMD64_BUS_SPACE_MEM) &&
402 (rman_get_end(r) >= 1024 * 1024)) {
405 psize = rman_get_size(r);
406 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
409 return (rman_deactivate_resource(r));
413 nexus_release_resource(device_t bus, device_t child, int type, int rid,
416 if (rman_get_flags(r) & RF_ACTIVE) {
417 int error = bus_deactivate_resource(child, type, rid, r);
421 return (rman_release_resource(r));
425 * Currently this uses the really grody interface from kern/kern_intr.c
426 * (which really doesn't belong in kern/anything.c). Eventually, all of
427 * the code in kern_intr.c and machdep_intr.c should get moved here, since
428 * this is going to be the official interface.
431 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
432 int flags, void (*ihand)(void *), void *arg, void **cookiep)
436 /* somebody tried to setup an irq that failed to allocate! */
438 panic("nexus_setup_intr: NULL irq resource!");
441 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
445 * We depend here on rman_activate_resource() being idempotent.
447 error = rman_activate_resource(irq);
451 error = intr_add_handler(device_get_nameunit(child),
452 rman_get_start(irq), ihand, arg, flags, cookiep);
458 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
460 return (intr_remove_handler(ih));
464 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
465 enum intr_polarity pol)
467 return (intr_config_intr(irq, trig, pol));
470 static struct resource_list *
471 nexus_get_reslist(device_t dev, device_t child)
473 struct nexus_device *ndev = DEVTONX(child);
475 return (&ndev->nx_resources);
479 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
481 struct nexus_device *ndev = DEVTONX(child);
482 struct resource_list *rl = &ndev->nx_resources;
484 /* XXX this should return a success/failure indicator */
485 resource_list_add(rl, type, rid, start, start + count - 1, count);
490 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
492 struct nexus_device *ndev = DEVTONX(child);
493 struct resource_list *rl = &ndev->nx_resources;
494 struct resource_list_entry *rle;
496 rle = resource_list_find(rl, type, rid);
500 *startp = rle->start;
502 *countp = rle->count;
507 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
509 struct nexus_device *ndev = DEVTONX(child);
510 struct resource_list *rl = &ndev->nx_resources;
512 resource_list_delete(rl, type, rid);
517 * Placeholder which claims PnP 'devices' which describe system
520 static struct isa_pnp_id sysresource_ids[] = {
521 { 0x010cd041 /* PNP0c01 */, "System Memory" },
522 { 0x020cd041 /* PNP0c02 */, "System Resource" },
527 sysresource_probe(device_t dev)
531 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
538 sysresource_attach(device_t dev)
543 static device_method_t sysresource_methods[] = {
544 /* Device interface */
545 DEVMETHOD(device_probe, sysresource_probe),
546 DEVMETHOD(device_attach, sysresource_attach),
547 DEVMETHOD(device_detach, bus_generic_detach),
548 DEVMETHOD(device_shutdown, bus_generic_shutdown),
549 DEVMETHOD(device_suspend, bus_generic_suspend),
550 DEVMETHOD(device_resume, bus_generic_resume),
554 static driver_t sysresource_driver = {
560 static devclass_t sysresource_devclass;
562 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);