pci-sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #include <linux/config.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/stat.h>
  20. #include <linux/topology.h>
  21. #include <linux/mm.h>
  22. #include "pci.h"
  23. static int sysfs_initialized; /* = 0 */
  24. /* show configuration fields */
  25. #define pci_config_attr(field, format_string) \
  26. static ssize_t \
  27. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  28. { \
  29. struct pci_dev *pdev; \
  30. \
  31. pdev = to_pci_dev (dev); \
  32. return sprintf (buf, format_string, pdev->field); \
  33. }
  34. pci_config_attr(vendor, "0x%04x\n");
  35. pci_config_attr(device, "0x%04x\n");
  36. pci_config_attr(subsystem_vendor, "0x%04x\n");
  37. pci_config_attr(subsystem_device, "0x%04x\n");
  38. pci_config_attr(class, "0x%06x\n");
  39. pci_config_attr(irq, "%u\n");
  40. static ssize_t local_cpus_show(struct device *dev, struct device_attribute *attr, char *buf)
  41. {
  42. cpumask_t mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  43. int len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  44. strcat(buf,"\n");
  45. return 1+len;
  46. }
  47. /* show resources */
  48. static ssize_t
  49. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  50. {
  51. struct pci_dev * pci_dev = to_pci_dev(dev);
  52. char * str = buf;
  53. int i;
  54. int max = 7;
  55. u64 start, end;
  56. if (pci_dev->subordinate)
  57. max = DEVICE_COUNT_RESOURCE;
  58. for (i = 0; i < max; i++) {
  59. struct resource *res = &pci_dev->resource[i];
  60. pci_resource_to_user(pci_dev, i, res, &start, &end);
  61. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  62. (unsigned long long)start,
  63. (unsigned long long)end,
  64. (unsigned long long)res->flags);
  65. }
  66. return (str - buf);
  67. }
  68. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  69. {
  70. struct pci_dev *pci_dev = to_pci_dev(dev);
  71. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  72. pci_dev->vendor, pci_dev->device,
  73. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  74. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  75. (u8)(pci_dev->class));
  76. }
  77. struct device_attribute pci_dev_attrs[] = {
  78. __ATTR_RO(resource),
  79. __ATTR_RO(vendor),
  80. __ATTR_RO(device),
  81. __ATTR_RO(subsystem_vendor),
  82. __ATTR_RO(subsystem_device),
  83. __ATTR_RO(class),
  84. __ATTR_RO(irq),
  85. __ATTR_RO(local_cpus),
  86. __ATTR_RO(modalias),
  87. __ATTR_NULL,
  88. };
  89. static ssize_t
  90. pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  91. {
  92. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  93. unsigned int size = 64;
  94. loff_t init_off = off;
  95. u8 *data = (u8*) buf;
  96. /* Several chips lock up trying to read undefined config space */
  97. if (capable(CAP_SYS_ADMIN)) {
  98. size = dev->cfg_size;
  99. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  100. size = 128;
  101. }
  102. if (off > size)
  103. return 0;
  104. if (off + count > size) {
  105. size -= off;
  106. count = size;
  107. } else {
  108. size = count;
  109. }
  110. if ((off & 1) && size) {
  111. u8 val;
  112. pci_read_config_byte(dev, off, &val);
  113. data[off - init_off] = val;
  114. off++;
  115. size--;
  116. }
  117. if ((off & 3) && size > 2) {
  118. u16 val;
  119. pci_read_config_word(dev, off, &val);
  120. data[off - init_off] = val & 0xff;
  121. data[off - init_off + 1] = (val >> 8) & 0xff;
  122. off += 2;
  123. size -= 2;
  124. }
  125. while (size > 3) {
  126. u32 val;
  127. pci_read_config_dword(dev, off, &val);
  128. data[off - init_off] = val & 0xff;
  129. data[off - init_off + 1] = (val >> 8) & 0xff;
  130. data[off - init_off + 2] = (val >> 16) & 0xff;
  131. data[off - init_off + 3] = (val >> 24) & 0xff;
  132. off += 4;
  133. size -= 4;
  134. }
  135. if (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. if (size > 0) {
  144. u8 val;
  145. pci_read_config_byte(dev, off, &val);
  146. data[off - init_off] = val;
  147. off++;
  148. --size;
  149. }
  150. return count;
  151. }
  152. static ssize_t
  153. pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  154. {
  155. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  156. unsigned int size = count;
  157. loff_t init_off = off;
  158. u8 *data = (u8*) buf;
  159. if (off > dev->cfg_size)
  160. return 0;
  161. if (off + count > dev->cfg_size) {
  162. size = dev->cfg_size - off;
  163. count = size;
  164. }
  165. if ((off & 1) && size) {
  166. pci_write_config_byte(dev, off, data[off - init_off]);
  167. off++;
  168. size--;
  169. }
  170. if ((off & 3) && size > 2) {
  171. u16 val = data[off - init_off];
  172. val |= (u16) data[off - init_off + 1] << 8;
  173. pci_write_config_word(dev, off, val);
  174. off += 2;
  175. size -= 2;
  176. }
  177. while (size > 3) {
  178. u32 val = data[off - init_off];
  179. val |= (u32) data[off - init_off + 1] << 8;
  180. val |= (u32) data[off - init_off + 2] << 16;
  181. val |= (u32) data[off - init_off + 3] << 24;
  182. pci_write_config_dword(dev, off, val);
  183. off += 4;
  184. size -= 4;
  185. }
  186. if (size >= 2) {
  187. u16 val = data[off - init_off];
  188. val |= (u16) data[off - init_off + 1] << 8;
  189. pci_write_config_word(dev, off, val);
  190. off += 2;
  191. size -= 2;
  192. }
  193. if (size) {
  194. pci_write_config_byte(dev, off, data[off - init_off]);
  195. off++;
  196. --size;
  197. }
  198. return count;
  199. }
  200. #ifdef HAVE_PCI_LEGACY
  201. /**
  202. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  203. * @kobj: kobject corresponding to file to read from
  204. * @buf: buffer to store results
  205. * @off: offset into legacy I/O port space
  206. * @count: number of bytes to read
  207. *
  208. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  209. * callback routine (pci_legacy_read).
  210. */
  211. ssize_t
  212. pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  213. {
  214. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  215. struct class_device,
  216. kobj));
  217. /* Only support 1, 2 or 4 byte accesses */
  218. if (count != 1 && count != 2 && count != 4)
  219. return -EINVAL;
  220. return pci_legacy_read(bus, off, (u32 *)buf, count);
  221. }
  222. /**
  223. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  224. * @kobj: kobject corresponding to file to read from
  225. * @buf: buffer containing value to be written
  226. * @off: offset into legacy I/O port space
  227. * @count: number of bytes to write
  228. *
  229. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  230. * callback routine (pci_legacy_write).
  231. */
  232. ssize_t
  233. pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
  234. {
  235. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  236. struct class_device,
  237. kobj));
  238. /* Only support 1, 2 or 4 byte accesses */
  239. if (count != 1 && count != 2 && count != 4)
  240. return -EINVAL;
  241. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  242. }
  243. /**
  244. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  245. * @kobj: kobject corresponding to device to be mapped
  246. * @attr: struct bin_attribute for this file
  247. * @vma: struct vm_area_struct passed to mmap
  248. *
  249. * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
  250. * legacy memory space (first meg of bus space) into application virtual
  251. * memory space.
  252. */
  253. int
  254. pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
  255. struct vm_area_struct *vma)
  256. {
  257. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  258. struct class_device,
  259. kobj));
  260. return pci_mmap_legacy_page_range(bus, vma);
  261. }
  262. #endif /* HAVE_PCI_LEGACY */
  263. #ifdef HAVE_PCI_MMAP
  264. /**
  265. * pci_mmap_resource - map a PCI resource into user memory space
  266. * @kobj: kobject for mapping
  267. * @attr: struct bin_attribute for the file being mapped
  268. * @vma: struct vm_area_struct passed into the mmap
  269. *
  270. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  271. * FIXME: write combining? maybe automatic for prefetchable regions?
  272. */
  273. static int
  274. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  275. struct vm_area_struct *vma)
  276. {
  277. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  278. struct device, kobj));
  279. struct resource *res = (struct resource *)attr->private;
  280. enum pci_mmap_state mmap_type;
  281. u64 start, end;
  282. int i;
  283. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  284. if (res == &pdev->resource[i])
  285. break;
  286. if (i >= PCI_ROM_RESOURCE)
  287. return -ENODEV;
  288. /* pci_mmap_page_range() expects the same kind of entry as coming
  289. * from /proc/bus/pci/ which is a "user visible" value. If this is
  290. * different from the resource itself, arch will do necessary fixup.
  291. */
  292. pci_resource_to_user(pdev, i, res, &start, &end);
  293. vma->vm_pgoff += start >> PAGE_SHIFT;
  294. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  295. return pci_mmap_page_range(pdev, vma, mmap_type, 0);
  296. }
  297. /**
  298. * pci_create_resource_files - create resource files in sysfs for @dev
  299. * @dev: dev in question
  300. *
  301. * Walk the resources in @dev creating files for each resource available.
  302. */
  303. static void
  304. pci_create_resource_files(struct pci_dev *pdev)
  305. {
  306. int i;
  307. /* Expose the PCI resources from this device as files */
  308. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  309. struct bin_attribute *res_attr;
  310. /* skip empty resources */
  311. if (!pci_resource_len(pdev, i))
  312. continue;
  313. /* allocate attribute structure, piggyback attribute name */
  314. res_attr = kcalloc(1, sizeof(*res_attr) + 10, GFP_ATOMIC);
  315. if (res_attr) {
  316. char *res_attr_name = (char *)(res_attr + 1);
  317. pdev->res_attr[i] = res_attr;
  318. sprintf(res_attr_name, "resource%d", i);
  319. res_attr->attr.name = res_attr_name;
  320. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  321. res_attr->attr.owner = THIS_MODULE;
  322. res_attr->size = pci_resource_len(pdev, i);
  323. res_attr->mmap = pci_mmap_resource;
  324. res_attr->private = &pdev->resource[i];
  325. sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  326. }
  327. }
  328. }
  329. /**
  330. * pci_remove_resource_files - cleanup resource files
  331. * @dev: dev to cleanup
  332. *
  333. * If we created resource files for @dev, remove them from sysfs and
  334. * free their resources.
  335. */
  336. static void
  337. pci_remove_resource_files(struct pci_dev *pdev)
  338. {
  339. int i;
  340. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  341. struct bin_attribute *res_attr;
  342. res_attr = pdev->res_attr[i];
  343. if (res_attr) {
  344. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  345. kfree(res_attr);
  346. }
  347. }
  348. }
  349. #else /* !HAVE_PCI_MMAP */
  350. static inline void pci_create_resource_files(struct pci_dev *dev) { return; }
  351. static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
  352. #endif /* HAVE_PCI_MMAP */
  353. /**
  354. * pci_write_rom - used to enable access to the PCI ROM display
  355. * @kobj: kernel object handle
  356. * @buf: user input
  357. * @off: file offset
  358. * @count: number of byte in input
  359. *
  360. * writing anything except 0 enables it
  361. */
  362. static ssize_t
  363. pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  364. {
  365. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  366. if ((off == 0) && (*buf == '0') && (count == 2))
  367. pdev->rom_attr_enabled = 0;
  368. else
  369. pdev->rom_attr_enabled = 1;
  370. return count;
  371. }
  372. /**
  373. * pci_read_rom - read a PCI ROM
  374. * @kobj: kernel object handle
  375. * @buf: where to put the data we read from the ROM
  376. * @off: file offset
  377. * @count: number of bytes to read
  378. *
  379. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  380. * device corresponding to @kobj.
  381. */
  382. static ssize_t
  383. pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
  384. {
  385. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  386. void __iomem *rom;
  387. size_t size;
  388. if (!pdev->rom_attr_enabled)
  389. return -EINVAL;
  390. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  391. if (!rom)
  392. return 0;
  393. if (off >= size)
  394. count = 0;
  395. else {
  396. if (off + count > size)
  397. count = size - off;
  398. memcpy_fromio(buf, rom + off, count);
  399. }
  400. pci_unmap_rom(pdev, rom);
  401. return count;
  402. }
  403. static struct bin_attribute pci_config_attr = {
  404. .attr = {
  405. .name = "config",
  406. .mode = S_IRUGO | S_IWUSR,
  407. .owner = THIS_MODULE,
  408. },
  409. .size = 256,
  410. .read = pci_read_config,
  411. .write = pci_write_config,
  412. };
  413. static struct bin_attribute pcie_config_attr = {
  414. .attr = {
  415. .name = "config",
  416. .mode = S_IRUGO | S_IWUSR,
  417. .owner = THIS_MODULE,
  418. },
  419. .size = 4096,
  420. .read = pci_read_config,
  421. .write = pci_write_config,
  422. };
  423. int pci_create_sysfs_dev_files (struct pci_dev *pdev)
  424. {
  425. if (!sysfs_initialized)
  426. return -EACCES;
  427. if (pdev->cfg_size < 4096)
  428. sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  429. else
  430. sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  431. pci_create_resource_files(pdev);
  432. /* If the device has a ROM, try to expose it in sysfs. */
  433. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  434. struct bin_attribute *rom_attr;
  435. rom_attr = kmalloc(sizeof(*rom_attr), GFP_ATOMIC);
  436. if (rom_attr) {
  437. memset(rom_attr, 0x00, sizeof(*rom_attr));
  438. pdev->rom_attr = rom_attr;
  439. rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  440. rom_attr->attr.name = "rom";
  441. rom_attr->attr.mode = S_IRUSR;
  442. rom_attr->attr.owner = THIS_MODULE;
  443. rom_attr->read = pci_read_rom;
  444. rom_attr->write = pci_write_rom;
  445. sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
  446. }
  447. }
  448. /* add platform-specific attributes */
  449. pcibios_add_platform_entries(pdev);
  450. return 0;
  451. }
  452. /**
  453. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  454. * @pdev: device whose entries we should free
  455. *
  456. * Cleanup when @pdev is removed from sysfs.
  457. */
  458. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  459. {
  460. if (pdev->cfg_size < 4096)
  461. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  462. else
  463. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  464. pci_remove_resource_files(pdev);
  465. if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
  466. if (pdev->rom_attr) {
  467. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  468. kfree(pdev->rom_attr);
  469. }
  470. }
  471. }
  472. static int __init pci_sysfs_init(void)
  473. {
  474. struct pci_dev *pdev = NULL;
  475. sysfs_initialized = 1;
  476. for_each_pci_dev(pdev)
  477. pci_create_sysfs_dev_files(pdev);
  478. return 0;
  479. }
  480. __initcall(pci_sysfs_init);