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, 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
, 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
, 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 struct device_attribute pci_dev_attrs
[] = {
80 __ATTR_RO(subsystem_vendor
),
81 __ATTR_RO(subsystem_device
),
84 __ATTR_RO(local_cpus
),
89 pci_read_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
91 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
92 unsigned int size
= 64;
93 loff_t init_off
= off
;
95 /* Several chips lock up trying to read undefined config space */
96 if (capable(CAP_SYS_ADMIN
)) {
98 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
104 if (off
+ count
> size
) {
113 pci_read_config_byte(dev
, off
, &val
);
114 buf
[off
- init_off
] = val
;
122 pci_read_config_dword(dev
, off
, &val
);
123 buf
[off
- init_off
] = val
& 0xff;
124 buf
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
125 buf
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
126 buf
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
133 pci_read_config_byte(dev
, off
, &val
);
134 buf
[off
- init_off
] = val
;
143 pci_write_config(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
145 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
146 unsigned int size
= count
;
147 loff_t init_off
= off
;
149 if (off
> dev
->cfg_size
)
151 if (off
+ count
> dev
->cfg_size
) {
152 size
= dev
->cfg_size
- off
;
157 pci_write_config_byte(dev
, off
, buf
[off
- init_off
]);
164 unsigned int val
= buf
[off
- init_off
];
165 val
|= (unsigned int) buf
[off
- init_off
+ 1] << 8;
166 val
|= (unsigned int) buf
[off
- init_off
+ 2] << 16;
167 val
|= (unsigned int) buf
[off
- init_off
+ 3] << 24;
168 pci_write_config_dword(dev
, off
, val
);
174 pci_write_config_byte(dev
, off
, buf
[off
- init_off
]);
182 #ifdef HAVE_PCI_LEGACY
184 * pci_read_legacy_io - read byte(s) from legacy I/O port space
185 * @kobj: kobject corresponding to file to read from
186 * @buf: buffer to store results
187 * @off: offset into legacy I/O port space
188 * @count: number of bytes to read
190 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
191 * callback routine (pci_legacy_read).
194 pci_read_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
196 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
200 /* Only support 1, 2 or 4 byte accesses */
201 if (count
!= 1 && count
!= 2 && count
!= 4)
204 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
208 * pci_write_legacy_io - write byte(s) to legacy I/O port space
209 * @kobj: kobject corresponding to file to read from
210 * @buf: buffer containing value to be written
211 * @off: offset into legacy I/O port space
212 * @count: number of bytes to write
214 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
215 * callback routine (pci_legacy_write).
218 pci_write_legacy_io(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
220 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
223 /* Only support 1, 2 or 4 byte accesses */
224 if (count
!= 1 && count
!= 2 && count
!= 4)
227 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
231 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
232 * @kobj: kobject corresponding to device to be mapped
233 * @attr: struct bin_attribute for this file
234 * @vma: struct vm_area_struct passed to mmap
236 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
237 * legacy memory space (first meg of bus space) into application virtual
241 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
242 struct vm_area_struct
*vma
)
244 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
248 return pci_mmap_legacy_page_range(bus
, vma
);
250 #endif /* HAVE_PCI_LEGACY */
254 * pci_mmap_resource - map a PCI resource into user memory space
255 * @kobj: kobject for mapping
256 * @attr: struct bin_attribute for the file being mapped
257 * @vma: struct vm_area_struct passed into the mmap
259 * Use the regular PCI mapping routines to map a PCI resource into userspace.
260 * FIXME: write combining? maybe automatic for prefetchable regions?
263 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
264 struct vm_area_struct
*vma
)
266 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
267 struct device
, kobj
));
268 struct resource
*res
= (struct resource
*)attr
->private;
269 enum pci_mmap_state mmap_type
;
271 vma
->vm_pgoff
+= res
->start
>> PAGE_SHIFT
;
272 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
274 return pci_mmap_page_range(pdev
, vma
, mmap_type
, 0);
278 * pci_create_resource_files - create resource files in sysfs for @dev
279 * @dev: dev in question
281 * Walk the resources in @dev creating files for each resource available.
284 pci_create_resource_files(struct pci_dev
*pdev
)
288 /* Expose the PCI resources from this device as files */
289 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
290 struct bin_attribute
*res_attr
;
292 /* skip empty resources */
293 if (!pci_resource_len(pdev
, i
))
296 res_attr
= kmalloc(sizeof(*res_attr
) + 10, GFP_ATOMIC
);
298 memset(res_attr
, 0, sizeof(*res_attr
) + 10);
299 pdev
->res_attr
[i
] = res_attr
;
300 /* Allocated above after the res_attr struct */
301 res_attr
->attr
.name
= (char *)(res_attr
+ 1);
302 sprintf(res_attr
->attr
.name
, "resource%d", i
);
303 res_attr
->size
= pci_resource_len(pdev
, i
);
304 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
305 res_attr
->attr
.owner
= THIS_MODULE
;
306 res_attr
->mmap
= pci_mmap_resource
;
307 res_attr
->private = &pdev
->resource
[i
];
308 sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
314 * pci_remove_resource_files - cleanup resource files
315 * @dev: dev to cleanup
317 * If we created resource files for @dev, remove them from sysfs and
318 * free their resources.
321 pci_remove_resource_files(struct pci_dev
*pdev
)
325 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
326 struct bin_attribute
*res_attr
;
328 res_attr
= pdev
->res_attr
[i
];
330 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
335 #else /* !HAVE_PCI_MMAP */
336 static inline void pci_create_resource_files(struct pci_dev
*dev
) { return; }
337 static inline void pci_remove_resource_files(struct pci_dev
*dev
) { return; }
338 #endif /* HAVE_PCI_MMAP */
341 * pci_write_rom - used to enable access to the PCI ROM display
342 * @kobj: kernel object handle
345 * @count: number of byte in input
347 * writing anything except 0 enables it
350 pci_write_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
352 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
354 if ((off
== 0) && (*buf
== '0') && (count
== 2))
355 pdev
->rom_attr_enabled
= 0;
357 pdev
->rom_attr_enabled
= 1;
363 * pci_read_rom - read a PCI ROM
364 * @kobj: kernel object handle
365 * @buf: where to put the data we read from the ROM
367 * @count: number of bytes to read
369 * Put @count bytes starting at @off into @buf from the ROM in the PCI
370 * device corresponding to @kobj.
373 pci_read_rom(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
375 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
379 if (!pdev
->rom_attr_enabled
)
382 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
389 if (off
+ count
> size
)
392 memcpy_fromio(buf
, rom
+ off
, count
);
394 pci_unmap_rom(pdev
, rom
);
399 static struct bin_attribute pci_config_attr
= {
402 .mode
= S_IRUGO
| S_IWUSR
,
403 .owner
= THIS_MODULE
,
406 .read
= pci_read_config
,
407 .write
= pci_write_config
,
410 static struct bin_attribute pcie_config_attr
= {
413 .mode
= S_IRUGO
| S_IWUSR
,
414 .owner
= THIS_MODULE
,
417 .read
= pci_read_config
,
418 .write
= pci_write_config
,
421 int pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
423 if (!sysfs_initialized
)
426 if (pdev
->cfg_size
< 4096)
427 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
429 sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
431 pci_create_resource_files(pdev
);
433 /* If the device has a ROM, try to expose it in sysfs. */
434 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
435 struct bin_attribute
*rom_attr
;
437 rom_attr
= kmalloc(sizeof(*rom_attr
), GFP_ATOMIC
);
439 memset(rom_attr
, 0x00, sizeof(*rom_attr
));
440 pdev
->rom_attr
= rom_attr
;
441 rom_attr
->size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
442 rom_attr
->attr
.name
= "rom";
443 rom_attr
->attr
.mode
= S_IRUSR
;
444 rom_attr
->attr
.owner
= THIS_MODULE
;
445 rom_attr
->read
= pci_read_rom
;
446 rom_attr
->write
= pci_write_rom
;
447 sysfs_create_bin_file(&pdev
->dev
.kobj
, rom_attr
);
450 /* add platform-specific attributes */
451 pcibios_add_platform_entries(pdev
);
457 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
458 * @pdev: device whose entries we should free
460 * Cleanup when @pdev is removed from sysfs.
462 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
464 if (pdev
->cfg_size
< 4096)
465 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
467 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
469 pci_remove_resource_files(pdev
);
471 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
)) {
472 if (pdev
->rom_attr
) {
473 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
474 kfree(pdev
->rom_attr
);
479 static int __init
pci_sysfs_init(void)
481 struct pci_dev
*pdev
= NULL
;
483 sysfs_initialized
= 1;
484 for_each_pci_dev(pdev
)
485 pci_create_sysfs_dev_files(pdev
);
490 __initcall(pci_sysfs_init
);