OpenAMP Library  353
virtio.h File Reference
#include <openamp/virtqueue.h>
#include <metal/errno.h>
#include <metal/spinlock.h>

Go to the source code of this file.

Data Structures

struct  virtio_device_id
 Virtio device identifier. More...
 
struct  virtio_feature_desc
 Device features. More...
 
struct  virtio_vring_info
 Virtio vring data structure. More...
 
struct  virtio_device
 Structure definition for virtio devices for use by the applications/drivers. More...
 
struct  virtio_dispatch
 Virtio device dispatcher functions. More...
 
struct  virtio_memory_ops
 

Macros

#define VIRTIO_ID_NETWORK   1UL
 
#define VIRTIO_ID_BLOCK   2UL
 
#define VIRTIO_ID_CONSOLE   3UL
 
#define VIRTIO_ID_ENTROPY   4UL
 
#define VIRTIO_ID_BALLOON   5UL
 
#define VIRTIO_ID_IOMEMORY   6UL
 
#define VIRTIO_ID_RPMSG   7UL /* remote processor messaging */
 
#define VIRTIO_ID_SCSI   8UL
 
#define VIRTIO_ID_9P   9UL
 
#define VIRTIO_ID_MAC80211_WLAN   10UL
 
#define VIRTIO_ID_RPROC_SERIAL   11UL
 
#define VIRTIO_ID_CAIF   12UL
 
#define VIRTIO_ID_MEMORY_BALLOON   13UL
 
#define VIRTIO_ID_GPU   16UL
 
#define VIRTIO_ID_CLOCK   17UL
 
#define VIRTIO_ID_INPUT   18UL
 
#define VIRTIO_ID_VSOCK   19UL
 
#define VIRTIO_ID_CRYPTO   20UL
 
#define VIRTIO_ID_SIGNAL_DIST   21UL
 
#define VIRTIO_ID_PSTORE   22UL
 
#define VIRTIO_ID_IOMMU   23UL
 
#define VIRTIO_ID_MEM   24UL
 
#define VIRTIO_ID_SOUND   25UL
 
#define VIRTIO_ID_FS   26UL
 
#define VIRTIO_ID_PMEM   27UL
 
#define VIRTIO_ID_RPMB   28UL
 
#define VIRTIO_ID_MAC80211_HWSIM   29UL
 
#define VIRTIO_ID_VIDEO_ENCODER   30UL
 
#define VIRTIO_ID_VIDEO_DECODER   31UL
 
#define VIRTIO_ID_SCMI   32UL
 
#define VIRTIO_ID_NITRO_SEC_MOD   33UL
 
#define VIRTIO_ID_I2C_ADAPTER   34UL
 
#define VIRTIO_ID_WATCHDOG   35UL
 
#define VIRTIO_ID_CAN   36UL
 
#define VIRTIO_ID_PARAM_SERV   38UL
 
#define VIRTIO_ID_AUDIO_POLICY   39UL
 
#define VIRTIO_ID_BT   40UL
 
#define VIRTIO_ID_GPIO   41UL
 
#define VIRTIO_ID_RDMA   42UL
 
#define VIRTIO_DEV_ANY_ID   -1UL
 
#define VIRTIO_CONFIG_STATUS_RESET   0x00
 
#define VIRTIO_CONFIG_STATUS_ACK   0x01
 
#define VIRTIO_CONFIG_STATUS_DRIVER   0x02
 
#define VIRTIO_CONFIG_STATUS_DRIVER_OK   0x04
 
#define VIRTIO_CONFIG_FEATURES_OK   0x08
 
#define VIRTIO_CONFIG_STATUS_NEEDS_RESET   0x40
 
#define VIRTIO_CONFIG_STATUS_FAILED   0x80
 
#define VIRTIO_DEV_DRIVER   0UL
 
#define VIRTIO_DEV_DEVICE   1UL
 
#define VIRTIO_ENABLED(option)   (option == 1)
 
#define VIRTIO_ROLE_IS_DRIVER(vdev)   ((vdev)->role == VIRTIO_DEV_DRIVER)
 
#define VIRTIO_ROLE_IS_DEVICE(vdev)   ((vdev)->role == VIRTIO_DEV_DEVICE)
 
#define VIRTIO_F_NOTIFY_ON_EMPTY   (1 << 24)
 
#define VIRTIO_F_ANY_LAYOUT   (1 << 27)
 
#define VIRTIO_F_BAD_FEATURE   (1 << 30)
 
#define VIRTIO_TRANSPORT_F_START   28
 
#define VIRTIO_TRANSPORT_F_END   32
 
#define VIRTIO_ASSERT(_exp, _msg)   metal_assert(_exp)
 
