proc.c 9.6 KB

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