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