public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ard.biesheuvel@arm.com>
To: Sami Mujawar <sami.mujawar@arm.com>, devel@edk2.groups.io
Cc: leif@nuviainc.com, lersek@redhat.com, Alexandru.Elisei@arm.com,
	Andre.Przywara@arm.com, Matteo.Carlini@arm.com,
	Laura.Moretta@arm.com, nd@arm.com
Subject: Re: [PATCH v4 02/15] ArmVirtPkg: Add Kvmtool RTC Fdt Client Library
Date: Tue, 7 Jul 2020 16:19:31 +0300	[thread overview]
Message-ID: <50274581-a51f-f66d-2cf0-86359984f2a9@arm.com> (raw)
In-Reply-To: <20200707124810.50668-3-sami.mujawar@arm.com>

On 7/7/20 3:47 PM, Sami Mujawar wrote:
> Add library that parses the Kvmtool device tree and updates
> the dynamic PCDs describing the RTC Memory map.
> 
> It also maps the MMIO region used by the RTC as runtime memory
> so that the RTC registers are accessible post ExitBootServices.
> 
> Since UEFI takes ownership of the RTC hardware disable the RTC
> node in the DT to prevent the OS from attaching its device
> driver as well.
> 
> Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
> ---
> 
> Notes:
>      v4:
>        - Updated based on review comments.                             [Sami]
>        - Cleanup include file list, make local functions static,       [Ard]
>          drop EFIAPI, use EFI_PAGE_MASK and use ReadUnaligned64.
>          Ref: https://edk2.groups.io/g/devel/topic/75081472
>      

You haven't answered my question about adding and removing the GCD 
memory space.


