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