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/nexusvar.h>
65 #include <machine/resource.h>
66 #include <machine/pc/bios.h>
71 #include <isa/isavar.h>
72 #include <amd64/isa/isa.h>
74 #include <sys/rtprio.h>
76 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
78 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
80 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);
91 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
93 static int nexus_config_intr(device_t, int, enum intr_trigger,
95 static int nexus_describe_intr(device_t dev, device_t child,
96 struct resource *irq, void *cookie,
98 static int nexus_activate_resource(device_t, device_t, int, int,
100 static int nexus_deactivate_resource(device_t, device_t, int, int,
102 static int nexus_release_resource(device_t, device_t, int, int,
104 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
105 driver_filter_t filter, void (*)(void *), void *,
107 static int nexus_teardown_intr(device_t, device_t, struct resource *,
109 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
110 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
111 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
112 static void nexus_delete_resource(device_t, device_t, int, int);
113 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
114 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
115 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
116 static int nexus_release_msix(device_t pcib, device_t dev, int irq);
117 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
119 static device_method_t nexus_methods[] = {
120 /* Device interface */
121 DEVMETHOD(device_probe, nexus_probe),
122 DEVMETHOD(device_attach, nexus_attach),
123 DEVMETHOD(device_detach, bus_generic_detach),
124 DEVMETHOD(device_shutdown, bus_generic_shutdown),
125 DEVMETHOD(device_suspend, bus_generic_suspend),
126 DEVMETHOD(device_resume, bus_generic_resume),
129 DEVMETHOD(bus_print_child, nexus_print_child),
130 DEVMETHOD(bus_add_child, nexus_add_child),
131 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
132 DEVMETHOD(bus_release_resource, nexus_release_resource),
133 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
134 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
135 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
136 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
138 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
140 DEVMETHOD(bus_config_intr, nexus_config_intr),
141 DEVMETHOD(bus_describe_intr, nexus_describe_intr),
142 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
143 DEVMETHOD(bus_set_resource, nexus_set_resource),
144 DEVMETHOD(bus_get_resource, nexus_get_resource),
145 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
148 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi),
149 DEVMETHOD(pcib_release_msi, nexus_release_msi),
150 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix),
151 DEVMETHOD(pcib_release_msix, nexus_release_msix),
152 DEVMETHOD(pcib_map_msi, nexus_map_msi),
157 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
158 static devclass_t nexus_devclass;
160 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
163 nexus_probe(device_t dev)
166 device_quiet(dev); /* suppress attach message for neatness */
167 return (BUS_PROBE_GENERIC);
171 nexus_init_resources(void)
178 * - IRQ resource creation should be moved to the PIC/APIC driver.
179 * - DRQ resource creation should be moved to the DMAC driver.
180 * - The above should be sorted to probe earlier than any child busses.
182 * - Leave I/O and memory creation here, as child probes may need them.
183 * (especially eg. ACPI)
187 * IRQ's are on the mainboard on old systems, but on the ISA part
188 * of PCI->ISA bridges. There would be multiple sets of IRQs on
189 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
190 * component, so in a way, PCI can be a partial child of an ISA bus(!).
191 * APIC interrupts are global though.
193 irq_rman.rm_start = 0;
194 irq_rman.rm_type = RMAN_ARRAY;
195 irq_rman.rm_descr = "Interrupt request lines";
196 irq_rman.rm_end = NUM_IO_INTS - 1;
197 if (rman_init(&irq_rman))
198 panic("nexus_init_resources irq_rman");
201 * We search for regions of existing IRQs and add those to the IRQ
204 for (irq = 0; irq < NUM_IO_INTS; irq++)
205 if (intr_lookup_source(irq) != NULL)
206 if (rman_manage_region(&irq_rman, irq, irq) != 0)
207 panic("nexus_init_resources irq_rman add");
210 * ISA DMA on PCI systems is implemented in the ISA part of each
211 * PCI->ISA bridge and the channels can be duplicated if there are
212 * multiple bridges. (eg: laptops with docking stations)
214 drq_rman.rm_start = 0;
216 drq_rman.rm_type = RMAN_ARRAY;
217 drq_rman.rm_descr = "DMA request lines";
218 /* XXX drq 0 not available on some machines */
219 if (rman_init(&drq_rman)
220 || rman_manage_region(&drq_rman,
221 drq_rman.rm_start, drq_rman.rm_end))
222 panic("nexus_init_resources drq_rman");
225 * However, IO ports and Memory truely are global at this level,
226 * as are APIC interrupts (however many IO APICS there turn out
227 * to be on large systems..)
229 port_rman.rm_start = 0;
230 port_rman.rm_end = 0xffff;
231 port_rman.rm_type = RMAN_ARRAY;
232 port_rman.rm_descr = "I/O ports";
233 if (rman_init(&port_rman)
234 || rman_manage_region(&port_rman, 0, 0xffff))
235 panic("nexus_init_resources port_rman");
237 mem_rman.rm_start = 0;
238 mem_rman.rm_end = ~0u;
239 mem_rman.rm_type = RMAN_ARRAY;
240 mem_rman.rm_descr = "I/O memory addresses";
241 if (rman_init(&mem_rman)
242 || rman_manage_region(&mem_rman, 0, ~0))
243 panic("nexus_init_resources mem_rman");
247 nexus_attach(device_t dev)
250 nexus_init_resources();
251 bus_generic_probe(dev);
254 * Explicitly add the legacy0 device here. Other platform
255 * types (such as ACPI), use their own nexus(4) subclass
256 * driver to override this routine and add their own root bus.
258 if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL)
259 panic("legacy: could not attach");
260 bus_generic_attach(dev);
265 nexus_print_all_resources(device_t dev)
267 struct nexus_device *ndev = DEVTONX(dev);
268 struct resource_list *rl = &ndev->nx_resources;
271 if (STAILQ_FIRST(rl))
272 retval += printf(" at");
274 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
275 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
276 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
282 nexus_print_child(device_t bus, device_t child)
286 retval += bus_print_child_header(bus, child);
287 retval += nexus_print_all_resources(child);
288 if (device_get_flags(child))
289 retval += printf(" flags %#x", device_get_flags(child));
290 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
296 nexus_add_child(device_t bus, int order, const char *name, int unit)
299 struct nexus_device *ndev;
301 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
304 resource_list_init(&ndev->nx_resources);
306 child = device_add_child_ordered(bus, order, name, unit);
308 /* should we free this in nexus_child_detached? */
309 device_set_ivars(child, ndev);
315 * Allocate a resource on behalf of child. NB: child is usually going to be a
316 * child of one of our descendants, not a direct child of nexus0.
318 static struct resource *
319 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
320 u_long start, u_long end, u_long count, u_int flags)
322 struct nexus_device *ndev = DEVTONX(child);
324 struct resource_list_entry *rle;
326 int needactivate = flags & RF_ACTIVE;
329 * If this is an allocation of the "default" range for a given RID, and
330 * we know what the resources for this device are (ie. they aren't maintained
331 * by a child bus), then work out the start/end values.
333 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
336 rle = resource_list_find(&ndev->nx_resources, type, *rid);
367 rv = rman_reserve_resource(rm, start, end, count, flags, child);
370 rman_set_rid(rv, *rid);
373 if (bus_activate_resource(child, type, *rid, rv)) {
374 rman_release_resource(rv);
383 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
388 * If this is a memory resource, map it into the kernel.
390 if (type == SYS_RES_MEMORY) {
393 vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r));
394 rman_set_virtual(r, vaddr);
395 rman_set_bustag(r, AMD64_BUS_SPACE_MEM);
396 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
397 } else if (type == SYS_RES_IOPORT) {
398 rman_set_bustag(r, AMD64_BUS_SPACE_IO);
399 rman_set_bushandle(r, rman_get_start(r));
401 return (rman_activate_resource(r));
405 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
409 * If this is a memory resource, unmap it.
411 if (type == SYS_RES_MEMORY) {
412 pmap_unmapdev((vm_offset_t)rman_get_virtual(r),
416 return (rman_deactivate_resource(r));
420 nexus_release_resource(device_t bus, device_t child, int type, int rid,
423 if (rman_get_flags(r) & RF_ACTIVE) {
424 int error = bus_deactivate_resource(child, type, rid, r);
428 return (rman_release_resource(r));
432 * Currently this uses the really grody interface from kern/kern_intr.c
433 * (which really doesn't belong in kern/anything.c). Eventually, all of
434 * the code in kern_intr.c and machdep_intr.c should get moved here, since
435 * this is going to be the official interface.
438 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
439 int flags, driver_filter_t filter, void (*ihand)(void *),
440 void *arg, void **cookiep)
444 /* somebody tried to setup an irq that failed to allocate! */
446 panic("nexus_setup_intr: NULL irq resource!");
449 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
453 * We depend here on rman_activate_resource() being idempotent.
455 error = rman_activate_resource(irq);
459 error = intr_add_handler(device_get_nameunit(child),
460 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
466 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
468 return (intr_remove_handler(ih));
473 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
475 return (intr_bind(rman_get_start(irq), cpu));
480 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
481 enum intr_polarity pol)
483 return (intr_config_intr(irq, trig, pol));
487 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
488 void *cookie, const char *descr)
491 return (intr_describe(rman_get_start(irq), cookie, descr));
494 static struct resource_list *
495 nexus_get_reslist(device_t dev, device_t child)
497 struct nexus_device *ndev = DEVTONX(child);
499 return (&ndev->nx_resources);
503 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
505 struct nexus_device *ndev = DEVTONX(child);
506 struct resource_list *rl = &ndev->nx_resources;
508 /* XXX this should return a success/failure indicator */
509 resource_list_add(rl, type, rid, start, start + count - 1, count);
514 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
516 struct nexus_device *ndev = DEVTONX(child);
517 struct resource_list *rl = &ndev->nx_resources;
518 struct resource_list_entry *rle;
520 rle = resource_list_find(rl, type, rid);
524 *startp = rle->start;
526 *countp = rle->count;
531 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
533 struct nexus_device *ndev = DEVTONX(child);
534 struct resource_list *rl = &ndev->nx_resources;
536 resource_list_delete(rl, type, rid);
539 /* Called from the MSI code to add new IRQs to the IRQ rman. */
541 nexus_add_irq(u_long irq)
544 if (rman_manage_region(&irq_rman, irq, irq) != 0)
545 panic("%s: failed", __func__);
549 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
552 return (msix_alloc(dev, irq));
556 nexus_release_msix(device_t pcib, device_t dev, int irq)
559 return (msix_release(irq));
563 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
566 return (msi_alloc(dev, count, maxcount, irqs));
570 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
573 return (msi_release(irqs, count));
577 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
580 return (msi_map(irq, addr, data));
583 /* Placeholder for system RAM. */
585 ram_identify(driver_t *driver, device_t parent)
588 if (resource_disabled("ram", 0))
590 if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
591 panic("ram_identify");
595 ram_probe(device_t dev)
599 device_set_desc(dev, "System RAM");
604 ram_attach(device_t dev)
606 struct bios_smap *smapbase, *smap, *smapend;
607 struct resource *res;
612 /* Retrieve the system memory map from the loader. */
613 kmdp = preload_search_by_type("elf kernel");
615 kmdp = preload_search_by_type("elf64 kernel");
616 smapbase = (struct bios_smap *)preload_search_info(kmdp,
617 MODINFO_METADATA | MODINFOMD_SMAP);
618 smapsize = *((u_int32_t *)smapbase - 1);
619 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
622 for (smap = smapbase; smap < smapend; smap++) {
623 if (smap->type != SMAP_TYPE_MEMORY || smap->length == 0)
625 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, smap->base,
628 panic("ram_attach: resource %d failed set with %d", rid,
630 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
632 panic("ram_attach: resource %d failed to attach", rid);
638 static device_method_t ram_methods[] = {
639 /* Device interface */
640 DEVMETHOD(device_identify, ram_identify),
641 DEVMETHOD(device_probe, ram_probe),
642 DEVMETHOD(device_attach, ram_attach),
646 static driver_t ram_driver = {
652 static devclass_t ram_devclass;
654 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0);
658 * Placeholder which claims PnP 'devices' which describe system
661 static struct isa_pnp_id sysresource_ids[] = {
662 { 0x010cd041 /* PNP0c01 */, "System Memory" },
663 { 0x020cd041 /* PNP0c02 */, "System Resource" },
668 sysresource_probe(device_t dev)
672 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
679 sysresource_attach(device_t dev)
684 static device_method_t sysresource_methods[] = {
685 /* Device interface */
686 DEVMETHOD(device_probe, sysresource_probe),
687 DEVMETHOD(device_attach, sysresource_attach),
688 DEVMETHOD(device_detach, bus_generic_detach),
689 DEVMETHOD(device_shutdown, bus_generic_shutdown),
690 DEVMETHOD(device_suspend, bus_generic_suspend),
691 DEVMETHOD(device_resume, bus_generic_resume),
695 static driver_t sysresource_driver = {
701 static devclass_t sysresource_devclass;
703 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);