From: Jan Viktorin <vikto...@rehivetech.com>

Additional features introduced:
 - Find kernel driver through sysfs bindings
 - Dummy implementation for mapping to kernel driver
 - DMA coherency value from sysfs
 - Numa node number from sysfs
 - Support for updating device during probe if already registered

Signed-off-by: Jan Viktorin <viktorin at rehivetech.com>
[Shreyansh: merge multiple patches into single set]
Signed-off-by: Shreyansh Jain <shreyansh.jain at nxp.com>
---
 lib/librte_eal/common/eal_common_soc.c  |  30 ++++++++
 lib/librte_eal/common/eal_private.h     |  23 ++++++
 lib/librte_eal/common/include/rte_soc.h |  28 +++++++
 lib/librte_eal/linuxapp/eal/eal_soc.c   | 129 ++++++++++++++++++++++++++++++++
 4 files changed, 210 insertions(+)

diff --git a/lib/librte_eal/common/eal_common_soc.c 
b/lib/librte_eal/common/eal_common_soc.c
index 44f5559..29c38e0 100644
--- a/lib/librte_eal/common/eal_common_soc.c
+++ b/lib/librte_eal/common/eal_common_soc.c
@@ -114,6 +114,26 @@ rte_eal_soc_probe_one_driver(struct rte_soc_driver *drv,
                return ret;
        }

+       if (!dev->is_dma_coherent) {
+               if (!(drv->drv_flags & RTE_SOC_DRV_ACCEPT_NONCC)) {
+                       RTE_LOG(DEBUG, EAL,
+                               "  device is not DMA coherent, skipping\n");
+                       return 1;
+               }
+       }
+
+       if (drv->drv_flags & RTE_SOC_DRV_NEED_MAPPING) {
+               /* map resources */
+               ret = rte_eal_soc_map_device(dev);
+               if (ret)
+                       return ret;
+       } else if (drv->drv_flags & RTE_SOC_DRV_FORCE_UNBIND
+               && rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               /* unbind */
+               if (soc_unbind_kernel_driver(dev) < 0)
+                       return -1;
+       }
+
        dev->driver = drv;
        RTE_VERIFY(drv->probe != NULL);
        return drv->probe(drv, dev);
