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