1 // SPDX-License-Identifier: GPL-2.0
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>
10 * File attributes for PCI devices
12 * Modeled after usb's driverfs.c
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/pci.h>
19 #include <linux/stat.h>
20 #include <linux/export.h>
21 #include <linux/topology.h>
24 #include <linux/capability.h>
25 #include <linux/security.h>
26 #include <linux/slab.h>
27 #include <linux/vgaarb.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/msi.h>
33 static int sysfs_initialized; /* = 0 */
35 /* show configuration fields */
36 #define pci_config_attr(field, format_string) \
38 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
40 struct pci_dev *pdev; \
42 pdev = to_pci_dev(dev); \
43 return sysfs_emit(buf, format_string, pdev->field); \
45 static DEVICE_ATTR_RO(field)
47 pci_config_attr(vendor, "0x%04x\n");
48 pci_config_attr(device, "0x%04x\n");
49 pci_config_attr(subsystem_vendor, "0x%04x\n");
50 pci_config_attr(subsystem_device, "0x%04x\n");
51 pci_config_attr(revision, "0x%02x\n");
52 pci_config_attr(class, "0x%06x\n");
54 static ssize_t irq_show(struct device *dev,
55 struct device_attribute *attr,
58 struct pci_dev *pdev = to_pci_dev(dev);
62 * For MSI, show the first MSI IRQ; for all other cases including
63 * MSI-X, show the legacy INTx IRQ.
65 if (pdev->msi_enabled) {
66 struct msi_desc *desc = first_pci_msi_entry(pdev);
68 return sysfs_emit(buf, "%u\n", desc->irq);
72 return sysfs_emit(buf, "%u\n", pdev->irq);
74 static DEVICE_ATTR_RO(irq);
76 static ssize_t broken_parity_status_show(struct device *dev,
77 struct device_attribute *attr,
80 struct pci_dev *pdev = to_pci_dev(dev);
81 return sysfs_emit(buf, "%u\n", pdev->broken_parity_status);
84 static ssize_t broken_parity_status_store(struct device *dev,
85 struct device_attribute *attr,
86 const char *buf, size_t count)
88 struct pci_dev *pdev = to_pci_dev(dev);
91 if (kstrtoul(buf, 0, &val) < 0)
94 pdev->broken_parity_status = !!val;
98 static DEVICE_ATTR_RW(broken_parity_status);
100 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
101 struct device_attribute *attr, char *buf)
103 const struct cpumask *mask;
106 if (dev_to_node(dev) == NUMA_NO_NODE)
107 mask = cpu_online_mask;
109 mask = cpumask_of_node(dev_to_node(dev));
111 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
113 return cpumap_print_to_pagebuf(list, buf, mask);
116 static ssize_t local_cpus_show(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 return pci_dev_show_local_cpu(dev, false, attr, buf);
121 static DEVICE_ATTR_RO(local_cpus);
123 static ssize_t local_cpulist_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 return pci_dev_show_local_cpu(dev, true, attr, buf);
128 static DEVICE_ATTR_RO(local_cpulist);
131 * PCI Bus Class Devices
133 static ssize_t cpuaffinity_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
138 return cpumap_print_to_pagebuf(false, buf, cpumask);
140 static DEVICE_ATTR_RO(cpuaffinity);
142 static ssize_t cpulistaffinity_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
145 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
147 return cpumap_print_to_pagebuf(true, buf, cpumask);
149 static DEVICE_ATTR_RO(cpulistaffinity);
151 static ssize_t power_state_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct pci_dev *pdev = to_pci_dev(dev);
156 return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state));
158 static DEVICE_ATTR_RO(power_state);
161 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
164 struct pci_dev *pci_dev = to_pci_dev(dev);
167 resource_size_t start, end;
170 if (pci_dev->subordinate)
171 max = DEVICE_COUNT_RESOURCE;
173 max = PCI_BRIDGE_RESOURCES;
175 for (i = 0; i < max; i++) {
176 struct resource *res = &pci_dev->resource[i];
177 pci_resource_to_user(pci_dev, i, res, &start, &end);
178 len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n",
179 (unsigned long long)start,
180 (unsigned long long)end,
181 (unsigned long long)res->flags);
185 static DEVICE_ATTR_RO(resource);
187 static ssize_t max_link_speed_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 struct pci_dev *pdev = to_pci_dev(dev);
192 return sysfs_emit(buf, "%s\n",
193 pci_speed_string(pcie_get_speed_cap(pdev)));
195 static DEVICE_ATTR_RO(max_link_speed);
197 static ssize_t max_link_width_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 struct pci_dev *pdev = to_pci_dev(dev);
202 return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
204 static DEVICE_ATTR_RO(max_link_width);
206 static ssize_t current_link_speed_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 struct pci_dev *pci_dev = to_pci_dev(dev);
212 enum pci_bus_speed speed;
214 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
218 speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
220 return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
222 static DEVICE_ATTR_RO(current_link_speed);
224 static ssize_t current_link_width_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
227 struct pci_dev *pci_dev = to_pci_dev(dev);
231 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
235 return sysfs_emit(buf, "%u\n",
236 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
238 static DEVICE_ATTR_RO(current_link_width);
240 static ssize_t secondary_bus_number_show(struct device *dev,
241 struct device_attribute *attr,
244 struct pci_dev *pci_dev = to_pci_dev(dev);
248 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
252 return sysfs_emit(buf, "%u\n", sec_bus);
254 static DEVICE_ATTR_RO(secondary_bus_number);
256 static ssize_t subordinate_bus_number_show(struct device *dev,
257 struct device_attribute *attr,
260 struct pci_dev *pci_dev = to_pci_dev(dev);
264 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
268 return sysfs_emit(buf, "%u\n", sub_bus);
270 static DEVICE_ATTR_RO(subordinate_bus_number);
272 static ssize_t ari_enabled_show(struct device *dev,
273 struct device_attribute *attr,
276 struct pci_dev *pci_dev = to_pci_dev(dev);
278 return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
280 static DEVICE_ATTR_RO(ari_enabled);
282 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
285 struct pci_dev *pci_dev = to_pci_dev(dev);
287 return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
288 pci_dev->vendor, pci_dev->device,
289 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
290 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
291 (u8)(pci_dev->class));
293 static DEVICE_ATTR_RO(modalias);
295 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
298 struct pci_dev *pdev = to_pci_dev(dev);
302 /* this can crash the machine when done on the "wrong" device */
303 if (!capable(CAP_SYS_ADMIN))
306 if (kstrtoul(buf, 0, &val) < 0)
313 result = pci_enable_device(pdev);
314 else if (pci_is_enabled(pdev))
315 pci_disable_device(pdev);
320 return result < 0 ? result : count;
323 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
326 struct pci_dev *pdev;
328 pdev = to_pci_dev(dev);
329 return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt));
331 static DEVICE_ATTR_RW(enable);
334 static ssize_t numa_node_store(struct device *dev,
335 struct device_attribute *attr, const char *buf,
338 struct pci_dev *pdev = to_pci_dev(dev);
341 if (!capable(CAP_SYS_ADMIN))
344 if (kstrtoint(buf, 0, &node) < 0)
347 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
350 if (node != NUMA_NO_NODE && !node_online(node))
353 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
354 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
357 dev->numa_node = node;
361 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
364 return sysfs_emit(buf, "%d\n", dev->numa_node);
366 static DEVICE_ATTR_RW(numa_node);
369 static ssize_t dma_mask_bits_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 struct pci_dev *pdev = to_pci_dev(dev);
374 return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask));
376 static DEVICE_ATTR_RO(dma_mask_bits);
378 static ssize_t consistent_dma_mask_bits_show(struct device *dev,
379 struct device_attribute *attr,
382 return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask));
384 static DEVICE_ATTR_RO(consistent_dma_mask_bits);
386 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
389 struct pci_dev *pdev = to_pci_dev(dev);
390 struct pci_bus *subordinate = pdev->subordinate;
392 return sysfs_emit(buf, "%u\n", subordinate ?
393 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
397 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
398 const char *buf, size_t count)
400 struct pci_dev *pdev = to_pci_dev(dev);
401 struct pci_bus *subordinate = pdev->subordinate;
404 if (!capable(CAP_SYS_ADMIN))
407 if (kstrtoul(buf, 0, &val) < 0)
411 * "no_msi" and "bus_flags" only affect what happens when a driver
412 * requests MSI or MSI-X. They don't affect any drivers that have
413 * already requested MSI or MSI-X.
417 pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
418 val ? "allowed" : "disallowed");
423 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
425 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
427 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
428 val ? "allowed" : "disallowed");
431 static DEVICE_ATTR_RW(msi_bus);
433 static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count)
436 struct pci_bus *b = NULL;
438 if (kstrtoul(buf, 0, &val) < 0)
442 pci_lock_rescan_remove();
443 while ((b = pci_find_next_bus(b)) != NULL)
445 pci_unlock_rescan_remove();
449 static BUS_ATTR_WO(rescan);
451 static struct attribute *pci_bus_attrs[] = {
452 &bus_attr_rescan.attr,
456 static const struct attribute_group pci_bus_group = {
457 .attrs = pci_bus_attrs,
460 const struct attribute_group *pci_bus_groups[] = {
465 static ssize_t dev_rescan_store(struct device *dev,
466 struct device_attribute *attr, const char *buf,
470 struct pci_dev *pdev = to_pci_dev(dev);
472 if (kstrtoul(buf, 0, &val) < 0)
476 pci_lock_rescan_remove();
477 pci_rescan_bus(pdev->bus);
478 pci_unlock_rescan_remove();
482 static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL,
485 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
486 const char *buf, size_t count)
490 if (kstrtoul(buf, 0, &val) < 0)
493 if (val && device_remove_file_self(dev, attr))
494 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
497 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
500 static ssize_t bus_rescan_store(struct device *dev,
501 struct device_attribute *attr,
502 const char *buf, size_t count)
505 struct pci_bus *bus = to_pci_bus(dev);
507 if (kstrtoul(buf, 0, &val) < 0)
511 pci_lock_rescan_remove();
512 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
513 pci_rescan_bus_bridge_resize(bus->self);
516 pci_unlock_rescan_remove();
520 static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL,
523 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
524 static ssize_t d3cold_allowed_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
528 struct pci_dev *pdev = to_pci_dev(dev);
531 if (kstrtoul(buf, 0, &val) < 0)
534 pdev->d3cold_allowed = !!val;
535 if (pdev->d3cold_allowed)
536 pci_d3cold_enable(pdev);
538 pci_d3cold_disable(pdev);
540 pm_runtime_resume(dev);
545 static ssize_t d3cold_allowed_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
548 struct pci_dev *pdev = to_pci_dev(dev);
549 return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed);
551 static DEVICE_ATTR_RW(d3cold_allowed);
555 static ssize_t devspec_show(struct device *dev,
556 struct device_attribute *attr, char *buf)
558 struct pci_dev *pdev = to_pci_dev(dev);
559 struct device_node *np = pci_device_to_OF_node(pdev);
563 return sysfs_emit(buf, "%pOF\n", np);
565 static DEVICE_ATTR_RO(devspec);
568 static ssize_t driver_override_store(struct device *dev,
569 struct device_attribute *attr,
570 const char *buf, size_t count)
572 struct pci_dev *pdev = to_pci_dev(dev);
573 char *driver_override, *old, *cp;
575 /* We need to keep extra room for a newline */
576 if (count >= (PAGE_SIZE - 1))
579 driver_override = kstrndup(buf, count, GFP_KERNEL);
580 if (!driver_override)
583 cp = strchr(driver_override, '\n');
588 old = pdev->driver_override;
589 if (strlen(driver_override)) {
590 pdev->driver_override = driver_override;
592 kfree(driver_override);
593 pdev->driver_override = NULL;
602 static ssize_t driver_override_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
605 struct pci_dev *pdev = to_pci_dev(dev);
609 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
613 static DEVICE_ATTR_RW(driver_override);
615 static struct attribute *pci_dev_attrs[] = {
616 &dev_attr_power_state.attr,
617 &dev_attr_resource.attr,
618 &dev_attr_vendor.attr,
619 &dev_attr_device.attr,
620 &dev_attr_subsystem_vendor.attr,
621 &dev_attr_subsystem_device.attr,
622 &dev_attr_revision.attr,
623 &dev_attr_class.attr,
625 &dev_attr_local_cpus.attr,
626 &dev_attr_local_cpulist.attr,
627 &dev_attr_modalias.attr,
629 &dev_attr_numa_node.attr,
631 &dev_attr_dma_mask_bits.attr,
632 &dev_attr_consistent_dma_mask_bits.attr,
633 &dev_attr_enable.attr,
634 &dev_attr_broken_parity_status.attr,
635 &dev_attr_msi_bus.attr,
636 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
637 &dev_attr_d3cold_allowed.attr,
640 &dev_attr_devspec.attr,
642 &dev_attr_driver_override.attr,
643 &dev_attr_ari_enabled.attr,
647 static struct attribute *pci_bridge_attrs[] = {
648 &dev_attr_subordinate_bus_number.attr,
649 &dev_attr_secondary_bus_number.attr,
653 static struct attribute *pcie_dev_attrs[] = {
654 &dev_attr_current_link_speed.attr,
655 &dev_attr_current_link_width.attr,
656 &dev_attr_max_link_width.attr,
657 &dev_attr_max_link_speed.attr,
661 static struct attribute *pcibus_attrs[] = {
662 &dev_attr_bus_rescan.attr,
663 &dev_attr_cpuaffinity.attr,
664 &dev_attr_cpulistaffinity.attr,
668 static const struct attribute_group pcibus_group = {
669 .attrs = pcibus_attrs,
672 const struct attribute_group *pcibus_groups[] = {
677 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
680 struct pci_dev *pdev = to_pci_dev(dev);
681 struct pci_dev *vga_dev = vga_default_device();
684 return sysfs_emit(buf, "%u\n", (pdev == vga_dev));
686 return sysfs_emit(buf, "%u\n",
687 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
688 IORESOURCE_ROM_SHADOW));
690 static DEVICE_ATTR_RO(boot_vga);
692 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
693 struct bin_attribute *bin_attr, char *buf,
694 loff_t off, size_t count)
696 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
697 unsigned int size = 64;
698 loff_t init_off = off;
699 u8 *data = (u8 *) buf;
701 /* Several chips lock up trying to read undefined config space */
702 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
703 size = dev->cfg_size;
704 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
709 if (off + count > size) {
716 pci_config_pm_runtime_get(dev);
718 if ((off & 1) && size) {
720 pci_user_read_config_byte(dev, off, &val);
721 data[off - init_off] = val;
726 if ((off & 3) && size > 2) {
728 pci_user_read_config_word(dev, off, &val);
729 data[off - init_off] = val & 0xff;
730 data[off - init_off + 1] = (val >> 8) & 0xff;
737 pci_user_read_config_dword(dev, off, &val);
738 data[off - init_off] = val & 0xff;
739 data[off - init_off + 1] = (val >> 8) & 0xff;
740 data[off - init_off + 2] = (val >> 16) & 0xff;
741 data[off - init_off + 3] = (val >> 24) & 0xff;
749 pci_user_read_config_word(dev, off, &val);
750 data[off - init_off] = val & 0xff;
751 data[off - init_off + 1] = (val >> 8) & 0xff;
758 pci_user_read_config_byte(dev, off, &val);
759 data[off - init_off] = val;
764 pci_config_pm_runtime_put(dev);
769 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
770 struct bin_attribute *bin_attr, char *buf,
771 loff_t off, size_t count)
773 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
774 unsigned int size = count;
775 loff_t init_off = off;
776 u8 *data = (u8 *) buf;
779 ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
783 if (off > dev->cfg_size)
785 if (off + count > dev->cfg_size) {
786 size = dev->cfg_size - off;
790 pci_config_pm_runtime_get(dev);
792 if ((off & 1) && size) {
793 pci_user_write_config_byte(dev, off, data[off - init_off]);
798 if ((off & 3) && size > 2) {
799 u16 val = data[off - init_off];
800 val |= (u16) data[off - init_off + 1] << 8;
801 pci_user_write_config_word(dev, off, val);
807 u32 val = data[off - init_off];
808 val |= (u32) data[off - init_off + 1] << 8;
809 val |= (u32) data[off - init_off + 2] << 16;
810 val |= (u32) data[off - init_off + 3] << 24;
811 pci_user_write_config_dword(dev, off, val);
817 u16 val = data[off - init_off];
818 val |= (u16) data[off - init_off + 1] << 8;
819 pci_user_write_config_word(dev, off, val);
825 pci_user_write_config_byte(dev, off, data[off - init_off]);
830 pci_config_pm_runtime_put(dev);
834 static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
836 static struct bin_attribute *pci_dev_config_attrs[] = {
841 static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
842 struct bin_attribute *a, int n)
844 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
846 a->size = PCI_CFG_SPACE_SIZE;
847 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
848 a->size = PCI_CFG_SPACE_EXP_SIZE;
853 static const struct attribute_group pci_dev_config_attr_group = {
854 .bin_attrs = pci_dev_config_attrs,
855 .is_bin_visible = pci_dev_config_attr_is_visible,
858 #ifdef HAVE_PCI_LEGACY
860 * pci_read_legacy_io - read byte(s) from legacy I/O port space
861 * @filp: open sysfs file
862 * @kobj: kobject corresponding to file to read from
863 * @bin_attr: struct bin_attribute for this file
864 * @buf: buffer to store results
865 * @off: offset into legacy I/O port space
866 * @count: number of bytes to read
868 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
869 * callback routine (pci_legacy_read).
871 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
872 struct bin_attribute *bin_attr, char *buf,
873 loff_t off, size_t count)
875 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
877 /* Only support 1, 2 or 4 byte accesses */
878 if (count != 1 && count != 2 && count != 4)
881 return pci_legacy_read(bus, off, (u32 *)buf, count);
885 * pci_write_legacy_io - write byte(s) to legacy I/O port space
886 * @filp: open sysfs file
887 * @kobj: kobject corresponding to file to read from
888 * @bin_attr: struct bin_attribute for this file
889 * @buf: buffer containing value to be written
890 * @off: offset into legacy I/O port space
891 * @count: number of bytes to write
893 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
894 * callback routine (pci_legacy_write).
896 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
897 struct bin_attribute *bin_attr, char *buf,
898 loff_t off, size_t count)
900 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
902 /* Only support 1, 2 or 4 byte accesses */
903 if (count != 1 && count != 2 && count != 4)
906 return pci_legacy_write(bus, off, *(u32 *)buf, count);
910 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
911 * @filp: open sysfs file
912 * @kobj: kobject corresponding to device to be mapped
913 * @attr: struct bin_attribute for this file
914 * @vma: struct vm_area_struct passed to mmap
916 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
917 * legacy memory space (first meg of bus space) into application virtual
920 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
921 struct bin_attribute *attr,
922 struct vm_area_struct *vma)
924 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
926 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
930 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
931 * @filp: open sysfs file
932 * @kobj: kobject corresponding to device to be mapped
933 * @attr: struct bin_attribute for this file
934 * @vma: struct vm_area_struct passed to mmap
936 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
937 * legacy IO space (first meg of bus space) into application virtual
938 * memory space. Returns -ENOSYS if the operation isn't supported
940 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
941 struct bin_attribute *attr,
942 struct vm_area_struct *vma)
944 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
946 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
950 * pci_adjust_legacy_attr - adjustment of legacy file attributes
951 * @b: bus to create files under
952 * @mmap_type: I/O port or memory
954 * Stub implementation. Can be overridden by arch if necessary.
956 void __weak pci_adjust_legacy_attr(struct pci_bus *b,
957 enum pci_mmap_state mmap_type)
962 * pci_create_legacy_files - create legacy I/O port and memory files
963 * @b: bus to create files under
965 * Some platforms allow access to legacy I/O port and ISA memory space on
966 * a per-bus basis. This routine creates the files and ties them into
967 * their associated read, write and mmap files from pci-sysfs.c
969 * On error unwind, but don't propagate the error to the caller
970 * as it is ok to set up the PCI bus without these files.
972 void pci_create_legacy_files(struct pci_bus *b)
976 if (!sysfs_initialized)
979 b->legacy_io = kcalloc(2, sizeof(struct bin_attribute),
984 sysfs_bin_attr_init(b->legacy_io);
985 b->legacy_io->attr.name = "legacy_io";
986 b->legacy_io->size = 0xffff;
987 b->legacy_io->attr.mode = 0600;
988 b->legacy_io->read = pci_read_legacy_io;
989 b->legacy_io->write = pci_write_legacy_io;
990 b->legacy_io->mmap = pci_mmap_legacy_io;
991 b->legacy_io->f_mapping = iomem_get_mapping;
992 pci_adjust_legacy_attr(b, pci_mmap_io);
993 error = device_create_bin_file(&b->dev, b->legacy_io);
997 /* Allocated above after the legacy_io struct */
998 b->legacy_mem = b->legacy_io + 1;
999 sysfs_bin_attr_init(b->legacy_mem);
1000 b->legacy_mem->attr.name = "legacy_mem";
1001 b->legacy_mem->size = 1024*1024;
1002 b->legacy_mem->attr.mode = 0600;
1003 b->legacy_mem->mmap = pci_mmap_legacy_mem;
1004 b->legacy_mem->f_mapping = iomem_get_mapping;
1005 pci_adjust_legacy_attr(b, pci_mmap_mem);
1006 error = device_create_bin_file(&b->dev, b->legacy_mem);
1008 goto legacy_mem_err;
1013 device_remove_bin_file(&b->dev, b->legacy_io);
1015 kfree(b->legacy_io);
1016 b->legacy_io = NULL;
1018 dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
1021 void pci_remove_legacy_files(struct pci_bus *b)
1024 device_remove_bin_file(&b->dev, b->legacy_io);
1025 device_remove_bin_file(&b->dev, b->legacy_mem);
1026 kfree(b->legacy_io); /* both are allocated here */
1029 #endif /* HAVE_PCI_LEGACY */
1031 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1033 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1034 enum pci_mmap_api mmap_api)
1036 unsigned long nr, start, size;
1037 resource_size_t pci_start = 0, pci_end;
1039 if (pci_resource_len(pdev, resno) == 0)
1041 nr = vma_pages(vma);
1042 start = vma->vm_pgoff;
1043 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
1044 if (mmap_api == PCI_MMAP_PROCFS) {
1045 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1046 &pci_start, &pci_end);
1047 pci_start >>= PAGE_SHIFT;
1049 if (start >= pci_start && start < pci_start + size &&
1050 start + nr <= pci_start + size)
1056 * pci_mmap_resource - map a PCI resource into user memory space
1057 * @kobj: kobject for mapping
1058 * @attr: struct bin_attribute for the file being mapped
1059 * @vma: struct vm_area_struct passed into the mmap
1060 * @write_combine: 1 for write_combine mapping
1062 * Use the regular PCI mapping routines to map a PCI resource into userspace.
1064 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1065 struct vm_area_struct *vma, int write_combine)
1067 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1068 int bar = (unsigned long)attr->private;
1069 enum pci_mmap_state mmap_type;
1070 struct resource *res = &pdev->resource[bar];
1073 ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1077 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1080 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
1083 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1085 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
1088 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1089 struct bin_attribute *attr,
1090 struct vm_area_struct *vma)
1092 return pci_mmap_resource(kobj, attr, vma, 0);
1095 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1096 struct bin_attribute *attr,
1097 struct vm_area_struct *vma)
1099 return pci_mmap_resource(kobj, attr, vma, 1);
1102 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1103 struct bin_attribute *attr, char *buf,
1104 loff_t off, size_t count, bool write)
1106 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1107 int bar = (unsigned long)attr->private;
1108 unsigned long port = off;
1110 port += pci_resource_start(pdev, bar);
1112 if (port > pci_resource_end(pdev, bar))
1115 if (port + count - 1 > pci_resource_end(pdev, bar))
1121 outb(*(u8 *)buf, port);
1123 *(u8 *)buf = inb(port);
1127 outw(*(u16 *)buf, port);
1129 *(u16 *)buf = inw(port);
1133 outl(*(u32 *)buf, port);
1135 *(u32 *)buf = inl(port);
1141 static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1142 struct bin_attribute *attr, char *buf,
1143 loff_t off, size_t count)
1145 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1148 static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1149 struct bin_attribute *attr, char *buf,
1150 loff_t off, size_t count)
1154 ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1158 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1162 * pci_remove_resource_files - cleanup resource files
1163 * @pdev: dev to cleanup
1165 * If we created resource files for @pdev, remove them from sysfs and
1166 * free their resources.
1168 static void pci_remove_resource_files(struct pci_dev *pdev)
1172 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1173 struct bin_attribute *res_attr;
1175 res_attr = pdev->res_attr[i];
1177 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1181 res_attr = pdev->res_attr_wc[i];
1183 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1189 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1191 /* allocate attribute structure, piggyback attribute name */
1192 int name_len = write_combine ? 13 : 10;
1193 struct bin_attribute *res_attr;
1194 char *res_attr_name;
1197 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1201 res_attr_name = (char *)(res_attr + 1);
1203 sysfs_bin_attr_init(res_attr);
1204 if (write_combine) {
1205 pdev->res_attr_wc[num] = res_attr;
1206 sprintf(res_attr_name, "resource%d_wc", num);
1207 res_attr->mmap = pci_mmap_resource_wc;
1209 pdev->res_attr[num] = res_attr;
1210 sprintf(res_attr_name, "resource%d", num);
1211 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1212 res_attr->read = pci_read_resource_io;
1213 res_attr->write = pci_write_resource_io;
1214 if (arch_can_pci_mmap_io())
1215 res_attr->mmap = pci_mmap_resource_uc;
1217 res_attr->mmap = pci_mmap_resource_uc;
1221 res_attr->f_mapping = iomem_get_mapping;
1222 res_attr->attr.name = res_attr_name;
1223 res_attr->attr.mode = 0600;
1224 res_attr->size = pci_resource_len(pdev, num);
1225 res_attr->private = (void *)(unsigned long)num;
1226 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1234 * pci_create_resource_files - create resource files in sysfs for @dev
1235 * @pdev: dev in question
1237 * Walk the resources in @pdev creating files for each resource available.
1239 static int pci_create_resource_files(struct pci_dev *pdev)
1244 /* Expose the PCI resources from this device as files */
1245 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1247 /* skip empty resources */
1248 if (!pci_resource_len(pdev, i))
1251 retval = pci_create_attr(pdev, i, 0);
1252 /* for prefetchable resources, create a WC mappable file */
1253 if (!retval && arch_can_pci_mmap_wc() &&
1254 pdev->resource[i].flags & IORESOURCE_PREFETCH)
1255 retval = pci_create_attr(pdev, i, 1);
1257 pci_remove_resource_files(pdev);
1263 #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
1264 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1265 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1269 * pci_write_rom - used to enable access to the PCI ROM display
1271 * @kobj: kernel object handle
1272 * @bin_attr: struct bin_attribute for this file
1275 * @count: number of byte in input
1277 * writing anything except 0 enables it
1279 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1280 struct bin_attribute *bin_attr, char *buf,
1281 loff_t off, size_t count)
1283 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1285 if ((off == 0) && (*buf == '0') && (count == 2))
1286 pdev->rom_attr_enabled = 0;
1288 pdev->rom_attr_enabled = 1;
1294 * pci_read_rom - read a PCI ROM
1296 * @kobj: kernel object handle
1297 * @bin_attr: struct bin_attribute for this file
1298 * @buf: where to put the data we read from the ROM
1300 * @count: number of bytes to read
1302 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1303 * device corresponding to @kobj.
1305 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1306 struct bin_attribute *bin_attr, char *buf,
1307 loff_t off, size_t count)
1309 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1313 if (!pdev->rom_attr_enabled)
1316 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1323 if (off + count > size)
1326 memcpy_fromio(buf, rom + off, count);
1328 pci_unmap_rom(pdev, rom);
1332 static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
1334 static struct bin_attribute *pci_dev_rom_attrs[] = {
1339 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
1340 struct bin_attribute *a, int n)
1342 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1345 /* If the device has a ROM, try to expose it in sysfs. */
1346 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1352 return a->attr.mode;
1355 static const struct attribute_group pci_dev_rom_attr_group = {
1356 .bin_attrs = pci_dev_rom_attrs,
1357 .is_bin_visible = pci_dev_rom_attr_is_visible,
1360 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1361 const char *buf, size_t count)
1363 struct pci_dev *pdev = to_pci_dev(dev);
1367 if (kstrtoul(buf, 0, &val) < 0)
1373 pm_runtime_get_sync(dev);
1374 result = pci_reset_function(pdev);
1375 pm_runtime_put(dev);
1381 static DEVICE_ATTR_WO(reset);
1383 static struct attribute *pci_dev_reset_attrs[] = {
1384 &dev_attr_reset.attr,
1388 static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj,
1389 struct attribute *a, int n)
1391 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1393 if (!pci_reset_supported(pdev))
1399 static const struct attribute_group pci_dev_reset_attr_group = {
1400 .attrs = pci_dev_reset_attrs,
1401 .is_visible = pci_dev_reset_attr_is_visible,
1404 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
1406 if (!sysfs_initialized)
1409 return pci_create_resource_files(pdev);
1413 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1414 * @pdev: device whose entries we should free
1416 * Cleanup when @pdev is removed from sysfs.
1418 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1420 if (!sysfs_initialized)
1423 pci_remove_resource_files(pdev);
1426 static int __init pci_sysfs_init(void)
1428 struct pci_dev *pdev = NULL;
1429 struct pci_bus *pbus = NULL;
1432 sysfs_initialized = 1;
1433 for_each_pci_dev(pdev) {
1434 retval = pci_create_sysfs_dev_files(pdev);
1441 while ((pbus = pci_find_next_bus(pbus)))
1442 pci_create_legacy_files(pbus);
1446 late_initcall(pci_sysfs_init);
1448 static struct attribute *pci_dev_dev_attrs[] = {
1449 &dev_attr_boot_vga.attr,
1453 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1454 struct attribute *a, int n)
1456 struct device *dev = kobj_to_dev(kobj);
1457 struct pci_dev *pdev = to_pci_dev(dev);
1459 if (a == &dev_attr_boot_vga.attr)
1460 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1466 static struct attribute *pci_dev_hp_attrs[] = {
1467 &dev_attr_remove.attr,
1468 &dev_attr_dev_rescan.attr,
1472 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1473 struct attribute *a, int n)
1475 struct device *dev = kobj_to_dev(kobj);
1476 struct pci_dev *pdev = to_pci_dev(dev);
1478 if (pdev->is_virtfn)
1484 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1485 struct attribute *a, int n)
1487 struct device *dev = kobj_to_dev(kobj);
1488 struct pci_dev *pdev = to_pci_dev(dev);
1490 if (pci_is_bridge(pdev))
1496 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1497 struct attribute *a, int n)
1499 struct device *dev = kobj_to_dev(kobj);
1500 struct pci_dev *pdev = to_pci_dev(dev);
1502 if (pci_is_pcie(pdev))
1508 static const struct attribute_group pci_dev_group = {
1509 .attrs = pci_dev_attrs,
1512 const struct attribute_group *pci_dev_groups[] = {
1514 &pci_dev_config_attr_group,
1515 &pci_dev_rom_attr_group,
1516 &pci_dev_reset_attr_group,
1517 &pci_dev_reset_method_attr_group,
1518 &pci_dev_vpd_attr_group,
1520 &pci_dev_smbios_attr_group,
1523 &pci_dev_acpi_attr_group,
1528 static const struct attribute_group pci_dev_hp_attr_group = {
1529 .attrs = pci_dev_hp_attrs,
1530 .is_visible = pci_dev_hp_attrs_are_visible,
1533 static const struct attribute_group pci_dev_attr_group = {
1534 .attrs = pci_dev_dev_attrs,
1535 .is_visible = pci_dev_attrs_are_visible,
1538 static const struct attribute_group pci_bridge_attr_group = {
1539 .attrs = pci_bridge_attrs,
1540 .is_visible = pci_bridge_attrs_are_visible,
1543 static const struct attribute_group pcie_dev_attr_group = {
1544 .attrs = pcie_dev_attrs,
1545 .is_visible = pcie_dev_attrs_are_visible,
1548 static const struct attribute_group *pci_dev_attr_groups[] = {
1549 &pci_dev_attr_group,
1550 &pci_dev_hp_attr_group,
1551 #ifdef CONFIG_PCI_IOV
1552 &sriov_pf_dev_attr_group,
1553 &sriov_vf_dev_attr_group,
1555 &pci_bridge_attr_group,
1556 &pcie_dev_attr_group,
1557 #ifdef CONFIG_PCIEAER
1558 &aer_stats_attr_group,
1560 #ifdef CONFIG_PCIEASPM
1561 &aspm_ctrl_attr_group,
1566 const struct device_type pci_dev_type = {
1567 .groups = pci_dev_attr_groups,