>      v3:
>        - Introduce library to read and map the MMIO base addresses     [Sami]
>          for the RTC registers from the DT and configure the Index
>          and Target register PCDs.
> 
>   ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.c   | 230 ++++++++++++++++++++
>   ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.inf |  42 ++++
>   2 files changed, 272 insertions(+)
> 
> diff --git a/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.c b/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..93715958952f933007ff143a22cbe81606cc539e
> --- /dev/null
> +++ b/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.c
> @@ -0,0 +1,230 @@
> +/** @file
> +  FDT client library for motorola,mc146818 RTC driver
> +
> +  Copyright (c) 2020, ARM Limited. All rights reserved.<BR>
> +
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/DxeServicesTableLib.h>
> +#include <Library/PcdLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +#include <Protocol/FdtClient.h>
> +
> +/** RTC Index register is at offset 0x0
> +*/
> +#define RTC_INDEX_REG_OFFSET    0x0ULL
> +
> +/** RTC Target register is at offset 0x1
> +*/
> +#define RTC_TARGET_REG_OFFSET   0x1ULL
> +
> +/** Add the RTC controller address range to the memory map.
> +
> +  @param [in]  ImageHandle  The handle to the image.
> +  @param [in]  RtcPageBase  Base address of the RTC controller.
> +
> +  @retval EFI_SUCCESS             Success.
> +  @retval EFI_INVALID_PARAMETER   A parameter is invalid.
> +  @retval EFI_NOT_FOUND           Flash device not found.
> +**/
> +STATIC
> +EFI_STATUS
> +KvmtoolRtcMapMemory (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN EFI_PHYSICAL_ADDRESS     RtcPageBase
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = gDS->AddMemorySpace (
> +                  EfiGcdMemoryTypeMemoryMappedIo,
> +                  RtcPageBase,
> +                  EFI_PAGE_SIZE,
> +                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR, "Failed to add memory space. Status = %r\n",
> +      Status
> +      ));
> +    return Status;
> +  }
> +
> +  Status = gDS->AllocateMemorySpace (
> +                  EfiGcdAllocateAddress,
> +                  EfiGcdMemoryTypeMemoryMappedIo,
> +                  0,
> +                  EFI_PAGE_SIZE,
> +                  &RtcPageBase,
> +                  ImageHandle,
> +                  NULL
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "Failed to allocate memory space. Status = %r\n",
> +      Status
> +      ));
> +    gDS->RemoveMemorySpace (
> +           RtcPageBase,
> +           EFI_PAGE_SIZE
> +           );
> +    return Status;
> +  }
> +
> +  Status = gDS->SetMemorySpaceAttributes (
> +                  RtcPageBase,
> +                  EFI_PAGE_SIZE,
> +                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "Failed to set memory attributes. Status = %r\n",
> +      Status
> +      ));
> +    gDS->FreeMemorySpace (
> +           RtcPageBase,
> +           EFI_PAGE_SIZE
> +           );
> +    gDS->RemoveMemorySpace (
> +           RtcPageBase,
> +           EFI_PAGE_SIZE
> +           );
> +  }
> +
> +  return Status;
> +}
> +
> +/** Entrypoint for KvmtoolRtcFdtClientLib.
> +
> +  Locate the RTC node in the DT and update the Index and
> +  Target register base addresses in the respective PCDs.
> +  Add the RTC memory region to the memory map.
> +  Disable the RTC node as the RTC is owned by UEFI.
> +
> +  @param [in]  ImageHandle  The handle to the image.
> +  @param [in]  SystemTable  Pointer to the System Table.
> +
> +  @retval EFI_SUCCESS             Success.
> +  @retval EFI_INVALID_PARAMETER   A parameter is invalid.
> +  @retval EFI_NOT_FOUND           Flash device not found.
> +**/
> +EFI_STATUS
> +EFIAPI
> +KvmtoolRtcFdtClientLibConstructor (
> +  IN EFI_HANDLE         ImageHandle,
> +  IN EFI_SYSTEM_TABLE   *SystemTable
> +  )
> +{
> +  EFI_STATUS            Status;
> +  FDT_CLIENT_PROTOCOL   *FdtClient;
> +  INT32                 Node;
> +  CONST UINT32          *Reg;
> +  UINT32                RegSize;
> +  UINT64                RegBase;
> +  UINT64                Range;
> +  RETURN_STATUS         PcdStatus;
> +
> +  Status = gBS->LocateProtocol (
> +                  &gFdtClientProtocolGuid,
> +                  NULL,
> +                  (VOID **)&FdtClient
> +                  );
> +  ASSERT_EFI_ERROR (Status);
> +
> +  Status = FdtClient->FindCompatibleNode (
> +                        FdtClient,
> +                        "motorola,mc146818",
> +                        &Node
> +                        );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: No 'motorola,mc146818' compatible DT node found\n",
> +      __FUNCTION__
> +      ));
> +    return Status;
> +  }
> +
> +  Status = FdtClient->GetNodeProperty (
> +                        FdtClient,
> +                        Node,
> +                        "reg",
> +                        (CONST VOID **)&Reg,
> +                        &RegSize
> +                        );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: No 'reg' property found in 'motorola,mc146818' compatible DT node\n",
> +      __FUNCTION__
> +      ));
> +    return Status;
> +  }
> +
> +  ASSERT (RegSize == 16);
> +
> +  RegBase = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
> +  Range = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
> +  DEBUG ((
> +    DEBUG_INFO,
> +    "Found motorola,mc146818 RTC @ 0x%Lx Range = 0x%x\n",
> +    RegBase,
> +    Range
> +    ));
> +
> +  // The address range must cover the RTC Index and the Target registers.
> +  ASSERT (Range >= 0x2);
> +
> +  // RTC Index register is at offset 0x0
> +  PcdStatus = PcdSet64S (
> +                PcdRtcIndexRegister64,
> +                (RegBase + RTC_INDEX_REG_OFFSET)
> +                );
> +  ASSERT_RETURN_ERROR (PcdStatus);
> +
> +  // RTC Target register is at offset 0x1
> +  PcdStatus = PcdSet64S (
> +                PcdRtcTargetRegister64,
> +                (RegBase + RTC_TARGET_REG_OFFSET)
> +                );
> +  ASSERT_RETURN_ERROR (PcdStatus);
> +
> +  Status = KvmtoolRtcMapMemory (ImageHandle, (RegBase & ~EFI_PAGE_MASK));
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "Failed to map memory for motorola,mc146818. Status = %r\n",
> +      Status
> +      ));
> +    return Status;
> +  }
> +
> +  //
> +  // UEFI takes ownership of the RTC hardware, and exposes its functionality
> +  // through the UEFI Runtime Services GetTime, SetTime, etc. This means we
> +  // need to disable it in the device tree to prevent the OS from attaching
> +  // its device driver as well.
> +  //
> +  Status = FdtClient->SetNodeProperty (
> +                        FdtClient,
> +                        Node,
> +                        "status",
> +                        "disabled",
> +                        sizeof ("disabled")
> +                        );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_WARN,
> +      "Failed to set motorola,mc146818 status to 'disabled', Status = %r\n",
> +      Status
> +      ));
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.inf b/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.inf
> new file mode 100644
> index 0000000000000000000000000000000000000000..0d1b27997ed00d7c1be3578cab4d1d2eba663a90
> --- /dev/null
> +++ b/ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.inf
> @@ -0,0 +1,42 @@
> +#/** @file
> +#  FDT client library for motorola,mc146818 RTC driver
> +#
> +#  Copyright (c) 2020, ARM Limited. All rights reserved.<BR>
> +#
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = KvmtoolRtcFdtClientLib
> +  FILE_GUID                      = 3254B4F7-30B5-48C6-B06A-D8FF97F3EF95
> +  MODULE_TYPE                    = DXE_DRIVER
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = KvmtoolRtcFdtClientLib|DXE_DRIVER DXE_RUNTIME_DRIVER
> +  CONSTRUCTOR                    = KvmtoolRtcFdtClientLibConstructor
> +
> +[Sources]
> +  KvmtoolRtcFdtClientLib.c
> +
> +[Packages]
> +  ArmVirtPkg/ArmVirtPkg.dec
> +  MdePkg/MdePkg.dec
> +  PcAtChipsetPkg/PcAtChipsetPkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  DebugLib
> +  PcdLib
> +  UefiBootServicesTableLib
> +  DxeServicesTableLib
> +
> +[Protocols]
> +  gFdtClientProtocolGuid                                ## CONSUMES
> +
> +[Pcd]
> +  gPcAtChipsetPkgTokenSpaceGuid.PcdRtcIndexRegister64
> +  gPcAtChipsetPkgTokenSpaceGuid.PcdRtcTargetRegister64
> +
> +[Depex]
> +  gFdtClientProtocolGuid
> 


  reply	other threads:[~2020-07-07 13:19 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-07 12:47 [PATCH v4 00/15] Kvmtool guest firmware support for Arm Sami Mujawar
2020-07-07 12:47 ` [PATCH v4 01/15] PcAtChipsetPkg: Add MMIO Support to RTC driver Sami Mujawar
2020-07-07 13:16   ` Ard Biesheuvel
2020-07-09 11:37     ` Sami Mujawar
2020-07-07 12:47 ` [PATCH v4 02/15] ArmVirtPkg: Add Kvmtool RTC Fdt Client Library Sami Mujawar
2020-07-07 13:19   ` Ard Biesheuvel [this message]
2020-07-09 11:34     ` [edk2-devel] " Sami Mujawar
2020-07-07 12:47 ` [PATCH v4 03/15] ArmPlatformPkg: Dynamic flash variable base Sami Mujawar
2020-07-07 12:47 ` [PATCH v4 04/15] ArmVirtPkg: Add kvmtool platform driver Sami Mujawar
2020-07-07 13:22   ` Ard Biesheuvel
2020-07-07 12:47 ` [PATCH v4 05/15] ArmVirtPkg: kvmtool platform memory map Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 06/15] ArmVirtPkg: Add Kvmtool NOR flash lib Sami Mujawar
2020-07-07 13:26   ` Ard Biesheuvel
2020-07-07 12:48 ` [PATCH v4 07/15] MdeModulePkg: Fix constructor invocation ordering Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 08/15] ArmVirtPkg: GUID Hob for 16550 UART base address Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 09/15] ArmVirtPkg: 16550 UART Platform hook library Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 10/15] ArmVirtPkg: Add Kvmtool Platform Pei Lib Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 11/15] ArmVirtPkg: Support for kvmtool virtual platform Sami Mujawar
2020-07-07 13:27   ` Ard Biesheuvel
2020-07-07 12:48 ` [PATCH v4 12/15] ArmVirtPkg: Package dependency for MC146818 RTC Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 13/15] ArmVirtPkg: Add kvmtool to package dictionary Sami Mujawar
2020-07-07 12:48 ` [PATCH v4 14/15] .python/SpellCheck: Add 'XIPFLAGS' to "words" section Sami Mujawar
2020-07-07 13:28   ` Ard Biesheuvel
2020-07-07 12:48 ` [PATCH v4 15/15] Maintainer.txt: Add Kvmtool platform reviewer Sami Mujawar
     [not found] ` <161F794363CF42EE.6803@groups.io>
2020-07-10 17:34   ` [edk2-devel] [PATCH v4 07/15] MdeModulePkg: Fix constructor invocation ordering Sami Mujawar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50274581-a51f-f66d-2cf0-86359984f2a9@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox