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