3 Utility functions used by virtio device drivers.
5 Copyright (C) 2012, Red Hat, Inc.
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <IndustryStandard/Pci22.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/VirtioLib.h>
29 Write a word into Region 0 of the device specified by PciIo.
31 Region 0 must be an iomem region. This is an internal function for the
32 driver-specific VIRTIO_CFG_WRITE() macros.
34 @param[in] PciIo Target PCI device.
36 @param[in] FieldOffset Destination offset.
38 @param[in] FieldSize Destination field size, must be in { 1, 2, 4, 8 }.
40 @param[in] Value Little endian value to write, converted to UINT64.
41 The least significant FieldSize bytes will be used.
44 @return Status code returned by PciIo->Io.Write().
50 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
57 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
62 Width
= EfiPciIoWidthUint8
;
66 Width
= EfiPciIoWidthUint16
;
74 Width
= EfiPciIoWidthUint32
;
81 return PciIo
->Io
.Write (
94 Read a word from Region 0 of the device specified by PciIo.
96 Region 0 must be an iomem region. This is an internal function for the
97 driver-specific VIRTIO_CFG_READ() macros.
99 @param[in] PciIo Source PCI device.
101 @param[in] FieldOffset Source offset.
103 @param[in] FieldSize Source field size, must be in { 1, 2, 4, 8 }.
105 @param[in] BufferSize Number of bytes available in the target buffer. Must
108 @param[out] Buffer Target buffer.
111 @return Status code returned by PciIo->Io.Read().
117 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
118 IN UINTN FieldOffset
,
125 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
127 ASSERT (FieldSize
== BufferSize
);
132 Width
= EfiPciIoWidthUint8
;
136 Width
= EfiPciIoWidthUint16
;
144 Width
= EfiPciIoWidthUint32
;
151 return PciIo
->Io
.Read (
164 Configure a virtio ring.
166 This function sets up internal storage (the guest-host communication area)
167 and lays out several "navigation" (ie. no-ownership) pointers to parts of
170 Relevant sections from the virtio-0.9.5 spec:
172 - 2.3 Virtqueue Configuration.
174 @param[in] The number of descriptors to allocate for the
175 virtio ring, as requested by the host.
177 @param[out] Ring The virtio ring to set up.
179 @retval EFI_OUT_OF_RESOURCES AllocatePages() failed to allocate contiguous
180 pages for the requested QueueSize. Fields of
181 Ring have indeterminate value.
183 @retval EFI_SUCCESS Allocation and setup successful. Ring->Base
184 (and nothing else) is responsible for
196 volatile UINT8
*RingPagesPtr
;
198 RingSize
= ALIGN_VALUE (
199 sizeof *Ring
->Desc
* QueueSize
+
200 sizeof *Ring
->Avail
.Flags
+
201 sizeof *Ring
->Avail
.Idx
+
202 sizeof *Ring
->Avail
.Ring
* QueueSize
+
203 sizeof *Ring
->Avail
.UsedEvent
,
206 RingSize
+= ALIGN_VALUE (
207 sizeof *Ring
->Used
.Flags
+
208 sizeof *Ring
->Used
.Idx
+
209 sizeof *Ring
->Used
.UsedElem
* QueueSize
+
210 sizeof *Ring
->Used
.AvailEvent
,
213 Ring
->NumPages
= EFI_SIZE_TO_PAGES (RingSize
);
214 Ring
->Base
= AllocatePages (Ring
->NumPages
);
215 if (Ring
->Base
== NULL
) {
216 return EFI_OUT_OF_RESOURCES
;
218 SetMem (Ring
->Base
, RingSize
, 0x00);
219 RingPagesPtr
= Ring
->Base
;
221 Ring
->Desc
= (volatile VOID
*) RingPagesPtr
;
222 RingPagesPtr
+= sizeof *Ring
->Desc
* QueueSize
;
224 Ring
->Avail
.Flags
= (volatile VOID
*) RingPagesPtr
;
225 RingPagesPtr
+= sizeof *Ring
->Avail
.Flags
;
227 Ring
->Avail
.Idx
= (volatile VOID
*) RingPagesPtr
;
228 RingPagesPtr
+= sizeof *Ring
->Avail
.Idx
;
230 Ring
->Avail
.Ring
= (volatile VOID
*) RingPagesPtr
;
231 RingPagesPtr
+= sizeof *Ring
->Avail
.Ring
* QueueSize
;
233 Ring
->Avail
.UsedEvent
= (volatile VOID
*) RingPagesPtr
;
234 RingPagesPtr
+= sizeof *Ring
->Avail
.UsedEvent
;
236 RingPagesPtr
= (volatile UINT8
*) Ring
->Base
+
237 ALIGN_VALUE (RingPagesPtr
- (volatile UINT8
*) Ring
->Base
,
240 Ring
->Used
.Flags
= (volatile VOID
*) RingPagesPtr
;
241 RingPagesPtr
+= sizeof *Ring
->Used
.Flags
;
243 Ring
->Used
.Idx
= (volatile VOID
*) RingPagesPtr
;
244 RingPagesPtr
+= sizeof *Ring
->Used
.Idx
;
246 Ring
->Used
.UsedElem
= (volatile VOID
*) RingPagesPtr
;
247 RingPagesPtr
+= sizeof *Ring
->Used
.UsedElem
* QueueSize
;
249 Ring
->Used
.AvailEvent
= (volatile VOID
*) RingPagesPtr
;
250 RingPagesPtr
+= sizeof *Ring
->Used
.AvailEvent
;
252 Ring
->QueueSize
= QueueSize
;
259 Tear down the internal resources of a configured virtio ring.
261 The caller is responsible to stop the host from using this ring before
262 invoking this function: the VSTAT_DRIVER_OK bit must be clear in
265 @param[out] Ring The virtio ring to clean up.
274 FreePages (Ring
->Base
, Ring
->NumPages
);
275 SetMem (Ring
, sizeof *Ring
, 0x00);
281 Turn off interrupt notifications from the host, and prepare for appending
282 multiple descriptors to the virtio ring.
284 The calling driver must be in VSTAT_DRIVER_OK state.
286 @param[in out] Ring The virtio ring we intend to append descriptors to.
288 @param[out] Indices The DESC_INDICES structure to initialize.
295 OUT DESC_INDICES
*Indices
299 // Prepare for virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device.
300 // We're going to poll the answer, the host should not send an interrupt.
302 *Ring
->Avail
.Flags
= (UINT16
) VRING_AVAIL_F_NO_INTERRUPT
;
305 // Prepare for virtio-0.9.5, 2.4.1 Supplying Buffers to the Device.
307 Indices
->HeadIdx
= *Ring
->Avail
.Idx
;
308 Indices
->NextAvailIdx
= Indices
->HeadIdx
;
314 Append a contiguous buffer for transmission / reception via the virtio ring.
316 This function implements the following sections from virtio-0.9.5:
317 - 2.4.1.1 Placing Buffers into the Descriptor Table
318 - 2.4.1.2 Updating the Available Ring
320 Free space is taken as granted, since the individual drivers support only
321 synchronous requests and host side status is processed in lock-step with
322 request submission. It is the calling driver's responsibility to verify the
323 ring size in advance.
325 The caller is responsible for initializing *Indices with VirtioPrepare()
328 @param[in out] Ring The virtio ring to append the buffer to, as a
331 @param [in] BufferPhysAddr (Guest pseudo-physical) start address of the
332 transmit / receive buffer.
334 @param [in] BufferSize Number of bytes to transmit or receive.
336 @param [in] Flags A bitmask of VRING_DESC_F_* flags. The caller
337 computes this mask dependent on further buffers
338 to append and transfer direction.
339 VRING_DESC_F_INDIRECT is unsupported. The
340 VRING_DESC.Next field is always set, but the
341 host only interprets it dependent on
346 @param [in] HeadIdx The index identifying the head buffer (first
347 buffer appended) belonging to this same
350 @param [in out] NextAvailIdx On input, the index identifying the next
351 descriptor available to carry the buffer. On
352 output, incremented by one, modulo 2^16.
359 IN UINTN BufferPhysAddr
,
360 IN UINT32 BufferSize
,
362 IN OUT DESC_INDICES
*Indices
365 volatile VRING_DESC
*Desc
;
367 Desc
= &Ring
->Desc
[Indices
->NextAvailIdx
% Ring
->QueueSize
];
368 Desc
->Addr
= BufferPhysAddr
;
369 Desc
->Len
= BufferSize
;
371 Ring
->Avail
.Ring
[Indices
->NextAvailIdx
++ % Ring
->QueueSize
] =
372 Indices
->HeadIdx
% Ring
->QueueSize
;
373 Desc
->Next
= Indices
->NextAvailIdx
% Ring
->QueueSize
;
379 Notify the host about appended descriptors and wait until it processes the
380 last one (ie. all of them).
382 @param[in] PciIo The target virtio PCI device to notify.
384 @param[in] VirtQueueId Identifies the queue for the target device.
386 @param[in out] Ring The virtio ring with descriptors to submit.
388 @param[in] Indices The function waits until the host processes
389 descriptors up to Indices->NextAvailIdx.
392 @return Error code from VirtioWrite() if it fails.
394 @retval EFI_SUCCESS Otherwise, the host processed all descriptors.
400 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
401 IN UINT16 VirtQueueId
,
403 IN DESC_INDICES
*Indices
407 UINTN PollPeriodUsecs
;
410 // virtio-0.9.5, 2.4.1.3 Updating the Index Field
413 *Ring
->Avail
.Idx
= Indices
->NextAvailIdx
;
416 // virtio-0.9.5, 2.4.1.4 Notifying the Device -- gratuitous notifications are
420 Status
= VirtioWrite (
422 OFFSET_OF (VIRTIO_HDR
, VhdrQueueNotify
),
426 if (EFI_ERROR (Status
)) {
431 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
432 // Wait until the host processes and acknowledges our descriptor chain. The
433 // condition we use for polling is greatly simplified and relies on the
434 // synchronous, lock-step progress.
436 // Keep slowing down until we reach a poll period of slightly above 1 ms.
440 while (*Ring
->Used
.Idx
!= Indices
->NextAvailIdx
) {
441 gBS
->Stall (PollPeriodUsecs
); // calls AcpiTimerLib::MicroSecondDelay
443 if (PollPeriodUsecs
< 1024) {
444 PollPeriodUsecs
*= 2;