1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H 3 #define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H 4 5 #include <fcntl.h> 6 #include <string.h> 7 #include <linux/vfio.h> 8 #include <linux/list.h> 9 #include <linux/pci_regs.h> 10 11 #include "../../../kselftest.h" 12 13 #define VFIO_LOG_AND_EXIT(...) do { \ 14 fprintf(stderr, " " __VA_ARGS__); \ 15 fprintf(stderr, "\n"); \ 16 exit(KSFT_FAIL); \ 17 } while (0) 18 19 #define VFIO_ASSERT_OP(_lhs, _rhs, _op, ...) do { \ 20 typeof(_lhs) __lhs = (_lhs); \ 21 typeof(_rhs) __rhs = (_rhs); \ 22 \ 23 if (__lhs _op __rhs) \ 24 break; \ 25 \ 26 fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__); \ 27 fprintf(stderr, " Expression: " #_lhs " " #_op " " #_rhs "\n"); \ 28 fprintf(stderr, " Observed: %#lx %s %#lx\n", \ 29 (u64)__lhs, #_op, (u64)__rhs); \ 30 fprintf(stderr, " [errno: %d - %s]\n", errno, strerror(errno)); \ 31 VFIO_LOG_AND_EXIT(__VA_ARGS__); \ 32 } while (0) 33 34 #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__) 35 #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__) 36 #define VFIO_ASSERT_LT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__) 37 #define VFIO_ASSERT_LE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__) 38 #define VFIO_ASSERT_GT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__) 39 #define VFIO_ASSERT_GE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__) 40 #define VFIO_ASSERT_TRUE(_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__) 41 #define VFIO_ASSERT_FALSE(_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__) 42 #define VFIO_ASSERT_NULL(_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__) 43 #define VFIO_ASSERT_NOT_NULL(_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__) 44 45 #define VFIO_FAIL(_fmt, ...) do { \ 46 fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__); \ 47 VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ 48 } while (0) 49 50 struct vfio_pci_bar { 51 struct vfio_region_info info; 52 void *vaddr; 53 }; 54 55 typedef u64 iova_t; 56 57 #define INVALID_IOVA UINT64_MAX 58 59 struct vfio_dma_region { 60 struct list_head link; 61 void *vaddr; 62 iova_t iova; 63 u64 size; 64 }; 65 66 struct vfio_pci_device { 67 int fd; 68 int group_fd; 69 int container_fd; 70 71 struct vfio_device_info info; 72 struct vfio_region_info config_space; 73 struct vfio_pci_bar bars[PCI_STD_NUM_BARS]; 74 75 struct vfio_irq_info msi_info; 76 struct vfio_irq_info msix_info; 77 78 struct list_head dma_regions; 79 80 /* eventfds for MSI and MSI-x interrupts */ 81 int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1]; 82 }; 83 84 /* 85 * Return the BDF string of the device that the test should use. 86 * 87 * If a BDF string is provided by the user on the command line (as the last 88 * element of argv[]), then this function will return that and decrement argc 89 * by 1. 90 * 91 * Otherwise this function will attempt to use the environment variable 92 * $VFIO_SELFTESTS_BDF. 93 * 94 * If BDF cannot be determined then the test will exit with KSFT_SKIP. 95 */ 96 const char *vfio_selftests_get_bdf(int *argc, char *argv[]); 97 98 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type); 99 void vfio_pci_device_cleanup(struct vfio_pci_device *device); 100 void vfio_pci_device_reset(struct vfio_pci_device *device); 101 102 void vfio_pci_dma_map(struct vfio_pci_device *device, 103 struct vfio_dma_region *region); 104 void vfio_pci_dma_unmap(struct vfio_pci_device *device, 105 struct vfio_dma_region *region); 106 107 void vfio_pci_config_access(struct vfio_pci_device *device, bool write, 108 size_t config, size_t size, void *data); 109 110 #define vfio_pci_config_read(_device, _offset, _type) ({ \ 111 _type __data; \ 112 vfio_pci_config_access((_device), false, _offset, sizeof(__data), &__data); \ 113 __data; \ 114 }) 115 116 #define vfio_pci_config_readb(_d, _o) vfio_pci_config_read(_d, _o, u8) 117 #define vfio_pci_config_readw(_d, _o) vfio_pci_config_read(_d, _o, u16) 118 #define vfio_pci_config_readl(_d, _o) vfio_pci_config_read(_d, _o, u32) 119 120 #define vfio_pci_config_write(_device, _offset, _value, _type) do { \ 121 _type __data = (_value); \ 122 vfio_pci_config_access((_device), true, _offset, sizeof(_type), &__data); \ 123 } while (0) 124 125 #define vfio_pci_config_writeb(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u8) 126 #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16) 127 #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32) 128 129 void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, 130 u32 vector, int count); 131 void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index); 132 void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector); 133 134 static inline void fcntl_set_nonblock(int fd) 135 { 136 int r; 137 138 r = fcntl(fd, F_GETFL, 0); 139 VFIO_ASSERT_NE(r, -1, "F_GETFL failed for fd %d\n", fd); 140 141 r = fcntl(fd, F_SETFL, r | O_NONBLOCK); 142 VFIO_ASSERT_NE(r, -1, "F_SETFL O_NONBLOCK failed for fd %d\n", fd); 143 } 144 145 static inline void vfio_pci_msi_enable(struct vfio_pci_device *device, 146 u32 vector, int count) 147 { 148 vfio_pci_irq_enable(device, VFIO_PCI_MSI_IRQ_INDEX, vector, count); 149 } 150 151 static inline void vfio_pci_msi_disable(struct vfio_pci_device *device) 152 { 153 vfio_pci_irq_disable(device, VFIO_PCI_MSI_IRQ_INDEX); 154 } 155 156 static inline void vfio_pci_msix_enable(struct vfio_pci_device *device, 157 u32 vector, int count) 158 { 159 vfio_pci_irq_enable(device, VFIO_PCI_MSIX_IRQ_INDEX, vector, count); 160 } 161 162 static inline void vfio_pci_msix_disable(struct vfio_pci_device *device) 163 { 164 vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX); 165 } 166 167 iova_t __to_iova(struct vfio_pci_device *device, void *vaddr); 168 iova_t to_iova(struct vfio_pci_device *device, void *vaddr); 169 170 static inline bool vfio_pci_device_match(struct vfio_pci_device *device, 171 u16 vendor_id, u16 device_id) 172 { 173 return (vendor_id == vfio_pci_config_readw(device, PCI_VENDOR_ID)) && 174 (device_id == vfio_pci_config_readw(device, PCI_DEVICE_ID)); 175 } 176 177 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */ 178