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