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