]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Include/Library/VirtioLib.h
OvmfPkg/MemEncryptSevLib: find pages of initial SMRAM save state map
[mirror_edk2.git] / OvmfPkg / Include / Library / VirtioLib.h
CommitLineData
263559b8 1/** @file\r
2\r
3 Declarations of utility functions used by virtio device drivers.\r
4\r
8bc951a2 5 Copyright (C) 2012-2016, Red Hat, Inc.\r
0a78d754 6 Copyright (C) 2017, AMD Inc, All rights reserved.<BR>\r
263559b8 7\r
8 This program and the accompanying materials are licensed and made available\r
9 under the terms and conditions of the BSD License which accompanies this\r
10 distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#ifndef _VIRTIO_LIB_H_\r
19#define _VIRTIO_LIB_H_\r
20\r
56f65ed8
OM
21#include <Protocol/VirtioDevice.h>\r
22\r
263559b8 23#include <IndustryStandard/Virtio.h>\r
24\r
263559b8 25\r
26/**\r
27\r
28 Configure a virtio ring.\r
29\r
30 This function sets up internal storage (the guest-host communication area)\r
31 and lays out several "navigation" (ie. no-ownership) pointers to parts of\r
32 that storage.\r
33\r
34 Relevant sections from the virtio-0.9.5 spec:\r
35 - 1.1 Virtqueues,\r
36 - 2.3 Virtqueue Configuration.\r
37\r
fc2c1543
BS
38 @param[in] VirtIo The virtio device which will use the ring.\r
39\r
263559b8 40 @param[in] The number of descriptors to allocate for the\r
41 virtio ring, as requested by the host.\r
42\r
43 @param[out] Ring The virtio ring to set up.\r
44\r
b0338c53
BS
45 @return Status codes propagated from\r
46 VirtIo->AllocateSharedPages().\r
263559b8 47\r
48 @retval EFI_SUCCESS Allocation and setup successful. Ring->Base\r
49 (and nothing else) is responsible for\r
50 deallocation.\r
51\r
52**/\r
53EFI_STATUS\r
54EFIAPI\r
55VirtioRingInit (\r
fc2c1543
BS
56 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
57 IN UINT16 QueueSize,\r
58 OUT VRING *Ring\r
263559b8 59 );\r
60\r
61\r
fef6becb
BS
62/**\r
63\r
64 Map the ring buffer so that it can be accessed equally by both guest\r
65 and hypervisor.\r
66\r
67 @param[in] VirtIo The virtio device instance.\r
68\r
69 @param[in] Ring The virtio ring to map.\r
70\r
71 @param[out] RingBaseShift A resulting translation offset, to be\r
72 passed to VirtIo->SetQueueAddress().\r
73\r
74 @param[out] Mapping A resulting token to pass to\r
75 VirtIo->UnmapSharedBuffer().\r
76\r
77 @return Status code from VirtIo->MapSharedBuffer()\r
78**/\r
79EFI_STATUS\r
80EFIAPI\r
81VirtioRingMap (\r
82 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
83 IN VRING *Ring,\r
84 OUT UINT64 *RingBaseShift,\r
85 OUT VOID **Mapping\r
86 );\r
87\r
263559b8 88/**\r
89\r
90 Tear down the internal resources of a configured virtio ring.\r
91\r
92 The caller is responsible to stop the host from using this ring before\r
93 invoking this function: the VSTAT_DRIVER_OK bit must be clear in\r
94 VhdrDeviceStatus.\r
95\r
fc2c1543
BS
96 @param[in] VirtIo The virtio device which was using the ring.\r
97\r
98 @param[out] Ring The virtio ring to clean up.\r
263559b8 99\r
100**/\r
101VOID\r
102EFIAPI\r
103VirtioRingUninit (\r
fc2c1543
BS
104 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
105 IN OUT VRING *Ring\r
263559b8 106 );\r
107\r
108\r
e371e7e5 109//\r
110// Internal use structure for tracking the submission of a multi-descriptor\r
111// request.\r
112//\r
113typedef struct {\r
635a3ca2 114 UINT16 HeadDescIdx;\r
115 UINT16 NextDescIdx;\r
e371e7e5 116} DESC_INDICES;\r
117\r
118\r
119/**\r
120\r
121 Turn off interrupt notifications from the host, and prepare for appending\r
122 multiple descriptors to the virtio ring.\r
123\r
124 The calling driver must be in VSTAT_DRIVER_OK state.\r
125\r
f2965f4e 126 @param[in,out] Ring The virtio ring we intend to append descriptors to.\r
e371e7e5 127\r
128 @param[out] Indices The DESC_INDICES structure to initialize.\r
129\r
130**/\r
131VOID\r
132EFIAPI\r
133VirtioPrepare (\r
134 IN OUT VRING *Ring,\r
135 OUT DESC_INDICES *Indices\r
136 );\r
137\r
138\r
263559b8 139/**\r
140\r
141 Append a contiguous buffer for transmission / reception via the virtio ring.\r
142\r
635a3ca2 143 This function implements the following section from virtio-0.9.5:\r
263559b8 144 - 2.4.1.1 Placing Buffers into the Descriptor Table\r
263559b8 145\r
146 Free space is taken as granted, since the individual drivers support only\r
147 synchronous requests and host side status is processed in lock-step with\r
148 request submission. It is the calling driver's responsibility to verify the\r
149 ring size in advance.\r
150\r
e371e7e5 151 The caller is responsible for initializing *Indices with VirtioPrepare()\r
152 first.\r
153\r
4b725858
BS
154 @param[in,out] Ring The virtio ring to append the buffer to,\r
155 as a descriptor.\r
11a5fdf4 156\r
4b725858
BS
157 @param[in] BufferDeviceAddress (Bus master device) start address of the\r
158 transmit / receive buffer.\r
11a5fdf4 159\r
4b725858 160 @param[in] BufferSize Number of bytes to transmit or receive.\r
11a5fdf4 161\r
4b725858
BS
162 @param[in] Flags A bitmask of VRING_DESC_F_* flags. The\r
163 caller computes this mask dependent on\r
164 further buffers to append and transfer\r
165 direction. VRING_DESC_F_INDIRECT is\r
166 unsupported. The VRING_DESC.Next field is\r
167 always set, but the host only interprets\r
168 it dependent on VRING_DESC_F_NEXT.\r
11a5fdf4 169\r
4b725858
BS
170 @param[in,out] Indices Indices->HeadDescIdx is not accessed.\r
171 On input, Indices->NextDescIdx identifies\r
172 the next descriptor to carry the buffer.\r
173 On output, Indices->NextDescIdx is\r
174 incremented by one, modulo 2^16.\r
263559b8 175\r
176**/\r
177VOID\r
178EFIAPI\r
7fcacd6c 179VirtioAppendDesc (\r
e371e7e5 180 IN OUT VRING *Ring,\r
4b725858 181 IN UINT64 BufferDeviceAddress,\r
e371e7e5 182 IN UINT32 BufferSize,\r
183 IN UINT16 Flags,\r
184 IN OUT DESC_INDICES *Indices\r
185 );\r
186\r
187\r
188/**\r
189\r
635a3ca2 190 Notify the host about the descriptor chain just built, and wait until the\r
191 host processes it.\r
e371e7e5 192\r
56f65ed8 193 @param[in] VirtIo The target virtio device to notify.\r
e371e7e5 194\r
195 @param[in] VirtQueueId Identifies the queue for the target device.\r
196\r
a7615fa8 197 @param[in,out] Ring The virtio ring with descriptors to submit.\r
e371e7e5 198\r
a7615fa8 199 @param[in] Indices Indices->NextDescIdx is not accessed.\r
200 Indices->HeadDescIdx identifies the head descriptor\r
201 of the descriptor chain.\r
e371e7e5 202\r
8bc951a2
LE
203 @param[out] UsedLen On success, the total number of bytes, consecutively\r
204 across the buffers linked by the descriptor chain,\r
205 that the host wrote. May be NULL if the caller\r
206 doesn't care, or can compute the same information\r
207 from device-specific request structures linked by the\r
208 descriptor chain.\r
e371e7e5 209\r
56f65ed8 210 @return Error code from VirtIo->SetQueueNotify() if it fails.\r
e371e7e5 211\r
212 @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r
213\r
214**/\r
215EFI_STATUS\r
216EFIAPI\r
217VirtioFlush (\r
56f65ed8
OM
218 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
219 IN UINT16 VirtQueueId,\r
220 IN OUT VRING *Ring,\r
8bc951a2
LE
221 IN DESC_INDICES *Indices,\r
222 OUT UINT32 *UsedLen OPTIONAL\r
263559b8 223 );\r
224\r
d0ece0d8
LE
225\r
226/**\r
227\r
228 Report the feature bits to the VirtIo 1.0 device that the VirtIo 1.0 driver\r
229 understands.\r
230\r
231 In VirtIo 1.0, a device can reject a self-inconsistent feature bitmap through\r
232 the new VSTAT_FEATURES_OK status bit. (For example if the driver requests a\r
233 higher level feature but clears a prerequisite feature.) This function is a\r
234 small wrapper around VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures() that also\r
235 verifies if the VirtIo 1.0 device accepts the feature bitmap.\r
236\r
237 @param[in] VirtIo Report feature bits to this device.\r
238\r
239 @param[in] Features The set of feature bits that the driver wishes\r
240 to report. The caller is responsible to perform\r
241 any masking before calling this function; the\r
242 value is directly written with\r
243 VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures().\r
244\r
245 @param[in,out] DeviceStatus On input, the status byte most recently written\r
246 to the device's status register. On output (even\r
247 on error), DeviceStatus will be updated so that\r
248 it is suitable for further status bit\r
249 manipulation and writing to the device's status\r
250 register.\r
251\r
252 @retval EFI_SUCCESS The device accepted the configuration in Features.\r
253\r
254 @return EFI_UNSUPPORTED The device rejected the configuration in Features.\r
255\r
256 @retval EFI_UNSUPPORTED VirtIo->Revision is smaller than 1.0.0.\r
257\r
258 @return Error codes from the SetGuestFeatures(),\r
259 SetDeviceStatus(), GetDeviceStatus() member\r
260 functions.\r
261\r
262**/\r
263EFI_STATUS\r
264EFIAPI\r
265Virtio10WriteFeatures (\r
266 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
267 IN UINT64 Features,\r
268 IN OUT UINT8 *DeviceStatus\r
269 );\r
270\r
0a78d754
BS
271/**\r
272 Provides the virtio device address required to access system memory from a\r
273 DMA bus master.\r
274\r
275 The interface follows the same usage pattern as defined in UEFI spec 2.6\r
276 (Section 13.2 PCI Root Bridge I/O Protocol)\r
277\r
278 The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r
279 with exception that NumberOfBytes is IN-only parameter. The function\r
280 maps all the bytes specified in NumberOfBytes param in one consecutive\r
281 range.\r
282\r
283 @param[in] VirtIo The virtio device for which the mapping is\r
284 requested.\r
285\r
286 @param[in] Operation Indicates if the bus master is going to\r
287 read or write to system memory.\r
288\r
289 @param[in] HostAddress The system memory address to map to shared\r
290 buffer address.\r
291\r
292 @param[in] NumberOfBytes Number of bytes to map.\r
293\r
294 @param[out] DeviceAddress The resulting shared map address for the\r
295 bus master to access the hosts HostAddress.\r
296\r
297 @param[out] Mapping A resulting token to pass to\r
298 VIRTIO_UNMAP_SHARED.\r
299\r
300\r
301 @retval EFI_SUCCESS The NumberOfBytes is succesfully mapped.\r
302 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a\r
303 common buffer.\r
304 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
305 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to\r
306 a lack of resources. This includes the case\r
307 when NumberOfBytes bytes cannot be mapped\r
308 in one consecutive range.\r
309 @retval EFI_DEVICE_ERROR The system hardware could not map the\r
310 requested address.\r
311**/\r
312EFI_STATUS\r
313EFIAPI\r
314VirtioMapAllBytesInSharedBuffer (\r
315 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
316 IN VIRTIO_MAP_OPERATION Operation,\r
317 IN VOID *HostAddress,\r
318 IN UINTN NumberOfBytes,\r
319 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
320 OUT VOID **Mapping\r
321 );\r
263559b8 322#endif // _VIRTIO_LIB_H_\r