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