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/pci.h>
20 #include <linux/stat.h>
21 #include <linux/topology.h>
23 #include <linux/capability.h>
24 #include <linux/pci-aspm.h>
27 static int sysfs_initialized
; /* = 0 */
29 /* show configuration fields */
30 #define pci_config_attr(field, format_string) \
32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 struct pci_dev *pdev; \
36 pdev = to_pci_dev (dev); \
37 return sprintf (buf, format_string, pdev->field); \
40 pci_config_attr(vendor
, "0x%04x\n");
41 pci_config_attr(device
, "0x%04x\n");
42 pci_config_attr(subsystem_vendor
, "0x%04x\n");
43 pci_config_attr(subsystem_device
, "0x%04x\n");
44 pci_config_attr(class, "0x%06x\n");
45 pci_config_attr(irq
, "%u\n");
47 static ssize_t
broken_parity_status_show(struct device
*dev
,
48 struct device_attribute
*attr
,
51 struct pci_dev
*pdev
= to_pci_dev(dev
);
52 return sprintf (buf
, "%u\n", pdev
->broken_parity_status
);
55 static ssize_t
broken_parity_status_store(struct device
*dev
,
56 struct device_attribute
*attr
,
57 const char *buf
, size_t count
)
59 struct pci_dev
*pdev
= to_pci_dev(dev
);
60 ssize_t consumed
= -EINVAL
;
62 if ((count
> 0) && (*buf
== '0' || *buf
== '1')) {
63 pdev
->broken_parity_status
= *buf
== '1' ? 1 : 0;
69 static ssize_t
local_cpus_show(struct device
*dev
,
70 struct device_attribute
*attr
, char *buf
)
75 mask
= pcibus_to_cpumask(to_pci_dev(dev
)->bus
);
76 len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
83 static ssize_t
local_cpulist_show(struct device
*dev
,
84 struct device_attribute
*attr
, char *buf
)
89 mask
= pcibus_to_cpumask(to_pci_dev(dev
)->bus
);
90 len
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
);
98 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
100 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
104 resource_size_t start
, end
;
106 if (pci_dev
->subordinate
)
107 max
= DEVICE_COUNT_RESOURCE
;
109 for (i
= 0; i
< max
; i
++) {
110 struct resource
*res
= &pci_dev
->resource
[i
];
111 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
112 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
113 (unsigned long long)start
,
114 (unsigned long long)end
,
115 (unsigned long long)res
->flags
);
120 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
122 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
124 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
125 pci_dev
->vendor
, pci_dev
->device
,
126 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
127 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
128 (u8
)(pci_dev
->class));
131 static ssize_t
is_enabled_store(struct device
*dev
,
132 struct device_attribute
*attr
, const char *buf
,
135 ssize_t result
= -EINVAL
;
136 struct pci_dev
*pdev
= to_pci_dev(dev
);
138 /* this can crash the machine when done on the "wrong" device */
139 if (!capable(CAP_SYS_ADMIN
))
143 if (atomic_read(&pdev
->enable_cnt
) != 0)
144 pci_disable_device(pdev
);
147 } else if (*buf
== '1')
148 result
= pci_enable_device(pdev
);
150 return result
< 0 ? result
: count
;
153 static ssize_t
is_enabled_show(struct device
*dev
,
154 struct device_attribute
*attr
, char *buf
)
156 struct pci_dev
*pdev
;
158 pdev
= to_pci_dev (dev
);
159 return sprintf (buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
164 numa_node_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
166 return sprintf (buf
, "%d\n", dev
->numa_node
);
171 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
173 struct pci_dev
*pdev
= to_pci_dev(dev
);
175 if (!pdev
->subordinate
)
178 return sprintf (buf
, "%u\n",
179 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
183 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
184 const char *buf
, size_t count
)
186 struct pci_dev
*pdev
= to_pci_dev(dev
);
188 /* bad things may happen if the no_msi flag is changed
189 * while some drivers are loaded */
190 if (!capable(CAP_SYS_ADMIN
))
193 if (!pdev
->subordinate
)
197 pdev
->subordinate
->bus_flags
|= PCI_BUS_FLAGS_NO_MSI
;
198 dev_warn(&pdev
->dev
, "forced subordinate bus to not support MSI,"
199 " bad things could happen.\n");
203 pdev
->subordinate
->bus_flags
&= ~PCI_BUS_FLAGS_NO_MSI
;
204 dev_warn(&pdev
->dev
, "forced subordinate bus to support MSI,"
205 " bad things could happen.\n");
211 struct device_attribute pci_dev_attrs
[] = {
215 __ATTR_RO(subsystem_vendor
),
216 __ATTR_RO(subsystem_device
),
219 __ATTR_RO(local_cpus
),
220 __ATTR_RO(local_cpulist
),
223 __ATTR_RO(numa_node
),
225 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
226 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
227 broken_parity_status_show
,broken_parity_status_store
),
228 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
233 pci_read_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
234 char *buf
, loff_t off
, size_t count
)
236 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
237 unsigned int size
= 64;
238 loff_t init_off
= off
;
239 u8
*data
= (u8
*) buf
;
241 /* Several chips lock up trying to read undefined config space */
242 if (capable(CAP_SYS_ADMIN
)) {
243 size
= dev
->cfg_size
;
244 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
250 if (off
+ count
> size
) {
257 if ((off
& 1) && size
) {
259 pci_user_read_config_byte(dev
, off
, &val
);
260 data
[off
- init_off
] = val
;
265 if ((off
& 3) && size
> 2) {
267 pci_user_read_config_word(dev
, off
, &val
);
268 data
[off
- init_off
] = val
& 0xff;
269 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
276 pci_user_read_config_dword(dev
, off
, &val
);
277 data
[off
- init_off
] = val
& 0xff;
278 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
279 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
280 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
287 pci_user_read_config_word(dev
, off
, &val
);
288 data
[off
- init_off
] = val
& 0xff;
289 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
296 pci_user_read_config_byte(dev
, off
, &val
);
297 data
[off
- init_off
] = val
;
306 pci_write_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
307 char *buf
, loff_t off
, size_t count
)
309 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
310 unsigned int size
= count
;
311 loff_t init_off
= off
;
312 u8
*data
= (u8
*) buf
;
314 if (off
> dev
->cfg_size
)
316 if (off
+ count
> dev
->cfg_size
) {
317 size
= dev
->cfg_size
- off
;
321 if ((off
& 1) && size
) {
322 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
327 if ((off
& 3) && size
> 2) {
328 u16 val
= data
[off
- init_off
];
329 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
330 pci_user_write_config_word(dev
, off
, val
);
336 u32 val
= data
[off
- init_off
];
337 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
338 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
339 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
340 pci_user_write_config_dword(dev
, off
, val
);
346 u16 val
= data
[off
- init_off
];
347 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
348 pci_user_write_config_word(dev
, off
, val
);
354 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
363 pci_read_vpd(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
364 char *buf
, loff_t off
, size_t count
)
366 struct pci_dev
*dev
=
367 to_pci_dev(container_of(kobj
, struct device
, kobj
));
371 if (off
> bin_attr
->size
)
373 else if (count
> bin_attr
->size
- off
)
374 count
= bin_attr
->size
- off
;
378 ret
= dev
->vpd
->ops
->read(dev
, off
, end
- off
, buf
);
389 pci_write_vpd(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
390 char *buf
, loff_t off
, size_t count
)
392 struct pci_dev
*dev
=
393 to_pci_dev(container_of(kobj
, struct device
, kobj
));
397 if (off
> bin_attr
->size
)
399 else if (count
> bin_attr
->size
- off
)
400 count
= bin_attr
->size
- off
;
404 ret
= dev
->vpd
->ops
->write(dev
, off
, end
- off
, buf
);
414 #ifdef HAVE_PCI_LEGACY
416 * pci_read_legacy_io - read byte(s) from legacy I/O port space
417 * @kobj: kobject corresponding to file to read from
418 * @buf: buffer to store results
419 * @off: offset into legacy I/O port space
420 * @count: number of bytes to read
422 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
423 * callback routine (pci_legacy_read).
426 pci_read_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
427 char *buf
, loff_t off
, size_t count
)
429 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
433 /* Only support 1, 2 or 4 byte accesses */
434 if (count
!= 1 && count
!= 2 && count
!= 4)
437 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
441 * pci_write_legacy_io - write byte(s) to legacy I/O port space
442 * @kobj: kobject corresponding to file to read from
443 * @buf: buffer containing value to be written
444 * @off: offset into legacy I/O port space
445 * @count: number of bytes to write
447 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
448 * callback routine (pci_legacy_write).
451 pci_write_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
452 char *buf
, loff_t off
, size_t count
)
454 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
457 /* Only support 1, 2 or 4 byte accesses */
458 if (count
!= 1 && count
!= 2 && count
!= 4)
461 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
465 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
466 * @kobj: kobject corresponding to device to be mapped
467 * @attr: struct bin_attribute for this file
468 * @vma: struct vm_area_struct passed to mmap
470 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
471 * legacy memory space (first meg of bus space) into application virtual
475 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
476 struct vm_area_struct
*vma
)
478 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
482 return pci_mmap_legacy_page_range(bus
, vma
);
484 #endif /* HAVE_PCI_LEGACY */
488 * pci_mmap_resource - map a PCI resource into user memory space
489 * @kobj: kobject for mapping
490 * @attr: struct bin_attribute for the file being mapped
491 * @vma: struct vm_area_struct passed into the mmap
493 * Use the regular PCI mapping routines to map a PCI resource into userspace.
494 * FIXME: write combining? maybe automatic for prefetchable regions?
497 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
498 struct vm_area_struct
*vma
)
500 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
501 struct device
, kobj
));
502 struct resource
*res
= (struct resource
*)attr
->private;
503 enum pci_mmap_state mmap_type
;
504 resource_size_t start
, end
;
507 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
508 if (res
== &pdev
->resource
[i
])
510 if (i
>= PCI_ROM_RESOURCE
)
513 /* pci_mmap_page_range() expects the same kind of entry as coming
514 * from /proc/bus/pci/ which is a "user visible" value. If this is
515 * different from the resource itself, arch will do necessary fixup.
517 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
518 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
519 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
521 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
525 * pci_remove_resource_files - cleanup resource files
526 * @dev: dev to cleanup
528 * If we created resource files for @dev, remove them from sysfs and
529 * free their resources.
532 pci_remove_resource_files(struct pci_dev
*pdev
)
536 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
537 struct bin_attribute
*res_attr
;
539 res_attr
= pdev
->res_attr
[i
];
541 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
548 * pci_create_resource_files - create resource files in sysfs for @dev
549 * @dev: dev in question
551 * Walk the resources in @dev creating files for each resource available.
553 static int pci_create_resource_files(struct pci_dev
*pdev
)
558 /* Expose the PCI resources from this device as files */
559 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
560 struct bin_attribute
*res_attr
;
562 /* skip empty resources */
563 if (!pci_resource_len(pdev
, i
))
566 /* allocate attribute structure, piggyback attribute name */
567 res_attr
= kzalloc(sizeof(*res_attr
) + 10, GFP_ATOMIC
);
569 char *res_attr_name
= (char *)(res_attr
+ 1);
571 pdev
->res_attr
[i
] = res_attr
;
572 sprintf(res_attr_name
, "resource%d", i
);
573 res_attr
->attr
.name
= res_attr_name
;
574 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
575 res_attr
->size
= pci_resource_len(pdev
, i
);
576 res_attr
->mmap
= pci_mmap_resource
;
577 res_attr
->private = &pdev
->resource
[i
];
578 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
580 pci_remove_resource_files(pdev
);
589 #else /* !HAVE_PCI_MMAP */
590 static inline int pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
591 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
592 #endif /* HAVE_PCI_MMAP */
595 * pci_write_rom - used to enable access to the PCI ROM display
596 * @kobj: kernel object handle
599 * @count: number of byte in input
601 * writing anything except 0 enables it
604 pci_write_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
605 char *buf
, loff_t off
, size_t count
)
607 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
609 if ((off
== 0) && (*buf
== '0') && (count
== 2))
610 pdev
->rom_attr_enabled
= 0;
612 pdev
->rom_attr_enabled
= 1;
618 * pci_read_rom - read a PCI ROM
619 * @kobj: kernel object handle
620 * @buf: where to put the data we read from the ROM
622 * @count: number of bytes to read
624 * Put @count bytes starting at @off into @buf from the ROM in the PCI
625 * device corresponding to @kobj.
628 pci_read_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
629 char *buf
, loff_t off
, size_t count
)
631 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
635 if (!pdev
->rom_attr_enabled
)
638 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
645 if (off
+ count
> size
)
648 memcpy_fromio(buf
, rom
+ off
, count
);
650 pci_unmap_rom(pdev
, rom
);
655 static struct bin_attribute pci_config_attr
= {
658 .mode
= S_IRUGO
| S_IWUSR
,
661 .read
= pci_read_config
,
662 .write
= pci_write_config
,
665 static struct bin_attribute pcie_config_attr
= {
668 .mode
= S_IRUGO
| S_IWUSR
,
671 .read
= pci_read_config
,
672 .write
= pci_write_config
,
675 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
680 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
682 struct bin_attribute
*attr
= NULL
;
685 if (!sysfs_initialized
)
688 if (pdev
->cfg_size
< 4096)
689 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
691 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
695 /* If the device has VPD, try to expose it in sysfs. */
697 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
699 pdev
->vpd
->attr
= attr
;
700 attr
->size
= pdev
->vpd
->ops
->get_size(pdev
);
701 attr
->attr
.name
= "vpd";
702 attr
->attr
.mode
= S_IRUGO
| S_IWUSR
;
703 attr
->read
= pci_read_vpd
;
704 attr
->write
= pci_write_vpd
;
705 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
710 goto err_config_file
;
714 retval
= pci_create_resource_files(pdev
);
718 /* If the device has a ROM, try to expose it in sysfs. */
719 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
) ||
720 (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)) {
721 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
723 pdev
->rom_attr
= attr
;
724 attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
725 attr
->attr
.name
= "rom";
726 attr
->attr
.mode
= S_IRUSR
;
727 attr
->read
= pci_read_rom
;
728 attr
->write
= pci_write_rom
;
729 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
734 goto err_resource_files
;
737 /* add platform-specific attributes */
738 if (pcibios_add_platform_entries(pdev
))
741 pcie_aspm_create_sysfs_dev_files(pdev
);
746 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
747 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
749 kfree(pdev
->rom_attr
);
751 pci_remove_resource_files(pdev
);
754 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->vpd
->attr
);
756 kfree(pdev
->vpd
->attr
);
759 if (pdev
->cfg_size
< 4096)
760 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
762 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
768 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
769 * @pdev: device whose entries we should free
771 * Cleanup when @pdev is removed from sysfs.
773 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
775 if (!sysfs_initialized
)
778 pcie_aspm_remove_sysfs_dev_files(pdev
);
781 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->vpd
->attr
);
782 kfree(pdev
->vpd
->attr
);
784 if (pdev
->cfg_size
< 4096)
785 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
787 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
789 pci_remove_resource_files(pdev
);
791 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
792 if (pdev
->rom_attr
) {
793 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
794 kfree(pdev
->rom_attr
);
799 static int __init
pci_sysfs_init(void)
801 struct pci_dev
*pdev
= NULL
;
804 sysfs_initialized
= 1;
805 for_each_pci_dev(pdev
) {
806 retval
= pci_create_sysfs_dev_files(pdev
);
816 late_initcall(pci_sysfs_init
);