2 * (C) Copyright IBM Corporation 2006
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
27 * Access PCI subsystem using Linux's sysfs interface. This interface is
28 * available starting somewhere in the late 2.5.x kernel phase, and is the
29 * prefered method on all 2.6.x kernels.
31 * \author Ian Romanick <idr@us.ibm.com>
40 #include <sys/types.h>
47 #include "pciaccess.h"
48 #include "pciaccess_private.h"
50 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
53 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
55 static int pci_device_linux_sysfs_map_region( struct pci_device * dev,
56 unsigned region, int write_enable );
58 static int pci_device_linux_sysfs_unmap_region( struct pci_device * dev,
61 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
62 pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
64 static int pci_device_linux_sysfs_write( struct pci_device * dev,
65 const void * data, pciaddr_t offset, pciaddr_t size,
66 pciaddr_t * bytes_wrtten );
68 static const struct pci_system_methods linux_sysfs_methods = {
70 .destroy_device = NULL,
71 .read_rom = pci_device_linux_sysfs_read_rom,
72 .probe = pci_device_linux_sysfs_probe,
73 .map = pci_device_linux_sysfs_map_region,
74 .unmap = pci_device_linux_sysfs_unmap_region,
76 .read = pci_device_linux_sysfs_read,
77 .write = pci_device_linux_sysfs_write,
79 .fill_capabilities = pci_fill_capabilities_generic
82 #define SYS_BUS_PCI "/sys/bus/pci/devices"
85 static void populate_entries( struct pci_system * pci_sys );
89 * Attempt to access PCI subsystem using Linux's sysfs interface.
92 pci_system_linux_sysfs_create( void )
98 /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
99 * can be accessed using this interface.
102 if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
103 pci_sys = calloc( 1, sizeof( struct pci_system ) );
104 if ( pci_sys != NULL ) {
105 pci_sys->methods = & linux_sysfs_methods;
106 populate_entries( pci_sys );
121 * Filter out the names "." and ".." from the scanned sysfs entries.
123 * \param d Directory entry being processed by \c scandir.
126 * Zero if the entry name matches either "." or "..", non-zero otherwise.
128 * \sa scandir, populate_entries
131 scan_sys_pci_filter( const struct dirent * d )
133 return !((strcmp( d->d_name, "." ) == 0)
134 || (strcmp( d->d_name, ".." ) == 0));
139 populate_entries( struct pci_system * p )
141 struct dirent ** devices;
146 n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
149 p->devices = calloc( n, sizeof( struct pci_device_private ) );
152 for ( i = 0 ; i < n ; i++ ) {
153 unsigned dom, bus, dev, func;
156 sscanf( devices[ i ]->d_name, "%04x:%02x:%02x.%1u",
157 & dom, & bus, & dev, & func );
159 p->devices[ i ].base.domain = dom;
160 p->devices[ i ].base.bus = bus;
161 p->devices[ i ].base.dev = dev;
162 p->devices[ i ].base.func = func;
169 pci_device_linux_sysfs_probe( struct pci_device * dev )
180 err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
182 dev->vendor_id = (uint16_t)config[0] + ((uint16_t)config[1] << 8);
183 dev->device_id = (uint16_t)config[2] + ((uint16_t)config[3] << 8);
184 dev->device_class = (uint32_t)config[11] + ((uint32_t)config[10] << 8)
185 + ((uint16_t)config[9] << 16);
186 dev->revision = config[8];
187 dev->subvendor_id = (uint16_t)config[44] + ((uint16_t)config[45] << 8);
188 dev->subdevice_id = (uint16_t)config[46] + ((uint16_t)config[47] << 8);
189 dev->irq = config[60];
192 /* The PCI config registers can be used to obtain information
193 * about the memory and I/O regions for the device. However,
194 * doing so requires some tricky parsing (to correctly handle
195 * 64-bit memory regions) and requires writing to the config
196 * registers. Since we'd like to avoid having to deal with the
197 * parsing issues and non-root users can write to PCI config
198 * registers, we use a different file in the device's sysfs
199 * directory called "resource".
201 * The resource file contains all of the needed information in
202 * a format that is consistent across all platforms. Each BAR
203 * and the expansion ROM have a single line of data containing
204 * 3, 64-bit hex values: the first address in the region,
205 * the last address in the region, and the region's flags.
207 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
213 fd = open( name, O_RDONLY );
221 bytes = read( fd, resource, 512 );
222 resource[511] = '\0';
227 for ( i = 0 ; i < 6 ; i++ ) {
229 dev->regions[i].base_addr = strtoull( next, & next, 16 );
230 high_addr = strtoull( next, & next, 16 );
231 flags = strtoull( next, & next, 16 );
233 if ( dev->regions[i].base_addr != 0 ) {
234 dev->regions[i].size = (high_addr
235 - dev->regions[i].base_addr) + 1;
237 dev->regions[i].is_IO = (flags & 0x01);
238 dev->regions[i].is_64 = (flags & 0x04);
239 dev->regions[i].is_prefetchable = (flags & 0x08);
243 low_addr = strtoull( next, & next, 16 );
244 high_addr = strtoull( next, & next, 16 );
245 flags = strtoull( next, & next, 16 );
246 if ( low_addr != 0 ) {
247 dev->rom_size = (high_addr - low_addr) + 1;
257 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
266 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
273 fd = open( name, O_RDWR );
279 if ( fstat( fd, & st ) == -1 ) {
285 /* This is a quirky thing on Linux. Even though the ROM and the file
286 * for the ROM in sysfs are read-only, the string "1" must be written to
287 * the file to enable the ROM. After the data has been read, "0" must be
288 * written to the file to disable the ROM.
291 lseek( fd, 0, SEEK_SET );
293 for ( total_bytes = 0 ; total_bytes < st.st_size ; /* empty */ ) {
294 const int bytes = read( fd, (char *) buffer + total_bytes,
295 st.st_size - total_bytes );
300 else if ( bytes == 0 ) {
304 total_bytes += bytes;
308 lseek( fd, 0, SEEK_SET );
317 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
318 pciaddr_t offset, pciaddr_t size,
319 pciaddr_t * bytes_read )
322 pciaddr_t temp_size = size;
327 if ( bytes_read != NULL ) {
331 /* Each device has a directory under sysfs. Within that directory there
332 * is a file named "config". This file used to access the PCI config
333 * space. It is used here to obtain most of the information about the
336 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
343 fd = open( name, O_RDONLY );
349 while ( temp_size > 0 ) {
350 const ssize_t bytes = pread64( fd, data, temp_size, offset );
352 /* If zero bytes were read, then we assume it's the end of the
365 if ( bytes_read != NULL ) {
366 *bytes_read = size - temp_size;
375 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
376 pciaddr_t offset, pciaddr_t size,
377 pciaddr_t * bytes_written )
380 pciaddr_t temp_size = size;
385 if ( bytes_written != NULL ) {
389 /* Each device has a directory under sysfs. Within that directory there
390 * is a file named "config". This file used to access the PCI config
391 * space. It is used here to obtain most of the information about the
394 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
401 fd = open( name, O_WRONLY );
407 while ( temp_size > 0 ) {
408 const ssize_t bytes = pwrite64( fd, data, temp_size, offset );
410 /* If zero bytes were written, then we assume it's the end of the
423 if ( bytes_written != NULL ) {
424 *bytes_written = size - temp_size;
433 * Map a memory region for a device using the Linux sysfs interface.
435 * \param dev Device whose memory region is to be mapped.
436 * \param region Region, on the range [0, 5], that is to be mapped.
437 * \param write_enable Map for writing (non-zero).
440 * Zero on success or an \c errno value on failure.
442 * \sa pci_device_map_region, pci_device_linux_sysfs_unmap_region
445 * Some older 2.6.x kernels don't implement the resourceN files. On those
446 * systems /dev/mem must be used. On these systems it is also possible that
447 * \c mmap64 may need to be used.
450 pci_device_linux_sysfs_map_region( struct pci_device * dev, unsigned region,
456 const int prot = (write_enable) ? (PROT_READ | PROT_WRITE) : PROT_READ;
459 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
467 fd = open( name, (write_enable) ? O_RDWR : O_RDONLY );
473 dev->regions[ region ].memory = mmap( NULL, dev->regions[ region ].size,
474 prot, MAP_SHARED, fd, 0 );
475 if ( dev->regions[ region ].memory == MAP_FAILED ) {
477 dev->regions[ region ].memory = NULL;
486 * Unmap the specified region using the Linux sysfs interface.
488 * \param dev Device whose memory region is to be mapped.
489 * \param region Region, on the range [0, 5], that is to be mapped.
492 * Zero on success or an \c errno value on failure.
494 * \sa pci_device_unmap_region, pci_device_linux_sysfs_map_region
497 * Some older 2.6.x kernels don't implement the resourceN files. On those
498 * systems /dev/mem must be used. On these systems it is also possible that
499 * \c mmap64 may need to be used.
502 pci_device_linux_sysfs_unmap_region( struct pci_device * dev, unsigned region )
506 if ( munmap( dev->regions[ region ].memory, dev->regions[ region ].size )
511 dev->regions[ region ].memory = NULL;