I'm trying to map an FPGA on the PCI-e bus using the vfio-pci driver. I can read registers correctly but, when I try to write registers, it seems that a register is written and I can read it back, but, when I stop the process I use to read/write and restart it, I find that the register has rolled back to the original value. If I map the FPGA using uio-pci everything works fine.
This is the script to load the vfio driver:
modprobe vfio-pci enable_sriov=1 disable_idle_d3=1
VID="10ee"
DID="903f"
DBDF="0000:"`lspci -n | grep -E ${VID}:${DID} | cut -d ' ' -f1`
echo ${DBDF}
ROOT_DBDF="0000:3a:00.0"
readlink /sys/bus/pci/devices/${DBDF}/iommu_group
GROUP=`readlink /sys/bus/pci/devices/${DBDF}/iommu_group | rev | cut -d '/' -f1 | rev`
echo "GROUP " ${GROUP}
# unbind the ROOT device makes the group viable
echo ${ROOT_DBDF} > /sys/bus/pci/devices/${ROOT_DBDF}/driver/unbind; sleep 1
echo ${DBDF} > /sys/bus/pci/devices/${DBDF}/driver/unbind; sleep 1
echo ${VID} ${DID} > /sys/bus/pci/drivers/vfio-pci/remove_id; sleep 1
echo ${VID} ${DID} > /sys/bus/pci/drivers/vfio-pci/new_id; sleep 1
echo 8086 2030 > /sys/bus/pci/drivers/vfio-pci/new_id; sleep 1
ls -l /sys/bus/pci/devices/${DBDF}/iommu_group/devices; sleep 1
chmod 660 /dev/vfio/vfio
Then I use the following program to read and write registers.
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/vfio.h>
int main(int argc, char** argv)
{
int container, group, device, i;
struct vfio_group_status group_status =
{ .argsz = sizeof(group_status) };
struct vfio_iommu_type1_info iommu_info = { .argsz = sizeof(iommu_info) };
struct vfio_iommu_type1_dma_map dma_map = { .argsz = sizeof(dma_map) };
struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
/* Create a new container */
container = open("/dev/vfio/vfio", O_RDWR);
if (ioctl(container, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
printf("Unknown API version\n");
/* Unknown API version */
if (!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU))
printf("Doesn't support IOMMU driver we want\n");
/* Open the group */
group = open("/dev/vfio/69", O_RDWR);
/* Test the group is viable and available */
ioctl(group, VFIO_GROUP_GET_STATUS, &group_status);
if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
printf("Group is not viable\n");
/* Group is not viable (ie, not all devices bound for vfio) */
/* Add the group to the container */
ioctl(group, VFIO_GROUP_SET_CONTAINER, &container);
/* Enable the IOMMU model we want */
ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
/* Get addition IOMMU info */
ioctl(container, VFIO_IOMMU_GET_INFO, &iommu_info);
/* Allocate some space and setup a DMA mapping */
/*
dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
dma_map.size = 1024 * 1024;
dma_map.iova = 0;
dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map);
*/
/* Get a file descriptor for the device */
device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:3b:00.0");
/* Test and setup the device */
ioctl(device, VFIO_DEVICE_GET_INFO, &device_info);
printf("NUM REGIONS %d\n", device_info.num_regions);
struct vfio_region_info regs[64];
for (i = 0; i < device_info.num_regions; i++) {
regs[i].argsz = sizeof(struct vfio_region_info);
regs[i].index = i;
ioctl(device, VFIO_DEVICE_GET_REGION_INFO, ®s[i]);
printf("region %d flags %08x offset %lld size %lld\n", i, regs[i].flags, regs[i].offset, regs[i].size);
/* Setup mappings... read/write offsets, mmaps
* For PCI devices, config space is a region */
}
volatile uint8_t* ptr = mmap(0, regs[0].size, PROT_READ | PROT_WRITE, MAP_SHARED, device, 0);
printf("addr %p\n", ptr);
printf("reg 0x38000 %08x\n", *(uint32_t*)(ptr + 0x38000));
{
uint32_t ival = *(volatile uint32_t*) (ptr + 0x38008);
*(volatile uint32_t*) (ptr + 0x38008) = ival + 0x1000;
printf("%08x\n", *(volatile uint32_t*) (ptr + 0x38008));
}
printf("reg 0x38008 %08x\n", *(volatile uint32_t*)(ptr + 0x38008));
}
MAP_SHARED is used in the mmap call but the program behaves like MAP_PRIVATE had been used