2 * drivers/pci/pci-sysfs.c
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
11 * File attributes for PCI devices
13 * Modeled after usb's driverfs.c
18 #include <linux/kernel.h>
19 #include <linux/pci.h>
20 #include <linux/stat.h>
21 #include <linux/topology.h>
23 #include <linux/capability.h>
24 #include <linux/pci-aspm.h>
27 static int sysfs_initialized; /* = 0 */
29 /* show configuration fields */
30 #define pci_config_attr(field, format_string) \
32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 struct pci_dev *pdev; \
36 pdev = to_pci_dev (dev); \
37 return sprintf (buf, format_string, pdev->field); \
40 pci_config_attr(vendor, "0x%04x\n");
41 pci_config_attr(device, "0x%04x\n");
42 pci_config_attr(subsystem_vendor, "0x%04x\n");
43 pci_config_attr(subsystem_device, "0x%04x\n");
44 pci_config_attr(class, "0x%06x\n");
45 pci_config_attr(irq, "%u\n");
47 static ssize_t broken_parity_status_show(struct device *dev,
48 struct device_attribute *attr,
51 struct pci_dev *pdev = to_pci_dev(dev);
52 return sprintf (buf, "%u\n", pdev->broken_parity_status);
55 static ssize_t broken_parity_status_store(struct device *dev,
56 struct device_attribute *attr,
57 const char *buf, size_t count)
59 struct pci_dev *pdev = to_pci_dev(dev);
60 ssize_t consumed = -EINVAL;
62 if ((count > 0) && (*buf == '0' || *buf == '1')) {
63 pdev->broken_parity_status = *buf == '1' ? 1 : 0;
69 static ssize_t local_cpus_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
75 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
76 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
83 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
85 struct pci_dev * pci_dev = to_pci_dev(dev);
89 resource_size_t start, end;
91 if (pci_dev->subordinate)
92 max = DEVICE_COUNT_RESOURCE;
94 for (i = 0; i < max; i++) {
95 struct resource *res = &pci_dev->resource[i];
96 pci_resource_to_user(pci_dev, i, res, &start, &end);
97 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
98 (unsigned long long)start,
99 (unsigned long long)end,
100 (unsigned long long)res->flags);
105 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
107 struct pci_dev *pci_dev = to_pci_dev(dev);
109 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
110 pci_dev->vendor, pci_dev->device,
111 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
112 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
113 (u8)(pci_dev->class));
116 static ssize_t is_enabled_store(struct device *dev,
117 struct device_attribute *attr, const char *buf,
120 ssize_t result = -EINVAL;
121 struct pci_dev *pdev = to_pci_dev(dev);
123 /* this can crash the machine when done on the "wrong" device */
124 if (!capable(CAP_SYS_ADMIN))
128 if (atomic_read(&pdev->enable_cnt) != 0)
129 pci_disable_device(pdev);
132 } else if (*buf == '1')
133 result = pci_enable_device(pdev);
135 return result < 0 ? result : count;
138 static ssize_t is_enabled_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
141 struct pci_dev *pdev;
143 pdev = to_pci_dev (dev);
144 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
149 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
151 return sprintf (buf, "%d\n", dev->numa_node);
156 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
158 struct pci_dev *pdev = to_pci_dev(dev);
160 if (!pdev->subordinate)
163 return sprintf (buf, "%u\n",
164 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
168 msi_bus_store(struct device *dev, struct device_attribute *attr,
169 const char *buf, size_t count)
171 struct pci_dev *pdev = to_pci_dev(dev);
173 /* bad things may happen if the no_msi flag is changed
174 * while some drivers are loaded */
175 if (!capable(CAP_SYS_ADMIN))
178 if (!pdev->subordinate)
182 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
183 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
184 " bad things could happen.\n");
188 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
189 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
190 " bad things could happen.\n");
196 struct device_attribute pci_dev_attrs[] = {
200 __ATTR_RO(subsystem_vendor),
201 __ATTR_RO(subsystem_device),
204 __ATTR_RO(local_cpus),
207 __ATTR_RO(numa_node),
209 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
210 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
211 broken_parity_status_show,broken_parity_status_store),
212 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
217 pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
218 char *buf, loff_t off, size_t count)
220 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
221 unsigned int size = 64;
222 loff_t init_off = off;
223 u8 *data = (u8*) buf;
225 /* Several chips lock up trying to read undefined config space */
226 if (capable(CAP_SYS_ADMIN)) {
227 size = dev->cfg_size;
228 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
234 if (off + count > size) {
241 if ((off & 1) && size) {
243 pci_user_read_config_byte(dev, off, &val);
244 data[off - init_off] = val;
249 if ((off & 3) && size > 2) {
251 pci_user_read_config_word(dev, off, &val);
252 data[off - init_off] = val & 0xff;
253 data[off - init_off + 1] = (val >> 8) & 0xff;
260 pci_user_read_config_dword(dev, off, &val);
261 data[off - init_off] = val & 0xff;
262 data[off - init_off + 1] = (val >> 8) & 0xff;
263 data[off - init_off + 2] = (val >> 16) & 0xff;
264 data[off - init_off + 3] = (val >> 24) & 0xff;
271 pci_user_read_config_word(dev, off, &val);
272 data[off - init_off] = val & 0xff;
273 data[off - init_off + 1] = (val >> 8) & 0xff;
280 pci_user_read_config_byte(dev, off, &val);
281 data[off - init_off] = val;
290 pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
291 char *buf, loff_t off, size_t count)
293 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
294 unsigned int size = count;
295 loff_t init_off = off;
296 u8 *data = (u8*) buf;
298 if (off > dev->cfg_size)
300 if (off + count > dev->cfg_size) {
301 size = dev->cfg_size - off;
305 if ((off & 1) && size) {
306 pci_user_write_config_byte(dev, off, data[off - init_off]);
311 if ((off & 3) && size > 2) {
312 u16 val = data[off - init_off];
313 val |= (u16) data[off - init_off + 1] << 8;
314 pci_user_write_config_word(dev, off, val);
320 u32 val = data[off - init_off];
321 val |= (u32) data[off - init_off + 1] << 8;
322 val |= (u32) data[off - init_off + 2] << 16;
323 val |= (u32) data[off - init_off + 3] << 24;
324 pci_user_write_config_dword(dev, off, val);
330 u16 val = data[off - init_off];
331 val |= (u16) data[off - init_off + 1] << 8;
332 pci_user_write_config_word(dev, off, val);
338 pci_user_write_config_byte(dev, off, data[off - init_off]);
347 pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
348 char *buf, loff_t off, size_t count)
350 struct pci_dev *dev =
351 to_pci_dev(container_of(kobj, struct device, kobj));
355 if (off > bin_attr->size)
357 else if (count > bin_attr->size - off)
358 count = bin_attr->size - off;
362 ret = dev->vpd->ops->read(dev, off, end - off, buf);
373 pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
374 char *buf, loff_t off, size_t count)
376 struct pci_dev *dev =
377 to_pci_dev(container_of(kobj, struct device, kobj));
381 if (off > bin_attr->size)
383 else if (count > bin_attr->size - off)
384 count = bin_attr->size - off;
388 ret = dev->vpd->ops->write(dev, off, end - off, buf);
398 #ifdef HAVE_PCI_LEGACY
400 * pci_read_legacy_io - read byte(s) from legacy I/O port space
401 * @kobj: kobject corresponding to file to read from
402 * @buf: buffer to store results
403 * @off: offset into legacy I/O port space
404 * @count: number of bytes to read
406 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
407 * callback routine (pci_legacy_read).
410 pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
411 char *buf, loff_t off, size_t count)
413 struct pci_bus *bus = to_pci_bus(container_of(kobj,
417 /* Only support 1, 2 or 4 byte accesses */
418 if (count != 1 && count != 2 && count != 4)
421 return pci_legacy_read(bus, off, (u32 *)buf, count);
425 * pci_write_legacy_io - write byte(s) to legacy I/O port space
426 * @kobj: kobject corresponding to file to read from
427 * @buf: buffer containing value to be written
428 * @off: offset into legacy I/O port space
429 * @count: number of bytes to write
431 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
432 * callback routine (pci_legacy_write).
435 pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
436 char *buf, loff_t off, size_t count)
438 struct pci_bus *bus = to_pci_bus(container_of(kobj,
441 /* Only support 1, 2 or 4 byte accesses */
442 if (count != 1 && count != 2 && count != 4)
445 return pci_legacy_write(bus, off, *(u32 *)buf, count);
449 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
450 * @kobj: kobject corresponding to device to be mapped
451 * @attr: struct bin_attribute for this file
452 * @vma: struct vm_area_struct passed to mmap
454 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
455 * legacy memory space (first meg of bus space) into application virtual
459 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
460 struct vm_area_struct *vma)
462 struct pci_bus *bus = to_pci_bus(container_of(kobj,
466 return pci_mmap_legacy_page_range(bus, vma);
468 #endif /* HAVE_PCI_LEGACY */
472 * pci_mmap_resource - map a PCI resource into user memory space
473 * @kobj: kobject for mapping
474 * @attr: struct bin_attribute for the file being mapped
475 * @vma: struct vm_area_struct passed into the mmap
477 * Use the regular PCI mapping routines to map a PCI resource into userspace.
478 * FIXME: write combining? maybe automatic for prefetchable regions?
481 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
482 struct vm_area_struct *vma)
484 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
485 struct device, kobj));
486 struct resource *res = (struct resource *)attr->private;
487 enum pci_mmap_state mmap_type;
488 resource_size_t start, end;
491 for (i = 0; i < PCI_ROM_RESOURCE; i++)
492 if (res == &pdev->resource[i])
494 if (i >= PCI_ROM_RESOURCE)
497 /* pci_mmap_page_range() expects the same kind of entry as coming
498 * from /proc/bus/pci/ which is a "user visible" value. If this is
499 * different from the resource itself, arch will do necessary fixup.
501 pci_resource_to_user(pdev, i, res, &start, &end);
502 vma->vm_pgoff += start >> PAGE_SHIFT;
503 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
505 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
509 * pci_remove_resource_files - cleanup resource files
510 * @dev: dev to cleanup
512 * If we created resource files for @dev, remove them from sysfs and
513 * free their resources.
516 pci_remove_resource_files(struct pci_dev *pdev)
520 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
521 struct bin_attribute *res_attr;
523 res_attr = pdev->res_attr[i];
525 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
532 * pci_create_resource_files - create resource files in sysfs for @dev
533 * @dev: dev in question
535 * Walk the resources in @dev creating files for each resource available.
537 static int pci_create_resource_files(struct pci_dev *pdev)
542 /* Expose the PCI resources from this device as files */
543 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
544 struct bin_attribute *res_attr;
546 /* skip empty resources */
547 if (!pci_resource_len(pdev, i))
550 /* allocate attribute structure, piggyback attribute name */
551 res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
553 char *res_attr_name = (char *)(res_attr + 1);
555 pdev->res_attr[i] = res_attr;
556 sprintf(res_attr_name, "resource%d", i);
557 res_attr->attr.name = res_attr_name;
558 res_attr->attr.mode = S_IRUSR | S_IWUSR;
559 res_attr->size = pci_resource_len(pdev, i);
560 res_attr->mmap = pci_mmap_resource;
561 res_attr->private = &pdev->resource[i];
562 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
564 pci_remove_resource_files(pdev);
573 #else /* !HAVE_PCI_MMAP */
574 static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
575 static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
576 #endif /* HAVE_PCI_MMAP */
579 * pci_write_rom - used to enable access to the PCI ROM display
580 * @kobj: kernel object handle
583 * @count: number of byte in input
585 * writing anything except 0 enables it
588 pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
589 char *buf, loff_t off, size_t count)
591 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
593 if ((off == 0) && (*buf == '0') && (count == 2))
594 pdev->rom_attr_enabled = 0;
596 pdev->rom_attr_enabled = 1;
602 * pci_read_rom - read a PCI ROM
603 * @kobj: kernel object handle
604 * @buf: where to put the data we read from the ROM
606 * @count: number of bytes to read
608 * Put @count bytes starting at @off into @buf from the ROM in the PCI
609 * device corresponding to @kobj.
612 pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
613 char *buf, loff_t off, size_t count)
615 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
619 if (!pdev->rom_attr_enabled)
622 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
629 if (off + count > size)
632 memcpy_fromio(buf, rom + off, count);
634 pci_unmap_rom(pdev, rom);
639 static struct bin_attribute pci_config_attr = {
642 .mode = S_IRUGO | S_IWUSR,
645 .read = pci_read_config,
646 .write = pci_write_config,
649 static struct bin_attribute pcie_config_attr = {
652 .mode = S_IRUGO | S_IWUSR,
655 .read = pci_read_config,
656 .write = pci_write_config,
659 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
664 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
666 struct bin_attribute *attr = NULL;
669 if (!sysfs_initialized)
672 if (pdev->cfg_size < 4096)
673 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
675 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
679 /* If the device has VPD, try to expose it in sysfs. */
681 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
683 pdev->vpd->attr = attr;
684 attr->size = pdev->vpd->ops->get_size(pdev);
685 attr->attr.name = "vpd";
686 attr->attr.mode = S_IRUGO | S_IWUSR;
687 attr->read = pci_read_vpd;
688 attr->write = pci_write_vpd;
689 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
694 goto err_config_file;
698 retval = pci_create_resource_files(pdev);
702 /* If the device has a ROM, try to expose it in sysfs. */
703 if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
704 (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
705 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
707 pdev->rom_attr = attr;
708 attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
709 attr->attr.name = "rom";
710 attr->attr.mode = S_IRUSR;
711 attr->read = pci_read_rom;
712 attr->write = pci_write_rom;
713 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
718 goto err_resource_files;
721 /* add platform-specific attributes */
722 if (pcibios_add_platform_entries(pdev))
725 pcie_aspm_create_sysfs_dev_files(pdev);
730 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
731 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
733 kfree(pdev->rom_attr);
735 pci_remove_resource_files(pdev);
738 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr);
740 kfree(pdev->vpd->attr);
743 if (pdev->cfg_size < 4096)
744 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
746 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
752 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
753 * @pdev: device whose entries we should free
755 * Cleanup when @pdev is removed from sysfs.
757 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
759 if (!sysfs_initialized)
762 pcie_aspm_remove_sysfs_dev_files(pdev);
765 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr);
766 kfree(pdev->vpd->attr);
768 if (pdev->cfg_size < 4096)
769 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
771 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
773 pci_remove_resource_files(pdev);
775 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
776 if (pdev->rom_attr) {
777 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
778 kfree(pdev->rom_attr);
783 static int __init pci_sysfs_init(void)
785 struct pci_dev *pdev = NULL;
788 sysfs_initialized = 1;
789 for_each_pci_dev(pdev) {
790 retval = pci_create_sysfs_dev_files(pdev);
800 late_initcall(pci_sysfs_init);