#define VIRTIO_MMIO_VRING_ALIGNMENT   4096
 

Typedefs

typedef void(* virtio_dev_reset_cb) (struct virtio_device *vdev)
 

Functions

int virtio_create_virtqueues (struct virtio_device *vdev, unsigned int flags, unsigned int nvqs, const char *names[], vq_callback callbacks[], void *callback_args[])
 Create the virtio device virtqueue. More...
 
static void virtio_delete_virtqueues (struct virtio_device *vdev)
 Delete the virtio device virtqueue. More...
 
static uint32_t virtio_get_devid (const struct virtio_device *vdev)
 Get device ID. More...
 
static int virtio_get_status (struct virtio_device *vdev, uint8_t *status)
 Retrieve device status. More...
 
static int virtio_set_status (struct virtio_device *vdev, uint8_t status)
 Set device status. More...
 
static int virtio_read_config (struct virtio_device *vdev, uint32_t offset, void *dst, int len)
 Retrieve configuration data from the device. More...
 
static int virtio_write_config (struct virtio_device *vdev, uint32_t offset, void *src, int len)
 Write configuration data to the device. More...
 
static int virtio_get_features (struct virtio_device *vdev, uint32_t *features)
 Get the virtio device features. More...
 
static int virtio_set_features (struct virtio_device *vdev, uint32_t features)
 Set features supported by the VIRTIO driver. More...
 
static int virtio_negotiate_features (struct virtio_device *vdev, uint32_t features, uint32_t *final_features)
 Negotiate features between virtio device and driver. More...
 
static int virtio_reset_device (struct virtio_device *vdev)
 Reset virtio device. More...
 
static int virtio_alloc_buf (struct virtio_device *vdev, void **buf, size_t size, size_t align)
 Allocate buffer from the virtio device. More...
 
static int virtio_free_buf (struct virtio_device *vdev, void *buf)
 Free the buffer allocated by virtio_alloc_buf from the virtio device. More...
 
static bool virtio_has_feature (struct virtio_device *vdev, unsigned int feature_bit)
 Check if the virtio device support a specific feature. More...
 

Macro Definition Documentation

◆ VIRTIO_ASSERT

#define VIRTIO_ASSERT (   _exp,
  _msg 
)    metal_assert(_exp)

◆ VIRTIO_CONFIG_FEATURES_OK

#define VIRTIO_CONFIG_FEATURES_OK   0x08

◆ VIRTIO_CONFIG_STATUS_ACK

#define VIRTIO_CONFIG_STATUS_ACK   0x01

◆ VIRTIO_CONFIG_STATUS_DRIVER

#define VIRTIO_CONFIG_STATUS_DRIVER   0x02

◆ VIRTIO_CONFIG_STATUS_DRIVER_OK

#define VIRTIO_CONFIG_STATUS_DRIVER_OK   0x04

◆ VIRTIO_CONFIG_STATUS_FAILED

#define VIRTIO_CONFIG_STATUS_FAILED   0x80

◆ VIRTIO_CONFIG_STATUS_NEEDS_RESET

#define VIRTIO_CONFIG_STATUS_NEEDS_RESET   0x40

◆ VIRTIO_CONFIG_STATUS_RESET

#define VIRTIO_CONFIG_STATUS_RESET   0x00

◆ VIRTIO_DEV_ANY_ID

#define VIRTIO_DEV_ANY_ID   -1UL

◆ VIRTIO_DEV_DEVICE

#define VIRTIO_DEV_DEVICE   1UL

◆ VIRTIO_DEV_DRIVER

#define VIRTIO_DEV_DRIVER   0UL

◆ VIRTIO_ENABLED

#define VIRTIO_ENABLED (   option)    (option == 1)

◆ VIRTIO_F_ANY_LAYOUT

#define VIRTIO_F_ANY_LAYOUT   (1 << 27)

◆ VIRTIO_F_BAD_FEATURE

#define VIRTIO_F_BAD_FEATURE   (1 << 30)

◆ VIRTIO_F_NOTIFY_ON_EMPTY

#define VIRTIO_F_NOTIFY_ON_EMPTY   (1 << 24)

◆ VIRTIO_ID_9P

#define VIRTIO_ID_9P   9UL

◆ VIRTIO_ID_AUDIO_POLICY

#define VIRTIO_ID_AUDIO_POLICY   39UL

◆ VIRTIO_ID_BALLOON

#define VIRTIO_ID_BALLOON   5UL

◆ VIRTIO_ID_BLOCK

#define VIRTIO_ID_BLOCK   2UL

◆ VIRTIO_ID_BT

#define VIRTIO_ID_BT   40UL

