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 52struct gic_v3_acpi_devinfo { 53 struct gic_v3_devinfo di_gic_dinfo; 54 struct resource_list di_rl; 55}; 56 57static device_identify_t gic_v3_acpi_identify; 58static device_probe_t gic_v3_acpi_probe; 59static device_attach_t gic_v3_acpi_attach; 60static bus_alloc_resource_t gic_v3_acpi_bus_alloc_res; 61 62static void gic_v3_acpi_bus_attach(device_t); 63 64static 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 78DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods, 79 sizeof(struct gic_v3_softc), gic_v3_driver); 80 81static devclass_t gic_v3_acpi_devclass; 82 83EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, gic_v3_acpi_devclass, 84 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE); 85 86struct madt_table_data { 87 device_t parent; 88 device_t dev; 89 ACPI_MADT_GENERIC_DISTRIBUTOR *dist; 90 int count; 91 bool rdist_use_gicc; 92}; 93 94static void 95madt_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 120static void 121rdist_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 159 default: 160 break; 161 } 162} 163 164static void 165gic_v3_acpi_identify(driver_t *driver, device_t parent) 166{ 167 struct madt_table_data madt_data; 168 ACPI_TABLE_MADT *madt; 169 vm_paddr_t physaddr; 170 device_t dev; 171 172 physaddr = acpi_find_table(ACPI_SIG_MADT); 173 if (physaddr == 0) 174 return; 175 176 madt = acpi_map_table(physaddr, ACPI_SIG_MADT); 177 if (madt == NULL) { 178 device_printf(parent, "gic: Unable to map the MADT\n"); 179 return; 180 } 181 182 madt_data.parent = parent; 183 madt_data.dist = NULL; 184 madt_data.count = 0; 185 186 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 187 madt_handler, &madt_data); 188 if (madt_data.dist == NULL) { 189 device_printf(parent, 190 "No gic interrupt or distributor table\n"); 191 goto out; 192 } 193 194 /* Check the GIC version is supported by thiss driver */ 195 switch(madt_data.dist->Version) { 196 case ACPI_MADT_GIC_VERSION_V3: 197 case ACPI_MADT_GIC_VERSION_V4: 198 break; 199 default: 200 goto out; 201 } 202 203 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE, 204 "gic", -1); 205 if (dev == NULL) { 206 device_printf(parent, "add gic child failed\n"); 207 goto out; 208 } 209 210 /* Add the MADT data */ 211 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0, 212 madt_data.dist->BaseAddress, 128 * 1024); 213 214 madt_data.dev = dev; 215 madt_data.rdist_use_gicc = false; 216 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 217 rdist_map, &madt_data); 218 if (madt_data.count == 0) { 219 /* 220 * No redistributors found, fall back to use the GICR 221 * address from the GICC sub-table. 222 */ 223 madt_data.rdist_use_gicc = true; 224 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 225 rdist_map, &madt_data); 226 } 227 228 acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version); 229 230out: 231 acpi_unmap_table(madt); 232} 233 234static int 235gic_v3_acpi_probe(device_t dev) 236{ 237 238 switch((uintptr_t)acpi_get_private(dev)) { 239 case ACPI_MADT_GIC_VERSION_V3: 240 case ACPI_MADT_GIC_VERSION_V4: 241 break; 242 default: 243 return (ENXIO); 244 } 245 246 device_set_desc(dev, GIC_V3_DEVSTR); 247 return (BUS_PROBE_NOWILDCARD); 248} 249 250static void 251madt_count_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg) 252{ 253 struct gic_v3_softc *sc = arg; 254 255 if (entry->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR) 256 sc->gic_redists.nregions++; 257} 258 259static void 260madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg) 261{ 262 struct gic_v3_softc *sc = arg; 263 264 if (entry->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) 265 sc->gic_redists.nregions++; 266} 267 268static int 269gic_v3_acpi_count_regions(device_t dev) 270{ 271 struct gic_v3_softc *sc; 272 ACPI_TABLE_MADT *madt; 273 vm_paddr_t physaddr; 274 275 sc = device_get_softc(dev); 276 277 physaddr = acpi_find_table(ACPI_SIG_MADT); 278 if (physaddr == 0) 279 return (ENXIO); 280 281 madt = acpi_map_table(physaddr, ACPI_SIG_MADT); 282 if (madt == NULL) { 283 device_printf(dev, "Unable to map the MADT\n"); 284 return (ENXIO); 285 } 286 287 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 288 madt_count_redistrib, sc); 289 /* Fall back to use the distributor GICR base address */ 290 if (sc->gic_redists.nregions == 0) { 291 acpi_walk_subtables(madt + 1, 292 (char *)madt + madt->Header.Length, 293 madt_count_gicc_redistrib, sc); 294 } 295 acpi_unmap_table(madt); 296 297 return (sc->gic_redists.nregions > 0 ? 0 : ENXIO); 298} 299 300static int 301gic_v3_acpi_attach(device_t dev) 302{ 303 struct gic_v3_softc *sc; 304 int err; 305 306 sc = device_get_softc(dev); 307 sc->dev = dev; 308 sc->gic_bus = GIC_BUS_ACPI; 309 310 err = gic_v3_acpi_count_regions(dev); 311 if (err != 0) 312 goto count_error; 313 314 err = gic_v3_attach(dev); 315 if (err != 0) 316 goto error; 317 318 sc->gic_pic = intr_pic_register(dev, ACPI_INTR_XREF); 319 if (sc->gic_pic == NULL) { 320 device_printf(dev, "could not register PIC\n"); 321 err = ENXIO; 322 goto error; 323 } 324 325 if (intr_pic_claim_root(dev, ACPI_INTR_XREF, arm_gic_v3_intr, sc, 326 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) { 327 err = ENXIO; 328 goto error; 329 } 330 331 /* 332 * Try to register the ITS driver to this GIC. The GIC will act as 333 * a bus in that case. Failure here will not affect the main GIC 334 * functionality. 335 */ 336 gic_v3_acpi_bus_attach(dev); 337 338 if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) !=0) 339 sc->gic_nchildren = 0; 340 341 return (0); 342 343error: 344 /* Failure so free resources */ 345 gic_v3_detach(dev); 346count_error: 347 if (bootverbose) { 348 device_printf(dev, 349 "Failed to attach. Error %d\n", err); 350 } 351 352 return (err); 353} 354 355static void 356gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg) 357{ 358 ACPI_MADT_GENERIC_TRANSLATOR *gict; 359 struct gic_v3_acpi_devinfo *di; 360 struct gic_v3_softc *sc; 361 device_t child, dev; 362 u_int xref; 363 int err, pxm; 364 365 if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) { 366 /* We have an ITS, add it as a child */ 367 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry; 368 dev = arg; 369 sc = device_get_softc(dev); 370 371 child = device_add_child(dev, "its", -1); 372 if (child == NULL) 373 return; 374 375 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO); 376 resource_list_init(&di->di_rl); 377 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0, 378 gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1, 379 128 * 1024); 380 err = acpi_iort_its_lookup(gict->TranslationId, &xref, &pxm); 381 if (err == 0) { 382 di->di_gic_dinfo.gic_domain = pxm; 383 di->di_gic_dinfo.msi_xref = xref; 384 } else { 385 di->di_gic_dinfo.gic_domain = -1; 386 di->di_gic_dinfo.msi_xref = ACPI_MSI_XREF; 387 } 388 sc->gic_nchildren++; 389 device_set_ivars(child, di); 390 } 391} 392 393static void 394gic_v3_acpi_bus_attach(device_t dev) 395{ 396 ACPI_TABLE_MADT *madt; 397 vm_paddr_t physaddr; 398 399 physaddr = acpi_find_table(ACPI_SIG_MADT); 400 if (physaddr == 0) 401 return; 402 403 madt = acpi_map_table(physaddr, ACPI_SIG_MADT); 404 if (madt == NULL) { 405 device_printf(dev, "Unable to map the MADT to add children\n"); 406 return; 407 } 408 409 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 410 gic_v3_add_children, dev); 411 412 acpi_unmap_table(madt); 413 414 bus_generic_attach(dev); 415} 416 417static struct resource * 418gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid, 419 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 420{ 421 struct gic_v3_acpi_devinfo *di; 422 struct resource_list_entry *rle; 423 424 /* We only allocate memory */ 425 if (type != SYS_RES_MEMORY) 426 return (NULL); 427 428 if (RMAN_IS_DEFAULT_RANGE(start, end)) { 429 if ((di = device_get_ivars(child)) == NULL) 430 return (NULL); 431 432 /* Find defaults for this rid */ 433 rle = resource_list_find(&di->di_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