From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2C394A046B for ; Wed, 24 Jul 2019 18:46:40 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E12761C238; Wed, 24 Jul 2019 18:46:39 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by dpdk.org (Postfix) with ESMTP id 6A9311C235 for ; Wed, 24 Jul 2019 18:46:38 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jul 2019 09:46:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,303,1559545200"; d="scan'208";a="181162571" Received: from silpixa00399498.ir.intel.com (HELO silpixa00399498.ger.corp.intel.com) ([10.237.223.125]) by orsmga002.jf.intel.com with ESMTP; 24 Jul 2019 09:46:35 -0700 From: Anatoly Burakov To: dev@dpdk.org Cc: david.marchand@redhat.com, jerinj@marvell.com, thomas@monjalon.net Date: Wed, 24 Jul 2019 17:46:35 +0100 Message-Id: <5d8f83fb7dd574d83a044c6a01e2613798f256c3.1563986790.git.anatoly.burakov@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [PATCH] eal: pick IOVA as PA if IOMMU is not available X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" When IOMMU is not available, /sys/kernel/iommu_groups will not be populated. This is happening since at least 3.6 when VFIO support was added. If the directory is empty, EAL should not pick IOVA as VA as the default IOVA mode. We also assume that VFIO equals IOMMU, so if VFIO support is not compiled, we always assume IOMMU support is not available. Signed-off-by: Anatoly Burakov --- lib/librte_eal/linux/eal/eal.c | 11 ++++++-- lib/librte_eal/linux/eal/eal_vfio.c | 39 +++++++++++++++++++++++++++++ lib/librte_eal/linux/eal/eal_vfio.h | 2 ++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c index 34db78753..584f97a96 100644 --- a/lib/librte_eal/linux/eal/eal.c +++ b/lib/librte_eal/linux/eal/eal.c @@ -1061,8 +1061,15 @@ rte_eal_init(int argc, char **argv) enum rte_iova_mode iova_mode = rte_bus_get_iommu_class(); if (iova_mode == RTE_IOVA_DC) { - iova_mode = RTE_IOVA_VA; - RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode, select IOVA as VA mode.\n"); + /* if we have an IOMMU, pick IOVA as VA mode */ + if (vfio_iommu_enabled()) { + iova_mode = RTE_IOVA_VA; + RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode, selecting IOVA as VA mode.\n"); + } else { + iova_mode = RTE_IOVA_PA; + RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode, but IOMMU is not available.\n"); + RTE_LOG(DEBUG, EAL, "Selecting IOVA as PA mode.\n"); + } } #ifdef RTE_LIBRTE_KNI /* Workaround for KNI which requires physical address to work */ diff --git a/lib/librte_eal/linux/eal/eal_vfio.c b/lib/librte_eal/linux/eal/eal_vfio.c index 501c74f23..6d5ca7903 100644 --- a/lib/librte_eal/linux/eal/eal_vfio.c +++ b/lib/librte_eal/linux/eal/eal_vfio.c @@ -2,6 +2,7 @@ * Copyright(c) 2010-2018 Intel Corporation */ +#include #include #include #include @@ -23,6 +24,8 @@ #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb" +#define VFIO_KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups" + /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can * recreate the mappings for DPDK segments, but we cannot do so for memory that * was registered by the user themselves, so we need to store the user mappings @@ -2026,6 +2029,33 @@ rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova, return container_dma_unmap(vfio_cfg, vaddr, iova, len); } +/* + * on Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the + * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel + * IOMMU groups. If IOMMU is not enabled, that path would be empty. Therefore, + * checking if the path is empty will tell us if IOMMU is enabled. + */ +int +vfio_iommu_enabled(void) +{ + DIR *dir = opendir(VFIO_KERNEL_IOMMU_GROUPS_PATH); + struct dirent *d; + int n = 0; + + /* if directory doesn't exist, assume IOMMU is not enabled */ + if (dir == NULL) + return 0; + + while ((d = readdir(dir)) != NULL) { + /* skip dot and dot-dot */ + if (++n > 2) + break; + } + closedir(dir); + + return n > 2; +} + #else int @@ -2146,4 +2176,13 @@ rte_vfio_container_dma_unmap(__rte_unused int container_fd, return -1; } +/* + * VFIO not compiled, so IOMMU unsupported. + */ +int +vfio_iommu_enabled(void) +{ + return 0; +} + #endif /* VFIO_PRESENT */ diff --git a/lib/librte_eal/linux/eal/eal_vfio.h b/lib/librte_eal/linux/eal/eal_vfio.h index cb2d35fb1..58c7a7309 100644 --- a/lib/librte_eal/linux/eal/eal_vfio.h +++ b/lib/librte_eal/linux/eal/eal_vfio.h @@ -133,6 +133,8 @@ vfio_has_supported_extensions(int vfio_container_fd); int vfio_mp_sync_setup(void); +int vfio_iommu_enabled(void); + #define EAL_VFIO_MP "eal_vfio_mp_sync" #define SOCKET_REQ_CONTAINER 0x100 -- 2.17.1