◆ VIRTIO_ID_CAIF

#define VIRTIO_ID_CAIF   12UL

◆ VIRTIO_ID_CAN

#define VIRTIO_ID_CAN   36UL

◆ VIRTIO_ID_CLOCK

#define VIRTIO_ID_CLOCK   17UL

◆ VIRTIO_ID_CONSOLE

#define VIRTIO_ID_CONSOLE   3UL

◆ VIRTIO_ID_CRYPTO

#define VIRTIO_ID_CRYPTO   20UL

◆ VIRTIO_ID_ENTROPY

#define VIRTIO_ID_ENTROPY   4UL

◆ VIRTIO_ID_FS

#define VIRTIO_ID_FS   26UL

◆ VIRTIO_ID_GPIO

#define VIRTIO_ID_GPIO   41UL

◆ VIRTIO_ID_GPU

#define VIRTIO_ID_GPU   16UL

◆ VIRTIO_ID_I2C_ADAPTER

#define VIRTIO_ID_I2C_ADAPTER   34UL

◆ VIRTIO_ID_INPUT

#define VIRTIO_ID_INPUT   18UL

◆ VIRTIO_ID_IOMEMORY

#define VIRTIO_ID_IOMEMORY   6UL

◆ VIRTIO_ID_IOMMU

#define VIRTIO_ID_IOMMU   23UL

◆ VIRTIO_ID_MAC80211_HWSIM

#define VIRTIO_ID_MAC80211_HWSIM   29UL

◆ VIRTIO_ID_MAC80211_WLAN

#define VIRTIO_ID_MAC80211_WLAN   10UL

◆ VIRTIO_ID_MEM

#define VIRTIO_ID_MEM   24UL

◆ VIRTIO_ID_MEMORY_BALLOON

#define VIRTIO_ID_MEMORY_BALLOON   13UL

◆ VIRTIO_ID_NETWORK

#define VIRTIO_ID_NETWORK   1UL

◆ VIRTIO_ID_NITRO_SEC_MOD

#define VIRTIO_ID_NITRO_SEC_MOD   33UL

◆ VIRTIO_ID_PARAM_SERV

#define VIRTIO_ID_PARAM_SERV   38UL

◆ VIRTIO_ID_PMEM

#define VIRTIO_ID_PMEM   27UL

◆ VIRTIO_ID_PSTORE

#define VIRTIO_ID_PSTORE   22UL

◆ VIRTIO_ID_RDMA

#define VIRTIO_ID_RDMA   42UL

◆ VIRTIO_ID_RPMB

#define VIRTIO_ID_RPMB   28UL

◆ VIRTIO_ID_RPMSG

#define VIRTIO_ID_RPMSG   7UL /* remote processor messaging */

◆ VIRTIO_ID_RPROC_SERIAL

#define VIRTIO_ID_RPROC_SERIAL   11UL

◆ VIRTIO_ID_SCMI

#define VIRTIO_ID_SCMI   32UL

◆ VIRTIO_ID_SCSI

#define VIRTIO_ID_SCSI   8UL

◆ VIRTIO_ID_SIGNAL_DIST

#define VIRTIO_ID_SIGNAL_DIST   21UL

◆ VIRTIO_ID_SOUND

#define VIRTIO_ID_SOUND   25UL

◆ VIRTIO_ID_VIDEO_DECODER

#define VIRTIO_ID_VIDEO_DECODER   31UL

◆ VIRTIO_ID_VIDEO_ENCODER

#define VIRTIO_ID_VIDEO_ENCODER   30UL

◆ VIRTIO_ID_VSOCK

#define VIRTIO_ID_VSOCK   19UL

◆ VIRTIO_ID_WATCHDOG

#define VIRTIO_ID_WATCHDOG   35UL

◆ VIRTIO_MMIO_VRING_ALIGNMENT

#define VIRTIO_MMIO_VRING_ALIGNMENT   4096

◆ VIRTIO_ROLE_IS_DEVICE

#define VIRTIO_ROLE_IS_DEVICE (   vdev)    ((vdev)->role == VIRTIO_DEV_DEVICE)

◆ VIRTIO_ROLE_IS_DRIVER

#define VIRTIO_ROLE_IS_DRIVER (   vdev)    ((vdev)->role == VIRTIO_DEV_DRIVER)

◆ VIRTIO_TRANSPORT_F_END

#define VIRTIO_TRANSPORT_F_END   32

◆ VIRTIO_TRANSPORT_F_START

#define VIRTIO_TRANSPORT_F_START   28

Typedef Documentation

◆ virtio_dev_reset_cb

typedef void(* virtio_dev_reset_cb) (struct virtio_device *vdev)