@@ -166,6 +186,10 @@ rte_eal_soc_detach_dev(struct rte_soc_driver *drv,
        if (drv->remove && (drv->remove(dev) < 0))
                return -1;      /* negative value is an error */

+       if (drv->drv_flags & RTE_SOC_DRV_NEED_MAPPING)
+               /* unmap resources for devices */
+               rte_eal_soc_unmap_device(dev);
+
        /* clear driver structure */
        dev->driver = NULL;

@@ -241,6 +265,12 @@ rte_eal_soc_probe_one(const struct rte_soc_addr *addr)
        if (addr == NULL)
                return -1;

+       /* update current SoC device in global list, kernel bindings might have
+        * changed since last time we looked at it.
+        */
+       if (soc_update_device(addr) < 0)
+               goto err_return;
+
        TAILQ_FOREACH(dev, &soc_device_list, next) {
                if (rte_eal_compare_soc_addr(&dev->addr, addr))
                        continue;
diff --git a/lib/librte_eal/common/eal_private.h 
b/lib/librte_eal/common/eal_private.h
index d810f9f..30c648d 100644
--- a/lib/librte_eal/common/eal_private.h
+++ b/lib/librte_eal/common/eal_private.h
@@ -159,6 +159,29 @@ int pci_update_device(const struct rte_pci_addr *addr);
 int pci_unbind_kernel_driver(struct rte_pci_device *dev);

 /**
+ * Update a soc device object by asking the kernel for the latest information.
+ *
+ * This function is private to EAL.
+ *
+ * @param addr
+ *      The SoC address to look for
+ * @return
+ *   - 0 on success.
+ *   - negative on error.
+ */
+int soc_update_device(const struct rte_soc_addr *addr);
+
+/**
+ * Unbind kernel driver for this device
+ *
+ * This function is private to EAL.
+ *
+ * @return
+ *   0 on success, negative on error
+ */
+int soc_unbind_kernel_driver(struct rte_soc_device *dev);
+
+/**
  * Map the PCI resource of a PCI device in virtual memory
  *
  * This function is private to EAL.
diff --git a/lib/librte_eal/common/include/rte_soc.h 
b/lib/librte_eal/common/include/rte_soc.h
index 1865be5..93205c9 100644
--- a/lib/librte_eal/common/include/rte_soc.h
+++ b/lib/librte_eal/common/include/rte_soc.h
@@ -46,9 +46,11 @@ extern "C" {

 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/queue.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <string.h>
+#include <limits.h>

 #include <rte_dev.h>
 #include <rte_eal.h>
@@ -63,6 +65,14 @@ extern struct soc_device_list soc_device_list;
 TAILQ_HEAD(soc_driver_list, rte_soc_driver); /**< SoC drivers in D-linked Q. */
 TAILQ_HEAD(soc_device_list, rte_soc_device); /**< SoC devices in D-linked Q. */

+#define SOC_MAX_RESOURCE 6
+
+struct rte_soc_resource {
+       uint64_t phys_addr;
+       uint64_t len;
+       void *addr;
+};
+
 struct rte_soc_id {
        union {
                const char *compatible; /**< OF compatible specification */
@@ -84,8 +94,12 @@ struct rte_soc_device {
        struct rte_device device;           /**< Inherit code device */
        struct rte_soc_addr addr;           /**< SoC device Location */
        struct rte_soc_id *id;              /**< SoC device ID list */
+       struct rte_soc_resource mem_resource[SOC_MAX_RESOURCE];
        struct rte_intr_handle intr_handle; /**< Interrupt handle */
        struct rte_soc_driver *driver;      /**< Associated driver */
+       int numa_node;                      /**< NUMA node connection */
+       int is_dma_coherent;                /**< DMA coherent device */
+       enum rte_kernel_driver kdrv;        /**< Kernel driver */
 };

 struct rte_soc_driver;
@@ -139,6 +153,8 @@ struct rte_soc_driver {
 #define RTE_SOC_DRV_INTR_LSC    0x0008
 /** Device driver supports detaching capability */
 #define RTE_SOC_DRV_DETACHABLE  0x0010
+/** Device driver accepts DMA non-coherent devices */
+#define RTE_SOC_DRV_ACCEPT_NONCC 0x0020

 /**
  * Utility function to write a SoC device name, this device name can later be
@@ -252,6 +268,18 @@ int rte_eal_soc_probe_one(const struct rte_soc_addr *addr);
 int rte_eal_soc_detach(const struct rte_soc_addr *addr);

 /**
+ * Map SoC device resources into userspace.
+ *
+ * This is called by the EAL if (drv_flags & RTE_SOC_DRV_NEED_MAPPING).
+ */
+int rte_eal_soc_map_device(struct rte_soc_device *dev);
+
+/**
+ * Unmap the device resources.
+ */
+void rte_eal_soc_unmap_device(struct rte_soc_device *dev);
+
+/**
  * Dump discovered SoC devices.
  *
  * @param f
diff --git a/lib/librte_eal/linuxapp/eal/eal_soc.c 
b/lib/librte_eal/linuxapp/eal/eal_soc.c
index d8286bb..d07bb40 100644
--- a/lib/librte_eal/linuxapp/eal/eal_soc.c
+++ b/lib/librte_eal/linuxapp/eal/eal_soc.c
@@ -63,6 +63,45 @@ soc_get_sysfs_path(void)
        return path;
 }

+int
+soc_unbind_kernel_driver(struct rte_soc_device *dev)
+{
+       char devpath[PATH_MAX];
+
+       snprintf(devpath, sizeof(devpath), "%s/%s",
+                soc_get_sysfs_path(), dev->addr.name);
+
+       return rte_eal_unbind_kernel_driver(devpath, dev->addr.name);
+}
+
+int
+rte_eal_soc_map_device(struct rte_soc_device *dev)
+{
+       int ret = -1;
+
+       /* try mapping the NIC resources using VFIO if it exists */
+       switch (dev->kdrv) {
+       default:
+               RTE_LOG(DEBUG, EAL,
+                       "  Not managed by a supported kernel driver, 
skipped\n");
+               ret = 1;
+               break;
+       }
+
+       return ret;
+}
+
+void
+rte_eal_soc_unmap_device(struct rte_soc_device *dev)
+{
+       switch (dev->kdrv) {
+       default:
+       RTE_LOG(DEBUG, EAL,
+               "  Not managed by a supported kernel driver, skipped\n");
+               break;
+       }
+}
+
 static char *
 dev_read_uevent(const char *dirname)
 {
@@ -260,6 +299,68 @@ dev_content_free(struct rte_soc_device *dev)
        }
 }

+static int
+dev_setup_associated_driver(struct rte_soc_device *dev, const char *dirname)
+{
+       char filename[PATH_MAX];
+       char driver[PATH_MAX];
+       int ret;
+
+       /* parse driver */
+       snprintf(filename, sizeof(filename), "%s/driver", dirname);
+       ret = rte_eal_get_kernel_driver_by_path(filename, driver);
+       if (ret < 0) {
+               RTE_LOG(ERR, EAL, "Fail to get kernel driver for %s\n",
+                       dirname);
+               return 1;
+       }
+
+       if (!ret)
+               dev->kdrv = RTE_KDRV_UNKNOWN;
+       else
+               dev->kdrv = RTE_KDRV_NONE;
+
+       return 0;
+}
+
+static int
+dev_setup_numa_node(struct rte_soc_device *dev, const char *dirname)
+{
+       char filename[PATH_MAX];
+
+       /* if no NUMA support, set default to 0 */
+       unsigned long tmp = 0;
+       int ret = 0;
+
+       /* get numa node */
+       snprintf(filename, sizeof(filename), "%s/numa_node", dirname);
+
+       if (eal_parse_sysfs_value(filename, &tmp) < 0)
+               ret = 1;
+
+       dev->numa_node = tmp;
+       return ret;
+}
+
+static int
+dev_detect_is_coherent(struct rte_soc_device *dev)
+{
+       char filename[PATH_MAX];
+       FILE *f;
+
+       if (dev->addr.fdt_path == NULL)
+               return 0; /* no way to detect */
+
+       snprintf(filename, sizeof(filename), "%s%s/dma-coherent",
+                       "/proc/device-tree", dev->addr.fdt_path);
+       f = fopen(filename, "r");
+       if (f == NULL)
+               return 0;
+
+       fclose(f);
+       return 1;
+}
+
 /**
  * Scan one SoC sysfs entry, and fill the devices list from it.
  * We require to have the uevent file with records: OF_FULLNAME and
@@ -299,6 +400,18 @@ soc_scan_one(const char *dirname, const char *name)
                goto fail;
        free(uevent); /* not needed anymore */

+       ret = dev_setup_associated_driver(dev, dirname);
+       if (ret)
+               goto fail;
+
+       ret = dev_setup_numa_node(dev, dirname);
+       if (ret < 0)
+               goto fail;
+
+       dev->is_dma_coherent = dev_detect_is_coherent(dev);
+       RTE_LOG(DEBUG, EAL, "  DMA %s\n",
+                       dev->is_dma_coherent ? "coherent" : "non-coherent");
+
        /* device is valid, add in list (sorted) */
        if (TAILQ_EMPTY(&soc_device_list)) {
                TAILQ_INSERT_TAIL(&soc_device_list, dev, next);
@@ -313,6 +426,11 @@ soc_scan_one(const char *dirname, const char *name)
                        if (ret < 0) {
                                TAILQ_INSERT_BEFORE(dev2, dev, next);
                        } else { /* already registered */
+                               dev2->kdrv = dev->kdrv;
+                               dev2->is_dma_coherent = dev->is_dma_coherent;
+                               memmove(dev2->mem_resource, dev->mem_resource,
+                                       sizeof(dev->mem_resource));
+
                                dev_content_free(dev2);
                                dev2->addr.fdt_path = dev->addr.fdt_path;
                                dev2->id = dev->id;
@@ -333,6 +451,17 @@ fail:
 }

 int
+soc_update_device(const struct rte_soc_addr *addr)
+{
+       char filename[PATH_MAX];
+
+       snprintf(filename, sizeof(filename), "%s/%s",
+                       soc_get_sysfs_path(), addr->name);
+
+       return soc_scan_one(filename, addr->name);
+}
+
+int
 rte_eal_soc_scan(void)
 {
        struct dirent *e;
-- 
2.7.4

Reply via email to