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