]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/gic_v3_fdt.c
MFV r299425:
[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/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 "pic_if.h"
48
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
58 static struct resource *gic_v3_ofw_bus_alloc_res(device_t, device_t, int, int *,
59     rman_res_t, rman_res_t, rman_res_t, u_int);
60 static const struct ofw_bus_devinfo *gic_v3_ofw_get_devinfo(device_t, device_t);
61
62 static device_method_t gic_v3_fdt_methods[] = {
63         /* Device interface */
64         DEVMETHOD(device_probe,         gic_v3_fdt_probe),
65         DEVMETHOD(device_attach,        gic_v3_fdt_attach),
66
67         /* Bus interface */
68         DEVMETHOD(bus_alloc_resource,           gic_v3_ofw_bus_alloc_res),
69         DEVMETHOD(bus_activate_resource,        bus_generic_activate_resource),
70
71         /* ofw_bus interface */
72         DEVMETHOD(ofw_bus_get_devinfo,  gic_v3_ofw_get_devinfo),
73         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
74         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
75         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
76         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
77         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
78
79         /* End */
80         DEVMETHOD_END
81 };
82
83 DEFINE_CLASS_1(gic, gic_v3_fdt_driver, gic_v3_fdt_methods,
84     sizeof(struct gic_v3_softc), gic_v3_driver);
85
86 static devclass_t gic_v3_fdt_devclass;
87
88 EARLY_DRIVER_MODULE(gic_v3, simplebus, gic_v3_fdt_driver, gic_v3_fdt_devclass,
89     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
90 EARLY_DRIVER_MODULE(gic_v3, ofwbus, gic_v3_fdt_driver, gic_v3_fdt_devclass,
91     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
92
93 /*
94  * Helper functions declarations.
95  */
96 static int gic_v3_ofw_bus_attach(device_t);
97
98 /*
99  * Device interface.
100  */
101 static int
102 gic_v3_fdt_probe(device_t dev)
103 {
104
105         if (!ofw_bus_status_okay(dev))
106                 return (ENXIO);
107
108         if (!ofw_bus_is_compatible(dev, "arm,gic-v3"))
109                 return (ENXIO);
110
111         device_set_desc(dev, GIC_V3_DEVSTR);
112         return (BUS_PROBE_DEFAULT);
113 }
114
115 static int
116 gic_v3_fdt_attach(device_t dev)
117 {
118         struct gic_v3_softc *sc;
119         pcell_t redist_regions;
120         int err;
121
122         sc = device_get_softc(dev);
123         sc->dev = dev;
124
125         /*
126          * Recover number of the Re-Distributor regions.
127          */
128         if (OF_getencprop(ofw_bus_get_node(dev), "#redistributor-regions",
129             &redist_regions, sizeof(redist_regions)) <= 0)
130                 sc->gic_redists.nregions = 1;
131         else
132                 sc->gic_redists.nregions = redist_regions;
133
134         err = gic_v3_attach(dev);
135         if (err)
136                 goto error;
137         /*
138          * Try to register ITS to this GIC.
139          * GIC will act as a bus in that case.
140          * Failure here will not affect main GIC functionality.
141          */
142         if (gic_v3_ofw_bus_attach(dev) != 0) {
143                 if (bootverbose) {
144                         device_printf(dev,
145                             "Failed to attach ITS to this GIC\n");
146                 }
147         }
148
149         return (err);
150
151 error:
152         if (bootverbose) {
153                 device_printf(dev,
154                     "Failed to attach. Error %d\n", err);
155         }
156         /* Failure so free resources */
157         gic_v3_detach(dev);
158
159         return (err);
160 }
161
162 /* OFW bus interface */
163 struct gic_v3_ofw_devinfo {
164         struct ofw_bus_devinfo  di_dinfo;
165         struct resource_list    di_rl;
166 };
167
168 static const struct ofw_bus_devinfo *
169 gic_v3_ofw_get_devinfo(device_t bus __unused, device_t child)
170 {
171         struct gic_v3_ofw_devinfo *di;
172
173         di = device_get_ivars(child);
174         return (&di->di_dinfo);
175 }
176
177 static struct resource *
178 gic_v3_ofw_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
179     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
180 {
181         struct gic_v3_ofw_devinfo *di;
182         struct resource_list_entry *rle;
183         int ranges_len;
184
185         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
186                 if ((di = device_get_ivars(child)) == NULL)
187                         return (NULL);
188                 if (type != SYS_RES_MEMORY)
189                         return (NULL);
190
191                 /* Find defaults for this rid */
192                 rle = resource_list_find(&di->di_rl, type, *rid);
193                 if (rle == NULL)
194                         return (NULL);
195
196                 start = rle->start;
197                 end = rle->end;
198                 count = rle->count;
199         }
200         /*
201          * XXX: No ranges remap!
202          *      Absolute address is expected.
203          */
204         if (ofw_bus_has_prop(bus, "ranges")) {
205                 ranges_len = OF_getproplen(ofw_bus_get_node(bus), "ranges");
206                 if (ranges_len != 0) {
207                         if (bootverbose) {
208                                 device_printf(child,
209                                     "Ranges remap not supported\n");
210                         }
211                         return (NULL);
212                 }
213         }
214         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
215             count, flags));
216 }
217
218 /* Helper functions */
219
220 /*
221  * Bus capability support for GICv3.
222  * Collects and configures device informations and finally
223  * adds ITS device as a child of GICv3 in Newbus hierarchy.
224  */
225 static int
226 gic_v3_ofw_bus_attach(device_t dev)
227 {
228         struct gic_v3_ofw_devinfo *di;
229         device_t child;
230         phandle_t parent, node;
231         pcell_t addr_cells, size_cells;
232
233         parent = ofw_bus_get_node(dev);
234         if (parent > 0) {
235                 addr_cells = 2;
236                 OF_getencprop(parent, "#address-cells", &addr_cells,
237                     sizeof(addr_cells));
238                 size_cells = 2;
239                 OF_getencprop(parent, "#size-cells", &size_cells,
240                     sizeof(size_cells));
241                 /* Iterate through all GIC subordinates */
242                 for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
243                         /* Allocate and populate devinfo. */
244                         di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
245                         if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node)) {
246                                 if (bootverbose) {
247                                         device_printf(dev,
248                                             "Could not set up devinfo for ITS\n");
249                                 }
250                                 free(di, M_GIC_V3);
251                                 continue;
252                         }
253
254                         /* Initialize and populate resource list. */
255                         resource_list_init(&di->di_rl);
256                         ofw_bus_reg_to_rl(dev, node, addr_cells, size_cells,
257                             &di->di_rl);
258
259                         /* Should not have any interrupts, so don't add any */
260
261                         /* Add newbus device for this FDT node */
262                         child = device_add_child(dev, NULL, -1);
263                         if (!child) {
264                                 if (bootverbose) {
265                                         device_printf(dev,
266                                             "Could not add child: %s\n",
267                                             di->di_dinfo.obd_name);
268                                 }
269                                 resource_list_free(&di->di_rl);
270                                 ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
271                                 free(di, M_GIC_V3);
272                                 continue;
273                         }
274
275                         device_set_ivars(child, di);
276                 }
277         }
278
279         return (bus_generic_attach(dev));
280 }
281
282 static int gic_v3_its_fdt_probe(device_t dev);
283
284 static device_method_t gic_v3_its_fdt_methods[] = {
285         /* Device interface */
286         DEVMETHOD(device_probe,         gic_v3_its_fdt_probe),
287
288         /* End */
289         DEVMETHOD_END
290 };
291
292 DEFINE_CLASS_1(its, gic_v3_its_fdt_driver, gic_v3_its_fdt_methods,
293     sizeof(struct gic_v3_its_softc), gic_v3_its_driver);
294
295 static devclass_t gic_v3_its_fdt_devclass;
296
297 EARLY_DRIVER_MODULE(its, gic, gic_v3_its_fdt_driver,
298     gic_v3_its_fdt_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
299
300 static int
301 gic_v3_its_fdt_probe(device_t dev)
302 {
303
304         if (!ofw_bus_status_okay(dev))
305                 return (ENXIO);
306
307         if (!ofw_bus_is_compatible(dev, GIC_V3_ITS_COMPSTR))
308                 return (ENXIO);
309
310         device_set_desc(dev, GIC_V3_ITS_DEVSTR);
311         return (BUS_PROBE_DEFAULT);
312 }