1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <fcntl.h> 3 #include <libgen.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #include <sys/eventfd.h> 9 #include <sys/ioctl.h> 10 #include <sys/mman.h> 11 12 #include <linux/limits.h> 13 #include <linux/mman.h> 14 #include <linux/types.h> 15 #include <linux/vfio.h> 16 17 #include "../../../kselftest.h" 18 #include <vfio_util.h> 19 20 #define VFIO_DEV_PATH "/dev/vfio/vfio" 21 #define PCI_SYSFS_PATH "/sys/bus/pci/devices" 22 23 #define ioctl_assert(_fd, _op, _arg) do { \ 24 void *__arg = (_arg); \ 25 int __ret = ioctl((_fd), (_op), (__arg)); \ 26 VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \ 27 } while (0) 28 29 static void vfio_pci_irq_set(struct vfio_pci_device *device, 30 u32 index, u32 vector, u32 count, int *fds) 31 { 32 u8 buf[sizeof(struct vfio_irq_set) + sizeof(int) * count] = {}; 33 struct vfio_irq_set *irq = (void *)&buf; 34 int *irq_fds = (void *)&irq->data; 35 36 irq->argsz = sizeof(buf); 37 irq->flags = VFIO_IRQ_SET_ACTION_TRIGGER; 38 irq->index = index; 39 irq->start = vector; 40 irq->count = count; 41 42 if (count) { 43 irq->flags |= VFIO_IRQ_SET_DATA_EVENTFD; 44 memcpy(irq_fds, fds, sizeof(int) * count); 45 } else { 46 irq->flags |= VFIO_IRQ_SET_DATA_NONE; 47 } 48 49 ioctl_assert(device->fd, VFIO_DEVICE_SET_IRQS, irq); 50 } 51 52 void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector) 53 { 54 struct vfio_irq_set irq = { 55 .argsz = sizeof(irq), 56 .flags = VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_NONE, 57 .index = index, 58 .start = vector, 59 .count = 1, 60 }; 61 62 ioctl_assert(device->fd, VFIO_DEVICE_SET_IRQS, &irq); 63 } 64 65 static void check_supported_irq_index(u32 index) 66 { 67 /* VFIO selftests only supports MSI and MSI-x for now. */ 68 VFIO_ASSERT_TRUE(index == VFIO_PCI_MSI_IRQ_INDEX || 69 index == VFIO_PCI_MSIX_IRQ_INDEX, 70 "Unsupported IRQ index: %u\n", index); 71 } 72 73 void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, u32 vector, 74 int count) 75 { 76 int i; 77 78 check_supported_irq_index(index); 79 80 for (i = vector; i < vector + count; i++) { 81 VFIO_ASSERT_LT(device->msi_eventfds[i], 0); 82 device->msi_eventfds[i] = eventfd(0, 0); 83 VFIO_ASSERT_GE(device->msi_eventfds[i], 0); 84 } 85 86 vfio_pci_irq_set(device, index, vector, count, device->msi_eventfds + vector); 87 } 88 89 void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index) 90 { 91 int i; 92 93 check_supported_irq_index(index); 94 95 for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) { 96 if (device->msi_eventfds[i] < 0) 97 continue; 98 99 VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0); 100 device->msi_eventfds[i] = -1; 101 } 102 103 vfio_pci_irq_set(device, index, 0, 0, NULL); 104 } 105 106 static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index, 107 struct vfio_irq_info *irq_info) 108 { 109 irq_info->argsz = sizeof(*irq_info); 110 irq_info->index = index; 111 112 ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info); 113 } 114 115 void vfio_pci_dma_map(struct vfio_pci_device *device, u64 iova, u64 size, void *vaddr) 116 { 117 struct vfio_iommu_type1_dma_map map = { 118 .argsz = sizeof(map), 119 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE, 120 .vaddr = (u64)vaddr, 121 .iova = iova, 122 .size = size, 123 }; 124 125 ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &map); 126 } 127 128 void vfio_pci_dma_unmap(struct vfio_pci_device *device, u64 iova, u64 size) 129 { 130 struct vfio_iommu_type1_dma_unmap unmap = { 131 .argsz = sizeof(unmap), 132 .iova = iova, 133 .size = size, 134 }; 135 136 ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &unmap); 137 } 138 139 static void vfio_pci_region_get(struct vfio_pci_device *device, int index, 140 struct vfio_region_info *info) 141 { 142 memset(info, 0, sizeof(*info)); 143 144 info->argsz = sizeof(*info); 145 info->index = index; 146 147 ioctl_assert(device->fd, VFIO_DEVICE_GET_REGION_INFO, info); 148 } 149 150 static void vfio_pci_bar_map(struct vfio_pci_device *device, int index) 151 { 152 struct vfio_pci_bar *bar = &device->bars[index]; 153 int prot = 0; 154 155 VFIO_ASSERT_LT(index, PCI_STD_NUM_BARS); 156 VFIO_ASSERT_NULL(bar->vaddr); 157 VFIO_ASSERT_TRUE(bar->info.flags & VFIO_REGION_INFO_FLAG_MMAP); 158 159 if (bar->info.flags & VFIO_REGION_INFO_FLAG_READ) 160 prot |= PROT_READ; 161 if (bar->info.flags & VFIO_REGION_INFO_FLAG_WRITE) 162 prot |= PROT_WRITE; 163 164 bar->vaddr = mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, 165 device->fd, bar->info.offset); 166 VFIO_ASSERT_NE(bar->vaddr, MAP_FAILED); 167 } 168 169 static void vfio_pci_bar_unmap(struct vfio_pci_device *device, int index) 170 { 171 struct vfio_pci_bar *bar = &device->bars[index]; 172 173 VFIO_ASSERT_LT(index, PCI_STD_NUM_BARS); 174 VFIO_ASSERT_NOT_NULL(bar->vaddr); 175 176 VFIO_ASSERT_EQ(munmap(bar->vaddr, bar->info.size), 0); 177 bar->vaddr = NULL; 178 } 179 180 static void vfio_pci_bar_unmap_all(struct vfio_pci_device *device) 181 { 182 int i; 183 184 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 185 if (device->bars[i].vaddr) 186 vfio_pci_bar_unmap(device, i); 187 } 188 } 189 190 void vfio_pci_config_access(struct vfio_pci_device *device, bool write, 191 size_t config, size_t size, void *data) 192 { 193 struct vfio_region_info *config_space = &device->config_space; 194 int ret; 195 196 if (write) 197 ret = pwrite(device->fd, data, size, config_space->offset + config); 198 else 199 ret = pread(device->fd, data, size, config_space->offset + config); 200 201 VFIO_ASSERT_EQ(ret, size, "Failed to %s PCI config space: 0x%lx\n", 202 write ? "write to" : "read from", config); 203 } 204 205 void vfio_pci_device_reset(struct vfio_pci_device *device) 206 { 207 ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); 208 } 209 210 static unsigned int vfio_pci_get_group_from_dev(const char *bdf) 211 { 212 char dev_iommu_group_path[PATH_MAX] = {0}; 213 char sysfs_path[PATH_MAX] = {0}; 214 unsigned int group; 215 int ret; 216 217 snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); 218 219 ret = readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group_path)); 220 VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n", bdf); 221 222 ret = sscanf(basename(dev_iommu_group_path), "%u", &group); 223 VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", bdf); 224 225 return group; 226 } 227 228 static void vfio_pci_container_setup(struct vfio_pci_device *device) 229 { 230 int version; 231 232 device->container_fd = open(VFIO_DEV_PATH, O_RDWR); 233 VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", VFIO_DEV_PATH); 234 235 version = ioctl(device->container_fd, VFIO_GET_API_VERSION); 236 VFIO_ASSERT_EQ(version, VFIO_API_VERSION); 237 } 238 239 static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) 240 { 241 struct vfio_group_status group_status = { 242 .argsz = sizeof(group_status), 243 }; 244 char group_path[32]; 245 int group; 246 247 group = vfio_pci_get_group_from_dev(bdf); 248 snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); 249 250 device->group_fd = open(group_path, O_RDWR); 251 VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); 252 253 ioctl_assert(device->group_fd, VFIO_GROUP_GET_STATUS, &group_status); 254 VFIO_ASSERT_TRUE(group_status.flags & VFIO_GROUP_FLAGS_VIABLE); 255 256 ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd); 257 } 258 259 static void vfio_pci_iommu_setup(struct vfio_pci_device *device, unsigned long iommu_type) 260 { 261 int ret; 262 263 ret = ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type); 264 VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); 265 266 ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); 267 } 268 269 static void vfio_pci_device_setup(struct vfio_pci_device *device, const char *bdf) 270 { 271 int i; 272 273 device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); 274 VFIO_ASSERT_GE(device->fd, 0); 275 276 device->info.argsz = sizeof(device->info); 277 ioctl_assert(device->fd, VFIO_DEVICE_GET_INFO, &device->info); 278 279 vfio_pci_region_get(device, VFIO_PCI_CONFIG_REGION_INDEX, &device->config_space); 280 281 /* Sanity check VFIO does not advertise mmap for config space */ 282 VFIO_ASSERT_TRUE(!(device->config_space.flags & VFIO_REGION_INFO_FLAG_MMAP), 283 "PCI config space should not support mmap()\n"); 284 285 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 286 struct vfio_pci_bar *bar = device->bars + i; 287 288 vfio_pci_region_get(device, i, &bar->info); 289 if (bar->info.flags & VFIO_REGION_INFO_FLAG_MMAP) 290 vfio_pci_bar_map(device, i); 291 } 292 293 vfio_pci_irq_get(device, VFIO_PCI_MSI_IRQ_INDEX, &device->msi_info); 294 vfio_pci_irq_get(device, VFIO_PCI_MSIX_IRQ_INDEX, &device->msix_info); 295 296 for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) 297 device->msi_eventfds[i] = -1; 298 } 299 300 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type) 301 { 302 struct vfio_pci_device *device; 303 304 device = calloc(1, sizeof(*device)); 305 VFIO_ASSERT_NOT_NULL(device); 306 307 vfio_pci_container_setup(device); 308 vfio_pci_group_setup(device, bdf); 309 vfio_pci_iommu_setup(device, iommu_type); 310 vfio_pci_device_setup(device, bdf); 311 312 return device; 313 } 314 315 void vfio_pci_device_cleanup(struct vfio_pci_device *device) 316 { 317 int i; 318 319 vfio_pci_bar_unmap_all(device); 320 321 VFIO_ASSERT_EQ(close(device->fd), 0); 322 323 for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) { 324 if (device->msi_eventfds[i] < 0) 325 continue; 326 327 VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0); 328 } 329 330 VFIO_ASSERT_EQ(close(device->group_fd), 0); 331 VFIO_ASSERT_EQ(close(device->container_fd), 0); 332 333 free(device); 334 } 335 336 static bool is_bdf(const char *str) 337 { 338 unsigned int s, b, d, f; 339 int length, count; 340 341 count = sscanf(str, "%4x:%2x:%2x.%2x%n", &s, &b, &d, &f, &length); 342 return count == 4 && length == strlen(str); 343 } 344 345 const char *vfio_selftests_get_bdf(int *argc, char *argv[]) 346 { 347 char *bdf; 348 349 if (*argc > 1 && is_bdf(argv[*argc - 1])) 350 return argv[--(*argc)]; 351 352 bdf = getenv("VFIO_SELFTESTS_BDF"); 353 if (bdf) { 354 VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf); 355 return bdf; 356 } 357 358 fprintf(stderr, "Unable to determine which device to use, skipping test.\n"); 359 fprintf(stderr, "\n"); 360 fprintf(stderr, "To pass the device address via environment variable:\n"); 361 fprintf(stderr, "\n"); 362 fprintf(stderr, " export VFIO_SELFTESTS_BDF=segment:bus:device.function\n"); 363 fprintf(stderr, " %s [options]\n", argv[0]); 364 fprintf(stderr, "\n"); 365 fprintf(stderr, "To pass the device address via argv:\n"); 366 fprintf(stderr, "\n"); 367 fprintf(stderr, " %s [options] segment:bus:device.function\n", argv[0]); 368 fprintf(stderr, "\n"); 369 exit(KSFT_SKIP); 370 } 371