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__)
54 #define outb(x,y) do {} while (0)
55 #define outw(x,y) do {} while (0)
56 #define outl(x,y) do {} while (0)
64 #include <sys/ioctl.h>
67 #include "pciaccess.h"
68 #include "pciaccess_private.h"
69 #include "linux_devmem.h"
71 static const struct pci_system_methods linux_sysfs_methods;
73 #define SYS_BUS_PCI "/sys/bus/pci/devices"
76 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
77 pciaddr_t offset, pciaddr_t size,
78 pciaddr_t * bytes_read );
80 static int populate_entries(struct pci_system * pci_sys);
83 * Attempt to access PCI subsystem using Linux's sysfs interface.
86 pci_system_linux_sysfs_create( void )
92 /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
93 * can be accessed using this interface.
96 if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
97 pci_sys = calloc( 1, sizeof( struct pci_system ) );
98 if ( pci_sys != NULL ) {
99 pci_sys->methods = & linux_sysfs_methods;
101 pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY | O_CLOEXEC);
103 err = populate_entries(pci_sys);
118 * Filter out the names "." and ".." from the scanned sysfs entries.
120 * \param d Directory entry being processed by \c scandir.
123 * Zero if the entry name matches either "." or "..", non-zero otherwise.
125 * \sa scandir, populate_entries
128 scan_sys_pci_filter( const struct dirent * d )
130 return !((strcmp( d->d_name, "." ) == 0)
131 || (strcmp( d->d_name, ".." ) == 0));
136 populate_entries( struct pci_system * p )
138 struct dirent ** devices = NULL;
144 n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
147 p->devices = calloc( n, sizeof( struct pci_device_private ) );
149 if (p->devices != NULL) {
150 for (i = 0 ; i < n ; i++) {
153 unsigned dom, bus, dev, func;
154 struct pci_device_private *device =
155 (struct pci_device_private *) &p->devices[i];
158 sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
159 & dom, & bus, & dev, & func);
161 device->base.domain = dom;
162 device->base.bus = bus;
163 device->base.dev = dev;
164 device->base.func = func;
167 err = pci_device_linux_sysfs_read(& device->base, config, 0,
169 if ((bytes == 48) && !err) {
170 device->base.vendor_id = (uint16_t)config[0]
171 + ((uint16_t)config[1] << 8);
172 device->base.device_id = (uint16_t)config[2]
173 + ((uint16_t)config[3] << 8);
174 device->base.device_class = (uint32_t)config[9]
175 + ((uint32_t)config[10] << 8)
176 + ((uint32_t)config[11] << 16);
177 device->base.revision = config[8];
178 device->base.subvendor_id = (uint16_t)config[44]
179 + ((uint16_t)config[45] << 8);
180 device->base.subdevice_id = (uint16_t)config[46]
181 + ((uint16_t)config[47] << 8);
194 for (i = 0; i < n; i++)
208 pci_device_linux_sysfs_probe( struct pci_device * dev )
219 err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
221 struct pci_device_private *priv = (struct pci_device_private *) dev;
223 dev->irq = config[60];
224 priv->header_type = config[14];
227 /* The PCI config registers can be used to obtain information
228 * about the memory and I/O regions for the device. However,
229 * doing so requires some tricky parsing (to correctly handle
230 * 64-bit memory regions) and requires writing to the config
231 * registers. Since we'd like to avoid having to deal with the
232 * parsing issues and non-root users can write to PCI config
233 * registers, we use a different file in the device's sysfs
234 * directory called "resource".
236 * The resource file contains all of the needed information in
237 * a format that is consistent across all platforms. Each BAR
238 * and the expansion ROM have a single line of data containing
239 * 3, 64-bit hex values: the first address in the region,
240 * the last address in the region, and the region's flags.
242 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
248 fd = open( name, O_RDONLY | O_CLOEXEC);
257 ret = read( fd, resource, 512 );
258 resource[511] = '\0';
263 for ( i = 0 ; i < 6 ; i++ ) {
265 dev->regions[i].base_addr = strtoull( next, & next, 16 );
266 high_addr = strtoull( next, & next, 16 );
267 flags = strtoull( next, & next, 16 );
269 if ( dev->regions[i].base_addr != 0 ) {
270 dev->regions[i].size = (high_addr
271 - dev->regions[i].base_addr) + 1;
273 dev->regions[i].is_IO = (flags & 0x01);
274 dev->regions[i].is_64 = (flags & 0x04);
275 dev->regions[i].is_prefetchable = (flags & 0x08);
279 low_addr = strtoull( next, & next, 16 );
280 high_addr = strtoull( next, & next, 16 );
281 flags = strtoull( next, & next, 16 );
282 if ( low_addr != 0 ) {
283 priv->rom_base = low_addr;
284 dev->rom_size = (high_addr - low_addr) + 1;
294 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
304 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
311 fd = open( name, O_RDWR | O_CLOEXEC);
314 /* If reading the ROM using sysfs fails, fall back to the old
315 * /dev/mem based interface.
316 * disable this for newer kernels using configure
318 return pci_device_linux_devmem_read_rom(dev, buffer);
325 if ( fstat( fd, & st ) == -1 ) {
330 rom_size = st.st_size;
334 /* This is a quirky thing on Linux. Even though the ROM and the file
335 * for the ROM in sysfs are read-only, the string "1" must be written to
336 * the file to enable the ROM. After the data has been read, "0" must be
337 * written to the file to disable the ROM.
340 lseek( fd, 0, SEEK_SET );
342 for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
343 const int bytes = read( fd, (char *) buffer + total_bytes,
344 rom_size - total_bytes );
349 else if ( bytes == 0 ) {
353 total_bytes += bytes;
357 lseek( fd, 0, SEEK_SET );
366 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
367 pciaddr_t offset, pciaddr_t size,
368 pciaddr_t * bytes_read )
371 pciaddr_t temp_size = size;
374 char *data_bytes = data;
376 if ( bytes_read != NULL ) {
380 /* Each device has a directory under sysfs. Within that directory there
381 * is a file named "config". This file used to access the PCI config
382 * space. It is used here to obtain most of the information about the
385 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
392 fd = open( name, O_RDONLY | O_CLOEXEC);
398 while ( temp_size > 0 ) {
399 const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
401 /* If zero bytes were read, then we assume it's the end of the
416 if ( bytes_read != NULL ) {
417 *bytes_read = size - temp_size;
426 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
427 pciaddr_t offset, pciaddr_t size,
428 pciaddr_t * bytes_written )
431 pciaddr_t temp_size = size;
434 const char *data_bytes = data;
436 if ( bytes_written != NULL ) {
440 /* Each device has a directory under sysfs. Within that directory there
441 * is a file named "config". This file used to access the PCI config
442 * space. It is used here to obtain most of the information about the
445 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
452 fd = open( name, O_WRONLY | O_CLOEXEC);
458 while ( temp_size > 0 ) {
459 const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
461 /* If zero bytes were written, then we assume it's the end of the
476 if ( bytes_written != NULL ) {
477 *bytes_written = size - temp_size;
485 pci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
486 struct pci_device_mapping *map)
490 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
491 ? (PROT_READ | PROT_WRITE) : PROT_READ;
492 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
494 const off_t offset = map->base - dev->regions[map->region].base_addr;
496 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
503 fd = open(name, open_flags | O_CLOEXEC);
507 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
508 if (map->memory == MAP_FAILED) {
520 * Map a memory region for a device using the Linux sysfs interface.
522 * \param dev Device whose memory region is to be mapped.
523 * \param map Parameters of the mapping that is to be created.
526 * Zero on success or an \c errno value on failure.
528 * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
531 * Some older 2.6.x kernels don't implement the resourceN files. On those
532 * systems /dev/mem must be used. On these systems it is also possible that
533 * \c mmap64 may need to be used.
536 pci_device_linux_sysfs_map_range(struct pci_device *dev,
537 struct pci_device_mapping *map)
542 const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
543 ? (PROT_READ | PROT_WRITE) : PROT_READ;
544 const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
546 const off_t offset = map->base - dev->regions[map->region].base_addr;
548 struct mtrr_sentry sentry = {
551 .type = MTRR_TYPE_UNCACHABLE
555 /* For WC mappings, try sysfs resourceN_wc file first */
556 if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
557 !pci_device_linux_sysfs_map_range_wc(dev, map))
560 snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
568 fd = open(name, open_flags | O_CLOEXEC);
574 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
575 if (map->memory == MAP_FAILED) {
582 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
583 sentry.type = MTRR_TYPE_WRBACK;
584 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
585 sentry.type = MTRR_TYPE_WRCOMB;
588 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
589 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
590 /* FIXME: Should we report an error in this case?
592 fprintf(stderr, "error setting MTRR "
593 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
594 sentry.base, sentry.size, sentry.type,
595 strerror(errno), errno);
598 /* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
599 mprotect (map->memory, map->size, PROT_NONE);
600 err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
603 fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
605 fprintf(stderr, "remapping without mprotect performance kludge.\n");
607 munmap(map->memory, map->size);
608 map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
609 if (map->memory == MAP_FAILED) {
624 * Unmap a memory region for a device using the Linux sysfs interface.
626 * \param dev Device whose memory region is to be unmapped.
627 * \param map Parameters of the mapping that is to be destroyed.
630 * Zero on success or an \c errno value on failure.
632 * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
635 * Some older 2.6.x kernels don't implement the resourceN files. On those
636 * systems /dev/mem must be used. On these systems it is also possible that
637 * \c mmap64 may need to be used.
640 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
641 struct pci_device_mapping *map)
645 struct mtrr_sentry sentry = {
648 .type = MTRR_TYPE_UNCACHABLE
652 err = pci_device_generic_unmap_range (dev, map);
657 if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
658 sentry.type = MTRR_TYPE_WRBACK;
659 } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
660 sentry.type = MTRR_TYPE_WRCOMB;
663 if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
664 if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
665 /* FIXME: Should we report an error in this case?
667 fprintf(stderr, "error setting MTRR "
668 "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
669 sentry.base, sentry.size, sentry.type,
670 strerror(errno), errno);
679 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
684 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
691 fd = open( name, O_RDWR | O_CLOEXEC);
699 static int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
706 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
713 fd = open( name, O_RDONLY | O_CLOEXEC);
717 bytes_read = read(fd, reply, 1);
727 static int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
733 snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
740 ret = stat(name, &dummy);
746 static struct pci_io_handle *
747 pci_device_linux_sysfs_open_device_io(struct pci_io_handle *ret,
748 struct pci_device *dev, int bar,
749 pciaddr_t base, pciaddr_t size)
753 snprintf(name, PATH_MAX, "%s/%04x:%02x:%02x.%1u/resource%d",
754 SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func, bar);
756 ret->fd = open(name, O_RDWR | O_CLOEXEC);
767 static struct pci_io_handle *
768 pci_device_linux_sysfs_open_legacy_io(struct pci_io_handle *ret,
769 struct pci_device *dev, pciaddr_t base,
774 /* First check if there's a legacy io method for the device */
776 snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_io",
777 dev->domain, dev->bus);
779 ret->fd = open(name, O_RDWR | O_CLOEXEC);
783 dev = pci_device_get_parent_bridge(dev);
787 * You would think you'd want to use /dev/port here. Don't make that
788 * mistake, /dev/port only does byte-wide i/o cycles which means it
789 * doesn't work. If you think this is stupid, well, you're right.
792 /* If we've no other choice, iopl */
805 pci_device_linux_sysfs_close_io(struct pci_device *dev,
806 struct pci_io_handle *handle)
813 pci_device_linux_sysfs_read32(struct pci_io_handle *handle, uint32_t port)
818 pread(handle->fd, &ret, 4, port + handle->base);
820 ret = inl(port + handle->base);
826 pci_device_linux_sysfs_read16(struct pci_io_handle *handle, uint32_t port)
831 pread(handle->fd, &ret, 2, port + handle->base);
833 ret = inw(port + handle->base);
839 pci_device_linux_sysfs_read8(struct pci_io_handle *handle, uint32_t port)
844 pread(handle->fd, &ret, 1, port + handle->base);
846 ret = inb(port + handle->base);
852 pci_device_linux_sysfs_write32(struct pci_io_handle *handle, uint32_t port,
856 pwrite(handle->fd, &data, 4, port + handle->base);
858 outl(data, port + handle->base);
862 pci_device_linux_sysfs_write16(struct pci_io_handle *handle, uint32_t port,
866 pwrite(handle->fd, &data, 2, port + handle->base);
868 outw(data, port + handle->base);
872 pci_device_linux_sysfs_write8(struct pci_io_handle *handle, uint32_t port,
876 pwrite(handle->fd, &data, 1, port + handle->base);
878 outb(data, port + handle->base);
882 pci_device_linux_sysfs_map_legacy(struct pci_device *dev, pciaddr_t base,
883 pciaddr_t size, unsigned map_flags, void **addr)
886 int flags = O_RDONLY;
887 int prot = PROT_READ;
891 if (map_flags & PCI_DEV_MAP_FLAG_WRITABLE) {
892 flags = O_RDWR; /* O_RDWR != O_WRONLY | O_RDONLY */;
896 /* First check if there's a legacy memory method for the device */
898 snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_mem",
899 dev->domain, dev->bus);
901 fd = open(name, flags | O_CLOEXEC);
905 dev = pci_device_get_parent_bridge(dev);
908 /* If not, /dev/mem is the best we can do */
910 fd = open("/dev/mem", flags | O_CLOEXEC);
915 *addr = mmap(NULL, size, prot, MAP_SHARED, fd, base);
916 if (*addr == MAP_FAILED) {
925 pci_device_linux_sysfs_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
927 return munmap(addr, size);
932 pci_system_linux_destroy(void)
935 if (pci_sys->mtrr_fd != -1)
936 close(pci_sys->mtrr_fd);
940 static const struct pci_system_methods linux_sysfs_methods = {
941 .destroy = pci_system_linux_destroy,
942 .destroy_device = NULL,
943 .read_rom = pci_device_linux_sysfs_read_rom,
944 .probe = pci_device_linux_sysfs_probe,
945 .map_range = pci_device_linux_sysfs_map_range,
946 .unmap_range = pci_device_linux_sysfs_unmap_range,
948 .read = pci_device_linux_sysfs_read,
949 .write = pci_device_linux_sysfs_write,
951 .fill_capabilities = pci_fill_capabilities_generic,
952 .enable = pci_device_linux_sysfs_enable,
953 .boot_vga = pci_device_linux_sysfs_boot_vga,
954 .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,
956 .open_device_io = pci_device_linux_sysfs_open_device_io,
957 .open_legacy_io = pci_device_linux_sysfs_open_legacy_io,
958 .close_io = pci_device_linux_sysfs_close_io,
959 .read32 = pci_device_linux_sysfs_read32,
960 .read16 = pci_device_linux_sysfs_read16,
961 .read8 = pci_device_linux_sysfs_read8,
962 .write32 = pci_device_linux_sysfs_write32,
963 .write16 = pci_device_linux_sysfs_write16,
964 .write8 = pci_device_linux_sysfs_write8,
966 .map_legacy = pci_device_linux_sysfs_map_legacy,
967 .unmap_legacy = pci_device_linux_sysfs_unmap_legacy,