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