]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/gic_v3_acpi.c
MFV: r329072
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / gic_v3_acpi.c
1 /*-
2  * Copyright (c) 2016 The FreeBSD Foundation
3  * All rights reserved.
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 <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40
41 #include <machine/intr.h>
42 #include <machine/resource.h>
43
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <dev/acpica/acpivar.h>
46
47 #include "gic_v3_reg.h"
48 #include "gic_v3_var.h"
49
50 struct gic_v3_acpi_devinfo {
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
59 static void gic_v3_acpi_bus_attach(device_t);
60
61 static device_method_t gic_v3_acpi_methods[] = {
62         /* Device interface */
63         DEVMETHOD(device_identify,              gic_v3_acpi_identify),
64         DEVMETHOD(device_probe,                 gic_v3_acpi_probe),
65         DEVMETHOD(device_attach,                gic_v3_acpi_attach),
66
67         /* Bus interface */
68         DEVMETHOD(bus_alloc_resource,           gic_v3_acpi_bus_alloc_res),
69         DEVMETHOD(bus_activate_resource,        bus_generic_activate_resource),
70
71         /* End */
72         DEVMETHOD_END
73 };
74
75 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
76     sizeof(struct gic_v3_softc), gic_v3_driver);
77
78 static devclass_t gic_v3_acpi_devclass;
79
80 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, gic_v3_acpi_devclass,
81     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
82
83
84 struct madt_table_data {
85         device_t parent;
86         device_t dev;
87         ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
88         int count;
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         struct madt_table_data *madt_data;
122
123         madt_data = (struct madt_table_data *)arg;
124
125         switch(entry->Type) {
126         case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
127                 redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
128
129                 madt_data->count++;
130                 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
131                     SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
132                     redist->Length);
133                 break;
134
135         default:
136                 break;
137         }
138 }
139
140 static void
141 gic_v3_acpi_identify(driver_t *driver, device_t parent)
142 {
143         struct madt_table_data madt_data;
144         ACPI_TABLE_MADT *madt;
145         vm_paddr_t physaddr;
146         device_t dev;
147
148         physaddr = acpi_find_table(ACPI_SIG_MADT);
149         if (physaddr == 0)
150                 return;
151
152         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
153         if (madt == NULL) {
154                 device_printf(parent, "gic: Unable to map the MADT\n");
155                 return;
156         }
157
158         madt_data.parent = parent;
159         madt_data.dist = NULL;
160         madt_data.count = 0;
161
162         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
163             madt_handler, &madt_data);
164         if (madt_data.dist == NULL) {
165                 device_printf(parent,
166                     "No gic interrupt or distributor table\n");
167                 goto out;
168         }
169         /* This is for the wrong GIC version */
170         if (madt_data.dist->Version != ACPI_MADT_GIC_VERSION_V3)
171                 goto out;
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         /* Add the MADT data */
181         BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
182             madt_data.dist->BaseAddress, 128 * 1024);
183
184         madt_data.dev = dev;
185         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
186             rdist_map, &madt_data);
187
188         acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
189
190 out:
191         acpi_unmap_table(madt);
192 }
193
194 static int
195 gic_v3_acpi_probe(device_t dev)
196 {
197
198         switch((uintptr_t)acpi_get_private(dev)) {
199         case ACPI_MADT_GIC_VERSION_V3:
200                 break;
201         default:
202                 return (ENXIO);
203         }
204
205         device_set_desc(dev, GIC_V3_DEVSTR);
206         return (BUS_PROBE_NOWILDCARD);
207 }
208
209 static int
210 gic_v3_acpi_attach(device_t dev)
211 {
212         struct gic_v3_softc *sc;
213         int err;
214
215         sc = device_get_softc(dev);
216         sc->dev = dev;
217         sc->gic_bus = GIC_BUS_ACPI;
218
219         /* TODO: Count these correctly */
220         sc->gic_redists.nregions = 1;
221
222         err = gic_v3_attach(dev);
223         if (err != 0)
224                 goto error;
225
226         sc->gic_pic = intr_pic_register(dev, 0);
227         if (sc->gic_pic == NULL) {
228                 device_printf(dev, "could not register PIC\n");
229                 err = ENXIO;
230                 goto error;
231         }
232
233         if (intr_pic_claim_root(dev, 0, arm_gic_v3_intr, sc,
234             GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
235                 err = ENXIO;
236                 goto error;
237         }
238
239         /*
240          * Try to register the ITS driver to this GIC. The GIC will act as
241          * a bus in that case. Failure here will not affect the main GIC
242          * functionality.
243          */
244         gic_v3_acpi_bus_attach(dev);
245
246         return (0);
247
248 error:
249         if (bootverbose) {
250                 device_printf(dev,
251                     "Failed to attach. Error %d\n", err);
252         }
253         /* Failure so free resources */
254         gic_v3_detach(dev);
255
256         return (err);
257 }
258
259 static void
260 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
261 {
262         ACPI_MADT_GENERIC_TRANSLATOR *gict;
263         struct gic_v3_acpi_devinfo *di;
264         device_t child, dev;
265
266         if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
267                 /* We have an ITS, add it as a child */
268                 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
269                 dev = arg;
270
271                 child = device_add_child(dev, "its", -1);
272                 if (child == NULL)
273                         return;
274
275                 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
276                 resource_list_init(&di->di_rl);
277                 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
278                     gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
279                     128 * 1024);
280                 device_set_ivars(child, di);
281         }
282 }
283
284 static void
285 gic_v3_acpi_bus_attach(device_t dev)
286 {
287         ACPI_TABLE_MADT *madt;
288         vm_paddr_t physaddr;
289
290         physaddr = acpi_find_table(ACPI_SIG_MADT);
291         if (physaddr == 0)
292                 return;
293
294         madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
295         if (madt == NULL) {
296                 device_printf(dev, "Unable to map the MADT to add children\n");
297                 return;
298         }
299
300         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
301             gic_v3_add_children, dev);
302
303         acpi_unmap_table(madt);
304
305         bus_generic_attach(dev);
306 }
307
308 static struct resource *
309 gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
310     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
311 {
312         struct gic_v3_acpi_devinfo *di;
313         struct resource_list_entry *rle;
314
315         /* We only allocate memory */
316         if (type != SYS_RES_MEMORY)
317                 return (NULL);
318
319         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
320                 if ((di = device_get_ivars(child)) == NULL)
321                         return (NULL);
322
323                 /* Find defaults for this rid */
324                 rle = resource_list_find(&di->di_rl, type, *rid);
325                 if (rle == NULL)
326                         return (NULL);
327
328                 start = rle->start;
329                 end = rle->end;
330                 count = rle->count;
331         }
332
333         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
334             count, flags));
335 }