public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] Add QemuIGDHelperDxe
@ 2018-04-25 12:13 Gerd Hoffmann
  2018-04-25 13:59 ` Laszlo Ersek
  0 siblings, 1 reply; 2+ messages in thread
From: Gerd Hoffmann @ 2018-04-25 12:13 UTC (permalink / raw)
  To: edk2-devel

Little driver to setup the OpRegion for Intel vgpu devices.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 OvmfPkg/QemuIGDHelperDxe/QemuIGDHelper.c      | 262 ++++++++++++++++++++++++++
 OvmfPkg/OvmfPkgIa32.dsc                       |   1 +
 OvmfPkg/OvmfPkgIa32.fdf                       |   1 +
 OvmfPkg/OvmfPkgIa32X64.dsc                    |   1 +
 OvmfPkg/OvmfPkgIa32X64.fdf                    |   1 +
 OvmfPkg/OvmfPkgX64.dsc                        |   1 +
 OvmfPkg/OvmfPkgX64.fdf                        |   1 +
 OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf |  32 ++++
 8 files changed, 300 insertions(+)
 create mode 100644 OvmfPkg/QemuIGDHelperDxe/QemuIGDHelper.c
 create mode 100644 OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf

diff --git a/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelper.c b/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelper.c
new file mode 100644
index 0000000000..f314049bb4
--- /dev/null
+++ b/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelper.c
@@ -0,0 +1,262 @@
+#include <Uefi.h>
+#include <Protocol/PciIo.h>
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+#include <Library/PciLib.h>
+#include <Library/UefiBootManagerLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiDriverEntryPoint.h>
+#include <Library/UefiLib.h>
+#include <Library/QemuFwCfgLib.h>
+
+#include <IndustryStandard/Pci.h>
+
+VOID *QemuIGDHelperOpRegion;
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperControllerDriverSupported (
+  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
+  IN EFI_HANDLE                     Controller,
+  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
+  )
+{
+  EFI_STATUS          Status;
+  EFI_PCI_IO_PROTOCOL *PciIo;
+  PCI_TYPE00          Pci;
+  UINT32              OpRegion;
+
+  Status = gBS->OpenProtocol (
+                  Controller,
+                  &gEfiPciIoProtocolGuid,
+                  (VOID **) &PciIo,
+                  This->DriverBindingHandle,
+                  Controller,
+                  EFI_OPEN_PROTOCOL_BY_DRIVER
+                  );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = PciIo->Pci.Read (
+                        PciIo,
+                        EfiPciIoWidthUint32,
+                        0,
+                        sizeof (Pci) / sizeof (UINT32),
+                        &Pci
+                        );
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+
+  Status = EFI_UNSUPPORTED;
+  if (!IS_PCI_VGA (&Pci)) {
+    goto Done;
+  }
+  if (Pci.Hdr.VendorId != 0x8086 /* Intel */) {
+    goto Done;
+  }
+
+  DEBUG ((EFI_D_INFO, "IGDHelper: intel gfx %x:%x found\n",
+          Pci.Hdr.VendorId, Pci.Hdr.DeviceId));
+  OpRegion = (UINTN)QemuIGDHelperOpRegion;
+  Status = PciIo->Pci.Write (
+                        PciIo,
+                        EfiPciIoWidthUint32,
+                        0xFC,
+                        1,
+                        &OpRegion
+                        );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((EFI_D_INFO, "IGDHelper: write error: %d\n", Status));
+  } else {
+    DEBUG ((EFI_D_INFO, "IGDHelper: opregion setup OK\n"));
+  }
+
+Done:
+  gBS->CloseProtocol (
+        Controller,
+        &gEfiPciIoProtocolGuid,
+        This->DriverBindingHandle,
+        Controller
+        );
+
+  return Status;
+}
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperControllerDriverStart (
+  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
+  IN EFI_HANDLE                     Controller,
+  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
+  )
+{
+  DEBUG ((EFI_D_INFO, "QemuIGDHelperControllerDriverStart\n"));
+  return EFI_SUCCESS;
+}
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperControllerDriverStop (
+  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
+  IN EFI_HANDLE                     Controller,
+  IN UINTN                          NumberOfChildren,
+  IN EFI_HANDLE                     *ChildHandleBuffer
+  )
+{
+  DEBUG ((EFI_D_INFO, "QemuIGDHelperControllerDriverStop\n"));
+  return EFI_SUCCESS;
+}
+
+EFI_DRIVER_BINDING_PROTOCOL gQemuIGDHelperDriverBinding = {
+  QemuIGDHelperControllerDriverSupported,
+  QemuIGDHelperControllerDriverStart,
+  QemuIGDHelperControllerDriverStop,
+  0x10,
+  NULL,
+  NULL
+};
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8                        *Language,
+  OUT CHAR16                       **DriverName
+  );
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
+  IN  EFI_HANDLE                                      ControllerHandle,
+  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
+  IN  CHAR8                                           *Language,
+  OUT CHAR16                                          **ControllerName
+  );
+
+EFI_UNICODE_STRING_TABLE mQemuIGDHelperDriverNameTable[] = {
+  { "eng;en", L"QEMU IGD Helper" },
+  { NULL , NULL }
+};
+
+EFI_UNICODE_STRING_TABLE mQemuIGDHelperControllerNameTable[] = {
+  { "eng;en", L"IGD" },
+  { NULL , NULL }
+};
+
+EFI_COMPONENT_NAME_PROTOCOL  gQemuIGDHelperComponentName = {
+  QemuIGDHelperComponentNameGetDriverName,
+  QemuIGDHelperComponentNameGetControllerName,
+  "eng"
+};
+
+EFI_COMPONENT_NAME2_PROTOCOL gQemuIGDHelperComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) QemuIGDHelperComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) QemuIGDHelperComponentNameGetControllerName,
+  "en"
+};
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8                        *Language,
+  OUT CHAR16                       **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+           Language,
+           This->SupportedLanguages,
+           mQemuIGDHelperDriverNameTable,
+           DriverName,
+           (BOOLEAN)(This == &gQemuIGDHelperComponentName)
+           );
+}
+
+EFI_STATUS
+EFIAPI
+QemuIGDHelperComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
+  IN  EFI_HANDLE                                      ControllerHandle,
+  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
+  IN  CHAR8                                           *Language,
+  OUT CHAR16                                          **ControllerName
+  )
+{
+  EFI_STATUS                      Status;
+
+  if (ChildHandle != NULL) {
+    return EFI_UNSUPPORTED;
+  }
+
+  Status = EfiTestManagedDevice (
+             ControllerHandle,
+             gQemuIGDHelperDriverBinding.DriverBindingHandle,
+             &gEfiPciIoProtocolGuid
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  return LookupUnicodeString2 (
+           Language,
+           This->SupportedLanguages,
+           mQemuIGDHelperControllerNameTable,
+           ControllerName,
+           (BOOLEAN)(This == &gQemuIGDHelperComponentName)
+           );
+}
+
+EFI_STATUS
+EFIAPI
+InitializeQemuIGDHelper (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_SYSTEM_TABLE     *SystemTable
+  )
+{
+  EFI_STATUS                Status;
+  RETURN_STATUS             Ret;
+  FIRMWARE_CONFIG_ITEM      Item;
+  UINTN                     Size,Pages;
+
+  DEBUG ((EFI_D_INFO, "InitializeQemuIGDHelper\n"));
+
+  if (!QemuFwCfgIsAvailable()) {
+    DEBUG ((EFI_D_INFO, "IGDHelper: no FwCfg\n"));
+    return EFI_NOT_FOUND;
+  }
+
+  Ret = QemuFwCfgFindFile("etc/igd-opregion", &Item, &Size);
+  if (Ret != RETURN_SUCCESS) {
+    DEBUG ((EFI_D_INFO, "IGDHelper: no etc/igd-opregion in FwCfg\n"));
+    return EFI_NOT_FOUND;
+  }
+
+  Pages = (Size + EFI_PAGE_SIZE - 1) / EFI_PAGE_SIZE;
+  DEBUG ((EFI_D_INFO, "IGDHelper: opregion: fwcfg entry %d, size %d (%d pages)\n",
+          Item, Size, Pages));
+
+  QemuIGDHelperOpRegion = AllocateRuntimePages(Pages);
+  QemuFwCfgSelectItem(Item);
+  QemuFwCfgReadBytes(Size, QemuIGDHelperOpRegion);
+  DEBUG ((EFI_D_INFO, "IGDHelper: opregion: allocated at %p\n",
+          QemuIGDHelperOpRegion));
+
+  Status = EfiLibInstallDriverBindingComponentName2 (
+             ImageHandle,
+             SystemTable,
+             &gQemuIGDHelperDriverBinding,
+             ImageHandle,
+             &gQemuIGDHelperComponentName,
+             &gQemuIGDHelperComponentName2
+             );
+  ASSERT_EFI_ERROR (Status);
+
+  return Status;
+}
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index 2d6c4c4615..16adbf2cff 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -740,6 +740,7 @@
   MdeModulePkg/Universal/MemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf
 
   OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
   OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 
   #
diff --git a/OvmfPkg/OvmfPkgIa32.fdf b/OvmfPkg/OvmfPkgIa32.fdf
index 0427ded492..7df5fd47d3 100644
--- a/OvmfPkg/OvmfPkgIa32.fdf
+++ b/OvmfPkg/OvmfPkgIa32.fdf
@@ -351,6 +351,7 @@ INF  RuleOverride=CSM OvmfPkg/Csm/Csm16/Csm16.inf
 !endif
 
 INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+INF  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 INF  OvmfPkg/PlatformDxe/Platform.inf
 INF  OvmfPkg/IoMmuDxe/IoMmuDxe.inf
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index 43158c5f06..dfa25ad056 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -749,6 +749,7 @@
   MdeModulePkg/Universal/MemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf
 
   OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
   OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 
   #
diff --git a/OvmfPkg/OvmfPkgIa32X64.fdf b/OvmfPkg/OvmfPkgIa32X64.fdf
index 6df47f48cd..c5e77f4298 100644
--- a/OvmfPkg/OvmfPkgIa32X64.fdf
+++ b/OvmfPkg/OvmfPkgIa32X64.fdf
@@ -357,6 +357,7 @@ INF  RuleOverride=CSM OvmfPkg/Csm/Csm16/Csm16.inf
 !endif
 
 INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+INF  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 INF  OvmfPkg/PlatformDxe/Platform.inf
 INF  OvmfPkg/AmdSevDxe/AmdSevDxe.inf
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index d1fdf7c307..72fac3429a 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -747,6 +747,7 @@
   MdeModulePkg/Universal/MemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf
 
   OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
   OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 
   #
diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf
index 2e2a1749b5..5e3ce00f8e 100644
--- a/OvmfPkg/OvmfPkgX64.fdf
+++ b/OvmfPkg/OvmfPkgX64.fdf
@@ -357,6 +357,7 @@ INF  RuleOverride=CSM OvmfPkg/Csm/Csm16/Csm16.inf
 !endif
 
 INF  OvmfPkg/QemuVideoDxe/QemuVideoDxe.inf
+INF  OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
 INF  OvmfPkg/VirtioGpuDxe/VirtioGpu.inf
 INF  OvmfPkg/PlatformDxe/Platform.inf
 INF  OvmfPkg/AmdSevDxe/AmdSevDxe.inf
diff --git a/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf b/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
new file mode 100644
index 0000000000..9d6c8200ce
--- /dev/null
+++ b/OvmfPkg/QemuIGDHelperDxe/QemuIGDHelperDxe.inf
@@ -0,0 +1,32 @@
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = QemuIGDHelperDxe
+  FILE_GUID                      = 26a95470-bc51-4f40-9dc6-f33af3321b25
+  MODULE_TYPE                    = UEFI_DRIVER
+  VERSION_STRING                 = 1.0
+
+  ENTRY_POINT                    = InitializeQemuIGDHelper
+
+[Sources.common]
+  QemuIGDHelper.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  OvmfPkg/OvmfPkg.dec
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+  DevicePathLib
+  MemoryAllocationLib
+  PcdLib
+  PciLib
+  UefiBootManagerLib
+  UefiBootServicesTableLib
+  UefiDriverEntryPoint
+  UefiLib
+  QemuFwCfgLib
+
+[Protocols]
+  gEfiPciIoProtocolGuid                         # PROTOCOL TO_START
-- 
2.9.3



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-04-25 13:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-25 12:13 [PATCH] Add QemuIGDHelperDxe Gerd Hoffmann
2018-04-25 13:59 ` Laszlo Ersek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox