x86, apic: Fix spurious error interrupts triggering on all non-boot APs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / pci-sysfs.c
blobf7bfd6fe6d36d5f5fa146d5a6707f0ea6a2159ed
1 /*
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>
23 #include <linux/mm.h>
24 #include <linux/capability.h>
25 #include <linux/pci-aspm.h>
26 #include "pci.h"
28 static int sysfs_initialized; /* = 0 */
30 /* show configuration fields */
31 #define pci_config_attr(field, format_string) \
32 static ssize_t \
33 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
34 { \
35 struct pci_dev *pdev; \
37 pdev = to_pci_dev (dev); \
38 return sprintf (buf, format_string, pdev->field); \
41 pci_config_attr(vendor, "0x%04x\n");
42 pci_config_attr(device, "0x%04x\n");
43 pci_config_attr(subsystem_vendor, "0x%04x\n");
44 pci_config_attr(subsystem_device, "0x%04x\n");
45 pci_config_attr(class, "0x%06x\n");
46 pci_config_attr(irq, "%u\n");
48 static ssize_t broken_parity_status_show(struct device *dev,
49 struct device_attribute *attr,
50 char *buf)
52 struct pci_dev *pdev = to_pci_dev(dev);
53 return sprintf (buf, "%u\n", pdev->broken_parity_status);
56 static ssize_t broken_parity_status_store(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
60 struct pci_dev *pdev = to_pci_dev(dev);
61 unsigned long val;
63 if (strict_strtoul(buf, 0, &val) < 0)
64 return -EINVAL;
66 pdev->broken_parity_status = !!val;
68 return count;
71 static ssize_t local_cpus_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 const struct cpumask *mask;
75 int len;
77 #ifdef CONFIG_NUMA
78 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
79 cpumask_of_node(dev_to_node(dev));
80 #else
81 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
82 #endif
83 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
84 buf[len++] = '\n';
85 buf[len] = '\0';
86 return len;
90 static ssize_t local_cpulist_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 const struct cpumask *mask;
94 int len;
96 #ifdef CONFIG_NUMA
97 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
98 cpumask_of_node(dev_to_node(dev));
99 #else
100 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
101 #endif
102 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
103 buf[len++] = '\n';
104 buf[len] = '\0';
105 return len;
108 /* show resources */
109 static ssize_t
110 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
112 struct pci_dev * pci_dev = to_pci_dev(dev);
113 char * str = buf;
114 int i;
115 int max;
116 resource_size_t start, end;
118 if (pci_dev->subordinate)
119 max = DEVICE_COUNT_RESOURCE;
120 else
121 max = PCI_BRIDGE_RESOURCES;
123 for (i = 0; i < max; i++) {
124 struct resource *res = &pci_dev->resource[i];
125 pci_resource_to_user(pci_dev, i, res, &start, &end);
126 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
127 (unsigned long long)start,
128 (unsigned long long)end,
129 (unsigned long long)res->flags);
131 return (str - buf);
134 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
136 struct pci_dev *pci_dev = to_pci_dev(dev);
138 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
139 pci_dev->vendor, pci_dev->device,
140 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
141 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
142 (u8)(pci_dev->class));
145 static ssize_t is_enabled_store(struct device *dev,
146 struct device_attribute *attr, const char *buf,
147 size_t count)
149 struct pci_dev *pdev = to_pci_dev(dev);
150 unsigned long val;
151 ssize_t result = strict_strtoul(buf, 0, &val);
153 if (result < 0)
154 return result;
156 /* this can crash the machine when done on the "wrong" device */
157 if (!capable(CAP_SYS_ADMIN))
158 return -EPERM;
160 if (!val) {
161 if (pci_is_enabled(pdev))
162 pci_disable_device(pdev);
163 else
164 result = -EIO;
165 } else
166 result = pci_enable_device(pdev);
168 return result < 0 ? result : count;
171 static ssize_t is_enabled_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
174 struct pci_dev *pdev;
176 pdev = to_pci_dev (dev);
177 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
180 #ifdef CONFIG_NUMA
181 static ssize_t
182 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
184 return sprintf (buf, "%d\n", dev->numa_node);
186 #endif
188 static ssize_t
189 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
191 struct pci_dev *pdev = to_pci_dev(dev);
193 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
196 static ssize_t
197 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
198 char *buf)
200 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
203 static ssize_t
204 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
206 struct pci_dev *pdev = to_pci_dev(dev);
208 if (!pdev->subordinate)
209 return 0;
211 return sprintf (buf, "%u\n",
212 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
215 static ssize_t
216 msi_bus_store(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
219 struct pci_dev *pdev = to_pci_dev(dev);
220 unsigned long val;
222 if (strict_strtoul(buf, 0, &val) < 0)
223 return -EINVAL;
225 /* bad things may happen if the no_msi flag is changed
226 * while some drivers are loaded */
227 if (!capable(CAP_SYS_ADMIN))
228 return -EPERM;
230 /* Maybe pci devices without subordinate busses shouldn't even have this
231 * attribute in the first place? */
232 if (!pdev->subordinate)
233 return count;
235 /* Is the flag going to change, or keep the value it already had? */
236 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
237 !!val) {
238 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
240 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
241 " bad things could happen\n", val ? "" : " not");
244 return count;
247 #ifdef CONFIG_HOTPLUG
248 static DEFINE_MUTEX(pci_remove_rescan_mutex);
249 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
250 size_t count)
252 unsigned long val;
253 struct pci_bus *b = NULL;
255 if (strict_strtoul(buf, 0, &val) < 0)
256 return -EINVAL;
258 if (val) {
259 mutex_lock(&pci_remove_rescan_mutex);
260 while ((b = pci_find_next_bus(b)) != NULL)
261 pci_rescan_bus(b);
262 mutex_unlock(&pci_remove_rescan_mutex);
264 return count;
267 struct bus_attribute pci_bus_attrs[] = {
268 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
269 __ATTR_NULL
272 static ssize_t
273 dev_rescan_store(struct device *dev, struct device_attribute *attr,
274 const char *buf, size_t count)
276 unsigned long val;
277 struct pci_dev *pdev = to_pci_dev(dev);
279 if (strict_strtoul(buf, 0, &val) < 0)
280 return -EINVAL;
282 if (val) {
283 mutex_lock(&pci_remove_rescan_mutex);
284 pci_rescan_bus(pdev->bus);
285 mutex_unlock(&pci_remove_rescan_mutex);
287 return count;
290 static void remove_callback(struct device *dev)
292 struct pci_dev *pdev = to_pci_dev(dev);
294 mutex_lock(&pci_remove_rescan_mutex);
295 pci_remove_bus_device(pdev);
296 mutex_unlock(&pci_remove_rescan_mutex);
299 static ssize_t
300 remove_store(struct device *dev, struct device_attribute *dummy,
301 const char *buf, size_t count)
303 int ret = 0;
304 unsigned long val;
306 if (strict_strtoul(buf, 0, &val) < 0)
307 return -EINVAL;
309 /* An attribute cannot be unregistered by one of its own methods,
310 * so we have to use this roundabout approach.
312 if (val)
313 ret = device_schedule_callback(dev, remove_callback);
314 if (ret)
315 count = ret;
316 return count;
318 #endif
320 struct device_attribute pci_dev_attrs[] = {
321 __ATTR_RO(resource),
322 __ATTR_RO(vendor),
323 __ATTR_RO(device),
324 __ATTR_RO(subsystem_vendor),
325 __ATTR_RO(subsystem_device),
326 __ATTR_RO(class),
327 __ATTR_RO(irq),
328 __ATTR_RO(local_cpus),
329 __ATTR_RO(local_cpulist),
330 __ATTR_RO(modalias),
331 #ifdef CONFIG_NUMA
332 __ATTR_RO(numa_node),
333 #endif
334 __ATTR_RO(dma_mask_bits),
335 __ATTR_RO(consistent_dma_mask_bits),
336 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
337 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
338 broken_parity_status_show,broken_parity_status_store),
339 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
340 #ifdef CONFIG_HOTPLUG
341 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
342 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
343 #endif
344 __ATTR_NULL,
347 static ssize_t
348 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
350 struct pci_dev *pdev = to_pci_dev(dev);
352 return sprintf(buf, "%u\n",
353 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
354 IORESOURCE_ROM_SHADOW));
356 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
358 static ssize_t
359 pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
360 char *buf, loff_t off, size_t count)
362 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
363 unsigned int size = 64;
364 loff_t init_off = off;
365 u8 *data = (u8*) buf;
367 /* Several chips lock up trying to read undefined config space */
368 if (capable(CAP_SYS_ADMIN)) {
369 size = dev->cfg_size;
370 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
371 size = 128;
374 if (off > size)
375 return 0;
376 if (off + count > size) {
377 size -= off;
378 count = size;
379 } else {
380 size = count;
383 if ((off & 1) && size) {
384 u8 val;
385 pci_user_read_config_byte(dev, off, &val);
386 data[off - init_off] = val;
387 off++;
388 size--;
391 if ((off & 3) && size > 2) {
392 u16 val;
393 pci_user_read_config_word(dev, off, &val);
394 data[off - init_off] = val & 0xff;
395 data[off - init_off + 1] = (val >> 8) & 0xff;
396 off += 2;
397 size -= 2;
400 while (size > 3) {
401 u32 val;
402 pci_user_read_config_dword(dev, off, &val);
403 data[off - init_off] = val & 0xff;
404 data[off - init_off + 1] = (val >> 8) & 0xff;
405 data[off - init_off + 2] = (val >> 16) & 0xff;
406 data[off - init_off + 3] = (val >> 24) & 0xff;
407 off += 4;
408 size -= 4;
411 if (size >= 2) {
412 u16 val;
413 pci_user_read_config_word(dev, off, &val);
414 data[off - init_off] = val & 0xff;
415 data[off - init_off + 1] = (val >> 8) & 0xff;
416 off += 2;
417 size -= 2;
420 if (size > 0) {
421 u8 val;
422 pci_user_read_config_byte(dev, off, &val);
423 data[off - init_off] = val;
424 off++;
425 --size;
428 return count;
431 static ssize_t
432 pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
433 char *buf, loff_t off, size_t count)
435 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
436 unsigned int size = count;
437 loff_t init_off = off;
438 u8 *data = (u8*) buf;
440 if (off > dev->cfg_size)
441 return 0;
442 if (off + count > dev->cfg_size) {
443 size = dev->cfg_size - off;
444 count = size;
447 if ((off & 1) && size) {
448 pci_user_write_config_byte(dev, off, data[off - init_off]);
449 off++;
450 size--;
453 if ((off & 3) && size > 2) {
454 u16 val = data[off - init_off];
455 val |= (u16) data[off - init_off + 1] << 8;
456 pci_user_write_config_word(dev, off, val);
457 off += 2;
458 size -= 2;
461 while (size > 3) {
462 u32 val = data[off - init_off];
463 val |= (u32) data[off - init_off + 1] << 8;
464 val |= (u32) data[off - init_off + 2] << 16;
465 val |= (u32) data[off - init_off + 3] << 24;
466 pci_user_write_config_dword(dev, off, val);
467 off += 4;
468 size -= 4;
471 if (size >= 2) {
472 u16 val = data[off - init_off];
473 val |= (u16) data[off - init_off + 1] << 8;
474 pci_user_write_config_word(dev, off, val);
475 off += 2;
476 size -= 2;
479 if (size) {
480 pci_user_write_config_byte(dev, off, data[off - init_off]);
481 off++;
482 --size;
485 return count;
488 static ssize_t
489 read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
490 char *buf, loff_t off, size_t count)
492 struct pci_dev *dev =
493 to_pci_dev(container_of(kobj, struct device, kobj));
495 if (off > bin_attr->size)
496 count = 0;
497 else if (count > bin_attr->size - off)
498 count = bin_attr->size - off;
500 return pci_read_vpd(dev, off, count, buf);
503 static ssize_t
504 write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
505 char *buf, loff_t off, size_t count)
507 struct pci_dev *dev =
508 to_pci_dev(container_of(kobj, struct device, kobj));
510 if (off > bin_attr->size)
511 count = 0;
512 else if (count > bin_attr->size - off)
513 count = bin_attr->size - off;
515 return pci_write_vpd(dev, off, count, buf);
518 #ifdef HAVE_PCI_LEGACY
520 * pci_read_legacy_io - read byte(s) from legacy I/O port space
521 * @kobj: kobject corresponding to file to read from
522 * @bin_attr: struct bin_attribute for this file
523 * @buf: buffer to store results
524 * @off: offset into legacy I/O port space
525 * @count: number of bytes to read
527 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
528 * callback routine (pci_legacy_read).
530 static ssize_t
531 pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
532 char *buf, loff_t off, size_t count)
534 struct pci_bus *bus = to_pci_bus(container_of(kobj,
535 struct device,
536 kobj));
538 /* Only support 1, 2 or 4 byte accesses */
539 if (count != 1 && count != 2 && count != 4)
540 return -EINVAL;
542 return pci_legacy_read(bus, off, (u32 *)buf, count);
546 * pci_write_legacy_io - write byte(s) to legacy I/O port space
547 * @kobj: kobject corresponding to file to read from
548 * @bin_attr: struct bin_attribute for this file
549 * @buf: buffer containing value to be written
550 * @off: offset into legacy I/O port space
551 * @count: number of bytes to write
553 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
554 * callback routine (pci_legacy_write).
556 static ssize_t
557 pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
558 char *buf, loff_t off, size_t count)
560 struct pci_bus *bus = to_pci_bus(container_of(kobj,
561 struct device,
562 kobj));
563 /* Only support 1, 2 or 4 byte accesses */
564 if (count != 1 && count != 2 && count != 4)
565 return -EINVAL;
567 return pci_legacy_write(bus, off, *(u32 *)buf, count);
571 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
572 * @kobj: kobject corresponding to device to be mapped
573 * @attr: struct bin_attribute for this file
574 * @vma: struct vm_area_struct passed to mmap
576 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
577 * legacy memory space (first meg of bus space) into application virtual
578 * memory space.
580 static int
581 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
582 struct vm_area_struct *vma)
584 struct pci_bus *bus = to_pci_bus(container_of(kobj,
585 struct device,
586 kobj));
588 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
592 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
593 * @kobj: kobject corresponding to device to be mapped
594 * @attr: struct bin_attribute for this file
595 * @vma: struct vm_area_struct passed to mmap
597 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
598 * legacy IO space (first meg of bus space) into application virtual
599 * memory space. Returns -ENOSYS if the operation isn't supported
601 static int
602 pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
603 struct vm_area_struct *vma)
605 struct pci_bus *bus = to_pci_bus(container_of(kobj,
606 struct device,
607 kobj));
609 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
613 * pci_adjust_legacy_attr - adjustment of legacy file attributes
614 * @b: bus to create files under
615 * @mmap_type: I/O port or memory
617 * Stub implementation. Can be overridden by arch if necessary.
619 void __weak
620 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
622 return;
626 * pci_create_legacy_files - create legacy I/O port and memory files
627 * @b: bus to create files under
629 * Some platforms allow access to legacy I/O port and ISA memory space on
630 * a per-bus basis. This routine creates the files and ties them into
631 * their associated read, write and mmap files from pci-sysfs.c
633 * On error unwind, but don't propogate the error to the caller
634 * as it is ok to set up the PCI bus without these files.
636 void pci_create_legacy_files(struct pci_bus *b)
638 int error;
640 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
641 GFP_ATOMIC);
642 if (!b->legacy_io)
643 goto kzalloc_err;
645 b->legacy_io->attr.name = "legacy_io";
646 b->legacy_io->size = 0xffff;
647 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
648 b->legacy_io->read = pci_read_legacy_io;
649 b->legacy_io->write = pci_write_legacy_io;
650 b->legacy_io->mmap = pci_mmap_legacy_io;
651 pci_adjust_legacy_attr(b, pci_mmap_io);
652 error = device_create_bin_file(&b->dev, b->legacy_io);
653 if (error)
654 goto legacy_io_err;
656 /* Allocated above after the legacy_io struct */
657 b->legacy_mem = b->legacy_io + 1;
658 b->legacy_mem->attr.name = "legacy_mem";
659 b->legacy_mem->size = 1024*1024;
660 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
661 b->legacy_mem->mmap = pci_mmap_legacy_mem;
662 pci_adjust_legacy_attr(b, pci_mmap_mem);
663 error = device_create_bin_file(&b->dev, b->legacy_mem);
664 if (error)
665 goto legacy_mem_err;
667 return;
669 legacy_mem_err:
670 device_remove_bin_file(&b->dev, b->legacy_io);
671 legacy_io_err:
672 kfree(b->legacy_io);
673 b->legacy_io = NULL;
674 kzalloc_err:
675 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
676 "and ISA memory resources to sysfs\n");
677 return;
680 void pci_remove_legacy_files(struct pci_bus *b)
682 if (b->legacy_io) {
683 device_remove_bin_file(&b->dev, b->legacy_io);
684 device_remove_bin_file(&b->dev, b->legacy_mem);
685 kfree(b->legacy_io); /* both are allocated here */
688 #endif /* HAVE_PCI_LEGACY */
690 #ifdef HAVE_PCI_MMAP
692 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
693 enum pci_mmap_api mmap_api)
695 unsigned long nr, start, size, pci_start;
697 if (pci_resource_len(pdev, resno) == 0)
698 return 0;
699 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
700 start = vma->vm_pgoff;
701 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
702 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
703 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
704 if (start >= pci_start && start < pci_start + size &&
705 start + nr <= pci_start + size)
706 return 1;
707 return 0;
711 * pci_mmap_resource - map a PCI resource into user memory space
712 * @kobj: kobject for mapping
713 * @attr: struct bin_attribute for the file being mapped
714 * @vma: struct vm_area_struct passed into the mmap
715 * @write_combine: 1 for write_combine mapping
717 * Use the regular PCI mapping routines to map a PCI resource into userspace.
719 static int
720 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
721 struct vm_area_struct *vma, int write_combine)
723 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
724 struct device, kobj));
725 struct resource *res = (struct resource *)attr->private;
726 enum pci_mmap_state mmap_type;
727 resource_size_t start, end;
728 int i;
730 for (i = 0; i < PCI_ROM_RESOURCE; i++)
731 if (res == &pdev->resource[i])
732 break;
733 if (i >= PCI_ROM_RESOURCE)
734 return -ENODEV;
736 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
737 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
738 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
739 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
740 pci_name(pdev), i,
741 pci_resource_start(pdev, i), pci_resource_len(pdev, i));
742 return -EINVAL;
745 /* pci_mmap_page_range() expects the same kind of entry as coming
746 * from /proc/bus/pci/ which is a "user visible" value. If this is
747 * different from the resource itself, arch will do necessary fixup.
749 pci_resource_to_user(pdev, i, res, &start, &end);
750 vma->vm_pgoff += start >> PAGE_SHIFT;
751 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
753 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
754 return -EINVAL;
756 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
759 static int
760 pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
761 struct vm_area_struct *vma)
763 return pci_mmap_resource(kobj, attr, vma, 0);
766 static int
767 pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
768 struct vm_area_struct *vma)
770 return pci_mmap_resource(kobj, attr, vma, 1);
774 * pci_remove_resource_files - cleanup resource files
775 * @pdev: dev to cleanup
777 * If we created resource files for @pdev, remove them from sysfs and
778 * free their resources.
780 static void
781 pci_remove_resource_files(struct pci_dev *pdev)
783 int i;
785 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
786 struct bin_attribute *res_attr;
788 res_attr = pdev->res_attr[i];
789 if (res_attr) {
790 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
791 kfree(res_attr);
794 res_attr = pdev->res_attr_wc[i];
795 if (res_attr) {
796 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
797 kfree(res_attr);
802 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
804 /* allocate attribute structure, piggyback attribute name */
805 int name_len = write_combine ? 13 : 10;
806 struct bin_attribute *res_attr;
807 int retval;
809 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
810 if (res_attr) {
811 char *res_attr_name = (char *)(res_attr + 1);
813 if (write_combine) {
814 pdev->res_attr_wc[num] = res_attr;
815 sprintf(res_attr_name, "resource%d_wc", num);
816 res_attr->mmap = pci_mmap_resource_wc;
817 } else {
818 pdev->res_attr[num] = res_attr;
819 sprintf(res_attr_name, "resource%d", num);
820 res_attr->mmap = pci_mmap_resource_uc;
822 res_attr->attr.name = res_attr_name;
823 res_attr->attr.mode = S_IRUSR | S_IWUSR;
824 res_attr->size = pci_resource_len(pdev, num);
825 res_attr->private = &pdev->resource[num];
826 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
827 } else
828 retval = -ENOMEM;
830 return retval;
834 * pci_create_resource_files - create resource files in sysfs for @dev
835 * @pdev: dev in question
837 * Walk the resources in @pdev creating files for each resource available.
839 static int pci_create_resource_files(struct pci_dev *pdev)
841 int i;
842 int retval;
844 /* Expose the PCI resources from this device as files */
845 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
847 /* skip empty resources */
848 if (!pci_resource_len(pdev, i))
849 continue;
851 retval = pci_create_attr(pdev, i, 0);
852 /* for prefetchable resources, create a WC mappable file */
853 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
854 retval = pci_create_attr(pdev, i, 1);
856 if (retval) {
857 pci_remove_resource_files(pdev);
858 return retval;
861 return 0;
863 #else /* !HAVE_PCI_MMAP */
864 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
865 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
866 #endif /* HAVE_PCI_MMAP */
869 * pci_write_rom - used to enable access to the PCI ROM display
870 * @kobj: kernel object handle
871 * @bin_attr: struct bin_attribute for this file
872 * @buf: user input
873 * @off: file offset
874 * @count: number of byte in input
876 * writing anything except 0 enables it
878 static ssize_t
879 pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
880 char *buf, loff_t off, size_t count)
882 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
884 if ((off == 0) && (*buf == '0') && (count == 2))
885 pdev->rom_attr_enabled = 0;
886 else
887 pdev->rom_attr_enabled = 1;
889 return count;
893 * pci_read_rom - read a PCI ROM
894 * @kobj: kernel object handle
895 * @bin_attr: struct bin_attribute for this file
896 * @buf: where to put the data we read from the ROM
897 * @off: file offset
898 * @count: number of bytes to read
900 * Put @count bytes starting at @off into @buf from the ROM in the PCI
901 * device corresponding to @kobj.
903 static ssize_t
904 pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
905 char *buf, loff_t off, size_t count)
907 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
908 void __iomem *rom;
909 size_t size;
911 if (!pdev->rom_attr_enabled)
912 return -EINVAL;
914 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
915 if (!rom || !size)
916 return -EIO;
918 if (off >= size)
919 count = 0;
920 else {
921 if (off + count > size)
922 count = size - off;
924 memcpy_fromio(buf, rom + off, count);
926 pci_unmap_rom(pdev, rom);
928 return count;
931 static struct bin_attribute pci_config_attr = {
932 .attr = {
933 .name = "config",
934 .mode = S_IRUGO | S_IWUSR,
936 .size = PCI_CFG_SPACE_SIZE,
937 .read = pci_read_config,
938 .write = pci_write_config,
941 static struct bin_attribute pcie_config_attr = {
942 .attr = {
943 .name = "config",
944 .mode = S_IRUGO | S_IWUSR,
946 .size = PCI_CFG_SPACE_EXP_SIZE,
947 .read = pci_read_config,
948 .write = pci_write_config,
951 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
953 return 0;
956 static ssize_t reset_store(struct device *dev,
957 struct device_attribute *attr, const char *buf,
958 size_t count)
960 struct pci_dev *pdev = to_pci_dev(dev);
961 unsigned long val;
962 ssize_t result = strict_strtoul(buf, 0, &val);
964 if (result < 0)
965 return result;
967 if (val != 1)
968 return -EINVAL;
970 result = pci_reset_function(pdev);
971 if (result < 0)
972 return result;
974 return count;
977 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
979 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
981 int retval;
982 struct bin_attribute *attr;
984 /* If the device has VPD, try to expose it in sysfs. */
985 if (dev->vpd) {
986 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
987 if (!attr)
988 return -ENOMEM;
990 attr->size = dev->vpd->len;
991 attr->attr.name = "vpd";
992 attr->attr.mode = S_IRUSR | S_IWUSR;
993 attr->read = read_vpd_attr;
994 attr->write = write_vpd_attr;
995 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
996 if (retval) {
997 kfree(attr);
998 return retval;
1000 dev->vpd->attr = attr;
1003 /* Active State Power Management */
1004 pcie_aspm_create_sysfs_dev_files(dev);
1006 if (!pci_probe_reset_function(dev)) {
1007 retval = device_create_file(&dev->dev, &reset_attr);
1008 if (retval)
1009 goto error;
1010 dev->reset_fn = 1;
1012 return 0;
1014 error:
1015 pcie_aspm_remove_sysfs_dev_files(dev);
1016 if (dev->vpd && dev->vpd->attr) {
1017 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1018 kfree(dev->vpd->attr);
1021 return retval;
1024 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1026 int retval;
1027 int rom_size = 0;
1028 struct bin_attribute *attr;
1030 if (!sysfs_initialized)
1031 return -EACCES;
1033 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1034 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1035 else
1036 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1037 if (retval)
1038 goto err;
1040 retval = pci_create_resource_files(pdev);
1041 if (retval)
1042 goto err_config_file;
1044 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1045 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1046 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1047 rom_size = 0x20000;
1049 /* If the device has a ROM, try to expose it in sysfs. */
1050 if (rom_size) {
1051 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1052 if (!attr) {
1053 retval = -ENOMEM;
1054 goto err_resource_files;
1056 attr->size = rom_size;
1057 attr->attr.name = "rom";
1058 attr->attr.mode = S_IRUSR;
1059 attr->read = pci_read_rom;
1060 attr->write = pci_write_rom;
1061 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1062 if (retval) {
1063 kfree(attr);
1064 goto err_resource_files;
1066 pdev->rom_attr = attr;
1069 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1070 retval = device_create_file(&pdev->dev, &vga_attr);
1071 if (retval)
1072 goto err_rom_file;
1075 /* add platform-specific attributes */
1076 retval = pcibios_add_platform_entries(pdev);
1077 if (retval)
1078 goto err_vga_file;
1080 /* add sysfs entries for various capabilities */
1081 retval = pci_create_capabilities_sysfs(pdev);
1082 if (retval)
1083 goto err_vga_file;
1085 return 0;
1087 err_vga_file:
1088 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1089 device_remove_file(&pdev->dev, &vga_attr);
1090 err_rom_file:
1091 if (rom_size) {
1092 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1093 kfree(pdev->rom_attr);
1094 pdev->rom_attr = NULL;
1096 err_resource_files:
1097 pci_remove_resource_files(pdev);
1098 err_config_file:
1099 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1100 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1101 else
1102 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1103 err:
1104 return retval;
1107 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1109 if (dev->vpd && dev->vpd->attr) {
1110 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1111 kfree(dev->vpd->attr);
1114 pcie_aspm_remove_sysfs_dev_files(dev);
1115 if (dev->reset_fn) {
1116 device_remove_file(&dev->dev, &reset_attr);
1117 dev->reset_fn = 0;
1122 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1123 * @pdev: device whose entries we should free
1125 * Cleanup when @pdev is removed from sysfs.
1127 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1129 int rom_size = 0;
1131 if (!sysfs_initialized)
1132 return;
1134 pci_remove_capabilities_sysfs(pdev);
1136 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1137 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1138 else
1139 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1141 pci_remove_resource_files(pdev);
1143 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1144 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1145 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1146 rom_size = 0x20000;
1148 if (rom_size && pdev->rom_attr) {
1149 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1150 kfree(pdev->rom_attr);
1154 static int __init pci_sysfs_init(void)
1156 struct pci_dev *pdev = NULL;
1157 int retval;
1159 sysfs_initialized = 1;
1160 for_each_pci_dev(pdev) {
1161 retval = pci_create_sysfs_dev_files(pdev);
1162 if (retval) {
1163 pci_dev_put(pdev);
1164 return retval;
1168 return 0;
1171 late_initcall(pci_sysfs_init);