Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | Accessing PCI device resources through sysfs |
Jesse Barnes | 5d135dff | 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 |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 12 | | |-- enable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | | |-- irq |
| 14 | | |-- local_cpus |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 15 | | |-- remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | | |-- resource |
| 17 | | |-- resource0 |
| 18 | | |-- resource1 |
| 19 | | |-- resource2 |
| 20 | | |-- rom |
| 21 | | |-- subsystem_device |
| 22 | | |-- subsystem_vendor |
| 23 | | `-- vendor |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 24 | `-- ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | The topmost element describes the PCI domain and bus number. In this case, |
| 27 | the domain number is 0000 and the bus number is 17 (both values are in hex). |
| 28 | This bus contains a single function device in slot 0. The domain and bus |
| 29 | numbers are reproduced for convenience. Under the device directory are several |
| 30 | files, each with their own function. |
| 31 | |
| 32 | file function |
| 33 | ---- -------- |
| 34 | class PCI class (ascii, ro) |
| 35 | config PCI config space (binary, rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | device PCI device (ascii, ro) |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 37 | enable Whether the device is enabled (ascii, rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | irq IRQ number (ascii, ro) |
| 39 | local_cpus nearby CPU mask (cpumask, ro) |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 40 | remove remove device from kernel's list (ascii, wo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | resource PCI resource host addresses (ascii, ro) |
| 42 | resource0..N PCI resource N, if present (binary, mmap) |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 43 | resource0_wc..N_wc PCI WC map resource N, if prefetchable (binary, mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | rom PCI ROM resource, if present (binary, ro) |
| 45 | subsystem_device PCI subsystem device (ascii, ro) |
| 46 | subsystem_vendor PCI subsystem vendor (ascii, ro) |
| 47 | vendor PCI vendor (ascii, ro) |
| 48 | |
| 49 | ro - read only file |
| 50 | rw - file is readable and writable |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 51 | wo - write only file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | mmap - file is mmapable |
| 53 | ascii - file contains ascii text |
| 54 | binary - file contains binary data |
| 55 | cpumask - file contains a cpumask type |
| 56 | |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 57 | The read only files are informational, writes to them will be ignored, with |
| 58 | the exception of the 'rom' file. Writable files can be used to perform |
| 59 | actions on the device (e.g. changing config space, detaching a device). |
| 60 | mmapable files are available via an mmap of the file at offset 0 and can be |
| 61 | used to do actual device programming from userspace. Note that some platforms |
| 62 | don't support mmapping of certain resources, so be sure to check the return |
| 63 | value from any attempted mmap. |
| 64 | |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 65 | The 'enable' file provides a counter that indicates how many times the device |
| 66 | has been enabled. If the 'enable' file currently returns '4', and a '1' is |
| 67 | echoed into it, it will then return '5'. Echoing a '0' into it will decrease |
| 68 | the count. Even when it returns to 0, though, some of the initialisation |
| 69 | may not be reversed. |
| 70 | |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 71 | The 'rom' file is special in that it provides read-only access to the device's |
| 72 | ROM file, if available. It's disabled by default, however, so applications |
| 73 | should write the string "1" to the file to enable it before attempting a read |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 74 | call, and disable it following the access by writing "0" to the file. Note |
Matt LaPlante | 19f5946 | 2009-04-27 15:06:31 +0200 | [diff] [blame] | 75 | that the device must be enabled for a rom read to return data successfully. |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 76 | In the event a driver is not bound to the device, it can be enabled using the |
| 77 | 'enable' file, documented above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 79 | The 'remove' file is used to remove the PCI device, by writing a non-zero |
| 80 | integer to the file. This does not involve any kind of hot-plug functionality, |
| 81 | e.g. powering off the device. The device is removed from the kernel's list of |
| 82 | PCI devices, the sysfs directory for it is removed, and the device will be |
| 83 | removed from any drivers attached to it. Removal of PCI root buses is |
| 84 | disallowed. |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | Accessing legacy resources through sysfs |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 87 | ---------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | 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] | 90 | underlying platform supports them. They're located in the PCI class hierarchy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | e.g. |
| 92 | |
| 93 | /sys/class/pci_bus/0000:17/ |
| 94 | |-- bridge -> ../../../devices/pci0000:17 |
| 95 | |-- cpuaffinity |
| 96 | |-- legacy_io |
| 97 | `-- legacy_mem |
| 98 | |
| 99 | The legacy_io file is a read/write file that can be used by applications to |
| 100 | do legacy port I/O. The application should open the file, seek to the desired |
| 101 | port (e.g. 0x3e8) and do a read or a write of 1, 2 or 4 bytes. The legacy_mem |
| 102 | file should be mmapped with an offset corresponding to the memory offset |
| 103 | desired, e.g. 0xa0000 for the VGA frame buffer. The application can then |
| 104 | simply dereference the returned pointer (after checking for errors of course) |
| 105 | to access legacy memory space. |
| 106 | |
| 107 | Supporting PCI access on new platforms |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 108 | -------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | In order to support PCI resource mapping as described above, Linux platform |
| 111 | code must define HAVE_PCI_MMAP and provide a pci_mmap_page_range function. |
| 112 | Platforms are free to only support subsets of the mmap functionality, but |
| 113 | useful return codes should be provided. |
| 114 | |
| 115 | Legacy resources are protected by the HAVE_PCI_LEGACY define. Platforms |
| 116 | wishing to support legacy functionality should define it and provide |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 117 | pci_legacy_read, pci_legacy_write and pci_mmap_legacy_page_range functions. |