The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
virtio.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file virtio.h
5 * @brief virtio 1.0 devices on PCI (the "modern" interface).
6 *
7 * the device's registers are in BARs that vendor capabilities point at:
8 * the common configuration, the notify area, the interrupt status and the
9 * device configuration. queues are split virtqueues in one page each.
10 *
11 * a queue is used in one of two ways:
12 * - requests: virtq_call puts one request and one response buffer on a
13 * queue, tells the device and waits (polling) for the answer, and
14 * virtq_post sends without waiting. enough for devices that are asked
15 * one thing at a time, such as the control queue of virtio-gpu.
16 * - buffers: virtq_give gives the device a buffer of the driver's own,
17 * and virtq_take takes back the buffers the device is done with (from
18 * an interrupt). for a stream of packets, such as virtio-net.
19 * a queue is used in one of the two ways only.
20 */
21
22#include "common/stdint.h"
23#include "kernel/memory/phys.h"
24#include "drivers/pci/pci.h"
26
27#define VIRTIO_VENDOR_ID 0x1AF4
28
29// device status bits
30#define VIRTIO_STATUS_ACKNOWLEDGE 1
31#define VIRTIO_STATUS_DRIVER 2
32#define VIRTIO_STATUS_DRIVER_OK 4
33#define VIRTIO_STATUS_FEATURES_OK 8
34#define VIRTIO_STATUS_FAILED 128
35
36// feature bits (the high word is feature select 1)
37#define VIRTIO_F_VERSION_1 32
38
39#define VIRTQ_SIZE 64 // entries per queue (the device may allow more)
40
41typedef struct
42{
44 volatile uint8_t *common; // struct virtio_pci_common_cfg
45 volatile uint8_t *notify; // the notify area
47 volatile uint8_t *isr; // read to clear the interrupt
48 volatile uint8_t *device; // the device's own configuration
50
51typedef struct
52{
54 uint16_t index; // the queue's number
55 uint16_t size; // entries
56 uint8_t *ring; // descriptors, then the available ring, then the used ring
57 uint8_t *buffer; // a page for requests and responses (virtq_call)
58 uint8_t *slots; // a page for requests on the way (virtq_post)
59 volatile uint16_t *note; // where to write to tell the device
60 uint16_t used_seen; // the used ring index the driver has seen
61 uint16_t avail_next; // the available ring index of the next request
63} virtq_t;
64
65/**
66 * find the registers, reset the device and agree on features. the device
67 * stays in that state until virtio_ready, so the driver can set up its
68 * queues first.
69 * @param features the bits the driver wants; VIRTIO_F_VERSION_1 is added.
70 * @return false if this is not a modern virtio device or it refuses.
71 */
72bool virtio_init(virtio_device_t *dev, pci_device_t *pci, uint64_t features);
73
74/**
75 * set up a queue (before virtio_ready).
76 * @return false if the device has no such queue or there is no memory.
77 */
79
80/**
81 * tell the device the driver is ready.
82 */
84
85/**
86 * route configuration change interrupts to MSI-X vector 0 (when the
87 * device uses MSI-X) and no queue interrupts: queues are polled.
88 */
90
91/**
92 * send a request and wait for the response.
93 * @param request request_size bytes the device reads.
94 * @param response response_size bytes the device writes.
95 * @return false if the device did not answer in time. sizes up to half a
96 * page each.
97 */
98bool virtq_call(virtq_t *q, const void *request, uint32_t request_size, void *response,
99 uint32_t response_size);
100
101/**
102 * send a request and do not wait: its response is not read. for a device
103 * that does its requests in order (virtio-gpu 2D). a virtq_call after it
104 * waits until it is done. with many on the way this waits for room.
105 * @param request up to 96 bytes the device reads.
106 * @return false if the device did not make room in time.
107 */
108bool virtq_post(virtq_t *q, const void *request, uint32_t request_size);
109
110/**
111 * send the interrupts of a queue to MSI-X vector 0 (before virtio_ready).
112 * without MSI-X the device raises its one interrupt (INTx or MSI), and
113 * bit 0 of the ISR status says a queue has used buffers.
114 */
116
117/**
118 * give the device a buffer, in descriptor id (each id is one buffer).
119 * @param id 0 to q->size - 1; the buffer comes back from virtq_take with
120 * this id.
121 * @param device_writes true for a buffer the device fills (receive),
122 * false for one it reads (send).
123 * @return false if id is out of range.
124 */
125bool virtq_give(virtq_t *q, uint16_t id, phys_addr_t phys, uint32_t length, bool device_writes);
126
127/**
128 * take back a buffer the device is done with.
129 * @param length the bytes the device wrote into it (0 for a buffer it read).
130 * @return false when there is none.
131 */
132bool virtq_take(virtq_t *q, uint16_t *id, uint32_t *length);
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
unsigned short uint16_t
Definition stdint.h:13
PCI bus enumeration through the legacy configuration ports.
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18
Definition pci.h:47
kernel locking rules:
Definition spinlock.h:67
Definition virtio.h:42
volatile uint8_t * common
Definition virtio.h:44
volatile uint8_t * isr
Definition virtio.h:47
volatile uint8_t * device
Definition virtio.h:48
uint32_t notify_multiplier
Definition virtio.h:46
volatile uint8_t * notify
Definition virtio.h:45
pci_device_t * pci
Definition virtio.h:43
Definition virtio.h:52
spinlock_t lock
Definition virtio.h:62
virtio_device_t * dev
Definition virtio.h:53
uint8_t * slots
Definition virtio.h:58
uint16_t used_seen
Definition virtio.h:60
uint16_t index
Definition virtio.h:54
volatile uint16_t * note
Definition virtio.h:59
uint8_t * ring
Definition virtio.h:56
uint16_t avail_next
Definition virtio.h:61
uint8_t * buffer
Definition virtio.h:57
uint16_t size
Definition virtio.h:55
bool virtio_queue_init(virtio_device_t *dev, virtq_t *q, uint16_t index)
set up a queue (before virtio_ready).
Definition virtio.c:191
bool virtio_init(virtio_device_t *dev, pci_device_t *pci, uint64_t features)
find the registers, reset the device and agree on features.
Definition virtio.c:153
void virtio_queue_interrupt(virtq_t *q)
send the interrupts of a queue to MSI-X vector 0 (before virtio_ready).
Definition virtio.c:364
void virtio_ready(virtio_device_t *dev)
tell the device the driver is ready.
Definition virtio.c:231
bool virtq_take(virtq_t *q, uint16_t *id, uint32_t *length)
take back a buffer the device is done with.
Definition virtio.c:388
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 int...
Definition virtio.c:226
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.
Definition virtio.c:279
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).
Definition virtio.c:374
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.
Definition virtio.c:319