proc.c 9.6 KB

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