]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Include/Library/VirtioLib.h
OvmfPkg/VirtioLib: alloc VRING buffer with AllocateSharedPages()
[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
11a5fdf4 154 @param[in,out] Ring The virtio ring to append the buffer to, as a\r
155 descriptor.\r
156\r
157 @param[in] BufferPhysAddr (Guest pseudo-physical) start address of the\r
158 transmit / receive buffer.\r
159\r
160 @param[in] BufferSize Number of bytes to transmit or receive.\r
161\r
162 @param[in] Flags A bitmask of VRING_DESC_F_* flags. The caller\r
163 computes this mask dependent on further buffers to\r
164 append and transfer direction.\r
165 VRING_DESC_F_INDIRECT is unsupported. The\r
166 VRING_DESC.Next field is always set, but the host\r
167 only interprets it dependent on VRING_DESC_F_NEXT.\r
168\r
169 @param[in,out] Indices Indices->HeadDescIdx is not accessed.\r
170 On input, Indices->NextDescIdx identifies the next\r
171 descriptor to carry the buffer. On output,\r
172 Indices->NextDescIdx is incremented by one, modulo\r
173 2^16.\r
263559b8 174\r
175**/\r
176VOID\r
177EFIAPI\r
7fcacd6c 178VirtioAppendDesc (\r
e371e7e5 179 IN OUT VRING *Ring,\r
180 IN UINTN BufferPhysAddr,\r
181 IN UINT32 BufferSize,\r
182 IN UINT16 Flags,\r
183 IN OUT DESC_INDICES *Indices\r
184 );\r
185\r
186\r
187/**\r
188\r
635a3ca2 189 Notify the host about the descriptor chain just built, and wait until the\r
190 host processes it.\r
e371e7e5 191\r
56f65ed8 192 @param[in] VirtIo The target virtio device to notify.\r
e371e7e5 193\r
194 @param[in] VirtQueueId Identifies the queue for the target device.\r
195\r
a7615fa8 196 @param[in,out] Ring The virtio ring with descriptors to submit.\r
e371e7e5 197\r
a7615fa8 198 @param[in] Indices Indices->NextDescIdx is not accessed.\r
199 Indices->HeadDescIdx identifies the head descriptor\r
200 of the descriptor chain.\r
e371e7e5 201\r
8bc951a2
LE
202 @param[out] UsedLen On success, the total number of bytes, consecutively\r
203 across the buffers linked by the descriptor chain,\r
204 that the host wrote. May be NULL if the caller\r
205 doesn't care, or can compute the same information\r
206 from device-specific request structures linked by the\r
207 descriptor chain.\r
e371e7e5 208\r
56f65ed8 209 @return Error code from VirtIo->SetQueueNotify() if it fails.\r
e371e7e5 210\r
211 @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r
212\r
213**/\r
214EFI_STATUS\r
215EFIAPI\r
216VirtioFlush (\r
56f65ed8
OM
217 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
218 IN UINT16 VirtQueueId,\r
219 IN OUT VRING *Ring,\r
8bc951a2
LE
220 IN DESC_INDICES *Indices,\r
221 OUT UINT32 *UsedLen OPTIONAL\r
263559b8 222 );\r
223\r
d0ece0d8
LE
224\r
225/**\r
226\r
227 Report the feature bits to the VirtIo 1.0 device that the VirtIo 1.0 driver\r
228 understands.\r
229\r
230 In VirtIo 1.0, a device can reject a self-inconsistent feature bitmap through\r
231 the new VSTAT_FEATURES_OK status bit. (For example if the driver requests a\r
232 higher level feature but clears a prerequisite feature.) This function is a\r
233 small wrapper around VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures() that also\r
234 verifies if the VirtIo 1.0 device accepts the feature bitmap.\r
235\r
236 @param[in] VirtIo Report feature bits to this device.\r
237\r
238 @param[in] Features The set of feature bits that the driver wishes\r
239 to report. The caller is responsible to perform\r
240 any masking before calling this function; the\r
241 value is directly written with\r
242 VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures().\r
243\r
244 @param[in,out] DeviceStatus On input, the status byte most recently written\r
245 to the device's status register. On output (even\r
246 on error), DeviceStatus will be updated so that\r
247 it is suitable for further status bit\r
248 manipulation and writing to the device's status\r
249 register.\r
250\r
251 @retval EFI_SUCCESS The device accepted the configuration in Features.\r
252\r
253 @return EFI_UNSUPPORTED The device rejected the configuration in Features.\r
254\r
255 @retval EFI_UNSUPPORTED VirtIo->Revision is smaller than 1.0.0.\r
256\r
257 @return Error codes from the SetGuestFeatures(),\r
258 SetDeviceStatus(), GetDeviceStatus() member\r
259 functions.\r
260\r
261**/\r
262EFI_STATUS\r
263EFIAPI\r
264Virtio10WriteFeatures (\r
265 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
266 IN UINT64 Features,\r
267 IN OUT UINT8 *DeviceStatus\r
268 );\r
269\r
0a78d754
BS
270/**\r
271 Provides the virtio device address required to access system memory from a\r
272 DMA bus master.\r
273\r
274 The interface follows the same usage pattern as defined in UEFI spec 2.6\r
275 (Section 13.2 PCI Root Bridge I/O Protocol)\r
276\r
277 The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r
278 with exception that NumberOfBytes is IN-only parameter. The function\r
279 maps all the bytes specified in NumberOfBytes param in one consecutive\r
280 range.\r
281\r
282 @param[in] VirtIo The virtio device for which the mapping is\r
283 requested.\r
284\r
285 @param[in] Operation Indicates if the bus master is going to\r
286 read or write to system memory.\r
287\r
288 @param[in] HostAddress The system memory address to map to shared\r
289 buffer address.\r
290\r
291 @param[in] NumberOfBytes Number of bytes to map.\r
292\r
293 @param[out] DeviceAddress The resulting shared map address for the\r
294 bus master to access the hosts HostAddress.\r
295\r
296 @param[out] Mapping A resulting token to pass to\r
297 VIRTIO_UNMAP_SHARED.\r
298\r
299\r
300 @retval EFI_SUCCESS The NumberOfBytes is succesfully mapped.\r
301 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a\r
302 common buffer.\r
303 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
304 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to\r
305 a lack of resources. This includes the case\r
306 when NumberOfBytes bytes cannot be mapped\r
307 in one consecutive range.\r
308 @retval EFI_DEVICE_ERROR The system hardware could not map the\r
309 requested address.\r
310**/\r
311EFI_STATUS\r
312EFIAPI\r
313VirtioMapAllBytesInSharedBuffer (\r
314 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
315 IN VIRTIO_MAP_OPERATION Operation,\r
316 IN VOID *HostAddress,\r
317 IN UINTN NumberOfBytes,\r
318 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
319 OUT VOID **Mapping\r
320 );\r
263559b8 321#endif // _VIRTIO_LIB_H_\r