[edk2-devel] [PATCH] ArmPlatformPkg/PL031RealTimeClockLib: remove superfluous instance init steps

Laszlo Ersek posted 1 patch 6 months, 1 week ago
Failed in applying to current master (apply log)
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c | 13 -------------
1 file changed, 13 deletions(-)
[edk2-devel] [PATCH] ArmPlatformPkg/PL031RealTimeClockLib: remove superfluous instance init steps
Posted by Laszlo Ersek 6 months, 1 week ago
RealTimeClockLib instances are consumed by edk2's
EmbeddedPkg/RealTimeClockRuntimeDxe driver. In its entry point function
InitializeRealTimeClock(), the driver:

(1) calls LibRtcInitialize(),

(2) sets the GetTime(), SetTime(), GetWakeupTime() and SetWakeupTime()
    runtime services to its own similarly-named functions -- where those
    functions wrap the corresponding RealTimeClockLib APIs,

(3) installs EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL with a NULL protocol
    interface.

Steps (2) and (3) conform to PI v1.8 sections II-9.7.2.4 through
II-9.7.2.7.

However, this means that LibRtcInitialize() (of any RealTimeClockLib
instance) should not itself (a) set the GetTime(), SetTime(),
GetWakeupTime() and SetWakeupTime() runtime services, nor (b) install
EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL. The runtime service pointers will be
overwritten in step (2) anyway, and step (3) will uselessly install a
second (NULL-interface) EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL instance in the
protocol database. (The protocol only serves to notify the DXE Foundation
about said runtime services being available.)

