[edk2] [PATCH v2 03/10] OvmfPkg/VirtioDevice.h: Introduced VIRTIO_DEVICE_PROTOCOL protocol

Subject: [edk2] [PATCH v2 03/10] OvmfPkg/VirtioDevice.h: Introduced VIRTIO_DEVICE_PROTOCOL protocol

From: Olivier Martin <olivier.martin@arm.com>

To: edk2-devel@lists.sourceforge.net

Date: 2013-09-19 22:56:45

This protocol introduces an abstraction to access the VirtIo Configuration and Device spaces.
The registers in these spaces are located at a different offset and have a different width
whether the transport layer is either PCI or MMIO. This protocol would also allow to support
VirtIo PCI devices with MSI-X capability in a transparent way (Device space is at a different
offset when a PCIe device has MSI-X capability).

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Olivier Martin 
---
 OvmfPkg/Include/Protocol/VirtioDevice.h |  284 +++++++++++++++++++++++++++++++
 OvmfPkg/OvmfPkg.dec                     |    1 +
 2 files changed, 285 insertions(+), 0 deletions(-)
 create mode 100644 OvmfPkg/Include/Protocol/VirtioDevice.h

diff --git a/OvmfPkg/Include/Protocol/VirtioDevice.h b/OvmfPkg/Include/Protocol/VirtioDevice.h
new file mode 100644
index 0000000..0b83403
--- /dev/null
+++ b/OvmfPkg/Include/Protocol/VirtioDevice.h
@@ -0,0 +1,284 @@
+/** @file
+  Virtio Device
+
+  Copyright (c) 2013, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef __VIRTIO_DEVICE_H__ +#define __VIRTIO_DEVICE_H__ + +#define VIRTIO_DEVICE_PROTOCOL_GUID \ + { \ + 0xfa920010, 0x6785, 0x4941, {0xb6, 0xec, 0x49, 0x8c, 0x57, 0x9f, 0x16, 0x0a } \ + } + +typedef struct _VIRTIO_DEVICE_PROTOCOL VIRTIO_DEVICE_PROTOCOL; + +/** + + Read a word from the device-specific I/O region of the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] FieldOffset Source offset. + + @param[in] FieldSize Source field size in bytes, must be in {1, 2, 4, 8}. + + @param[in] BufferSize Number of bytes available in the target buffer. Must + equal FieldSize. + + @param[out] Buffer Target buffer. + + @retval EFI_SUCCESS The data was read successfully. + @retval EFI_UNSUPPORTED The underlying IO device doesn't support the + provided address offset and read size. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +typedef EFI_STATUS (*VIRTIO_DEVICE_READ) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINTN FieldOffset, + IN UINTN FieldSize, + IN UINTN BufferSize, + OUT VOID *Buffer + ); + +/** + + Write a word to the device-specific I/O region of the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] FieldOffset Destination offset. + + @param[in] FieldSize Destination field size in bytes, + must be in {1, 2, 4, 8}. + + @param[out] Value Value to write. + + @retval EFI_SUCCESS The data was written successfully. + @retval EFI_UNSUPPORTED The underlying IO device doesn't support the + provided address offset and read size. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + +**/ +typedef EFI_STATUS (*VIRTIO_DEVICE_WRITE) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINTN FieldOffset, + IN UINTN FieldSize, + IN UINT64 Value + ); + +/** + Read the device features field from the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @return The 32-bit device features field. + +**/ +typedef UINT32 (*VIRTIO_GET_DEVICE_FEATURES) ( + IN VIRTIO_DEVICE_PROTOCOL *This + ); + +/** + Read the guest features field from the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @return The 32-bit guest features field. + +**/ +typedef UINT32 (*VIRTIO_GET_GUEST_FEATURES) ( + IN VIRTIO_DEVICE_PROTOCOL *This + ); + +/** + Write the guest features field in the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] Features The 32-bit guest guest features field + +**/ +typedef EFI_STATUS (*VIRTIO_SET_GUEST_FEATURES) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT32 Features + ); + +/** + Read the queue address field from the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @return The 32-bit queue address field. + +**/ +typedef UINT32 (*VIRTIO_GET_QUEUE_ADDRESS) ( + IN VIRTIO_DEVICE_PROTOCOL *This + ); + +/** + Write the queue address field in the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] Address The 32-bit Queue Address field + +**/ +typedef EFI_STATUS (*VIRTIO_SET_QUEUE_ADDRESS) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT32 Address + ); + +/** + + Write the queue select field in the Virtio Header. + + Writing to the queue select field sets the index of the queue to which + operations such as SetQueueAlign and GetQueueNumMax apply. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] Index The index of the queue to select + +**/ +typedef EFI_STATUS (*VIRTIO_SET_QUEUE_SEL) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT16 Index + ); + +/** + + Write the queue notify field in the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] Address The 32-bit Queue Notify field + +**/ +typedef EFI_STATUS (*VIRTIO_SET_QUEUE_NOTIFY) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT16 Index + ); + +/** + Write the queue alignment field in the Virtio Header. + + The queue to which the alignment applies is selected by the Queue Select + field. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] Alignment The alignment boundary of the Used Ring in bytes. + Must be a power of 2. + +**/ +typedef EFI_STATUS (*VIRTIO_SET_QUEUE_ALIGN) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT32 Alignment + ); + +/** + + Get the size of the virtqueue selected by the queue select field. + + See Virtio spec Section 2.3 + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @return The size of the virtqueue in bytes. + Always a power of 2. + +**/ +typedef UINT16 (*VIRTIO_GET_QUEUE_NUM_MAX) ( + IN VIRTIO_DEVICE_PROTOCOL *This + ); + +/** + + Write to the QueueNum field in the Virtio Header. + + This function only applies to Virtio-MMIO and may be a stub for other + implementations. See Virtio Spec appendix X. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] QueueSize The number of elements in the queue. + +**/ +typedef VOID (*VIRTIO_SET_QUEUE_NUM) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT16 QueueSize + ); + +/** + + Write the QueueNum field in the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + +**/ +typedef UINT8 (*VIRTIO_GET_DEVICE_STATUS) ( + IN VIRTIO_DEVICE_PROTOCOL *This + ); + +/** + + Write the DeviceStatus field in the Virtio Header. + + @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL + + @param[in] DeviceStatus The 8-bit value for the Device status field +**/ +typedef EFI_STATUS (*VIRTIO_SET_DEVICE_STATUS) ( + IN VIRTIO_DEVICE_PROTOCOL *This, + IN UINT8 DeviceStatus + ); + + +/// +/// This protocol provides an abstraction over the VirtIo transport layer +/// +struct _VIRTIO_DEVICE_PROTOCOL { + /// From the Virtio Spec + INT32 SubSystemDeviceId; + + VIRTIO_GET_DEVICE_FEATURES GetDeviceFeatures; + VIRTIO_GET_GUEST_FEATURES GetGuestFeatures; + VIRTIO_SET_GUEST_FEATURES SetGuestFeatures; + + VIRTIO_GET_QUEUE_ADDRESS GetQueueAddress; + VIRTIO_SET_QUEUE_ADDRESS SetQueueAddress; + + VIRTIO_SET_QUEUE_SEL SetQueueSel; + + VIRTIO_SET_QUEUE_NOTIFY SetQueueNotify; + + VIRTIO_SET_QUEUE_ALIGN SetQueueAlign; + + VIRTIO_GET_QUEUE_NUM_MAX GetQueueNumMax; + VIRTIO_SET_QUEUE_NUM SetQueueNum; + + VIRTIO_GET_DEVICE_STATUS GetDeviceStatus; + VIRTIO_SET_DEVICE_STATUS SetDeviceStatus; + + // Functions to read/write Device Specific headers + VIRTIO_DEVICE_WRITE WriteDevice; + VIRTIO_DEVICE_READ ReadDevice; +}; + +extern EFI_GUID gVirtioDeviceProtocolGuid; + +#endif diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index d874f0c..7ccc025 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -44,6 +44,7 @@ gEfiXenInfoGuid = {0xd3b46f3b, 0xd441, 0x1244, {0x9a, 0x12, 0x0, 0x12, 0x27, 0x3f, 0xc1, 0x4d}} [Protocols] + gVirtioDeviceProtocolGuid = {0xfa920010, 0x6785, 0x4941, {0xb6, 0xec, 0x49, 0x8c, 0x57, 0x9f, 0x16, 0x0a}} gBlockMmioProtocolGuid = {0x6b558ce3, 0x69e5, 0x4c67, {0xa6, 0x34, 0xf7, 0xfe, 0x72, 0xad, 0xbe, 0x84}} [PcdsFixedAtBuild] -- 1.7.0.4 ------------------------------------------------------------------------------ LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99! 1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint 2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk _______________________________________________ edk2-devel mailing list edk2-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/edk2-devel