proc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Procfs interface for the PCI bus
  4. *
  5. * Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/capability.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/security.h>
  16. #include <asm/byteorder.h>
  17. #include "pci.h"
  18. static int proc_initialized; /* = 0 */
  19. static loff_t proc_bus_pci_lseek(struct file *file, loff_t off, int whence)
  20. {
  21. struct pci_dev *dev = pde_data(file_inode(file));
  22. return fixed_size_llseek(file, off, whence, dev->cfg_size);
  23. }
  24. static ssize_t proc_bus_pci_read(struct file *file, char __user *buf,
  25. size_t nbytes, loff_t *ppos)
  26. {
  27. struct pci_dev *dev = pde_data(file_inode(file));
  28. unsigned int pos = *ppos;
  29. unsigned int cnt, size;
  30. /*
  31. * Normal users can read only the standardized portion of the
  32. * configuration space as several chips lock up when trying to read
  33. * undefined locations (think of Intel PIIX4 as a typical example).
  34. */
  35. if (capable(CAP_SYS_ADMIN))
  36. size = dev->cfg_size;
  37. else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  38. size = 128;
  39. else
  40. size = 64;
  41. if (pos >= size)
  42. return 0;
  43. if (nbytes >= size)
  44. nbytes = size;
  45. if (pos + nbytes > size)
  46. nbytes = size - pos;
  47. cnt = nbytes;
  48. if (!access_ok(buf, cnt))
  49. return -EINVAL;
  50. pci_config_pm_runtime_get(dev);
  51. if ((pos & 1) && cnt) {
  52. unsigned char val;
  53. pci_user_read_config_byte(dev, pos, &val);
  54. __put_user(val, buf);
  55. buf++;
  56. pos++;
  57. cnt--;
  58. }
  59. if ((pos & 3) && cnt > 2) {
  60. unsigned short val;
  61. pci_user_read_config_word(dev, pos, &val);
  62. __put_user(cpu_to_le16(val), (__le16 __user *) buf);
  63. buf += 2;
  64. pos += 2;
  65. cnt -= 2;
  66. }
  67. while (cnt >= 4) {
  68. unsigned int val;
  69. pci_user_read_config_dword(dev, pos, &val);
  70. __put_user(cpu_to_le32(val), (__le32 __user *) buf);
  71. buf += 4;
  72. pos += 4;
  73. cnt -= 4;
  74. cond_resched();
  75. }
  76. if (cnt >= 2) {
  77. unsigned short val;
  78. pci_user_read_config_word(dev, pos, &val);
  79. __put_user(cpu_to_le16(val), (__le16 __user *) buf);
  80. buf += 2;
  81. pos += 2;
  82. cnt -= 2;
  83. }
  84. if (cnt) {
  85. unsigned char val;
  86. pci_user_read_config_byte(dev, pos, &val);
  87. __put_user(val, buf);
  88. pos++;
  89. }
  90. pci_config_pm_runtime_put(dev);
  91. *ppos = pos;
  92. return nbytes;
  93. }
  94. static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
  95. size_t nbytes, loff_t *ppos)
  96. {
  97. struct inode *ino = file_inode(file);
  98. struct pci_dev *dev = pde_data(ino);
  99. int pos = *ppos;
  100. int size = dev->cfg_size;
  101. int cnt, ret;
  102. ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
  103. if (ret)
  104. return ret;
  105. if (pos >= size)
  106. return 0;
  107. if (nbytes >= size)
  108. nbytes = size;
  109. if (pos + nbytes > size)
  110. nbytes = size - pos;
  111. cnt = nbytes;
  112. if (!access_ok(buf, cnt))
  113. return -EINVAL;
  114. pci_config_pm_runtime_get(dev);
  115. if ((pos & 1) && cnt) {
  116. unsigned char val;
  117. __get_user(val, buf);
  118. pci_user_write_config_byte(dev, pos, val);
  119. buf++;
  120. pos++;
  121. cnt--;
  122. }
  123. if ((pos & 3) && cnt > 2) {
  124. __le16 val;
  125. __get_user(val, (__le16 __user *) buf);
  126. pci_user_write_config_word(dev, pos, le16_to_cpu(val));
  127. buf += 2;
  128. pos += 2;
  129. cnt -= 2;
  130. }
  131. while (cnt >= 4) {
  132. __le32 val;
  133. __get_user(val, (__le32 __user *) buf);
  134. pci_user_write_config_dword(dev, pos, le32_to_cpu(val));
  135. buf += 4;
  136. pos += 4;
  137. cnt -= 4;
  138. }
  139. if (cnt >= 2) {
  140. __le16 val;
  141. __get_user(val, (__le16 __user *) buf);
  142. pci_user_write_config_word(dev, pos, le16_to_cpu(val));
  143. buf += 2;
  144. pos += 2;
  145. cnt -= 2;
  146. }
  147. if (cnt) {
  148. unsigned char val;
  149. __get_user(val, buf);
  150. pci_user_write_config_byte(dev, pos, val);
  151. pos++;
  152. }
  153. pci_config_pm_runtime_put(dev);
  154. *ppos = pos;
  155. i_size_write(ino, dev->cfg_size);
  156. return nbytes;
  157. }
  158. #ifdef HAVE_PCI_MMAP
  159. struct pci_filp_private {
  160. enum pci_mmap_state mmap_state;
  161. int write_combine;
  162. };
  163. #endif /* HAVE_PCI_MMAP */
  164. static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
  165. unsigned long arg)
  166. {
  167. struct pci_dev *dev = pde_data(file_inode(file));
  168. #ifdef HAVE_PCI_MMAP
  169. struct pci_filp_private *fpriv = file->private_data;
  170. #endif /* HAVE_PCI_MMAP */
  171. int ret = 0;
  172. ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
  173. if (ret)
  174. return ret;
  175. switch (cmd) {
  176. case PCIIOC_CONTROLLER:
  177. ret = pci_domain_nr(dev->bus);
  178. break;
  179. #ifdef HAVE_PCI_MMAP
  180. case PCIIOC_MMAP_IS_IO:
  181. if (!arch_can_pci_mmap_io())
  182. return -EINVAL;
  183. fpriv->mmap_state = pci_mmap_io;
  184. break;
  185. case PCIIOC_MMAP_IS_MEM:
  186. fpriv->mmap_state = pci_mmap_mem;
  187. break;
  188. case PCIIOC_WRITE_COMBINE:
  189. if (arch_can_pci_mmap_wc()) {
  190. if (arg)
  191. fpriv->write_combine = 1;
  192. else
  193. fpriv->write_combine = 0;
  194. break;
  195. }
  196. /* If arch decided it can't, fall through... */
  197. fallthrough;
  198. #endif /* HAVE_PCI_MMAP */
  199. default:
  200. ret = -EINVAL;
  201. break;
  202. }
  203. return ret;
  204. }
  205. #ifdef HAVE_PCI_MMAP
  206. static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
  207. {
  208. struct pci_dev *dev = pde_data(file_inode(file));
  209. struct pci_filp_private *fpriv = file->private_data;
  210. resource_size_t start, end;
  211. int i, ret, write_combine = 0, res_bit = IORESOURCE_MEM;
  212. if (!capable(CAP_SYS_RAWIO) ||
  213. security_locked_down(LOCKDOWN_PCI_ACCESS))
  214. return -EPERM;
  215. /* Skip devices with non-mappable BARs */
  216. if (dev->non_mappable_bars)
  217. return -EINVAL;
  218. if (fpriv->mmap_state == pci_mmap_io) {
  219. if (!arch_can_pci_mmap_io())
  220. return -EINVAL;
  221. res_bit = IORESOURCE_IO;
  222. }
  223. /* Make sure the caller is mapping a real resource for this device */
  224. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  225. if (dev->resource[i].flags & res_bit &&
  226. pci_mmap_fits(dev, i, vma, PCI_MMAP_PROCFS))
  227. break;
  228. }
  229. if (i >= PCI_STD_NUM_BARS)
  230. return -ENODEV;
  231. if (fpriv->mmap_state == pci_mmap_mem &&
  232. fpriv->write_combine) {
  233. if (dev->resource[i].flags & IORESOURCE_PREFETCH)
  234. write_combine = 1;
  235. else
  236. return -EINVAL;
  237. }
  238. if (dev->resource[i].flags & IORESOURCE_MEM &&
  239. iomem_is_exclusive(dev->resource[i].start))
  240. return -EINVAL;
  241. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  242. /* Adjust vm_pgoff to be the offset within the resource */
  243. vma->vm_pgoff -= start >> PAGE_SHIFT;
  244. ret = pci_mmap_resource_range(dev, i, vma,
  245. fpriv->mmap_state, write_combine);
  246. if (ret < 0)
  247. return ret;
  248. return 0;
  249. }
  250. static int proc_bus_pci_open(struct inode *inode, struct file *file)
  251. {
  252. struct pci_filp_private *fpriv = kmalloc_obj(*fpriv);
  253. if (!fpriv)
  254. return -ENOMEM;
  255. fpriv->mmap_state = pci_mmap_io;
  256. fpriv->write_combine = 0;
  257. file->private_data = fpriv;
  258. file->f_mapping = iomem_get_mapping();
  259. return 0;
  260. }
  261. static int proc_bus_pci_release(struct inode *inode, struct file *file)
  262. {
  263. kfree(file->private_data);
  264. file->private_data = NULL;
  265. return 0;
  266. }
  267. #endif /* HAVE_PCI_MMAP */
  268. static const struct proc_ops proc_bus_pci_ops = {
  269. .proc_lseek = proc_bus_pci_lseek,
  270. .proc_read = proc_bus_pci_read,
  271. .proc_write = proc_bus_pci_write,
  272. .proc_ioctl = proc_bus_pci_ioctl,
  273. #ifdef CONFIG_COMPAT
  274. .proc_compat_ioctl = proc_bus_pci_ioctl,
  275. #endif
  276. #ifdef HAVE_PCI_MMAP
  277. .proc_open = proc_bus_pci_open,
  278. .proc_release = proc_bus_pci_release,
  279. .proc_mmap = proc_bus_pci_mmap,
  280. #ifdef HAVE_ARCH_PCI_GET_UNMAPPED_AREA
  281. .proc_get_unmapped_area = get_pci_unmapped_area,
  282. #endif /* HAVE_ARCH_PCI_GET_UNMAPPED_AREA */
  283. #endif /* HAVE_PCI_MMAP */
  284. };
  285. /* iterator */
  286. static void *pci_seq_start(struct seq_file *m, loff_t *pos)
  287. {
  288. struct pci_dev *dev = NULL;
  289. loff_t n = *pos;
  290. for_each_pci_dev(dev) {
  291. if (!n--)
  292. break;
  293. }
  294. return dev;
  295. }
  296. static void *pci_seq_next(struct seq_file *m, void *v, loff_t *pos)
  297. {
  298. struct pci_dev *dev = v;
  299. (*pos)++;
  300. dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
  301. return dev;
  302. }
  303. static void pci_seq_stop(struct seq_file *m, void *v)
  304. {
  305. if (v) {
  306. struct pci_dev *dev = v;
  307. pci_dev_put(dev);
  308. }
  309. }
  310. static int show_device(struct seq_file *m, void *v)
  311. {
  312. const struct pci_dev *dev = v;
  313. const struct pci_driver *drv;
  314. int i;
  315. if (dev == NULL)
  316. return 0;
  317. drv = pci_dev_driver(dev);
  318. seq_printf(m, "%02x%02x\t%04x%04x\t%x",
  319. dev->bus->number,
  320. dev->devfn,
  321. dev->vendor,
  322. dev->device,
  323. dev->irq);
  324. /* only print standard and ROM resources to preserve compatibility */
  325. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  326. resource_size_t start, end;
  327. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  328. seq_printf(m, "\t%16llx",
  329. (unsigned long long)(start |
  330. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  331. }
  332. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  333. resource_size_t start, end;
  334. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  335. seq_printf(m, "\t%16llx",
  336. dev->resource[i].start < dev->resource[i].end ?
  337. (unsigned long long)(end - start) + 1 : 0);
  338. }
  339. seq_putc(m, '\t');
  340. if (drv)
  341. seq_puts(m, drv->name);
  342. seq_putc(m, '\n');
  343. return 0;
  344. }
  345. static const struct seq_operations proc_bus_pci_devices_op = {
  346. .start = pci_seq_start,
  347. .next = pci_seq_next,
  348. .stop = pci_seq_stop,
  349. .show = show_device
  350. };
  351. static struct proc_dir_entry *proc_bus_pci_dir;
  352. int pci_proc_attach_device(struct pci_dev *dev)
  353. {
  354. struct pci_bus *bus = dev->bus;
  355. struct proc_dir_entry *e;
  356. char name[16];
  357. if (!proc_initialized)
  358. return -EACCES;
  359. if (!bus->procdir) {
  360. if (pci_proc_domain(bus)) {
  361. sprintf(name, "%04x:%02x", pci_domain_nr(bus),
  362. bus->number);
  363. } else {
  364. sprintf(name, "%02x", bus->number);
  365. }
  366. bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
  367. if (!bus->procdir)
  368. return -ENOMEM;
  369. }
  370. sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  371. e = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUSR, bus->procdir,
  372. &proc_bus_pci_ops, dev);
  373. if (!e)
  374. return -ENOMEM;
  375. proc_set_size(e, dev->cfg_size);
  376. dev->procent = e;
  377. return 0;
  378. }
  379. int pci_proc_detach_device(struct pci_dev *dev)
  380. {
  381. proc_remove(dev->procent);
  382. dev->procent = NULL;
  383. return 0;
  384. }
  385. int pci_proc_detach_bus(struct pci_bus *bus)
  386. {
  387. proc_remove(bus->procdir);
  388. return 0;
  389. }
  390. static int __init pci_proc_init(void)
  391. {
  392. struct pci_dev *dev = NULL;
  393. proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
  394. proc_create_seq("devices", 0, proc_bus_pci_dir,
  395. &proc_bus_pci_devices_op);
  396. proc_initialized = 1;
  397. for_each_pci_dev(dev)
  398. pci_proc_attach_device(dev);
  399. return 0;
  400. }
  401. device_initcall(pci_proc_init);