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