vfio: move common code out of PCI file
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <sys/ioctl.h>
36
37 #include <rte_log.h>
38 #include <rte_memory.h>
39
40 #include "eal_vfio.h"
41
42 int
43 vfio_type1_dma_map(int vfio_container_fd)
44 {
45         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
46         int i, ret;
47
48         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
49         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
50                 struct vfio_iommu_type1_dma_map dma_map;
51
52                 if (ms[i].addr == NULL)
53                         break;
54
55                 memset(&dma_map, 0, sizeof(dma_map));
56                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
57                 dma_map.vaddr = ms[i].addr_64;
58                 dma_map.size = ms[i].len;
59                 dma_map.iova = ms[i].phys_addr;
60                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
61
62                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
63
64                 if (ret) {
65                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
66                                         "error %i (%s)\n", errno, strerror(errno));
67                         return -1;
68                 }
69         }
70
71         return 0;
72 }
73
74 int
75 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
76 {
77         /* No-IOMMU mode does not need DMA mapping */
78         return 0;
79 }