blob: a15f94072a6f554a4e43b70db5c24d7e1b64240a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-sysfs.c
3 *
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>
10 *
11 * File attributes for PCI devices
12 *
13 * Modeled after usb's driverfs.c
14 *
15 */
16
17
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>
23#include <linux/mm.h>
24
25#include "pci.h"
26
27static int sysfs_initialized; /* = 0 */
28
29/* show configuration fields */
30#define pci_config_attr(field, format_string) \
31static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040032field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{ \
34 struct pci_dev *pdev; \
35 \
36 pdev = to_pci_dev (dev); \
37 return sprintf (buf, format_string, pdev->field); \
38}
39
40pci_config_attr(vendor, "0x%04x\n");
41pci_config_attr(device, "0x%04x\n");
42pci_config_attr(subsystem_vendor, "0x%04x\n");
43pci_config_attr(subsystem_device, "0x%04x\n");
44pci_config_attr(class, "0x%06x\n");
45pci_config_attr(irq, "%u\n");
46
Yani Ioannoue404e272005-05-17 06:42:58 -040047static ssize_t local_cpus_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 cpumask_t mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
50 int len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
51 strcat(buf,"\n");
52 return 1+len;
53}
54
55/* show resources */
56static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -040057resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct pci_dev * pci_dev = to_pci_dev(dev);
60 char * str = buf;
61 int i;
62 int max = 7;
63
64 if (pci_dev->subordinate)
65 max = DEVICE_COUNT_RESOURCE;
66
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));
72 }
73 return (str - buf);
74}
75
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +020076static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -070077{
78 struct pci_dev *pci_dev = to_pci_dev(dev);
79
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));
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct device_attribute pci_dev_attrs[] = {
88 __ATTR_RO(resource),
89 __ATTR_RO(vendor),
90 __ATTR_RO(device),
91 __ATTR_RO(subsystem_vendor),
92 __ATTR_RO(subsystem_device),
93 __ATTR_RO(class),
94 __ATTR_RO(irq),
95 __ATTR_RO(local_cpus),
Greg KH98885492005-05-05 11:57:25 -070096 __ATTR_RO(modalias),
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 __ATTR_NULL,
98};
99
100static ssize_t
101pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
102{
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;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900106 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
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) {
112 size = 128;
113 }
114
115 if (off > size)
116 return 0;
117 if (off + count > size) {
118 size -= off;
119 count = size;
120 } else {
121 size = count;
122 }
123
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900124 if ((off & 1) && size) {
125 u8 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 pci_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900127 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900129 size--;
130 }
131
132 if ((off & 3) && size > 2) {
133 u16 val;
134 pci_read_config_word(dev, off, &val);
135 data[off - init_off] = val & 0xff;
136 data[off - init_off + 1] = (val >> 8) & 0xff;
137 off += 2;
138 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
141 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900142 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 pci_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900144 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 off += 4;
149 size -= 4;
150 }
151
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900152 if (size >= 2) {
153 u16 val;
154 pci_read_config_word(dev, off, &val);
155 data[off - init_off] = val & 0xff;
156 data[off - init_off + 1] = (val >> 8) & 0xff;
157 off += 2;
158 size -= 2;
159 }
160
161 if (size > 0) {
162 u8 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 pci_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900164 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 off++;
166 --size;
167 }
168
169 return count;
170}
171
172static ssize_t
173pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
174{
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;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900178 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (off > dev->cfg_size)
181 return 0;
182 if (off + count > dev->cfg_size) {
183 size = dev->cfg_size - off;
184 count = size;
185 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900186
187 if ((off & 1) && size) {
188 pci_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900190 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900192
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);
197 off += 2;
198 size -= 2;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900202 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 pci_write_config_dword(dev, off, val);
207 off += 4;
208 size -= 4;
209 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900210
211 if (size >= 2) {
212 u16 val = data[off - init_off];
213 val |= (u16) data[off - init_off + 1] << 8;
214 pci_write_config_word(dev, off, val);
215 off += 2;
216 size -= 2;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900219 if (size) {
220 pci_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 off++;
222 --size;
223 }
224
225 return count;
226}
227
228#ifdef HAVE_PCI_LEGACY
229/**
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
235 *
236 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
237 * callback routine (pci_legacy_read).
238 */
239ssize_t
240pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
241{
242 struct pci_bus *bus = to_pci_bus(container_of(kobj,
243 struct class_device,
244 kobj));
245
246 /* Only support 1, 2 or 4 byte accesses */
247 if (count != 1 && count != 2 && count != 4)
248 return -EINVAL;
249
250 return pci_legacy_read(bus, off, (u32 *)buf, count);
251}
252
253/**
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
259 *
260 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
261 * callback routine (pci_legacy_write).
262 */
263ssize_t
264pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
265{
266 struct pci_bus *bus = to_pci_bus(container_of(kobj,
267 struct class_device,
268 kobj));
269 /* Only support 1, 2 or 4 byte accesses */
270 if (count != 1 && count != 2 && count != 4)
271 return -EINVAL;
272
273 return pci_legacy_write(bus, off, *(u32 *)buf, count);
274}
275
276/**
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
281 *
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
284 * memory space.
285 */
286int
287pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
288 struct vm_area_struct *vma)
289{
290 struct pci_bus *bus = to_pci_bus(container_of(kobj,
291 struct class_device,
292 kobj));
293
294 return pci_mmap_legacy_page_range(bus, vma);
295}
296#endif /* HAVE_PCI_LEGACY */
297
298#ifdef HAVE_PCI_MMAP
299/**
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
304 *
305 * Use the regular PCI mapping routines to map a PCI resource into userspace.
306 * FIXME: write combining? maybe automatic for prefetchable regions?
307 */
308static int
309pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
310 struct vm_area_struct *vma)
311{
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;
316
317 vma->vm_pgoff += res->start >> PAGE_SHIFT;
318 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
319
320 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
321}
322
323/**
324 * pci_create_resource_files - create resource files in sysfs for @dev
325 * @dev: dev in question
326 *
327 * Walk the resources in @dev creating files for each resource available.
328 */
329static void
330pci_create_resource_files(struct pci_dev *pdev)
331{
332 int i;
333
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;
337
338 /* skip empty resources */
339 if (!pci_resource_len(pdev, i))
340 continue;
341
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500342 /* allocate attribute structure, piggyback attribute name */
343 res_attr = kcalloc(1, sizeof(*res_attr) + 10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (res_attr) {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500345 char *res_attr_name = (char *)(res_attr + 1);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 pdev->res_attr[i] = res_attr;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500348 sprintf(res_attr_name, "resource%d", i);
349 res_attr->attr.name = res_attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 res_attr->attr.mode = S_IRUSR | S_IWUSR;
351 res_attr->attr.owner = THIS_MODULE;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500352 res_attr->size = pci_resource_len(pdev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 res_attr->mmap = pci_mmap_resource;
354 res_attr->private = &pdev->resource[i];
355 sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
356 }
357 }
358}
359
360/**
361 * pci_remove_resource_files - cleanup resource files
362 * @dev: dev to cleanup
363 *
364 * If we created resource files for @dev, remove them from sysfs and
365 * free their resources.
366 */
367static void
368pci_remove_resource_files(struct pci_dev *pdev)
369{
370 int i;
371
372 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
373 struct bin_attribute *res_attr;
374
375 res_attr = pdev->res_attr[i];
376 if (res_attr) {
377 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
378 kfree(res_attr);
379 }
380 }
381}
382#else /* !HAVE_PCI_MMAP */
383static inline void pci_create_resource_files(struct pci_dev *dev) { return; }
384static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
385#endif /* HAVE_PCI_MMAP */
386
387/**
388 * pci_write_rom - used to enable access to the PCI ROM display
389 * @kobj: kernel object handle
390 * @buf: user input
391 * @off: file offset
392 * @count: number of byte in input
393 *
394 * writing anything except 0 enables it
395 */
396static ssize_t
397pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
398{
399 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
400
401 if ((off == 0) && (*buf == '0') && (count == 2))
402 pdev->rom_attr_enabled = 0;
403 else
404 pdev->rom_attr_enabled = 1;
405
406 return count;
407}
408
409/**
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
413 * @off: file offset
414 * @count: number of bytes to read
415 *
416 * Put @count bytes starting at @off into @buf from the ROM in the PCI
417 * device corresponding to @kobj.
418 */
419static ssize_t
420pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
421{
422 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
423 void __iomem *rom;
424 size_t size;
425
426 if (!pdev->rom_attr_enabled)
427 return -EINVAL;
428
429 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
430 if (!rom)
431 return 0;
432
433 if (off >= size)
434 count = 0;
435 else {
436 if (off + count > size)
437 count = size - off;
438
439 memcpy_fromio(buf, rom + off, count);
440 }
441 pci_unmap_rom(pdev, rom);
442
443 return count;
444}
445
446static struct bin_attribute pci_config_attr = {
447 .attr = {
448 .name = "config",
449 .mode = S_IRUGO | S_IWUSR,
450 .owner = THIS_MODULE,
451 },
452 .size = 256,
453 .read = pci_read_config,
454 .write = pci_write_config,
455};
456
457static struct bin_attribute pcie_config_attr = {
458 .attr = {
459 .name = "config",
460 .mode = S_IRUGO | S_IWUSR,
461 .owner = THIS_MODULE,
462 },
463 .size = 4096,
464 .read = pci_read_config,
465 .write = pci_write_config,
466};
467
468int pci_create_sysfs_dev_files (struct pci_dev *pdev)
469{
470 if (!sysfs_initialized)
471 return -EACCES;
472
473 if (pdev->cfg_size < 4096)
474 sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
475 else
476 sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
477
478 pci_create_resource_files(pdev);
479
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;
483
484 rom_attr = kmalloc(sizeof(*rom_attr), GFP_ATOMIC);
485 if (rom_attr) {
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);
495 }
496 }
497 /* add platform-specific attributes */
498 pcibios_add_platform_entries(pdev);
499
500 return 0;
501}
502
503/**
504 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
505 * @pdev: device whose entries we should free
506 *
507 * Cleanup when @pdev is removed from sysfs.
508 */
509void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
510{
511 if (pdev->cfg_size < 4096)
512 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
513 else
514 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
515
516 pci_remove_resource_files(pdev);
517
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);
522 }
523 }
524}
525
526static int __init pci_sysfs_init(void)
527{
528 struct pci_dev *pdev = NULL;
529
530 sysfs_initialized = 1;
531 for_each_pci_dev(pdev)
532 pci_create_sysfs_dev_files(pdev);
533
534 return 0;
535}
536
537__initcall(pci_sysfs_init);