8. VFIO Platform Library

Platform devices in Linux refer to System-on-Chip (SoC) components that aren’t situated on standard buses such as PCI or USB. You can see them in Linux at the path /sys/bus/platform/devices/. To interact with platform devices from user space, the vfio-platform driver provides a framework. This library provides DAO APIs built upon this framework, enabling access to the device resources.

8.1. Prerequisites:

To make use of VFIO platform framework, the vfio-platform module must be loaded first:

sudo modprobe vfio-platform

Note

By default vfio-platform assumes that platform device has dedicated reset driver. If such driver is missing or device does not require one, this option can be turned off by setting reset_required=0 module parameter.

Afterwards, the platform device needs to be bound to vfio-platform, following a standard two-step procedure. Initially, the driver_override, located within the platform device directory, must be configured to vfio-platform:

echo vfio-platform | sudo tee /sys/bus/platform/devices/DEV/driver_override

Next DEV device must be bound to vfio-platform driver:

echo DEV | sudo tee /sys/bus/platform/drivers/vfio-platform/bind

8.2. Platform device initialization

Invoking the dao_vfio_platform_init() API creates a VFIO container by opening the /dev/vfio/vfio character device and initializes the memory used for storing the details of platform devices. This API should be invoked only once to initiate the library.

int dao_vfio_platform_init(void);

After initializing the library, the dao_vfio_platform_device_setup() API can be used to initialize a platform device. The function takes the memory for storing platform device details, specified by the struct dao_vfio_platform_device argument. Upon successful execution, the resources of the platform devices are mapped, and the device structure is populated.

int dao_vfio_platform_device_setup(const char *dev_name, struct dao_vfio_platform_device *pdev);
struct dao_vfio_mem_resouce {
	uint8_t *addr; /**< Mapped virtual address. */
	uint64_t len;  /**< Length of the resource. */
};

/** VFIO platform device */
struct dao_vfio_platform_device {
	char name[VFIO_DEV_NAME_MAX_LEN]; /**< Device name */
	int device_fd;                    /**< VFIO device fd */
	int group_fd;                     /**< VFIO group fd */
	unsigned int num_resource;        /**< Number of device resources */
	struct dao_vfio_mem_resouce *mem; /**< Device resources */
};

8.3. Platform device cleanup

dao_vfio_platform_device_free() releases the VFIO platform device and frees the associated memory.

void dao_vfio_platform_device_free(struct dao_vfio_platform_device *pdev);

Upon closing all open devices, the container can be shut down by calling dao_vfio_platform_fini().

void dao_vfio_platform_fini(void);