1 | /* $Id: Virtio.h 57808 2015-09-17 16:09:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtio.h - Virtio Declarations
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___VBox_Virtio_h
|
---|
19 | #define ___VBox_Virtio_h
|
---|
20 |
|
---|
21 | #include <iprt/ctype.h>
|
---|
22 |
|
---|
23 |
|
---|
24 | /** @name Saved state versions.
|
---|
25 | * The saved state version is changed if either common or any of specific
|
---|
26 | * parts are changed. That is, it is perfectly possible that the version
|
---|
27 | * of saved vnet state will increase as a result of change in vblk structure
|
---|
28 | * for example.
|
---|
29 | */
|
---|
30 | #define VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1 1
|
---|
31 | #define VIRTIO_SAVEDSTATE_VERSION 2
|
---|
32 | /** @} */
|
---|
33 |
|
---|
34 | #define DEVICE_PCI_VENDOR_ID 0x1AF4
|
---|
35 | #define DEVICE_PCI_BASE_ID 0x1000
|
---|
36 | #define DEVICE_PCI_BASE_ID_V1 0x1040
|
---|
37 | #define DEVICE_PCI_SUBSYSTEM_VENDOR_ID 0x1AF4
|
---|
38 | #define DEVICE_PCI_SUBSYSTEM_BASE_ID 1
|
---|
39 | #define DEVICE_PCI_SUBSYSTEM_BASE_ID_V1 0x40
|
---|
40 |
|
---|
41 | #define VIRTIO_MAX_NQUEUES 3
|
---|
42 |
|
---|
43 | #define VPCI_HOST_FEATURES 0x0
|
---|
44 | #define VPCI_GUEST_FEATURES 0x4
|
---|
45 | #define VPCI_QUEUE_PFN 0x8
|
---|
46 | #define VPCI_QUEUE_NUM 0xC
|
---|
47 | #define VPCI_QUEUE_SEL 0xE
|
---|
48 | #define VPCI_QUEUE_NOTIFY 0x10
|
---|
49 | #define VPCI_STATUS 0x12
|
---|
50 | #define VPCI_ISR 0x13
|
---|
51 | #define VPCI_CONFIG 0x14
|
---|
52 |
|
---|
53 | #define VPCI_ISR_QUEUE 0x1
|
---|
54 | #define VPCI_ISR_CONFIG 0x3
|
---|
55 |
|
---|
56 | #define VPCI_STATUS_ACK 0x01
|
---|
57 | #define VPCI_STATUS_DRV 0x02
|
---|
58 | #define VPCI_STATUS_DRV_OK 0x04
|
---|
59 | #define VPCI_STATUS_FAILED 0x80
|
---|
60 |
|
---|
61 | #define VPCI_F_NOTIFY_ON_EMPTY 0x01000000
|
---|
62 | #define VPCI_F_BAD_FEATURE 0x40000000
|
---|
63 |
|
---|
64 | #define VRINGDESC_MAX_SIZE (2 * 1024 * 1024)
|
---|
65 | #define VRINGDESC_F_NEXT 0x01
|
---|
66 | #define VRINGDESC_F_WRITE 0x02
|
---|
67 |
|
---|
68 | typedef struct VRingDesc
|
---|
69 | {
|
---|
70 | uint64_t u64Addr;
|
---|
71 | uint32_t uLen;
|
---|
72 | uint16_t u16Flags;
|
---|
73 | uint16_t u16Next;
|
---|
74 | } VRINGDESC;
|
---|
75 | typedef VRINGDESC *PVRINGDESC;
|
---|
76 |
|
---|
77 | #define VRINGAVAIL_F_NO_INTERRUPT 0x01
|
---|
78 |
|
---|
79 | typedef struct VRingAvail
|
---|
80 | {
|
---|
81 | uint16_t uFlags;
|
---|
82 | uint16_t uNextFreeIndex;
|
---|
83 | uint16_t auRing[1];
|
---|
84 | } VRINGAVAIL;
|
---|
85 |
|
---|
86 | typedef struct VRingUsedElem
|
---|
87 | {
|
---|
88 | uint32_t uId;
|
---|
89 | uint32_t uLen;
|
---|
90 | } VRINGUSEDELEM;
|
---|
91 |
|
---|
92 | #define VRINGUSED_F_NO_NOTIFY 0x01
|
---|
93 |
|
---|
94 | typedef struct VRingUsed
|
---|
95 | {
|
---|
96 | uint16_t uFlags;
|
---|
97 | uint16_t uIndex;
|
---|
98 | VRINGUSEDELEM aRing[1];
|
---|
99 | } VRINGUSED;
|
---|
100 | typedef VRINGUSED *PVRINGUSED;
|
---|
101 |
|
---|
102 | #define VRING_MAX_SIZE 1024
|
---|
103 |
|
---|
104 | typedef struct VRing
|
---|
105 | {
|
---|
106 | uint16_t uSize;
|
---|
107 | uint16_t padding[3];
|
---|
108 | RTGCPHYS addrDescriptors;
|
---|
109 | RTGCPHYS addrAvail;
|
---|
110 | RTGCPHYS addrUsed;
|
---|
111 | } VRING;
|
---|
112 | typedef VRING *PVRING;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Queue callback (consumer?).
|
---|
116 | *
|
---|
117 | * @param pvState Pointer to the VirtIO PCI core state, VPCISTATE.
|
---|
118 | * @param pQueue Pointer to the queue structure.
|
---|
119 | */
|
---|
120 | typedef DECLCALLBACK(void) FNVPCIQUEUECALLBACK(void *pvState, struct VQueue *pQueue);
|
---|
121 | /** Pointer to a VQUEUE callback function. */
|
---|
122 | typedef FNVPCIQUEUECALLBACK *PFNVPCIQUEUECALLBACK;
|
---|
123 |
|
---|
124 | typedef struct VQueue
|
---|
125 | {
|
---|
126 | VRING VRing;
|
---|
127 | uint16_t uNextAvailIndex;
|
---|
128 | uint16_t uNextUsedIndex;
|
---|
129 | uint32_t uPageNumber;
|
---|
130 | R3PTRTYPE(PFNVPCIQUEUECALLBACK) pfnCallback;
|
---|
131 | R3PTRTYPE(const char *) pcszName;
|
---|
132 | } VQUEUE;
|
---|
133 | typedef VQUEUE *PVQUEUE;
|
---|
134 |
|
---|
135 | typedef struct VQueueElemSeg
|
---|
136 | {
|
---|
137 | RTGCPHYS addr;
|
---|
138 | void *pv;
|
---|
139 | uint32_t cb;
|
---|
140 | } VQUEUESEG;
|
---|
141 |
|
---|
142 | typedef struct VQueueElem
|
---|
143 | {
|
---|
144 | uint32_t uIndex;
|
---|
145 | uint32_t nIn;
|
---|
146 | uint32_t nOut;
|
---|
147 | VQUEUESEG aSegsIn[VRING_MAX_SIZE];
|
---|
148 | VQUEUESEG aSegsOut[VRING_MAX_SIZE];
|
---|
149 | } VQUEUEELEM;
|
---|
150 | typedef VQUEUEELEM *PVQUEUEELEM;
|
---|
151 |
|
---|
152 |
|
---|
153 | enum VirtioDeviceType
|
---|
154 | {
|
---|
155 | VIRTIO_NET_ID = 0,
|
---|
156 | VIRTIO_BLK_ID = 1,
|
---|
157 | VIRTIO_GPU_ID = 16,
|
---|
158 | VIRTIO_32BIT_HACK = 0x7fffffff
|
---|
159 | };
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * The core (/common) state of the VirtIO PCI device
|
---|
164 | *
|
---|
165 | * @implements PDMILEDPORTS
|
---|
166 | */
|
---|
167 | typedef struct VPCIState_st
|
---|
168 | {
|
---|
169 | PDMCRITSECT cs; /**< Critical section - what is it protecting? */
|
---|
170 | /* Read-only part, never changes after initialization. */
|
---|
171 | char szInstance[8]; /**< Instance name, e.g. VNet#1. */
|
---|
172 |
|
---|
173 | #if HC_ARCH_BITS != 64
|
---|
174 | uint32_t padding1;
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | /** Status LUN: Base interface. */
|
---|
178 | PDMIBASE IBase;
|
---|
179 | /** Status LUN: LED port interface. */
|
---|
180 | PDMILEDPORTS ILeds;
|
---|
181 | /** Status LUN: LED connector (peer). */
|
---|
182 | R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
|
---|
183 |
|
---|
184 | PPDMDEVINSR3 pDevInsR3; /**< Device instance - R3. */
|
---|
185 | PPDMDEVINSR0 pDevInsR0; /**< Device instance - R0. */
|
---|
186 | PPDMDEVINSRC pDevInsRC; /**< Device instance - RC. */
|
---|
187 |
|
---|
188 | #if HC_ARCH_BITS == 64
|
---|
189 | uint32_t padding2;
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /** TODO */
|
---|
193 | PCIDEVICE pciDevice;
|
---|
194 | /** Base port of I/O space region. */
|
---|
195 | RTIOPORT IOPortBase;
|
---|
196 |
|
---|
197 | /* Read/write part, protected with critical section. */
|
---|
198 | /** Status LED. */
|
---|
199 | PDMLED led;
|
---|
200 |
|
---|
201 | uint32_t uGuestFeatures;
|
---|
202 | uint16_t uQueueSelector; /**< An index in aQueues array. */
|
---|
203 | uint8_t uStatus; /**< Device Status (bits are device-specific). */
|
---|
204 | uint8_t uISR; /**< Interrupt Status Register. */
|
---|
205 |
|
---|
206 | #if HC_ARCH_BITS != 64
|
---|
207 | uint32_t padding3;
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | uint32_t nQueues; /**< Actual number of queues used. */
|
---|
211 | VQUEUE Queues[VIRTIO_MAX_NQUEUES];
|
---|
212 |
|
---|
213 | #if defined(VBOX_WITH_STATISTICS)
|
---|
214 | STAMPROFILEADV StatIOReadGC;
|
---|
215 | STAMPROFILEADV StatIOReadHC;
|
---|
216 | STAMPROFILEADV StatIOWriteGC;
|
---|
217 | STAMPROFILEADV StatIOWriteHC;
|
---|
218 | STAMCOUNTER StatIntsRaised;
|
---|
219 | STAMCOUNTER StatIntsSkipped;
|
---|
220 | STAMPROFILE StatCsGC;
|
---|
221 | STAMPROFILE StatCsHC;
|
---|
222 | #endif /* VBOX_WITH_STATISTICS */
|
---|
223 | } VPCISTATE;
|
---|
224 | /** Pointer to the core (/common) state of a VirtIO PCI device. */
|
---|
225 | typedef VPCISTATE *PVPCISTATE;
|
---|
226 |
|
---|
227 | typedef DECLCALLBACK(uint32_t) FNGETHOSTFEATURES(void *pvState);
|
---|
228 | typedef FNGETHOSTFEATURES *PFNGETHOSTFEATURES;
|
---|
229 |
|
---|
230 | /** @name VirtIO port I/O callbacks.
|
---|
231 | * @{ */
|
---|
232 | typedef struct VPCIIOCALLBACKS
|
---|
233 | {
|
---|
234 | DECLCALLBACKMEMBER(uint32_t, pfnGetHostFeatures)(void *pvState);
|
---|
235 | DECLCALLBACKMEMBER(uint32_t, pfnGetHostMinimalFeatures)(void *pvState);
|
---|
236 | DECLCALLBACKMEMBER(void, pfnSetHostFeatures)(void *pvState, uint32_t fFeatures);
|
---|
237 | DECLCALLBACKMEMBER(int, pfnGetConfig)(void *pvState, uint32_t offCfg, uint32_t cb, void *pvData);
|
---|
238 | DECLCALLBACKMEMBER(int, pfnSetConfig)(void *pvState, uint32_t offCfg, uint32_t cb, void *pvData);
|
---|
239 | DECLCALLBACKMEMBER(int, pfnReset)(void *pvState);
|
---|
240 | DECLCALLBACKMEMBER(void, pfnReady)(void *pvState);
|
---|
241 | } VPCIIOCALLBACKS;
|
---|
242 | /** Pointer to a const VirtIO port I/O callback structure. */
|
---|
243 | typedef const VPCIIOCALLBACKS *PCVPCIIOCALLBACKS;
|
---|
244 | /** @} */
|
---|
245 |
|
---|
246 | int vpciRaiseInterrupt(VPCISTATE *pState, int rcBusy, uint8_t u8IntCause);
|
---|
247 | int vpciIOPortIn(PPDMDEVINS pDevIns,
|
---|
248 | void *pvUser,
|
---|
249 | RTIOPORT port,
|
---|
250 | uint32_t *pu32,
|
---|
251 | unsigned cb,
|
---|
252 | PCVPCIIOCALLBACKS pCallbacks);
|
---|
253 |
|
---|
254 | int vpciIOPortOut(PPDMDEVINS pDevIns,
|
---|
255 | void *pvUser,
|
---|
256 | RTIOPORT port,
|
---|
257 | uint32_t u32,
|
---|
258 | unsigned cb,
|
---|
259 | PCVPCIIOCALLBACKS pCallbacks);
|
---|
260 |
|
---|
261 | void vpciSetWriteLed(PVPCISTATE pState, bool fOn);
|
---|
262 | void vpciSetReadLed(PVPCISTATE pState, bool fOn);
|
---|
263 | int vpciSaveExec(PVPCISTATE pState, PSSMHANDLE pSSM);
|
---|
264 | int vpciLoadExec(PVPCISTATE pState, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass, uint32_t nQueues);
|
---|
265 | int vpciConstruct(PPDMDEVINS pDevIns, VPCISTATE *pState, int iInstance, const char *pcszNameFmt,
|
---|
266 | uint16_t uDeviceId, uint16_t uClass, uint32_t nQueues, bool fVersion1);
|
---|
267 | int vpciDestruct(VPCISTATE* pState);
|
---|
268 | void vpciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta);
|
---|
269 | void vpciReset(PVPCISTATE pState);
|
---|
270 | void *vpciQueryInterface(struct PDMIBASE *pInterface, const char *pszIID);
|
---|
271 | PVQUEUE vpciAddQueue(VPCISTATE* pState, unsigned uSize, PFNVPCIQUEUECALLBACK pfnCallback, const char *pcszName);
|
---|
272 |
|
---|
273 | #define VPCI_CS
|
---|
274 | DECLINLINE(int) vpciCsEnter(VPCISTATE *pState, int rcBusy)
|
---|
275 | {
|
---|
276 | #ifdef VPCI_CS
|
---|
277 | STAM_PROFILE_START(&pState->CTXSUFF(StatCs), a);
|
---|
278 | int rc = PDMCritSectEnter(&pState->cs, rcBusy);
|
---|
279 | STAM_PROFILE_STOP(&pState->CTXSUFF(StatCs), a);
|
---|
280 | return rc;
|
---|
281 | #else
|
---|
282 | return VINF_SUCCESS;
|
---|
283 | #endif
|
---|
284 | }
|
---|
285 |
|
---|
286 | DECLINLINE(void) vpciCsLeave(VPCISTATE *pState)
|
---|
287 | {
|
---|
288 | #ifdef VPCI_CS
|
---|
289 | PDMCritSectLeave(&pState->cs);
|
---|
290 | #endif
|
---|
291 | }
|
---|
292 |
|
---|
293 | void vringSetNotification(PVPCISTATE pState, PVRING pVRing, bool fEnabled);
|
---|
294 |
|
---|
295 | DECLINLINE(uint16_t) vringReadAvailIndex(PVPCISTATE pState, PVRING pVRing)
|
---|
296 | {
|
---|
297 | uint16_t tmp;
|
---|
298 |
|
---|
299 | PDMDevHlpPhysRead(pState->CTX_SUFF(pDevIns),
|
---|
300 | pVRing->addrAvail + RT_OFFSETOF(VRINGAVAIL, uNextFreeIndex),
|
---|
301 | &tmp, sizeof(tmp));
|
---|
302 | return tmp;
|
---|
303 | }
|
---|
304 |
|
---|
305 | bool vqueueSkip(PVPCISTATE pState, PVQUEUE pQueue);
|
---|
306 | bool vqueueGet(PVPCISTATE pState, PVQUEUE pQueue, PVQUEUEELEM pElem, bool fRemove = true);
|
---|
307 | void vqueuePut(PVPCISTATE pState, PVQUEUE pQueue, PVQUEUEELEM pElem, uint32_t uLen, uint32_t uReserved = 0);
|
---|
308 | void vqueueNotify(PVPCISTATE pState, PVQUEUE pQueue);
|
---|
309 | void vqueueSync(PVPCISTATE pState, PVQUEUE pQueue);
|
---|
310 |
|
---|
311 | DECLINLINE(bool) vqueuePeek(PVPCISTATE pState, PVQUEUE pQueue, PVQUEUEELEM pElem)
|
---|
312 | {
|
---|
313 | return vqueueGet(pState, pQueue, pElem, /* fRemove */ false);
|
---|
314 | }
|
---|
315 |
|
---|
316 | DECLINLINE(bool) vqueueIsReady(PVPCISTATE pState, PVQUEUE pQueue)
|
---|
317 | {
|
---|
318 | NOREF(pState);
|
---|
319 | return !!pQueue->VRing.addrAvail;
|
---|
320 | }
|
---|
321 |
|
---|
322 | DECLINLINE(bool) vqueueIsEmpty(PVPCISTATE pState, PVQUEUE pQueue)
|
---|
323 | {
|
---|
324 | return (vringReadAvailIndex(pState, &pQueue->VRing) == pQueue->uNextAvailIndex);
|
---|
325 | }
|
---|
326 |
|
---|
327 | #endif /* !___VBox_Virtio_h */
|
---|