1 | /* $Id: Virtio_1_0.h 80683 2019-09-09 19:57:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtio_1_0.h - Virtio Declarations
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 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_INCLUDED_SRC_VirtIO_Virtio_1_0_h
|
---|
19 | #define VBOX_INCLUDED_SRC_VirtIO_Virtio_1_0_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <iprt/ctype.h>
|
---|
25 | #include <iprt/sg.h>
|
---|
26 |
|
---|
27 | typedef void * VIRTIOHANDLE; /**< Opaque handle to the VirtIO framework */
|
---|
28 |
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Important sizing and bounds params for this impl. of VirtIO 1.0 PCI device
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * TEMPORARY NOTE: Some of these values are experimental during development and will likely change.
|
---|
35 | */
|
---|
36 | #define VIRTIO_MAX_QUEUE_NAME_SIZE 32 /**< Maximum length of a queue name */
|
---|
37 | #define VIRTQ_MAX_SIZE 1024 /**< Max size (# desc elements) of a virtq */
|
---|
38 | #define VIRTQ_MAX_CNT 24 /**< Max queues we allow guest to create */
|
---|
39 | #define VIRTIO_NOTIFY_OFFSET_MULTIPLIER 2 /**< VirtIO Notify Cap. MMIO config param */
|
---|
40 | #define VIRTIOSCSI_REGION_MEM_IO 0 /**< BAR for MMIO (implementation specific) */
|
---|
41 | #define VIRTIOSCSI_REGION_PORT_IO 1 /**< BAR for PORT I/O (impl specific) */
|
---|
42 | #define VIRTIOSCSI_REGION_PCI_CAP 2 /**< BAR for VirtIO Cap. MMIO (impl specific) */
|
---|
43 |
|
---|
44 | #define VIRTIO_HEX_DUMP(logLevel, pv, cb, base, title) \
|
---|
45 | do { \
|
---|
46 | if (LogIsItEnabled(logLevel, LOG_GROUP)) \
|
---|
47 | virtioHexDump((pv), (cb), (base), (title)); \
|
---|
48 | } while (0)
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The following structure is used to pass the PCI parameters from the consumer
|
---|
52 | * to this generic VirtIO framework. This framework provides the Vendor ID as Virtio.
|
---|
53 | */
|
---|
54 | typedef struct VIRTIOPCIPARAMS
|
---|
55 | {
|
---|
56 | uint16_t uDeviceId; /**< PCI Cfg Device ID */
|
---|
57 | uint16_t uClassBase; /**< PCI Cfg Base Class */
|
---|
58 | uint16_t uClassSub; /**< PCI Cfg Subclass */
|
---|
59 | uint16_t uClassProg; /**< PCI Cfg Programming Interface Class */
|
---|
60 | uint16_t uSubsystemId; /**< PCI Cfg Card Manufacturer Vendor ID */
|
---|
61 | uint16_t uSubsystemVendorId; /**< PCI Cfg Chipset Manufacturer Vendor ID */
|
---|
62 | uint16_t uRevisionId; /**< PCI Cfg Revision ID */
|
---|
63 | uint16_t uInterruptLine; /**< PCI Cfg Interrupt line */
|
---|
64 | uint16_t uInterruptPin; /**< PCI Cfg Interrupt pin */
|
---|
65 | } VIRTIOPCIPARAMS, *PVIRTIOPCIPARAMS;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Implementation-specific client callback to notify client of significant device status
|
---|
69 | * changes.
|
---|
70 | *
|
---|
71 | * @param hVirtio Handle to VirtIO framework
|
---|
72 | * @param fDriverOk True if guest driver is okay (thus queues, etc... are valid)
|
---|
73 | * @param pClient Pointer to opaque client data (state)
|
---|
74 | */
|
---|
75 | typedef DECLCALLBACK(void) FNVIRTIOSTATUSCHANGED(VIRTIOHANDLE hVirtio, void *pClient, bool fDriverOk);
|
---|
76 | typedef FNVIRTIOSTATUSCHANGED *PFNVIRTIOSTATUSCHANGED;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * When guest-to-host queue notifications are enabled, the guest driver notifies the host
|
---|
80 | * that the avail queue has buffers, and this callback informs the client.
|
---|
81 | *
|
---|
82 | * @param hVirtio Handle to the VirtIO framework
|
---|
83 | * @param qIdx Index of the notified queue
|
---|
84 | * @param pClient Pointer to opaque client data (state)
|
---|
85 | */
|
---|
86 | typedef DECLCALLBACK(void) FNVIRTIOQUEUENOTIFIED(VIRTIOHANDLE hVirtio, void *pClient, uint16_t qIdx);
|
---|
87 | typedef FNVIRTIOQUEUENOTIFIED *PFNVIRTIOQUEUENOTIFIED;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Implementation-specific client callback to access VirtIO Device-specific capabilities
|
---|
91 | * (other VirtIO capabilities and features are handled in VirtIO implementation)
|
---|
92 | *
|
---|
93 | * @param pDevIns The device instance.
|
---|
94 | * @param offset Offset within device specific capabilities struct
|
---|
95 | * @param pvBuf Buffer in which to save read data
|
---|
96 | * @param cbWrite Number of bytes to write
|
---|
97 | */
|
---|
98 | typedef DECLCALLBACK(int) FNVIRTIODEVCAPWRITE(PPDMDEVINS pDevIns, uint32_t uOffset, const void *pvBuf, size_t cbWrite);
|
---|
99 | typedef FNVIRTIODEVCAPWRITE *PFNVIRTIODEVCAPWRITE;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Implementation-specific client ballback to access VirtIO Device-specific capabilities
|
---|
103 | * (other VirtIO capabilities and features are handled in VirtIO implementation)
|
---|
104 | *
|
---|
105 | * @param pDevIns The device instance.
|
---|
106 | * @param offset Offset within device specific capabilities struct
|
---|
107 | * @param pvBuf Buffer in which to save read data
|
---|
108 | * @param cbRead Number of bytes to read
|
---|
109 | */
|
---|
110 | typedef DECLCALLBACK(int) FNVIRTIODEVCAPREAD(PPDMDEVINS pDevIns, uint32_t uOffset, const void *pvBuf, size_t cbRead);
|
---|
111 | typedef FNVIRTIODEVCAPREAD *PFNVIRTIODEVCAPREAD;
|
---|
112 |
|
---|
113 |
|
---|
114 | /** @name VirtIO port I/O callbacks.
|
---|
115 | * @{ */
|
---|
116 | typedef struct VIRTIOCALLBACKS
|
---|
117 | {
|
---|
118 | DECLCALLBACKMEMBER(void, pfnVirtioStatusChanged)
|
---|
119 | (VIRTIOHANDLE hVirtio, void *pClient, bool fDriverOk);
|
---|
120 | DECLCALLBACKMEMBER(void, pfnVirtioQueueNotified)
|
---|
121 | (VIRTIOHANDLE hVirtio, void *pClient, uint16_t qIdx);
|
---|
122 | DECLCALLBACKMEMBER(int, pfnVirtioDevCapRead)
|
---|
123 | (PPDMDEVINS pDevIns, uint32_t uOffset, const void *pvBuf, size_t cbRead);
|
---|
124 | DECLCALLBACKMEMBER(int, pfnVirtioDevCapWrite)
|
---|
125 | (PPDMDEVINS pDevIns, uint32_t uOffset, const void *pvBuf, size_t cbWrite);
|
---|
126 | DECLCALLBACKMEMBER(int, pfnSSMDevLiveExec)
|
---|
127 | (PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass);
|
---|
128 | DECLCALLBACKMEMBER(int, pfnSSMDevSaveExec)
|
---|
129 | (PPDMDEVINS pDevIns, PSSMHANDLE pSSM);
|
---|
130 | DECLCALLBACKMEMBER(int, pfnSSMDevLoadExec)
|
---|
131 | (PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
|
---|
132 | DECLCALLBACKMEMBER(int, pfnSSMDevLoadDone)
|
---|
133 | (PPDMDEVINS pDevIns, PSSMHANDLE pSSM);
|
---|
134 |
|
---|
135 | } VIRTIOCALLBACKS, *PVIRTIOCALLBACKS;
|
---|
136 | /** @} */
|
---|
137 |
|
---|
138 | /** API for VirtIO client */
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Allocate client context for client to work with VirtIO-provided with queue
|
---|
142 | * As a side effect creates a buffer vector a client can get a pointer to
|
---|
143 | * with a call to virtioQueueDescChain()
|
---|
144 | *
|
---|
145 | * @param hVirtio - Handle to VirtIO framework
|
---|
146 | * @param qIdx - Queue number
|
---|
147 | * @param pcszName - Name to give queue
|
---|
148 | *
|
---|
149 | * @returns status VINF_SUCCESS - Success
|
---|
150 | * VERR_INVALID_STATE - VirtIO not in ready state
|
---|
151 | * VERR_NO_MEMORY - Out of memory
|
---|
152 | *
|
---|
153 | * @returns status. If false, the call failed and the client should call virtioResetAll()
|
---|
154 | */
|
---|
155 | int virtioQueueAttach(VIRTIOHANDLE hVirtio, uint16_t qIdx, const char *pcszName);
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Get the features VirtIO is running withnow.
|
---|
160 | *
|
---|
161 | * @returns Features the guest driver has accepted, finalizing the operational features
|
---|
162 | *
|
---|
163 | */
|
---|
164 | uint64_t virtioGetNegotiatedFeatures(VIRTIOHANDLE hVirtio);
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Detaches from queue and release resources
|
---|
168 | *
|
---|
169 | * @param hVirtio - Handle for VirtIO framework
|
---|
170 | * @param qIdx - Queue number
|
---|
171 | *
|
---|
172 | */
|
---|
173 | int virtioQueueDetach(VIRTIOHANDLE hVirtio, uint16_t qIdx);
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Get name of queue, by qIdx, assigned at virtioQueueAttach()
|
---|
177 | *
|
---|
178 | * @param hVirtio - Handle for VirtIO framework
|
---|
179 | * @param qIdx - Queue number
|
---|
180 | *
|
---|
181 | * @returns Success: Returns pointer to queue name
|
---|
182 | * Failure: Returns "<null>" (never returns NULL pointer).
|
---|
183 | */
|
---|
184 | const char *virtioQueueGetName(VIRTIOHANDLE hVirtio, uint16_t qIdx);
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Removes descriptor chain from avail ring of indicated queue and converts it to
|
---|
188 | * scatter/gather buffer (whose segments contain guest phys. data pointers)
|
---|
189 | *
|
---|
190 | * @param hVirtio - Handle for VirtIO framework
|
---|
191 | * @param qIdx - Queue number
|
---|
192 | * @param ppInSegs - Address to store pointer to host-to-guest data retrieved from virtq as RTSGBUF
|
---|
193 | * @param ppOutSegs - Address to store pointer to host-to-guest data retrieved from virtq as RTSGBUF
|
---|
194 | *
|
---|
195 | * @returns status VINF_SUCCESS - Success
|
---|
196 | * VERR_INVALID_STATE - VirtIO not in ready state
|
---|
197 | */
|
---|
198 | int virtioQueueGet(VIRTIOHANDLE hVirtio, uint16_t qIdx, bool fRemove, PPRTSGBUF ppInSegs, PPRTSGBUF ppOutSegs);
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Same as virtioQueueGet() but leaves the item on the avail ring of the queue.
|
---|
202 | *
|
---|
203 | * @param hVirtio - Handle for VirtIO framework
|
---|
204 | * @param qIdx - Queue number
|
---|
205 | * @param ppInSegs - Address to store pointer to host-to-guest data retrieved from virtq as RTSGBUF
|
---|
206 | * @param ppOutSegs - Address to store pointer to host-to-guest data retrieved from virtq as RTSGBUF
|
---|
207 | *
|
---|
208 | * @returns VINF_SUCCESS - Success
|
---|
209 | * VERR_INVALID_STATE - VirtIO not in ready state
|
---|
210 | * VERR_NOT_AVAILABLE - Queue is empty
|
---|
211 | */
|
---|
212 | int virtioQueuePeek(VIRTIOHANDLE hVirtio, uint16_t qIdx, PPRTSGBUF ppInSegs, PPRTSGBUF ppOutSegs);
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Writes a scatter/gather buffer (whose segments point to virtual memory) to next
|
---|
216 | * available descriptor chain (consisting of segments pointing to guest phys. memory),
|
---|
217 | * for the indicated virtq. The data won't be seen by the guest until the next
|
---|
218 | * client call to virtioQueueSync(), which puts aforementioned descriptor
|
---|
219 | * chain's head index on the indicated virtq's used ring (see VirtIO 1.0
|
---|
220 | * specification, Section 2.4 "Virtqueues").
|
---|
221 | *
|
---|
222 | * @param hVirtio - Handle for VirtIO framework
|
---|
223 | * @param qIdx - Queue number
|
---|
224 | * @param pSgBuf - Caller's sgbuf of one or more virtual memory segments
|
---|
225 | * to write to the queue. This is useful because some kinds
|
---|
226 | * of transactions involve variable length subcomponents
|
---|
227 | * whose size can only be known near the time of writing.
|
---|
228 | * @parame fFence - If set put up copy fence (memory barrier) after
|
---|
229 | * copying to guest phys. mem.
|
---|
230 | *
|
---|
231 | * @returns VINF_SUCCESS - Success
|
---|
232 | * VERR_INVALID_STATE - VirtIO not in ready state
|
---|
233 | * VERR_NOT_AVAILABLE - Queue is empty
|
---|
234 | */
|
---|
235 | int virtioQueuePut(VIRTIOHANDLE hVirtio, uint16_t qIdx, PRTSGBUF pSgBuf, bool fFence);
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Updates the indicated virtq's "used ring" descriptor index to match the
|
---|
239 | * current write-head index, thus exposing the data added to the used ring by all
|
---|
240 | * virtioQueuePut() calls since the last sync. This should be called after one or
|
---|
241 | * more virtQueuePut() calls to inform the guest driver there is data in the queue.
|
---|
242 | * Explicit notifications (e.g. interrupt or MSI-X) will be sent to the guest,
|
---|
243 | * depending on VirtIO features negotiated and conditions, otherwise the guest
|
---|
244 | * will detect the update by polling. (see VirtIO 1.0
|
---|
245 | * specification, Section 2.4 "Virtqueues").
|
---|
246 | *
|
---|
247 | * @param hVirtio - Handle for VirtIO framework
|
---|
248 | * @param qIdx - Queue number
|
---|
249 | *
|
---|
250 | * @returns VINF_SUCCESS - Success
|
---|
251 | * VERR_INVALID_STATE - VirtIO not in ready state
|
---|
252 | */
|
---|
253 | int virtioQueueSync(VIRTIOHANDLE hVirtio, uint16_t qIdx);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Check if the associated queue is empty
|
---|
257 | *
|
---|
258 | * @param hVirtio - Handle for VirtIO framework
|
---|
259 | * @param qIdx - Queue number
|
---|
260 | *
|
---|
261 | * @returns true - Queue is empty or unavailable.
|
---|
262 | * false - Queue is available and has entries
|
---|
263 | */
|
---|
264 | bool virtioQueueIsEmpty (VIRTIOHANDLE hVirtio, uint16_t qIdx);
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Return queue enable state
|
---|
268 | *
|
---|
269 | * @param hVirtio - Handle for VirtIO framework
|
---|
270 | * @param qIdx - Queue number
|
---|
271 | * @param fEnabled - Flag indicating whether to enable queue or not
|
---|
272 | */
|
---|
273 | bool virtioIsQueueEnabled(VIRTIOHANDLE hVirtio, uint16_t qIdx);
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Enable or disable queue
|
---|
277 | *
|
---|
278 | * @param hVirtio - Handle for VirtIO framework
|
---|
279 | * @param qIdx - Queue number
|
---|
280 | * @param fEnabled - Flag indicating whether to enable queue or not
|
---|
281 | */
|
---|
282 | void virtioQueueEnable(VIRTIOHANDLE hVirtio, uint16_t qIdx, bool fEnabled);
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Request orderly teardown of VirtIO on host and guest
|
---|
286 | * @param hVirtio - Handle for VirtIO framework
|
---|
287 | *
|
---|
288 | */
|
---|
289 | void virtioResetAll(VIRTIOHANDLE hVirtio);
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * This sends notification ('kicks') guest driver to check queues for any new
|
---|
293 | * elements in the used queue to process. It should be called after resuming
|
---|
294 | * in case anything was added to the queues during suspend/quiescing and a
|
---|
295 | * notification was missed, to prevent the guest from stalling after suspend.
|
---|
296 | */
|
---|
297 | void virtioPropagateResumeNotification(VIRTIOHANDLE hVirtio);
|
---|
298 |
|
---|
299 |
|
---|
300 |
|
---|
301 | /** CLIENT MUST CALL ON RELOCATE CALLBACK! */
|
---|
302 | void virtioRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Constructor sets up the PCI device controller and VirtIO state
|
---|
306 | *
|
---|
307 | * @param pDevIns Device instance data
|
---|
308 | * @param pClientContext Opaque client context (such as state struct)
|
---|
309 | * @param pVirtio Device State
|
---|
310 | * @param pPciParams Values to populate industry standard PCI Configuration Space data structure
|
---|
311 | * @param pcszInstance Device instance name
|
---|
312 | * @param uDevSpecificFeatures VirtIO device-specific features offered by client
|
---|
313 | * @param devCapReadCallback Client handler to call upon guest read to device specific capabilities.
|
---|
314 | * @param devCapWriteCallback Client handler to call upon guest write to device specific capabilities.
|
---|
315 | * @param devStatusChangedCallback Client handler to call for major device status changes
|
---|
316 | * @param queueNotifiedCallback Client handler for guest-to-host notifications that avail queue has ring data
|
---|
317 | * @param ssmLiveExecCallback Client handler for SSM live exec
|
---|
318 | * @param ssmSaveExecCallback Client handler for SSM save exec
|
---|
319 | * @param ssmLoadExecCallback Client handler for SSM load exec
|
---|
320 | * @param ssmLoadDoneCallback Client handler for SSM load done
|
---|
321 | * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific configuration struct
|
---|
322 | * @param pDevSpecificCfg Address of client's VirtIO dev-specific configuration struct
|
---|
323 | */
|
---|
324 | int virtioConstruct(PPDMDEVINS pDevIns,
|
---|
325 | void *pClientContext,
|
---|
326 | VIRTIOHANDLE *phVirtio,
|
---|
327 | PVIRTIOPCIPARAMS pPciParams,
|
---|
328 | const char *pcszInstance,
|
---|
329 | uint64_t uDevSpecificFeatures,
|
---|
330 | PFNVIRTIODEVCAPREAD devCapReadCallback,
|
---|
331 | PFNVIRTIODEVCAPWRITE devCapWriteCallback,
|
---|
332 | PFNVIRTIOSTATUSCHANGED devStatusChangedCallback,
|
---|
333 | PFNVIRTIOQUEUENOTIFIED queueNotifiedCallback,
|
---|
334 | PFNSSMDEVLIVEEXEC ssmLiveExecCallback,
|
---|
335 | PFNSSMDEVSAVEEXEC ssmSaveExecCallback,
|
---|
336 | PFNSSMDEVLOADEXEC ssmLoadExecCallback,
|
---|
337 | PFNSSMDEVLOADDONE ssmLoadDoneCallback,
|
---|
338 | uint16_t cbDevSpecificCfg,
|
---|
339 | void *pDevSpecificCfg);
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Log memory-mapped I/O input or output value.
|
---|
343 | *
|
---|
344 | * This is designed to be invoked by macros that can make contextual assumptions
|
---|
345 | * (e.g. implicitly derive MACRO parameters from the invoking function). It is exposed
|
---|
346 | * for the VirtIO client doing the device-specific implementation in order to log in a
|
---|
347 | * similar fashion accesses to the device-specific MMIO configuration structure. Macros
|
---|
348 | * that leverage this function are in Virtio_1_0_impl.h and can be used as an example
|
---|
349 | * of how to use this effectively for the device-specific code.
|
---|
350 | *
|
---|
351 | * @param pszFunc - To avoid displaying this function's name via __FUNCTION__ or LogFunc()
|
---|
352 | * @param pszMember - Name of struct member
|
---|
353 | * @param pv - pointer to value
|
---|
354 | * @param cb - size of value
|
---|
355 | * @param uOffset - offset into member where value starts
|
---|
356 | * @param fWrite - True if write I/O
|
---|
357 | * @param fHasIndex - True if the member is indexed
|
---|
358 | * @param idx - The index if fHasIndex
|
---|
359 | */
|
---|
360 | void virtioLogMappedIoValue(const char *pszFunc, const char *pszMember, size_t uMemberSize,
|
---|
361 | const void *pv, uint32_t cb, uint32_t uOffset,
|
---|
362 | bool fWrite, bool fHasIndex, uint32_t idx);
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Does a formatted hex dump using Log(()), recommend using VIRTIO_HEX_DUMP() macro to
|
---|
366 | * control enabling of logging efficiently.
|
---|
367 | *
|
---|
368 | * @param pv - pointer to buffer to dump contents of
|
---|
369 | * @param cb - count of characters to dump from buffer
|
---|
370 | * @param uBase - base address of per-row address prefixing of hex output
|
---|
371 | * @param pszTitle - Optional title. If present displays title that lists
|
---|
372 | * provided text with value of cb to indicate size next to it.
|
---|
373 | */
|
---|
374 | void virtioHexDump(uint8_t *pv, size_t cb, uint32_t uBase, const char *pszTitle);
|
---|
375 |
|
---|
376 | #endif /* !VBOX_INCLUDED_SRC_VirtIO_Virtio_1_0_h */
|
---|