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