proc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 <asm/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. fpriv->mmap_state = pci_mmap_io;
  175. break;
  176. case PCIIOC_MMAP_IS_MEM:
  177. fpriv->mmap_state = pci_mmap_mem;
  178. break;
  179. case PCIIOC_WRITE_COMBINE:
  180. if (arg)
  181. fpriv->write_combine = 1;
  182. else
  183. fpriv->write_combine = 0;
  184. break;
  185. #endif /* HAVE_PCI_MMAP */
  186. default:
  187. ret = -EINVAL;
  188. break;
  189. }
  190. return ret;
  191. }
  192. #ifdef HAVE_PCI_MMAP
  193. static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
  194. {
  195. struct pci_dev *dev = PDE_DATA(file_inode(file));
  196. struct pci_filp_private *fpriv = file->private_data;
  197. int i, ret, write_combine = 0, res_bit;
  198. if (!capable(CAP_SYS_RAWIO))
  199. return -EPERM;
  200. if (fpriv->mmap_state == pci_mmap_io)
  201. res_bit = IORESOURCE_IO;
  202. else
  203. res_bit = IORESOURCE_MEM;
  204. /* Make sure the caller is mapping a real resource for this device */
  205. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  206. if (dev->resource[i].flags & res_bit &&
  207. pci_mmap_fits(dev, i, vma, PCI_MMAP_PROCFS))
  208. break;
  209. }
  210. if (i >= PCI_ROM_RESOURCE)
  211. return -ENODEV;
  212. if (fpriv->mmap_state == pci_mmap_mem &&
  213. fpriv->write_combine) {
  214. if (dev->resource[i].flags & IORESOURCE_PREFETCH)
  215. write_combine = 1;
  216. else
  217. return -EINVAL;
  218. }
  219. ret = pci_mmap_page_range(dev, vma,
  220. fpriv->mmap_state, write_combine);
  221. if (ret < 0)
  222. return ret;
  223. return 0;
  224. }
  225. static int proc_bus_pci_open(struct inode *inode, struct file *file)
  226. {
  227. struct pci_filp_private *fpriv = kmalloc(sizeof(*fpriv), GFP_KERNEL);
  228. if (!fpriv)
  229. return -ENOMEM;
  230. fpriv->mmap_state = pci_mmap_io;
  231. fpriv->write_combine = 0;
  232. file->private_data = fpriv;
  233. return 0;
  234. }
  235. static int proc_bus_pci_release(struct inode *inode, struct file *file)
  236. {
  237. kfree(file->private_data);
  238. file->private_data = NULL;
  239. return 0;
  240. }
  241. #endif /* HAVE_PCI_MMAP */
  242. static const struct file_operations proc_bus_pci_operations = {
  243. .owner = THIS_MODULE,
  244. .llseek = proc_bus_pci_lseek,
  245. .read = proc_bus_pci_read,
  246. .write = proc_bus_pci_write,
  247. .unlocked_ioctl = proc_bus_pci_ioctl,
  248. .compat_ioctl = proc_bus_pci_ioctl,
  249. #ifdef HAVE_PCI_MMAP
  250. .open = proc_bus_pci_open,
  251. .release = proc_bus_pci_release,
  252. .mmap = proc_bus_pci_mmap,
  253. #ifdef HAVE_ARCH_PCI_GET_UNMAPPED_AREA
  254. .get_unmapped_area = get_pci_unmapped_area,
  255. #endif /* HAVE_ARCH_PCI_GET_UNMAPPED_AREA */
  256. #endif /* HAVE_PCI_MMAP */
  257. };
  258. /* iterator */
  259. static void *pci_seq_start(struct seq_file *m, loff_t *pos)
  260. {
  261. struct pci_dev *dev = NULL;
  262. loff_t n = *pos;
  263. for_each_pci_dev(dev) {
  264. if (!n--)
  265. break;
  266. }
  267. return dev;
  268. }
  269. static void *pci_seq_next(struct seq_file *m, void *v, loff_t *pos)
  270. {
  271. struct pci_dev *dev = v;
  272. (*pos)++;
  273. dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
  274. return dev;
  275. }
  276. static void pci_seq_stop(struct seq_file *m, void *v)
  277. {
  278. if (v) {
  279. struct pci_dev *dev = v;
  280. pci_dev_put(dev);
  281. }
  282. }
  283. static int show_device(struct seq_file *m, void *v)
  284. {
  285. const struct pci_dev *dev = v;
  286. const struct pci_driver *drv;
  287. int i;
  288. if (dev == NULL)
  289. return 0;
  290. drv = pci_dev_driver(dev);
  291. seq_printf(m, "%02x%02x\t%04x%04x\t%x",
  292. dev->bus->number,
  293. dev->devfn,
  294. dev->vendor,
  295. dev->device,
  296. dev->irq);
  297. /* only print standard and ROM resources to preserve compatibility */
  298. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  299. resource_size_t start, end;
  300. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  301. seq_printf(m, "\t%16llx",
  302. (unsigned long long)(start |
  303. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  304. }
  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. dev->resource[i].start < dev->resource[i].end ?
  310. (unsigned long long)(end - start) + 1 : 0);
  311. }
  312. seq_putc(m, '\t');
  313. if (drv)
  314. seq_printf(m, "%s", drv->name);
  315. seq_putc(m, '\n');
  316. return 0;
  317. }
  318. static const struct seq_operations proc_bus_pci_devices_op = {
  319. .start = pci_seq_start,
  320. .next = pci_seq_next,
  321. .stop = pci_seq_stop,
  322. .show = show_device
  323. };
  324. static struct proc_dir_entry *proc_bus_pci_dir;
  325. int pci_proc_attach_device(struct pci_dev *dev)
  326. {
  327. struct pci_bus *bus = dev->bus;
  328. struct proc_dir_entry *e;
  329. char name[16];
  330. if (!proc_initialized)
  331. return -EACCES;
  332. if (!bus->procdir) {
  333. if (pci_proc_domain(bus)) {
  334. sprintf(name, "%04x:%02x", pci_domain_nr(bus),
  335. bus->number);
  336. } else {
  337. sprintf(name, "%02x", bus->number);
  338. }
  339. bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
  340. if (!bus->procdir)
  341. return -ENOMEM;
  342. }
  343. sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  344. e = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUSR, bus->procdir,
  345. &proc_bus_pci_operations, dev);
  346. if (!e)
  347. return -ENOMEM;
  348. proc_set_size(e, dev->cfg_size);
  349. dev->procent = e;
  350. return 0;
  351. }
  352. int pci_proc_detach_device(struct pci_dev *dev)
  353. {
  354. proc_remove(dev->procent);
  355. dev->procent = NULL;
  356. return 0;
  357. }
  358. int pci_proc_detach_bus(struct pci_bus *bus)
  359. {
  360. proc_remove(bus->procdir);
  361. return 0;
  362. }
  363. static int proc_bus_pci_dev_open(struct inode *inode, struct file *file)
  364. {
  365. return seq_open(file, &proc_bus_pci_devices_op);
  366. }
  367. static const struct file_operations proc_bus_pci_dev_operations = {
  368. .owner = THIS_MODULE,
  369. .open = proc_bus_pci_dev_open,
  370. .read = seq_read,
  371. .llseek = seq_lseek,
  372. .release = seq_release,
  373. };
  374. static int __init pci_proc_init(void)
  375. {
  376. struct pci_dev *dev = NULL;
  377. proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
  378. proc_create("devices", 0, proc_bus_pci_dir,
  379. &proc_bus_pci_dev_operations);
  380. proc_initialized = 1;
  381. for_each_pci_dev(dev)
  382. pci_proc_attach_device(dev);
  383. return 0;
  384. }
  385. device_initcall(pci_proc_init);