From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by mx.groups.io with SMTP id smtpd.web11.60670.1640837409124932095 for ; Wed, 29 Dec 2021 20:10:09 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@nuviainc-com.20210112.gappssmtp.com header.s=20210112 header.b=6G1R2T04; spf=pass (domain: nuviainc.com, ip: 209.85.214.177, mailfrom: rebecca@nuviainc.com) Received: by mail-pl1-f177.google.com with SMTP id n16so17402300plc.2 for ; Wed, 29 Dec 2021 20:10:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuviainc-com.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=IeWSTTkydbX4hbZuLHPE70Qawd3JIT29kHT2WrX/sd0=; b=6G1R2T04dmdCcWMR9WborgFJNq8DPyvuD53dKRU2khGL+1bqemoO1I55xNTTh+/9wY qEjBYbz9EDDRIlAtS5EFybW/ez5qktbG3NxU+8pWGxUxH/f+Sr7oqdkCGRJ93Tpr5W8M D1xJuXWJGL0jkzk4b76ZXkhcQjDjT44xGO93DhmvTayG6D8lcGTxtt3hAAg2PW1LwWzD ZXP7ZYvVe/CbW92olYY4LEi6w/Uwo+lhlzNj45G1qtFUW1R44upTjqQ4bmMrdvzRwhPt +3G4xQEIvlPi+8cmHS2l+y5rKSVaHcTMVl4E3gTVZdogHo4qT3Fp7cuP/YdRrqDwc4eG Xrqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=IeWSTTkydbX4hbZuLHPE70Qawd3JIT29kHT2WrX/sd0=; b=uZyiIUdd0GPfswqAyETGYLxLLWgShhtUJIUMwS2IJ/K73PVoWWHRbkDkslkDXG7+7R CkrI3/YfYG3iW2I29RWjGHnzlaXwcuxrVCyvIVbLFgVfauAmGd93UoTNEwmxNdE9bDIg AqhF9n4ZdAh/A4f/3ncjEO8ClU4GUYJdrd8ypySUtWsjJ35Xa2Sz/mq2aCS7RfJtLXtE KqDmDinik4rrujbuVACBafrnOY66yNFEHr+R9yRrnWGLvMY0cjJiSFWMGhgxWIPr22BP KmAW53kl8i4Gmw4SBbbT09ebKtnmYoX7WskK1Euzc+dtreOa8iVTaT+J8G1Ul4O/YFmp 8lPg== X-Gm-Message-State: AOAM531P+AN+Z4ApDJN0qpen9TN/508XOjceCHskgrk15hnJBOuGIfVY 3rm7VUeJiMOwFbD+iVNg48tRnZT1h3lkksnj7FoFQCHYWycQDws6D9C7TM3B0FrkcMaje/FfUOB MaBZc6xK+cvSKOk6kPkpjq04/Yyw059BeJB4KyjG9FW4r+AAvOZclTBCv30rchVBzjguB5Q== X-Google-Smtp-Source: ABdhPJy/+UVGPYeNyk3eESe+8UY735204ma6I75nnSSckvK8iMjKLSMo8ilrqxvKym38ADyxHKxwmw== X-Received: by 2002:a17:90b:1950:: with SMTP id nk16mr35627597pjb.129.1640837408185; Wed, 29 Dec 2021 20:10:08 -0800 (PST) Return-Path: Received: from ?IPV6:2601:681:4300:69e::1d5a? ([2601:681:4300:69e::1d5a]) by smtp.gmail.com with ESMTPSA id q69sm26752391pjq.41.2021.12.29.20.10.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Dec 2021 20:10:07 -0800 (PST) Message-ID: <30857b66-c173-fa45-8c64-dd20ae71e318@nuviainc.com> Date: Wed, 29 Dec 2021 21:10:05 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [edk2-devel] [PATCH v4 1/1] MdeModulePkg: Add MpServicesTest application to exercise MP Services To: devel@edk2.groups.io, fanjianfeng@byosoft.com.cn, Ard Biesheuvel , Gerd Hoffmann , Samer El-Haj-Mahmoud , Leif Lindholm , "Wang, Jian J" , 'gaoliming' , nd , Sami Mujawar References: <20211212180846.6563-1-rebecca@nuviainc.com> <20211212180846.6563-2-rebecca@nuviainc.com> <2021123011214518968235@byosoft.com.cn> From: "Rebecca Cran" In-Reply-To: <2021123011214518968235@byosoft.com.cn> Content-Type: multipart/alternative; boundary="------------p4QxOCAjsjDXxC0xvxXHsmEC" Content-Language: en-US --------------p4QxOCAjsjDXxC0xvxXHsmEC Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit No, I expect there to only ever be a single MP services instance. I agree that the AP function should do something, but I'm hoping to get this initial implementation committed first. -- Rebecca Cran On 12/29/21 20:21, Jeff Fan wrote: > Hi,Rebecca > > I'd like to give two comments on this patch. > > 1, I don't think there are more than one MP services instances > installed reuirement on one system.  X86 platform installed one MP > service instance even on mutiple-sockets system. >     For ARM platform, is there any requirement to handle multple MP > services instance? > > 2,  AP function is NULL implementation that could not make sure BSP > recongized if APs run indeed.  You could simply update AP function to > use one global variable seamphoere wrapped by lock protections. >     I think this patch is very good start to add MP test app in open > source world.  This comment is only sugestion for your next plan. :-) > > Best Regards, > Jeff > > *From:* Rebecca Cran > *Date:* 2021-12-13 02:08 > *To:* devel ; Ard Biesheuvel > ; Gerd Hoffmann > ; Samer El-Haj-Mahmoud > ; Leif Lindholm > ; Jian J Wang > ; Liming Gao > ; nd ; Sami > Mujawar > *CC:* Rebecca Cran > *Subject:* [edk2-devel] [PATCH v4 1/1] MdeModulePkg: Add > MpServicesTest application to exercise MP Services > Add a new MpServicesTest application under > MdeModulePkg/Application that > exercises the EFI_MP_SERVICES_PROTOCOL. > Signed-off-by: Rebecca Cran > Reviewed-by: Sami Mujawar > --- > MdeModulePkg/Application/MpServicesTest/MpServicesTest.c   | 422 > ++++++++++++++++++++ > MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf |  38 ++ > MdeModulePkg/MdeModulePkg.dsc |   2 + > 3 files changed, 462 insertions(+) > diff --git > a/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c > b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c > new file mode 100644 > index 000000000000..933813e19e05 > --- /dev/null > +++ b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c > @@ -0,0 +1,422 @@ > +/** @file > + > +    Copyright (c) 2021, NUVIA Inc. All rights reserved.
> +    SPDX-License-Identifier: BSD-2-Clause-Patent > +**/ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define MAX_RANDOM_PROCESSOR_RETRIES  10 > + > +#define AP_STARTUP_TEST_TIMEOUT_US  50000 > +#define INFINITE_TIMEOUT            0 > + > +/** The procedure to run with the MP Services interface. > + > +  @param Buffer The procedure argument. > + > +**/ > +STATIC > +VOID > +EFIAPI > +ApFunction ( > +  IN OUT VOID  *Buffer > +  ) > +{ > +} > + > +/** Displays information returned from MP Services Protocol. > + > +  @param Mp  The MP Services Protocol > + > +  @return The number of CPUs in the system. > + > +**/ > +STATIC > +UINTN > +PrintProcessorInformation ( > +  IN EFI_MP_SERVICES_PROTOCOL  *Mp > +  ) > +{ > +  EFI_STATUS                 Status; > +  EFI_PROCESSOR_INFORMATION  CpuInfo; > +  UINTN                      Index; > +  UINTN                      NumCpu; > +  UINTN                      NumEnabledCpu; > + > +  Status = Mp->GetNumberOfProcessors (Mp, &NumCpu, &NumEnabledCpu); > +  if (EFI_ERROR (Status)) { > +    Print (L"GetNumberOfProcessors failed: %r\n", Status); > +  } else { > +    Print (L"Number of CPUs: %ld, Enabled: %d\n", NumCpu, > NumEnabledCpu); > +  } > + > +  for (Index = 0; Index < NumCpu; Index++) { > +    Status = Mp->GetProcessorInfo (Mp, CPU_V2_EXTENDED_TOPOLOGY | > Index, &CpuInfo); > +    if (EFI_ERROR (Status)) { > +      Print (L"GetProcessorInfo for Processor %d failed: %r\n", > Index, Status); > +    } else { > +      Print ( > +        L"Processor %d:\n" > +        L"\tID: %016lx\n" > +        L"\tStatus: %s | ", > +        Index, > +        CpuInfo.ProcessorId, > +        (CpuInfo.StatusFlag & PROCESSOR_AS_BSP_BIT) ? L"BSP" : L"AP" > +        ); > + > +      Print (L"%s | ", (CpuInfo.StatusFlag & > PROCESSOR_ENABLED_BIT) ? L"Enabled" : L"Disabled"); > +      Print (L"%s\n", (CpuInfo.StatusFlag & > PROCESSOR_HEALTH_STATUS_BIT) ? L"Healthy" : L"Faulted"); > + > +      Print ( > +        L"\tLocation: Package %d, Core %d, Thread %d\n" > +        L"\tExtended Information: Package %d, Module %d, Tile %d, > Die %d, Core %d, Thread %d\n\n", > +        CpuInfo.Location.Package, > +        CpuInfo.Location.Core, > +        CpuInfo.Location.Thread, > +        CpuInfo.ExtendedInformation.Location2.Package, > +        CpuInfo.ExtendedInformation.Location2.Module, > +        CpuInfo.ExtendedInformation.Location2.Tile, > +        CpuInfo.ExtendedInformation.Location2.Die, > +        CpuInfo.ExtendedInformation.Location2.Core, > +        CpuInfo.ExtendedInformation.Location2.Thread > +        ); > +    } > +  } > + > +  return NumCpu; > +} > + > +/** Returns the index of an enabled AP selected at random. > + > +  @param Mp             The MP Services Protocol. > +  @param ProcessorIndex The index of a random enabled AP. > + > +  @retval EFI_SUCCESS   An enabled processor was found and returned. > +  @retval EFI_NOT_FOUND A processor was unable to be selected. > + > +**/ > +STATIC > +EFI_STATUS > +GetRandomEnabledProcessorIndex ( > +  IN EFI_MP_SERVICES_PROTOCOL  *Mp, > +  OUT UINTN                    *ProcessorIndex > +  ) > +{ > +  UINTN                      Index; > +  UINTN                      IndexOfEnabledCpu; > +  UINTN                      NumCpus; > +  UINTN                      NumEnabledCpus; > +  UINTN                      IndexOfEnabledCpuToUse; > +  UINT16                     RandomNumber; > +  BOOLEAN                    Success; > +  EFI_STATUS                 Status; > +  EFI_PROCESSOR_INFORMATION  CpuInfo; > + > +  IndexOfEnabledCpu = 0; > + > +  Success = GetRandomNumber16 (&RandomNumber); > +  ASSERT (Success == TRUE); > + > +  Status = Mp->GetNumberOfProcessors (Mp, &NumCpus, &NumEnabledCpus); > +  ASSERT_EFI_ERROR (Status); > + > +  if (NumEnabledCpus == 1) { > +    Print (L"All APs are disabled\n"); > +    return EFI_NOT_FOUND; > +  } > + > +  IndexOfEnabledCpuToUse = RandomNumber % NumEnabledCpus; > + > +  for (Index = 0; Index < NumCpus; Index++) { > +    Status = Mp->GetProcessorInfo (Mp, Index, &CpuInfo); > +    ASSERT_EFI_ERROR (Status); > +    if ((CpuInfo.StatusFlag & PROCESSOR_ENABLED_BIT) && > +        !(CpuInfo.StatusFlag & PROCESSOR_AS_BSP_BIT)) > +    { > +      if (IndexOfEnabledCpuToUse == IndexOfEnabledCpu) { > +        *ProcessorIndex = Index; > +        Status          = EFI_SUCCESS; > +        break; > +      } > + > +      IndexOfEnabledCpu++; > +    } > +  } > + > +  if (Index == NumCpus) { > +    Status = EFI_NOT_FOUND; > +  } > + > +  return Status; > +} > + > +/** Tests for the StartupThisAP function. > + > +  @param Mp The MP Services Protocol. > + > +**/ > +STATIC > +VOID > +StartupThisApTests ( > +  IN EFI_MP_SERVICES_PROTOCOL  *Mp > +  ) > +{ > +  EFI_STATUS  Status; > +  UINTN       ProcessorIndex; > +  UINT32      Retries; > + > +  Retries = 0; > + > +  do { > +    Status = GetRandomEnabledProcessorIndex (Mp, &ProcessorIndex); > +  } while (EFI_ERROR (Status) && Retries++ < > MAX_RANDOM_PROCESSOR_RETRIES); > + > +  if (EFI_ERROR (Status)) { > +    return; > +  } > + > +  Print ( > +    L"StartupThisAP on Processor %d with 0 (infinite) timeout...", > +    ProcessorIndex > +    ); > + > +  Status = Mp->StartupThisAP ( > +                 Mp, > +                 ApFunction, > +                 ProcessorIndex, > +                 NULL, > +                 INFINITE_TIMEOUT, > +                 NULL, > +                 NULL > +                 ); > + > +  if (EFI_ERROR (Status)) { > +    Print (L"failed: %r\n", Status); > +    return; > +  } else { > +    Print (L"done.\n"); > +  } > + > +  Retries = 0; > + > +  do { > +    Status = GetRandomEnabledProcessorIndex (Mp, &ProcessorIndex); > +  } while (EFI_ERROR (Status) && Retries++ < > MAX_RANDOM_PROCESSOR_RETRIES); > + > +  if (EFI_ERROR (Status)) { > +    return; > +  } > + > +  Print ( > +    L"StartupThisAP on Processor %d with %dms timeout...", > +    ProcessorIndex, > +    AP_STARTUP_TEST_TIMEOUT_US / 1000 > +    ); > +  Status = Mp->StartupThisAP ( > +                 Mp, > +                 ApFunction, > +                 ProcessorIndex, > +                 NULL, > +                 AP_STARTUP_TEST_TIMEOUT_US, > +                 NULL, > +                 NULL > +                 ); > +  if (EFI_ERROR (Status)) { > +    Print (L"failed: %r\n", Status); > +    return; > +  } else { > +    Print (L"done.\n"); > +  } > +} > + > +/** Tests for the StartupAllAPs function. > + > +  @param Mp      The MP Services Protocol. > +  @param NumCpus The number of CPUs in the system. > + > +**/ > +STATIC > +VOID > +StartupAllAPsTests ( > +  IN EFI_MP_SERVICES_PROTOCOL  *Mp, > +  IN UINTN                     NumCpus > +  ) > +{ > +  EFI_STATUS  Status; > +  UINTN       Timeout; > + > +  Print (L"Running with SingleThread FALSE, 0 (infinite) > timeout..."); > +  Status = Mp->StartupAllAPs (Mp, ApFunction, FALSE, NULL, > INFINITE_TIMEOUT, NULL, NULL); > +  if (EFI_ERROR (Status)) { > +    Print (L"failed: %r\n", Status); > +    return; > +  } else { > +    Print (L"done.\n"); > +  } > + > +  Timeout = NumCpus * AP_STARTUP_TEST_TIMEOUT_US; > + > +  Print (L"Running with SingleThread TRUE, %dms timeout...", > Timeout / 1000); > +  Status = Mp->StartupAllAPs ( > +                 Mp, > +                 ApFunction, > +                 TRUE, > +                 NULL, > +                 Timeout, > +                 NULL, > +                 NULL > +                 ); > +  if (EFI_ERROR (Status)) { > +    Print (L"failed: %r\n", Status); > +    return; > +  } else { > +    Print (L"done.\n"); > +  } > +} > + > +/** Tests for the EnableDisableAP function. > + > +  @param Mp      The MP Services Protocol. > +  @param NumCpus The number of CPUs in the system. > + > +**/ > +STATIC > +VOID > +EnableDisableAPTests ( > +  IN EFI_MP_SERVICES_PROTOCOL  *Mp, > +  IN UINTN                     NumCpus > +  ) > +{ > +  EFI_STATUS  Status; > +  UINTN       Index; > +  UINT32      HealthFlag; > + > +  HealthFlag = 0; > + > +  for (Index = 1; Index < NumCpus; Index++) { > +    Print (L"Disabling Processor %d with HealthFlag faulted...", > Index); > +    Status = Mp->EnableDisableAP (Mp, Index, FALSE, &HealthFlag); > +    if (EFI_ERROR (Status)) { > +      Print (L"failed: %r\n", Status); > +      return; > +    } else { > +      Print (L"done.\n"); > +    } > +  } > + > +  HealthFlag = PROCESSOR_HEALTH_STATUS_BIT; > + > +  for (Index = 1; Index < NumCpus; Index++) { > +    Print (L"Enabling Processor %d with HealthFlag healthy...", > Index); > +    Status = Mp->EnableDisableAP (Mp, Index, TRUE, &HealthFlag); > +    if (EFI_ERROR (Status)) { > +      Print (L"failed: %r\n", Status); > +      return; > +    } else { > +      Print (L"done.\n"); > +    } > +  } > +} > + > +/** > +  The user Entry Point for Application. The user code starts with > this function > +  as the real entry point for the application. > + > +  @param[in] ImageHandle    The firmware allocated handle for the > EFI image. > +  @param[in] SystemTable    A pointer to the EFI System Table. > + > +  @retval EFI_SUCCESS       The entry point is executed successfully. > +  @retval other             Some error occurs when executing this > entry point. > + > +**/ > +EFI_STATUS > +EFIAPI > +UefiMain ( > +  IN EFI_HANDLE        ImageHandle, > +  IN EFI_SYSTEM_TABLE  *SystemTable > +  ) > +{ > +  EFI_STATUS                Status; > +  EFI_MP_SERVICES_PROTOCOL  *Mp; > +  EFI_HANDLE                *pHandle; > +  UINTN                     HandleCount; > +  UINTN                     BspId; > +  UINTN                     NumCpus; > +  UINTN                     Index; > + > +  pHandle     = NULL; > +  HandleCount = 0; > +  BspId       = 0; > + > +  Status = gBS->LocateHandleBuffer ( > +                  ByProtocol, > +                  &gEfiMpServiceProtocolGuid, > +                  NULL, > +                  &HandleCount, > +                  &pHandle > +                  ); > + > +  if (EFI_ERROR (Status)) { > +    Print (L"Failed to locate EFI_MP_SERVICES_PROTOCOL (%r). Not > installed on platform?\n", Status); > +    return EFI_NOT_FOUND; > +  } > + > +  for (Index = 0; Index < HandleCount; Index++) { > +    Status = gBS->OpenProtocol ( > +                    *pHandle, > +                    &gEfiMpServiceProtocolGuid, > +                    (VOID **)&Mp, > +                    NULL, > +                    gImageHandle, > +                    EFI_OPEN_PROTOCOL_GET_PROTOCOL > +                    ); > + > +    if (EFI_ERROR (Status)) { > +      return Status; > +    } > + > +    pHandle++; > +  } > + > +  Print (L"Exercising WhoAmI\n\n"); > +  Status = Mp->WhoAmI (Mp, &BspId); > +  if (EFI_ERROR (Status)) { > +    Print (L"WhoAmI failed: %r\n", Status); > +    return Status; > +  } else { > +    Print (L"WhoAmI: %016lx\n", BspId); > +  } > + > +  Print (L"\n"); > +  Print ( > +    L"Exercising GetNumberOfProcessors and > GetProcessorInformation with " > +    L"CPU_V2_EXTENDED_TOPOLOGY\n\n" > +    ); > +  NumCpus = PrintProcessorInformation (Mp); > +  if (NumCpus < 2) { > +    Print (L"UP system found. Not running further tests.\n"); > +    return EFI_INVALID_PARAMETER; > +  } > + > +  Print (L"\n"); > +  Print (L"Exercising StartupThisAP:\n\n"); > +  StartupThisApTests (Mp); > + > +  Print (L"\n"); > +  Print (L"Exercising StartupAllAPs:\n\n"); > +  StartupAllAPsTests (Mp, NumCpus); > + > +  Print (L"\n"); > +  Print (L"Exercising EnableDisableAP:\n\n"); > +  EnableDisableAPTests (Mp, NumCpus); > + > +  gBS->CloseProtocol (pHandle, &gEfiMpServiceProtocolGuid, > gImageHandle, NULL); > +  return EFI_SUCCESS; > +} > diff --git > a/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf > b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf > new file mode 100644 > index 000000000000..8a21ca70d8fa > --- /dev/null > +++ b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf > @@ -0,0 +1,38 @@ > +## @file > +#  UEFI Application to exercise EFI_MP_SERVICES_PROTOCOL. > +# > +#  Copyright (c) 2021, NUVIA Inc. All rights reserved.
> +# > +#  SPDX-License-Identifier: BSD-2-Clause-Patent > +# > +## > + > +[Defines] > +  INF_VERSION                    = 1.29 > +  BASE_NAME                      = MpServicesTest > +  FILE_GUID                      = > 43e9defa-7209-4b0d-b136-cc4ca02cb469 > +  MODULE_TYPE                    = UEFI_APPLICATION > +  VERSION_STRING                 = 0.1 > +  ENTRY_POINT                    = UefiMain > + > +# > +# The following information is for reference only and not > required by the build tools. > +# > +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64 > +# > + > +[Sources] > +  MpServicesTest.c > + > +[Packages] > +  MdePkg/MdePkg.dec > + > +[LibraryClasses] > +  BaseLib > +  RngLib > +  UefiApplicationEntryPoint > +  UefiLib > + > +[Protocols] > +  gEfiMpServiceProtocolGuid    ## CONSUMES > + > diff --git a/MdeModulePkg/MdeModulePkg.dsc > b/MdeModulePkg/MdeModulePkg.dsc > index b1d83461865e..1cf5ccd30d40 100644 > --- a/MdeModulePkg/MdeModulePkg.dsc > +++ b/MdeModulePkg/MdeModulePkg.dsc > @@ -164,6 +164,7 @@ > MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf > DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf > FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf > +  RngLib|MdePkg/Library/DxeRngLib/DxeRngLib.inf > [LibraryClasses.common.MM_STANDALONE] > HobLib|MdeModulePkg/Library/BaseHobLibNull/BaseHobLibNull.inf > @@ -215,6 +216,7 @@ >    MdeModulePkg/Application/HelloWorld/HelloWorld.inf >    MdeModulePkg/Application/DumpDynPcd/DumpDynPcd.inf > MdeModulePkg/Application/MemoryProfileInfo/MemoryProfileInfo.inf > + MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf >    MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf >    MdeModulePkg/Logo/Logo.inf > -- > 2.31.1 > > --------------p4QxOCAjsjDXxC0xvxXHsmEC Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

