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 rman_res_t mstart, mend; 1148 int error; 1149 1150 sc = device_get_softc(dev); 1151 error = 0; 1152 1153 /* Verify basic constraints */ 1154 if (end <= start) 1155 return (EINVAL); 1156 1157 /* Fetch resource manager */ 1158 rm = bhndb_get_rman(sc, child, type); 1159 if (rm == NULL) 1160 return (ENXIO); 1161 1162 if (!rman_is_region_manager(r, rm)) 1163 return (ENXIO); 1164 1165 BHNDB_LOCK(sc); 1166 1167 /* If not active, allow any range permitted by the resource manager */ 1168 if (!(rman_get_flags(r) & RF_ACTIVE)) 1169 goto done; 1170 1171 /* Otherwise, the range is limited to the existing register window 1172 * mapping */ 1173 error = bhndb_find_resource_limits(sc->bus_res, r, &mstart, &mend); 1174 if (error) 1175 goto done; 1176 1177 if (start < mstart || end > mend) { 1178 error = EINVAL; 1179 goto done; 1180 } 1181 1182 /* Fall through */ 1183 done: 1184 if (!error) 1185 error = rman_adjust_resource(r, start, end); 1186 1187 BHNDB_UNLOCK(sc); 1188 return (error); 1189 } 1190 1191 /** 1192 * Initialize child resource @p r with a virtual address, tag, and handle 1193 * copied from @p parent, adjusted to contain only the range defined by 1194 * @p offsize and @p size. 1195 * 1196 * @param r The register to be initialized. 1197 * @param parent The parent bus resource that fully contains the subregion. 1198 * @param offset The subregion offset within @p parent. 1199 * @param size The subregion size. 1200 * @p r. 1201 */ 1202 static int 1203 bhndb_init_child_resource(struct resource *r, 1204 struct resource *parent, bhnd_size_t offset, bhnd_size_t size) 1205 { 1206 bus_space_handle_t bh, child_bh; 1207 bus_space_tag_t bt; 1208 uintptr_t vaddr; 1209 int error; 1210 1211 /* Fetch the parent resource's real bus values */ 1212 vaddr = (uintptr_t) rman_get_virtual(parent); 1213 bt = rman_get_bustag(parent); 1214 bh = rman_get_bushandle(parent); 1215 1216 /* Configure child resource with window-adjusted real bus values */ 1217 vaddr += offset; 1218 error = bus_space_subregion(bt, bh, offset, size, &child_bh); 1219 if (error) 1220 return (error); 1221 1222 rman_set_virtual(r, (void *) vaddr); 1223 rman_set_bustag(r, bt); 1224 rman_set_bushandle(r, child_bh); 1225 1226 return (0); 1227 } 1228 1229 /** 1230 * Attempt activation of a fixed register window mapping for @p child. 1231 * 1232 * @param sc BHNDB device state. 1233 * @param region The static region definition capable of mapping @p r. 1234 * @param child A child requesting resource activation. 1235 * @param type Resource type. 1236 * @param rid Resource identifier. 1237 * @param r Resource to be activated. 1238 * 1239 * @retval 0 if @p r was activated successfully 1240 * @retval ENOENT if no fixed register window was found. 1241 * @retval non-zero if @p r could not be activated. 1242 */ 1243 static int 1244 bhndb_activate_static_region(struct bhndb_softc *sc, 1245 struct bhndb_region *region, device_t child, int type, int rid, 1246 struct resource *r) 1247 { 1248 struct resource *bridge_res; 1249 const struct bhndb_regwin *win; 1250 bhnd_size_t parent_offset; 1251 rman_res_t r_start, r_size; 1252 int error; 1253 1254 win = region->static_regwin; 1255 1256 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type), 1257 ("can't activate non-static region")); 1258 1259 r_start = rman_get_start(r); 1260 r_size = rman_get_size(r); 1261 1262 /* Find the corresponding bridge resource */ 1263 bridge_res = bhndb_find_regwin_resource(sc->bus_res, win); 1264 if (bridge_res == NULL) 1265 return (ENXIO); 1266 1267 /* Calculate subregion offset within the parent resource */ 1268 parent_offset = r_start - region->addr; 1269 parent_offset += win->win_offset; 1270 1271 /* Configure resource with its real bus values. */ 1272 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size); 1273 if (error) 1274 return (error); 1275 1276 /* Mark active */ 1277 if ((error = rman_activate_resource(r))) 1278 return (error); 1279 1280 return (0); 1281 } 1282 1283 /** 1284 * Attempt to allocate/retain a dynamic register window for @p r, returning 1285 * the retained window. 1286 * 1287 * @param sc The bhndb driver state. 1288 * @param r The resource for which a window will be retained. 1289 */ 1290 static struct bhndb_dw_alloc * 1291 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r) 1292 { 1293 struct bhndb_dw_alloc *dwa; 1294 rman_res_t r_start, r_size; 1295 int error; 1296 1297 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1298 1299 r_start = rman_get_start(r); 1300 r_size = rman_get_size(r); 1301 1302 /* Look for an existing dynamic window we can reference */ 1303 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size); 1304 if (dwa != NULL) { 1305 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0) 1306 return (dwa); 1307 1308 return (NULL); 1309 } 1310 1311 /* Otherwise, try to reserve a free window */ 1312 dwa = bhndb_dw_next_free(sc->bus_res); 1313 if (dwa == NULL) { 1314 /* No free windows */ 1315 return (NULL); 1316 } 1317 1318 /* Set the window target */ 1319 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r), 1320 rman_get_size(r)); 1321 if (error) { 1322 device_printf(sc->dev, "dynamic window initialization " 1323 "for 0x%llx-0x%llx failed: %d\n", 1324 (unsigned long long) r_start, 1325 (unsigned long long) r_start + r_size - 1, 1326 error); 1327 return (NULL); 1328 } 1329 1330 /* Add our reservation */ 1331 if (bhndb_dw_retain(sc->bus_res, dwa, r)) 1332 return (NULL); 1333 1334 return (dwa); 1335 } 1336 1337 /** 1338 * Activate a resource using any viable static or dynamic register window. 1339 * 1340 * @param sc The bhndb driver state. 1341 * @param child The child holding ownership of @p r. 1342 * @param type The type of the resource to be activated. 1343 * @param rid The resource ID of @p r. 1344 * @param r The resource to be activated 1345 * @param[out] indirect On error and if not NULL, will be set to 'true' if 1346 * the caller should instead use an indirect resource mapping. 1347 * 1348 * @retval 0 success 1349 * @retval non-zero activation failed. 1350 */ 1351 static int 1352 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type, 1353 int rid, struct resource *r, bool *indirect) 1354 { 1355 struct bhndb_region *region; 1356 struct bhndb_dw_alloc *dwa; 1357 bhndb_priority_t dw_priority; 1358 rman_res_t r_start, r_size; 1359 rman_res_t parent_offset; 1360 int error; 1361 1362 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED); 1363 1364 // TODO - IRQs 1365 if (type != SYS_RES_MEMORY) 1366 return (ENXIO); 1367 1368 if (indirect) 1369 *indirect = false; 1370 1371 r_start = rman_get_start(r); 1372 r_size = rman_get_size(r); 1373 1374 /* Activate native addrspace resources using the host address space */ 1375 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) { 1376 struct resource *parent; 1377 1378 /* Find the bridge resource referenced by the child */ 1379 parent = bhndb_find_resource_range(sc->bus_res, r_start, 1380 r_size); 1381 if (parent == NULL) { 1382 device_printf(sc->dev, "host resource not found " 1383 "for 0x%llx-0x%llx\n", 1384 (unsigned long long) r_start, 1385 (unsigned long long) r_start + r_size - 1); 1386 return (ENOENT); 1387 } 1388 1389 /* Initialize child resource with the real bus values */ 1390 error = bhndb_init_child_resource(r, parent, 1391 r_start - rman_get_start(parent), r_size); 1392 if (error) 1393 return (error); 1394 1395 /* Try to activate child resource */ 1396 return (rman_activate_resource(r)); 1397 } 1398 1399 /* Default to low priority */ 1400 dw_priority = BHNDB_PRIORITY_LOW; 1401 1402 /* Look for a bus region matching the resource's address range */ 1403 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size); 1404 if (region != NULL) 1405 dw_priority = region->priority; 1406 1407 /* Prefer static mappings over consuming a dynamic windows. */ 1408 if (region && region->static_regwin) { 1409 error = bhndb_activate_static_region(sc, region, child, type, 1410 rid, r); 1411 if (error) 1412 device_printf(sc->dev, "static window allocation " 1413 "for 0x%llx-0x%llx failed\n", 1414 (unsigned long long) r_start, 1415 (unsigned long long) r_start + r_size - 1); 1416 return (error); 1417 } 1418 1419 /* A dynamic window will be required; is this resource high enough 1420 * priority to be reserved a dynamic window? */ 1421 if (dw_priority < sc->bus_res->min_prio) { 1422 if (indirect) 1423 *indirect = true; 1424 1425 return (ENOMEM); 1426 } 1427 1428 /* Find and retain a usable window */ 1429 BHNDB_LOCK(sc); { 1430 dwa = bhndb_retain_dynamic_window(sc, r); 1431 } BHNDB_UNLOCK(sc); 1432 1433 if (dwa == NULL) { 1434 if (indirect) 1435 *indirect = true; 1436 return (ENOMEM); 1437 } 1438 1439 /* Configure resource with its real bus values. */ 1440 parent_offset = dwa->win->win_offset; 1441 parent_offset += r_start - dwa->target; 1442 1443 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset, 1444 dwa->win->win_size); 1445 if (error) 1446 goto failed; 1447 1448 /* Mark active */ 1449 if ((error = rman_activate_resource(r))) 1450 goto failed; 1451 1452 return (0); 1453 1454 failed: 1455 /* Release our region allocation. */ 1456 BHNDB_LOCK(sc); 1457 bhndb_dw_release(sc->bus_res, dwa, r); 1458 BHNDB_UNLOCK(sc); 1459 1460 return (error); 1461 } 1462 1463 /** 1464 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE(). 1465 * 1466 * Maps resource activation requests to a viable static or dynamic 1467 * register window, if any. 1468 */ 1469 static int 1470 bhndb_activate_resource(device_t dev, device_t child, int type, int rid, 1471 struct resource *r) 1472 { 1473 struct bhndb_softc *sc = device_get_softc(dev); 1474 1475 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL)); 1476 } 1477 1478 /** 1479 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE(). 1480 */ 1481 static int 1482 bhndb_deactivate_resource(device_t dev, device_t child, int type, 1483 int rid, struct resource *r) 1484 { 1485 struct bhndb_dw_alloc *dwa; 1486 struct bhndb_softc *sc; 1487 struct rman *rm; 1488 int error; 1489 1490 sc = device_get_softc(dev); 1491 1492 if ((rm = bhndb_get_rman(sc, child, type)) == NULL) 1493 return (EINVAL); 1494 1495 /* Mark inactive */ 1496 if ((error = rman_deactivate_resource(r))) 1497 return (error); 1498 1499 /* Free any dynamic window allocation. */ 1500 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1501 BHNDB_LOCK(sc); 1502 dwa = bhndb_dw_find_resource(sc->bus_res, r); 1503 if (dwa != NULL) 1504 bhndb_dw_release(sc->bus_res, dwa, r); 1505 BHNDB_UNLOCK(sc); 1506 } 1507 1508 return (0); 1509 } 1510 1511 /** 1512 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST(). 1513 */ 1514 static struct resource_list * 1515 bhndb_get_resource_list(device_t dev, device_t child) 1516 { 1517 struct bhndb_devinfo *dinfo = device_get_ivars(child); 1518 return (&dinfo->resources); 1519 } 1520 1521 /** 1522 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE(). 1523 * 1524 * For BHNDB_ADDRSPACE_NATIVE children, all resources may be assumed to 1525 * be activated by the bridge. 1526 * 1527 * For BHNDB_ADDRSPACE_BRIDGED children, attempts to activate a static register 1528 * window, a dynamic register window, or configures @p r as an indirect 1529 * resource -- in that order. 1530 */ 1531 static int 1532 bhndb_activate_bhnd_resource(device_t dev, device_t child, 1533 int type, int rid, struct bhnd_resource *r) 1534 { 1535 struct bhndb_softc *sc; 1536 struct bhndb_region *region; 1537 rman_res_t r_start, r_size; 1538 int error; 1539 bool indirect; 1540 1541 KASSERT(!r->direct, 1542 ("direct flag set on inactive resource")); 1543 1544 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE), 1545 ("RF_ACTIVE set on inactive resource")); 1546 1547 sc = device_get_softc(dev); 1548 1549 r_start = rman_get_start(r->res); 1550 r_size = rman_get_size(r->res); 1551 1552 /* Verify bridged address range's resource priority, and skip direct 1553 * allocation if the priority is too low. */ 1554 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1555 bhndb_priority_t r_prio; 1556 1557 region = bhndb_find_resource_region(sc->bus_res, r_start, 1558 r_size); 1559 if (region != NULL) 1560 r_prio = region->priority; 1561 else 1562 r_prio = BHNDB_PRIORITY_NONE; 1563 1564 /* If less than the minimum dynamic window priority, this 1565 * resource should always be indirect. */ 1566 if (r_prio < sc->bus_res->min_prio) 1567 return (0); 1568 } 1569 1570 /* Attempt direct activation */ 1571 error = bhndb_try_activate_resource(sc, child, type, rid, r->res, 1572 &indirect); 1573 if (!error) { 1574 r->direct = true; 1575 } else if (indirect) { 1576 /* The request was valid, but no viable register window is 1577 * available; indirection must be employed. */ 1578 error = 0; 1579 r->direct = false; 1580 } 1581 1582 if (BHNDB_DEBUG(PRIO) && 1583 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) 1584 { 1585 device_printf(child, "activated 0x%llx-0x%llx as %s " 1586 "resource\n", 1587 (unsigned long long) r_start, 1588 (unsigned long long) r_start + r_size - 1, 1589 r->direct ? "direct" : "indirect"); 1590 } 1591 1592 return (error); 1593 }; 1594 1595 /** 1596 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE(). 1597 */ 1598 static int 1599 bhndb_deactivate_bhnd_resource(device_t dev, device_t child, 1600 int type, int rid, struct bhnd_resource *r) 1601 { 1602 int error; 1603 1604 /* Indirect resources don't require activation */ 1605 if (!r->direct) 1606 return (0); 1607 1608 KASSERT(rman_get_flags(r->res) & RF_ACTIVE, 1609 ("RF_ACTIVE not set on direct resource")); 1610 1611 /* Perform deactivation */ 1612 error = bus_deactivate_resource(child, type, rid, r->res); 1613 if (!error) 1614 r->direct = false; 1615 1616 return (error); 1617 }; 1618 1619 /** 1620 * Slow path for bhndb_io_resource(). 1621 * 1622 * Iterates over the existing allocated dynamic windows looking for a viable 1623 * in-use region; the first matching region is returned. 1624 */ 1625 static struct bhndb_dw_alloc * 1626 bhndb_io_resource_slow(struct bhndb_softc *sc, bus_addr_t addr, 1627 bus_size_t size, bus_size_t *offset) 1628 { 1629 struct bhndb_resources *br; 1630 struct bhndb_dw_alloc *dwa; 1631 1632 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1633 1634 br = sc->bus_res; 1635 1636 /* Search for an existing dynamic mapping of this address range. 1637 * Static regions are not searched, as a statically mapped 1638 * region would never be allocated as an indirect resource. */ 1639 for (size_t i = 0; i < br->dwa_count; i++) { 1640 const struct bhndb_regwin *win; 1641 1642 dwa = &br->dw_alloc[i]; 1643 win = dwa->win; 1644 1645 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN, 1646 ("invalid register window type")); 1647 1648 /* Verify the range */ 1649 if (addr < dwa->target) 1650 continue; 1651 1652 if (addr + size > dwa->target + win->win_size) 1653 continue; 1654 1655 /* Found */ 1656 *offset = dwa->win->win_offset; 1657 *offset += addr - dwa->target; 1658 1659 return (dwa); 1660 } 1661 1662 /* not found */ 1663 return (NULL); 1664 } 1665 1666 /** 1667 * Find the bridge resource to be used for I/O requests. 1668 * 1669 * @param sc Bridge driver state. 1670 * @param addr The I/O target address. 1671 * @param size The size of the I/O operation to be performed at @p addr. 1672 * @param[out] offset The offset within the returned resource at which 1673 * to perform the I/O request. 1674 */ 1675 static inline struct bhndb_dw_alloc * 1676 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size, 1677 bus_size_t *offset) 1678 { 1679 struct bhndb_resources *br; 1680 struct bhndb_dw_alloc *dwa; 1681 int error; 1682 1683 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1684 1685 br = sc->bus_res; 1686 1687 /* Try to fetch a free window */ 1688 dwa = bhndb_dw_next_free(br); 1689 1690 /* 1691 * If no dynamic windows are available, look for an existing 1692 * region that maps the target range. 1693 * 1694 * If none are found, this is a child driver bug -- our window 1695 * over-commit should only fail in the case where a child driver leaks 1696 * resources, or perform operations out-of-order. 1697 * 1698 * Broadcom HND chipsets are designed to not require register window 1699 * swapping during execution; as long as the child devices are 1700 * attached/detached correctly, using the hardware's required order 1701 * of operations, there should always be a window available for the 1702 * current operation. 1703 */ 1704 if (dwa == NULL) { 1705 dwa = bhndb_io_resource_slow(sc, addr, size, offset); 1706 if (dwa == NULL) { 1707 panic("register windows exhausted attempting to map " 1708 "0x%llx-0x%llx\n", 1709 (unsigned long long) addr, 1710 (unsigned long long) addr+size-1); 1711 } 1712 1713 return (dwa); 1714 } 1715 1716 /* Adjust the window if the I/O request won't fit in the current 1717 * target range. */ 1718 if (addr < dwa->target || 1719 (dwa->target + dwa->win->win_size) - addr < size) 1720 { 1721 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr, 1722 size); 1723 if (error) { 1724 panic("failed to set register window target mapping " 1725 "0x%llx-0x%llx\n", 1726 (unsigned long long) addr, 1727 (unsigned long long) addr+size-1); 1728 } 1729 } 1730 1731 /* Calculate the offset and return */ 1732 *offset = (addr - dwa->target) + dwa->win->win_offset; 1733 return (dwa); 1734 } 1735 1736 /* 1737 * BHND_BUS_(READ|WRITE_* implementations 1738 */ 1739 1740 /* bhndb_bus_(read|write) common implementation */ 1741 #define BHNDB_IO_COMMON_SETUP(_io_size) \ 1742 struct bhndb_softc *sc; \ 1743 struct bhndb_dw_alloc *dwa; \ 1744 struct resource *io_res; \ 1745 bus_size_t io_offset; \ 1746 \ 1747 sc = device_get_softc(dev); \ 1748 \ 1749 BHNDB_LOCK(sc); \ 1750 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \ 1751 offset, _io_size, &io_offset); \ 1752 io_res = dwa->parent_res; \ 1753 \ 1754 KASSERT(!r->direct, \ 1755 ("bhnd_bus slow path used for direct resource")); \ 1756 \ 1757 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \ 1758 ("i/o resource is not active")); 1759 1760 #define BHNDB_IO_COMMON_TEARDOWN() \ 1761 BHNDB_UNLOCK(sc); 1762 1763 /* Defines a bhndb_bus_read_* method implementation */ 1764 #define BHNDB_IO_READ(_type, _name) \ 1765 static _type \ 1766 bhndb_bus_read_ ## _name (device_t dev, device_t child, \ 1767 struct bhnd_resource *r, bus_size_t offset) \ 1768 { \ 1769 _type v; \ 1770 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1771 v = bus_read_ ## _name (io_res, io_offset); \ 1772 BHNDB_IO_COMMON_TEARDOWN(); \ 1773 \ 1774 return (v); \ 1775 } 1776 1777 /* Defines a bhndb_bus_write_* method implementation */ 1778 #define BHNDB_IO_WRITE(_type, _name) \ 1779 static void \ 1780 bhndb_bus_write_ ## _name (device_t dev, device_t child, \ 1781 struct bhnd_resource *r, bus_size_t offset, _type value) \ 1782 { \ 1783 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1784 bus_write_ ## _name (io_res, io_offset, value); \ 1785 BHNDB_IO_COMMON_TEARDOWN(); \ 1786 } 1787 1788 /* Defines a bhndb_bus_(read|write|set)_(multi|region)_* method */ 1789 #define BHNDB_IO_MISC(_type, _ptr, _op, _size) \ 1790 static void \ 1791 bhndb_bus_ ## _op ## _ ## _size (device_t dev, \ 1792 device_t child, struct bhnd_resource *r, bus_size_t offset, \ 1793 _type _ptr datap, bus_size_t count) \ 1794 { \ 1795 BHNDB_IO_COMMON_SETUP(sizeof(_type) * count); \ 1796 bus_ ## _op ## _ ## _size (io_res, io_offset, \ 1797 datap, count); \ 1798 BHNDB_IO_COMMON_TEARDOWN(); \ 1799 } 1800 1801 /* Defines a complete set of read/write methods */ 1802 #define BHNDB_IO_METHODS(_type, _size) \ 1803 BHNDB_IO_READ(_type, _size) \ 1804 BHNDB_IO_WRITE(_type, _size) \ 1805 \ 1806 BHNDB_IO_READ(_type, stream_ ## _size) \ 1807 BHNDB_IO_WRITE(_type, stream_ ## _size) \ 1808 \ 1809 BHNDB_IO_MISC(_type, *, read_multi, _size) \ 1810 BHNDB_IO_MISC(_type, *, write_multi, _size) \ 1811 \ 1812 BHNDB_IO_MISC(_type, *, read_multi_stream, _size) \ 1813 BHNDB_IO_MISC(_type, *, write_multi_stream, _size) \ 1814 \ 1815 BHNDB_IO_MISC(_type, , set_multi, _size) \ 1816 BHNDB_IO_MISC(_type, , set_region, _size) \ 1817 BHNDB_IO_MISC(_type, *, read_region, _size) \ 1818 BHNDB_IO_MISC(_type, *, write_region, _size) \ 1819 \ 1820 BHNDB_IO_MISC(_type, *, read_region_stream, _size) \ 1821 BHNDB_IO_MISC(_type, *, write_region_stream, _size) 1822 1823 BHNDB_IO_METHODS(uint8_t, 1); 1824 BHNDB_IO_METHODS(uint16_t, 2); 1825 BHNDB_IO_METHODS(uint32_t, 4); 1826 1827 /** 1828 * Default bhndb(4) implementation of BHND_BUS_BARRIER(). 1829 */ 1830 static void 1831 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r, 1832 bus_size_t offset, bus_size_t length, int flags) 1833 { 1834 BHNDB_IO_COMMON_SETUP(length); 1835 1836 bus_barrier(io_res, io_offset + offset, length, flags); 1837 1838 BHNDB_IO_COMMON_TEARDOWN(); 1839 } 1840 1841 /** 1842 * Default bhndb(4) implementation of BUS_SETUP_INTR(). 1843 */ 1844 static int 1845 bhndb_setup_intr(device_t dev, device_t child, struct resource *r, 1846 int flags, driver_filter_t filter, driver_intr_t handler, void *arg, 1847 void **cookiep) 1848 { 1849 // TODO 1850 return (EOPNOTSUPP); 1851 } 1852 1853 /** 1854 * Default bhndb(4) implementation of BUS_TEARDOWN_INTR(). 1855 */ 1856 static int 1857 bhndb_teardown_intr(device_t dev, device_t child, struct resource *r, 1858 void *cookie) 1859 { 1860 // TODO 1861 return (EOPNOTSUPP); 1862 } 1863 1864 /** 1865 * Default bhndb(4) implementation of BUS_CONFIG_INTR(). 1866 */ 1867 static int 1868 bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig, 1869 enum intr_polarity pol) 1870 { 1871 // TODO 1872 return (EOPNOTSUPP); 1873 } 1874 1875 /** 1876 * Default bhndb(4) implementation of BUS_BIND_INTR(). 1877 */ 1878 static int 1879 bhndb_bind_intr(device_t dev, device_t child, struct resource *r, int cpu) 1880 { 1881 // TODO 1882 return (EOPNOTSUPP); 1883 } 1884 1885 /** 1886 * Default bhndb(4) implementation of BUS_DESCRIBE_INTR(). 1887 */ 1888 static int 1889 bhndb_describe_intr(device_t dev, device_t child, struct resource *irq, void *cookie, 1890 const char *descr) 1891 { 1892 // TODO 1893 return (EOPNOTSUPP); 1894 } 1895 1896 /** 1897 * Default bhndb(4) implementation of BUS_GET_DMA_TAG(). 1898 */ 1899 static bus_dma_tag_t 1900 bhndb_get_dma_tag(device_t dev, device_t child) 1901 { 1902 // TODO 1903 return (NULL); 1904 } 1905 1906 static device_method_t bhndb_methods[] = { 1907 /* Device interface */ \ 1908 DEVMETHOD(device_probe, bhndb_generic_probe), 1909 DEVMETHOD(device_detach, bhndb_generic_detach), 1910 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1911 DEVMETHOD(device_suspend, bhndb_generic_suspend), 1912 DEVMETHOD(device_resume, bhndb_generic_resume), 1913 1914 /* Bus interface */ 1915 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch), 1916 DEVMETHOD(bus_print_child, bhndb_print_child), 1917 DEVMETHOD(bus_child_pnpinfo_str, bhndb_child_pnpinfo_str), 1918 DEVMETHOD(bus_child_location_str, bhndb_child_location_str), 1919 DEVMETHOD(bus_add_child, bhndb_add_child), 1920 DEVMETHOD(bus_child_deleted, bhndb_child_deleted), 1921 1922 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource), 1923 DEVMETHOD(bus_release_resource, bhndb_release_resource), 1924 DEVMETHOD(bus_activate_resource, bhndb_activate_resource), 1925 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource), 1926 1927 DEVMETHOD(bus_setup_intr, bhndb_setup_intr), 1928 DEVMETHOD(bus_teardown_intr, bhndb_teardown_intr), 1929 DEVMETHOD(bus_config_intr, bhndb_config_intr), 1930 DEVMETHOD(bus_bind_intr, bhndb_bind_intr), 1931 DEVMETHOD(bus_describe_intr, bhndb_describe_intr), 1932 1933 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag), 1934 1935 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource), 1936 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 1937 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 1938 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 1939 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list), 1940 1941 DEVMETHOD(bus_read_ivar, bhndb_read_ivar), 1942 DEVMETHOD(bus_write_ivar, bhndb_write_ivar), 1943 1944 /* BHNDB interface */ 1945 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid), 1946 DEVMETHOD(bhndb_init_full_config, bhndb_generic_init_full_config), 1947 DEVMETHOD(bhndb_find_hostb_device, bhndb_find_hostb_device), 1948 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource), 1949 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource), 1950 1951 /* BHND interface */ 1952 DEVMETHOD(bhnd_bus_is_hw_disabled, bhndb_is_hw_disabled), 1953 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid), 1954 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource), 1955 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource), 1956 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_bus_generic_get_nvram_var), 1957 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1), 1958 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2), 1959 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4), 1960 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1), 1961 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2), 1962 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4), 1963 1964 DEVMETHOD(bhnd_bus_read_stream_1, bhndb_bus_read_stream_1), 1965 DEVMETHOD(bhnd_bus_read_stream_2, bhndb_bus_read_stream_2), 1966 DEVMETHOD(bhnd_bus_read_stream_4, bhndb_bus_read_stream_4), 1967 DEVMETHOD(bhnd_bus_write_stream_1, bhndb_bus_write_stream_1), 1968 DEVMETHOD(bhnd_bus_write_stream_2, bhndb_bus_write_stream_2), 1969 DEVMETHOD(bhnd_bus_write_stream_4, bhndb_bus_write_stream_4), 1970 1971 DEVMETHOD(bhnd_bus_read_multi_1, bhndb_bus_read_multi_1), 1972 DEVMETHOD(bhnd_bus_read_multi_2, bhndb_bus_read_multi_2), 1973 DEVMETHOD(bhnd_bus_read_multi_4, bhndb_bus_read_multi_4), 1974 DEVMETHOD(bhnd_bus_write_multi_1, bhndb_bus_write_multi_1), 1975 DEVMETHOD(bhnd_bus_write_multi_2, bhndb_bus_write_multi_2), 1976 DEVMETHOD(bhnd_bus_write_multi_4, bhndb_bus_write_multi_4), 1977 1978 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhndb_bus_read_multi_stream_1), 1979 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhndb_bus_read_multi_stream_2), 1980 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhndb_bus_read_multi_stream_4), 1981 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1), 1982 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2), 1983 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4), 1984 1985 DEVMETHOD(bhnd_bus_set_multi_1, bhndb_bus_set_multi_1), 1986 DEVMETHOD(bhnd_bus_set_multi_2, bhndb_bus_set_multi_2), 1987 DEVMETHOD(bhnd_bus_set_multi_4, bhndb_bus_set_multi_4), 1988 DEVMETHOD(bhnd_bus_set_region_1, bhndb_bus_set_region_1), 1989 DEVMETHOD(bhnd_bus_set_region_2, bhndb_bus_set_region_2), 1990 DEVMETHOD(bhnd_bus_set_region_4, bhndb_bus_set_region_4), 1991 1992 DEVMETHOD(bhnd_bus_read_region_1, bhndb_bus_read_region_1), 1993 DEVMETHOD(bhnd_bus_read_region_2, bhndb_bus_read_region_2), 1994 DEVMETHOD(bhnd_bus_read_region_4, bhndb_bus_read_region_4), 1995 DEVMETHOD(bhnd_bus_write_region_1, bhndb_bus_write_region_1), 1996 DEVMETHOD(bhnd_bus_write_region_2, bhndb_bus_write_region_2), 1997 DEVMETHOD(bhnd_bus_write_region_4, bhndb_bus_write_region_4), 1998 1999 DEVMETHOD(bhnd_bus_read_region_stream_1,bhndb_bus_read_region_stream_1), 2000 DEVMETHOD(bhnd_bus_read_region_stream_2,bhndb_bus_read_region_stream_2), 2001 DEVMETHOD(bhnd_bus_read_region_stream_4,bhndb_bus_read_region_stream_4), 2002 DEVMETHOD(bhnd_bus_write_region_stream_1,bhndb_bus_write_region_stream_1), 2003 DEVMETHOD(bhnd_bus_write_region_stream_2,bhndb_bus_write_region_stream_2), 2004 DEVMETHOD(bhnd_bus_write_region_stream_4,bhndb_bus_write_region_stream_4), 2005 2006 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier), 2007 2008 DEVMETHOD_END 2009 }; 2010 2011 devclass_t bhndb_devclass; 2012 2013 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc)); 2014 2015 MODULE_VERSION(bhndb, 1); 2016 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1); 2017 MODULE_DEPEND(bhndb, bhnd_chipc, 1, 1, 1); 2018