]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic_fdt.c
Update llvm, clang, lld and lldb to release_39 branch r288513.
[FreeBSD/FreeBSD.git] / sys / arm / arm / gic_fdt.c
1 /*-
2  * Copyright (c) 2011,2016 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Andrew Turner under
6  * sponsorship from the FreeBSD Foundation.
7  *
8  * Developed by Damjan Marion <damjan.marion@gmail.com>
9  *
10  * Based on OMAP4 GIC code by Ben Gray
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the company nor the name of the author may be used to
21  *    endorse or promote products derived from this software without specific
22  *    prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include "opt_platform.h"
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47
48 #include <machine/intr.h>
49
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53
54 #include <arm/arm/gic.h>
55
56 #ifdef INTRNG
57 struct arm_gic_devinfo {
58         struct ofw_bus_devinfo  obdinfo;
59         struct resource_list    rl;
60 };
61 #endif
62
63 static device_probe_t gic_fdt_probe;
64 static device_attach_t gic_fdt_attach;
65 static ofw_bus_get_devinfo_t gic_ofw_get_devinfo;
66 #ifdef INTRNG
67 static bus_get_resource_list_t gic_fdt_get_resource_list;
68 static bool arm_gic_add_children(device_t);
69 #endif
70
71 static struct ofw_compat_data compat_data[] = {
72         {"arm,gic",             true},  /* Non-standard, used in FreeBSD dts. */
73         {"arm,gic-400",         true},
74         {"arm,cortex-a15-gic",  true},
75         {"arm,cortex-a9-gic",   true},
76         {"arm,cortex-a7-gic",   true},
77         {"arm,arm11mp-gic",     true},
78         {"brcm,brahma-b15-gic", true},
79         {"qcom,msm-qgic2",      true},
80         {NULL,                  false}
81 };
82
83 static device_method_t gic_fdt_methods[] = {
84         /* Device interface */
85         DEVMETHOD(device_probe,         gic_fdt_probe),
86         DEVMETHOD(device_attach,        gic_fdt_attach),
87
88 #ifdef INTRNG
89         /* Bus interface */
90         DEVMETHOD(bus_get_resource_list,gic_fdt_get_resource_list),
91
92         /* ofw_bus interface */
93         DEVMETHOD(ofw_bus_get_devinfo,  gic_ofw_get_devinfo),
94         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
95         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
96         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
97         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
98         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
99 #endif
100
101         DEVMETHOD_END,
102 };
103
104 DEFINE_CLASS_1(gic, gic_fdt_driver, gic_fdt_methods,
105     sizeof(struct arm_gic_softc), arm_gic_driver);
106
107 static devclass_t gic_fdt_devclass;
108
109 EARLY_DRIVER_MODULE(gic, simplebus, gic_fdt_driver, gic_fdt_devclass, 0, 0,
110     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
111 EARLY_DRIVER_MODULE(gic, ofwbus, gic_fdt_driver, gic_fdt_devclass, 0, 0,
112     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
113
114 static int
115 gic_fdt_probe(device_t dev)
116 {
117
118         if (!ofw_bus_status_okay(dev))
119                 return (ENXIO);
120
121         if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
122                 return (ENXIO);
123         device_set_desc(dev, "ARM Generic Interrupt Controller");
124         return (BUS_PROBE_DEFAULT);
125 }
126
127 static int
128 gic_fdt_attach(device_t dev)
129 {
130 #ifdef INTRNG
131         struct arm_gic_softc *sc = device_get_softc(dev);
132         phandle_t pxref;
133         intptr_t xref;
134 #endif
135         int err;
136
137         err = arm_gic_attach(dev);
138         if (err != 0)
139                 return (err);
140
141 #ifdef INTRNG
142         xref = OF_xref_from_node(ofw_bus_get_node(dev));
143
144         /*
145          * Now, when everything is initialized, it's right time to
146          * register interrupt controller to interrupt framefork.
147          */
148         if (intr_pic_register(dev, xref) == NULL) {
149                 device_printf(dev, "could not register PIC\n");
150                 goto cleanup;
151         }
152
153         /*
154          * Controller is root if:
155          * - doesn't have interrupt parent
156          * - his interrupt parent is this controller
157          */
158         pxref = ofw_bus_find_iparent(ofw_bus_get_node(dev));
159         if (pxref == 0 || xref == pxref) {
160                 if (intr_pic_claim_root(dev, xref, arm_gic_intr, sc,
161                     GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
162                         device_printf(dev, "could not set PIC as a root\n");
163                         intr_pic_deregister(dev, xref);
164                         goto cleanup;
165                 }
166         } else {
167                 if (sc->gic_res[2] == NULL) {
168                         device_printf(dev,
169                             "not root PIC must have defined interrupt\n");
170                         intr_pic_deregister(dev, xref);
171                         goto cleanup;
172                 }
173                 if (bus_setup_intr(dev, sc->gic_res[2], INTR_TYPE_CLK,
174                     arm_gic_intr, NULL, sc, &sc->gic_intrhand)) {
175                         device_printf(dev, "could not setup irq handler\n");
176                         intr_pic_deregister(dev, xref);
177                         goto cleanup;
178                 }
179         }
180
181         OF_device_register_xref(xref, dev);
182
183         /* If we have children probe and attach them */
184         if (arm_gic_add_children(dev)) {
185                 bus_generic_probe(dev);
186                 return (bus_generic_attach(dev));
187         }
188 #endif
189
190         return (0);
191
192 #ifdef INTRNG
193 cleanup:
194         arm_gic_detach(dev);
195         return(ENXIO);
196 #endif
197 }
198
199 #ifdef INTRNG
200 static struct resource_list *
201 gic_fdt_get_resource_list(device_t bus, device_t child)
202 {
203         struct arm_gic_devinfo *di;
204
205         di = device_get_ivars(child);
206         KASSERT(di != NULL, ("gic_fdt_get_resource_list: No devinfo"));
207
208         return (&di->rl);
209 }
210
211 static int
212 arm_gic_fill_ranges(phandle_t node, struct arm_gic_softc *sc)
213 {
214         pcell_t host_cells;
215         cell_t *base_ranges;
216         ssize_t nbase_ranges;
217         int i, j, k;
218
219         host_cells = 1;
220         OF_getencprop(OF_parent(node), "#address-cells", &host_cells,
221             sizeof(host_cells));
222         sc->addr_cells = 2;
223         OF_getencprop(node, "#address-cells", &sc->addr_cells,
224             sizeof(sc->addr_cells));
225         sc->size_cells = 2;
226         OF_getencprop(node, "#size-cells", &sc->size_cells,
227             sizeof(sc->size_cells));
228
229         nbase_ranges = OF_getproplen(node, "ranges");
230         if (nbase_ranges < 0)
231                 return (-1);
232         sc->nranges = nbase_ranges / sizeof(cell_t) /
233             (sc->addr_cells + host_cells + sc->size_cells);
234         if (sc->nranges == 0)
235                 return (0);
236
237         sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
238             M_DEVBUF, M_WAITOK);
239         base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
240         OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
241
242         for (i = 0, j = 0; i < sc->nranges; i++) {
243                 sc->ranges[i].bus = 0;
244                 for (k = 0; k < sc->addr_cells; k++) {
245                         sc->ranges[i].bus <<= 32;
246                         sc->ranges[i].bus |= base_ranges[j++];
247                 }
248                 sc->ranges[i].host = 0;
249                 for (k = 0; k < host_cells; k++) {
250                         sc->ranges[i].host <<= 32;
251                         sc->ranges[i].host |= base_ranges[j++];
252                 }
253                 sc->ranges[i].size = 0;
254                 for (k = 0; k < sc->size_cells; k++) {
255                         sc->ranges[i].size <<= 32;
256                         sc->ranges[i].size |= base_ranges[j++];
257                 }
258         }
259
260         free(base_ranges, M_DEVBUF);
261         return (sc->nranges);
262 }
263
264 static bool
265 arm_gic_add_children(device_t dev)
266 {
267         struct arm_gic_softc *sc;
268         struct arm_gic_devinfo *dinfo;
269         phandle_t child, node;
270         device_t cdev;
271
272         sc = device_get_softc(dev);
273         node = ofw_bus_get_node(dev);
274
275         /* If we have no children don't probe for them */
276         child = OF_child(node);
277         if (child == 0)
278                 return (false);
279
280         if (arm_gic_fill_ranges(node, sc) < 0) {
281                 device_printf(dev, "Have a child, but no ranges\n");
282                 return (false);
283         }
284
285         for (; child != 0; child = OF_peer(child)) {
286                 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
287
288                 if (ofw_bus_gen_setup_devinfo(&dinfo->obdinfo, child) != 0) {
289                         free(dinfo, M_DEVBUF);
290                         continue;
291                 }
292
293                 resource_list_init(&dinfo->rl);
294                 ofw_bus_reg_to_rl(dev, child, sc->addr_cells,
295                     sc->size_cells, &dinfo->rl);
296
297                 cdev = device_add_child(dev, NULL, -1);
298                 if (cdev == NULL) {
299                         device_printf(dev, "<%s>: device_add_child failed\n",
300                             dinfo->obdinfo.obd_name);
301                         resource_list_free(&dinfo->rl);
302                         ofw_bus_gen_destroy_devinfo(&dinfo->obdinfo);
303                         free(dinfo, M_DEVBUF);
304                         continue;
305                 }
306                 device_set_ivars(cdev, dinfo);
307         }
308
309         return (true);
310 }
311
312 static const struct ofw_bus_devinfo *
313 gic_ofw_get_devinfo(device_t bus __unused, device_t child)
314 {
315         struct arm_gic_devinfo *di;
316
317         di = device_get_ivars(child);
318
319         return (&di->obdinfo);
320 }
321
322 static struct ofw_compat_data gicv2m_compat_data[] = {
323         {"arm,gic-v2m-frame",   true},
324         {NULL,                  false}
325 };
326
327 static int
328 arm_gicv2m_fdt_probe(device_t dev)
329 {
330
331         if (!ofw_bus_status_okay(dev))
332                 return (ENXIO);
333
334         if (!ofw_bus_search_compatible(dev, gicv2m_compat_data)->ocd_data)
335                 return (ENXIO);
336
337         device_set_desc(dev, "ARM Generic Interrupt Controller MSI/MSIX");
338         return (BUS_PROBE_DEFAULT);
339 }
340
341 static int
342 arm_gicv2m_fdt_attach(device_t dev)
343 {
344         struct arm_gicv2m_softc *sc;
345
346         sc = device_get_softc(dev);
347         sc->sc_xref = OF_xref_from_node(ofw_bus_get_node(dev));
348
349         return (arm_gicv2m_attach(dev));
350 }
351
352 static device_method_t arm_gicv2m_fdt_methods[] = {
353         /* Device interface */
354         DEVMETHOD(device_probe,         arm_gicv2m_fdt_probe),
355         DEVMETHOD(device_attach,        arm_gicv2m_fdt_attach),
356
357         /* End */
358         DEVMETHOD_END
359 };
360
361 DEFINE_CLASS_1(gicv2m, arm_gicv2m_fdt_driver, arm_gicv2m_fdt_methods,
362     sizeof(struct arm_gicv2m_softc), arm_gicv2m_driver);
363
364 static devclass_t arm_gicv2m_fdt_devclass;
365
366 EARLY_DRIVER_MODULE(gicv2m, gic, arm_gicv2m_fdt_driver,
367     arm_gicv2m_fdt_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
368 #endif