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