pci-sysfs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/pci.h>
  19. #include <linux/stat.h>
  20. #include <linux/topology.h>
  21. #include <linux/mm.h>
  22. #include <linux/fs.h>
  23. #include <linux/capability.h>
  24. #include <linux/pci-aspm.h>
  25. #include <linux/slab.h>
  26. #include "pci.h"
  27. static int sysfs_initialized; /* = 0 */
  28. /* show configuration fields */
  29. #define pci_config_attr(field, format_string) \
  30. static ssize_t \
  31. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  32. { \
  33. struct pci_dev *pdev; \
  34. \
  35. pdev = to_pci_dev (dev); \
  36. return sprintf (buf, format_string, pdev->field); \
  37. }
  38. pci_config_attr(vendor, "0x%04x\n");
  39. pci_config_attr(device, "0x%04x\n");
  40. pci_config_attr(subsystem_vendor, "0x%04x\n");
  41. pci_config_attr(subsystem_device, "0x%04x\n");
  42. pci_config_attr(class, "0x%06x\n");
  43. pci_config_attr(irq, "%u\n");
  44. static ssize_t broken_parity_status_show(struct device *dev,
  45. struct device_attribute *attr,
  46. char *buf)
  47. {
  48. struct pci_dev *pdev = to_pci_dev(dev);
  49. return sprintf (buf, "%u\n", pdev->broken_parity_status);
  50. }
  51. static ssize_t broken_parity_status_store(struct device *dev,
  52. struct device_attribute *attr,
  53. const char *buf, size_t count)
  54. {
  55. struct pci_dev *pdev = to_pci_dev(dev);
  56. unsigned long val;
  57. if (strict_strtoul(buf, 0, &val) < 0)
  58. return -EINVAL;
  59. pdev->broken_parity_status = !!val;
  60. return count;
  61. }
  62. static ssize_t local_cpus_show(struct device *dev,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. const struct cpumask *mask;
  66. int len;
  67. #ifdef CONFIG_NUMA
  68. mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
  69. cpumask_of_node(dev_to_node(dev));
  70. #else
  71. mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
  72. #endif
  73. len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  74. buf[len++] = '\n';
  75. buf[len] = '\0';
  76. return len;
  77. }
  78. static ssize_t local_cpulist_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. const struct cpumask *mask;
  82. int len;
  83. #ifdef CONFIG_NUMA
  84. mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
  85. cpumask_of_node(dev_to_node(dev));
  86. #else
  87. mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
  88. #endif
  89. len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
  90. buf[len++] = '\n';
  91. buf[len] = '\0';
  92. return len;
  93. }
  94. /* show resources */
  95. static ssize_t
  96. resource_show(struct device * dev, struct device_attribute *attr, char * buf)
  97. {
  98. struct pci_dev * pci_dev = to_pci_dev(dev);
  99. char * str = buf;
  100. int i;
  101. int max;
  102. resource_size_t start, end;
  103. if (pci_dev->subordinate)
  104. max = DEVICE_COUNT_RESOURCE;
  105. else
  106. max = PCI_BRIDGE_RESOURCES;
  107. for (i = 0; i < max; i++) {
  108. struct resource *res = &pci_dev->resource[i];
  109. pci_resource_to_user(pci_dev, i, res, &start, &end);
  110. str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
  111. (unsigned long long)start,
  112. (unsigned long long)end,
  113. (unsigned long long)res->flags);
  114. }
  115. return (str - buf);
  116. }
  117. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  118. {
  119. struct pci_dev *pci_dev = to_pci_dev(dev);
  120. return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
  121. pci_dev->vendor, pci_dev->device,
  122. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  123. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  124. (u8)(pci_dev->class));
  125. }
  126. static ssize_t is_enabled_store(struct device *dev,
  127. struct device_attribute *attr, const char *buf,
  128. size_t count)
  129. {
  130. struct pci_dev *pdev = to_pci_dev(dev);
  131. unsigned long val;
  132. ssize_t result = strict_strtoul(buf, 0, &val);
  133. if (result < 0)
  134. return result;
  135. /* this can crash the machine when done on the "wrong" device */
  136. if (!capable(CAP_SYS_ADMIN))
  137. return -EPERM;
  138. if (!val) {
  139. if (pci_is_enabled(pdev))
  140. pci_disable_device(pdev);
  141. else
  142. result = -EIO;
  143. } else
  144. result = pci_enable_device(pdev);
  145. return result < 0 ? result : count;
  146. }
  147. static ssize_t is_enabled_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct pci_dev *pdev;
  151. pdev = to_pci_dev (dev);
  152. return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
  153. }
  154. #ifdef CONFIG_NUMA
  155. static ssize_t
  156. numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
  157. {
  158. return sprintf (buf, "%d\n", dev->numa_node);
  159. }
  160. #endif
  161. static ssize_t
  162. dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct pci_dev *pdev = to_pci_dev(dev);
  165. return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
  166. }
  167. static ssize_t
  168. consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
  169. char *buf)
  170. {
  171. return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
  172. }
  173. static ssize_t
  174. msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
  175. {
  176. struct pci_dev *pdev = to_pci_dev(dev);
  177. if (!pdev->subordinate)
  178. return 0;
  179. return sprintf (buf, "%u\n",
  180. !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
  181. }
  182. static ssize_t
  183. msi_bus_store(struct device *dev, struct device_attribute *attr,
  184. const char *buf, size_t count)
  185. {
  186. struct pci_dev *pdev = to_pci_dev(dev);
  187. unsigned long val;
  188. if (strict_strtoul(buf, 0, &val) < 0)
  189. return -EINVAL;
  190. /* bad things may happen if the no_msi flag is changed
  191. * while some drivers are loaded */
  192. if (!capable(CAP_SYS_ADMIN))
  193. return -EPERM;
  194. /* Maybe pci devices without subordinate busses shouldn't even have this
  195. * attribute in the first place? */
  196. if (!pdev->subordinate)
  197. return count;
  198. /* Is the flag going to change, or keep the value it already had? */
  199. if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
  200. !!val) {
  201. pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
  202. dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
  203. " bad things could happen\n", val ? "" : " not");
  204. }
  205. return count;
  206. }
  207. #ifdef CONFIG_HOTPLUG
  208. static DEFINE_MUTEX(pci_remove_rescan_mutex);
  209. static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
  210. size_t count)
  211. {
  212. unsigned long val;
  213. struct pci_bus *b = NULL;
  214. if (strict_strtoul(buf, 0, &val) < 0)
  215. return -EINVAL;
  216. if (val) {
  217. mutex_lock(&pci_remove_rescan_mutex);
  218. while ((b = pci_find_next_bus(b)) != NULL)
  219. pci_rescan_bus(b);
  220. mutex_unlock(&pci_remove_rescan_mutex);
  221. }
  222. return count;
  223. }
  224. struct bus_attribute pci_bus_attrs[] = {
  225. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
  226. __ATTR_NULL
  227. };
  228. static ssize_t
  229. dev_rescan_store(struct device *dev, struct device_attribute *attr,
  230. const char *buf, size_t count)
  231. {
  232. unsigned long val;
  233. struct pci_dev *pdev = to_pci_dev(dev);
  234. if (strict_strtoul(buf, 0, &val) < 0)
  235. return -EINVAL;
  236. if (val) {
  237. mutex_lock(&pci_remove_rescan_mutex);
  238. pci_rescan_bus(pdev->bus);
  239. mutex_unlock(&pci_remove_rescan_mutex);
  240. }
  241. return count;
  242. }
  243. static void remove_callback(struct device *dev)
  244. {
  245. struct pci_dev *pdev = to_pci_dev(dev);
  246. mutex_lock(&pci_remove_rescan_mutex);
  247. pci_remove_bus_device(pdev);
  248. mutex_unlock(&pci_remove_rescan_mutex);
  249. }
  250. static ssize_t
  251. remove_store(struct device *dev, struct device_attribute *dummy,
  252. const char *buf, size_t count)
  253. {
  254. int ret = 0;
  255. unsigned long val;
  256. if (strict_strtoul(buf, 0, &val) < 0)
  257. return -EINVAL;
  258. /* An attribute cannot be unregistered by one of its own methods,
  259. * so we have to use this roundabout approach.
  260. */
  261. if (val)
  262. ret = device_schedule_callback(dev, remove_callback);
  263. if (ret)
  264. count = ret;
  265. return count;
  266. }
  267. #endif
  268. struct device_attribute pci_dev_attrs[] = {
  269. __ATTR_RO(resource),
  270. __ATTR_RO(vendor),
  271. __ATTR_RO(device),
  272. __ATTR_RO(subsystem_vendor),
  273. __ATTR_RO(subsystem_device),
  274. __ATTR_RO(class),
  275. __ATTR_RO(irq),
  276. __ATTR_RO(local_cpus),
  277. __ATTR_RO(local_cpulist),
  278. __ATTR_RO(modalias),
  279. #ifdef CONFIG_NUMA
  280. __ATTR_RO(numa_node),
  281. #endif
  282. __ATTR_RO(dma_mask_bits),
  283. __ATTR_RO(consistent_dma_mask_bits),
  284. __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
  285. __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  286. broken_parity_status_show,broken_parity_status_store),
  287. __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
  288. #ifdef CONFIG_HOTPLUG
  289. __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
  290. __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
  291. #endif
  292. __ATTR_NULL,
  293. };
  294. static ssize_t
  295. boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
  296. {
  297. struct pci_dev *pdev = to_pci_dev(dev);
  298. return sprintf(buf, "%u\n",
  299. !!(pdev->resource[PCI_ROM_RESOURCE].flags &
  300. IORESOURCE_ROM_SHADOW));
  301. }
  302. struct device_attribute vga_attr = __ATTR_RO(boot_vga);
  303. static ssize_t
  304. pci_read_config(struct file *filp, struct kobject *kobj,
  305. struct bin_attribute *bin_attr,
  306. char *buf, loff_t off, size_t count)
  307. {
  308. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  309. unsigned int size = 64;
  310. loff_t init_off = off;
  311. u8 *data = (u8*) buf;
  312. /* Several chips lock up trying to read undefined config space */
  313. if (cap_raised(filp->f_cred->cap_effective, CAP_SYS_ADMIN)) {
  314. size = dev->cfg_size;
  315. } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  316. size = 128;
  317. }
  318. if (off > size)
  319. return 0;
  320. if (off + count > size) {
  321. size -= off;
  322. count = size;
  323. } else {
  324. size = count;
  325. }
  326. if ((off & 1) && size) {
  327. u8 val;
  328. pci_user_read_config_byte(dev, off, &val);
  329. data[off - init_off] = val;
  330. off++;
  331. size--;
  332. }
  333. if ((off & 3) && size > 2) {
  334. u16 val;
  335. pci_user_read_config_word(dev, off, &val);
  336. data[off - init_off] = val & 0xff;
  337. data[off - init_off + 1] = (val >> 8) & 0xff;
  338. off += 2;
  339. size -= 2;
  340. }
  341. while (size > 3) {
  342. u32 val;
  343. pci_user_read_config_dword(dev, off, &val);
  344. data[off - init_off] = val & 0xff;
  345. data[off - init_off + 1] = (val >> 8) & 0xff;
  346. data[off - init_off + 2] = (val >> 16) & 0xff;
  347. data[off - init_off + 3] = (val >> 24) & 0xff;
  348. off += 4;
  349. size -= 4;
  350. }
  351. if (size >= 2) {
  352. u16 val;
  353. pci_user_read_config_word(dev, off, &val);
  354. data[off - init_off] = val & 0xff;
  355. data[off - init_off + 1] = (val >> 8) & 0xff;
  356. off += 2;
  357. size -= 2;
  358. }
  359. if (size > 0) {
  360. u8 val;
  361. pci_user_read_config_byte(dev, off, &val);
  362. data[off - init_off] = val;
  363. off++;
  364. --size;
  365. }
  366. return count;
  367. }
  368. static ssize_t
  369. pci_write_config(struct file* filp, struct kobject *kobj,
  370. struct bin_attribute *bin_attr,
  371. char *buf, loff_t off, size_t count)
  372. {
  373. struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
  374. unsigned int size = count;
  375. loff_t init_off = off;
  376. u8 *data = (u8*) buf;
  377. if (off > dev->cfg_size)
  378. return 0;
  379. if (off + count > dev->cfg_size) {
  380. size = dev->cfg_size - off;
  381. count = size;
  382. }
  383. if ((off & 1) && size) {
  384. pci_user_write_config_byte(dev, off, data[off - init_off]);
  385. off++;
  386. size--;
  387. }
  388. if ((off & 3) && size > 2) {
  389. u16 val = data[off - init_off];
  390. val |= (u16) data[off - init_off + 1] << 8;
  391. pci_user_write_config_word(dev, off, val);
  392. off += 2;
  393. size -= 2;
  394. }
  395. while (size > 3) {
  396. u32 val = data[off - init_off];
  397. val |= (u32) data[off - init_off + 1] << 8;
  398. val |= (u32) data[off - init_off + 2] << 16;
  399. val |= (u32) data[off - init_off + 3] << 24;
  400. pci_user_write_config_dword(dev, off, val);
  401. off += 4;
  402. size -= 4;
  403. }
  404. if (size >= 2) {
  405. u16 val = data[off - init_off];
  406. val |= (u16) data[off - init_off + 1] << 8;
  407. pci_user_write_config_word(dev, off, val);
  408. off += 2;
  409. size -= 2;
  410. }
  411. if (size) {
  412. pci_user_write_config_byte(dev, off, data[off - init_off]);
  413. off++;
  414. --size;
  415. }
  416. return count;
  417. }
  418. static ssize_t
  419. read_vpd_attr(struct file *filp, struct kobject *kobj,
  420. struct bin_attribute *bin_attr,
  421. char *buf, loff_t off, size_t count)
  422. {
  423. struct pci_dev *dev =
  424. to_pci_dev(container_of(kobj, struct device, kobj));
  425. if (off > bin_attr->size)
  426. count = 0;
  427. else if (count > bin_attr->size - off)
  428. count = bin_attr->size - off;
  429. return pci_read_vpd(dev, off, count, buf);
  430. }
  431. static ssize_t
  432. write_vpd_attr(struct file *filp, struct kobject *kobj,
  433. struct bin_attribute *bin_attr,
  434. char *buf, loff_t off, size_t count)
  435. {
  436. struct pci_dev *dev =
  437. to_pci_dev(container_of(kobj, struct device, kobj));
  438. if (off > bin_attr->size)
  439. count = 0;
  440. else if (count > bin_attr->size - off)
  441. count = bin_attr->size - off;
  442. return pci_write_vpd(dev, off, count, buf);
  443. }
  444. #ifdef HAVE_PCI_LEGACY
  445. /**
  446. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  447. * @filp: open sysfs file
  448. * @kobj: kobject corresponding to file to read from
  449. * @bin_attr: struct bin_attribute for this file
  450. * @buf: buffer to store results
  451. * @off: offset into legacy I/O port space
  452. * @count: number of bytes to read
  453. *
  454. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  455. * callback routine (pci_legacy_read).
  456. */
  457. static ssize_t
  458. pci_read_legacy_io(struct file *filp, struct kobject *kobj,
  459. struct bin_attribute *bin_attr,
  460. char *buf, loff_t off, size_t count)
  461. {
  462. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  463. struct device,
  464. kobj));
  465. /* Only support 1, 2 or 4 byte accesses */
  466. if (count != 1 && count != 2 && count != 4)
  467. return -EINVAL;
  468. return pci_legacy_read(bus, off, (u32 *)buf, count);
  469. }
  470. /**
  471. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  472. * @filp: open sysfs file
  473. * @kobj: kobject corresponding to file to read from
  474. * @bin_attr: struct bin_attribute for this file
  475. * @buf: buffer containing value to be written
  476. * @off: offset into legacy I/O port space
  477. * @count: number of bytes to write
  478. *
  479. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  480. * callback routine (pci_legacy_write).
  481. */
  482. static ssize_t
  483. pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  484. struct bin_attribute *bin_attr,
  485. char *buf, loff_t off, size_t count)
  486. {
  487. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  488. struct device,
  489. kobj));
  490. /* Only support 1, 2 or 4 byte accesses */
  491. if (count != 1 && count != 2 && count != 4)
  492. return -EINVAL;
  493. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  494. }
  495. /**
  496. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  497. * @filp: open sysfs file
  498. * @kobj: kobject corresponding to device to be mapped
  499. * @attr: struct bin_attribute for this file
  500. * @vma: struct vm_area_struct passed to mmap
  501. *
  502. * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
  503. * legacy memory space (first meg of bus space) into application virtual
  504. * memory space.
  505. */
  506. static int
  507. pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  508. struct bin_attribute *attr,
  509. struct vm_area_struct *vma)
  510. {
  511. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  512. struct device,
  513. kobj));
  514. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  515. }
  516. /**
  517. * pci_mmap_legacy_io - map legacy PCI IO into user memory space
  518. * @filp: open sysfs file
  519. * @kobj: kobject corresponding to device to be mapped
  520. * @attr: struct bin_attribute for this file
  521. * @vma: struct vm_area_struct passed to mmap
  522. *
  523. * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
  524. * legacy IO space (first meg of bus space) into application virtual
  525. * memory space. Returns -ENOSYS if the operation isn't supported
  526. */
  527. static int
  528. pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
  529. struct bin_attribute *attr,
  530. struct vm_area_struct *vma)
  531. {
  532. struct pci_bus *bus = to_pci_bus(container_of(kobj,
  533. struct device,
  534. kobj));
  535. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  536. }
  537. /**
  538. * pci_adjust_legacy_attr - adjustment of legacy file attributes
  539. * @b: bus to create files under
  540. * @mmap_type: I/O port or memory
  541. *
  542. * Stub implementation. Can be overridden by arch if necessary.
  543. */
  544. void __weak
  545. pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
  546. {
  547. return;
  548. }
  549. /**
  550. * pci_create_legacy_files - create legacy I/O port and memory files
  551. * @b: bus to create files under
  552. *
  553. * Some platforms allow access to legacy I/O port and ISA memory space on
  554. * a per-bus basis. This routine creates the files and ties them into
  555. * their associated read, write and mmap files from pci-sysfs.c
  556. *
  557. * On error unwind, but don't propogate the error to the caller
  558. * as it is ok to set up the PCI bus without these files.
  559. */
  560. void pci_create_legacy_files(struct pci_bus *b)
  561. {
  562. int error;
  563. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  564. GFP_ATOMIC);
  565. if (!b->legacy_io)
  566. goto kzalloc_err;
  567. sysfs_bin_attr_init(b->legacy_io);
  568. b->legacy_io->attr.name = "legacy_io";
  569. b->legacy_io->size = 0xffff;
  570. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  571. b->legacy_io->read = pci_read_legacy_io;
  572. b->legacy_io->write = pci_write_legacy_io;
  573. b->legacy_io->mmap = pci_mmap_legacy_io;
  574. pci_adjust_legacy_attr(b, pci_mmap_io);
  575. error = device_create_bin_file(&b->dev, b->legacy_io);
  576. if (error)
  577. goto legacy_io_err;
  578. /* Allocated above after the legacy_io struct */
  579. b->legacy_mem = b->legacy_io + 1;
  580. sysfs_bin_attr_init(b->legacy_mem);
  581. b->legacy_mem->attr.name = "legacy_mem";
  582. b->legacy_mem->size = 1024*1024;
  583. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  584. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  585. pci_adjust_legacy_attr(b, pci_mmap_mem);
  586. error = device_create_bin_file(&b->dev, b->legacy_mem);
  587. if (error)
  588. goto legacy_mem_err;
  589. return;
  590. legacy_mem_err:
  591. device_remove_bin_file(&b->dev, b->legacy_io);
  592. legacy_io_err:
  593. kfree(b->legacy_io);
  594. b->legacy_io = NULL;
  595. kzalloc_err:
  596. printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
  597. "and ISA memory resources to sysfs\n");
  598. return;
  599. }
  600. void pci_remove_legacy_files(struct pci_bus *b)
  601. {
  602. if (b->legacy_io) {
  603. device_remove_bin_file(&b->dev, b->legacy_io);
  604. device_remove_bin_file(&b->dev, b->legacy_mem);
  605. kfree(b->legacy_io); /* both are allocated here */
  606. }
  607. }
  608. #endif /* HAVE_PCI_LEGACY */
  609. #ifdef HAVE_PCI_MMAP
  610. int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
  611. enum pci_mmap_api mmap_api)
  612. {
  613. unsigned long nr, start, size, pci_start;
  614. if (pci_resource_len(pdev, resno) == 0)
  615. return 0;
  616. nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  617. start = vma->vm_pgoff;
  618. size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
  619. pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
  620. pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
  621. if (start >= pci_start && start < pci_start + size &&
  622. start + nr <= pci_start + size)
  623. return 1;
  624. return 0;
  625. }
  626. /**
  627. * pci_mmap_resource - map a PCI resource into user memory space
  628. * @kobj: kobject for mapping
  629. * @attr: struct bin_attribute for the file being mapped
  630. * @vma: struct vm_area_struct passed into the mmap
  631. * @write_combine: 1 for write_combine mapping
  632. *
  633. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  634. */
  635. static int
  636. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  637. struct vm_area_struct *vma, int write_combine)
  638. {
  639. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  640. struct device, kobj));
  641. struct resource *res = attr->private;
  642. enum pci_mmap_state mmap_type;
  643. resource_size_t start, end;
  644. int i;
  645. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  646. if (res == &pdev->resource[i])
  647. break;
  648. if (i >= PCI_ROM_RESOURCE)
  649. return -ENODEV;
  650. if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
  651. WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
  652. "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
  653. current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
  654. pci_name(pdev), i,
  655. (u64)pci_resource_start(pdev, i),
  656. (u64)pci_resource_len(pdev, i));
  657. return -EINVAL;
  658. }
  659. /* pci_mmap_page_range() expects the same kind of entry as coming
  660. * from /proc/bus/pci/ which is a "user visible" value. If this is
  661. * different from the resource itself, arch will do necessary fixup.
  662. */
  663. pci_resource_to_user(pdev, i, res, &start, &end);
  664. vma->vm_pgoff += start >> PAGE_SHIFT;
  665. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  666. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
  667. return -EINVAL;
  668. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  669. }
  670. static int
  671. pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
  672. struct bin_attribute *attr,
  673. struct vm_area_struct *vma)
  674. {
  675. return pci_mmap_resource(kobj, attr, vma, 0);
  676. }
  677. static int
  678. pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
  679. struct bin_attribute *attr,
  680. struct vm_area_struct *vma)
  681. {
  682. return pci_mmap_resource(kobj, attr, vma, 1);
  683. }
  684. static ssize_t
  685. pci_resource_io(struct file *filp, struct kobject *kobj,
  686. struct bin_attribute *attr, char *buf,
  687. loff_t off, size_t count, bool write)
  688. {
  689. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  690. struct device, kobj));
  691. struct resource *res = attr->private;
  692. unsigned long port = off;
  693. int i;
  694. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  695. if (res == &pdev->resource[i])
  696. break;
  697. if (i >= PCI_ROM_RESOURCE)
  698. return -ENODEV;
  699. port += pci_resource_start(pdev, i);
  700. if (port > pci_resource_end(pdev, i))
  701. return 0;
  702. if (port + count - 1 > pci_resource_end(pdev, i))
  703. return -EINVAL;
  704. switch (count) {
  705. case 1:
  706. if (write)
  707. outb(*(u8 *)buf, port);
  708. else
  709. *(u8 *)buf = inb(port);
  710. return 1;
  711. case 2:
  712. if (write)
  713. outw(*(u16 *)buf, port);
  714. else
  715. *(u16 *)buf = inw(port);
  716. return 2;
  717. case 4:
  718. if (write)
  719. outl(*(u32 *)buf, port);
  720. else
  721. *(u32 *)buf = inl(port);
  722. return 4;
  723. }
  724. return -EINVAL;
  725. }
  726. static ssize_t
  727. pci_read_resource_io(struct file *filp, struct kobject *kobj,
  728. struct bin_attribute *attr, char *buf,
  729. loff_t off, size_t count)
  730. {
  731. return pci_resource_io(filp, kobj, attr, buf, off, count, false);
  732. }
  733. static ssize_t
  734. pci_write_resource_io(struct file *filp, struct kobject *kobj,
  735. struct bin_attribute *attr, char *buf,
  736. loff_t off, size_t count)
  737. {
  738. return pci_resource_io(filp, kobj, attr, buf, off, count, true);
  739. }
  740. /**
  741. * pci_remove_resource_files - cleanup resource files
  742. * @pdev: dev to cleanup
  743. *
  744. * If we created resource files for @pdev, remove them from sysfs and
  745. * free their resources.
  746. */
  747. static void
  748. pci_remove_resource_files(struct pci_dev *pdev)
  749. {
  750. int i;
  751. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  752. struct bin_attribute *res_attr;
  753. res_attr = pdev->res_attr[i];
  754. if (res_attr) {
  755. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  756. kfree(res_attr);
  757. }
  758. res_attr = pdev->res_attr_wc[i];
  759. if (res_attr) {
  760. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  761. kfree(res_attr);
  762. }
  763. }
  764. }
  765. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  766. {
  767. /* allocate attribute structure, piggyback attribute name */
  768. int name_len = write_combine ? 13 : 10;
  769. struct bin_attribute *res_attr;
  770. int retval;
  771. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  772. if (res_attr) {
  773. char *res_attr_name = (char *)(res_attr + 1);
  774. sysfs_bin_attr_init(res_attr);
  775. if (write_combine) {
  776. pdev->res_attr_wc[num] = res_attr;
  777. sprintf(res_attr_name, "resource%d_wc", num);
  778. res_attr->mmap = pci_mmap_resource_wc;
  779. } else {
  780. pdev->res_attr[num] = res_attr;
  781. sprintf(res_attr_name, "resource%d", num);
  782. res_attr->mmap = pci_mmap_resource_uc;
  783. }
  784. if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
  785. res_attr->read = pci_read_resource_io;
  786. res_attr->write = pci_write_resource_io;
  787. }
  788. res_attr->attr.name = res_attr_name;
  789. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  790. res_attr->size = pci_resource_len(pdev, num);
  791. res_attr->private = &pdev->resource[num];
  792. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  793. } else
  794. retval = -ENOMEM;
  795. return retval;
  796. }
  797. /**
  798. * pci_create_resource_files - create resource files in sysfs for @dev
  799. * @pdev: dev in question
  800. *
  801. * Walk the resources in @pdev creating files for each resource available.
  802. */
  803. static int pci_create_resource_files(struct pci_dev *pdev)
  804. {
  805. int i;
  806. int retval;
  807. /* Expose the PCI resources from this device as files */
  808. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  809. /* skip empty resources */
  810. if (!pci_resource_len(pdev, i))
  811. continue;
  812. retval = pci_create_attr(pdev, i, 0);
  813. /* for prefetchable resources, create a WC mappable file */
  814. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  815. retval = pci_create_attr(pdev, i, 1);
  816. if (retval) {
  817. pci_remove_resource_files(pdev);
  818. return retval;
  819. }
  820. }
  821. return 0;
  822. }
  823. #else /* !HAVE_PCI_MMAP */
  824. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  825. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  826. #endif /* HAVE_PCI_MMAP */
  827. /**
  828. * pci_write_rom - used to enable access to the PCI ROM display
  829. * @filp: sysfs file
  830. * @kobj: kernel object handle
  831. * @bin_attr: struct bin_attribute for this file
  832. * @buf: user input
  833. * @off: file offset
  834. * @count: number of byte in input
  835. *
  836. * writing anything except 0 enables it
  837. */
  838. static ssize_t
  839. pci_write_rom(struct file *filp, struct kobject *kobj,
  840. struct bin_attribute *bin_attr,
  841. char *buf, loff_t off, size_t count)
  842. {
  843. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  844. if ((off == 0) && (*buf == '0') && (count == 2))
  845. pdev->rom_attr_enabled = 0;
  846. else
  847. pdev->rom_attr_enabled = 1;
  848. return count;
  849. }
  850. /**
  851. * pci_read_rom - read a PCI ROM
  852. * @filp: sysfs file
  853. * @kobj: kernel object handle
  854. * @bin_attr: struct bin_attribute for this file
  855. * @buf: where to put the data we read from the ROM
  856. * @off: file offset
  857. * @count: number of bytes to read
  858. *
  859. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  860. * device corresponding to @kobj.
  861. */
  862. static ssize_t
  863. pci_read_rom(struct file *filp, struct kobject *kobj,
  864. struct bin_attribute *bin_attr,
  865. char *buf, loff_t off, size_t count)
  866. {
  867. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  868. void __iomem *rom;
  869. size_t size;
  870. if (!pdev->rom_attr_enabled)
  871. return -EINVAL;
  872. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  873. if (!rom || !size)
  874. return -EIO;
  875. if (off >= size)
  876. count = 0;
  877. else {
  878. if (off + count > size)
  879. count = size - off;
  880. memcpy_fromio(buf, rom + off, count);
  881. }
  882. pci_unmap_rom(pdev, rom);
  883. return count;
  884. }
  885. static struct bin_attribute pci_config_attr = {
  886. .attr = {
  887. .name = "config",
  888. .mode = S_IRUGO | S_IWUSR,
  889. },
  890. .size = PCI_CFG_SPACE_SIZE,
  891. .read = pci_read_config,
  892. .write = pci_write_config,
  893. };
  894. static struct bin_attribute pcie_config_attr = {
  895. .attr = {
  896. .name = "config",
  897. .mode = S_IRUGO | S_IWUSR,
  898. },
  899. .size = PCI_CFG_SPACE_EXP_SIZE,
  900. .read = pci_read_config,
  901. .write = pci_write_config,
  902. };
  903. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  904. {
  905. return 0;
  906. }
  907. static ssize_t reset_store(struct device *dev,
  908. struct device_attribute *attr, const char *buf,
  909. size_t count)
  910. {
  911. struct pci_dev *pdev = to_pci_dev(dev);
  912. unsigned long val;
  913. ssize_t result = strict_strtoul(buf, 0, &val);
  914. if (result < 0)
  915. return result;
  916. if (val != 1)
  917. return -EINVAL;
  918. result = pci_reset_function(pdev);
  919. if (result < 0)
  920. return result;
  921. return count;
  922. }
  923. static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
  924. static int pci_create_capabilities_sysfs(struct pci_dev *dev)
  925. {
  926. int retval;
  927. struct bin_attribute *attr;
  928. /* If the device has VPD, try to expose it in sysfs. */
  929. if (dev->vpd) {
  930. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  931. if (!attr)
  932. return -ENOMEM;
  933. sysfs_bin_attr_init(attr);
  934. attr->size = dev->vpd->len;
  935. attr->attr.name = "vpd";
  936. attr->attr.mode = S_IRUSR | S_IWUSR;
  937. attr->read = read_vpd_attr;
  938. attr->write = write_vpd_attr;
  939. retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
  940. if (retval) {
  941. kfree(dev->vpd->attr);
  942. return retval;
  943. }
  944. dev->vpd->attr = attr;
  945. }
  946. /* Active State Power Management */
  947. pcie_aspm_create_sysfs_dev_files(dev);
  948. if (!pci_probe_reset_function(dev)) {
  949. retval = device_create_file(&dev->dev, &reset_attr);
  950. if (retval)
  951. goto error;
  952. dev->reset_fn = 1;
  953. }
  954. return 0;
  955. error:
  956. pcie_aspm_remove_sysfs_dev_files(dev);
  957. if (dev->vpd && dev->vpd->attr) {
  958. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  959. kfree(dev->vpd->attr);
  960. }
  961. return retval;
  962. }
  963. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  964. {
  965. int retval;
  966. int rom_size = 0;
  967. struct bin_attribute *attr;
  968. if (!sysfs_initialized)
  969. return -EACCES;
  970. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  971. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  972. else
  973. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  974. if (retval)
  975. goto err;
  976. retval = pci_create_resource_files(pdev);
  977. if (retval)
  978. goto err_config_file;
  979. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  980. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  981. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  982. rom_size = 0x20000;
  983. /* If the device has a ROM, try to expose it in sysfs. */
  984. if (rom_size) {
  985. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  986. if (!attr) {
  987. retval = -ENOMEM;
  988. goto err_resource_files;
  989. }
  990. sysfs_bin_attr_init(attr);
  991. attr->size = rom_size;
  992. attr->attr.name = "rom";
  993. attr->attr.mode = S_IRUSR | S_IWUSR;
  994. attr->read = pci_read_rom;
  995. attr->write = pci_write_rom;
  996. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  997. if (retval) {
  998. kfree(attr);
  999. goto err_resource_files;
  1000. }
  1001. pdev->rom_attr = attr;
  1002. }
  1003. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
  1004. retval = device_create_file(&pdev->dev, &vga_attr);
  1005. if (retval)
  1006. goto err_rom_file;
  1007. }
  1008. /* add platform-specific attributes */
  1009. retval = pcibios_add_platform_entries(pdev);
  1010. if (retval)
  1011. goto err_vga_file;
  1012. /* add sysfs entries for various capabilities */
  1013. retval = pci_create_capabilities_sysfs(pdev);
  1014. if (retval)
  1015. goto err_vga_file;
  1016. pci_create_firmware_label_files(pdev);
  1017. return 0;
  1018. err_vga_file:
  1019. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
  1020. device_remove_file(&pdev->dev, &vga_attr);
  1021. err_rom_file:
  1022. if (rom_size) {
  1023. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1024. kfree(pdev->rom_attr);
  1025. pdev->rom_attr = NULL;
  1026. }
  1027. err_resource_files:
  1028. pci_remove_resource_files(pdev);
  1029. err_config_file:
  1030. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1031. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1032. else
  1033. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1034. err:
  1035. return retval;
  1036. }
  1037. static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
  1038. {
  1039. if (dev->vpd && dev->vpd->attr) {
  1040. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1041. kfree(dev->vpd->attr);
  1042. }
  1043. pcie_aspm_remove_sysfs_dev_files(dev);
  1044. if (dev->reset_fn) {
  1045. device_remove_file(&dev->dev, &reset_attr);
  1046. dev->reset_fn = 0;
  1047. }
  1048. }
  1049. /**
  1050. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  1051. * @pdev: device whose entries we should free
  1052. *
  1053. * Cleanup when @pdev is removed from sysfs.
  1054. */
  1055. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  1056. {
  1057. int rom_size = 0;
  1058. if (!sysfs_initialized)
  1059. return;
  1060. pci_remove_capabilities_sysfs(pdev);
  1061. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1062. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1063. else
  1064. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1065. pci_remove_resource_files(pdev);
  1066. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1067. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1068. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1069. rom_size = 0x20000;
  1070. if (rom_size && pdev->rom_attr) {
  1071. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1072. kfree(pdev->rom_attr);
  1073. }
  1074. pci_remove_firmware_label_files(pdev);
  1075. }
  1076. static int __init pci_sysfs_init(void)
  1077. {
  1078. struct pci_dev *pdev = NULL;
  1079. int retval;
  1080. sysfs_initialized = 1;
  1081. for_each_pci_dev(pdev) {
  1082. retval = pci_create_sysfs_dev_files(pdev);
  1083. if (retval) {
  1084. pci_dev_put(pdev);
  1085. return retval;
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. late_initcall(pci_sysfs_init);