]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/VirtioLib/VirtioLib.c
OvmfPkg/VirtioLib: add VirtioMapAllBytesInSharedBuffer() helper function
[mirror_edk2.git] / OvmfPkg / Library / VirtioLib / VirtioLib.c
CommitLineData
263559b8 1/** @file\r
2\r
3 Utility functions used by virtio device drivers.\r
4\r
8bc951a2 5 Copyright (C) 2012-2016, Red Hat, Inc.\r
56f65ed8 6 Portion of Copyright (C) 2013, ARM Ltd.\r
0a78d754 7 Copyright (C) 2017, AMD Inc, All rights reserved.<BR>\r
263559b8 8\r
9 This program and the accompanying materials are licensed and made available\r
10 under the terms and conditions of the BSD License which accompanies this\r
11 distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
e371e7e5 19#include <Library/BaseLib.h>\r
263559b8 20#include <Library/BaseMemoryLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
e371e7e5 23#include <Library/UefiBootServicesTableLib.h>\r
263559b8 24\r
25#include <Library/VirtioLib.h>\r
26\r
27\r
263559b8 28/**\r
29\r
30 Configure a virtio ring.\r
31\r
32 This function sets up internal storage (the guest-host communication area)\r
33 and lays out several "navigation" (ie. no-ownership) pointers to parts of\r
34 that storage.\r
35\r
36 Relevant sections from the virtio-0.9.5 spec:\r
37 - 1.1 Virtqueues,\r
38 - 2.3 Virtqueue Configuration.\r
39\r
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
57 IN UINT16 QueueSize,\r
58 OUT VRING *Ring\r
59 )\r
60{\r
61 UINTN RingSize;\r
62 volatile UINT8 *RingPagesPtr;\r
63\r
64 RingSize = ALIGN_VALUE (\r
65 sizeof *Ring->Desc * QueueSize +\r
66 sizeof *Ring->Avail.Flags +\r
67 sizeof *Ring->Avail.Idx +\r
68 sizeof *Ring->Avail.Ring * QueueSize +\r
69 sizeof *Ring->Avail.UsedEvent,\r
70 EFI_PAGE_SIZE);\r
71\r
72 RingSize += ALIGN_VALUE (\r
73 sizeof *Ring->Used.Flags +\r
74 sizeof *Ring->Used.Idx +\r
75 sizeof *Ring->Used.UsedElem * QueueSize +\r
76 sizeof *Ring->Used.AvailEvent,\r
77 EFI_PAGE_SIZE);\r
78\r
79 Ring->NumPages = EFI_SIZE_TO_PAGES (RingSize);\r
80 Ring->Base = AllocatePages (Ring->NumPages);\r
81 if (Ring->Base == NULL) {\r
82 return EFI_OUT_OF_RESOURCES;\r
83 }\r
84 SetMem (Ring->Base, RingSize, 0x00);\r
85 RingPagesPtr = Ring->Base;\r
86\r
87 Ring->Desc = (volatile VOID *) RingPagesPtr;\r
88 RingPagesPtr += sizeof *Ring->Desc * QueueSize;\r
89\r
90 Ring->Avail.Flags = (volatile VOID *) RingPagesPtr;\r
91 RingPagesPtr += sizeof *Ring->Avail.Flags;\r
92\r
93 Ring->Avail.Idx = (volatile VOID *) RingPagesPtr;\r
94 RingPagesPtr += sizeof *Ring->Avail.Idx;\r
95\r
96 Ring->Avail.Ring = (volatile VOID *) RingPagesPtr;\r
97 RingPagesPtr += sizeof *Ring->Avail.Ring * QueueSize;\r
98\r
99 Ring->Avail.UsedEvent = (volatile VOID *) RingPagesPtr;\r
100 RingPagesPtr += sizeof *Ring->Avail.UsedEvent;\r
101\r
102 RingPagesPtr = (volatile UINT8 *) Ring->Base +\r
103 ALIGN_VALUE (RingPagesPtr - (volatile UINT8 *) Ring->Base,\r
104 EFI_PAGE_SIZE);\r
105\r
106 Ring->Used.Flags = (volatile VOID *) RingPagesPtr;\r
107 RingPagesPtr += sizeof *Ring->Used.Flags;\r
108\r
109 Ring->Used.Idx = (volatile VOID *) RingPagesPtr;\r
110 RingPagesPtr += sizeof *Ring->Used.Idx;\r
111\r
112 Ring->Used.UsedElem = (volatile VOID *) RingPagesPtr;\r
113 RingPagesPtr += sizeof *Ring->Used.UsedElem * QueueSize;\r
114\r
115 Ring->Used.AvailEvent = (volatile VOID *) RingPagesPtr;\r
116 RingPagesPtr += sizeof *Ring->Used.AvailEvent;\r
117\r
118 Ring->QueueSize = QueueSize;\r
119 return EFI_SUCCESS;\r
120}\r
121\r
122\r
123/**\r
124\r
125 Tear down the internal resources of a configured virtio ring.\r
126\r
127 The caller is responsible to stop the host from using this ring before\r
128 invoking this function: the VSTAT_DRIVER_OK bit must be clear in\r
129 VhdrDeviceStatus.\r
130\r
131 @param[out] Ring The virtio ring to clean up.\r
132\r
133**/\r
134VOID\r
135EFIAPI\r
136VirtioRingUninit (\r
137 IN OUT VRING *Ring\r
138 )\r
139{\r
140 FreePages (Ring->Base, Ring->NumPages);\r
141 SetMem (Ring, sizeof *Ring, 0x00);\r
142}\r
143\r
144\r
e371e7e5 145/**\r
146\r
147 Turn off interrupt notifications from the host, and prepare for appending\r
148 multiple descriptors to the virtio ring.\r
149\r
150 The calling driver must be in VSTAT_DRIVER_OK state.\r
151\r
f2965f4e 152 @param[in,out] Ring The virtio ring we intend to append descriptors to.\r
e371e7e5 153\r
154 @param[out] Indices The DESC_INDICES structure to initialize.\r
155\r
156**/\r
157VOID\r
158EFIAPI\r
159VirtioPrepare (\r
160 IN OUT VRING *Ring,\r
161 OUT DESC_INDICES *Indices\r
162 )\r
163{\r
164 //\r
165 // Prepare for virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device.\r
166 // We're going to poll the answer, the host should not send an interrupt.\r
167 //\r
168 *Ring->Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
169\r
170 //\r
171 // Prepare for virtio-0.9.5, 2.4.1 Supplying Buffers to the Device.\r
172 //\r
635a3ca2 173 // Since we support only one in-flight descriptor chain, we can always build\r
174 // that chain starting at entry #0 of the descriptor table.\r
175 //\r
176 Indices->HeadDescIdx = 0;\r
177 Indices->NextDescIdx = Indices->HeadDescIdx;\r
e371e7e5 178}\r
179\r
180\r
263559b8 181/**\r
182\r
183 Append a contiguous buffer for transmission / reception via the virtio ring.\r
184\r
635a3ca2 185 This function implements the following section from virtio-0.9.5:\r
263559b8 186 - 2.4.1.1 Placing Buffers into the Descriptor Table\r
263559b8 187\r
188 Free space is taken as granted, since the individual drivers support only\r
189 synchronous requests and host side status is processed in lock-step with\r
190 request submission. It is the calling driver's responsibility to verify the\r
191 ring size in advance.\r
192\r
e371e7e5 193 The caller is responsible for initializing *Indices with VirtioPrepare()\r
194 first.\r
195\r
11a5fdf4 196 @param[in,out] Ring The virtio ring to append the buffer to, as a\r
197 descriptor.\r
198\r
199 @param[in] BufferPhysAddr (Guest pseudo-physical) start address of the\r
200 transmit / receive buffer.\r
201\r
202 @param[in] BufferSize Number of bytes to transmit or receive.\r
203\r
204 @param[in] Flags A bitmask of VRING_DESC_F_* flags. The caller\r
205 computes this mask dependent on further buffers to\r
206 append and transfer direction.\r
207 VRING_DESC_F_INDIRECT is unsupported. The\r
208 VRING_DESC.Next field is always set, but the host\r
209 only interprets it dependent on VRING_DESC_F_NEXT.\r
210\r
211 @param[in,out] Indices Indices->HeadDescIdx is not accessed.\r
212 On input, Indices->NextDescIdx identifies the next\r
213 descriptor to carry the buffer. On output,\r
214 Indices->NextDescIdx is incremented by one, modulo\r
215 2^16.\r
263559b8 216\r
217**/\r
218VOID\r
219EFIAPI\r
7fcacd6c 220VirtioAppendDesc (\r
e371e7e5 221 IN OUT VRING *Ring,\r
222 IN UINTN BufferPhysAddr,\r
223 IN UINT32 BufferSize,\r
224 IN UINT16 Flags,\r
225 IN OUT DESC_INDICES *Indices\r
263559b8 226 )\r
227{\r
228 volatile VRING_DESC *Desc;\r
229\r
635a3ca2 230 Desc = &Ring->Desc[Indices->NextDescIdx++ % Ring->QueueSize];\r
263559b8 231 Desc->Addr = BufferPhysAddr;\r
232 Desc->Len = BufferSize;\r
233 Desc->Flags = Flags;\r
635a3ca2 234 Desc->Next = Indices->NextDescIdx % Ring->QueueSize;\r
e371e7e5 235}\r
236\r
237\r
238/**\r
239\r
635a3ca2 240 Notify the host about the descriptor chain just built, and wait until the\r
241 host processes it.\r
e371e7e5 242\r
56f65ed8 243 @param[in] VirtIo The target virtio device to notify.\r
e371e7e5 244\r
245 @param[in] VirtQueueId Identifies the queue for the target device.\r
246\r
a7615fa8 247 @param[in,out] Ring The virtio ring with descriptors to submit.\r
e371e7e5 248\r
a7615fa8 249 @param[in] Indices Indices->NextDescIdx is not accessed.\r
250 Indices->HeadDescIdx identifies the head descriptor\r
251 of the descriptor chain.\r
e371e7e5 252\r
8bc951a2
LE
253 @param[out] UsedLen On success, the total number of bytes, consecutively\r
254 across the buffers linked by the descriptor chain,\r
255 that the host wrote. May be NULL if the caller\r
256 doesn't care, or can compute the same information\r
257 from device-specific request structures linked by the\r
258 descriptor chain.\r
e371e7e5 259\r
56f65ed8 260 @return Error code from VirtIo->SetQueueNotify() if it fails.\r
e371e7e5 261\r
262 @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r
263\r
264**/\r
265EFI_STATUS\r
266EFIAPI\r
267VirtioFlush (\r
56f65ed8
OM
268 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
269 IN UINT16 VirtQueueId,\r
270 IN OUT VRING *Ring,\r
8bc951a2
LE
271 IN DESC_INDICES *Indices,\r
272 OUT UINT32 *UsedLen OPTIONAL\r
e371e7e5 273 )\r
274{\r
635a3ca2 275 UINT16 NextAvailIdx;\r
8bc951a2 276 UINT16 LastUsedIdx;\r
e371e7e5 277 EFI_STATUS Status;\r
278 UINTN PollPeriodUsecs;\r
279\r
635a3ca2 280 //\r
281 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r
282 //\r
283 // It is not exactly clear from the wording of the virtio-0.9.5\r
284 // specification, but each entry in the Available Ring references only the\r
285 // head descriptor of any given descriptor chain.\r
286 //\r
287 NextAvailIdx = *Ring->Avail.Idx;\r
8bc951a2
LE
288 //\r
289 // (Due to our lock-step progress, this is where the host will produce the\r
290 // used element with the head descriptor's index in it.)\r
291 //\r
292 LastUsedIdx = NextAvailIdx;\r
635a3ca2 293 Ring->Avail.Ring[NextAvailIdx++ % Ring->QueueSize] =\r
294 Indices->HeadDescIdx % Ring->QueueSize;\r
295\r
e371e7e5 296 //\r
297 // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r
298 //\r
299 MemoryFence();\r
635a3ca2 300 *Ring->Avail.Idx = NextAvailIdx;\r
e371e7e5 301\r
302 //\r
303 // virtio-0.9.5, 2.4.1.4 Notifying the Device -- gratuitous notifications are\r
304 // OK.\r
305 //\r
306 MemoryFence();\r
56f65ed8 307 Status = VirtIo->SetQueueNotify (VirtIo, VirtQueueId);\r
e371e7e5 308 if (EFI_ERROR (Status)) {\r
309 return Status;\r
310 }\r
311\r
312 //\r
313 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
314 // Wait until the host processes and acknowledges our descriptor chain. The\r
315 // condition we use for polling is greatly simplified and relies on the\r
316 // synchronous, lock-step progress.\r
317 //\r
318 // Keep slowing down until we reach a poll period of slightly above 1 ms.\r
319 //\r
320 PollPeriodUsecs = 1;\r
321 MemoryFence();\r
635a3ca2 322 while (*Ring->Used.Idx != NextAvailIdx) {\r
e371e7e5 323 gBS->Stall (PollPeriodUsecs); // calls AcpiTimerLib::MicroSecondDelay\r
324\r
325 if (PollPeriodUsecs < 1024) {\r
326 PollPeriodUsecs *= 2;\r
327 }\r
328 MemoryFence();\r
329 }\r
330\r
dc9447bd 331 MemoryFence();\r
8bc951a2
LE
332\r
333 if (UsedLen != NULL) {\r
334 volatile CONST VRING_USED_ELEM *UsedElem;\r
335\r
336 UsedElem = &Ring->Used.UsedElem[LastUsedIdx % Ring->QueueSize];\r
337 ASSERT (UsedElem->Id == Indices->HeadDescIdx);\r
338 *UsedLen = UsedElem->Len;\r
339 }\r
340\r
e371e7e5 341 return EFI_SUCCESS;\r
263559b8 342}\r
d0ece0d8
LE
343\r
344\r
345/**\r
346\r
347 Report the feature bits to the VirtIo 1.0 device that the VirtIo 1.0 driver\r
348 understands.\r
349\r
350 In VirtIo 1.0, a device can reject a self-inconsistent feature bitmap through\r
351 the new VSTAT_FEATURES_OK status bit. (For example if the driver requests a\r
352 higher level feature but clears a prerequisite feature.) This function is a\r
353 small wrapper around VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures() that also\r
354 verifies if the VirtIo 1.0 device accepts the feature bitmap.\r
355\r
356 @param[in] VirtIo Report feature bits to this device.\r
357\r
358 @param[in] Features The set of feature bits that the driver wishes\r
359 to report. The caller is responsible to perform\r
360 any masking before calling this function; the\r
361 value is directly written with\r
362 VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures().\r
363\r
364 @param[in,out] DeviceStatus On input, the status byte most recently written\r
365 to the device's status register. On output (even\r
366 on error), DeviceStatus will be updated so that\r
367 it is suitable for further status bit\r
368 manipulation and writing to the device's status\r
369 register.\r
370\r
371 @retval EFI_SUCCESS The device accepted the configuration in Features.\r
372\r
373 @return EFI_UNSUPPORTED The device rejected the configuration in Features.\r
374\r
375 @retval EFI_UNSUPPORTED VirtIo->Revision is smaller than 1.0.0.\r
376\r
377 @return Error codes from the SetGuestFeatures(),\r
378 SetDeviceStatus(), GetDeviceStatus() member\r
379 functions.\r
380\r
381**/\r
382EFI_STATUS\r
383EFIAPI\r
384Virtio10WriteFeatures (\r
385 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
386 IN UINT64 Features,\r
387 IN OUT UINT8 *DeviceStatus\r
388 )\r
389{\r
390 EFI_STATUS Status;\r
391\r
392 if (VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
393 return EFI_UNSUPPORTED;\r
394 }\r
395\r
396 Status = VirtIo->SetGuestFeatures (VirtIo, Features);\r
397 if (EFI_ERROR (Status)) {\r
398 return Status;\r
399 }\r
400\r
401 *DeviceStatus |= VSTAT_FEATURES_OK;\r
402 Status = VirtIo->SetDeviceStatus (VirtIo, *DeviceStatus);\r
403 if (EFI_ERROR (Status)) {\r
404 return Status;\r
405 }\r
406\r
407 Status = VirtIo->GetDeviceStatus (VirtIo, DeviceStatus);\r
408 if (EFI_ERROR (Status)) {\r
409 return Status;\r
410 }\r
411\r
412 if ((*DeviceStatus & VSTAT_FEATURES_OK) == 0) {\r
413 Status = EFI_UNSUPPORTED;\r
414 }\r
415\r
416 return Status;\r
417}\r
0a78d754
BS
418\r
419/**\r
420 Provides the virtio device address required to access system memory from a\r
421 DMA bus master.\r
422\r
423 The interface follows the same usage pattern as defined in UEFI spec 2.6\r
424 (Section 13.2 PCI Root Bridge I/O Protocol)\r
425\r
426 The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r
427 with exception that NumberOfBytes is IN-only parameter. The function\r
428 maps all the bytes specified in NumberOfBytes param in one consecutive\r
429 range.\r
430\r
431 @param[in] VirtIo The virtio device for which the mapping is\r
432 requested.\r
433\r
434 @param[in] Operation Indicates if the bus master is going to\r
435 read or write to system memory.\r
436\r
437 @param[in] HostAddress The system memory address to map to shared\r
438 buffer address.\r
439\r
440 @param[in] NumberOfBytes Number of bytes to map.\r
441\r
442 @param[out] DeviceAddress The resulting shared map address for the\r
443 bus master to access the hosts HostAddress.\r
444\r
445 @param[out] Mapping A resulting token to pass to\r
446 VIRTIO_UNMAP_SHARED.\r
447\r
448\r
449 @retval EFI_SUCCESS The NumberOfBytes is succesfully mapped.\r
450 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a\r
451 common buffer.\r
452 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
453 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to\r
454 a lack of resources. This includes the case\r
455 when NumberOfBytes bytes cannot be mapped\r
456 in one consecutive range.\r
457 @retval EFI_DEVICE_ERROR The system hardware could not map the\r
458 requested address.\r
459**/\r
460EFI_STATUS\r
461EFIAPI\r
462VirtioMapAllBytesInSharedBuffer (\r
463 IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
464 IN VIRTIO_MAP_OPERATION Operation,\r
465 IN VOID *HostAddress,\r
466 IN UINTN NumberOfBytes,\r
467 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
468 OUT VOID **Mapping\r
469 )\r
470{\r
471 EFI_STATUS Status;\r
472 VOID *MapInfo;\r
473 UINTN Size;\r
474 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
475\r
476 Size = NumberOfBytes;\r
477 Status = VirtIo->MapSharedBuffer (\r
478 VirtIo,\r
479 Operation,\r
480 HostAddress,\r
481 &Size,\r
482 &PhysicalAddress,\r
483 &MapInfo\r
484 );\r
485 if (EFI_ERROR (Status)) {\r
486 return Status;\r
487 }\r
488\r
489 if (Size < NumberOfBytes) {\r
490 goto Failed;\r
491 }\r
492\r
493 *Mapping = MapInfo;\r
494 *DeviceAddress = PhysicalAddress;\r
495\r
496 return EFI_SUCCESS;\r
497\r
498Failed:\r
499 VirtIo->UnmapSharedBuffer (VirtIo, MapInfo);\r
500 return EFI_OUT_OF_RESOURCES;\r
501}\r