pci-sysfs.c 14 KB

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