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);
112 * PCI Bus Class Devices
114 static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
116 struct device_attribute *attr,
120 const struct cpumask *cpumask;
122 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
124 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
125 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
131 static inline ssize_t pci_bus_show_cpumaskaffinity(struct device *dev,
132 struct device_attribute *attr,
135 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
138 static inline ssize_t pci_bus_show_cpulistaffinity(struct device *dev,
139 struct device_attribute *attr,
142 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
147 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
149 struct pci_dev * pci_dev = to_pci_dev(dev);
153 resource_size_t start, end;
155 if (pci_dev->subordinate)
156 max = DEVICE_COUNT_RESOURCE;
158 max = PCI_BRIDGE_RESOURCES;
160 for (i = 0; i < max; i++) {
161 struct resource *res = &pci_dev->resource[i];
162 pci_resource_to_user(pci_dev, i, res, &start, &end);
163 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
164 (unsigned long long)start,
165 (unsigned long long)end,
166 (unsigned long long)res->flags);
171 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
173 struct pci_dev *pci_dev = to_pci_dev(dev);
175 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
176 pci_dev->vendor, pci_dev->device,
177 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
178 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
179 (u8)(pci_dev->class));
182 static ssize_t is_enabled_store(struct device *dev,
183 struct device_attribute *attr, const char *buf,
186 struct pci_dev *pdev = to_pci_dev(dev);
188 ssize_t result = strict_strtoul(buf, 0, &val);
193 /* this can crash the machine when done on the "wrong" device */
194 if (!capable(CAP_SYS_ADMIN))
198 if (pci_is_enabled(pdev))
199 pci_disable_device(pdev);
203 result = pci_enable_device(pdev);
205 return result < 0 ? result : count;
208 static ssize_t is_enabled_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct pci_dev *pdev;
213 pdev = to_pci_dev (dev);
214 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
219 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
221 return sprintf (buf, "%d\n", dev->numa_node);
226 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
228 struct pci_dev *pdev = to_pci_dev(dev);
230 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
234 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
237 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
241 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
243 struct pci_dev *pdev = to_pci_dev(dev);
245 if (!pdev->subordinate)
248 return sprintf (buf, "%u\n",
249 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
253 msi_bus_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
256 struct pci_dev *pdev = to_pci_dev(dev);
259 if (strict_strtoul(buf, 0, &val) < 0)
262 /* bad things may happen if the no_msi flag is changed
263 * while some drivers are loaded */
264 if (!capable(CAP_SYS_ADMIN))
267 /* Maybe pci devices without subordinate busses shouldn't even have this
268 * attribute in the first place? */
269 if (!pdev->subordinate)
272 /* Is the flag going to change, or keep the value it already had? */
273 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
275 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
277 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
278 " bad things could happen\n", val ? "" : " not");
284 #ifdef CONFIG_HOTPLUG
285 static DEFINE_MUTEX(pci_remove_rescan_mutex);
286 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
290 struct pci_bus *b = NULL;
292 if (strict_strtoul(buf, 0, &val) < 0)
296 mutex_lock(&pci_remove_rescan_mutex);
297 while ((b = pci_find_next_bus(b)) != NULL)
299 mutex_unlock(&pci_remove_rescan_mutex);
304 struct bus_attribute pci_bus_attrs[] = {
305 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
310 dev_rescan_store(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t count)
314 struct pci_dev *pdev = to_pci_dev(dev);
316 if (strict_strtoul(buf, 0, &val) < 0)
320 mutex_lock(&pci_remove_rescan_mutex);
321 pci_rescan_bus(pdev->bus);
322 mutex_unlock(&pci_remove_rescan_mutex);
327 static void remove_callback(struct device *dev)
329 struct pci_dev *pdev = to_pci_dev(dev);
331 mutex_lock(&pci_remove_rescan_mutex);
332 pci_remove_bus_device(pdev);
333 mutex_unlock(&pci_remove_rescan_mutex);
337 remove_store(struct device *dev, struct device_attribute *dummy,
338 const char *buf, size_t count)
343 if (strict_strtoul(buf, 0, &val) < 0)
346 /* An attribute cannot be unregistered by one of its own methods,
347 * so we have to use this roundabout approach.
350 ret = device_schedule_callback(dev, remove_callback);
357 dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
358 const char *buf, size_t count)
361 struct pci_bus *bus = to_pci_bus(dev);
363 if (strict_strtoul(buf, 0, &val) < 0)
367 mutex_lock(&pci_remove_rescan_mutex);
369 mutex_unlock(&pci_remove_rescan_mutex);
376 struct device_attribute pci_dev_attrs[] = {
380 __ATTR_RO(subsystem_vendor),
381 __ATTR_RO(subsystem_device),
384 __ATTR_RO(local_cpus),
385 __ATTR_RO(local_cpulist),
388 __ATTR_RO(numa_node),
390 __ATTR_RO(dma_mask_bits),
391 __ATTR_RO(consistent_dma_mask_bits),
392 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
393 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
394 broken_parity_status_show,broken_parity_status_store),
395 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
396 #ifdef CONFIG_HOTPLUG
397 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
398 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
403 struct device_attribute pcibus_dev_attrs[] = {
404 #ifdef CONFIG_HOTPLUG
405 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
407 __ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL),
408 __ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL),
413 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
415 struct pci_dev *pdev = to_pci_dev(dev);
417 return sprintf(buf, "%u\n",
418 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
419 IORESOURCE_ROM_SHADOW));
421 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
424 pci_read_config(struct file *filp, struct kobject *kobj,
425 struct bin_attribute *bin_attr,
426 char *buf, loff_t off, size_t count)
428 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
429 unsigned int size = 64;
430 loff_t init_off = off;
431 u8 *data = (u8*) buf;
433 /* Several chips lock up trying to read undefined config space */
434 if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) {
435 size = dev->cfg_size;
436 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
442 if (off + count > size) {
449 if ((off & 1) && size) {
451 pci_user_read_config_byte(dev, off, &val);
452 data[off - init_off] = val;
457 if ((off & 3) && size > 2) {
459 pci_user_read_config_word(dev, off, &val);
460 data[off - init_off] = val & 0xff;
461 data[off - init_off + 1] = (val >> 8) & 0xff;
468 pci_user_read_config_dword(dev, off, &val);
469 data[off - init_off] = val & 0xff;
470 data[off - init_off + 1] = (val >> 8) & 0xff;
471 data[off - init_off + 2] = (val >> 16) & 0xff;
472 data[off - init_off + 3] = (val >> 24) & 0xff;
479 pci_user_read_config_word(dev, off, &val);
480 data[off - init_off] = val & 0xff;
481 data[off - init_off + 1] = (val >> 8) & 0xff;
488 pci_user_read_config_byte(dev, off, &val);
489 data[off - init_off] = val;
498 pci_write_config(struct file* filp, struct kobject *kobj,
499 struct bin_attribute *bin_attr,
500 char *buf, loff_t off, size_t count)
502 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
503 unsigned int size = count;
504 loff_t init_off = off;
505 u8 *data = (u8*) buf;
507 if (off > dev->cfg_size)
509 if (off + count > dev->cfg_size) {
510 size = dev->cfg_size - off;
514 if ((off & 1) && size) {
515 pci_user_write_config_byte(dev, off, data[off - init_off]);
520 if ((off & 3) && size > 2) {
521 u16 val = data[off - init_off];
522 val |= (u16) data[off - init_off + 1] << 8;
523 pci_user_write_config_word(dev, off, val);
529 u32 val = data[off - init_off];
530 val |= (u32) data[off - init_off + 1] << 8;
531 val |= (u32) data[off - init_off + 2] << 16;
532 val |= (u32) data[off - init_off + 3] << 24;
533 pci_user_write_config_dword(dev, off, val);
539 u16 val = data[off - init_off];
540 val |= (u16) data[off - init_off + 1] << 8;
541 pci_user_write_config_word(dev, off, val);
547 pci_user_write_config_byte(dev, off, data[off - init_off]);
556 read_vpd_attr(struct file *filp, struct kobject *kobj,
557 struct bin_attribute *bin_attr,
558 char *buf, loff_t off, size_t count)
560 struct pci_dev *dev =
561 to_pci_dev(container_of(kobj, struct device, kobj));
563 if (off > bin_attr->size)
565 else if (count > bin_attr->size - off)
566 count = bin_attr->size - off;
568 return pci_read_vpd(dev, off, count, buf);
572 write_vpd_attr(struct file *filp, struct kobject *kobj,
573 struct bin_attribute *bin_attr,
574 char *buf, loff_t off, size_t count)
576 struct pci_dev *dev =
577 to_pci_dev(container_of(kobj, struct device, kobj));
579 if (off > bin_attr->size)
581 else if (count > bin_attr->size - off)
582 count = bin_attr->size - off;
584 return pci_write_vpd(dev, off, count, buf);
587 #ifdef HAVE_PCI_LEGACY
589 * pci_read_legacy_io - read byte(s) from legacy I/O port space
590 * @filp: open sysfs file
591 * @kobj: kobject corresponding to file to read from
592 * @bin_attr: struct bin_attribute for this file
593 * @buf: buffer to store results
594 * @off: offset into legacy I/O port space
595 * @count: number of bytes to read
597 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
598 * callback routine (pci_legacy_read).
601 pci_read_legacy_io(struct file *filp, struct kobject *kobj,
602 struct bin_attribute *bin_attr,
603 char *buf, loff_t off, size_t count)
605 struct pci_bus *bus = to_pci_bus(container_of(kobj,
609 /* Only support 1, 2 or 4 byte accesses */
610 if (count != 1 && count != 2 && count != 4)
613 return pci_legacy_read(bus, off, (u32 *)buf, count);
617 * pci_write_legacy_io - write byte(s) to legacy I/O port space
618 * @filp: open sysfs file
619 * @kobj: kobject corresponding to file to read from
620 * @bin_attr: struct bin_attribute for this file
621 * @buf: buffer containing value to be written
622 * @off: offset into legacy I/O port space
623 * @count: number of bytes to write
625 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
626 * callback routine (pci_legacy_write).
629 pci_write_legacy_io(struct file *filp, struct kobject *kobj,
630 struct bin_attribute *bin_attr,
631 char *buf, loff_t off, size_t count)
633 struct pci_bus *bus = to_pci_bus(container_of(kobj,
636 /* Only support 1, 2 or 4 byte accesses */
637 if (count != 1 && count != 2 && count != 4)
640 return pci_legacy_write(bus, off, *(u32 *)buf, count);
644 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
645 * @filp: open sysfs file
646 * @kobj: kobject corresponding to device to be mapped
647 * @attr: struct bin_attribute for this file
648 * @vma: struct vm_area_struct passed to mmap
650 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
651 * legacy memory space (first meg of bus space) into application virtual
655 pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
656 struct bin_attribute *attr,
657 struct vm_area_struct *vma)
659 struct pci_bus *bus = to_pci_bus(container_of(kobj,
663 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
667 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
668 * @filp: open sysfs file
669 * @kobj: kobject corresponding to device to be mapped
670 * @attr: struct bin_attribute for this file
671 * @vma: struct vm_area_struct passed to mmap
673 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
674 * legacy IO space (first meg of bus space) into application virtual
675 * memory space. Returns -ENOSYS if the operation isn't supported
678 pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
679 struct bin_attribute *attr,
680 struct vm_area_struct *vma)
682 struct pci_bus *bus = to_pci_bus(container_of(kobj,
686 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
690 * pci_adjust_legacy_attr - adjustment of legacy file attributes
691 * @b: bus to create files under
692 * @mmap_type: I/O port or memory
694 * Stub implementation. Can be overridden by arch if necessary.
697 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
703 * pci_create_legacy_files - create legacy I/O port and memory files
704 * @b: bus to create files under
706 * Some platforms allow access to legacy I/O port and ISA memory space on
707 * a per-bus basis. This routine creates the files and ties them into
708 * their associated read, write and mmap files from pci-sysfs.c
710 * On error unwind, but don't propagate the error to the caller
711 * as it is ok to set up the PCI bus without these files.
713 void pci_create_legacy_files(struct pci_bus *b)
717 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
722 sysfs_bin_attr_init(b->legacy_io);
723 b->legacy_io->attr.name = "legacy_io";
724 b->legacy_io->size = 0xffff;
725 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
726 b->legacy_io->read = pci_read_legacy_io;
727 b->legacy_io->write = pci_write_legacy_io;
728 b->legacy_io->mmap = pci_mmap_legacy_io;
729 pci_adjust_legacy_attr(b, pci_mmap_io);
730 error = device_create_bin_file(&b->dev, b->legacy_io);
734 /* Allocated above after the legacy_io struct */
735 b->legacy_mem = b->legacy_io + 1;
736 sysfs_bin_attr_init(b->legacy_mem);
737 b->legacy_mem->attr.name = "legacy_mem";
738 b->legacy_mem->size = 1024*1024;
739 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
740 b->legacy_mem->mmap = pci_mmap_legacy_mem;
741 pci_adjust_legacy_attr(b, pci_mmap_mem);
742 error = device_create_bin_file(&b->dev, b->legacy_mem);
749 device_remove_bin_file(&b->dev, b->legacy_io);
754 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
755 "and ISA memory resources to sysfs\n");
759 void pci_remove_legacy_files(struct pci_bus *b)
762 device_remove_bin_file(&b->dev, b->legacy_io);
763 device_remove_bin_file(&b->dev, b->legacy_mem);
764 kfree(b->legacy_io); /* both are allocated here */
767 #endif /* HAVE_PCI_LEGACY */
771 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
772 enum pci_mmap_api mmap_api)
774 unsigned long nr, start, size, pci_start;
776 if (pci_resource_len(pdev, resno) == 0)
778 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
779 start = vma->vm_pgoff;
780 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
781 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
782 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
783 if (start >= pci_start && start < pci_start + size &&
784 start + nr <= pci_start + size)
790 * pci_mmap_resource - map a PCI resource into user memory space
791 * @kobj: kobject for mapping
792 * @attr: struct bin_attribute for the file being mapped
793 * @vma: struct vm_area_struct passed into the mmap
794 * @write_combine: 1 for write_combine mapping
796 * Use the regular PCI mapping routines to map a PCI resource into userspace.
799 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
800 struct vm_area_struct *vma, int write_combine)
802 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
803 struct device, kobj));
804 struct resource *res = attr->private;
805 enum pci_mmap_state mmap_type;
806 resource_size_t start, end;
809 for (i = 0; i < PCI_ROM_RESOURCE; i++)
810 if (res == &pdev->resource[i])
812 if (i >= PCI_ROM_RESOURCE)
815 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
816 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
817 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
818 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
820 (u64)pci_resource_start(pdev, i),
821 (u64)pci_resource_len(pdev, i));
825 /* pci_mmap_page_range() expects the same kind of entry as coming
826 * from /proc/bus/pci/ which is a "user visible" value. If this is
827 * different from the resource itself, arch will do necessary fixup.
829 pci_resource_to_user(pdev, i, res, &start, &end);
830 vma->vm_pgoff += start >> PAGE_SHIFT;
831 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
833 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
836 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
840 pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
841 struct bin_attribute *attr,
842 struct vm_area_struct *vma)
844 return pci_mmap_resource(kobj, attr, vma, 0);
848 pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
849 struct bin_attribute *attr,
850 struct vm_area_struct *vma)
852 return pci_mmap_resource(kobj, attr, vma, 1);
856 pci_resource_io(struct file *filp, struct kobject *kobj,
857 struct bin_attribute *attr, char *buf,
858 loff_t off, size_t count, bool write)
860 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
861 struct device, kobj));
862 struct resource *res = attr->private;
863 unsigned long port = off;
866 for (i = 0; i < PCI_ROM_RESOURCE; i++)
867 if (res == &pdev->resource[i])
869 if (i >= PCI_ROM_RESOURCE)
872 port += pci_resource_start(pdev, i);
874 if (port > pci_resource_end(pdev, i))
877 if (port + count - 1 > pci_resource_end(pdev, i))
883 outb(*(u8 *)buf, port);
885 *(u8 *)buf = inb(port);
889 outw(*(u16 *)buf, port);
891 *(u16 *)buf = inw(port);
895 outl(*(u32 *)buf, port);
897 *(u32 *)buf = inl(port);
904 pci_read_resource_io(struct file *filp, struct kobject *kobj,
905 struct bin_attribute *attr, char *buf,
906 loff_t off, size_t count)
908 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
912 pci_write_resource_io(struct file *filp, struct kobject *kobj,
913 struct bin_attribute *attr, char *buf,
914 loff_t off, size_t count)
916 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
920 * pci_remove_resource_files - cleanup resource files
921 * @pdev: dev to cleanup
923 * If we created resource files for @pdev, remove them from sysfs and
924 * free their resources.
927 pci_remove_resource_files(struct pci_dev *pdev)
931 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
932 struct bin_attribute *res_attr;
934 res_attr = pdev->res_attr[i];
936 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
940 res_attr = pdev->res_attr_wc[i];
942 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
948 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
950 /* allocate attribute structure, piggyback attribute name */
951 int name_len = write_combine ? 13 : 10;
952 struct bin_attribute *res_attr;
955 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
957 char *res_attr_name = (char *)(res_attr + 1);
959 sysfs_bin_attr_init(res_attr);
961 pdev->res_attr_wc[num] = res_attr;
962 sprintf(res_attr_name, "resource%d_wc", num);
963 res_attr->mmap = pci_mmap_resource_wc;
965 pdev->res_attr[num] = res_attr;
966 sprintf(res_attr_name, "resource%d", num);
967 res_attr->mmap = pci_mmap_resource_uc;
969 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
970 res_attr->read = pci_read_resource_io;
971 res_attr->write = pci_write_resource_io;
973 res_attr->attr.name = res_attr_name;
974 res_attr->attr.mode = S_IRUSR | S_IWUSR;
975 res_attr->size = pci_resource_len(pdev, num);
976 res_attr->private = &pdev->resource[num];
977 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
985 * pci_create_resource_files - create resource files in sysfs for @dev
986 * @pdev: dev in question
988 * Walk the resources in @pdev creating files for each resource available.
990 static int pci_create_resource_files(struct pci_dev *pdev)
995 /* Expose the PCI resources from this device as files */
996 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
998 /* skip empty resources */
999 if (!pci_resource_len(pdev, i))
1002 retval = pci_create_attr(pdev, i, 0);
1003 /* for prefetchable resources, create a WC mappable file */
1004 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1005 retval = pci_create_attr(pdev, i, 1);
1008 pci_remove_resource_files(pdev);
1014 #else /* !HAVE_PCI_MMAP */
1015 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1016 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1017 #endif /* HAVE_PCI_MMAP */
1020 * pci_write_rom - used to enable access to the PCI ROM display
1022 * @kobj: kernel object handle
1023 * @bin_attr: struct bin_attribute for this file
1026 * @count: number of byte in input
1028 * writing anything except 0 enables it
1031 pci_write_rom(struct file *filp, struct kobject *kobj,
1032 struct bin_attribute *bin_attr,
1033 char *buf, loff_t off, size_t count)
1035 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1037 if ((off == 0) && (*buf == '0') && (count == 2))
1038 pdev->rom_attr_enabled = 0;
1040 pdev->rom_attr_enabled = 1;
1046 * pci_read_rom - read a PCI ROM
1048 * @kobj: kernel object handle
1049 * @bin_attr: struct bin_attribute for this file
1050 * @buf: where to put the data we read from the ROM
1052 * @count: number of bytes to read
1054 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1055 * device corresponding to @kobj.
1058 pci_read_rom(struct file *filp, struct kobject *kobj,
1059 struct bin_attribute *bin_attr,
1060 char *buf, loff_t off, size_t count)
1062 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1066 if (!pdev->rom_attr_enabled)
1069 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1076 if (off + count > size)
1079 memcpy_fromio(buf, rom + off, count);
1081 pci_unmap_rom(pdev, rom);
1086 static struct bin_attribute pci_config_attr = {
1089 .mode = S_IRUGO | S_IWUSR,
1091 .size = PCI_CFG_SPACE_SIZE,
1092 .read = pci_read_config,
1093 .write = pci_write_config,
1096 static struct bin_attribute pcie_config_attr = {
1099 .mode = S_IRUGO | S_IWUSR,
1101 .size = PCI_CFG_SPACE_EXP_SIZE,
1102 .read = pci_read_config,
1103 .write = pci_write_config,
1106 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
1111 static ssize_t reset_store(struct device *dev,
1112 struct device_attribute *attr, const char *buf,
1115 struct pci_dev *pdev = to_pci_dev(dev);
1117 ssize_t result = strict_strtoul(buf, 0, &val);
1125 result = pci_reset_function(pdev);
1132 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1134 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1137 struct bin_attribute *attr;
1139 /* If the device has VPD, try to expose it in sysfs. */
1141 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1145 sysfs_bin_attr_init(attr);
1146 attr->size = dev->vpd->len;
1147 attr->attr.name = "vpd";
1148 attr->attr.mode = S_IRUSR | S_IWUSR;
1149 attr->read = read_vpd_attr;
1150 attr->write = write_vpd_attr;
1151 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1156 dev->vpd->attr = attr;
1159 /* Active State Power Management */
1160 pcie_aspm_create_sysfs_dev_files(dev);
1162 if (!pci_probe_reset_function(dev)) {
1163 retval = device_create_file(&dev->dev, &reset_attr);
1171 pcie_aspm_remove_sysfs_dev_files(dev);
1172 if (dev->vpd && dev->vpd->attr) {
1173 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1174 kfree(dev->vpd->attr);
1180 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1184 struct bin_attribute *attr;
1186 if (!sysfs_initialized)
1189 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1190 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1192 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1196 retval = pci_create_resource_files(pdev);
1198 goto err_config_file;
1200 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1201 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1202 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1205 /* If the device has a ROM, try to expose it in sysfs. */
1207 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1210 goto err_resource_files;
1212 sysfs_bin_attr_init(attr);
1213 attr->size = rom_size;
1214 attr->attr.name = "rom";
1215 attr->attr.mode = S_IRUSR | S_IWUSR;
1216 attr->read = pci_read_rom;
1217 attr->write = pci_write_rom;
1218 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1221 goto err_resource_files;
1223 pdev->rom_attr = attr;
1226 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1227 retval = device_create_file(&pdev->dev, &vga_attr);
1232 /* add platform-specific attributes */
1233 retval = pcibios_add_platform_entries(pdev);
1237 /* add sysfs entries for various capabilities */
1238 retval = pci_create_capabilities_sysfs(pdev);
1242 pci_create_firmware_label_files(pdev);
1247 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1248 device_remove_file(&pdev->dev, &vga_attr);
1251 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1252 kfree(pdev->rom_attr);
1253 pdev->rom_attr = NULL;
1256 pci_remove_resource_files(pdev);
1258 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1259 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1261 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1266 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1268 if (dev->vpd && dev->vpd->attr) {
1269 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1270 kfree(dev->vpd->attr);
1273 pcie_aspm_remove_sysfs_dev_files(dev);
1274 if (dev->reset_fn) {
1275 device_remove_file(&dev->dev, &reset_attr);
1281 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1282 * @pdev: device whose entries we should free
1284 * Cleanup when @pdev is removed from sysfs.
1286 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1290 if (!sysfs_initialized)
1293 pci_remove_capabilities_sysfs(pdev);
1295 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1296 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1298 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1300 pci_remove_resource_files(pdev);
1302 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1303 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1304 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1307 if (rom_size && pdev->rom_attr) {
1308 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1309 kfree(pdev->rom_attr);
1312 pci_remove_firmware_label_files(pdev);
1316 static int __init pci_sysfs_init(void)
1318 struct pci_dev *pdev = NULL;
1321 sysfs_initialized = 1;
1322 for_each_pci_dev(pdev) {
1323 retval = pci_create_sysfs_dev_files(pdev);
1333 late_initcall(pci_sysfs_init);