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/security.h>
27 #include <linux/pci-aspm.h>
28 #include <linux/slab.h>
31 static int sysfs_initialized
; /* = 0 */
33 /* show configuration fields */
34 #define pci_config_attr(field, format_string) \
36 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
38 struct pci_dev *pdev; \
40 pdev = to_pci_dev (dev); \
41 return sprintf (buf, format_string, pdev->field); \
44 pci_config_attr(vendor
, "0x%04x\n");
45 pci_config_attr(device
, "0x%04x\n");
46 pci_config_attr(subsystem_vendor
, "0x%04x\n");
47 pci_config_attr(subsystem_device
, "0x%04x\n");
48 pci_config_attr(class, "0x%06x\n");
49 pci_config_attr(irq
, "%u\n");
51 static ssize_t
broken_parity_status_show(struct device
*dev
,
52 struct device_attribute
*attr
,
55 struct pci_dev
*pdev
= to_pci_dev(dev
);
56 return sprintf (buf
, "%u\n", pdev
->broken_parity_status
);
59 static ssize_t
broken_parity_status_store(struct device
*dev
,
60 struct device_attribute
*attr
,
61 const char *buf
, size_t count
)
63 struct pci_dev
*pdev
= to_pci_dev(dev
);
66 if (strict_strtoul(buf
, 0, &val
) < 0)
69 pdev
->broken_parity_status
= !!val
;
74 static ssize_t
local_cpus_show(struct device
*dev
,
75 struct device_attribute
*attr
, char *buf
)
77 const struct cpumask
*mask
;
81 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
82 cpumask_of_node(dev_to_node(dev
));
84 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
86 len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
93 static ssize_t
local_cpulist_show(struct device
*dev
,
94 struct device_attribute
*attr
, char *buf
)
96 const struct cpumask
*mask
;
100 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
101 cpumask_of_node(dev_to_node(dev
));
103 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
105 len
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
);
113 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
115 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
119 resource_size_t start
, end
;
121 if (pci_dev
->subordinate
)
122 max
= DEVICE_COUNT_RESOURCE
;
124 max
= PCI_BRIDGE_RESOURCES
;
126 for (i
= 0; i
< max
; i
++) {
127 struct resource
*res
= &pci_dev
->resource
[i
];
128 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
129 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
130 (unsigned long long)start
,
131 (unsigned long long)end
,
132 (unsigned long long)res
->flags
);
137 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
139 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
141 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
142 pci_dev
->vendor
, pci_dev
->device
,
143 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
144 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
145 (u8
)(pci_dev
->class));
148 static ssize_t
is_enabled_store(struct device
*dev
,
149 struct device_attribute
*attr
, const char *buf
,
152 struct pci_dev
*pdev
= to_pci_dev(dev
);
154 ssize_t result
= strict_strtoul(buf
, 0, &val
);
159 /* this can crash the machine when done on the "wrong" device */
160 if (!capable(CAP_SYS_ADMIN
))
164 if (pci_is_enabled(pdev
))
165 pci_disable_device(pdev
);
169 result
= pci_enable_device(pdev
);
171 return result
< 0 ? result
: count
;
174 static ssize_t
is_enabled_show(struct device
*dev
,
175 struct device_attribute
*attr
, char *buf
)
177 struct pci_dev
*pdev
;
179 pdev
= to_pci_dev (dev
);
180 return sprintf (buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
185 numa_node_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
187 return sprintf (buf
, "%d\n", dev
->numa_node
);
192 dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
194 struct pci_dev
*pdev
= to_pci_dev(dev
);
196 return sprintf (buf
, "%d\n", fls64(pdev
->dma_mask
));
200 consistent_dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
,
203 return sprintf (buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
207 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
209 struct pci_dev
*pdev
= to_pci_dev(dev
);
211 if (!pdev
->subordinate
)
214 return sprintf (buf
, "%u\n",
215 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
219 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
220 const char *buf
, size_t count
)
222 struct pci_dev
*pdev
= to_pci_dev(dev
);
225 if (strict_strtoul(buf
, 0, &val
) < 0)
228 /* bad things may happen if the no_msi flag is changed
229 * while some drivers are loaded */
230 if (!capable(CAP_SYS_ADMIN
))
233 /* Maybe pci devices without subordinate busses shouldn't even have this
234 * attribute in the first place? */
235 if (!pdev
->subordinate
)
238 /* Is the flag going to change, or keep the value it already had? */
239 if (!(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
) ^
241 pdev
->subordinate
->bus_flags
^= PCI_BUS_FLAGS_NO_MSI
;
243 dev_warn(&pdev
->dev
, "forced subordinate bus to%s support MSI,"
244 " bad things could happen\n", val
? "" : " not");
250 #ifdef CONFIG_HOTPLUG
251 static DEFINE_MUTEX(pci_remove_rescan_mutex
);
252 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
256 struct pci_bus
*b
= NULL
;
258 if (strict_strtoul(buf
, 0, &val
) < 0)
262 mutex_lock(&pci_remove_rescan_mutex
);
263 while ((b
= pci_find_next_bus(b
)) != NULL
)
265 mutex_unlock(&pci_remove_rescan_mutex
);
270 struct bus_attribute pci_bus_attrs
[] = {
271 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
),
276 dev_rescan_store(struct device
*dev
, struct device_attribute
*attr
,
277 const char *buf
, size_t count
)
280 struct pci_dev
*pdev
= to_pci_dev(dev
);
282 if (strict_strtoul(buf
, 0, &val
) < 0)
286 mutex_lock(&pci_remove_rescan_mutex
);
287 pci_rescan_bus(pdev
->bus
);
288 mutex_unlock(&pci_remove_rescan_mutex
);
293 static void remove_callback(struct device
*dev
)
295 struct pci_dev
*pdev
= to_pci_dev(dev
);
297 mutex_lock(&pci_remove_rescan_mutex
);
298 pci_remove_bus_device(pdev
);
299 mutex_unlock(&pci_remove_rescan_mutex
);
303 remove_store(struct device
*dev
, struct device_attribute
*dummy
,
304 const char *buf
, size_t count
)
309 if (strict_strtoul(buf
, 0, &val
) < 0)
312 /* An attribute cannot be unregistered by one of its own methods,
313 * so we have to use this roundabout approach.
316 ret
= device_schedule_callback(dev
, remove_callback
);
323 struct device_attribute pci_dev_attrs
[] = {
327 __ATTR_RO(subsystem_vendor
),
328 __ATTR_RO(subsystem_device
),
331 __ATTR_RO(local_cpus
),
332 __ATTR_RO(local_cpulist
),
335 __ATTR_RO(numa_node
),
337 __ATTR_RO(dma_mask_bits
),
338 __ATTR_RO(consistent_dma_mask_bits
),
339 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
340 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
341 broken_parity_status_show
,broken_parity_status_store
),
342 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
343 #ifdef CONFIG_HOTPLUG
344 __ATTR(remove
, (S_IWUSR
|S_IWGRP
), NULL
, remove_store
),
345 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_rescan_store
),
351 boot_vga_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
353 struct pci_dev
*pdev
= to_pci_dev(dev
);
355 return sprintf(buf
, "%u\n",
356 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
357 IORESOURCE_ROM_SHADOW
));
359 struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
362 pci_read_config(struct file
*filp
, struct kobject
*kobj
,
363 struct bin_attribute
*bin_attr
,
364 char *buf
, loff_t off
, size_t count
)
366 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
367 unsigned int size
= 64;
368 loff_t init_off
= off
;
369 u8
*data
= (u8
*) buf
;
371 /* Several chips lock up trying to read undefined config space */
372 if (security_capable(&init_user_ns
, filp
->f_cred
, CAP_SYS_ADMIN
) == 0) {
373 size
= dev
->cfg_size
;
374 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
380 if (off
+ count
> size
) {
387 if ((off
& 1) && size
) {
389 pci_user_read_config_byte(dev
, off
, &val
);
390 data
[off
- init_off
] = val
;
395 if ((off
& 3) && size
> 2) {
397 pci_user_read_config_word(dev
, off
, &val
);
398 data
[off
- init_off
] = val
& 0xff;
399 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
406 pci_user_read_config_dword(dev
, off
, &val
);
407 data
[off
- init_off
] = val
& 0xff;
408 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
409 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
410 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
417 pci_user_read_config_word(dev
, off
, &val
);
418 data
[off
- init_off
] = val
& 0xff;
419 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
426 pci_user_read_config_byte(dev
, off
, &val
);
427 data
[off
- init_off
] = val
;
436 pci_write_config(struct file
* filp
, struct kobject
*kobj
,
437 struct bin_attribute
*bin_attr
,
438 char *buf
, loff_t off
, size_t count
)
440 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
441 unsigned int size
= count
;
442 loff_t init_off
= off
;
443 u8
*data
= (u8
*) buf
;
445 if (off
> dev
->cfg_size
)
447 if (off
+ count
> dev
->cfg_size
) {
448 size
= dev
->cfg_size
- off
;
452 if ((off
& 1) && size
) {
453 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
458 if ((off
& 3) && size
> 2) {
459 u16 val
= data
[off
- init_off
];
460 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
461 pci_user_write_config_word(dev
, off
, val
);
467 u32 val
= data
[off
- init_off
];
468 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
469 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
470 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
471 pci_user_write_config_dword(dev
, off
, val
);
477 u16 val
= data
[off
- init_off
];
478 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
479 pci_user_write_config_word(dev
, off
, val
);
485 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
494 read_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
495 struct bin_attribute
*bin_attr
,
496 char *buf
, loff_t off
, size_t count
)
498 struct pci_dev
*dev
=
499 to_pci_dev(container_of(kobj
, struct device
, kobj
));
501 if (off
> bin_attr
->size
)
503 else if (count
> bin_attr
->size
- off
)
504 count
= bin_attr
->size
- off
;
506 return pci_read_vpd(dev
, off
, count
, buf
);
510 write_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
511 struct bin_attribute
*bin_attr
,
512 char *buf
, loff_t off
, size_t count
)
514 struct pci_dev
*dev
=
515 to_pci_dev(container_of(kobj
, struct device
, kobj
));
517 if (off
> bin_attr
->size
)
519 else if (count
> bin_attr
->size
- off
)
520 count
= bin_attr
->size
- off
;
522 return pci_write_vpd(dev
, off
, count
, buf
);
525 #ifdef HAVE_PCI_LEGACY
527 * pci_read_legacy_io - read byte(s) from legacy I/O port space
528 * @filp: open sysfs file
529 * @kobj: kobject corresponding to file to read from
530 * @bin_attr: struct bin_attribute for this file
531 * @buf: buffer to store results
532 * @off: offset into legacy I/O port space
533 * @count: number of bytes to read
535 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
536 * callback routine (pci_legacy_read).
539 pci_read_legacy_io(struct file
*filp
, struct kobject
*kobj
,
540 struct bin_attribute
*bin_attr
,
541 char *buf
, loff_t off
, size_t count
)
543 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
547 /* Only support 1, 2 or 4 byte accesses */
548 if (count
!= 1 && count
!= 2 && count
!= 4)
551 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
555 * pci_write_legacy_io - write byte(s) to legacy I/O port space
556 * @filp: open sysfs file
557 * @kobj: kobject corresponding to file to read from
558 * @bin_attr: struct bin_attribute for this file
559 * @buf: buffer containing value to be written
560 * @off: offset into legacy I/O port space
561 * @count: number of bytes to write
563 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
564 * callback routine (pci_legacy_write).
567 pci_write_legacy_io(struct file
*filp
, struct kobject
*kobj
,
568 struct bin_attribute
*bin_attr
,
569 char *buf
, loff_t off
, size_t count
)
571 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
574 /* Only support 1, 2 or 4 byte accesses */
575 if (count
!= 1 && count
!= 2 && count
!= 4)
578 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
582 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
583 * @filp: open sysfs file
584 * @kobj: kobject corresponding to device to be mapped
585 * @attr: struct bin_attribute for this file
586 * @vma: struct vm_area_struct passed to mmap
588 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
589 * legacy memory space (first meg of bus space) into application virtual
593 pci_mmap_legacy_mem(struct file
*filp
, struct kobject
*kobj
,
594 struct bin_attribute
*attr
,
595 struct vm_area_struct
*vma
)
597 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
601 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
605 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
606 * @filp: open sysfs file
607 * @kobj: kobject corresponding to device to be mapped
608 * @attr: struct bin_attribute for this file
609 * @vma: struct vm_area_struct passed to mmap
611 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
612 * legacy IO space (first meg of bus space) into application virtual
613 * memory space. Returns -ENOSYS if the operation isn't supported
616 pci_mmap_legacy_io(struct file
*filp
, struct kobject
*kobj
,
617 struct bin_attribute
*attr
,
618 struct vm_area_struct
*vma
)
620 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
624 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
628 * pci_adjust_legacy_attr - adjustment of legacy file attributes
629 * @b: bus to create files under
630 * @mmap_type: I/O port or memory
632 * Stub implementation. Can be overridden by arch if necessary.
635 pci_adjust_legacy_attr(struct pci_bus
*b
, enum pci_mmap_state mmap_type
)
641 * pci_create_legacy_files - create legacy I/O port and memory files
642 * @b: bus to create files under
644 * Some platforms allow access to legacy I/O port and ISA memory space on
645 * a per-bus basis. This routine creates the files and ties them into
646 * their associated read, write and mmap files from pci-sysfs.c
648 * On error unwind, but don't propagate the error to the caller
649 * as it is ok to set up the PCI bus without these files.
651 void pci_create_legacy_files(struct pci_bus
*b
)
655 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
660 sysfs_bin_attr_init(b
->legacy_io
);
661 b
->legacy_io
->attr
.name
= "legacy_io";
662 b
->legacy_io
->size
= 0xffff;
663 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
664 b
->legacy_io
->read
= pci_read_legacy_io
;
665 b
->legacy_io
->write
= pci_write_legacy_io
;
666 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
667 pci_adjust_legacy_attr(b
, pci_mmap_io
);
668 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
672 /* Allocated above after the legacy_io struct */
673 b
->legacy_mem
= b
->legacy_io
+ 1;
674 sysfs_bin_attr_init(b
->legacy_mem
);
675 b
->legacy_mem
->attr
.name
= "legacy_mem";
676 b
->legacy_mem
->size
= 1024*1024;
677 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
678 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
679 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
680 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
687 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
692 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port "
693 "and ISA memory resources to sysfs\n");
697 void pci_remove_legacy_files(struct pci_bus
*b
)
700 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
701 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
702 kfree(b
->legacy_io
); /* both are allocated here */
705 #endif /* HAVE_PCI_LEGACY */
709 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
,
710 enum pci_mmap_api mmap_api
)
712 unsigned long nr
, start
, size
, pci_start
;
714 if (pci_resource_len(pdev
, resno
) == 0)
716 nr
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
717 start
= vma
->vm_pgoff
;
718 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
719 pci_start
= (mmap_api
== PCI_MMAP_PROCFS
) ?
720 pci_resource_start(pdev
, resno
) >> PAGE_SHIFT
: 0;
721 if (start
>= pci_start
&& start
< pci_start
+ size
&&
722 start
+ nr
<= pci_start
+ size
)
728 * pci_mmap_resource - map a PCI resource into user memory space
729 * @kobj: kobject for mapping
730 * @attr: struct bin_attribute for the file being mapped
731 * @vma: struct vm_area_struct passed into the mmap
732 * @write_combine: 1 for write_combine mapping
734 * Use the regular PCI mapping routines to map a PCI resource into userspace.
737 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
738 struct vm_area_struct
*vma
, int write_combine
)
740 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
741 struct device
, kobj
));
742 struct resource
*res
= attr
->private;
743 enum pci_mmap_state mmap_type
;
744 resource_size_t start
, end
;
747 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
748 if (res
== &pdev
->resource
[i
])
750 if (i
>= PCI_ROM_RESOURCE
)
753 if (!pci_mmap_fits(pdev
, i
, vma
, PCI_MMAP_SYSFS
)) {
754 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
755 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
756 current
->comm
, vma
->vm_end
-vma
->vm_start
, vma
->vm_pgoff
,
758 (u64
)pci_resource_start(pdev
, i
),
759 (u64
)pci_resource_len(pdev
, i
));
763 /* pci_mmap_page_range() expects the same kind of entry as coming
764 * from /proc/bus/pci/ which is a "user visible" value. If this is
765 * different from the resource itself, arch will do necessary fixup.
767 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
768 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
769 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
771 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(start
))
774 return pci_mmap_page_range(pdev
, vma
, mmap_type
, write_combine
);
778 pci_mmap_resource_uc(struct file
*filp
, struct kobject
*kobj
,
779 struct bin_attribute
*attr
,
780 struct vm_area_struct
*vma
)
782 return pci_mmap_resource(kobj
, attr
, vma
, 0);
786 pci_mmap_resource_wc(struct file
*filp
, struct kobject
*kobj
,
787 struct bin_attribute
*attr
,
788 struct vm_area_struct
*vma
)
790 return pci_mmap_resource(kobj
, attr
, vma
, 1);
794 pci_resource_io(struct file
*filp
, struct kobject
*kobj
,
795 struct bin_attribute
*attr
, char *buf
,
796 loff_t off
, size_t count
, bool write
)
798 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
799 struct device
, kobj
));
800 struct resource
*res
= attr
->private;
801 unsigned long port
= off
;
804 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
805 if (res
== &pdev
->resource
[i
])
807 if (i
>= PCI_ROM_RESOURCE
)
810 port
+= pci_resource_start(pdev
, i
);
812 if (port
> pci_resource_end(pdev
, i
))
815 if (port
+ count
- 1 > pci_resource_end(pdev
, i
))
821 outb(*(u8
*)buf
, port
);
823 *(u8
*)buf
= inb(port
);
827 outw(*(u16
*)buf
, port
);
829 *(u16
*)buf
= inw(port
);
833 outl(*(u32
*)buf
, port
);
835 *(u32
*)buf
= inl(port
);
842 pci_read_resource_io(struct file
*filp
, struct kobject
*kobj
,
843 struct bin_attribute
*attr
, char *buf
,
844 loff_t off
, size_t count
)
846 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, false);
850 pci_write_resource_io(struct file
*filp
, struct kobject
*kobj
,
851 struct bin_attribute
*attr
, char *buf
,
852 loff_t off
, size_t count
)
854 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, true);
858 * pci_remove_resource_files - cleanup resource files
859 * @pdev: dev to cleanup
861 * If we created resource files for @pdev, remove them from sysfs and
862 * free their resources.
865 pci_remove_resource_files(struct pci_dev
*pdev
)
869 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
870 struct bin_attribute
*res_attr
;
872 res_attr
= pdev
->res_attr
[i
];
874 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
878 res_attr
= pdev
->res_attr_wc
[i
];
880 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
886 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
888 /* allocate attribute structure, piggyback attribute name */
889 int name_len
= write_combine
? 13 : 10;
890 struct bin_attribute
*res_attr
;
893 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
895 char *res_attr_name
= (char *)(res_attr
+ 1);
897 sysfs_bin_attr_init(res_attr
);
899 pdev
->res_attr_wc
[num
] = res_attr
;
900 sprintf(res_attr_name
, "resource%d_wc", num
);
901 res_attr
->mmap
= pci_mmap_resource_wc
;
903 pdev
->res_attr
[num
] = res_attr
;
904 sprintf(res_attr_name
, "resource%d", num
);
905 res_attr
->mmap
= pci_mmap_resource_uc
;
907 if (pci_resource_flags(pdev
, num
) & IORESOURCE_IO
) {
908 res_attr
->read
= pci_read_resource_io
;
909 res_attr
->write
= pci_write_resource_io
;
911 res_attr
->attr
.name
= res_attr_name
;
912 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
913 res_attr
->size
= pci_resource_len(pdev
, num
);
914 res_attr
->private = &pdev
->resource
[num
];
915 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
923 * pci_create_resource_files - create resource files in sysfs for @dev
924 * @pdev: dev in question
926 * Walk the resources in @pdev creating files for each resource available.
928 static int pci_create_resource_files(struct pci_dev
*pdev
)
933 /* Expose the PCI resources from this device as files */
934 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
936 /* skip empty resources */
937 if (!pci_resource_len(pdev
, i
))
940 retval
= pci_create_attr(pdev
, i
, 0);
941 /* for prefetchable resources, create a WC mappable file */
942 if (!retval
&& pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
943 retval
= pci_create_attr(pdev
, i
, 1);
946 pci_remove_resource_files(pdev
);
952 #else /* !HAVE_PCI_MMAP */
953 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
954 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
955 #endif /* HAVE_PCI_MMAP */
958 * pci_write_rom - used to enable access to the PCI ROM display
960 * @kobj: kernel object handle
961 * @bin_attr: struct bin_attribute for this file
964 * @count: number of byte in input
966 * writing anything except 0 enables it
969 pci_write_rom(struct file
*filp
, struct kobject
*kobj
,
970 struct bin_attribute
*bin_attr
,
971 char *buf
, loff_t off
, size_t count
)
973 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
975 if ((off
== 0) && (*buf
== '0') && (count
== 2))
976 pdev
->rom_attr_enabled
= 0;
978 pdev
->rom_attr_enabled
= 1;
984 * pci_read_rom - read a PCI ROM
986 * @kobj: kernel object handle
987 * @bin_attr: struct bin_attribute for this file
988 * @buf: where to put the data we read from the ROM
990 * @count: number of bytes to read
992 * Put @count bytes starting at @off into @buf from the ROM in the PCI
993 * device corresponding to @kobj.
996 pci_read_rom(struct file
*filp
, struct kobject
*kobj
,
997 struct bin_attribute
*bin_attr
,
998 char *buf
, loff_t off
, size_t count
)
1000 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
1004 if (!pdev
->rom_attr_enabled
)
1007 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
1014 if (off
+ count
> size
)
1017 memcpy_fromio(buf
, rom
+ off
, count
);
1019 pci_unmap_rom(pdev
, rom
);
1024 static struct bin_attribute pci_config_attr
= {
1027 .mode
= S_IRUGO
| S_IWUSR
,
1029 .size
= PCI_CFG_SPACE_SIZE
,
1030 .read
= pci_read_config
,
1031 .write
= pci_write_config
,
1034 static struct bin_attribute pcie_config_attr
= {
1037 .mode
= S_IRUGO
| S_IWUSR
,
1039 .size
= PCI_CFG_SPACE_EXP_SIZE
,
1040 .read
= pci_read_config
,
1041 .write
= pci_write_config
,
1044 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
1049 static ssize_t
reset_store(struct device
*dev
,
1050 struct device_attribute
*attr
, const char *buf
,
1053 struct pci_dev
*pdev
= to_pci_dev(dev
);
1055 ssize_t result
= strict_strtoul(buf
, 0, &val
);
1063 result
= pci_reset_function(pdev
);
1070 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
1072 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
1075 struct bin_attribute
*attr
;
1077 /* If the device has VPD, try to expose it in sysfs. */
1079 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1083 sysfs_bin_attr_init(attr
);
1084 attr
->size
= dev
->vpd
->len
;
1085 attr
->attr
.name
= "vpd";
1086 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1087 attr
->read
= read_vpd_attr
;
1088 attr
->write
= write_vpd_attr
;
1089 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
1094 dev
->vpd
->attr
= attr
;
1097 /* Active State Power Management */
1098 pcie_aspm_create_sysfs_dev_files(dev
);
1100 if (!pci_probe_reset_function(dev
)) {
1101 retval
= device_create_file(&dev
->dev
, &reset_attr
);
1109 pcie_aspm_remove_sysfs_dev_files(dev
);
1110 if (dev
->vpd
&& dev
->vpd
->attr
) {
1111 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1112 kfree(dev
->vpd
->attr
);
1118 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
1122 struct bin_attribute
*attr
;
1124 if (!sysfs_initialized
)
1127 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1128 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1130 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1134 retval
= pci_create_resource_files(pdev
);
1136 goto err_config_file
;
1138 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1139 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1140 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1143 /* If the device has a ROM, try to expose it in sysfs. */
1145 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1148 goto err_resource_files
;
1150 sysfs_bin_attr_init(attr
);
1151 attr
->size
= rom_size
;
1152 attr
->attr
.name
= "rom";
1153 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1154 attr
->read
= pci_read_rom
;
1155 attr
->write
= pci_write_rom
;
1156 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1159 goto err_resource_files
;
1161 pdev
->rom_attr
= attr
;
1164 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
) {
1165 retval
= device_create_file(&pdev
->dev
, &vga_attr
);
1170 /* add platform-specific attributes */
1171 retval
= pcibios_add_platform_entries(pdev
);
1175 /* add sysfs entries for various capabilities */
1176 retval
= pci_create_capabilities_sysfs(pdev
);
1180 pci_create_firmware_label_files(pdev
);
1185 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
1186 device_remove_file(&pdev
->dev
, &vga_attr
);
1189 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1190 kfree(pdev
->rom_attr
);
1191 pdev
->rom_attr
= NULL
;
1194 pci_remove_resource_files(pdev
);
1196 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1197 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1199 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1204 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1206 if (dev
->vpd
&& dev
->vpd
->attr
) {
1207 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1208 kfree(dev
->vpd
->attr
);
1211 pcie_aspm_remove_sysfs_dev_files(dev
);
1212 if (dev
->reset_fn
) {
1213 device_remove_file(&dev
->dev
, &reset_attr
);
1219 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1220 * @pdev: device whose entries we should free
1222 * Cleanup when @pdev is removed from sysfs.
1224 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1228 if (!sysfs_initialized
)
1231 pci_remove_capabilities_sysfs(pdev
);
1233 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1234 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1236 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1238 pci_remove_resource_files(pdev
);
1240 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1241 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1242 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1245 if (rom_size
&& pdev
->rom_attr
) {
1246 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1247 kfree(pdev
->rom_attr
);
1250 pci_remove_firmware_label_files(pdev
);
1254 static int __init
pci_sysfs_init(void)
1256 struct pci_dev
*pdev
= NULL
;
1259 sysfs_initialized
= 1;
1260 for_each_pci_dev(pdev
) {
1261 retval
= pci_create_sysfs_dev_files(pdev
);
1271 late_initcall(pci_sysfs_init
);