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/config.h>
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/stat.h>
22 #include <linux/topology.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
local_cpus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
49 cpumask_t mask
= pcibus_to_cpumask(to_pci_dev(dev
)->bus
);
50 int len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
57 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
59 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
65 if (pci_dev
->subordinate
)
66 max
= DEVICE_COUNT_RESOURCE
;
68 for (i
= 0; i
< max
; i
++) {
69 struct resource
*res
= &pci_dev
->resource
[i
];
70 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
71 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
72 (unsigned long long)start
,
73 (unsigned long long)end
,
74 (unsigned long long)res
->flags
);
79 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
81 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
83 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
84 pci_dev
->vendor
, pci_dev
->device
,
85 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
86 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
87 (u8
)(pci_dev
->class));
90 struct device_attribute pci_dev_attrs
[] = {
94 __ATTR_RO(subsystem_vendor
),
95 __ATTR_RO(subsystem_device
),
98 __ATTR_RO(local_cpus
),
104 pci_read_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
106 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
107 unsigned int size
= 64;
108 loff_t init_off
= off
;
109 u8
*data
= (u8
*) buf
;
111 /* Several chips lock up trying to read undefined config space */
112 if (capable(CAP_SYS_ADMIN
)) {
113 size
= dev
->cfg_size
;
114 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
120 if (off
+ count
> size
) {
127 if ((off
& 1) && size
) {
129 pci_read_config_byte(dev
, off
, &val
);
130 data
[off
- init_off
] = val
;
135 if ((off
& 3) && size
> 2) {
137 pci_read_config_word(dev
, off
, &val
);
138 data
[off
- init_off
] = val
& 0xff;
139 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
146 pci_read_config_dword(dev
, off
, &val
);
147 data
[off
- init_off
] = val
& 0xff;
148 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
149 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
150 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
157 pci_read_config_word(dev
, off
, &val
);
158 data
[off
- init_off
] = val
& 0xff;
159 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
166 pci_read_config_byte(dev
, off
, &val
);
167 data
[off
- init_off
] = val
;
176 pci_write_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
178 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
179 unsigned int size
= count
;
180 loff_t init_off
= off
;
181 u8
*data
= (u8
*) buf
;
183 if (off
> dev
->cfg_size
)
185 if (off
+ count
> dev
->cfg_size
) {
186 size
= dev
->cfg_size
- off
;
190 if ((off
& 1) && size
) {
191 pci_write_config_byte(dev
, off
, data
[off
- init_off
]);
196 if ((off
& 3) && size
> 2) {
197 u16 val
= data
[off
- init_off
];
198 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
199 pci_write_config_word(dev
, off
, val
);
205 u32 val
= data
[off
- init_off
];
206 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
207 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
208 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
209 pci_write_config_dword(dev
, off
, val
);
215 u16 val
= data
[off
- init_off
];
216 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
217 pci_write_config_word(dev
, off
, val
);
223 pci_write_config_byte(dev
, off
, data
[off
- init_off
]);
231 #ifdef HAVE_PCI_LEGACY
233 * pci_read_legacy_io - read byte(s) from legacy I/O port space
234 * @kobj: kobject corresponding to file to read from
235 * @buf: buffer to store results
236 * @off: offset into legacy I/O port space
237 * @count: number of bytes to read
239 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
240 * callback routine (pci_legacy_read).
243 pci_read_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
245 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
249 /* Only support 1, 2 or 4 byte accesses */
250 if (count
!= 1 && count
!= 2 && count
!= 4)
253 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
257 * pci_write_legacy_io - write byte(s) to legacy I/O port space
258 * @kobj: kobject corresponding to file to read from
259 * @buf: buffer containing value to be written
260 * @off: offset into legacy I/O port space
261 * @count: number of bytes to write
263 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
264 * callback routine (pci_legacy_write).
267 pci_write_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
269 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
272 /* Only support 1, 2 or 4 byte accesses */
273 if (count
!= 1 && count
!= 2 && count
!= 4)
276 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
280 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
281 * @kobj: kobject corresponding to device to be mapped
282 * @attr: struct bin_attribute for this file
283 * @vma: struct vm_area_struct passed to mmap
285 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
286 * legacy memory space (first meg of bus space) into application virtual
290 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
291 struct vm_area_struct
*vma
)
293 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
297 return pci_mmap_legacy_page_range(bus
, vma
);
299 #endif /* HAVE_PCI_LEGACY */
303 * pci_mmap_resource - map a PCI resource into user memory space
304 * @kobj: kobject for mapping
305 * @attr: struct bin_attribute for the file being mapped
306 * @vma: struct vm_area_struct passed into the mmap
308 * Use the regular PCI mapping routines to map a PCI resource into userspace.
309 * FIXME: write combining? maybe automatic for prefetchable regions?
312 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
313 struct vm_area_struct
*vma
)
315 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
316 struct device
, kobj
));
317 struct resource
*res
= (struct resource
*)attr
->private;
318 enum pci_mmap_state mmap_type
;
322 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
323 if (res
== &pdev
->resource
[i
])
325 if (i
>= PCI_ROM_RESOURCE
)
328 /* pci_mmap_page_range() expects the same kind of entry as coming
329 * from /proc/bus/pci/ which is a "user visible" value. If this is
330 * different from the resource itself, arch will do necessary fixup.
332 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
333 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
334 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
336 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
340 * pci_create_resource_files - create resource files in sysfs for @dev
341 * @dev: dev in question
343 * Walk the resources in @dev creating files for each resource available.
346 pci_create_resource_files(struct pci_dev
*pdev
)
350 /* Expose the PCI resources from this device as files */
351 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
352 struct bin_attribute
*res_attr
;
354 /* skip empty resources */
355 if (!pci_resource_len(pdev
, i
))
358 /* allocate attribute structure, piggyback attribute name */
359 res_attr
= kcalloc(1, sizeof(*res_attr
) + 10, GFP_ATOMIC
);
361 char *res_attr_name
= (char *)(res_attr
+ 1);
363 pdev
->res_attr
[i
] = res_attr
;
364 sprintf(res_attr_name
, "resource%d", i
);
365 res_attr
->attr
.name
= res_attr_name
;
366 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
367 res_attr
->attr
.owner
= THIS_MODULE
;
368 res_attr
->size
= pci_resource_len(pdev
, i
);
369 res_attr
->mmap
= pci_mmap_resource
;
370 res_attr
->private = &pdev
->resource
[i
];
371 sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
377 * pci_remove_resource_files - cleanup resource files
378 * @dev: dev to cleanup
380 * If we created resource files for @dev, remove them from sysfs and
381 * free their resources.
384 pci_remove_resource_files(struct pci_dev
*pdev
)
388 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
389 struct bin_attribute
*res_attr
;
391 res_attr
= pdev
->res_attr
[i
];
393 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
398 #else /* !HAVE_PCI_MMAP */
399 static inline void pci_create_resource_files(struct pci_dev
*dev
) { return; }
400 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
401 #endif /* HAVE_PCI_MMAP */
404 * pci_write_rom - used to enable access to the PCI ROM display
405 * @kobj: kernel object handle
408 * @count: number of byte in input
410 * writing anything except 0 enables it
413 pci_write_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
415 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
417 if ((off
== 0) && (*buf
== '0') && (count
== 2))
418 pdev
->rom_attr_enabled
= 0;
420 pdev
->rom_attr_enabled
= 1;
426 * pci_read_rom - read a PCI ROM
427 * @kobj: kernel object handle
428 * @buf: where to put the data we read from the ROM
430 * @count: number of bytes to read
432 * Put @count bytes starting at @off into @buf from the ROM in the PCI
433 * device corresponding to @kobj.
436 pci_read_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
438 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
442 if (!pdev
->rom_attr_enabled
)
445 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
452 if (off
+ count
> size
)
455 memcpy_fromio(buf
, rom
+ off
, count
);
457 pci_unmap_rom(pdev
, rom
);
462 static struct bin_attribute pci_config_attr
= {
465 .mode
= S_IRUGO
| S_IWUSR
,
466 .owner
= THIS_MODULE
,
469 .read
= pci_read_config
,
470 .write
= pci_write_config
,
473 static struct bin_attribute pcie_config_attr
= {
476 .mode
= S_IRUGO
| S_IWUSR
,
477 .owner
= THIS_MODULE
,
480 .read
= pci_read_config
,
481 .write
= pci_write_config
,
484 int pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
486 if (!sysfs_initialized
)
489 if (pdev
->cfg_size
< 4096)
490 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
492 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
494 pci_create_resource_files(pdev
);
496 /* If the device has a ROM, try to expose it in sysfs. */
497 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
498 struct bin_attribute
*rom_attr
;
500 rom_attr
= kmalloc(sizeof(*rom_attr
), GFP_ATOMIC
);
502 memset(rom_attr
, 0x00, sizeof(*rom_attr
));
503 pdev
->rom_attr
= rom_attr
;
504 rom_attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
505 rom_attr
->attr
.name
= "rom";
506 rom_attr
->attr
.mode
= S_IRUSR
;
507 rom_attr
->attr
.owner
= THIS_MODULE
;
508 rom_attr
->read
= pci_read_rom
;
509 rom_attr
->write
= pci_write_rom
;
510 sysfs_create_bin_file(&pdev
->dev
.kobj
, rom_attr
);
513 /* add platform-specific attributes */
514 pcibios_add_platform_entries(pdev
);
520 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
521 * @pdev: device whose entries we should free
523 * Cleanup when @pdev is removed from sysfs.
525 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
527 if (pdev
->cfg_size
< 4096)
528 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
530 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
532 pci_remove_resource_files(pdev
);
534 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
535 if (pdev
->rom_attr
) {
536 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
537 kfree(pdev
->rom_attr
);
542 static int __init
pci_sysfs_init(void)
544 struct pci_dev
*pdev
= NULL
;
546 sysfs_initialized
= 1;
547 for_each_pci_dev(pdev
)
548 pci_create_sysfs_dev_files(pdev
);
553 __initcall(pci_sysfs_init
);