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