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