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