device_util.c
1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 3 #include <assert.h> 4 #include <commonlib/bsd/helpers.h> 5 #include <console/console.h> 6 #include <device/device.h> 7 #include <device/pci_def.h> 8 #include <device/pci_ids.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <types.h> 13 14 /** 15 * Given a Local APIC ID, find the device structure. 16 * 17 * @param apic_id The Local APIC ID number. 18 * @return Pointer to the device structure (if found), 0 otherwise. 19 */ 20 struct device *dev_find_lapic(unsigned int apic_id) 21 { 22 struct device *dev; 23 struct device *result = NULL; 24 25 for (dev = all_devices; dev; dev = dev->next) { 26 if (dev->path.type == DEVICE_PATH_APIC && 27 dev->path.apic.apic_id == apic_id) { 28 result = dev; 29 break; 30 } 31 } 32 return result; 33 } 34 35 /** 36 * Find a device of a given vendor and type. 37 * 38 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel). 39 * @param device A PCI device ID. 40 * @param from Pointer to the device structure, used as a starting point in 41 * the linked list of all_devices, which can be 0 to start at the 42 * head of the list (i.e. all_devices). 43 * @return Pointer to the device struct. 44 */ 45 struct device *dev_find_device(u16 vendor, u16 device, struct device *from) 46 { 47 if (!from) 48 from = all_devices; 49 else 50 from = from->next; 51 52 while (from && (from->vendor != vendor || from->device != device)) 53 from = from->next; 54 55 return from; 56 } 57 58 /** 59 * Find a device of a given class. 60 * 61 * @param class Class of the device. 62 * @param from Pointer to the device structure, used as a starting point in 63 * the linked list of all_devices, which can be 0 to start at the 64 * head of the list (i.e. all_devices). 65 * @return Pointer to the device struct. 66 */ 67 struct device *dev_find_class(unsigned int class, struct device *from) 68 { 69 if (!from) 70 from = all_devices; 71 else 72 from = from->next; 73 74 while (from && (from->class & 0xffffff00) != class) 75 from = from->next; 76 77 return from; 78 } 79 80 /** 81 * Encode the device path into 3 bytes for logging to CMOS. 82 * 83 * @param dev The device path to encode. 84 * @return Device path encoded into lower 3 bytes of dword. 85 */ 86 u32 dev_path_encode(const struct device *dev) 87 { 88 u32 ret; 89 90 if (!dev) 91 return 0; 92 93 /* Store the device type in 3rd byte. */ 94 ret = dev->path.type << 16; 95 96 /* Encode the device specific path in the low word. */ 97 switch (dev->path.type) { 98 case DEVICE_PATH_ROOT: 99 break; 100 case DEVICE_PATH_PCI: 101 ret |= dev->upstream->segment_group << 16 | dev->upstream->secondary << 8 | dev->path.pci.devfn; 102 break; 103 case DEVICE_PATH_PNP: 104 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device; 105 break; 106 case DEVICE_PATH_I2C: 107 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device; 108 break; 109 case DEVICE_PATH_APIC: 110 ret |= dev->path.apic.apic_id; 111 break; 112 case DEVICE_PATH_DOMAIN: 113 ret |= dev->path.domain.domain_id; 114 break; 115 case DEVICE_PATH_CPU_CLUSTER: 116 ret |= dev->path.cpu_cluster.cluster; 117 break; 118 case DEVICE_PATH_CPU: 119 ret |= dev->path.cpu.id; 120 break; 121 case DEVICE_PATH_CPU_BUS: 122 ret |= dev->path.cpu_bus.id; 123 break; 124 case DEVICE_PATH_IOAPIC: 125 ret |= dev->path.ioapic.ioapic_id; 126 break; 127 case DEVICE_PATH_GENERIC: 128 ret |= dev->path.generic.subid << 8 | dev->path.generic.id; 129 break; 130 case DEVICE_PATH_SPI: 131 ret |= dev->path.spi.cs; 132 break; 133 case DEVICE_PATH_USB: 134 ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id; 135 break; 136 case DEVICE_PATH_GPIO: 137 ret |= dev->path.gpio.id; 138 break; 139 case DEVICE_PATH_MDIO: 140 ret |= dev->path.mdio.addr; 141 break; 142 case DEVICE_PATH_NONE: 143 case DEVICE_PATH_MMIO: /* don't care */ 144 default: 145 break; 146 } 147 148 return ret; 149 } 150 151 /* 152 * Warning: This function uses a static buffer. Don't call it more than once 153 * from the same print statement! 154 */ 155 const char *dev_path(const struct device *dev) 156 { 157 static char buffer[DEVICE_PATH_MAX]; 158 159 buffer[0] = '\0'; 160 if (!dev) { 161 strcpy(buffer, "<null>"); 162 } else { 163 switch (dev->path.type) { 164 case DEVICE_PATH_NONE: 165 strcpy(buffer, "NONE"); 166 break; 167 case DEVICE_PATH_ROOT: 168 strcpy(buffer, "Root Device"); 169 break; 170 case DEVICE_PATH_PCI: 171 snprintf(buffer, sizeof(buffer), 172 "PCI: %02x:%02x:%02x.%01x", 173 dev->upstream->segment_group, 174 dev->upstream->secondary, 175 PCI_SLOT(dev->path.pci.devfn), 176 PCI_FUNC(dev->path.pci.devfn)); 177 break; 178 case DEVICE_PATH_PNP: 179 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x", 180 dev->path.pnp.port, dev->path.pnp.device); 181 break; 182 case DEVICE_PATH_I2C: 183 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x", 184 dev->upstream->secondary, 185 dev->path.i2c.device); 186 break; 187 case DEVICE_PATH_APIC: 188 snprintf(buffer, sizeof(buffer), "APIC: %02x", 189 dev->path.apic.apic_id); 190 break; 191 case DEVICE_PATH_IOAPIC: 192 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x", 193 dev->path.ioapic.ioapic_id); 194 break; 195 case DEVICE_PATH_DOMAIN: 196 snprintf(buffer, sizeof(buffer), "DOMAIN: %08x", 197 dev->path.domain.domain_id); 198 break; 199 case DEVICE_PATH_CPU_CLUSTER: 200 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x", 201 dev->path.cpu_cluster.cluster); 202 break; 203 case DEVICE_PATH_CPU: 204 snprintf(buffer, sizeof(buffer), 205 "CPU: %02x", dev->path.cpu.id); 206 break; 207 case DEVICE_PATH_CPU_BUS: 208 snprintf(buffer, sizeof(buffer), 209 "CPU_BUS: %02x", dev->path.cpu_bus.id); 210 break; 211 case DEVICE_PATH_GENERIC: 212 snprintf(buffer, sizeof(buffer), 213 "GENERIC: %d.%d", dev->path.generic.id, 214 dev->path.generic.subid); 215 break; 216 case DEVICE_PATH_SPI: 217 snprintf(buffer, sizeof(buffer), "SPI: %02x", 218 dev->path.spi.cs); 219 break; 220 case DEVICE_PATH_USB: 221 snprintf(buffer, sizeof(buffer), "USB%u port %u", 222 dev->path.usb.port_type, dev->path.usb.port_id); 223 break; 224 case DEVICE_PATH_MMIO: 225 snprintf(buffer, sizeof(buffer), "MMIO: %08lx", 226 dev->path.mmio.addr); 227 break; 228 case DEVICE_PATH_GPIO: 229 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id); 230 break; 231 case DEVICE_PATH_MDIO: 232 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr); 233 break; 234 case DEVICE_PATH_GICC_V3: 235 snprintf(buffer, sizeof(buffer), "GICV3: %02x", dev->path.gicc_v3.mpidr); 236 break; 237 default: 238 printk(BIOS_ERR, "Unknown device path type: %d\n", 239 dev->path.type); 240 break; 241 } 242 } 243 return buffer; 244 } 245 246 const char *dev_name(const struct device *dev) 247 { 248 if (dev->name) 249 return dev->name; 250 else if (dev->chip_ops && dev->chip_ops->name) 251 return dev->chip_ops->name; 252 else 253 return "unknown"; 254 } 255 256 /* Returns the domain for the given device */ 257 const struct device *dev_get_domain(const struct device *dev) 258 { 259 /* Walk up the tree up to the domain */ 260 while (dev && dev->upstream && !is_root_device(dev)) { 261 if (dev->path.type == DEVICE_PATH_DOMAIN) 262 return dev; 263 dev = dev->upstream->dev; 264 } 265 266 return NULL; 267 } 268 269 unsigned int dev_get_domain_id(const struct device *dev) 270 { 271 const struct device *domain_dev = dev_get_domain(dev); 272 273 assert(domain_dev); 274 275 if (!domain_dev) { 276 printk(BIOS_ERR, "%s: doesn't have a domain device\n", dev_path(dev)); 277 return 0; 278 } 279 280 return domain_dev->path.domain.domain_id; 281 } 282 283 bool is_domain0(const struct device *dev) 284 { 285 return dev && dev->path.type == DEVICE_PATH_DOMAIN && dev->path.domain.domain_id == 0; 286 } 287 288 bool is_dev_on_domain0(const struct device *dev) 289 { 290 return is_domain0(dev_get_domain(dev)); 291 } 292 293 /** 294 * Allocate 64 more resources to the free list. 295 * 296 * @return TODO. 297 */ 298 static int allocate_more_resources(void) 299 { 300 int i; 301 struct resource *new_res_list; 302 303 new_res_list = malloc(64 * sizeof(*new_res_list)); 304 305 if (new_res_list == NULL) 306 return 0; 307 308 memset(new_res_list, 0, 64 * sizeof(*new_res_list)); 309 310 for (i = 0; i < 64 - 1; i++) 311 new_res_list[i].next = &new_res_list[i+1]; 312 313 free_resources = new_res_list; 314 return 1; 315 } 316 317 /** 318 * Remove resource res from the device's list and add it to the free list. 319 * 320 * @param dev TODO 321 * @param res TODO 322 * @param prev TODO 323 * @return TODO. 324 */ 325 static void free_resource(struct device *dev, struct resource *res, 326 struct resource *prev) 327 { 328 if (prev) 329 prev->next = res->next; 330 else 331 dev->resource_list = res->next; 332 333 res->next = free_resources; 334 free_resources = res; 335 } 336 337 /** 338 * See if we have unused but allocated resource structures. 339 * 340 * If so remove the allocation. 341 * 342 * @param dev The device to find the resource on. 343 */ 344 void compact_resources(struct device *dev) 345 { 346 struct resource *res, *next, *prev = NULL; 347 348 /* Move all of the free resources to the end */ 349 for (res = dev->resource_list; res; res = next) { 350 next = res->next; 351 if (!res->flags) 352 free_resource(dev, res, prev); 353 else 354 prev = res; 355 } 356 } 357 358 /** 359 * See if a resource structure already exists for a given index. 360 * 361 * @param dev The device to find the resource on. 362 * @param index The index of the resource on the device. 363 * @return The resource, if it already exists. 364 */ 365 struct resource *probe_resource(const struct device *dev, unsigned int index) 366 { 367 struct resource *res; 368 369 /* See if there is a resource with the appropriate index */ 370 for (res = dev->resource_list; res; res = res->next) { 371 if (res->index == index) 372 break; 373 } 374 375 return res; 376 } 377 378 /** 379 * See if a resource structure already exists for a given index and if not 380 * allocate one. 381 * 382 * Then initialize the resource to default values. 383 * 384 * @param dev The device to find the resource on. 385 * @param index The index of the resource on the device. 386 * @return TODO. 387 */ 388 struct resource *new_resource(struct device *dev, unsigned int index) 389 { 390 struct resource *resource, *tail; 391 392 /* First move all of the free resources to the end. */ 393 compact_resources(dev); 394 395 /* See if there is a resource with the appropriate index. */ 396 resource = probe_resource(dev, index); 397 if (!resource) { 398 if (free_resources == NULL && !allocate_more_resources()) 399 die("Couldn't allocate more resources."); 400 401 resource = free_resources; 402 free_resources = free_resources->next; 403 memset(resource, 0, sizeof(*resource)); 404 resource->next = NULL; 405 tail = dev->resource_list; 406 if (tail) { 407 while (tail->next) 408 tail = tail->next; 409 tail->next = resource; 410 } else { 411 dev->resource_list = resource; 412 } 413 } 414 415 /* Initialize the resource values. */ 416 if (!(resource->flags & IORESOURCE_FIXED)) { 417 resource->flags = 0; 418 resource->base = 0; 419 } 420 resource->size = 0; 421 resource->limit = 0; 422 resource->index = index; 423 resource->align = 0; 424 resource->gran = 0; 425 426 return resource; 427 } 428 429 /** 430 * Return an existing resource structure for a given index. 431 * 432 * @param dev The device to find the resource on. 433 * @param index The index of the resource on the device. 434 * return TODO. 435 */ 436 struct resource *find_resource(const struct device *dev, unsigned int index) 437 { 438 struct resource *resource; 439 440 /* See if there is a resource with the appropriate index. */ 441 resource = probe_resource(dev, index); 442 if (!resource) 443 die("%s missing resource: %02x\n", dev_path(dev), index); 444 return resource; 445 } 446 447 /** 448 * Round a number up to the next multiple of gran. 449 * 450 * @param val The starting value. 451 * @param gran Granularity we are aligning the number to. 452 * @return The aligned value. 453 */ 454 static resource_t align_up(resource_t val, unsigned long gran) 455 { 456 resource_t mask; 457 mask = (1ULL << gran) - 1ULL; 458 val += mask; 459 val &= ~mask; 460 return val; 461 } 462 463 /** 464 * Round a number up to the previous multiple of gran. 465 * 466 * @param val The starting value. 467 * @param gran Granularity we are aligning the number to. 468 * @return The aligned value. 469 */ 470 static resource_t align_down(resource_t val, unsigned long gran) 471 { 472 resource_t mask; 473 mask = (1ULL << gran) - 1ULL; 474 val &= ~mask; 475 return val; 476 } 477 478 /** 479 * Compute the maximum address that is part of a resource. 480 * 481 * @param resource The resource whose limit is desired. 482 * @return The end. 483 */ 484 resource_t resource_end(const struct resource *resource) 485 { 486 resource_t base, end; 487 488 /* Get the base address. */ 489 base = resource->base; 490 491 /* 492 * For a non bridge resource granularity and alignment are the same. 493 * For a bridge resource align is the largest needed alignment below 494 * the bridge. While the granularity is simply how many low bits of 495 * the address cannot be set. 496 */ 497 498 /* Get the end (rounded up). */ 499 end = base + align_up(resource->size, resource->gran) - 1; 500 501 return end; 502 } 503 504 /** 505 * Compute the maximum legal value for resource->base. 506 * 507 * @param resource The resource whose maximum is desired. 508 * @return The maximum. 509 */ 510 resource_t resource_max(const struct resource *resource) 511 { 512 resource_t max; 513 514 max = align_down(resource->limit - resource->size + 1, resource->align); 515 516 return max; 517 } 518 519 /** 520 * Return the resource type of a resource. 521 * 522 * @param resource The resource type to decode. 523 * @return TODO. 524 */ 525 const char *resource_type(const struct resource *resource) 526 { 527 static char buffer[RESOURCE_TYPE_MAX]; 528 snprintf(buffer, sizeof(buffer), "%s%s%s%s", 529 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""), 530 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""), 531 ((resource->flags == 0) ? "unused" : 532 (resource->flags & IORESOURCE_IO) ? "io" : 533 (resource->flags & IORESOURCE_DRQ) ? "drq" : 534 (resource->flags & IORESOURCE_IRQ) ? "irq" : 535 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"), 536 ((resource->flags & IORESOURCE_PCI64) ? "64" : "")); 537 return buffer; 538 } 539 540 /** 541 * Print the resource that was just stored. 542 * 543 * @param dev The device the stored resource lives on. 544 * @param resource The resource that was just stored. 545 * @param comment TODO 546 */ 547 void report_resource_stored(struct device *dev, const struct resource *resource, 548 const char *comment) 549 { 550 char buf[16]; 551 unsigned long long base, end; 552 553 if (!(resource->flags & IORESOURCE_STORED)) 554 return; 555 556 base = resource->base; 557 end = resource_end(resource); 558 buf[0] = '\0'; 559 560 if (dev->downstream && (resource->flags & IORESOURCE_PCI_BRIDGE)) { 561 snprintf(buf, sizeof(buf), 562 "seg %02x bus %02x ", dev->downstream->segment_group, 563 dev->downstream->secondary); 564 } 565 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx " 566 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index, 567 base, end, resource->size, resource->gran, buf, 568 resource_type(resource), comment); 569 } 570 571 void search_bus_resources(struct bus *bus, unsigned long type_mask, 572 unsigned long type, resource_search_t search, 573 void *gp) 574 { 575 struct device *curdev; 576 577 for (curdev = bus->children; curdev; curdev = curdev->sibling) { 578 struct resource *res; 579 580 /* Ignore disabled devices. */ 581 if (!curdev->enabled) 582 continue; 583 584 for (res = curdev->resource_list; res; res = res->next) { 585 /* If it isn't the right kind of resource ignore it. */ 586 if ((res->flags & type_mask) != type) 587 continue; 588 589 /* If it is a subtractive resource recurse. */ 590 if (res->flags & IORESOURCE_SUBTRACTIVE) { 591 if (curdev->downstream) 592 search_bus_resources(curdev->downstream, type_mask, type, 593 search, gp); 594 continue; 595 } 596 search(gp, curdev, res); 597 } 598 } 599 } 600 601 void search_global_resources(unsigned long type_mask, unsigned long type, 602 resource_search_t search, void *gp) 603 { 604 struct device *curdev; 605 606 for (curdev = all_devices; curdev; curdev = curdev->next) { 607 struct resource *res; 608 609 /* Ignore disabled devices. */ 610 if (!curdev->enabled) 611 continue; 612 613 for (res = curdev->resource_list; res; res = res->next) { 614 /* If it isn't the right kind of resource ignore it. */ 615 if ((res->flags & type_mask) != type) 616 continue; 617 618 /* If it is a subtractive resource ignore it. */ 619 if (res->flags & IORESOURCE_SUBTRACTIVE) 620 continue; 621 622 /* If the resource is not assigned ignore it. */ 623 if (!(res->flags & IORESOURCE_ASSIGNED)) 624 continue; 625 626 search(gp, curdev, res); 627 } 628 } 629 } 630 631 void dev_set_enabled(struct device *dev, int enable) 632 { 633 if (dev->enabled == enable) 634 return; 635 636 dev->enabled = enable; 637 if (dev->ops && dev->ops->enable) 638 dev->ops->enable(dev); 639 else if (dev->chip_ops && dev->chip_ops->enable_dev) 640 dev->chip_ops->enable_dev(dev); 641 } 642 643 void disable_children(struct bus *bus) 644 { 645 struct device *child; 646 647 for (child = bus->children; child; child = child->sibling) { 648 if (child->downstream) 649 disable_children(child->downstream); 650 dev_set_enabled(child, 0); 651 } 652 } 653 654 /* 655 * Returns true if the device is an enabled bridge that has at least 656 * one enabled device on its secondary bus that is not of type NONE. 657 */ 658 bool dev_is_active_bridge(const struct device *dev) 659 { 660 struct device *child; 661 662 if (!dev || !dev->enabled) 663 return 0; 664 665 if (!dev->downstream || !dev->downstream->children) 666 return 0; 667 668 for (child = dev->downstream->children; child; child = child->sibling) { 669 if (child->path.type == DEVICE_PATH_NONE) 670 continue; 671 if (child->enabled) 672 return 1; 673 } 674 675 return 0; 676 } 677 678 static void resource_tree(const struct device *root, int debug_level, int depth) 679 { 680 int i = 0; 681 struct device *child; 682 struct resource *res; 683 char indent[30]; /* If your tree has more levels, it's wrong. */ 684 685 for (i = 0; i < depth + 1 && i < 29; i++) 686 indent[i] = ' '; 687 indent[i] = '\0'; 688 689 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root)); 690 if (root->downstream && root->downstream->children) 691 printk(BIOS_DEBUG, " child on link 0 %s", 692 dev_path(root->downstream->children)); 693 printk(BIOS_DEBUG, "\n"); 694 695 for (res = root->resource_list; res; res = res->next) { 696 printk(debug_level, "%s%s resource base %llx size %llx " 697 "align %d gran %d limit %llx flags %lx index %lx\n", 698 indent, dev_path(root), res->base, res->size, 699 res->align, res->gran, res->limit, res->flags, 700 res->index); 701 } 702 703 if (!root->downstream) 704 return; 705 706 for (child = root->downstream->children; child; child = child->sibling) 707 resource_tree(child, debug_level, depth + 1); 708 } 709 710 void print_resource_tree(const struct device *root, int debug_level, 711 const char *msg) 712 { 713 /* Bail if root is null. */ 714 if (!root) { 715 printk(debug_level, "%s passed NULL for root!\n", __func__); 716 return; 717 } 718 719 /* Bail if not printing to screen. */ 720 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n", 721 dev_path(root), msg)) 722 return; 723 724 resource_tree(root, debug_level, 0); 725 } 726 727 void show_devs_tree(const struct device *dev, int debug_level, int depth) 728 { 729 char depth_str[20]; 730 int i; 731 struct device *sibling; 732 733 for (i = 0; i < depth; i++) 734 depth_str[i] = ' '; 735 depth_str[i] = '\0'; 736 737 printk(debug_level, "%s%s: enabled %d\n", 738 depth_str, dev_path(dev), dev->enabled); 739 740 if (!dev->downstream) 741 return; 742 743 for (sibling = dev->downstream->children; sibling; sibling = sibling->sibling) 744 show_devs_tree(sibling, debug_level, depth + 1); 745 } 746 747 void show_all_devs_tree(int debug_level, const char *msg) 748 { 749 /* Bail if not printing to screen. */ 750 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg)) 751 return; 752 show_devs_tree(all_devices, debug_level, 0); 753 } 754 755 void show_devs_subtree(struct device *root, int debug_level, const char *msg) 756 { 757 /* Bail if not printing to screen. */ 758 if (!printk(debug_level, "Show all devs in subtree %s... %s\n", 759 dev_path(root), msg)) 760 return; 761 printk(debug_level, "%s\n", msg); 762 show_devs_tree(root, debug_level, 0); 763 } 764 765 void show_all_devs(int debug_level, const char *msg) 766 { 767 struct device *dev; 768 769 /* Bail if not printing to screen. */ 770 if (!printk(debug_level, "Show all devs... %s\n", msg)) 771 return; 772 for (dev = all_devices; dev; dev = dev->next) { 773 printk(debug_level, "%s: enabled %d\n", 774 dev_path(dev), dev->enabled); 775 } 776 } 777 778 void show_one_resource(int debug_level, struct device *dev, 779 struct resource *resource, const char *comment) 780 { 781 char buf[10]; 782 unsigned long long base, end; 783 base = resource->base; 784 end = resource_end(resource); 785 buf[0] = '\0'; 786 787 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] " 788 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev), 789 resource->index, base, end, resource->size, resource->gran, 790 buf, resource_type(resource), comment); 791 } 792 793 void show_all_devs_resources(int debug_level, const char *msg) 794 { 795 struct device *dev; 796 797 if (!printk(debug_level, "Show all devs with resources... %s\n", msg)) 798 return; 799 800 for (dev = all_devices; dev; dev = dev->next) { 801 struct resource *res; 802 printk(debug_level, "%s: enabled %d\n", 803 dev_path(dev), dev->enabled); 804 for (res = dev->resource_list; res; res = res->next) 805 show_one_resource(debug_level, dev, res, ""); 806 } 807 } 808 809 const struct resource *resource_range_idx(struct device *dev, unsigned long index, 810 uint64_t base, uint64_t size, unsigned long flags) 811 { 812 struct resource *resource; 813 if (!size) 814 return NULL; 815 816 resource = new_resource(dev, index); 817 resource->base = base; 818 819 if (flags & IORESOURCE_FIXED) 820 resource->size = size; 821 if (flags & IORESOURCE_BRIDGE) 822 resource->limit = base + size - 1; 823 824 resource->flags = IORESOURCE_ASSIGNED; 825 resource->flags |= flags; 826 827 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n", 828 dev_path(dev), resource->index, resource->base, resource->size); 829 830 return resource; 831 } 832 833 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end) 834 { 835 return ram_from_to(dev, index, 0, end); 836 } 837 838 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end) 839 { 840 if (end <= 4ull * GiB) 841 return NULL; 842 843 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB); 844 845 return ram_from_to(dev, index, 4ull * GiB, end); 846 } 847 848 void mmconf_resource(struct device *dev, unsigned long index) 849 { 850 struct resource *resource = new_resource(dev, index); 851 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS; 852 resource->size = CONFIG_ECAM_MMCONF_LENGTH; 853 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE | 854 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; 855 856 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n", 857 (unsigned long)(resource->base), 858 (unsigned long)(resource->base + resource->size)); 859 } 860 861 void tolm_test(void *gp, struct device *dev, struct resource *new) 862 { 863 struct resource **best_p = gp; 864 struct resource *best; 865 866 best = *best_p; 867 868 /* 869 * If resource is not allocated any space i.e. size is zero, 870 * then do not consider this resource in tolm calculations. 871 */ 872 if (new->size == 0) 873 return; 874 875 if (!best || (best->base > new->base)) 876 best = new; 877 878 *best_p = best; 879 } 880 881 u32 find_pci_tolm(struct bus *bus) 882 { 883 struct resource *min = NULL; 884 u32 tolm; 885 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED; 886 887 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min); 888 889 tolm = 0xffffffffUL; 890 891 if (min && tolm > min->base) 892 tolm = min->base; 893 894 return tolm; 895 } 896 897 /* Count of enabled CPUs */ 898 int dev_count_cpu(void) 899 { 900 struct device *cpu; 901 int count = 0; 902 903 for (cpu = all_devices; cpu; cpu = cpu->next) { 904 if (!is_enabled_cpu(cpu)) 905 continue; 906 count++; 907 } 908 909 return count; 910 } 911 912 /* Get device path name */ 913 const char *dev_path_name(enum device_path_type type) 914 { 915 static const char *const type_names[] = DEVICE_PATH_NAMES; 916 const char *type_name = "Unknown"; 917 918 /* Translate the type value into a string */ 919 if (type < ARRAY_SIZE(type_names)) 920 type_name = type_names[type]; 921 return type_name; 922 } 923 924 bool dev_path_hotplug(const struct device *dev) 925 { 926 for (dev = dev->upstream->dev; dev != dev->upstream->dev; dev = dev->upstream->dev) { 927 if (dev->hotplug_port) 928 return true; 929 } 930 return false; 931 } 932 933 void log_resource(const char *type, const struct device *dev, const struct resource *res, 934 const char *srcfile, const int line) 935 { 936 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, " 937 "end: 0x%llx, size_kb: 0x%llx\n", 938 srcfile, line, type, dev_path(dev), res->index, res->base, 939 resource_end(res), res->size / KiB); 940 } 941 942 bool is_cpu(const struct device *cpu) 943 { 944 return cpu->path.type == DEVICE_PATH_APIC && 945 cpu->upstream->dev->path.type == DEVICE_PATH_CPU_CLUSTER; 946 } 947 948 bool is_enabled_cpu(const struct device *cpu) 949 { 950 return is_cpu(cpu) && cpu->enabled; 951 } 952 953 bool is_pci(const struct device *pci) 954 { 955 return pci->path.type == DEVICE_PATH_PCI; 956 } 957 958 bool is_enabled_pci(const struct device *pci) 959 { 960 return is_pci(pci) && pci->enabled; 961 } 962 963 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus) 964 { 965 return is_pci(pci) && pci->upstream->segment_group == 0 966 && pci->upstream->secondary == bus; 967 } 968 969 bool is_pci_bridge(const struct device *pci) 970 { 971 return is_pci(pci) && ((pci->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE); 972 } 973 974 bool is_pci_ioapic(const struct device *pci) 975 { 976 return is_pci(pci) && ((pci->class >> 16) == PCI_BASE_CLASS_SYSTEM) && 977 ((pci->class >> 8) == PCI_CLASS_SYSTEM_PIC) && 978 ((pci->class & 0xff) >= 0x10); 979 }