1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/kernel/vfio.h

112 lines
3.4 KiB
C
Raw Permalink Normal View History

2016-06-14 01:19:17 +02:00
/** Virtual Function IO wrapper around kernel API
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Steffen Vogel
2016-06-14 01:19:17 +02:00
*********************************************************************************/
/** @addtogroup fpga Kernel @{ */
2017-02-16 09:04:12 -03:00
#pragma once
2016-06-14 01:19:17 +02:00
2017-02-18 10:58:17 -05:00
#include <stdint.h>
2016-06-14 01:19:17 +02:00
#include <stdbool.h>
2016-07-08 13:32:18 +02:00
#include <sys/mman.h>
2016-06-14 01:19:17 +02:00
#include <linux/vfio.h>
#include <linux/pci_regs.h>
2016-07-08 13:32:18 +02:00
#include "list.h"
2016-06-14 01:19:17 +02:00
#define VFIO_DEV(x) "/dev/vfio/" x
2017-02-16 09:11:49 -03:00
/* Forward declarations */
struct pci_device;
2016-07-08 13:32:18 +02:00
2016-06-14 01:19:17 +02:00
struct vfio_group {
int fd; /**< VFIO group file descriptor */
int index; /**< Index of the IOMMU group as listed under /sys/kernel/iommu_groups/ */
2016-06-14 01:19:17 +02:00
struct vfio_group_status status; /**< Status of group */
2016-06-14 01:19:17 +02:00
struct list devices;
struct vfio_container *container; /**< The VFIO container to which this group is belonging */
2016-06-14 01:19:17 +02:00
};
struct vfio_device {
2016-06-14 01:19:17 +02:00
char *name; /**< Name of the device as listed under /sys/kernel/iommu_groups/[vfio_group::index]/devices/ */
int fd; /**< VFIO device file descriptor */
struct vfio_device_info info;
struct vfio_irq_info *irqs;
struct vfio_region_info *regions;
2016-07-08 13:32:18 +02:00
2016-06-14 01:19:17 +02:00
void **mappings;
struct pci_device *pci_device; /**< libpci handle of the device */
2016-06-14 01:19:17 +02:00
struct vfio_group *group; /**< The VFIO group this device belongs to */
};
struct vfio_container {
int fd;
int version;
int extensions;
2017-02-16 09:11:18 -03:00
uint64_t iova_next; /**< Next free IOVA address */
2016-07-08 13:32:18 +02:00
2016-06-14 01:19:17 +02:00
struct list groups;
};
/** Initialize a new VFIO container. */
int vfio_init(struct vfio_container *c);
/** Initialize a VFIO group and attach it to an existing VFIO container. */
int vfio_group_attach(struct vfio_group *g, struct vfio_container *c, int index);
/** Initialize a VFIO device, lookup the VFIO group it belongs to, create the group if not already existing. */
int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const char *name, int index);
2016-06-14 01:19:17 +02:00
/** Initialie a VFIO-PCI device (uses vfio_device_attach() internally) */
int vfio_pci_attach(struct vfio_device *d, struct vfio_container *c, struct pci_device *pdev);
2016-06-14 01:19:17 +02:00
/** Hot resets a VFIO-PCI device */
int vfio_pci_reset(struct vfio_device *d);
2016-06-14 01:19:17 +02:00
int vfio_pci_msi_init(struct vfio_device *d, int efds[32]);
2016-07-08 13:32:18 +02:00
int vfio_pci_msi_deinit(struct vfio_device *d, int efds[32]);
2016-07-08 13:32:18 +02:00
int vfio_pci_msi_find(struct vfio_device *d, int nos[32]);
2016-06-14 01:19:17 +02:00
/** Enable memory accesses and bus mastering for PCI device */
int vfio_pci_enable(struct vfio_device *d);
2016-06-14 01:19:17 +02:00
/** Reset a VFIO device */
int vfio_device_reset(struct vfio_device *d);
2016-06-14 01:19:17 +02:00
/** Release memory and close container */
int vfio_destroy(struct vfio_container *c);
/** Release memory of group */
2017-02-18 10:45:05 -05:00
int vfio_group_destroy(struct vfio_group *g);
2016-06-14 01:19:17 +02:00
/** Release memory of device */
int vfio_device_destroy(struct vfio_device *g);
2016-06-14 01:19:17 +02:00
/** Print a dump of all attached groups and their devices including regions and IRQs */
void vfio_dump(struct vfio_container *c);
/** Map a device memory region to the application address space (e.g. PCI BARs) */
void * vfio_map_region(struct vfio_device *d, int idx);
2016-06-14 01:19:17 +02:00
2016-07-08 13:32:18 +02:00
/** Map VM to an IOVA, which is accessible by devices in the container */
2017-02-16 09:11:18 -03:00
int vfio_map_dma(struct vfio_container *c, uint64_t virt, uint64_t phys, size_t len);
2016-07-08 13:32:18 +02:00
/** Unmap DMA memory */
2017-02-16 09:11:18 -03:00
int vfio_unmap_dma(struct vfio_container *c, uint64_t virt, uint64_t phys, size_t len);
2016-06-14 01:19:17 +02:00
/** munmap() a region which has been mapped by vfio_map_region() */
int vfio_unmap_region(struct vfio_device *d, int idx);
/** @} */