bhndb.c revision 299097
1193323Sed/*- 2193323Sed * Copyright (c) 2015 Landon Fuller <landon@landonf.org> 3193323Sed * All rights reserved. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer, 10193323Sed * without modification. 11193323Sed * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12193323Sed * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13193323Sed * redistribution must be conditioned upon including a substantially 14193323Sed * similar Disclaimer requirement for further binary redistribution. 15193323Sed * 16193323Sed * NO WARRANTY 17193323Sed * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18193323Sed * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19219077Sdim * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20193323Sed * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21193323Sed * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22193323Sed * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23193323Sed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24193323Sed * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25193323Sed * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27193323Sed * THE POSSIBILITY OF SUCH DAMAGES. 28243830Sdim */ 29193323Sed 30193323Sed#include <sys/cdefs.h> 31193323Sed__FBSDID("$FreeBSD: head/sys/dev/bhnd/bhndb/bhndb.c 299097 2016-05-04 23:38:27Z adrian $"); 32193323Sed 33193323Sed/* 34193323Sed * Abstract BHND Bridge Device Driver 35193323Sed * 36193323Sed * Provides generic support for bridging from a parent bus (such as PCI) to 37234353Sdim * a BHND-compatible bus (e.g. bcma or siba). 38226633Sdim */ 39193323Sed 40193323Sed#include <sys/param.h> 41226633Sdim#include <sys/kernel.h> 42193323Sed#include <sys/bus.h> 43193323Sed#include <sys/module.h> 44193323Sed#include <sys/systm.h> 45193323Sed 46226633Sdim#include <machine/bus.h> 47193323Sed#include <sys/rman.h> 48198090Srdivacky#include <machine/resource.h> 49198090Srdivacky 50198090Srdivacky#include <dev/bhnd/bhndvar.h> 51198090Srdivacky#include <dev/bhnd/bhndreg.h> 52198090Srdivacky 53198090Srdivacky#include <dev/bhnd/cores/chipc/chipcreg.h> 54198090Srdivacky 55193323Sed#include "bhndbvar.h" 56193323Sed#include "bhndb_bus_if.h" 57193323Sed#include "bhndb_hwdata.h" 58226633Sdim#include "bhndb_private.h" 59193323Sed 60193323Sed/* Debugging flags */ 61193323Sedstatic u_long bhndb_debug = 0; 62193323SedTUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug); 63193323Sed 64193323Sedenum { 65193323Sed BHNDB_DEBUG_PRIO = 1 << 0, 66193323Sed}; 67193323Sed 68193323Sed#define BHNDB_DEBUG(_type) (BHNDB_DEBUG_ ## _type & bhndb_debug) 69193323Sed 70193323Sedstatic bool bhndb_hw_matches(device_t *devlist, 71193323Sed int num_devs, 72193323Sed const struct bhndb_hw *hw); 73193323Sed 74193323Sedstatic int bhndb_initialize_region_cfg( 75193323Sed struct bhndb_softc *sc, device_t *devs, 76193323Sed int ndevs, 77193323Sed const struct bhndb_hw_priority *table, 78193323Sed struct bhndb_resources *r); 79207618Srdivacky 80193323Sedstatic int bhndb_find_hwspec(struct bhndb_softc *sc, 81193323Sed device_t *devs, int ndevs, 82193323Sed const struct bhndb_hw **hw); 83193323Sed 84221345Sdimstatic int bhndb_read_chipid(struct bhndb_softc *sc, 85193323Sed const struct bhndb_hwcfg *cfg, 86193323Sed struct bhnd_chipid *result); 87207618Srdivacky 88193323Sedbhndb_addrspace bhndb_get_addrspace(struct bhndb_softc *sc, 89193323Sed device_t child); 90193323Sed 91193323Sedstatic struct rman *bhndb_get_rman(struct bhndb_softc *sc, 92193323Sed device_t child, int type); 93193323Sed 94198090Srdivackystatic int bhndb_init_child_resource(struct resource *r, 95193323Sed struct resource *parent, 96193323Sed bhnd_size_t offset, 97221345Sdim bhnd_size_t size); 98221345Sdim 99221345Sdimstatic int bhndb_activate_static_region( 100193323Sed struct bhndb_softc *sc, 101193323Sed struct bhndb_region *region, 102193323Sed device_t child, int type, int rid, 103193323Sed struct resource *r); 104193323Sed 105193323Sedstatic int bhndb_try_activate_resource( 106193323Sed struct bhndb_softc *sc, device_t child, 107193323Sed int type, int rid, struct resource *r, 108226633Sdim bool *indirect); 109198090Srdivacky 110198090Srdivacky 111198090Srdivacky/** 112198090Srdivacky * Default bhndb(4) implementation of DEVICE_PROBE(). 113198090Srdivacky * 114198090Srdivacky * This function provides the default bhndb implementation of DEVICE_PROBE(), 115198090Srdivacky * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge(). 116198090Srdivacky */ 117198090Srdivackyint 118198090Srdivackybhndb_generic_probe(device_t dev) 119198090Srdivacky{ 120198090Srdivacky return (BUS_PROBE_NOWILDCARD); 121198090Srdivacky} 122193323Sed 123193323Sedstatic void 124193323Sedbhndb_probe_nomatch(device_t dev, device_t child) 125193323Sed{ 126193323Sed const char *name; 127193323Sed 128193323Sed name = device_get_name(child); 129193323Sed if (name == NULL) 130193323Sed name = "unknown device"; 131193323Sed 132193323Sed device_printf(dev, "<%s> (no driver attached)\n", name); 133193323Sed} 134193323Sed 135263508Sdimstatic int 136263508Sdimbhndb_print_child(device_t dev, device_t child) 137193323Sed{ 138219077Sdim struct bhndb_softc *sc; 139219077Sdim struct resource_list *rl; 140263508Sdim int retval = 0; 141263508Sdim 142193323Sed sc = device_get_softc(dev); 143193323Sed 144263508Sdim retval += bus_print_child_header(dev, child); 145263508Sdim 146193323Sed rl = BUS_GET_RESOURCE_LIST(dev, child); 147193323Sed if (rl != NULL) { 148203954Srdivacky retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, 149193323Sed "%#jx"); 150193323Sed retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, 151193323Sed "%jd"); 152193323Sed } 153193323Sed 154193323Sed retval += bus_print_child_domain(dev, child); 155207618Srdivacky retval += bus_print_child_footer(dev, child); 156193323Sed 157193323Sed return (retval); 158193323Sed} 159193323Sed 160193323Sedstatic int 161193323Sedbhndb_child_pnpinfo_str(device_t bus, device_t child, char *buf, 162193323Sed size_t buflen) 163193323Sed{ 164193323Sed *buf = '\0'; 165193323Sed return (0); 166193323Sed} 167193323Sed 168193323Sedstatic int 169193323Sedbhndb_child_location_str(device_t dev, device_t child, char *buf, 170193323Sed size_t buflen) 171193323Sed{ 172193323Sed struct bhndb_softc *sc; 173193323Sed 174193323Sed sc = device_get_softc(dev); 175193323Sed 176193323Sed snprintf(buf, buflen, "base=0x%llx", 177 (unsigned long long) sc->chipid.enum_addr); 178 return (0); 179} 180 181/** 182 * Return true if @p devlist matches the @p hw specification. 183 * 184 * @param devlist A device table to match against. 185 * @param num_devs The number of devices in @p devlist. 186 * @param hw The hardware description to be matched against. 187 */ 188static bool 189bhndb_hw_matches(device_t *devlist, int num_devs, const struct bhndb_hw *hw) 190{ 191 for (u_int i = 0; i < hw->num_hw_reqs; i++) { 192 const struct bhnd_core_match *match; 193 bool found; 194 195 match = &hw->hw_reqs[i]; 196 found = false; 197 198 for (int d = 0; d < num_devs; d++) { 199 if (!bhnd_device_matches(devlist[d], match)) 200 continue; 201 202 found = true; 203 break; 204 } 205 206 if (!found) 207 return (false); 208 } 209 210 return (true); 211} 212 213/** 214 * Initialize the region maps and priority configuration in @p r using 215 * the provided priority @p table and the set of devices attached to 216 * the bridged @p bus_dev . 217 * 218 * @param sc The bhndb device state. 219 * @param devs All devices enumerated on the bridged bhnd bus. 220 * @param ndevs The length of @p devs. 221 * @param table Hardware priority table to be used to determine the relative 222 * priorities of per-core port resources. 223 * @param r The resource state to be configured. 224 */ 225static int 226bhndb_initialize_region_cfg(struct bhndb_softc *sc, device_t *devs, int ndevs, 227 const struct bhndb_hw_priority *table, struct bhndb_resources *r) 228{ 229 const struct bhndb_hw_priority *hp; 230 bhnd_addr_t addr; 231 bhnd_size_t size; 232 size_t prio_low, prio_default, prio_high; 233 int error; 234 235 /* The number of port regions per priority band that must be accessible 236 * via dynamic register windows */ 237 prio_low = 0; 238 prio_default = 0; 239 prio_high = 0; 240 241 /* 242 * Register bridge regions covering all statically mapped ports. 243 */ 244 for (int i = 0; i < ndevs; i++) { 245 const struct bhndb_regwin *regw; 246 device_t child; 247 248 child = devs[i]; 249 250 for (regw = r->cfg->register_windows; 251 regw->win_type != BHNDB_REGWIN_T_INVALID; regw++) 252 { 253 /* Only core windows are supported */ 254 if (regw->win_type != BHNDB_REGWIN_T_CORE) 255 continue; 256 257 /* Skip non-applicable register windows. */ 258 if (!bhndb_regwin_matches_device(regw, child)) 259 continue; 260 261 /* Fetch the base address of the mapped port. */ 262 error = bhnd_get_region_addr(child, 263 regw->core.port_type, regw->core.port, 264 regw->core.region, &addr, &size); 265 if (error) 266 return (error); 267 268 /* 269 * Always defer to the register window's size. 270 * 271 * If the port size is smaller than the window size, 272 * this ensures that we fully utilize register windows 273 * larger than the referenced port. 274 * 275 * If the port size is larger than the window size, this 276 * ensures that we do not directly map the allocations 277 * within the region to a too-small window. 278 */ 279 size = regw->win_size; 280 281 /* 282 * Add to the bus region list. 283 * 284 * The window priority for a statically mapped 285 * region is always HIGH. 286 */ 287 error = bhndb_add_resource_region(r, addr, size, 288 BHNDB_PRIORITY_HIGH, regw); 289 if (error) 290 return (error); 291 } 292 } 293 294 /* 295 * Perform priority accounting and register bridge regions for all 296 * ports defined in the priority table 297 */ 298 for (int i = 0; i < ndevs; i++) { 299 struct bhndb_region *region; 300 device_t child; 301 302 child = devs[i]; 303 304 /* 305 * Skip priority accounting for cores that ... 306 */ 307 308 /* ... do not require bridge resources */ 309 if (bhnd_is_hw_disabled(child) || !device_is_enabled(child)) 310 continue; 311 312 /* ... do not have a priority table entry */ 313 hp = bhndb_hw_priority_find_device(table, child); 314 if (hp == NULL) 315 continue; 316 317 /* ... are explicitly disabled in the priority table. */ 318 if (hp->priority == BHNDB_PRIORITY_NONE) 319 continue; 320 321 /* Determine the number of dynamic windows required and 322 * register their bus_region entries. */ 323 for (u_int i = 0; i < hp->num_ports; i++) { 324 const struct bhndb_port_priority *pp; 325 326 pp = &hp->ports[i]; 327 328 /* Skip ports not defined on this device */ 329 if (!bhnd_is_region_valid(child, pp->type, pp->port, 330 pp->region)) 331 { 332 continue; 333 } 334 335 /* Fetch the address+size of the mapped port. */ 336 error = bhnd_get_region_addr(child, pp->type, pp->port, 337 pp->region, &addr, &size); 338 if (error) 339 return (error); 340 341 /* Skip ports with an existing static mapping */ 342 region = bhndb_find_resource_region(r, addr, size); 343 if (region != NULL && region->static_regwin != NULL) 344 continue; 345 346 /* Define a dynamic region for this port */ 347 error = bhndb_add_resource_region(r, addr, size, 348 pp->priority, NULL); 349 if (error) 350 return (error); 351 352 /* Update port mapping counts */ 353 switch (pp->priority) { 354 case BHNDB_PRIORITY_NONE: 355 break; 356 case BHNDB_PRIORITY_LOW: 357 prio_low++; 358 break; 359 case BHNDB_PRIORITY_DEFAULT: 360 prio_default++; 361 break; 362 case BHNDB_PRIORITY_HIGH: 363 prio_high++; 364 break; 365 } 366 } 367 } 368 369 /* Determine the minimum priority at which we'll allocate direct 370 * register windows from our dynamic pool */ 371 size_t prio_total = prio_low + prio_default + prio_high; 372 if (prio_total <= r->dwa_count) { 373 /* low+default+high priority regions get windows */ 374 r->min_prio = BHNDB_PRIORITY_LOW; 375 376 } else if (prio_default + prio_high <= r->dwa_count) { 377 /* default+high priority regions get windows */ 378 r->min_prio = BHNDB_PRIORITY_DEFAULT; 379 380 } else { 381 /* high priority regions get windows */ 382 r->min_prio = BHNDB_PRIORITY_HIGH; 383 } 384 385 if (BHNDB_DEBUG(PRIO)) { 386 struct bhndb_region *region; 387 const char *direct_msg, *type_msg; 388 bhndb_priority_t prio, prio_min; 389 390 prio_min = r->min_prio; 391 device_printf(sc->dev, "min_prio: %d\n", prio_min); 392 393 STAILQ_FOREACH(region, &r->bus_regions, link) { 394 prio = region->priority; 395 396 direct_msg = prio >= prio_min ? "direct" : "indirect"; 397 type_msg = region->static_regwin ? "static" : "dynamic"; 398 399 device_printf(sc->dev, "region 0x%llx+0x%llx priority " 400 "%u %s/%s\n", 401 (unsigned long long) region->addr, 402 (unsigned long long) region->size, 403 region->priority, 404 direct_msg, type_msg); 405 } 406 } 407 408 return (0); 409} 410 411/** 412 * Find a hardware specification for @p dev. 413 * 414 * @param sc The bhndb device state. 415 * @param devs All devices enumerated on the bridged bhnd bus. 416 * @param ndevs The length of @p devs. 417 * @param[out] hw On success, the matched hardware specification. 418 * with @p dev. 419 * 420 * @retval 0 success 421 * @retval non-zero if an error occurs fetching device info for comparison. 422 */ 423static int 424bhndb_find_hwspec(struct bhndb_softc *sc, device_t *devs, int ndevs, 425 const struct bhndb_hw **hw) 426{ 427 const struct bhndb_hw *next, *hw_table; 428 429 /* Search for the first matching hardware config. */ 430 hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev); 431 for (next = hw_table; next->hw_reqs != NULL; next++) { 432 if (!bhndb_hw_matches(devs, ndevs, next)) 433 continue; 434 435 /* Found */ 436 *hw = next; 437 return (0); 438 } 439 440 return (ENOENT); 441} 442 443/** 444 * Read the ChipCommon identification data for this device. 445 * 446 * @param sc bhndb device state. 447 * @param cfg The hardware configuration to use when mapping the ChipCommon 448 * registers. 449 * @param[out] result the chip identification data. 450 * 451 * @retval 0 success 452 * @retval non-zero if the ChipCommon identification data could not be read. 453 */ 454static int 455bhndb_read_chipid(struct bhndb_softc *sc, const struct bhndb_hwcfg *cfg, 456 struct bhnd_chipid *result) 457{ 458 const struct bhnd_chipid *parent_cid; 459 const struct bhndb_regwin *cc_win; 460 struct resource_spec rs; 461 int error; 462 463 /* Let our parent device override the discovery process */ 464 parent_cid = BHNDB_BUS_GET_CHIPID(sc->parent_dev, sc->dev); 465 if (parent_cid != NULL) { 466 *result = *parent_cid; 467 return (0); 468 } 469 470 /* Find a register window we can use to map the first CHIPC_CHIPID_SIZE 471 * of ChipCommon registers. */ 472 cc_win = bhndb_regwin_find_best(cfg->register_windows, 473 BHND_DEVCLASS_CC, 0, BHND_PORT_DEVICE, 0, 0, CHIPC_CHIPID_SIZE); 474 if (cc_win == NULL) { 475 device_printf(sc->dev, "no chipcommon register window\n"); 476 return (0); 477 } 478 479 /* We can assume a device without a static ChipCommon window uses the 480 * default ChipCommon address. */ 481 if (cc_win->win_type == BHNDB_REGWIN_T_DYN) { 482 error = BHNDB_SET_WINDOW_ADDR(sc->dev, cc_win, 483 BHND_DEFAULT_CHIPC_ADDR); 484 485 if (error) { 486 device_printf(sc->dev, "failed to set chipcommon " 487 "register window\n"); 488 return (error); 489 } 490 } 491 492 /* Let the default bhnd implemenation alloc/release the resource and 493 * perform the read */ 494 rs.type = cc_win->res.type; 495 rs.rid = cc_win->res.rid; 496 rs.flags = RF_ACTIVE; 497 498 return (bhnd_read_chipid(sc->parent_dev, &rs, cc_win->win_offset, 499 result)); 500} 501 502/** 503 * Helper function that must be called by subclass bhndb(4) drivers 504 * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4) 505 * APIs on the bridge device. 506 * 507 * @param dev The bridge device to attach. 508 * @param bridge_devclass The device class of the bridging core. This is used 509 * to automatically detect the bridge core, and to disable additional bridge 510 * cores (e.g. PCMCIA on a PCIe device). 511 */ 512int 513bhndb_attach(device_t dev, bhnd_devclass_t bridge_devclass) 514{ 515 struct bhndb_devinfo *dinfo; 516 struct bhndb_softc *sc; 517 const struct bhndb_hwcfg *cfg; 518 int error; 519 520 sc = device_get_softc(dev); 521 sc->dev = dev; 522 sc->parent_dev = device_get_parent(dev); 523 sc->bridge_class = bridge_devclass; 524 525 BHNDB_LOCK_INIT(sc); 526 527 /* Read our chip identification data */ 528 cfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, sc->dev); 529 if ((error = bhndb_read_chipid(sc, cfg, &sc->chipid))) 530 return (error); 531 532 /* Populate generic resource allocation state. */ 533 sc->bus_res = bhndb_alloc_resources(dev, sc->parent_dev, cfg); 534 if (sc->bus_res == NULL) { 535 return (ENXIO); 536 } 537 538 /* Attach our bridged bus device */ 539 sc->bus_dev = BUS_ADD_CHILD(dev, 0, "bhnd", -1); 540 if (sc->bus_dev == NULL) { 541 error = ENXIO; 542 goto failed; 543 } 544 545 /* Configure address space */ 546 dinfo = device_get_ivars(sc->bus_dev); 547 dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED; 548 549 /* Finish attach */ 550 return (bus_generic_attach(dev)); 551 552failed: 553 BHNDB_LOCK_DESTROY(sc); 554 555 if (sc->bus_res != NULL) 556 bhndb_free_resources(sc->bus_res); 557 558 return (error); 559} 560 561/** 562 * Default bhndb(4) implementation of BHNDB_INIT_FULL_CONFIG(). 563 * 564 * This function provides the default bhndb implementation of 565 * BHNDB_INIT_FULL_CONFIG(), and must be called by any subclass driver 566 * overriding BHNDB_INIT_FULL_CONFIG(). 567 * 568 * As documented by BHNDB_INIT_FULL_CONFIG, this function performs final 569 * bridge configuration based on the hardware information enumerated by the 570 * child bus, and will reset all resource allocation state on the bridge. 571 * 572 * When calling this method: 573 * - Any bus resources previously allocated by @p child must be deallocated. 574 * - The @p child bus must have performed initial enumeration -- but not 575 * probe or attachment -- of its children. 576 */ 577int 578bhndb_generic_init_full_config(device_t dev, device_t child, 579 const struct bhndb_hw_priority *hw_prio_table) 580{ 581 struct bhndb_softc *sc; 582 const struct bhndb_hw *hw; 583 struct bhndb_resources *r; 584 device_t *devs; 585 device_t hostb; 586 int ndevs; 587 int error; 588 589 sc = device_get_softc(dev); 590 hostb = NULL; 591 592 /* Fetch the full set of attached devices */ 593 if ((error = device_get_children(sc->bus_dev, &devs, &ndevs))) 594 return (error); 595 596 /* Find our host bridge device */ 597 for (int i = 0; i < ndevs; i++) { 598 if (bhnd_is_hostb_device(devs[i])) { 599 hostb = devs[i]; 600 break; 601 } 602 } 603 604 if (hostb == NULL) { 605 device_printf(sc->dev, "no host bridge core found\n"); 606 error = ENODEV; 607 goto cleanup; 608 } 609 610 /* Find our full register window configuration */ 611 if ((error = bhndb_find_hwspec(sc, devs, ndevs, &hw))) { 612 device_printf(sc->dev, "unable to identify device, " 613 " using generic bridge resource definitions\n"); 614 error = 0; 615 goto cleanup; 616 } 617 618 if (bootverbose) 619 device_printf(sc->dev, "%s resource configuration\n", hw->name); 620 621 /* Release existing resource state */ 622 BHNDB_LOCK(sc); 623 bhndb_free_resources(sc->bus_res); 624 sc->bus_res = NULL; 625 BHNDB_UNLOCK(sc); 626 627 /* Allocate new resource state */ 628 r = bhndb_alloc_resources(dev, sc->parent_dev, hw->cfg); 629 if (r == NULL) { 630 error = ENXIO; 631 goto cleanup; 632 } 633 634 /* Initialize our resource priority configuration */ 635 error = bhndb_initialize_region_cfg(sc, devs, ndevs, hw_prio_table, r); 636 if (error) { 637 bhndb_free_resources(r); 638 goto cleanup; 639 } 640 641 /* Update our bridge state */ 642 BHNDB_LOCK(sc); 643 sc->bus_res = r; 644 sc->hostb_dev = hostb; 645 BHNDB_UNLOCK(sc); 646 647cleanup: 648 free(devs, M_TEMP); 649 return (error); 650} 651 652/** 653 * Default bhndb(4) implementation of DEVICE_DETACH(). 654 * 655 * This function detaches any child devices, and if successful, releases all 656 * resources held by the bridge device. 657 */ 658int 659bhndb_generic_detach(device_t dev) 660{ 661 struct bhndb_softc *sc; 662 int error; 663 664 sc = device_get_softc(dev); 665 666 /* Detach children */ 667 if ((error = bus_generic_detach(dev))) 668 return (error); 669 670 /* Clean up our driver state. */ 671 bhndb_free_resources(sc->bus_res); 672 673 BHNDB_LOCK_DESTROY(sc); 674 675 return (0); 676} 677 678/** 679 * Default bhndb(4) implementation of DEVICE_SUSPEND(). 680 * 681 * This function calls bus_generic_suspend() (or implements equivalent 682 * behavior). 683 */ 684int 685bhndb_generic_suspend(device_t dev) 686{ 687 return (bus_generic_suspend(dev)); 688} 689 690/** 691 * Default bhndb(4) implementation of DEVICE_RESUME(). 692 * 693 * This function calls bus_generic_resume() (or implements equivalent 694 * behavior). 695 */ 696int 697bhndb_generic_resume(device_t dev) 698{ 699 struct bhndb_softc *sc; 700 struct bhndb_resources *bus_res; 701 struct bhndb_dw_alloc *dwa; 702 int error; 703 704 sc = device_get_softc(dev); 705 bus_res = sc->bus_res; 706 707 /* Guarantee that all in-use dynamic register windows are mapped to 708 * their previously configured target address. */ 709 BHNDB_LOCK(sc); 710 for (size_t i = 0; i < bus_res->dwa_count; i++) { 711 dwa = &bus_res->dw_alloc[i]; 712 713 /* Skip regions that were not previously used */ 714 if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0) 715 continue; 716 717 /* Otherwise, ensure the register window is correct before 718 * any children attempt MMIO */ 719 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target); 720 if (error) 721 break; 722 } 723 BHNDB_UNLOCK(sc); 724 725 /* Error restoring hardware state; children cannot be safely resumed */ 726 if (error) { 727 device_printf(dev, "Unable to restore hardware configuration; " 728 "cannot resume: %d\n", error); 729 return (error); 730 } 731 732 return (bus_generic_resume(dev)); 733} 734 735/** 736 * Default implementation of BHNDB_SUSPEND_RESOURCE. 737 */ 738static void 739bhndb_suspend_resource(device_t dev, device_t child, int type, 740 struct resource *r) 741{ 742 struct bhndb_softc *sc; 743 struct bhndb_dw_alloc *dwa; 744 745 sc = device_get_softc(dev); 746 747 // TODO: IRQs? 748 if (type != SYS_RES_MEMORY) 749 return; 750 751 BHNDB_LOCK(sc); 752 dwa = bhndb_dw_find_resource(sc->bus_res, r); 753 if (dwa == NULL) { 754 BHNDB_UNLOCK(sc); 755 return; 756 } 757 758 if (BHNDB_DEBUG(PRIO)) 759 device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n", 760 type, rman_get_start(r), rman_get_size(r)); 761 762 /* Release the resource's window reference */ 763 bhndb_dw_release(sc->bus_res, dwa, r); 764 BHNDB_UNLOCK(sc); 765} 766 767/** 768 * Default implementation of BHNDB_RESUME_RESOURCE. 769 */ 770static int 771bhndb_resume_resource(device_t dev, device_t child, int type, 772 struct resource *r) 773{ 774 struct bhndb_softc *sc; 775 776 sc = device_get_softc(dev); 777 778 // TODO: IRQs? 779 if (type != SYS_RES_MEMORY) 780 return (0); 781 782 /* Inactive resources don't require reallocation of bridge resources */ 783 if (!(rman_get_flags(r) & RF_ACTIVE)) 784 return (0); 785 786 if (BHNDB_DEBUG(PRIO)) 787 device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n", 788 type, rman_get_start(r), rman_get_size(r)); 789 790 return (bhndb_try_activate_resource(sc, rman_get_device(r), type, 791 rman_get_rid(r), r, NULL)); 792} 793 794 795/** 796 * Default bhndb(4) implementation of BUS_READ_IVAR(). 797 */ 798static int 799bhndb_read_ivar(device_t dev, device_t child, int index, 800 uintptr_t *result) 801{ 802 return (ENOENT); 803} 804 805/** 806 * Default bhndb(4) implementation of BUS_WRITE_IVAR(). 807 */ 808static int 809bhndb_write_ivar(device_t dev, device_t child, int index, 810 uintptr_t value) 811{ 812 return (ENOENT); 813} 814 815/** 816 * Return the address space for the given @p child device. 817 */ 818bhndb_addrspace 819bhndb_get_addrspace(struct bhndb_softc *sc, device_t child) 820{ 821 struct bhndb_devinfo *dinfo; 822 device_t imd_dev; 823 824 /* Find the directly attached parent of the requesting device */ 825 imd_dev = child; 826 while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev) 827 imd_dev = device_get_parent(imd_dev); 828 829 if (imd_dev == NULL) 830 panic("bhndb address space request for non-child device %s\n", 831 device_get_nameunit(child)); 832 833 dinfo = device_get_ivars(imd_dev); 834 return (dinfo->addrspace); 835} 836 837/** 838 * Return the rman instance for a given resource @p type, if any. 839 * 840 * @param sc The bhndb device state. 841 * @param child The requesting child. 842 * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...) 843 */ 844static struct rman * 845bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type) 846{ 847 switch (bhndb_get_addrspace(sc, child)) { 848 case BHNDB_ADDRSPACE_NATIVE: 849 switch (type) { 850 case SYS_RES_MEMORY: 851 return (&sc->bus_res->ht_mem_rman); 852 case SYS_RES_IRQ: 853 return (NULL); 854 default: 855 return (NULL); 856 }; 857 858 case BHNDB_ADDRSPACE_BRIDGED: 859 switch (type) { 860 case SYS_RES_MEMORY: 861 return (&sc->bus_res->br_mem_rman); 862 case SYS_RES_IRQ: 863 // TODO 864 // return &sc->irq_rman; 865 return (NULL); 866 default: 867 return (NULL); 868 }; 869 } 870} 871 872/** 873 * Default implementation of BUS_ADD_CHILD() 874 */ 875static device_t 876bhndb_add_child(device_t dev, u_int order, const char *name, int unit) 877{ 878 struct bhndb_devinfo *dinfo; 879 device_t child; 880 881 child = device_add_child_ordered(dev, order, name, unit); 882 if (child == NULL) 883 return (NULL); 884 885 dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT); 886 if (dinfo == NULL) { 887 device_delete_child(dev, child); 888 return (NULL); 889 } 890 891 dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE; 892 resource_list_init(&dinfo->resources); 893 894 device_set_ivars(child, dinfo); 895 896 return (child); 897} 898 899/** 900 * Default implementation of BUS_CHILD_DELETED(). 901 */ 902static void 903bhndb_child_deleted(device_t dev, device_t child) 904{ 905 struct bhndb_devinfo *dinfo = device_get_ivars(child); 906 if (dinfo != NULL) { 907 resource_list_free(&dinfo->resources); 908 free(dinfo, M_BHND); 909 } 910 911 device_set_ivars(child, NULL); 912} 913 914/** 915 * Default implementation of BHNDB_GET_CHIPID(). 916 */ 917static const struct bhnd_chipid * 918bhndb_get_chipid(device_t dev, device_t child) 919{ 920 struct bhndb_softc *sc = device_get_softc(dev); 921 return (&sc->chipid); 922} 923 924 925/** 926 * Default implementation of BHNDB_IS_HW_DISABLED(). 927 */ 928static bool 929bhndb_is_hw_disabled(device_t dev, device_t child) { 930 struct bhndb_softc *sc; 931 struct bhnd_core_info core; 932 933 sc = device_get_softc(dev); 934 935 /* Requestor must be attached to the bhnd bus */ 936 if (device_get_parent(child) != sc->bus_dev) { 937 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 938 } 939 940 /* Fetch core info */ 941 core = bhnd_get_core_info(child); 942 943 /* Try to defer to the bhndb bus parent */ 944 if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, &core)) 945 return (true); 946 947 /* Otherwise, we treat bridge-capable cores as unpopulated if they're 948 * not the configured host bridge */ 949 if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(&core))) 950 return (!BHND_BUS_IS_HOSTB_DEVICE(dev, child)); 951 952 /* Otherwise, assume the core is populated */ 953 return (false); 954} 955 956/* ascending core index comparison used by bhndb_is_hostb_device() */ 957static int 958compare_core_index(const void *lhs, const void *rhs) 959{ 960 u_int left = bhnd_get_core_index(*(const device_t *) lhs); 961 u_int right = bhnd_get_core_index(*(const device_t *) rhs); 962 963 if (left < right) 964 return (-1); 965 else if (left > right) 966 return (1); 967 else 968 return (0); 969} 970 971/** 972 * Default bhndb(4) implementation of BHND_BUS_IS_HOSTB_DEVICE(). 973 * 974 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged 975 * bhnd(4) devices to determine the hostb core: 976 * 977 * - The core must have a Broadcom vendor ID. 978 * - The core devclass must match the bridge type. 979 * - The core must be the first device on the bus with the bridged device 980 * class. 981 * 982 * @param sc The bridge device state. 983 * @param cores The table of bridge-enumerated cores. 984 * @param num_cores The length of @p cores. 985 * @param core The core to check. 986 */ 987static bool 988bhndb_is_hostb_device(device_t dev, device_t child) 989{ 990 struct bhndb_softc *sc; 991 struct bhnd_core_match md; 992 device_t hostb_dev, *devlist; 993 int devcnt, error; 994 995 996 sc = device_get_softc(dev); 997 998 /* Requestor must be attached to the bhnd bus */ 999 if (device_get_parent(child) != sc->bus_dev) 1000 return (BHND_BUS_IS_HOSTB_DEVICE(device_get_parent(dev), 1001 child)); 1002 1003 /* Determine required device class and set up a match descriptor. */ 1004 md = (struct bhnd_core_match) { 1005 .vendor = BHND_MFGID_BCM, 1006 .device = BHND_COREID_INVALID, 1007 .hwrev = { BHND_HWREV_INVALID, BHND_HWREV_INVALID }, 1008 .class = sc->bridge_class, 1009 .unit = 0 1010 }; 1011 1012 /* Pre-screen the device before searching over the full device list. */ 1013 if (!bhnd_device_matches(child, &md)) 1014 return (false); 1015 1016 /* Must be the absolute first matching device on the bus. */ 1017 if ((error = device_get_children(sc->bus_dev, &devlist, &devcnt))) 1018 return (false); 1019 1020 /* Sort by core index value, ascending */ 1021 qsort(devlist, devcnt, sizeof(*devlist), compare_core_index); 1022 1023 /* Find the actual hostb device */ 1024 hostb_dev = NULL; 1025 for (int i = 0; i < devcnt; i++) { 1026 if (bhnd_device_matches(devlist[i], &md)) { 1027 hostb_dev = devlist[i]; 1028 break; 1029 } 1030 } 1031 1032 /* Clean up */ 1033 free(devlist, M_TEMP); 1034 1035 return (child == hostb_dev); 1036} 1037 1038/** 1039 * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE(). 1040 */ 1041static struct resource * 1042bhndb_alloc_resource(device_t dev, device_t child, int type, 1043 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1044{ 1045 struct bhndb_softc *sc; 1046 struct resource_list_entry *rle; 1047 struct resource *rv; 1048 struct rman *rm; 1049 int error; 1050 bool passthrough, isdefault; 1051 1052 sc = device_get_softc(dev); 1053 passthrough = (device_get_parent(child) != dev); 1054 isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 1055 rle = NULL; 1056 1057 /* Populate defaults */ 1058 if (!passthrough && isdefault) { 1059 /* Fetch the resource list entry. */ 1060 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child), 1061 type, *rid); 1062 if (rle == NULL) { 1063 device_printf(dev, 1064 "default resource %#x type %d for child %s " 1065 "not found\n", *rid, type, 1066 device_get_nameunit(child)); 1067 1068 return (NULL); 1069 } 1070 1071 if (rle->res != NULL) { 1072 device_printf(dev, 1073 "resource entry %#x type %d for child %s is busy\n", 1074 *rid, type, device_get_nameunit(child)); 1075 1076 return (NULL); 1077 } 1078 1079 start = rle->start; 1080 end = rle->end; 1081 count = ulmax(count, rle->count); 1082 } 1083 1084 /* Validate resource addresses */ 1085 if (start > end || count > ((end - start) + 1)) 1086 return (NULL); 1087 1088 /* Fetch the resource manager */ 1089 rm = bhndb_get_rman(sc, child, type); 1090 if (rm == NULL) 1091 return (NULL); 1092 1093 /* Make our reservation */ 1094 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1095 child); 1096 if (rv == NULL) 1097 return (NULL); 1098 1099 rman_set_rid(rv, *rid); 1100 1101 /* Activate */ 1102 if (flags & RF_ACTIVE) { 1103 error = bus_activate_resource(child, type, *rid, rv); 1104 if (error) { 1105 device_printf(dev, 1106 "failed to activate entry %#x type %d for " 1107 "child %s: %d\n", 1108 *rid, type, device_get_nameunit(child), error); 1109 1110 rman_release_resource(rv); 1111 1112 return (NULL); 1113 } 1114 } 1115 1116 /* Update child's resource list entry */ 1117 if (rle != NULL) { 1118 rle->res = rv; 1119 rle->start = rman_get_start(rv); 1120 rle->end = rman_get_end(rv); 1121 rle->count = rman_get_size(rv); 1122 } 1123 1124 return (rv); 1125} 1126 1127/** 1128 * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE(). 1129 */ 1130static int 1131bhndb_release_resource(device_t dev, device_t child, int type, int rid, 1132 struct resource *r) 1133{ 1134 int error; 1135 1136 /* Deactivate resources */ 1137 if (rman_get_flags(r) & RF_ACTIVE) { 1138 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r); 1139 if (error) 1140 return (error); 1141 } 1142 1143 if ((error = rman_release_resource(r))) 1144 return (error); 1145 1146 return (0); 1147} 1148 1149/** 1150 * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE(). 1151 */ 1152static int 1153bhndb_adjust_resource(device_t dev, device_t child, int type, 1154 struct resource *r, rman_res_t start, rman_res_t end) 1155{ 1156 struct bhndb_softc *sc; 1157 struct rman *rm; 1158 int error; 1159 1160 sc = device_get_softc(dev); 1161 error = 0; 1162 1163 /* Fetch resource manager */ 1164 rm = bhndb_get_rman(sc, child, type); 1165 if (rm == NULL) 1166 return (ENXIO); 1167 1168 if (!rman_is_region_manager(r, rm)) 1169 return (ENXIO); 1170 1171 /* If active, adjustment is limited by the assigned window. */ 1172 BHNDB_LOCK(sc); 1173 1174 // TODO: Currently unsupported 1175 error = ENODEV; 1176 1177 BHNDB_UNLOCK(sc); 1178 if (!error) 1179 error = rman_adjust_resource(r, start, end); 1180 1181 return (error); 1182} 1183 1184/** 1185 * Initialize child resource @p r with a virtual address, tag, and handle 1186 * copied from @p parent, adjusted to contain only the range defined by 1187 * @p offsize and @p size. 1188 * 1189 * @param r The register to be initialized. 1190 * @param parent The parent bus resource that fully contains the subregion. 1191 * @param offset The subregion offset within @p parent. 1192 * @param size The subregion size. 1193 * @p r. 1194 */ 1195static int 1196bhndb_init_child_resource(struct resource *r, 1197 struct resource *parent, bhnd_size_t offset, bhnd_size_t size) 1198{ 1199 bus_space_handle_t bh, child_bh; 1200 bus_space_tag_t bt; 1201 uintptr_t vaddr; 1202 int error; 1203 1204 /* Fetch the parent resource's real bus values */ 1205 vaddr = (uintptr_t) rman_get_virtual(parent); 1206 bt = rman_get_bustag(parent); 1207 bh = rman_get_bushandle(parent); 1208 1209 /* Configure child resource with window-adjusted real bus values */ 1210 vaddr += offset; 1211 error = bus_space_subregion(bt, bh, offset, size, &child_bh); 1212 if (error) 1213 return (error); 1214 1215 rman_set_virtual(r, (void *) vaddr); 1216 rman_set_bustag(r, bt); 1217 rman_set_bushandle(r, child_bh); 1218 1219 return (0); 1220} 1221 1222/** 1223 * Attempt activation of a fixed register window mapping for @p child. 1224 * 1225 * @param sc BHNDB device state. 1226 * @param region The static region definition capable of mapping @p r. 1227 * @param child A child requesting resource activation. 1228 * @param type Resource type. 1229 * @param rid Resource identifier. 1230 * @param r Resource to be activated. 1231 * 1232 * @retval 0 if @p r was activated successfully 1233 * @retval ENOENT if no fixed register window was found. 1234 * @retval non-zero if @p r could not be activated. 1235 */ 1236static int 1237bhndb_activate_static_region(struct bhndb_softc *sc, 1238 struct bhndb_region *region, device_t child, int type, int rid, 1239 struct resource *r) 1240{ 1241 struct resource *bridge_res; 1242 const struct bhndb_regwin *win; 1243 bhnd_size_t parent_offset; 1244 rman_res_t r_start, r_size; 1245 int error; 1246 1247 win = region->static_regwin; 1248 1249 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type), 1250 ("can't activate non-static region")); 1251 1252 r_start = rman_get_start(r); 1253 r_size = rman_get_size(r); 1254 1255 /* Find the corresponding bridge resource */ 1256 bridge_res = bhndb_find_regwin_resource(sc->bus_res, win); 1257 if (bridge_res == NULL) 1258 return (ENXIO); 1259 1260 /* Calculate subregion offset within the parent resource */ 1261 parent_offset = r_start - region->addr; 1262 parent_offset += win->win_offset; 1263 1264 /* Configure resource with its real bus values. */ 1265 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size); 1266 if (error) 1267 return (error); 1268 1269 /* Mark active */ 1270 if ((error = rman_activate_resource(r))) 1271 return (error); 1272 1273 return (0); 1274} 1275 1276/** 1277 * Attempt to allocate/retain a dynamic register window for @p r, returning 1278 * the retained window. 1279 * 1280 * @param sc The bhndb driver state. 1281 * @param r The resource for which a window will be retained. 1282 */ 1283static struct bhndb_dw_alloc * 1284bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r) 1285{ 1286 struct bhndb_dw_alloc *dwa; 1287 rman_res_t r_start, r_size; 1288 int error; 1289 1290 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1291 1292 r_start = rman_get_start(r); 1293 r_size = rman_get_size(r); 1294 1295 /* Look for an existing dynamic window we can reference */ 1296 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size); 1297 if (dwa != NULL) { 1298 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0) 1299 return (dwa); 1300 1301 return (NULL); 1302 } 1303 1304 /* Otherwise, try to reserve a free window */ 1305 dwa = bhndb_dw_next_free(sc->bus_res); 1306 if (dwa == NULL) { 1307 /* No free windows */ 1308 return (NULL); 1309 } 1310 1311 /* Set the window target */ 1312 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r), 1313 rman_get_size(r)); 1314 if (error) { 1315 device_printf(sc->dev, "dynamic window initialization " 1316 "for 0x%llx-0x%llx failed\n", 1317 (unsigned long long) r_start, 1318 (unsigned long long) r_start + r_size - 1); 1319 return (NULL); 1320 } 1321 1322 /* Add our reservation */ 1323 if (bhndb_dw_retain(sc->bus_res, dwa, r)) 1324 return (NULL); 1325 1326 return (dwa); 1327} 1328 1329/** 1330 * Activate a resource using any viable static or dynamic register window. 1331 * 1332 * @param sc The bhndb driver state. 1333 * @param child The child holding ownership of @p r. 1334 * @param type The type of the resource to be activated. 1335 * @param rid The resource ID of @p r. 1336 * @param r The resource to be activated 1337 * @param[out] indirect On error and if not NULL, will be set to 'true' if 1338 * the caller should instead use an indirect resource mapping. 1339 * 1340 * @retval 0 success 1341 * @retval non-zero activation failed. 1342 */ 1343static int 1344bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type, 1345 int rid, struct resource *r, bool *indirect) 1346{ 1347 struct bhndb_region *region; 1348 struct bhndb_dw_alloc *dwa; 1349 bhndb_priority_t dw_priority; 1350 rman_res_t r_start, r_size; 1351 rman_res_t parent_offset; 1352 int error; 1353 1354 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED); 1355 1356 // TODO - IRQs 1357 if (type != SYS_RES_MEMORY) 1358 return (ENXIO); 1359 1360 if (indirect) 1361 *indirect = false; 1362 1363 r_start = rman_get_start(r); 1364 r_size = rman_get_size(r); 1365 1366 /* Activate native addrspace resources using the host address space */ 1367 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) { 1368 struct resource *parent; 1369 1370 /* Find the bridge resource referenced by the child */ 1371 parent = bhndb_find_resource_range(sc->bus_res, r_start, 1372 r_size); 1373 if (parent == NULL) { 1374 device_printf(sc->dev, "host resource not found " 1375 "for 0x%llx-0x%llx\n", 1376 (unsigned long long) r_start, 1377 (unsigned long long) r_start + r_size - 1); 1378 return (ENOENT); 1379 } 1380 1381 /* Initialize child resource with the real bus values */ 1382 error = bhndb_init_child_resource(r, parent, 1383 r_start - rman_get_start(parent), r_size); 1384 if (error) 1385 return (error); 1386 1387 /* Try to activate child resource */ 1388 return (rman_activate_resource(r)); 1389 } 1390 1391 /* Default to low priority */ 1392 dw_priority = BHNDB_PRIORITY_LOW; 1393 1394 /* Look for a bus region matching the resource's address range */ 1395 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size); 1396 if (region != NULL) 1397 dw_priority = region->priority; 1398 1399 /* Prefer static mappings over consuming a dynamic windows. */ 1400 if (region && region->static_regwin) { 1401 error = bhndb_activate_static_region(sc, region, child, type, 1402 rid, r); 1403 if (error) 1404 device_printf(sc->dev, "static window allocation " 1405 "for 0x%llx-0x%llx failed\n", 1406 (unsigned long long) r_start, 1407 (unsigned long long) r_start + r_size - 1); 1408 return (error); 1409 } 1410 1411 /* A dynamic window will be required; is this resource high enough 1412 * priority to be reserved a dynamic window? */ 1413 if (dw_priority < sc->bus_res->min_prio) { 1414 if (indirect) 1415 *indirect = true; 1416 1417 return (ENOMEM); 1418 } 1419 1420 /* Find and retain a usable window */ 1421 BHNDB_LOCK(sc); { 1422 dwa = bhndb_retain_dynamic_window(sc, r); 1423 } BHNDB_UNLOCK(sc); 1424 1425 if (dwa == NULL) { 1426 if (indirect) 1427 *indirect = true; 1428 return (ENOMEM); 1429 } 1430 1431 /* Configure resource with its real bus values. */ 1432 parent_offset = dwa->win->win_offset; 1433 parent_offset += r_start - dwa->target; 1434 1435 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset, 1436 dwa->win->win_size); 1437 if (error) 1438 goto failed; 1439 1440 /* Mark active */ 1441 if ((error = rman_activate_resource(r))) 1442 goto failed; 1443 1444 return (0); 1445 1446failed: 1447 /* Release our region allocation. */ 1448 BHNDB_LOCK(sc); 1449 bhndb_dw_release(sc->bus_res, dwa, r); 1450 BHNDB_UNLOCK(sc); 1451 1452 return (error); 1453} 1454 1455/** 1456 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE(). 1457 * 1458 * Maps resource activation requests to a viable static or dynamic 1459 * register window, if any. 1460 */ 1461static int 1462bhndb_activate_resource(device_t dev, device_t child, int type, int rid, 1463 struct resource *r) 1464{ 1465 struct bhndb_softc *sc = device_get_softc(dev); 1466 1467 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL)); 1468} 1469 1470/** 1471 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE(). 1472 */ 1473static int 1474bhndb_deactivate_resource(device_t dev, device_t child, int type, 1475 int rid, struct resource *r) 1476{ 1477 struct bhndb_dw_alloc *dwa; 1478 struct bhndb_softc *sc; 1479 struct rman *rm; 1480 int error; 1481 1482 sc = device_get_softc(dev); 1483 1484 if ((rm = bhndb_get_rman(sc, child, type)) == NULL) 1485 return (EINVAL); 1486 1487 /* Mark inactive */ 1488 if ((error = rman_deactivate_resource(r))) 1489 return (error); 1490 1491 /* Free any dynamic window allocation. */ 1492 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1493 BHNDB_LOCK(sc); 1494 dwa = bhndb_dw_find_resource(sc->bus_res, r); 1495 if (dwa != NULL) 1496 bhndb_dw_release(sc->bus_res, dwa, r); 1497 BHNDB_UNLOCK(sc); 1498 } 1499 1500 return (0); 1501} 1502 1503/** 1504 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST(). 1505 */ 1506static struct resource_list * 1507bhndb_get_resource_list(device_t dev, device_t child) 1508{ 1509 struct bhndb_devinfo *dinfo = device_get_ivars(child); 1510 return (&dinfo->resources); 1511} 1512 1513/** 1514 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE(). 1515 * 1516 * For BHNDB_ADDRSPACE_NATIVE children, all resources may be assumed to 1517 * be activated by the bridge. 1518 * 1519 * For BHNDB_ADDRSPACE_BRIDGED children, attempts to activate a static register 1520 * window, a dynamic register window, or configures @p r as an indirect 1521 * resource -- in that order. 1522 */ 1523static int 1524bhndb_activate_bhnd_resource(device_t dev, device_t child, 1525 int type, int rid, struct bhnd_resource *r) 1526{ 1527 struct bhndb_softc *sc; 1528 struct bhndb_region *region; 1529 rman_res_t r_start, r_size; 1530 int error; 1531 bool indirect; 1532 1533 KASSERT(!r->direct, 1534 ("direct flag set on inactive resource")); 1535 1536 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE), 1537 ("RF_ACTIVE set on inactive resource")); 1538 1539 sc = device_get_softc(dev); 1540 1541 r_start = rman_get_start(r->res); 1542 r_size = rman_get_size(r->res); 1543 1544 /* Verify bridged address range's resource priority, and skip direct 1545 * allocation if the priority is too low. */ 1546 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) { 1547 bhndb_priority_t r_prio; 1548 1549 region = bhndb_find_resource_region(sc->bus_res, 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, this 1556 * resource should always be indirect. */ 1557 if (r_prio < sc->bus_res->min_prio) 1558 return (0); 1559 } 1560 1561 /* Attempt direct activation */ 1562 error = bhndb_try_activate_resource(sc, child, type, rid, r->res, 1563 &indirect); 1564 if (!error) { 1565 r->direct = true; 1566 } else if (indirect) { 1567 /* The request was valid, but no viable register window is 1568 * available; indirection must be employed. */ 1569 error = 0; 1570 r->direct = false; 1571 } 1572 1573 if (BHNDB_DEBUG(PRIO) && 1574 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) 1575 { 1576 device_printf(child, "activated 0x%llx-0x%llx as %s " 1577 "resource\n", 1578 (unsigned long long) r_start, 1579 (unsigned long long) r_start + r_size - 1, 1580 r->direct ? "direct" : "indirect"); 1581 } 1582 1583 return (error); 1584}; 1585 1586/** 1587 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE(). 1588 */ 1589static int 1590bhndb_deactivate_bhnd_resource(device_t dev, device_t child, 1591 int type, int rid, struct bhnd_resource *r) 1592{ 1593 int error; 1594 1595 /* Indirect resources don't require activation */ 1596 if (!r->direct) 1597 return (0); 1598 1599 KASSERT(rman_get_flags(r->res) & RF_ACTIVE, 1600 ("RF_ACTIVE not set on direct resource")); 1601 1602 /* Perform deactivation */ 1603 error = bus_deactivate_resource(child, type, rid, r->res); 1604 if (!error) 1605 r->direct = false; 1606 1607 return (error); 1608}; 1609 1610/** 1611 * Slow path for bhndb_io_resource(). 1612 * 1613 * Iterates over the existing allocated dynamic windows looking for a viable 1614 * in-use region; the first matching region is returned. 1615 */ 1616static struct bhndb_dw_alloc * 1617bhndb_io_resource_slow(struct bhndb_softc *sc, bus_addr_t addr, 1618 bus_size_t size, bus_size_t *offset) 1619{ 1620 struct bhndb_resources *br; 1621 struct bhndb_dw_alloc *dwa; 1622 1623 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1624 1625 br = sc->bus_res; 1626 1627 /* Search for an existing dynamic mapping of this address range. 1628 * Static regions are not searched, as a statically mapped 1629 * region would never be allocated as an indirect resource. */ 1630 for (size_t i = 0; i < br->dwa_count; i++) { 1631 const struct bhndb_regwin *win; 1632 1633 dwa = &br->dw_alloc[i]; 1634 win = dwa->win; 1635 1636 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN, 1637 ("invalid register window type")); 1638 1639 /* Verify the range */ 1640 if (addr < dwa->target) 1641 continue; 1642 1643 if (addr + size > dwa->target + win->win_size) 1644 continue; 1645 1646 /* Found */ 1647 *offset = dwa->win->win_offset; 1648 *offset += addr - dwa->target; 1649 1650 return (dwa); 1651 } 1652 1653 /* not found */ 1654 return (NULL); 1655} 1656 1657/** 1658 * Find the bridge resource to be used for I/O requests. 1659 * 1660 * @param sc Bridge driver state. 1661 * @param addr The I/O target address. 1662 * @param size The size of the I/O operation to be performed at @p addr. 1663 * @param[out] offset The offset within the returned resource at which 1664 * to perform the I/O request. 1665 */ 1666static inline struct bhndb_dw_alloc * 1667bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size, 1668 bus_size_t *offset) 1669{ 1670 struct bhndb_resources *br; 1671 struct bhndb_dw_alloc *dwa; 1672 int error; 1673 1674 BHNDB_LOCK_ASSERT(sc, MA_OWNED); 1675 1676 br = sc->bus_res; 1677 1678 /* Try to fetch a free window */ 1679 dwa = bhndb_dw_next_free(br); 1680 1681 /* 1682 * If no dynamic windows are available, look for an existing 1683 * region that maps the target range. 1684 * 1685 * If none are found, this is a child driver bug -- our window 1686 * over-commit should only fail in the case where a child driver leaks 1687 * resources, or perform operations out-of-order. 1688 * 1689 * Broadcom HND chipsets are designed to not require register window 1690 * swapping during execution; as long as the child devices are 1691 * attached/detached correctly, using the hardware's required order 1692 * of operations, there should always be a window available for the 1693 * current operation. 1694 */ 1695 if (dwa == NULL) { 1696 dwa = bhndb_io_resource_slow(sc, addr, size, offset); 1697 if (dwa == NULL) { 1698 panic("register windows exhausted attempting to map " 1699 "0x%llx-0x%llx\n", 1700 (unsigned long long) addr, 1701 (unsigned long long) addr+size-1); 1702 } 1703 1704 return (dwa); 1705 } 1706 1707 /* Adjust the window if the I/O request won't fit in the current 1708 * target range. */ 1709 if (addr < dwa->target || 1710 (dwa->target + dwa->win->win_size) - addr < size) 1711 { 1712 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr, 1713 size); 1714 if (error) { 1715 panic("failed to set register window target mapping " 1716 "0x%llx-0x%llx\n", 1717 (unsigned long long) addr, 1718 (unsigned long long) addr+size-1); 1719 } 1720 } 1721 1722 /* Calculate the offset and return */ 1723 *offset = (addr - dwa->target) + dwa->win->win_offset; 1724 return (dwa); 1725} 1726 1727/* 1728 * BHND_BUS_(READ|WRITE_* implementations 1729 */ 1730 1731/* bhndb_bus_(read|write) common implementation */ 1732#define BHNDB_IO_COMMON_SETUP(_io_size) \ 1733 struct bhndb_softc *sc; \ 1734 struct bhndb_dw_alloc *dwa; \ 1735 struct resource *io_res; \ 1736 bus_size_t io_offset; \ 1737 \ 1738 sc = device_get_softc(dev); \ 1739 \ 1740 BHNDB_LOCK(sc); \ 1741 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \ 1742 offset, _io_size, &io_offset); \ 1743 io_res = dwa->parent_res; \ 1744 \ 1745 KASSERT(!r->direct, \ 1746 ("bhnd_bus slow path used for direct resource")); \ 1747 \ 1748 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \ 1749 ("i/o resource is not active")); 1750 1751#define BHNDB_IO_COMMON_TEARDOWN() \ 1752 BHNDB_UNLOCK(sc); 1753 1754/* Defines a bhndb_bus_read_* method implementation */ 1755#define BHNDB_IO_READ(_type, _size) \ 1756static _type \ 1757bhndb_bus_read_ ## _size (device_t dev, device_t child, \ 1758 struct bhnd_resource *r, bus_size_t offset) \ 1759{ \ 1760 _type v; \ 1761 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1762 v = bus_read_ ## _size (io_res, io_offset); \ 1763 BHNDB_IO_COMMON_TEARDOWN(); \ 1764 \ 1765 return (v); \ 1766} 1767 1768/* Defines a bhndb_bus_write_* method implementation */ 1769#define BHNDB_IO_WRITE(_type, _size) \ 1770static void \ 1771bhndb_bus_write_ ## _size (device_t dev, device_t child, \ 1772 struct bhnd_resource *r, bus_size_t offset, _type value) \ 1773{ \ 1774 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \ 1775 bus_write_ ## _size (io_res, io_offset, value); \ 1776 BHNDB_IO_COMMON_TEARDOWN(); \ 1777} 1778 1779BHNDB_IO_READ(uint8_t, 1); 1780BHNDB_IO_READ(uint16_t, 2); 1781BHNDB_IO_READ(uint32_t, 4); 1782 1783BHNDB_IO_WRITE(uint8_t, 1); 1784BHNDB_IO_WRITE(uint16_t, 2); 1785BHNDB_IO_WRITE(uint32_t, 4); 1786 1787/** 1788 * Default bhndb(4) implementation of BHND_BUS_BARRIER(). 1789 */ 1790static void 1791bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r, 1792 bus_size_t offset, bus_size_t length, int flags) 1793{ 1794 bus_size_t remain; 1795 1796 BHNDB_IO_COMMON_SETUP(length); 1797 1798 /* TODO: It's unclear whether we need a barrier implementation, 1799 * and if we do, what it needs to actually do. This may need 1800 * revisiting once we have a better idea of requirements after 1801 * porting the core drivers. */ 1802 panic("implementation incorrect"); 1803 1804 /* Use 4-byte reads where possible */ 1805 remain = length % sizeof(uint32_t); 1806 for (bus_size_t i = 0; i < (length - remain); i += 4) 1807 bus_read_4(io_res, io_offset + offset + i); 1808 1809 /* Use 1 byte reads for the remainder */ 1810 for (bus_size_t i = 0; i < remain; i++) 1811 bus_read_1(io_res, io_offset + offset + length + i); 1812 1813 BHNDB_IO_COMMON_TEARDOWN(); 1814} 1815 1816/** 1817 * Default bhndb(4) implementation of BUS_SETUP_INTR(). 1818 */ 1819static int 1820bhndb_setup_intr(device_t dev, device_t child, struct resource *r, 1821 int flags, driver_filter_t filter, driver_intr_t handler, void *arg, 1822 void **cookiep) 1823{ 1824 // TODO 1825 return (EOPNOTSUPP); 1826} 1827 1828/** 1829 * Default bhndb(4) implementation of BUS_TEARDOWN_INTR(). 1830 */ 1831static int 1832bhndb_teardown_intr(device_t dev, device_t child, struct resource *r, 1833 void *cookie) 1834{ 1835 // TODO 1836 return (EOPNOTSUPP); 1837} 1838 1839/** 1840 * Default bhndb(4) implementation of BUS_CONFIG_INTR(). 1841 */ 1842static int 1843bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig, 1844 enum intr_polarity pol) 1845{ 1846 // TODO 1847 return (EOPNOTSUPP); 1848} 1849 1850/** 1851 * Default bhndb(4) implementation of BUS_BIND_INTR(). 1852 */ 1853static int 1854bhndb_bind_intr(device_t dev, device_t child, struct resource *r, int cpu) 1855{ 1856 // TODO 1857 return (EOPNOTSUPP); 1858} 1859 1860/** 1861 * Default bhndb(4) implementation of BUS_DESCRIBE_INTR(). 1862 */ 1863static int 1864bhndb_describe_intr(device_t dev, device_t child, struct resource *irq, void *cookie, 1865 const char *descr) 1866{ 1867 // TODO 1868 return (EOPNOTSUPP); 1869} 1870 1871/** 1872 * Default bhndb(4) implementation of BUS_GET_DMA_TAG(). 1873 */ 1874static bus_dma_tag_t 1875bhndb_get_dma_tag(device_t dev, device_t child) 1876{ 1877 // TODO 1878 return (NULL); 1879} 1880 1881static device_method_t bhndb_methods[] = { 1882 /* Device interface */ \ 1883 DEVMETHOD(device_probe, bhndb_generic_probe), 1884 DEVMETHOD(device_detach, bhndb_generic_detach), 1885 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1886 DEVMETHOD(device_suspend, bhndb_generic_suspend), 1887 DEVMETHOD(device_resume, bhndb_generic_resume), 1888 1889 /* Bus interface */ 1890 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch), 1891 DEVMETHOD(bus_print_child, bhndb_print_child), 1892 DEVMETHOD(bus_child_pnpinfo_str, bhndb_child_pnpinfo_str), 1893 DEVMETHOD(bus_child_location_str, bhndb_child_location_str), 1894 DEVMETHOD(bus_add_child, bhndb_add_child), 1895 DEVMETHOD(bus_child_deleted, bhndb_child_deleted), 1896 1897 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource), 1898 DEVMETHOD(bus_release_resource, bhndb_release_resource), 1899 DEVMETHOD(bus_activate_resource, bhndb_activate_resource), 1900 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource), 1901 1902 DEVMETHOD(bus_setup_intr, bhndb_setup_intr), 1903 DEVMETHOD(bus_teardown_intr, bhndb_teardown_intr), 1904 DEVMETHOD(bus_config_intr, bhndb_config_intr), 1905 DEVMETHOD(bus_bind_intr, bhndb_bind_intr), 1906 DEVMETHOD(bus_describe_intr, bhndb_describe_intr), 1907 1908 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag), 1909 1910 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource), 1911 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 1912 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 1913 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 1914 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list), 1915 1916 DEVMETHOD(bus_read_ivar, bhndb_read_ivar), 1917 DEVMETHOD(bus_write_ivar, bhndb_write_ivar), 1918 1919 /* BHNDB interface */ 1920 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid), 1921 DEVMETHOD(bhndb_init_full_config, bhndb_generic_init_full_config), 1922 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource), 1923 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource), 1924 1925 /* BHND interface */ 1926 DEVMETHOD(bhnd_bus_is_hw_disabled, bhndb_is_hw_disabled), 1927 DEVMETHOD(bhnd_bus_is_hostb_device, bhndb_is_hostb_device), 1928 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid), 1929 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource), 1930 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource), 1931 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1), 1932 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2), 1933 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4), 1934 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1), 1935 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2), 1936 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4), 1937 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier), 1938 1939 DEVMETHOD_END 1940}; 1941 1942devclass_t bhndb_devclass; 1943 1944DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc)); 1945 1946MODULE_VERSION(bhndb, 1); 1947MODULE_DEPEND(bhndb, bhnd, 1, 1, 1); 1948MODULE_DEPEND(bhndb, bhnd_chipc, 1, 1, 1); 1949