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