xref: /freebsd/sys/arm64/arm64/gic_v3_fdt.c (revision 1323ec57)
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 static int gic_v3_fdt_print_child(device_t, 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_print_child,		gic_v3_fdt_print_child),
69 	DEVMETHOD(bus_alloc_resource,		gic_v3_ofw_bus_alloc_res),
70 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
71 
72 	/* ofw_bus interface */
73 	DEVMETHOD(ofw_bus_get_devinfo,	gic_v3_ofw_get_devinfo),
74 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
75 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
76 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
77 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
78 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
79 
80 	/* End */
81 	DEVMETHOD_END
82 };
83 
84 DEFINE_CLASS_1(gic, gic_v3_fdt_driver, gic_v3_fdt_methods,
85     sizeof(struct gic_v3_softc), gic_v3_driver);
86 
87 static devclass_t gic_v3_fdt_devclass;
88 
89 EARLY_DRIVER_MODULE(gic_v3, simplebus, gic_v3_fdt_driver, gic_v3_fdt_devclass,
90     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
91 EARLY_DRIVER_MODULE(gic_v3, ofwbus, gic_v3_fdt_driver, gic_v3_fdt_devclass,
92     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
93 
94 /*
95  * Helper functions declarations.
96  */
97 static int gic_v3_ofw_bus_attach(device_t);
98 
99 /*
100  * Device interface.
101  */
102 static int
103 gic_v3_fdt_probe(device_t dev)
104 {
105 
106 	if (!ofw_bus_status_okay(dev))
107 		return (ENXIO);
108 
109 	if (!ofw_bus_is_compatible(dev, "arm,gic-v3"))
110 		return (ENXIO);
111 
112 	device_set_desc(dev, GIC_V3_DEVSTR);
113 	return (BUS_PROBE_DEFAULT);
114 }
115 
116 static int
117 gic_v3_fdt_attach(device_t dev)
118 {
119 	struct gic_v3_softc *sc;
120 	pcell_t redist_regions;
121 	intptr_t xref;
122 	int err;
123 	uint32_t *mbi_ranges;
124 	ssize_t ret;
125 
126 	sc = device_get_softc(dev);
127 	sc->dev = dev;
128 	sc->gic_bus = GIC_BUS_FDT;
129 
130 	/*
131 	 * Recover number of the Re-Distributor regions.
132 	 */
133 	if (OF_getencprop(ofw_bus_get_node(dev), "#redistributor-regions",
134 	    &redist_regions, sizeof(redist_regions)) <= 0)
135 		sc->gic_redists.nregions = 1;
136 	else
137 		sc->gic_redists.nregions = redist_regions;
138 
139 	/* Add Message Based Interrupts using SPIs. */
140 	ret = OF_getencprop_alloc_multi(ofw_bus_get_node(dev), "mbi-ranges",
141 	    sizeof(*mbi_ranges), (void **)&mbi_ranges);
142 	if (ret > 0) {
143 		if (ret % 2 == 0) {
144 			/* Limit to a single range for now. */
145 			sc->gic_mbi_start = mbi_ranges[0];
146 			sc->gic_mbi_end = mbi_ranges[0] + mbi_ranges[1];
147 		} else {
148 			if (bootverbose)
149 				device_printf(dev, "Malformed mbi-ranges property\n");
150 		}
151 		free(mbi_ranges, M_OFWPROP);
152 	}
153 
154 	err = gic_v3_attach(dev);
155 	if (err != 0)
156 		goto error;
157 
158 	xref = OF_xref_from_node(ofw_bus_get_node(dev));
159 	sc->gic_pic = intr_pic_register(dev, xref);
160 	if (sc->gic_pic == NULL) {
161 		device_printf(dev, "could not register PIC\n");
162 		err = ENXIO;
163 		goto error;
164 	}
165 
166 	if (sc->gic_mbi_start > 0)
167 		intr_msi_register(dev, xref);
168 
169 	/* Register xref */
170 	OF_device_register_xref(xref, dev);
171 
172 	if (intr_pic_claim_root(dev, xref, arm_gic_v3_intr, sc,
173 	    GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
174 		err = ENXIO;
175 		goto error;
176 	}
177 
178 	/*
179 	 * Try to register ITS to this GIC.
180 	 * GIC will act as a bus in that case.
181 	 * Failure here will not affect main GIC functionality.
182 	 */
183 	if (gic_v3_ofw_bus_attach(dev) != 0) {
184 		if (bootverbose) {
185 			device_printf(dev,
186 			    "Failed to attach ITS to this GIC\n");
187 		}
188 	}
189 
190 	if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) != 0)
191 		sc->gic_nchildren = 0;
192 
193 	return (err);
194 
195 error:
196 	if (bootverbose) {
197 		device_printf(dev,
198 		    "Failed to attach. Error %d\n", err);
199 	}
200 	/* Failure so free resources */
201 	gic_v3_detach(dev);
202 
203 	return (err);
204 }
205 
206 /* OFW bus interface */
207 struct gic_v3_ofw_devinfo {
208 	struct gic_v3_devinfo	di_gic_dinfo;
209 	struct ofw_bus_devinfo	di_dinfo;
210 	struct resource_list	di_rl;
211 };
212 
213 static int
214 gic_v3_fdt_print_child(device_t bus, device_t child)
215 {
216 	struct gic_v3_ofw_devinfo *di = device_get_ivars(child);
217 	struct resource_list *rl = &di->di_rl;
218 	int retval = 0;
219 
220 	retval += bus_print_child_header(bus, child);
221 	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
222 	retval += bus_print_child_footer(bus, child);
223 
224 	return (retval);
225 }
226 
227 static const struct ofw_bus_devinfo *
228 gic_v3_ofw_get_devinfo(device_t bus __unused, device_t child)
229 {
230 	struct gic_v3_ofw_devinfo *di;
231 
232 	di = device_get_ivars(child);
233 	return (&di->di_dinfo);
234 }
235 
236 static struct resource *
237 gic_v3_ofw_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
238     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
239 {
240 	struct gic_v3_ofw_devinfo *di;
241 	struct resource_list_entry *rle;
242 	int ranges_len;
243 
244 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
245 		if ((di = device_get_ivars(child)) == NULL)
246 			return (NULL);
247 		if (type != SYS_RES_MEMORY)
248 			return (NULL);
249 
250 		/* Find defaults for this rid */
251 		rle = resource_list_find(&di->di_rl, type, *rid);
252 		if (rle == NULL)
253 			return (NULL);
254 
255 		start = rle->start;
256 		end = rle->end;
257 		count = rle->count;
258 	}
259 	/*
260 	 * XXX: No ranges remap!
261 	 *	Absolute address is expected.
262 	 */
263 	if (ofw_bus_has_prop(bus, "ranges")) {
264 		ranges_len = OF_getproplen(ofw_bus_get_node(bus), "ranges");
265 		if (ranges_len != 0) {
266 			if (bootverbose) {
267 				device_printf(child,
268 				    "Ranges remap not supported\n");
269 			}
270 			return (NULL);
271 		}
272 	}
273 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
274 	    count, flags));
275 }
276 
277 /* Helper functions */
278 
279 /*
280  * Bus capability support for GICv3.
281  * Collects and configures device informations and finally
282  * adds ITS device as a child of GICv3 in Newbus hierarchy.
283  */
284 static int
285 gic_v3_ofw_bus_attach(device_t dev)
286 {
287 	struct gic_v3_ofw_devinfo *di;
288 	struct gic_v3_softc *sc;
289 	device_t child;
290 	phandle_t parent, node;
291 	pcell_t addr_cells, size_cells;
292 
293 	sc = device_get_softc(dev);
294 	parent = ofw_bus_get_node(dev);
295 	if (parent > 0) {
296 		addr_cells = 2;
297 		OF_getencprop(parent, "#address-cells", &addr_cells,
298 		    sizeof(addr_cells));
299 		size_cells = 2;
300 		OF_getencprop(parent, "#size-cells", &size_cells,
301 		    sizeof(size_cells));
302 		/* Iterate through all GIC subordinates */
303 		for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
304 			/* Allocate and populate devinfo. */
305 			di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
306 
307 			/* Read the numa node, or -1 if there is none */
308 			if (OF_getencprop(node, "numa-node-id",
309 			    &di->di_gic_dinfo.gic_domain,
310 			    sizeof(di->di_gic_dinfo.gic_domain)) <= 0) {
311 				di->di_gic_dinfo.gic_domain = -1;
312 			}
313 
314 			if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node)) {
315 				if (bootverbose) {
316 					device_printf(dev,
317 					    "Could not set up devinfo for ITS\n");
318 				}
319 				free(di, M_GIC_V3);
320 				continue;
321 			}
322 
323 			/* Initialize and populate resource list. */
324 			resource_list_init(&di->di_rl);
325 			ofw_bus_reg_to_rl(dev, node, addr_cells, size_cells,
326 			    &di->di_rl);
327 
328 			/* Should not have any interrupts, so don't add any */
329 
330 			/* Add newbus device for this FDT node */
331 			child = device_add_child(dev, NULL, -1);
332 			if (!child) {
333 				if (bootverbose) {
334 					device_printf(dev,
335 					    "Could not add child: %s\n",
336 					    di->di_dinfo.obd_name);
337 				}
338 				resource_list_free(&di->di_rl);
339 				ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
340 				free(di, M_GIC_V3);
341 				continue;
342 			}
343 
344 			sc->gic_nchildren++;
345 			device_set_ivars(child, di);
346 		}
347 	}
348 
349 	return (bus_generic_attach(dev));
350 }
351