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
);
64 if (pci_dev
->subordinate
)
65 max
= DEVICE_COUNT_RESOURCE
;
67 for (i
= 0; i
< max
; i
++) {
68 str
+= sprintf(str
,"0x%016lx 0x%016lx 0x%016lx\n",
69 pci_resource_start(pci_dev
,i
),
70 pci_resource_end(pci_dev
,i
),
71 pci_resource_flags(pci_dev
,i
));
76 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
78 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
80 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
81 pci_dev
->vendor
, pci_dev
->device
,
82 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
83 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
84 (u8
)(pci_dev
->class));
87 struct device_attribute pci_dev_attrs
[] = {
91 __ATTR_RO(subsystem_vendor
),
92 __ATTR_RO(subsystem_device
),
95 __ATTR_RO(local_cpus
),
101 pci_read_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
103 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
104 unsigned int size
= 64;
105 loff_t init_off
= off
;
106 u8
*data
= (u8
*) buf
;
108 /* Several chips lock up trying to read undefined config space */
109 if (capable(CAP_SYS_ADMIN
)) {
110 size
= dev
->cfg_size
;
111 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
117 if (off
+ count
> size
) {
124 if ((off
& 1) && size
) {
126 pci_read_config_byte(dev
, off
, &val
);
127 data
[off
- init_off
] = val
;
132 if ((off
& 3) && size
> 2) {
134 pci_read_config_word(dev
, off
, &val
);
135 data
[off
- init_off
] = val
& 0xff;
136 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
143 pci_read_config_dword(dev
, off
, &val
);
144 data
[off
- init_off
] = val
& 0xff;
145 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
146 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
147 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
154 pci_read_config_word(dev
, off
, &val
);
155 data
[off
- init_off
] = val
& 0xff;
156 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
163 pci_read_config_byte(dev
, off
, &val
);
164 data
[off
- init_off
] = val
;
173 pci_write_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
175 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
176 unsigned int size
= count
;
177 loff_t init_off
= off
;
178 u8
*data
= (u8
*) buf
;
180 if (off
> dev
->cfg_size
)
182 if (off
+ count
> dev
->cfg_size
) {
183 size
= dev
->cfg_size
- off
;
187 if ((off
& 1) && size
) {
188 pci_write_config_byte(dev
, off
, data
[off
- init_off
]);
193 if ((off
& 3) && size
> 2) {
194 u16 val
= data
[off
- init_off
];
195 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
196 pci_write_config_word(dev
, off
, val
);
202 u32 val
= data
[off
- init_off
];
203 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
204 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
205 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
206 pci_write_config_dword(dev
, off
, val
);
212 u16 val
= data
[off
- init_off
];
213 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
214 pci_write_config_word(dev
, off
, val
);
220 pci_write_config_byte(dev
, off
, data
[off
- init_off
]);
228 #ifdef HAVE_PCI_LEGACY
230 * pci_read_legacy_io - read byte(s) from legacy I/O port space
231 * @kobj: kobject corresponding to file to read from
232 * @buf: buffer to store results
233 * @off: offset into legacy I/O port space
234 * @count: number of bytes to read
236 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
237 * callback routine (pci_legacy_read).
240 pci_read_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
242 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
246 /* Only support 1, 2 or 4 byte accesses */
247 if (count
!= 1 && count
!= 2 && count
!= 4)
250 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
254 * pci_write_legacy_io - write byte(s) to legacy I/O port space
255 * @kobj: kobject corresponding to file to read from
256 * @buf: buffer containing value to be written
257 * @off: offset into legacy I/O port space
258 * @count: number of bytes to write
260 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
261 * callback routine (pci_legacy_write).
264 pci_write_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
266 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
269 /* Only support 1, 2 or 4 byte accesses */
270 if (count
!= 1 && count
!= 2 && count
!= 4)
273 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
277 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
278 * @kobj: kobject corresponding to device to be mapped
279 * @attr: struct bin_attribute for this file
280 * @vma: struct vm_area_struct passed to mmap
282 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
283 * legacy memory space (first meg of bus space) into application virtual
287 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
288 struct vm_area_struct
*vma
)
290 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
294 return pci_mmap_legacy_page_range(bus
, vma
);
296 #endif /* HAVE_PCI_LEGACY */
300 * pci_mmap_resource - map a PCI resource into user memory space
301 * @kobj: kobject for mapping
302 * @attr: struct bin_attribute for the file being mapped
303 * @vma: struct vm_area_struct passed into the mmap
305 * Use the regular PCI mapping routines to map a PCI resource into userspace.
306 * FIXME: write combining? maybe automatic for prefetchable regions?
309 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
310 struct vm_area_struct
*vma
)
312 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
313 struct device
, kobj
));
314 struct resource
*res
= (struct resource
*)attr
->private;
315 enum pci_mmap_state mmap_type
;
317 vma
->vm_pgoff
+= res
->start
>> PAGE_SHIFT
;
318 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
320 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
324 * pci_create_resource_files - create resource files in sysfs for @dev
325 * @dev: dev in question
327 * Walk the resources in @dev creating files for each resource available.
330 pci_create_resource_files(struct pci_dev
*pdev
)
334 /* Expose the PCI resources from this device as files */
335 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
336 struct bin_attribute
*res_attr
;
338 /* skip empty resources */
339 if (!pci_resource_len(pdev
, i
))
342 /* allocate attribute structure, piggyback attribute name */
343 res_attr
= kcalloc(1, sizeof(*res_attr
) + 10, GFP_ATOMIC
);
345 char *res_attr_name
= (char *)(res_attr
+ 1);
347 pdev
->res_attr
[i
] = res_attr
;
348 sprintf(res_attr_name
, "resource%d", i
);
349 res_attr
->attr
.name
= res_attr_name
;
350 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
351 res_attr
->attr
.owner
= THIS_MODULE
;
352 res_attr
->size
= pci_resource_len(pdev
, i
);
353 res_attr
->mmap
= pci_mmap_resource
;
354 res_attr
->private = &pdev
->resource
[i
];
355 sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
361 * pci_remove_resource_files - cleanup resource files
362 * @dev: dev to cleanup
364 * If we created resource files for @dev, remove them from sysfs and
365 * free their resources.
368 pci_remove_resource_files(struct pci_dev
*pdev
)
372 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
373 struct bin_attribute
*res_attr
;
375 res_attr
= pdev
->res_attr
[i
];
377 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
382 #else /* !HAVE_PCI_MMAP */
383 static inline void pci_create_resource_files(struct pci_dev
*dev
) { return; }
384 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
385 #endif /* HAVE_PCI_MMAP */
388 * pci_write_rom - used to enable access to the PCI ROM display
389 * @kobj: kernel object handle
392 * @count: number of byte in input
394 * writing anything except 0 enables it
397 pci_write_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
399 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
401 if ((off
== 0) && (*buf
== '0') && (count
== 2))
402 pdev
->rom_attr_enabled
= 0;
404 pdev
->rom_attr_enabled
= 1;
410 * pci_read_rom - read a PCI ROM
411 * @kobj: kernel object handle
412 * @buf: where to put the data we read from the ROM
414 * @count: number of bytes to read
416 * Put @count bytes starting at @off into @buf from the ROM in the PCI
417 * device corresponding to @kobj.
420 pci_read_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
422 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
426 if (!pdev
->rom_attr_enabled
)
429 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
436 if (off
+ count
> size
)
439 memcpy_fromio(buf
, rom
+ off
, count
);
441 pci_unmap_rom(pdev
, rom
);
446 static struct bin_attribute pci_config_attr
= {
449 .mode
= S_IRUGO
| S_IWUSR
,
450 .owner
= THIS_MODULE
,
453 .read
= pci_read_config
,
454 .write
= pci_write_config
,
457 static struct bin_attribute pcie_config_attr
= {
460 .mode
= S_IRUGO
| S_IWUSR
,
461 .owner
= THIS_MODULE
,
464 .read
= pci_read_config
,
465 .write
= pci_write_config
,
468 int pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
470 if (!sysfs_initialized
)
473 if (pdev
->cfg_size
< 4096)
474 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
476 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
478 pci_create_resource_files(pdev
);
480 /* If the device has a ROM, try to expose it in sysfs. */
481 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
482 struct bin_attribute
*rom_attr
;
484 rom_attr
= kmalloc(sizeof(*rom_attr
), GFP_ATOMIC
);
486 memset(rom_attr
, 0x00, sizeof(*rom_attr
));
487 pdev
->rom_attr
= rom_attr
;
488 rom_attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
489 rom_attr
->attr
.name
= "rom";
490 rom_attr
->attr
.mode
= S_IRUSR
;
491 rom_attr
->attr
.owner
= THIS_MODULE
;
492 rom_attr
->read
= pci_read_rom
;
493 rom_attr
->write
= pci_write_rom
;
494 sysfs_create_bin_file(&pdev
->dev
.kobj
, rom_attr
);
497 /* add platform-specific attributes */
498 pcibios_add_platform_entries(pdev
);
504 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
505 * @pdev: device whose entries we should free
507 * Cleanup when @pdev is removed from sysfs.
509 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
511 if (pdev
->cfg_size
< 4096)
512 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
514 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
516 pci_remove_resource_files(pdev
);
518 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
519 if (pdev
->rom_attr
) {
520 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
521 kfree(pdev
->rom_attr
);
526 static int __init
pci_sysfs_init(void)
528 struct pci_dev
*pdev
= NULL
;
530 sysfs_initialized
= 1;
531 for_each_pci_dev(pdev
)
532 pci_create_sysfs_dev_files(pdev
);
537 __initcall(pci_sysfs_init
);