1 /* 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 27 static int sysfs_initialized; /* = 0 */ 28 29 /* show configuration fields */ 30 #define pci_config_attr(field, format_string) \ 31 static ssize_t \ 32 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 33 { \ 34 struct pci_dev *pdev; \ 35 \ 36 pdev = to_pci_dev (dev); \ 37 return sprintf (buf, format_string, pdev->field); \ 38 } 39 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"); 46 47 static ssize_t local_cpus_show(struct device *dev, struct device_attribute *attr, char *buf) 48 { 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 */ 56 static ssize_t 57 resource_show(struct device * dev, struct device_attribute *attr, char * buf) 58 { 59 struct pci_dev * pci_dev = to_pci_dev(dev); 60 char * str = buf; 61 int i; 62 int max = 7; 63 u64 start, end; 64 65 if (pci_dev->subordinate) 66 max = DEVICE_COUNT_RESOURCE; 67 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); 75 } 76 return (str - buf); 77 } 78 79 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf) 80 { 81 struct pci_dev *pci_dev = to_pci_dev(dev); 82 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)); 88 } 89 90 struct device_attribute pci_dev_attrs[] = { 91 __ATTR_RO(resource), 92 __ATTR_RO(vendor), 93 __ATTR_RO(device), 94 __ATTR_RO(subsystem_vendor), 95 __ATTR_RO(subsystem_device), 96 __ATTR_RO(class), 97 __ATTR_RO(irq), 98 __ATTR_RO(local_cpus), 99 __ATTR_RO(modalias), 100 __ATTR_NULL, 101 }; 102 103 static ssize_t 104 pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count) 105 { 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; 110 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) { 115 size = 128; 116 } 117 118 if (off > size) 119 return 0; 120 if (off + count > size) { 121 size -= off; 122 count = size; 123 } else { 124 size = count; 125 } 126 127 if ((off & 1) && size) { 128 u8 val; 129 pci_read_config_byte(dev, off, &val); 130 data[off - init_off] = val; 131 off++; 132 size--; 133 } 134 135 if ((off & 3) && size > 2) { 136 u16 val; 137 pci_read_config_word(dev, off, &val); 138 data[off - init_off] = val & 0xff; 139 data[off - init_off + 1] = (val >> 8) & 0xff; 140 off += 2; 141 size -= 2; 142 } 143 144 while (size > 3) { 145 u32 val; 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; 151 off += 4; 152 size -= 4; 153 } 154 155 if (size >= 2) { 156 u16 val; 157 pci_read_config_word(dev, off, &val); 158 data[off - init_off] = val & 0xff; 159 data[off - init_off + 1] = (val >> 8) & 0xff; 160 off += 2; 161 size -= 2; 162 } 163 164 if (size > 0) { 165 u8 val; 166 pci_read_config_byte(dev, off, &val); 167 data[off - init_off] = val; 168 off++; 169 --size; 170 } 171 172 return count; 173 } 174 175 static ssize_t 176 pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count) 177 { 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; 182 183 if (off > dev->cfg_size) 184 return 0; 185 if (off + count > dev->cfg_size) { 186 size = dev->cfg_size - off; 187 count = size; 188 } 189 190 if ((off & 1) && size) { 191 pci_write_config_byte(dev, off, data[off - init_off]); 192 off++; 193 size--; 194 } 195 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); 200 off += 2; 201 size -= 2; 202 } 203 204 while (size > 3) { 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); 210 off += 4; 211 size -= 4; 212 } 213 214 if (size >= 2) { 215 u16 val = data[off - init_off]; 216 val |= (u16) data[off - init_off + 1] << 8; 217 pci_write_config_word(dev, off, val); 218 off += 2; 219 size -= 2; 220 } 221 222 if (size) { 223 pci_write_config_byte(dev, off, data[off - init_off]); 224 off++; 225 --size; 226 } 227 228 return count; 229 } 230 231 #ifdef HAVE_PCI_LEGACY 232 /** 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 238 * 239 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific 240 * callback routine (pci_legacy_read). 241 */ 242 ssize_t 243 pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count) 244 { 245 struct pci_bus *bus = to_pci_bus(container_of(kobj, 246 struct class_device, 247 kobj)); 248 249 /* Only support 1, 2 or 4 byte accesses */ 250 if (count != 1 && count != 2 && count != 4) 251 return -EINVAL; 252 253 return pci_legacy_read(bus, off, (u32 *)buf, count); 254 } 255 256 /** 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 262 * 263 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific 264 * callback routine (pci_legacy_write). 265 */ 266 ssize_t 267 pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count) 268 { 269 struct pci_bus *bus = to_pci_bus(container_of(kobj, 270 struct class_device, 271 kobj)); 272 /* Only support 1, 2 or 4 byte accesses */ 273 if (count != 1 && count != 2 && count != 4) 274 return -EINVAL; 275 276 return pci_legacy_write(bus, off, *(u32 *)buf, count); 277 } 278 279 /** 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 284 * 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 287 * memory space. 288 */ 289 int 290 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr, 291 struct vm_area_struct *vma) 292 { 293 struct pci_bus *bus = to_pci_bus(container_of(kobj, 294 struct class_device, 295 kobj)); 296 297 return pci_mmap_legacy_page_range(bus, vma); 298 } 299 #endif /* HAVE_PCI_LEGACY */ 300 301 #ifdef HAVE_PCI_MMAP 302 /** 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 307 * 308 * Use the regular PCI mapping routines to map a PCI resource into userspace. 309 * FIXME: write combining? maybe automatic for prefetchable regions? 310 */ 311 static int 312 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, 313 struct vm_area_struct *vma) 314 { 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; 319 u64 start, end; 320 int i; 321 322 for (i = 0; i < PCI_ROM_RESOURCE; i++) 323 if (res == &pdev->resource[i]) 324 break; 325 if (i >= PCI_ROM_RESOURCE) 326 return -ENODEV; 327 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. 331 */ 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; 335 336 return pci_mmap_page_range(pdev, vma, mmap_type, 0); 337 } 338 339 /** 340 * pci_create_resource_files - create resource files in sysfs for @dev 341 * @dev: dev in question 342 * 343 * Walk the resources in @dev creating files for each resource available. 344 */ 345 static void 346 pci_create_resource_files(struct pci_dev *pdev) 347 { 348 int i; 349 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; 353 354 /* skip empty resources */ 355 if (!pci_resource_len(pdev, i)) 356 continue; 357 358 /* allocate attribute structure, piggyback attribute name */ 359 res_attr = kcalloc(1, sizeof(*res_attr) + 10, GFP_ATOMIC); 360 if (res_attr) { 361 char *res_attr_name = (char *)(res_attr + 1); 362 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); 372 } 373 } 374 } 375 376 /** 377 * pci_remove_resource_files - cleanup resource files 378 * @dev: dev to cleanup 379 * 380 * If we created resource files for @dev, remove them from sysfs and 381 * free their resources. 382 */ 383 static void 384 pci_remove_resource_files(struct pci_dev *pdev) 385 { 386 int i; 387 388 for (i = 0; i < PCI_ROM_RESOURCE; i++) { 389 struct bin_attribute *res_attr; 390 391 res_attr = pdev->res_attr[i]; 392 if (res_attr) { 393 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); 394 kfree(res_attr); 395 } 396 } 397 } 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 */ 402 403 /** 404 * pci_write_rom - used to enable access to the PCI ROM display 405 * @kobj: kernel object handle 406 * @buf: user input 407 * @off: file offset 408 * @count: number of byte in input 409 * 410 * writing anything except 0 enables it 411 */ 412 static ssize_t 413 pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count) 414 { 415 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); 416 417 if ((off == 0) && (*buf == '0') && (count == 2)) 418 pdev->rom_attr_enabled = 0; 419 else 420 pdev->rom_attr_enabled = 1; 421 422 return count; 423 } 424 425 /** 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 429 * @off: file offset 430 * @count: number of bytes to read 431 * 432 * Put @count bytes starting at @off into @buf from the ROM in the PCI 433 * device corresponding to @kobj. 434 */ 435 static ssize_t 436 pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count) 437 { 438 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); 439 void __iomem *rom; 440 size_t size; 441 442 if (!pdev->rom_attr_enabled) 443 return -EINVAL; 444 445 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ 446 if (!rom) 447 return 0; 448 449 if (off >= size) 450 count = 0; 451 else { 452 if (off + count > size) 453 count = size - off; 454 455 memcpy_fromio(buf, rom + off, count); 456 } 457 pci_unmap_rom(pdev, rom); 458 459 return count; 460 } 461 462 static struct bin_attribute pci_config_attr = { 463 .attr = { 464 .name = "config", 465 .mode = S_IRUGO | S_IWUSR, 466 .owner = THIS_MODULE, 467 }, 468 .size = 256, 469 .read = pci_read_config, 470 .write = pci_write_config, 471 }; 472 473 static struct bin_attribute pcie_config_attr = { 474 .attr = { 475 .name = "config", 476 .mode = S_IRUGO | S_IWUSR, 477 .owner = THIS_MODULE, 478 }, 479 .size = 4096, 480 .read = pci_read_config, 481 .write = pci_write_config, 482 }; 483 484 int pci_create_sysfs_dev_files (struct pci_dev *pdev) 485 { 486 if (!sysfs_initialized) 487 return -EACCES; 488 489 if (pdev->cfg_size < 4096) 490 sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); 491 else 492 sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); 493 494 pci_create_resource_files(pdev); 495 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; 499 500 rom_attr = kmalloc(sizeof(*rom_attr), GFP_ATOMIC); 501 if (rom_attr) { 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); 511 } 512 } 513 /* add platform-specific attributes */ 514 pcibios_add_platform_entries(pdev); 515 516 return 0; 517 } 518 519 /** 520 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files 521 * @pdev: device whose entries we should free 522 * 523 * Cleanup when @pdev is removed from sysfs. 524 */ 525 void pci_remove_sysfs_dev_files(struct pci_dev *pdev) 526 { 527 if (pdev->cfg_size < 4096) 528 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); 529 else 530 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); 531 532 pci_remove_resource_files(pdev); 533 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); 538 } 539 } 540 } 541 542 static int __init pci_sysfs_init(void) 543 { 544 struct pci_dev *pdev = NULL; 545 546 sysfs_initialized = 1; 547 for_each_pci_dev(pdev) 548 pci_create_sysfs_dev_files(pdev); 549 550 return 0; 551 } 552 553 __initcall(pci_sysfs_init); 554