LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_vfio.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 62 803 7.7 %
Date: 2025-12-01 19:08:10 Functions: 4 51 7.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 622 5.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <uapi/linux/vfio.h>
       6                 :            : 
       7                 :            : #include <inttypes.h>
       8                 :            : #include <string.h>
       9                 :            : #include <fcntl.h>
      10                 :            : #include <unistd.h>
      11                 :            : #include <sys/ioctl.h>
      12                 :            : #include <dirent.h>
      13                 :            : 
      14                 :            : #include <rte_errno.h>
      15                 :            : #include <rte_log.h>
      16                 :            : #include <rte_memory.h>
      17                 :            : #include <rte_eal_memconfig.h>
      18                 :            : #include <rte_vfio.h>
      19                 :            : 
      20                 :            : #include <eal_export.h>
      21                 :            : #include "eal_filesystem.h"
      22                 :            : #include "eal_memcfg.h"
      23                 :            : #include "eal_vfio.h"
      24                 :            : #include "eal_private.h"
      25                 :            : #include "eal_internal_cfg.h"
      26                 :            : 
      27                 :            : #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
      28                 :            : 
      29                 :            : /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
      30                 :            :  * recreate the mappings for DPDK segments, but we cannot do so for memory that
      31                 :            :  * was registered by the user themselves, so we need to store the user mappings
      32                 :            :  * somewhere, to recreate them later.
      33                 :            :  */
      34                 :            : #define EAL_VFIO_MAX_USER_MEM_MAPS 256
      35                 :            : struct user_mem_map {
      36                 :            :         uint64_t addr;  /**< start VA */
      37                 :            :         uint64_t iova;  /**< start IOVA */
      38                 :            :         uint64_t len;   /**< total length of the mapping */
      39                 :            :         uint64_t chunk; /**< this mapping can be split in chunks of this size */
      40                 :            : };
      41                 :            : 
      42                 :            : struct user_mem_maps {
      43                 :            :         rte_spinlock_recursive_t lock;
      44                 :            :         int n_maps;
      45                 :            :         struct user_mem_map maps[EAL_VFIO_MAX_USER_MEM_MAPS];
      46                 :            : };
      47                 :            : 
      48                 :            : struct vfio_config {
      49                 :            :         int vfio_enabled;
      50                 :            :         int vfio_container_fd;
      51                 :            :         int vfio_active_groups;
      52                 :            :         const struct vfio_iommu_type *vfio_iommu_type;
      53                 :            :         struct vfio_group vfio_groups[RTE_MAX_VFIO_GROUPS];
      54                 :            :         struct user_mem_maps mem_maps;
      55                 :            : };
      56                 :            : 
      57                 :            : /* per-process VFIO config */
      58                 :            : static struct vfio_config vfio_cfgs[RTE_MAX_VFIO_CONTAINERS];
      59                 :            : static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
      60                 :            : 
      61                 :            : static int vfio_type1_dma_map(int);
      62                 :            : static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      63                 :            : static int vfio_spapr_dma_map(int);
      64                 :            : static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      65                 :            : static int vfio_noiommu_dma_map(int);
      66                 :            : static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      67                 :            : static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
      68                 :            :                 uint64_t iova, uint64_t len, int do_map);
      69                 :            : 
      70                 :            : /* IOMMU types we support */
      71                 :            : static const struct vfio_iommu_type iommu_types[] = {
      72                 :            :         /* x86 IOMMU, otherwise known as type 1 */
      73                 :            :         {
      74                 :            :                 .type_id = VFIO_TYPE1_IOMMU,
      75                 :            :                 .name = "Type 1",
      76                 :            :                 .partial_unmap = false,
      77                 :            :                 .dma_map_func = &vfio_type1_dma_map,
      78                 :            :                 .dma_user_map_func = &vfio_type1_dma_mem_map
      79                 :            :         },
      80                 :            :         /* ppc64 IOMMU, otherwise known as spapr */
      81                 :            :         {
      82                 :            :                 .type_id = VFIO_SPAPR_TCE_v2_IOMMU,
      83                 :            :                 .name = "sPAPR",
      84                 :            :                 .partial_unmap = true,
      85                 :            :                 .dma_map_func = &vfio_spapr_dma_map,
      86                 :            :                 .dma_user_map_func = &vfio_spapr_dma_mem_map
      87                 :            :         },
      88                 :            :         /* IOMMU-less mode */
      89                 :            :         {
      90                 :            :                 .type_id = VFIO_NOIOMMU_IOMMU,
      91                 :            :                 .name = "No-IOMMU",
      92                 :            :                 .partial_unmap = true,
      93                 :            :                 .dma_map_func = &vfio_noiommu_dma_map,
      94                 :            :                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
      95                 :            :         },
      96                 :            : };
      97                 :            : 
      98                 :            : static int
      99                 :            : is_null_map(const struct user_mem_map *map)
     100                 :            : {
     101   [ #  #  #  #  :          0 :         return map->addr == 0 && map->iova == 0 &&
          #  #  #  #  #  
                      # ]
     102   [ #  #  #  #  :          0 :                         map->len == 0 && map->chunk == 0;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     103                 :            : }
     104                 :            : 
     105                 :            : /* we may need to merge user mem maps together in case of user mapping/unmapping
     106                 :            :  * chunks of memory, so we'll need a comparator function to sort segments.
     107                 :            :  */
     108                 :            : static int
     109         [ #  # ]:          0 : user_mem_map_cmp(const void *a, const void *b)
     110                 :            : {
     111                 :            :         const struct user_mem_map *umm_a = a;
     112                 :            :         const struct user_mem_map *umm_b = b;
     113                 :            : 
     114                 :            :         /* move null entries to end */
     115                 :            :         if (is_null_map(umm_a))
     116                 :            :                 return 1;
     117                 :            :         if (is_null_map(umm_b))
     118                 :            :                 return -1;
     119                 :            : 
     120                 :            :         /* sort by iova first */
     121         [ #  # ]:          0 :         if (umm_a->iova < umm_b->iova)
     122                 :            :                 return -1;
     123         [ #  # ]:          0 :         if (umm_a->iova > umm_b->iova)
     124                 :            :                 return 1;
     125                 :            : 
     126         [ #  # ]:          0 :         if (umm_a->addr < umm_b->addr)
     127                 :            :                 return -1;
     128         [ #  # ]:          0 :         if (umm_a->addr > umm_b->addr)
     129                 :            :                 return 1;
     130                 :            : 
     131         [ #  # ]:          0 :         if (umm_a->len < umm_b->len)
     132                 :            :                 return -1;
     133         [ #  # ]:          0 :         if (umm_a->len > umm_b->len)
     134                 :            :                 return 1;
     135                 :            : 
     136         [ #  # ]:          0 :         if (umm_a->chunk < umm_b->chunk)
     137                 :            :                 return -1;
     138         [ #  # ]:          0 :         if (umm_a->chunk > umm_b->chunk)
     139                 :          0 :                 return 1;
     140                 :            : 
     141                 :            :         return 0;
     142                 :            : }
     143                 :            : 
     144                 :            : /*
     145                 :            :  * Take in an address range and list of current mappings, and produce a list of
     146                 :            :  * mappings that will be kept.
     147                 :            :  */
     148                 :            : static int
     149                 :          0 : process_maps(struct user_mem_map *src, size_t src_len,
     150                 :            :                 struct user_mem_map newmap[2], uint64_t vaddr, uint64_t len)
     151                 :            : {
     152                 :            :         struct user_mem_map *src_first = &src[0];
     153                 :          0 :         struct user_mem_map *src_last = &src[src_len - 1];
     154                 :            :         struct user_mem_map *dst_first = &newmap[0];
     155                 :            :         /* we can get at most two new segments */
     156                 :          0 :         struct user_mem_map *dst_last = &newmap[1];
     157                 :          0 :         uint64_t first_off = vaddr - src_first->addr;
     158                 :          0 :         uint64_t last_off = (src_last->addr + src_last->len) - (vaddr + len);
     159                 :            :         int newmap_len = 0;
     160                 :            : 
     161         [ #  # ]:          0 :         if (first_off != 0) {
     162                 :          0 :                 dst_first->addr = src_first->addr;
     163                 :          0 :                 dst_first->iova = src_first->iova;
     164                 :          0 :                 dst_first->len = first_off;
     165                 :          0 :                 dst_first->chunk = src_first->chunk;
     166                 :            : 
     167                 :            :                 newmap_len++;
     168                 :            :         }
     169         [ #  # ]:          0 :         if (last_off != 0) {
     170                 :            :                 /* if we had start offset, we have two segments */
     171                 :            :                 struct user_mem_map *last =
     172         [ #  # ]:          0 :                                 first_off == 0 ? dst_first : dst_last;
     173                 :          0 :                 last->addr = (src_last->addr + src_last->len) - last_off;
     174                 :          0 :                 last->iova = (src_last->iova + src_last->len) - last_off;
     175                 :          0 :                 last->len = last_off;
     176                 :          0 :                 last->chunk = src_last->chunk;
     177                 :            : 
     178                 :          0 :                 newmap_len++;
     179                 :            :         }
     180                 :          0 :         return newmap_len;
     181                 :            : }
     182                 :            : 
     183                 :            : /* erase certain maps from the list */
     184                 :            : static void
     185                 :          0 : delete_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *del_maps,
     186                 :            :                 size_t n_del)
     187                 :            : {
     188                 :            :         unsigned int i;
     189                 :            :         size_t j;
     190                 :            : 
     191         [ #  # ]:          0 :         for (i = 0, j = 0; i < RTE_DIM(user_mem_maps->maps) && j < n_del; i++) {
     192                 :          0 :                 struct user_mem_map *left = &user_mem_maps->maps[i];
     193                 :          0 :                 struct user_mem_map *right = &del_maps[j];
     194                 :            : 
     195         [ #  # ]:          0 :                 if (user_mem_map_cmp(left, right) == 0) {
     196                 :            :                         memset(left, 0, sizeof(*left));
     197                 :          0 :                         j++;
     198                 :          0 :                         user_mem_maps->n_maps--;
     199                 :            :                 }
     200                 :            :         }
     201                 :          0 : }
     202                 :            : 
     203                 :            : static void
     204                 :          0 : copy_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *add_maps,
     205                 :            :                 size_t n_add)
     206                 :            : {
     207                 :            :         unsigned int i;
     208                 :            :         size_t j;
     209                 :            : 
     210         [ #  # ]:          0 :         for (i = 0, j = 0; i < RTE_DIM(user_mem_maps->maps) && j < n_add; i++) {
     211                 :          0 :                 struct user_mem_map *left = &user_mem_maps->maps[i];
     212         [ #  # ]:          0 :                 struct user_mem_map *right = &add_maps[j];
     213                 :            : 
     214                 :            :                 /* insert into empty space */
     215                 :            :                 if (is_null_map(left)) {
     216                 :            :                         memcpy(left, right, sizeof(*left));
     217                 :          0 :                         j++;
     218                 :          0 :                         user_mem_maps->n_maps++;
     219                 :            :                 }
     220                 :            :         }
     221                 :          0 : }
     222                 :            : 
     223                 :            : /* try merging two maps into one, return 1 if succeeded */
     224                 :            : static int
     225                 :          0 : merge_map(struct user_mem_map *left, struct user_mem_map *right)
     226                 :            : {
     227                 :            :         /* merge the same maps into one */
     228         [ #  # ]:          0 :         if (memcmp(left, right, sizeof(struct user_mem_map)) == 0)
     229                 :          0 :                 goto out;
     230                 :            : 
     231         [ #  # ]:          0 :         if (left->addr + left->len != right->addr)
     232                 :            :                 return 0;
     233         [ #  # ]:          0 :         if (left->iova + left->len != right->iova)
     234                 :            :                 return 0;
     235         [ #  # ]:          0 :         if (left->chunk != right->chunk)
     236                 :            :                 return 0;
     237                 :          0 :         left->len += right->len;
     238                 :            : 
     239                 :          0 : out:
     240                 :            :         memset(right, 0, sizeof(*right));
     241                 :            : 
     242                 :          0 :         return 1;
     243                 :            : }
     244                 :            : 
     245                 :            : static bool
     246                 :          0 : addr_is_chunk_aligned(struct user_mem_map *maps, size_t n_maps,
     247                 :            :                 uint64_t vaddr, uint64_t iova)
     248                 :            : {
     249                 :            :         unsigned int i;
     250                 :            : 
     251         [ #  # ]:          0 :         for (i = 0; i < n_maps; i++) {
     252                 :          0 :                 struct user_mem_map *map = &maps[i];
     253                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     254                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     255                 :          0 :                 uint64_t map_va_off = vaddr - map->addr;
     256                 :          0 :                 uint64_t map_iova_off = iova - map->iova;
     257                 :            : 
     258                 :            :                 /* we include end of the segment in comparison as well */
     259   [ #  #  #  # ]:          0 :                 bool addr_in_map = (vaddr >= map->addr) && (vaddr <= map_va_end);
     260   [ #  #  #  # ]:          0 :                 bool iova_in_map = (iova >= map->iova) && (iova <= map_iova_end);
     261                 :            :                 /* chunk may not be power of two, so use modulo */
     262                 :          0 :                 bool addr_is_aligned = (map_va_off % map->chunk) == 0;
     263                 :          0 :                 bool iova_is_aligned = (map_iova_off % map->chunk) == 0;
     264                 :            : 
     265         [ #  # ]:          0 :                 if (addr_in_map && iova_in_map &&
     266         [ #  # ]:          0 :                                 addr_is_aligned && iova_is_aligned)
     267                 :            :                         return true;
     268                 :            :         }
     269                 :            :         return false;
     270                 :            : }
     271                 :            : 
     272                 :            : static int
     273                 :          0 : find_user_mem_maps(struct user_mem_maps *user_mem_maps, uint64_t addr,
     274                 :            :                 uint64_t iova, uint64_t len, struct user_mem_map *dst,
     275                 :            :                 size_t dst_len)
     276                 :            : {
     277                 :          0 :         uint64_t va_end = addr + len;
     278                 :          0 :         uint64_t iova_end = iova + len;
     279                 :            :         bool found = false;
     280                 :            :         size_t j;
     281                 :            :         int i, ret;
     282                 :            : 
     283         [ #  # ]:          0 :         for (i = 0, j = 0; i < user_mem_maps->n_maps; i++) {
     284                 :          0 :                 struct user_mem_map *map = &user_mem_maps->maps[i];
     285                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     286                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     287                 :            : 
     288   [ #  #  #  # ]:          0 :                 bool start_addr_in_map = (addr >= map->addr) &&
     289                 :            :                                 (addr < map_va_end);
     290   [ #  #  #  # ]:          0 :                 bool end_addr_in_map = (va_end > map->addr) &&
     291                 :            :                                 (va_end <= map_va_end);
     292   [ #  #  #  # ]:          0 :                 bool start_iova_in_map = (iova >= map->iova) &&
     293                 :            :                                 (iova < map_iova_end);
     294   [ #  #  #  # ]:          0 :                 bool end_iova_in_map = (iova_end > map->iova) &&
     295                 :            :                                 (iova_end <= map_iova_end);
     296                 :            : 
     297                 :            :                 /* do we have space in temporary map? */
     298         [ #  # ]:          0 :                 if (j == dst_len) {
     299                 :            :                         ret = -ENOSPC;
     300                 :          0 :                         goto err;
     301                 :            :                 }
     302                 :            :                 /* check if current map is start of our segment */
     303   [ #  #  #  # ]:          0 :                 if (!found && start_addr_in_map && start_iova_in_map)
     304                 :            :                         found = true;
     305                 :            :                 /* if we have previously found a segment, add it to the map */
     306         [ #  # ]:          0 :                 if (found) {
     307                 :            :                         /* copy the segment into our temporary map */
     308         [ #  # ]:          0 :                         memcpy(&dst[j++], map, sizeof(*map));
     309                 :            : 
     310                 :            :                         /* if we match end of segment, quit */
     311         [ #  # ]:          0 :                         if (end_addr_in_map && end_iova_in_map)
     312                 :          0 :                                 return j;
     313                 :            :                 }
     314                 :            :         }
     315                 :            :         /* we didn't find anything */
     316                 :            :         ret = -ENOENT;
     317                 :          0 : err:
     318                 :          0 :         memset(dst, 0, sizeof(*dst) * dst_len);
     319                 :          0 :         return ret;
     320                 :            : }
     321                 :            : 
     322                 :            : /* this will sort all user maps, and merge/compact any adjacent maps */
     323                 :            : static void
     324                 :          0 : compact_user_maps(struct user_mem_maps *user_mem_maps)
     325                 :            : {
     326                 :            :         unsigned int i;
     327                 :            : 
     328                 :          0 :         qsort(user_mem_maps->maps, RTE_DIM(user_mem_maps->maps),
     329                 :            :                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
     330                 :            : 
     331                 :            :         /* we'll go over the list backwards when merging */
     332         [ #  # ]:          0 :         for (i = RTE_DIM(user_mem_maps->maps) - 2; i != 0; i--) {
     333                 :            :                 struct user_mem_map *l, *r;
     334                 :            : 
     335                 :          0 :                 l = &user_mem_maps->maps[i];
     336         [ #  # ]:          0 :                 r = &user_mem_maps->maps[i + 1];
     337                 :            : 
     338                 :          0 :                 if (is_null_map(l) || is_null_map(r))
     339                 :          0 :                         continue;
     340                 :            : 
     341                 :            :                 /* try and merge the maps */
     342         [ #  # ]:          0 :                 if (merge_map(l, r))
     343                 :          0 :                         user_mem_maps->n_maps--;
     344                 :            :         }
     345                 :            : 
     346                 :            :         /* the entries are still sorted, but now they have holes in them, so
     347                 :            :          * sort the list again.
     348                 :            :          */
     349                 :          0 :         qsort(user_mem_maps->maps, RTE_DIM(user_mem_maps->maps),
     350                 :            :                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
     351                 :          0 : }
     352                 :            : 
     353                 :            : static int
     354                 :          0 : vfio_open_group_fd(int iommu_group_num, bool mp_request)
     355                 :            : {
     356                 :            :         int vfio_group_fd;
     357                 :            :         char filename[PATH_MAX];
     358                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     359                 :          0 :         struct rte_mp_reply mp_reply = {0};
     360                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     361                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     362                 :            : 
     363                 :            :         /* if not requesting via mp, open the group locally */
     364         [ #  # ]:          0 :         if (!mp_request) {
     365                 :            :                 /* try regular group format */
     366                 :            :                 snprintf(filename, sizeof(filename), RTE_VFIO_GROUP_FMT, iommu_group_num);
     367                 :            :                 vfio_group_fd = open(filename, O_RDWR);
     368         [ #  # ]:          0 :                 if (vfio_group_fd < 0) {
     369                 :            :                         /* if file not found, it's not an error */
     370         [ #  # ]:          0 :                         if (errno != ENOENT) {
     371                 :          0 :                                 EAL_LOG(ERR, "Cannot open %s: %s",
     372                 :            :                                                 filename, strerror(errno));
     373                 :          0 :                                 return -1;
     374                 :            :                         }
     375                 :            : 
     376                 :            :                         /* special case: try no-IOMMU path as well */
     377                 :            :                         snprintf(filename, sizeof(filename), RTE_VFIO_NOIOMMU_GROUP_FMT,
     378                 :            :                                 iommu_group_num);
     379                 :            :                         vfio_group_fd = open(filename, O_RDWR);
     380         [ #  # ]:          0 :                         if (vfio_group_fd < 0) {
     381         [ #  # ]:          0 :                                 if (errno != ENOENT) {
     382                 :          0 :                                         EAL_LOG(ERR,
     383                 :            :                                                 "Cannot open %s: %s",
     384                 :            :                                                 filename, strerror(errno));
     385                 :          0 :                                         return -1;
     386                 :            :                                 }
     387                 :            :                                 return -ENOENT;
     388                 :            :                         }
     389                 :            :                         /* noiommu group found */
     390                 :            :                 }
     391                 :            : 
     392                 :          0 :                 return vfio_group_fd;
     393                 :            :         }
     394                 :            :         /* if we're in a secondary process, request group fd from the primary
     395                 :            :          * process via mp channel.
     396                 :            :          */
     397                 :          0 :         p->req = SOCKET_REQ_GROUP;
     398                 :          0 :         p->group_num = iommu_group_num;
     399                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
     400                 :          0 :         mp_req.len_param = sizeof(*p);
     401                 :          0 :         mp_req.num_fds = 0;
     402                 :            : 
     403                 :            :         vfio_group_fd = -1;
     404         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
     405         [ #  # ]:          0 :             mp_reply.nb_received == 1) {
     406                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     407                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     408   [ #  #  #  # ]:          0 :                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
     409                 :          0 :                         vfio_group_fd = mp_rep->fds[0];
     410         [ #  # ]:          0 :                 } else if (p->result == SOCKET_NO_FD) {
     411                 :          0 :                         EAL_LOG(ERR, "Bad VFIO group fd");
     412                 :            :                         vfio_group_fd = -ENOENT;
     413                 :            :                 }
     414                 :            :         }
     415                 :            : 
     416                 :          0 :         free(mp_reply.msgs);
     417         [ #  # ]:          0 :         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
     418                 :          0 :                 EAL_LOG(ERR, "Cannot request VFIO group fd");
     419                 :            :         return vfio_group_fd;
     420                 :            : }
     421                 :            : 
     422                 :            : static struct vfio_config *
     423                 :            : get_vfio_cfg_by_group_num(int iommu_group_num)
     424                 :            : {
     425                 :            :         struct vfio_config *vfio_cfg;
     426                 :            :         unsigned int i, j;
     427                 :            : 
     428   [ #  #  #  #  :          0 :         for (i = 0; i < RTE_DIM(vfio_cfgs); i++) {
                   #  # ]
     429                 :          0 :                 vfio_cfg = &vfio_cfgs[i];
     430   [ #  #  #  #  :          0 :                 for (j = 0; j < RTE_DIM(vfio_cfg->vfio_groups); j++) {
                   #  # ]
     431   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].group_num ==
                   #  # ]
     432                 :            :                                         iommu_group_num)
     433                 :            :                                 return vfio_cfg;
     434                 :            :                 }
     435                 :            :         }
     436                 :            : 
     437                 :            :         return NULL;
     438                 :            : }
     439                 :            : 
     440                 :            : static int
     441                 :          0 : vfio_get_group_fd(struct vfio_config *vfio_cfg,
     442                 :            :                 int iommu_group_num)
     443                 :            : {
     444                 :            :         struct vfio_group *cur_grp = NULL;
     445                 :            :         int vfio_group_fd;
     446                 :            :         unsigned int i;
     447                 :            : 
     448                 :            :         /* check if we already have the group descriptor open */
     449         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(vfio_cfg->vfio_groups); i++)
     450         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
     451                 :          0 :                         return vfio_cfg->vfio_groups[i].fd;
     452                 :            : 
     453                 :            :         /* Lets see first if there is room for a new group */
     454         [ #  # ]:          0 :         if (vfio_cfg->vfio_active_groups == RTE_DIM(vfio_cfg->vfio_groups)) {
     455                 :          0 :                 EAL_LOG(ERR, "Maximum number of VFIO groups reached!");
     456                 :          0 :                 return -1;
     457                 :            :         }
     458                 :            : 
     459                 :            :         /* Now lets get an index for the new group */
     460         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(vfio_cfg->vfio_groups); i++)
     461         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
     462                 :          0 :                         cur_grp = &vfio_cfg->vfio_groups[i];
     463                 :          0 :                         break;
     464                 :            :                 }
     465                 :            : 
     466                 :            :         /* This should not happen */
     467         [ #  # ]:          0 :         if (cur_grp == NULL) {
     468                 :          0 :                 EAL_LOG(ERR, "No VFIO group free slot found");
     469                 :          0 :                 return -1;
     470                 :            :         }
     471                 :            : 
     472                 :            :         /*
     473                 :            :          * When opening a group fd, we need to decide whether to open it locally
     474                 :            :          * or request it from the primary process via mp_sync.
     475                 :            :          *
     476                 :            :          * For the default container, secondary processes use mp_sync so that
     477                 :            :          * the primary process tracks the group fd and maintains VFIO state
     478                 :            :          * across all processes.
     479                 :            :          *
     480                 :            :          * For custom containers, we open the group fd locally in each process
     481                 :            :          * since custom containers are process-local and the primary has no
     482                 :            :          * knowledge of them. Requesting a group fd from the primary for a
     483                 :            :          * container it doesn't know about would be incorrect.
     484                 :            :          */
     485                 :          0 :         const struct internal_config *internal_conf = eal_get_internal_configuration();
     486         [ #  # ]:          0 :         bool mp_request = (internal_conf->process_type == RTE_PROC_SECONDARY) &&
     487         [ #  # ]:          0 :                         (vfio_cfg == default_vfio_cfg);
     488                 :            : 
     489                 :          0 :         vfio_group_fd = vfio_open_group_fd(iommu_group_num, mp_request);
     490         [ #  # ]:          0 :         if (vfio_group_fd < 0) {
     491                 :          0 :                 EAL_LOG(ERR, "Failed to open VFIO group %d",
     492                 :            :                         iommu_group_num);
     493                 :          0 :                 return vfio_group_fd;
     494                 :            :         }
     495                 :            : 
     496                 :          0 :         cur_grp->group_num = iommu_group_num;
     497                 :          0 :         cur_grp->fd = vfio_group_fd;
     498                 :          0 :         vfio_cfg->vfio_active_groups++;
     499                 :            : 
     500                 :          0 :         return vfio_group_fd;
     501                 :            : }
     502                 :            : 
     503                 :            : static struct vfio_config *
     504                 :            : get_vfio_cfg_by_group_fd(int vfio_group_fd)
     505                 :            : {
     506                 :            :         struct vfio_config *vfio_cfg;
     507                 :            :         unsigned int i, j;
     508                 :            : 
     509   [ #  #  #  #  :          0 :         for (i = 0; i < RTE_DIM(vfio_cfgs); i++) {
             #  #  #  # ]
     510                 :          0 :                 vfio_cfg = &vfio_cfgs[i];
     511   [ #  #  #  #  :          0 :                 for (j = 0; j < RTE_DIM(vfio_cfg->vfio_groups); j++)
             #  #  #  # ]
     512   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
             #  #  #  # ]
     513                 :            :                                 return vfio_cfg;
     514                 :            :         }
     515                 :            : 
     516                 :            :         return NULL;
     517                 :            : }
     518                 :            : 
     519                 :            : static struct vfio_config *
     520                 :            : get_vfio_cfg_by_container_fd(int container_fd)
     521                 :            : {
     522                 :            :         unsigned int i;
     523                 :            : 
     524   [ #  #  #  # ]:          0 :         if (container_fd == RTE_VFIO_DEFAULT_CONTAINER_FD)
     525                 :          0 :                 return default_vfio_cfg;
     526                 :            : 
     527   [ #  #  #  #  :          0 :         for (i = 0; i < RTE_DIM(vfio_cfgs); i++) {
          #  #  #  #  #  
                      # ]
     528   [ #  #  #  #  :          0 :                 if (vfio_cfgs[i].vfio_container_fd == container_fd)
          #  #  #  #  #  
                      # ]
     529                 :          0 :                         return &vfio_cfgs[i];
     530                 :            :         }
     531                 :            : 
     532                 :            :         return NULL;
     533                 :            : }
     534                 :            : 
     535                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_group_fd)
     536                 :            : int
     537                 :          0 : rte_vfio_get_group_fd(int iommu_group_num)
     538                 :            : {
     539                 :            :         struct vfio_config *vfio_cfg;
     540                 :            : 
     541                 :            :         /* get the vfio_config it belongs to */
     542                 :            :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
     543         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
     544                 :            : 
     545                 :          0 :         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
     546                 :            : }
     547                 :            : 
     548                 :            : static int
     549                 :            : get_vfio_group_idx(int vfio_group_fd)
     550                 :            : {
     551                 :            :         struct vfio_config *vfio_cfg;
     552                 :            :         unsigned int i, j;
     553                 :            : 
     554   [ #  #  #  #  :          0 :         for (i = 0; i < RTE_DIM(vfio_cfgs); i++) {
             #  #  #  # ]
     555                 :            :                 vfio_cfg = &vfio_cfgs[i];
     556   [ #  #  #  #  :          0 :                 for (j = 0; j < RTE_DIM(vfio_cfg->vfio_groups); j++)
             #  #  #  # ]
     557   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
             #  #  #  # ]
     558                 :          0 :                                 return j;
     559                 :            :         }
     560                 :            : 
     561                 :            :         return -1;
     562                 :            : }
     563                 :            : 
     564                 :            : static void
     565                 :          0 : vfio_group_device_get(int vfio_group_fd)
     566                 :            : {
     567                 :            :         struct vfio_config *vfio_cfg;
     568                 :            :         int i;
     569                 :            : 
     570                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     571         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     572                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     573                 :          0 :                 return;
     574                 :            :         }
     575                 :            : 
     576                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     577         [ #  # ]:          0 :         if (i < 0)
     578                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     579                 :            :         else
     580                 :          0 :                 vfio_cfg->vfio_groups[i].devices++;
     581                 :            : }
     582                 :            : 
     583                 :            : static void
     584                 :          0 : vfio_group_device_put(int vfio_group_fd)
     585                 :            : {
     586                 :            :         struct vfio_config *vfio_cfg;
     587                 :            :         int i;
     588                 :            : 
     589                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     590         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     591                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     592                 :          0 :                 return;
     593                 :            :         }
     594                 :            : 
     595                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     596         [ #  # ]:          0 :         if (i < 0)
     597                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     598                 :            :         else
     599                 :          0 :                 vfio_cfg->vfio_groups[i].devices--;
     600                 :            : }
     601                 :            : 
     602                 :            : static int
     603                 :          0 : vfio_group_device_count(int vfio_group_fd)
     604                 :            : {
     605                 :            :         struct vfio_config *vfio_cfg;
     606                 :            :         int i;
     607                 :            : 
     608                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     609         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     610                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     611                 :          0 :                 return -1;
     612                 :            :         }
     613                 :            : 
     614                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     615         [ #  # ]:          0 :         if (i < 0) {
     616                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     617                 :          0 :                 return -1;
     618                 :            :         }
     619                 :            : 
     620                 :          0 :         return vfio_cfg->vfio_groups[i].devices;
     621                 :            : }
     622                 :            : 
     623                 :            : static void
     624                 :          0 : vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
     625                 :            :                 void *arg __rte_unused)
     626                 :            : {
     627                 :            :         struct rte_memseg_list *msl;
     628                 :            :         struct rte_memseg *ms;
     629                 :            :         size_t cur_len = 0;
     630                 :            : 
     631                 :          0 :         msl = rte_mem_virt2memseg_list(addr);
     632                 :            : 
     633                 :            :         /* for IOVA as VA mode, no need to care for IOVA addresses */
     634   [ #  #  #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
     635                 :          0 :                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
     636                 :          0 :                 uint64_t page_sz = msl->page_sz;
     637                 :            : 
     638                 :            :                 /* Maintain granularity of DMA map/unmap to memseg size */
     639         [ #  # ]:          0 :                 for (; cur_len < len; cur_len += page_sz) {
     640         [ #  # ]:          0 :                         if (type == RTE_MEM_EVENT_ALLOC)
     641                 :          0 :                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
     642                 :            :                                                  vfio_va, page_sz, 1);
     643                 :            :                         else
     644                 :          0 :                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
     645                 :            :                                                  vfio_va, page_sz, 0);
     646                 :          0 :                         vfio_va += page_sz;
     647                 :            :                 }
     648                 :            : 
     649                 :            :                 return;
     650                 :            :         }
     651                 :            : 
     652                 :            :         /* memsegs are contiguous in memory */
     653                 :          0 :         ms = rte_mem_virt2memseg(addr, msl);
     654         [ #  # ]:          0 :         while (cur_len < len) {
     655                 :            :                 /* some memory segments may have invalid IOVA */
     656         [ #  # ]:          0 :                 if (ms->iova == RTE_BAD_IOVA) {
     657                 :          0 :                         EAL_LOG(DEBUG,
     658                 :            :                                 "Memory segment at %p has bad IOVA, skipping",
     659                 :            :                                 ms->addr);
     660                 :          0 :                         goto next;
     661                 :            :                 }
     662         [ #  # ]:          0 :                 if (type == RTE_MEM_EVENT_ALLOC)
     663                 :          0 :                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
     664                 :            :                                         ms->iova, ms->len, 1);
     665                 :            :                 else
     666                 :          0 :                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
     667                 :            :                                         ms->iova, ms->len, 0);
     668                 :          0 : next:
     669                 :          0 :                 cur_len += ms->len;
     670                 :          0 :                 ++ms;
     671                 :            :         }
     672                 :            : }
     673                 :            : 
     674                 :            : static int
     675                 :          0 : vfio_sync_default_container(void)
     676                 :            : {
     677                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     678                 :          0 :         struct rte_mp_reply mp_reply = {0};
     679                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     680                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     681                 :            :         int iommu_type_id;
     682                 :            :         unsigned int i;
     683                 :            : 
     684                 :            :         /* cannot be called from primary */
     685         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_SECONDARY)
     686                 :            :                 return -1;
     687                 :            : 
     688                 :            :         /* default container fd should have been opened in rte_vfio_enable() */
     689         [ #  # ]:          0 :         if (!default_vfio_cfg->vfio_enabled ||
     690         [ #  # ]:          0 :                         default_vfio_cfg->vfio_container_fd < 0) {
     691                 :          0 :                 EAL_LOG(ERR, "VFIO support is not initialized");
     692                 :          0 :                 return -1;
     693                 :            :         }
     694                 :            : 
     695                 :            :         /* find default container's IOMMU type */
     696                 :          0 :         p->req = SOCKET_REQ_IOMMU_TYPE;
     697                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
     698                 :          0 :         mp_req.len_param = sizeof(*p);
     699                 :          0 :         mp_req.num_fds = 0;
     700                 :            : 
     701                 :            :         iommu_type_id = -1;
     702         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
     703         [ #  # ]:          0 :                         mp_reply.nb_received == 1) {
     704                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     705                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     706         [ #  # ]:          0 :                 if (p->result == SOCKET_OK)
     707                 :          0 :                         iommu_type_id = p->iommu_type_id;
     708                 :            :         }
     709                 :          0 :         free(mp_reply.msgs);
     710         [ #  # ]:          0 :         if (iommu_type_id < 0) {
     711                 :          0 :                 EAL_LOG(ERR,
     712                 :            :                         "Could not get IOMMU type for default container");
     713                 :          0 :                 return -1;
     714                 :            :         }
     715                 :            : 
     716                 :            :         /* we now have an fd for default container, as well as its IOMMU type.
     717                 :            :          * now, set up default VFIO container config to match.
     718                 :            :          */
     719         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(iommu_types); i++) {
     720                 :          0 :                 const struct vfio_iommu_type *t = &iommu_types[i];
     721         [ #  # ]:          0 :                 if (t->type_id != iommu_type_id)
     722                 :            :                         continue;
     723                 :            : 
     724                 :            :                 /* we found our IOMMU type */
     725                 :          0 :                 default_vfio_cfg->vfio_iommu_type = t;
     726                 :            : 
     727                 :          0 :                 return 0;
     728                 :            :         }
     729                 :          0 :         EAL_LOG(ERR, "Could not find IOMMU type id (%i)",
     730                 :            :                         iommu_type_id);
     731                 :          0 :         return -1;
     732                 :            : }
     733                 :            : 
     734                 :            : RTE_EXPORT_SYMBOL(rte_vfio_clear_group)
     735                 :            : int
     736                 :          0 : rte_vfio_clear_group(int vfio_group_fd)
     737                 :            : {
     738                 :            :         int i;
     739                 :            :         struct vfio_config *vfio_cfg;
     740                 :            : 
     741                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     742         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     743                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     744                 :          0 :                 return -1;
     745                 :            :         }
     746                 :            : 
     747                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     748         [ #  # ]:          0 :         if (i < 0)
     749                 :            :                 return -1;
     750                 :          0 :         vfio_cfg->vfio_groups[i].group_num = -1;
     751                 :          0 :         vfio_cfg->vfio_groups[i].fd = -1;
     752                 :          0 :         vfio_cfg->vfio_groups[i].devices = 0;
     753                 :          0 :         vfio_cfg->vfio_active_groups--;
     754                 :            : 
     755                 :          0 :         return 0;
     756                 :            : }
     757                 :            : 
     758                 :            : RTE_EXPORT_SYMBOL(rte_vfio_setup_device)
     759                 :            : int
     760                 :          0 : rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
     761                 :            :                 int *vfio_dev_fd, struct vfio_device_info *device_info)
     762                 :            : {
     763                 :          0 :         struct vfio_group_status group_status = {
     764                 :            :                         .argsz = sizeof(group_status)
     765                 :            :         };
     766                 :            :         struct vfio_config *vfio_cfg;
     767                 :            :         struct user_mem_maps *user_mem_maps;
     768                 :            :         int vfio_container_fd;
     769                 :            :         int vfio_group_fd;
     770                 :            :         int iommu_group_num;
     771                 :            :         rte_uuid_t vf_token;
     772                 :            :         int i, ret;
     773                 :            :         const struct internal_config *internal_conf =
     774                 :          0 :                 eal_get_internal_configuration();
     775                 :            : 
     776                 :            :         /* get group number */
     777                 :          0 :         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
     778         [ #  # ]:          0 :         if (ret == 0) {
     779                 :          0 :                 EAL_LOG(NOTICE,
     780                 :            :                                 "%s not managed by VFIO driver, skipping",
     781                 :            :                                 dev_addr);
     782                 :          0 :                 return 1;
     783                 :            :         }
     784                 :            : 
     785                 :            :         /* if negative, something failed */
     786         [ #  # ]:          0 :         if (ret < 0)
     787                 :            :                 return -1;
     788                 :            : 
     789                 :            :         /* get the actual group fd */
     790                 :          0 :         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
     791         [ #  # ]:          0 :         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
     792                 :            :                 return -1;
     793                 :            : 
     794                 :            :         /*
     795                 :            :          * if vfio_group_fd == -ENOENT, that means the device
     796                 :            :          * isn't managed by VFIO
     797                 :            :          */
     798         [ #  # ]:          0 :         if (vfio_group_fd == -ENOENT) {
     799                 :          0 :                 EAL_LOG(NOTICE,
     800                 :            :                                 "%s not managed by VFIO driver, skipping",
     801                 :            :                                 dev_addr);
     802                 :          0 :                 return 1;
     803                 :            :         }
     804                 :            : 
     805                 :            :         /*
     806                 :            :          * at this point, we know that this group is viable (meaning, all devices
     807                 :            :          * are either bound to VFIO or not bound to anything)
     808                 :            :          */
     809                 :            : 
     810                 :            :         /* check if the group is viable */
     811                 :          0 :         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
     812         [ #  # ]:          0 :         if (ret) {
     813                 :          0 :                 EAL_LOG(ERR, "%s cannot get VFIO group status, "
     814                 :            :                         "error %i (%s)", dev_addr, errno, strerror(errno));
     815                 :          0 :                 close(vfio_group_fd);
     816                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     817                 :          0 :                 return -1;
     818         [ #  # ]:          0 :         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
     819                 :          0 :                 EAL_LOG(ERR, "%s VFIO group is not viable! "
     820                 :            :                         "Not all devices in IOMMU group bound to VFIO or unbound",
     821                 :            :                         dev_addr);
     822                 :          0 :                 close(vfio_group_fd);
     823                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     824                 :          0 :                 return -1;
     825                 :            :         }
     826                 :            : 
     827                 :            :         /* get the vfio_config it belongs to */
     828                 :          0 :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
     829         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
     830                 :          0 :         vfio_container_fd = vfio_cfg->vfio_container_fd;
     831                 :            :         user_mem_maps = &vfio_cfg->mem_maps;
     832                 :            : 
     833                 :            :         /* check if group does not have a container yet */
     834         [ #  # ]:          0 :         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
     835                 :            : 
     836                 :            :                 /* add group to a container */
     837                 :          0 :                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
     838                 :            :                                 &vfio_container_fd);
     839         [ #  # ]:          0 :                 if (ret) {
     840                 :          0 :                         EAL_LOG(ERR,
     841                 :            :                                 "%s cannot add VFIO group to container, error "
     842                 :            :                                 "%i (%s)", dev_addr, errno, strerror(errno));
     843                 :          0 :                         close(vfio_group_fd);
     844                 :          0 :                         rte_vfio_clear_group(vfio_group_fd);
     845                 :          0 :                         return -1;
     846                 :            :                 }
     847                 :            : 
     848                 :            :                 /*
     849                 :            :                  * pick an IOMMU type and set up DMA mappings for container
     850                 :            :                  *
     851                 :            :                  * needs to be done only once, only when first group is
     852                 :            :                  * assigned to a container and only in primary process.
     853                 :            :                  * Note this can happen several times with the hotplug
     854                 :            :                  * functionality.
     855                 :            :                  */
     856         [ #  # ]:          0 :                 if (internal_conf->process_type == RTE_PROC_PRIMARY &&
     857   [ #  #  #  # ]:          0 :                                 vfio_cfg->vfio_active_groups == 1 &&
     858                 :          0 :                                 vfio_group_device_count(vfio_group_fd) == 0) {
     859                 :            :                         const struct vfio_iommu_type *t;
     860                 :            : 
     861                 :            :                         /* select an IOMMU type which we will be using */
     862                 :          0 :                         t = vfio_set_iommu_type(vfio_container_fd);
     863         [ #  # ]:          0 :                         if (!t) {
     864                 :          0 :                                 EAL_LOG(ERR,
     865                 :            :                                         "%s failed to select IOMMU type",
     866                 :            :                                         dev_addr);
     867                 :          0 :                                 close(vfio_group_fd);
     868                 :          0 :                                 rte_vfio_clear_group(vfio_group_fd);
     869                 :          0 :                                 return -1;
     870                 :            :                         }
     871                 :            :                         /* lock memory hotplug before mapping and release it
     872                 :            :                          * after registering callback, to prevent races
     873                 :            :                          */
     874                 :          0 :                         rte_mcfg_mem_read_lock();
     875         [ #  # ]:          0 :                         if (vfio_cfg == default_vfio_cfg)
     876                 :          0 :                                 ret = t->dma_map_func(vfio_container_fd);
     877                 :            :                         else
     878                 :            :                                 ret = 0;
     879         [ #  # ]:          0 :                         if (ret) {
     880                 :          0 :                                 EAL_LOG(ERR,
     881                 :            :                                         "%s DMA remapping failed, error "
     882                 :            :                                         "%i (%s)",
     883                 :            :                                         dev_addr, errno, strerror(errno));
     884                 :          0 :                                 close(vfio_group_fd);
     885                 :          0 :                                 rte_vfio_clear_group(vfio_group_fd);
     886                 :          0 :                                 rte_mcfg_mem_read_unlock();
     887                 :          0 :                                 return -1;
     888                 :            :                         }
     889                 :            : 
     890                 :          0 :                         vfio_cfg->vfio_iommu_type = t;
     891                 :            : 
     892                 :            :                         /* re-map all user-mapped segments */
     893                 :          0 :                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
     894                 :            : 
     895                 :            :                         /* this IOMMU type may not support DMA mapping, but
     896                 :            :                          * if we have mappings in the list - that means we have
     897                 :            :                          * previously mapped something successfully, so we can
     898                 :            :                          * be sure that DMA mapping is supported.
     899                 :            :                          */
     900         [ #  # ]:          0 :                         for (i = 0; i < user_mem_maps->n_maps; i++) {
     901                 :            :                                 struct user_mem_map *map;
     902                 :            :                                 map = &user_mem_maps->maps[i];
     903                 :            : 
     904                 :          0 :                                 ret = t->dma_user_map_func(
     905                 :            :                                                 vfio_container_fd,
     906                 :            :                                                 map->addr, map->iova, map->len,
     907                 :            :                                                 1);
     908         [ #  # ]:          0 :                                 if (ret) {
     909                 :          0 :                                         EAL_LOG(ERR, "Couldn't map user memory for DMA: "
     910                 :            :                                                         "va: 0x%" PRIx64 " "
     911                 :            :                                                         "iova: 0x%" PRIx64 " "
     912                 :            :                                                         "len: 0x%" PRIu64,
     913                 :            :                                                         map->addr, map->iova,
     914                 :            :                                                         map->len);
     915                 :            :                                         rte_spinlock_recursive_unlock(
     916                 :            :                                                         &user_mem_maps->lock);
     917                 :          0 :                                         rte_mcfg_mem_read_unlock();
     918                 :          0 :                                         return -1;
     919                 :            :                                 }
     920                 :            :                         }
     921                 :            :                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
     922                 :            : 
     923                 :            :                         /* register callback for mem events */
     924         [ #  # ]:          0 :                         if (vfio_cfg == default_vfio_cfg)
     925                 :          0 :                                 ret = rte_mem_event_callback_register(
     926                 :            :                                         VFIO_MEM_EVENT_CLB_NAME,
     927                 :            :                                         vfio_mem_event_callback, NULL);
     928                 :            :                         else
     929                 :            :                                 ret = 0;
     930                 :            :                         /* unlock memory hotplug */
     931                 :          0 :                         rte_mcfg_mem_read_unlock();
     932                 :            : 
     933   [ #  #  #  # ]:          0 :                         if (ret && rte_errno != ENOTSUP) {
     934                 :          0 :                                 EAL_LOG(ERR, "Could not install memory event callback for VFIO");
     935                 :          0 :                                 return -1;
     936                 :            :                         }
     937         [ #  # ]:          0 :                         if (ret)
     938                 :          0 :                                 EAL_LOG(DEBUG, "Memory event callbacks not supported");
     939                 :            :                         else
     940                 :          0 :                                 EAL_LOG(DEBUG, "Installed memory event callback for VFIO");
     941                 :            :                 }
     942         [ #  # ]:          0 :         } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
     943         [ #  # ]:          0 :                         vfio_cfg == default_vfio_cfg &&
     944         [ #  # ]:          0 :                         vfio_cfg->vfio_iommu_type == NULL) {
     945                 :            :                 /* if we're not a primary process, we do not set up the VFIO
     946                 :            :                  * container because it's already been set up by the primary
     947                 :            :                  * process. instead, we simply ask the primary about VFIO type
     948                 :            :                  * we are using, and set the VFIO config up appropriately.
     949                 :            :                  */
     950                 :          0 :                 ret = vfio_sync_default_container();
     951         [ #  # ]:          0 :                 if (ret < 0) {
     952                 :          0 :                         EAL_LOG(ERR, "Could not sync default VFIO container");
     953                 :          0 :                         close(vfio_group_fd);
     954                 :          0 :                         rte_vfio_clear_group(vfio_group_fd);
     955                 :          0 :                         return -1;
     956                 :            :                 }
     957                 :            :                 /* we have successfully initialized VFIO, notify user */
     958                 :          0 :                 const struct vfio_iommu_type *t =
     959                 :          0 :                                 default_vfio_cfg->vfio_iommu_type;
     960                 :          0 :                 EAL_LOG(INFO, "Using IOMMU type %d (%s)",
     961                 :            :                                 t->type_id, t->name);
     962                 :            :         }
     963                 :            : 
     964                 :          0 :         rte_eal_vfio_get_vf_token(vf_token);
     965                 :            : 
     966                 :            :         /* get a file descriptor for the device with VF token firstly */
     967         [ #  # ]:          0 :         if (!rte_uuid_is_null(vf_token)) {
     968                 :            :                 char vf_token_str[RTE_UUID_STRLEN];
     969                 :            :                 char dev[PATH_MAX];
     970                 :            : 
     971                 :          0 :                 rte_uuid_unparse(vf_token, vf_token_str, sizeof(vf_token_str));
     972                 :            :                 snprintf(dev, sizeof(dev),
     973                 :            :                          "%s vf_token=%s", dev_addr, vf_token_str);
     974                 :            : 
     975                 :          0 :                 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD,
     976                 :            :                                      dev);
     977         [ #  # ]:          0 :                 if (*vfio_dev_fd >= 0)
     978                 :          0 :                         goto dev_get_info;
     979                 :            :         }
     980                 :            : 
     981                 :            :         /* get a file descriptor for the device */
     982                 :          0 :         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
     983         [ #  # ]:          0 :         if (*vfio_dev_fd < 0) {
     984                 :            :                 /* if we cannot get a device fd, this implies a problem with
     985                 :            :                  * the VFIO group or the container not having IOMMU configured.
     986                 :            :                  */
     987                 :            : 
     988                 :          0 :                 EAL_LOG(WARNING, "Getting a vfio_dev_fd for %s failed",
     989                 :            :                                 dev_addr);
     990                 :          0 :                 close(vfio_group_fd);
     991                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     992                 :          0 :                 return -1;
     993                 :            :         }
     994                 :            : 
     995                 :            :         /* test and setup the device */
     996                 :          0 : dev_get_info:
     997                 :          0 :         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
     998         [ #  # ]:          0 :         if (ret) {
     999                 :          0 :                 EAL_LOG(ERR, "%s cannot get device info, "
    1000                 :            :                                 "error %i (%s)", dev_addr, errno,
    1001                 :            :                                 strerror(errno));
    1002                 :          0 :                 close(*vfio_dev_fd);
    1003                 :          0 :                 close(vfio_group_fd);
    1004                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
    1005                 :          0 :                 return -1;
    1006                 :            :         }
    1007                 :          0 :         vfio_group_device_get(vfio_group_fd);
    1008                 :            : 
    1009                 :          0 :         return 0;
    1010                 :            : }
    1011                 :            : 
    1012                 :            : RTE_EXPORT_SYMBOL(rte_vfio_release_device)
    1013                 :            : int
    1014                 :          0 : rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
    1015                 :            :                     int vfio_dev_fd)
    1016                 :            : {
    1017                 :            :         struct vfio_config *vfio_cfg;
    1018                 :            :         int vfio_group_fd;
    1019                 :            :         int iommu_group_num;
    1020                 :            :         int ret;
    1021                 :            : 
    1022                 :            :         /* we don't want any DMA mapping messages to come while we're detaching
    1023                 :            :          * VFIO device, because this might be the last device and we might need
    1024                 :            :          * to unregister the callback.
    1025                 :            :          */
    1026                 :          0 :         rte_mcfg_mem_read_lock();
    1027                 :            : 
    1028                 :            :         /* get group number */
    1029                 :          0 :         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
    1030         [ #  # ]:          0 :         if (ret <= 0) {
    1031                 :          0 :                 EAL_LOG(WARNING, "%s not managed by VFIO driver",
    1032                 :            :                         dev_addr);
    1033                 :            :                 /* This is an error at this point. */
    1034                 :            :                 ret = -1;
    1035                 :          0 :                 goto out;
    1036                 :            :         }
    1037                 :            : 
    1038                 :            :         /* get the actual group fd */
    1039                 :          0 :         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
    1040         [ #  # ]:          0 :         if (vfio_group_fd < 0) {
    1041                 :          0 :                 EAL_LOG(INFO, "rte_vfio_get_group_fd failed for %s",
    1042                 :            :                                    dev_addr);
    1043                 :            :                 ret = vfio_group_fd;
    1044                 :          0 :                 goto out;
    1045                 :            :         }
    1046                 :            : 
    1047                 :            :         /* get the vfio_config it belongs to */
    1048                 :          0 :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
    1049         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
    1050                 :            : 
    1051                 :            :         /* At this point we got an active group. Closing it will make the
    1052                 :            :          * container detachment. If this is the last active group, VFIO kernel
    1053                 :            :          * code will unset the container and the IOMMU mappings.
    1054                 :            :          */
    1055                 :            : 
    1056                 :            :         /* Closing a device */
    1057         [ #  # ]:          0 :         if (close(vfio_dev_fd) < 0) {
    1058                 :          0 :                 EAL_LOG(INFO, "Error when closing vfio_dev_fd for %s",
    1059                 :            :                                    dev_addr);
    1060                 :            :                 ret = -1;
    1061                 :          0 :                 goto out;
    1062                 :            :         }
    1063                 :            : 
    1064                 :            :         /* An VFIO group can have several devices attached. Just when there is
    1065                 :            :          * no devices remaining should the group be closed.
    1066                 :            :          */
    1067                 :          0 :         vfio_group_device_put(vfio_group_fd);
    1068         [ #  # ]:          0 :         if (!vfio_group_device_count(vfio_group_fd)) {
    1069                 :            : 
    1070         [ #  # ]:          0 :                 if (close(vfio_group_fd) < 0) {
    1071                 :          0 :                         EAL_LOG(INFO, "Error when closing vfio_group_fd for %s",
    1072                 :            :                                 dev_addr);
    1073                 :            :                         ret = -1;
    1074                 :          0 :                         goto out;
    1075                 :            :                 }
    1076                 :            : 
    1077         [ #  # ]:          0 :                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
    1078                 :          0 :                         EAL_LOG(INFO, "Error when clearing group for %s",
    1079                 :            :                                            dev_addr);
    1080                 :            :                         ret = -1;
    1081                 :          0 :                         goto out;
    1082                 :            :                 }
    1083                 :            :         }
    1084                 :            : 
    1085                 :            :         /* if there are no active device groups, unregister the callback to
    1086                 :            :          * avoid spurious attempts to map/unmap memory from VFIO.
    1087                 :            :          */
    1088   [ #  #  #  #  :          0 :         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
                   #  # ]
    1089                 :          0 :                         rte_eal_process_type() != RTE_PROC_SECONDARY)
    1090                 :          0 :                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
    1091                 :            :                                 NULL);
    1092                 :            : 
    1093                 :            :         /* success */
    1094                 :            :         ret = 0;
    1095                 :            : 
    1096                 :          0 : out:
    1097                 :          0 :         rte_mcfg_mem_read_unlock();
    1098                 :          0 :         return ret;
    1099                 :            : }
    1100                 :            : 
    1101                 :            : RTE_EXPORT_SYMBOL(rte_vfio_enable)
    1102                 :            : int
    1103                 :        185 : rte_vfio_enable(const char *modname)
    1104                 :            : {
    1105                 :            :         /* initialize group list */
    1106                 :            :         unsigned int i, j;
    1107                 :            :         int vfio_available;
    1108                 :            :         DIR *dir;
    1109                 :            :         const struct internal_config *internal_conf =
    1110                 :        185 :                 eal_get_internal_configuration();
    1111                 :            : 
    1112                 :        185 :         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
    1113                 :            : 
    1114         [ +  + ]:      12025 :         for (i = 0; i < RTE_DIM(vfio_cfgs); i++) {
    1115                 :      11840 :                 vfio_cfgs[i].vfio_container_fd = -1;
    1116                 :      11840 :                 vfio_cfgs[i].vfio_active_groups = 0;
    1117                 :      11840 :                 vfio_cfgs[i].vfio_iommu_type = NULL;
    1118                 :      11840 :                 vfio_cfgs[i].mem_maps.lock = lock;
    1119                 :            : 
    1120         [ +  + ]:     769600 :                 for (j = 0; j < RTE_DIM(vfio_cfgs[i].vfio_groups); j++) {
    1121                 :     757760 :                         vfio_cfgs[i].vfio_groups[j].fd = -1;
    1122                 :     757760 :                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
    1123                 :     757760 :                         vfio_cfgs[i].vfio_groups[j].devices = 0;
    1124                 :            :                 }
    1125                 :            :         }
    1126                 :            : 
    1127                 :        185 :         EAL_LOG(DEBUG, "Probing VFIO support...");
    1128                 :            : 
    1129                 :            :         /* check if vfio module is loaded */
    1130                 :        185 :         vfio_available = rte_eal_check_module(modname);
    1131                 :            : 
    1132                 :            :         /* return error directly */
    1133         [ -  + ]:        185 :         if (vfio_available == -1) {
    1134                 :          0 :                 EAL_LOG(INFO, "Could not get loaded module details!");
    1135                 :          0 :                 return -1;
    1136                 :            :         }
    1137                 :            : 
    1138                 :            :         /* return 0 if VFIO modules not loaded */
    1139         [ -  + ]:        185 :         if (vfio_available == 0) {
    1140                 :          0 :                 EAL_LOG(DEBUG,
    1141                 :            :                         "VFIO modules not loaded, skipping VFIO support...");
    1142                 :          0 :                 return 0;
    1143                 :            :         }
    1144                 :            : 
    1145                 :            :         /* VFIO directory might not exist (e.g., unprivileged containers) */
    1146                 :        185 :         dir = opendir(RTE_VFIO_DIR);
    1147         [ -  + ]:        185 :         if (dir == NULL) {
    1148                 :          0 :                 EAL_LOG(DEBUG,
    1149                 :            :                         "VFIO directory does not exist, skipping VFIO support...");
    1150                 :          0 :                 return 0;
    1151                 :            :         }
    1152                 :        185 :         closedir(dir);
    1153                 :            : 
    1154         [ +  + ]:        185 :         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
    1155         [ -  + ]:        158 :                 if (vfio_mp_sync_setup() == -1) {
    1156                 :          0 :                         default_vfio_cfg->vfio_container_fd = -1;
    1157                 :            :                 } else {
    1158                 :            :                         /* open a default container */
    1159                 :        158 :                         default_vfio_cfg->vfio_container_fd = vfio_open_container_fd(false);
    1160                 :            :                 }
    1161                 :            :         } else {
    1162                 :            :                 /* get the default container from the primary process */
    1163                 :         27 :                 default_vfio_cfg->vfio_container_fd =
    1164                 :         27 :                         vfio_open_container_fd(true);
    1165                 :            :         }
    1166                 :            : 
    1167                 :            :         /* check if we have VFIO driver enabled */
    1168         [ +  + ]:        185 :         if (default_vfio_cfg->vfio_container_fd != -1) {
    1169                 :        184 :                 EAL_LOG(INFO, "VFIO support initialized");
    1170                 :        184 :                 default_vfio_cfg->vfio_enabled = 1;
    1171                 :            :         } else {
    1172                 :          1 :                 EAL_LOG(NOTICE, "VFIO support could not be initialized");
    1173                 :            :         }
    1174                 :            : 
    1175                 :            :         return 0;
    1176                 :            : }
    1177                 :            : 
    1178                 :            : RTE_EXPORT_SYMBOL(rte_vfio_is_enabled)
    1179                 :            : int
    1180                 :          0 : rte_vfio_is_enabled(const char *modname)
    1181                 :            : {
    1182                 :          0 :         const int mod_available = rte_eal_check_module(modname) > 0;
    1183   [ #  #  #  # ]:          0 :         return default_vfio_cfg->vfio_enabled && mod_available;
    1184                 :            : }
    1185                 :            : 
    1186                 :            : int
    1187                 :          0 : vfio_get_iommu_type(void)
    1188                 :            : {
    1189         [ #  # ]:          0 :         if (default_vfio_cfg->vfio_iommu_type == NULL)
    1190                 :            :                 return -1;
    1191                 :            : 
    1192                 :          0 :         return default_vfio_cfg->vfio_iommu_type->type_id;
    1193                 :            : }
    1194                 :            : 
    1195                 :            : const struct vfio_iommu_type *
    1196                 :          0 : vfio_set_iommu_type(int vfio_container_fd)
    1197                 :            : {
    1198                 :            :         unsigned idx;
    1199         [ #  # ]:          0 :         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
    1200                 :          0 :                 const struct vfio_iommu_type *t = &iommu_types[idx];
    1201                 :            : 
    1202                 :          0 :                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
    1203                 :          0 :                                 t->type_id);
    1204         [ #  # ]:          0 :                 if (!ret) {
    1205                 :          0 :                         EAL_LOG(INFO, "Using IOMMU type %d (%s)",
    1206                 :            :                                         t->type_id, t->name);
    1207                 :          0 :                         return t;
    1208                 :            :                 }
    1209                 :            :                 /* not an error, there may be more supported IOMMU types */
    1210                 :          0 :                 EAL_LOG(DEBUG, "Set IOMMU type %d (%s) failed, error "
    1211                 :            :                                 "%i (%s)", t->type_id, t->name, errno,
    1212                 :            :                                 strerror(errno));
    1213                 :            :         }
    1214                 :            :         /* if we didn't find a suitable IOMMU type, fail */
    1215                 :            :         return NULL;
    1216                 :            : }
    1217                 :            : 
    1218                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vfio_get_device_info, 24.03)
    1219                 :            : int
    1220                 :          0 : rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr,
    1221                 :            :                 int *vfio_dev_fd, struct vfio_device_info *device_info)
    1222                 :            : {
    1223                 :            :         int ret;
    1224                 :            : 
    1225   [ #  #  #  # ]:          0 :         if (device_info == NULL || *vfio_dev_fd < 0)
    1226                 :            :                 return -1;
    1227                 :            : 
    1228         [ #  # ]:          0 :         if (*vfio_dev_fd == 0) {
    1229                 :          0 :                 ret = rte_vfio_setup_device(sysfs_base, dev_addr,
    1230                 :            :                                 vfio_dev_fd, device_info);
    1231         [ #  # ]:          0 :                 if (ret)
    1232                 :          0 :                         return -1;
    1233                 :            :         } else {
    1234                 :          0 :                 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
    1235         [ #  # ]:          0 :                 if (ret) {
    1236                 :          0 :                         EAL_LOG(ERR, "%s cannot get device info, error %i (%s)",
    1237                 :            :                                         dev_addr, errno, strerror(errno));
    1238                 :          0 :                         return -1;
    1239                 :            :                 }
    1240                 :            :         }
    1241                 :            : 
    1242                 :            :         return 0;
    1243                 :            : }
    1244                 :            : 
    1245                 :            : int
    1246                 :        158 : vfio_has_supported_extensions(int vfio_container_fd)
    1247                 :            : {
    1248                 :            :         int ret;
    1249                 :            :         unsigned idx, n_extensions = 0;
    1250         [ +  + ]:        632 :         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
    1251                 :            :                 const struct vfio_iommu_type *t = &iommu_types[idx];
    1252                 :            : 
    1253                 :        474 :                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
    1254                 :        474 :                                 t->type_id);
    1255         [ -  + ]:        474 :                 if (ret < 0) {
    1256                 :          0 :                         EAL_LOG(ERR, "Could not get IOMMU type, error "
    1257                 :            :                                         "%i (%s)", errno, strerror(errno));
    1258                 :          0 :                         close(vfio_container_fd);
    1259                 :          0 :                         return -1;
    1260         [ +  + ]:        474 :                 } else if (ret == 1) {
    1261                 :            :                         /* we found a supported extension */
    1262                 :        158 :                         n_extensions++;
    1263                 :            :                 }
    1264         [ +  + ]:        790 :                 EAL_LOG(DEBUG, "IOMMU type %d (%s) is %s",
    1265                 :            :                                 t->type_id, t->name,
    1266                 :            :                                 ret ? "supported" : "not supported");
    1267                 :            :         }
    1268                 :            : 
    1269                 :            :         /* if we didn't find any supported IOMMU types, fail */
    1270         [ -  + ]:        158 :         if (!n_extensions) {
    1271                 :          0 :                 close(vfio_container_fd);
    1272                 :          0 :                 return -1;
    1273                 :            :         }
    1274                 :            : 
    1275                 :            :         return 0;
    1276                 :            : }
    1277                 :            : 
    1278                 :            : /*
    1279                 :            :  * Open a new VFIO container fd.
    1280                 :            :  *
    1281                 :            :  * If mp_request is true, requests a new container fd from the primary process
    1282                 :            :  * via mp channel (for secondary processes that need to open the default container).
    1283                 :            :  *
    1284                 :            :  * Otherwise, opens a new container fd locally by opening /dev/vfio/vfio.
    1285                 :            :  */
    1286                 :            : int
    1287                 :        185 : vfio_open_container_fd(bool mp_request)
    1288                 :            : {
    1289                 :            :         int ret, vfio_container_fd;
    1290                 :            :         struct rte_mp_msg mp_req, *mp_rep;
    1291                 :        185 :         struct rte_mp_reply mp_reply = {0};
    1292                 :        185 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
    1293                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
    1294                 :            : 
    1295                 :            :         /* if not requesting via mp, open a new container locally */
    1296         [ +  + ]:        185 :         if (!mp_request) {
    1297                 :            :                 vfio_container_fd = open(RTE_VFIO_CONTAINER_PATH, O_RDWR);
    1298         [ -  + ]:        158 :                 if (vfio_container_fd < 0) {
    1299                 :          0 :                         EAL_LOG(ERR, "Cannot open VFIO container %s, error %i (%s)",
    1300                 :            :                                 RTE_VFIO_CONTAINER_PATH, errno, strerror(errno));
    1301                 :          0 :                         return -1;
    1302                 :            :                 }
    1303                 :            : 
    1304                 :            :                 /* check VFIO API version */
    1305                 :        158 :                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
    1306         [ -  + ]:        158 :                 if (ret != VFIO_API_VERSION) {
    1307         [ #  # ]:          0 :                         if (ret < 0)
    1308                 :          0 :                                 EAL_LOG(ERR,
    1309                 :            :                                         "Could not get VFIO API version, error "
    1310                 :            :                                         "%i (%s)", errno, strerror(errno));
    1311                 :            :                         else
    1312                 :          0 :                                 EAL_LOG(ERR, "Unsupported VFIO API version!");
    1313                 :          0 :                         close(vfio_container_fd);
    1314                 :          0 :                         return -1;
    1315                 :            :                 }
    1316                 :            : 
    1317                 :        158 :                 ret = vfio_has_supported_extensions(vfio_container_fd);
    1318         [ -  + ]:        158 :                 if (ret) {
    1319                 :          0 :                         EAL_LOG(ERR,
    1320                 :            :                                 "No supported IOMMU extensions found!");
    1321                 :          0 :                         return -1;
    1322                 :            :                 }
    1323                 :            : 
    1324                 :            :                 return vfio_container_fd;
    1325                 :            :         }
    1326                 :            :         /*
    1327                 :            :          * if we're in a secondary process, request container fd from the
    1328                 :            :          * primary process via mp channel
    1329                 :            :          */
    1330                 :         27 :         p->req = SOCKET_REQ_CONTAINER;
    1331                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
    1332                 :         27 :         mp_req.len_param = sizeof(*p);
    1333                 :         27 :         mp_req.num_fds = 0;
    1334                 :            : 
    1335                 :            :         vfio_container_fd = -1;
    1336         [ +  + ]:         27 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
    1337         [ +  - ]:         26 :             mp_reply.nb_received == 1) {
    1338                 :         26 :                 mp_rep = &mp_reply.msgs[0];
    1339                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
    1340   [ +  -  +  - ]:         26 :                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
    1341                 :         26 :                         vfio_container_fd = mp_rep->fds[0];
    1342                 :         26 :                         free(mp_reply.msgs);
    1343                 :         26 :                         return vfio_container_fd;
    1344                 :            :                 }
    1345                 :            :         }
    1346                 :            : 
    1347                 :          1 :         free(mp_reply.msgs);
    1348                 :          1 :         EAL_LOG(ERR, "Cannot request VFIO container fd");
    1349                 :          1 :         return -1;
    1350                 :            : }
    1351                 :            : 
    1352                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_container_fd)
    1353                 :            : int
    1354                 :         26 : rte_vfio_get_container_fd(void)
    1355                 :            : {
    1356                 :            :         /* Return the default container fd if VFIO is enabled.
    1357                 :            :          * The default container is set up during rte_vfio_enable().
    1358                 :            :          * This function does not create a new container.
    1359                 :            :          */
    1360         [ +  - ]:         26 :         if (!default_vfio_cfg->vfio_enabled)
    1361                 :            :                 return -1;
    1362                 :            : 
    1363                 :         26 :         return default_vfio_cfg->vfio_container_fd;
    1364                 :            : }
    1365                 :            : 
    1366                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_group_num)
    1367                 :            : int
    1368                 :          0 : rte_vfio_get_group_num(const char *sysfs_base,
    1369                 :            :                 const char *dev_addr, int *iommu_group_num)
    1370                 :            : {
    1371                 :            :         char linkname[PATH_MAX];
    1372                 :            :         char filename[PATH_MAX];
    1373                 :            :         char *tok[16], *group_tok, *end;
    1374                 :            :         int ret;
    1375                 :            : 
    1376                 :            :         memset(linkname, 0, sizeof(linkname));
    1377                 :            :         memset(filename, 0, sizeof(filename));
    1378                 :            : 
    1379                 :            :         /* try to find out IOMMU group for this device */
    1380                 :            :         snprintf(linkname, sizeof(linkname),
    1381                 :            :                          "%s/%s/iommu_group", sysfs_base, dev_addr);
    1382                 :            : 
    1383                 :          0 :         ret = readlink(linkname, filename, sizeof(filename));
    1384                 :            : 
    1385                 :            :         /* if the link doesn't exist, no VFIO for us */
    1386         [ #  # ]:          0 :         if (ret < 0)
    1387                 :            :                 return 0;
    1388                 :            : 
    1389                 :          0 :         ret = rte_strsplit(filename, sizeof(filename),
    1390                 :            :                         tok, RTE_DIM(tok), '/');
    1391                 :            : 
    1392         [ #  # ]:          0 :         if (ret <= 0) {
    1393                 :          0 :                 EAL_LOG(ERR, "%s cannot get IOMMU group", dev_addr);
    1394                 :          0 :                 return -1;
    1395                 :            :         }
    1396                 :            : 
    1397                 :            :         /* IOMMU group is always the last token */
    1398                 :          0 :         errno = 0;
    1399                 :          0 :         group_tok = tok[ret - 1];
    1400                 :          0 :         end = group_tok;
    1401                 :          0 :         *iommu_group_num = strtol(group_tok, &end, 10);
    1402   [ #  #  #  #  :          0 :         if ((end != group_tok && *end != '\0') || errno != 0) {
                   #  # ]
    1403                 :          0 :                 EAL_LOG(ERR, "%s error parsing IOMMU number!", dev_addr);
    1404                 :          0 :                 return -1;
    1405                 :            :         }
    1406                 :            : 
    1407                 :            :         return 1;
    1408                 :            : }
    1409                 :            : 
    1410                 :            : static int
    1411                 :          0 : type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
    1412                 :            :                 void *arg)
    1413                 :            : {
    1414                 :            :         int *vfio_container_fd = arg;
    1415                 :            : 
    1416                 :            :         /* skip external memory that isn't a heap */
    1417   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
    1418                 :            :                 return 0;
    1419                 :            : 
    1420                 :            :         /* skip any segments with invalid IOVA addresses */
    1421         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
    1422                 :            :                 return 0;
    1423                 :            : 
    1424                 :          0 :         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
    1425                 :          0 :                         ms->len, 1);
    1426                 :            : }
    1427                 :            : 
    1428                 :            : static int
    1429                 :          0 : vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
    1430                 :            :                 uint64_t len, int do_map)
    1431                 :            : {
    1432                 :            :         struct vfio_iommu_type1_dma_map dma_map;
    1433                 :            :         struct vfio_iommu_type1_dma_unmap dma_unmap;
    1434                 :            :         int ret;
    1435                 :            : 
    1436         [ #  # ]:          0 :         if (do_map != 0) {
    1437                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
    1438                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
    1439                 :          0 :                 dma_map.vaddr = vaddr;
    1440                 :          0 :                 dma_map.size = len;
    1441                 :          0 :                 dma_map.iova = iova;
    1442                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
    1443                 :            :                                 VFIO_DMA_MAP_FLAG_WRITE;
    1444                 :            : 
    1445                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
    1446         [ #  # ]:          0 :                 if (ret) {
    1447                 :            :                         /**
    1448                 :            :                          * In case the mapping was already done EEXIST will be
    1449                 :            :                          * returned from kernel.
    1450                 :            :                          */
    1451         [ #  # ]:          0 :                         if (errno == EEXIST) {
    1452                 :          0 :                                 EAL_LOG(DEBUG,
    1453                 :            :                                         "Memory segment is already mapped, skipping");
    1454                 :            :                         } else {
    1455                 :          0 :                                 EAL_LOG(ERR,
    1456                 :            :                                         "Cannot set up DMA remapping, error "
    1457                 :            :                                         "%i (%s)", errno, strerror(errno));
    1458                 :          0 :                                 return -1;
    1459                 :            :                         }
    1460                 :            :                 }
    1461                 :            :         } else {
    1462                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
    1463                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
    1464                 :          0 :                 dma_unmap.size = len;
    1465                 :          0 :                 dma_unmap.iova = iova;
    1466                 :            : 
    1467                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
    1468                 :            :                                 &dma_unmap);
    1469         [ #  # ]:          0 :                 if (ret) {
    1470                 :          0 :                         EAL_LOG(ERR, "Cannot clear DMA remapping, error "
    1471                 :            :                                         "%i (%s)", errno, strerror(errno));
    1472                 :          0 :                         return -1;
    1473         [ #  # ]:          0 :                 } else if (dma_unmap.size != len) {
    1474                 :          0 :                         EAL_LOG(ERR, "Unexpected size %"PRIu64
    1475                 :            :                                 " of DMA remapping cleared instead of %"PRIu64,
    1476                 :            :                                 (uint64_t)dma_unmap.size, len);
    1477                 :          0 :                         rte_errno = EIO;
    1478                 :          0 :                         return -1;
    1479                 :            :                 }
    1480                 :            :         }
    1481                 :            : 
    1482                 :            :         return 0;
    1483                 :            : }
    1484                 :            : 
    1485                 :            : static int
    1486                 :          0 : vfio_type1_dma_map(int vfio_container_fd)
    1487                 :            : {
    1488                 :          0 :         return rte_memseg_walk(type1_map, &vfio_container_fd);
    1489                 :            : }
    1490                 :            : 
    1491                 :            : /* Track the size of the statically allocated DMA window for SPAPR */
    1492                 :            : uint64_t spapr_dma_win_len;
    1493                 :            : uint64_t spapr_dma_win_page_sz;
    1494                 :            : 
    1495                 :            : static int
    1496                 :          0 : vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
    1497                 :            :                 uint64_t len, int do_map)
    1498                 :            : {
    1499                 :          0 :         struct vfio_iommu_spapr_register_memory reg = {
    1500                 :            :                 .argsz = sizeof(reg),
    1501                 :            :                 .vaddr = (uintptr_t) vaddr,
    1502                 :            :                 .size = len,
    1503                 :            :                 .flags = 0
    1504                 :            :         };
    1505                 :            :         int ret;
    1506                 :            : 
    1507         [ #  # ]:          0 :         if (do_map != 0) {
    1508                 :            :                 struct vfio_iommu_type1_dma_map dma_map;
    1509                 :            : 
    1510         [ #  # ]:          0 :                 if (iova + len > spapr_dma_win_len) {
    1511                 :          0 :                         EAL_LOG(ERR, "DMA map attempt outside DMA window");
    1512                 :          0 :                         return -1;
    1513                 :            :                 }
    1514                 :            : 
    1515                 :          0 :                 ret = ioctl(vfio_container_fd,
    1516                 :            :                                 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
    1517         [ #  # ]:          0 :                 if (ret) {
    1518                 :          0 :                         EAL_LOG(ERR,
    1519                 :            :                                 "Cannot register vaddr for IOMMU, error "
    1520                 :            :                                 "%i (%s)", errno, strerror(errno));
    1521                 :          0 :                         return -1;
    1522                 :            :                 }
    1523                 :            : 
    1524                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
    1525                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
    1526                 :          0 :                 dma_map.vaddr = vaddr;
    1527                 :          0 :                 dma_map.size = len;
    1528                 :          0 :                 dma_map.iova = iova;
    1529                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
    1530                 :            :                                 VFIO_DMA_MAP_FLAG_WRITE;
    1531                 :            : 
    1532                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
    1533         [ #  # ]:          0 :                 if (ret) {
    1534                 :          0 :                         EAL_LOG(ERR, "Cannot map vaddr for IOMMU, error "
    1535                 :            :                                         "%i (%s)", errno, strerror(errno));
    1536                 :          0 :                         return -1;
    1537                 :            :                 }
    1538                 :            : 
    1539                 :            :         } else {
    1540                 :            :                 struct vfio_iommu_type1_dma_map dma_unmap;
    1541                 :            : 
    1542                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
    1543                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
    1544                 :          0 :                 dma_unmap.size = len;
    1545                 :          0 :                 dma_unmap.iova = iova;
    1546                 :            : 
    1547                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
    1548                 :            :                                 &dma_unmap);
    1549         [ #  # ]:          0 :                 if (ret) {
    1550                 :          0 :                         EAL_LOG(ERR, "Cannot unmap vaddr for IOMMU, error "
    1551                 :            :                                         "%i (%s)", errno, strerror(errno));
    1552                 :          0 :                         return -1;
    1553                 :            :                 }
    1554                 :            : 
    1555                 :          0 :                 ret = ioctl(vfio_container_fd,
    1556                 :            :                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
    1557         [ #  # ]:          0 :                 if (ret) {
    1558                 :          0 :                         EAL_LOG(ERR,
    1559                 :            :                                 "Cannot unregister vaddr for IOMMU, error "
    1560                 :            :                                 "%i (%s)", errno, strerror(errno));
    1561                 :          0 :                         return -1;
    1562                 :            :                 }
    1563                 :            :         }
    1564                 :            : 
    1565                 :            :         return ret;
    1566                 :            : }
    1567                 :            : 
    1568                 :            : static int
    1569                 :          0 : vfio_spapr_map_walk(const struct rte_memseg_list *msl,
    1570                 :            :                 const struct rte_memseg *ms, void *arg)
    1571                 :            : {
    1572                 :            :         int *vfio_container_fd = arg;
    1573                 :            : 
    1574                 :            :         /* skip external memory that isn't a heap */
    1575   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
    1576                 :            :                 return 0;
    1577                 :            : 
    1578                 :            :         /* skip any segments with invalid IOVA addresses */
    1579         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
    1580                 :            :                 return 0;
    1581                 :            : 
    1582                 :          0 :         return vfio_spapr_dma_do_map(*vfio_container_fd,
    1583                 :          0 :                 ms->addr_64, ms->iova, ms->len, 1);
    1584                 :            : }
    1585                 :            : 
    1586                 :            : struct spapr_size_walk_param {
    1587                 :            :         uint64_t max_va;
    1588                 :            :         uint64_t page_sz;
    1589                 :            :         bool is_user_managed;
    1590                 :            : };
    1591                 :            : 
    1592                 :            : /*
    1593                 :            :  * In order to set the DMA window size required for the SPAPR IOMMU
    1594                 :            :  * we need to walk the existing virtual memory allocations as well as
    1595                 :            :  * find the hugepage size used.
    1596                 :            :  */
    1597                 :            : static int
    1598                 :          0 : vfio_spapr_size_walk(const struct rte_memseg_list *msl, void *arg)
    1599                 :            : {
    1600                 :            :         struct spapr_size_walk_param *param = arg;
    1601                 :          0 :         uint64_t max = (uint64_t) msl->base_va + (uint64_t) msl->len;
    1602                 :            : 
    1603   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap) {
    1604                 :            :                 /* ignore user managed external memory */
    1605                 :          0 :                 param->is_user_managed = true;
    1606                 :          0 :                 return 0;
    1607                 :            :         }
    1608                 :            : 
    1609         [ #  # ]:          0 :         if (max > param->max_va) {
    1610                 :          0 :                 param->page_sz = msl->page_sz;
    1611                 :          0 :                 param->max_va = max;
    1612                 :            :         }
    1613                 :            : 
    1614                 :            :         return 0;
    1615                 :            : }
    1616                 :            : 
    1617                 :            : /*
    1618                 :            :  * Find the highest memory address used in physical or virtual address
    1619                 :            :  * space and use that as the top of the DMA window.
    1620                 :            :  */
    1621                 :            : static int
    1622                 :          0 : find_highest_mem_addr(struct spapr_size_walk_param *param)
    1623                 :            : {
    1624                 :            :         /* find the maximum IOVA address for setting the DMA window size */
    1625         [ #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_PA) {
    1626                 :            :                 static const char proc_iomem[] = "/proc/iomem";
    1627                 :            :                 static const char str_sysram[] = "System RAM";
    1628                 :            :                 uint64_t start, end, max = 0;
    1629                 :          0 :                 char *line = NULL;
    1630                 :            :                 char *dash, *space;
    1631                 :            :                 size_t line_len;
    1632                 :            : 
    1633                 :            :                 /*
    1634                 :            :                  * Example "System RAM" in /proc/iomem:
    1635                 :            :                  * 00000000-1fffffffff : System RAM
    1636                 :            :                  * 200000000000-201fffffffff : System RAM
    1637                 :            :                  */
    1638                 :          0 :                 FILE *fd = fopen(proc_iomem, "r");
    1639         [ #  # ]:          0 :                 if (fd == NULL) {
    1640                 :          0 :                         EAL_LOG(ERR, "Cannot open %s", proc_iomem);
    1641                 :          0 :                         return -1;
    1642                 :            :                 }
    1643                 :            :                 /* Scan /proc/iomem for the highest PA in the system */
    1644         [ #  # ]:          0 :                 while (getline(&line, &line_len, fd) != -1) {
    1645         [ #  # ]:          0 :                         if (strstr(line, str_sysram) == NULL)
    1646                 :          0 :                                 continue;
    1647                 :            : 
    1648                 :          0 :                         space = strstr(line, " ");
    1649                 :          0 :                         dash = strstr(line, "-");
    1650                 :            : 
    1651                 :            :                         /* Validate the format of the memory string */
    1652   [ #  #  #  # ]:          0 :                         if (space == NULL || dash == NULL || space < dash) {
    1653                 :          0 :                                 EAL_LOG(ERR, "Can't parse line \"%s\" in file %s",
    1654                 :            :                                         line, proc_iomem);
    1655                 :          0 :                                 continue;
    1656                 :            :                         }
    1657                 :            : 
    1658                 :          0 :                         start = strtoull(line, NULL, 16);
    1659                 :          0 :                         end   = strtoull(dash + 1, NULL, 16);
    1660                 :          0 :                         EAL_LOG(DEBUG, "Found system RAM from 0x%" PRIx64
    1661                 :            :                                 " to 0x%" PRIx64, start, end);
    1662                 :            :                         if (end > max)
    1663                 :            :                                 max = end;
    1664                 :            :                 }
    1665                 :          0 :                 free(line);
    1666                 :          0 :                 fclose(fd);
    1667                 :            : 
    1668         [ #  # ]:          0 :                 if (max == 0) {
    1669                 :          0 :                         EAL_LOG(ERR, "Failed to find valid \"System RAM\" "
    1670                 :            :                                 "entry in file %s", proc_iomem);
    1671                 :          0 :                         return -1;
    1672                 :            :                 }
    1673                 :            : 
    1674                 :          0 :                 spapr_dma_win_len = rte_align64pow2(max + 1);
    1675                 :          0 :                 return 0;
    1676         [ #  # ]:          0 :         } else if (rte_eal_iova_mode() == RTE_IOVA_VA) {
    1677                 :          0 :                 EAL_LOG(DEBUG, "Highest VA address in memseg list is 0x%"
    1678                 :            :                         PRIx64, param->max_va);
    1679                 :          0 :                 spapr_dma_win_len = rte_align64pow2(param->max_va);
    1680                 :          0 :                 return 0;
    1681                 :            :         }
    1682                 :            : 
    1683                 :          0 :         spapr_dma_win_len = 0;
    1684                 :          0 :         EAL_LOG(ERR, "Unsupported IOVA mode");
    1685                 :          0 :         return -1;
    1686                 :            : }
    1687                 :            : 
    1688                 :            : 
    1689                 :            : /*
    1690                 :            :  * The SPAPRv2 IOMMU supports 2 DMA windows with starting
    1691                 :            :  * address at 0 or 1<<59.  By default, a DMA window is set
    1692                 :            :  * at address 0, 2GB long, with a 4KB page.  For DPDK we
    1693                 :            :  * must remove the default window and setup a new DMA window
    1694                 :            :  * based on the hugepage size and memory requirements of
    1695                 :            :  * the application before we can map memory for DMA.
    1696                 :            :  */
    1697                 :            : static int
    1698                 :          0 : spapr_dma_win_size(void)
    1699                 :            : {
    1700                 :            :         struct spapr_size_walk_param param;
    1701                 :            : 
    1702                 :            :         /* only create DMA window once */
    1703         [ #  # ]:          0 :         if (spapr_dma_win_len > 0)
    1704                 :            :                 return 0;
    1705                 :            : 
    1706                 :            :         /* walk the memseg list to find the page size/max VA address */
    1707                 :            :         memset(&param, 0, sizeof(param));
    1708         [ #  # ]:          0 :         if (rte_memseg_list_walk(vfio_spapr_size_walk, &param) < 0) {
    1709                 :          0 :                 EAL_LOG(ERR, "Failed to walk memseg list for DMA window size");
    1710                 :          0 :                 return -1;
    1711                 :            :         }
    1712                 :            : 
    1713                 :            :         /* we can't be sure if DMA window covers external memory */
    1714         [ #  # ]:          0 :         if (param.is_user_managed)
    1715                 :          0 :                 EAL_LOG(WARNING, "Detected user managed external memory which may not be managed by the IOMMU");
    1716                 :            : 
    1717                 :            :         /* check physical/virtual memory size */
    1718         [ #  # ]:          0 :         if (find_highest_mem_addr(&param) < 0)
    1719                 :            :                 return -1;
    1720                 :          0 :         EAL_LOG(DEBUG, "Setting DMA window size to 0x%" PRIx64,
    1721                 :            :                 spapr_dma_win_len);
    1722                 :          0 :         spapr_dma_win_page_sz = param.page_sz;
    1723                 :          0 :         rte_mem_set_dma_mask(rte_ctz64(spapr_dma_win_len));
    1724                 :          0 :         return 0;
    1725                 :            : }
    1726                 :            : 
    1727                 :            : static int
    1728                 :          0 : vfio_spapr_create_dma_window(int vfio_container_fd)
    1729                 :            : {
    1730                 :          0 :         struct vfio_iommu_spapr_tce_create create = {
    1731                 :            :                 .argsz = sizeof(create), };
    1732                 :          0 :         struct vfio_iommu_spapr_tce_remove remove = {
    1733                 :            :                 .argsz = sizeof(remove), };
    1734                 :          0 :         struct vfio_iommu_spapr_tce_info info = {
    1735                 :            :                 .argsz = sizeof(info), };
    1736                 :            :         int ret;
    1737                 :            : 
    1738                 :          0 :         ret = spapr_dma_win_size();
    1739         [ #  # ]:          0 :         if (ret < 0)
    1740                 :            :                 return ret;
    1741                 :            : 
    1742                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
    1743         [ #  # ]:          0 :         if (ret) {
    1744                 :          0 :                 EAL_LOG(ERR, "Cannot get IOMMU info, error %i (%s)",
    1745                 :            :                         errno, strerror(errno));
    1746                 :          0 :                 return -1;
    1747                 :            :         }
    1748                 :            : 
    1749                 :            :         /*
    1750                 :            :          * sPAPR v1/v2 IOMMU always has a default 1G DMA window set.  The window
    1751                 :            :          * can't be changed for v1 but it can be changed for v2. Since DPDK only
    1752                 :            :          * supports v2, remove the default DMA window so it can be resized.
    1753                 :            :          */
    1754                 :          0 :         remove.start_addr = info.dma32_window_start;
    1755                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
    1756         [ #  # ]:          0 :         if (ret)
    1757                 :            :                 return -1;
    1758                 :            : 
    1759                 :            :         /* create a new DMA window (start address is not selectable) */
    1760                 :          0 :         create.window_size = spapr_dma_win_len;
    1761                 :          0 :         create.page_shift  = rte_ctz64(spapr_dma_win_page_sz);
    1762                 :          0 :         create.levels = 1;
    1763                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
    1764                 :            : #ifdef VFIO_IOMMU_SPAPR_INFO_DDW
    1765                 :            :         /*
    1766                 :            :          * The vfio_iommu_spapr_tce_info structure was modified in
    1767                 :            :          * Linux kernel 4.2.0 to add support for the
    1768                 :            :          * vfio_iommu_spapr_tce_ddw_info structure needed to try
    1769                 :            :          * multiple table levels.  Skip the attempt if running with
    1770                 :            :          * an older kernel.
    1771                 :            :          */
    1772         [ #  # ]:          0 :         if (ret) {
    1773                 :            :                 /* if at first we don't succeed, try more levels */
    1774                 :            :                 uint32_t levels;
    1775                 :            : 
    1776                 :          0 :                 for (levels = create.levels + 1;
    1777   [ #  #  #  # ]:          0 :                         ret && levels <= info.ddw.levels; levels++) {
    1778                 :          0 :                         create.levels = levels;
    1779                 :          0 :                         ret = ioctl(vfio_container_fd,
    1780                 :            :                                 VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
    1781                 :            :                 }
    1782                 :            :         }
    1783                 :            : #endif /* VFIO_IOMMU_SPAPR_INFO_DDW */
    1784         [ #  # ]:          0 :         if (ret) {
    1785                 :          0 :                 EAL_LOG(ERR, "Cannot create new DMA window, error "
    1786                 :            :                                 "%i (%s)", errno, strerror(errno));
    1787                 :          0 :                 EAL_LOG(ERR,
    1788                 :            :                         "Consider using a larger hugepage size if supported by the system");
    1789                 :          0 :                 return -1;
    1790                 :            :         }
    1791                 :            : 
    1792                 :            :         /* verify the start address  */
    1793         [ #  # ]:          0 :         if (create.start_addr != 0) {
    1794                 :          0 :                 EAL_LOG(ERR, "Received unsupported start address 0x%"
    1795                 :            :                         PRIx64, (uint64_t)create.start_addr);
    1796                 :          0 :                 return -1;
    1797                 :            :         }
    1798                 :            :         return ret;
    1799                 :            : }
    1800                 :            : 
    1801                 :            : static int
    1802                 :          0 : vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr,
    1803                 :            :                 uint64_t iova, uint64_t len, int do_map)
    1804                 :            : {
    1805                 :            :         int ret = 0;
    1806                 :            : 
    1807         [ #  # ]:          0 :         if (do_map) {
    1808         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(vfio_container_fd,
    1809                 :            :                         vaddr, iova, len, 1)) {
    1810                 :          0 :                         EAL_LOG(ERR, "Failed to map DMA");
    1811                 :            :                         ret = -1;
    1812                 :            :                 }
    1813                 :            :         } else {
    1814         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(vfio_container_fd,
    1815                 :            :                         vaddr, iova, len, 0)) {
    1816                 :          0 :                         EAL_LOG(ERR, "Failed to unmap DMA");
    1817                 :            :                         ret = -1;
    1818                 :            :                 }
    1819                 :            :         }
    1820                 :            : 
    1821                 :          0 :         return ret;
    1822                 :            : }
    1823                 :            : 
    1824                 :            : static int
    1825                 :          0 : vfio_spapr_dma_map(int vfio_container_fd)
    1826                 :            : {
    1827         [ #  # ]:          0 :         if (vfio_spapr_create_dma_window(vfio_container_fd) < 0) {
    1828                 :          0 :                 EAL_LOG(ERR, "Could not create new DMA window!");
    1829                 :          0 :                 return -1;
    1830                 :            :         }
    1831                 :            : 
    1832                 :            :         /* map all existing DPDK segments for DMA */
    1833         [ #  # ]:          0 :         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
    1834                 :          0 :                 return -1;
    1835                 :            : 
    1836                 :            :         return 0;
    1837                 :            : }
    1838                 :            : 
    1839                 :            : static int
    1840                 :          0 : vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
    1841                 :            : {
    1842                 :            :         /* No-IOMMU mode does not need DMA mapping */
    1843                 :          0 :         return 0;
    1844                 :            : }
    1845                 :            : 
    1846                 :            : static int
    1847                 :          0 : vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
    1848                 :            :                          uint64_t __rte_unused vaddr,
    1849                 :            :                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
    1850                 :            :                          int __rte_unused do_map)
    1851                 :            : {
    1852                 :            :         /* No-IOMMU mode does not need DMA mapping */
    1853                 :          0 :         return 0;
    1854                 :            : }
    1855                 :            : 
    1856                 :            : static int
    1857                 :          0 : vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1858                 :            :                 uint64_t len, int do_map)
    1859                 :            : {
    1860                 :          0 :         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
    1861                 :            : 
    1862         [ #  # ]:          0 :         if (!t) {
    1863                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1864                 :          0 :                 rte_errno = ENODEV;
    1865                 :          0 :                 return -1;
    1866                 :            :         }
    1867                 :            : 
    1868         [ #  # ]:          0 :         if (!t->dma_user_map_func) {
    1869                 :          0 :                 EAL_LOG(ERR,
    1870                 :            :                         "VFIO custom DMA region mapping not supported by IOMMU %s",
    1871                 :            :                         t->name);
    1872                 :          0 :                 rte_errno = ENOTSUP;
    1873                 :          0 :                 return -1;
    1874                 :            :         }
    1875                 :            : 
    1876                 :          0 :         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
    1877                 :            :                         len, do_map);
    1878                 :            : }
    1879                 :            : 
    1880                 :            : static int
    1881                 :          0 : container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1882                 :            :                 uint64_t len)
    1883                 :            : {
    1884                 :            :         struct user_mem_map *new_map;
    1885                 :            :         struct user_mem_maps *user_mem_maps;
    1886                 :            :         bool has_partial_unmap;
    1887                 :            :         int ret = 0;
    1888                 :            : 
    1889                 :          0 :         user_mem_maps = &vfio_cfg->mem_maps;
    1890                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1891         [ #  # ]:          0 :         if (user_mem_maps->n_maps == RTE_DIM(user_mem_maps->maps)) {
    1892                 :          0 :                 EAL_LOG(ERR, "No more space for user mem maps");
    1893                 :          0 :                 rte_errno = ENOMEM;
    1894                 :            :                 ret = -1;
    1895                 :          0 :                 goto out;
    1896                 :            :         }
    1897                 :            :         /* map the entry */
    1898         [ #  # ]:          0 :         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
    1899                 :            :                 /* technically, this will fail if there are currently no devices
    1900                 :            :                  * plugged in, even if a device were added later, this mapping
    1901                 :            :                  * might have succeeded. however, since we cannot verify if this
    1902                 :            :                  * is a valid mapping without having a device attached, consider
    1903                 :            :                  * this to be unsupported, because we can't just store any old
    1904                 :            :                  * mapping and pollute list of active mappings willy-nilly.
    1905                 :            :                  */
    1906                 :          0 :                 EAL_LOG(ERR, "Couldn't map new region for DMA");
    1907                 :            :                 ret = -1;
    1908                 :          0 :                 goto out;
    1909                 :            :         }
    1910                 :            :         /* do we have partial unmap support? */
    1911                 :          0 :         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
    1912                 :            : 
    1913                 :            :         /* create new user mem map entry */
    1914                 :          0 :         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
    1915                 :          0 :         new_map->addr = vaddr;
    1916                 :          0 :         new_map->iova = iova;
    1917                 :          0 :         new_map->len = len;
    1918                 :            :         /* for IOMMU types supporting partial unmap, we don't need chunking */
    1919         [ #  # ]:          0 :         new_map->chunk = has_partial_unmap ? 0 : len;
    1920                 :            : 
    1921                 :          0 :         compact_user_maps(user_mem_maps);
    1922         [ #  # ]:          0 : out:
    1923                 :            :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    1924                 :          0 :         return ret;
    1925                 :            : }
    1926                 :            : 
    1927                 :            : static int
    1928                 :          0 : container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1929                 :            :                 uint64_t len)
    1930                 :            : {
    1931                 :            :         struct user_mem_map orig_maps[RTE_DIM(vfio_cfg->mem_maps.maps)];
    1932                 :            :         struct user_mem_map new_maps[2]; /* can be at most 2 */
    1933                 :            :         struct user_mem_maps *user_mem_maps;
    1934                 :            :         int n_orig, n_new, ret = 0;
    1935                 :            :         bool has_partial_unmap;
    1936                 :            :         unsigned int newlen;
    1937                 :            : 
    1938                 :          0 :         user_mem_maps = &vfio_cfg->mem_maps;
    1939                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1940                 :            : 
    1941                 :            :         /*
    1942                 :            :          * Previously, we had adjacent mappings entirely contained within one
    1943                 :            :          * mapping entry. Since we now store original mapping length in some
    1944                 :            :          * cases, this is no longer the case, so unmapping can potentially go
    1945                 :            :          * over multiple segments and split them in any number of ways.
    1946                 :            :          *
    1947                 :            :          * To complicate things further, some IOMMU types support arbitrary
    1948                 :            :          * partial unmapping, while others will only support unmapping along the
    1949                 :            :          * chunk size, so there are a lot of cases we need to handle. To make
    1950                 :            :          * things easier code wise, instead of trying to adjust existing
    1951                 :            :          * mappings, let's just rebuild them using information we have.
    1952                 :            :          */
    1953                 :            : 
    1954                 :            :         /*
    1955                 :            :          * first thing to do is check if there exists a mapping that includes
    1956                 :            :          * the start and the end of our requested unmap. We need to collect all
    1957                 :            :          * maps that include our unmapped region.
    1958                 :            :          */
    1959                 :          0 :         n_orig = find_user_mem_maps(user_mem_maps, vaddr, iova, len,
    1960                 :            :                         orig_maps, RTE_DIM(orig_maps));
    1961                 :            :         /* did we find anything? */
    1962         [ #  # ]:          0 :         if (n_orig < 0) {
    1963                 :          0 :                 EAL_LOG(ERR, "Couldn't find previously mapped region");
    1964                 :          0 :                 rte_errno = EINVAL;
    1965                 :            :                 ret = -1;
    1966                 :          0 :                 goto out;
    1967                 :            :         }
    1968                 :            : 
    1969                 :            :         /* do we have partial unmap capability? */
    1970                 :          0 :         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
    1971                 :            : 
    1972                 :            :         /*
    1973                 :            :          * if we don't support partial unmap, we must check if start and end of
    1974                 :            :          * current unmap region are chunk-aligned.
    1975                 :            :          */
    1976         [ #  # ]:          0 :         if (!has_partial_unmap) {
    1977                 :            :                 bool start_aligned, end_aligned;
    1978                 :            : 
    1979                 :          0 :                 start_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
    1980                 :            :                                 vaddr, iova);
    1981                 :          0 :                 end_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
    1982                 :            :                                 vaddr + len, iova + len);
    1983                 :            : 
    1984         [ #  # ]:          0 :                 if (!start_aligned || !end_aligned) {
    1985                 :          0 :                         EAL_LOG(DEBUG, "DMA partial unmap unsupported");
    1986                 :          0 :                         rte_errno = ENOTSUP;
    1987                 :            :                         ret = -1;
    1988                 :          0 :                         goto out;
    1989                 :            :                 }
    1990                 :            :         }
    1991                 :            : 
    1992                 :            :         /*
    1993                 :            :          * now we know we can potentially unmap the region, but we still have to
    1994                 :            :          * figure out if there is enough space in our list to store remaining
    1995                 :            :          * maps. for this, we will figure out how many segments we are going to
    1996                 :            :          * remove, and how many new segments we are going to create.
    1997                 :            :          */
    1998                 :          0 :         n_new = process_maps(orig_maps, n_orig, new_maps, vaddr, len);
    1999                 :            : 
    2000                 :            :         /* can we store the new maps in our list? */
    2001                 :          0 :         newlen = (user_mem_maps->n_maps - n_orig) + n_new;
    2002         [ #  # ]:          0 :         if (newlen >= RTE_DIM(user_mem_maps->maps)) {
    2003                 :          0 :                 EAL_LOG(ERR, "Not enough space to store partial mapping");
    2004                 :          0 :                 rte_errno = ENOMEM;
    2005                 :            :                 ret = -1;
    2006                 :          0 :                 goto out;
    2007                 :            :         }
    2008                 :            : 
    2009                 :            :         /* unmap the entry */
    2010         [ #  # ]:          0 :         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
    2011                 :            :                 /* there may not be any devices plugged in, so unmapping will
    2012                 :            :                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
    2013                 :            :                  * stop us from removing the mapping, as the assumption is we
    2014                 :            :                  * won't be needing this memory any more and thus will want to
    2015                 :            :                  * prevent it from being remapped again on hotplug. so, only
    2016                 :            :                  * fail if we indeed failed to unmap (e.g. if the mapping was
    2017                 :            :                  * within our mapped range but had invalid alignment).
    2018                 :            :                  */
    2019         [ #  # ]:          0 :                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
    2020                 :          0 :                         EAL_LOG(ERR, "Couldn't unmap region for DMA");
    2021                 :            :                         ret = -1;
    2022                 :          0 :                         goto out;
    2023                 :            :                 } else {
    2024                 :          0 :                         EAL_LOG(DEBUG, "DMA unmapping failed, but removing mappings anyway");
    2025                 :            :                 }
    2026                 :            :         }
    2027                 :            : 
    2028                 :            :         /* we have unmapped the region, so now update the maps */
    2029                 :          0 :         delete_maps(user_mem_maps, orig_maps, n_orig);
    2030                 :          0 :         copy_maps(user_mem_maps, new_maps, n_new);
    2031                 :          0 :         compact_user_maps(user_mem_maps);
    2032         [ #  # ]:          0 : out:
    2033                 :            :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    2034                 :          0 :         return ret;
    2035                 :            : }
    2036                 :            : 
    2037                 :            : RTE_EXPORT_SYMBOL(rte_vfio_noiommu_is_enabled)
    2038                 :            : int
    2039                 :          0 : rte_vfio_noiommu_is_enabled(void)
    2040                 :            : {
    2041                 :            :         int fd;
    2042                 :            :         ssize_t cnt;
    2043                 :            :         char c;
    2044                 :            : 
    2045                 :            :         fd = open(RTE_VFIO_NOIOMMU_MODE, O_RDONLY);
    2046         [ #  # ]:          0 :         if (fd < 0) {
    2047         [ #  # ]:          0 :                 if (errno != ENOENT) {
    2048                 :          0 :                         EAL_LOG(ERR, "Cannot open VFIO noiommu file "
    2049                 :            :                                         "%i (%s)", errno, strerror(errno));
    2050                 :          0 :                         return -1;
    2051                 :            :                 }
    2052                 :            :                 /*
    2053                 :            :                  * else the file does not exists
    2054                 :            :                  * i.e. noiommu is not enabled
    2055                 :            :                  */
    2056                 :            :                 return 0;
    2057                 :            :         }
    2058                 :            : 
    2059                 :            :         cnt = read(fd, &c, 1);
    2060                 :          0 :         close(fd);
    2061         [ #  # ]:          0 :         if (cnt != 1) {
    2062                 :          0 :                 EAL_LOG(ERR, "Unable to read from VFIO noiommu file "
    2063                 :            :                                 "%i (%s)", errno, strerror(errno));
    2064                 :          0 :                 return -1;
    2065                 :            :         }
    2066                 :            : 
    2067                 :          0 :         return c == 'Y';
    2068                 :            : }
    2069                 :            : 
    2070                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_create)
    2071                 :            : int
    2072                 :          0 : rte_vfio_container_create(void)
    2073                 :            : {
    2074                 :            :         unsigned int i;
    2075                 :            : 
    2076                 :            :         /* Find an empty slot to store new vfio config */
    2077         [ #  # ]:          0 :         for (i = 1; i < RTE_DIM(vfio_cfgs); i++) {
    2078         [ #  # ]:          0 :                 if (vfio_cfgs[i].vfio_container_fd == -1)
    2079                 :            :                         break;
    2080                 :            :         }
    2081                 :            : 
    2082         [ #  # ]:          0 :         if (i == RTE_DIM(vfio_cfgs)) {
    2083                 :          0 :                 EAL_LOG(ERR, "Exceed max VFIO container limit");
    2084                 :          0 :                 return -1;
    2085                 :            :         }
    2086                 :            : 
    2087                 :            :         /* Create a new container fd */
    2088                 :          0 :         vfio_cfgs[i].vfio_container_fd = vfio_open_container_fd(false);
    2089         [ #  # ]:          0 :         if (vfio_cfgs[i].vfio_container_fd < 0) {
    2090                 :          0 :                 EAL_LOG(NOTICE, "Fail to create a new VFIO container");
    2091                 :          0 :                 return -1;
    2092                 :            :         }
    2093                 :            : 
    2094                 :            :         return vfio_cfgs[i].vfio_container_fd;
    2095                 :            : }
    2096                 :            : 
    2097                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_destroy)
    2098                 :            : int
    2099         [ #  # ]:          0 : rte_vfio_container_destroy(int container_fd)
    2100                 :            : {
    2101                 :            :         struct vfio_config *vfio_cfg;
    2102                 :            :         unsigned int i;
    2103                 :            : 
    2104                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2105         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2106                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2107                 :          0 :                 return -1;
    2108                 :            :         }
    2109                 :            : 
    2110         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(vfio_cfg->vfio_groups); i++)
    2111         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num != -1)
    2112                 :          0 :                         rte_vfio_container_group_unbind(container_fd,
    2113                 :            :                                 vfio_cfg->vfio_groups[i].group_num);
    2114                 :            : 
    2115                 :          0 :         close(container_fd);
    2116                 :          0 :         vfio_cfg->vfio_container_fd = -1;
    2117                 :          0 :         vfio_cfg->vfio_active_groups = 0;
    2118                 :          0 :         vfio_cfg->vfio_iommu_type = NULL;
    2119                 :            : 
    2120                 :          0 :         return 0;
    2121                 :            : }
    2122                 :            : 
    2123                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_group_bind)
    2124                 :            : int
    2125         [ #  # ]:          0 : rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
    2126                 :            : {
    2127                 :            :         struct vfio_config *vfio_cfg;
    2128                 :            : 
    2129                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2130         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2131                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2132                 :          0 :                 return -1;
    2133                 :            :         }
    2134                 :            : 
    2135                 :          0 :         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
    2136                 :            : }
    2137                 :            : 
    2138                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_group_unbind)
    2139                 :            : int
    2140         [ #  # ]:          0 : rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
    2141                 :            : {
    2142                 :            :         struct vfio_group *cur_grp = NULL;
    2143                 :            :         struct vfio_config *vfio_cfg;
    2144                 :            :         unsigned int i;
    2145                 :            : 
    2146                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2147         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2148                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2149                 :          0 :                 return -1;
    2150                 :            :         }
    2151                 :            : 
    2152         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(vfio_cfg->vfio_groups); i++) {
    2153         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
    2154                 :          0 :                         cur_grp = &vfio_cfg->vfio_groups[i];
    2155                 :          0 :                         break;
    2156                 :            :                 }
    2157                 :            :         }
    2158                 :            : 
    2159                 :            :         /* This should not happen */
    2160         [ #  # ]:          0 :         if (cur_grp == NULL) {
    2161                 :          0 :                 EAL_LOG(ERR, "Specified VFIO group number not found");
    2162                 :          0 :                 return -1;
    2163                 :            :         }
    2164                 :            : 
    2165   [ #  #  #  # ]:          0 :         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
    2166                 :          0 :                 EAL_LOG(ERR,
    2167                 :            :                         "Error when closing vfio_group_fd for iommu_group_num "
    2168                 :            :                         "%d", iommu_group_num);
    2169                 :          0 :                 return -1;
    2170                 :            :         }
    2171                 :          0 :         cur_grp->group_num = -1;
    2172                 :          0 :         cur_grp->fd = -1;
    2173                 :          0 :         cur_grp->devices = 0;
    2174                 :          0 :         vfio_cfg->vfio_active_groups--;
    2175                 :            : 
    2176                 :          0 :         return 0;
    2177                 :            : }
    2178                 :            : 
    2179                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_dma_map)
    2180                 :            : int
    2181                 :          0 : rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
    2182                 :            :                 uint64_t len)
    2183                 :            : {
    2184                 :            :         struct vfio_config *vfio_cfg;
    2185                 :            : 
    2186         [ #  # ]:          0 :         if (len == 0) {
    2187                 :          0 :                 rte_errno = EINVAL;
    2188                 :          0 :                 return -1;
    2189                 :            :         }
    2190                 :            : 
    2191                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2192         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2193                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2194                 :          0 :                 return -1;
    2195                 :            :         }
    2196                 :            : 
    2197                 :          0 :         return container_dma_map(vfio_cfg, vaddr, iova, len);
    2198                 :            : }
    2199                 :            : 
    2200                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_dma_unmap)
    2201                 :            : int
    2202                 :          0 : rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
    2203                 :            :                 uint64_t len)
    2204                 :            : {
    2205                 :            :         struct vfio_config *vfio_cfg;
    2206                 :            : 
    2207         [ #  # ]:          0 :         if (len == 0) {
    2208                 :          0 :                 rte_errno = EINVAL;
    2209                 :          0 :                 return -1;
    2210                 :            :         }
    2211                 :            : 
    2212                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2213         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2214                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2215                 :          0 :                 return -1;
    2216                 :            :         }
    2217                 :            : 
    2218                 :          0 :         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
    2219                 :            : }

Generated by: LCOV version 1.14