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.
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/linker.h>
56 #include <sys/malloc.h>
57 #include <sys/module.h>
58 #include <machine/bus.h>
59 #include <machine/intr_machdep.h>
61 #include <sys/interrupt.h>
63 #include <machine/vmparam.h>
66 #include <machine/pmap.h>
68 #include <machine/metadata.h>
69 #include <machine/nexusvar.h>
70 #include <machine/resource.h>
71 #include <machine/pc/bios.h>
78 #include <isa/isavar.h>
80 #include <pc98/cbus/cbus.h>
82 #include <isa/isareg.h>
85 #include <sys/rtprio.h>
87 #define ELF_KERN_STR ("elf"__XSTRING(__ELF_WORD_SIZE)" kernel")
89 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
91 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
93 struct rman irq_rman, drq_rman, port_rman, mem_rman;
95 static int nexus_probe(device_t);
96 static int nexus_attach(device_t);
97 static int nexus_print_all_resources(device_t dev);
98 static int nexus_print_child(device_t, device_t);
99 static device_t nexus_add_child(device_t bus, u_int order, const char *name,
101 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
102 u_long, u_long, u_long, u_int);
103 static int nexus_adjust_resource(device_t, device_t, int, struct resource *,
106 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
108 static int nexus_config_intr(device_t, int, enum intr_trigger,
110 static int nexus_describe_intr(device_t dev, device_t child,
111 struct resource *irq, void *cookie,
113 static int nexus_activate_resource(device_t, device_t, int, int,
115 static int nexus_deactivate_resource(device_t, device_t, int, int,
117 static int nexus_release_resource(device_t, device_t, int, int,
119 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
120 driver_filter_t filter, void (*)(void *), void *,
122 static int nexus_teardown_intr(device_t, device_t, struct resource *,
124 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
125 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
126 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
127 static void nexus_delete_resource(device_t, device_t, int, int);
129 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
130 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
131 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
132 static int nexus_release_msix(device_t pcib, device_t dev, int irq);
133 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
136 static device_method_t nexus_methods[] = {
137 /* Device interface */
138 DEVMETHOD(device_probe, nexus_probe),
139 DEVMETHOD(device_attach, nexus_attach),
140 DEVMETHOD(device_detach, bus_generic_detach),
141 DEVMETHOD(device_shutdown, bus_generic_shutdown),
142 DEVMETHOD(device_suspend, bus_generic_suspend),
143 DEVMETHOD(device_resume, bus_generic_resume),
146 DEVMETHOD(bus_print_child, nexus_print_child),
147 DEVMETHOD(bus_add_child, nexus_add_child),
148 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
149 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource),
150 DEVMETHOD(bus_release_resource, nexus_release_resource),
151 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
152 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
153 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
154 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
156 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
158 DEVMETHOD(bus_config_intr, nexus_config_intr),
159 DEVMETHOD(bus_describe_intr, nexus_describe_intr),
160 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
161 DEVMETHOD(bus_set_resource, nexus_set_resource),
162 DEVMETHOD(bus_get_resource, nexus_get_resource),
163 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
167 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi),
168 DEVMETHOD(pcib_release_msi, nexus_release_msi),
169 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix),
170 DEVMETHOD(pcib_release_msix, nexus_release_msix),
171 DEVMETHOD(pcib_map_msi, nexus_map_msi),
177 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
178 static devclass_t nexus_devclass;
180 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
183 nexus_probe(device_t dev)
186 device_quiet(dev); /* suppress attach message for neatness */
187 return (BUS_PROBE_GENERIC);
191 nexus_init_resources(void)
198 * - IRQ resource creation should be moved to the PIC/APIC driver.
199 * - DRQ resource creation should be moved to the DMAC driver.
200 * - The above should be sorted to probe earlier than any child busses.
202 * - Leave I/O and memory creation here, as child probes may need them.
203 * (especially eg. ACPI)
207 * IRQ's are on the mainboard on old systems, but on the ISA part
208 * of PCI->ISA bridges. There would be multiple sets of IRQs on
209 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
210 * component, so in a way, PCI can be a partial child of an ISA bus(!).
211 * APIC interrupts are global though.
213 irq_rman.rm_start = 0;
214 irq_rman.rm_type = RMAN_ARRAY;
215 irq_rman.rm_descr = "Interrupt request lines";
216 irq_rman.rm_end = NUM_IO_INTS - 1;
217 if (rman_init(&irq_rman))
218 panic("nexus_init_resources irq_rman");
221 * We search for regions of existing IRQs and add those to the IRQ
224 for (irq = 0; irq < NUM_IO_INTS; irq++)
225 if (intr_lookup_source(irq) != NULL)
226 if (rman_manage_region(&irq_rman, irq, irq) != 0)
227 panic("nexus_init_resources irq_rman add");
230 * ISA DMA on PCI systems is implemented in the ISA part of each
231 * PCI->ISA bridge and the channels can be duplicated if there are
232 * multiple bridges. (eg: laptops with docking stations)
234 drq_rman.rm_start = 0;
240 drq_rman.rm_type = RMAN_ARRAY;
241 drq_rman.rm_descr = "DMA request lines";
242 /* XXX drq 0 not available on some machines */
243 if (rman_init(&drq_rman)
244 || rman_manage_region(&drq_rman,
245 drq_rman.rm_start, drq_rman.rm_end))
246 panic("nexus_init_resources drq_rman");
249 * However, IO ports and Memory truely are global at this level,
250 * as are APIC interrupts (however many IO APICS there turn out
251 * to be on large systems..)
253 port_rman.rm_start = 0;
254 port_rman.rm_end = 0xffff;
255 port_rman.rm_type = RMAN_ARRAY;
256 port_rman.rm_descr = "I/O ports";
257 if (rman_init(&port_rman)
258 || rman_manage_region(&port_rman, 0, 0xffff))
259 panic("nexus_init_resources port_rman");
261 mem_rman.rm_start = 0;
262 mem_rman.rm_end = ~0ul;
263 mem_rman.rm_type = RMAN_ARRAY;
264 mem_rman.rm_descr = "I/O memory addresses";
265 if (rman_init(&mem_rman)
266 || rman_manage_region(&mem_rman, 0, ~0))
267 panic("nexus_init_resources mem_rman");
271 nexus_attach(device_t dev)
274 nexus_init_resources();
275 bus_generic_probe(dev);
278 * Explicitly add the legacy0 device here. Other platform
279 * types (such as ACPI), use their own nexus(4) subclass
280 * driver to override this routine and add their own root bus.
282 if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL)
283 panic("legacy: could not attach");
284 bus_generic_attach(dev);
289 nexus_print_all_resources(device_t dev)
291 struct nexus_device *ndev = DEVTONX(dev);
292 struct resource_list *rl = &ndev->nx_resources;
295 if (STAILQ_FIRST(rl))
296 retval += printf(" at");
298 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
299 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
300 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
306 nexus_print_child(device_t bus, device_t child)
310 retval += bus_print_child_header(bus, child);
311 retval += nexus_print_all_resources(child);
312 if (device_get_flags(child))
313 retval += printf(" flags %#x", device_get_flags(child));
314 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
320 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
323 struct nexus_device *ndev;
325 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
328 resource_list_init(&ndev->nx_resources);
330 child = device_add_child_ordered(bus, order, name, unit);
332 /* should we free this in nexus_child_detached? */
333 device_set_ivars(child, ndev);
356 * Allocate a resource on behalf of child. NB: child is usually going to be a
357 * child of one of our descendants, not a direct child of nexus0.
358 * (Exceptions include npx.)
360 static struct resource *
361 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
362 u_long start, u_long end, u_long count, u_int flags)
364 struct nexus_device *ndev = DEVTONX(child);
366 struct resource_list_entry *rle;
368 int needactivate = flags & RF_ACTIVE;
371 * If this is an allocation of the "default" range for a given
372 * RID, and we know what the resources for this device are
373 * (ie. they aren't maintained by a child bus), then work out
374 * the start/end values.
376 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
377 if (device_get_parent(child) != bus || ndev == NULL)
379 rle = resource_list_find(&ndev->nx_resources, type, *rid);
388 rm = nexus_rman(type);
392 rv = rman_reserve_resource(rm, start, end, count, flags, child);
395 rman_set_rid(rv, *rid);
398 if (bus_activate_resource(child, type, *rid, rv)) {
399 rman_release_resource(rv);
408 nexus_adjust_resource(device_t bus, device_t child, int type,
409 struct resource *r, u_long start, u_long end)
413 rm = nexus_rman(type);
416 if (!rman_is_region_manager(r, rm))
418 return (rman_adjust_resource(r, start, end));
422 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
426 bus_space_handle_t bh;
432 * If this is a memory resource, map it into the kernel.
437 error = i386_bus_space_handle_alloc(X86_BUS_SPACE_IO,
438 rman_get_start(r), rman_get_size(r), &bh);
441 rman_set_bushandle(r, bh);
443 rman_set_bushandle(r, rman_get_start(r));
445 rman_set_bustag(r, X86_BUS_SPACE_IO);
449 error = i386_bus_space_handle_alloc(X86_BUS_SPACE_MEM,
450 rman_get_start(r), rman_get_size(r), &bh);
454 vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r));
455 rman_set_virtual(r, vaddr);
456 rman_set_bustag(r, X86_BUS_SPACE_MEM);
458 /* PC-98: the type of bus_space_handle_t is the structure. */
459 bh->bsh_base = (bus_addr_t) vaddr;
460 rman_set_bushandle(r, bh);
462 /* IBM-PC: the type of bus_space_handle_t is u_int */
463 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
466 return (rman_activate_resource(r));
470 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
475 * If this is a memory resource, unmap it.
477 if (type == SYS_RES_MEMORY) {
478 pmap_unmapdev((vm_offset_t)rman_get_virtual(r),
482 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
483 bus_space_handle_t bh;
485 bh = rman_get_bushandle(r);
486 i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz);
489 return (rman_deactivate_resource(r));
493 nexus_release_resource(device_t bus, device_t child, int type, int rid,
497 if (rman_get_flags(r) & RF_ACTIVE) {
498 int error = bus_deactivate_resource(child, type, rid, r);
502 return (rman_release_resource(r));
506 * Currently this uses the really grody interface from kern/kern_intr.c
507 * (which really doesn't belong in kern/anything.c). Eventually, all of
508 * the code in kern_intr.c and machdep_intr.c should get moved here, since
509 * this is going to be the official interface.
512 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
513 int flags, driver_filter_t filter, void (*ihand)(void *),
514 void *arg, void **cookiep)
518 /* somebody tried to setup an irq that failed to allocate! */
520 panic("nexus_setup_intr: NULL irq resource!");
523 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
527 * We depend here on rman_activate_resource() being idempotent.
529 error = rman_activate_resource(irq);
533 error = intr_add_handler(device_get_nameunit(child),
534 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
540 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
542 return (intr_remove_handler(ih));
547 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
549 return (intr_bind(rman_get_start(irq), cpu));
554 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
555 enum intr_polarity pol)
557 return (intr_config_intr(irq, trig, pol));
561 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
562 void *cookie, const char *descr)
565 return (intr_describe(rman_get_start(irq), cookie, descr));
568 static struct resource_list *
569 nexus_get_reslist(device_t dev, device_t child)
571 struct nexus_device *ndev = DEVTONX(child);
573 return (&ndev->nx_resources);
577 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
579 struct nexus_device *ndev = DEVTONX(child);
580 struct resource_list *rl = &ndev->nx_resources;
582 /* XXX this should return a success/failure indicator */
583 resource_list_add(rl, type, rid, start, start + count - 1, count);
588 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
590 struct nexus_device *ndev = DEVTONX(child);
591 struct resource_list *rl = &ndev->nx_resources;
592 struct resource_list_entry *rle;
594 rle = resource_list_find(rl, type, rid);
598 *startp = rle->start;
600 *countp = rle->count;
605 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
607 struct nexus_device *ndev = DEVTONX(child);
608 struct resource_list *rl = &ndev->nx_resources;
610 resource_list_delete(rl, type, rid);
613 /* Called from the MSI code to add new IRQs to the IRQ rman. */
615 nexus_add_irq(u_long irq)
618 if (rman_manage_region(&irq_rman, irq, irq) != 0)
619 panic("%s: failed", __func__);
624 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
627 return (msix_alloc(dev, irq));
631 nexus_release_msix(device_t pcib, device_t dev, int irq)
634 return (msix_release(irq));
638 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
641 return (msi_alloc(dev, count, maxcount, irqs));
645 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
648 return (msi_release(irqs, count));
652 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
655 return (msi_map(irq, addr, data));
659 /* Placeholder for system RAM. */
661 ram_identify(driver_t *driver, device_t parent)
664 if (resource_disabled("ram", 0))
666 if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
667 panic("ram_identify");
671 ram_probe(device_t dev)
675 device_set_desc(dev, "System RAM");
680 ram_attach(device_t dev)
682 struct bios_smap *smapbase, *smap, *smapend;
683 struct resource *res;
689 /* Retrieve the system memory map from the loader. */
690 kmdp = preload_search_by_type("elf kernel");
692 kmdp = preload_search_by_type(ELF_KERN_STR);
694 smapbase = (struct bios_smap *)preload_search_info(kmdp,
695 MODINFO_METADATA | MODINFOMD_SMAP);
698 if (smapbase != NULL) {
699 smapsize = *((u_int32_t *)smapbase - 1);
700 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
703 for (smap = smapbase; smap < smapend; smap++) {
704 if (smap->type != SMAP_TYPE_MEMORY ||
709 * Resources use long's to track resources, so
710 * we can't include memory regions above 4GB.
712 if (smap->base > ~0ul)
715 error = bus_set_resource(dev, SYS_RES_MEMORY, rid,
716 smap->base, smap->length);
719 "ram_attach: resource %d failed set with %d",
721 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
724 panic("ram_attach: resource %d failed to attach",
732 * If the system map is not available, fall back to using
733 * dump_avail[]. We use the dump_avail[] array rather than
734 * phys_avail[] for the memory map as phys_avail[] contains
735 * holes for kernel memory, page 0, the message buffer, and
736 * the dcons buffer. We test the end address in the loop
737 * instead of the start since the start address for the first
740 for (rid = 0, p = dump_avail; p[1] != 0; rid++, p += 2) {
743 * Resources use long's to track resources, so we can't
744 * include memory regions above 4GB.
749 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, p[0],
752 panic("ram_attach: resource %d failed set with %d", rid,
754 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
756 panic("ram_attach: resource %d failed to attach", rid);
761 static device_method_t ram_methods[] = {
762 /* Device interface */
763 DEVMETHOD(device_identify, ram_identify),
764 DEVMETHOD(device_probe, ram_probe),
765 DEVMETHOD(device_attach, ram_attach),
769 static driver_t ram_driver = {
775 static devclass_t ram_devclass;
777 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0);
781 * Placeholder which claims PnP 'devices' which describe system
784 static struct isa_pnp_id sysresource_ids[] = {
785 { 0x010cd041 /* PNP0c01 */, "System Memory" },
786 { 0x020cd041 /* PNP0c02 */, "System Resource" },
791 sysresource_probe(device_t dev)
795 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
802 sysresource_attach(device_t dev)
807 static device_method_t sysresource_methods[] = {
808 /* Device interface */
809 DEVMETHOD(device_probe, sysresource_probe),
810 DEVMETHOD(device_attach, sysresource_attach),
811 DEVMETHOD(device_detach, bus_generic_detach),
812 DEVMETHOD(device_shutdown, bus_generic_shutdown),
813 DEVMETHOD(device_suspend, bus_generic_suspend),
814 DEVMETHOD(device_resume, bus_generic_resume),
818 static driver_t sysresource_driver = {
824 static devclass_t sysresource_devclass;
826 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);