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, struct resource *r,
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.
340 switch (rman_get_type(r)) {
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, r, &args, &map);
355 rman_deactivate_resource(r);
359 rman_set_mapping(r, &map);
363 err = intr_activate_irq(child, r);
365 rman_deactivate_resource(r);
373 nexus_activate_resource(device_t dev, device_t child, struct resource *r)
375 return (nexus_activate_resource_flags(dev, child, r, 0));
378 static struct resource_list *
379 nexus_get_reslist(device_t dev, device_t child)
381 struct nexus_device *ndev = DEVTONX(child);
383 return (&ndev->nx_resources);
387 nexus_deactivate_resource(device_t bus, device_t child, struct resource *r)
391 switch (rman_get_type(r)) {
394 return (bus_generic_rman_deactivate_resource(bus, child, r));
396 error = rman_deactivate_resource(r);
399 intr_deactivate_irq(child, r);
407 nexus_map_resource(device_t bus, device_t child, struct resource *r,
408 struct resource_map_request *argsp, struct resource_map *map)
410 struct resource_map_request args;
411 rman_res_t length, start;
414 /* Resources must be active to be mapped. */
415 if ((rman_get_flags(r) & RF_ACTIVE) == 0)
418 /* Mappings are only supported on I/O and memory resources. */
419 switch (rman_get_type(r)) {
427 resource_init_map_request(&args);
428 error = resource_validate_map_request(r, argsp, &args, &start, &length);
432 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
433 map->r_bustag = &memmap_bus;
434 map->r_size = length;
437 * The handle is the virtual address.
439 map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
444 nexus_unmap_resource(device_t bus, device_t child, struct resource *r,
445 struct resource_map *map)
448 switch (rman_get_type(r)) {
451 pmap_unmapdev(map->r_vaddr, map->r_size);
459 static device_method_t nexus_fdt_methods[] = {
460 /* Device interface */
461 DEVMETHOD(device_probe, nexus_fdt_probe),
462 DEVMETHOD(device_attach, nexus_fdt_attach),
465 DEVMETHOD(bus_activate_resource, nexus_fdt_activate_resource),
468 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
473 #define nexus_baseclasses nexus_fdt_baseclasses
474 DEFINE_CLASS_1(nexus, nexus_fdt_driver, nexus_fdt_methods, 1, nexus_driver);
475 #undef nexus_baseclasses
477 EARLY_DRIVER_MODULE(nexus_fdt, root, nexus_fdt_driver, 0, 0,
478 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
481 nexus_fdt_probe(device_t dev)
484 if (arm64_bus_method != ARM64_BUS_FDT)
488 return (BUS_PROBE_DEFAULT);
492 nexus_fdt_attach(device_t dev)
495 nexus_add_child(dev, 10, "ofwbus", 0);
496 return (nexus_attach(dev));
500 nexus_fdt_activate_resource(device_t bus, device_t child, struct resource *r)
502 phandle_t node, parent;
506 switch (rman_get_type(r)) {
510 * If the fdt parent has the nonposted-mmio property we
511 * need to use non-posted IO to access the device. When
512 * we find this property set the BUS_SPACE_MAP_NONPOSTED
513 * flag to be passed to bus_space_map.
515 node = ofw_bus_get_node(child);
517 parent = OF_parent(node);
519 OF_hasprop(parent, "nonposted-mmio")) {
520 flags |= BUS_SPACE_MAP_NONPOSTED;
528 return (nexus_activate_resource_flags(bus, child, r, flags));
532 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
536 struct intr_map_data_fdt *fdt_data;
539 len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
540 fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
541 INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
542 fdt_data->iparent = iparent;
543 fdt_data->ncells = icells;
544 memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
545 irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
551 static int nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol);
553 static device_method_t nexus_acpi_methods[] = {
554 /* Device interface */
555 DEVMETHOD(device_probe, nexus_acpi_probe),
556 DEVMETHOD(device_attach, nexus_acpi_attach),
559 DEVMETHOD(acpi_bus_map_intr, nexus_acpi_map_intr),
564 #define nexus_baseclasses nexus_acpi_baseclasses
565 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1,
567 #undef nexus_baseclasses
569 EARLY_DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, 0, 0,
570 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
573 nexus_acpi_probe(device_t dev)
576 if (arm64_bus_method != ARM64_BUS_ACPI || acpi_identify() != 0)
580 return (BUS_PROBE_LOW_PRIORITY);
584 nexus_acpi_attach(device_t dev)
587 nexus_add_child(dev, 10, "acpi", 0);
588 return (nexus_attach(dev));
592 nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol)
594 struct intr_map_data_acpi *acpi_data;
597 len = sizeof(*acpi_data);
598 acpi_data = (struct intr_map_data_acpi *)intr_alloc_map_data(
599 INTR_MAP_DATA_ACPI, len, M_WAITOK | M_ZERO);
600 acpi_data->irq = irq;
601 acpi_data->pol = pol;
602 acpi_data->trig = trig;
605 * TODO: This will only handle a single interrupt controller.
606 * ACPI will map multiple controllers into a single virtual IRQ
607 * space. Each controller has a System Vector Base to hold the
608 * first irq it handles in this space. As such the correct way
609 * to handle interrupts with ACPI is to search through the
610 * controllers for the largest base value that is no larger than
613 irq = intr_map_irq(NULL, ACPI_INTR_XREF,
614 (struct intr_map_data *)acpi_data);