Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | Accessing PCI device resources through sysfs |
Jesse Barnes | 5d135df | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 2 | -------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | sysfs, usually mounted at /sys, provides access to PCI resources on platforms |
| 5 | that support it. For example, a given bus might look like this: |
| 6 | |
| 7 | /sys/devices/pci0000:17 |
| 8 | |-- 0000:17:00.0 |
| 9 | | |-- class |
| 10 | | |-- config |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | | |-- device |
| 12 | | |-- irq |
| 13 | | |-- local_cpus |
| 14 | | |-- resource |
| 15 | | |-- resource0 |
| 16 | | |-- resource1 |
| 17 | | |-- resource2 |
| 18 | | |-- rom |
| 19 | | |-- subsystem_device |
| 20 | | |-- subsystem_vendor |
| 21 | | `-- vendor |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 22 | `-- ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | The topmost element describes the PCI domain and bus number. In this case, |
| 25 | the domain number is 0000 and the bus number is 17 (both values are in hex). |
| 26 | This bus contains a single function device in slot 0. The domain and bus |
| 27 | numbers are reproduced for convenience. Under the device directory are several |
| 28 | files, each with their own function. |
| 29 | |
| 30 | file function |
| 31 | ---- -------- |
| 32 | class PCI class (ascii, ro) |
| 33 | config PCI config space (binary, rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | device PCI device (ascii, ro) |
| 35 | irq IRQ number (ascii, ro) |
| 36 | local_cpus nearby CPU mask (cpumask, ro) |
| 37 | resource PCI resource host addresses (ascii, ro) |
| 38 | resource0..N PCI resource N, if present (binary, mmap) |
| 39 | rom PCI ROM resource, if present (binary, ro) |
| 40 | subsystem_device PCI subsystem device (ascii, ro) |
| 41 | subsystem_vendor PCI subsystem vendor (ascii, ro) |
| 42 | vendor PCI vendor (ascii, ro) |
| 43 | |
| 44 | ro - read only file |
| 45 | rw - file is readable and writable |
| 46 | mmap - file is mmapable |
| 47 | ascii - file contains ascii text |
| 48 | binary - file contains binary data |
| 49 | cpumask - file contains a cpumask type |
| 50 | |
Jesse Barnes | 5d135df | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 51 | The read only files are informational, writes to them will be ignored, with |
| 52 | the exception of the 'rom' file. Writable files can be used to perform |
| 53 | actions on the device (e.g. changing config space, detaching a device). |
| 54 | mmapable files are available via an mmap of the file at offset 0 and can be |
| 55 | used to do actual device programming from userspace. Note that some platforms |
| 56 | don't support mmapping of certain resources, so be sure to check the return |
| 57 | value from any attempted mmap. |
| 58 | |
| 59 | The 'rom' file is special in that it provides read-only access to the device's |
| 60 | ROM file, if available. It's disabled by default, however, so applications |
| 61 | should write the string "1" to the file to enable it before attempting a read |
| 62 | call, and disable it following the access by writing "0" to the file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | Accessing legacy resources through sysfs |
Jesse Barnes | 5d135df | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 65 | ---------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | Legacy I/O port and ISA memory resources are also provided in sysfs if the |
Uwe Kleine-König | 1b3c371 | 2007-02-17 19:23:03 +0100 | [diff] [blame] | 68 | underlying platform supports them. They're located in the PCI class hierarchy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | e.g. |
| 70 | |
| 71 | /sys/class/pci_bus/0000:17/ |
| 72 | |-- bridge -> ../../../devices/pci0000:17 |
| 73 | |-- cpuaffinity |
| 74 | |-- legacy_io |
| 75 | `-- legacy_mem |
| 76 | |
| 77 | The legacy_io file is a read/write file that can be used by applications to |
| 78 | do legacy port I/O. The application should open the file, seek to the desired |
| 79 | port (e.g. 0x3e8) and do a read or a write of 1, 2 or 4 bytes. The legacy_mem |
| 80 | file should be mmapped with an offset corresponding to the memory offset |
| 81 | desired, e.g. 0xa0000 for the VGA frame buffer. The application can then |
| 82 | simply dereference the returned pointer (after checking for errors of course) |
| 83 | to access legacy memory space. |
| 84 | |
| 85 | Supporting PCI access on new platforms |
Jesse Barnes | 5d135df | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 86 | -------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | In order to support PCI resource mapping as described above, Linux platform |
| 89 | code must define HAVE_PCI_MMAP and provide a pci_mmap_page_range function. |
| 90 | Platforms are free to only support subsets of the mmap functionality, but |
| 91 | useful return codes should be provided. |
| 92 | |
| 93 | Legacy resources are protected by the HAVE_PCI_LEGACY define. Platforms |
| 94 | wishing to support legacy functionality should define it and provide |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 95 | pci_legacy_read, pci_legacy_write and pci_mmap_legacy_page_range functions. |