VirtualBox

source: vbox/trunk/src/VBox/Devices/VirtIO/Virtio.h@ 71930

Last change on this file since 71930 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette