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