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/topology.h>
25 #include <linux/capability.h>
26 #include <linux/pci-aspm.h>
27 #include <linux/slab.h>
30 static int sysfs_initialized
; /* = 0 */
32 /* show configuration fields */
33 #define pci_config_attr(field, format_string) \
35 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
37 struct pci_dev *pdev; \
39 pdev = to_pci_dev (dev); \
40 return sprintf (buf, format_string, pdev->field); \
43 pci_config_attr(vendor
, "0x%04x\n");
44 pci_config_attr(device
, "0x%04x\n");
45 pci_config_attr(subsystem_vendor
, "0x%04x\n");
46 pci_config_attr(subsystem_device
, "0x%04x\n");
47 pci_config_attr(class, "0x%06x\n");
48 pci_config_attr(irq
, "%u\n");
50 static ssize_t
broken_parity_status_show(struct device
*dev
,
51 struct device_attribute
*attr
,
54 struct pci_dev
*pdev
= to_pci_dev(dev
);
55 return sprintf (buf
, "%u\n", pdev
->broken_parity_status
);
58 static ssize_t
broken_parity_status_store(struct device
*dev
,
59 struct device_attribute
*attr
,
60 const char *buf
, size_t count
)
62 struct pci_dev
*pdev
= to_pci_dev(dev
);
65 if (strict_strtoul(buf
, 0, &val
) < 0)
68 pdev
->broken_parity_status
= !!val
;
73 static ssize_t
local_cpus_show(struct device
*dev
,
74 struct device_attribute
*attr
, char *buf
)
76 const struct cpumask
*mask
;
80 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
81 cpumask_of_node(dev_to_node(dev
));
83 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
85 len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
92 static ssize_t
local_cpulist_show(struct device
*dev
,
93 struct device_attribute
*attr
, char *buf
)
95 const struct cpumask
*mask
;
99 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
100 cpumask_of_node(dev_to_node(dev
));
102 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
104 len
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
);
112 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
114 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
118 resource_size_t start
, end
;
120 if (pci_dev
->subordinate
)
121 max
= DEVICE_COUNT_RESOURCE
;
123 max
= PCI_BRIDGE_RESOURCES
;
125 for (i
= 0; i
< max
; i
++) {
126 struct resource
*res
= &pci_dev
->resource
[i
];
127 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
128 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
129 (unsigned long long)start
,
130 (unsigned long long)end
,
131 (unsigned long long)res
->flags
);
136 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
138 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
140 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
141 pci_dev
->vendor
, pci_dev
->device
,
142 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
143 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
144 (u8
)(pci_dev
->class));
147 static ssize_t
is_enabled_store(struct device
*dev
,
148 struct device_attribute
*attr
, const char *buf
,
151 struct pci_dev
*pdev
= to_pci_dev(dev
);
153 ssize_t result
= strict_strtoul(buf
, 0, &val
);
158 /* this can crash the machine when done on the "wrong" device */
159 if (!capable(CAP_SYS_ADMIN
))
163 if (pci_is_enabled(pdev
))
164 pci_disable_device(pdev
);
168 result
= pci_enable_device(pdev
);
170 return result
< 0 ? result
: count
;
173 static ssize_t
is_enabled_show(struct device
*dev
,
174 struct device_attribute
*attr
, char *buf
)
176 struct pci_dev
*pdev
;
178 pdev
= to_pci_dev (dev
);
179 return sprintf (buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
184 numa_node_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
186 return sprintf (buf
, "%d\n", dev
->numa_node
);
191 dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
193 struct pci_dev
*pdev
= to_pci_dev(dev
);
195 return sprintf (buf
, "%d\n", fls64(pdev
->dma_mask
));
199 consistent_dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
,
202 return sprintf (buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
206 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
208 struct pci_dev
*pdev
= to_pci_dev(dev
);
210 if (!pdev
->subordinate
)
213 return sprintf (buf
, "%u\n",
214 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
218 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
219 const char *buf
, size_t count
)
221 struct pci_dev
*pdev
= to_pci_dev(dev
);
224 if (strict_strtoul(buf
, 0, &val
) < 0)
227 /* bad things may happen if the no_msi flag is changed
228 * while some drivers are loaded */
229 if (!capable(CAP_SYS_ADMIN
))
232 /* Maybe pci devices without subordinate busses shouldn't even have this
233 * attribute in the first place? */
234 if (!pdev
->subordinate
)
237 /* Is the flag going to change, or keep the value it already had? */
238 if (!(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
) ^
240 pdev
->subordinate
->bus_flags
^= PCI_BUS_FLAGS_NO_MSI
;
242 dev_warn(&pdev
->dev
, "forced subordinate bus to%s support MSI,"
243 " bad things could happen\n", val
? "" : " not");
249 #ifdef CONFIG_HOTPLUG
250 static DEFINE_MUTEX(pci_remove_rescan_mutex
);
251 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
255 struct pci_bus
*b
= NULL
;
257 if (strict_strtoul(buf
, 0, &val
) < 0)
261 mutex_lock(&pci_remove_rescan_mutex
);
262 while ((b
= pci_find_next_bus(b
)) != NULL
)
264 mutex_unlock(&pci_remove_rescan_mutex
);
269 struct bus_attribute pci_bus_attrs
[] = {
270 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
),
275 dev_rescan_store(struct device
*dev
, struct device_attribute
*attr
,
276 const char *buf
, size_t count
)
279 struct pci_dev
*pdev
= to_pci_dev(dev
);
281 if (strict_strtoul(buf
, 0, &val
) < 0)
285 mutex_lock(&pci_remove_rescan_mutex
);
286 pci_rescan_bus(pdev
->bus
);
287 mutex_unlock(&pci_remove_rescan_mutex
);
292 static void remove_callback(struct device
*dev
)
294 struct pci_dev
*pdev
= to_pci_dev(dev
);
296 mutex_lock(&pci_remove_rescan_mutex
);
297 pci_remove_bus_device(pdev
);
298 mutex_unlock(&pci_remove_rescan_mutex
);
302 remove_store(struct device
*dev
, struct device_attribute
*dummy
,
303 const char *buf
, size_t count
)
308 if (strict_strtoul(buf
, 0, &val
) < 0)
311 /* An attribute cannot be unregistered by one of its own methods,
312 * so we have to use this roundabout approach.
315 ret
= device_schedule_callback(dev
, remove_callback
);
322 struct device_attribute pci_dev_attrs
[] = {
326 __ATTR_RO(subsystem_vendor
),
327 __ATTR_RO(subsystem_device
),
330 __ATTR_RO(local_cpus
),
331 __ATTR_RO(local_cpulist
),
334 __ATTR_RO(numa_node
),
336 __ATTR_RO(dma_mask_bits
),
337 __ATTR_RO(consistent_dma_mask_bits
),
338 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
339 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
340 broken_parity_status_show
,broken_parity_status_store
),
341 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
342 #ifdef CONFIG_HOTPLUG
343 __ATTR(remove
, (S_IWUSR
|S_IWGRP
), NULL
, remove_store
),
344 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_rescan_store
),
350 boot_vga_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
352 struct pci_dev
*pdev
= to_pci_dev(dev
);
354 return sprintf(buf
, "%u\n",
355 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
356 IORESOURCE_ROM_SHADOW
));
358 struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
361 pci_read_config(struct file
*filp
, struct kobject
*kobj
,
362 struct bin_attribute
*bin_attr
,
363 char *buf
, loff_t off
, size_t count
)
365 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
366 unsigned int size
= 64;
367 loff_t init_off
= off
;
368 u8
*data
= (u8
*) buf
;
370 /* Several chips lock up trying to read undefined config space */
371 if (cap_raised(filp
->f_cred
->cap_effective
, CAP_SYS_ADMIN
)) {
372 size
= dev
->cfg_size
;
373 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
379 if (off
+ count
> size
) {
386 if ((off
& 1) && size
) {
388 pci_user_read_config_byte(dev
, off
, &val
);
389 data
[off
- init_off
] = val
;
394 if ((off
& 3) && size
> 2) {
396 pci_user_read_config_word(dev
, off
, &val
);
397 data
[off
- init_off
] = val
& 0xff;
398 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
405 pci_user_read_config_dword(dev
, off
, &val
);
406 data
[off
- init_off
] = val
& 0xff;
407 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
408 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
409 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
416 pci_user_read_config_word(dev
, off
, &val
);
417 data
[off
- init_off
] = val
& 0xff;
418 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
425 pci_user_read_config_byte(dev
, off
, &val
);
426 data
[off
- init_off
] = val
;
435 pci_write_config(struct file
* filp
, struct kobject
*kobj
,
436 struct bin_attribute
*bin_attr
,
437 char *buf
, loff_t off
, size_t count
)
439 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
440 unsigned int size
= count
;
441 loff_t init_off
= off
;
442 u8
*data
= (u8
*) buf
;
444 if (off
> dev
->cfg_size
)
446 if (off
+ count
> dev
->cfg_size
) {
447 size
= dev
->cfg_size
- off
;
451 if ((off
& 1) && size
) {
452 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
457 if ((off
& 3) && size
> 2) {
458 u16 val
= data
[off
- init_off
];
459 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
460 pci_user_write_config_word(dev
, off
, val
);
466 u32 val
= data
[off
- init_off
];
467 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
468 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
469 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
470 pci_user_write_config_dword(dev
, off
, val
);
476 u16 val
= data
[off
- init_off
];
477 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
478 pci_user_write_config_word(dev
, off
, val
);
484 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
493 read_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
494 struct bin_attribute
*bin_attr
,
495 char *buf
, loff_t off
, size_t count
)
497 struct pci_dev
*dev
=
498 to_pci_dev(container_of(kobj
, struct device
, kobj
));
500 if (off
> bin_attr
->size
)
502 else if (count
> bin_attr
->size
- off
)
503 count
= bin_attr
->size
- off
;
505 return pci_read_vpd(dev
, off
, count
, buf
);
509 write_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
510 struct bin_attribute
*bin_attr
,
511 char *buf
, loff_t off
, size_t count
)
513 struct pci_dev
*dev
=
514 to_pci_dev(container_of(kobj
, struct device
, kobj
));
516 if (off
> bin_attr
->size
)
518 else if (count
> bin_attr
->size
- off
)
519 count
= bin_attr
->size
- off
;
521 return pci_write_vpd(dev
, off
, count
, buf
);
524 #ifdef HAVE_PCI_LEGACY
526 * pci_read_legacy_io - read byte(s) from legacy I/O port space
527 * @filp: open sysfs file
528 * @kobj: kobject corresponding to file to read from
529 * @bin_attr: struct bin_attribute for this file
530 * @buf: buffer to store results
531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to read
534 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_read).
538 pci_read_legacy_io(struct file
*filp
, struct kobject
*kobj
,
539 struct bin_attribute
*bin_attr
,
540 char *buf
, loff_t off
, size_t count
)
542 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
546 /* Only support 1, 2 or 4 byte accesses */
547 if (count
!= 1 && count
!= 2 && count
!= 4)
550 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
554 * pci_write_legacy_io - write byte(s) to legacy I/O port space
555 * @filp: open sysfs file
556 * @kobj: kobject corresponding to file to read from
557 * @bin_attr: struct bin_attribute for this file
558 * @buf: buffer containing value to be written
559 * @off: offset into legacy I/O port space
560 * @count: number of bytes to write
562 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
563 * callback routine (pci_legacy_write).
566 pci_write_legacy_io(struct file
*filp
, struct kobject
*kobj
,
567 struct bin_attribute
*bin_attr
,
568 char *buf
, loff_t off
, size_t count
)
570 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
573 /* Only support 1, 2 or 4 byte accesses */
574 if (count
!= 1 && count
!= 2 && count
!= 4)
577 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
581 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
582 * @filp: open sysfs file
583 * @kobj: kobject corresponding to device to be mapped
584 * @attr: struct bin_attribute for this file
585 * @vma: struct vm_area_struct passed to mmap
587 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
588 * legacy memory space (first meg of bus space) into application virtual
592 pci_mmap_legacy_mem(struct file
*filp
, struct kobject
*kobj
,
593 struct bin_attribute
*attr
,
594 struct vm_area_struct
*vma
)
596 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
600 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
604 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
605 * @filp: open sysfs file
606 * @kobj: kobject corresponding to device to be mapped
607 * @attr: struct bin_attribute for this file
608 * @vma: struct vm_area_struct passed to mmap
610 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
611 * legacy IO space (first meg of bus space) into application virtual
612 * memory space. Returns -ENOSYS if the operation isn't supported
615 pci_mmap_legacy_io(struct file
*filp
, struct kobject
*kobj
,
616 struct bin_attribute
*attr
,
617 struct vm_area_struct
*vma
)
619 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
623 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
627 * pci_adjust_legacy_attr - adjustment of legacy file attributes
628 * @b: bus to create files under
629 * @mmap_type: I/O port or memory
631 * Stub implementation. Can be overridden by arch if necessary.
634 pci_adjust_legacy_attr(struct pci_bus
*b
, enum pci_mmap_state mmap_type
)
640 * pci_create_legacy_files - create legacy I/O port and memory files
641 * @b: bus to create files under
643 * Some platforms allow access to legacy I/O port and ISA memory space on
644 * a per-bus basis. This routine creates the files and ties them into
645 * their associated read, write and mmap files from pci-sysfs.c
647 * On error unwind, but don't propogate the error to the caller
648 * as it is ok to set up the PCI bus without these files.
650 void pci_create_legacy_files(struct pci_bus
*b
)
654 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
659 sysfs_bin_attr_init(b
->legacy_io
);
660 b
->legacy_io
->attr
.name
= "legacy_io";
661 b
->legacy_io
->size
= 0xffff;
662 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
663 b
->legacy_io
->read
= pci_read_legacy_io
;
664 b
->legacy_io
->write
= pci_write_legacy_io
;
665 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
666 pci_adjust_legacy_attr(b
, pci_mmap_io
);
667 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
671 /* Allocated above after the legacy_io struct */
672 b
->legacy_mem
= b
->legacy_io
+ 1;
673 sysfs_bin_attr_init(b
->legacy_mem
);
674 b
->legacy_mem
->attr
.name
= "legacy_mem";
675 b
->legacy_mem
->size
= 1024*1024;
676 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
677 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
678 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
679 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
686 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
691 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port "
692 "and ISA memory resources to sysfs\n");
696 void pci_remove_legacy_files(struct pci_bus
*b
)
699 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
700 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
701 kfree(b
->legacy_io
); /* both are allocated here */
704 #endif /* HAVE_PCI_LEGACY */
708 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
)
710 unsigned long nr
, start
, size
;
712 nr
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
713 start
= vma
->vm_pgoff
;
714 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
715 if (start
< size
&& size
- start
>= nr
)
717 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
718 current
->comm
, start
, start
+nr
, pci_name(pdev
), resno
, size
);
723 * pci_mmap_resource - map a PCI resource into user memory space
724 * @kobj: kobject for mapping
725 * @attr: struct bin_attribute for the file being mapped
726 * @vma: struct vm_area_struct passed into the mmap
727 * @write_combine: 1 for write_combine mapping
729 * Use the regular PCI mapping routines to map a PCI resource into userspace.
732 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
733 struct vm_area_struct
*vma
, int write_combine
)
735 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
736 struct device
, kobj
));
737 struct resource
*res
= (struct resource
*)attr
->private;
738 enum pci_mmap_state mmap_type
;
739 resource_size_t start
, end
;
742 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
743 if (res
== &pdev
->resource
[i
])
745 if (i
>= PCI_ROM_RESOURCE
)
748 if (!pci_mmap_fits(pdev
, i
, vma
))
751 /* pci_mmap_page_range() expects the same kind of entry as coming
752 * from /proc/bus/pci/ which is a "user visible" value. If this is
753 * different from the resource itself, arch will do necessary fixup.
755 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
756 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
757 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
759 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(start
))
762 return pci_mmap_page_range(pdev
, vma
, mmap_type
, write_combine
);
766 pci_mmap_resource_uc(struct file
*filp
, struct kobject
*kobj
,
767 struct bin_attribute
*attr
,
768 struct vm_area_struct
*vma
)
770 return pci_mmap_resource(kobj
, attr
, vma
, 0);
774 pci_mmap_resource_wc(struct file
*filp
, struct kobject
*kobj
,
775 struct bin_attribute
*attr
,
776 struct vm_area_struct
*vma
)
778 return pci_mmap_resource(kobj
, attr
, vma
, 1);
782 * pci_remove_resource_files - cleanup resource files
783 * @pdev: dev to cleanup
785 * If we created resource files for @pdev, remove them from sysfs and
786 * free their resources.
789 pci_remove_resource_files(struct pci_dev
*pdev
)
793 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
794 struct bin_attribute
*res_attr
;
796 res_attr
= pdev
->res_attr
[i
];
798 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
802 res_attr
= pdev
->res_attr_wc
[i
];
804 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
810 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
812 /* allocate attribute structure, piggyback attribute name */
813 int name_len
= write_combine
? 13 : 10;
814 struct bin_attribute
*res_attr
;
817 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
819 char *res_attr_name
= (char *)(res_attr
+ 1);
821 sysfs_bin_attr_init(res_attr
);
823 pdev
->res_attr_wc
[num
] = res_attr
;
824 sprintf(res_attr_name
, "resource%d_wc", num
);
825 res_attr
->mmap
= pci_mmap_resource_wc
;
827 pdev
->res_attr
[num
] = res_attr
;
828 sprintf(res_attr_name
, "resource%d", num
);
829 res_attr
->mmap
= pci_mmap_resource_uc
;
831 res_attr
->attr
.name
= res_attr_name
;
832 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
833 res_attr
->size
= pci_resource_len(pdev
, num
);
834 res_attr
->private = &pdev
->resource
[num
];
835 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
843 * pci_create_resource_files - create resource files in sysfs for @dev
844 * @pdev: dev in question
846 * Walk the resources in @pdev creating files for each resource available.
848 static int pci_create_resource_files(struct pci_dev
*pdev
)
853 /* Expose the PCI resources from this device as files */
854 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
856 /* skip empty resources */
857 if (!pci_resource_len(pdev
, i
))
860 retval
= pci_create_attr(pdev
, i
, 0);
861 /* for prefetchable resources, create a WC mappable file */
862 if (!retval
&& pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
863 retval
= pci_create_attr(pdev
, i
, 1);
866 pci_remove_resource_files(pdev
);
872 #else /* !HAVE_PCI_MMAP */
873 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
874 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
875 #endif /* HAVE_PCI_MMAP */
878 * pci_write_rom - used to enable access to the PCI ROM display
880 * @kobj: kernel object handle
881 * @bin_attr: struct bin_attribute for this file
884 * @count: number of byte in input
886 * writing anything except 0 enables it
889 pci_write_rom(struct file
*filp
, struct kobject
*kobj
,
890 struct bin_attribute
*bin_attr
,
891 char *buf
, loff_t off
, size_t count
)
893 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
895 if ((off
== 0) && (*buf
== '0') && (count
== 2))
896 pdev
->rom_attr_enabled
= 0;
898 pdev
->rom_attr_enabled
= 1;
904 * pci_read_rom - read a PCI ROM
906 * @kobj: kernel object handle
907 * @bin_attr: struct bin_attribute for this file
908 * @buf: where to put the data we read from the ROM
910 * @count: number of bytes to read
912 * Put @count bytes starting at @off into @buf from the ROM in the PCI
913 * device corresponding to @kobj.
916 pci_read_rom(struct file
*filp
, struct kobject
*kobj
,
917 struct bin_attribute
*bin_attr
,
918 char *buf
, loff_t off
, size_t count
)
920 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
924 if (!pdev
->rom_attr_enabled
)
927 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
934 if (off
+ count
> size
)
937 memcpy_fromio(buf
, rom
+ off
, count
);
939 pci_unmap_rom(pdev
, rom
);
944 static struct bin_attribute pci_config_attr
= {
947 .mode
= S_IRUGO
| S_IWUSR
,
949 .size
= PCI_CFG_SPACE_SIZE
,
950 .read
= pci_read_config
,
951 .write
= pci_write_config
,
954 static struct bin_attribute pcie_config_attr
= {
957 .mode
= S_IRUGO
| S_IWUSR
,
959 .size
= PCI_CFG_SPACE_EXP_SIZE
,
960 .read
= pci_read_config
,
961 .write
= pci_write_config
,
964 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
969 static ssize_t
reset_store(struct device
*dev
,
970 struct device_attribute
*attr
, const char *buf
,
973 struct pci_dev
*pdev
= to_pci_dev(dev
);
975 ssize_t result
= strict_strtoul(buf
, 0, &val
);
983 result
= pci_reset_function(pdev
);
990 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
992 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
995 struct bin_attribute
*attr
;
997 /* If the device has VPD, try to expose it in sysfs. */
999 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1003 sysfs_bin_attr_init(attr
);
1004 attr
->size
= dev
->vpd
->len
;
1005 attr
->attr
.name
= "vpd";
1006 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1007 attr
->read
= read_vpd_attr
;
1008 attr
->write
= write_vpd_attr
;
1009 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
1011 kfree(dev
->vpd
->attr
);
1014 dev
->vpd
->attr
= attr
;
1017 /* Active State Power Management */
1018 pcie_aspm_create_sysfs_dev_files(dev
);
1020 if (!pci_probe_reset_function(dev
)) {
1021 retval
= device_create_file(&dev
->dev
, &reset_attr
);
1029 pcie_aspm_remove_sysfs_dev_files(dev
);
1030 if (dev
->vpd
&& dev
->vpd
->attr
) {
1031 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1032 kfree(dev
->vpd
->attr
);
1038 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
1042 struct bin_attribute
*attr
;
1044 if (!sysfs_initialized
)
1047 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1048 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1050 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1054 retval
= pci_create_resource_files(pdev
);
1056 goto err_config_file
;
1058 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1059 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1060 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1063 /* If the device has a ROM, try to expose it in sysfs. */
1065 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1068 goto err_resource_files
;
1070 sysfs_bin_attr_init(attr
);
1071 attr
->size
= rom_size
;
1072 attr
->attr
.name
= "rom";
1073 attr
->attr
.mode
= S_IRUSR
;
1074 attr
->read
= pci_read_rom
;
1075 attr
->write
= pci_write_rom
;
1076 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1079 goto err_resource_files
;
1081 pdev
->rom_attr
= attr
;
1084 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
) {
1085 retval
= device_create_file(&pdev
->dev
, &vga_attr
);
1090 /* add platform-specific attributes */
1091 retval
= pcibios_add_platform_entries(pdev
);
1095 /* add sysfs entries for various capabilities */
1096 retval
= pci_create_capabilities_sysfs(pdev
);
1103 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
1104 device_remove_file(&pdev
->dev
, &vga_attr
);
1107 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1108 kfree(pdev
->rom_attr
);
1109 pdev
->rom_attr
= NULL
;
1112 pci_remove_resource_files(pdev
);
1114 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1115 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1117 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1122 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1124 if (dev
->vpd
&& dev
->vpd
->attr
) {
1125 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1126 kfree(dev
->vpd
->attr
);
1129 pcie_aspm_remove_sysfs_dev_files(dev
);
1130 if (dev
->reset_fn
) {
1131 device_remove_file(&dev
->dev
, &reset_attr
);
1137 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1138 * @pdev: device whose entries we should free
1140 * Cleanup when @pdev is removed from sysfs.
1142 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1146 if (!sysfs_initialized
)
1149 pci_remove_capabilities_sysfs(pdev
);
1151 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1152 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1154 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1156 pci_remove_resource_files(pdev
);
1158 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1159 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1160 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1163 if (rom_size
&& pdev
->rom_attr
) {
1164 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1165 kfree(pdev
->rom_attr
);
1169 static int __init
pci_sysfs_init(void)
1171 struct pci_dev
*pdev
= NULL
;
1174 sysfs_initialized
= 1;
1175 for_each_pci_dev(pdev
) {
1176 retval
= pci_create_sysfs_dev_files(pdev
);
1186 late_initcall(pci_sysfs_init
);