No, I expect there to only ever be a single MP services instance.

I agree that the AP function should do something, but I'm hoping to get this initial implementation committed first.


--
Rebecca Cran


On 12/29/21 20:21, Jeff Fan wrote:
Hi,Rebecca

I'd like to give two comments on this patch.

1,  I don't think there are more than one MP services instances installed reuirement on one system.  X86 platform installed one MP service instance even on mutiple-sockets system. 
    For ARM platform, is there any requirement to handle multple MP services instance?

2,  AP function is NULL implementation that could not make sure BSP recongized if APs run indeed.  You could simply update AP function to use one global variable seamphoere wrapped by lock protections.
    I think this patch is very good start to add MP test app in open source world.  This comment is only sugestion for your next plan. :-)

Best Regards,
Jeff
 
Date: 2021-12-13 02:08
Subject: [edk2-devel] [PATCH v4 1/1] MdeModulePkg: Add MpServicesTest application to exercise MP Services
Add a new MpServicesTest application under MdeModulePkg/Application that
exercises the EFI_MP_SERVICES_PROTOCOL.
 
Signed-off-by: Rebecca Cran <rebecca@nuviainc.com>
Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
---
MdeModulePkg/Application/MpServicesTest/MpServicesTest.c   | 422 ++++++++++++++++++++
MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf |  38 ++
MdeModulePkg/MdeModulePkg.dsc                              |   2 +
3 files changed, 462 insertions(+)
 
