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
32 * This code implements a `root nexus' for Arm Architecture
33 * machines. The function of the root nexus is to serve as an
34 * attachment point for both processors and buses, and to manage
35 * resources which are common to all of them. In particular,
36 * this code implements the core resource managers for interrupt
37 * requests and I/O memory address space.
41 #include "opt_platform.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/interrupt.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/sysctl.h>
56 #include <machine/bus.h>
57 #include <machine/intr.h>
58 #include <machine/machdep.h>
59 #include <machine/pcb.h>
60 #include <machine/resource.h>
61 #include <machine/vmparam.h>
64 #include <dev/ofw/ofw_bus_subr.h>
65 #include <dev/ofw/ofw_bus.h>
66 #include <dev/ofw/openfirm.h>
67 #include "ofw_bus_if.h"
70 #include <contrib/dev/acpica/include/acpi.h>
71 #include <dev/acpica/acpivar.h>
72 #include "acpi_bus_if.h"
75 extern struct bus_space memmap_bus;
77 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
80 struct resource_list nx_resources;
84 SYSCTL_INT(_kern, OID_AUTO, force_nonposted, CTLFLAG_RDTUN, &force_np, 0,
85 "Force all devices to use non-posted device memory");
87 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
89 static struct rman mem_rman;
90 static struct rman irq_rman;
92 static int nexus_attach(device_t);
95 static device_probe_t nexus_fdt_probe;
96 static device_attach_t nexus_fdt_attach;
97 static bus_activate_resource_t nexus_fdt_activate_resource;
100 static device_probe_t nexus_acpi_probe;
101 static device_attach_t nexus_acpi_attach;
104 static bus_add_child_t nexus_add_child;
105 static bus_print_child_t nexus_print_child;
107 static bus_activate_resource_t nexus_activate_resource;
108 static bus_alloc_resource_t nexus_alloc_resource;
109 static bus_deactivate_resource_t nexus_deactivate_resource;
110 static bus_get_resource_list_t nexus_get_reslist;
111 static bus_get_rman_t nexus_get_rman;
112 static bus_map_resource_t nexus_map_resource;
113 static bus_unmap_resource_t nexus_unmap_resource;
116 static bus_bind_intr_t nexus_bind_intr;
118 static bus_config_intr_t nexus_config_intr;
119 static bus_describe_intr_t nexus_describe_intr;
120 static bus_setup_intr_t nexus_setup_intr;
121 static bus_teardown_intr_t nexus_teardown_intr;
123 static bus_get_bus_tag_t nexus_get_bus_tag;
126 static ofw_bus_map_intr_t nexus_ofw_map_intr;
129 static device_method_t nexus_methods[] = {
131 DEVMETHOD(bus_add_child, nexus_add_child),
132 DEVMETHOD(bus_print_child, nexus_print_child),
133 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
134 DEVMETHOD(bus_adjust_resource, bus_generic_rman_adjust_resource),
135 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
136 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
137 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
138 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
139 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
140 DEVMETHOD(bus_get_rman, nexus_get_rman),
141 DEVMETHOD(bus_map_resource, nexus_map_resource),
142 DEVMETHOD(bus_release_resource, bus_generic_rman_release_resource),
143 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
144 DEVMETHOD(bus_unmap_resource, nexus_unmap_resource),
146 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
148 DEVMETHOD(bus_config_intr, nexus_config_intr),
149 DEVMETHOD(bus_describe_intr, nexus_describe_intr),
150 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
151 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
152 DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag),
157 static driver_t nexus_driver = {
164 nexus_attach(device_t dev)
167 mem_rman.rm_start = 0;
168 mem_rman.rm_end = BUS_SPACE_MAXADDR;
169 mem_rman.rm_type = RMAN_ARRAY;
170 mem_rman.rm_descr = "I/O memory addresses";
171 if (rman_init(&mem_rman) ||
172 rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
173 panic("nexus_attach mem_rman");
174 irq_rman.rm_start = 0;
175 irq_rman.rm_end = ~0;
176 irq_rman.rm_type = RMAN_ARRAY;
177 irq_rman.rm_descr = "Interrupts";
178 if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
179 panic("nexus_attach irq_rman");
181 bus_generic_probe(dev);
182 bus_generic_attach(dev);
188 nexus_print_child(device_t bus, device_t child)
192 retval += bus_print_child_header(bus, child);
193 retval += printf("\n");
199 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
202 struct nexus_device *ndev;
204 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
207 resource_list_init(&ndev->nx_resources);
209 child = device_add_child_ordered(bus, order, name, unit);
211 /* should we free this in nexus_child_detached? */
212 device_set_ivars(child, ndev);
218 nexus_get_rman(device_t bus, int type, u_int flags)
233 * Allocate a resource on behalf of child. NB: child is usually going to be a
234 * child of one of our descendants, not a direct child of nexus0.
236 static struct resource *
237 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
238 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
240 struct nexus_device *ndev = DEVTONX(child);
241 struct resource_list_entry *rle;
244 * If this is an allocation of the "default" range for a given
245 * RID, and we know what the resources for this device are
246 * (ie. they aren't maintained by a child bus), then work out
247 * the start/end values.
249 if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) {
250 if (device_get_parent(child) != bus || ndev == NULL)
252 rle = resource_list_find(&ndev->nx_resources, type, *rid);
260 return (bus_generic_rman_alloc_resource(bus, child, type, rid, start,
265 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
266 enum intr_polarity pol)
270 * On arm64 (due to INTRNG), ACPI interrupt configuration is
271 * done in nexus_acpi_map_intr().
277 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
278 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
282 if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
285 /* We depend here on rman_activate_resource() being idempotent. */
286 error = rman_activate_resource(res);
290 error = intr_setup_irq(child, res, filt, intr, arg, flags, cookiep);
296 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
299 return (intr_teardown_irq(child, r, ih));
303 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
304 void *cookie, const char *descr)
307 return (intr_describe_irq(child, irq, cookie, descr));
312 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
315 return (intr_bind_irq(child, irq, cpu));
319 static bus_space_tag_t
320 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
323 return (&memmap_bus);
327 nexus_activate_resource_flags(device_t bus, device_t child, int type, int rid,
328 struct resource *r, int flags)
330 struct resource_map_request args;
331 struct resource_map map;
334 if ((err = rman_activate_resource(r)) != 0)
338 * If this is a memory resource, map it into the kernel.
343 if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
344 resource_init_map_request(&args);
345 use_np = (flags & BUS_SPACE_MAP_NONPOSTED) != 0 ||
348 resource_int_value(device_get_name(child),
349 device_get_unit(child), "force_nonposted",
352 args.memattr = VM_MEMATTR_DEVICE_NP;
353 err = nexus_map_resource(bus, child, type, r, &args,
356 rman_deactivate_resource(r);
360 rman_set_mapping(r, &map);
364 err = intr_activate_irq(child, r);
366 rman_deactivate_resource(r);
374 nexus_activate_resource(device_t dev, device_t child, int type, int rid,
377 return (nexus_activate_resource_flags(dev, child, type, rid, r, 0));
380 static struct resource_list *
381 nexus_get_reslist(device_t dev, device_t child)
383 struct nexus_device *ndev = DEVTONX(child);
385 return (&ndev->nx_resources);
389 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
397 return (bus_generic_rman_deactivate_resource(bus, child, type,
400 error = rman_deactivate_resource(r);
403 intr_deactivate_irq(child, r);
411 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
412 struct resource_map_request *argsp, struct resource_map *map)
414 struct resource_map_request args;
415 rman_res_t length, start;
418 /* Resources must be active to be mapped. */
419 if ((rman_get_flags(r) & RF_ACTIVE) == 0)
422 /* Mappings are only supported on I/O and memory resources. */
431 resource_init_map_request(&args);
432 error = resource_validate_map_request(r, argsp, &args, &start, &length);
436 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
437 map->r_bustag = &memmap_bus;
438 map->r_size = length;
441 * The handle is the virtual address.
443 map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
448 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
449 struct resource_map *map)
455 pmap_unmapdev(map->r_vaddr, map->r_size);
463 static device_method_t nexus_fdt_methods[] = {
464 /* Device interface */
465 DEVMETHOD(device_probe, nexus_fdt_probe),
466 DEVMETHOD(device_attach, nexus_fdt_attach),
469 DEVMETHOD(bus_activate_resource, nexus_fdt_activate_resource),
472 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
477 #define nexus_baseclasses nexus_fdt_baseclasses
478 DEFINE_CLASS_1(nexus, nexus_fdt_driver, nexus_fdt_methods, 1, nexus_driver);
479 #undef nexus_baseclasses
481 EARLY_DRIVER_MODULE(nexus_fdt, root, nexus_fdt_driver, 0, 0,
482 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
485 nexus_fdt_probe(device_t dev)
488 if (arm64_bus_method != ARM64_BUS_FDT)
492 return (BUS_PROBE_DEFAULT);
496 nexus_fdt_attach(device_t dev)
499 nexus_add_child(dev, 10, "ofwbus", 0);
500 return (nexus_attach(dev));
504 nexus_fdt_activate_resource(device_t bus, device_t child, int type, int rid,
507 phandle_t node, parent;
515 * If the fdt parent has the nonposted-mmio property we
516 * need to use non-posted IO to access the device. When
517 * we find this property set the BUS_SPACE_MAP_NONPOSTED
518 * flag to be passed to bus_space_map.
520 node = ofw_bus_get_node(child);
522 parent = OF_parent(node);
524 OF_hasprop(parent, "nonposted-mmio")) {
525 flags |= BUS_SPACE_MAP_NONPOSTED;
533 return (nexus_activate_resource_flags(bus, child, type, rid, r, flags));
537 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
541 struct intr_map_data_fdt *fdt_data;
544 len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
545 fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
546 INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
547 fdt_data->iparent = iparent;
548 fdt_data->ncells = icells;
549 memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
550 irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
556 static int nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol);
558 static device_method_t nexus_acpi_methods[] = {
559 /* Device interface */
560 DEVMETHOD(device_probe, nexus_acpi_probe),
561 DEVMETHOD(device_attach, nexus_acpi_attach),
564 DEVMETHOD(acpi_bus_map_intr, nexus_acpi_map_intr),
569 #define nexus_baseclasses nexus_acpi_baseclasses
570 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1,
572 #undef nexus_baseclasses
574 EARLY_DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, 0, 0,
575 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
578 nexus_acpi_probe(device_t dev)
581 if (arm64_bus_method != ARM64_BUS_ACPI || acpi_identify() != 0)
585 return (BUS_PROBE_LOW_PRIORITY);
589 nexus_acpi_attach(device_t dev)
592 nexus_add_child(dev, 10, "acpi", 0);
593 return (nexus_attach(dev));
597 nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol)
599 struct intr_map_data_acpi *acpi_data;
602 len = sizeof(*acpi_data);
603 acpi_data = (struct intr_map_data_acpi *)intr_alloc_map_data(
604 INTR_MAP_DATA_ACPI, len, M_WAITOK | M_ZERO);
605 acpi_data->irq = irq;
606 acpi_data->pol = pol;
607 acpi_data->trig = trig;
610 * TODO: This will only handle a single interrupt controller.
611 * ACPI will map multiple controllers into a single virtual IRQ
612 * space. Each controller has a System Vector Base to hold the
613 * first irq it handles in this space. As such the correct way
614 * to handle interrupts with ACPI is to search through the
615 * controllers for the largest base value that is no larger than
618 irq = intr_map_irq(NULL, ACPI_INTR_XREF,
619 (struct intr_map_data *)acpi_data);