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