2 * drivers/pci/pci-sysfs.c
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
11 * File attributes for PCI devices
13 * Modeled after usb's driverfs.c
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/pci.h>
21 #include <linux/stat.h>
22 #include <linux/export.h>
23 #include <linux/topology.h>
26 #include <linux/capability.h>
27 #include <linux/security.h>
28 #include <linux/pci-aspm.h>
29 #include <linux/slab.h>
30 #include <linux/vgaarb.h>
31 #include <linux/pm_runtime.h>
34 static int sysfs_initialized; /* = 0 */
36 /* show configuration fields */
37 #define pci_config_attr(field, format_string) \
39 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
41 struct pci_dev *pdev; \
43 pdev = to_pci_dev (dev); \
44 return sprintf (buf, format_string, pdev->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(class, "0x%06x\n");
52 pci_config_attr(irq, "%u\n");
54 static ssize_t broken_parity_status_show(struct device *dev,
55 struct device_attribute *attr,
58 struct pci_dev *pdev = to_pci_dev(dev);
59 return sprintf (buf, "%u\n", pdev->broken_parity_status);
62 static ssize_t broken_parity_status_store(struct device *dev,
63 struct device_attribute *attr,
64 const char *buf, size_t count)
66 struct pci_dev *pdev = to_pci_dev(dev);
69 if (strict_strtoul(buf, 0, &val) < 0)
72 pdev->broken_parity_status = !!val;
77 static ssize_t local_cpus_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
80 const struct cpumask *mask;
84 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
85 cpumask_of_node(dev_to_node(dev));
87 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
89 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
96 static ssize_t local_cpulist_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 const struct cpumask *mask;
103 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
104 cpumask_of_node(dev_to_node(dev));
106 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
108 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
115 * PCI Bus Class Devices
117 static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
119 struct device_attribute *attr,
123 const struct cpumask *cpumask;
125 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
127 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
128 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
134 static inline ssize_t pci_bus_show_cpumaskaffinity(struct device *dev,
135 struct device_attribute *attr,
138 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
141 static inline ssize_t pci_bus_show_cpulistaffinity(struct device *dev,
142 struct device_attribute *attr,
145 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
150 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
152 struct pci_dev * pci_dev = to_pci_dev(dev);
156 resource_size_t start, end;
158 if (pci_dev->subordinate)
159 max = DEVICE_COUNT_RESOURCE;
161 max = PCI_BRIDGE_RESOURCES;
163 for (i = 0; i < max; i++) {
164 struct resource *res = &pci_dev->resource[i];
165 pci_resource_to_user(pci_dev, i, res, &start, &end);
166 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
167 (unsigned long long)start,
168 (unsigned long long)end,
169 (unsigned long long)res->flags);
174 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
176 struct pci_dev *pci_dev = to_pci_dev(dev);
178 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
179 pci_dev->vendor, pci_dev->device,
180 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
181 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
182 (u8)(pci_dev->class));
185 static ssize_t is_enabled_store(struct device *dev,
186 struct device_attribute *attr, const char *buf,
189 struct pci_dev *pdev = to_pci_dev(dev);
191 ssize_t result = strict_strtoul(buf, 0, &val);
196 /* this can crash the machine when done on the "wrong" device */
197 if (!capable(CAP_SYS_ADMIN))
201 if (pci_is_enabled(pdev))
202 pci_disable_device(pdev);
206 result = pci_enable_device(pdev);
208 return result < 0 ? result : count;
211 static ssize_t is_enabled_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct pci_dev *pdev;
216 pdev = to_pci_dev (dev);
217 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
222 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
224 return sprintf (buf, "%d\n", dev->numa_node);
229 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
231 struct pci_dev *pdev = to_pci_dev(dev);
233 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
237 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
240 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
244 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
246 struct pci_dev *pdev = to_pci_dev(dev);
248 if (!pdev->subordinate)
251 return sprintf (buf, "%u\n",
252 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
256 msi_bus_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
259 struct pci_dev *pdev = to_pci_dev(dev);
262 if (strict_strtoul(buf, 0, &val) < 0)
265 /* bad things may happen if the no_msi flag is changed
266 * while some drivers are loaded */
267 if (!capable(CAP_SYS_ADMIN))
270 /* Maybe pci devices without subordinate busses shouldn't even have this
271 * attribute in the first place? */
272 if (!pdev->subordinate)
275 /* Is the flag going to change, or keep the value it already had? */
276 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
278 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
280 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
281 " bad things could happen\n", val ? "" : " not");
287 #ifdef CONFIG_HOTPLUG
288 static DEFINE_MUTEX(pci_remove_rescan_mutex);
289 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
293 struct pci_bus *b = NULL;
295 if (strict_strtoul(buf, 0, &val) < 0)
299 mutex_lock(&pci_remove_rescan_mutex);
300 while ((b = pci_find_next_bus(b)) != NULL)
302 mutex_unlock(&pci_remove_rescan_mutex);
307 struct bus_attribute pci_bus_attrs[] = {
308 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
313 dev_rescan_store(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t count)
317 struct pci_dev *pdev = to_pci_dev(dev);
319 if (strict_strtoul(buf, 0, &val) < 0)
323 mutex_lock(&pci_remove_rescan_mutex);
324 pci_rescan_bus(pdev->bus);
325 mutex_unlock(&pci_remove_rescan_mutex);
330 static void remove_callback(struct device *dev)
332 struct pci_dev *pdev = to_pci_dev(dev);
334 mutex_lock(&pci_remove_rescan_mutex);
335 pci_stop_and_remove_bus_device(pdev);
336 mutex_unlock(&pci_remove_rescan_mutex);
340 remove_store(struct device *dev, struct device_attribute *dummy,
341 const char *buf, size_t count)
346 if (strict_strtoul(buf, 0, &val) < 0)
349 /* An attribute cannot be unregistered by one of its own methods,
350 * so we have to use this roundabout approach.
353 ret = device_schedule_callback(dev, remove_callback);
360 dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
364 struct pci_bus *bus = to_pci_bus(dev);
366 if (strict_strtoul(buf, 0, &val) < 0)
370 mutex_lock(&pci_remove_rescan_mutex);
371 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
372 pci_rescan_bus_bridge_resize(bus->self);
375 mutex_unlock(&pci_remove_rescan_mutex);
382 #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
383 static ssize_t d3cold_allowed_store(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
387 struct pci_dev *pdev = to_pci_dev(dev);
390 if (strict_strtoul(buf, 0, &val) < 0)
393 pdev->d3cold_allowed = !!val;
394 pm_runtime_resume(dev);
399 static ssize_t d3cold_allowed_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct pci_dev *pdev = to_pci_dev(dev);
403 return sprintf (buf, "%u\n", pdev->d3cold_allowed);
407 #ifdef CONFIG_PCI_IOV
408 static ssize_t sriov_totalvfs_show(struct device *dev,
409 struct device_attribute *attr,
412 struct pci_dev *pdev = to_pci_dev(dev);
414 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
418 static ssize_t sriov_numvfs_show(struct device *dev,
419 struct device_attribute *attr,
422 struct pci_dev *pdev = to_pci_dev(dev);
424 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
428 * num_vfs > 0; number of vfs to enable
429 * num_vfs = 0; disable all vfs
431 * Note: SRIOV spec doesn't allow partial VF
432 * disable, so its all or none.
434 static ssize_t sriov_numvfs_store(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
438 struct pci_dev *pdev = to_pci_dev(dev);
439 int num_vfs_enabled = 0;
444 if (kstrtoint(buf, 0, &num_vfs) < 0)
447 /* is PF driver loaded w/callback */
448 if (!pdev->driver || !pdev->driver->sriov_configure) {
450 "Driver doesn't support SRIOV configuration via sysfs\n");
454 /* if enabling vf's ... */
455 total = pci_sriov_get_totalvfs(pdev);
456 /* Requested VFs to enable < totalvfs and none enabled already */
457 if ((num_vfs > 0) && (num_vfs <= total)) {
458 if (pdev->sriov->num_VFs == 0) {
460 pdev->driver->sriov_configure(pdev, num_vfs);
461 if ((num_vfs_enabled >= 0) &&
462 (num_vfs_enabled != num_vfs)) {
464 "Only %d VFs enabled\n",
467 } else if (num_vfs_enabled < 0)
468 /* error code from driver callback */
469 return num_vfs_enabled;
470 } else if (num_vfs == pdev->sriov->num_VFs) {
472 "%d VFs already enabled; no enable action taken\n",
477 "%d VFs already enabled. Disable before enabling %d VFs\n",
478 pdev->sriov->num_VFs, num_vfs);
485 if (pdev->sriov->num_VFs != 0) {
486 ret = pdev->driver->sriov_configure(pdev, 0);
487 return ret ? ret : count;
490 "All VFs disabled; no disable action taken\n");
496 "Invalid value for number of VFs to enable: %d\n", num_vfs);
501 static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
502 static struct device_attribute sriov_numvfs_attr =
503 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
504 sriov_numvfs_show, sriov_numvfs_store);
505 #endif /* CONFIG_PCI_IOV */
507 struct device_attribute pci_dev_attrs[] = {
511 __ATTR_RO(subsystem_vendor),
512 __ATTR_RO(subsystem_device),
515 __ATTR_RO(local_cpus),
516 __ATTR_RO(local_cpulist),
519 __ATTR_RO(numa_node),
521 __ATTR_RO(dma_mask_bits),
522 __ATTR_RO(consistent_dma_mask_bits),
523 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
524 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
525 broken_parity_status_show,broken_parity_status_store),
526 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
527 #ifdef CONFIG_HOTPLUG
528 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
529 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
531 #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
532 __ATTR(d3cold_allowed, 0644, d3cold_allowed_show, d3cold_allowed_store),
537 struct device_attribute pcibus_dev_attrs[] = {
538 #ifdef CONFIG_HOTPLUG
539 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
541 __ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL),
542 __ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL),
547 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
549 struct pci_dev *pdev = to_pci_dev(dev);
550 struct pci_dev *vga_dev = vga_default_device();
553 return sprintf(buf, "%u\n", (pdev == vga_dev));
555 return sprintf(buf, "%u\n",
556 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
557 IORESOURCE_ROM_SHADOW));
559 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
562 pci_read_config(struct file *filp, struct kobject *kobj,
563 struct bin_attribute *bin_attr,
564 char *buf, loff_t off, size_t count)
566 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
567 unsigned int size = 64;
568 loff_t init_off = off;
569 u8 *data = (u8*) buf;
571 /* Several chips lock up trying to read undefined config space */
572 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
573 size = dev->cfg_size;
574 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
580 if (off + count > size) {
587 pci_config_pm_runtime_get(dev);
589 if ((off & 1) && size) {
591 pci_user_read_config_byte(dev, off, &val);
592 data[off - init_off] = val;
597 if ((off & 3) && size > 2) {
599 pci_user_read_config_word(dev, off, &val);
600 data[off - init_off] = val & 0xff;
601 data[off - init_off + 1] = (val >> 8) & 0xff;
608 pci_user_read_config_dword(dev, off, &val);
609 data[off - init_off] = val & 0xff;
610 data[off - init_off + 1] = (val >> 8) & 0xff;
611 data[off - init_off + 2] = (val >> 16) & 0xff;
612 data[off - init_off + 3] = (val >> 24) & 0xff;
619 pci_user_read_config_word(dev, off, &val);
620 data[off - init_off] = val & 0xff;
621 data[off - init_off + 1] = (val >> 8) & 0xff;
628 pci_user_read_config_byte(dev, off, &val);
629 data[off - init_off] = val;
634 pci_config_pm_runtime_put(dev);
640 pci_write_config(struct file* filp, struct kobject *kobj,
641 struct bin_attribute *bin_attr,
642 char *buf, loff_t off, size_t count)
644 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
645 unsigned int size = count;
646 loff_t init_off = off;
647 u8 *data = (u8*) buf;
649 if (off > dev->cfg_size)
651 if (off + count > dev->cfg_size) {
652 size = dev->cfg_size - off;
656 pci_config_pm_runtime_get(dev);
658 if ((off & 1) && size) {
659 pci_user_write_config_byte(dev, off, data[off - init_off]);
664 if ((off & 3) && size > 2) {
665 u16 val = data[off - init_off];
666 val |= (u16) data[off - init_off + 1] << 8;
667 pci_user_write_config_word(dev, off, val);
673 u32 val = data[off - init_off];
674 val |= (u32) data[off - init_off + 1] << 8;
675 val |= (u32) data[off - init_off + 2] << 16;
676 val |= (u32) data[off - init_off + 3] << 24;
677 pci_user_write_config_dword(dev, off, val);
683 u16 val = data[off - init_off];
684 val |= (u16) data[off - init_off + 1] << 8;
685 pci_user_write_config_word(dev, off, val);
691 pci_user_write_config_byte(dev, off, data[off - init_off]);
696 pci_config_pm_runtime_put(dev);
702 read_vpd_attr(struct file *filp, struct kobject *kobj,
703 struct bin_attribute *bin_attr,
704 char *buf, loff_t off, size_t count)
706 struct pci_dev *dev =
707 to_pci_dev(container_of(kobj, struct device, kobj));
709 if (off > bin_attr->size)
711 else if (count > bin_attr->size - off)
712 count = bin_attr->size - off;
714 return pci_read_vpd(dev, off, count, buf);
718 write_vpd_attr(struct file *filp, struct kobject *kobj,
719 struct bin_attribute *bin_attr,
720 char *buf, loff_t off, size_t count)
722 struct pci_dev *dev =
723 to_pci_dev(container_of(kobj, struct device, kobj));
725 if (off > bin_attr->size)
727 else if (count > bin_attr->size - off)
728 count = bin_attr->size - off;
730 return pci_write_vpd(dev, off, count, buf);
733 #ifdef HAVE_PCI_LEGACY
735 * pci_read_legacy_io - read byte(s) from legacy I/O port space
736 * @filp: open sysfs file
737 * @kobj: kobject corresponding to file to read from
738 * @bin_attr: struct bin_attribute for this file
739 * @buf: buffer to store results
740 * @off: offset into legacy I/O port space
741 * @count: number of bytes to read
743 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
744 * callback routine (pci_legacy_read).
747 pci_read_legacy_io(struct file *filp, struct kobject *kobj,
748 struct bin_attribute *bin_attr,
749 char *buf, loff_t off, size_t count)
751 struct pci_bus *bus = to_pci_bus(container_of(kobj,
755 /* Only support 1, 2 or 4 byte accesses */
756 if (count != 1 && count != 2 && count != 4)
759 return pci_legacy_read(bus, off, (u32 *)buf, count);
763 * pci_write_legacy_io - write byte(s) to legacy I/O port space
764 * @filp: open sysfs file
765 * @kobj: kobject corresponding to file to read from
766 * @bin_attr: struct bin_attribute for this file
767 * @buf: buffer containing value to be written
768 * @off: offset into legacy I/O port space
769 * @count: number of bytes to write
771 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
772 * callback routine (pci_legacy_write).
775 pci_write_legacy_io(struct file *filp, struct kobject *kobj,
776 struct bin_attribute *bin_attr,
777 char *buf, loff_t off, size_t count)
779 struct pci_bus *bus = to_pci_bus(container_of(kobj,
782 /* Only support 1, 2 or 4 byte accesses */
783 if (count != 1 && count != 2 && count != 4)
786 return pci_legacy_write(bus, off, *(u32 *)buf, count);
790 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
791 * @filp: open sysfs file
792 * @kobj: kobject corresponding to device to be mapped
793 * @attr: struct bin_attribute for this file
794 * @vma: struct vm_area_struct passed to mmap
796 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
797 * legacy memory space (first meg of bus space) into application virtual
801 pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
802 struct bin_attribute *attr,
803 struct vm_area_struct *vma)
805 struct pci_bus *bus = to_pci_bus(container_of(kobj,
809 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
813 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
814 * @filp: open sysfs file
815 * @kobj: kobject corresponding to device to be mapped
816 * @attr: struct bin_attribute for this file
817 * @vma: struct vm_area_struct passed to mmap
819 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
820 * legacy IO space (first meg of bus space) into application virtual
821 * memory space. Returns -ENOSYS if the operation isn't supported
824 pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
825 struct bin_attribute *attr,
826 struct vm_area_struct *vma)
828 struct pci_bus *bus = to_pci_bus(container_of(kobj,
832 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
836 * pci_adjust_legacy_attr - adjustment of legacy file attributes
837 * @b: bus to create files under
838 * @mmap_type: I/O port or memory
840 * Stub implementation. Can be overridden by arch if necessary.
843 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
849 * pci_create_legacy_files - create legacy I/O port and memory files
850 * @b: bus to create files under
852 * Some platforms allow access to legacy I/O port and ISA memory space on
853 * a per-bus basis. This routine creates the files and ties them into
854 * their associated read, write and mmap files from pci-sysfs.c
856 * On error unwind, but don't propagate the error to the caller
857 * as it is ok to set up the PCI bus without these files.
859 void pci_create_legacy_files(struct pci_bus *b)
863 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
868 sysfs_bin_attr_init(b->legacy_io);
869 b->legacy_io->attr.name = "legacy_io";
870 b->legacy_io->size = 0xffff;
871 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
872 b->legacy_io->read = pci_read_legacy_io;
873 b->legacy_io->write = pci_write_legacy_io;
874 b->legacy_io->mmap = pci_mmap_legacy_io;
875 pci_adjust_legacy_attr(b, pci_mmap_io);
876 error = device_create_bin_file(&b->dev, b->legacy_io);
880 /* Allocated above after the legacy_io struct */
881 b->legacy_mem = b->legacy_io + 1;
882 sysfs_bin_attr_init(b->legacy_mem);
883 b->legacy_mem->attr.name = "legacy_mem";
884 b->legacy_mem->size = 1024*1024;
885 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
886 b->legacy_mem->mmap = pci_mmap_legacy_mem;
887 pci_adjust_legacy_attr(b, pci_mmap_mem);
888 error = device_create_bin_file(&b->dev, b->legacy_mem);
895 device_remove_bin_file(&b->dev, b->legacy_io);
900 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
901 "and ISA memory resources to sysfs\n");
905 void pci_remove_legacy_files(struct pci_bus *b)
908 device_remove_bin_file(&b->dev, b->legacy_io);
909 device_remove_bin_file(&b->dev, b->legacy_mem);
910 kfree(b->legacy_io); /* both are allocated here */
913 #endif /* HAVE_PCI_LEGACY */
917 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
918 enum pci_mmap_api mmap_api)
920 unsigned long nr, start, size, pci_start;
922 if (pci_resource_len(pdev, resno) == 0)
924 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
925 start = vma->vm_pgoff;
926 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
927 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
928 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
929 if (start >= pci_start && start < pci_start + size &&
930 start + nr <= pci_start + size)
936 * pci_mmap_resource - map a PCI resource into user memory space
937 * @kobj: kobject for mapping
938 * @attr: struct bin_attribute for the file being mapped
939 * @vma: struct vm_area_struct passed into the mmap
940 * @write_combine: 1 for write_combine mapping
942 * Use the regular PCI mapping routines to map a PCI resource into userspace.
945 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
946 struct vm_area_struct *vma, int write_combine)
948 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
949 struct device, kobj));
950 struct resource *res = attr->private;
951 enum pci_mmap_state mmap_type;
952 resource_size_t start, end;
955 for (i = 0; i < PCI_ROM_RESOURCE; i++)
956 if (res == &pdev->resource[i])
958 if (i >= PCI_ROM_RESOURCE)
961 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
962 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
963 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
964 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
966 (u64)pci_resource_start(pdev, i),
967 (u64)pci_resource_len(pdev, i));
971 /* pci_mmap_page_range() expects the same kind of entry as coming
972 * from /proc/bus/pci/ which is a "user visible" value. If this is
973 * different from the resource itself, arch will do necessary fixup.
975 pci_resource_to_user(pdev, i, res, &start, &end);
976 vma->vm_pgoff += start >> PAGE_SHIFT;
977 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
979 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
982 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
986 pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
987 struct bin_attribute *attr,
988 struct vm_area_struct *vma)
990 return pci_mmap_resource(kobj, attr, vma, 0);
994 pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
995 struct bin_attribute *attr,
996 struct vm_area_struct *vma)
998 return pci_mmap_resource(kobj, attr, vma, 1);
1002 pci_resource_io(struct file *filp, struct kobject *kobj,
1003 struct bin_attribute *attr, char *buf,
1004 loff_t off, size_t count, bool write)
1006 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
1007 struct device, kobj));
1008 struct resource *res = attr->private;
1009 unsigned long port = off;
1012 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1013 if (res == &pdev->resource[i])
1015 if (i >= PCI_ROM_RESOURCE)
1018 port += pci_resource_start(pdev, i);
1020 if (port > pci_resource_end(pdev, i))
1023 if (port + count - 1 > pci_resource_end(pdev, i))
1029 outb(*(u8 *)buf, port);
1031 *(u8 *)buf = inb(port);
1035 outw(*(u16 *)buf, port);
1037 *(u16 *)buf = inw(port);
1041 outl(*(u32 *)buf, port);
1043 *(u32 *)buf = inl(port);
1050 pci_read_resource_io(struct file *filp, struct kobject *kobj,
1051 struct bin_attribute *attr, char *buf,
1052 loff_t off, size_t count)
1054 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1058 pci_write_resource_io(struct file *filp, struct kobject *kobj,
1059 struct bin_attribute *attr, char *buf,
1060 loff_t off, size_t count)
1062 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1066 * pci_remove_resource_files - cleanup resource files
1067 * @pdev: dev to cleanup
1069 * If we created resource files for @pdev, remove them from sysfs and
1070 * free their resources.
1073 pci_remove_resource_files(struct pci_dev *pdev)
1077 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1078 struct bin_attribute *res_attr;
1080 res_attr = pdev->res_attr[i];
1082 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1086 res_attr = pdev->res_attr_wc[i];
1088 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1094 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1096 /* allocate attribute structure, piggyback attribute name */
1097 int name_len = write_combine ? 13 : 10;
1098 struct bin_attribute *res_attr;
1101 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1103 char *res_attr_name = (char *)(res_attr + 1);
1105 sysfs_bin_attr_init(res_attr);
1106 if (write_combine) {
1107 pdev->res_attr_wc[num] = res_attr;
1108 sprintf(res_attr_name, "resource%d_wc", num);
1109 res_attr->mmap = pci_mmap_resource_wc;
1111 pdev->res_attr[num] = res_attr;
1112 sprintf(res_attr_name, "resource%d", num);
1113 res_attr->mmap = pci_mmap_resource_uc;
1115 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1116 res_attr->read = pci_read_resource_io;
1117 res_attr->write = pci_write_resource_io;
1119 res_attr->attr.name = res_attr_name;
1120 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1121 res_attr->size = pci_resource_len(pdev, num);
1122 res_attr->private = &pdev->resource[num];
1123 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1131 * pci_create_resource_files - create resource files in sysfs for @dev
1132 * @pdev: dev in question
1134 * Walk the resources in @pdev creating files for each resource available.
1136 static int pci_create_resource_files(struct pci_dev *pdev)
1141 /* Expose the PCI resources from this device as files */
1142 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1144 /* skip empty resources */
1145 if (!pci_resource_len(pdev, i))
1148 retval = pci_create_attr(pdev, i, 0);
1149 /* for prefetchable resources, create a WC mappable file */
1150 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1151 retval = pci_create_attr(pdev, i, 1);
1154 pci_remove_resource_files(pdev);
1160 #else /* !HAVE_PCI_MMAP */
1161 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1162 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1163 #endif /* HAVE_PCI_MMAP */
1166 * pci_write_rom - used to enable access to the PCI ROM display
1168 * @kobj: kernel object handle
1169 * @bin_attr: struct bin_attribute for this file
1172 * @count: number of byte in input
1174 * writing anything except 0 enables it
1177 pci_write_rom(struct file *filp, struct kobject *kobj,
1178 struct bin_attribute *bin_attr,
1179 char *buf, loff_t off, size_t count)
1181 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1183 if ((off == 0) && (*buf == '0') && (count == 2))
1184 pdev->rom_attr_enabled = 0;
1186 pdev->rom_attr_enabled = 1;
1192 * pci_read_rom - read a PCI ROM
1194 * @kobj: kernel object handle
1195 * @bin_attr: struct bin_attribute for this file
1196 * @buf: where to put the data we read from the ROM
1198 * @count: number of bytes to read
1200 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1201 * device corresponding to @kobj.
1204 pci_read_rom(struct file *filp, struct kobject *kobj,
1205 struct bin_attribute *bin_attr,
1206 char *buf, loff_t off, size_t count)
1208 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1212 if (!pdev->rom_attr_enabled)
1215 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1222 if (off + count > size)
1225 memcpy_fromio(buf, rom + off, count);
1227 pci_unmap_rom(pdev, rom);
1232 static struct bin_attribute pci_config_attr = {
1235 .mode = S_IRUGO | S_IWUSR,
1237 .size = PCI_CFG_SPACE_SIZE,
1238 .read = pci_read_config,
1239 .write = pci_write_config,
1242 static struct bin_attribute pcie_config_attr = {
1245 .mode = S_IRUGO | S_IWUSR,
1247 .size = PCI_CFG_SPACE_EXP_SIZE,
1248 .read = pci_read_config,
1249 .write = pci_write_config,
1252 int __weak pcibios_add_platform_entries(struct pci_dev *dev)
1257 static ssize_t reset_store(struct device *dev,
1258 struct device_attribute *attr, const char *buf,
1261 struct pci_dev *pdev = to_pci_dev(dev);
1263 ssize_t result = strict_strtoul(buf, 0, &val);
1271 result = pci_reset_function(pdev);
1278 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1280 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1283 struct bin_attribute *attr;
1285 /* If the device has VPD, try to expose it in sysfs. */
1287 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1291 sysfs_bin_attr_init(attr);
1292 attr->size = dev->vpd->len;
1293 attr->attr.name = "vpd";
1294 attr->attr.mode = S_IRUSR | S_IWUSR;
1295 attr->read = read_vpd_attr;
1296 attr->write = write_vpd_attr;
1297 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1302 dev->vpd->attr = attr;
1305 /* Active State Power Management */
1306 pcie_aspm_create_sysfs_dev_files(dev);
1308 if (!pci_probe_reset_function(dev)) {
1309 retval = device_create_file(&dev->dev, &reset_attr);
1317 pcie_aspm_remove_sysfs_dev_files(dev);
1318 if (dev->vpd && dev->vpd->attr) {
1319 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1320 kfree(dev->vpd->attr);
1326 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1330 struct bin_attribute *attr;
1332 if (!sysfs_initialized)
1335 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1336 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1338 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1342 retval = pci_create_resource_files(pdev);
1344 goto err_config_file;
1346 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1347 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1348 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1351 /* If the device has a ROM, try to expose it in sysfs. */
1353 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1356 goto err_resource_files;
1358 sysfs_bin_attr_init(attr);
1359 attr->size = rom_size;
1360 attr->attr.name = "rom";
1361 attr->attr.mode = S_IRUSR | S_IWUSR;
1362 attr->read = pci_read_rom;
1363 attr->write = pci_write_rom;
1364 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1367 goto err_resource_files;
1369 pdev->rom_attr = attr;
1372 /* add platform-specific attributes */
1373 retval = pcibios_add_platform_entries(pdev);
1377 /* add sysfs entries for various capabilities */
1378 retval = pci_create_capabilities_sysfs(pdev);
1382 pci_create_firmware_label_files(pdev);
1388 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1389 kfree(pdev->rom_attr);
1390 pdev->rom_attr = NULL;
1393 pci_remove_resource_files(pdev);
1395 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1396 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1398 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1403 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1405 if (dev->vpd && dev->vpd->attr) {
1406 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1407 kfree(dev->vpd->attr);
1410 pcie_aspm_remove_sysfs_dev_files(dev);
1411 if (dev->reset_fn) {
1412 device_remove_file(&dev->dev, &reset_attr);
1418 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1419 * @pdev: device whose entries we should free
1421 * Cleanup when @pdev is removed from sysfs.
1423 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1427 if (!sysfs_initialized)
1430 pci_remove_capabilities_sysfs(pdev);
1432 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1433 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1435 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1437 pci_remove_resource_files(pdev);
1439 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1440 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1441 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1444 if (rom_size && pdev->rom_attr) {
1445 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1446 kfree(pdev->rom_attr);
1449 pci_remove_firmware_label_files(pdev);
1453 static int __init pci_sysfs_init(void)
1455 struct pci_dev *pdev = NULL;
1458 sysfs_initialized = 1;
1459 for_each_pci_dev(pdev) {
1460 retval = pci_create_sysfs_dev_files(pdev);
1470 late_initcall(pci_sysfs_init);
1472 static struct attribute *pci_dev_dev_attrs[] = {
1477 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1478 struct attribute *a, int n)
1480 struct device *dev = container_of(kobj, struct device, kobj);
1481 struct pci_dev *pdev = to_pci_dev(dev);
1483 if (a == &vga_attr.attr)
1484 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1490 #ifdef CONFIG_PCI_IOV
1491 static struct attribute *sriov_dev_attrs[] = {
1492 &sriov_totalvfs_attr.attr,
1493 &sriov_numvfs_attr.attr,
1497 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1498 struct attribute *a, int n)
1500 struct device *dev = container_of(kobj, struct device, kobj);
1502 if (!dev_is_pf(dev))
1508 static struct attribute_group sriov_dev_attr_group = {
1509 .attrs = sriov_dev_attrs,
1510 .is_visible = sriov_attrs_are_visible,
1512 #endif /* CONFIG_PCI_IOV */
1514 static struct attribute_group pci_dev_attr_group = {
1515 .attrs = pci_dev_dev_attrs,
1516 .is_visible = pci_dev_attrs_are_visible,
1519 static const struct attribute_group *pci_dev_attr_groups[] = {
1520 &pci_dev_attr_group,
1521 #ifdef CONFIG_PCI_IOV
1522 &sriov_dev_attr_group,
1527 struct device_type pci_dev_type = {
1528 .groups = pci_dev_attr_groups,