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>
26 static int sysfs_initialized
; /* = 0 */
28 /* show configuration fields */
29 #define pci_config_attr(field, format_string) \
31 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
33 struct pci_dev *pdev; \
35 pdev = to_pci_dev (dev); \
36 return sprintf (buf, format_string, pdev->field); \
39 pci_config_attr(vendor
, "0x%04x\n");
40 pci_config_attr(device
, "0x%04x\n");
41 pci_config_attr(subsystem_vendor
, "0x%04x\n");
42 pci_config_attr(subsystem_device
, "0x%04x\n");
43 pci_config_attr(class, "0x%06x\n");
44 pci_config_attr(irq
, "%u\n");
45 pci_config_attr(is_enabled
, "%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 is_enabled_store(struct device
*dev
, struct device_attribute
*attr
,
117 const char *buf
, size_t count
)
119 struct pci_dev
*pdev
= to_pci_dev(dev
);
121 /* this can crash the machine when done on the "wrong" device */
122 if (!capable(CAP_SYS_ADMIN
))
126 pci_disable_device(pdev
);
129 pci_enable_device(pdev
);
135 struct device_attribute pci_dev_attrs
[] = {
139 __ATTR_RO(subsystem_vendor
),
140 __ATTR_RO(subsystem_device
),
143 __ATTR_RO(local_cpus
),
145 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
146 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
147 broken_parity_status_show
,broken_parity_status_store
),
152 pci_read_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
154 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
155 unsigned int size
= 64;
156 loff_t init_off
= off
;
157 u8
*data
= (u8
*) buf
;
159 /* Several chips lock up trying to read undefined config space */
160 if (capable(CAP_SYS_ADMIN
)) {
161 size
= dev
->cfg_size
;
162 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
168 if (off
+ count
> size
) {
175 if ((off
& 1) && size
) {
177 pci_user_read_config_byte(dev
, off
, &val
);
178 data
[off
- init_off
] = val
;
183 if ((off
& 3) && size
> 2) {
185 pci_user_read_config_word(dev
, off
, &val
);
186 data
[off
- init_off
] = val
& 0xff;
187 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
194 pci_user_read_config_dword(dev
, off
, &val
);
195 data
[off
- init_off
] = val
& 0xff;
196 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
197 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
198 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
205 pci_user_read_config_word(dev
, off
, &val
);
206 data
[off
- init_off
] = val
& 0xff;
207 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
214 pci_user_read_config_byte(dev
, off
, &val
);
215 data
[off
- init_off
] = val
;
224 pci_write_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
226 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
227 unsigned int size
= count
;
228 loff_t init_off
= off
;
229 u8
*data
= (u8
*) buf
;
231 if (off
> dev
->cfg_size
)
233 if (off
+ count
> dev
->cfg_size
) {
234 size
= dev
->cfg_size
- off
;
238 if ((off
& 1) && size
) {
239 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
244 if ((off
& 3) && size
> 2) {
245 u16 val
= data
[off
- init_off
];
246 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
247 pci_user_write_config_word(dev
, off
, val
);
253 u32 val
= data
[off
- init_off
];
254 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
255 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
256 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
257 pci_user_write_config_dword(dev
, off
, val
);
263 u16 val
= data
[off
- init_off
];
264 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
265 pci_user_write_config_word(dev
, off
, val
);
271 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
279 #ifdef HAVE_PCI_LEGACY
281 * pci_read_legacy_io - read byte(s) from legacy I/O port space
282 * @kobj: kobject corresponding to file to read from
283 * @buf: buffer to store results
284 * @off: offset into legacy I/O port space
285 * @count: number of bytes to read
287 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
288 * callback routine (pci_legacy_read).
291 pci_read_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
293 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
297 /* Only support 1, 2 or 4 byte accesses */
298 if (count
!= 1 && count
!= 2 && count
!= 4)
301 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
305 * pci_write_legacy_io - write byte(s) to legacy I/O port space
306 * @kobj: kobject corresponding to file to read from
307 * @buf: buffer containing value to be written
308 * @off: offset into legacy I/O port space
309 * @count: number of bytes to write
311 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
312 * callback routine (pci_legacy_write).
315 pci_write_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
317 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
320 /* Only support 1, 2 or 4 byte accesses */
321 if (count
!= 1 && count
!= 2 && count
!= 4)
324 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
328 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
329 * @kobj: kobject corresponding to device to be mapped
330 * @attr: struct bin_attribute for this file
331 * @vma: struct vm_area_struct passed to mmap
333 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
334 * legacy memory space (first meg of bus space) into application virtual
338 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
339 struct vm_area_struct
*vma
)
341 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
345 return pci_mmap_legacy_page_range(bus
, vma
);
347 #endif /* HAVE_PCI_LEGACY */
351 * pci_mmap_resource - map a PCI resource into user memory space
352 * @kobj: kobject for mapping
353 * @attr: struct bin_attribute for the file being mapped
354 * @vma: struct vm_area_struct passed into the mmap
356 * Use the regular PCI mapping routines to map a PCI resource into userspace.
357 * FIXME: write combining? maybe automatic for prefetchable regions?
360 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
361 struct vm_area_struct
*vma
)
363 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
364 struct device
, kobj
));
365 struct resource
*res
= (struct resource
*)attr
->private;
366 enum pci_mmap_state mmap_type
;
367 resource_size_t start
, end
;
370 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
371 if (res
== &pdev
->resource
[i
])
373 if (i
>= PCI_ROM_RESOURCE
)
376 /* pci_mmap_page_range() expects the same kind of entry as coming
377 * from /proc/bus/pci/ which is a "user visible" value. If this is
378 * different from the resource itself, arch will do necessary fixup.
380 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
381 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
382 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
384 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
388 * pci_create_resource_files - create resource files in sysfs for @dev
389 * @dev: dev in question
391 * Walk the resources in @dev creating files for each resource available.
394 pci_create_resource_files(struct pci_dev
*pdev
)
398 /* Expose the PCI resources from this device as files */
399 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
400 struct bin_attribute
*res_attr
;
402 /* skip empty resources */
403 if (!pci_resource_len(pdev
, i
))
406 /* allocate attribute structure, piggyback attribute name */
407 res_attr
= kzalloc(sizeof(*res_attr
) + 10, GFP_ATOMIC
);
409 char *res_attr_name
= (char *)(res_attr
+ 1);
411 pdev
->res_attr
[i
] = res_attr
;
412 sprintf(res_attr_name
, "resource%d", i
);
413 res_attr
->attr
.name
= res_attr_name
;
414 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
415 res_attr
->attr
.owner
= THIS_MODULE
;
416 res_attr
->size
= pci_resource_len(pdev
, i
);
417 res_attr
->mmap
= pci_mmap_resource
;
418 res_attr
->private = &pdev
->resource
[i
];
419 sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
425 * pci_remove_resource_files - cleanup resource files
426 * @dev: dev to cleanup
428 * If we created resource files for @dev, remove them from sysfs and
429 * free their resources.
432 pci_remove_resource_files(struct pci_dev
*pdev
)
436 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
437 struct bin_attribute
*res_attr
;
439 res_attr
= pdev
->res_attr
[i
];
441 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
446 #else /* !HAVE_PCI_MMAP */
447 static inline void pci_create_resource_files(struct pci_dev
*dev
) { return; }
448 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
449 #endif /* HAVE_PCI_MMAP */
452 * pci_write_rom - used to enable access to the PCI ROM display
453 * @kobj: kernel object handle
456 * @count: number of byte in input
458 * writing anything except 0 enables it
461 pci_write_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
463 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
465 if ((off
== 0) && (*buf
== '0') && (count
== 2))
466 pdev
->rom_attr_enabled
= 0;
468 pdev
->rom_attr_enabled
= 1;
474 * pci_read_rom - read a PCI ROM
475 * @kobj: kernel object handle
476 * @buf: where to put the data we read from the ROM
478 * @count: number of bytes to read
480 * Put @count bytes starting at @off into @buf from the ROM in the PCI
481 * device corresponding to @kobj.
484 pci_read_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
486 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
490 if (!pdev
->rom_attr_enabled
)
493 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
500 if (off
+ count
> size
)
503 memcpy_fromio(buf
, rom
+ off
, count
);
505 pci_unmap_rom(pdev
, rom
);
510 static struct bin_attribute pci_config_attr
= {
513 .mode
= S_IRUGO
| S_IWUSR
,
514 .owner
= THIS_MODULE
,
517 .read
= pci_read_config
,
518 .write
= pci_write_config
,
521 static struct bin_attribute pcie_config_attr
= {
524 .mode
= S_IRUGO
| S_IWUSR
,
525 .owner
= THIS_MODULE
,
528 .read
= pci_read_config
,
529 .write
= pci_write_config
,
532 int pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
534 if (!sysfs_initialized
)
537 if (pdev
->cfg_size
< 4096)
538 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
540 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
542 pci_create_resource_files(pdev
);
544 /* If the device has a ROM, try to expose it in sysfs. */
545 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
546 struct bin_attribute
*rom_attr
;
548 rom_attr
= kzalloc(sizeof(*rom_attr
), GFP_ATOMIC
);
550 pdev
->rom_attr
= rom_attr
;
551 rom_attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
552 rom_attr
->attr
.name
= "rom";
553 rom_attr
->attr
.mode
= S_IRUSR
;
554 rom_attr
->attr
.owner
= THIS_MODULE
;
555 rom_attr
->read
= pci_read_rom
;
556 rom_attr
->write
= pci_write_rom
;
557 sysfs_create_bin_file(&pdev
->dev
.kobj
, rom_attr
);
560 /* add platform-specific attributes */
561 pcibios_add_platform_entries(pdev
);
567 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
568 * @pdev: device whose entries we should free
570 * Cleanup when @pdev is removed from sysfs.
572 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
574 if (pdev
->cfg_size
< 4096)
575 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
577 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
579 pci_remove_resource_files(pdev
);
581 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
582 if (pdev
->rom_attr
) {
583 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
584 kfree(pdev
->rom_attr
);
589 static int __init
pci_sysfs_init(void)
591 struct pci_dev
*pdev
= NULL
;
593 sysfs_initialized
= 1;
594 for_each_pci_dev(pdev
)
595 pci_create_sysfs_dev_files(pdev
);
600 __initcall(pci_sysfs_init
);