xref: /freebsd/sys/arm64/arm64/gic_v3_acpi.c (revision 716fd348)
1 /*-
2  * Copyright (c) 2016 The FreeBSD Foundation
3  *
4  * This software was developed by Andrew Turner 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 "opt_acpi.h"
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 
42 #include <machine/intr.h>
43 #include <machine/resource.h>
44 
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <dev/acpica/acpivar.h>
47 
48 #include "gic_v3_reg.h"
49 #include "gic_v3_var.h"
50 
51 struct gic_v3_acpi_devinfo {
52 	struct gic_v3_devinfo	di_gic_dinfo;
53 	struct resource_list	di_rl;
54 };
55 
56 static device_identify_t gic_v3_acpi_identify;
57 static device_probe_t gic_v3_acpi_probe;
58 static device_attach_t gic_v3_acpi_attach;
59 static bus_alloc_resource_t gic_v3_acpi_bus_alloc_res;
60 
61 static void gic_v3_acpi_bus_attach(device_t);
62 
63 static device_method_t gic_v3_acpi_methods[] = {
64 	/* Device interface */
65 	DEVMETHOD(device_identify,		gic_v3_acpi_identify),
66 	DEVMETHOD(device_probe,			gic_v3_acpi_probe),
67 	DEVMETHOD(device_attach,		gic_v3_acpi_attach),
68 
69 	/* Bus interface */
70 	DEVMETHOD(bus_alloc_resource,		gic_v3_acpi_bus_alloc_res),
71 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
72 
73 	/* End */
74 	DEVMETHOD_END
75 };
76 
77 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
78     sizeof(struct gic_v3_softc), gic_v3_driver);
79 
80 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, 0, 0,
81     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
82 
83 struct madt_table_data {
84 	device_t parent;
85 	device_t dev;
86 	ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
87 	int count;
88 	bool rdist_use_gicc;
89 };
90 
91 static void
92 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
93 {
94 	struct madt_table_data *madt_data;
95 
96 	madt_data = (struct madt_table_data *)arg;
97 
98 	switch(entry->Type) {
99 	case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
100 		if (madt_data->dist != NULL) {
101 			if (bootverbose)
102 				device_printf(madt_data->parent,
103 				    "gic: Already have a distributor table");
104 			break;
105 		}
106 		madt_data->dist = (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
107 		break;
108 
109 	case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
110 		break;
111 
112 	default:
113 		break;
114 	}
115 }
116 
117 static void
118 rdist_map(ACPI_SUBTABLE_HEADER *entry, void *arg)
119 {
120 	ACPI_MADT_GENERIC_REDISTRIBUTOR *redist;
121 	ACPI_MADT_GENERIC_INTERRUPT *intr;
122 	struct madt_table_data *madt_data;
123 	rman_res_t count;
124 
125 	madt_data = (struct madt_table_data *)arg;
126 
127 	switch(entry->Type) {
128 	case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
129 		if (madt_data->rdist_use_gicc)
130 			break;
131 		redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
132 
133 		madt_data->count++;
134 		BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
135 		    SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
136 		    redist->Length);
137 		break;
138 
139 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
140 		if (!madt_data->rdist_use_gicc)
141 			break;
142 
143 		intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
144 
145 		madt_data->count++;
146 		/*
147 		 * Map the two 64k redistributor frames.
148 		 */
149 		count = GICR_RD_BASE_SIZE + GICR_SGI_BASE_SIZE;
150 		if (madt_data->dist->Version == ACPI_MADT_GIC_VERSION_V4)
151 			count += GICR_VLPI_BASE_SIZE + GICR_RESERVED_SIZE;
152 		BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
153 		    SYS_RES_MEMORY, madt_data->count, intr->GicrBaseAddress,
154 		    count);
155 
156 	default:
157 		break;
158 	}
159 }
160 
161 static void
162 gic_v3_acpi_identify(driver_t *driver, device_t parent)
163 {
164 	struct madt_table_data madt_data;
165 	ACPI_TABLE_MADT *madt;
166 	vm_paddr_t physaddr;
167 	device_t dev;
168 
169 	physaddr = acpi_find_table(ACPI_SIG_MADT);
170 	if (physaddr == 0)
171 		return;
172 
173 	madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
174 	if (madt == NULL) {
175 		device_printf(parent, "gic: Unable to map the MADT\n");
176 		return;
177 	}
178 
179 	madt_data.parent = parent;
180 	madt_data.dist = NULL;
181 	madt_data.count = 0;
182 
183 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
184 	    madt_handler, &madt_data);
185 	if (madt_data.dist == NULL) {
186 		device_printf(parent,
187 		    "No gic interrupt or distributor table\n");
188 		goto out;
189 	}
190 
191 	/* Check the GIC version is supported by thiss driver */
192 	switch(madt_data.dist->Version) {
193 	case ACPI_MADT_GIC_VERSION_V3:
194 	case ACPI_MADT_GIC_VERSION_V4:
195 		break;
196 	default:
197 		goto out;
198 	}
199 
200 	dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
201 	    "gic", -1);
202 	if (dev == NULL) {
203 		device_printf(parent, "add gic child failed\n");
204 		goto out;
205 	}
206 
207 	/* Add the MADT data */
208 	BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
209 	    madt_data.dist->BaseAddress, 128 * 1024);
210 
211 	madt_data.dev = dev;
212 	madt_data.rdist_use_gicc = false;
213 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
214 	    rdist_map, &madt_data);
215 	if (madt_data.count == 0) {
216 		/*
217 		 * No redistributors found, fall back to use the GICR
218 		 * address from the GICC sub-table.
219 		 */
220 		madt_data.rdist_use_gicc = true;
221 		acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
222 		    rdist_map, &madt_data);
223 	}
224 
225 	acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
226 
227 out:
228 	acpi_unmap_table(madt);
229 }
230 
231 static int
232 gic_v3_acpi_probe(device_t dev)
233 {
234 
235 	switch((uintptr_t)acpi_get_private(dev)) {
236 	case ACPI_MADT_GIC_VERSION_V3:
237 	case ACPI_MADT_GIC_VERSION_V4:
238 		break;
239 	default:
240 		return (ENXIO);
241 	}
242 
243 	device_set_desc(dev, GIC_V3_DEVSTR);
244 	return (BUS_PROBE_NOWILDCARD);
245 }
246 
247 static void
248 madt_count_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
249 {
250 	struct gic_v3_softc *sc = arg;
251 
252 	if (entry->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
253 		sc->gic_redists.nregions++;
254 }
255 
256 static void
257 madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
258 {
259 	struct gic_v3_softc *sc = arg;
260 
261 	if (entry->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
262 		sc->gic_redists.nregions++;
263 }
264 
265 static int
266 gic_v3_acpi_count_regions(device_t dev)
267 {
268 	struct gic_v3_softc *sc;
269 	ACPI_TABLE_MADT *madt;
270 	vm_paddr_t physaddr;
271 
272 	sc = device_get_softc(dev);
273 
274 	physaddr = acpi_find_table(ACPI_SIG_MADT);
275 	if (physaddr == 0)
276 		return (ENXIO);
277 
278 	madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
279 	if (madt == NULL) {
280 		device_printf(dev, "Unable to map the MADT\n");
281 		return (ENXIO);
282 	}
283 
284 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
285 	    madt_count_redistrib, sc);
286 	/* Fall back to use the distributor GICR base address */
287 	if (sc->gic_redists.nregions == 0) {
288 		acpi_walk_subtables(madt + 1,
289 		    (char *)madt + madt->Header.Length,
290 		    madt_count_gicc_redistrib, sc);
291 	}
292 	acpi_unmap_table(madt);
293 
294 	return (sc->gic_redists.nregions > 0 ? 0 : ENXIO);
295 }
296 
297 static int
298 gic_v3_acpi_attach(device_t dev)
299 {
300 	struct gic_v3_softc *sc;
301 	int err;
302 
303 	sc = device_get_softc(dev);
304 	sc->dev = dev;
305 	sc->gic_bus = GIC_BUS_ACPI;
306 
307 	err = gic_v3_acpi_count_regions(dev);
308 	if (err != 0)
309 		goto count_error;
310 
311 	err = gic_v3_attach(dev);
312 	if (err != 0)
313 		goto error;
314 
315 	sc->gic_pic = intr_pic_register(dev, ACPI_INTR_XREF);
316 	if (sc->gic_pic == NULL) {
317 		device_printf(dev, "could not register PIC\n");
318 		err = ENXIO;
319 		goto error;
320 	}
321 
322 	if (intr_pic_claim_root(dev, ACPI_INTR_XREF, arm_gic_v3_intr, sc,
323 	    GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
324 		err = ENXIO;
325 		goto error;
326 	}
327 
328 	/*
329 	 * Try to register the ITS driver to this GIC. The GIC will act as
330 	 * a bus in that case. Failure here will not affect the main GIC
331 	 * functionality.
332 	 */
333 	gic_v3_acpi_bus_attach(dev);
334 
335 	if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) !=0)
336 		sc->gic_nchildren = 0;
337 
338 	return (0);
339 
340 error:
341 	/* Failure so free resources */
342 	gic_v3_detach(dev);
343 count_error:
344 	if (bootverbose) {
345 		device_printf(dev,
346 		    "Failed to attach. Error %d\n", err);
347 	}
348 
349 	return (err);
350 }
351 
352 static void
353 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
354 {
355 	ACPI_MADT_GENERIC_TRANSLATOR *gict;
356 	struct gic_v3_acpi_devinfo *di;
357 	struct gic_v3_softc *sc;
358 	device_t child, dev;
359 	u_int xref;
360 	int err, pxm;
361 
362 	if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
363 		/* We have an ITS, add it as a child */
364 		gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
365 		dev = arg;
366 		sc = device_get_softc(dev);
367 
368 		child = device_add_child(dev, "its", -1);
369 		if (child == NULL)
370 			return;
371 
372 		di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
373 		resource_list_init(&di->di_rl);
374 		resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
375 		    gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
376 		    128 * 1024);
377 		err = acpi_iort_its_lookup(gict->TranslationId, &xref, &pxm);
378 		if (err == 0) {
379 			di->di_gic_dinfo.gic_domain = pxm;
380 			di->di_gic_dinfo.msi_xref = xref;
381 		} else {
382 			di->di_gic_dinfo.gic_domain = -1;
383 			di->di_gic_dinfo.msi_xref = ACPI_MSI_XREF;
384 		}
385 		sc->gic_nchildren++;
386 		device_set_ivars(child, di);
387 	}
388 }
389 
390 static void
391 gic_v3_acpi_bus_attach(device_t dev)
392 {
393 	ACPI_TABLE_MADT *madt;
394 	vm_paddr_t physaddr;
395 
396 	physaddr = acpi_find_table(ACPI_SIG_MADT);
397 	if (physaddr == 0)
398 		return;
399 
400 	madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
401 	if (madt == NULL) {
402 		device_printf(dev, "Unable to map the MADT to add children\n");
403 		return;
404 	}
405 
406 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
407 	    gic_v3_add_children, dev);
408 
409 	acpi_unmap_table(madt);
410 
411 	bus_generic_attach(dev);
412 }
413 
414 static struct resource *
415 gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
416     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
417 {
418 	struct gic_v3_acpi_devinfo *di;
419 	struct resource_list_entry *rle;
420 
421 	/* We only allocate memory */
422 	if (type != SYS_RES_MEMORY)
423 		return (NULL);
424 
425 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
426 		if ((di = device_get_ivars(child)) == NULL)
427 			return (NULL);
428 
429 		/* Find defaults for this rid */
430 		rle = resource_list_find(&di->di_rl, type, *rid);
431 		if (rle == NULL)
432 			return (NULL);
433 
434 		start = rle->start;
435 		end = rle->end;
436 		count = rle->count;
437 	}
438 
439 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
440 	    count, flags));
441 }
442