OSDN Git Service

Solaris: refactor pci_device_solx_devfs_map_range to reduce code duplication
[android-x86/external-libpciaccess.git] / src / solx_devfs.c
index b668318..3eefefb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * (C) Copyright IBM Corporation 2006
- * Copyright (c) 2007, 2009, 2011, Oracle and/or its affiliates.
+ * Copyright (c) 2007, 2009, 2011, 2012, Oracle and/or its affiliates.
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
 #include <libdevinfo.h>
 #include "pci_tools.h"
 
+#ifdef __x86
+# include <sys/sysi86.h>
+# include <sys/psw.h>
+#endif
+
 #include "pciaccess.h"
 #include "pciaccess_private.h"
 
@@ -126,48 +131,6 @@ static di_prom_handle_t di_phdl;
 # define U45_SB_CLASS_RID      0x06040000
 #endif
 
-static int pci_device_solx_devfs_map_range(struct pci_device *dev,
-    struct pci_device_mapping *map);
-
-static int pci_device_solx_devfs_read_rom( struct pci_device * dev,
-    void * buffer );
-
-static int pci_device_solx_devfs_probe( struct pci_device * dev );
-
-static int pci_device_solx_devfs_read( struct pci_device * dev, void * data,
-    pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
-
-static int pci_device_solx_devfs_write( struct pci_device * dev,
-    const void * data, pciaddr_t offset, pciaddr_t size,
-    pciaddr_t * bytes_written );
-
-static int probe_dev(nexus_t *nexus, pcitool_reg_t *prg_p, probe_info_t *pinfo);
-
-static int do_probe(nexus_t *nexus, probe_info_t *pinfo);
-
-static int probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg);
-
-static void pci_system_solx_devfs_destroy( void );
-
-static int get_config_header(int fd, uint8_t bus_no, uint8_t dev_no,
-                            uint8_t func_no, pci_conf_hdr_t *config_hdr_p);
-
-int pci_system_solx_devfs_create( void );
-
-static const struct pci_system_methods solx_devfs_methods = {
-    .destroy = pci_system_solx_devfs_destroy,
-    .destroy_device = NULL,
-    .read_rom = pci_device_solx_devfs_read_rom,
-    .probe = pci_device_solx_devfs_probe,
-    .map_range = pci_device_solx_devfs_map_range,
-    .unmap_range = pci_device_generic_unmap_range,
-
-    .read = pci_device_solx_devfs_read,
-    .write = pci_device_solx_devfs_write,
-
-    .fill_capabilities = pci_fill_capabilities_generic
-};
-
 #ifdef __sparc
 static nexus_t *
 find_nexus_for_dev(struct pci_device *dev)
@@ -256,61 +219,6 @@ pci_system_solx_devfs_destroy( void )
 }
 
 /*
- * Attempt to access PCI subsystem using Solaris's devfs interface.
- * Solaris version
- */
-_pci_hidden int
-pci_system_solx_devfs_create( void )
-{
-    int err = 0;
-    di_node_t di_node;
-    probe_info_t pinfo;
-    struct pci_device_private *devices;
-
-    if (nexus_list != NULL) {
-       return 0;
-    }
-
-    if ((di_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
-       err = errno;
-       (void) fprintf(stderr, "di_init() failed: %s\n",
-                      strerror(errno));
-       return (err);
-    }
-
-    if ((devices = calloc(INITIAL_NUM_DEVICES,
-                       sizeof (struct pci_device_private))) == NULL) {
-       err = errno;
-       di_fini(di_node);
-       return (err);
-    }
-
-#ifdef __sparc
-    if ((di_phdl = di_prom_init()) == DI_PROM_HANDLE_NIL)
-       (void) fprintf(stderr, "di_prom_init failed: %s\n", strerror(errno));
-#endif
-
-    pinfo.num_allocated_elems = INITIAL_NUM_DEVICES;
-    pinfo.num_devices = 0;
-    pinfo.devices = devices;
-    (void) di_walk_minor(di_node, DDI_NT_REGACC, 0, &pinfo, probe_nexus_node);
-
-    di_fini(di_node);
-
-    if ((pci_sys = calloc(1, sizeof (struct pci_system))) == NULL) {
-       err = errno;
-       free(devices);
-       return (err);
-    }
-
-    pci_sys->methods = &solx_devfs_methods;
-    pci_sys->devices = pinfo.devices;
-    pci_sys->num_devices = pinfo.num_devices;
-
-    return (err);
-}
-
-/*
  * Retrieve first 16 dwords of device's config header, except for the first
  * dword.  First 16 dwords are defined by the PCI specification.
  */
@@ -562,6 +470,64 @@ probe_dev(nexus_t *nexus, pcitool_reg_t *prg_p, probe_info_t *pinfo)
     return (rval);
 }
 
