proc.c 10 KB

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