]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Include/Library/VirtioLib.h
Add Copyright notices and comments for Red Hat contributor.
[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
5 Copyright (C) 2012, Red Hat, Inc.\r
6\r
7 This program and the accompanying materials are licensed and made available\r
8 under the terms and conditions of the BSD License which accompanies this\r
9 distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#ifndef _VIRTIO_LIB_H_\r
18#define _VIRTIO_LIB_H_\r
19\r
20#include <Protocol/PciIo.h>\r
21#include <IndustryStandard/Virtio.h>\r
22\r
23/**\r
24\r
25 Write a word into Region 0 of the device specified by PciIo.\r
26\r
27 Region 0 must be an iomem region. This is an internal function for the\r
28 driver-specific VIRTIO_CFG_WRITE() macros.\r
29\r
30 @param[in] PciIo Target PCI device.\r
31\r
32 @param[in] FieldOffset Destination offset.\r
33\r
34 @param[in] FieldSize Destination field size, must be in { 1, 2, 4, 8 }.\r
35\r
36 @param[in] Value Little endian value to write, converted to UINT64.\r
37 The least significant FieldSize bytes will be used.\r
38\r
39\r
40 @return Status code returned by PciIo->Io.Write().\r
41\r
42**/\r
263559b8 43EFI_STATUS\r
9de0355b 44EFIAPI\r
263559b8 45VirtioWrite (\r
46 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
47 IN UINTN FieldOffset,\r
48 IN UINTN FieldSize,\r
49 IN UINT64 Value\r
50 );\r
51\r
52\r
53/**\r
54\r
55 Read a word from Region 0 of the device specified by PciIo.\r
56\r
57 Region 0 must be an iomem region. This is an internal function for the\r
58 driver-specific VIRTIO_CFG_READ() macros.\r
59\r
60 @param[in] PciIo Source PCI device.\r
61\r
62 @param[in] FieldOffset Source offset.\r
63\r
64 @param[in] FieldSize Source field size, must be in { 1, 2, 4, 8 }.\r
65\r
66 @param[in] BufferSize Number of bytes available in the target buffer. Must\r
67 equal FieldSize.\r
68\r
69 @param[out] Buffer Target buffer.\r
70\r
71\r
72 @return Status code returned by PciIo->Io.Read().\r
73\r
74**/\r
263559b8 75EFI_STATUS\r
9de0355b 76EFIAPI\r
263559b8 77VirtioRead (\r
78 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
79 IN UINTN FieldOffset,\r
80 IN UINTN FieldSize,\r
81 IN UINTN BufferSize,\r
82 OUT VOID *Buffer\r
83 );\r
84\r
85\r
86/**\r
87\r
88 Configure a virtio ring.\r
89\r
90 This function sets up internal storage (the guest-host communication area)\r
91 and lays out several "navigation" (ie. no-ownership) pointers to parts of\r
92 that storage.\r
93\r
94 Relevant sections from the virtio-0.9.5 spec:\r
95 - 1.1 Virtqueues,\r
96 - 2.3 Virtqueue Configuration.\r
97\r
98 @param[in] The number of descriptors to allocate for the\r
99 virtio ring, as requested by the host.\r
100\r
101 @param[out] Ring The virtio ring to set up.\r
102\r
103 @retval EFI_OUT_OF_RESOURCES AllocatePages() failed to allocate contiguous\r
104 pages for the requested QueueSize. Fields of\r
105 Ring have indeterminate value.\r
106\r
107 @retval EFI_SUCCESS Allocation and setup successful. Ring->Base\r
108 (and nothing else) is responsible for\r
109 deallocation.\r
110\r
111**/\r
112EFI_STATUS\r
113EFIAPI\r
114VirtioRingInit (\r
115 IN UINT16 QueueSize,\r
116 OUT VRING *Ring\r
117 );\r
118\r
119\r
120/**\r
121\r
122 Tear down the internal resources of a configured virtio ring.\r
123\r
124 The caller is responsible to stop the host from using this ring before\r
125 invoking this function: the VSTAT_DRIVER_OK bit must be clear in\r
126 VhdrDeviceStatus.\r
127\r
128 @param[out] Ring The virtio ring to clean up.\r
129\r
130**/\r
131VOID\r
132EFIAPI\r
133VirtioRingUninit (\r
134 IN OUT VRING *Ring\r
135 );\r
136\r
137\r
e371e7e5 138//\r
139// Internal use structure for tracking the submission of a multi-descriptor\r
140// request.\r
141//\r
142typedef struct {\r
143 UINT16 HeadIdx;\r
144 UINT16 NextAvailIdx;\r
145} DESC_INDICES;\r
146\r
147\r
148/**\r
149\r
150 Turn off interrupt notifications from the host, and prepare for appending\r
151 multiple descriptors to the virtio ring.\r
152\r
153 The calling driver must be in VSTAT_DRIVER_OK state.\r
154\r
155 @param[in out] Ring The virtio ring we intend to append descriptors to.\r
156\r
157 @param[out] Indices The DESC_INDICES structure to initialize.\r
158\r
159**/\r
160VOID\r
161EFIAPI\r
162VirtioPrepare (\r
163 IN OUT VRING *Ring,\r
164 OUT DESC_INDICES *Indices\r
165 );\r
166\r
167\r
263559b8 168/**\r
169\r
170 Append a contiguous buffer for transmission / reception via the virtio ring.\r
171\r
172 This function implements the following sections from virtio-0.9.5:\r
173 - 2.4.1.1 Placing Buffers into the Descriptor Table\r
174 - 2.4.1.2 Updating the Available Ring\r
175\r
176 Free space is taken as granted, since the individual drivers support only\r
177 synchronous requests and host side status is processed in lock-step with\r
178 request submission. It is the calling driver's responsibility to verify the\r
179 ring size in advance.\r
180\r
e371e7e5 181 The caller is responsible for initializing *Indices with VirtioPrepare()\r
182 first.\r
183\r
263559b8 184 @param[in out] Ring The virtio ring to append the buffer to, as a\r
185 descriptor.\r
186\r
187 @param [in] BufferPhysAddr (Guest pseudo-physical) start address of the\r
188 transmit / receive buffer.\r
189\r
190 @param [in] BufferSize Number of bytes to transmit or receive.\r
191\r
192 @param [in] Flags A bitmask of VRING_DESC_F_* flags. The caller\r
193 computes this mask dependent on further buffers\r
194 to append and transfer direction.\r
195 VRING_DESC_F_INDIRECT is unsupported. The\r
196 VRING_DESC.Next field is always set, but the\r
197 host only interprets it dependent on\r
198 VRING_DESC_F_NEXT.\r
199\r
e371e7e5 200 In *Indices:\r
201\r
263559b8 202 @param [in] HeadIdx The index identifying the head buffer (first\r
203 buffer appended) belonging to this same\r
204 request.\r
205\r
206 @param [in out] NextAvailIdx On input, the index identifying the next\r
207 descriptor available to carry the buffer. On\r
208 output, incremented by one, modulo 2^16.\r
209\r
210**/\r
211VOID\r
212EFIAPI\r
7fcacd6c 213VirtioAppendDesc (\r
e371e7e5 214 IN OUT VRING *Ring,\r
215 IN UINTN BufferPhysAddr,\r
216 IN UINT32 BufferSize,\r
217 IN UINT16 Flags,\r
218 IN OUT DESC_INDICES *Indices\r
219 );\r
220\r
221\r
222/**\r
223\r
224 Notify the host about appended descriptors and wait until it processes the\r
225 last one (ie. all of them).\r
226\r
227 @param[in] PciIo The target virtio PCI device to notify.\r
228\r
229 @param[in] VirtQueueId Identifies the queue for the target device.\r
230\r
231 @param[in out] Ring The virtio ring with descriptors to submit.\r
232\r
233 @param[in] Indices The function waits until the host processes\r
234 descriptors up to Indices->NextAvailIdx.\r
235\r
236\r
237 @return Error code from VirtioWrite() if it fails.\r
238\r
239 @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r
240\r
241**/\r
242EFI_STATUS\r
243EFIAPI\r
244VirtioFlush (\r
245 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
246 IN UINT16 VirtQueueId,\r
247 IN OUT VRING *Ring,\r
248 IN DESC_INDICES *Indices\r
263559b8 249 );\r
250\r
251#endif // _VIRTIO_LIB_H_\r