+
+/*
+ * Solaris version
+ * Probe a given nexus config space for devices.
+ *
+ * fd is the file descriptor of the nexus.
+ * input_args contains commandline options as specified by the user.
+ */
+static int
+do_probe(nexus_t *nexus, probe_info_t *pinfo)
+{
+    pcitool_reg_t prg;
+    uint32_t bus;
+    uint8_t dev;
+    uint32_t last_bus = nexus->last_bus;
+    uint8_t last_dev = PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT;
+    uint8_t first_bus = nexus->first_bus;
+    uint8_t first_dev = 0;
+    int rval = 0;
+
+    prg.barnum = 0;    /* Config space. */
+
+    /* Must read in 4-byte quantities. */
+    prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
+
+    prg.data = 0;
+
+    /*
+     * Loop through all valid bus / dev / func combinations to check for
+     * all devices, with the following exceptions:
+     *
+     * When nothing is found at function 0 of a bus / dev combination, skip
+     * the other functions of that bus / dev combination.
+     *
+     * When a found device's function 0 is probed and it is determined that
+     * it is not a multifunction device, skip probing of that device's
+     * other functions.
+     */
+    for (bus = first_bus; ((bus <= last_bus) && (rval == 0)); bus++) {
+       prg.bus_no = (uint8_t)bus;
+
+       for (dev = first_dev; ((dev <= last_dev) && (rval == 0)); dev++) {
+           prg.dev_no = dev;
+           rval = probe_dev(nexus, &prg, pinfo);
+       }
+
+       /*
+        * Ultra-45 southbridge workaround:
+        * ECANCELED tells to skip to the next bus.
+        */
+       if (rval == ECANCELED) {
+           rval = 0;
+       }
+    }
+
+    return (rval);
+}
+
 /*
  * This function is called from di_walk_minor() when any PROBE is processed
  */
@@ -702,7 +668,7 @@ probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg)
            nexus_path, first_bus, last_bus);
 #endif
 
-    if ((fd = open(nexus_path, O_RDWR)) >= 0) {
+    if ((fd = open(nexus_path, O_RDWR | O_CLOEXEC)) >= 0) {
        nexus->fd = fd;
        nexus->path = strdup(nexus_path);
        nexus_dev_path = di_devfs_path(di_node);
@@ -728,64 +694,6 @@ probe_nexus_node(di_node_t di_node, di_minor_t minor, void *arg)
     return DI_WALK_CONTINUE;
 }
 
-
-/*
- * Solaris version
- * Probe a given nexus config space for devices.
- *
- * fd is the file descriptor of the nexus.
- * input_args contains commandline options as specified by the user.
- */
-static int
-do_probe(nexus_t *nexus, probe_info_t *pinfo)
-{
-    pcitool_reg_t prg;
-    uint32_t bus;
-    uint8_t dev;
-    uint32_t last_bus = nexus->last_bus;
-    uint8_t last_dev = PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT;
-    uint8_t first_bus = nexus->first_bus;
-    uint8_t first_dev = 0;
-    int rval = 0;
-
-    prg.barnum = 0;    /* Config space. */
-
-    /* Must read in 4-byte quantities. */
-    prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
-
-    prg.data = 0;
-
-    /*
-     * Loop through all valid bus / dev / func combinations to check for
-     * all devices, with the following exceptions:
-     *
-     * When nothing is found at function 0 of a bus / dev combination, skip
-     * the other functions of that bus / dev combination.
-     *
-     * When a found device's function 0 is probed and it is determined that
-     * it is not a multifunction device, skip probing of that device's
-     * other functions.
-     */
-    for (bus = first_bus; ((bus <= last_bus) && (rval == 0)); bus++) {
-       prg.bus_no = (uint8_t)bus;
-
-       for (dev = first_dev; ((dev <= last_dev) && (rval == 0)); dev++) {
-           prg.dev_no = dev;
-           rval = probe_dev(nexus, &prg, pinfo);
-       }
-
-       /*
-        * Ultra-45 southbridge workaround:
-        * ECANCELED tells to skip to the next bus.
-        */
-       if (rval == ECANCELED) {
-           rval = 0;
-       }
-    }
-
-    return (rval);
-}
-
 static int
 find_target_node(di_node_t node, void *arg)
 {
@@ -1002,6 +910,71 @@ pci_device_solx_devfs_probe( struct pci_device * dev )
     return (err);
 }
 
+/**
+ * Map a memory region for a device using /dev/xsvc (x86) or fb device (sparc)
+ *
+ * \param dev   Device whose memory region is to be mapped.
+ * \param map   Parameters of the mapping that is to be created.
+ *
+ * \return
+ * Zero on success or an \c errno value on failure.
+ */
+static int
+pci_device_solx_devfs_map_range(struct pci_device *dev,
+                               struct pci_device_mapping *map)
+{
+    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
+                       ? (PROT_READ | PROT_WRITE) : PROT_READ;
+    int err = 0;
+
+    const char *map_dev;
+    int                map_fd;
+
+#ifdef __sparc
+    char       map_dev_buf[128];
+
+    if (MAPPING_DEV_PATH(dev)) {
+       snprintf(map_dev_buf, sizeof (map_dev_buf), "%s%s",
+                "/devices", MAPPING_DEV_PATH(dev));
+       map_dev = map_dev_buf;
+    }
+    else
+       map_dev = "/dev/fb0";
+
+    map_fd = -1;
+#else
+    /*
+     * Still uses xsvc to do the user space mapping on x86/x64,
+     * caches open fd across multiple calls.
+     */
+    map_dev = "/dev/xsvc";
+    map_fd = xsvc_fd;
+#endif
+
+    if (map_fd < 0) {
+       if ((map_fd = open(map_dev, O_RDWR | O_CLOEXEC)) < 0) {
+           err = errno;
+           (void) fprintf(stderr, "can not open %s: %s\n", map_dev,
+                          strerror(errno));
+           return err;
+       }
+    }
+
+    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, map_fd, map->base);
+    if (map->memory == MAP_FAILED) {
+       err = errno;
+
+       (void) fprintf(stderr, "map rom region =%llx failed: %s\n",
+                      map->base, strerror(errno));
+    }
+
+#ifdef __sparc
+    close (map_fd);
+#endif
+
+    return err;
+}
+
 /*
  * Solaris version: read the VGA ROM data
  */
@@ -1115,19 +1088,19 @@ pci_device_solx_devfs_write( struct pci_device * dev, const void * data,
     switch (size) {
         case 1:
            cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_1 + NATIVE_ENDIAN;
-           cfg_prg.data = *((uint8_t *)data);
+           cfg_prg.data = *((const uint8_t *)data);
            break;
         case 2:
            cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_2 + NATIVE_ENDIAN;
-           cfg_prg.data = *((uint16_t *)data);
+           cfg_prg.data = *((const uint16_t *)data);
            break;
         case 4:
            cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_4 + NATIVE_ENDIAN;
-           cfg_prg.data = *((uint32_t *)data);
+           cfg_prg.data = *((const uint32_t *)data);
            break;
         case 8:
            cfg_prg.acc_attr = PCITOOL_ACC_ATTR_SIZE_8 + NATIVE_ENDIAN;
-           cfg_prg.data = *((uint64_t *)data);
+           cfg_prg.data = *((const uint64_t *)data);
            break;
         default:
            return EINVAL;
@@ -1155,67 +1128,201 @@ pci_device_solx_devfs_write( struct pci_device * dev, const void * data,
     return (err);
 }
 
+static struct pci_io_handle *
+pci_device_solx_devfs_open_legacy_io(struct pci_io_handle *ret,
+                                    struct pci_device *dev,
+                                    pciaddr_t base, pciaddr_t size)
+{
+#ifdef __x86
+    if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) == 0) {
+       ret->base = base;
+       ret->size = size;
+       return ret;
+    }
+#endif
+    return NULL;
+}
 
-/**
- * Map a memory region for a device using /dev/xsvc.
- *
- * \param dev   Device whose memory region is to be mapped.
- * \param map   Parameters of the mapping that is to be created.
- *
- * \return
- * Zero on success or an \c errno value on failure.
- */
-static int
-pci_device_solx_devfs_map_range(struct pci_device *dev,
-                               struct pci_device_mapping *map)
+static uint32_t
+pci_device_solx_devfs_read32(struct pci_io_handle *handle, uint32_t reg)
 {
-    const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
-                       ? (PROT_READ | PROT_WRITE) : PROT_READ;
-    int err = 0;
+#ifdef __x86
+    uint16_t port = (uint16_t) (handle->base + reg);
+    uint32_t ret;
+    __asm__ __volatile__("inl %1,%0":"=a"(ret):"d"(port));
+    return ret;
+#else
+    return *(uint32_t *)((uintptr_t)handle->memory + reg);
+#endif
+}
 
-#ifdef __sparc
-    char       map_dev[128];
-    int                map_fd;
+static uint16_t
+pci_device_solx_devfs_read16(struct pci_io_handle *handle, uint32_t reg)
+{
+#ifdef __x86
+    uint16_t port = (uint16_t) (handle->base + reg);
+    uint16_t ret;
+    __asm__ __volatile__("inw %1,%0":"=a"(ret):"d"(port));
+    return ret;
+#else
+    return *(uint16_t *)((uintptr_t)handle->memory + reg);
+#endif
+}
 
-    if (MAPPING_DEV_PATH(dev))
-       snprintf(map_dev, sizeof (map_dev), "%s%s", "/devices", MAPPING_DEV_PATH(dev));
-    else
-       strcpy (map_dev, "/dev/fb0");
+static uint8_t
+pci_device_solx_devfs_read8(struct pci_io_handle *handle, uint32_t reg)
+{
+#ifdef __x86
+    uint16_t port = (uint16_t) (handle->base + reg);
+    uint8_t ret;
+    __asm__ __volatile__("inb %1,%0":"=a"(ret):"d"(port));
+    return ret;
+#else
+    return *(uint8_t *)((uintptr_t)handle->memory + reg);
+#endif
+}
 
-    if ((map_fd = open(map_dev, O_RDWR)) < 0) {
-       err = errno;
-       (void) fprintf(stderr, "can not open %s: %s\n", map_dev,
-                          strerror(errno));
-       return err;
-    }
+static void
+pci_device_solx_devfs_write32(struct pci_io_handle *handle, uint32_t reg,
+    uint32_t data)
+{
+#ifdef __x86
+      uint16_t port = (uint16_t) (handle->base + reg);
+      __asm__ __volatile__("outl %0,%1"::"a"(data), "d"(port));
+#else
+      *(uint16_t *)((uintptr_t)handle->memory + reg) = data;
+#endif
+}
 
-    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, map_fd, map->base);
+static void
+pci_device_solx_devfs_write16(struct pci_io_handle *handle, uint32_t reg,
+    uint16_t data)
+{
+#ifdef __x86
+      uint16_t port = (uint16_t) (handle->base + reg);
+      __asm__ __volatile__("outw %0,%1"::"a"(data), "d"(port));
 #else
-    /*
-     * Still used xsvc to do the user space mapping
-     */
-    if (xsvc_fd < 0) {
-       if ((xsvc_fd = open("/dev/xsvc", O_RDWR)) < 0) {
-           err = errno;
-           (void) fprintf(stderr, "can not open /dev/xsvc: %s\n",
-                          strerror(errno));
-           return err;
-       }
-    }
+    *(uint8_t *)((uintptr_t)handle->memory + reg) = data;
+#endif
+}
 
