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>
33 #include <linux/module.h>
36 static int sysfs_initialized
; /* = 0 */
38 /* show configuration fields */
39 #define pci_config_attr(field, format_string) \
41 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
43 struct pci_dev *pdev; \
45 pdev = to_pci_dev(dev); \
46 return sprintf(buf, format_string, pdev->field); \
48 static DEVICE_ATTR_RO(field)
50 pci_config_attr(vendor
, "0x%04x\n");
51 pci_config_attr(device
, "0x%04x\n");
52 pci_config_attr(subsystem_vendor
, "0x%04x\n");
53 pci_config_attr(subsystem_device
, "0x%04x\n");
54 pci_config_attr(revision
, "0x%02x\n");
55 pci_config_attr(class, "0x%06x\n");
56 pci_config_attr(irq
, "%u\n");
58 static ssize_t
broken_parity_status_show(struct device
*dev
,
59 struct device_attribute
*attr
,
62 struct pci_dev
*pdev
= to_pci_dev(dev
);
63 return sprintf(buf
, "%u\n", pdev
->broken_parity_status
);
66 static ssize_t
broken_parity_status_store(struct device
*dev
,
67 struct device_attribute
*attr
,
68 const char *buf
, size_t count
)
70 struct pci_dev
*pdev
= to_pci_dev(dev
);
73 if (kstrtoul(buf
, 0, &val
) < 0)
76 pdev
->broken_parity_status
= !!val
;
80 static DEVICE_ATTR_RW(broken_parity_status
);
82 static ssize_t
pci_dev_show_local_cpu(struct device
*dev
, bool list
,
83 struct device_attribute
*attr
, char *buf
)
85 const struct cpumask
*mask
;
88 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
89 cpumask_of_node(dev_to_node(dev
));
91 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
93 return cpumap_print_to_pagebuf(list
, buf
, mask
);
96 static ssize_t
local_cpus_show(struct device
*dev
,
97 struct device_attribute
*attr
, char *buf
)
99 return pci_dev_show_local_cpu(dev
, false, attr
, buf
);
101 static DEVICE_ATTR_RO(local_cpus
);
103 static ssize_t
local_cpulist_show(struct device
*dev
,
104 struct device_attribute
*attr
, char *buf
)
106 return pci_dev_show_local_cpu(dev
, true, attr
, buf
);
108 static DEVICE_ATTR_RO(local_cpulist
);
111 * PCI Bus Class Devices
113 static ssize_t
cpuaffinity_show(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 const struct cpumask
*cpumask
= cpumask_of_pcibus(to_pci_bus(dev
));
118 return cpumap_print_to_pagebuf(false, buf
, cpumask
);
120 static DEVICE_ATTR_RO(cpuaffinity
);
122 static ssize_t
cpulistaffinity_show(struct device
*dev
,
123 struct device_attribute
*attr
, char *buf
)
125 const struct cpumask
*cpumask
= cpumask_of_pcibus(to_pci_bus(dev
));
127 return cpumap_print_to_pagebuf(true, buf
, cpumask
);
129 static DEVICE_ATTR_RO(cpulistaffinity
);
132 static ssize_t
resource_show(struct device
*dev
, struct device_attribute
*attr
,
135 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
139 resource_size_t start
, end
;
141 if (pci_dev
->subordinate
)
142 max
= DEVICE_COUNT_RESOURCE
;
144 max
= PCI_BRIDGE_RESOURCES
;
146 for (i
= 0; i
< max
; i
++) {
147 struct resource
*res
= &pci_dev
->resource
[i
];
148 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
149 str
+= sprintf(str
, "0x%016llx 0x%016llx 0x%016llx\n",
150 (unsigned long long)start
,
151 (unsigned long long)end
,
152 (unsigned long long)res
->flags
);
156 static DEVICE_ATTR_RO(resource
);
158 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
161 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
163 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
164 pci_dev
->vendor
, pci_dev
->device
,
165 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
166 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
167 (u8
)(pci_dev
->class));
169 static DEVICE_ATTR_RO(modalias
);
171 static ssize_t
enable_store(struct device
*dev
, struct device_attribute
*attr
,
172 const char *buf
, size_t count
)
174 struct pci_dev
*pdev
= to_pci_dev(dev
);
176 ssize_t result
= kstrtoul(buf
, 0, &val
);
181 /* this can crash the machine when done on the "wrong" device */
182 if (!capable(CAP_SYS_ADMIN
))
186 if (pci_is_enabled(pdev
))
187 pci_disable_device(pdev
);
191 result
= pci_enable_device(pdev
);
193 return result
< 0 ? result
: count
;
196 static ssize_t
enable_show(struct device
*dev
, struct device_attribute
*attr
,
199 struct pci_dev
*pdev
;
201 pdev
= to_pci_dev(dev
);
202 return sprintf(buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
204 static DEVICE_ATTR_RW(enable
);
207 static ssize_t
numa_node_store(struct device
*dev
,
208 struct device_attribute
*attr
, const char *buf
,
211 struct pci_dev
*pdev
= to_pci_dev(dev
);
214 if (!capable(CAP_SYS_ADMIN
))
217 ret
= kstrtoint(buf
, 0, &node
);
221 if ((node
< 0 && node
!= NUMA_NO_NODE
) || node
>= MAX_NUMNODES
)
224 if (node
!= NUMA_NO_NODE
&& !node_online(node
))
227 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_STILL_OK
);
228 dev_alert(&pdev
->dev
, FW_BUG
"Overriding NUMA node to %d. Contact your vendor for updates.",
231 dev
->numa_node
= node
;
235 static ssize_t
numa_node_show(struct device
*dev
, struct device_attribute
*attr
,
238 return sprintf(buf
, "%d\n", dev
->numa_node
);
240 static DEVICE_ATTR_RW(numa_node
);
243 static ssize_t
dma_mask_bits_show(struct device
*dev
,
244 struct device_attribute
*attr
, char *buf
)
246 struct pci_dev
*pdev
= to_pci_dev(dev
);
248 return sprintf(buf
, "%d\n", fls64(pdev
->dma_mask
));
250 static DEVICE_ATTR_RO(dma_mask_bits
);
252 static ssize_t
consistent_dma_mask_bits_show(struct device
*dev
,
253 struct device_attribute
*attr
,
256 return sprintf(buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
258 static DEVICE_ATTR_RO(consistent_dma_mask_bits
);
260 static ssize_t
msi_bus_show(struct device
*dev
, struct device_attribute
*attr
,
263 struct pci_dev
*pdev
= to_pci_dev(dev
);
264 struct pci_bus
*subordinate
= pdev
->subordinate
;
266 return sprintf(buf
, "%u\n", subordinate
?
267 !(subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
271 static ssize_t
msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
272 const char *buf
, size_t count
)
274 struct pci_dev
*pdev
= to_pci_dev(dev
);
275 struct pci_bus
*subordinate
= pdev
->subordinate
;
278 if (kstrtoul(buf
, 0, &val
) < 0)
281 if (!capable(CAP_SYS_ADMIN
))
285 * "no_msi" and "bus_flags" only affect what happens when a driver
286 * requests MSI or MSI-X. They don't affect any drivers that have
287 * already requested MSI or MSI-X.
291 dev_info(&pdev
->dev
, "MSI/MSI-X %s for future drivers\n",
292 val
? "allowed" : "disallowed");
297 subordinate
->bus_flags
&= ~PCI_BUS_FLAGS_NO_MSI
;
299 subordinate
->bus_flags
|= PCI_BUS_FLAGS_NO_MSI
;
301 dev_info(&subordinate
->dev
, "MSI/MSI-X %s for future drivers of devices on this bus\n",
302 val
? "allowed" : "disallowed");
305 static DEVICE_ATTR_RW(msi_bus
);
307 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
311 struct pci_bus
*b
= NULL
;
313 if (kstrtoul(buf
, 0, &val
) < 0)
317 pci_lock_rescan_remove();
318 while ((b
= pci_find_next_bus(b
)) != NULL
)
320 pci_unlock_rescan_remove();
324 static BUS_ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
);
326 static struct attribute
*pci_bus_attrs
[] = {
327 &bus_attr_rescan
.attr
,
331 static const struct attribute_group pci_bus_group
= {
332 .attrs
= pci_bus_attrs
,
335 const struct attribute_group
*pci_bus_groups
[] = {
340 static ssize_t
dev_rescan_store(struct device
*dev
,
341 struct device_attribute
*attr
, const char *buf
,
345 struct pci_dev
*pdev
= to_pci_dev(dev
);
347 if (kstrtoul(buf
, 0, &val
) < 0)
351 pci_lock_rescan_remove();
352 pci_rescan_bus(pdev
->bus
);
353 pci_unlock_rescan_remove();
357 static struct device_attribute dev_rescan_attr
= __ATTR(rescan
,
359 NULL
, dev_rescan_store
);
361 static ssize_t
remove_store(struct device
*dev
, struct device_attribute
*attr
,
362 const char *buf
, size_t count
)
366 if (kstrtoul(buf
, 0, &val
) < 0)
369 if (val
&& device_remove_file_self(dev
, attr
))
370 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev
));
373 static struct device_attribute dev_remove_attr
= __ATTR(remove
,
377 static ssize_t
dev_bus_rescan_store(struct device
*dev
,
378 struct device_attribute
*attr
,
379 const char *buf
, size_t count
)
382 struct pci_bus
*bus
= to_pci_bus(dev
);
384 if (kstrtoul(buf
, 0, &val
) < 0)
388 pci_lock_rescan_remove();
389 if (!pci_is_root_bus(bus
) && list_empty(&bus
->devices
))
390 pci_rescan_bus_bridge_resize(bus
->self
);
393 pci_unlock_rescan_remove();
397 static DEVICE_ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_bus_rescan_store
);
399 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
400 static ssize_t
d3cold_allowed_store(struct device
*dev
,
401 struct device_attribute
*attr
,
402 const char *buf
, size_t count
)
404 struct pci_dev
*pdev
= to_pci_dev(dev
);
407 if (kstrtoul(buf
, 0, &val
) < 0)
410 pdev
->d3cold_allowed
= !!val
;
411 if (pdev
->d3cold_allowed
)
412 pci_d3cold_enable(pdev
);
414 pci_d3cold_disable(pdev
);
416 pm_runtime_resume(dev
);
421 static ssize_t
d3cold_allowed_show(struct device
*dev
,
422 struct device_attribute
*attr
, char *buf
)
424 struct pci_dev
*pdev
= to_pci_dev(dev
);
425 return sprintf(buf
, "%u\n", pdev
->d3cold_allowed
);
427 static DEVICE_ATTR_RW(d3cold_allowed
);
431 static ssize_t
devspec_show(struct device
*dev
,
432 struct device_attribute
*attr
, char *buf
)
434 struct pci_dev
*pdev
= to_pci_dev(dev
);
435 struct device_node
*np
= pci_device_to_OF_node(pdev
);
437 if (np
== NULL
|| np
->full_name
== NULL
)
439 return sprintf(buf
, "%s", np
->full_name
);
441 static DEVICE_ATTR_RO(devspec
);
444 #ifdef CONFIG_PCI_IOV
445 static ssize_t
sriov_totalvfs_show(struct device
*dev
,
446 struct device_attribute
*attr
,
449 struct pci_dev
*pdev
= to_pci_dev(dev
);
451 return sprintf(buf
, "%u\n", pci_sriov_get_totalvfs(pdev
));
455 static ssize_t
sriov_numvfs_show(struct device
*dev
,
456 struct device_attribute
*attr
,
459 struct pci_dev
*pdev
= to_pci_dev(dev
);
461 return sprintf(buf
, "%u\n", pdev
->sriov
->num_VFs
);
465 * num_vfs > 0; number of VFs to enable
466 * num_vfs = 0; disable all VFs
468 * Note: SRIOV spec doesn't allow partial VF
469 * disable, so it's all or none.
471 static ssize_t
sriov_numvfs_store(struct device
*dev
,
472 struct device_attribute
*attr
,
473 const char *buf
, size_t count
)
475 struct pci_dev
*pdev
= to_pci_dev(dev
);
476 struct pci_sriov
*iov
= pdev
->sriov
;
480 ret
= kstrtou16(buf
, 0, &num_vfs
);
484 if (num_vfs
> pci_sriov_get_totalvfs(pdev
))
487 mutex_lock(&iov
->dev
->sriov
->lock
);
489 if (num_vfs
== pdev
->sriov
->num_VFs
)
492 /* is PF driver loaded w/callback */
493 if (!pdev
->driver
|| !pdev
->driver
->sriov_configure
) {
494 dev_info(&pdev
->dev
, "Driver doesn't support SRIOV configuration via sysfs\n");
501 ret
= pdev
->driver
->sriov_configure(pdev
, 0);
506 if (pdev
->sriov
->num_VFs
) {
507 dev_warn(&pdev
->dev
, "%d VFs already enabled. Disable before enabling %d VFs\n",
508 pdev
->sriov
->num_VFs
, num_vfs
);
513 ret
= pdev
->driver
->sriov_configure(pdev
, num_vfs
);
518 dev_warn(&pdev
->dev
, "%d VFs requested; only %d enabled\n",
522 mutex_unlock(&iov
->dev
->sriov
->lock
);
530 static struct device_attribute sriov_totalvfs_attr
= __ATTR_RO(sriov_totalvfs
);
531 static struct device_attribute sriov_numvfs_attr
=
532 __ATTR(sriov_numvfs
, (S_IRUGO
|S_IWUSR
|S_IWGRP
),
533 sriov_numvfs_show
, sriov_numvfs_store
);
534 #endif /* CONFIG_PCI_IOV */
536 static ssize_t
driver_override_store(struct device
*dev
,
537 struct device_attribute
*attr
,
538 const char *buf
, size_t count
)
540 struct pci_dev
*pdev
= to_pci_dev(dev
);
541 char *driver_override
, *old
= pdev
->driver_override
, *cp
;
543 /* We need to keep extra room for a newline */
544 if (count
>= (PAGE_SIZE
- 1))
547 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
548 if (!driver_override
)
551 cp
= strchr(driver_override
, '\n');
555 if (strlen(driver_override
)) {
556 pdev
->driver_override
= driver_override
;
558 kfree(driver_override
);
559 pdev
->driver_override
= NULL
;
567 static ssize_t
driver_override_show(struct device
*dev
,
568 struct device_attribute
*attr
, char *buf
)
570 struct pci_dev
*pdev
= to_pci_dev(dev
);
572 return snprintf(buf
, PAGE_SIZE
, "%s\n", pdev
->driver_override
);
574 static DEVICE_ATTR_RW(driver_override
);
576 static struct attribute
*pci_dev_attrs
[] = {
577 &dev_attr_resource
.attr
,
578 &dev_attr_vendor
.attr
,
579 &dev_attr_device
.attr
,
580 &dev_attr_subsystem_vendor
.attr
,
581 &dev_attr_subsystem_device
.attr
,
582 &dev_attr_revision
.attr
,
583 &dev_attr_class
.attr
,
585 &dev_attr_local_cpus
.attr
,
586 &dev_attr_local_cpulist
.attr
,
587 &dev_attr_modalias
.attr
,
589 &dev_attr_numa_node
.attr
,
591 &dev_attr_dma_mask_bits
.attr
,
592 &dev_attr_consistent_dma_mask_bits
.attr
,
593 &dev_attr_enable
.attr
,
594 &dev_attr_broken_parity_status
.attr
,
595 &dev_attr_msi_bus
.attr
,
596 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
597 &dev_attr_d3cold_allowed
.attr
,
600 &dev_attr_devspec
.attr
,
602 &dev_attr_driver_override
.attr
,
606 static const struct attribute_group pci_dev_group
= {
607 .attrs
= pci_dev_attrs
,
610 const struct attribute_group
*pci_dev_groups
[] = {
615 static struct attribute
*pcibus_attrs
[] = {
616 &dev_attr_rescan
.attr
,
617 &dev_attr_cpuaffinity
.attr
,
618 &dev_attr_cpulistaffinity
.attr
,
622 static const struct attribute_group pcibus_group
= {
623 .attrs
= pcibus_attrs
,
626 const struct attribute_group
*pcibus_groups
[] = {
631 static ssize_t
boot_vga_show(struct device
*dev
, struct device_attribute
*attr
,
634 struct pci_dev
*pdev
= to_pci_dev(dev
);
635 struct pci_dev
*vga_dev
= vga_default_device();
638 return sprintf(buf
, "%u\n", (pdev
== vga_dev
));
640 return sprintf(buf
, "%u\n",
641 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
642 IORESOURCE_ROM_SHADOW
));
644 static struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
646 static ssize_t
pci_read_config(struct file
*filp
, struct kobject
*kobj
,
647 struct bin_attribute
*bin_attr
, char *buf
,
648 loff_t off
, size_t count
)
650 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
651 unsigned int size
= 64;
652 loff_t init_off
= off
;
653 u8
*data
= (u8
*) buf
;
655 /* Several chips lock up trying to read undefined config space */
656 if (file_ns_capable(filp
, &init_user_ns
, CAP_SYS_ADMIN
))
657 size
= dev
->cfg_size
;
658 else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
663 if (off
+ count
> size
) {
670 pci_config_pm_runtime_get(dev
);
672 if ((off
& 1) && size
) {
674 pci_user_read_config_byte(dev
, off
, &val
);
675 data
[off
- init_off
] = val
;
680 if ((off
& 3) && size
> 2) {
682 pci_user_read_config_word(dev
, off
, &val
);
683 data
[off
- init_off
] = val
& 0xff;
684 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
691 pci_user_read_config_dword(dev
, off
, &val
);
692 data
[off
- init_off
] = val
& 0xff;
693 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
694 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
695 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
702 pci_user_read_config_word(dev
, off
, &val
);
703 data
[off
- init_off
] = val
& 0xff;
704 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
711 pci_user_read_config_byte(dev
, off
, &val
);
712 data
[off
- init_off
] = val
;
717 pci_config_pm_runtime_put(dev
);
722 static ssize_t
pci_write_config(struct file
*filp
, struct kobject
*kobj
,
723 struct bin_attribute
*bin_attr
, char *buf
,
724 loff_t off
, size_t count
)
726 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
727 unsigned int size
= count
;
728 loff_t init_off
= off
;
729 u8
*data
= (u8
*) buf
;
731 if (secure_modules())
734 if (off
> dev
->cfg_size
)
736 if (off
+ count
> dev
->cfg_size
) {
737 size
= dev
->cfg_size
- off
;
741 pci_config_pm_runtime_get(dev
);
743 if ((off
& 1) && size
) {
744 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
749 if ((off
& 3) && size
> 2) {
750 u16 val
= data
[off
- init_off
];
751 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
752 pci_user_write_config_word(dev
, off
, val
);
758 u32 val
= data
[off
- init_off
];
759 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
760 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
761 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
762 pci_user_write_config_dword(dev
, off
, val
);
768 u16 val
= data
[off
- init_off
];
769 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
770 pci_user_write_config_word(dev
, off
, val
);
776 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
781 pci_config_pm_runtime_put(dev
);
786 static ssize_t
read_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
787 struct bin_attribute
*bin_attr
, char *buf
,
788 loff_t off
, size_t count
)
790 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
792 if (bin_attr
->size
> 0) {
793 if (off
> bin_attr
->size
)
795 else if (count
> bin_attr
->size
- off
)
796 count
= bin_attr
->size
- off
;
799 return pci_read_vpd(dev
, off
, count
, buf
);
802 static ssize_t
write_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
803 struct bin_attribute
*bin_attr
, char *buf
,
804 loff_t off
, size_t count
)
806 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
808 if (bin_attr
->size
> 0) {
809 if (off
> bin_attr
->size
)
811 else if (count
> bin_attr
->size
- off
)
812 count
= bin_attr
->size
- off
;
815 return pci_write_vpd(dev
, off
, count
, buf
);
818 #ifdef HAVE_PCI_LEGACY
820 * pci_read_legacy_io - read byte(s) from legacy I/O port space
821 * @filp: open sysfs file
822 * @kobj: kobject corresponding to file to read from
823 * @bin_attr: struct bin_attribute for this file
824 * @buf: buffer to store results
825 * @off: offset into legacy I/O port space
826 * @count: number of bytes to read
828 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
829 * callback routine (pci_legacy_read).
831 static ssize_t
pci_read_legacy_io(struct file
*filp
, struct kobject
*kobj
,
832 struct bin_attribute
*bin_attr
, char *buf
,
833 loff_t off
, size_t count
)
835 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
837 /* Only support 1, 2 or 4 byte accesses */
838 if (count
!= 1 && count
!= 2 && count
!= 4)
841 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
845 * pci_write_legacy_io - write byte(s) to legacy I/O port space
846 * @filp: open sysfs file
847 * @kobj: kobject corresponding to file to read from
848 * @bin_attr: struct bin_attribute for this file
849 * @buf: buffer containing value to be written
850 * @off: offset into legacy I/O port space
851 * @count: number of bytes to write
853 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
854 * callback routine (pci_legacy_write).
856 static ssize_t
pci_write_legacy_io(struct file
*filp
, struct kobject
*kobj
,
857 struct bin_attribute
*bin_attr
, char *buf
,
858 loff_t off
, size_t count
)
860 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
862 /* Only support 1, 2 or 4 byte accesses */
863 if (count
!= 1 && count
!= 2 && count
!= 4)
866 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
870 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
871 * @filp: open sysfs file
872 * @kobj: kobject corresponding to device to be mapped
873 * @attr: struct bin_attribute for this file
874 * @vma: struct vm_area_struct passed to mmap
876 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
877 * legacy memory space (first meg of bus space) into application virtual
880 static int pci_mmap_legacy_mem(struct file
*filp
, struct kobject
*kobj
,
881 struct bin_attribute
*attr
,
882 struct vm_area_struct
*vma
)
884 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
886 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
890 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
891 * @filp: open sysfs file
892 * @kobj: kobject corresponding to device to be mapped
893 * @attr: struct bin_attribute for this file
894 * @vma: struct vm_area_struct passed to mmap
896 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
897 * legacy IO space (first meg of bus space) into application virtual
898 * memory space. Returns -ENOSYS if the operation isn't supported
900 static int pci_mmap_legacy_io(struct file
*filp
, struct kobject
*kobj
,
901 struct bin_attribute
*attr
,
902 struct vm_area_struct
*vma
)
904 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
906 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
910 * pci_adjust_legacy_attr - adjustment of legacy file attributes
911 * @b: bus to create files under
912 * @mmap_type: I/O port or memory
914 * Stub implementation. Can be overridden by arch if necessary.
916 void __weak
pci_adjust_legacy_attr(struct pci_bus
*b
,
917 enum pci_mmap_state mmap_type
)
922 * pci_create_legacy_files - create legacy I/O port and memory files
923 * @b: bus to create files under
925 * Some platforms allow access to legacy I/O port and ISA memory space on
926 * a per-bus basis. This routine creates the files and ties them into
927 * their associated read, write and mmap files from pci-sysfs.c
929 * On error unwind, but don't propagate the error to the caller
930 * as it is ok to set up the PCI bus without these files.
932 void pci_create_legacy_files(struct pci_bus
*b
)
936 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
941 sysfs_bin_attr_init(b
->legacy_io
);
942 b
->legacy_io
->attr
.name
= "legacy_io";
943 b
->legacy_io
->size
= 0xffff;
944 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
945 b
->legacy_io
->read
= pci_read_legacy_io
;
946 b
->legacy_io
->write
= pci_write_legacy_io
;
947 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
948 pci_adjust_legacy_attr(b
, pci_mmap_io
);
949 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
953 /* Allocated above after the legacy_io struct */
954 b
->legacy_mem
= b
->legacy_io
+ 1;
955 sysfs_bin_attr_init(b
->legacy_mem
);
956 b
->legacy_mem
->attr
.name
= "legacy_mem";
957 b
->legacy_mem
->size
= 1024*1024;
958 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
959 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
960 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
961 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
968 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
973 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n");
977 void pci_remove_legacy_files(struct pci_bus
*b
)
980 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
981 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
982 kfree(b
->legacy_io
); /* both are allocated here */
985 #endif /* HAVE_PCI_LEGACY */
989 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
,
990 enum pci_mmap_api mmap_api
)
992 unsigned long nr
, start
, size
, pci_start
;
994 if (pci_resource_len(pdev
, resno
) == 0)
997 start
= vma
->vm_pgoff
;
998 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
999 pci_start
= (mmap_api
== PCI_MMAP_PROCFS
) ?
1000 pci_resource_start(pdev
, resno
) >> PAGE_SHIFT
: 0;
1001 if (start
>= pci_start
&& start
< pci_start
+ size
&&
1002 start
+ nr
<= pci_start
+ size
)
1008 * pci_mmap_resource - map a PCI resource into user memory space
1009 * @kobj: kobject for mapping
1010 * @attr: struct bin_attribute for the file being mapped
1011 * @vma: struct vm_area_struct passed into the mmap
1012 * @write_combine: 1 for write_combine mapping
1014 * Use the regular PCI mapping routines to map a PCI resource into userspace.
1016 static int pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
1017 struct vm_area_struct
*vma
, int write_combine
)
1019 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1020 struct resource
*res
= attr
->private;
1021 enum pci_mmap_state mmap_type
;
1022 resource_size_t start
, end
;
1025 if (secure_modules())
1028 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
1029 if (res
== &pdev
->resource
[i
])
1031 if (i
>= PCI_ROM_RESOURCE
)
1034 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(res
->start
))
1037 if (!pci_mmap_fits(pdev
, i
, vma
, PCI_MMAP_SYSFS
)) {
1038 WARN(1, "process \"%s\" tried to map 0x%08lx bytes at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
1039 current
->comm
, vma
->vm_end
-vma
->vm_start
, vma
->vm_pgoff
,
1041 (u64
)pci_resource_start(pdev
, i
),
1042 (u64
)pci_resource_len(pdev
, i
));
1046 /* pci_mmap_page_range() expects the same kind of entry as coming
1047 * from /proc/bus/pci/ which is a "user visible" value. If this is
1048 * different from the resource itself, arch will do necessary fixup.
1050 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
1051 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
1052 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
1053 return pci_mmap_page_range(pdev
, vma
, mmap_type
, write_combine
);
1056 static int pci_mmap_resource_uc(struct file
*filp
, struct kobject
*kobj
,
1057 struct bin_attribute
*attr
,
1058 struct vm_area_struct
*vma
)
1060 return pci_mmap_resource(kobj
, attr
, vma
, 0);
1063 static int pci_mmap_resource_wc(struct file
*filp
, struct kobject
*kobj
,
1064 struct bin_attribute
*attr
,
1065 struct vm_area_struct
*vma
)
1067 return pci_mmap_resource(kobj
, attr
, vma
, 1);
1070 static ssize_t
pci_resource_io(struct file
*filp
, struct kobject
*kobj
,
1071 struct bin_attribute
*attr
, char *buf
,
1072 loff_t off
, size_t count
, bool write
)
1074 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1075 struct resource
*res
= attr
->private;
1076 unsigned long port
= off
;
1079 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
1080 if (res
== &pdev
->resource
[i
])
1082 if (i
>= PCI_ROM_RESOURCE
)
1085 port
+= pci_resource_start(pdev
, i
);
1087 if (port
> pci_resource_end(pdev
, i
))
1090 if (port
+ count
- 1 > pci_resource_end(pdev
, i
))
1096 outb(*(u8
*)buf
, port
);
1098 *(u8
*)buf
= inb(port
);
1102 outw(*(u16
*)buf
, port
);
1104 *(u16
*)buf
= inw(port
);
1108 outl(*(u32
*)buf
, port
);
1110 *(u32
*)buf
= inl(port
);
1116 static ssize_t
pci_read_resource_io(struct file
*filp
, struct kobject
*kobj
,
1117 struct bin_attribute
*attr
, char *buf
,
1118 loff_t off
, size_t count
)
1120 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, false);
1123 static ssize_t
pci_write_resource_io(struct file
*filp
, struct kobject
*kobj
,
1124 struct bin_attribute
*attr
, char *buf
,
1125 loff_t off
, size_t count
)
1127 if (secure_modules())
1130 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, true);
1134 * pci_remove_resource_files - cleanup resource files
1135 * @pdev: dev to cleanup
1137 * If we created resource files for @pdev, remove them from sysfs and
1138 * free their resources.
1140 static void pci_remove_resource_files(struct pci_dev
*pdev
)
1144 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
1145 struct bin_attribute
*res_attr
;
1147 res_attr
= pdev
->res_attr
[i
];
1149 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
1153 res_attr
= pdev
->res_attr_wc
[i
];
1155 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
1161 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
1163 /* allocate attribute structure, piggyback attribute name */
1164 int name_len
= write_combine
? 13 : 10;
1165 struct bin_attribute
*res_attr
;
1166 char *res_attr_name
;
1169 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
1173 res_attr_name
= (char *)(res_attr
+ 1);
1175 sysfs_bin_attr_init(res_attr
);
1176 if (write_combine
) {
1177 pdev
->res_attr_wc
[num
] = res_attr
;
1178 sprintf(res_attr_name
, "resource%d_wc", num
);
1179 res_attr
->mmap
= pci_mmap_resource_wc
;
1181 pdev
->res_attr
[num
] = res_attr
;
1182 sprintf(res_attr_name
, "resource%d", num
);
1183 res_attr
->mmap
= pci_mmap_resource_uc
;
1185 if (pci_resource_flags(pdev
, num
) & IORESOURCE_IO
) {
1186 res_attr
->read
= pci_read_resource_io
;
1187 res_attr
->write
= pci_write_resource_io
;
1189 res_attr
->attr
.name
= res_attr_name
;
1190 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1191 res_attr
->size
= pci_resource_len(pdev
, num
);
1192 res_attr
->private = &pdev
->resource
[num
];
1193 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
1201 * pci_create_resource_files - create resource files in sysfs for @dev
1202 * @pdev: dev in question
1204 * Walk the resources in @pdev creating files for each resource available.
1206 static int pci_create_resource_files(struct pci_dev
*pdev
)
1211 /* Expose the PCI resources from this device as files */
1212 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
1214 /* skip empty resources */
1215 if (!pci_resource_len(pdev
, i
))
1218 retval
= pci_create_attr(pdev
, i
, 0);
1219 /* for prefetchable resources, create a WC mappable file */
1220 if (!retval
&& pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
1221 retval
= pci_create_attr(pdev
, i
, 1);
1224 pci_remove_resource_files(pdev
);
1230 #else /* !HAVE_PCI_MMAP */
1231 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
1232 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
1233 #endif /* HAVE_PCI_MMAP */
1236 * pci_write_rom - used to enable access to the PCI ROM display
1238 * @kobj: kernel object handle
1239 * @bin_attr: struct bin_attribute for this file
1242 * @count: number of byte in input
1244 * writing anything except 0 enables it
1246 static ssize_t
pci_write_rom(struct file
*filp
, struct kobject
*kobj
,
1247 struct bin_attribute
*bin_attr
, char *buf
,
1248 loff_t off
, size_t count
)
1250 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1252 if ((off
== 0) && (*buf
== '0') && (count
== 2))
1253 pdev
->rom_attr_enabled
= 0;
1255 pdev
->rom_attr_enabled
= 1;
1261 * pci_read_rom - read a PCI ROM
1263 * @kobj: kernel object handle
1264 * @bin_attr: struct bin_attribute for this file
1265 * @buf: where to put the data we read from the ROM
1267 * @count: number of bytes to read
1269 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1270 * device corresponding to @kobj.
1272 static ssize_t
pci_read_rom(struct file
*filp
, struct kobject
*kobj
,
1273 struct bin_attribute
*bin_attr
, char *buf
,
1274 loff_t off
, size_t count
)
1276 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1280 if (!pdev
->rom_attr_enabled
)
1283 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
1290 if (off
+ count
> size
)
1293 memcpy_fromio(buf
, rom
+ off
, count
);
1295 pci_unmap_rom(pdev
, rom
);
1300 static struct bin_attribute pci_config_attr
= {
1303 .mode
= S_IRUGO
| S_IWUSR
,
1305 .size
= PCI_CFG_SPACE_SIZE
,
1306 .read
= pci_read_config
,
1307 .write
= pci_write_config
,
1310 static struct bin_attribute pcie_config_attr
= {
1313 .mode
= S_IRUGO
| S_IWUSR
,
1315 .size
= PCI_CFG_SPACE_EXP_SIZE
,
1316 .read
= pci_read_config
,
1317 .write
= pci_write_config
,
1320 static ssize_t
reset_store(struct device
*dev
, struct device_attribute
*attr
,
1321 const char *buf
, size_t count
)
1323 struct pci_dev
*pdev
= to_pci_dev(dev
);
1325 ssize_t result
= kstrtoul(buf
, 0, &val
);
1333 result
= pci_reset_function(pdev
);
1340 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
1342 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
1345 struct bin_attribute
*attr
;
1347 /* If the device has VPD, try to expose it in sysfs. */
1349 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1353 sysfs_bin_attr_init(attr
);
1355 attr
->attr
.name
= "vpd";
1356 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1357 attr
->read
= read_vpd_attr
;
1358 attr
->write
= write_vpd_attr
;
1359 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
1364 dev
->vpd
->attr
= attr
;
1367 /* Active State Power Management */
1368 pcie_aspm_create_sysfs_dev_files(dev
);
1370 if (!pci_probe_reset_function(dev
)) {
1371 retval
= device_create_file(&dev
->dev
, &reset_attr
);
1379 pcie_aspm_remove_sysfs_dev_files(dev
);
1380 if (dev
->vpd
&& dev
->vpd
->attr
) {
1381 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1382 kfree(dev
->vpd
->attr
);
1388 int __must_check
pci_create_sysfs_dev_files(struct pci_dev
*pdev
)
1392 struct bin_attribute
*attr
;
1394 if (!sysfs_initialized
)
1397 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1398 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1400 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1404 retval
= pci_create_resource_files(pdev
);
1406 goto err_config_file
;
1408 /* If the device has a ROM, try to expose it in sysfs. */
1409 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1411 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1414 goto err_resource_files
;
1416 sysfs_bin_attr_init(attr
);
1417 attr
->size
= rom_size
;
1418 attr
->attr
.name
= "rom";
1419 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1420 attr
->read
= pci_read_rom
;
1421 attr
->write
= pci_write_rom
;
1422 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1425 goto err_resource_files
;
1427 pdev
->rom_attr
= attr
;
1430 /* add sysfs entries for various capabilities */
1431 retval
= pci_create_capabilities_sysfs(pdev
);
1435 pci_create_firmware_label_files(pdev
);
1440 if (pdev
->rom_attr
) {
1441 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1442 kfree(pdev
->rom_attr
);
1443 pdev
->rom_attr
= NULL
;
1446 pci_remove_resource_files(pdev
);
1448 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1449 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1451 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1456 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1458 if (dev
->vpd
&& dev
->vpd
->attr
) {
1459 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1460 kfree(dev
->vpd
->attr
);
1463 pcie_aspm_remove_sysfs_dev_files(dev
);
1464 if (dev
->reset_fn
) {
1465 device_remove_file(&dev
->dev
, &reset_attr
);
1471 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1472 * @pdev: device whose entries we should free
1474 * Cleanup when @pdev is removed from sysfs.
1476 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1478 if (!sysfs_initialized
)
1481 pci_remove_capabilities_sysfs(pdev
);
1483 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1484 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1486 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1488 pci_remove_resource_files(pdev
);
1490 if (pdev
->rom_attr
) {
1491 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1492 kfree(pdev
->rom_attr
);
1493 pdev
->rom_attr
= NULL
;
1496 pci_remove_firmware_label_files(pdev
);
1499 static int __init
pci_sysfs_init(void)
1501 struct pci_dev
*pdev
= NULL
;
1504 sysfs_initialized
= 1;
1505 for_each_pci_dev(pdev
) {
1506 retval
= pci_create_sysfs_dev_files(pdev
);
1515 late_initcall(pci_sysfs_init
);
1517 static struct attribute
*pci_dev_dev_attrs
[] = {
1522 static umode_t
pci_dev_attrs_are_visible(struct kobject
*kobj
,
1523 struct attribute
*a
, int n
)
1525 struct device
*dev
= kobj_to_dev(kobj
);
1526 struct pci_dev
*pdev
= to_pci_dev(dev
);
1528 if (a
== &vga_attr
.attr
)
1529 if ((pdev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
1535 static struct attribute
*pci_dev_hp_attrs
[] = {
1536 &dev_remove_attr
.attr
,
1537 &dev_rescan_attr
.attr
,
1541 static umode_t
pci_dev_hp_attrs_are_visible(struct kobject
*kobj
,
1542 struct attribute
*a
, int n
)
1544 struct device
*dev
= kobj_to_dev(kobj
);
1545 struct pci_dev
*pdev
= to_pci_dev(dev
);
1547 if (pdev
->is_virtfn
)
1553 static struct attribute_group pci_dev_hp_attr_group
= {
1554 .attrs
= pci_dev_hp_attrs
,
1555 .is_visible
= pci_dev_hp_attrs_are_visible
,
1558 #ifdef CONFIG_PCI_IOV
1559 static struct attribute
*sriov_dev_attrs
[] = {
1560 &sriov_totalvfs_attr
.attr
,
1561 &sriov_numvfs_attr
.attr
,
1565 static umode_t
sriov_attrs_are_visible(struct kobject
*kobj
,
1566 struct attribute
*a
, int n
)
1568 struct device
*dev
= kobj_to_dev(kobj
);
1570 if (!dev_is_pf(dev
))
1576 static struct attribute_group sriov_dev_attr_group
= {
1577 .attrs
= sriov_dev_attrs
,
1578 .is_visible
= sriov_attrs_are_visible
,
1580 #endif /* CONFIG_PCI_IOV */
1582 static struct attribute_group pci_dev_attr_group
= {
1583 .attrs
= pci_dev_dev_attrs
,
1584 .is_visible
= pci_dev_attrs_are_visible
,
1587 static const struct attribute_group
*pci_dev_attr_groups
[] = {
1588 &pci_dev_attr_group
,
1589 &pci_dev_hp_attr_group
,
1590 #ifdef CONFIG_PCI_IOV
1591 &sriov_dev_attr_group
,
1596 struct device_type pci_dev_type
= {
1597 .groups
= pci_dev_attr_groups
,