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 * preferred 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 const struct pci_system_methods linux_sysfs_methods;
60 #define SYS_BUS_PCI "/sys/bus/pci/devices"
63 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
64 pciaddr_t offset, pciaddr_t size,
65 pciaddr_t * bytes_read );
67 static int populate_entries(struct pci_system * pci_sys);
70 * Attempt to access PCI subsystem using Linux's sysfs interface.
73 pci_system_linux_sysfs_create( void )
79 /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
80 * can be accessed using this interface.
83 if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
84 pci_sys = calloc( 1, sizeof( struct pci_system ) );
85 if ( pci_sys != NULL ) {
86 pci_sys->methods = & linux_sysfs_methods;
88 pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY);
90 err = populate_entries(pci_sys);
105 * Filter out the names "." and ".." from the scanned sysfs entries.
107 * \param d Directory entry being processed by \c scandir.
110 * Zero if the entry name matches either "." or "..", non-zero otherwise.
112 * \sa scandir, populate_entries
115 scan_sys_pci_filter( const struct dirent * d )
117 return !((strcmp( d->d_name, "." ) == 0)
118 || (strcmp( d->d_name, ".." ) == 0));
123 populate_entries( struct pci_system * p )
125 struct dirent ** devices;
131 n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
134 p->devices = calloc( n, sizeof( struct pci_device_private ) );
136 if (p->devices != NULL) {
137 for (i = 0 ; i < n ; i++) {
140 unsigned dom, bus, dev, func;
141 struct pci_device_private *device =
142 (struct pci_device_private *) &p->devices[i];
145 sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
146 & dom, & bus, & dev, & func);
148 device->base.domain = dom;
149 device->base.bus = bus;
150 device->base.dev = dev;
151 device->base.func = func;
154 err = pci_device_linux_sysfs_read(& device->base, config, 0,
156 if ((bytes == 48) && !err) {
157 device->base.vendor_id = (uint16_t)config[0]
158 + ((uint16_t)config[1] << 8);
159 device->base.device_id = (uint16_t)config[2]
160 + ((uint16_t)config[3] << 8);
161 device->base.device_class = (uint32_t)config[9]
162 + ((uint32_t)config[10] << 8)
163 + ((uint32_t)config[11] << 16);
164 device->base.revision = config[8];
165 device->base.subvendor_id = (uint16_t)config[44]
166 + ((uint16_t)config[45] << 8);
167 device->base.subdevice_id = (uint16_t)config[46]
168 + ((uint16_t)config[47] << 8);
181 for (i = 0; i < n; i++)
195 pci_device_linux_sysfs_probe( struct pci_device * dev )
206 err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
208 struct pci_device_private *priv = (struct pci_device_private *) dev;
210 dev->irq = config[60];
211 priv->header_type = config[14];
214 /* The PCI config registers can be used to obtain information
215 * about the memory and I/O regions for the device. However,
216 * doing so requires some tricky parsing (to correctly handle
217 * 64-bit memory regions) and requires writing to the config
218 * registers. Since we'd like to avoid having to deal with the
219 * parsing issues and non-root users can write to PCI config
220 * registers, we use a different file in the device's sysfs
221 * directory called "resource".
223 * The resource file contains all of the needed information in
224 * a format that is consistent across all platforms. Each BAR
225 * and the expansion ROM have a single line of data containing
226 * 3, 64-bit hex values: the first address in the region,
227 * the last address in the region, and the region's flags.
229 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
235 fd = open( name, O_RDONLY );
243 bytes = read( fd, resource, 512 );
244 resource[511] = '\0';
249 for ( i = 0 ; i < 6 ; i++ ) {
251 dev->regions[i].base_addr = strtoull( next, & next, 16 );
252 high_addr = strtoull( next, & next, 16 );
253 flags = strtoull( next, & next, 16 );
255 if ( dev->regions[i].base_addr != 0 ) {
256 dev->regions[i].size = (high_addr
257 - dev->regions[i].base_addr) + 1;
259 dev->regions[i].is_IO = (flags & 0x01);
260 dev->regions[i].is_64 = (flags & 0x04);
261 dev->regions[i].is_prefetchable = (flags & 0x08);
265 low_addr = strtoull( next, & next, 16 );
266 high_addr = strtoull( next, & next, 16 );
267 flags = strtoull( next, & next, 16 );
268 if ( low_addr != 0 ) {
269 priv->rom_base = low_addr;
270 dev->rom_size = (high_addr - low_addr) + 1;
280 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
290 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
297 fd = open( name, O_RDWR );
300 /* If reading the ROM using sysfs fails, fall back to the old
301 * /dev/mem based interface.
302 * disable this for newer kernels using configure
304 return pci_device_linux_devmem_read_rom(dev, buffer);
311 if ( fstat( fd, & st ) == -1 ) {
316 rom_size = st.st_size;
320 /* This is a quirky thing on Linux. Even though the ROM and the file
321 * for the ROM in sysfs are read-only, the string "1" must be written to
322 * the file to enable the ROM. After the data has been read, "0" must be
323 * written to the file to disable the ROM.
326 lseek( fd, 0, SEEK_SET );
328 for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
329 const int bytes = read( fd, (char *) buffer + total_bytes,
330 rom_size - total_bytes );
335 else if ( bytes == 0 ) {
339 total_bytes += bytes;
343 lseek( fd, 0, SEEK_SET );
352 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
353 pciaddr_t offset, pciaddr_t size,
354 pciaddr_t * bytes_read )
357 pciaddr_t temp_size = size;
360 char *data_bytes = data;
362 if ( bytes_read != NULL ) {
366 /* Each device has a directory under sysfs. Within that directory there
367 * is a file named "config". This file used to access the PCI config
368 * space. It is used here to obtain most of the information about the
371 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
378 fd = open( name, O_RDONLY );
384 while ( temp_size > 0 ) {
385 const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
387 /* If zero bytes were read, then we assume it's the end of the
400 if ( bytes_read != NULL ) {
401 *bytes_read = size - temp_size;
410 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
411 pciaddr_t offset, pciaddr_t size,
412 pciaddr_t * bytes_written )
415 pciaddr_t temp_size = size;
418 const char *data_bytes = data;
420 if ( bytes_written != NULL ) {
424 /* Each device has a directory under sysfs. Within that directory there
425 * is a file named "config". This file used to access the PCI config
426 * space. It is used here to obtain most of the information about the
429 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
436 fd = open( name, O_WRONLY );
442 while ( temp_size > 0 ) {
443 const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
445 /* If zero bytes were written, then we assume it's the end of the
458 if ( bytes_written != NULL ) {
459 *bytes_written = size - temp_size;
467 pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
468 struct pci_device_mapping *map)
472 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
473 ? (PROT_READ | PROT_WRITE) : PROT_READ;
474 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
476 const off_t offset = map->base - dev->regions[map->region].base_addr;
478 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
485 fd = open(name, open_flags);
489 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
490 if (map->memory == MAP_FAILED) {
502 * Map a memory region for a device using the Linux sysfs interface.
504 * \param dev Device whose memory region is to be mapped.
505 * \param map Parameters of the mapping that is to be created.
508 * Zero on success or an \c errno value on failure.
510 * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
513 * Some older 2.6.x kernels don't implement the resourceN files. On those
514 * systems /dev/mem must be used. On these systems it is also possible that
515 * \c mmap64 may need to be used.
518 pci_device_linux_sysfs_map_range(struct pci_device *dev,
519 struct pci_device_mapping *map)
524 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
525 ? (PROT_READ | PROT_WRITE) : PROT_READ;
526 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
528 const off_t offset = map->base - dev->regions[map->region].base_addr;
530 struct mtrr_sentry sentry = {
533 .type = MTRR_TYPE_UNCACHABLE
537 /* For WC mappings, try sysfs resourceN_wc file first */
538 if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
539 !pci_device_linux_sysfs_map_range_wc(dev, map))
542 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
550 fd = open(name, open_flags);
556 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
557 if (map->memory == MAP_FAILED) {
564 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
565 sentry.type = MTRR_TYPE_WRBACK;
566 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
567 sentry.type = MTRR_TYPE_WRCOMB;
570 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
571 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
572 /* FIXME: Should we report an error in this case?
574 fprintf(stderr, "error setting MTRR "
575 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
576 sentry.base, sentry.size, sentry.type,
577 strerror(errno), errno);
580 /* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
581 mprotect (map->memory, map->size, PROT_NONE);
582 err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
585 fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
587 fprintf(stderr, "remapping without mprotect performance kludge.\n");
589 munmap(map->memory, map->size);
590 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
591 if (map->memory == MAP_FAILED) {
606 * Unmap a memory region for a device using the Linux sysfs interface.
608 * \param dev Device whose memory region is to be unmapped.
609 * \param map Parameters of the mapping that is to be destroyed.
612 * Zero on success or an \c errno value on failure.
614 * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
617 * Some older 2.6.x kernels don't implement the resourceN files. On those
618 * systems /dev/mem must be used. On these systems it is also possible that
619 * \c mmap64 may need to be used.
622 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
623 struct pci_device_mapping *map)
627 struct mtrr_sentry sentry = {
630 .type = MTRR_TYPE_UNCACHABLE
634 err = pci_device_generic_unmap_range (dev, map);
639 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
640 sentry.type = MTRR_TYPE_WRBACK;
641 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
642 sentry.type = MTRR_TYPE_WRCOMB;
645 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
646 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
647 /* FIXME: Should we report an error in this case?
649 fprintf(stderr, "error setting MTRR "
650 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
651 sentry.base, sentry.size, sentry.type,
652 strerror(errno), errno);
661 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
666 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
673 fd = open( name, O_RDWR );
681 static int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
688 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
695 fd = open( name, O_RDONLY );
699 bytes_read = read(fd, reply, 1);
709 static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
715 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
722 ret = stat(name, &dummy);
728 static struct pci_io_handle *
729 pci_device_linux_sysfs_open_device_io(struct pci_io_handle *ret,
730 struct pci_device *dev, int bar,
731 pciaddr_t base, pciaddr_t size)
735 snprintf(name, PATH_MAX, "%s/%04x:%02x:%02x.%1u/resource%d",
736 SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func, bar);
738 ret->fd = open(name, O_RDWR);
749 static struct pci_io_handle *
750 pci_device_linux_sysfs_open_legacy_io(struct pci_io_handle *ret,
751 struct pci_device *dev, pciaddr_t base,
756 /* First check if there's a legacy io method for the device */
758 snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_io",
759 dev->domain, dev->bus);
761 ret->fd = open(name, O_RDWR);
765 dev = pci_device_get_parent_bridge(dev);
768 /* If not, /dev/port is the best we can do */
770 ret->fd = open("/dev/port", O_RDWR);
782 pci_device_linux_sysfs_close_io(struct pci_device *dev,
783 struct pci_io_handle *handle)
789 pci_device_linux_sysfs_read32(struct pci_io_handle *handle, uint32_t port)
793 pread(handle->fd, &ret, 4, port + handle->base);
799 pci_device_linux_sysfs_read16(struct pci_io_handle *handle, uint32_t port)
803 pread(handle->fd, &ret, 2, port + handle->base);
809 pci_device_linux_sysfs_read8(struct pci_io_handle *handle, uint32_t port)
813 pread(handle->fd, &ret, 1, port + handle->base);
819 pci_device_linux_sysfs_write32(struct pci_io_handle *handle, uint32_t port,
822 pwrite(handle->fd, &data, 4, port + handle->base);
826 pci_device_linux_sysfs_write16(struct pci_io_handle *handle, uint32_t port,
829 pwrite(handle->fd, &data, 2, port + handle->base);
833 pci_device_linux_sysfs_write8(struct pci_io_handle *handle, uint32_t port,
836 pwrite(handle->fd, &data, 1, port + handle->base);
839 static const struct pci_system_methods linux_sysfs_methods = {
841 .destroy_device = NULL,
842 .read_rom = pci_device_linux_sysfs_read_rom,
843 .probe = pci_device_linux_sysfs_probe,
844 .map_range = pci_device_linux_sysfs_map_range,
845 .unmap_range = pci_device_linux_sysfs_unmap_range,
847 .read = pci_device_linux_sysfs_read,
848 .write = pci_device_linux_sysfs_write,
850 .fill_capabilities = pci_fill_capabilities_generic,
851 .enable = pci_device_linux_sysfs_enable,
852 .boot_vga = pci_device_linux_sysfs_boot_vga,
853 .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,
855 .open_device_io = pci_device_linux_sysfs_open_device_io,
856 .open_legacy_io = pci_device_linux_sysfs_open_legacy_io,
857 .close_io = pci_device_linux_sysfs_close_io,
858 .read32 = pci_device_linux_sysfs_read32,
859 .read16 = pci_device_linux_sysfs_read16,
860 .read8 = pci_device_linux_sysfs_read8,
861 .write32 = pci_device_linux_sysfs_write32,
862 .write16 = pci_device_linux_sysfs_write16,
863 .write8 = pci_device_linux_sysfs_write8,