]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/gic_v3_fdt.c
contrib/tzdata: import tzdata 2022e
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / gic_v3_fdt.c
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  *
4  * This software was developed by Semihalf under
5  * the sponsorship of the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bitstring.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39
40 #include <machine/intr.h>
41 #include <machine/resource.h>
42
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <arm/arm/gic_common.h>
48 #include "gic_v3_reg.h"
49 #include "gic_v3_var.h"
50
51 /*
52  * FDT glue.
53  */
54 static int gic_v3_fdt_probe(device_t);
55 static int gic_v3_fdt_attach(device_t);
56
57 static const struct ofw_bus_devinfo *gic_v3_ofw_get_devinfo(device_t, device_t);
58 static bus_get_resource_list_t gic_v3_fdt_get_resource_list;
59
60 static device_method_t gic_v3_fdt_methods[] = {
61         /* Device interface */
62         DEVMETHOD(device_probe,         gic_v3_fdt_probe),
63         DEVMETHOD(device_attach,        gic_v3_fdt_attach),
64
65         /* Bus interface */
66         DEVMETHOD(bus_get_resource_list,        gic_v3_fdt_get_resource_list),
67
68         /* ofw_bus interface */
69         DEVMETHOD(ofw_bus_get_devinfo,  gic_v3_ofw_get_devinfo),
70         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
71         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
72         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
73         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
74         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
75
76         /* End */
77         DEVMETHOD_END
78 };
79
80 DEFINE_CLASS_1(gic, gic_v3_fdt_driver, gic_v3_fdt_methods,
81     sizeof(struct gic_v3_softc), gic_v3_driver);
82
83 EARLY_DRIVER_MODULE(gic_v3, simplebus, gic_v3_fdt_driver, 0, 0,
84     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
85 EARLY_DRIVER_MODULE(gic_v3, ofwbus, gic_v3_fdt_driver, 0, 0,
86     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
87
88 /*
89  * Helper functions declarations.
90  */
91 static int gic_v3_ofw_bus_attach(device_t);
92
93 /*
94  * Device interface.
95  */
96 static int
97 gic_v3_fdt_probe(device_t dev)
98 {
99
100         if (!ofw_bus_status_okay(dev))
101                 return (ENXIO);
102
103         if (!ofw_bus_is_compatible(dev, "arm,gic-v3"))
104                 return (ENXIO);
105
106         device_set_desc(dev, GIC_V3_DEVSTR);
107         return (BUS_PROBE_DEFAULT);
108 }
109
110 static int
111 gic_v3_fdt_attach(device_t dev)
112 {
113         struct gic_v3_softc *sc;
114         pcell_t redist_regions;
115         intptr_t xref;
116         int err;
117         uint32_t *mbi_ranges;
118         ssize_t ret;
119
120         sc = device_get_softc(dev);
121         sc->dev = dev;
122         sc->gic_bus = GIC_BUS_FDT;
123
124         /*
125          * Recover number of the Re-Distributor regions.
126          */
127         if (OF_getencprop(ofw_bus_get_node(dev), "#redistributor-regions",
128             &redist_regions, sizeof(redist_regions)) <= 0)
129                 sc->gic_redists.nregions = 1;
130         else
131                 sc->gic_redists.nregions = redist_regions;
132
133         /* Add Message Based Interrupts using SPIs. */
134         ret = OF_getencprop_alloc_multi(ofw_bus_get_node(dev), "mbi-ranges",
135             sizeof(*mbi_ranges), (void **)&mbi_ranges);
136         if (ret > 0) {
137                 if (ret % 2 == 0) {
138                         /* Limit to a single range for now. */
139                         sc->gic_mbi_start = mbi_ranges[0];
140                         sc->gic_mbi_end = mbi_ranges[0] + mbi_ranges[1];
141                 } else {
142                         if (bootverbose)
143                                 device_printf(dev, "Malformed mbi-ranges property\n");
144                 }
145                 free(mbi_ranges, M_OFWPROP);
146         }
147
148         err = gic_v3_attach(dev);
149         if (err != 0)
150                 goto error;
151
152         xref = OF_xref_from_node(ofw_bus_get_node(dev));
153         sc->gic_pic = intr_pic_register(dev, xref);
154         if (sc->gic_pic == NULL) {
155                 device_printf(dev, "could not register PIC\n");
156                 err = ENXIO;
157                 goto error;
158         }
159
160         if (sc->gic_mbi_start > 0)
161                 intr_msi_register(dev, xref);
162
163         /* Register xref */
164         OF_device_register_xref(xref, dev);
165
166         if (intr_pic_claim_root(dev, xref, arm_gic_v3_intr, sc,
167             GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
168                 err = ENXIO;
169                 goto error;
170         }
171
172         /*
173          * Try to register ITS to this GIC.
174          * GIC will act as a bus in that case.
175          * Failure here will not affect main GIC functionality.
176          */
177         if (gic_v3_ofw_bus_attach(dev) != 0) {
178                 if (bootverbose) {
179                         device_printf(dev,
180                             "Failed to attach ITS to this GIC\n");
181                 }
182         }
183
184         if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) != 0)
185                 sc->gic_nchildren = 0;
186
187         return (err);
188
189 error:
190         if (bootverbose) {
191                 device_printf(dev,
192                     "Failed to attach. Error %d\n", err);
193         }
194         /* Failure so free resources */
195         gic_v3_detach(dev);
196
197         return (err);
198 }
199
200 /* OFW bus interface */
201 struct gic_v3_ofw_devinfo {
202         struct gic_v3_devinfo   di_gic_dinfo;
203         struct ofw_bus_devinfo  di_dinfo;
204         struct resource_list    di_rl;
205 };
206
207 static const struct ofw_bus_devinfo *
208 gic_v3_ofw_get_devinfo(device_t bus __unused, device_t child)
209 {
210         struct gic_v3_ofw_devinfo *di;
211
212         di = device_get_ivars(child);
213         if (di->di_gic_dinfo.is_vgic)
214                 return (NULL);
215         return (&di->di_dinfo);
216 }
217
218 /* Helper functions */
219 static int
220 gic_v3_ofw_fill_ranges(phandle_t parent, struct gic_v3_softc *sc,
221     pcell_t *addr_cellsp, pcell_t *size_cellsp)
222 {
223         pcell_t addr_cells, host_cells, size_cells;
224         cell_t *base_ranges;
225         ssize_t nbase_ranges;
226         int i, j, k;
227
228         host_cells = 1;
229         OF_getencprop(OF_parent(parent), "#address-cells", &host_cells,
230             sizeof(host_cells));
231         addr_cells = 2;
232         OF_getencprop(parent, "#address-cells", &addr_cells,
233             sizeof(addr_cells));
234         size_cells = 2;
235         OF_getencprop(parent, "#size-cells", &size_cells,
236             sizeof(size_cells));
237
238         *addr_cellsp = addr_cells;
239         *size_cellsp = size_cells;
240
241         nbase_ranges = OF_getproplen(parent, "ranges");
242         if (nbase_ranges < 0)
243                 return (EINVAL);
244
245         sc->nranges = nbase_ranges / sizeof(cell_t) /
246             (addr_cells + host_cells + size_cells);
247         if (sc->nranges == 0)
248                 return (0);
249
250         sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]), M_GIC_V3,
251             M_WAITOK);
252         base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
253         OF_getencprop(parent, "ranges", base_ranges, nbase_ranges);
254
255         for (i = 0, j = 0; i < sc->nranges; i++) {
256                 sc->ranges[i].bus = 0;
257                 for (k = 0; k < addr_cells; k++) {
258                         sc->ranges[i].bus <<= 32;
259                         sc->ranges[i].bus |= base_ranges[j++];
260                 }
261                 sc->ranges[i].host = 0;
262                 for (k = 0; k < host_cells; k++) {
263                         sc->ranges[i].host <<= 32;
264                         sc->ranges[i].host |= base_ranges[j++];
265                 }
266                 sc->ranges[i].size = 0;
267                 for (k = 0; k < size_cells; k++) {
268                         sc->ranges[i].size <<= 32;
269                         sc->ranges[i].size |= base_ranges[j++];
270                 }
271         }
272
273         free(base_ranges, M_DEVBUF);
274         return (0);
275 }
276
277 /*
278  * Bus capability support for GICv3.
279  * Collects and configures device informations and finally
280  * adds ITS device as a child of GICv3 in Newbus hierarchy.
281  */
282 static int
283 gic_v3_ofw_bus_attach(device_t dev)
284 {
285         struct gic_v3_ofw_devinfo *di;
286         struct gic_v3_softc *sc;
287         device_t child;
288         phandle_t parent, node;
289         pcell_t addr_cells, size_cells;
290         int rv;
291
292         sc = device_get_softc(dev);
293         parent = ofw_bus_get_node(dev);
294         if (parent > 0) {
295                 rv = gic_v3_ofw_fill_ranges(parent, sc, &addr_cells,
296                     &size_cells);
297                 if (rv != 0)
298                         return (rv);
299
300                 /* Iterate through all GIC subordinates */
301                 for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
302                         /*
303                          * Ignore children that lack a compatible property.
304                          * Some of them may be for configuration, for example
305                          * ppi-partitions.
306                          */
307                         if (!OF_hasprop(node, "compatible"))
308                                 continue;
309
310                         /* Allocate and populate devinfo. */
311                         di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
312
313                         /* Read the numa node, or -1 if there is none */
314                         if (OF_getencprop(node, "numa-node-id",
315                             &di->di_gic_dinfo.gic_domain,
316                             sizeof(di->di_gic_dinfo.gic_domain)) <= 0) {
317                                 di->di_gic_dinfo.gic_domain = -1;
318                         }
319
320                         if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node)) {
321                                 if (bootverbose) {
322                                         device_printf(dev,
323                                             "Could not set up devinfo for ITS\n");
324                                 }
325                                 free(di, M_GIC_V3);
326                                 continue;
327                         }
328
329                         /* Initialize and populate resource list. */
330                         resource_list_init(&di->di_rl);
331                         ofw_bus_reg_to_rl(dev, node, addr_cells, size_cells,
332                             &di->di_rl);
333
334                         /* Should not have any interrupts, so don't add any */
335
336                         /* Add newbus device for this FDT node */
337                         child = device_add_child(dev, NULL, -1);
338                         if (!child) {
339                                 if (bootverbose) {
340                                         device_printf(dev,
341                                             "Could not add child: %s\n",
342                                             di->di_dinfo.obd_name);
343                                 }
344                                 resource_list_free(&di->di_rl);
345                                 ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
346                                 free(di, M_GIC_V3);
347                                 continue;
348                         }
349
350                         sc->gic_nchildren++;
351                         device_set_ivars(child, di);
352                 }
353         }
354
355         /*
356          * If there is a vgic maintanance interrupt add a virtual gic
357          * child so we can use this in the vmm module for bhyve.
358          */
359         if (OF_hasprop(parent, "interrupts")) {
360                 child = device_add_child(dev, "vgic", -1);
361                 if (child == NULL) {
362                         device_printf(dev, "Could not add vgic child\n");
363                 } else {
364                         di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
365                         resource_list_init(&di->di_rl);
366                         di->di_gic_dinfo.gic_domain = -1;
367                         di->di_gic_dinfo.is_vgic = 1;
368                         device_set_ivars(child, di);
369                         sc->gic_nchildren++;
370                 }
371         }
372
373         return (bus_generic_attach(dev));
374 }
375
376 static struct resource_list *
377 gic_v3_fdt_get_resource_list(device_t bus, device_t child)
378 {
379         struct gic_v3_ofw_devinfo *di;
380
381         di = device_get_ivars(child);
382         KASSERT(di != NULL, ("%s: No devinfo", __func__));
383
384         return (&di->di_rl);
385 }