Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 1 | /* |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 15 | |
| 16 | #include <unistd.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <linux/vfio.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | |
| 23 | #include <vppinfra/linux/sysfs.h> |
| 24 | |
| 25 | #include <vlib/vlib.h> |
| 26 | #include <vlib/unix/unix.h> |
| 27 | #include <vlib/pci/pci.h> |
| 28 | #include <vlib/linux/vfio.h> |
| 29 | #include <vlib/physmem.h> |
| 30 | |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 31 | #ifndef VFIO_NOIOMMU_IOMMU |
| 32 | #define VFIO_NOIOMMU_IOMMU 8 |
| 33 | #endif |
| 34 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 35 | linux_vfio_main_t vfio_main; |
| 36 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 37 | clib_error_t * |
| 38 | vfio_map_physmem_page (vlib_main_t * vm, void *addr) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 39 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 40 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
Damjan Marion | 7d42653 | 2018-09-27 14:54:49 +0200 | [diff] [blame] | 41 | linux_vfio_main_t *lvm = &vfio_main; |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 42 | struct vfio_iommu_type1_dma_map dm = { 0 }; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 43 | uword log2_page_size = vpm->pmalloc_main->log2_page_sz; |
| 44 | uword physmem_start = pointer_to_uword (vpm->pmalloc_main->base); |
| 45 | |
| 46 | if (lvm->container_fd == -1) |
| 47 | return clib_error_return (0, "No cointainer fd"); |
| 48 | |
| 49 | u32 page_index = vlib_physmem_get_page_index (vm, addr); |
| 50 | |
| 51 | if (clib_bitmap_get (lvm->physmem_pages_mapped, page_index)) |
| 52 | { |
| 53 | vlib_log_debug (lvm->log_default, "map DMA va:%p page:%u already " |
| 54 | "mapped", addr, page_index); |
| 55 | return 0; |
| 56 | } |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 57 | |
| 58 | dm.argsz = sizeof (struct vfio_iommu_type1_dma_map); |
| 59 | dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 60 | dm.vaddr = physmem_start + (page_index << log2_page_size); |
| 61 | dm.size = 1ULL << log2_page_size; |
| 62 | dm.iova = dm.vaddr; |
| 63 | vlib_log_debug (lvm->log_default, "map DMA page:%u va:0x%lx iova:%lx " |
| 64 | "size:0x%lx", page_index, dm.vaddr, dm.iova, dm.size); |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 65 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 66 | if (ioctl (lvm->container_fd, VFIO_IOMMU_MAP_DMA, &dm) == -1) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 67 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 68 | vlib_log_err (lvm->log_default, "map DMA page:%u va:0x%lx iova:%lx " |
| 69 | "size:0x%lx failed, error %s (errno %d)", page_index, |
| 70 | dm.vaddr, dm.iova, dm.size, strerror (errno), errno); |
| 71 | return clib_error_return_unix (0, "physmem DMA map failed"); |
| 72 | } |
Damjan Marion | 7d42653 | 2018-09-27 14:54:49 +0200 | [diff] [blame] | 73 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame^] | 74 | lvm->physmem_pages_mapped = clib_bitmap_set (lvm->physmem_pages_mapped, |
| 75 | page_index, 1); |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 79 | static linux_pci_vfio_iommu_group_t * |
| 80 | get_vfio_iommu_group (int group) |
| 81 | { |
| 82 | linux_vfio_main_t *lvm = &vfio_main; |
| 83 | uword *p; |
| 84 | |
| 85 | p = hash_get (lvm->iommu_pool_index_by_group, group); |
| 86 | |
| 87 | return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0; |
| 88 | } |
| 89 | |
| 90 | static clib_error_t * |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 91 | open_vfio_iommu_group (int group, int is_noiommu) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 92 | { |
| 93 | linux_vfio_main_t *lvm = &vfio_main; |
| 94 | linux_pci_vfio_iommu_group_t *g; |
| 95 | clib_error_t *err = 0; |
| 96 | struct vfio_group_status group_status; |
| 97 | u8 *s = 0; |
| 98 | int fd; |
| 99 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 100 | if (lvm->container_fd == -1) |
| 101 | { |
| 102 | if ((fd = open ("/dev/vfio/vfio", O_RDWR)) == -1) |
| 103 | return clib_error_return_unix (0, "failed to open VFIO container"); |
| 104 | |
| 105 | if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION) |
| 106 | { |
| 107 | close (fd); |
| 108 | return clib_error_return_unix (0, "incompatible VFIO version"); |
| 109 | } |
| 110 | |
| 111 | lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword)); |
| 112 | lvm->container_fd = fd; |
| 113 | } |
| 114 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 115 | g = get_vfio_iommu_group (group); |
| 116 | if (g) |
| 117 | { |
| 118 | g->refcnt++; |
| 119 | return 0; |
| 120 | } |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 121 | s = format (s, "/dev/vfio/%s%u%c", is_noiommu ? "noiommu-" : "", group, 0); |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 122 | fd = open ((char *) s, O_RDWR); |
| 123 | if (fd < 0) |
| 124 | return clib_error_return_unix (0, "open '%s'", s); |
| 125 | |
| 126 | group_status.argsz = sizeof (group_status); |
| 127 | if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0) |
| 128 | { |
| 129 | err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'", |
| 130 | s); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) |
| 135 | { |
| 136 | err = clib_error_return (0, "iommu group %d is not viable (not all " |
| 137 | "devices in this group bound to vfio-pci)", |
| 138 | group); |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0) |
| 143 | { |
| 144 | err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'", |
| 145 | s); |
| 146 | goto error; |
| 147 | } |
| 148 | |
| 149 | if (lvm->iommu_mode == 0) |
| 150 | { |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 151 | if (is_noiommu) |
| 152 | lvm->iommu_mode = VFIO_NOIOMMU_IOMMU; |
| 153 | else |
| 154 | lvm->iommu_mode = VFIO_TYPE1_IOMMU; |
| 155 | |
| 156 | if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, lvm->iommu_mode) < 0) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 157 | { |
| 158 | err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) " |
| 159 | "'/dev/vfio/vfio'"); |
| 160 | goto error; |
| 161 | } |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | pool_get (lvm->iommu_groups, g); |
| 166 | g->fd = fd; |
| 167 | g->refcnt = 1; |
| 168 | hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups); |
| 169 | vec_free (s); |
| 170 | return 0; |
| 171 | error: |
| 172 | close (fd); |
| 173 | return err; |
| 174 | } |
| 175 | |
| 176 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 177 | linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp, |
| 178 | int *is_noiommu) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 179 | { |
| 180 | clib_error_t *err = 0; |
| 181 | linux_pci_vfio_iommu_group_t *g; |
| 182 | u8 *s = 0; |
| 183 | int iommu_group; |
| 184 | u8 *tmpstr; |
| 185 | int fd; |
| 186 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 187 | *is_noiommu = 0; |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 188 | s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr, |
| 189 | addr); |
| 190 | tmpstr = clib_sysfs_link_to_name ((char *) s); |
| 191 | if (tmpstr) |
| 192 | { |
| 193 | iommu_group = atoi ((char *) tmpstr); |
| 194 | vec_free (tmpstr); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | err = clib_error_return (0, "Cannot find IOMMU group for PCI device ", |
| 199 | "'%U'", format_vlib_pci_addr, addr); |
| 200 | goto error; |
| 201 | } |
| 202 | vec_reset_length (s); |
| 203 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 204 | s = format (s, "/sys/bus/pci/devices/%U/iommu_group/name", |
| 205 | format_vlib_pci_addr, addr); |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 206 | err = clib_sysfs_read ((char *) s, "%s", &tmpstr); |
| 207 | if (err == 0) |
| 208 | { |
| 209 | if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 210 | *is_noiommu = 1; |
| 211 | |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 212 | vec_free (tmpstr); |
| 213 | } |
| 214 | else |
| 215 | clib_error_free (err); |
| 216 | vec_reset_length (s); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 217 | if ((err = open_vfio_iommu_group (iommu_group, *is_noiommu))) |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 218 | return err; |
| 219 | |
| 220 | g = get_vfio_iommu_group (iommu_group); |
| 221 | |
| 222 | s = format (s, "%U%c", format_vlib_pci_addr, addr, 0); |
| 223 | if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0) |
| 224 | { |
| 225 | err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'", |
| 226 | format_vlib_pci_addr, addr); |
| 227 | goto error; |
| 228 | } |
| 229 | vec_reset_length (s); |
| 230 | |
| 231 | *fdp = fd; |
| 232 | |
| 233 | error: |
| 234 | vec_free (s); |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | clib_error_t * |
| 239 | linux_vfio_init (vlib_main_t * vm) |
| 240 | { |
| 241 | linux_vfio_main_t *lvm = &vfio_main; |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 242 | |
Damjan Marion | 7d42653 | 2018-09-27 14:54:49 +0200 | [diff] [blame] | 243 | lvm->log_default = vlib_log_register_class ("vfio", 0); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 244 | lvm->container_fd = -1; |
Damjan Marion | 7d42653 | 2018-09-27 14:54:49 +0200 | [diff] [blame] | 245 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * fd.io coding-style-patch-verification: ON |
| 251 | * |
| 252 | * Local Variables: |
| 253 | * eval: (c-set-style "gnu") |
| 254 | * End: |
| 255 | */ |