1 /*- 2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Abstract BHND Bridge Device Driver 35 * 36 * Provides generic support for bridging from a parent bus (such as PCI) to 37 * a BHND-compatible bus (e.g. bcma or siba). 38 */ 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/bus.h> 43 #include <sys/module.h> 44 #include <sys/systm.h> 45 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #include <machine/resource.h> 49 50 #include <dev/bhnd/bhndvar.h> 51 #include <dev/bhnd/bhndreg.h> 52 53 #include <dev/bhnd/cores/chipc/chipcreg.h> 54 #include <dev/bhnd/nvram/bhnd_nvram.h> 55 56 #include "bhnd_chipc_if.h" 57 #include "bhnd_nvram_if.h" 58 59 #include "bhndbvar.h" 60 #include "bhndb_bus_if.h" 61 #include "bhndb_hwdata.h" 62 #include "bhndb_private.h" 63 64 /* Debugging flags */ 65 static u_long bhndb_debug = 0; 66 TUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug); 67 68 enum { 69 BHNDB_DEBUG_PRIO = 1 << 0, 70 }; 71 72 #define BHNDB_DEBUG(_type) (BHNDB_DEBUG_ ## _type & bhndb_debug) 73 74 static bool bhndb_hw_matches(device_t *devlist, 75 int num_devs, 76 const struct bhndb_hw *hw); 77 78 static int bhndb_initialize_region_cfg( 79 struct bhndb_softc *sc, device_t *devs, 80 int ndevs, 81 const struct bhndb_hw_priority *table, 82 struct bhndb_resources *r); 83 84 static int bhndb_find_hwspec(struct bhndb_softc *sc, 85 device_t *devs, int ndevs, 86 const struct bhndb_hw **hw); 87 88 static int bhndb_read_chipid(struct bhndb_softc *sc, 89 const struct bhndb_hwcfg *cfg, 90 struct bhnd_chipid *result); 91 92 bhndb_addrspace bhndb_get_addrspace(struct bhndb_softc *sc, 93 device_t child); 94 95 static struct rman *bhndb_get_rman(struct bhndb_softc *sc, 96 device_t child, int type); 97 98 static int bhndb_init_child_resource(struct resource *r, 99 struct resource *parent, 100 bhnd_size_t offset, 101 bhnd_size_t size); 102 103 static int bhndb_activate_static_region( 104 struct bhndb_softc *sc, 105 struct bhndb_region *region, 106 device_t child, int type, int rid, 107 struct resource *r); 108 109 static int bhndb_try_activate_resource( 110 struct bhndb_softc *sc, device_t child, 111 int type, int rid, struct resource *r, 112 bool *indirect); 113 114 115 /** 116 * Default bhndb(4) implementation of DEVICE_PROBE(). 117 * 118 * This function provides the default bhndb implementation of DEVICE_PROBE(), 119 * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge(). 120 */ 121 int 122 bhndb_generic_probe(device_t dev) 123 { 124 return (BUS_PROBE_NOWILDCARD); 125 } 126 127 static void 128 bhndb_probe_nomatch(device_t dev, device_t child) 129 { 130 const char *name; 131 132 name = device_get_name(child); 133 if (name == NULL) 134 name = "unknown device"; 135 136 device_printf(dev, "<%s> (no driver attached)\n", name); 137 } 138 139 static int 140 bhndb_print_child(device_t dev, device_t child) 141 { 142 struct bhndb_softc *sc; 143 struct resource_list *rl; 144 int retval = 0; 145 146 sc = device_get_softc(dev); 147 148 retval += bus_print_child_header(dev, child); 149 150 rl = BUS_GET_RESOURCE_LIST(dev, child); 151 if (rl != NULL) { 152 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, 153 "%#jx"); 154 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, 155 "%jd"); 156 } 157 158 retval += bus_print_child_domain(dev, child); 159 retval += bus_print_child_footer(dev, child); 160 161 return (retval); 162 } 163 164 static int 165 bhndb_child_pnpinfo_str(device_t bus, device_t child, char *buf, 166 size_t buflen) 167 { 168 *buf = '\0'; 169 return (0); 170 } 171 172 static int 173 bhndb_child_location_str(device_t dev, device_t child, char *buf, 174 size_t buflen) 175 { 176 struct bhndb_softc *sc; 177 178 sc = device_get_softc(dev); 179 180 snprintf(buf, buflen, "base=0x%llx", 181 (unsigned long long) sc->chipid.enum_addr); 182 return (0); 183 } 184 185 /** 186 * Return true if @p devlist matches the @p hw specification. 187 * 188 * @param devlist A device table to match against. 189 * @param num_devs The number of devices in @p devlist. 190 * @param hw The hardware description to be matched against. 191 */ 192 static bool 193 bhndb_hw_matches(device_t *devlist, int num_devs, const struct bhndb_hw *hw) 194 { 195 for (u_int i = 0; i < hw->num_hw_reqs; i++) { 196 const struct bhnd_core_match *match; 197 bool found; 198 199 match = &hw->hw_reqs[i]; 200 found = false; 201 202 for (int d = 0; d < num_devs; d++) { 203 if (!bhnd_device_matches(devlist[d], match)) 204 continue; 205 206 found = true; 207 break; 208 } 209 210 if (!found) 211 return (false); 212 } 213 214 return (true); 215 } 216 217 /** 218 * Initialize the region maps and priority configuration in @p r using 219 * the provided priority @p table and the set of devices attached to 220 * the bridged @p bus_dev . 221 * 222 * @param sc The bhndb device state. 223 * @param devs All devices enumerated on the bridged bhnd bus. 224 * @param ndevs The length of @p devs. 225 * @param table Hardware priority table to be used to determine the relative 226 * priorities of per-core port resources. 227 * @param r The resource state to be configured. 228 */ 229 static int 230 bhndb_initialize_region_cfg(struct bhndb_softc *sc, device_t *devs, int ndevs, 231 const struct bhndb_hw_priority *table, struct bhndb_resources *r) 232 { 233 const struct bhndb_hw_priority *hp; 234 bhnd_addr_t addr; 235 bhnd_size_t size; 236 size_t prio_low, prio_default, prio_high; 237 int error; 238 239 /* The number of port regions per priority band that must be accessible 240 * via dynamic register windows */ 241 prio_low = 0; 242 prio_default = 0; 243 prio_high = 0; 244 245 /* 246 * Register bridge regions covering all statically mapped ports. 247 */ 248 for (int i = 0; i < ndevs; i++) { 249 const struct bhndb_regwin *regw; 250 device_t child; 251 252 child = devs[i]; 253 254 for (regw = r->cfg->register_windows; 255 regw->win_type != BHNDB_REGWIN_T_INVALID; regw++) 256 { 257 /* Only core windows are supported */ 258 if (regw->win_type != BHNDB_REGWIN_T_CORE) 259 continue; 260 261 /* Skip non-applicable register windows. */ 262 if (!bhndb_regwin_matches_device(regw, child)) 263 continue; 264 265 /* Fetch the base address of the mapped port. */ 266 error = bhnd_get_region_addr(child, 267 regw->d.core.port_type, regw->d.core.port, 268 regw->d.core.region, &addr, &size); 269 if (error) 270 return (error); 271 272 /* 273 * Always defer to the register window's size. 274 * 275 * If the port size is smaller than the window size, 276 * this ensures that we fully utilize register windows 277 * larger than the referenced port. 278 * 279 * If the port size is larger than the window size, this 280 * ensures that we do not directly map the allocations 281 * within the region to a too-small window. 282 */ 283 size = regw->win_size; 284 285 /* 286 * Add to the bus region list. 287 * 288 * The window priority for a statically mapped 289 * region is always HIGH. 290 */ 291 error = bhndb_add_resource_region(r, addr, size, 292 BHNDB_PRIORITY_HIGH, regw); 293 if (error) 294 return (error); 295 } 296 } 297 298 /* 299 * Perform priority accounting and register bridge regions for all 300 * ports defined in the priority table 301 */ 302 for (int i = 0; i < ndevs; i++) { 303 struct bhndb_region *region; 304 device_t child; 305 306 child = devs[i]; 307 308 /* 309 * Skip priority accounting for cores that ... 310 */ 311 312 /* ... do not require bridge resources */ 313 if (bhnd_is_hw_disabled(child) || !device_is_enabled(child)) 314 continue; 315 316 /* ... do not have a priority table entry */ 317 hp = bhndb_hw_priority_find_device(table, child); 318 if (hp == NULL) 319 continue; 320 321 /* ... are explicitly disabled in the priority table. */ 322 if (hp->priority == BHNDB_PRIORITY_NONE) 323 continue; 324 325 /* Determine the number of dynamic windows required and 326 * register their bus_region entries. */ 327 for (u_int i = 0; i < hp->num_ports; i++) { 328 const struct bhndb_port_priority *pp; 329 330 pp = &hp->ports[i]; 331 332 /* Skip ports not defined on this device */ 333 if (!bhnd_is_region_valid(child, pp->type, pp->port, 334 pp->region)) 335 { 336 continue; 337 } 338 339 /* Fetch the address+size of the mapped port. */ 340 error = bhnd_get_region_addr(child, pp->type, pp->port, 341 pp->region, &addr, &size); 342 if (error) 343 return (error); 344 345 /* Skip ports with an existing static mapping */ 346 region = bhndb_find_resource_region(r, addr, size); 347 if (region != NULL && region->static_regwin != NULL) 348 continue; 349 350 /* Define a dynamic region for this port */ 351 error = bhndb_add_resource_region(r, addr, size, 352 pp->priority, NULL); 353 if (error) 354 return (error); 355 356 /* Update port mapping counts */ 357 switch (pp->priority) { 358 case BHNDB_PRIORITY_NONE: 359 break; 360 case BHNDB_PRIORITY_LOW: 361 prio_low++; 362 break; 363 case BHNDB_PRIORITY_DEFAULT: 364 prio_default++; 365 break; 366 case BHNDB_PRIORITY_HIGH: 367 prio_high++; 368 break; 369 } 370 } 371 } 372 373 /* Determine the minimum priority at which we'll allocate direct 374 * register windows from our dynamic pool */ 375 size_t prio_total = prio_low + prio_default + prio_high; 376 if (prio_total <= r->dwa_count) { 377 /* low+default+high priority regions get windows */ 378 r->min_prio = BHNDB_PRIORITY_LOW; 379 380 } else if (prio_default + prio_high <= r->dwa_count) { 381 /* default+high priority regions get windows */ 382 r->min_prio = BHNDB_PRIORITY_DEFAULT; 383 384 } else { 385 /* high priority regions get windows */ 386 r->min_prio = BHNDB_PRIORITY_HIGH; 387 } 388 389 if (BHNDB_DEBUG(PRIO)) { 390 struct bhndb_region *region; 391 const char *direct_msg, *type_msg; 392 bhndb_priority_t prio, prio_min; 393 394 prio_min = r->min_prio; 395 device_printf(sc->dev, "min_prio: %d\n", prio_min); 396 397 STAILQ_FOREACH(region, &r->bus_regions, link) { 398 prio = region->priority; 399 400 direct_msg = prio >= prio_min ? "direct" : "indirect"; 401 type_msg = region->static_regwin ? "static" : "dynamic"; 402 403 device_printf(sc->dev, "region 0x%llx+0x%llx priority " 404 "%u %s/%s\n", 405 (unsigned long long) region->addr, 406 (unsigned long long) region->size, 407 region->priority, 408 direct_msg, type_msg); 409 } 410 } 411 412 return (0); 413 } 414 415 /** 416 * Find a hardware specification for @p dev. 417 * 418 * @param sc The bhndb device state. 419 * @param devs All devices enumerated on the bridged bhnd bus. 420 * @param ndevs The length of @p devs. 421 * @param[out] hw On success, the matched hardware specification. 422 * with @p dev. 423 * 424 * @retval 0 success 425 * @retval non-zero if an error occurs fetching device info for comparison. 426 */ 427 static int 428 bhndb_find_hwspec(struct bhndb_softc *sc, device_t *devs, int ndevs, 429 const struct bhndb_hw **hw) 430 { 431 const struct bhndb_hw *next, *hw_table; 432 433 /* Search for the first matching hardware config. */ 434 hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev); 435 for (next = hw_table; next->hw_reqs != NULL; next++) { 436 if (!bhndb_hw_matches(devs, ndevs, next)) 437 continue; 438 439 /* Found */ 440 *hw = next; 441 return (0); 442 } 443 444 return (ENOENT); 445 } 446 447 /** 448 * Read the ChipCommon identification data for this device. 449 * 450 * @param sc bhndb device state. 451 * @param cfg The hardware configuration to use when mapping the ChipCommon 452 * registers. 453 * @param[out] result the chip identification data. 454 * 455 * @retval 0 success 456 * @retval non-zero if the ChipCommon identification data could not be read. 457 */ 458 static int 459 bhndb_read_chipid(struct bhndb_softc *sc, const struct bhndb_hwcfg *cfg, 460 struct bhnd_chipid *result) 461 { 462 const struct bhnd_chipid *parent_cid; 463 const struct bhndb_regwin *cc_win; 464 struct resource_spec rs; 465 int error; 466 467 /* Let our parent device override the discovery process */ 468 parent_cid = BHNDB_BUS_GET_CHIPID(sc->parent_dev, sc->dev); 469 if (parent_cid != NULL) { 470 *result = *parent_cid; 471 return (0); 472 } 473 474 /* Find a register window we can use to map the first CHIPC_CHIPID_SIZE 475 * of ChipCommon registers. */ 476 cc_win = bhndb_regwin_find_best(cfg->register_windows, 477 BHND_DEVCLASS_CC, 0, BHND_PORT_DEVICE, 0, 0, CHIPC_CHIPID_SIZE); 478 if (cc_win == NULL) { 479 device_printf(sc->dev, "no chipcommon register window\n"); 480 return (0); 481 } 482 483 /* We can assume a device without a static ChipCommon window uses the 484 * default ChipCommon address. */ 485 if (cc_win->win_type == BHNDB_REGWIN_T_DYN) { 486 error = BHNDB_SET_WINDOW_ADDR(sc->dev, cc_win, 487 BHND_DEFAULT_CHIPC_ADDR); 488 489 if (error) { 490 device_printf(sc->dev, "failed to set chipcommon " 491 "register window\n"); 492 return (error); 493 } 494 } 495 496 /* Let the default bhnd implemenation alloc/release the resource and 497 * perform the read */ 498 rs.type = cc_win->res.type; 499 rs.rid = cc_win->res.rid; 500 rs.flags = RF_ACTIVE; 501 502 return (bhnd_read_chipid(sc->parent_dev, &rs, cc_win->win_offset, 503 result)); 504 } 505 506 /** 507 * Helper function that must be called by subclass bhndb(4) drivers 508 * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4) 509 * APIs on the bridge device. 510 * 511 * @param dev The bridge device to attach. 512 * @param bridge_devclass The device class of the bridging core. This is used 513 * to automatically detect the bridge core, and to disable additional bridge 514 * cores (e.g. PCMCIA on a PCIe device). 515 */ 516 int 517 bhndb_attach(device_t dev, bhnd_devclass_t bridge_devclass) 518 { 519 struct bhndb_devinfo *dinfo; 520 struct bhndb_softc *sc; 521 const struct bhndb_hwcfg *cfg; 522 int error; 523 524 sc = device_get_softc(dev); 525 sc->dev = dev; 526 sc->parent_dev = device_get_parent(dev); 527 sc->bridge_class = bridge_devclass; 528 529 BHNDB_LOCK_INIT(sc); 530 531 /* Read our chip identification data */ 532 cfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, sc->dev); 533 if ((error = bhndb_read_chipid(sc, cfg, &sc->chipid))) 534 return (error); 535 536 /* Populate generic resource allocation state. */ 537 sc->bus_res = bhndb_alloc_resources(dev, sc->parent_dev, cfg); 538 if (sc->bus_res == NULL) { 539 return (ENXIO); 540 } 541 542 /* Attach our bridged bus device */ 543 sc->bus_dev = BUS_ADD_CHILD(dev, 0, "bhnd", -1); 544 if (sc->bus_dev == NULL) { 545 error = ENXIO; 546 goto failed; 547 } 548 549 /* Configure address space */ 550 dinfo = device_get_ivars(sc->bus_dev); 551 dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED; 552 553 /* Finish attach */ 554 return (bus_generic_attach(dev)); 555 556 failed: 557 BHNDB_LOCK_DESTROY(sc); 558 559 if (sc->bus_res != NULL) 560 bhndb_free_resources(sc->bus_res); 561 562 return (error); 563 } 564 565 /** 566 * Default bhndb(4) implementation of BHNDB_INIT_FULL_CONFIG(). 567 * 568 * This function provides the default bhndb implementation of 569 * BHNDB_INIT_FULL_CONFIG(), and must be called by any subclass driver 570 * overriding BHNDB_INIT_FULL_CONFIG(). 571 * 572 * As documented by BHNDB_INIT_FULL_CONFIG, this function performs final 573 * bridge configuration based on the hardware information enumerated by the 574 * child bus, and will reset all resource allocation state on the bridge. 575 * 576 * When calling this method: 577 * - Any bus resources previously allocated by @p child must be deallocated. 578 * - The @p child bus must have performed initial enumeration -- but not 579 * probe or attachment -- of its children. 580 */ 581 int 582 bhndb_generic_init_full_config(device_t dev, device_t child, 583 const struct bhndb_hw_priority *hw_prio_table) 584 { 585 struct bhndb_softc *sc; 586 const struct bhndb_hw *hw; 587 struct bhndb_resources *r; 588 device_t *devs; 589 device_t hostb; 590 int ndevs; 591 int error; 592 593 sc = device_get_softc(dev); 594 hostb = NULL; 595 596 /* Fetch the full set of bhnd-attached cores */ 597 if ((error = device_get_children(sc->bus_dev, &devs, &ndevs))) 598 return (error); 599 600 /* Find our host bridge device */ 601 hostb = BHNDB_FIND_HOSTB_DEVICE(dev, child); 602 if (hostb == NULL) { 603 device_printf(sc->dev, "no host bridge core found\n"); 604 error = ENODEV; 605 goto cleanup; 606 } 607 608 /* Find our full register window configuration */ 609 if ((error = bhndb_find_hwspec(sc, devs, ndevs, &hw))) { 610 device_printf(sc->dev, "unable to identify device, " 611 " using generic bridge resource definitions\n"); 612 error = 0; 613 goto cleanup; 614 } 615 616 if (bootverbose || BHNDB_DEBUG(PRIO)) 617 device_printf(sc->dev, "%s resource configuration\n", hw->name); 618 619 /* Release existing resource state */ 620 BHNDB_LOCK(sc); 621 bhndb_free_resources(sc->bus_res); 622 sc->bus_res = NULL; 623 BHNDB_UNLOCK(sc); 624 625 /* Allocate new resource state */ 626 r = bhndb_alloc_resources(dev, sc->parent_dev, hw->cfg); 627 if (r == NULL) { 628 error = ENXIO; 629 goto cleanup; 630 } 631 632 /* Initialize our resource priority configuration */ 633 error = bhndb_initialize_region_cfg(sc, devs, ndevs, hw_prio_table, r); 634 if (error) { 635 bhndb_free_resources(r); 636 goto cleanup; 637 } 638 639 /* Update our bridge state */ 640 BHNDB_LOCK(sc); 641 sc->bus_res = r; 642 sc->hostb_dev = hostb; 643 BHNDB_UNLOCK(sc); 644 645 cleanup: 646 free(devs, M_TEMP); 647 return (error); 648 } 649 650 /** 651 * Default bhndb(4) implementation of DEVICE_DETACH(). 652 * 653 * This function detaches any child devices, and if successful, releases all 654 * resources held by the bridge device. 655 */ 656 int 657 bhndb_generic_detach(device_t dev) 658 { 659 struct bhndb_softc *sc; 660 int error; 661 662 sc = device_get_softc(dev); 663 664 /* Detach children */ 665 if ((error = bus_generic_detach(dev))) 666 return (error); 667 668 /* Clean up our driver state. */ 669 bhndb_free_resources(sc->bus_res); 670 671 BHNDB_LOCK_DESTROY(sc); 672 673 return (0); 674 } 675 676 /** 677 * Default bhndb(4) implementation of DEVICE_SUSPEND(). 678 * 679 * This function calls bus_generic_suspend() (or implements equivalent 680 * behavior). 681 */ 682 int 683 bhndb_generic_suspend(device_t dev) 684 { 685 return (bus_generic_suspend(dev)); 686 } 687 688 /** 689 * Default bhndb(4) implementation of DEVICE_RESUME(). 690 * 691 * This function calls bus_generic_resume() (or implements equivalent 692 * behavior). 693 */ 694 int 695 bhndb_generic_resume(device_t dev) 696 { 697 struct bhndb_softc *sc; 698 struct bhndb_resources *bus_res; 699 struct bhndb_dw_alloc *dwa; 700 int error; 701 702 sc = device_get_softc(dev); 703 bus_res = sc->bus_res; 704 705 /* Guarantee that all in-use dynamic register windows are mapped to 706 * their previously configured target address. */ 707 BHNDB_LOCK(sc); 708 for (size_t i = 0; i < bus_res->dwa_count; i++) { 709 dwa = &bus_res->dw_alloc[i]; 710 711 /* Skip regions that were not previously used */ 712 if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0) 713 continue; 714 715 /* Otherwise, ensure the register window is correct before 716 * any children attempt MMIO */ 717 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target); 718 if (error) 719 break; 720 } 721 BHNDB_UNLOCK(sc); 722 723 /* Error restoring hardware state; children cannot be safely resumed */ 724 if (error) { 725 device_printf(dev, "Unable to restore hardware configuration; " 726 "cannot resume: %d\n", error); 727 return (error); 728 } 729 730 return (bus_generic_resume(dev)); 731 } 732 733 /** 734 * Default implementation of BHNDB_SUSPEND_RESOURCE. 735 */ 736 static void 737 bhndb_suspend_resource(device_t dev, device_t child, int type, 738 struct resource *r) 739 { 740 struct bhndb_softc *sc; 741 struct bhndb_dw_alloc *dwa; 742 743 sc = device_get_softc(dev); 744 745 // TODO: IRQs? 746 if (type != SYS_RES_MEMORY) 747 return; 748 749 BHNDB_LOCK(sc); 750 dwa = bhndb_dw_find_resource(sc->bus_res, r); 751 if (dwa == NULL) { 752 BHNDB_UNLOCK(sc); 753 return; 754 } 755 756 if (BHNDB_DEBUG(PRIO)) 757 device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n", 758 type, rman_get_start(r), rman_get_size(r)); 759 760 /* Release the resource's window reference */ 761 bhndb_dw_release(sc->bus_res, dwa, r); 762 BHNDB_UNLOCK(sc); 763 } 764 765 /** 766 * Default implementation of BHNDB_RESUME_RESOURCE. 767 */ 768 static int 769 bhndb_resume_resource(device_t dev, device_t child, int type, 770 struct resource *r) 771 { 772 struct bhndb_softc *sc; 773 774 sc = device_get_softc(dev); 775 776 // TODO: IRQs? 777 if (type != SYS_RES_MEMORY) 778 return (0); 779 780 /* Inactive resources don't require reallocation of bridge resources */ 781 if (!(rman_get_flags(r) & RF_ACTIVE)) 782 return (0); 783 784 if (BHNDB_DEBUG(PRIO)) 785 device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n", 786 type, rman_get_start(r), rman_get_size(r)); 787 788 return (bhndb_try_activate_resource(sc, rman_get_device(r), type, 789 rman_get_rid(r), r, NULL)); 790 } 791 792 793 /** 794 * Default bhndb(4) implementation of BUS_READ_IVAR(). 795 */ 796 static int 797 bhndb_read_ivar(device_t dev, device_t child, int index, 798 uintptr_t *result) 799 { 800 return (ENOENT); 801 } 802 803 /** 804 * Default bhndb(4) implementation of BUS_WRITE_IVAR(). 805 */ 806 static int 807 bhndb_write_ivar(device_t dev, device_t child, int index, 808 uintptr_t value) 809 { 810 return (ENOENT); 811 } 812 813 /** 814 * Return the address space for the given @p child device. 815 */ 816 bhndb_addrspace 817 bhndb_get_addrspace(struct bhndb_softc *sc, device_t child) 818 { 819 struct bhndb_devinfo *dinfo; 820 device_t imd_dev; 821 822 /* Find the directly attached parent of the requesting device */ 823 imd_dev = child; 824 while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev) 825 imd_dev = device_get_parent(imd_dev); 826 827 if (imd_dev == NULL) 828 panic("bhndb address space request for non-child device %s\n", 829 device_get_nameunit(child)); 830 831 dinfo = device_get_ivars(imd_dev); 832 return (dinfo->addrspace); 833 } 834 835 /** 836 * Return the rman instance for a given resource @p type, if any. 837 * 838 * @param sc The bhndb device state. 839 * @param child The requesting child. 840 * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...) 841 */ 842 static struct rman * 843 bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type) 844 { 845 switch (bhndb_get_addrspace(sc, child)) { 846 case BHNDB_ADDRSPACE_NATIVE: 847 switch (type) { 848 case SYS_RES_MEMORY: 849 return (&sc->bus_res->ht_mem_rman); 850 case SYS_RES_IRQ: 851 return (NULL); 852 default: 853 return (NULL); 854 }; 855 856 case BHNDB_ADDRSPACE_BRIDGED: 857 switch (type) { 858 case SYS_RES_MEMORY: 859 return (&sc->bus_res->br_mem_rman); 860 case SYS_RES_IRQ: 861 // TODO 862 // return &sc->irq_rman; 863 return (NULL); 864 default: 865 return (NULL); 866 }; 867 } 868 869 /* Quieten gcc */ 870 return (NULL); 871 } 872 873 /** 874 * Default implementation of BUS_ADD_CHILD() 875 */ 876 static device_t 877 bhndb_add_child(device_t dev, u_int order, const char *name, int unit) 878 { 879 struct bhndb_devinfo *dinfo; 880 device_t child; 881 882 child = device_add_child_ordered(dev, order, name, unit); 883 if (child == NULL) 884 return (NULL); 885 886 dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT); 887 if (dinfo == NULL) { 888 device_delete_child(dev, child); 889 return (NULL); 890 } 891 892 dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE; 893 resource_list_init(&dinfo->resources); 894 895 device_set_ivars(child, dinfo); 896 897 return (child); 898 } 899 900 /** 901 * Default implementation of BUS_CHILD_DELETED(). 902 */ 903 static void 904 bhndb_child_deleted(device_t dev, device_t child) 905 { 906 struct bhndb_devinfo *dinfo = device_get_ivars(child); 907 if (dinfo != NULL) { 908 resource_list_free(&dinfo->resources); 909 free(dinfo, M_BHND); 910 } 911 912 device_set_ivars(child, NULL); 913 } 914 915 /** 916 * Default implementation of BHNDB_GET_CHIPID(). 917 */ 918 static const struct bhnd_chipid * 919 bhndb_get_chipid(device_t dev, device_t child) 920 { 921 struct bhndb_softc *sc = device_get_softc(dev); 922 return (&sc->chipid); 923 } 924 925 926 /** 927 * Default implementation of BHNDB_IS_HW_DISABLED(). 928 */ 929 static bool 930 bhndb_is_hw_disabled(device_t dev, device_t child) { 931 struct bhndb_softc *sc; 932 struct bhnd_core_info core; 933 934 sc = device_get_softc(dev); 935 936 /* Requestor must be attached to the bhnd bus */ 937 if (device_get_parent(child) != sc->bus_dev) { 938 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 939 } 940 941 /* Fetch core info */ 942 core = bhnd_get_core_info(child); 943 944 /* Try to defer to the bhndb bus parent */ 945 if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, &core)) 946 return (true); 947 948 /* Otherwise, we treat bridge-capable cores as unpopulated if they're 949 * not the configured host bridge */ 950 if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(&core))) 951 return (BHNDB_FIND_HOSTB_DEVICE(dev, sc->bus_dev) != child); 952 953 /* Otherwise, assume the core is populated */ 954 return (false); 955 } 956 957 /* ascending core index comparison used by bhndb_find_hostb_device() */ 958 static int 959 compare_core_index(const void *lhs, const void *rhs) 960 { 961 u_int left = bhnd_get_core_index(*(const device_t *) lhs); 962 u_int right = bhnd_get_core_index(*(const device_t *) rhs); 963 964 if (left < right) 965 return (-1); 966 else if (left > right) 967 return (1); 968 else 969 return (0); 970 } 971 972 /** 973 * Default bhndb(4) implementation of BHND_BUS_FIND_HOSTB_DEVICE(). 974 * 975 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged 976 * bhnd(4) devices to determine the hostb core: 977 * 978 * - The core must have a Broadcom vendor ID. 979 * - The core devclass must match the bridge type. 980 * - The core must be the first device on the bus with the bridged device 981 * class. 982 * 983 * @param dev The bhndb device 984 * @param child The requesting bhnd bus. 985 */ 986 static device_t 987 bhndb_find_hostb_device(device_t dev, device_t child) 988 { 989 struct bhndb_softc *sc; 990 struct bhnd_core_match md; 991 device_t hostb_dev, *devlist; 992 int devcnt, error; 993 994 sc = device_get_softc(dev); 995 996 /* Determine required device class and set up a match descriptor. */ 997 md = (struct bhnd_core_match) { 998 .vendor = BHND_MFGID_BCM, 999 .device = BHND_COREID_INVALID, 1000 .hwrev = { BHND_HWREV_INVALID, BHND_HWREV_INVALID }, 1001 .class = sc->bridge_class, 1002 .unit = 0 1003 }; 1004 1005 /* Must be the absolute first matching device on the bus. */ 1006 if ((error = device_get_children(child, &devlist, &devcnt))) 1007 return (false); 1008 1009 /* Sort by core index value, ascending */ 1010 qsort(devlist, devcnt, sizeof(*devlist), compare_core_index); 1011 1012 /* Find the hostb device */ 1013 hostb_dev = NULL; 1014 for (int i = 0; i < devcnt; i++) { 1015 if (bhnd_device_matches(devlist[i], &md)) { 1016 hostb_dev = devlist[i]; 1017 break; 1018 } 1019 } 1020 1021 /* Clean up */ 1022 free(devlist, M_TEMP); 1023 1024 return (hostb_dev); 1025 } 1026 1027 /** 1028 * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE(). 1029 */ 1030 static struct resource * 1031 bhndb_alloc_resource(device_t dev, device_t child, int type, 1032 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1033 { 1034 struct bhndb_softc *sc; 1035 struct resource_list_entry *rle; 1036 struct resource *rv; 1037 struct rman *rm; 1038 int error; 1039 bool passthrough, isdefault; 1040 1041 sc = device_get_softc(dev); 1042 passthrough = (device_get_parent(child) != dev); 1043 isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 1044 rle = NULL; 1045 1046 /* Populate defaults */ 1047 if (!passthrough && isdefault) { 1048 /* Fetch the resource list entry. */ 1049 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child), 1050 type, *rid); 1051 if (rle == NULL) { 1052 device_printf(dev, 1053 "default resource %#x type %d for child %s " 1054 "not found\n", *rid, type, 1055 device_get_nameunit(child)); 1056 1057 return (NULL); 1058 } 1059 1060 if (rle->res != NULL) { 1061 device_printf(dev, 1062 "resource entry %#x type %d for child %s is busy\n", 1063 *rid, type, device_get_nameunit(child)); 1064 1065 return (NULL); 1066 } 1067 1068 start = rle->start; 1069 end = rle->end; 1070 count = ulmax(count, rle->count); 1071 } 1072 1073 /* Validate resource addresses */ 1074 if (start > end || count > ((end - start) + 1)) 1075 return (NULL); 1076 1077 /* Fetch the resource manager */ 1078 rm = bhndb_get_rman(sc, child, type); 1079 if (rm == NULL) 1080 return (NULL); 1081 1082 /* Make our reservation */ 1083 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1084 child); 1085 if (rv == NULL) 1086 return (NULL); 1087 1088 rman_set_rid(rv, *rid); 1089 1090 /* Activate */ 1091 if (flags & RF_ACTIVE) { 1092 error = bus_activate_resource(child, type, *rid, rv); 1093 if (error) { 1094 device_printf(dev, 1095 "failed to activate entry %#x type %d for " 1096 "child %s: %d\n", 1097 *rid, type, device_get_nameunit(child), error); 1098 1099 rman_release_resource(rv); 1100 1101 return (NULL); 1102 } 1103 } 1104 1105 /* Update child's resource list entry */ 1106 if (rle != NULL) { 1107 rle->res = rv; 1108 rle->start = rman_get_start(rv); 1109 rle->end = rman_get_end(rv); 1110 rle->count = rman_get_size(rv); 1111 } 1112 1113 return (rv); 1114 } 1115 1116 /** 1117 * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE(). 1118 */ 1119 static int 1120 bhndb_release_resource(device_t dev, device_t child, int type, int rid, 1121 struct resource *r) 1122 { 1123 int error; 1124 1125 /* Deactivate resources */ 1126 if (rman_get_flags(r) & RF_ACTIVE) { 1127 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r); 1128 if (error) 1129 return (error); 1130 } 1131 1132 if ((error = rman_release_resource(r))) 1133 return (error); 1134 1135 return (0); 1136 } 1137 1138 /** 1139 * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE(). 1140 */ 1141 static int 1142 bhndb_adjust_resource(device_t dev, device_t child, int type, 1143 struct resource *r, rman_res_t start, rman_res_t end) 1144 { 1145 struct bhndb_softc *sc; 1146 struct rman *rm; 1147 int error; 1148 1149 sc = device_get_softc(dev); 1150 error = 0; 1151 1152 /* Fetch resource manager */ 1153 rm = bhndb_get_rman(sc, child, type); 1154 if (rm == NULL) 1155 return (ENXIO); 1156 1157 if (!rman_is_region_manager(r, rm)) 1158 return (ENXIO); 1159 1160 /* If active, adjustment is limited by the assigned window. */ 1161 BHNDB_LOCK(sc); 1162 1163 // TODO: Currently unsupported 1164 error = ENODEV; 1165 1166 BHNDB_UNLOCK(sc); 1167 if (!error) 1168 error = rman_adjust_resource(r, start, end); 1169 1170 return (error); 1171 } 1172 1173 /** 1174 * Initialize child resource @p r with a virtual address, tag, and handle 1175 * copied from @p parent, adjusted to contain only the range defined by 1176 * @p offsize and @p size. 1177 * 1178 * @param r The register to be initialized. 1179 * @param parent The parent bus resource that fully contains the subregion. 1180 * @param offset The subregion offset within @p parent. 1181 * @param size The subregion size. 1182 * @p r. 1183 */ 1184 static int 1185 bhndb_init_child_resource(struct resource *r, 1186 struct resource *parent, bhnd_size_t offset, bhnd_size_t size) 1187 { 1188 bus_space_handle_t bh, child_bh; 1189 bus_space_tag_t bt; 1190 uintptr_t vaddr; 1191 int error; 1192 1193 /* Fetch the parent resource's real bus values */ 1194 vaddr = (uintptr_t) rman_get_virtual(parent); 1195 bt = rman_get_bustag(parent); 1196 bh = rman_get_bushandle(parent); 1197 1198 /* Configure child resource with window-adjusted real bus values */ 1199 vaddr += offset; 1200 error = bus_space_subregion(bt, bh, offset, size, &child_bh); 1201 if (error) 1202 return (error); 1203 1204 rman_set_virtual(r, (void *) vaddr); 1205 rman_set_bustag(r, bt); 1206 rman_set_bushandle(r, child_bh); 1207 1208 return (0); 1209 } 1210 1211 /** 1212 * Attempt activation of a fixed register window mapping for @p child. 1213 * 1214 * @param sc BHNDB device state. 1215 * @param region The static region definition capable of mapping @p r. 1216 * @param child A child requesting resource activation. 1217 * @param type Resource type. 1218 * @param rid Resource identifier. 1219 * @param r Resource to be activated. 1220 * 1221 * @retval 0 if @p r was activated successfully 1222 * @retval ENOENT if no fixed register window was found. 1223 * @retval non-zero if @p r could not be activated. 1224 */ 1225 static int 1226 bhndb_activate_static_region(struct bhndb_softc *sc, 1227 struct bhndb_region *region, device_t child, int type, int rid, 1228 struct resource *r) 1229 { 1230 struct resource *bridge_res; 1231 const struct bhndb_regwin *win; 1232 bhnd_size_t parent_offset; 1233 rman_res_t r_start, r_size; 1234 int error; 1235 1236 win = region->static_regwin; 1237 1238 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type), 1239 ("can't activate non-static region")); 1240 1241 r_start = rman_get_start(r); 1242 r_size = rman_get_size(r); 1243 1244 /* Find the corresponding bridge resource */ 1245 bridge_res = bhndb_find_regwin_resource(sc->bus_res, win); 1246 if (bridge_res == NULL) 1247 return (ENXIO); 1248 1249 /* Calculate subregion offset within the parent resource */ 1250 parent_offset = r_start - region->addr; 1251 parent_offset += win->win_offset; 1252 1253 /* Configure resource with its real bus values. */ 1254 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size); 1255 if (error) 1256 return (error); 1257 1258 /* Mark active */ 1259 if ((error = rman_activate_resource(r))) 1260 return (error); 1261 1262 return (0); 1263 } 1264 1265 /** 1266 * Attempt to allocate/retain a dynamic register window for @p r, returning 1267 * the retained window. 1268 * 1269 * @param sc The bhndb driver state. 1270 * @param r The resource for which a window will be retained. 1271 */ 1272 static struct bhndb_dw_alloc * 1273 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r) 1274 { 1275 struct bhndb_dw_alloc *dwa; 1276 rman_res_t r_start, r_size; 1277 int error; 1278 1279 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1280 1281 r_start = rman_get_start(r); 1282 r_size = rman_get_size(r); 1283 1284 /* Look for an existing dynamic window we can reference */ 1285 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size); 1286 if (dwa != NULL) { 1287 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0) 1288 return (dwa); 1289 1290 return (NULL); 1291 } 1292 1293 /* Otherwise, try to reserve a free window */ 1294 dwa = bhndb_dw_next_free(sc->bus_res); 1295 if (dwa == NULL) { 1296 /* No free windows */ 1297 return (NULL); 1298 } 1299 1300 /* Set the window target */ 1301 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r), 1302 rman_get_size(r)); 1303 if (error) { 1304 device_printf(sc->dev, "dynamic window initialization " 1305 "for 0x%llx-0x%llx failed: %d\n", 1306 (unsigned long long) r_start, 1307 (unsigned long long) r_start + r_size - 1, 1308 error); 1309 return (NULL); 1310 } 1311 1312 /* Add our reservation */ 1313 if (bhndb_dw_retain(sc->bus_res, dwa, r)) 1314 return (NULL); 1315 1316 return (dwa); 1317 } 1318 1319 /** 1320 * Activate a resource using any viable static or dynamic register window. 1321 * 1322 * @param sc The bhndb driver state. 1323 * @param child The child holding ownership of @p r. 1324 * @param type The type of the resource to be activated. 1325 * @param rid The resource ID of @p r. 1326 * @param r The resource to be activated 1327 * @param[out] indirect On error and if not NULL, will be set to 'true' if 1328 * the caller should instead use an indirect resource mapping. 1329 * 1330 * @retval 0 success 1331 * @retval non-zero activation failed. 1332 */ 1333 static int 1334 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type, 1335 int rid, struct resource *r, bool *indirect) 1336 { 1337 struct bhndb_region *region; 1338 struct bhndb_dw_alloc *dwa; 1339 bhndb_priority_t dw_priority; 1340 rman_res_t r_start, r_size; 1341 rman_res_t parent_offset; 1342 int error; 1343 1344 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED); 1345 1346 // TODO - IRQs 1347 if (type != SYS_RES_MEMORY) 1348 return (ENXIO); 1349 1350 if (indirect) 1351 *indirect = false; 1352 1353 r_start = rman_get_start(r); 1354 r_size = rman_get_size(r); 1355 1356 /* Activate native addrspace resources using the host address space */ 1357 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) { 1358 struct resource *parent; 1359 1360 /* Find the bridge resource referenced by the child */ 1361 parent = bhndb_find_resource_range(sc->bus_res, r_start, 1362 r_size); 1363 if (parent == NULL) { 1364 device_printf(sc->dev, "host resource not found " 1365 "for 0x%llx-0x%llx\n", 1366 (unsigned long long) r_start, 1367 (unsigned long long) r_start + r_size - 1); 1368 return (ENOENT); 1369 } 1370 1371 /* Initialize child resource with the real bus values */ 1372 error = bhndb_init_child_resource(r, parent, 1373 r_start - rman_get_start(parent), r_size); 1374 if (error) 1375 return (error); 1376 1377 /* Try to activate child resource */ 1378 return (rman_activate_resource(r)); 1379 } 1380 1381 /* Default to low priority */ 1382 dw_priority = BHNDB_PRIORITY_LOW; 1383 1384 /* Look for a bus region matching the resource's address range */ 1385 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size); 1386 if (region != NULL) 1387 dw_priority = region->priority; 1388 1389 /* Prefer static mappings over consuming a dynamic windows. */ 1390 if (region && region->static_regwin) { 1391 error = bhndb_activate_static_region(sc, region, child, type, 1392 rid, r); 1393 if (error) 1394 device_printf(sc->dev, "static window allocation " 1395 "for 0x%llx-0x%llx failed\n", 1396 (unsigned long long) r_start, 1397 (unsigned long long) r_start + r_size - 1); 1398 return (error); 1399 } 1400 1401 /* A dynamic window will be required; is this resource high enough 1402 * priority to be reserved a dynamic window? */ 1403 if (dw_priority < sc->bus_res->min_prio) { 1404 if (indirect) 1405 *indirect = true; 1406 1407 return (ENOMEM); 1408 } 1409 1410 /* Find and retain a usable window */ 1411 BHNDB_LOCK(sc); { 1412 dwa = bhndb_retain_dynamic_window(sc, r); 1413 } BHNDB_UNLOCK(sc); 1414 1415 if (dwa == NULL) { 1416 if (indirect) 1417 *indirect = true; 1418 return (ENOMEM); 1419 } 1420 1421 /* Configure resource with its real bus values. */ 1422 parent_offset = dwa->win->win_offset; 1423 parent_offset += r_start - dwa->target; 1424 1425 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset, 1426 dwa->win->win_size); 1427 if (error) 1428 goto failed; 1429 1430 /* Mark active */ 1431 if ((error = rman_activate_resource(r))) 1432 goto failed; 1433 1434 return (0); 1435 1436 failed: 1437 /* Release our region allocation. */ 1438 BHNDB_LOCK(sc); 1439 bhndb_dw_release(sc->bus_res, dwa, r); 1440 BHNDB_UNLOCK(sc); 1441 1442 return (error); 1443 } 1444 1445 /** 1446 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE(). 1447 * 1448 * Maps resource activation requests to a viable static or dynamic 1449 * register window, if any. 1450 */ 1451 static int 1452 bhndb_activate_resource(device_t dev, device_t child, int type, int rid, 1453 struct resource *r) 1454 { 1455 struct bhndb_softc *sc = device_get_softc(dev); 1456 1457 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL)); 1458 } 1459 1460 /** 1461 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE(). 1462 */ 1463 static int 1464 bhndb_deactivate_resource(device_t dev, device_t child, int type, 1465 int rid, struct resource *r) 1466 { 1467 struct bhndb_dw_alloc *dwa; 1468 struct bhndb_softc *sc; 1469 struct rman *rm; 1470 int error; 1471 1472 sc = device_get_softc(dev); 1473 1474 if ((rm = bhndb_get_rman(sc, child, type)) == NULL) 1475 return (EINVAL); 1476 1477 /* Mark inactive */ 1478 if ((error = rman_deactivate_resource(r))) 1479 return (error); 1480 1481 /* Free any dynamic window allocation. */ 1482 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1483 BHNDB_LOCK(sc); 1484 dwa = bhndb_dw_find_resource(sc->bus_res, r); 1485 if (dwa != NULL) 1486 bhndb_dw_release(sc->bus_res, dwa, r); 1487 BHNDB_UNLOCK(sc); 1488 } 1489 1490 return (0); 1491 } 1492 1493 /** 1494 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST(). 1495 */ 1496 static struct resource_list * 1497 bhndb_get_resource_list(device_t dev, device_t child) 1498 { 1499 struct bhndb_devinfo *dinfo = device_get_ivars(child); 1500 return (&dinfo->resources); 1501 } 1502 1503 /** 1504 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE(). 1505 * 1506 * For BHNDB_ADDRSPACE_NATIVE children, all resources may be assumed to 1507 * be activated by the bridge. 1508 * 1509 * For BHNDB_ADDRSPACE_BRIDGED children, attempts to activate a static register 1510 * window, a dynamic register window, or configures @p r as an indirect 1511 * resource -- in that order. 1512 */ 1513 static int 1514 bhndb_activate_bhnd_resource(device_t dev, device_t child, 1515 int type, int rid, struct bhnd_resource *r) 1516 { 1517 struct bhndb_softc *sc; 1518 struct bhndb_region *region; 1519 rman_res_t r_start, r_size; 1520 int error; 1521 bool indirect; 1522 1523 KASSERT(!r->direct, 1524 ("direct flag set on inactive resource")); 1525 1526 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE), 1527 ("RF_ACTIVE set on inactive resource")); 1528 1529 sc = device_get_softc(dev); 1530 1531 r_start = rman_get_start(r->res); 1532 r_size = rman_get_size(r->res); 1533 1534 /* Verify bridged address range's resource priority, and skip direct 1535 * allocation if the priority is too low. */ 1536 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1537 bhndb_priority_t r_prio; 1538 1539 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size); 1540 if (region != NULL) 1541 r_prio = region->priority; 1542 else 1543 r_prio = BHNDB_PRIORITY_NONE; 1544 1545 /* If less than the minimum dynamic window priority, this 1546 * resource should always be indirect. */ 1547 if (r_prio < sc->bus_res->min_prio) 1548 return (0); 1549 } 1550 1551 /* Attempt direct activation */ 1552 error = bhndb_try_activate_resource(sc, child, type, rid, r->res, 1553 &indirect); 1554 if (!error) { 1555 r->direct = true; 1556 } else if (indirect) { 1557 /* The request was valid, but no viable register window is 1558 * available; indirection must be employed. */ 1559 error = 0; 1560 r->direct = false; 1561 } 1562 1563 if (BHNDB_DEBUG(PRIO) && 1564 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) 1565 { 1566 device_printf(child, "activated 0x%llx-0x%llx as %s " 1567 "resource\n", 1568 (unsigned long long) r_start, 1569 (unsigned long long) r_start + r_size - 1, 1570 r->direct ? "direct" : "indirect"); 1571 } 1572 1573 return (error); 1574 }; 1575 1576 /** 1577 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE(). 1578 */ 1579 static int 1580 bhndb_deactivate_bhnd_resource(device_t dev, device_t child, 1581 int type, int rid, struct bhnd_resource *r) 1582 { 1583 int error; 1584 1585 /* Indirect resources don't require activation */ 1586 if (!r->direct) 1587 return (0); 1588 1589 KASSERT(rman_get_flags(r->res) & RF_ACTIVE, 1590 ("RF_ACTIVE not set on direct resource")); 1591 1592 /* Perform deactivation */ 1593 error = bus_deactivate_resource(child, type, rid, r->res); 1594 if (!error) 1595 r->direct = false; 1596 1597 return (error); 1598 }; 1599 1600 /** 1601 * Slow path for bhndb_io_resource(). 1602 * 1603 * Iterates over the existing allocated dynamic windows looking for a viable 1604 * in-use region; the first matching region is returned. 1605 */ 1606 static struct bhndb_dw_alloc * 1607 bhndb_io_resource_slow(struct bhndb_softc *sc, bus_addr_t addr, 1608 bus_size_t size, bus_size_t *offset) 1609 { 1610 struct bhndb_resources *br; 1611 struct bhndb_dw_alloc *dwa; 1612 1613 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1614 1615 br = sc->bus_res; 1616 1617 /* Search for an existing dynamic mapping of this address range. 1618 * Static regions are not searched, as a statically mapped 1619 * region would never be allocated as an indirect resource. */ 1620 for (size_t i = 0; i < br->dwa_count; i++) { 1621 const struct bhndb_regwin *win; 1622 1623 dwa = &br->dw_alloc[i]; 1624 win = dwa->win; 1625 1626 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN, 1627 ("invalid register window type")); 1628 1629 /* Verify the range */ 1630 if (addr < dwa->target) 1631 continue; 1632 1633 if (addr + size > dwa->target + win->win_size) 1634 continue; 1635 1636 /* Found */ 1637 *offset = dwa->win->win_offset; 1638 *offset += addr - dwa->target; 1639 1640 return (dwa); 1641 } 1642 1643 /* not found */ 1644 return (NULL); 1645 } 1646 1647 /** 1648 * Find the bridge resource to be used for I/O requests. 1649 * 1650 * @param sc Bridge driver state. 1651 * @param addr The I/O target address. 1652 * @param size The size of the I/O operation to be performed at @p addr. 1653 * @param[out] offset The offset within the returned resource at which 1654 * to perform the I/O request. 1655 */ 1656 static inline struct bhndb_dw_alloc * 1657 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size, 1658 bus_size_t *offset) 1659 { 1660 struct bhndb_resources *br; 1661 struct bhndb_dw_alloc *dwa; 1662 int error; 1663 1664 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1665 1666 br = sc->bus_res; 1667 1668 /* Try to fetch a free window */ 1669 dwa = bhndb_dw_next_free(br); 1670 1671 /* 1672 * If no dynamic windows are available, look for an existing 1673 * region that maps the target range. 1674 * 1675 * If none are found, this is a child driver bug -- our window 1676 * over-commit should only fail in the case where a child driver leaks 1677 * resources, or perform operations out-of-order. 1678 * 1679 * Broadcom HND chipsets are designed to not require register window 1680 * swapping during execution; as long as the child devices are 1681 * attached/detached correctly, using the hardware's required order 1682 * of operations, there should always be a window available for the 1683 * current operation. 1684 */ 1685 if (dwa == NULL) { 1686 dwa = bhndb_io_resource_slow(sc, addr, size, offset); 1687 if (dwa == NULL) { 1688 panic("register windows exhausted attempting to map " 1689 "0x%llx-0x%llx\n", 1690 (unsigned long long) addr, 1691 (unsigned long long) addr+size-1); 1692 } 1693 1694 return (dwa); 1695 } 1696 1697 /* Adjust the window if the I/O request won't fit in the current 1698 * target range. */ 1699 if (addr < dwa->target || 1700 (dwa->target + dwa->win->win_size) - addr < size) 1701 { 1702 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr, 1703 size); 1704 if (error) { 1705 panic("failed to set register window target mapping " 1706 "0x%llx-0x%llx\n", 1707 (unsigned long long) addr, 1708 (unsigned long long) addr+size-1); 1709 } 1710 } 1711 1712 /* Calculate the offset and return */ 1713 *offset = (addr - dwa->target) + dwa->win->win_offset; 1714 return (dwa); 1715 } 1716 1717 /* 1718 * BHND_BUS_(READ|WRITE_* implementations 1719 */ 1720 1721 /* bhndb_bus_(read|write) common implementation */ 1722 #define BHNDB_IO_COMMON_SETUP(_io_size) \ 1723 struct bhndb_softc *sc; \ 1724 struct bhndb_dw_alloc *dwa; \ 1725 struct resource *io_res; \ 1726 bus_size_t io_offset; \ 1727 \ 1728 sc = device_get_softc(dev); \ 1729 \ 1730 BHNDB_LOCK(sc); \ 1731 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \ 1732 offset, _io_size, &io_offset); \ 1733 io_res = dwa->parent_res; \ 1734 \ 1735 KASSERT(!r->direct, \ 1736 ("bhnd_bus slow path used for direct resource")); \ 1737 \ 1738 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \ 1739 ("i/o resource is not active")); 1740 1741 #define BHNDB_IO_COMMON_TEARDOWN() \ 1742 BHNDB_UNLOCK(sc); 1743 1744 /* Defines a bhndb_bus_read_* method implementation */ 1745 #define BHNDB_IO_READ(_type, _name) \ 1746 static _type \ 1747 bhndb_bus_read_ ## _name (device_t dev, device_t child, \ 1748 struct bhnd_resource *r, bus_size_t offset) \ 1749 { \ 1750 _type v; \ 1751 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1752 v = bus_read_ ## _name (io_res, io_offset); \ 1753 BHNDB_IO_COMMON_TEARDOWN(); \ 1754 \ 1755 return (v); \ 1756 } 1757 1758 /* Defines a bhndb_bus_write_* method implementation */ 1759 #define BHNDB_IO_WRITE(_type, _name) \ 1760 static void \ 1761 bhndb_bus_write_ ## _name (device_t dev, device_t child, \ 1762 struct bhnd_resource *r, bus_size_t offset, _type value) \ 1763 { \ 1764 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1765 bus_write_ ## _name (io_res, io_offset, value); \ 1766 BHNDB_IO_COMMON_TEARDOWN(); \ 1767 } 1768 1769 /* Defines a bhndb_bus_(read|write)_multi_* method implementation */ 1770 #define BHNDB_IO_MULTI(_type, _rw, _name) \ 1771 static void \ 1772 bhndb_bus_ ## _rw ## _multi_ ## _name (device_t dev, \ 1773 device_t child, struct bhnd_resource *r, bus_size_t offset, \ 1774 _type *datap, bus_size_t count) \ 1775 { \ 1776 BHNDB_IO_COMMON_SETUP(sizeof(_type) * count); \ 1777 bus_ ## _rw ## _multi_ ## _name (io_res, io_offset, \ 1778 datap, count); \ 1779 BHNDB_IO_COMMON_TEARDOWN(); \ 1780 } 1781 1782 /* Defines a complete set of read/write methods */ 1783 #define BHNDB_IO_METHODS(_type, _size) \ 1784 BHNDB_IO_READ(_type, _size) \ 1785 BHNDB_IO_WRITE(_type, _size) \ 1786 \ 1787 BHNDB_IO_READ(_type, stream_ ## _size) \ 1788 BHNDB_IO_WRITE(_type, stream_ ## _size) \ 1789 \ 1790 BHNDB_IO_MULTI(_type, read, _size) \ 1791 BHNDB_IO_MULTI(_type, write, _size) \ 1792 \ 1793 BHNDB_IO_MULTI(_type, read, stream_ ## _size) \ 1794 BHNDB_IO_MULTI(_type, write, stream_ ## _size) 1795 1796 BHNDB_IO_METHODS(uint8_t, 1); 1797 BHNDB_IO_METHODS(uint16_t, 2); 1798 BHNDB_IO_METHODS(uint32_t, 4); 1799 1800 /** 1801 * Default bhndb(4) implementation of BHND_BUS_BARRIER(). 1802 */ 1803 static void 1804 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r, 1805 bus_size_t offset, bus_size_t length, int flags) 1806 { 1807 bus_size_t remain; 1808 1809 BHNDB_IO_COMMON_SETUP(length); 1810 1811 /* TODO: It's unclear whether we need a barrier implementation, 1812 * and if we do, what it needs to actually do. This may need 1813 * revisiting once we have a better idea of requirements after 1814 * porting the core drivers. */ 1815 panic("implementation incorrect"); 1816 1817 /* Use 4-byte reads where possible */ 1818 remain = length % sizeof(uint32_t); 1819 for (bus_size_t i = 0; i < (length - remain); i += 4) 1820 bus_read_4(io_res, io_offset + offset + i); 1821 1822 /* Use 1 byte reads for the remainder */ 1823 for (bus_size_t i = 0; i < remain; i++) 1824 bus_read_1(io_res, io_offset + offset + length + i); 1825 1826 BHNDB_IO_COMMON_TEARDOWN(); 1827 } 1828 1829 /** 1830 * Default bhndb(4) implementation of BUS_SETUP_INTR(). 1831 */ 1832 static int 1833 bhndb_setup_intr(device_t dev, device_t child, struct resource *r, 1834 int flags, driver_filter_t filter, driver_intr_t handler, void *arg, 1835 void **cookiep) 1836 { 1837 // TODO 1838 return (EOPNOTSUPP); 1839 } 1840 1841 /** 1842 * Default bhndb(4) implementation of BUS_TEARDOWN_INTR(). 1843 */ 1844 static int 1845 bhndb_teardown_intr(device_t dev, device_t child, struct resource *r, 1846 void *cookie) 1847 { 1848 // TODO 1849 return (EOPNOTSUPP); 1850 } 1851 1852 /** 1853 * Default bhndb(4) implementation of BUS_CONFIG_INTR(). 1854 */ 1855 static int 1856 bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig, 1857 enum intr_polarity pol) 1858 { 1859 // TODO 1860 return (EOPNOTSUPP); 1861 } 1862 1863 /** 1864 * Default bhndb(4) implementation of BUS_BIND_INTR(). 1865 */ 1866 static int 1867 bhndb_bind_intr(device_t dev, device_t child, struct resource *r, int cpu) 1868 { 1869 // TODO 1870 return (EOPNOTSUPP); 1871 } 1872 1873 /** 1874 * Default bhndb(4) implementation of BUS_DESCRIBE_INTR(). 1875 */ 1876 static int 1877 bhndb_describe_intr(device_t dev, device_t child, struct resource *irq, void *cookie, 1878 const char *descr) 1879 { 1880 // TODO 1881 return (EOPNOTSUPP); 1882 } 1883 1884 /** 1885 * Default bhndb(4) implementation of BUS_GET_DMA_TAG(). 1886 */ 1887 static bus_dma_tag_t 1888 bhndb_get_dma_tag(device_t dev, device_t child) 1889 { 1890 // TODO 1891 return (NULL); 1892 } 1893 1894 static device_method_t bhndb_methods[] = { 1895 /* Device interface */ \ 1896 DEVMETHOD(device_probe, bhndb_generic_probe), 1897 DEVMETHOD(device_detach, bhndb_generic_detach), 1898 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1899 DEVMETHOD(device_suspend, bhndb_generic_suspend), 1900 DEVMETHOD(device_resume, bhndb_generic_resume), 1901 1902 /* Bus interface */ 1903 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch), 1904 DEVMETHOD(bus_print_child, bhndb_print_child), 1905 DEVMETHOD(bus_child_pnpinfo_str, bhndb_child_pnpinfo_str), 1906 DEVMETHOD(bus_child_location_str, bhndb_child_location_str), 1907 DEVMETHOD(bus_add_child, bhndb_add_child), 1908 DEVMETHOD(bus_child_deleted, bhndb_child_deleted), 1909 1910 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource), 1911 DEVMETHOD(bus_release_resource, bhndb_release_resource), 1912 DEVMETHOD(bus_activate_resource, bhndb_activate_resource), 1913 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource), 1914 1915 DEVMETHOD(bus_setup_intr, bhndb_setup_intr), 1916 DEVMETHOD(bus_teardown_intr, bhndb_teardown_intr), 1917 DEVMETHOD(bus_config_intr, bhndb_config_intr), 1918 DEVMETHOD(bus_bind_intr, bhndb_bind_intr), 1919 DEVMETHOD(bus_describe_intr, bhndb_describe_intr), 1920 1921 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag), 1922 1923 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource), 1924 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 1925 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 1926 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 1927 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list), 1928 1929 DEVMETHOD(bus_read_ivar, bhndb_read_ivar), 1930 DEVMETHOD(bus_write_ivar, bhndb_write_ivar), 1931 1932 /* BHNDB interface */ 1933 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid), 1934 DEVMETHOD(bhndb_init_full_config, bhndb_generic_init_full_config), 1935 DEVMETHOD(bhndb_find_hostb_device, bhndb_find_hostb_device), 1936 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource), 1937 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource), 1938 1939 /* BHND interface */ 1940 DEVMETHOD(bhnd_bus_is_hw_disabled, bhndb_is_hw_disabled), 1941 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid), 1942 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource), 1943 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource), 1944 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_bus_generic_get_nvram_var), 1945 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1), 1946 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2), 1947 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4), 1948 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1), 1949 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2), 1950 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4), 1951 1952 DEVMETHOD(bhnd_bus_read_stream_1, bhndb_bus_read_stream_1), 1953 DEVMETHOD(bhnd_bus_read_stream_2, bhndb_bus_read_stream_2), 1954 DEVMETHOD(bhnd_bus_read_stream_4, bhndb_bus_read_stream_4), 1955 DEVMETHOD(bhnd_bus_write_stream_1, bhndb_bus_write_stream_1), 1956 DEVMETHOD(bhnd_bus_write_stream_2, bhndb_bus_write_stream_2), 1957 DEVMETHOD(bhnd_bus_write_stream_4, bhndb_bus_write_stream_4), 1958 1959 DEVMETHOD(bhnd_bus_read_multi_1, bhndb_bus_read_multi_1), 1960 DEVMETHOD(bhnd_bus_read_multi_2, bhndb_bus_read_multi_2), 1961 DEVMETHOD(bhnd_bus_read_multi_4, bhndb_bus_read_multi_4), 1962 DEVMETHOD(bhnd_bus_write_multi_1, bhndb_bus_write_multi_1), 1963 DEVMETHOD(bhnd_bus_write_multi_2, bhndb_bus_write_multi_2), 1964 DEVMETHOD(bhnd_bus_write_multi_4, bhndb_bus_write_multi_4), 1965 1966 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhndb_bus_read_multi_stream_1), 1967 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhndb_bus_read_multi_stream_2), 1968 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhndb_bus_read_multi_stream_4), 1969 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1), 1970 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2), 1971 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4), 1972 1973 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier), 1974 1975 DEVMETHOD_END 1976 }; 1977 1978 devclass_t bhndb_devclass; 1979 1980 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc)); 1981 1982 MODULE_VERSION(bhndb, 1); 1983 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1); 1984 MODULE_DEPEND(bhndb, bhnd_chipc, 1, 1, 1); 1985