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