2 * (C) Copyright IBM Corporation 2006
4 * Copyright 2012 Red Hat, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
28 * Access PCI subsystem using Linux's sysfs interface. This interface is
29 * available starting somewhere in the late 2.5.x kernel phase, and is the
30 * preferred method on all 2.6.x kernels.
32 * \author Ian Romanick <idr@us.ibm.com>
41 #include <sys/types.h>
48 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
54 #define outb(x,y) do {} while (0)
55 #define outw(x,y) do {} while (0)
56 #define outl(x,y) do {} while (0)
66 #include <sys/ioctl.h>
69 #include "pciaccess.h"
70 #include "pciaccess_private.h"
71 #include "linux_devmem.h"
73 static const struct pci_system_methods linux_sysfs_methods;
75 #define SYS_BUS_PCI "/sys/bus/pci/devices"
78 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
79 pciaddr_t offset, pciaddr_t size,
80 pciaddr_t * bytes_read );
82 static int populate_entries(struct pci_system * pci_sys);
85 * Attempt to access PCI subsystem using Linux's sysfs interface.
88 pci_system_linux_sysfs_create( void )
94 /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
95 * can be accessed using this interface.
98 if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
99 pci_sys = calloc( 1, sizeof( struct pci_system ) );
100 if ( pci_sys != NULL ) {
101 pci_sys->methods = & linux_sysfs_methods;
103 pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY | O_CLOEXEC);
105 err = populate_entries(pci_sys);
120 * Filter out the names "." and ".." from the scanned sysfs entries.
122 * \param d Directory entry being processed by \c scandir.
125 * Zero if the entry name matches either "." or "..", non-zero otherwise.
127 * \sa scandir, populate_entries
130 scan_sys_pci_filter( const struct dirent * d )
132 return !((strcmp( d->d_name, "." ) == 0)
133 || (strcmp( d->d_name, ".." ) == 0));
138 populate_entries( struct pci_system * p )
140 struct dirent ** devices = NULL;
146 n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
149 p->devices = calloc( n, sizeof( struct pci_device_private ) );
151 if (p->devices != NULL) {
152 for (i = 0 ; i < n ; i++) {
155 unsigned dom, bus, dev, func;
156 struct pci_device_private *device =
157 (struct pci_device_private *) &p->devices[i];
160 sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
161 & dom, & bus, & dev, & func);
163 device->base.domain = dom;
164 device->base.bus = bus;
165 device->base.dev = dev;
166 device->base.func = func;
169 err = pci_device_linux_sysfs_read(& device->base, config, 0,
171 if ((bytes == 48) && !err) {
172 device->base.vendor_id = (uint16_t)config[0]
173 + ((uint16_t)config[1] << 8);
174 device->base.device_id = (uint16_t)config[2]
175 + ((uint16_t)config[3] << 8);
176 device->base.device_class = (uint32_t)config[9]
177 + ((uint32_t)config[10] << 8)
178 + ((uint32_t)config[11] << 16);
179 device->base.revision = config[8];
180 device->base.subvendor_id = (uint16_t)config[44]
181 + ((uint16_t)config[45] << 8);
182 device->base.subdevice_id = (uint16_t)config[46]
183 + ((uint16_t)config[47] << 8);
196 for (i = 0; i < n; i++)
210 pci_device_linux_sysfs_probe( struct pci_device * dev )
221 err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
223 struct pci_device_private *priv = (struct pci_device_private *) dev;
225 dev->irq = config[60];
226 priv->header_type = config[14];
229 /* The PCI config registers can be used to obtain information
230 * about the memory and I/O regions for the device. However,
231 * doing so requires some tricky parsing (to correctly handle
232 * 64-bit memory regions) and requires writing to the config
233 * registers. Since we'd like to avoid having to deal with the
234 * parsing issues and non-root users can write to PCI config
235 * registers, we use a different file in the device's sysfs
236 * directory called "resource".
238 * The resource file contains all of the needed information in
239 * a format that is consistent across all platforms. Each BAR
240 * and the expansion ROM have a single line of data containing
241 * 3, 64-bit hex values: the first address in the region,
242 * the last address in the region, and the region's flags.
244 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
250 fd = open( name, O_RDONLY | O_CLOEXEC);
258 bytes = read( fd, resource, 512 );
259 resource[511] = '\0';
264 for ( i = 0 ; i < 6 ; i++ ) {
266 dev->regions[i].base_addr = strtoull( next, & next, 16 );
267 high_addr = strtoull( next, & next, 16 );
268 flags = strtoull( next, & next, 16 );
270 if ( dev->regions[i].base_addr != 0 ) {
271 dev->regions[i].size = (high_addr
272 - dev->regions[i].base_addr) + 1;
274 dev->regions[i].is_IO = (flags & 0x01);
275 dev->regions[i].is_64 = (flags & 0x04);
276 dev->regions[i].is_prefetchable = (flags & 0x08);
280 low_addr = strtoull( next, & next, 16 );
281 high_addr = strtoull( next, & next, 16 );
282 flags = strtoull( next, & next, 16 );
283 if ( low_addr != 0 ) {
284 priv->rom_base = low_addr;
285 dev->rom_size = (high_addr - low_addr) + 1;
295 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
305 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
312 fd = open( name, O_RDWR | O_CLOEXEC);
315 /* If reading the ROM using sysfs fails, fall back to the old
316 * /dev/mem based interface.
317 * disable this for newer kernels using configure
319 return pci_device_linux_devmem_read_rom(dev, buffer);
326 if ( fstat( fd, & st ) == -1 ) {
331 rom_size = st.st_size;
335 /* This is a quirky thing on Linux. Even though the ROM and the file
336 * for the ROM in sysfs are read-only, the string "1" must be written to
337 * the file to enable the ROM. After the data has been read, "0" must be
338 * written to the file to disable the ROM.
341 lseek( fd, 0, SEEK_SET );
343 for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
344 const int bytes = read( fd, (char *) buffer + total_bytes,
345 rom_size - total_bytes );
350 else if ( bytes == 0 ) {
354 total_bytes += bytes;
358 lseek( fd, 0, SEEK_SET );
367 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
368 pciaddr_t offset, pciaddr_t size,
369 pciaddr_t * bytes_read )
372 pciaddr_t temp_size = size;
375 char *data_bytes = data;
377 if ( bytes_read != NULL ) {
381 /* Each device has a directory under sysfs. Within that directory there
382 * is a file named "config". This file used to access the PCI config
383 * space. It is used here to obtain most of the information about the
386 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
393 fd = open( name, O_RDONLY | O_CLOEXEC);
399 while ( temp_size > 0 ) {
400 const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
402 /* If zero bytes were read, then we assume it's the end of the
417 if ( bytes_read != NULL ) {
418 *bytes_read = size - temp_size;
427 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
428 pciaddr_t offset, pciaddr_t size,
429 pciaddr_t * bytes_written )
432 pciaddr_t temp_size = size;
435 const char *data_bytes = data;
437 if ( bytes_written != NULL ) {
441 /* Each device has a directory under sysfs. Within that directory there
442 * is a file named "config". This file used to access the PCI config
443 * space. It is used here to obtain most of the information about the
446 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
453 fd = open( name, O_WRONLY | O_CLOEXEC);
459 while ( temp_size > 0 ) {
460 const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
462 /* If zero bytes were written, then we assume it's the end of the
477 if ( bytes_written != NULL ) {
478 *bytes_written = size - temp_size;
486 pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
487 struct pci_device_mapping *map)
491 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
492 ? (PROT_READ | PROT_WRITE) : PROT_READ;
493 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
495 const off_t offset = map->base - dev->regions[map->region].base_addr;
497 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
504 fd = open(name, open_flags | O_CLOEXEC);
508 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
509 if (map->memory == MAP_FAILED) {
521 * Map a memory region for a device using the Linux sysfs interface.
523 * \param dev Device whose memory region is to be mapped.
524 * \param map Parameters of the mapping that is to be created.
527 * Zero on success or an \c errno value on failure.
529 * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
532 * Some older 2.6.x kernels don't implement the resourceN files. On those
533 * systems /dev/mem must be used. On these systems it is also possible that
534 * \c mmap64 may need to be used.
537 pci_device_linux_sysfs_map_range(struct pci_device *dev,
538 struct pci_device_mapping *map)
543 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
544 ? (PROT_READ | PROT_WRITE) : PROT_READ;
545 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
547 const off_t offset = map->base - dev->regions[map->region].base_addr;
549 struct mtrr_sentry sentry = {
552 .type = MTRR_TYPE_UNCACHABLE
556 /* For WC mappings, try sysfs resourceN_wc file first */
557 if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
558 !pci_device_linux_sysfs_map_range_wc(dev, map))
561 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
569 fd = open(name, open_flags | O_CLOEXEC);
575 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
576 if (map->memory == MAP_FAILED) {
583 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
584 sentry.type = MTRR_TYPE_WRBACK;
585 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
586 sentry.type = MTRR_TYPE_WRCOMB;
589 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
590 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
591 /* FIXME: Should we report an error in this case?
593 fprintf(stderr, "error setting MTRR "
594 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
595 sentry.base, sentry.size, sentry.type,
596 strerror(errno), errno);
599 /* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
600 mprotect (map->memory, map->size, PROT_NONE);
601 err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
604 fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
606 fprintf(stderr, "remapping without mprotect performance kludge.\n");
608 munmap(map->memory, map->size);
609 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
610 if (map->memory == MAP_FAILED) {
625 * Unmap a memory region for a device using the Linux sysfs interface.
627 * \param dev Device whose memory region is to be unmapped.
628 * \param map Parameters of the mapping that is to be destroyed.
631 * Zero on success or an \c errno value on failure.
633 * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
636 * Some older 2.6.x kernels don't implement the resourceN files. On those
637 * systems /dev/mem must be used. On these systems it is also possible that
638 * \c mmap64 may need to be used.
641 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
642 struct pci_device_mapping *map)
646 struct mtrr_sentry sentry = {
649 .type = MTRR_TYPE_UNCACHABLE
653 err = pci_device_generic_unmap_range (dev, map);
658 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
659 sentry.type = MTRR_TYPE_WRBACK;
660 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
661 sentry.type = MTRR_TYPE_WRCOMB;
664 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
665 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
666 /* FIXME: Should we report an error in this case?
668 fprintf(stderr, "error setting MTRR "
669 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
670 sentry.base, sentry.size, sentry.type,
671 strerror(errno), errno);
680 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
685 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
692 fd = open( name, O_RDWR | O_CLOEXEC);
700 static int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
707 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
714 fd = open( name, O_RDONLY | O_CLOEXEC);
718 bytes_read = read(fd, reply, 1);
728 static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
734 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
741 ret = stat(name, &dummy);
747 static struct pci_io_handle *
748 pci_device_linux_sysfs_open_device_io(struct pci_io_handle *ret,
749 struct pci_device *dev, int bar,
750 pciaddr_t base, pciaddr_t size)
754 snprintf(name, PATH_MAX, "%s/%04x:%02x:%02x.%1u/resource%d",
755 SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func, bar);
757 ret->fd = open(name, O_RDWR | O_CLOEXEC);
769 static struct pci_io_handle *
770 pci_device_linux_sysfs_open_legacy_io(struct pci_io_handle *ret,
771 struct pci_device *dev, pciaddr_t base,
776 /* First check if there's a legacy io method for the device */
778 snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_io",
779 dev->domain, dev->bus);
781 ret->fd = open(name, O_RDWR | O_CLOEXEC);
785 dev = pci_device_get_parent_bridge(dev);
789 * You would think you'd want to use /dev/port here. Don't make that
790 * mistake, /dev/port only does byte-wide i/o cycles which means it
791 * doesn't work. If you think this is stupid, well, you're right.
794 /* If we've no other choice, iopl */
808 pci_device_linux_sysfs_close_io(struct pci_device *dev,
809 struct pci_io_handle *handle)
816 pci_device_linux_sysfs_read32(struct pci_io_handle *handle, uint32_t port)
820 if (handle->fd > -1) {
821 if (handle->is_legacy)
822 pread(handle->fd, &ret, 4, port + handle->base);
824 pread(handle->fd, &ret, 4, port);
826 ret = inl(port + handle->base);
833 pci_device_linux_sysfs_read16(struct pci_io_handle *handle, uint32_t port)
837 if (handle->fd > -1) {
838 if (handle->is_legacy)
839 pread(handle->fd, &ret, 2, port + handle->base);
841 pread(handle->fd, &ret, 2, port);
843 ret = inw(port + handle->base);
850 pci_device_linux_sysfs_read8(struct pci_io_handle *handle, uint32_t port)
854 if (handle->fd > -1) {
855 if (handle->is_legacy)
856 pread(handle->fd, &ret, 1, port + handle->base);
858 pread(handle->fd, &ret, 1, port);
860 ret = inb(port + handle->base);
867 pci_device_linux_sysfs_write32(struct pci_io_handle *handle, uint32_t port,
870 if (handle->fd > -1) {
871 if (handle->is_legacy)
872 pwrite(handle->fd, &data, 4, port + handle->base);
874 pwrite(handle->fd, &data, 4, port);
876 outl(data, port + handle->base);
881 pci_device_linux_sysfs_write16(struct pci_io_handle *handle, uint32_t port,
884 if (handle->fd > -1) {
885 if (handle->is_legacy)
886 pwrite(handle->fd, &data, 2, port + handle->base);
888 pwrite(handle->fd, &data, 2, port);
890 outw(data, port + handle->base);
895 pci_device_linux_sysfs_write8(struct pci_io_handle *handle, uint32_t port,
898 if (handle->fd > -1) {
899 if (handle->is_legacy)
900 pwrite(handle->fd, &data, 1, port + handle->base);
902 pwrite(handle->fd, &data, 1, port);
904 outb(data, port + handle->base);
909 pci_device_linux_sysfs_map_legacy(struct pci_device *dev, pciaddr_t base,
910 pciaddr_t size, unsigned map_flags, void **addr)
913 int flags = O_RDONLY;
914 int prot = PROT_READ;
918 if (map_flags & PCI_DEV_MAP_FLAG_WRITABLE) {
919 flags = O_RDWR; /* O_RDWR != O_WRONLY | O_RDONLY */;
923 /* First check if there's a legacy memory method for the device */
925 snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_mem",
926 dev->domain, dev->bus);
928 fd = open(name, flags | O_CLOEXEC);
932 dev = pci_device_get_parent_bridge(dev);
935 /* If not, /dev/mem is the best we can do */
937 fd = open("/dev/mem", flags | O_CLOEXEC);
942 *addr = mmap(NULL, size, prot, MAP_SHARED, fd, base);
943 if (*addr == MAP_FAILED) {
952 pci_device_linux_sysfs_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
954 return munmap(addr, size);
959 pci_system_linux_destroy(void)
962 if (pci_sys->mtrr_fd != -1)
963 close(pci_sys->mtrr_fd);
967 static const struct pci_system_methods linux_sysfs_methods = {
968 .destroy = pci_system_linux_destroy,
969 .destroy_device = NULL,
970 .read_rom = pci_device_linux_sysfs_read_rom,
971 .probe = pci_device_linux_sysfs_probe,
972 .map_range = pci_device_linux_sysfs_map_range,
973 .unmap_range = pci_device_linux_sysfs_unmap_range,
975 .read = pci_device_linux_sysfs_read,
976 .write = pci_device_linux_sysfs_write,
978 .fill_capabilities = pci_fill_capabilities_generic,
979 .enable = pci_device_linux_sysfs_enable,
980 .boot_vga = pci_device_linux_sysfs_boot_vga,
981 .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,
983 .open_device_io = pci_device_linux_sysfs_open_device_io,
984 .open_legacy_io = pci_device_linux_sysfs_open_legacy_io,
985 .close_io = pci_device_linux_sysfs_close_io,
986 .read32 = pci_device_linux_sysfs_read32,
987 .read16 = pci_device_linux_sysfs_read16,
988 .read8 = pci_device_linux_sysfs_read8,
989 .write32 = pci_device_linux_sysfs_write32,
990 .write16 = pci_device_linux_sysfs_write16,
991 .write8 = pci_device_linux_sysfs_write8,
993 .map_legacy = pci_device_linux_sysfs_map_legacy,
994 .unmap_legacy = pci_device_linux_sysfs_unmap_legacy,