pci-sysfs.c 13 KB

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