1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com> 4 * (C) Copyright 2002-2004 IBM Corp. 5 * (C) Copyright 2003 Matthew Wilcox 6 * (C) Copyright 2003 Hewlett-Packard 7 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> 8 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> 9 * 10 * File attributes for PCI devices 11 * 12 * Modeled after usb's driverfs.c 13 */ 14 15 #include <linux/bitfield.h> 16 #include <linux/cleanup.h> 17 #include <linux/kernel.h> 18 #include <linux/sched.h> 19 #include <linux/pci.h> 20 #include <linux/stat.h> 21 #include <linux/export.h> 22 #include <linux/topology.h> 23 #include <linux/mm.h> 24 #include <linux/fs.h> 25 #include <linux/capability.h> 26 #include <linux/security.h> 27 #include <linux/slab.h> 28 #include <linux/vgaarb.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/msi.h> 31 #include <linux/of.h> 32 #include <linux/aperture.h> 33 #include <linux/unaligned.h> 34 #include "pci.h" 35 36 #ifndef ARCH_PCI_DEV_GROUPS 37 #define ARCH_PCI_DEV_GROUPS 38 #endif 39 40 static int sysfs_initialized; /* = 0 */ 41 42 /* show configuration fields */ 43 #define pci_config_attr(field, format_string) \ 44 static ssize_t \ 45 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 46 { \ 47 struct pci_dev *pdev; \ 48 \ 49 pdev = to_pci_dev(dev); \ 50 return sysfs_emit(buf, format_string, pdev->field); \ 51 } \ 52 static DEVICE_ATTR_RO(field) 53 54 pci_config_attr(vendor, "0x%04x\n"); 55 pci_config_attr(device, "0x%04x\n"); 56 pci_config_attr(subsystem_vendor, "0x%04x\n"); 57 pci_config_attr(subsystem_device, "0x%04x\n"); 58 pci_config_attr(revision, "0x%02x\n"); 59 pci_config_attr(class, "0x%06x\n"); 60 61 static ssize_t irq_show(struct device *dev, 62 struct device_attribute *attr, 63 char *buf) 64 { 65 struct pci_dev *pdev = to_pci_dev(dev); 66 67 #ifdef CONFIG_PCI_MSI 68 /* 69 * For MSI, show the first MSI IRQ; for all other cases including 70 * MSI-X, show the legacy INTx IRQ. 71 */ 72 if (pdev->msi_enabled) 73 return sysfs_emit(buf, "%u\n", pci_irq_vector(pdev, 0)); 74 #endif 75 76 return sysfs_emit(buf, "%u\n", pdev->irq); 77 } 78 static DEVICE_ATTR_RO(irq); 79 80 static ssize_t broken_parity_status_show(struct device *dev, 81 struct device_attribute *attr, 82 char *buf) 83 { 84 struct pci_dev *pdev = to_pci_dev(dev); 85 return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); 86 } 87 88 static ssize_t broken_parity_status_store(struct device *dev, 89 struct device_attribute *attr, 90 const char *buf, size_t count) 91 { 92 struct pci_dev *pdev = to_pci_dev(dev); 93 unsigned long val; 94 95 if (kstrtoul(buf, 0, &val) < 0) 96 return -EINVAL; 97 98 pdev->broken_parity_status = !!val; 99 100 return count; 101 } 102 static DEVICE_ATTR_RW(broken_parity_status); 103 104 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, 105 struct device_attribute *attr, char *buf) 106 { 107 const struct cpumask *mask; 108 109 #ifdef CONFIG_NUMA 110 if (dev_to_node(dev) == NUMA_NO_NODE) 111 mask = cpu_online_mask; 112 else 113 mask = cpumask_of_node(dev_to_node(dev)); 114 #else 115 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); 116 #endif 117 return cpumap_print_to_pagebuf(list, buf, mask); 118 } 119 120 static ssize_t local_cpus_show(struct device *dev, 121 struct device_attribute *attr, char *buf) 122 { 123 return pci_dev_show_local_cpu(dev, false, attr, buf); 124 } 125 static DEVICE_ATTR_RO(local_cpus); 126 127 static ssize_t local_cpulist_show(struct device *dev, 128 struct device_attribute *attr, char *buf) 129 { 130 return pci_dev_show_local_cpu(dev, true, attr, buf); 131 } 132 static DEVICE_ATTR_RO(local_cpulist); 133 134 /* 135 * PCI Bus Class Devices 136 */ 137 static ssize_t cpuaffinity_show(struct device *dev, 138 struct device_attribute *attr, char *buf) 139 { 140 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); 141 142 return cpumap_print_to_pagebuf(false, buf, cpumask); 143 } 144 static DEVICE_ATTR_RO(cpuaffinity); 145 146 static ssize_t cpulistaffinity_show(struct device *dev, 147 struct device_attribute *attr, char *buf) 148 { 149 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); 150 151 return cpumap_print_to_pagebuf(true, buf, cpumask); 152 } 153 static DEVICE_ATTR_RO(cpulistaffinity); 154 155 static ssize_t power_state_show(struct device *dev, 156 struct device_attribute *attr, char *buf) 157 { 158 struct pci_dev *pdev = to_pci_dev(dev); 159 160 return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state)); 161 } 162 static DEVICE_ATTR_RO(power_state); 163 164 /* show resources */ 165 static ssize_t resource_show(struct device *dev, struct device_attribute *attr, 166 char *buf) 167 { 168 struct pci_dev *pci_dev = to_pci_dev(dev); 169 int i; 170 int max; 171 resource_size_t start, end; 172 size_t len = 0; 173 174 if (pci_dev->subordinate) 175 max = DEVICE_COUNT_RESOURCE; 176 else 177 max = PCI_BRIDGE_RESOURCES; 178 179 for (i = 0; i < max; i++) { 180 struct resource *res = &pci_dev->resource[i]; 181 struct resource zerores = {}; 182 183 /* For backwards compatibility */ 184 if (pci_resource_is_bridge_win(i) && 185 res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) 186 res = &zerores; 187 188 pci_resource_to_user(pci_dev, i, res, &start, &end); 189 len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n", 190 (unsigned long long)start, 191 (unsigned long long)end, 192 (unsigned long long)res->flags); 193 } 194 return len; 195 } 196 static DEVICE_ATTR_RO(resource); 197 198 static ssize_t max_link_speed_show(struct device *dev, 199 struct device_attribute *attr, char *buf) 200 { 201 struct pci_dev *pdev = to_pci_dev(dev); 202 203 return sysfs_emit(buf, "%s\n", 204 pci_speed_string(pcie_get_speed_cap(pdev))); 205 } 206 static DEVICE_ATTR_RO(max_link_speed); 207 208 static ssize_t max_link_width_show(struct device *dev, 209 struct device_attribute *attr, char *buf) 210 { 211 struct pci_dev *pdev = to_pci_dev(dev); 212 ssize_t ret; 213 214 /* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */ 215 pci_config_pm_runtime_get(pdev); 216 ret = sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev)); 217 pci_config_pm_runtime_put(pdev); 218 219 return ret; 220 } 221 static DEVICE_ATTR_RO(max_link_width); 222 223 static ssize_t current_link_speed_show(struct device *dev, 224 struct device_attribute *attr, char *buf) 225 { 226 struct pci_dev *pci_dev = to_pci_dev(dev); 227 u16 linkstat; 228 int err; 229 enum pci_bus_speed speed; 230 231 pci_config_pm_runtime_get(pci_dev); 232 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); 233 pci_config_pm_runtime_put(pci_dev); 234 235 if (err) 236 return -EINVAL; 237 238 speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS]; 239 240 return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); 241 } 242 static DEVICE_ATTR_RO(current_link_speed); 243 244 static ssize_t current_link_width_show(struct device *dev, 245 struct device_attribute *attr, char *buf) 246 { 247 struct pci_dev *pci_dev = to_pci_dev(dev); 248 u16 linkstat; 249 int err; 250 251 pci_config_pm_runtime_get(pci_dev); 252 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); 253 pci_config_pm_runtime_put(pci_dev); 254 255 if (err) 256 return -EINVAL; 257 258 return sysfs_emit(buf, "%u\n", FIELD_GET(PCI_EXP_LNKSTA_NLW, linkstat)); 259 } 260 static DEVICE_ATTR_RO(current_link_width); 261 262 static ssize_t secondary_bus_number_show(struct device *dev, 263 struct device_attribute *attr, 264 char *buf) 265 { 266 struct pci_dev *pci_dev = to_pci_dev(dev); 267 u8 sec_bus; 268 int err; 269 270 pci_config_pm_runtime_get(pci_dev); 271 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); 272 pci_config_pm_runtime_put(pci_dev); 273 274 if (err) 275 return -EINVAL; 276 277 return sysfs_emit(buf, "%u\n", sec_bus); 278 } 279 static DEVICE_ATTR_RO(secondary_bus_number); 280 281 static ssize_t subordinate_bus_number_show(struct device *dev, 282 struct device_attribute *attr, 283 char *buf) 284 { 285 struct pci_dev *pci_dev = to_pci_dev(dev); 286 u8 sub_bus; 287 int err; 288 289 pci_config_pm_runtime_get(pci_dev); 290 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); 291 pci_config_pm_runtime_put(pci_dev); 292 293 if (err) 294 return -EINVAL; 295 296 return sysfs_emit(buf, "%u\n", sub_bus); 297 } 298 static DEVICE_ATTR_RO(subordinate_bus_number); 299 300 static ssize_t ari_enabled_show(struct device *dev, 301 struct device_attribute *attr, 302 char *buf) 303 { 304 struct pci_dev *pci_dev = to_pci_dev(dev); 305 306 return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); 307 } 308 static DEVICE_ATTR_RO(ari_enabled); 309 310 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 311 char *buf) 312 { 313 struct pci_dev *pci_dev = to_pci_dev(dev); 314 315 return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", 316 pci_dev->vendor, pci_dev->device, 317 pci_dev->subsystem_vendor, pci_dev->subsystem_device, 318 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), 319 (u8)(pci_dev->class)); 320 } 321 static DEVICE_ATTR_RO(modalias); 322 323 static ssize_t enable_store(struct device *dev, struct device_attribute *attr, 324 const char *buf, size_t count) 325 { 326 struct pci_dev *pdev = to_pci_dev(dev); 327 unsigned long val; 328 ssize_t result = 0; 329 330 /* this can crash the machine when done on the "wrong" device */ 331 if (!capable(CAP_SYS_ADMIN)) 332 return -EPERM; 333 334 if (kstrtoul(buf, 0, &val) < 0) 335 return -EINVAL; 336 337 device_lock(dev); 338 if (dev->driver) 339 result = -EBUSY; 340 else if (val) 341 result = pci_enable_device(pdev); 342 else if (pci_is_enabled(pdev)) 343 pci_disable_device(pdev); 344 else 345 result = -EIO; 346 device_unlock(dev); 347 348 return result < 0 ? result : count; 349 } 350 351 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, 352 char *buf) 353 { 354 struct pci_dev *pdev; 355 356 pdev = to_pci_dev(dev); 357 return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); 358 } 359 static DEVICE_ATTR_RW(enable); 360 361 #ifdef CONFIG_NUMA 362 static ssize_t numa_node_store(struct device *dev, 363 struct device_attribute *attr, const char *buf, 364 size_t count) 365 { 366 struct pci_dev *pdev = to_pci_dev(dev); 367 int node; 368 369 if (!capable(CAP_SYS_ADMIN)) 370 return -EPERM; 371 372 if (kstrtoint(buf, 0, &node) < 0) 373 return -EINVAL; 374 375 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) 376 return -EINVAL; 377 378 if (node != NUMA_NO_NODE && !node_online(node)) 379 return -EINVAL; 380 381 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); 382 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", 383 node); 384 385 dev->numa_node = node; 386 return count; 387 } 388 389 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, 390 char *buf) 391 { 392 return sysfs_emit(buf, "%d\n", dev->numa_node); 393 } 394 static DEVICE_ATTR_RW(numa_node); 395 #endif 396 397 static ssize_t dma_mask_bits_show(struct device *dev, 398 struct device_attribute *attr, char *buf) 399 { 400 struct pci_dev *pdev = to_pci_dev(dev); 401 402 return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask)); 403 } 404 static DEVICE_ATTR_RO(dma_mask_bits); 405 406 static ssize_t consistent_dma_mask_bits_show(struct device *dev, 407 struct device_attribute *attr, 408 char *buf) 409 { 410 return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask)); 411 } 412 static DEVICE_ATTR_RO(consistent_dma_mask_bits); 413 414 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, 415 char *buf) 416 { 417 struct pci_dev *pdev = to_pci_dev(dev); 418 struct pci_bus *subordinate = pdev->subordinate; 419 420 return sysfs_emit(buf, "%u\n", subordinate ? 421 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) 422 : !pdev->no_msi); 423 } 424 425 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, 426 const char *buf, size_t count) 427 { 428 struct pci_dev *pdev = to_pci_dev(dev); 429 struct pci_bus *subordinate = pdev->subordinate; 430 unsigned long val; 431 432 if (!capable(CAP_SYS_ADMIN)) 433 return -EPERM; 434 435 if (kstrtoul(buf, 0, &val) < 0) 436 return -EINVAL; 437 438 /* 439 * "no_msi" and "bus_flags" only affect what happens when a driver 440 * requests MSI or MSI-X. They don't affect any drivers that have 441 * already requested MSI or MSI-X. 442 */ 443 if (!subordinate) { 444 pdev->no_msi = !val; 445 pci_info(pdev, "MSI/MSI-X %s for future drivers\n", 446 val ? "allowed" : "disallowed"); 447 return count; 448 } 449 450 if (val) 451 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; 452 else 453 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; 454 455 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", 456 val ? "allowed" : "disallowed"); 457 return count; 458 } 459 static DEVICE_ATTR_RW(msi_bus); 460 461 static ssize_t rescan_store(const struct bus_type *bus, const char *buf, size_t count) 462 { 463 unsigned long val; 464 struct pci_bus *b = NULL; 465 466 if (kstrtoul(buf, 0, &val) < 0) 467 return -EINVAL; 468 469 if (val) { 470 pci_lock_rescan_remove(); 471 while ((b = pci_find_next_bus(b)) != NULL) 472 pci_rescan_bus(b); 473 pci_unlock_rescan_remove(); 474 } 475 return count; 476 } 477 static BUS_ATTR_WO(rescan); 478 479 static struct attribute *pci_bus_attrs[] = { 480 &bus_attr_rescan.attr, 481 NULL, 482 }; 483 484 static const struct attribute_group pci_bus_group = { 485 .attrs = pci_bus_attrs, 486 }; 487 488 const struct attribute_group *pci_bus_groups[] = { 489 &pci_bus_group, 490 NULL, 491 }; 492 493 static ssize_t dev_rescan_store(struct device *dev, 494 struct device_attribute *attr, const char *buf, 495 size_t count) 496 { 497 unsigned long val; 498 struct pci_dev *pdev = to_pci_dev(dev); 499 500 if (kstrtoul(buf, 0, &val) < 0) 501 return -EINVAL; 502 503 if (val) { 504 pci_lock_rescan_remove(); 505 pci_rescan_bus(pdev->bus); 506 pci_unlock_rescan_remove(); 507 } 508 return count; 509 } 510 static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, 511 dev_rescan_store); 512 513 static ssize_t remove_store(struct device *dev, struct device_attribute *attr, 514 const char *buf, size_t count) 515 { 516 unsigned long val; 517 518 if (kstrtoul(buf, 0, &val) < 0) 519 return -EINVAL; 520 521 if (val && device_remove_file_self(dev, attr)) 522 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); 523 return count; 524 } 525 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, 526 remove_store); 527 528 static ssize_t bus_rescan_store(struct device *dev, 529 struct device_attribute *attr, 530 const char *buf, size_t count) 531 { 532 unsigned long val; 533 struct pci_bus *bus = to_pci_bus(dev); 534 535 if (kstrtoul(buf, 0, &val) < 0) 536 return -EINVAL; 537 538 if (val) { 539 pci_lock_rescan_remove(); 540 if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) 541 pci_rescan_bus_bridge_resize(bus->self); 542 else 543 pci_rescan_bus(bus); 544 pci_unlock_rescan_remove(); 545 } 546 return count; 547 } 548 static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, 549 bus_rescan_store); 550 551 static ssize_t reset_subordinate_store(struct device *dev, 552 struct device_attribute *attr, 553 const char *buf, size_t count) 554 { 555 struct pci_dev *pdev = to_pci_dev(dev); 556 struct pci_bus *bus = pdev->subordinate; 557 unsigned long val; 558 559 if (!capable(CAP_SYS_ADMIN)) 560 return -EPERM; 561 562 if (kstrtoul(buf, 0, &val) < 0) 563 return -EINVAL; 564 565 if (val) { 566 int ret = __pci_reset_bus(bus); 567 568 if (ret) 569 return ret; 570 } 571 572 return count; 573 } 574 static DEVICE_ATTR_WO(reset_subordinate); 575 576 #if defined(CONFIG_PM) && defined(CONFIG_ACPI) 577 static ssize_t d3cold_allowed_store(struct device *dev, 578 struct device_attribute *attr, 579 const char *buf, size_t count) 580 { 581 struct pci_dev *pdev = to_pci_dev(dev); 582 unsigned long val; 583 584 if (kstrtoul(buf, 0, &val) < 0) 585 return -EINVAL; 586 587 pdev->d3cold_allowed = !!val; 588 pci_bridge_d3_update(pdev); 589 590 pm_runtime_resume(dev); 591 592 return count; 593 } 594 595 static ssize_t d3cold_allowed_show(struct device *dev, 596 struct device_attribute *attr, char *buf) 597 { 598 struct pci_dev *pdev = to_pci_dev(dev); 599 return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); 600 } 601 static DEVICE_ATTR_RW(d3cold_allowed); 602 #endif 603 604 #ifdef CONFIG_OF 605 static ssize_t devspec_show(struct device *dev, 606 struct device_attribute *attr, char *buf) 607 { 608 struct pci_dev *pdev = to_pci_dev(dev); 609 struct device_node *np = pci_device_to_OF_node(pdev); 610 611 if (np == NULL) 612 return 0; 613 return sysfs_emit(buf, "%pOF\n", np); 614 } 615 static DEVICE_ATTR_RO(devspec); 616 #endif 617 618 static ssize_t driver_override_store(struct device *dev, 619 struct device_attribute *attr, 620 const char *buf, size_t count) 621 { 622 struct pci_dev *pdev = to_pci_dev(dev); 623 int ret; 624 625 ret = driver_set_override(dev, &pdev->driver_override, buf, count); 626 if (ret) 627 return ret; 628 629 return count; 630 } 631 632 static ssize_t driver_override_show(struct device *dev, 633 struct device_attribute *attr, char *buf) 634 { 635 struct pci_dev *pdev = to_pci_dev(dev); 636 ssize_t len; 637 638 device_lock(dev); 639 len = sysfs_emit(buf, "%s\n", pdev->driver_override); 640 device_unlock(dev); 641 return len; 642 } 643 static DEVICE_ATTR_RW(driver_override); 644 645 static struct attribute *pci_dev_attrs[] = { 646 &dev_attr_power_state.attr, 647 &dev_attr_resource.attr, 648 &dev_attr_vendor.attr, 649 &dev_attr_device.attr, 650 &dev_attr_subsystem_vendor.attr, 651 &dev_attr_subsystem_device.attr, 652 &dev_attr_revision.attr, 653 &dev_attr_class.attr, 654 &dev_attr_irq.attr, 655 &dev_attr_local_cpus.attr, 656 &dev_attr_local_cpulist.attr, 657 &dev_attr_modalias.attr, 658 #ifdef CONFIG_NUMA 659 &dev_attr_numa_node.attr, 660 #endif 661 &dev_attr_dma_mask_bits.attr, 662 &dev_attr_consistent_dma_mask_bits.attr, 663 &dev_attr_enable.attr, 664 &dev_attr_broken_parity_status.attr, 665 &dev_attr_msi_bus.attr, 666 #if defined(CONFIG_PM) && defined(CONFIG_ACPI) 667 &dev_attr_d3cold_allowed.attr, 668 #endif 669 #ifdef CONFIG_OF 670 &dev_attr_devspec.attr, 671 #endif 672 &dev_attr_driver_override.attr, 673 &dev_attr_ari_enabled.attr, 674 NULL, 675 }; 676 677 static struct attribute *pci_bridge_attrs[] = { 678 &dev_attr_subordinate_bus_number.attr, 679 &dev_attr_secondary_bus_number.attr, 680 &dev_attr_reset_subordinate.attr, 681 NULL, 682 }; 683 684 static struct attribute *pcie_dev_attrs[] = { 685 &dev_attr_current_link_speed.attr, 686 &dev_attr_current_link_width.attr, 687 &dev_attr_max_link_width.attr, 688 &dev_attr_max_link_speed.attr, 689 NULL, 690 }; 691 692 static struct attribute *pcibus_attrs[] = { 693 &dev_attr_bus_rescan.attr, 694 &dev_attr_cpuaffinity.attr, 695 &dev_attr_cpulistaffinity.attr, 696 NULL, 697 }; 698 699 static const struct attribute_group pcibus_group = { 700 .attrs = pcibus_attrs, 701 }; 702 703 const struct attribute_group *pcibus_groups[] = { 704 &pcibus_group, 705 NULL, 706 }; 707 708 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, 709 char *buf) 710 { 711 struct pci_dev *pdev = to_pci_dev(dev); 712 struct pci_dev *vga_dev = vga_default_device(); 713 714 if (vga_dev) 715 return sysfs_emit(buf, "%u\n", (pdev == vga_dev)); 716 717 return sysfs_emit(buf, "%u\n", 718 !!(pdev->resource[PCI_ROM_RESOURCE].flags & 719 IORESOURCE_ROM_SHADOW)); 720 } 721 static DEVICE_ATTR_RO(boot_vga); 722 723 static ssize_t serial_number_show(struct device *dev, 724 struct device_attribute *attr, char *buf) 725 { 726 struct pci_dev *pci_dev = to_pci_dev(dev); 727 u64 dsn; 728 u8 bytes[8]; 729 730 dsn = pci_get_dsn(pci_dev); 731 if (!dsn) 732 return -EIO; 733 734 put_unaligned_be64(dsn, bytes); 735 return sysfs_emit(buf, "%8phD\n", bytes); 736 } 737 static DEVICE_ATTR_ADMIN_RO(serial_number); 738 739 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, 740 const struct bin_attribute *bin_attr, char *buf, 741 loff_t off, size_t count) 742 { 743 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); 744 unsigned int size = 64; 745 loff_t init_off = off; 746 u8 *data = (u8 *) buf; 747 748 /* Several chips lock up trying to read undefined config space */ 749 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) 750 size = dev->cfg_size; 751 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 752 size = 128; 753 754 if (off > size) 755 return 0; 756 if (off + count > size) { 757 size -= off; 758 count = size; 759 } else { 760 size = count; 761 } 762 763 pci_config_pm_runtime_get(dev); 764 765 if ((off & 1) && size) { 766 u8 val; 767 pci_user_read_config_byte(dev, off, &val); 768 data[off - init_off] = val; 769 off++; 770 size--; 771 } 772 773 if ((off & 3) && size > 2) { 774 u16 val; 775 pci_user_read_config_word(dev, off, &val); 776 data[off - init_off] = val & 0xff; 777 data[off - init_off + 1] = (val >> 8) & 0xff; 778 off += 2; 779 size -= 2; 780 } 781 782 while (size > 3) { 783 u32 val; 784 pci_user_read_config_dword(dev, off, &val); 785 data[off - init_off] = val & 0xff; 786 data[off - init_off + 1] = (val >> 8) & 0xff; 787 data[off - init_off + 2] = (val >> 16) & 0xff; 788 data[off - init_off + 3] = (val >> 24) & 0xff; 789 off += 4; 790 size -= 4; 791 cond_resched(); 792 } 793 794 if (size >= 2) { 795 u16 val; 796 pci_user_read_config_word(dev, off, &val); 797 data[off - init_off] = val & 0xff; 798 data[off - init_off + 1] = (val >> 8) & 0xff; 799 off += 2; 800 size -= 2; 801 } 802 803 if (size > 0) { 804 u8 val; 805 pci_user_read_config_byte(dev, off, &val); 806 data[off - init_off] = val; 807 } 808 809 pci_config_pm_runtime_put(dev); 810 811 return count; 812 } 813 814 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, 815 const struct bin_attribute *bin_attr, char *buf, 816 loff_t off, size_t count) 817 { 818 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); 819 unsigned int size = count; 820 loff_t init_off = off; 821 u8 *data = (u8 *) buf; 822 int ret; 823 824 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 825 if (ret) 826 return ret; 827 828 if (resource_is_exclusive(&dev->driver_exclusive_resource, off, 829 count)) { 830 pci_warn_once(dev, "%s: Unexpected write to kernel-exclusive config offset %llx", 831 current->comm, off); 832 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 833 } 834 835 if (off > dev->cfg_size) 836 return 0; 837 if (off + count > dev->cfg_size) { 838 size = dev->cfg_size - off; 839 count = size; 840 } 841 842 pci_config_pm_runtime_get(dev); 843 844 if ((off & 1) && size) { 845 pci_user_write_config_byte(dev, off, data[off - init_off]); 846 off++; 847 size--; 848 } 849 850 if ((off & 3) && size > 2) { 851 u16 val = data[off - init_off]; 852 val |= (u16) data[off - init_off + 1] << 8; 853 pci_user_write_config_word(dev, off, val); 854 off += 2; 855 size -= 2; 856 } 857 858 while (size > 3) { 859 u32 val = data[off - init_off]; 860 val |= (u32) data[off - init_off + 1] << 8; 861 val |= (u32) data[off - init_off + 2] << 16; 862 val |= (u32) data[off - init_off + 3] << 24; 863 pci_user_write_config_dword(dev, off, val); 864 off += 4; 865 size -= 4; 866 } 867 868 if (size >= 2) { 869 u16 val = data[off - init_off]; 870 val |= (u16) data[off - init_off + 1] << 8; 871 pci_user_write_config_word(dev, off, val); 872 off += 2; 873 size -= 2; 874 } 875 876 if (size) 877 pci_user_write_config_byte(dev, off, data[off - init_off]); 878 879 pci_config_pm_runtime_put(dev); 880 881 return count; 882 } 883 static const BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0); 884 885 static const struct bin_attribute *const pci_dev_config_attrs[] = { 886 &bin_attr_config, 887 NULL, 888 }; 889 890 static size_t pci_dev_config_attr_bin_size(struct kobject *kobj, 891 const struct bin_attribute *a, 892 int n) 893 { 894 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 895 896 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) 897 return PCI_CFG_SPACE_EXP_SIZE; 898 return PCI_CFG_SPACE_SIZE; 899 } 900 901 static const struct attribute_group pci_dev_config_attr_group = { 902 .bin_attrs = pci_dev_config_attrs, 903 .bin_size = pci_dev_config_attr_bin_size, 904 }; 905 906 /* 907 * llseek operation for mmappable PCI resources. 908 * May be left unused if the arch doesn't provide them. 909 */ 910 static __maybe_unused loff_t 911 pci_llseek_resource(struct file *filep, 912 struct kobject *kobj __always_unused, 913 const struct bin_attribute *attr, 914 loff_t offset, int whence) 915 { 916 return fixed_size_llseek(filep, offset, whence, attr->size); 917 } 918 919 #ifdef HAVE_PCI_LEGACY 920 /** 921 * pci_read_legacy_io - read byte(s) from legacy I/O port space 922 * @filp: open sysfs file 923 * @kobj: kobject corresponding to file to read from 924 * @bin_attr: struct bin_attribute for this file 925 * @buf: buffer to store results 926 * @off: offset into legacy I/O port space 927 * @count: number of bytes to read 928 * 929 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific 930 * callback routine (pci_legacy_read). 931 */ 932 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, 933 const struct bin_attribute *bin_attr, 934 char *buf, loff_t off, size_t count) 935 { 936 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 937 938 /* Only support 1, 2 or 4 byte accesses */ 939 if (count != 1 && count != 2 && count != 4) 940 return -EINVAL; 941 942 return pci_legacy_read(bus, off, (u32 *)buf, count); 943 } 944 945 /** 946 * pci_write_legacy_io - write byte(s) to legacy I/O port space 947 * @filp: open sysfs file 948 * @kobj: kobject corresponding to file to read from 949 * @bin_attr: struct bin_attribute for this file 950 * @buf: buffer containing value to be written 951 * @off: offset into legacy I/O port space 952 * @count: number of bytes to write 953 * 954 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific 955 * callback routine (pci_legacy_write). 956 */ 957 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, 958 const struct bin_attribute *bin_attr, 959 char *buf, loff_t off, size_t count) 960 { 961 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 962 963 /* Only support 1, 2 or 4 byte accesses */ 964 if (count != 1 && count != 2 && count != 4) 965 return -EINVAL; 966 967 return pci_legacy_write(bus, off, *(u32 *)buf, count); 968 } 969 970 /** 971 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space 972 * @filp: open sysfs file 973 * @kobj: kobject corresponding to device to be mapped 974 * @attr: struct bin_attribute for this file 975 * @vma: struct vm_area_struct passed to mmap 976 * 977 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap 978 * legacy memory space (first meg of bus space) into application virtual 979 * memory space. 980 */ 981 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, 982 const struct bin_attribute *attr, 983 struct vm_area_struct *vma) 984 { 985 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 986 987 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); 988 } 989 990 /** 991 * pci_mmap_legacy_io - map legacy PCI IO into user memory space 992 * @filp: open sysfs file 993 * @kobj: kobject corresponding to device to be mapped 994 * @attr: struct bin_attribute for this file 995 * @vma: struct vm_area_struct passed to mmap 996 * 997 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap 998 * legacy IO space (first meg of bus space) into application virtual 999 * memory space. Returns -ENOSYS if the operation isn't supported 1000 */ 1001 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, 1002 const struct bin_attribute *attr, 1003 struct vm_area_struct *vma) 1004 { 1005 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 1006 1007 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); 1008 } 1009 1010 /** 1011 * pci_adjust_legacy_attr - adjustment of legacy file attributes 1012 * @b: bus to create files under 1013 * @mmap_type: I/O port or memory 1014 * 1015 * Stub implementation. Can be overridden by arch if necessary. 1016 */ 1017 void __weak pci_adjust_legacy_attr(struct pci_bus *b, 1018 enum pci_mmap_state mmap_type) 1019 { 1020 } 1021 1022 /** 1023 * pci_create_legacy_files - create legacy I/O port and memory files 1024 * @b: bus to create files under 1025 * 1026 * Some platforms allow access to legacy I/O port and ISA memory space on 1027 * a per-bus basis. This routine creates the files and ties them into 1028 * their associated read, write and mmap files from pci-sysfs.c 1029 * 1030 * On error unwind, but don't propagate the error to the caller 1031 * as it is ok to set up the PCI bus without these files. 1032 */ 1033 void pci_create_legacy_files(struct pci_bus *b) 1034 { 1035 int error; 1036 1037 if (!sysfs_initialized) 1038 return; 1039 1040 b->legacy_io = kzalloc_objs(struct bin_attribute, 2, GFP_ATOMIC); 1041 if (!b->legacy_io) 1042 goto kzalloc_err; 1043 1044 sysfs_bin_attr_init(b->legacy_io); 1045 b->legacy_io->attr.name = "legacy_io"; 1046 b->legacy_io->size = 0xffff; 1047 b->legacy_io->attr.mode = 0600; 1048 b->legacy_io->read = pci_read_legacy_io; 1049 b->legacy_io->write = pci_write_legacy_io; 1050 /* See pci_create_attr() for motivation */ 1051 b->legacy_io->llseek = pci_llseek_resource; 1052 b->legacy_io->mmap = pci_mmap_legacy_io; 1053 b->legacy_io->f_mapping = iomem_get_mapping; 1054 pci_adjust_legacy_attr(b, pci_mmap_io); 1055 error = device_create_bin_file(&b->dev, b->legacy_io); 1056 if (error) 1057 goto legacy_io_err; 1058 1059 /* Allocated above after the legacy_io struct */ 1060 b->legacy_mem = b->legacy_io + 1; 1061 sysfs_bin_attr_init(b->legacy_mem); 1062 b->legacy_mem->attr.name = "legacy_mem"; 1063 b->legacy_mem->size = 1024*1024; 1064 b->legacy_mem->attr.mode = 0600; 1065 b->legacy_mem->mmap = pci_mmap_legacy_mem; 1066 /* See pci_create_attr() for motivation */ 1067 b->legacy_mem->llseek = pci_llseek_resource; 1068 b->legacy_mem->f_mapping = iomem_get_mapping; 1069 pci_adjust_legacy_attr(b, pci_mmap_mem); 1070 error = device_create_bin_file(&b->dev, b->legacy_mem); 1071 if (error) 1072 goto legacy_mem_err; 1073 1074 return; 1075 1076 legacy_mem_err: 1077 device_remove_bin_file(&b->dev, b->legacy_io); 1078 legacy_io_err: 1079 kfree(b->legacy_io); 1080 b->legacy_io = NULL; 1081 kzalloc_err: 1082 dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n"); 1083 } 1084 1085 void pci_remove_legacy_files(struct pci_bus *b) 1086 { 1087 if (b->legacy_io) { 1088 device_remove_bin_file(&b->dev, b->legacy_io); 1089 device_remove_bin_file(&b->dev, b->legacy_mem); 1090 kfree(b->legacy_io); /* both are allocated here */ 1091 } 1092 } 1093 #endif /* HAVE_PCI_LEGACY */ 1094 1095 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1096 /** 1097 * pci_mmap_resource - map a PCI resource into user memory space 1098 * @kobj: kobject for mapping 1099 * @attr: struct bin_attribute for the file being mapped 1100 * @vma: struct vm_area_struct passed into the mmap 1101 * @write_combine: 1 for write_combine mapping 1102 * 1103 * Use the regular PCI mapping routines to map a PCI resource into userspace. 1104 */ 1105 static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr, 1106 struct vm_area_struct *vma, int write_combine) 1107 { 1108 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1109 int bar = (unsigned long)attr->private; 1110 enum pci_mmap_state mmap_type; 1111 struct resource *res = &pdev->resource[bar]; 1112 int ret; 1113 1114 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 1115 if (ret) 1116 return ret; 1117 1118 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start)) 1119 return -EINVAL; 1120 1121 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) 1122 return -EINVAL; 1123 1124 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; 1125 1126 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); 1127 } 1128 1129 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, 1130 const struct bin_attribute *attr, 1131 struct vm_area_struct *vma) 1132 { 1133 return pci_mmap_resource(kobj, attr, vma, 0); 1134 } 1135 1136 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, 1137 const struct bin_attribute *attr, 1138 struct vm_area_struct *vma) 1139 { 1140 return pci_mmap_resource(kobj, attr, vma, 1); 1141 } 1142 1143 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, 1144 const struct bin_attribute *attr, char *buf, 1145 loff_t off, size_t count, bool write) 1146 { 1147 #ifdef CONFIG_HAS_IOPORT 1148 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1149 int bar = (unsigned long)attr->private; 1150 unsigned long port = off; 1151 1152 port += pci_resource_start(pdev, bar); 1153 1154 if (port > pci_resource_end(pdev, bar)) 1155 return 0; 1156 1157 if (port + count - 1 > pci_resource_end(pdev, bar)) 1158 return -EINVAL; 1159 1160 switch (count) { 1161 case 1: 1162 if (write) 1163 outb(*(u8 *)buf, port); 1164 else 1165 *(u8 *)buf = inb(port); 1166 return 1; 1167 case 2: 1168 if (write) 1169 outw(*(u16 *)buf, port); 1170 else 1171 *(u16 *)buf = inw(port); 1172 return 2; 1173 case 4: 1174 if (write) 1175 outl(*(u32 *)buf, port); 1176 else 1177 *(u32 *)buf = inl(port); 1178 return 4; 1179 } 1180 return -EINVAL; 1181 #else 1182 return -ENXIO; 1183 #endif 1184 } 1185 1186 static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj, 1187 const struct bin_attribute *attr, char *buf, 1188 loff_t off, size_t count) 1189 { 1190 return pci_resource_io(filp, kobj, attr, buf, off, count, false); 1191 } 1192 1193 static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, 1194 const struct bin_attribute *attr, char *buf, 1195 loff_t off, size_t count) 1196 { 1197 int ret; 1198 1199 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 1200 if (ret) 1201 return ret; 1202 1203 return pci_resource_io(filp, kobj, attr, buf, off, count, true); 1204 } 1205 1206 /** 1207 * pci_remove_resource_files - cleanup resource files 1208 * @pdev: dev to cleanup 1209 * 1210 * If we created resource files for @pdev, remove them from sysfs and 1211 * free their resources. 1212 */ 1213 static void pci_remove_resource_files(struct pci_dev *pdev) 1214 { 1215 int i; 1216 1217 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1218 struct bin_attribute *res_attr; 1219 1220 res_attr = pdev->res_attr[i]; 1221 if (res_attr) { 1222 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); 1223 kfree(res_attr); 1224 } 1225 1226 res_attr = pdev->res_attr_wc[i]; 1227 if (res_attr) { 1228 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); 1229 kfree(res_attr); 1230 } 1231 } 1232 } 1233 1234 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) 1235 { 1236 /* allocate attribute structure, piggyback attribute name */ 1237 int name_len = write_combine ? 13 : 10; 1238 struct bin_attribute *res_attr; 1239 char *res_attr_name; 1240 int retval; 1241 1242 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); 1243 if (!res_attr) 1244 return -ENOMEM; 1245 1246 res_attr_name = (char *)(res_attr + 1); 1247 1248 sysfs_bin_attr_init(res_attr); 1249 if (write_combine) { 1250 sprintf(res_attr_name, "resource%d_wc", num); 1251 res_attr->mmap = pci_mmap_resource_wc; 1252 } else { 1253 sprintf(res_attr_name, "resource%d", num); 1254 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { 1255 res_attr->read = pci_read_resource_io; 1256 res_attr->write = pci_write_resource_io; 1257 if (arch_can_pci_mmap_io()) 1258 res_attr->mmap = pci_mmap_resource_uc; 1259 } else { 1260 res_attr->mmap = pci_mmap_resource_uc; 1261 } 1262 } 1263 if (res_attr->mmap) { 1264 res_attr->f_mapping = iomem_get_mapping; 1265 /* 1266 * generic_file_llseek() consults f_mapping->host to determine 1267 * the file size. As iomem_inode knows nothing about the 1268 * attribute, it's not going to work, so override it as well. 1269 */ 1270 res_attr->llseek = pci_llseek_resource; 1271 } 1272 res_attr->attr.name = res_attr_name; 1273 res_attr->attr.mode = 0600; 1274 res_attr->size = pci_resource_len(pdev, num); 1275 res_attr->private = (void *)(unsigned long)num; 1276 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); 1277 if (retval) { 1278 kfree(res_attr); 1279 return retval; 1280 } 1281 1282 if (write_combine) 1283 pdev->res_attr_wc[num] = res_attr; 1284 else 1285 pdev->res_attr[num] = res_attr; 1286 1287 return 0; 1288 } 1289 1290 /** 1291 * pci_create_resource_files - create resource files in sysfs for @dev 1292 * @pdev: dev in question 1293 * 1294 * Walk the resources in @pdev creating files for each resource available. 1295 */ 1296 static int pci_create_resource_files(struct pci_dev *pdev) 1297 { 1298 int i; 1299 int retval; 1300 1301 /* Skip devices with non-mappable BARs */ 1302 if (pdev->non_mappable_bars) 1303 return 0; 1304 1305 /* Expose the PCI resources from this device as files */ 1306 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1307 1308 /* skip empty resources */ 1309 if (!pci_resource_len(pdev, i)) 1310 continue; 1311 1312 retval = pci_create_attr(pdev, i, 0); 1313 /* for prefetchable resources, create a WC mappable file */ 1314 if (!retval && arch_can_pci_mmap_wc() && 1315 pdev->resource[i].flags & IORESOURCE_PREFETCH) 1316 retval = pci_create_attr(pdev, i, 1); 1317 if (retval) { 1318 pci_remove_resource_files(pdev); 1319 return retval; 1320 } 1321 } 1322 return 0; 1323 } 1324 #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ 1325 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } 1326 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } 1327 #endif 1328 1329 /** 1330 * pci_write_rom - used to enable access to the PCI ROM display 1331 * @filp: sysfs file 1332 * @kobj: kernel object handle 1333 * @bin_attr: struct bin_attribute for this file 1334 * @buf: user input 1335 * @off: file offset 1336 * @count: number of byte in input 1337 * 1338 * writing anything except 0 enables it 1339 */ 1340 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, 1341 const struct bin_attribute *bin_attr, char *buf, 1342 loff_t off, size_t count) 1343 { 1344 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1345 1346 if ((off == 0) && (*buf == '0') && (count == 2)) 1347 pdev->rom_attr_enabled = 0; 1348 else 1349 pdev->rom_attr_enabled = 1; 1350 1351 return count; 1352 } 1353 1354 /** 1355 * pci_read_rom - read a PCI ROM 1356 * @filp: sysfs file 1357 * @kobj: kernel object handle 1358 * @bin_attr: struct bin_attribute for this file 1359 * @buf: where to put the data we read from the ROM 1360 * @off: file offset 1361 * @count: number of bytes to read 1362 * 1363 * Put @count bytes starting at @off into @buf from the ROM in the PCI 1364 * device corresponding to @kobj. 1365 */ 1366 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, 1367 const struct bin_attribute *bin_attr, char *buf, 1368 loff_t off, size_t count) 1369 { 1370 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1371 void __iomem *rom; 1372 size_t size; 1373 1374 if (!pdev->rom_attr_enabled) 1375 return -EINVAL; 1376 1377 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ 1378 if (!rom || !size) 1379 return -EIO; 1380 1381 if (off >= size) 1382 count = 0; 1383 else { 1384 if (off + count > size) 1385 count = size - off; 1386 1387 memcpy_fromio(buf, rom + off, count); 1388 } 1389 pci_unmap_rom(pdev, rom); 1390 1391 return count; 1392 } 1393 static const BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0); 1394 1395 static const struct bin_attribute *const pci_dev_rom_attrs[] = { 1396 &bin_attr_rom, 1397 NULL, 1398 }; 1399 1400 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, 1401 const struct bin_attribute *a, int n) 1402 { 1403 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1404 1405 /* If the device has a ROM, try to expose it in sysfs. */ 1406 if (!pci_resource_end(pdev, PCI_ROM_RESOURCE)) 1407 return 0; 1408 1409 return a->attr.mode; 1410 } 1411 1412 static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj, 1413 const struct bin_attribute *a, int n) 1414 { 1415 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1416 1417 return pci_resource_len(pdev, PCI_ROM_RESOURCE); 1418 } 1419 1420 static const struct attribute_group pci_dev_rom_attr_group = { 1421 .bin_attrs = pci_dev_rom_attrs, 1422 .is_bin_visible = pci_dev_rom_attr_is_visible, 1423 .bin_size = pci_dev_rom_attr_bin_size, 1424 }; 1425 1426 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 1427 const char *buf, size_t count) 1428 { 1429 struct pci_dev *pdev = to_pci_dev(dev); 1430 unsigned long val; 1431 ssize_t result; 1432 1433 if (kstrtoul(buf, 0, &val) < 0) 1434 return -EINVAL; 1435 1436 if (val != 1) 1437 return -EINVAL; 1438 1439 pm_runtime_get_sync(dev); 1440 result = pci_reset_function(pdev); 1441 pm_runtime_put(dev); 1442 if (result < 0) 1443 return result; 1444 1445 return count; 1446 } 1447 static DEVICE_ATTR_WO(reset); 1448 1449 static struct attribute *pci_dev_reset_attrs[] = { 1450 &dev_attr_reset.attr, 1451 NULL, 1452 }; 1453 1454 static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, 1455 struct attribute *a, int n) 1456 { 1457 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1458 1459 if (!pci_reset_supported(pdev)) 1460 return 0; 1461 1462 return a->mode; 1463 } 1464 1465 static const struct attribute_group pci_dev_reset_attr_group = { 1466 .attrs = pci_dev_reset_attrs, 1467 .is_visible = pci_dev_reset_attr_is_visible, 1468 }; 1469 1470 static ssize_t reset_method_show(struct device *dev, 1471 struct device_attribute *attr, char *buf) 1472 { 1473 struct pci_dev *pdev = to_pci_dev(dev); 1474 ssize_t len = 0; 1475 int i, m; 1476 1477 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) { 1478 m = pdev->reset_methods[i]; 1479 if (!m) 1480 break; 1481 1482 len += sysfs_emit_at(buf, len, "%s%s", len ? " " : "", 1483 pci_reset_fn_methods[m].name); 1484 } 1485 1486 if (len) 1487 len += sysfs_emit_at(buf, len, "\n"); 1488 1489 return len; 1490 } 1491 1492 static int reset_method_lookup(const char *name) 1493 { 1494 int m; 1495 1496 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) { 1497 if (sysfs_streq(name, pci_reset_fn_methods[m].name)) 1498 return m; 1499 } 1500 1501 return 0; /* not found */ 1502 } 1503 1504 static ssize_t reset_method_store(struct device *dev, 1505 struct device_attribute *attr, 1506 const char *buf, size_t count) 1507 { 1508 struct pci_dev *pdev = to_pci_dev(dev); 1509 char *tmp_options, *name; 1510 int m, n; 1511 u8 reset_methods[PCI_NUM_RESET_METHODS] = {}; 1512 1513 if (sysfs_streq(buf, "")) { 1514 pdev->reset_methods[0] = 0; 1515 pci_warn(pdev, "All device reset methods disabled by user"); 1516 return count; 1517 } 1518 1519 PM_RUNTIME_ACQUIRE(dev, pm); 1520 if (PM_RUNTIME_ACQUIRE_ERR(&pm)) 1521 return -ENXIO; 1522 1523 if (sysfs_streq(buf, "default")) { 1524 pci_init_reset_methods(pdev); 1525 return count; 1526 } 1527 1528 char *options __free(kfree) = kstrndup(buf, count, GFP_KERNEL); 1529 if (!options) 1530 return -ENOMEM; 1531 1532 n = 0; 1533 tmp_options = options; 1534 while ((name = strsep(&tmp_options, " ")) != NULL) { 1535 if (sysfs_streq(name, "")) 1536 continue; 1537 1538 name = strim(name); 1539 1540 /* Leave previous methods unchanged if input is invalid */ 1541 m = reset_method_lookup(name); 1542 if (!m) { 1543 pci_err(pdev, "Invalid reset method '%s'", name); 1544 return -EINVAL; 1545 } 1546 1547 if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) { 1548 pci_err(pdev, "Unsupported reset method '%s'", name); 1549 return -EINVAL; 1550 } 1551 1552 if (n == PCI_NUM_RESET_METHODS - 1) { 1553 pci_err(pdev, "Too many reset methods\n"); 1554 return -EINVAL; 1555 } 1556 1557 reset_methods[n++] = m; 1558 } 1559 1560 reset_methods[n] = 0; 1561 1562 /* Warn if dev-specific supported but not highest priority */ 1563 if (pci_reset_fn_methods[1].reset_fn(pdev, PCI_RESET_PROBE) == 0 && 1564 reset_methods[0] != 1) 1565 pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user"); 1566 memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods)); 1567 return count; 1568 } 1569 static DEVICE_ATTR_RW(reset_method); 1570 1571 static struct attribute *pci_dev_reset_method_attrs[] = { 1572 &dev_attr_reset_method.attr, 1573 NULL, 1574 }; 1575 1576 static const struct attribute_group pci_dev_reset_method_attr_group = { 1577 .attrs = pci_dev_reset_method_attrs, 1578 .is_visible = pci_dev_reset_attr_is_visible, 1579 }; 1580 1581 static ssize_t __resource_resize_show(struct device *dev, int n, char *buf) 1582 { 1583 struct pci_dev *pdev = to_pci_dev(dev); 1584 ssize_t ret; 1585 1586 pci_config_pm_runtime_get(pdev); 1587 1588 ret = sysfs_emit(buf, "%016llx\n", 1589 pci_rebar_get_possible_sizes(pdev, n)); 1590 1591 pci_config_pm_runtime_put(pdev); 1592 1593 return ret; 1594 } 1595 1596 static ssize_t __resource_resize_store(struct device *dev, int n, 1597 const char *buf, size_t count) 1598 { 1599 struct pci_dev *pdev = to_pci_dev(dev); 1600 struct pci_bus *bus = pdev->bus; 1601 unsigned long size; 1602 int ret; 1603 u16 cmd; 1604 1605 if (kstrtoul(buf, 0, &size) < 0) 1606 return -EINVAL; 1607 1608 device_lock(dev); 1609 if (dev->driver || pci_num_vf(pdev)) { 1610 ret = -EBUSY; 1611 goto unlock; 1612 } 1613 1614 pci_config_pm_runtime_get(pdev); 1615 1616 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { 1617 ret = aperture_remove_conflicting_pci_devices(pdev, 1618 "resourceN_resize"); 1619 if (ret) 1620 goto pm_put; 1621 } 1622 1623 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 1624 pci_write_config_word(pdev, PCI_COMMAND, 1625 cmd & ~PCI_COMMAND_MEMORY); 1626 1627 pci_remove_resource_files(pdev); 1628 1629 ret = pci_resize_resource(pdev, n, size, 0); 1630 1631 pci_assign_unassigned_bus_resources(bus); 1632 1633 if (pci_create_resource_files(pdev)) 1634 pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n"); 1635 1636 pci_write_config_word(pdev, PCI_COMMAND, cmd); 1637 pm_put: 1638 pci_config_pm_runtime_put(pdev); 1639 unlock: 1640 device_unlock(dev); 1641 1642 return ret ? ret : count; 1643 } 1644 1645 #define pci_dev_resource_resize_attr(n) \ 1646 static ssize_t resource##n##_resize_show(struct device *dev, \ 1647 struct device_attribute *attr, \ 1648 char *buf) \ 1649 { \ 1650 return __resource_resize_show(dev, n, buf); \ 1651 } \ 1652 static ssize_t resource##n##_resize_store(struct device *dev, \ 1653 struct device_attribute *attr,\ 1654 const char *buf, size_t count)\ 1655 { \ 1656 return __resource_resize_store(dev, n, buf, count); \ 1657 } \ 1658 static DEVICE_ATTR_RW(resource##n##_resize) 1659 1660 pci_dev_resource_resize_attr(0); 1661 pci_dev_resource_resize_attr(1); 1662 pci_dev_resource_resize_attr(2); 1663 pci_dev_resource_resize_attr(3); 1664 pci_dev_resource_resize_attr(4); 1665 pci_dev_resource_resize_attr(5); 1666 1667 static struct attribute *resource_resize_attrs[] = { 1668 &dev_attr_resource0_resize.attr, 1669 &dev_attr_resource1_resize.attr, 1670 &dev_attr_resource2_resize.attr, 1671 &dev_attr_resource3_resize.attr, 1672 &dev_attr_resource4_resize.attr, 1673 &dev_attr_resource5_resize.attr, 1674 NULL, 1675 }; 1676 1677 static umode_t resource_resize_is_visible(struct kobject *kobj, 1678 struct attribute *a, int n) 1679 { 1680 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1681 1682 return pci_rebar_get_current_size(pdev, n) < 0 ? 0 : a->mode; 1683 } 1684 1685 static const struct attribute_group pci_dev_resource_resize_group = { 1686 .attrs = resource_resize_attrs, 1687 .is_visible = resource_resize_is_visible, 1688 }; 1689 1690 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) 1691 { 1692 if (!sysfs_initialized) 1693 return -EACCES; 1694 1695 return pci_create_resource_files(pdev); 1696 } 1697 1698 /** 1699 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files 1700 * @pdev: device whose entries we should free 1701 * 1702 * Cleanup when @pdev is removed from sysfs. 1703 */ 1704 void pci_remove_sysfs_dev_files(struct pci_dev *pdev) 1705 { 1706 if (!sysfs_initialized) 1707 return; 1708 1709 pci_remove_resource_files(pdev); 1710 } 1711 1712 static int __init pci_sysfs_init(void) 1713 { 1714 struct pci_dev *pdev = NULL; 1715 struct pci_bus *pbus = NULL; 1716 int retval; 1717 1718 sysfs_initialized = 1; 1719 for_each_pci_dev(pdev) { 1720 retval = pci_create_sysfs_dev_files(pdev); 1721 if (retval) { 1722 pci_dev_put(pdev); 1723 return retval; 1724 } 1725 } 1726 1727 while ((pbus = pci_find_next_bus(pbus))) 1728 pci_create_legacy_files(pbus); 1729 1730 return 0; 1731 } 1732 late_initcall(pci_sysfs_init); 1733 1734 static struct attribute *pci_dev_dev_attrs[] = { 1735 &dev_attr_boot_vga.attr, 1736 &dev_attr_serial_number.attr, 1737 NULL, 1738 }; 1739 1740 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, 1741 struct attribute *a, int n) 1742 { 1743 struct device *dev = kobj_to_dev(kobj); 1744 struct pci_dev *pdev = to_pci_dev(dev); 1745 1746 if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev)) 1747 return a->mode; 1748 1749 if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev)) 1750 return a->mode; 1751 1752 return 0; 1753 } 1754 1755 static struct attribute *pci_dev_hp_attrs[] = { 1756 &dev_attr_remove.attr, 1757 &dev_attr_dev_rescan.attr, 1758 NULL, 1759 }; 1760 1761 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, 1762 struct attribute *a, int n) 1763 { 1764 struct device *dev = kobj_to_dev(kobj); 1765 struct pci_dev *pdev = to_pci_dev(dev); 1766 1767 if (pdev->is_virtfn) 1768 return 0; 1769 1770 return a->mode; 1771 } 1772 1773 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, 1774 struct attribute *a, int n) 1775 { 1776 struct device *dev = kobj_to_dev(kobj); 1777 struct pci_dev *pdev = to_pci_dev(dev); 1778 1779 if (pci_is_bridge(pdev)) 1780 return a->mode; 1781 1782 return 0; 1783 } 1784 1785 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, 1786 struct attribute *a, int n) 1787 { 1788 struct device *dev = kobj_to_dev(kobj); 1789 struct pci_dev *pdev = to_pci_dev(dev); 1790 1791 if (pci_is_pcie(pdev)) 1792 return a->mode; 1793 1794 return 0; 1795 } 1796 1797 static const struct attribute_group pci_dev_group = { 1798 .attrs = pci_dev_attrs, 1799 }; 1800 1801 const struct attribute_group *pci_dev_groups[] = { 1802 &pci_dev_group, 1803 &pci_dev_config_attr_group, 1804 &pci_dev_rom_attr_group, 1805 &pci_dev_reset_attr_group, 1806 &pci_dev_reset_method_attr_group, 1807 &pci_dev_vpd_attr_group, 1808 #ifdef CONFIG_DMI 1809 &pci_dev_smbios_attr_group, 1810 #endif 1811 #ifdef CONFIG_ACPI 1812 &pci_dev_acpi_attr_group, 1813 #endif 1814 &pci_dev_resource_resize_group, 1815 ARCH_PCI_DEV_GROUPS 1816 NULL, 1817 }; 1818 1819 static const struct attribute_group pci_dev_hp_attr_group = { 1820 .attrs = pci_dev_hp_attrs, 1821 .is_visible = pci_dev_hp_attrs_are_visible, 1822 }; 1823 1824 static const struct attribute_group pci_dev_attr_group = { 1825 .attrs = pci_dev_dev_attrs, 1826 .is_visible = pci_dev_attrs_are_visible, 1827 }; 1828 1829 static const struct attribute_group pci_bridge_attr_group = { 1830 .attrs = pci_bridge_attrs, 1831 .is_visible = pci_bridge_attrs_are_visible, 1832 }; 1833 1834 static const struct attribute_group pcie_dev_attr_group = { 1835 .attrs = pcie_dev_attrs, 1836 .is_visible = pcie_dev_attrs_are_visible, 1837 }; 1838 1839 const struct attribute_group *pci_dev_attr_groups[] = { 1840 &pci_dev_attr_group, 1841 &pci_dev_hp_attr_group, 1842 #ifdef CONFIG_PCI_IOV 1843 &sriov_pf_dev_attr_group, 1844 &sriov_vf_dev_attr_group, 1845 #endif 1846 &pci_bridge_attr_group, 1847 &pcie_dev_attr_group, 1848 #ifdef CONFIG_PCIEAER 1849 &aer_stats_attr_group, 1850 &aer_attr_group, 1851 #endif 1852 #ifdef CONFIG_PCIEASPM 1853 &aspm_ctrl_attr_group, 1854 #endif 1855 #ifdef CONFIG_PCI_DOE 1856 &pci_doe_sysfs_group, 1857 #endif 1858 #ifdef CONFIG_PCI_TSM 1859 &pci_tsm_auth_attr_group, 1860 &pci_tsm_attr_group, 1861 #endif 1862 NULL, 1863 }; 1864