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>
26 static int sysfs_initialized
; /* = 0 */
28 /* show configuration fields */
29 #define pci_config_attr(field, format_string) \
31 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
33 struct pci_dev *pdev; \
35 pdev = to_pci_dev (dev); \
36 return sprintf (buf, format_string, pdev->field); \
39 pci_config_attr(vendor
, "0x%04x\n");
40 pci_config_attr(device
, "0x%04x\n");
41 pci_config_attr(subsystem_vendor
, "0x%04x\n");
42 pci_config_attr(subsystem_device
, "0x%04x\n");
43 pci_config_attr(class, "0x%06x\n");
44 pci_config_attr(irq
, "%u\n");
46 static ssize_t
broken_parity_status_show(struct device
*dev
,
47 struct device_attribute
*attr
,
50 struct pci_dev
*pdev
= to_pci_dev(dev
);
51 return sprintf (buf
, "%u\n", pdev
->broken_parity_status
);
54 static ssize_t
broken_parity_status_store(struct device
*dev
,
55 struct device_attribute
*attr
,
56 const char *buf
, size_t count
)
58 struct pci_dev
*pdev
= to_pci_dev(dev
);
59 ssize_t consumed
= -EINVAL
;
61 if ((count
> 0) && (*buf
== '0' || *buf
== '1')) {
62 pdev
->broken_parity_status
= *buf
== '1' ? 1 : 0;
68 static ssize_t
local_cpus_show(struct device
*dev
,
69 struct device_attribute
*attr
, char *buf
)
74 mask
= pcibus_to_cpumask(to_pci_dev(dev
)->bus
);
75 len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
82 static ssize_t
local_cpulist_show(struct device
*dev
,
83 struct device_attribute
*attr
, char *buf
)
88 mask
= pcibus_to_cpumask(to_pci_dev(dev
)->bus
);
89 len
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
);
97 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
99 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
103 resource_size_t start
, end
;
105 if (pci_dev
->subordinate
)
106 max
= DEVICE_COUNT_RESOURCE
;
108 for (i
= 0; i
< max
; i
++) {
109 struct resource
*res
= &pci_dev
->resource
[i
];
110 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
111 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
112 (unsigned long long)start
,
113 (unsigned long long)end
,
114 (unsigned long long)res
->flags
);
119 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
121 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
123 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
124 pci_dev
->vendor
, pci_dev
->device
,
125 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
126 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
127 (u8
)(pci_dev
->class));
130 static ssize_t
is_enabled_store(struct device
*dev
,
131 struct device_attribute
*attr
, const char *buf
,
134 ssize_t result
= -EINVAL
;
135 struct pci_dev
*pdev
= to_pci_dev(dev
);
137 /* this can crash the machine when done on the "wrong" device */
138 if (!capable(CAP_SYS_ADMIN
))
142 if (atomic_read(&pdev
->enable_cnt
) != 0)
143 pci_disable_device(pdev
);
146 } else if (*buf
== '1')
147 result
= pci_enable_device(pdev
);
149 return result
< 0 ? result
: count
;
152 static ssize_t
is_enabled_show(struct device
*dev
,
153 struct device_attribute
*attr
, char *buf
)
155 struct pci_dev
*pdev
;
157 pdev
= to_pci_dev (dev
);
158 return sprintf (buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
163 numa_node_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
165 return sprintf (buf
, "%d\n", dev
->numa_node
);
170 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
172 struct pci_dev
*pdev
= to_pci_dev(dev
);
174 if (!pdev
->subordinate
)
177 return sprintf (buf
, "%u\n",
178 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
182 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
183 const char *buf
, size_t count
)
185 struct pci_dev
*pdev
= to_pci_dev(dev
);
187 /* bad things may happen if the no_msi flag is changed
188 * while some drivers are loaded */
189 if (!capable(CAP_SYS_ADMIN
))
192 if (!pdev
->subordinate
)
196 pdev
->subordinate
->bus_flags
|= PCI_BUS_FLAGS_NO_MSI
;
197 dev_warn(&pdev
->dev
, "forced subordinate bus to not support MSI,"
198 " bad things could happen.\n");
202 pdev
->subordinate
->bus_flags
&= ~PCI_BUS_FLAGS_NO_MSI
;
203 dev_warn(&pdev
->dev
, "forced subordinate bus to support MSI,"
204 " bad things could happen.\n");
210 struct device_attribute pci_dev_attrs
[] = {
214 __ATTR_RO(subsystem_vendor
),
215 __ATTR_RO(subsystem_device
),
218 __ATTR_RO(local_cpus
),
219 __ATTR_RO(local_cpulist
),
222 __ATTR_RO(numa_node
),
224 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
225 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
226 broken_parity_status_show
,broken_parity_status_store
),
227 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
232 pci_read_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
233 char *buf
, loff_t off
, size_t count
)
235 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
236 unsigned int size
= 64;
237 loff_t init_off
= off
;
238 u8
*data
= (u8
*) buf
;
240 /* Several chips lock up trying to read undefined config space */
241 if (capable(CAP_SYS_ADMIN
)) {
242 size
= dev
->cfg_size
;
243 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
249 if (off
+ count
> size
) {
256 if ((off
& 1) && size
) {
258 pci_user_read_config_byte(dev
, off
, &val
);
259 data
[off
- init_off
] = val
;
264 if ((off
& 3) && size
> 2) {
266 pci_user_read_config_word(dev
, off
, &val
);
267 data
[off
- init_off
] = val
& 0xff;
268 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
275 pci_user_read_config_dword(dev
, off
, &val
);
276 data
[off
- init_off
] = val
& 0xff;
277 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
278 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
279 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
286 pci_user_read_config_word(dev
, off
, &val
);
287 data
[off
- init_off
] = val
& 0xff;
288 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
295 pci_user_read_config_byte(dev
, off
, &val
);
296 data
[off
- init_off
] = val
;
305 pci_write_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
306 char *buf
, loff_t off
, size_t count
)
308 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
309 unsigned int size
= count
;
310 loff_t init_off
= off
;
311 u8
*data
= (u8
*) buf
;
313 if (off
> dev
->cfg_size
)
315 if (off
+ count
> dev
->cfg_size
) {
316 size
= dev
->cfg_size
- off
;
320 if ((off
& 1) && size
) {
321 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
326 if ((off
& 3) && size
> 2) {
327 u16 val
= data
[off
- init_off
];
328 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
329 pci_user_write_config_word(dev
, off
, val
);
335 u32 val
= data
[off
- init_off
];
336 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
337 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
338 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
339 pci_user_write_config_dword(dev
, off
, val
);
345 u16 val
= data
[off
- init_off
];
346 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
347 pci_user_write_config_word(dev
, off
, val
);
353 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
361 #ifdef HAVE_PCI_LEGACY
363 * pci_read_legacy_io - read byte(s) from legacy I/O port space
364 * @kobj: kobject corresponding to file to read from
365 * @buf: buffer to store results
366 * @off: offset into legacy I/O port space
367 * @count: number of bytes to read
369 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
370 * callback routine (pci_legacy_read).
373 pci_read_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
374 char *buf
, loff_t off
, size_t count
)
376 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
380 /* Only support 1, 2 or 4 byte accesses */
381 if (count
!= 1 && count
!= 2 && count
!= 4)
384 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
388 * pci_write_legacy_io - write byte(s) to legacy I/O port space
389 * @kobj: kobject corresponding to file to read from
390 * @buf: buffer containing value to be written
391 * @off: offset into legacy I/O port space
392 * @count: number of bytes to write
394 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
395 * callback routine (pci_legacy_write).
398 pci_write_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
399 char *buf
, loff_t off
, size_t count
)
401 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
404 /* Only support 1, 2 or 4 byte accesses */
405 if (count
!= 1 && count
!= 2 && count
!= 4)
408 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
412 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
413 * @kobj: kobject corresponding to device to be mapped
414 * @attr: struct bin_attribute for this file
415 * @vma: struct vm_area_struct passed to mmap
417 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
418 * legacy memory space (first meg of bus space) into application virtual
422 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
423 struct vm_area_struct
*vma
)
425 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
429 return pci_mmap_legacy_page_range(bus
, vma
);
431 #endif /* HAVE_PCI_LEGACY */
435 * pci_mmap_resource - map a PCI resource into user memory space
436 * @kobj: kobject for mapping
437 * @attr: struct bin_attribute for the file being mapped
438 * @vma: struct vm_area_struct passed into the mmap
440 * Use the regular PCI mapping routines to map a PCI resource into userspace.
441 * FIXME: write combining? maybe automatic for prefetchable regions?
444 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
445 struct vm_area_struct
*vma
)
447 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
448 struct device
, kobj
));
449 struct resource
*res
= (struct resource
*)attr
->private;
450 enum pci_mmap_state mmap_type
;
451 resource_size_t start
, end
;
454 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
455 if (res
== &pdev
->resource
[i
])
457 if (i
>= PCI_ROM_RESOURCE
)
460 /* pci_mmap_page_range() expects the same kind of entry as coming
461 * from /proc/bus/pci/ which is a "user visible" value. If this is
462 * different from the resource itself, arch will do necessary fixup.
464 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
465 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
466 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
468 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
472 * pci_remove_resource_files - cleanup resource files
473 * @dev: dev to cleanup
475 * If we created resource files for @dev, remove them from sysfs and
476 * free their resources.
479 pci_remove_resource_files(struct pci_dev
*pdev
)
483 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
484 struct bin_attribute
*res_attr
;
486 res_attr
= pdev
->res_attr
[i
];
488 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
495 * pci_create_resource_files - create resource files in sysfs for @dev
496 * @dev: dev in question
498 * Walk the resources in @dev creating files for each resource available.
500 static int pci_create_resource_files(struct pci_dev
*pdev
)
505 /* Expose the PCI resources from this device as files */
506 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
507 struct bin_attribute
*res_attr
;
509 /* skip empty resources */
510 if (!pci_resource_len(pdev
, i
))
513 /* allocate attribute structure, piggyback attribute name */
514 res_attr
= kzalloc(sizeof(*res_attr
) + 10, GFP_ATOMIC
);
516 char *res_attr_name
= (char *)(res_attr
+ 1);
518 pdev
->res_attr
[i
] = res_attr
;
519 sprintf(res_attr_name
, "resource%d", i
);
520 res_attr
->attr
.name
= res_attr_name
;
521 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
522 res_attr
->size
= pci_resource_len(pdev
, i
);
523 res_attr
->mmap
= pci_mmap_resource
;
524 res_attr
->private = &pdev
->resource
[i
];
525 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
527 pci_remove_resource_files(pdev
);
536 #else /* !HAVE_PCI_MMAP */
537 static inline int pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
538 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
539 #endif /* HAVE_PCI_MMAP */
542 * pci_write_rom - used to enable access to the PCI ROM display
543 * @kobj: kernel object handle
546 * @count: number of byte in input
548 * writing anything except 0 enables it
551 pci_write_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
552 char *buf
, loff_t off
, size_t count
)
554 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
556 if ((off
== 0) && (*buf
== '0') && (count
== 2))
557 pdev
->rom_attr_enabled
= 0;
559 pdev
->rom_attr_enabled
= 1;
565 * pci_read_rom - read a PCI ROM
566 * @kobj: kernel object handle
567 * @buf: where to put the data we read from the ROM
569 * @count: number of bytes to read
571 * Put @count bytes starting at @off into @buf from the ROM in the PCI
572 * device corresponding to @kobj.
575 pci_read_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
576 char *buf
, loff_t off
, size_t count
)
578 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
582 if (!pdev
->rom_attr_enabled
)
585 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
592 if (off
+ count
> size
)
595 memcpy_fromio(buf
, rom
+ off
, count
);
597 pci_unmap_rom(pdev
, rom
);
602 static struct bin_attribute pci_config_attr
= {
605 .mode
= S_IRUGO
| S_IWUSR
,
608 .read
= pci_read_config
,
609 .write
= pci_write_config
,
612 static struct bin_attribute pcie_config_attr
= {
615 .mode
= S_IRUGO
| S_IWUSR
,
618 .read
= pci_read_config
,
619 .write
= pci_write_config
,
622 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
627 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
629 struct bin_attribute
*rom_attr
= NULL
;
632 if (!sysfs_initialized
)
635 if (pdev
->cfg_size
< 4096)
636 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
638 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
642 retval
= pci_create_resource_files(pdev
);
646 /* If the device has a ROM, try to expose it in sysfs. */
647 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
) ||
648 (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)) {
649 rom_attr
= kzalloc(sizeof(*rom_attr
), GFP_ATOMIC
);
651 pdev
->rom_attr
= rom_attr
;
652 rom_attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
653 rom_attr
->attr
.name
= "rom";
654 rom_attr
->attr
.mode
= S_IRUSR
;
655 rom_attr
->read
= pci_read_rom
;
656 rom_attr
->write
= pci_write_rom
;
657 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, rom_attr
);
662 goto err_resource_files
;
665 /* add platform-specific attributes */
666 if (pcibios_add_platform_entries(pdev
))
672 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
673 sysfs_remove_bin_file(&pdev
->dev
.kobj
, rom_attr
);
677 pci_remove_resource_files(pdev
);
679 if (pdev
->cfg_size
< 4096)
680 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
682 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
688 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
689 * @pdev: device whose entries we should free
691 * Cleanup when @pdev is removed from sysfs.
693 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
695 if (!sysfs_initialized
)
698 if (pdev
->cfg_size
< 4096)
699 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
701 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
703 pci_remove_resource_files(pdev
);
705 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
706 if (pdev
->rom_attr
) {
707 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
708 kfree(pdev
->rom_attr
);
713 static int __init
pci_sysfs_init(void)
715 struct pci_dev
*pdev
= NULL
;
718 sysfs_initialized
= 1;
719 for_each_pci_dev(pdev
) {
720 retval
= pci_create_sysfs_dev_files(pdev
);
730 late_initcall(pci_sysfs_init
);