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>
26 #include <linux/capability.h>
29 #include <linux/slab.h>
34 static int sysfs_initialized;
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); \
84 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
89 len = cpumask_scnprintf(buf,
PAGE_SIZE-2, mask);
103 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
108 len = cpulist_scnprintf(buf,
PAGE_SIZE-2, mask);
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,
138 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
141 static inline ssize_t pci_bus_show_cpulistaffinity(
struct device *dev,
145 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
163 for (i = 0; i <
max; i++) {
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);
178 return sprintf(buf,
"pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
201 if (pci_is_enabled(pdev))
208 return result < 0 ? result :
count;
214 struct pci_dev *pdev;
224 return sprintf (buf,
"%d\n", dev->numa_node);
257 const char *buf,
size_t count)
280 dev_warn(&pdev->
dev,
"forced subordinate bus to%s support MSI,"
281 " bad things could happen\n", val ?
"" :
" not");
287 #ifdef CONFIG_HOTPLUG
314 const char *buf,
size_t count)
324 pci_rescan_bus(pdev->
bus);
330 static void remove_callback(
struct device *dev)
341 const char *buf,
size_t count)
361 const char *buf,
size_t count)
371 if (!pci_is_root_bus(bus) && list_empty(&bus->
devices))
372 pci_rescan_bus_bridge_resize(bus->
self);
382 #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
385 const char *buf,
size_t count)
394 pm_runtime_resume(dev);
423 __ATTR(
enable, 0600, is_enabled_show, is_enabled_store),
425 broken_parity_status_show,broken_parity_status_store),
426 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
427 #ifdef CONFIG_HOTPLUG
431 #if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
432 __ATTR(d3cold_allowed, 0644, d3cold_allowed_show, d3cold_allowed_store),
438 #ifdef CONFIG_HOTPLUG
453 return sprintf(buf,
"%u\n", (pdev == vga_dev));
462 pci_read_config(
struct file *filp,
struct kobject *kobj,
464 char *buf, loff_t off,
size_t count)
467 unsigned int size = 64;
468 loff_t init_off = off;
480 if (off + count > size) {
489 if ((off & 1) && size) {
491 pci_user_read_config_byte(dev, off, &val);
492 data[off - init_off] =
val;
497 if ((off & 3) && size > 2) {
499 pci_user_read_config_word(dev, off, &val);
500 data[off - init_off] = val & 0xff;
501 data[off - init_off + 1] = (val >> 8) & 0xff;
508 pci_user_read_config_dword(dev, off, &val);
509 data[off - init_off] = val & 0xff;
510 data[off - init_off + 1] = (val >> 8) & 0xff;
511 data[off - init_off + 2] = (val >> 16) & 0xff;
512 data[off - init_off + 3] = (val >> 24) & 0xff;
519 pci_user_read_config_word(dev, off, &val);
520 data[off - init_off] = val & 0xff;
521 data[off - init_off + 1] = (val >> 8) & 0xff;
528 pci_user_read_config_byte(dev, off, &val);
529 data[off - init_off] =
val;
540 pci_write_config(
struct file* filp,
struct kobject *kobj,
542 char *buf, loff_t off,
size_t count)
545 unsigned int size =
count;
546 loff_t init_off = off;
547 u8 *data = (
u8*) buf;
558 if ((off & 1) && size) {
559 pci_user_write_config_byte(dev, off, data[off - init_off]);
564 if ((off & 3) && size > 2) {
565 u16 val = data[off - init_off];
566 val |= (
u16) data[off - init_off + 1] << 8;
567 pci_user_write_config_word(dev, off, val);
573 u32 val = data[off - init_off];
574 val |= (
u32) data[off - init_off + 1] << 8;
575 val |= (
u32) data[off - init_off + 2] << 16;
576 val |= (
u32) data[off - init_off + 3] << 24;
577 pci_user_write_config_dword(dev, off, val);
583 u16 val = data[off - init_off];
584 val |= (
u16) data[off - init_off + 1] << 8;
585 pci_user_write_config_word(dev, off, val);
591 pci_user_write_config_byte(dev, off, data[off - init_off]);
602 read_vpd_attr(
struct file *filp,
struct kobject *kobj,
604 char *buf, loff_t off,
size_t count)
606 struct pci_dev *dev =
609 if (off > bin_attr->
size)
611 else if (count > bin_attr->
size - off)
612 count = bin_attr->
size - off;
618 write_vpd_attr(
struct file *filp,
struct kobject *kobj,
620 char *buf, loff_t off,
size_t count)
622 struct pci_dev *dev =
625 if (off > bin_attr->
size)
627 else if (count > bin_attr->
size - off)
628 count = bin_attr->
size - off;
633 #ifdef HAVE_PCI_LEGACY
647 pci_read_legacy_io(
struct file *filp,
struct kobject *kobj,
649 char *buf, loff_t off,
size_t count)
656 if (count != 1 && count != 2 && count != 4)
675 pci_write_legacy_io(
struct file *filp,
struct kobject *kobj,
677 char *buf, loff_t off,
size_t count)
683 if (count != 1 && count != 2 && count != 4)
701 pci_mmap_legacy_mem(
struct file *filp,
struct kobject *kobj,
724 pci_mmap_legacy_io(
struct file *filp,
struct kobject *kobj,
759 void pci_create_legacy_files(
struct pci_bus *b)
773 b->
legacy_io->write = pci_write_legacy_io;
801 "and ISA memory resources to sysfs\n");
805 void pci_remove_legacy_files(
struct pci_bus *b)
817 int pci_mmap_fits(
struct pci_dev *pdev,
int resno,
struct vm_area_struct *vma,
818 enum pci_mmap_api mmap_api)
827 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
829 if (start >= pci_start && start < pci_start + size &&
830 start + nr <= pci_start + size)
858 if (i >= PCI_ROM_RESOURCE)
861 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
862 WARN(1,
"process \"%s\" tried to map 0x%08lx bytes "
863 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
886 pci_mmap_resource_uc(
struct file *filp,
struct kobject *kobj,
890 return pci_mmap_resource(kobj, attr, vma, 0);
894 pci_mmap_resource_wc(
struct file *filp,
struct kobject *kobj,
898 return pci_mmap_resource(kobj, attr, vma, 1);
902 pci_resource_io(
struct file *filp,
struct kobject *kobj,
904 loff_t off,
size_t count,
bool write)
909 unsigned long port = off;
915 if (i >= PCI_ROM_RESOURCE)
931 *(
u8 *)buf =
inb(port);
950 pci_read_resource_io(
struct file *filp,
struct kobject *kobj,
952 loff_t off,
size_t count)
954 return pci_resource_io(filp, kobj, attr, buf, off, count,
false);
958 pci_write_resource_io(
struct file *filp,
struct kobject *kobj,
960 loff_t off,
size_t count)
962 return pci_resource_io(filp, kobj, attr, buf, off, count,
true);
994 static int pci_create_attr(
struct pci_dev *pdev,
int num,
int write_combine)
997 int name_len = write_combine ? 13 : 10;
1001 res_attr = kzalloc(
sizeof(*res_attr) + name_len,
GFP_ATOMIC);
1003 char *res_attr_name = (
char *)(res_attr + 1);
1006 if (write_combine) {
1008 sprintf(res_attr_name,
"resource%d_wc", num);
1009 res_attr->
mmap = pci_mmap_resource_wc;
1012 sprintf(res_attr_name,
"resource%d", num);
1013 res_attr->
mmap = pci_mmap_resource_uc;
1016 res_attr->
read = pci_read_resource_io;
1017 res_attr->
write = pci_write_resource_io;
1019 res_attr->
attr.name = res_attr_name;
1048 retval = pci_create_attr(pdev, i, 0);
1051 retval = pci_create_attr(pdev, i, 1);
1077 pci_write_rom(
struct file *filp,
struct kobject *kobj,
1079 char *buf, loff_t off,
size_t count)
1083 if ((off == 0) && (*buf ==
'0') && (count == 2))
1104 pci_read_rom(
struct file *filp,
struct kobject *kobj,
1106 char *buf, loff_t off,
size_t count)
1122 if (off + count > size)
1138 .read = pci_read_config,
1139 .write = pci_write_config,
1148 .read = pci_read_config,
1149 .write = pci_write_config,
1180 static int pci_create_capabilities_sysfs(
struct pci_dev *dev)
1193 attr->
attr.name =
"vpd";
1195 attr->
read = read_vpd_attr;
1196 attr->
write = write_vpd_attr;
1206 pcie_aspm_create_sysfs_dev_files(dev);
1217 pcie_aspm_remove_sysfs_dev_files(dev);
1218 if (dev->
vpd && dev->
vpd->attr) {
1232 if (!sysfs_initialized)
1244 goto err_config_file;
1256 goto err_resource_files;
1259 attr->
size = rom_size;
1260 attr->
attr.name =
"rom";
1262 attr->
read = pci_read_rom;
1263 attr->
write = pci_write_rom;
1267 goto err_resource_files;
1284 retval = pci_create_capabilities_sysfs(pdev);
1312 static void pci_remove_capabilities_sysfs(
struct pci_dev *dev)
1314 if (dev->
vpd && dev->
vpd->attr) {
1319 pcie_aspm_remove_sysfs_dev_files(dev);
1336 if (!sysfs_initialized)
1339 pci_remove_capabilities_sysfs(pdev);
1362 static int __init pci_sysfs_init(
void)
1364 struct pci_dev *pdev =
NULL;
1367 sysfs_initialized = 1;