proc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Procfs interface for the PCI bus
  4. *
  5. * Copyright (c) 1997--1999 Martin Mares <[email protected]>
  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. if (fpriv->mmap_state == pci_mmap_io) {
  216. if (!arch_can_pci_mmap_io())
  217. return -EINVAL;
  218. res_bit = IORESOURCE_IO;
  219. }
  220. /* Make sure the caller is mapping a real resource for this device */
  221. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  222. if (dev->resource[i].flags & res_bit &&
  223. pci_mmap_fits(dev, i, vma, PCI_MMAP_PROCFS))
  224. break;
  225. }
  226. if (i >= PCI_STD_NUM_BARS)
  227. return -ENODEV;
  228. if (fpriv->mmap_state == pci_mmap_mem &&
  229. fpriv->write_combine) {
  230. if (dev->resource[i].flags & IORESOURCE_PREFETCH)
  231. write_combine = 1;
  232. else
  233. return -EINVAL;
  234. }
  235. if (dev->resource[i].flags & IORESOURCE_MEM &&
  236. iomem_is_exclusive(dev->resource[i].start))
  237. return -EINVAL;
  238. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  239. /* Adjust vm_pgoff to be the offset within the resource */
  240. vma->vm_pgoff -= start >> PAGE_SHIFT;
  241. ret = pci_mmap_resource_range(dev, i, vma,
  242. fpriv->mmap_state, write_combine);
  243. if (ret < 0)
  244. return ret;
  245. return 0;
  246. }
  247. static int proc_bus_pci_open(struct inode *inode, struct file *file)
  248. {
  249. struct pci_filp_private *fpriv = kmalloc(sizeof(*fpriv), GFP_KERNEL);
  250. if (!fpriv)
  251. return -ENOMEM;
  252. fpriv->mmap_state = pci_mmap_io;
  253. fpriv->write_combine = 0;
  254. file->private_data = fpriv;
  255. file->f_mapping = iomem_get_mapping();
  256. return 0;
  257. }
  258. static int proc_bus_pci_release(struct inode *inode, struct file *file)
  259. {
  260. kfree(file->private_data);
  261. file->private_data = NULL;
  262. return 0;
  263. }
  264. #endif /* HAVE_PCI_MMAP */
  265. static const struct proc_ops proc_bus_pci_ops = {
  266. .proc_lseek = proc_bus_pci_lseek,
  267. .proc_read = proc_bus_pci_read,
  268. .proc_write = proc_bus_pci_write,
  269. .proc_ioctl = proc_bus_pci_ioctl,
  270. #ifdef CONFIG_COMPAT
  271. .proc_compat_ioctl = proc_bus_pci_ioctl,
  272. #endif
  273. #ifdef HAVE_PCI_MMAP
  274. .proc_open = proc_bus_pci_open,
  275. .proc_release = proc_bus_pci_release,
  276. .proc_mmap = proc_bus_pci_mmap,
  277. #ifdef HAVE_ARCH_PCI_GET_UNMAPPED_AREA
  278. .proc_get_unmapped_area = get_pci_unmapped_area,
  279. #endif /* HAVE_ARCH_PCI_GET_UNMAPPED_AREA */
  280. #endif /* HAVE_PCI_MMAP */
  281. };
  282. /* iterator */
  283. static void *pci_seq_start(struct seq_file *m, loff_t *pos)
  284. {
  285. struct pci_dev *dev = NULL;
  286. loff_t n = *pos;
  287. for_each_pci_dev(dev) {
  288. if (!n--)
  289. break;
  290. }
  291. return dev;
  292. }
  293. static void *pci_seq_next(struct seq_file *m, void *v, loff_t *pos)
  294. {
  295. struct pci_dev *dev = v;
  296. (*pos)++;
  297. dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
  298. return dev;
  299. }
  300. static void pci_seq_stop(struct seq_file *m, void *v)
  301. {
  302. if (v) {
  303. struct pci_dev *dev = v;
  304. pci_dev_put(dev);
  305. }
  306. }
  307. static int show_device(struct seq_file *m, void *v)
  308. {
  309. const struct pci_dev *dev = v;
  310. const struct pci_driver *drv;
  311. int i;
  312. if (dev == NULL)
  313. return 0;
  314. drv = pci_dev_driver(dev);
  315. seq_printf(m, "%02x%02x\t%04x%04x\t%x",
  316. dev->bus->number,
  317. dev->devfn,
  318. dev->vendor,
  319. dev->device,
  320. dev->irq);
  321. /* only print standard and ROM resources to preserve compatibility */
  322. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  323. resource_size_t start, end;
  324. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  325. seq_printf(m, "\t%16llx",
  326. (unsigned long long)(start |
  327. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  328. }
  329. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  330. resource_size_t start, end;
  331. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  332. seq_printf(m, "\t%16llx",
  333. dev->resource[i].start < dev->resource[i].end ?
  334. (unsigned long long)(end - start) + 1 : 0);
  335. }
  336. seq_putc(m, '\t');
  337. if (drv)
  338. seq_puts(m, drv->name);
  339. seq_putc(m, '\n');
  340. return 0;
  341. }
  342. static const struct seq_operations proc_bus_pci_devices_op = {
  343. .start = pci_seq_start,
  344. .next = pci_seq_next,
  345. .stop = pci_seq_stop,
  346. .show = show_device
  347. };
  348. static struct proc_dir_entry *proc_bus_pci_dir;
  349. int pci_proc_attach_device(struct pci_dev *dev)
  350. {
  351. struct pci_bus *bus = dev->bus;
  352. struct proc_dir_entry *e;
  353. char name[16];
  354. if (!proc_initialized)
  355. return -EACCES;
  356. if (!bus->procdir) {
  357. if (pci_proc_domain(bus)) {
  358. sprintf(name, "%04x:%02x", pci_domain_nr(bus),
  359. bus->number);
  360. } else {
  361. sprintf(name, "%02x", bus->number);
  362. }
  363. bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
  364. if (!bus->procdir)
  365. return -ENOMEM;
  366. }
  367. sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  368. e = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUSR, bus->procdir,
  369. &proc_bus_pci_ops, dev);
  370. if (!e)
  371. return -ENOMEM;
  372. proc_set_size(e, dev->cfg_size);
  373. dev->procent = e;
  374. return 0;
  375. }
  376. int pci_proc_detach_device(struct pci_dev *dev)
  377. {
  378. proc_remove(dev->procent);
  379. dev->procent = NULL;
  380. return 0;
  381. }
  382. int pci_proc_detach_bus(struct pci_bus *bus)
  383. {
  384. proc_remove(bus->procdir);
  385. return 0;
  386. }
  387. static int __init pci_proc_init(void)
  388. {
  389. struct pci_dev *dev = NULL;
  390. proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
  391. proc_create_seq("devices", 0, proc_bus_pci_dir,
  392. &proc_bus_pci_devices_op);
  393. proc_initialized = 1;
  394. for_each_pci_dev(dev)
  395. pci_proc_attach_device(dev);
  396. return 0;
  397. }
  398. device_initcall(pci_proc_init);