The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
virtio.h File Reference

virtio 1.0 devices on PCI (the "modern" interface). More...

Go to the source code of this file.

Data Structures

struct  virtio_device_t
struct  virtq_t

Macros

#define VIRTIO_VENDOR_ID   0x1AF4
#define VIRTIO_STATUS_ACKNOWLEDGE   1
#define VIRTIO_STATUS_DRIVER   2
#define VIRTIO_STATUS_DRIVER_OK   4
#define VIRTIO_STATUS_FEATURES_OK   8
#define VIRTIO_STATUS_FAILED   128
#define VIRTIO_F_VERSION_1   32
#define VIRTQ_SIZE   64

Functions

bool virtio_init (virtio_device_t *dev, pci_device_t *pci, uint64_t features)
 find the registers, reset the device and agree on features.
bool virtio_queue_init (virtio_device_t *dev, virtq_t *q, uint16_t index)
 set up a queue (before virtio_ready).
void virtio_ready (virtio_device_t *dev)
 tell the device the driver is ready.
void virtio_use_config_vector (virtio_device_t *dev)
 route configuration change interrupts to MSI-X vector 0 (when the device uses MSI-X) and no queue interrupts: queues are polled.
bool virtq_call (virtq_t *q, const void *request, uint32_t request_size, void *response, uint32_t response_size)
 send a request and wait for the response.
bool virtq_post (virtq_t *q, const void *request, uint32_t request_size)
 send a request and do not wait: its response is not read.
void virtio_queue_interrupt (virtq_t *q)
 send the interrupts of a queue to MSI-X vector 0 (before virtio_ready).
bool virtq_give (virtq_t *q, uint16_t id, phys_addr_t phys, uint32_t length, bool device_writes)
 give the device a buffer, in descriptor id (each id is one buffer).
bool virtq_take (virtq_t *q, uint16_t *id, uint32_t *length)
 take back a buffer the device is done with.

Detailed Description

virtio 1.0 devices on PCI (the "modern" interface).

the device's registers are in BARs that vendor capabilities point at: the common configuration, the notify area, the interrupt status and the device configuration. queues are split virtqueues in one page each.

a queue is used in one of two ways:

  • requests: virtq_call puts one request and one response buffer on a queue, tells the device and waits (polling) for the answer, and virtq_post sends without waiting. enough for devices that are asked one thing at a time, such as the control queue of virtio-gpu.
  • buffers: virtq_give gives the device a buffer of the driver's own, and virtq_take takes back the buffers the device is done with (from an interrupt). for a stream of packets, such as virtio-net. a queue is used in one of the two ways only.

Macro Definition Documentation

◆ VIRTIO_VENDOR_ID

#define VIRTIO_VENDOR_ID   0x1AF4

◆ VIRTIO_STATUS_ACKNOWLEDGE

#define VIRTIO_STATUS_ACKNOWLEDGE   1

◆ VIRTIO_STATUS_DRIVER

#define VIRTIO_STATUS_DRIVER   2

◆ VIRTIO_STATUS_DRIVER_OK

#define VIRTIO_STATUS_DRIVER_OK   4

◆ VIRTIO_STATUS_FEATURES_OK

#define VIRTIO_STATUS_FEATURES_OK   8

◆ VIRTIO_STATUS_FAILED

#define VIRTIO_STATUS_FAILED   128

◆ VIRTIO_F_VERSION_1

#define VIRTIO_F_VERSION_1   32

◆ VIRTQ_SIZE

#define VIRTQ_SIZE   64

Function Documentation

◆ virtio_init()

bool virtio_init ( virtio_device_t * dev,
pci_device_t * pci,
uint64_t features )

find the registers, reset the device and agree on features.

the device stays in that state until virtio_ready, so the driver can set up its queues first.

Parameters
featuresthe bits the driver wants; VIRTIO_F_VERSION_1 is added.
Returns
false if this is not a modern virtio device or it refuses.

◆ virtio_queue_init()

bool virtio_queue_init ( virtio_device_t * dev,
virtq_t * q,
uint16_t index )

set up a queue (before virtio_ready).

Returns
false if the device has no such queue or there is no memory.

◆ virtio_ready()

void virtio_ready ( virtio_device_t * dev)

tell the device the driver is ready.

◆ virtio_use_config_vector()

void virtio_use_config_vector ( virtio_device_t * dev)

route configuration change interrupts to MSI-X vector 0 (when the device uses MSI-X) and no queue interrupts: queues are polled.

◆ virtq_call()

bool virtq_call ( virtq_t * q,
const void * request,
uint32_t request_size,
void * response,
uint32_t response_size )

send a request and wait for the response.

Parameters
requestrequest_size bytes the device reads.
responseresponse_size bytes the device writes.
Returns
false if the device did not answer in time. sizes up to half a page each.

◆ virtq_post()

bool virtq_post ( virtq_t * q,
const void * request,
uint32_t request_size )

send a request and do not wait: its response is not read.

for a device that does its requests in order (virtio-gpu 2D). a virtq_call after it waits until it is done. with many on the way this waits for room.

Parameters
requestup to 96 bytes the device reads.
Returns
false if the device did not make room in time.

◆ virtio_queue_interrupt()

void virtio_queue_interrupt ( virtq_t * q)

send the interrupts of a queue to MSI-X vector 0 (before virtio_ready).

without MSI-X the device raises its one interrupt (INTx or MSI), and bit 0 of the ISR status says a queue has used buffers.

◆ virtq_give()

bool virtq_give ( virtq_t * q,
uint16_t id,
phys_addr_t phys,
uint32_t length,
bool device_writes )

give the device a buffer, in descriptor id (each id is one buffer).

Parameters
id0 to q->size - 1; the buffer comes back from virtq_take with this id.
device_writestrue for a buffer the device fills (receive), false for one it reads (send).
Returns
false if id is out of range.

◆ virtq_take()

bool virtq_take ( virtq_t * q,
uint16_t * id,
uint32_t * length )

take back a buffer the device is done with.

Parameters
lengththe bytes the device wrote into it (0 for a buffer it read).
Returns
false when there is none.