Accessing PCI device resources through sysfs

sysfs, usually mounted at /sys, provides access to PCI resources on platformsthat support it. For example, a given bus might look like this:

/sys/devices/pci0000:17|-- 0000:17:00.0|   |-- class|   |-- config|   |-- device|   |-- enable|   |-- irq|   |-- local_cpus|   |-- remove|   |-- resource|   |-- resource0|   |-- resource1|   |-- resource2|   |-- revision|   |-- rom|   |-- subsystem_device|   |-- subsystem_vendor|   `-- vendor`-- ...

The topmost element describes the PCI domain and bus number. In this case,the domain number is 0000 and the bus number is 17 (both values are in hex).This bus contains a single function device in slot 0. The domain and busnumbers are reproduced for convenience. Under the device directory are severalfiles, each with their own function.

filefunction
classPCI class (ascii, ro)
configPCI config space (binary, rw)
devicePCI device (ascii, ro)
enableWhether the device is enabled (ascii, rw)
irqIRQ number (ascii, ro)
local_cpusnearby CPU mask (cpumask, ro)
removeremove device from kernel’s list (ascii, wo)
resourcePCI resource host addresses (ascii, ro)
resource0..NPCI resource N, if present (binary, mmap, rw[1])
resource0_wc..N_wcPCI WC map resource N, if prefetchable (binary, mmap)
revisionPCI revision (ascii, ro)
romPCI ROM resource, if present (binary, ro)
subsystem_devicePCI subsystem device (ascii, ro)
subsystem_vendorPCI subsystem vendor (ascii, ro)
vendorPCI vendor (ascii, ro)
ro - read only filerw - file is readable and writablewo - write only filemmap - file is mmapableascii - file contains ascii textbinary - file contains binary datacpumask - file contains a cpumask type
[1]rw for IORESOURCE_IO (I/O port) regions only

The read only files are informational, writes to them will be ignored, withthe exception of the ‘rom’ file. Writable files can be used to performactions on the device (e.g. changing config space, detaching a device).mmapable files are available via an mmap of the file at offset 0 and can beused to do actual device programming from userspace. Note that some platformsdon’t support mmapping of certain resources, so be sure to check the returnvalue from any attempted mmap. The most notable of these are I/O portresources, which also provide read/write access.

The ‘enable’ file provides a counter that indicates how many times the devicehas been enabled. If the ‘enable’ file currently returns ‘4’, and a ‘1’ isechoed into it, it will then return ‘5’. Echoing a ‘0’ into it will decreasethe count. Even when it returns to 0, though, some of the initialisationmay not be reversed.

The ‘rom’ file is special in that it provides read-only access to the device’sROM file, if available. It’s disabled by default, however, so applicationsshould write the string “1” to the file to enable it before attempting a readcall, and disable it following the access by writing “0” to the file. Notethat the device must be enabled for a rom read to return data successfully.In the event a driver is not bound to the device, it can be enabled using the‘enable’ file, documented above.

The ‘remove’ file is used to remove the PCI device, by writing a non-zerointeger to the file. This does not involve any kind of hot-plug functionality,e.g. powering off the device. The device is removed from the kernel’s list ofPCI devices, the sysfs directory for it is removed, and the device will beremoved from any drivers attached to it. Removal of PCI root buses isdisallowed.

Accessing legacy resources through sysfs

Legacy I/O port and ISA memory resources are also provided in sysfs if theunderlying platform supports them. They’re located in the PCI class hierarchy,e.g.:

/sys/class/pci_bus/0000:17/|-- bridge -> ../../../devices/pci0000:17|-- cpuaffinity|-- legacy_io`-- legacy_mem

The legacy_io file is a read/write file that can be used by applications todo legacy port I/O. The application should open the file, seek to the desiredport (e.g. 0x3e8) and do a read or a write of 1, 2 or 4 bytes. The legacy_memfile should be mmapped with an offset corresponding to the memory offsetdesired, e.g. 0xa0000 for the VGA frame buffer. The application can thensimply dereference the returned pointer (after checking for errors of course)to access legacy memory space.

Supporting PCI access on new platforms

In order to support PCI resource mapping as described above, Linux platformcode should ideally define ARCH_GENERIC_PCI_MMAP_RESOURCE and use the genericimplementation of that functionality. To support the historical interface ofmmap() through files in /proc/bus/pci, platforms may also set HAVE_PCI_MMAP.

Alternatively, platforms which set HAVE_PCI_MMAP may provide their ownimplementation of pci_mmap_page_range() instead of definingARCH_GENERIC_PCI_MMAP_RESOURCE.

Platforms which support write-combining maps of PCI resources must definearch_can_pci_mmap_wc() which shall evaluate to non-zero at runtime whenwrite-combining is permitted. Platforms which support maps of I/O resourcesdefine arch_can_pci_mmap_io() similarly.

Legacy resources are protected by the HAVE_PCI_LEGACY define. Platformswishing to support legacy functionality should define it and providepci_legacy_read, pci_legacy_write and pci_mmap_legacy_page_range functions.