]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic_acpi.c
Add the missing header for malloc(9). It was pulled in through header
[FreeBSD/FreeBSD.git] / sys / arm / arm / gic_acpi.c
1 /*-
2  * Copyright (c) 2011,2016 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Andrew Turner under
6  * sponsorship from the FreeBSD Foundation.
7  *
8  * Developed by Damjan Marion <damjan.marion@gmail.com>
9  *
10  * Based on OMAP4 GIC code by Ben Gray
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the company nor the name of the author may be used to
21  *    endorse or promote products derived from this software without specific
22  *    prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include "opt_platform.h"
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48
49 #include <machine/intr.h>
50
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <dev/acpica/acpivar.h>
53
54 #include <arm/arm/gic.h>
55 #include <arm/arm/gic_common.h>
56
57 struct gic_acpi_devinfo {
58         struct resource_list    rl;
59 };
60
61 static device_identify_t gic_acpi_identify;
62 static device_probe_t gic_acpi_probe;
63 static device_attach_t gic_acpi_attach;
64 static bus_get_resource_list_t gic_acpi_get_resource_list;
65 static bool arm_gic_add_children(device_t);
66
67 static device_method_t gic_acpi_methods[] = {
68         /* Device interface */
69         DEVMETHOD(device_identify,      gic_acpi_identify),
70         DEVMETHOD(device_probe,         gic_acpi_probe),
71         DEVMETHOD(device_attach,        gic_acpi_attach),
72
73         /* Bus interface */
74         DEVMETHOD(bus_get_resource_list, gic_acpi_get_resource_list),
75
76         DEVMETHOD_END,
77 };
78
79 DEFINE_CLASS_1(gic, gic_acpi_driver, gic_acpi_methods,
80     sizeof(struct arm_gic_softc), arm_gic_driver);
81
82 static devclass_t gic_acpi_devclass;
83
84 EARLY_DRIVER_MODULE(gic, acpi, gic_acpi_driver, gic_acpi_devclass, 0, 0,
85     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
86
87 struct madt_table_data {
88         device_t parent;
89         ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
90         ACPI_MADT_GENERIC_INTERRUPT *intr[MAXCPU];
91 };
92
93 static void
94 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
95 {
96         struct madt_table_data *madt_data;
97         ACPI_MADT_GENERIC_INTERRUPT *intr;
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                 } else
108                         madt_data->dist =
109                             (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
110                 break;
111         case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
112                 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
113                 if (intr->CpuInterfaceNumber < MAXCPU)
114                         madt_data->intr[intr->CpuInterfaceNumber] = intr;
115                 break;
116         }
117 }
118
119 static void
120 gic_acpi_identify(driver_t *driver, device_t parent)
121 {
122         struct madt_table_data madt_data;
123         ACPI_MADT_GENERIC_INTERRUPT *intr;
124         ACPI_TABLE_MADT *madt;
125         vm_paddr_t physaddr;
126         device_t dev;
127         int i;
128
129         physaddr = acpi_find_table(ACPI_SIG_MADT);
130         if (physaddr == 0)
131                 return;
132
133         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
134         if (madt == NULL) {
135                 device_printf(parent, "gic: Unable to map the MADT\n");
136                 return;
137         }
138
139         bzero(&madt_data, sizeof(madt_data));
140         madt_data.parent = parent;
141         madt_data.dist = NULL;
142
143         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
144             madt_handler, &madt_data);
145
146         /* Check the version of the GIC we have */
147         switch (madt_data.dist->Version) {
148         case ACPI_MADT_GIC_VERSION_NONE:
149         case ACPI_MADT_GIC_VERSION_V1:
150         case ACPI_MADT_GIC_VERSION_V2:
151                 break;
152         default:
153                 goto out;
154         }
155
156         intr = NULL;
157         for (i = 0; i < MAXCPU; i++) {
158                 if (madt_data.intr[i] != NULL) {
159                         if (intr == NULL) {
160                                 intr = madt_data.intr[i];
161                         } else if (intr->BaseAddress !=
162                             madt_data.intr[i]->BaseAddress) {
163                                 device_printf(parent,
164 "gic: Not all CPU interfaces at the same address, this may fail\n");
165                         }
166                 }
167         }
168         if (intr == NULL) {
169                 device_printf(parent, "gic: No CPU interfaces found\n");
170                 goto out;
171         }
172
173         dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
174             "gic", -1);
175         if (dev == NULL) {
176                 device_printf(parent, "add gic child failed\n");
177                 goto out;
178         }
179
180         BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
181             madt_data.dist->BaseAddress, 4 * 1024);
182         BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 1,
183             intr->BaseAddress, 4 * 1024);
184
185         acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
186 out:
187         acpi_unmap_table(madt);
188 }
189
190 static int
191 gic_acpi_probe(device_t dev)
192 {
193
194         switch((uintptr_t)acpi_get_private(dev)) {
195         case ACPI_MADT_GIC_VERSION_NONE:
196         case ACPI_MADT_GIC_VERSION_V1:
197         case ACPI_MADT_GIC_VERSION_V2:
198                 break;
199         default:
200                 return (ENXIO);
201         }
202
203         device_set_desc(dev, "ARM Generic Interrupt Controller");
204         return (BUS_PROBE_NOWILDCARD);
205 }
206
207 static int
208 gic_acpi_attach(device_t dev)
209 {
210         struct arm_gic_softc *sc = device_get_softc(dev);
211         intptr_t xref;
212         int err;
213
214         sc->gic_bus = GIC_BUS_ACPI;
215
216         err = arm_gic_attach(dev);
217         if (err != 0)
218                 return (err);
219
220         xref = 0;
221
222         /*
223          * Now, when everything is initialized, it's right time to
224          * register interrupt controller to interrupt framefork.
225          */
226         if (intr_pic_register(dev, xref) == NULL) {
227                 device_printf(dev, "could not register PIC\n");
228                 goto cleanup;
229         }
230
231         /*
232          * Controller is root:
233          */
234         if (intr_pic_claim_root(dev, xref, arm_gic_intr, sc,
235             GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
236                 device_printf(dev, "could not set PIC as a root\n");
237                 intr_pic_deregister(dev, xref);
238                 goto cleanup;
239         }
240         /* If we have children probe and attach them */
241         if (arm_gic_add_children(dev)) {
242                 bus_generic_probe(dev);
243                 return (bus_generic_attach(dev));
244         }
245
246         return (0);
247
248 cleanup:
249         arm_gic_detach(dev);
250         return(ENXIO);
251 }
252
253 static struct resource_list *
254 gic_acpi_get_resource_list(device_t bus, device_t child)
255 {
256         struct gic_acpi_devinfo *di;
257
258         di = device_get_ivars(child);
259         KASSERT(di != NULL, ("gic_acpi_get_resource_list: No devinfo"));
260
261         return (&di->rl);
262 }
263
264 static void
265 madt_gicv2m_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
266 {
267         struct arm_gic_softc *sc;
268         ACPI_MADT_GENERIC_MSI_FRAME *msi;
269         struct gic_acpi_devinfo *dinfo;
270         device_t dev, cdev;
271
272         if (entry->Type == ACPI_MADT_TYPE_GENERIC_MSI_FRAME) {
273                 sc = arg;
274                 dev = sc->gic_dev;
275                 msi = (ACPI_MADT_GENERIC_MSI_FRAME *)entry;
276
277                 device_printf(dev, "frame: %x %lx %x %u %u\n", msi->MsiFrameId,
278                     msi->BaseAddress, msi->Flags, msi->SpiCount, msi->SpiBase);
279
280                 cdev = device_add_child(dev, NULL, -1);
281                 if (cdev == NULL)
282                         return;
283
284                 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
285                 resource_list_init(&dinfo->rl);
286                 resource_list_add(&dinfo->rl, SYS_RES_MEMORY, 0,
287                     msi->BaseAddress, msi->BaseAddress + PAGE_SIZE - 1,
288                     PAGE_SIZE);
289                 device_set_ivars(cdev, dinfo);
290         }
291 }
292
293 static bool
294 arm_gic_add_children(device_t dev)
295 {
296         struct arm_gic_softc *sc = device_get_softc(dev);
297         ACPI_TABLE_MADT *madt;
298         vm_paddr_t physaddr;
299
300         /* This should return a valid address as it did in gic_acpi_identify */
301         physaddr = acpi_find_table(ACPI_SIG_MADT);
302         if (physaddr == 0)
303                 return (false);
304
305         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
306         if (madt == NULL) {
307                 device_printf(dev, "gic: Unable to map the MADT\n");
308                 return (false);
309         }
310
311         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
312             madt_gicv2m_handler, sc);
313
314         acpi_unmap_table(madt);
315
316         return (true);
317 }
318
319 static int
320 arm_gicv2m_acpi_probe(device_t dev)
321 {
322
323         if (gic_get_bus(dev) != GIC_BUS_ACPI)
324                 return (EINVAL);
325
326         if (gic_get_hw_rev(dev) > 2)
327                 return (EINVAL);
328
329         device_set_desc(dev, "ARM Generic Interrupt Controller MSI/MSIX");
330         return (BUS_PROBE_DEFAULT);
331 }
332
333 static device_method_t arm_gicv2m_acpi_methods[] = {
334         /* Device interface */
335         DEVMETHOD(device_probe,         arm_gicv2m_acpi_probe),
336
337         /* End */
338         DEVMETHOD_END
339 };
340
341 DEFINE_CLASS_1(gicv2m, arm_gicv2m_acpi_driver, arm_gicv2m_acpi_methods,
342     sizeof(struct arm_gicv2m_softc), arm_gicv2m_driver);
343
344 static devclass_t arm_gicv2m_acpi_devclass;
345
346 EARLY_DRIVER_MODULE(gicv2m_acpi, gic, arm_gicv2m_acpi_driver,
347     arm_gicv2m_acpi_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);