diff --git a/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c
new file mode 100644
index 000000000000..933813e19e05
--- /dev/null
+++ b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.c
@@ -0,0 +1,422 @@
+/** @file
+
+    Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
+    SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include <Uefi.h>
+#include <Library/DebugLib.h>
+#include <Library/RngLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Pi/PiMultiPhase.h>
+#include <Protocol/MpService.h>
+
+#define MAX_RANDOM_PROCESSOR_RETRIES  10
+
+#define AP_STARTUP_TEST_TIMEOUT_US  50000
+#define INFINITE_TIMEOUT            0
+
+/** The procedure to run with the MP Services interface.
+
+  @param Buffer The procedure argument.
+
+**/
+STATIC
+VOID
+EFIAPI
+ApFunction (
+  IN OUT VOID  *Buffer
+  )
+{
+}
+
+/** Displays information returned from MP Services Protocol.
+
+  @param Mp  The MP Services Protocol
+
+  @return The number of CPUs in the system.
+
+**/
+STATIC
+UINTN
+PrintProcessorInformation (
+  IN EFI_MP_SERVICES_PROTOCOL  *Mp
+  )
+{
+  EFI_STATUS                 Status;
+  EFI_PROCESSOR_INFORMATION  CpuInfo;
+  UINTN                      Index;
+  UINTN                      NumCpu;
+  UINTN                      NumEnabledCpu;
+
+  Status = Mp->GetNumberOfProcessors (Mp, &NumCpu, &NumEnabledCpu);
+  if (EFI_ERROR (Status)) {
+    Print (L"GetNumberOfProcessors failed: %r\n", Status);
+  } else {
+    Print (L"Number of CPUs: %ld, Enabled: %d\n", NumCpu, NumEnabledCpu);
+  }
+
+  for (Index = 0; Index < NumCpu; Index++) {
+    Status = Mp->GetProcessorInfo (Mp, CPU_V2_EXTENDED_TOPOLOGY | Index, &CpuInfo);
+    if (EFI_ERROR (Status)) {
+      Print (L"GetProcessorInfo for Processor %d failed: %r\n", Index, Status);
+    } else {
+      Print (
+        L"Processor %d:\n"
+        L"\tID: %016lx\n"
+        L"\tStatus: %s | ",
+        Index,
+        CpuInfo.ProcessorId,
+        (CpuInfo.StatusFlag & PROCESSOR_AS_BSP_BIT) ? L"BSP" : L"AP"
+        );
+
+      Print (L"%s | ", (CpuInfo.StatusFlag & PROCESSOR_ENABLED_BIT) ? L"Enabled" : L"Disabled");
+      Print (L"%s\n", (CpuInfo.StatusFlag & PROCESSOR_HEALTH_STATUS_BIT) ? L"Healthy" : L"Faulted");
+
+      Print (
+        L"\tLocation: Package %d, Core %d, Thread %d\n"
+        L"\tExtended Information: Package %d, Module %d, Tile %d, Die %d, Core %d, Thread %d\n\n",
+        CpuInfo.Location.Package,
+        CpuInfo.Location.Core,
+        CpuInfo.Location.Thread,
+        CpuInfo.ExtendedInformation.Location2.Package,
+        CpuInfo.ExtendedInformation.Location2.Module,
+        CpuInfo.ExtendedInformation.Location2.Tile,
+        CpuInfo.ExtendedInformation.Location2.Die,
+        CpuInfo.ExtendedInformation.Location2.Core,
+        CpuInfo.ExtendedInformation.Location2.Thread
+        );
+    }
+  }
+
+  return NumCpu;
+}
+
+/** Returns the index of an enabled AP selected at random.
+
+  @param Mp             The MP Services Protocol.
+  @param ProcessorIndex The index of a random enabled AP.
+
+  @retval EFI_SUCCESS   An enabled processor was found and returned.
+  @retval EFI_NOT_FOUND A processor was unable to be selected.
+
+**/
+STATIC
+EFI_STATUS
+GetRandomEnabledProcessorIndex (
+  IN EFI_MP_SERVICES_PROTOCOL  *Mp,
+  OUT UINTN                    *ProcessorIndex
+  )
+{
+  UINTN                      Index;
+  UINTN                      IndexOfEnabledCpu;
+  UINTN                      NumCpus;
+  UINTN                      NumEnabledCpus;
+  UINTN                      IndexOfEnabledCpuToUse;
+  UINT16                     RandomNumber;
+  BOOLEAN                    Success;
+  EFI_STATUS                 Status;
+  EFI_PROCESSOR_INFORMATION  CpuInfo;
+
+  IndexOfEnabledCpu = 0;
+
+  Success = GetRandomNumber16 (&RandomNumber);
+  ASSERT (Success == TRUE);
+
+  Status = Mp->GetNumberOfProcessors (Mp, &NumCpus, &NumEnabledCpus);
+  ASSERT_EFI_ERROR (Status);
+
+  if (NumEnabledCpus == 1) {
+    Print (L"All APs are disabled\n");
+    return EFI_NOT_FOUND;
+  }
+
+  IndexOfEnabledCpuToUse = RandomNumber % NumEnabledCpus;
+
+  for (Index = 0; Index < NumCpus; Index++) {
+    Status = Mp->GetProcessorInfo (Mp, Index, &CpuInfo);
+    ASSERT_EFI_ERROR (Status);
+    if ((CpuInfo.StatusFlag & PROCESSOR_ENABLED_BIT) &&
+        !(CpuInfo.StatusFlag & PROCESSOR_AS_BSP_BIT))
+    {
+      if (IndexOfEnabledCpuToUse == IndexOfEnabledCpu) {
+        *ProcessorIndex = Index;
+        Status          = EFI_SUCCESS;
+        break;
+      }
+
+      IndexOfEnabledCpu++;
+    }
+  }
+
+  if (Index == NumCpus) {
+    Status = EFI_NOT_FOUND;
+  }
+
+  return Status;
+}
+
+/** Tests for the StartupThisAP function.
+
+  @param Mp The MP Services Protocol.
+
+**/
+STATIC
+VOID
+StartupThisApTests (
+  IN EFI_MP_SERVICES_PROTOCOL  *Mp
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       ProcessorIndex;
+  UINT32      Retries;
+
+  Retries = 0;
+
+  do {
+    Status = GetRandomEnabledProcessorIndex (Mp, &ProcessorIndex);
+  } while (EFI_ERROR (Status) && Retries++ < MAX_RANDOM_PROCESSOR_RETRIES);
+
+  if (EFI_ERROR (Status)) {
+    return;
+  }
+
+  Print (
+    L"StartupThisAP on Processor %d with 0 (infinite) timeout...",
+    ProcessorIndex
+    );
+
+  Status = Mp->StartupThisAP (
+                 Mp,
+                 ApFunction,
+                 ProcessorIndex,
+                 NULL,
+                 INFINITE_TIMEOUT,
+                 NULL,
+                 NULL
+                 );
+
+  if (EFI_ERROR (Status)) {
+    Print (L"failed: %r\n", Status);
+    return;
+  } else {
+    Print (L"done.\n");
+  }
+
+  Retries = 0;
+
+  do {
+    Status = GetRandomEnabledProcessorIndex (Mp, &ProcessorIndex);
+  } while (EFI_ERROR (Status) && Retries++ < MAX_RANDOM_PROCESSOR_RETRIES);
+
+  if (EFI_ERROR (Status)) {
+    return;
+  }
+
+  Print (
+    L"StartupThisAP on Processor %d with %dms timeout...",
+    ProcessorIndex,
+    AP_STARTUP_TEST_TIMEOUT_US / 1000
+    );
+  Status = Mp->StartupThisAP (
+                 Mp,
+                 ApFunction,
+                 ProcessorIndex,
+                 NULL,
+                 AP_STARTUP_TEST_TIMEOUT_US,
+                 NULL,
+                 NULL
+                 );
+  if (EFI_ERROR (Status)) {
+    Print (L"failed: %r\n", Status);
+    return;
+  } else {
+    Print (L"done.\n");
+  }
+}
+
+/** Tests for the StartupAllAPs function.
+
+  @param Mp      The MP Services Protocol.
+  @param NumCpus The number of CPUs in the system.
+
+**/
+STATIC
+VOID
+StartupAllAPsTests (
+  IN EFI_MP_SERVICES_PROTOCOL  *Mp,
+  IN UINTN                     NumCpus
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       Timeout;
+
+  Print (L"Running with SingleThread FALSE, 0 (infinite) timeout...");
+  Status = Mp->StartupAllAPs (Mp, ApFunction, FALSE, NULL, INFINITE_TIMEOUT, NULL, NULL);
+  if (EFI_ERROR (Status)) {
+    Print (L"failed: %r\n", Status);
+    return;
+  } else {
+    Print (L"done.\n");
+  }
+
+  Timeout = NumCpus * AP_STARTUP_TEST_TIMEOUT_US;
+
+  Print (L"Running with SingleThread TRUE, %dms timeout...", Timeout / 1000);
+  Status = Mp->StartupAllAPs (
+                 Mp,
+                 ApFunction,
+                 TRUE,
+                 NULL,
+                 Timeout,
+                 NULL,
+                 NULL
+                 );
+  if (EFI_ERROR (Status)) {
+    Print (L"failed: %r\n", Status);
+    return;
+  } else {
+    Print (L"done.\n");
+  }
+}
+
+/** Tests for the EnableDisableAP function.
+
+  @param Mp      The MP Services Protocol.
+  @param NumCpus The number of CPUs in the system.
+
+**/
+STATIC
+VOID
+EnableDisableAPTests (
+  IN EFI_MP_SERVICES_PROTOCOL  *Mp,
+  IN UINTN                     NumCpus
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       Index;
+  UINT32      HealthFlag;
+
+  HealthFlag = 0;
+
+  for (Index = 1; Index < NumCpus; Index++) {
+    Print (L"Disabling Processor %d with HealthFlag faulted...", Index);
+    Status = Mp->EnableDisableAP (Mp, Index, FALSE, &HealthFlag);
+    if (EFI_ERROR (Status)) {
+      Print (L"failed: %r\n", Status);
+      return;
+    } else {
+      Print (L"done.\n");
+    }
+  }
+
+  HealthFlag = PROCESSOR_HEALTH_STATUS_BIT;
+
+  for (Index = 1; Index < NumCpus; Index++) {
+    Print (L"Enabling Processor %d with HealthFlag healthy...", Index);
+    Status = Mp->EnableDisableAP (Mp, Index, TRUE, &HealthFlag);
+    if (EFI_ERROR (Status)) {
+      Print (L"failed: %r\n", Status);
+      return;
+    } else {
+      Print (L"done.\n");
+    }
+  }
+}
+
+/**
+  The user Entry Point for Application. The user code starts with this function
+  as the real entry point for the application.
+
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in] SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+  @retval other             Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+UefiMain (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS                Status;
+  EFI_MP_SERVICES_PROTOCOL  *Mp;
+  EFI_HANDLE                *pHandle;
+  UINTN                     HandleCount;
+  UINTN                     BspId;
+  UINTN                     NumCpus;
+  UINTN                     Index;
+
+  pHandle     = NULL;
+  HandleCount = 0;
+  BspId       = 0;
+
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEfiMpServiceProtocolGuid,
+                  NULL,
+                  &HandleCount,
+                  &pHandle
+                  );
+
+  if (EFI_ERROR (Status)) {
+    Print (L"Failed to locate EFI_MP_SERVICES_PROTOCOL (%r). Not installed on platform?\n", Status);
+    return EFI_NOT_FOUND;
+  }
+
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->OpenProtocol (
+                    *pHandle,
+                    &gEfiMpServiceProtocolGuid,
+                    (VOID **)&Mp,
+                    NULL,
+                    gImageHandle,
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL
+                    );
+
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    pHandle++;
+  }
+
+  Print (L"Exercising WhoAmI\n\n");
+  Status = Mp->WhoAmI (Mp, &BspId);
+  if (EFI_ERROR (Status)) {
+    Print (L"WhoAmI failed: %r\n", Status);
+    return Status;
+  } else {
+    Print (L"WhoAmI: %016lx\n", BspId);
+  }
+
+  Print (L"\n");
+  Print (
+    L"Exercising GetNumberOfProcessors and GetProcessorInformation with "
+    L"CPU_V2_EXTENDED_TOPOLOGY\n\n"
+    );
+  NumCpus = PrintProcessorInformation (Mp);
+  if (NumCpus < 2) {
+    Print (L"UP system found. Not running further tests.\n");
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Print (L"\n");
+  Print (L"Exercising StartupThisAP:\n\n");
+  StartupThisApTests (Mp);
+
+  Print (L"\n");
+  Print (L"Exercising StartupAllAPs:\n\n");
+  StartupAllAPsTests (Mp, NumCpus);
+
+  Print (L"\n");
+  Print (L"Exercising EnableDisableAP:\n\n");
+  EnableDisableAPTests (Mp, NumCpus);
+
+  gBS->CloseProtocol (pHandle, &gEfiMpServiceProtocolGuid, gImageHandle, NULL);
+  return EFI_SUCCESS;
+}
diff --git a/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf
new file mode 100644
index 000000000000..8a21ca70d8fa
--- /dev/null
+++ b/MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf
@@ -0,0 +1,38 @@
+## @file
+#  UEFI Application to exercise EFI_MP_SERVICES_PROTOCOL.
+#
+#  Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 1.29
+  BASE_NAME                      = MpServicesTest
+  FILE_GUID                      = 43e9defa-7209-4b0d-b136-cc4ca02cb469
+  MODULE_TYPE                    = UEFI_APPLICATION
+  VERSION_STRING                 = 0.1
+  ENTRY_POINT                    = UefiMain
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+#
+
+[Sources]
+  MpServicesTest.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  BaseLib
+  RngLib
+  UefiApplicationEntryPoint
+  UefiLib
+
+[Protocols]
+  gEfiMpServiceProtocolGuid    ## CONSUMES
+
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index b1d83461865e..1cf5ccd30d40 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -164,6 +164,7 @@
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
   DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf
+  RngLib|MdePkg/Library/DxeRngLib/DxeRngLib.inf
[LibraryClasses.common.MM_STANDALONE]
   HobLib|MdeModulePkg/Library/BaseHobLibNull/BaseHobLibNull.inf
@@ -215,6 +216,7 @@
   MdeModulePkg/Application/HelloWorld/HelloWorld.inf
   MdeModulePkg/Application/DumpDynPcd/DumpDynPcd.inf
   MdeModulePkg/Application/MemoryProfileInfo/MemoryProfileInfo.inf
+  MdeModulePkg/Application/MpServicesTest/MpServicesTest.inf
   MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf
   MdeModulePkg/Logo/Logo.inf
--
2.31.1
 
 
 
 
 
 
--------------p4QxOCAjsjDXxC0xvxXHsmEC--