-    map->memory = mmap(NULL, map->size, prot, MAP_SHARED, xsvc_fd, map->base);
+static void
+pci_device_solx_devfs_write8(struct pci_io_handle *handle, uint32_t reg,
+    uint8_t data)
+{
+#ifdef __x86
+      uint16_t port = (uint16_t) (handle->base + reg);
+      __asm__ __volatile__("outb %0,%1"::"a"(data), "d"(port));
+#else
+      *(uint32_t *)((uintptr_t)handle->memory + reg) = data;
 #endif
+}
 
-    if (map->memory == MAP_FAILED) {
+static int
+pci_device_solx_devfs_map_legacy(struct pci_device *dev, pciaddr_t base,
+                                pciaddr_t size, unsigned map_flags,
+                                void **addr)
+{
+    int err;
+    struct pci_device_mapping map = {
+       .base = base,
+       .size = size,
+       .flags = map_flags,
+    };
+
+    err = pci_device_solx_devfs_map_range(dev, &map);
+    if (err == 0)
+       *addr = map.memory;
+    return err;
+}
+
+static int
+pci_device_solx_devfs_unmap_legacy(struct pci_device *dev,
+                                  void *addr, pciaddr_t size)
+{
+    struct pci_device_mapping map = {
+       .memory = addr,
+       .size = size,
+    };
+
+    return pci_device_generic_unmap_range(dev, &map);
+}
+
+static const struct pci_system_methods solx_devfs_methods = {
+    .destroy = pci_system_solx_devfs_destroy,
+    .destroy_device = NULL,
+    .read_rom = pci_device_solx_devfs_read_rom,
+    .probe = pci_device_solx_devfs_probe,
+    .map_range = pci_device_solx_devfs_map_range,
+    .unmap_range = pci_device_generic_unmap_range,
+
+    .read = pci_device_solx_devfs_read,
+    .write = pci_device_solx_devfs_write,
+
+    .fill_capabilities = pci_fill_capabilities_generic,
+
+    .open_legacy_io = pci_device_solx_devfs_open_legacy_io,
+    .read32 = pci_device_solx_devfs_read32,
+    .read16 = pci_device_solx_devfs_read16,
+    .read8 = pci_device_solx_devfs_read8,
+    .write32 = pci_device_solx_devfs_write32,
+    .write16 = pci_device_solx_devfs_write16,
+    .write8 = pci_device_solx_devfs_write8,
+    .map_legacy = pci_device_solx_devfs_map_legacy,
+    .unmap_legacy = pci_device_solx_devfs_unmap_legacy,
+};
+
+/*
+ * Attempt to access PCI subsystem using Solaris's devfs interface.
+ * Solaris version
+ */
+_pci_hidden int
+pci_system_solx_devfs_create( void )
+{
+    int err = 0;
+    di_node_t di_node;
+    probe_info_t pinfo;
+    struct pci_device_private *devices;
+
+    if (nexus_list != NULL) {
+       return 0;
+    }
+
+    if ((di_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
        err = errno;
+       (void) fprintf(stderr, "di_init() failed: %s\n",
+                      strerror(errno));
+       return (err);
+    }
 
-       (void) fprintf(stderr, "map rom region =%llx failed: %s\n",
-                      map->base, strerror(errno));
+    if ((devices = calloc(INITIAL_NUM_DEVICES,
+                       sizeof (struct pci_device_private))) == NULL) {
+       err = errno;
+       di_fini(di_node);
+       return (err);
     }
 
 #ifdef __sparc
-    close (map_fd);
+    if ((di_phdl = di_prom_init()) == DI_PROM_HANDLE_NIL)
+       (void) fprintf(stderr, "di_prom_init failed: %s\n", strerror(errno));
 #endif
 
-    return err;
+    pinfo.num_allocated_elems = INITIAL_NUM_DEVICES;
+    pinfo.num_devices = 0;
+    pinfo.devices = devices;
+    (void) di_walk_minor(di_node, DDI_NT_REGACC, 0, &pinfo, probe_nexus_node);
+
+    di_fini(di_node);
+
+    if ((pci_sys = calloc(1, sizeof (struct pci_system))) == NULL) {
+       err = errno;
+       free(devices);
+       return (err);
+    }
+
+    pci_sys->methods = &solx_devfs_methods;
+    pci_sys->devices = pinfo.devices;
+    pci_sys->num_devices = pinfo.num_devices;
+
+    return (err);
 }