linux_sysfs.c revision cad31331
14f5e7dd7Smrg/*
24f5e7dd7Smrg * (C) Copyright IBM Corporation 2006
34f5e7dd7Smrg * All Rights Reserved.
4cad31331Smrg * Copyright 2012 Red Hat, Inc.
54f5e7dd7Smrg *
64f5e7dd7Smrg * Permission is hereby granted, free of charge, to any person obtaining a
74f5e7dd7Smrg * copy of this software and associated documentation files (the "Software"),
84f5e7dd7Smrg * to deal in the Software without restriction, including without limitation
94f5e7dd7Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
104f5e7dd7Smrg * license, and/or sell copies of the Software, and to permit persons to whom
114f5e7dd7Smrg * the Software is furnished to do so, subject to the following conditions:
124f5e7dd7Smrg *
134f5e7dd7Smrg * The above copyright notice and this permission notice (including the next
144f5e7dd7Smrg * paragraph) shall be included in all copies or substantial portions of the
154f5e7dd7Smrg * Software.
164f5e7dd7Smrg *
174f5e7dd7Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184f5e7dd7Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
194f5e7dd7Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
204f5e7dd7Smrg * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
214f5e7dd7Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
224f5e7dd7Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
234f5e7dd7Smrg * DEALINGS IN THE SOFTWARE.
244f5e7dd7Smrg */
254f5e7dd7Smrg
264f5e7dd7Smrg/**
274f5e7dd7Smrg * \file linux_sysfs.c
284f5e7dd7Smrg * Access PCI subsystem using Linux's sysfs interface.  This interface is
294f5e7dd7Smrg * available starting somewhere in the late 2.5.x kernel phase, and is the
304f5e7dd7Smrg * preferred method on all 2.6.x kernels.
314f5e7dd7Smrg *
324f5e7dd7Smrg * \author Ian Romanick <idr@us.ibm.com>
334f5e7dd7Smrg */
344f5e7dd7Smrg
354f5e7dd7Smrg#define _GNU_SOURCE
364f5e7dd7Smrg
374f5e7dd7Smrg#include <stdlib.h>
384f5e7dd7Smrg#include <string.h>
394f5e7dd7Smrg#include <stdio.h>
404f5e7dd7Smrg#include <unistd.h>
414f5e7dd7Smrg#include <sys/types.h>
424f5e7dd7Smrg#include <sys/stat.h>
434f5e7dd7Smrg#include <fcntl.h>
444f5e7dd7Smrg#include <sys/mman.h>
454f5e7dd7Smrg#include <dirent.h>
464f5e7dd7Smrg#include <errno.h>
474f5e7dd7Smrg
48cad31331Smrg#if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
49cad31331Smrg#include <sys/io.h>
50cad31331Smrg#else
51cad31331Smrg#define inb(x) -1
52cad31331Smrg#define inw(x) -1
53cad31331Smrg#define inl(x) -1
54cad31331Smrg#define outb(x,y) do {} while (0)
55cad31331Smrg#define outw(x,y) do {} while (0)
56cad31331Smrg#define outl(x,y) do {} while (0)
57cad31331Smrg#define iopl(x) -1
58cad31331Smrg#endif
59cad31331Smrg
604f5e7dd7Smrg#include "config.h"
614f5e7dd7Smrg
624f5e7dd7Smrg#ifdef HAVE_MTRR
634f5e7dd7Smrg#include <asm/mtrr.h>
644f5e7dd7Smrg#include <sys/ioctl.h>
654f5e7dd7Smrg#endif
664f5e7dd7Smrg
674f5e7dd7Smrg#include "pciaccess.h"
684f5e7dd7Smrg#include "pciaccess_private.h"
694f5e7dd7Smrg#include "linux_devmem.h"
704f5e7dd7Smrg
71e432255dSmrgstatic const struct pci_system_methods linux_sysfs_methods;
724f5e7dd7Smrg
734f5e7dd7Smrg#define SYS_BUS_PCI "/sys/bus/pci/devices"
744f5e7dd7Smrg
75e432255dSmrgstatic int
76e432255dSmrgpci_device_linux_sysfs_read( struct pci_device * dev, void * data,
77e432255dSmrg			     pciaddr_t offset, pciaddr_t size,
78e432255dSmrg			     pciaddr_t * bytes_read );
794f5e7dd7Smrg
804f5e7dd7Smrgstatic int populate_entries(struct pci_system * pci_sys);
814f5e7dd7Smrg
824f5e7dd7Smrg/**
834f5e7dd7Smrg * Attempt to access PCI subsystem using Linux's sysfs interface.
844f5e7dd7Smrg */
854f5e7dd7Smrg_pci_hidden int
864f5e7dd7Smrgpci_system_linux_sysfs_create( void )
874f5e7dd7Smrg{
884f5e7dd7Smrg    int err = 0;
894f5e7dd7Smrg    struct stat st;
904f5e7dd7Smrg
914f5e7dd7Smrg
924f5e7dd7Smrg    /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
934f5e7dd7Smrg     * can be accessed using this interface.
944f5e7dd7Smrg     */
95cad31331Smrg
964f5e7dd7Smrg    if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
974f5e7dd7Smrg	pci_sys = calloc( 1, sizeof( struct pci_system ) );
984f5e7dd7Smrg	if ( pci_sys != NULL ) {
994f5e7dd7Smrg	    pci_sys->methods = & linux_sysfs_methods;
1004f5e7dd7Smrg#ifdef HAVE_MTRR
101cad31331Smrg	    pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY | O_CLOEXEC);
1024f5e7dd7Smrg#endif
1034f5e7dd7Smrg	    err = populate_entries(pci_sys);
1044f5e7dd7Smrg	}
1054f5e7dd7Smrg	else {
1064f5e7dd7Smrg	    err = ENOMEM;
1074f5e7dd7Smrg	}
1084f5e7dd7Smrg    }
1094f5e7dd7Smrg    else {
1104f5e7dd7Smrg	err = errno;
1114f5e7dd7Smrg    }
1124f5e7dd7Smrg
1134f5e7dd7Smrg    return err;
1144f5e7dd7Smrg}
1154f5e7dd7Smrg
1164f5e7dd7Smrg
1174f5e7dd7Smrg/**
1184f5e7dd7Smrg * Filter out the names "." and ".." from the scanned sysfs entries.
1194f5e7dd7Smrg *
1204f5e7dd7Smrg * \param d  Directory entry being processed by \c scandir.
1214f5e7dd7Smrg *
1224f5e7dd7Smrg * \return
1234f5e7dd7Smrg * Zero if the entry name matches either "." or "..", non-zero otherwise.
1244f5e7dd7Smrg *
1254f5e7dd7Smrg * \sa scandir, populate_entries
1264f5e7dd7Smrg */
1274f5e7dd7Smrgstatic int
1284f5e7dd7Smrgscan_sys_pci_filter( const struct dirent * d )
1294f5e7dd7Smrg{
130cad31331Smrg    return !((strcmp( d->d_name, "." ) == 0)
1314f5e7dd7Smrg	     || (strcmp( d->d_name, ".." ) == 0));
1324f5e7dd7Smrg}
1334f5e7dd7Smrg
1344f5e7dd7Smrg
1354f5e7dd7Smrgint
1364f5e7dd7Smrgpopulate_entries( struct pci_system * p )
1374f5e7dd7Smrg{
138cad31331Smrg    struct dirent ** devices = NULL;
1394f5e7dd7Smrg    int n;
1404f5e7dd7Smrg    int i;
1414f5e7dd7Smrg    int err = 0;
1424f5e7dd7Smrg
1434f5e7dd7Smrg
1444f5e7dd7Smrg    n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
1454f5e7dd7Smrg    if ( n > 0 ) {
1464f5e7dd7Smrg	p->num_devices = n;
1474f5e7dd7Smrg	p->devices = calloc( n, sizeof( struct pci_device_private ) );
1484f5e7dd7Smrg
1494f5e7dd7Smrg	if (p->devices != NULL) {
1504f5e7dd7Smrg	    for (i = 0 ; i < n ; i++) {
1514f5e7dd7Smrg		uint8_t config[48];
1524f5e7dd7Smrg		pciaddr_t bytes;
1534f5e7dd7Smrg		unsigned dom, bus, dev, func;
1544f5e7dd7Smrg		struct pci_device_private *device =
1554f5e7dd7Smrg			(struct pci_device_private *) &p->devices[i];
1564f5e7dd7Smrg
1574f5e7dd7Smrg
1584f5e7dd7Smrg		sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
1594f5e7dd7Smrg		       & dom, & bus, & dev, & func);
1604f5e7dd7Smrg
1614f5e7dd7Smrg		device->base.domain = dom;
1624f5e7dd7Smrg		device->base.bus = bus;
1634f5e7dd7Smrg		device->base.dev = dev;
1644f5e7dd7Smrg		device->base.func = func;
1654f5e7dd7Smrg
1664f5e7dd7Smrg
1674f5e7dd7Smrg		err = pci_device_linux_sysfs_read(& device->base, config, 0,
1684f5e7dd7Smrg						  48, & bytes);
1694f5e7dd7Smrg		if ((bytes == 48) && !err) {
1704f5e7dd7Smrg		    device->base.vendor_id = (uint16_t)config[0]
1714f5e7dd7Smrg			+ ((uint16_t)config[1] << 8);
1724f5e7dd7Smrg		    device->base.device_id = (uint16_t)config[2]
1734f5e7dd7Smrg			+ ((uint16_t)config[3] << 8);
1744f5e7dd7Smrg		    device->base.device_class = (uint32_t)config[9]
1754f5e7dd7Smrg			+ ((uint32_t)config[10] << 8)
1764f5e7dd7Smrg			+ ((uint32_t)config[11] << 16);
1774f5e7dd7Smrg		    device->base.revision = config[8];
1784f5e7dd7Smrg		    device->base.subvendor_id = (uint16_t)config[44]
1794f5e7dd7Smrg			+ ((uint16_t)config[45] << 8);
1804f5e7dd7Smrg		    device->base.subdevice_id = (uint16_t)config[46]
1814f5e7dd7Smrg			+ ((uint16_t)config[47] << 8);
1824f5e7dd7Smrg		}
1834f5e7dd7Smrg
1844f5e7dd7Smrg		if (err) {
1854f5e7dd7Smrg		    break;
1864f5e7dd7Smrg		}
1874f5e7dd7Smrg	    }
1884f5e7dd7Smrg	}
1894f5e7dd7Smrg	else {
1904f5e7dd7Smrg	    err = ENOMEM;
1914f5e7dd7Smrg	}
1924f5e7dd7Smrg    }
1934f5e7dd7Smrg
19449f872b5Smrg    for (i = 0; i < n; i++)
19549f872b5Smrg	free(devices[i]);
19649f872b5Smrg    free(devices);
19749f872b5Smrg
1984f5e7dd7Smrg    if (err) {
1994f5e7dd7Smrg	free(p->devices);
2004f5e7dd7Smrg	p->devices = NULL;
2014f5e7dd7Smrg    }
2024f5e7dd7Smrg
2034f5e7dd7Smrg    return err;
2044f5e7dd7Smrg}
2054f5e7dd7Smrg
2064f5e7dd7Smrg
2074f5e7dd7Smrgstatic int
2084f5e7dd7Smrgpci_device_linux_sysfs_probe( struct pci_device * dev )
2094f5e7dd7Smrg{
2104f5e7dd7Smrg    char     name[256];
2114f5e7dd7Smrg    uint8_t  config[256];
2124f5e7dd7Smrg    char     resource[512];
2134f5e7dd7Smrg    int fd;
2144f5e7dd7Smrg    pciaddr_t bytes;
2154f5e7dd7Smrg    unsigned i;
2164f5e7dd7Smrg    int err;
2174f5e7dd7Smrg
2184f5e7dd7Smrg
2194f5e7dd7Smrg    err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
2204f5e7dd7Smrg    if ( bytes >= 64 ) {
2214f5e7dd7Smrg	struct pci_device_private *priv = (struct pci_device_private *) dev;
2224f5e7dd7Smrg
2234f5e7dd7Smrg	dev->irq = config[60];
2244f5e7dd7Smrg	priv->header_type = config[14];
2254f5e7dd7Smrg
2264f5e7dd7Smrg
2274f5e7dd7Smrg	/* The PCI config registers can be used to obtain information
2284f5e7dd7Smrg	 * about the memory and I/O regions for the device.  However,
2294f5e7dd7Smrg	 * doing so requires some tricky parsing (to correctly handle
2304f5e7dd7Smrg	 * 64-bit memory regions) and requires writing to the config
2314f5e7dd7Smrg	 * registers.  Since we'd like to avoid having to deal with the
2324f5e7dd7Smrg	 * parsing issues and non-root users can write to PCI config
2334f5e7dd7Smrg	 * registers, we use a different file in the device's sysfs
2344f5e7dd7Smrg	 * directory called "resource".
235cad31331Smrg	 *
2364f5e7dd7Smrg	 * The resource file contains all of the needed information in
2374f5e7dd7Smrg	 * a format that is consistent across all platforms.  Each BAR
2384f5e7dd7Smrg	 * and the expansion ROM have a single line of data containing
2394f5e7dd7Smrg	 * 3, 64-bit hex values:  the first address in the region,
2404f5e7dd7Smrg	 * the last address in the region, and the region's flags.
2414f5e7dd7Smrg	 */
2424f5e7dd7Smrg	snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
2434f5e7dd7Smrg		  SYS_BUS_PCI,
2444f5e7dd7Smrg		  dev->domain,
2454f5e7dd7Smrg		  dev->bus,
2464f5e7dd7Smrg		  dev->dev,
2474f5e7dd7Smrg		  dev->func );
248cad31331Smrg	fd = open( name, O_RDONLY | O_CLOEXEC);
2494f5e7dd7Smrg	if ( fd != -1 ) {
2504f5e7dd7Smrg	    char * next;
2514f5e7dd7Smrg	    pciaddr_t  low_addr;
2524f5e7dd7Smrg	    pciaddr_t  high_addr;
2534f5e7dd7Smrg	    pciaddr_t  flags;
2544f5e7dd7Smrg
2554f5e7dd7Smrg
2564f5e7dd7Smrg	    bytes = read( fd, resource, 512 );
2574f5e7dd7Smrg	    resource[511] = '\0';
2584f5e7dd7Smrg
2594f5e7dd7Smrg	    close( fd );
2604f5e7dd7Smrg
2614f5e7dd7Smrg	    next = resource;
2624f5e7dd7Smrg	    for ( i = 0 ; i < 6 ; i++ ) {
2634f5e7dd7Smrg
2644f5e7dd7Smrg		dev->regions[i].base_addr = strtoull( next, & next, 16 );
2654f5e7dd7Smrg		high_addr = strtoull( next, & next, 16 );
2664f5e7dd7Smrg		flags = strtoull( next, & next, 16 );
267cad31331Smrg
2684f5e7dd7Smrg		if ( dev->regions[i].base_addr != 0 ) {
269cad31331Smrg		    dev->regions[i].size = (high_addr
2704f5e7dd7Smrg					    - dev->regions[i].base_addr) + 1;
2714f5e7dd7Smrg
2724f5e7dd7Smrg		    dev->regions[i].is_IO = (flags & 0x01);
2734f5e7dd7Smrg		    dev->regions[i].is_64 = (flags & 0x04);
2744f5e7dd7Smrg		    dev->regions[i].is_prefetchable = (flags & 0x08);
2754f5e7dd7Smrg		}
2764f5e7dd7Smrg	    }
2774f5e7dd7Smrg
2784f5e7dd7Smrg	    low_addr = strtoull( next, & next, 16 );
2794f5e7dd7Smrg	    high_addr = strtoull( next, & next, 16 );
2804f5e7dd7Smrg	    flags = strtoull( next, & next, 16 );
2814f5e7dd7Smrg	    if ( low_addr != 0 ) {
2824f5e7dd7Smrg		priv->rom_base = low_addr;
2834f5e7dd7Smrg		dev->rom_size = (high_addr - low_addr) + 1;
2844f5e7dd7Smrg	    }
2854f5e7dd7Smrg	}
2864f5e7dd7Smrg    }
2874f5e7dd7Smrg
2884f5e7dd7Smrg    return err;
2894f5e7dd7Smrg}
2904f5e7dd7Smrg
2914f5e7dd7Smrg
2924f5e7dd7Smrgstatic int
2934f5e7dd7Smrgpci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
2944f5e7dd7Smrg{
2954f5e7dd7Smrg    char name[256];
2964f5e7dd7Smrg    int fd;
2974f5e7dd7Smrg    struct stat  st;
2984f5e7dd7Smrg    int err = 0;
2994f5e7dd7Smrg    size_t rom_size;
3004f5e7dd7Smrg    size_t total_bytes;
3014f5e7dd7Smrg
3024f5e7dd7Smrg
3034f5e7dd7Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
3044f5e7dd7Smrg	      SYS_BUS_PCI,
3054f5e7dd7Smrg	      dev->domain,
3064f5e7dd7Smrg	      dev->bus,
3074f5e7dd7Smrg	      dev->dev,
3084f5e7dd7Smrg	      dev->func );
309cad31331Smrg
310cad31331Smrg    fd = open( name, O_RDWR | O_CLOEXEC);
3114f5e7dd7Smrg    if ( fd == -1 ) {
31249f872b5Smrg#ifdef LINUX_ROM
3134f5e7dd7Smrg	/* If reading the ROM using sysfs fails, fall back to the old
3144f5e7dd7Smrg	 * /dev/mem based interface.
31549f872b5Smrg	 * disable this for newer kernels using configure
3164f5e7dd7Smrg	 */
3174f5e7dd7Smrg	return pci_device_linux_devmem_read_rom(dev, buffer);
31849f872b5Smrg#else
31949f872b5Smrg	return errno;
32049f872b5Smrg#endif
3214f5e7dd7Smrg    }
3224f5e7dd7Smrg
3234f5e7dd7Smrg
3244f5e7dd7Smrg    if ( fstat( fd, & st ) == -1 ) {
3254f5e7dd7Smrg	close( fd );
3264f5e7dd7Smrg	return errno;
3274f5e7dd7Smrg    }
3284f5e7dd7Smrg
3294f5e7dd7Smrg    rom_size = st.st_size;
3304f5e7dd7Smrg    if ( rom_size == 0 )
3314f5e7dd7Smrg	rom_size = 0x10000;
3324f5e7dd7Smrg
3334f5e7dd7Smrg    /* This is a quirky thing on Linux.  Even though the ROM and the file
3344f5e7dd7Smrg     * for the ROM in sysfs are read-only, the string "1" must be written to
3354f5e7dd7Smrg     * the file to enable the ROM.  After the data has been read, "0" must be
3364f5e7dd7Smrg     * written to the file to disable the ROM.
3374f5e7dd7Smrg     */
3384f5e7dd7Smrg    write( fd, "1", 1 );
3394f5e7dd7Smrg    lseek( fd, 0, SEEK_SET );
3404f5e7dd7Smrg
3414f5e7dd7Smrg    for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
3424f5e7dd7Smrg	const int bytes = read( fd, (char *) buffer + total_bytes,
3434f5e7dd7Smrg				rom_size - total_bytes );
3444f5e7dd7Smrg	if ( bytes == -1 ) {
3454f5e7dd7Smrg	    err = errno;
3464f5e7dd7Smrg	    break;
3474f5e7dd7Smrg	}
3484f5e7dd7Smrg	else if ( bytes == 0 ) {
3494f5e7dd7Smrg	    break;
3504f5e7dd7Smrg	}
3514f5e7dd7Smrg
3524f5e7dd7Smrg	total_bytes += bytes;
3534f5e7dd7Smrg    }
354cad31331Smrg
3554f5e7dd7Smrg
3564f5e7dd7Smrg    lseek( fd, 0, SEEK_SET );
3574f5e7dd7Smrg    write( fd, "0", 1 );
3584f5e7dd7Smrg
3594f5e7dd7Smrg    close( fd );
3604f5e7dd7Smrg    return err;
3614f5e7dd7Smrg}
3624f5e7dd7Smrg
3634f5e7dd7Smrg
3644f5e7dd7Smrgstatic int
3654f5e7dd7Smrgpci_device_linux_sysfs_read( struct pci_device * dev, void * data,
3664f5e7dd7Smrg			     pciaddr_t offset, pciaddr_t size,
3674f5e7dd7Smrg			     pciaddr_t * bytes_read )
3684f5e7dd7Smrg{
3694f5e7dd7Smrg    char name[256];
3704f5e7dd7Smrg    pciaddr_t temp_size = size;
3714f5e7dd7Smrg    int err = 0;
3724f5e7dd7Smrg    int fd;
3734f5e7dd7Smrg    char *data_bytes = data;
3744f5e7dd7Smrg
3754f5e7dd7Smrg    if ( bytes_read != NULL ) {
3764f5e7dd7Smrg	*bytes_read = 0;
3774f5e7dd7Smrg    }
3784f5e7dd7Smrg
3794f5e7dd7Smrg    /* Each device has a directory under sysfs.  Within that directory there
3804f5e7dd7Smrg     * is a file named "config".  This file used to access the PCI config
3814f5e7dd7Smrg     * space.  It is used here to obtain most of the information about the
3824f5e7dd7Smrg     * device.
3834f5e7dd7Smrg     */
3844f5e7dd7Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
3854f5e7dd7Smrg	      SYS_BUS_PCI,
3864f5e7dd7Smrg	      dev->domain,
3874f5e7dd7Smrg	      dev->bus,
3884f5e7dd7Smrg	      dev->dev,
3894f5e7dd7Smrg	      dev->func );
3904f5e7dd7Smrg
391cad31331Smrg    fd = open( name, O_RDONLY | O_CLOEXEC);
3924f5e7dd7Smrg    if ( fd == -1 ) {
3934f5e7dd7Smrg	return errno;
3944f5e7dd7Smrg    }
3954f5e7dd7Smrg
3964f5e7dd7Smrg
3974f5e7dd7Smrg    while ( temp_size > 0 ) {
3984f5e7dd7Smrg	const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
3994f5e7dd7Smrg
4004f5e7dd7Smrg	/* If zero bytes were read, then we assume it's the end of the
4014f5e7dd7Smrg	 * config file.
4024f5e7dd7Smrg	 */
403cad31331Smrg	if (bytes == 0)
404cad31331Smrg	    break;
405cad31331Smrg	if ( bytes < 0 ) {
4064f5e7dd7Smrg	    err = errno;
4074f5e7dd7Smrg	    break;
4084f5e7dd7Smrg	}
4094f5e7dd7Smrg
4104f5e7dd7Smrg	temp_size -= bytes;
4114f5e7dd7Smrg	offset += bytes;
4124f5e7dd7Smrg	data_bytes += bytes;
4134f5e7dd7Smrg    }
414cad31331Smrg
4154f5e7dd7Smrg    if ( bytes_read != NULL ) {
4164f5e7dd7Smrg	*bytes_read = size - temp_size;
4174f5e7dd7Smrg    }
4184f5e7dd7Smrg
4194f5e7dd7Smrg    close( fd );
4204f5e7dd7Smrg    return err;
4214f5e7dd7Smrg}
4224f5e7dd7Smrg
4234f5e7dd7Smrg
4244f5e7dd7Smrgstatic int
4254f5e7dd7Smrgpci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
4264f5e7dd7Smrg			     pciaddr_t offset, pciaddr_t size,
4274f5e7dd7Smrg			     pciaddr_t * bytes_written )
4284f5e7dd7Smrg{
4294f5e7dd7Smrg    char name[256];
4304f5e7dd7Smrg    pciaddr_t temp_size = size;
4314f5e7dd7Smrg    int err = 0;
4324f5e7dd7Smrg    int fd;
4334f5e7dd7Smrg    const char *data_bytes = data;
4344f5e7dd7Smrg
4354f5e7dd7Smrg    if ( bytes_written != NULL ) {
4364f5e7dd7Smrg	*bytes_written = 0;
4374f5e7dd7Smrg    }
4384f5e7dd7Smrg
4394f5e7dd7Smrg    /* Each device has a directory under sysfs.  Within that directory there
4404f5e7dd7Smrg     * is a file named "config".  This file used to access the PCI config
4414f5e7dd7Smrg     * space.  It is used here to obtain most of the information about the
4424f5e7dd7Smrg     * device.
4434f5e7dd7Smrg     */
4444f5e7dd7Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
4454f5e7dd7Smrg	      SYS_BUS_PCI,
4464f5e7dd7Smrg	      dev->domain,
4474f5e7dd7Smrg	      dev->bus,
4484f5e7dd7Smrg	      dev->dev,
4494f5e7dd7Smrg	      dev->func );
4504f5e7dd7Smrg
451cad31331Smrg    fd = open( name, O_WRONLY | O_CLOEXEC);
4524f5e7dd7Smrg    if ( fd == -1 ) {
4534f5e7dd7Smrg	return errno;
4544f5e7dd7Smrg    }
4554f5e7dd7Smrg
4564f5e7dd7Smrg
4574f5e7dd7Smrg    while ( temp_size > 0 ) {
4584f5e7dd7Smrg	const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
4594f5e7dd7Smrg
4604f5e7dd7Smrg	/* If zero bytes were written, then we assume it's the end of the
4614f5e7dd7Smrg	 * config file.
4624f5e7dd7Smrg	 */
463cad31331Smrg	if ( bytes == 0 )
464cad31331Smrg	    break;
465cad31331Smrg	if ( bytes < 0 ) {
4664f5e7dd7Smrg	    err = errno;
4674f5e7dd7Smrg	    break;
4684f5e7dd7Smrg	}
4694f5e7dd7Smrg
4704f5e7dd7Smrg	temp_size -= bytes;
4714f5e7dd7Smrg	offset += bytes;
4724f5e7dd7Smrg	data_bytes += bytes;
4734f5e7dd7Smrg    }
474cad31331Smrg
4754f5e7dd7Smrg    if ( bytes_written != NULL ) {
4764f5e7dd7Smrg	*bytes_written = size - temp_size;
4774f5e7dd7Smrg    }
4784f5e7dd7Smrg
4794f5e7dd7Smrg    close( fd );
4804f5e7dd7Smrg    return err;
4814f5e7dd7Smrg}
4824f5e7dd7Smrg
4834f5e7dd7Smrgstatic int
4844f5e7dd7Smrgpci_device_linux_sysfs_map_range_wc(struct pci_device *dev,
4854f5e7dd7Smrg				    struct pci_device_mapping *map)
4864f5e7dd7Smrg{
4874f5e7dd7Smrg    char name[256];
4884f5e7dd7Smrg    int fd;
489cad31331Smrg    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
4904f5e7dd7Smrg        ? (PROT_READ | PROT_WRITE) : PROT_READ;
491cad31331Smrg    const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
4924f5e7dd7Smrg        ? O_RDWR : O_RDONLY;
4934f5e7dd7Smrg    const off_t offset = map->base - dev->regions[map->region].base_addr;
4944f5e7dd7Smrg
4954f5e7dd7Smrg    snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u_wc",
4964f5e7dd7Smrg	     SYS_BUS_PCI,
4974f5e7dd7Smrg	     dev->domain,
4984f5e7dd7Smrg	     dev->bus,
4994f5e7dd7Smrg	     dev->dev,
5004f5e7dd7Smrg	     dev->func,
5014f5e7dd7Smrg	     map->region);
502cad31331Smrg    fd = open(name, open_flags | O_CLOEXEC);
5034f5e7dd7Smrg    if (fd == -1)
5044f5e7dd7Smrg	    return errno;
5054f5e7dd7Smrg
5064f5e7dd7Smrg    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
5074f5e7dd7Smrg    if (map->memory == MAP_FAILED) {
5084f5e7dd7Smrg        map->memory = NULL;
5094f5e7dd7Smrg	close(fd);
5104f5e7dd7Smrg	return errno;
5114f5e7dd7Smrg    }
5124f5e7dd7Smrg
5134f5e7dd7Smrg    close(fd);
5144f5e7dd7Smrg
5154f5e7dd7Smrg    return 0;
5164f5e7dd7Smrg}
5174f5e7dd7Smrg
5184f5e7dd7Smrg/**
5194f5e7dd7Smrg * Map a memory region for a device using the Linux sysfs interface.
520cad31331Smrg *
5214f5e7dd7Smrg * \param dev   Device whose memory region is to be mapped.
5224f5e7dd7Smrg * \param map   Parameters of the mapping that is to be created.
523cad31331Smrg *
5244f5e7dd7Smrg * \return
5254f5e7dd7Smrg * Zero on success or an \c errno value on failure.
5264f5e7dd7Smrg *
5274f5e7dd7Smrg * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
5284f5e7dd7Smrg *
5294f5e7dd7Smrg * \todo
5304f5e7dd7Smrg * Some older 2.6.x kernels don't implement the resourceN files.  On those
5314f5e7dd7Smrg * systems /dev/mem must be used.  On these systems it is also possible that
5324f5e7dd7Smrg * \c mmap64 may need to be used.
5334f5e7dd7Smrg */
5344f5e7dd7Smrgstatic int
5354f5e7dd7Smrgpci_device_linux_sysfs_map_range(struct pci_device *dev,
5364f5e7dd7Smrg                                 struct pci_device_mapping *map)
5374f5e7dd7Smrg{
5384f5e7dd7Smrg    char name[256];
5394f5e7dd7Smrg    int fd;
5404f5e7dd7Smrg    int err = 0;
541cad31331Smrg    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
5424f5e7dd7Smrg        ? (PROT_READ | PROT_WRITE) : PROT_READ;
543cad31331Smrg    const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
5444f5e7dd7Smrg        ? O_RDWR : O_RDONLY;
5454f5e7dd7Smrg    const off_t offset = map->base - dev->regions[map->region].base_addr;
5464f5e7dd7Smrg#ifdef HAVE_MTRR
5474f5e7dd7Smrg    struct mtrr_sentry sentry = {
5484f5e7dd7Smrg	.base = map->base,
5494f5e7dd7Smrg        .size = map->size,
5504f5e7dd7Smrg	.type = MTRR_TYPE_UNCACHABLE
5514f5e7dd7Smrg    };
5524f5e7dd7Smrg#endif
5534f5e7dd7Smrg
5544f5e7dd7Smrg    /* For WC mappings, try sysfs resourceN_wc file first */
5554f5e7dd7Smrg    if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) &&
5564f5e7dd7Smrg	!pci_device_linux_sysfs_map_range_wc(dev, map))
5574f5e7dd7Smrg	    return 0;
5584f5e7dd7Smrg
5594f5e7dd7Smrg    snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
5604f5e7dd7Smrg             SYS_BUS_PCI,
5614f5e7dd7Smrg             dev->domain,
5624f5e7dd7Smrg             dev->bus,
5634f5e7dd7Smrg             dev->dev,
5644f5e7dd7Smrg             dev->func,
5654f5e7dd7Smrg             map->region);
5664f5e7dd7Smrg
567cad31331Smrg    fd = open(name, open_flags | O_CLOEXEC);
5684f5e7dd7Smrg    if (fd == -1) {
5694f5e7dd7Smrg        return errno;
5704f5e7dd7Smrg    }
5714f5e7dd7Smrg
5724f5e7dd7Smrg
5734f5e7dd7Smrg    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
5744f5e7dd7Smrg    if (map->memory == MAP_FAILED) {
5754f5e7dd7Smrg        map->memory = NULL;
5764f5e7dd7Smrg	close(fd);
5774f5e7dd7Smrg	return errno;
5784f5e7dd7Smrg    }
5794f5e7dd7Smrg
5804f5e7dd7Smrg#ifdef HAVE_MTRR
5814f5e7dd7Smrg    if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
5824f5e7dd7Smrg        sentry.type = MTRR_TYPE_WRBACK;
5834f5e7dd7Smrg    } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
5844f5e7dd7Smrg        sentry.type = MTRR_TYPE_WRCOMB;
5854f5e7dd7Smrg    }
5864f5e7dd7Smrg
5874f5e7dd7Smrg    if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
5884f5e7dd7Smrg	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
5894f5e7dd7Smrg	    /* FIXME: Should we report an error in this case?
5904f5e7dd7Smrg	     */
5914f5e7dd7Smrg	    fprintf(stderr, "error setting MTRR "
5924f5e7dd7Smrg		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
5934f5e7dd7Smrg		    sentry.base, sentry.size, sentry.type,
5944f5e7dd7Smrg		    strerror(errno), errno);
5954f5e7dd7Smrg/*            err = errno;*/
5964f5e7dd7Smrg	}
5974f5e7dd7Smrg	/* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
5984f5e7dd7Smrg	mprotect (map->memory, map->size, PROT_NONE);
5994f5e7dd7Smrg	err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
6004f5e7dd7Smrg
6014f5e7dd7Smrg	if (err != 0) {
6024f5e7dd7Smrg	    fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
6034f5e7dd7Smrg		    strerror(errno));
6044f5e7dd7Smrg	    fprintf(stderr, "remapping without mprotect performance kludge.\n");
6054f5e7dd7Smrg
6064f5e7dd7Smrg	    munmap(map->memory, map->size);
6074f5e7dd7Smrg	    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
6084f5e7dd7Smrg	    if (map->memory == MAP_FAILED) {
6094f5e7dd7Smrg		map->memory = NULL;
6104f5e7dd7Smrg		close(fd);
6114f5e7dd7Smrg		return errno;
6124f5e7dd7Smrg	    }
6134f5e7dd7Smrg	}
6144f5e7dd7Smrg    }
6154f5e7dd7Smrg#endif
6164f5e7dd7Smrg
6174f5e7dd7Smrg    close(fd);
6184f5e7dd7Smrg
6194f5e7dd7Smrg    return 0;
6204f5e7dd7Smrg}
6214f5e7dd7Smrg
6224f5e7dd7Smrg/**
6234f5e7dd7Smrg * Unmap a memory region for a device using the Linux sysfs interface.
624cad31331Smrg *
6254f5e7dd7Smrg * \param dev   Device whose memory region is to be unmapped.
6264f5e7dd7Smrg * \param map   Parameters of the mapping that is to be destroyed.
627cad31331Smrg *
6284f5e7dd7Smrg * \return
6294f5e7dd7Smrg * Zero on success or an \c errno value on failure.
6304f5e7dd7Smrg *
6314f5e7dd7Smrg * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
6324f5e7dd7Smrg *
6334f5e7dd7Smrg * \todo
6344f5e7dd7Smrg * Some older 2.6.x kernels don't implement the resourceN files.  On those
6354f5e7dd7Smrg * systems /dev/mem must be used.  On these systems it is also possible that
6364f5e7dd7Smrg * \c mmap64 may need to be used.
6374f5e7dd7Smrg */
6384f5e7dd7Smrgstatic int
6394f5e7dd7Smrgpci_device_linux_sysfs_unmap_range(struct pci_device *dev,
6404f5e7dd7Smrg				   struct pci_device_mapping *map)
6414f5e7dd7Smrg{
6424f5e7dd7Smrg    int err = 0;
6434f5e7dd7Smrg#ifdef HAVE_MTRR
6444f5e7dd7Smrg    struct mtrr_sentry sentry = {
6454f5e7dd7Smrg	.base = map->base,
6464f5e7dd7Smrg        .size = map->size,
6474f5e7dd7Smrg	.type = MTRR_TYPE_UNCACHABLE
6484f5e7dd7Smrg    };
6494f5e7dd7Smrg#endif
6504f5e7dd7Smrg
6514f5e7dd7Smrg    err = pci_device_generic_unmap_range (dev, map);
6524f5e7dd7Smrg    if (err)
6534f5e7dd7Smrg	return err;
654cad31331Smrg
6554f5e7dd7Smrg#ifdef HAVE_MTRR
6564f5e7dd7Smrg    if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
6574f5e7dd7Smrg        sentry.type = MTRR_TYPE_WRBACK;
6584f5e7dd7Smrg    } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
6594f5e7dd7Smrg        sentry.type = MTRR_TYPE_WRCOMB;
6604f5e7dd7Smrg    }
6614f5e7dd7Smrg
6624f5e7dd7Smrg    if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
6634f5e7dd7Smrg	if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
6644f5e7dd7Smrg	    /* FIXME: Should we report an error in this case?
6654f5e7dd7Smrg	     */
6664f5e7dd7Smrg	    fprintf(stderr, "error setting MTRR "
6674f5e7dd7Smrg		    "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
6684f5e7dd7Smrg		    sentry.base, sentry.size, sentry.type,
6694f5e7dd7Smrg		    strerror(errno), errno);
6704f5e7dd7Smrg/*            err = errno;*/
6714f5e7dd7Smrg	}
6724f5e7dd7Smrg    }
6734f5e7dd7Smrg#endif
6744f5e7dd7Smrg
6754f5e7dd7Smrg    return err;
6764f5e7dd7Smrg}
6774f5e7dd7Smrg
6784f5e7dd7Smrgstatic void pci_device_linux_sysfs_enable(struct pci_device *dev)
6794f5e7dd7Smrg{
6804f5e7dd7Smrg    char name[256];
6814f5e7dd7Smrg    int fd;
6824f5e7dd7Smrg
6834f5e7dd7Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
6844f5e7dd7Smrg	      SYS_BUS_PCI,
6854f5e7dd7Smrg	      dev->domain,
6864f5e7dd7Smrg	      dev->bus,
6874f5e7dd7Smrg	      dev->dev,
6884f5e7dd7Smrg	      dev->func );
689cad31331Smrg
690cad31331Smrg    fd = open( name, O_RDWR | O_CLOEXEC);
6914f5e7dd7Smrg    if (fd == -1)
6924f5e7dd7Smrg       return;
6934f5e7dd7Smrg
6944f5e7dd7Smrg    write( fd, "1", 1 );
6954f5e7dd7Smrg    close(fd);
6964f5e7dd7Smrg}
69749f872b5Smrg
69849f872b5Smrgstatic int pci_device_linux_sysfs_boot_vga(struct pci_device *dev)
69949f872b5Smrg{
70049f872b5Smrg    char name[256];
70149f872b5Smrg    char reply[3];
70249f872b5Smrg    int fd, bytes_read;
70349f872b5Smrg    int ret = 0;
70449f872b5Smrg
70549f872b5Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/boot_vga",
70649f872b5Smrg	      SYS_BUS_PCI,
70749f872b5Smrg	      dev->domain,
70849f872b5Smrg	      dev->bus,
70949f872b5Smrg	      dev->dev,
71049f872b5Smrg	      dev->func );
711cad31331Smrg
712cad31331Smrg    fd = open( name, O_RDONLY | O_CLOEXEC);
71349f872b5Smrg    if (fd == -1)
71449f872b5Smrg       return 0;
71549f872b5Smrg
71649f872b5Smrg    bytes_read = read(fd, reply, 1);
71749f872b5Smrg    if (bytes_read != 1)
71849f872b5Smrg	goto out;
71949f872b5Smrg    if (reply[0] == '1')
72049f872b5Smrg	ret = 1;
72149f872b5Smrgout:
72249f872b5Smrg    close(fd);
72349f872b5Smrg    return ret;
72449f872b5Smrg}
72549f872b5Smrg
72649f872b5Smrgstatic int pci_device_linux_sysfs_has_kernel_driver(struct pci_device *dev)
72749f872b5Smrg{
72849f872b5Smrg    char name[256];
72949f872b5Smrg    struct stat dummy;
73049f872b5Smrg    int ret;
73149f872b5Smrg
73249f872b5Smrg    snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/driver",
73349f872b5Smrg	      SYS_BUS_PCI,
73449f872b5Smrg	      dev->domain,
73549f872b5Smrg	      dev->bus,
73649f872b5Smrg	      dev->dev,
73749f872b5Smrg	      dev->func );
738cad31331Smrg
73949f872b5Smrg    ret = stat(name, &dummy);
74049f872b5Smrg    if (ret < 0)
74149f872b5Smrg	return 0;
74249f872b5Smrg    return 1;
74349f872b5Smrg}
744e432255dSmrg
745e432255dSmrgstatic struct pci_io_handle *
746e432255dSmrgpci_device_linux_sysfs_open_device_io(struct pci_io_handle *ret,
747e432255dSmrg				      struct pci_device *dev, int bar,
748e432255dSmrg				      pciaddr_t base, pciaddr_t size)
749e432255dSmrg{
750e432255dSmrg    char name[PATH_MAX];
751e432255dSmrg
752e432255dSmrg    snprintf(name, PATH_MAX, "%s/%04x:%02x:%02x.%1u/resource%d",
753e432255dSmrg	     SYS_BUS_PCI, dev->domain, dev->bus, dev->dev, dev->func, bar);
754e432255dSmrg
755cad31331Smrg    ret->fd = open(name, O_RDWR | O_CLOEXEC);
756e432255dSmrg
757e432255dSmrg    if (ret->fd < 0)
758e432255dSmrg	return NULL;
759e432255dSmrg
760e432255dSmrg    ret->base = base;
761e432255dSmrg    ret->size = size;
762e432255dSmrg
763e432255dSmrg    return ret;
764e432255dSmrg}
765e432255dSmrg
766e432255dSmrgstatic struct pci_io_handle *
767e432255dSmrgpci_device_linux_sysfs_open_legacy_io(struct pci_io_handle *ret,
768e432255dSmrg				      struct pci_device *dev, pciaddr_t base,
769e432255dSmrg				      pciaddr_t size)
770e432255dSmrg{
771e432255dSmrg    char name[PATH_MAX];
772e432255dSmrg
773e432255dSmrg    /* First check if there's a legacy io method for the device */
774e432255dSmrg    while (dev) {
775e432255dSmrg	snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_io",
776e432255dSmrg		 dev->domain, dev->bus);
777e432255dSmrg
778cad31331Smrg	ret->fd = open(name, O_RDWR | O_CLOEXEC);
779e432255dSmrg	if (ret->fd >= 0)
780e432255dSmrg	    break;
781e432255dSmrg
782e432255dSmrg	dev = pci_device_get_parent_bridge(dev);
783e432255dSmrg    }
784e432255dSmrg
785cad31331Smrg    /*
786cad31331Smrg     * You would think you'd want to use /dev/port here.  Don't make that
787cad31331Smrg     * mistake, /dev/port only does byte-wide i/o cycles which means it
788cad31331Smrg     * doesn't work.  If you think this is stupid, well, you're right.
789cad31331Smrg     */
790e432255dSmrg
791cad31331Smrg    /* If we've no other choice, iopl */
792cad31331Smrg    if (ret->fd < 0) {
793cad31331Smrg	if (iopl(3))
794cad31331Smrg	    return NULL;
795cad31331Smrg    }
796e432255dSmrg
797e432255dSmrg    ret->base = base;
798e432255dSmrg    ret->size = size;
799e432255dSmrg
800e432255dSmrg    return ret;
801e432255dSmrg}
802e432255dSmrg
803e432255dSmrgstatic void
804e432255dSmrgpci_device_linux_sysfs_close_io(struct pci_device *dev,
805e432255dSmrg				struct pci_io_handle *handle)
806e432255dSmrg{
807cad31331Smrg    if (handle->fd > -1)
808cad31331Smrg	close(handle->fd);
809e432255dSmrg}
810e432255dSmrg
811e432255dSmrgstatic uint32_t
812e432255dSmrgpci_device_linux_sysfs_read32(struct pci_io_handle *handle, uint32_t port)
813e432255dSmrg{
814e432255dSmrg    uint32_t ret;
815e432255dSmrg
816cad31331Smrg    if (handle->fd > -1)
817cad31331Smrg	pread(handle->fd, &ret, 4, port + handle->base);
818cad31331Smrg    else
819cad31331Smrg	ret = inl(port + handle->base);
820cad31331Smrg
821e432255dSmrg    return ret;
822e432255dSmrg}
823e432255dSmrg
824e432255dSmrgstatic uint16_t
825e432255dSmrgpci_device_linux_sysfs_read16(struct pci_io_handle *handle, uint32_t port)
826e432255dSmrg{
827e432255dSmrg    uint16_t ret;
828e432255dSmrg
829cad31331Smrg    if (handle->fd > -1)
830cad31331Smrg	pread(handle->fd, &ret, 2, port + handle->base);
831cad31331Smrg    else
832cad31331Smrg	ret = inw(port + handle->base);
833e432255dSmrg
834e432255dSmrg    return ret;
835e432255dSmrg}
836e432255dSmrg
837e432255dSmrgstatic uint8_t
838e432255dSmrgpci_device_linux_sysfs_read8(struct pci_io_handle *handle, uint32_t port)
839e432255dSmrg{
840e432255dSmrg    uint8_t ret;
841e432255dSmrg
842cad31331Smrg    if (handle->fd > -1)
843cad31331Smrg	pread(handle->fd, &ret, 1, port + handle->base);
844cad31331Smrg    else
845cad31331Smrg	ret = inb(port + handle->base);
846e432255dSmrg
847e432255dSmrg    return ret;
848e432255dSmrg}
849e432255dSmrg
850e432255dSmrgstatic void
851e432255dSmrgpci_device_linux_sysfs_write32(struct pci_io_handle *handle, uint32_t port,
852e432255dSmrg			       uint32_t data)
853e432255dSmrg{
854cad31331Smrg    if (handle->fd > -1)
855cad31331Smrg	pwrite(handle->fd, &data, 4, port + handle->base);
856cad31331Smrg    else
857cad31331Smrg	outl(data, port + handle->base);
858e432255dSmrg}
859e432255dSmrg
860e432255dSmrgstatic void
861e432255dSmrgpci_device_linux_sysfs_write16(struct pci_io_handle *handle, uint32_t port,
862e432255dSmrg			       uint16_t data)
863e432255dSmrg{
864cad31331Smrg    if (handle->fd > -1)
865cad31331Smrg	pwrite(handle->fd, &data, 2, port + handle->base);
866cad31331Smrg    else
867cad31331Smrg	outw(data, port + handle->base);
868e432255dSmrg}
869e432255dSmrg
870e432255dSmrgstatic void
871e432255dSmrgpci_device_linux_sysfs_write8(struct pci_io_handle *handle, uint32_t port,
872e432255dSmrg			      uint8_t data)
873e432255dSmrg{
874cad31331Smrg    if (handle->fd > -1)
875cad31331Smrg	pwrite(handle->fd, &data, 1, port + handle->base);
876cad31331Smrg    else
877cad31331Smrg	outb(data, port + handle->base);
878cad31331Smrg}
879cad31331Smrg
880cad31331Smrgstatic int
881cad31331Smrgpci_device_linux_sysfs_map_legacy(struct pci_device *dev, pciaddr_t base,
882cad31331Smrg				  pciaddr_t size, unsigned map_flags, void **addr)
883cad31331Smrg{
884cad31331Smrg    char name[PATH_MAX];
885cad31331Smrg    int flags = O_RDONLY;
886cad31331Smrg    int prot = PROT_READ;
887cad31331Smrg    int fd;
888cad31331Smrg    int ret=0;
889cad31331Smrg
890cad31331Smrg    if (map_flags & PCI_DEV_MAP_FLAG_WRITABLE) {
891cad31331Smrg	flags = O_RDWR; /* O_RDWR != O_WRONLY | O_RDONLY */;
892cad31331Smrg	prot |= PROT_WRITE;
893cad31331Smrg    }
894cad31331Smrg
895cad31331Smrg    /* First check if there's a legacy memory method for the device */
896cad31331Smrg    while (dev) {
897cad31331Smrg	snprintf(name, PATH_MAX, "/sys/class/pci_bus/%04x:%02x/legacy_mem",
898cad31331Smrg		 dev->domain, dev->bus);
899cad31331Smrg
900cad31331Smrg	fd = open(name, flags | O_CLOEXEC);
901cad31331Smrg	if (fd >= 0)
902cad31331Smrg	    break;
903cad31331Smrg
904cad31331Smrg	dev = pci_device_get_parent_bridge(dev);
905cad31331Smrg    }
906cad31331Smrg
907cad31331Smrg    /* If not, /dev/mem is the best we can do */
908cad31331Smrg    if (!dev)
909cad31331Smrg	fd = open("/dev/mem", flags | O_CLOEXEC);
910cad31331Smrg
911cad31331Smrg    if (fd < 0)
912cad31331Smrg	return errno;
913cad31331Smrg
914cad31331Smrg    *addr = mmap(NULL, size, prot, MAP_SHARED, fd, base);
915cad31331Smrg    if (*addr == MAP_FAILED) {
916cad31331Smrg	ret = errno;
917cad31331Smrg    }
918cad31331Smrg
919cad31331Smrg    close(fd);
920cad31331Smrg    return ret;
921cad31331Smrg}
922cad31331Smrg
923cad31331Smrgstatic int
924cad31331Smrgpci_device_linux_sysfs_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
925cad31331Smrg{
926cad31331Smrg    return munmap(addr, size);
927cad31331Smrg}
928cad31331Smrg
929cad31331Smrg
930cad31331Smrgstatic void
931cad31331Smrgpci_system_linux_destroy(void)
932cad31331Smrg{
933cad31331Smrg#ifdef HAVE_MTRR
934cad31331Smrg	if (pci_sys->mtrr_fd != -1)
935cad31331Smrg		close(pci_sys->mtrr_fd);
936cad31331Smrg#endif
937e432255dSmrg}
938e432255dSmrg
939e432255dSmrgstatic const struct pci_system_methods linux_sysfs_methods = {
940cad31331Smrg    .destroy = pci_system_linux_destroy,
941e432255dSmrg    .destroy_device = NULL,
942e432255dSmrg    .read_rom = pci_device_linux_sysfs_read_rom,
943e432255dSmrg    .probe = pci_device_linux_sysfs_probe,
944e432255dSmrg    .map_range = pci_device_linux_sysfs_map_range,
945e432255dSmrg    .unmap_range = pci_device_linux_sysfs_unmap_range,
946e432255dSmrg
947e432255dSmrg    .read = pci_device_linux_sysfs_read,
948e432255dSmrg    .write = pci_device_linux_sysfs_write,
949e432255dSmrg
950e432255dSmrg    .fill_capabilities = pci_fill_capabilities_generic,
951e432255dSmrg    .enable = pci_device_linux_sysfs_enable,
952e432255dSmrg    .boot_vga = pci_device_linux_sysfs_boot_vga,
953e432255dSmrg    .has_kernel_driver = pci_device_linux_sysfs_has_kernel_driver,
954e432255dSmrg
955e432255dSmrg    .open_device_io = pci_device_linux_sysfs_open_device_io,
956e432255dSmrg    .open_legacy_io = pci_device_linux_sysfs_open_legacy_io,
957e432255dSmrg    .close_io = pci_device_linux_sysfs_close_io,
958e432255dSmrg    .read32 = pci_device_linux_sysfs_read32,
959e432255dSmrg    .read16 = pci_device_linux_sysfs_read16,
960e432255dSmrg    .read8 = pci_device_linux_sysfs_read8,
961e432255dSmrg    .write32 = pci_device_linux_sysfs_write32,
962e432255dSmrg    .write16 = pci_device_linux_sysfs_write16,
963e432255dSmrg    .write8 = pci_device_linux_sysfs_write8,
964cad31331Smrg
965cad31331Smrg    .map_legacy = pci_device_linux_sysfs_map_legacy,
966cad31331Smrg    .unmap_legacy = pci_device_linux_sysfs_unmap_legacy,
967e432255dSmrg};
968