1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declarat/*
* Copyright (c) 2018 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/vfio.h>
#include <sys/ioctl.h>
#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlib/pci/pci.h>
#include <vlib/linux/vfio.h>
#include <vlib/physmem.h>
#ifndef VFIO_NOIOMMU_IOMMU
#define VFIO_NOIOMMU_IOMMU 8
#endif
linux_vfio_main_t vfio_main;
static int
map_regions (vlib_main_t * vm, int fd)
{
vlib_physmem_main_t *vpm = &physmem_main;
vlib_physmem_region_t *pr;
struct vfio_iommu_type1_dma_map dm = { 0 };
int i;
dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
/* *INDENT-OFF* */
pool_foreach (pr, vpm->regions,
{
vec_foreach_index (i, pr->page_table)
{
int rv;
dm.vaddr = pointer_to_uword (pr->mem) + (i << pr->log2_page_size);
dm.size = 1 << pr->log2_page_size;
dm.iova = dm.vaddr;
if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
return rv;
}
});
/* *INDENT-ON* */
return 0;
}
void
linux_vfio_dma_map_regions (vlib_main_t * vm)
{
linux_vfio_main_t *lvm = &vfio_main;
if (lvm->container_fd != -1)
map_regions (vm, lvm->container_fd);
}
static linux_pci_vfio_iommu_group_t *
get_vfio_iommu_group (int group)
{
linux_vfio_main_t *lvm = &vfio_main;
uword *p;
p = hash_get (lvm->iommu_pool_index_by_group, group);
return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
}
static clib_error_t *
open_vfio_iommu_group (int group, int is_noiommu)
{
linux_vfio_main_t *lvm = &vfio_main;
linux_pci_vfio_iommu_group_t *g;
clib_error_t *err = 0;
struct vfio_group_status group_status;
u8 *s = 0;
int fd;
g = get_vfio_iommu_group (group);
if (g)
{
g->refcnt++;
return 0;
}
s = format (s, "/dev/vfio/%s%u%c", is_noiommu ? "noiommu-" : "", group, 0);
fd = open ((char *) s, O_RDWR);
if (fd < 0)
return clib_error_return_unix (0, "open '%s'", s);
group_status.argsz = sizeof (group_status);
if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
{
err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
s);
goto error;
}
if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
{
err = clib_error_return (0, "iommu group %d is not viable (not all "
"devices in this group bound to vfio-pci)",
group);
goto error;
}
if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
{
err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
s);
goto error;
}
if (lvm->iommu_mode == 0)
{
if (is_noiommu)
lvm->iommu_mode = VFIO_NOIOMMU_IOMMU;
else
lvm->iommu_mode = VFIO_TYPE1_IOMMU;
if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, lvm->iommu_mode) < 0)
{
err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
"'/dev/vfio/vfio'");
goto error;
}
}
pool_get (lvm->iommu_groups, g);
g->fd = fd;
g->refcnt = 1;
hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
vec_free (s);
return 0;
error:
close (fd);
return err;
}
clib_error_t *
linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
{
clib_error_t *err = 0;
linux_pci_vfio_iommu_group_t *g;
u8 *s = 0;
int iommu_group;
u8 *tmpstr;
int fd;
int is_noiommu = 0;
s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
addr);
tmpstr = clib_sysfs_link_to_name ((char *) s);
if (tmpstr)
{
iommu_group = atoi ((char *) tmpstr);
vec_free (tmpstr);
}
else
{
err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
"'%U'", format_vlib_pci_addr, addr);
goto error;
}
vec_reset_length (s);
s =
format (s, "/sys/bus/pci/devices/%U/iommu_group/name",
format_vlib_pci_addr, addr);
err = clib_sysfs_read ((char *) s, "%s", &tmpstr);
if (err == 0)
{
if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
is_noiommu = 1;
vec_free (tmpstr);
}
else
clib_error_free (err);
vec_reset_length (s);
if ((err = open_vfio_iommu_group (iommu_group, is_noiommu)))
return err;
g = get_vfio_iommu_group (iommu_group);
s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
{
err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
format_vlib_pci_addr, addr);
goto error;
}
vec_reset_length (s);
*fdp = fd;
error:
vec_free (s);
return err;
}
clib_error_t *
linux_vfio_init (vlib_main_t * vm)
{
linux_vfio_main_t *lvm = &vfio_main;
int fd;
fd = open ("/dev/vfio/vfio", O_RDWR);
/* check if iommu is available */
if (fd != -1)
{
if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
{
close (fd);
fd = -1;
}
else
{
if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_NOIOMMU_IOMMU) == 1)
lvm->flags |= LINUX_VFIO_F_HAVE_NOIOMMU;
}
}
lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
lvm->container_fd = fd;
return 0;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|