xref: /freebsd/sys/arm64/arm64/nexus.c (revision 4713f54013176fc73ada29cf094016fd3b328c80)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
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
14  * warranty.
15  *
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
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /*
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.
38  */
39 
40 #include "opt_acpi.h"
41 #include "opt_platform.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/interrupt.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/rman.h>
51 #include <sys/sysctl.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 
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>
62 
63 #ifdef FDT
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"
68 #endif
69 #ifdef DEV_ACPI
70 #include <contrib/dev/acpica/include/acpi.h>
71 #include <dev/acpica/acpivar.h>
72 #include "acpi_bus_if.h"
73 #endif
74 
75 #include "pcib_if.h"
76 
77 extern struct bus_space memmap_bus;
78 
79 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
80 
81 struct nexus_device {
82 	struct resource_list	nx_resources;
83 };
84 
85 static int force_np;
86 SYSCTL_INT(_kern, OID_AUTO, force_nonposted, CTLFLAG_RDTUN, &force_np, 0,
87     "Force all devices to use non-posted device memory");
88 
89 #define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
90 
91 static struct rman mem_rman;
92 static struct rman irq_rman;
93 
94 static	int nexus_attach(device_t);
95 
96 #ifdef FDT
97 static device_probe_t	nexus_fdt_probe;
98 static device_attach_t	nexus_fdt_attach;
99 static bus_activate_resource_t nexus_fdt_activate_resource;
100 #endif
101 #ifdef DEV_ACPI
102 static device_probe_t		nexus_acpi_probe;
103 static device_attach_t		nexus_acpi_attach;
104 #endif
105 
106 static bus_add_child_t		nexus_add_child;
107 static bus_print_child_t	nexus_print_child;
108 
109 static bus_activate_resource_t	nexus_activate_resource;
110 static bus_alloc_resource_t	nexus_alloc_resource;
111 static bus_get_resource_list_t	nexus_get_reslist;
112 static bus_get_rman_t		nexus_get_rman;
113 static bus_map_resource_t	nexus_map_resource;
114 static bus_unmap_resource_t	nexus_unmap_resource;
115 
116 #ifdef SMP
117 static bus_bind_intr_t		nexus_bind_intr;
118 #endif
119 static bus_config_intr_t	nexus_config_intr;
120 static bus_describe_intr_t	nexus_describe_intr;
121 static bus_setup_intr_t		nexus_setup_intr;
122 static bus_teardown_intr_t	nexus_teardown_intr;
123 
124 static bus_get_bus_tag_t	nexus_get_bus_tag;
125 
126 #ifdef FDT
127 static ofw_bus_map_intr_t	nexus_ofw_map_intr;
128 /*
129  * PCIB interface
130  */
131 static pcib_alloc_msi_t		nexus_fdt_pcib_alloc_msi;
132 static pcib_release_msi_t	nexus_fdt_pcib_release_msi;
133 static pcib_alloc_msix_t	nexus_fdt_pcib_alloc_msix;
134 static pcib_release_msix_t	nexus_fdt_pcib_release_msix;
135 static pcib_map_msi_t		nexus_fdt_pcib_map_msi;
136 
137 #endif
138 
139 static device_method_t nexus_methods[] = {
140 	/* Device interface */
141 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
142 
143 	/* Bus interface */
144 	DEVMETHOD(bus_add_child,	nexus_add_child),
145 	DEVMETHOD(bus_print_child,	nexus_print_child),
146 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
147 	DEVMETHOD(bus_adjust_resource,	bus_generic_rman_adjust_resource),
148 	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
149 	DEVMETHOD(bus_deactivate_resource, bus_generic_rman_deactivate_resource),
150 	DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
151 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
152 	DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
153 	DEVMETHOD(bus_get_rman,		nexus_get_rman),
154 	DEVMETHOD(bus_map_resource,	nexus_map_resource),
155 	DEVMETHOD(bus_release_resource,	bus_generic_rman_release_resource),
156 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
157 	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
158 #ifdef SMP
159 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
160 #endif
161 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
162 	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
163 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
164 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
165 	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
166 
167 	DEVMETHOD_END
168 };
169 
170 static driver_t nexus_driver = {
171 	"nexus",
172 	nexus_methods,
173 	1			/* no softc */
174 };
175 
176 static int
nexus_attach(device_t dev)177 nexus_attach(device_t dev)
178 {
179 
180 	mem_rman.rm_start = 0;
181 	mem_rman.rm_end = BUS_SPACE_MAXADDR;
182 	mem_rman.rm_type = RMAN_ARRAY;
183 	mem_rman.rm_descr = "I/O memory addresses";
184 	if (rman_init(&mem_rman) ||
185 	    rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
186 		panic("nexus_attach mem_rman");
187 	irq_rman.rm_start = 0;
188 	irq_rman.rm_end = ~0;
189 	irq_rman.rm_type = RMAN_ARRAY;
190 	irq_rman.rm_descr = "Interrupts";
191 	if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
192 		panic("nexus_attach irq_rman");
193 
194 	bus_identify_children(dev);
195 	bus_attach_children(dev);
196 
197 	return (0);
198 }
199 
200 static int
nexus_print_child(device_t bus,device_t child)201 nexus_print_child(device_t bus, device_t child)
202 {
203 	int retval = 0;
204 
205 	retval += bus_print_child_header(bus, child);
206 	retval += printf("\n");
207 
208 	return (retval);
209 }
210 
211 static device_t
nexus_add_child(device_t bus,u_int order,const char * name,int unit)212 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
213 {
214 	device_t child;
215 	struct nexus_device *ndev;
216 
217 	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
218 	if (!ndev)
219 		return (0);
220 	resource_list_init(&ndev->nx_resources);
221 
222 	child = device_add_child_ordered(bus, order, name, unit);
223 
224 	/* should we free this in nexus_child_detached? */
225 	device_set_ivars(child, ndev);
226 
227 	return (child);
228 }
229 
230 static struct rman *
nexus_get_rman(device_t bus,int type,u_int flags)231 nexus_get_rman(device_t bus, int type, u_int flags)
232 {
233 
234 	switch (type) {
235 	case SYS_RES_IRQ:
236 		return (&irq_rman);
237 	case SYS_RES_MEMORY:
238 		return (&mem_rman);
239 	default:
240 		return (NULL);
241 	}
242 }
243 
244 /*
245  * Allocate a resource on behalf of child.  NB: child is usually going to be a
246  * child of one of our descendants, not a direct child of nexus0.
247  */
248 static struct resource *
nexus_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)249 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
250     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
251 {
252 	struct nexus_device *ndev = DEVTONX(child);
253 	struct resource_list_entry *rle;
254 
255 	/*
256 	 * If this is an allocation of the "default" range for a given
257 	 * RID, and we know what the resources for this device are
258 	 * (ie. they aren't maintained by a child bus), then work out
259 	 * the start/end values.
260 	 */
261 	if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) {
262 		if (device_get_parent(child) != bus || ndev == NULL)
263 			return (NULL);
264 		rle = resource_list_find(&ndev->nx_resources, type, *rid);
265 		if (rle == NULL)
266 			return (NULL);
267 		start = rle->start;
268 		end = rle->end;
269 		count = rle->count;
270 	}
271 
272 	return (bus_generic_rman_alloc_resource(bus, child, type, rid, start,
273 	    end, count, flags));
274 }
275 
276 static int
nexus_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)277 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
278     enum intr_polarity pol)
279 {
280 
281 	/*
282 	 * On arm64 (due to INTRNG), ACPI interrupt configuration is
283 	 * done in nexus_acpi_map_intr().
284 	 */
285 	return (0);
286 }
287 
288 static int
nexus_setup_intr(device_t dev,device_t child,struct resource * res,int flags,driver_filter_t * filt,driver_intr_t * intr,void * arg,void ** cookiep)289 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
290     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
291 {
292 	int error;
293 
294 	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
295 		flags |= INTR_EXCL;
296 
297 	/* We depend here on rman_activate_resource() being idempotent. */
298 	error = rman_activate_resource(res);
299 	if (error)
300 		return (error);
301 
302 	error = intr_setup_irq(child, res, filt, intr, arg, flags, cookiep);
303 
304 	return (error);
305 }
306 
307 static int
nexus_teardown_intr(device_t dev,device_t child,struct resource * r,void * ih)308 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
309 {
310 
311 	return (intr_teardown_irq(child, r, ih));
312 }
313 
314 static int
nexus_describe_intr(device_t dev,device_t child,struct resource * irq,void * cookie,const char * descr)315 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
316     void *cookie, const char *descr)
317 {
318 
319 	return (intr_describe_irq(child, irq, cookie, descr));
320 }
321 
322 #ifdef SMP
323 static int
nexus_bind_intr(device_t dev,device_t child,struct resource * irq,int cpu)324 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
325 {
326 
327 	return (intr_bind_irq(child, irq, cpu));
328 }
329 #endif
330 
331 static bus_space_tag_t
nexus_get_bus_tag(device_t bus __unused,device_t child __unused)332 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
333 {
334 
335 	return (&memmap_bus);
336 }
337 
338 static int
nexus_activate_resource_flags(device_t bus,device_t child,struct resource * r,int flags)339 nexus_activate_resource_flags(device_t bus, device_t child, struct resource *r,
340     int flags)
341 {
342 	struct resource_map_request args;
343 	struct resource_map map;
344 	int err, use_np;
345 
346 	/*
347 	 * If this is a memory resource, map it into the kernel.
348 	 */
349 	switch (rman_get_type(r)) {
350 	case SYS_RES_MEMORY:
351 		if ((err = rman_activate_resource(r)) != 0)
352 			return (err);
353 
354 		if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
355 			resource_init_map_request(&args);
356 			use_np = (flags & BUS_SPACE_MAP_NONPOSTED) != 0 ||
357 			    force_np;
358 			if (!use_np)
359 				resource_int_value(device_get_name(child),
360 				    device_get_unit(child), "force_nonposted",
361 				    &use_np);
362 			if (use_np)
363 				args.memattr = VM_MEMATTR_DEVICE_NP;
364 			err = nexus_map_resource(bus, child, r, &args, &map);
365 			if (err != 0) {
366 				rman_deactivate_resource(r);
367 				return (err);
368 			}
369 
370 			rman_set_mapping(r, &map);
371 		}
372 		break;
373 	default:
374 		return (bus_generic_rman_activate_resource(bus, child, r));
375 	}
376 	return (0);
377 }
378 
379 static int
nexus_activate_resource(device_t dev,device_t child,struct resource * r)380 nexus_activate_resource(device_t dev, device_t child, struct resource *r)
381 {
382 	return (nexus_activate_resource_flags(dev, child, r, 0));
383 }
384 
385 static struct resource_list *
nexus_get_reslist(device_t dev,device_t child)386 nexus_get_reslist(device_t dev, device_t child)
387 {
388 	struct nexus_device *ndev = DEVTONX(child);
389 
390 	return (&ndev->nx_resources);
391 }
392 
393 static int
nexus_map_resource(device_t bus,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)394 nexus_map_resource(device_t bus, device_t child, struct resource *r,
395     struct resource_map_request *argsp, struct resource_map *map)
396 {
397 	struct resource_map_request args;
398 	rman_res_t length, start;
399 	int error;
400 
401 	/* Resources must be active to be mapped. */
402 	if ((rman_get_flags(r) & RF_ACTIVE) == 0)
403 		return (ENXIO);
404 
405 	/* Mappings are only supported on memory resources. */
406 	switch (rman_get_type(r)) {
407 	case SYS_RES_MEMORY:
408 		break;
409 	default:
410 		return (EINVAL);
411 	}
412 
413 	resource_init_map_request(&args);
414 	error = resource_validate_map_request(r, argsp, &args, &start, &length);
415 	if (error)
416 		return (error);
417 
418 	map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
419 	map->r_bustag = &memmap_bus;
420 	map->r_size = length;
421 
422 	/*
423 	 * The handle is the virtual address.
424 	 */
425 	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
426 	return (0);
427 }
428 
429 static int
nexus_unmap_resource(device_t bus,device_t child,struct resource * r,struct resource_map * map)430 nexus_unmap_resource(device_t bus, device_t child, struct resource *r,
431     struct resource_map *map)
432 {
433 
434 	switch (rman_get_type(r)) {
435 	case SYS_RES_MEMORY:
436 		pmap_unmapdev(map->r_vaddr, map->r_size);
437 		return (0);
438 	default:
439 		return (EINVAL);
440 	}
441 }
442 
443 #ifdef FDT
444 static device_method_t nexus_fdt_methods[] = {
445 	/* Device interface */
446 	DEVMETHOD(device_probe,		nexus_fdt_probe),
447 	DEVMETHOD(device_attach,	nexus_fdt_attach),
448 
449 	/* Bus interface */
450 	DEVMETHOD(bus_activate_resource,	nexus_fdt_activate_resource),
451 
452 	/* OFW interface */
453 	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
454 
455 	/* PCIB interface */
456 	DEVMETHOD(pcib_alloc_msi,	nexus_fdt_pcib_alloc_msi),
457 	DEVMETHOD(pcib_release_msi,	nexus_fdt_pcib_release_msi),
458 	DEVMETHOD(pcib_alloc_msix,	nexus_fdt_pcib_alloc_msix),
459 	DEVMETHOD(pcib_release_msix,	nexus_fdt_pcib_release_msix),
460 	DEVMETHOD(pcib_map_msi,		nexus_fdt_pcib_map_msi),
461 
462 	DEVMETHOD_END,
463 };
464 
465 #define nexus_baseclasses nexus_fdt_baseclasses
466 DEFINE_CLASS_1(nexus, nexus_fdt_driver, nexus_fdt_methods, 1, nexus_driver);
467 #undef nexus_baseclasses
468 
469 EARLY_DRIVER_MODULE(nexus_fdt, root, nexus_fdt_driver, 0, 0,
470     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
471 
472 static int
nexus_fdt_probe(device_t dev)473 nexus_fdt_probe(device_t dev)
474 {
475 
476 	if (arm64_bus_method != ARM64_BUS_FDT)
477 		return (ENXIO);
478 
479 	device_quiet(dev);
480 	return (BUS_PROBE_DEFAULT);
481 }
482 
483 static int
nexus_fdt_attach(device_t dev)484 nexus_fdt_attach(device_t dev)
485 {
486 
487 	nexus_add_child(dev, 10, "ofwbus", 0);
488 	return (nexus_attach(dev));
489 }
490 
491 static int
nexus_fdt_activate_resource(device_t bus,device_t child,struct resource * r)492 nexus_fdt_activate_resource(device_t bus, device_t child, struct resource *r)
493 {
494 	phandle_t node, parent;
495 	int flags;
496 
497 	flags = 0;
498 	switch (rman_get_type(r)) {
499 	case SYS_RES_MEMORY:
500 		/*
501 		 * If the fdt parent has the nonposted-mmio property we
502 		 * need to use non-posted IO to access the device. When
503 		 * we find this property set the BUS_SPACE_MAP_NONPOSTED
504 		 * flag to be passed to bus_space_map.
505 		 */
506 		node = ofw_bus_get_node(child);
507 		if (node != -1) {
508 			parent = OF_parent(node);
509 			if (parent != 0 &&
510 			    OF_hasprop(parent, "nonposted-mmio")) {
511 				flags |= BUS_SPACE_MAP_NONPOSTED;
512 			}
513 		}
514 		break;
515 	default:
516 		break;
517 	}
518 
519 	return (nexus_activate_resource_flags(bus, child, r, flags));
520 }
521 
522 static int
nexus_ofw_map_intr(device_t dev,device_t child,phandle_t iparent,int icells,pcell_t * intr)523 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
524     pcell_t *intr)
525 {
526 	u_int irq;
527 	struct intr_map_data_fdt *fdt_data;
528 	size_t len;
529 
530 	len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
531 	fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
532 	    INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
533 	fdt_data->iparent = iparent;
534 	fdt_data->ncells = icells;
535 	memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
536 	irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
537 	return (irq);
538 }
539 
540 static int
nexus_fdt_pcib_alloc_msi(device_t dev,device_t child,int count,int maxcount,int * irqs)541 nexus_fdt_pcib_alloc_msi(device_t dev, device_t child, int count, int maxcount,
542     int *irqs)
543 {
544 	phandle_t msi_parent;
545 	int error;
546 
547 	error = ofw_bus_msimap(ofw_bus_get_node(child), 0, &msi_parent, NULL);
548 	if (error != 0)
549 		return (error);
550 
551 	return (intr_alloc_msi(dev, child, msi_parent, count, maxcount, irqs));
552 }
553 
554 static int
nexus_fdt_pcib_release_msi(device_t dev,device_t child,int count,int * irqs)555 nexus_fdt_pcib_release_msi(device_t dev, device_t child, int count, int *irqs)
556 {
557 	phandle_t msi_parent;
558 	int error;
559 
560 	error = ofw_bus_msimap(ofw_bus_get_node(child), 0, &msi_parent, NULL);
561 	if (error != 0)
562 		return (error);
563 
564 	return (intr_release_msi(dev, child, msi_parent, count, irqs));
565 }
566 
567 static int
nexus_fdt_pcib_alloc_msix(device_t dev,device_t child,int * irq)568 nexus_fdt_pcib_alloc_msix(device_t dev, device_t child, int *irq)
569 {
570 	phandle_t msi_parent;
571 	int error;
572 
573 	error = ofw_bus_msimap(ofw_bus_get_node(child), 0, &msi_parent, NULL);
574 	if (error != 0)
575 		return (error);
576 
577 	return (intr_alloc_msix(dev, child, msi_parent, irq));
578 }
579 
580 static int
nexus_fdt_pcib_release_msix(device_t dev,device_t child,int irq)581 nexus_fdt_pcib_release_msix(device_t dev, device_t child, int irq)
582 {
583 	phandle_t msi_parent;
584 	int error;
585 
586 	error = ofw_bus_msimap(ofw_bus_get_node(child), 0, &msi_parent, NULL);
587 	if (error != 0)
588 		return (error);
589 
590 	return (intr_release_msix(dev, child, msi_parent, irq));
591 }
592 
593 static int
nexus_fdt_pcib_map_msi(device_t dev,device_t child,int irq,uint64_t * addr,uint32_t * data)594 nexus_fdt_pcib_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
595     uint32_t *data)
596 {
597 	phandle_t msi_parent;
598 	int error;
599 
600 	error = ofw_bus_msimap(ofw_bus_get_node(child), 0, &msi_parent, NULL);
601 	if (error != 0)
602 		return (error);
603 
604 	return (intr_map_msi(dev, child, msi_parent, irq, addr, data));
605 }
606 #endif
607 
608 #ifdef DEV_ACPI
609 static int nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol);
610 
611 static device_method_t nexus_acpi_methods[] = {
612 	/* Device interface */
613 	DEVMETHOD(device_probe,		nexus_acpi_probe),
614 	DEVMETHOD(device_attach,	nexus_acpi_attach),
615 
616 	/* ACPI interface */
617 	DEVMETHOD(acpi_bus_map_intr,	nexus_acpi_map_intr),
618 
619 	DEVMETHOD_END,
620 };
621 
622 #define nexus_baseclasses nexus_acpi_baseclasses
623 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1,
624     nexus_driver);
625 #undef nexus_baseclasses
626 
627 EARLY_DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, 0, 0,
628     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
629 
630 static int
nexus_acpi_probe(device_t dev)631 nexus_acpi_probe(device_t dev)
632 {
633 
634 	if (arm64_bus_method != ARM64_BUS_ACPI || acpi_identify() != 0)
635 		return (ENXIO);
636 
637 	device_quiet(dev);
638 	return (BUS_PROBE_LOW_PRIORITY);
639 }
640 
641 static int
nexus_acpi_attach(device_t dev)642 nexus_acpi_attach(device_t dev)
643 {
644 
645 	nexus_add_child(dev, 10, "acpi", 0);
646 	return (nexus_attach(dev));
647 }
648 
649 static int
nexus_acpi_map_intr(device_t dev,device_t child,u_int irq,int trig,int pol)650 nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol)
651 {
652 	struct intr_map_data_acpi *acpi_data;
653 	size_t len;
654 
655 	len = sizeof(*acpi_data);
656 	acpi_data = (struct intr_map_data_acpi *)intr_alloc_map_data(
657 	    INTR_MAP_DATA_ACPI, len, M_WAITOK | M_ZERO);
658 	acpi_data->irq = irq;
659 	acpi_data->pol = pol;
660 	acpi_data->trig = trig;
661 
662 	/*
663 	 * TODO: This will only handle a single interrupt controller.
664 	 * ACPI will map multiple controllers into a single virtual IRQ
665 	 * space. Each controller has a System Vector Base to hold the
666 	 * first irq it handles in this space. As such the correct way
667 	 * to handle interrupts with ACPI is to search through the
668 	 * controllers for the largest base value that is no larger than
669 	 * the IRQ value.
670 	 */
671 	irq = intr_map_irq(NULL, ACPI_INTR_XREF,
672 	    (struct intr_map_data *)acpi_data);
673 	return (irq);
674 }
675 #endif
676