Clean up ArmPlatformPkg/PL031RealTimeClockLib accordingly (it only has
code that's redundant for step (3); it does not try to set "gRT" fields).

(Note that the lib instance INF file already does not list
gEfiRealTimeClockArchProtocolGuid.)

Tested with ArmVirtQemu.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=4565
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---

Notes:
    context:-W

 ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c | 13 -------------
 1 file changed, 13 deletions(-)

diff --git a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
index 9e852696d2fd..1896f9d16d3b 100644
--- a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
+++ b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
@@ -1,34 +1,32 @@
 /** @file
   Implement EFI RealTimeClock runtime services via RTC Lib.
 
   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
   Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.<BR>
   Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include <PiDxe.h>
 
 #include <Guid/EventGroup.h>
 #include <Guid/GlobalVariable.h>
 
 #include <Library/BaseLib.h>
 #include <Library/DebugLib.h>
 #include <Library/DxeServicesTableLib.h>
 #include <Library/IoLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/PcdLib.h>
 #include <Library/RealTimeClockLib.h>
 #include <Library/TimeBaseLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiLib.h>
 #include <Library/UefiRuntimeServicesTableLib.h>
 #include <Library/UefiRuntimeLib.h>
 
-#include <Protocol/RealTimeClock.h>
-
 #include "PL031RealTimeClock.h"
 
 STATIC BOOLEAN    mPL031Initialized = FALSE;
@@ -307,52 +305,41 @@ EFIAPI
 LibRtcInitialize (
   IN EFI_HANDLE        ImageHandle,
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
   EFI_STATUS  Status;
-  EFI_HANDLE  Handle;
 
   // Initialize RTC Base Address
   mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
 
   // Declare the controller as EFI_MEMORY_RUNTIME
   Status = gDS->AddMemorySpace (
                   EfiGcdMemoryTypeMemoryMappedIo,
                   mPL031RtcBase,
                   SIZE_4KB,
                   EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
                   );
   if (EFI_ERROR (Status)) {
     return Status;
   }
 
   Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
   if (EFI_ERROR (Status)) {
     return Status;
   }
 
-  // Install the protocol
-  Handle = NULL;
-  Status = gBS->InstallMultipleProtocolInterfaces (
-                  &Handle,
-                  &gEfiRealTimeClockArchProtocolGuid,
-                  NULL,
-                  NULL
-                  );
-  ASSERT_EFI_ERROR (Status);
-
   //
   // Register for the virtual address change event
   //
   Status = gBS->CreateEventEx (
                   EVT_NOTIFY_SIGNAL,
                   TPL_NOTIFY,
                   VirtualNotifyEvent,
                   NULL,
                   &gEfiEventVirtualAddressChangeGuid,
                   &mRtcVirtualAddrChangeEvent
                   );
   ASSERT_EFI_ERROR (Status);
 
   return Status;
 }


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109835): https://edk2.groups.io/g/devel/message/109835
Mute This Topic: https://groups.io/mt/102079629/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3901457/1787277/102458076/xyzzy [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-


Re: [edk2-devel] [PATCH] ArmPlatformPkg/PL031RealTimeClockLib: remove superfluous instance init steps
Posted by Ard Biesheuvel 6 months, 1 week ago
On Fri, 20 Oct 2023 at 14:17, Laszlo Ersek <lersek@redhat.com> wrote:
>
> RealTimeClockLib instances are consumed by edk2's
> EmbeddedPkg/RealTimeClockRuntimeDxe driver. In its entry point function
> InitializeRealTimeClock(), the driver:
>
> (1) calls LibRtcInitialize(),
>
> (2) sets the GetTime(), SetTime(), GetWakeupTime() and SetWakeupTime()
>     runtime services to its own similarly-named functions -- where those
>     functions wrap the corresponding RealTimeClockLib APIs,
>
> (3) installs EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL with a NULL protocol
>     interface.
>
> Steps (2) and (3) conform to PI v1.8 sections II-9.7.2.4 through
> II-9.7.2.7.
>
> However, this means that LibRtcInitialize() (of any RealTimeClockLib
> instance) should not itself (a) set the GetTime(), SetTime(),
> GetWakeupTime() and SetWakeupTime() runtime services, nor (b) install
> EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL. The runtime service pointers will be
> overwritten in step (2) anyway, and step (3) will uselessly install a
> second (NULL-interface) EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL instance in the
> protocol database. (The protocol only serves to notify the DXE Foundation
> about said runtime services being available.)
>
> Clean up ArmPlatformPkg/PL031RealTimeClockLib accordingly (it only has
> code that's redundant for step (3); it does not try to set "gRT" fields).
>
> (Note that the lib instance INF file already does not list
> gEfiRealTimeClockArchProtocolGuid.)
>
> Tested with ArmVirtQemu.
>
> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Leif Lindholm <quic_llindhol@quicinc.com>
> Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=4565
> Signed-off-by: Laszlo Ersek <lersek@redhat.com>
> ---
>
> Notes:
>     context:-W
>
>  ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c | 13 -------------
>  1 file changed, 13 deletions(-)
>

Acked-by: Ard Biesheuvel <ardb@kernel.org>

Thanks a lot for cleaning up this mess.


> diff --git a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
> index 9e852696d2fd..1896f9d16d3b 100644
> --- a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
> +++ b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
> @@ -1,34 +1,32 @@
>  /** @file
>    Implement EFI RealTimeClock runtime services via RTC Lib.
>
>    Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
>    Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.<BR>
>    Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>  **/
>
>  #include <PiDxe.h>
>
>  #include <Guid/EventGroup.h>
>  #include <Guid/GlobalVariable.h>
>
>  #include <Library/BaseLib.h>
>  #include <Library/DebugLib.h>
>  #include <Library/DxeServicesTableLib.h>
>  #include <Library/IoLib.h>
>  #include <Library/MemoryAllocationLib.h>
>  #include <Library/PcdLib.h>
>  #include <Library/RealTimeClockLib.h>
>  #include <Library/TimeBaseLib.h>
>  #include <Library/UefiBootServicesTableLib.h>
>  #include <Library/UefiLib.h>
>  #include <Library/UefiRuntimeServicesTableLib.h>
>  #include <Library/UefiRuntimeLib.h>
>
> -#include <Protocol/RealTimeClock.h>
> -
>  #include "PL031RealTimeClock.h"
>
>  STATIC BOOLEAN    mPL031Initialized = FALSE;
> @@ -307,52 +305,41 @@ EFIAPI
>  LibRtcInitialize (
>    IN EFI_HANDLE        ImageHandle,
>    IN EFI_SYSTEM_TABLE  *SystemTable
>    )
>  {
>    EFI_STATUS  Status;
> -  EFI_HANDLE  Handle;
>
>    // Initialize RTC Base Address
>    mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
>
>    // Declare the controller as EFI_MEMORY_RUNTIME
>    Status = gDS->AddMemorySpace (
>                    EfiGcdMemoryTypeMemoryMappedIo,
>                    mPL031RtcBase,
>                    SIZE_4KB,
>                    EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
>                    );
>    if (EFI_ERROR (Status)) {
>      return Status;
>    }
>
>    Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
>    if (EFI_ERROR (Status)) {
>      return Status;
>    }
>
> -  // Install the protocol
> -  Handle = NULL;
> -  Status = gBS->InstallMultipleProtocolInterfaces (
> -                  &Handle,
> -                  &gEfiRealTimeClockArchProtocolGuid,
> -                  NULL,
> -                  NULL
> -                  );
> -  ASSERT_EFI_ERROR (Status);
> -
>    //
>    // Register for the virtual address change event
>    //
>    Status = gBS->CreateEventEx (
>                    EVT_NOTIFY_SIGNAL,
>                    TPL_NOTIFY,
>                    VirtualNotifyEvent,
>                    NULL,
>                    &gEfiEventVirtualAddressChangeGuid,
>                    &mRtcVirtualAddrChangeEvent
>                    );
>    ASSERT_EFI_ERROR (Status);
>
>    return Status;
>  }


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109848): https://edk2.groups.io/g/devel/message/109848
Mute This Topic: https://groups.io/mt/102079629/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH] ArmPlatformPkg/PL031RealTimeClockLib: remove superfluous instance init steps
Posted by Laszlo Ersek 6 months, 1 week ago
On 10/20/23 14:27, Ard Biesheuvel wrote:
> On Fri, 20 Oct 2023 at 14:17, Laszlo Ersek <lersek@redhat.com> wrote:
>>
>> RealTimeClockLib instances are consumed by edk2's
>> EmbeddedPkg/RealTimeClockRuntimeDxe driver. In its entry point function
>> InitializeRealTimeClock(), the driver:
>>
>> (1) calls LibRtcInitialize(),
>>
>> (2) sets the GetTime(), SetTime(), GetWakeupTime() and SetWakeupTime()
>>     runtime services to its own similarly-named functions -- where those
>>     functions wrap the corresponding RealTimeClockLib APIs,
>>
>> (3) installs EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL with a NULL protocol
>>     interface.
>>
>> Steps (2) and (3) conform to PI v1.8 sections II-9.7.2.4 through
>> II-9.7.2.7.
>>
>> However, this means that LibRtcInitialize() (of any RealTimeClockLib
>> instance) should not itself (a) set the GetTime(), SetTime(),
>> GetWakeupTime() and SetWakeupTime() runtime services, nor (b) install
>> EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL. The runtime service pointers will be
>> overwritten in step (2) anyway, and step (3) will uselessly install a
>> second (NULL-interface) EFI_REAL_TIME_CLOCK_ARCH_PROTOCOL instance in the
>> protocol database. (The protocol only serves to notify the DXE Foundation
>> about said runtime services being available.)
>>
>> Clean up ArmPlatformPkg/PL031RealTimeClockLib accordingly (it only has
>> code that's redundant for step (3); it does not try to set "gRT" fields).
>>
>> (Note that the lib instance INF file already does not list
>> gEfiRealTimeClockArchProtocolGuid.)
>>
>> Tested with ArmVirtQemu.
>>
>> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
>> Cc: Leif Lindholm <quic_llindhol@quicinc.com>
>> Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=4565
>> Signed-off-by: Laszlo Ersek <lersek@redhat.com>
>> ---
>>
>> Notes:
>>     context:-W
>>
>>  ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c | 13 -------------
>>  1 file changed, 13 deletions(-)
>>
> 
> Acked-by: Ard Biesheuvel <ardb@kernel.org>
> 
> Thanks a lot for cleaning up this mess.

Merged via <https://github.com/tianocore/edk2/pull/4950> as commit
d85bf54b7f46.

Thanks!
Laszlo

> 
> 
>> diff --git a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
>> index 9e852696d2fd..1896f9d16d3b 100644
>> --- a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
>> +++ b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
>> @@ -1,34 +1,32 @@
>>  /** @file
>>    Implement EFI RealTimeClock runtime services via RTC Lib.
>>
>>    Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
>>    Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.<BR>
>>    Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>
>>
>>    SPDX-License-Identifier: BSD-2-Clause-Patent
>>
>>  **/
>>
>>  #include <PiDxe.h>
>>
>>  #include <Guid/EventGroup.h>
>>  #include <Guid/GlobalVariable.h>
>>
>>  #include <Library/BaseLib.h>
>>  #include <Library/DebugLib.h>
>>  #include <Library/DxeServicesTableLib.h>
>>  #include <Library/IoLib.h>
>>  #include <Library/MemoryAllocationLib.h>
>>  #include <Library/PcdLib.h>
>>  #include <Library/RealTimeClockLib.h>
>>  #include <Library/TimeBaseLib.h>
>>  #include <Library/UefiBootServicesTableLib.h>
>>  #include <Library/UefiLib.h>
>>  #include <Library/UefiRuntimeServicesTableLib.h>
>>  #include <Library/UefiRuntimeLib.h>
>>
>> -#include <Protocol/RealTimeClock.h>
>> -
>>  #include "PL031RealTimeClock.h"
>>
>>  STATIC BOOLEAN    mPL031Initialized = FALSE;
>> @@ -307,52 +305,41 @@ EFIAPI
>>  LibRtcInitialize (
>>    IN EFI_HANDLE        ImageHandle,
>>    IN EFI_SYSTEM_TABLE  *SystemTable
>>    )
>>  {
>>    EFI_STATUS  Status;
>> -  EFI_HANDLE  Handle;
>>
>>    // Initialize RTC Base Address
>>    mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
>>
>>    // Declare the controller as EFI_MEMORY_RUNTIME
>>    Status = gDS->AddMemorySpace (
>>                    EfiGcdMemoryTypeMemoryMappedIo,
>>                    mPL031RtcBase,
>>                    SIZE_4KB,
>>                    EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
>>                    );
>>    if (EFI_ERROR (Status)) {
>>      return Status;
>>    }
>>
>>    Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
>>    if (EFI_ERROR (Status)) {
>>      return Status;
>>    }
>>
>> -  // Install the protocol
>> -  Handle = NULL;
>> -  Status = gBS->InstallMultipleProtocolInterfaces (
>> -                  &Handle,
>> -                  &gEfiRealTimeClockArchProtocolGuid,
>> -                  NULL,
>> -                  NULL
>> -                  );
>> -  ASSERT_EFI_ERROR (Status);
>> -
>>    //
>>    // Register for the virtual address change event
>>    //
>>    Status = gBS->CreateEventEx (
>>                    EVT_NOTIFY_SIGNAL,
>>                    TPL_NOTIFY,
>>                    VirtualNotifyEvent,
>>                    NULL,
>>                    &gEfiEventVirtualAddressChangeGuid,
>>                    &mRtcVirtualAddrChangeEvent
>>                    );
>>    ASSERT_EFI_ERROR (Status);
>>
>>    return Status;
>>  }
> 
> 
> 
> 
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#110008): https://edk2.groups.io/g/devel/message/110008
Mute This Topic: https://groups.io/mt/102079629/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3901457/1787277/102458076/xyzzy [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-