pci-sysfs.c 13 KB

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