Function Documentation

◆ virtio_alloc_buf()

static int virtio_alloc_buf ( struct virtio_device vdev,
void **  buf,
size_t  size,
size_t  align 
)
inlinestatic

Allocate buffer from the virtio device.

Parameters
vdevPointer to virtio device structure.
bufPointer to the allocated buffer (virtual address).
sizeAllocated buffer size.
alignAllocated buffer alignment.
Returns
0 on success, otherwise error code.

◆ virtio_create_virtqueues()

int virtio_create_virtqueues ( struct virtio_device vdev,
unsigned int  flags,
unsigned int  nvqs,
const char *  names[],
vq_callback  callbacks[],
void *  callback_args[] 
)

Create the virtio device virtqueue.

Parameters
vdevPointer to virtio device structure.
flagsCreate flag.
nvqsThe virtqueue number.
namesVirtqueue names.
callbacksVirtqueue callback functions.
callback_argsVirtqueue callback function arguments.
Returns
0 on success, otherwise error code.

◆ virtio_delete_virtqueues()

static void virtio_delete_virtqueues ( struct virtio_device vdev)
inlinestatic

Delete the virtio device virtqueue.

Parameters
vdevPointer to virtio device structure.

◆ virtio_free_buf()

static int virtio_free_buf ( struct virtio_device vdev,
void *  buf 
)
inlinestatic

Free the buffer allocated by virtio_alloc_buf from the virtio device.

Parameters
vdevPointer to virtio device structure.
bufBuffer need to be freed.
Returns
0 on success, otherwise error code.

◆ virtio_get_devid()

static uint32_t virtio_get_devid ( const struct virtio_device vdev)
inlinestatic

Get device ID.

Parameters
vdevPointer to device structure.
Returns
Device ID value.

◆ virtio_get_features()

static int virtio_get_features ( struct virtio_device vdev,
uint32_t *  features 
)
inlinestatic

Get the virtio device features.

Parameters
vdevPointer to device structure.
featuresPointer to features supported by both the driver and the device as a bitfield.
Returns
0 on success, otherwise error code.

◆ virtio_get_status()

static int virtio_get_status ( struct virtio_device vdev,
uint8_t *  status 
)
inlinestatic

Retrieve device status.

Parameters
vdevPointer to device structure.
statusPointer to the virtio device status.
Returns
0 on success, otherwise error code.

◆ virtio_has_feature()

static bool virtio_has_feature ( struct virtio_device vdev,
unsigned int  feature_bit 
)
inlinestatic

Check if the virtio device support a specific feature.

Parameters
vdevPointer to device structure.
feature_bitFeature bit to check.
Returns
true if the feature is supported, otherwise false.

◆ virtio_negotiate_features()

static int virtio_negotiate_features ( struct virtio_device vdev,
uint32_t  features,
uint32_t *  final_features 
)
inlinestatic

Negotiate features between virtio device and driver.

Parameters
vdevPointer to device structure.
featuresSupported features.
final_featuresPointer to the final features after negotiate.
Returns
0 on success, otherwise error code.

◆ virtio_read_config()

static int virtio_read_config ( struct virtio_device vdev,
uint32_t  offset,
void *  dst,
int  len 
)
inlinestatic

Retrieve configuration data from the device.

Parameters
vdevPointer to device structure.
offsetOffset of the data within the configuration area.
dstAddress of the buffer that will hold the data.
lenLength of the data to be retrieved.
Returns
0 on success, otherwise error code.

◆ virtio_reset_device()

static int virtio_reset_device ( struct virtio_device vdev)
inlinestatic

Reset virtio device.

Parameters
vdevPointer to virtio_device structure.
Returns
0 on success, otherwise error code.

◆ virtio_set_features()

static int virtio_set_features ( struct virtio_device vdev,
uint32_t  features 
)
inlinestatic

Set features supported by the VIRTIO driver.

Parameters
vdevPointer to device structure.
featuresFeatures supported by the driver as a bitfield.
Returns
0 on success, otherwise error code.

◆ virtio_set_status()

static int virtio_set_status ( struct virtio_device vdev,
uint8_t  status 
)
inlinestatic

Set device status.

Parameters
vdevPointer to device structure.
statusValue to be set as device status.
Returns
0 on success, otherwise error code.

◆ virtio_write_config()

static int virtio_write_config ( struct virtio_device vdev,
uint32_t  offset,
void *  src,
int  len 
)
inlinestatic

Write configuration data to the device.

Parameters
vdevPointer to device structure.
offsetOffset of the data within the configuration area.
srcAddress of the buffer that holds the data to write.
lenLength of the data to be written.
Returns
0 on success, otherwise error code.