/* SPDX-License-Identifier: GPL-2.0-only */ #ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H #define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H #include #include #include #include #include "../../../kselftest.h" #define VFIO_LOG_AND_EXIT(...) do { \ fprintf(stderr, " " __VA_ARGS__); \ fprintf(stderr, "\n"); \ exit(KSFT_FAIL); \ } while (0) #define VFIO_ASSERT_OP(_lhs, _rhs, _op, ...) do { \ typeof(_lhs) __lhs = (_lhs); \ typeof(_rhs) __rhs = (_rhs); \ \ if (__lhs _op __rhs) \ break; \ \ fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__); \ fprintf(stderr, " Expression: " #_lhs " " #_op " " #_rhs "\n"); \ fprintf(stderr, " Observed: %#lx %s %#lx\n", \ (u64)__lhs, #_op, (u64)__rhs); \ fprintf(stderr, " [errno: %d - %s]\n", errno, strerror(errno)); \ VFIO_LOG_AND_EXIT(__VA_ARGS__); \ } while (0) #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__) #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__) #define VFIO_ASSERT_LT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__) #define VFIO_ASSERT_LE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__) #define VFIO_ASSERT_GT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__) #define VFIO_ASSERT_GE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__) #define VFIO_ASSERT_TRUE(_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__) #define VFIO_ASSERT_FALSE(_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__) #define VFIO_ASSERT_NULL(_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__) #define VFIO_ASSERT_NOT_NULL(_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__) #define VFIO_FAIL(_fmt, ...) do { \ fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__); \ VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ } while (0) struct vfio_pci_bar { struct vfio_region_info info; void *vaddr; }; struct vfio_pci_device { int fd; int group_fd; int container_fd; struct vfio_device_info info; struct vfio_region_info config_space; struct vfio_pci_bar bars[PCI_STD_NUM_BARS]; struct vfio_irq_info msi_info; struct vfio_irq_info msix_info; /* eventfds for MSI and MSI-x interrupts */ int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1]; }; /* * Return the BDF string of the device that the test should use. * * If a BDF string is provided by the user on the command line (as the last * element of argv[]), then this function will return that and decrement argc * by 1. * * Otherwise this function will attempt to use the environment variable * $VFIO_SELFTESTS_BDF. * * If BDF cannot be determined then the test will exit with KSFT_SKIP. */ const char *vfio_selftests_get_bdf(int *argc, char *argv[]); struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type); void vfio_pci_device_cleanup(struct vfio_pci_device *device); void vfio_pci_device_reset(struct vfio_pci_device *device); void vfio_pci_dma_map(struct vfio_pci_device *device, u64 iova, u64 size, void *vaddr); void vfio_pci_dma_unmap(struct vfio_pci_device *device, u64 iova, u64 size); void vfio_pci_config_access(struct vfio_pci_device *device, bool write, size_t config, size_t size, void *data); #define vfio_pci_config_read(_device, _offset, _type) ({ \ _type __data; \ vfio_pci_config_access((_device), false, _offset, sizeof(__data), &__data); \ __data; \ }) #define vfio_pci_config_readb(_d, _o) vfio_pci_config_read(_d, _o, u8) #define vfio_pci_config_readw(_d, _o) vfio_pci_config_read(_d, _o, u16) #define vfio_pci_config_readl(_d, _o) vfio_pci_config_read(_d, _o, u32) #define vfio_pci_config_write(_device, _offset, _value, _type) do { \ _type __data = (_value); \ vfio_pci_config_access((_device), true, _offset, sizeof(_type), &__data); \ } while (0) #define vfio_pci_config_writeb(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u8) #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16) #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32) void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, u32 vector, int count); void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index); void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector); static inline void vfio_pci_msi_enable(struct vfio_pci_device *device, u32 vector, int count) { vfio_pci_irq_enable(device, VFIO_PCI_MSI_IRQ_INDEX, vector, count); } static inline void vfio_pci_msi_disable(struct vfio_pci_device *device) { vfio_pci_irq_disable(device, VFIO_PCI_MSI_IRQ_INDEX); } static inline void vfio_pci_msix_enable(struct vfio_pci_device *device, u32 vector, int count) { vfio_pci_irq_enable(device, VFIO_PCI_MSIX_IRQ_INDEX, vector, count); } static inline void vfio_pci_msix_disable(struct vfio_pci_device *device) { vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX); } #endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */