]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/nexus.c
arm64/amd64/riscv nexus: Use bus_generic_rl_*
[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 #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
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 #endif
129
130 static device_method_t nexus_methods[] = {
131         /* Bus interface */
132         DEVMETHOD(bus_add_child,        nexus_add_child),
133         DEVMETHOD(bus_print_child,      nexus_print_child),
134         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
135         DEVMETHOD(bus_adjust_resource,  nexus_adjust_resource),
136         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
137         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
138         DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
139         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
140         DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
141         DEVMETHOD(bus_map_resource,     nexus_map_resource),
142         DEVMETHOD(bus_release_resource, nexus_release_resource),
143         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_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
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_generic_probe(dev);
181         bus_generic_attach(dev);
182
183         return (0);
184 }
185
186 static int
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
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 /*
217  * Allocate a resource on behalf of child.  NB: child is usually going to be a
218  * child of one of our descendants, not a direct child of nexus0.
219  */
220 static struct resource *
221 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
222     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
223 {
224         struct nexus_device *ndev = DEVTONX(child);
225         struct resource *rv;
226         struct resource_list_entry *rle;
227         struct rman *rm;
228         int needactivate = flags & RF_ACTIVE;
229
230         /*
231          * If this is an allocation of the "default" range for a given
232          * RID, and we know what the resources for this device are
233          * (ie. they aren't maintained by a child bus), then work out
234          * the start/end values.
235          */
236         if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) {
237                 if (device_get_parent(child) != bus || ndev == NULL)
238                         return (NULL);
239                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
240                 if (rle == NULL)
241                         return (NULL);
242                 start = rle->start;
243                 end = rle->end;
244                 count = rle->count;
245         }
246
247         switch (type) {
248         case SYS_RES_IRQ:
249                 rm = &irq_rman;
250                 break;
251
252         case SYS_RES_MEMORY:
253         case SYS_RES_IOPORT:
254                 rm = &mem_rman;
255                 break;
256
257         default:
258                 return (NULL);
259         }
260
261         rv = rman_reserve_resource(rm, start, end, count, flags, child);
262         if (rv == NULL)
263                 return (NULL);
264
265         rman_set_rid(rv, *rid);
266         rman_set_bushandle(rv, rman_get_start(rv));
267
268         if (needactivate) {
269                 if (bus_activate_resource(child, type, *rid, rv)) {
270                         rman_release_resource(rv);
271                         return (NULL);
272                 }
273         }
274
275         return (rv);
276 }
277
278 static int
279 nexus_adjust_resource(device_t bus __unused, device_t child __unused, int type,
280     struct resource *r, rman_res_t start, rman_res_t end)
281 {
282         struct rman *rm;
283
284         switch (type) {
285         case SYS_RES_IRQ:
286                 rm = &irq_rman;
287                 break;
288         case SYS_RES_MEMORY:
289                 rm = &mem_rman;
290                 break;
291         default:
292                 return (EINVAL);
293         }
294         if (rman_is_region_manager(r, rm) == 0)
295                 return (EINVAL);
296         return (rman_adjust_resource(r, start, end));
297 }
298
299 static int
300 nexus_release_resource(device_t bus, device_t child, int type, int rid,
301     struct resource *res)
302 {
303         int error;
304
305         if (rman_get_flags(res) & RF_ACTIVE) {
306                 error = bus_deactivate_resource(child, type, rid, res);
307                 if (error)
308                         return (error);
309         }
310         return (rman_release_resource(res));
311 }
312
313 static int
314 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
315     enum intr_polarity pol)
316 {
317
318         /*
319          * On arm64 (due to INTRNG), ACPI interrupt configuration is 
320          * done in nexus_acpi_map_intr().
321          */
322         return (0);
323 }
324
325 static int
326 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
327     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
328 {
329         int error;
330
331         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
332                 flags |= INTR_EXCL;
333
334         /* We depend here on rman_activate_resource() being idempotent. */
335         error = rman_activate_resource(res);
336         if (error)
337                 return (error);
338
339         error = intr_setup_irq(child, res, filt, intr, arg, flags, cookiep);
340
341         return (error);
342 }
343
344 static int
345 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
346 {
347
348         return (intr_teardown_irq(child, r, ih));
349 }
350
351 static int
352 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
353     void *cookie, const char *descr)
354 {
355
356         return (intr_describe_irq(child, irq, cookie, descr));
357 }
358
359 #ifdef SMP
360 static int
361 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
362 {
363
364         return (intr_bind_irq(child, irq, cpu));
365 }
366 #endif
367
368 static bus_space_tag_t
369 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
370 {
371
372         return (&memmap_bus);
373 }
374
375 static int
376 nexus_activate_resource_flags(device_t bus, device_t child, int type, int rid,
377     struct resource *r, int flags)
378 {
379         struct resource_map_request args;
380         struct resource_map map;
381         int err, use_np;
382
383         if ((err = rman_activate_resource(r)) != 0)
384                 return (err);
385
386         /*
387          * If this is a memory resource, map it into the kernel.
388          */
389         switch (type) {
390         case SYS_RES_IOPORT:
391         case SYS_RES_MEMORY:
392                 if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
393                         resource_init_map_request(&args);
394                         use_np = (flags & BUS_SPACE_MAP_NONPOSTED) != 0 ||
395                             force_np;
396                         if (!use_np)
397                                 resource_int_value(device_get_name(child),
398                                     device_get_unit(child), "force_nonposted",
399                                     &use_np);
400                         if (use_np)
401                                 args.memattr = VM_MEMATTR_DEVICE_NP;
402                         err = nexus_map_resource(bus, child, type, r, &args,
403                             &map);
404                         if (err != 0) {
405                                 rman_deactivate_resource(r);
406                                 return (err);
407                         }
408
409                         rman_set_mapping(r, &map);
410                 }
411                 break;
412         case SYS_RES_IRQ:
413                 err = intr_activate_irq(child, r);
414                 if (err != 0) {
415                         rman_deactivate_resource(r);
416                         return (err);
417                 }
418         }
419         return (0);
420 }
421
422 static int
423 nexus_activate_resource(device_t dev, device_t child, int type, int rid,
424     struct resource *r)
425 {
426         return (nexus_activate_resource_flags(dev, child, type, rid, r, 0));
427 }
428
429 static struct resource_list *
430 nexus_get_reslist(device_t dev, device_t child)
431 {
432         struct nexus_device *ndev = DEVTONX(child);
433
434         return (&ndev->nx_resources);
435 }
436
437 static int
438 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
439     struct resource *r)
440 {
441         bus_size_t psize;
442         bus_space_handle_t vaddr;
443
444         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
445                 psize = (bus_size_t)rman_get_size(r);
446                 vaddr = rman_get_bushandle(r);
447
448                 if (vaddr != 0) {
449                         bus_space_unmap(&memmap_bus, vaddr, psize);
450                         rman_set_virtual(r, NULL);
451                         rman_set_bushandle(r, 0);
452                 }
453         } else if (type == SYS_RES_IRQ) {
454                 intr_deactivate_irq(child, r);
455         }
456
457         return (rman_deactivate_resource(r));
458 }
459
460 static int
461 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
462     struct resource_map_request *argsp, struct resource_map *map)
463 {
464         struct resource_map_request args;
465         rman_res_t end, length, start;
466
467         /* Resources must be active to be mapped. */
468         if ((rman_get_flags(r) & RF_ACTIVE) == 0)
469                 return (ENXIO);
470
471         /* Mappings are only supported on I/O and memory resources. */
472         switch (type) {
473         case SYS_RES_IOPORT:
474         case SYS_RES_MEMORY:
475                 break;
476         default:
477                 return (EINVAL);
478         }
479
480         resource_init_map_request(&args);
481         if (argsp != NULL)
482                 bcopy(argsp, &args, imin(argsp->size, args.size));
483         start = rman_get_start(r) + args.offset;
484         if (args.length == 0)
485                 length = rman_get_size(r);
486         else
487                 length = args.length;
488         end = start + length - 1;
489         if (start > rman_get_end(r) || start < rman_get_start(r))
490                 return (EINVAL);
491         if (end > rman_get_end(r) || end < start)
492                 return (EINVAL);
493
494         map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
495         map->r_bustag = &memmap_bus;
496         map->r_size = length;
497
498         /*
499          * The handle is the virtual address.
500          */
501         map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
502         return (0);
503 }
504
505 #ifdef FDT
506 static device_method_t nexus_fdt_methods[] = {
507         /* Device interface */
508         DEVMETHOD(device_probe,         nexus_fdt_probe),
509         DEVMETHOD(device_attach,        nexus_fdt_attach),
510
511         /* Bus interface */
512         DEVMETHOD(bus_activate_resource,        nexus_fdt_activate_resource),
513
514         /* OFW interface */
515         DEVMETHOD(ofw_bus_map_intr,     nexus_ofw_map_intr),
516
517         DEVMETHOD_END,
518 };
519
520 #define nexus_baseclasses nexus_fdt_baseclasses
521 DEFINE_CLASS_1(nexus, nexus_fdt_driver, nexus_fdt_methods, 1, nexus_driver);
522 #undef nexus_baseclasses
523
524 EARLY_DRIVER_MODULE(nexus_fdt, root, nexus_fdt_driver, 0, 0,
525     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
526
527 static int
528 nexus_fdt_probe(device_t dev)
529 {
530
531         if (arm64_bus_method != ARM64_BUS_FDT)
532                 return (ENXIO);
533
534         device_quiet(dev);
535         return (BUS_PROBE_DEFAULT);
536 }
537
538 static int
539 nexus_fdt_attach(device_t dev)
540 {
541
542         nexus_add_child(dev, 10, "ofwbus", 0);
543         return (nexus_attach(dev));
544 }
545
546 static int
547 nexus_fdt_activate_resource(device_t bus, device_t child, int type, int rid,
548     struct resource *r)
549 {
550         phandle_t node, parent;
551         int flags;
552
553         flags = 0;
554         switch (type) {
555         case SYS_RES_MEMORY:
556         case SYS_RES_IOPORT:
557                 /*
558                  * If the fdt parent has the nonposted-mmio property we
559                  * need to use non-posted IO to access the device. When
560                  * we find this property set the BUS_SPACE_MAP_NONPOSTED
561                  * flag to be passed to bus_space_map.
562                  */
563                 node = ofw_bus_get_node(child);
564                 if (node != -1) {
565                         parent = OF_parent(node);
566                         if (parent != 0 &&
567                             OF_hasprop(parent, "nonposted-mmio")) {
568                                 flags |= BUS_SPACE_MAP_NONPOSTED;
569                         }
570                 }
571                 break;
572         default:
573                 break;
574         }
575
576         return (nexus_activate_resource_flags(bus, child, type, rid, r, flags));
577 }
578
579 static int
580 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
581     pcell_t *intr)
582 {
583         u_int irq;
584         struct intr_map_data_fdt *fdt_data;
585         size_t len;
586
587         len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
588         fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
589             INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
590         fdt_data->iparent = iparent;
591         fdt_data->ncells = icells;
592         memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
593         irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
594         return (irq);
595 }
596 #endif
597
598 #ifdef DEV_ACPI
599 static int nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol);
600
601 static device_method_t nexus_acpi_methods[] = {
602         /* Device interface */
603         DEVMETHOD(device_probe,         nexus_acpi_probe),
604         DEVMETHOD(device_attach,        nexus_acpi_attach),
605
606         /* ACPI interface */
607         DEVMETHOD(acpi_bus_map_intr,    nexus_acpi_map_intr),
608
609         DEVMETHOD_END,
610 };
611
612 #define nexus_baseclasses nexus_acpi_baseclasses
613 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1,
614     nexus_driver);
615 #undef nexus_baseclasses
616
617 EARLY_DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, 0, 0,
618     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
619
620 static int
621 nexus_acpi_probe(device_t dev)
622 {
623
624         if (arm64_bus_method != ARM64_BUS_ACPI || acpi_identify() != 0)
625                 return (ENXIO);
626
627         device_quiet(dev);
628         return (BUS_PROBE_LOW_PRIORITY);
629 }
630
631 static int
632 nexus_acpi_attach(device_t dev)
633 {
634
635         nexus_add_child(dev, 10, "acpi", 0);
636         return (nexus_attach(dev));
637 }
638
639 static int
640 nexus_acpi_map_intr(device_t dev, device_t child, u_int irq, int trig, int pol)
641 {
642         struct intr_map_data_acpi *acpi_data;
643         size_t len;
644
645         len = sizeof(*acpi_data);
646         acpi_data = (struct intr_map_data_acpi *)intr_alloc_map_data(
647             INTR_MAP_DATA_ACPI, len, M_WAITOK | M_ZERO);
648         acpi_data->irq = irq;
649         acpi_data->pol = pol;
650         acpi_data->trig = trig;
651
652         /*
653          * TODO: This will only handle a single interrupt controller.
654          * ACPI will map multiple controllers into a single virtual IRQ
655          * space. Each controller has a System Vector Base to hold the
656          * first irq it handles in this space. As such the correct way
657          * to handle interrupts with ACPI is to search through the
658          * controllers for the largest base value that is no larger than
659          * the IRQ value.
660          */
661         irq = intr_map_irq(NULL, ACPI_INTR_XREF,
662             (struct intr_map_data *)acpi_data);
663         return (irq);
664 }
665 #endif