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

virtio 1.0 on PCI: registers, features, split virtqueues (see virtio.h). More...

#include "virtio.h"
#include "common/memory.h"
#include "kernel/memory/mmio.h"
#include "kernel/memory/page.h"
#include "kernel/memory/phys.h"
#include "kernel/memory/pmm.h"

Data Structures

struct  __attribute__
 ELF32 file header. More...

Macros

#define CAP_VENDOR   0x09
#define CFG_COMMON   1
#define CFG_NOTIFY   2
#define CFG_ISR   3
#define CFG_DEVICE   4
#define DEVICE_FEATURE_SELECT   0
#define DEVICE_FEATURE   4
#define DRIVER_FEATURE_SELECT   8
#define DRIVER_FEATURE   12
#define MSIX_CONFIG   16
#define DEVICE_STATUS   20
#define QUEUE_SELECT   22
#define QUEUE_SIZE   24
#define QUEUE_MSIX_VECTOR   26
#define QUEUE_ENABLE   28
#define QUEUE_NOTIFY_OFF   30
#define QUEUE_DESC   32
#define QUEUE_DRIVER   40
#define QUEUE_DEVICE   48
#define NO_VECTOR   0xFFFF
#define DESC_NEXT   1
#define DESC_WRITE   2
#define WAIT_LOOPS   50000000u
#define SLOTS   16
#define SLOT_SIZE   128
#define RESPONSE_AT   96
#define AVAIL_OFFSET   (VIRTQ_SIZE * sizeof(desc_t))
#define USED_OFFSET   2048

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_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.
void virtio_ready (virtio_device_t *dev)
 tell the device the driver is ready.
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.
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.
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 on PCI: registers, features, split virtqueues (see virtio.h).

Macro Definition Documentation

◆ CAP_VENDOR

#define CAP_VENDOR   0x09

◆ CFG_COMMON

#define CFG_COMMON   1

◆ CFG_NOTIFY

#define CFG_NOTIFY   2

◆ CFG_ISR

#define CFG_ISR   3

◆ CFG_DEVICE

#define CFG_DEVICE   4

◆ DEVICE_FEATURE_SELECT

#define DEVICE_FEATURE_SELECT   0

◆ DEVICE_FEATURE

#define DEVICE_FEATURE   4

◆ DRIVER_FEATURE_SELECT

#define DRIVER_FEATURE_SELECT   8

◆ DRIVER_FEATURE

#define DRIVER_FEATURE   12

◆ MSIX_CONFIG

#define MSIX_CONFIG   16

◆ DEVICE_STATUS

#define DEVICE_STATUS   20

◆ QUEUE_SELECT

#define QUEUE_SELECT   22

◆ QUEUE_SIZE

#define QUEUE_SIZE   24

◆ QUEUE_MSIX_VECTOR

#define QUEUE_MSIX_VECTOR   26

◆ QUEUE_ENABLE

#define QUEUE_ENABLE   28

◆ QUEUE_NOTIFY_OFF

#define QUEUE_NOTIFY_OFF   30

◆ QUEUE_DESC

#define QUEUE_DESC   32

◆ QUEUE_DRIVER

#define QUEUE_DRIVER   40

◆ QUEUE_DEVICE

#define QUEUE_DEVICE   48

◆ NO_VECTOR

#define NO_VECTOR   0xFFFF

◆ DESC_NEXT

#define DESC_NEXT   1

◆ DESC_WRITE

#define DESC_WRITE   2

◆ WAIT_LOOPS

#define WAIT_LOOPS   50000000u

◆ SLOTS

#define SLOTS   16

◆ SLOT_SIZE

#define SLOT_SIZE   128

◆ RESPONSE_AT

#define RESPONSE_AT   96

◆ AVAIL_OFFSET

#define AVAIL_OFFSET   (VIRTQ_SIZE * sizeof(desc_t))

◆ USED_OFFSET

#define USED_OFFSET   2048

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_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.

◆ virtio_ready()

void virtio_ready ( virtio_device_t * dev)

tell the device the driver is ready.

◆ 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.

◆ 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.

◆ 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.