VirtualBox

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

Last change on this file since 33089 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: Virtio.h 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * Virtio.h - Virtio Declarations
4 */
5
6/*
7 * Copyright (C) 2009 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#define VIRTIO_RELOCATE(p, o) *(RTHCUINTPTR *)&p += o
24
25/*
26 * The saved state version is changed if either common or any of specific
27 * parts are changed. That is, it is perfectly possible that the version
28 * of saved vnet state will increase as a result of change in vblk structure
29 * for example.
30 */
31#define VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1 1
32#define VIRTIO_SAVEDSTATE_VERSION 2
33
34#define DEVICE_PCI_VENDOR_ID 0x1AF4
35#define DEVICE_PCI_DEVICE_ID 0x1000
36#define DEVICE_PCI_SUBSYSTEM_VENDOR_ID 0x1AF4
37
38#define VIRTIO_MAX_NQUEUES 3
39
40#define VPCI_HOST_FEATURES 0x0
41#define VPCI_GUEST_FEATURES 0x4
42#define VPCI_QUEUE_PFN 0x8
43#define VPCI_QUEUE_NUM 0xC
44#define VPCI_QUEUE_SEL 0xE
45#define VPCI_QUEUE_NOTIFY 0x10
46#define VPCI_STATUS 0x12
47#define VPCI_ISR 0x13
48#define VPCI_CONFIG 0x14
49
50#define VPCI_ISR_QUEUE 0x1
51#define VPCI_ISR_CONFIG 0x3
52
53#define VPCI_STATUS_ACK 0x01
54#define VPCI_STATUS_DRV 0x02
55#define VPCI_STATUS_DRV_OK 0x04
56#define VPCI_STATUS_FAILED 0x80
57
58#define VPCI_F_NOTIFY_ON_EMPTY 0x01000000
59#define VPCI_F_BAD_FEATURE 0x40000000
60
61#define VRINGDESC_MAX_SIZE (2 * 1024 * 1024)
62#define VRINGDESC_F_NEXT 0x01
63#define VRINGDESC_F_WRITE 0x02
64
65struct VRingDesc
66{
67 uint64_t u64Addr;
68 uint32_t uLen;
69 uint16_t u16Flags;
70 uint16_t u16Next;
71};
72typedef struct VRingDesc VRINGDESC;
73typedef VRINGDESC *PVRINGDESC;
74
75#define VRINGAVAIL_F_NO_INTERRUPT 0x01
76
77struct VRingAvail
78{
79 uint16_t uFlags;
80 uint16_t uNextFreeIndex;
81 uint16_t auRing[1];
82};
83typedef struct VRingAvail VRINGAVAIL;
84
85struct VRingUsedElem
86{
87 uint32_t uId;
88 uint32_t uLen;
89};
90typedef struct VRingUsedElem VRINGUSEDELEM;
91
92#define VRINGUSED_F_NO_NOTIFY 0x01
93
94struct VRingUsed
95{
96 uint16_t uFlags;
97 uint16_t uIndex;
98 VRINGUSEDELEM aRing[1];
99};
100typedef struct VRingUsed VRINGUSED;
101typedef VRINGUSED *PVRINGUSED;
102
103#define VRING_MAX_SIZE 1024
104
105struct VRing
106{
107 uint16_t uSize;
108 uint16_t padding[3];
109 RTGCPHYS addrDescriptors;
110 RTGCPHYS addrAvail;
111 RTGCPHYS addrUsed;
112};
113typedef struct VRing VRING;
114typedef VRING *PVRING;
115
116struct VQueue
117{
118 VRING VRing;
119 uint16_t uNextAvailIndex;
120 uint16_t uNextUsedIndex;
121 uint32_t uPageNumber;
122#ifdef IN_RING3
123 void (*pfnCallback)(void *pvState, struct VQueue *pQueue);
124#else
125 RTR3UINTPTR pfnCallback;
126#endif
127 R3PTRTYPE(const char *) pcszName;
128};
129typedef struct VQueue VQUEUE;
130typedef VQUEUE *PVQUEUE;
131
132struct VQueueElemSeg
133{
134 RTGCPHYS addr;
135 void *pv;
136 uint32_t cb;
137};
138typedef struct VQueueElemSeg VQUEUESEG;
139
140struct VQueueElem
141{
142 uint32_t uIndex;
143 uint32_t nIn;
144 uint32_t nOut;
145 VQUEUESEG aSegsIn[VRING_MAX_SIZE];
146 VQUEUESEG aSegsOut[VRING_MAX_SIZE];
147};
148typedef struct VQueueElem VQUEUEELEM;
149typedef VQUEUEELEM *PVQUEUEELEM;
150
151
152enum VirtioDeviceType
153{
154 VIRTIO_NET_ID = 0,
155 VIRTIO_BLK_ID = 1,
156 VIRTIO_32BIT_HACK = 0x7fffffff
157};
158
159
160/**
161 * The state of the VirtIO PCI device
162 *
163 * @implements PDMILEDPORTS
164 */
165struct VPCIState_st
166{
167 PDMCRITSECT cs; /**< Critical section - what is it protecting? */
168 /* Read-only part, never changes after initialization. */
169 char szInstance[8]; /**< Instance name, e.g. VNet#1. */
170
171#if HC_ARCH_BITS != 64
172 uint32_t padding1;
173#endif
174
175 /** Status LUN: Base interface. */
176 PDMIBASE IBase;
177 /** Status LUN: LED port interface. */
178 PDMILEDPORTS ILeds;
179 /** Status LUN: LED connector (peer). */
180 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
181
182 PPDMDEVINSR3 pDevInsR3; /**< Device instance - R3. */
183 PPDMDEVINSR0 pDevInsR0; /**< Device instance - R0. */
184 PPDMDEVINSRC pDevInsRC; /**< Device instance - RC. */
185
186#if HC_ARCH_BITS == 64
187 uint32_t padding2;
188#endif
189
190 /** TODO */
191 PCIDEVICE pciDevice;
192 /** Base port of I/O space region. */
193 RTIOPORT addrIOPort;
194
195 /* Read/write part, protected with critical section. */
196 /** Status LED. */
197 PDMLED led;
198
199 uint32_t uGuestFeatures;
200 uint16_t uQueueSelector; /**< An index in aQueues array. */
201 uint8_t uStatus; /**< Device Status (bits are device-specific). */
202 uint8_t uISR; /**< Interrupt Status Register. */
203
204#if HC_ARCH_BITS != 64
205 uint32_t padding3;
206#endif
207
208 uint32_t nQueues; /**< Actual number of queues used. */
209 VQUEUE Queues[VIRTIO_MAX_NQUEUES];
210
211#if defined(VBOX_WITH_STATISTICS)
212 STAMPROFILEADV StatIOReadGC;
213 STAMPROFILEADV StatIOReadHC;
214 STAMPROFILEADV StatIOWriteGC;
215 STAMPROFILEADV StatIOWriteHC;
216 STAMCOUNTER StatIntsRaised;
217 STAMCOUNTER StatIntsSkipped;
218 STAMPROFILE StatCsGC;
219 STAMPROFILE StatCsHC;
220#endif /* VBOX_WITH_STATISTICS */
221};
222typedef struct VPCIState_st VPCISTATE;
223typedef VPCISTATE *PVPCISTATE;
224
225/* Callbacks *****************************************************************/
226typedef uint32_t (*PFNGETHOSTFEATURES)(void *pState);
227typedef uint32_t (*PFNGETHOSTMINIMALFEATURES)(void *pState);
228typedef void (*PFNSETHOSTFEATURES)(void *pState, uint32_t uFeatures);
229typedef int (*PFNGETCONFIG)(void *pState, uint32_t port, uint32_t cb, void *data);
230typedef int (*PFNSETCONFIG)(void *pState, uint32_t port, uint32_t cb, void *data);
231typedef int (*PFNRESET)(void *pState);
232typedef void (*PFNREADY)(void *pState);
233/*****************************************************************************/
234
235int vpciRaiseInterrupt(VPCISTATE *pState, int rcBusy, uint8_t u8IntCause);
236int vpciIOPortIn(PPDMDEVINS pDevIns,
237 void *pvUser,
238 RTIOPORT port,
239 uint32_t *pu32,
240 unsigned cb,
241 PFNGETHOSTFEATURES pfnGetHostFeatures,
242 PFNGETCONFIG pfnGetConfig);
243
244int vpciIOPortOut(PPDMDEVINS pDevIns,
245 void *pvUser,
246 RTIOPORT port,
247 uint32_t u32,
248 unsigned cb,
249 PFNGETHOSTMINIMALFEATURES pfnGetHostMinimalFeatures,
250 PFNGETHOSTFEATURES pfnGetHostFeatures,
251 PFNSETHOSTFEATURES pfnSetHostFeatures,
252 PFNRESET pfnReset,
253 PFNREADY pfnReady,
254 PFNSETCONFIG pfnSetConfig);
255
256void vpciSetWriteLed(PVPCISTATE pState, bool fOn);
257void vpciSetReadLed(PVPCISTATE pState, bool fOn);
258int vpciSaveExec(PVPCISTATE pState, PSSMHANDLE pSSM);
259int vpciLoadExec(PVPCISTATE pState, PSSMHANDLE pSSM,
260 uint32_t uVersion, uint32_t uPass,
261 uint32_t nQueues);
262int vpciConstruct(PPDMDEVINS pDevIns, VPCISTATE *pState,
263 int iInstance, const char *pcszNameFmt,
264 uint16_t uSubsystemId, uint16_t uClass,
265 uint32_t nQueues);
266int vpciDestruct(VPCISTATE* pState);
267void vpciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta);
268void vpciReset(PVPCISTATE pState);
269void *vpciQueryInterface(struct PDMIBASE *pInterface, const char *pszIID);
270PVQUEUE vpciAddQueue(VPCISTATE* pState, unsigned uSize,
271 void (*pfnCallback)(void *pvState, PVQUEUE pQueue),
272 const char *pcszName);
273
274#define VPCI_CS
275DECLINLINE(int) vpciCsEnter(VPCISTATE *pState, int iBusyRc)
276{
277#ifdef VPCI_CS
278 STAM_PROFILE_START(&pState->CTXSUFF(StatCs), a);
279 int rc = PDMCritSectEnter(&pState->cs, iBusyRc);
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 vqueueGet(PVPCISTATE pState, PVQUEUE pQueue, PVQUEUEELEM pElem);
307void vqueuePut(PVPCISTATE pState, PVQUEUE pQueue, PVQUEUEELEM pElem, uint32_t uLen);
308void vqueueNotify(PVPCISTATE pState, PVQUEUE pQueue);
309void vqueueSync(PVPCISTATE pState, PVQUEUE pQueue);
310
311
312DECLINLINE(bool) vqueueIsReady(PVPCISTATE pState, PVQUEUE pQueue)
313{
314 return !!pQueue->VRing.addrAvail;
315}
316
317DECLINLINE(bool) vqueueIsEmpty(PVPCISTATE pState, PVQUEUE pQueue)
318{
319 return (vringReadAvailIndex(pState, &pQueue->VRing) == pQueue->uNextAvailIndex);
320}
321
322#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