proc.c 9.1 KB

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