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>
51 #include <sys/ioctl.h>
54 #include "pciaccess.h"
55 #include "pciaccess_private.h"
56 #include "linux_devmem.h"
58 static void pci_device_linux_sysfs_enable(struct pci_device *dev);
60 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
63 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
65 static int pci_device_linux_sysfs_map_range(struct pci_device *dev,
66 struct pci_device_mapping *map);
68 static int pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
69 struct pci_device_mapping *map);
71 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
72 pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
74 static int pci_device_linux_sysfs_write( struct pci_device * dev,
75 const void * data, pciaddr_t offset, pciaddr_t size,
76 pciaddr_t * bytes_wrtten );
78 static const struct pci_system_methods linux_sysfs_methods = {
80 .destroy_device = NULL,
81 .read_rom = pci_device_linux_sysfs_read_rom,
82 .probe = pci_device_linux_sysfs_probe,
83 .map_range = pci_device_linux_sysfs_map_range,
84 .unmap_range = pci_device_linux_sysfs_unmap_range,
86 .read = pci_device_linux_sysfs_read,
87 .write = pci_device_linux_sysfs_write,
89 .fill_capabilities = pci_fill_capabilities_generic,
90 .enable = pci_device_linux_sysfs_enable,
93 #define SYS_BUS_PCI "/sys/bus/pci/devices"
96 static int populate_entries(struct pci_system * pci_sys);
100 * Attempt to access PCI subsystem using Linux's sysfs interface.
103 pci_system_linux_sysfs_create( void )
109 /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
110 * can be accessed using this interface.
113 if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
114 pci_sys = calloc( 1, sizeof( struct pci_system ) );
115 if ( pci_sys != NULL ) {
116 pci_sys->methods = & linux_sysfs_methods;
118 pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY);
120 err = populate_entries(pci_sys);
135 * Filter out the names "." and ".." from the scanned sysfs entries.
137 * \param d Directory entry being processed by \c scandir.
140 * Zero if the entry name matches either "." or "..", non-zero otherwise.
142 * \sa scandir, populate_entries
145 scan_sys_pci_filter( const struct dirent * d )
147 return !((strcmp( d->d_name, "." ) == 0)
148 || (strcmp( d->d_name, ".." ) == 0));
153 populate_entries( struct pci_system * p )
155 struct dirent ** devices;
161 n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
164 p->devices = calloc( n, sizeof( struct pci_device_private ) );
166 if (p->devices != NULL) {
167 for (i = 0 ; i < n ; i++) {
170 unsigned dom, bus, dev, func;
171 struct pci_device_private *device =
172 (struct pci_device_private *) &p->devices[i];
175 sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
176 & dom, & bus, & dev, & func);
178 device->base.domain = dom;
179 device->base.bus = bus;
180 device->base.dev = dev;
181 device->base.func = func;
184 err = pci_device_linux_sysfs_read(& device->base, config, 0,
186 if ((bytes == 48) && !err) {
187 device->base.vendor_id = (uint16_t)config[0]
188 + ((uint16_t)config[1] << 8);
189 device->base.device_id = (uint16_t)config[2]
190 + ((uint16_t)config[3] << 8);
191 device->base.device_class = (uint32_t)config[9]
192 + ((uint32_t)config[10] << 8)
193 + ((uint32_t)config[11] << 16);
194 device->base.revision = config[8];
195 device->base.subvendor_id = (uint16_t)config[44]
196 + ((uint16_t)config[45] << 8);
197 device->base.subdevice_id = (uint16_t)config[46]
198 + ((uint16_t)config[47] << 8);
221 pci_device_linux_sysfs_probe( struct pci_device * dev )
232 err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
234 struct pci_device_private *priv = (struct pci_device_private *) dev;
236 dev->irq = config[60];
237 priv->header_type = config[14];
240 /* The PCI config registers can be used to obtain information
241 * about the memory and I/O regions for the device. However,
242 * doing so requires some tricky parsing (to correctly handle
243 * 64-bit memory regions) and requires writing to the config
244 * registers. Since we'd like to avoid having to deal with the
245 * parsing issues and non-root users can write to PCI config
246 * registers, we use a different file in the device's sysfs
247 * directory called "resource".
249 * The resource file contains all of the needed information in
250 * a format that is consistent across all platforms. Each BAR
251 * and the expansion ROM have a single line of data containing
252 * 3, 64-bit hex values: the first address in the region,
253 * the last address in the region, and the region's flags.
255 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
261 fd = open( name, O_RDONLY );
269 bytes = read( fd, resource, 512 );
270 resource[511] = '\0';
275 for ( i = 0 ; i < 6 ; i++ ) {
277 dev->regions[i].base_addr = strtoull( next, & next, 16 );
278 high_addr = strtoull( next, & next, 16 );
279 flags = strtoull( next, & next, 16 );
281 if ( dev->regions[i].base_addr != 0 ) {
282 dev->regions[i].size = (high_addr
283 - dev->regions[i].base_addr) + 1;
285 dev->regions[i].is_IO = (flags & 0x01);
286 dev->regions[i].is_64 = (flags & 0x04);
287 dev->regions[i].is_prefetchable = (flags & 0x08);
291 low_addr = strtoull( next, & next, 16 );
292 high_addr = strtoull( next, & next, 16 );
293 flags = strtoull( next, & next, 16 );
294 if ( low_addr != 0 ) {
295 priv->rom_base = low_addr;
296 dev->rom_size = (high_addr - low_addr) + 1;
306 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
316 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
323 fd = open( name, O_RDWR );
325 /* If reading the ROM using sysfs fails, fall back to the old
326 * /dev/mem based interface.
328 return pci_device_linux_devmem_read_rom(dev, buffer);
332 if ( fstat( fd, & st ) == -1 ) {
337 rom_size = st.st_size;
341 /* This is a quirky thing on Linux. Even though the ROM and the file
342 * for the ROM in sysfs are read-only, the string "1" must be written to
343 * the file to enable the ROM. After the data has been read, "0" must be
344 * written to the file to disable the ROM.
347 lseek( fd, 0, SEEK_SET );
349 for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
350 const int bytes = read( fd, (char *) buffer + total_bytes,
351 rom_size - total_bytes );
356 else if ( bytes == 0 ) {
360 total_bytes += bytes;
364 lseek( fd, 0, SEEK_SET );
373 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
374 pciaddr_t offset, pciaddr_t size,
375 pciaddr_t * bytes_read )
378 pciaddr_t temp_size = size;
381 char *data_bytes = data;
383 if ( bytes_read != NULL ) {
387 /* Each device has a directory under sysfs. Within that directory there
388 * is a file named "config". This file used to access the PCI config
389 * space. It is used here to obtain most of the information about the
392 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
399 fd = open( name, O_RDONLY );
405 while ( temp_size > 0 ) {
406 const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
408 /* If zero bytes were read, then we assume it's the end of the
421 if ( bytes_read != NULL ) {
422 *bytes_read = size - temp_size;
431 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
432 pciaddr_t offset, pciaddr_t size,
433 pciaddr_t * bytes_written )
436 pciaddr_t temp_size = size;
439 const char *data_bytes = data;
441 if ( bytes_written != NULL ) {
445 /* Each device has a directory under sysfs. Within that directory there
446 * is a file named "config". This file used to access the PCI config
447 * space. It is used here to obtain most of the information about the
450 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
457 fd = open( name, O_WRONLY );
463 while ( temp_size > 0 ) {
464 const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
466 /* If zero bytes were written, then we assume it's the end of the
479 if ( bytes_written != NULL ) {
480 *bytes_written = size - temp_size;
488 pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
489 struct pci_device_mapping *map)
493 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
494 ? (PROT_READ | PROT_WRITE) : PROT_READ;
495 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
497 const off_t offset = map->base - dev->regions[map->region].base_addr;
499 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
506 fd = open(name, open_flags);
510 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
511 if (map->memory == MAP_FAILED) {
523 * Map a memory region for a device using the Linux sysfs interface.
525 * \param dev Device whose memory region is to be mapped.
526 * \param map Parameters of the mapping that is to be created.
529 * Zero on success or an \c errno value on failure.
531 * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
534 * Some older 2.6.x kernels don't implement the resourceN files. On those
535 * systems /dev/mem must be used. On these systems it is also possible that
536 * \c mmap64 may need to be used.
539 pci_device_linux_sysfs_map_range(struct pci_device *dev,
540 struct pci_device_mapping *map)
545 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
546 ? (PROT_READ | PROT_WRITE) : PROT_READ;
547 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
549 const off_t offset = map->base - dev->regions[map->region].base_addr;
551 struct mtrr_sentry sentry = {
554 .type = MTRR_TYPE_UNCACHABLE
558 /* For WC mappings, try sysfs resourceN_wc file first */
559 if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
560 !pci_device_linux_sysfs_map_range_wc(dev, map))
563 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
571 fd = open(name, open_flags);
577 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
578 if (map->memory == MAP_FAILED) {
585 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
586 sentry.type = MTRR_TYPE_WRBACK;
587 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
588 sentry.type = MTRR_TYPE_WRCOMB;
591 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
592 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
593 /* FIXME: Should we report an error in this case?
595 fprintf(stderr, "error setting MTRR "
596 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
597 sentry.base, sentry.size, sentry.type,
598 strerror(errno), errno);
601 /* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
602 mprotect (map->memory, map->size, PROT_NONE);
603 err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
606 fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
608 fprintf(stderr, "remapping without mprotect performance kludge.\n");
610 munmap(map->memory, map->size);
611 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
612 if (map->memory == MAP_FAILED) {
627 * Unmap a memory region for a device using the Linux sysfs interface.
629 * \param dev Device whose memory region is to be unmapped.
630 * \param map Parameters of the mapping that is to be destroyed.
633 * Zero on success or an \c errno value on failure.
635 * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
638 * Some older 2.6.x kernels don't implement the resourceN files. On those
639 * systems /dev/mem must be used. On these systems it is also possible that
640 * \c mmap64 may need to be used.
643 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
644 struct pci_device_mapping *map)
648 struct mtrr_sentry sentry = {
651 .type = MTRR_TYPE_UNCACHABLE
655 err = pci_device_generic_unmap_range (dev, map);
660 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
661 sentry.type = MTRR_TYPE_WRBACK;
662 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
663 sentry.type = MTRR_TYPE_WRCOMB;
666 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
667 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
668 /* FIXME: Should we report an error in this case?
670 fprintf(stderr, "error setting MTRR "
671 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
672 sentry.base, sentry.size, sentry.type,
673 strerror(errno), errno);
682 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
687 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
694 fd = open( name, O_RDWR );