blob: 0b009470e6db548005b6f66539b18e372e8f2261 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Procfs interface for the PCI bus.
3 *
4 * Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
5 */
6
7#include <linux/init.h>
8#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/proc_fs.h>
12#include <linux/seq_file.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15#include <asm/byteorder.h>
Greg KHbc56b9e2005-04-08 14:53:31 +090016#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static int proc_initialized; /* = 0 */
19
20static loff_t
21proc_bus_pci_lseek(struct file *file, loff_t off, int whence)
22{
23 loff_t new = -1;
Al Viro496ad9a2013-01-23 17:07:38 -050024 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080026 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 switch (whence) {
28 case 0:
29 new = off;
30 break;
31 case 1:
32 new = file->f_pos + off;
33 break;
34 case 2:
35 new = inode->i_size + off;
36 break;
37 }
38 if (new < 0 || new > inode->i_size)
39 new = -EINVAL;
40 else
41 file->f_pos = new;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080042 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return new;
44}
45
46static ssize_t
47proc_bus_pci_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
48{
Al Viro496ad9a2013-01-23 17:07:38 -050049 const struct inode *ino = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 const struct proc_dir_entry *dp = PDE(ino);
51 struct pci_dev *dev = dp->data;
52 unsigned int pos = *ppos;
53 unsigned int cnt, size;
54
55 /*
56 * Normal users can read only the standardized portion of the
57 * configuration space as several chips lock up when trying to read
58 * undefined locations (think of Intel PIIX4 as a typical example).
59 */
60
61 if (capable(CAP_SYS_ADMIN))
David Rientjescd686022007-09-27 13:41:16 -070062 size = dp->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
64 size = 128;
65 else
66 size = 64;
67
68 if (pos >= size)
69 return 0;
70 if (nbytes >= size)
71 nbytes = size;
72 if (pos + nbytes > size)
73 nbytes = size - pos;
74 cnt = nbytes;
75
76 if (!access_ok(VERIFY_WRITE, buf, cnt))
77 return -EINVAL;
78
Huang Yingb3c32c42012-10-25 09:36:03 +080079 pci_config_pm_runtime_get(dev);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if ((pos & 1) && cnt) {
82 unsigned char val;
Brian Kinge04b0ea2005-09-27 01:21:55 -070083 pci_user_read_config_byte(dev, pos, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 __put_user(val, buf);
85 buf++;
86 pos++;
87 cnt--;
88 }
89
90 if ((pos & 3) && cnt > 2) {
91 unsigned short val;
Brian Kinge04b0ea2005-09-27 01:21:55 -070092 pci_user_read_config_word(dev, pos, &val);
Harvey Harrisonf17a0772008-07-22 14:40:47 -070093 __put_user(cpu_to_le16(val), (__le16 __user *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 buf += 2;
95 pos += 2;
96 cnt -= 2;
97 }
98
99 while (cnt >= 4) {
100 unsigned int val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700101 pci_user_read_config_dword(dev, pos, &val);
Harvey Harrisonf17a0772008-07-22 14:40:47 -0700102 __put_user(cpu_to_le32(val), (__le32 __user *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 buf += 4;
104 pos += 4;
105 cnt -= 4;
106 }
107
108 if (cnt >= 2) {
109 unsigned short val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700110 pci_user_read_config_word(dev, pos, &val);
Harvey Harrisonf17a0772008-07-22 14:40:47 -0700111 __put_user(cpu_to_le16(val), (__le16 __user *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 buf += 2;
113 pos += 2;
114 cnt -= 2;
115 }
116
117 if (cnt) {
118 unsigned char val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700119 pci_user_read_config_byte(dev, pos, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 __put_user(val, buf);
121 buf++;
122 pos++;
123 cnt--;
124 }
125
Huang Yingb3c32c42012-10-25 09:36:03 +0800126 pci_config_pm_runtime_put(dev);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 *ppos = pos;
129 return nbytes;
130}
131
132static ssize_t
133proc_bus_pci_write(struct file *file, const char __user *buf, size_t nbytes, loff_t *ppos)
134{
Al Viro496ad9a2013-01-23 17:07:38 -0500135 struct inode *ino = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 const struct proc_dir_entry *dp = PDE(ino);
137 struct pci_dev *dev = dp->data;
138 int pos = *ppos;
David Rientjescd686022007-09-27 13:41:16 -0700139 int size = dp->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int cnt;
141
142 if (pos >= size)
143 return 0;
144 if (nbytes >= size)
145 nbytes = size;
146 if (pos + nbytes > size)
147 nbytes = size - pos;
148 cnt = nbytes;
149
150 if (!access_ok(VERIFY_READ, buf, cnt))
151 return -EINVAL;
152
Huang Yingb3c32c42012-10-25 09:36:03 +0800153 pci_config_pm_runtime_get(dev);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if ((pos & 1) && cnt) {
156 unsigned char val;
157 __get_user(val, buf);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700158 pci_user_write_config_byte(dev, pos, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 buf++;
160 pos++;
161 cnt--;
162 }
163
164 if ((pos & 3) && cnt > 2) {
Harvey Harrisonf17a0772008-07-22 14:40:47 -0700165 __le16 val;
166 __get_user(val, (__le16 __user *) buf);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700167 pci_user_write_config_word(dev, pos, le16_to_cpu(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 buf += 2;
169 pos += 2;
170 cnt -= 2;
171 }
172
173 while (cnt >= 4) {
Harvey Harrisonf17a0772008-07-22 14:40:47 -0700174 __le32 val;
175 __get_user(val, (__le32 __user *) buf);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700176 pci_user_write_config_dword(dev, pos, le32_to_cpu(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 buf += 4;
178 pos += 4;
179 cnt -= 4;
180 }
181
182 if (cnt >= 2) {
Harvey Harrisonf17a0772008-07-22 14:40:47 -0700183 __le16 val;
184 __get_user(val, (__le16 __user *) buf);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700185 pci_user_write_config_word(dev, pos, le16_to_cpu(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 buf += 2;
187 pos += 2;
188 cnt -= 2;
189 }
190
191 if (cnt) {
192 unsigned char val;
193 __get_user(val, buf);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700194 pci_user_write_config_byte(dev, pos, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 buf++;
196 pos++;
197 cnt--;
198 }
199
Huang Yingb3c32c42012-10-25 09:36:03 +0800200 pci_config_pm_runtime_put(dev);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *ppos = pos;
David Rientjesecb39082007-09-27 13:41:17 -0700203 i_size_write(ino, dp->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return nbytes;
205}
206
207struct pci_filp_private {
208 enum pci_mmap_state mmap_state;
209 int write_combine;
210};
211
Mathieu Segaudadd77182008-01-10 14:27:12 +0100212static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
213 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Al Viro496ad9a2013-01-23 17:07:38 -0500215 const struct proc_dir_entry *dp = PDE(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct pci_dev *dev = dp->data;
217#ifdef HAVE_PCI_MMAP
218 struct pci_filp_private *fpriv = file->private_data;
219#endif /* HAVE_PCI_MMAP */
220 int ret = 0;
221
222 switch (cmd) {
223 case PCIIOC_CONTROLLER:
224 ret = pci_domain_nr(dev->bus);
225 break;
226
227#ifdef HAVE_PCI_MMAP
228 case PCIIOC_MMAP_IS_IO:
229 fpriv->mmap_state = pci_mmap_io;
230 break;
231
232 case PCIIOC_MMAP_IS_MEM:
233 fpriv->mmap_state = pci_mmap_mem;
234 break;
235
236 case PCIIOC_WRITE_COMBINE:
237 if (arg)
238 fpriv->write_combine = 1;
239 else
240 fpriv->write_combine = 0;
241 break;
242
243#endif /* HAVE_PCI_MMAP */
244
245 default:
246 ret = -EINVAL;
247 break;
248 };
249
250 return ret;
251}
252
253#ifdef HAVE_PCI_MMAP
254static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
255{
Al Viro496ad9a2013-01-23 17:07:38 -0500256 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 const struct proc_dir_entry *dp = PDE(inode);
258 struct pci_dev *dev = dp->data;
259 struct pci_filp_private *fpriv = file->private_data;
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700260 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!capable(CAP_SYS_RAWIO))
263 return -EPERM;
264
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700265 /* Make sure the caller is mapping a real resource for this device */
266 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Martin Wilck3b519e42010-11-10 11:03:21 +0100267 if (pci_mmap_fits(dev, i, vma, PCI_MMAP_PROCFS))
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700268 break;
269 }
270
271 if (i >= PCI_ROM_RESOURCE)
272 return -ENODEV;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ret = pci_mmap_page_range(dev, vma,
275 fpriv->mmap_state,
276 fpriv->write_combine);
277 if (ret < 0)
278 return ret;
279
280 return 0;
281}
282
283static int proc_bus_pci_open(struct inode *inode, struct file *file)
284{
285 struct pci_filp_private *fpriv = kmalloc(sizeof(*fpriv), GFP_KERNEL);
286
287 if (!fpriv)
288 return -ENOMEM;
289
290 fpriv->mmap_state = pci_mmap_io;
291 fpriv->write_combine = 0;
292
293 file->private_data = fpriv;
294
295 return 0;
296}
297
298static int proc_bus_pci_release(struct inode *inode, struct file *file)
299{
300 kfree(file->private_data);
301 file->private_data = NULL;
302
303 return 0;
304}
305#endif /* HAVE_PCI_MMAP */
306
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800307static const struct file_operations proc_bus_pci_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700308 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 .llseek = proc_bus_pci_lseek,
310 .read = proc_bus_pci_read,
311 .write = proc_bus_pci_write,
Mathieu Segaudadd77182008-01-10 14:27:12 +0100312 .unlocked_ioctl = proc_bus_pci_ioctl,
Arnd Bergmann991f7392010-07-04 00:02:28 +0200313 .compat_ioctl = proc_bus_pci_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#ifdef HAVE_PCI_MMAP
315 .open = proc_bus_pci_open,
316 .release = proc_bus_pci_release,
317 .mmap = proc_bus_pci_mmap,
318#ifdef HAVE_ARCH_PCI_GET_UNMAPPED_AREA
319 .get_unmapped_area = get_pci_unmapped_area,
320#endif /* HAVE_ARCH_PCI_GET_UNMAPPED_AREA */
321#endif /* HAVE_PCI_MMAP */
322};
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* iterator */
325static void *pci_seq_start(struct seq_file *m, loff_t *pos)
326{
327 struct pci_dev *dev = NULL;
328 loff_t n = *pos;
329
330 for_each_pci_dev(dev) {
331 if (!n--)
332 break;
333 }
334 return dev;
335}
336
337static void *pci_seq_next(struct seq_file *m, void *v, loff_t *pos)
338{
339 struct pci_dev *dev = v;
340
341 (*pos)++;
342 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
343 return dev;
344}
345
346static void pci_seq_stop(struct seq_file *m, void *v)
347{
348 if (v) {
349 struct pci_dev *dev = v;
350 pci_dev_put(dev);
351 }
352}
353
354static int show_device(struct seq_file *m, void *v)
355{
356 const struct pci_dev *dev = v;
357 const struct pci_driver *drv;
358 int i;
359
360 if (dev == NULL)
361 return 0;
362
363 drv = pci_dev_driver(dev);
364 seq_printf(m, "%02x%02x\t%04x%04x\t%x",
365 dev->bus->number,
366 dev->devfn,
367 dev->vendor,
368 dev->device,
369 dev->irq);
Yu Zhaofde09c62008-11-22 02:39:32 +0800370
371 /* only print standard and ROM resources to preserve compatibility */
372 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700373 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000374 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700375 seq_printf(m, "\t%16llx",
376 (unsigned long long)(start |
377 (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000378 }
Yu Zhaofde09c62008-11-22 02:39:32 +0800379 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700380 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000381 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700382 seq_printf(m, "\t%16llx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dev->resource[i].start < dev->resource[i].end ?
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700384 (unsigned long long)(end - start) + 1 : 0);
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 seq_putc(m, '\t');
387 if (drv)
388 seq_printf(m, "%s", drv->name);
389 seq_putc(m, '\n');
390 return 0;
391}
392
Jan Engelhardt02d90fc2008-01-22 20:53:43 +0100393static const struct seq_operations proc_bus_pci_devices_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .start = pci_seq_start,
395 .next = pci_seq_next,
396 .stop = pci_seq_stop,
397 .show = show_device
398};
399
400static struct proc_dir_entry *proc_bus_pci_dir;
401
402int pci_proc_attach_device(struct pci_dev *dev)
403{
404 struct pci_bus *bus = dev->bus;
405 struct proc_dir_entry *e;
406 char name[16];
407
408 if (!proc_initialized)
409 return -EACCES;
410
411 if (!bus->procdir) {
412 if (pci_proc_domain(bus)) {
413 sprintf(name, "%04x:%02x", pci_domain_nr(bus),
414 bus->number);
415 } else {
416 sprintf(name, "%02x", bus->number);
417 }
418 bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
419 if (!bus->procdir)
420 return -ENOMEM;
421 }
422
423 sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700424 e = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUSR, bus->procdir,
425 &proc_bus_pci_operations, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!e)
427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 e->size = dev->cfg_size;
429 dev->procent = e;
430
431 return 0;
432}
433
434int pci_proc_detach_device(struct pci_dev *dev)
435{
436 struct proc_dir_entry *e;
437
438 if ((e = dev->procent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 remove_proc_entry(e->name, dev->bus->procdir);
440 dev->procent = NULL;
441 }
442 return 0;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445int pci_proc_detach_bus(struct pci_bus* bus)
446{
447 struct proc_dir_entry *de = bus->procdir;
448 if (de)
449 remove_proc_entry(de->name, proc_bus_pci_dir);
450 return 0;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static int proc_bus_pci_dev_open(struct inode *inode, struct file *file)
454{
455 return seq_open(file, &proc_bus_pci_devices_op);
456}
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800457static const struct file_operations proc_bus_pci_dev_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700458 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 .open = proc_bus_pci_dev_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = seq_release,
463};
464
465static int __init pci_proc_init(void)
466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct pci_dev *dev = NULL;
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700468 proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700469 proc_create("devices", 0, proc_bus_pci_dir,
470 &proc_bus_pci_dev_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 proc_initialized = 1;
Kulikov Vasiliy4e344b12010-07-03 20:04:39 +0400472 for_each_pci_dev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 pci_proc_attach_device(dev);
Kulikov Vasiliy4e344b12010-07-03 20:04:39 +0400474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476}
477
Robert P. J. Dayeaf61142008-05-11 16:58:53 -0400478device_initcall(pci_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479