pci-sysfs.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. {
  612. unsigned long nr, start, size;
  613. nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  614. start = vma->vm_pgoff;
  615. size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
  616. if (start < size && size - start >= nr)
  617. return 1;
  618. WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
  619. current->comm, start, start+nr, pci_name(pdev), resno, size);
  620. return 0;
  621. }
  622. /**
  623. * pci_mmap_resource - map a PCI resource into user memory space
  624. * @kobj: kobject for mapping
  625. * @attr: struct bin_attribute for the file being mapped
  626. * @vma: struct vm_area_struct passed into the mmap
  627. * @write_combine: 1 for write_combine mapping
  628. *
  629. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  630. */
  631. static int
  632. pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
  633. struct vm_area_struct *vma, int write_combine)
  634. {
  635. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  636. struct device, kobj));
  637. struct resource *res = attr->private;
  638. enum pci_mmap_state mmap_type;
  639. resource_size_t start, end;
  640. int i;
  641. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  642. if (res == &pdev->resource[i])
  643. break;
  644. if (i >= PCI_ROM_RESOURCE)
  645. return -ENODEV;
  646. if (!pci_mmap_fits(pdev, i, vma))
  647. return -EINVAL;
  648. /* pci_mmap_page_range() expects the same kind of entry as coming
  649. * from /proc/bus/pci/ which is a "user visible" value. If this is
  650. * different from the resource itself, arch will do necessary fixup.
  651. */
  652. pci_resource_to_user(pdev, i, res, &start, &end);
  653. vma->vm_pgoff += start >> PAGE_SHIFT;
  654. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  655. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
  656. return -EINVAL;
  657. return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
  658. }
  659. static int
  660. pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
  661. struct bin_attribute *attr,
  662. struct vm_area_struct *vma)
  663. {
  664. return pci_mmap_resource(kobj, attr, vma, 0);
  665. }
  666. static int
  667. pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
  668. struct bin_attribute *attr,
  669. struct vm_area_struct *vma)
  670. {
  671. return pci_mmap_resource(kobj, attr, vma, 1);
  672. }
  673. static ssize_t
  674. pci_resource_io(struct file *filp, struct kobject *kobj,
  675. struct bin_attribute *attr, char *buf,
  676. loff_t off, size_t count, bool write)
  677. {
  678. struct pci_dev *pdev = to_pci_dev(container_of(kobj,
  679. struct device, kobj));
  680. struct resource *res = attr->private;
  681. unsigned long port = off;
  682. int i;
  683. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  684. if (res == &pdev->resource[i])
  685. break;
  686. if (i >= PCI_ROM_RESOURCE)
  687. return -ENODEV;
  688. port += pci_resource_start(pdev, i);
  689. if (port > pci_resource_end(pdev, i))
  690. return 0;
  691. if (port + count - 1 > pci_resource_end(pdev, i))
  692. return -EINVAL;
  693. switch (count) {
  694. case 1:
  695. if (write)
  696. outb(*(u8 *)buf, port);
  697. else
  698. *(u8 *)buf = inb(port);
  699. return 1;
  700. case 2:
  701. if (write)
  702. outw(*(u16 *)buf, port);
  703. else
  704. *(u16 *)buf = inw(port);
  705. return 2;
  706. case 4:
  707. if (write)
  708. outl(*(u32 *)buf, port);
  709. else
  710. *(u32 *)buf = inl(port);
  711. return 4;
  712. }
  713. return -EINVAL;
  714. }
  715. static ssize_t
  716. pci_read_resource_io(struct file *filp, struct kobject *kobj,
  717. struct bin_attribute *attr, char *buf,
  718. loff_t off, size_t count)
  719. {
  720. return pci_resource_io(filp, kobj, attr, buf, off, count, false);
  721. }
  722. static ssize_t
  723. pci_write_resource_io(struct file *filp, struct kobject *kobj,
  724. struct bin_attribute *attr, char *buf,
  725. loff_t off, size_t count)
  726. {
  727. return pci_resource_io(filp, kobj, attr, buf, off, count, true);
  728. }
  729. /**
  730. * pci_remove_resource_files - cleanup resource files
  731. * @pdev: dev to cleanup
  732. *
  733. * If we created resource files for @pdev, remove them from sysfs and
  734. * free their resources.
  735. */
  736. static void
  737. pci_remove_resource_files(struct pci_dev *pdev)
  738. {
  739. int i;
  740. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  741. struct bin_attribute *res_attr;
  742. res_attr = pdev->res_attr[i];
  743. if (res_attr) {
  744. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  745. kfree(res_attr);
  746. }
  747. res_attr = pdev->res_attr_wc[i];
  748. if (res_attr) {
  749. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  750. kfree(res_attr);
  751. }
  752. }
  753. }
  754. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  755. {
  756. /* allocate attribute structure, piggyback attribute name */
  757. int name_len = write_combine ? 13 : 10;
  758. struct bin_attribute *res_attr;
  759. int retval;
  760. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  761. if (res_attr) {
  762. char *res_attr_name = (char *)(res_attr + 1);
  763. sysfs_bin_attr_init(res_attr);
  764. if (write_combine) {
  765. pdev->res_attr_wc[num] = res_attr;
  766. sprintf(res_attr_name, "resource%d_wc", num);
  767. res_attr->mmap = pci_mmap_resource_wc;
  768. } else {
  769. pdev->res_attr[num] = res_attr;
  770. sprintf(res_attr_name, "resource%d", num);
  771. res_attr->mmap = pci_mmap_resource_uc;
  772. }
  773. if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
  774. res_attr->read = pci_read_resource_io;
  775. res_attr->write = pci_write_resource_io;
  776. }
  777. res_attr->attr.name = res_attr_name;
  778. res_attr->attr.mode = S_IRUSR | S_IWUSR;
  779. res_attr->size = pci_resource_len(pdev, num);
  780. res_attr->private = &pdev->resource[num];
  781. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  782. } else
  783. retval = -ENOMEM;
  784. return retval;
  785. }
  786. /**
  787. * pci_create_resource_files - create resource files in sysfs for @dev
  788. * @pdev: dev in question
  789. *
  790. * Walk the resources in @pdev creating files for each resource available.
  791. */
  792. static int pci_create_resource_files(struct pci_dev *pdev)
  793. {
  794. int i;
  795. int retval;
  796. /* Expose the PCI resources from this device as files */
  797. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  798. /* skip empty resources */
  799. if (!pci_resource_len(pdev, i))
  800. continue;
  801. retval = pci_create_attr(pdev, i, 0);
  802. /* for prefetchable resources, create a WC mappable file */
  803. if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
  804. retval = pci_create_attr(pdev, i, 1);
  805. if (retval) {
  806. pci_remove_resource_files(pdev);
  807. return retval;
  808. }
  809. }
  810. return 0;
  811. }
  812. #else /* !HAVE_PCI_MMAP */
  813. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  814. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  815. #endif /* HAVE_PCI_MMAP */
  816. /**
  817. * pci_write_rom - used to enable access to the PCI ROM display
  818. * @filp: sysfs file
  819. * @kobj: kernel object handle
  820. * @bin_attr: struct bin_attribute for this file
  821. * @buf: user input
  822. * @off: file offset
  823. * @count: number of byte in input
  824. *
  825. * writing anything except 0 enables it
  826. */
  827. static ssize_t
  828. pci_write_rom(struct file *filp, struct kobject *kobj,
  829. struct bin_attribute *bin_attr,
  830. char *buf, loff_t off, size_t count)
  831. {
  832. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  833. if ((off == 0) && (*buf == '0') && (count == 2))
  834. pdev->rom_attr_enabled = 0;
  835. else
  836. pdev->rom_attr_enabled = 1;
  837. return count;
  838. }
  839. /**
  840. * pci_read_rom - read a PCI ROM
  841. * @filp: sysfs file
  842. * @kobj: kernel object handle
  843. * @bin_attr: struct bin_attribute for this file
  844. * @buf: where to put the data we read from the ROM
  845. * @off: file offset
  846. * @count: number of bytes to read
  847. *
  848. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  849. * device corresponding to @kobj.
  850. */
  851. static ssize_t
  852. pci_read_rom(struct file *filp, struct kobject *kobj,
  853. struct bin_attribute *bin_attr,
  854. char *buf, loff_t off, size_t count)
  855. {
  856. struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
  857. void __iomem *rom;
  858. size_t size;
  859. if (!pdev->rom_attr_enabled)
  860. return -EINVAL;
  861. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  862. if (!rom || !size)
  863. return -EIO;
  864. if (off >= size)
  865. count = 0;
  866. else {
  867. if (off + count > size)
  868. count = size - off;
  869. memcpy_fromio(buf, rom + off, count);
  870. }
  871. pci_unmap_rom(pdev, rom);
  872. return count;
  873. }
  874. static struct bin_attribute pci_config_attr = {
  875. .attr = {
  876. .name = "config",
  877. .mode = S_IRUGO | S_IWUSR,
  878. },
  879. .size = PCI_CFG_SPACE_SIZE,
  880. .read = pci_read_config,
  881. .write = pci_write_config,
  882. };
  883. static struct bin_attribute pcie_config_attr = {
  884. .attr = {
  885. .name = "config",
  886. .mode = S_IRUGO | S_IWUSR,
  887. },
  888. .size = PCI_CFG_SPACE_EXP_SIZE,
  889. .read = pci_read_config,
  890. .write = pci_write_config,
  891. };
  892. int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
  893. {
  894. return 0;
  895. }
  896. static ssize_t reset_store(struct device *dev,
  897. struct device_attribute *attr, const char *buf,
  898. size_t count)
  899. {
  900. struct pci_dev *pdev = to_pci_dev(dev);
  901. unsigned long val;
  902. ssize_t result = strict_strtoul(buf, 0, &val);
  903. if (result < 0)
  904. return result;
  905. if (val != 1)
  906. return -EINVAL;
  907. result = pci_reset_function(pdev);
  908. if (result < 0)
  909. return result;
  910. return count;
  911. }
  912. static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
  913. static int pci_create_capabilities_sysfs(struct pci_dev *dev)
  914. {
  915. int retval;
  916. struct bin_attribute *attr;
  917. /* If the device has VPD, try to expose it in sysfs. */
  918. if (dev->vpd) {
  919. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  920. if (!attr)
  921. return -ENOMEM;
  922. sysfs_bin_attr_init(attr);
  923. attr->size = dev->vpd->len;
  924. attr->attr.name = "vpd";
  925. attr->attr.mode = S_IRUSR | S_IWUSR;
  926. attr->read = read_vpd_attr;
  927. attr->write = write_vpd_attr;
  928. retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
  929. if (retval) {
  930. kfree(dev->vpd->attr);
  931. return retval;
  932. }
  933. dev->vpd->attr = attr;
  934. }
  935. /* Active State Power Management */
  936. pcie_aspm_create_sysfs_dev_files(dev);
  937. if (!pci_probe_reset_function(dev)) {
  938. retval = device_create_file(&dev->dev, &reset_attr);
  939. if (retval)
  940. goto error;
  941. dev->reset_fn = 1;
  942. }
  943. return 0;
  944. error:
  945. pcie_aspm_remove_sysfs_dev_files(dev);
  946. if (dev->vpd && dev->vpd->attr) {
  947. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  948. kfree(dev->vpd->attr);
  949. }
  950. return retval;
  951. }
  952. int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
  953. {
  954. int retval;
  955. int rom_size = 0;
  956. struct bin_attribute *attr;
  957. if (!sysfs_initialized)
  958. return -EACCES;
  959. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  960. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
  961. else
  962. retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  963. if (retval)
  964. goto err;
  965. retval = pci_create_resource_files(pdev);
  966. if (retval)
  967. goto err_config_file;
  968. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  969. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  970. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  971. rom_size = 0x20000;
  972. /* If the device has a ROM, try to expose it in sysfs. */
  973. if (rom_size) {
  974. attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
  975. if (!attr) {
  976. retval = -ENOMEM;
  977. goto err_resource_files;
  978. }
  979. sysfs_bin_attr_init(attr);
  980. attr->size = rom_size;
  981. attr->attr.name = "rom";
  982. attr->attr.mode = S_IRUSR;
  983. attr->read = pci_read_rom;
  984. attr->write = pci_write_rom;
  985. retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
  986. if (retval) {
  987. kfree(attr);
  988. goto err_resource_files;
  989. }
  990. pdev->rom_attr = attr;
  991. }
  992. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
  993. retval = device_create_file(&pdev->dev, &vga_attr);
  994. if (retval)
  995. goto err_rom_file;
  996. }
  997. /* add platform-specific attributes */
  998. retval = pcibios_add_platform_entries(pdev);
  999. if (retval)
  1000. goto err_vga_file;
  1001. /* add sysfs entries for various capabilities */
  1002. retval = pci_create_capabilities_sysfs(pdev);
  1003. if (retval)
  1004. goto err_vga_file;
  1005. pci_create_firmware_label_files(pdev);
  1006. return 0;
  1007. err_vga_file:
  1008. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
  1009. device_remove_file(&pdev->dev, &vga_attr);
  1010. err_rom_file:
  1011. if (rom_size) {
  1012. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1013. kfree(pdev->rom_attr);
  1014. pdev->rom_attr = NULL;
  1015. }
  1016. err_resource_files:
  1017. pci_remove_resource_files(pdev);
  1018. err_config_file:
  1019. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1020. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1021. else
  1022. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1023. err:
  1024. return retval;
  1025. }
  1026. static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
  1027. {
  1028. if (dev->vpd && dev->vpd->attr) {
  1029. sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
  1030. kfree(dev->vpd->attr);
  1031. }
  1032. pcie_aspm_remove_sysfs_dev_files(dev);
  1033. if (dev->reset_fn) {
  1034. device_remove_file(&dev->dev, &reset_attr);
  1035. dev->reset_fn = 0;
  1036. }
  1037. }
  1038. /**
  1039. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  1040. * @pdev: device whose entries we should free
  1041. *
  1042. * Cleanup when @pdev is removed from sysfs.
  1043. */
  1044. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  1045. {
  1046. int rom_size = 0;
  1047. if (!sysfs_initialized)
  1048. return;
  1049. pci_remove_capabilities_sysfs(pdev);
  1050. if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
  1051. sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
  1052. else
  1053. sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
  1054. pci_remove_resource_files(pdev);
  1055. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  1056. rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1057. else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  1058. rom_size = 0x20000;
  1059. if (rom_size && pdev->rom_attr) {
  1060. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  1061. kfree(pdev->rom_attr);
  1062. }
  1063. pci_remove_firmware_label_files(pdev);
  1064. }
  1065. static int __init pci_sysfs_init(void)
  1066. {
  1067. struct pci_dev *pdev = NULL;
  1068. int retval;
  1069. sysfs_initialized = 1;
  1070. for_each_pci_dev(pdev) {
  1071. retval = pci_create_sysfs_dev_files(pdev);
  1072. if (retval) {
  1073. pci_dev_put(pdev);
  1074. return retval;
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. late_initcall(pci_sysfs_init);