interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / virtio_q.h

86 lines · 3701 bytes Raw Blame History
1/* virtio_q.h -- a small polled virtio 1.x device core for request/response
2 * devices (virtio-gpu, virtio-sound): reset and feature handshake, split
3 * virtqueues, and chained requests (a driver-written part, then a
4 * device-written part). Written from the virtio 1.x specification sections
5 * 2.6 (split virtqueues), 3.1 (device initialization) and 4.1.4 (PCI
6 * capabilities); register access goes through the same vnet_ops windows as
7 * the virtio-net core (net/aienos_virtio_net.h), whose common-configuration
8 * offsets it reuses. No heap, no clock, no interrupts. All rings live in one
9 * caller region (mem, len) the device sees at bus address dma; request
10 * buffers are bus addresses the caller owns and has checked. The device is
11 * untrusted: a used id out of range or not outstanding, or a used index
12 * running ahead of what was posted, marks it broken (sticky). Features:
13 * VERSION_1 required, ACCESS_PLATFORM accepted when offered (and required
14 * with VQ_INIT_REQUIRE_ACCESS_PLATFORM), every other feature declined. */
15#ifndef AIENOS_CK_VIRTIO_Q_H
16#define AIENOS_CK_VIRTIO_Q_H
17#include <stddef.h>
18#include <stdint.h>
19#include "aienos_virtio_net.h"
20
21#define VQ_MAX_QUEUES 4u
22#define VQ_SIZE 16u /* descriptors a queue uses (the device may offer more) */
23#define VQ_INIT_REQUIRE_ACCESS_PLATFORM 1u
24
25enum {
26 VQ_OK = 0,
27 VQ_E_ARG = 1,
28 VQ_E_MEM = 2,
29 VQ_E_RESET = 3,
30 VQ_E_NO_VERSION_1 = 4,
31 VQ_E_FEATURES = 5,
32 VQ_E_QUEUES = 6,
33 VQ_E_FULL = 7,
34 VQ_E_DEVICE = 8, /* hostile or broken completion; the device is now broken */
35 VQ_E_TIMEOUT = 9, /* no completion within the spin bound */
36 VQ_E_NO_ACCESS_PLATFORM = 10,
37};
38
39typedef struct {
40 uint64_t addr; /* bus address */
41 uint32_t len;
42 uint32_t device_writes; /* 1: the device writes this part (a response) */
43} vq_buf;
44
45typedef struct {
46 uint16_t size, avail_idx, last_used, in_flight;
47 uint32_t notify_off;
48 size_t desc_off, avail_off, used_off;
49 uint8_t posted[VQ_SIZE]; /* head ids outstanding */
50 uint16_t free_next; /* descriptors are handed out in rounds of a chain */
51} vq_queue;
52
53typedef struct {
54 vnet_ops ops;
55 uint8_t *mem;
56 uint64_t dma;
57 size_t len;
58 unsigned nq;
59 uint32_t notify_mult;
60 uint64_t features;
61 int broken;
62 vq_queue q[VQ_MAX_QUEUES];
63} vq_dev;
64
65/* Bytes of ring memory nq queues need (4 KiB aligned queues). */
66size_t vq_mem_bytes(unsigned nq);
67/* Resets the device and brings up queues 0..nq-1. mem must be 4 KiB aligned
68 * and zeroed. The device stays without DRIVER_OK until vq_ready. */
69int vq_init(vq_dev *d, const vnet_ops *ops, const virtio_pci_caps *caps, uint8_t *mem, uint64_t dma, size_t len,
70 unsigned nq, unsigned flags);
71/* Sets DRIVER_OK (the device may start using the queues). */
72void vq_ready(vq_dev *d);
73/* Posts bufs[0..n) as one chain on queue q and rings its doorbell; the
74 * head id comes back in *head. VQ_E_FULL when the queue has no room. */
75int vq_post(vq_dev *d, unsigned q, const vq_buf *bufs, unsigned n, uint16_t *head);
76/* Takes one completion off queue q: VQ_OK with its head id and the bytes
77 * the device wrote, VQ_E_FULL when there is none yet (nothing changes). */
78int vq_reap(vq_dev *d, unsigned q, uint16_t *head, uint32_t *written);
79/* vq_post, then spins (at most spins polls) for that chain to complete. */
80int vq_call(vq_dev *d, unsigned q, const vq_buf *bufs, unsigned n, uint32_t spins, uint32_t *written);
81/* Writes status 0 (device reset) and marks the core unusable. */
82void vq_reset(vq_dev *d);
83/* Reads the device-specific config (32-bit, after the generation settles). */
84uint32_t vq_cfg32(vq_dev *d, uint32_t off);
85const char *vq_err_name(int e);
86#endif