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/linker.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <machine/intr_machdep.h>
56 #include <sys/interrupt.h>
58 #include <machine/vmparam.h>
61 #include <machine/pmap.h>
63 #include <machine/metadata.h>
64 #include <machine/resource.h>
65 #include <machine/pc/bios.h>
70 #include <isa/isavar.h>
71 #include <amd64/isa/isa.h>
73 #include <sys/rtprio.h>
75 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
77 struct resource_list nx_resources;
80 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
82 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
84 static int nexus_probe(device_t);
85 static int nexus_attach(device_t);
86 static int nexus_print_all_resources(device_t dev);
87 static int nexus_print_child(device_t, device_t);
88 static device_t nexus_add_child(device_t bus, int order, const char *name,
90 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
91 u_long, u_long, u_long, u_int);
92 static int nexus_config_intr(device_t, int, enum intr_trigger,
94 static int nexus_activate_resource(device_t, device_t, int, int,
96 static int nexus_deactivate_resource(device_t, device_t, int, int,
98 static int nexus_release_resource(device_t, device_t, int, int,
100 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
101 driver_filter_t filter, void (*)(void *), void *,
103 static int nexus_teardown_intr(device_t, device_t, struct resource *,
105 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
106 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
107 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
108 static void nexus_delete_resource(device_t, device_t, int, int);
109 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
110 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
111 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
112 static int nexus_release_msix(device_t pcib, device_t dev, int irq);
113 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
115 static device_method_t nexus_methods[] = {
116 /* Device interface */
117 DEVMETHOD(device_probe, nexus_probe),
118 DEVMETHOD(device_attach, nexus_attach),
119 DEVMETHOD(device_detach, bus_generic_detach),
120 DEVMETHOD(device_shutdown, bus_generic_shutdown),
121 DEVMETHOD(device_suspend, bus_generic_suspend),
122 DEVMETHOD(device_resume, bus_generic_resume),
125 DEVMETHOD(bus_print_child, nexus_print_child),
126 DEVMETHOD(bus_add_child, nexus_add_child),
127 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
128 DEVMETHOD(bus_release_resource, nexus_release_resource),
129 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
130 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
131 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
132 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
133 DEVMETHOD(bus_config_intr, nexus_config_intr),
134 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
135 DEVMETHOD(bus_set_resource, nexus_set_resource),
136 DEVMETHOD(bus_get_resource, nexus_get_resource),
137 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
140 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi),
141 DEVMETHOD(pcib_release_msi, nexus_release_msi),
142 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix),
143 DEVMETHOD(pcib_release_msix, nexus_release_msix),
144 DEVMETHOD(pcib_map_msi, nexus_map_msi),
149 static driver_t nexus_driver = {
154 static devclass_t nexus_devclass;
156 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
159 nexus_probe(device_t dev)
163 device_quiet(dev); /* suppress attach message for neatness */
168 * - IRQ resource creation should be moved to the PIC/APIC driver.
169 * - DRQ resource creation should be moved to the DMAC driver.
170 * - The above should be sorted to probe earlier than any child busses.
172 * - Leave I/O and memory creation here, as child probes may need them.
173 * (especially eg. ACPI)
177 * IRQ's are on the mainboard on old systems, but on the ISA part
178 * of PCI->ISA bridges. There would be multiple sets of IRQs on
179 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
180 * component, so in a way, PCI can be a partial child of an ISA bus(!).
181 * APIC interrupts are global though.
183 irq_rman.rm_start = 0;
184 irq_rman.rm_type = RMAN_ARRAY;
185 irq_rman.rm_descr = "Interrupt request lines";
186 irq_rman.rm_end = NUM_IO_INTS - 1;
187 if (rman_init(&irq_rman))
188 panic("nexus_probe irq_rman");
191 * We search for regions of existing IRQs and add those to the IRQ
194 for (irq = 0; irq < NUM_IO_INTS; irq++)
195 if (intr_lookup_source(irq) != NULL)
196 if (rman_manage_region(&irq_rman, irq, irq) != 0)
197 panic("nexus_probe irq_rman add");
200 * ISA DMA on PCI systems is implemented in the ISA part of each
201 * PCI->ISA bridge and the channels can be duplicated if there are
202 * multiple bridges. (eg: laptops with docking stations)
204 drq_rman.rm_start = 0;
206 drq_rman.rm_type = RMAN_ARRAY;
207 drq_rman.rm_descr = "DMA request lines";
208 /* XXX drq 0 not available on some machines */
209 if (rman_init(&drq_rman)
210 || rman_manage_region(&drq_rman,
211 drq_rman.rm_start, drq_rman.rm_end))
212 panic("nexus_probe drq_rman");
215 * However, IO ports and Memory truely are global at this level,
216 * as are APIC interrupts (however many IO APICS there turn out
217 * to be on large systems..)
219 port_rman.rm_start = 0;
220 port_rman.rm_end = 0xffff;
221 port_rman.rm_type = RMAN_ARRAY;
222 port_rman.rm_descr = "I/O ports";
223 if (rman_init(&port_rman)
224 || rman_manage_region(&port_rman, 0, 0xffff))
225 panic("nexus_probe port_rman");
227 mem_rman.rm_start = 0;
228 mem_rman.rm_end = ~0u;
229 mem_rman.rm_type = RMAN_ARRAY;
230 mem_rman.rm_descr = "I/O memory addresses";
231 if (rman_init(&mem_rman)
232 || rman_manage_region(&mem_rman, 0, ~0))
233 panic("nexus_probe mem_rman");
239 nexus_attach(device_t dev)
242 bus_generic_probe(dev);
243 bus_generic_attach(dev);
248 nexus_print_all_resources(device_t dev)
250 struct nexus_device *ndev = DEVTONX(dev);
251 struct resource_list *rl = &ndev->nx_resources;
254 if (STAILQ_FIRST(rl))
255 retval += printf(" at");
257 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
258 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
259 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
265 nexus_print_child(device_t bus, device_t child)
269 retval += bus_print_child_header(bus, child);
270 retval += nexus_print_all_resources(child);
271 if (device_get_flags(child))
272 retval += printf(" flags %#x", device_get_flags(child));
273 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
279 nexus_add_child(device_t bus, int order, const char *name, int unit)
282 struct nexus_device *ndev;
284 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
287 resource_list_init(&ndev->nx_resources);
289 child = device_add_child_ordered(bus, order, name, unit);
291 /* should we free this in nexus_child_detached? */
292 device_set_ivars(child, ndev);
298 * Allocate a resource on behalf of child. NB: child is usually going to be a
299 * child of one of our descendants, not a direct child of nexus0.
301 static struct resource *
302 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
303 u_long start, u_long end, u_long count, u_int flags)
305 struct nexus_device *ndev = DEVTONX(child);
307 struct resource_list_entry *rle;
309 int needactivate = flags & RF_ACTIVE;
312 * If this is an allocation of the "default" range for a given RID, and
313 * we know what the resources for this device are (ie. they aren't maintained
314 * by a child bus), then work out the start/end values.
316 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
319 rle = resource_list_find(&ndev->nx_resources, type, *rid);
350 rv = rman_reserve_resource(rm, start, end, count, flags, child);
353 rman_set_rid(rv, *rid);
356 if (bus_activate_resource(child, type, *rid, rv)) {
357 rman_release_resource(rv);
366 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
371 * If this is a memory resource, map it into the kernel.
373 if (type == SYS_RES_MEMORY) {
376 vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r));
377 rman_set_virtual(r, vaddr);
378 rman_set_bustag(r, AMD64_BUS_SPACE_MEM);
379 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
380 } else if (type == SYS_RES_IOPORT) {
381 rman_set_bustag(r, AMD64_BUS_SPACE_IO);
382 rman_set_bushandle(r, rman_get_start(r));
384 return (rman_activate_resource(r));
388 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
392 * If this is a memory resource, unmap it.
394 if (type == SYS_RES_MEMORY) {
395 pmap_unmapdev((vm_offset_t)rman_get_virtual(r),
399 return (rman_deactivate_resource(r));
403 nexus_release_resource(device_t bus, device_t child, int type, int rid,
406 if (rman_get_flags(r) & RF_ACTIVE) {
407 int error = bus_deactivate_resource(child, type, rid, r);
411 return (rman_release_resource(r));
415 * Currently this uses the really grody interface from kern/kern_intr.c
416 * (which really doesn't belong in kern/anything.c). Eventually, all of
417 * the code in kern_intr.c and machdep_intr.c should get moved here, since
418 * this is going to be the official interface.
421 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
422 int flags, driver_filter_t filter, void (*ihand)(void *),
423 void *arg, void **cookiep)
427 /* somebody tried to setup an irq that failed to allocate! */
429 panic("nexus_setup_intr: NULL irq resource!");
432 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
436 * We depend here on rman_activate_resource() being idempotent.
438 error = rman_activate_resource(irq);
442 error = intr_add_handler(device_get_nameunit(child),
443 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
449 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
451 return (intr_remove_handler(ih));
455 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
456 enum intr_polarity pol)
458 return (intr_config_intr(irq, trig, pol));
461 static struct resource_list *
462 nexus_get_reslist(device_t dev, device_t child)
464 struct nexus_device *ndev = DEVTONX(child);
466 return (&ndev->nx_resources);
470 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
472 struct nexus_device *ndev = DEVTONX(child);
473 struct resource_list *rl = &ndev->nx_resources;
475 /* XXX this should return a success/failure indicator */
476 resource_list_add(rl, type, rid, start, start + count - 1, count);
481 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
483 struct nexus_device *ndev = DEVTONX(child);
484 struct resource_list *rl = &ndev->nx_resources;
485 struct resource_list_entry *rle;
487 rle = resource_list_find(rl, type, rid);
491 *startp = rle->start;
493 *countp = rle->count;
498 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
500 struct nexus_device *ndev = DEVTONX(child);
501 struct resource_list *rl = &ndev->nx_resources;
503 resource_list_delete(rl, type, rid);
506 /* Called from the MSI code to add new IRQs to the IRQ rman. */
508 nexus_add_irq(u_long irq)
511 if (rman_manage_region(&irq_rman, irq, irq) != 0)
512 panic("%s: failed", __func__);
516 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
519 return (msix_alloc(dev, irq));
523 nexus_release_msix(device_t pcib, device_t dev, int irq)
526 return (msix_release(irq));
530 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
533 return (msi_alloc(dev, count, maxcount, irqs));
537 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
540 return (msi_release(irqs, count));
544 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
547 return (msi_map(irq, addr, data));
550 /* Placeholder for system RAM. */
552 ram_identify(driver_t *driver, device_t parent)
555 if (resource_disabled("ram", 0))
557 if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
558 panic("ram_identify");
562 ram_probe(device_t dev)
566 device_set_desc(dev, "System RAM");
571 ram_attach(device_t dev)
573 struct bios_smap *smapbase, *smap, *smapend;
574 struct resource *res;
579 /* Retrieve the system memory map from the loader. */
580 kmdp = preload_search_by_type("elf kernel");
582 kmdp = preload_search_by_type("elf64 kernel");
583 smapbase = (struct bios_smap *)preload_search_info(kmdp,
584 MODINFO_METADATA | MODINFOMD_SMAP);
585 smapsize = *((u_int32_t *)smapbase - 1);
586 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
589 for (smap = smapbase; smap < smapend; smap++) {
590 if (smap->type != 0x01 || smap->length == 0)
592 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, smap->base,
595 panic("ram_attach: resource %d failed set with %d", rid,
597 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
599 panic("ram_attach: resource %d failed to attach", rid);
605 static device_method_t ram_methods[] = {
606 /* Device interface */
607 DEVMETHOD(device_identify, ram_identify),
608 DEVMETHOD(device_probe, ram_probe),
609 DEVMETHOD(device_attach, ram_attach),
613 static driver_t ram_driver = {
619 static devclass_t ram_devclass;
621 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0);
625 * Placeholder which claims PnP 'devices' which describe system
628 static struct isa_pnp_id sysresource_ids[] = {
629 { 0x010cd041 /* PNP0c01 */, "System Memory" },
630 { 0x020cd041 /* PNP0c02 */, "System Resource" },
635 sysresource_probe(device_t dev)
639 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
646 sysresource_attach(device_t dev)
651 static device_method_t sysresource_methods[] = {
652 /* Device interface */
653 DEVMETHOD(device_probe, sysresource_probe),
654 DEVMETHOD(device_attach, sysresource_attach),
655 DEVMETHOD(device_detach, bus_generic_detach),
656 DEVMETHOD(device_shutdown, bus_generic_shutdown),
657 DEVMETHOD(device_suspend, bus_generic_suspend),
658 DEVMETHOD(device_resume, bus_generic_resume),
662 static driver_t sysresource_driver = {
668 static devclass_t sysresource_devclass;
670 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);