]>
Commit | Line | Data |
---|---|---|
311e666a MM |
1 | /* |
2 | * libqos virtio definitions | |
3 | * | |
4 | * Copyright (c) 2014 Marc Marí | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | */ | |
9 | ||
10 | #ifndef LIBQOS_VIRTIO_H | |
11 | #define LIBQOS_VIRTIO_H | |
12 | ||
bf3c63d2 MM |
13 | #include "libqos/malloc.h" |
14 | ||
311e666a MM |
15 | #define QVIRTIO_VENDOR_ID 0x1AF4 |
16 | ||
46e0cf76 MM |
17 | #define QVIRTIO_RESET 0x0 |
18 | #define QVIRTIO_ACKNOWLEDGE 0x1 | |
19 | #define QVIRTIO_DRIVER 0x2 | |
bf3c63d2 | 20 | #define QVIRTIO_DRIVER_OK 0x4 |
46e0cf76 | 21 | |
311e666a MM |
22 | #define QVIRTIO_NET_DEVICE_ID 0x1 |
23 | #define QVIRTIO_BLK_DEVICE_ID 0x2 | |
24 | ||
f294b029 MM |
25 | #define QVIRTIO_F_NOTIFY_ON_EMPTY 0x01000000 |
26 | #define QVIRTIO_F_ANY_LAYOUT 0x08000000 | |
27 | #define QVIRTIO_F_RING_INDIRECT_DESC 0x10000000 | |
28 | #define QVIRTIO_F_RING_EVENT_IDX 0x20000000 | |
29 | ||
bf3c63d2 MM |
30 | #define QVRING_DESC_F_NEXT 0x1 |
31 | #define QVRING_DESC_F_WRITE 0x2 | |
32 | #define QVRING_DESC_F_INDIRECT 0x4 | |
33 | ||
34 | #define QVIRTIO_F_NOTIFY_ON_EMPTY 0x01000000 | |
35 | #define QVIRTIO_F_ANY_LAYOUT 0x08000000 | |
36 | #define QVIRTIO_F_RING_INDIRECT_DESC 0x10000000 | |
37 | #define QVIRTIO_F_RING_EVENT_IDX 0x20000000 | |
38 | #define QVIRTIO_F_BAD_FEATURE 0x40000000 | |
39 | ||
40 | #define QVRING_AVAIL_F_NO_INTERRUPT 1 | |
41 | ||
42 | #define QVRING_USED_F_NO_NOTIFY 1 | |
43 | ||
311e666a MM |
44 | typedef struct QVirtioDevice { |
45 | /* Device type */ | |
46 | uint16_t device_type; | |
47 | } QVirtioDevice; | |
48 | ||
bf3c63d2 MM |
49 | typedef struct QVRingDesc { |
50 | uint64_t addr; | |
51 | uint32_t len; | |
52 | uint16_t flags; | |
53 | uint16_t next; | |
54 | } QVRingDesc; | |
55 | ||
56 | typedef struct QVRingAvail { | |
57 | uint16_t flags; | |
58 | uint16_t idx; | |
59 | uint16_t ring[0]; /* This is an array of uint16_t */ | |
60 | } QVRingAvail; | |
61 | ||
62 | typedef struct QVRingUsedElem { | |
63 | uint32_t id; | |
64 | uint32_t len; | |
65 | } QVRingUsedElem; | |
66 | ||
67 | typedef struct QVRingUsed { | |
68 | uint16_t flags; | |
69 | uint16_t idx; | |
70 | QVRingUsedElem ring[0]; /* This is an array of QVRingUsedElem structs */ | |
71 | } QVRingUsed; | |
72 | ||
73 | typedef struct QVirtQueue { | |
74 | uint64_t desc; /* This points to an array of QVRingDesc */ | |
75 | uint64_t avail; /* This points to a QVRingAvail */ | |
76 | uint64_t used; /* This points to a QVRingDesc */ | |
77 | uint16_t index; | |
78 | uint32_t size; | |
79 | uint32_t free_head; | |
80 | uint32_t num_free; | |
81 | uint32_t align; | |
f294b029 | 82 | bool indirect; |
bf3c63d2 MM |
83 | } QVirtQueue; |
84 | ||
f294b029 MM |
85 | typedef struct QVRingIndirectDesc { |
86 | uint64_t desc; /* This points to an array fo QVRingDesc */ | |
87 | uint16_t index; | |
88 | uint16_t elem; | |
89 | } QVRingIndirectDesc; | |
90 | ||
46e0cf76 MM |
91 | typedef struct QVirtioBus { |
92 | uint8_t (*config_readb)(QVirtioDevice *d, void *addr); | |
93 | uint16_t (*config_readw)(QVirtioDevice *d, void *addr); | |
94 | uint32_t (*config_readl)(QVirtioDevice *d, void *addr); | |
95 | uint64_t (*config_readq)(QVirtioDevice *d, void *addr); | |
96 | ||
bf3c63d2 MM |
97 | /* Get features of the device */ |
98 | uint32_t (*get_features)(QVirtioDevice *d); | |
99 | ||
f294b029 | 100 | /* Set features of the device */ |
bf3c63d2 MM |
101 | void (*set_features)(QVirtioDevice *d, uint32_t features); |
102 | ||
f294b029 MM |
103 | /* Get features of the guest */ |
104 | uint32_t (*get_guest_features)(QVirtioDevice *d); | |
105 | ||
46e0cf76 MM |
106 | /* Get status of the device */ |
107 | uint8_t (*get_status)(QVirtioDevice *d); | |
108 | ||
109 | /* Set status of the device */ | |
110 | void (*set_status)(QVirtioDevice *d, uint8_t status); | |
bf3c63d2 MM |
111 | |
112 | /* Get the ISR status of the device */ | |
113 | uint8_t (*get_isr_status)(QVirtioDevice *d); | |
114 | ||
115 | /* Select a queue to work on */ | |
116 | void (*queue_select)(QVirtioDevice *d, uint16_t index); | |
117 | ||
118 | /* Get the size of the selected queue */ | |
119 | uint16_t (*get_queue_size)(QVirtioDevice *d); | |
120 | ||
121 | /* Set the address of the selected queue */ | |
122 | void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn); | |
123 | ||
124 | /* Setup the virtqueue specified by index */ | |
125 | QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc, | |
126 | uint16_t index); | |
127 | ||
128 | /* Notify changes in virtqueue */ | |
129 | void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq); | |
46e0cf76 MM |
130 | } QVirtioBus; |
131 | ||
bf3c63d2 MM |
132 | static inline uint32_t qvring_size(uint32_t num, uint32_t align) |
133 | { | |
134 | return ((sizeof(struct QVRingDesc) * num + sizeof(uint16_t) * (3 + num) | |
135 | + align - 1) & ~(align - 1)) | |
136 | + sizeof(uint16_t) * 3 + sizeof(struct QVRingUsedElem) * num; | |
137 | } | |
138 | ||
46e0cf76 MM |
139 | uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d, |
140 | void *addr); | |
141 | uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d, | |
142 | void *addr); | |
143 | uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d, | |
144 | void *addr); | |
145 | uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d, | |
146 | void *addr); | |
bf3c63d2 MM |
147 | uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d); |
148 | void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d, | |
149 | uint32_t features); | |
46e0cf76 MM |
150 | |
151 | void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d); | |
152 | void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d); | |
153 | void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d); | |
bf3c63d2 MM |
154 | void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d); |
155 | ||
156 | bool qvirtio_wait_isr(const QVirtioBus *bus, QVirtioDevice *d, uint8_t mask, | |
157 | uint64_t timeout); | |
158 | QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d, | |
159 | QGuestAllocator *alloc, uint16_t index); | |
160 | ||
161 | void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr); | |
f294b029 MM |
162 | QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d, |
163 | QGuestAllocator *alloc, uint16_t elem); | |
164 | void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data, | |
165 | uint32_t len, bool write); | |
bf3c63d2 MM |
166 | uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, |
167 | bool next); | |
f294b029 | 168 | uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect); |
bf3c63d2 MM |
169 | void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq, |
170 | uint32_t free_head); | |
46e0cf76 | 171 | |
311e666a | 172 | #endif |