Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ============================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | Accessing PCI device resources through sysfs |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 5 | ============================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | sysfs, usually mounted at /sys, provides access to PCI resources on platforms |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 8 | that support it. For example, a given bus might look like this:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| 10 | /sys/devices/pci0000:17 |
| 11 | |-- 0000:17:00.0 |
| 12 | | |-- class |
| 13 | | |-- config |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | | |-- device |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 15 | | |-- enable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | | |-- irq |
| 17 | | |-- local_cpus |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 18 | | |-- remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | | |-- resource |
| 20 | | |-- resource0 |
| 21 | | |-- resource1 |
| 22 | | |-- resource2 |
Emil Velikov | 702ed3b | 2016-11-21 16:24:49 -0600 | [diff] [blame] | 23 | | |-- revision |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | | |-- rom |
| 25 | | |-- subsystem_device |
| 26 | | |-- subsystem_vendor |
| 27 | | `-- vendor |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 28 | `-- ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | The topmost element describes the PCI domain and bus number. In this case, |
| 31 | the domain number is 0000 and the bus number is 17 (both values are in hex). |
| 32 | This bus contains a single function device in slot 0. The domain and bus |
| 33 | numbers are reproduced for convenience. Under the device directory are several |
| 34 | files, each with their own function. |
| 35 | |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 36 | =================== ===================================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | file function |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 38 | =================== ===================================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | class PCI class (ascii, ro) |
| 40 | config PCI config space (binary, rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | device PCI device (ascii, ro) |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 42 | enable Whether the device is enabled (ascii, rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | irq IRQ number (ascii, ro) |
| 44 | local_cpus nearby CPU mask (cpumask, ro) |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 45 | remove remove device from kernel's list (ascii, wo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | resource PCI resource host addresses (ascii, ro) |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 47 | resource0..N PCI resource N, if present (binary, mmap, rw\ [1]_) |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 48 | resource0_wc..N_wc PCI WC map resource N, if prefetchable (binary, mmap) |
Emil Velikov | 702ed3b | 2016-11-21 16:24:49 -0600 | [diff] [blame] | 49 | revision PCI revision (ascii, ro) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | rom PCI ROM resource, if present (binary, ro) |
| 51 | subsystem_device PCI subsystem device (ascii, ro) |
| 52 | subsystem_vendor PCI subsystem vendor (ascii, ro) |
| 53 | vendor PCI vendor (ascii, ro) |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 54 | =================== ===================================================== |
| 55 | |
| 56 | :: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | ro - read only file |
| 59 | rw - file is readable and writable |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 60 | wo - write only file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | mmap - file is mmapable |
| 62 | ascii - file contains ascii text |
| 63 | binary - file contains binary data |
| 64 | cpumask - file contains a cpumask type |
| 65 | |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 66 | .. [1] rw for RESOURCE_IO (I/O port) regions only |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 67 | |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 68 | The read only files are informational, writes to them will be ignored, with |
| 69 | the exception of the 'rom' file. Writable files can be used to perform |
| 70 | actions on the device (e.g. changing config space, detaching a device). |
| 71 | mmapable files are available via an mmap of the file at offset 0 and can be |
| 72 | used to do actual device programming from userspace. Note that some platforms |
| 73 | don't support mmapping of certain resources, so be sure to check the return |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 74 | value from any attempted mmap. The most notable of these are I/O port |
| 75 | resources, which also provide read/write access. |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 76 | |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 77 | The 'enable' file provides a counter that indicates how many times the device |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 78 | has been enabled. If the 'enable' file currently returns '4', and a '1' is |
| 79 | echoed into it, it will then return '5'. Echoing a '0' into it will decrease |
| 80 | the count. Even when it returns to 0, though, some of the initialisation |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 81 | may not be reversed. |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 82 | |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 83 | The 'rom' file is special in that it provides read-only access to the device's |
| 84 | ROM file, if available. It's disabled by default, however, so applications |
| 85 | 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] | 86 | 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] | 87 | 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] | 88 | In the event a driver is not bound to the device, it can be enabled using the |
| 89 | 'enable' file, documented above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 91 | The 'remove' file is used to remove the PCI device, by writing a non-zero |
| 92 | integer to the file. This does not involve any kind of hot-plug functionality, |
| 93 | e.g. powering off the device. The device is removed from the kernel's list of |
| 94 | PCI devices, the sysfs directory for it is removed, and the device will be |
| 95 | removed from any drivers attached to it. Removal of PCI root buses is |
| 96 | disallowed. |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | Accessing legacy resources through sysfs |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 99 | ---------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | 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] | 102 | underlying platform supports them. They're located in the PCI class hierarchy, |
Mauro Carvalho Chehab | 28bcadf | 2020-04-27 23:17:17 +0200 | [diff] [blame] | 103 | e.g.:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /sys/class/pci_bus/0000:17/ |
| 106 | |-- bridge -> ../../../devices/pci0000:17 |
| 107 | |-- cpuaffinity |
| 108 | |-- legacy_io |
| 109 | `-- legacy_mem |
| 110 | |
| 111 | The legacy_io file is a read/write file that can be used by applications to |
| 112 | do legacy port I/O. The application should open the file, seek to the desired |
| 113 | port (e.g. 0x3e8) and do a read or a write of 1, 2 or 4 bytes. The legacy_mem |
| 114 | file should be mmapped with an offset corresponding to the memory offset |
| 115 | desired, e.g. 0xa0000 for the VGA frame buffer. The application can then |
| 116 | simply dereference the returned pointer (after checking for errors of course) |
| 117 | to access legacy memory space. |
| 118 | |
| 119 | Supporting PCI access on new platforms |
Jesse Barnes | 5d135dff | 2005-12-09 11:55:03 -0800 | [diff] [blame] | 120 | -------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | In order to support PCI resource mapping as described above, Linux platform |
David Woodhouse | f719582 | 2017-04-12 13:25:59 +0100 | [diff] [blame] | 123 | code should ideally define ARCH_GENERIC_PCI_MMAP_RESOURCE and use the generic |
| 124 | implementation of that functionality. To support the historical interface of |
| 125 | mmap() through files in /proc/bus/pci, platforms may also set HAVE_PCI_MMAP. |
| 126 | |
| 127 | Alternatively, platforms which set HAVE_PCI_MMAP may provide their own |
| 128 | implementation of pci_mmap_page_range() instead of defining |
| 129 | ARCH_GENERIC_PCI_MMAP_RESOURCE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
David Woodhouse | ae749c7 | 2017-04-12 13:25:54 +0100 | [diff] [blame] | 131 | Platforms which support write-combining maps of PCI resources must define |
| 132 | arch_can_pci_mmap_wc() which shall evaluate to non-zero at runtime when |
David Woodhouse | e854d8b | 2017-04-12 13:25:56 +0100 | [diff] [blame] | 133 | write-combining is permitted. Platforms which support maps of I/O resources |
| 134 | define arch_can_pci_mmap_io() similarly. |
David Woodhouse | ae749c7 | 2017-04-12 13:25:54 +0100 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | Legacy resources are protected by the HAVE_PCI_LEGACY define. Platforms |
| 137 | wishing to support legacy functionality should define it and provide |
David Brownell | 0b405a0 | 2005-05-12 12:06:27 -0700 | [diff] [blame] | 138 | pci_legacy_read, pci_legacy_write and pci_mmap_legacy_page_range functions. |