From e0c23cba5eaeb6c934a10ecdabcb235ef5d63799 Mon Sep 17 00:00:00 2001 From: Abner Chang Date: Mon, 11 Oct 2021 21:39:13 +0800 Subject: ArmVirtPkg/VirtioFdtDxe: Relocate VirtioFdtDxe to OvmfPkg/Fdt Relocate VirtioFdtDxe to OvmfPkg/Fdt, this driver is leverage by both ARM and RISC-V archs. Signed-off-by: Abner Chang Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Sami Mujawar Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Daniel Schaefer Cc: Sunil V L Reviewed-by: Daniel Schaefer Reviewed-by: Sunil V L Acked-by: Gerd Hoffmann Acked-by: Jiewen Yao --- OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.c | 118 ++++++++++++++++++++++++++++++ OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.inf | 45 ++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.c create mode 100644 OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.inf (limited to 'OvmfPkg/Fdt') diff --git a/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.c b/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.c new file mode 100644 index 0000000000..9625693b15 --- /dev/null +++ b/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.c @@ -0,0 +1,118 @@ +/** @file +* Virtio FDT client protocol driver for virtio,mmio DT node +* +* Copyright (c) 2014 - 2016, Linaro Ltd. All rights reserved.
+* +* SPDX-License-Identifier: BSD-2-Clause-Patent +* +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#pragma pack (1) +typedef struct { + VENDOR_DEVICE_PATH Vendor; + UINT64 PhysBase; + EFI_DEVICE_PATH_PROTOCOL End; +} VIRTIO_TRANSPORT_DEVICE_PATH; +#pragma pack () + +EFI_STATUS +EFIAPI +InitializeVirtioFdtDxe ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status, FindNodeStatus; + FDT_CLIENT_PROTOCOL *FdtClient; + INT32 Node; + CONST UINT64 *Reg; + UINT32 RegSize; + VIRTIO_TRANSPORT_DEVICE_PATH *DevicePath; + EFI_HANDLE Handle; + UINT64 RegBase; + + Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL, + (VOID **)&FdtClient); + ASSERT_EFI_ERROR (Status); + + for (FindNodeStatus = FdtClient->FindCompatibleNode (FdtClient, + "virtio,mmio", &Node); + !EFI_ERROR (FindNodeStatus); + FindNodeStatus = FdtClient->FindNextCompatibleNode (FdtClient, + "virtio,mmio", Node, &Node)) { + + Status = FdtClient->GetNodeProperty (FdtClient, Node, "reg", + (CONST VOID **)&Reg, &RegSize); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "%a: GetNodeProperty () failed (Status == %r)\n", + __FUNCTION__, Status)); + continue; + } + + ASSERT (RegSize == 16); + + // + // Create a unique device path for this transport on the fly + // + RegBase = SwapBytes64 (*Reg); + DevicePath = (VIRTIO_TRANSPORT_DEVICE_PATH *)CreateDeviceNode ( + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + sizeof (VIRTIO_TRANSPORT_DEVICE_PATH)); + if (DevicePath == NULL) { + DEBUG ((EFI_D_ERROR, "%a: Out of memory\n", __FUNCTION__)); + continue; + } + + CopyGuid (&DevicePath->Vendor.Guid, &gVirtioMmioTransportGuid); + DevicePath->PhysBase = RegBase; + SetDevicePathNodeLength (&DevicePath->Vendor, + sizeof (*DevicePath) - sizeof (DevicePath->End)); + SetDevicePathEndNode (&DevicePath->End); + + Handle = NULL; + Status = gBS->InstallProtocolInterface (&Handle, + &gEfiDevicePathProtocolGuid, EFI_NATIVE_INTERFACE, + DevicePath); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "%a: Failed to install the EFI_DEVICE_PATH " + "protocol on a new handle (Status == %r)\n", + __FUNCTION__, Status)); + FreePool (DevicePath); + continue; + } + + Status = VirtioMmioInstallDevice (RegBase, Handle); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "%a: Failed to install VirtIO transport @ 0x%Lx " + "on handle %p (Status == %r)\n", __FUNCTION__, RegBase, + Handle, Status)); + + Status = gBS->UninstallProtocolInterface (Handle, + &gEfiDevicePathProtocolGuid, DevicePath); + ASSERT_EFI_ERROR (Status); + FreePool (DevicePath); + continue; + } + } + + if (EFI_ERROR (FindNodeStatus) && FindNodeStatus != EFI_NOT_FOUND) { + DEBUG ((EFI_D_ERROR, "%a: Error occurred while iterating DT nodes " + "(FindNodeStatus == %r)\n", __FUNCTION__, FindNodeStatus)); + } + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.inf b/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.inf new file mode 100644 index 0000000000..dece5c806b --- /dev/null +++ b/OvmfPkg/Fdt/VirtioFdtDxe/VirtioFdtDxe.inf @@ -0,0 +1,45 @@ +## @file +# Virtio FDT client protocol driver for virtio,mmio DT node +# +# Copyright (c) 2014 - 2016, Linaro Ltd. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VirtioFdtDxe + FILE_GUID = 0049858F-8CA7-4CCD-918B-D952CBF32975 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + + ENTRY_POINT = InitializeVirtioFdtDxe + +[Sources] + VirtioFdtDxe.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + DevicePathLib + MemoryAllocationLib + UefiBootServicesTableLib + UefiDriverEntryPoint + VirtioMmioDeviceLib + +[Guids] + gVirtioMmioTransportGuid + +[Protocols] + gEfiDevicePathProtocolGuid ## PRODUCES + gFdtClientProtocolGuid ## CONSUMES + +[Depex] + gFdtClientProtocolGuid -- cgit v1.2.3