public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Eric Dong <eric.dong@intel.com>
To: edk2-devel@lists.01.org
Cc: Ruiyu Ni <ruiyu.ni@intel.com>, Laszlo Ersek <lersek@redhat.com>
Subject: [Patch 2/2] UefiCpuPkg/RegisterCpuFeaturesLib: Avoid AP calls PeiService.
Date: Mon,  7 Jan 2019 09:05:55 +0800	[thread overview]
Message-ID: <20190107010555.23264-3-eric.dong@intel.com> (raw)
In-Reply-To: <20190107010555.23264-1-eric.dong@intel.com>

V2:
1. Initialize CpuFeaturesData->MpService in CpuInitDataInitialize
   and make this function been called at the begin of the
   initialization.
2. let all other functions use CpuFeaturesData->MpService install
   of locate the protocol itself.

V1:
GetProcessorIndex function calls GetMpPpi to get the MP Ppi.
Ap will calls GetProcessorIndex function which final let AP calls
PeiService.

This patch avoid GetProcessorIndex call PeiService.

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1411

Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Eric Dong <eric.dong@intel.com>

---
 .../RegisterCpuFeaturesLib/CpuFeaturesInitialize.c | 22 +++++---
 .../DxeRegisterCpuFeaturesLib.c                    | 57 +++++++++++---------
 .../PeiRegisterCpuFeaturesLib.c                    | 62 +++++++++-------------
 .../RegisterCpuFeaturesLib/RegisterCpuFeatures.h   | 16 +++++-
 4 files changed, 85 insertions(+), 72 deletions(-)

diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c
index 624ddee055..5866e022f0 100644
--- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c
+++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c
@@ -138,7 +138,7 @@ FillProcessorInfo (
 **/
 VOID
 CpuInitDataInitialize (
-  IN UINTN                             NumberOfCpus
+  VOID
   )
 {
   EFI_STATUS                           Status;
@@ -157,12 +157,22 @@ CpuInitDataInitialize (
   ACPI_CPU_DATA                        *AcpiCpuData;
   CPU_STATUS_INFORMATION               *CpuStatus;
   UINT32                               *ValidCoreCountPerPackage;
+  UINTN                                NumberOfCpus;
+  UINTN                                NumberOfEnabledProcessors;
 
   Core    = 0;
   Package = 0;
   Thread  = 0;
 
   CpuFeaturesData = GetCpuFeaturesData ();
+
+  //
+  // Initialize CpuFeaturesData->MpService as early as possile, so later function can use it.
+  //
+  CpuFeaturesData->MpService = GetMpService ();
+
+  GetNumberOfProcessor (&NumberOfCpus, &NumberOfEnabledProcessors);
+
   CpuFeaturesData->InitOrder = AllocateZeroPool (sizeof (CPU_FEATURES_INIT_ORDER) * NumberOfCpus);
   ASSERT (CpuFeaturesData->InitOrder != NULL);
 
@@ -409,7 +419,7 @@ CollectProcessorData (
   CPU_FEATURES_DATA                    *CpuFeaturesData;
 
   CpuFeaturesData = (CPU_FEATURES_DATA *)Buffer;
-  ProcessorNumber = GetProcessorIndex ();
+  ProcessorNumber = GetProcessorIndex (CpuFeaturesData);
   CpuInfo = &CpuFeaturesData->InitOrder[ProcessorNumber].CpuInfo;
   //
   // collect processor information
@@ -1105,15 +1115,11 @@ CpuFeaturesDetect (
   VOID
   )
 {
-  UINTN                  NumberOfCpus;
-  UINTN                  NumberOfEnabledProcessors;
   CPU_FEATURES_DATA      *CpuFeaturesData;
 
   CpuFeaturesData = GetCpuFeaturesData();
 
-  GetNumberOfProcessor (&NumberOfCpus, &NumberOfEnabledProcessors);
-
-  CpuInitDataInitialize (NumberOfCpus);
+  CpuInitDataInitialize ();
 
   //
   // Wakeup all APs for data collection.
@@ -1125,6 +1131,6 @@ CpuFeaturesDetect (
   //
   CollectProcessorData (CpuFeaturesData);
 
-  AnalysisProcessorFeatures (NumberOfCpus);
+  AnalysisProcessorFeatures (CpuFeaturesData->NumberOfCpus);
 }
 
diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegisterCpuFeaturesLib.c b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegisterCpuFeaturesLib.c
index 926698dc95..2d2095e437 100644
--- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegisterCpuFeaturesLib.c
+++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegisterCpuFeaturesLib.c
@@ -20,7 +20,6 @@
 #include "RegisterCpuFeatures.h"
 
 CPU_FEATURES_DATA          mCpuFeaturesData = {0};
-EFI_MP_SERVICES_PROTOCOL   *mCpuFeaturesMpServices = NULL;
 
 /**
   Worker function to get CPU_FEATURES_DATA pointer.
@@ -40,44 +39,44 @@ GetCpuFeaturesData (
 
   @return Pointer to EFI_MP_SERVICES_PROTOCOL.
 **/
-EFI_MP_SERVICES_PROTOCOL *
-GetMpProtocol (
+VOID *
+GetMpService (
   VOID
   )
 {
-  EFI_STATUS             Status;
-
-  if (mCpuFeaturesMpServices == NULL) {
-    //
-    // Get MP Services Protocol
-    //
-    Status = gBS->LocateProtocol (
-                  &gEfiMpServiceProtocolGuid,
-                  NULL,
-                  (VOID **)&mCpuFeaturesMpServices
-                  );
-    ASSERT_EFI_ERROR (Status);
-  }
+  EFI_STATUS                Status;
+  EFI_MP_SERVICES_PROTOCOL  *MpServices;
 
-  ASSERT (mCpuFeaturesMpServices != NULL);
-  return mCpuFeaturesMpServices;
+  //
+  // Get MP Services Protocol
+  //
+  Status = gBS->LocateProtocol (
+                &gEfiMpServiceProtocolGuid,
+                NULL,
+                (VOID **)&MpServices
+                );
+  ASSERT_EFI_ERROR (Status);
+
+  return MpServices;
 }
 
 /**
   Worker function to return processor index.
 
+  @param  CpuFeaturesData    Cpu Feature Data structure.
+
   @return  The processor index.
 **/
 UINTN
 GetProcessorIndex (
-  VOID
+  IN CPU_FEATURES_DATA        *CpuFeaturesData
   )
 {
   EFI_STATUS                           Status;
   UINTN                                ProcessorIndex;
   EFI_MP_SERVICES_PROTOCOL             *MpServices;
 
-  MpServices = GetMpProtocol ();
+  MpServices = (EFI_MP_SERVICES_PROTOCOL *)CpuFeaturesData->MpService;
   Status = MpServices->WhoAmI(MpServices, &ProcessorIndex);
   ASSERT_EFI_ERROR (Status);
   return ProcessorIndex;
@@ -101,8 +100,11 @@ GetProcessorInformation (
 {
   EFI_STATUS                           Status;
   EFI_MP_SERVICES_PROTOCOL             *MpServices;
+  CPU_FEATURES_DATA                    *CpuFeaturesData;
+
+  CpuFeaturesData = GetCpuFeaturesData ();
+  MpServices = (EFI_MP_SERVICES_PROTOCOL *)CpuFeaturesData->MpService;
 
-  MpServices = GetMpProtocol ();
   Status = MpServices->GetProcessorInfo (
                MpServices,
                ProcessorNumber,
@@ -130,8 +132,8 @@ StartupAPsWorker (
   CPU_FEATURES_DATA                    *CpuFeaturesData;
 
   CpuFeaturesData = GetCpuFeaturesData ();
+  MpServices = (EFI_MP_SERVICES_PROTOCOL *)CpuFeaturesData->MpService;
 
-  MpServices = GetMpProtocol ();
   //
   // Wakeup all APs
   //
@@ -159,8 +161,11 @@ SwitchNewBsp (
 {
   EFI_STATUS                           Status;
   EFI_MP_SERVICES_PROTOCOL             *MpServices;
+  CPU_FEATURES_DATA                    *CpuFeaturesData;
+
+  CpuFeaturesData = GetCpuFeaturesData ();
+  MpServices = (EFI_MP_SERVICES_PROTOCOL *)CpuFeaturesData->MpService;
 
-  MpServices = GetMpProtocol ();
   //
   // Wakeup all APs
   //
@@ -190,8 +195,10 @@ GetNumberOfProcessor (
 {
   EFI_STATUS                           Status;
   EFI_MP_SERVICES_PROTOCOL             *MpServices;
+  CPU_FEATURES_DATA                    *CpuFeaturesData;
 
-  MpServices = GetMpProtocol ();
+  CpuFeaturesData = GetCpuFeaturesData ();
+  MpServices = (EFI_MP_SERVICES_PROTOCOL *)CpuFeaturesData->MpService;
 
   //
   // Get the number of CPUs
@@ -225,7 +232,7 @@ CpuFeaturesInitialize (
 
   CpuFeaturesData = GetCpuFeaturesData ();
 
-  OldBspNumber = GetProcessorIndex();
+  OldBspNumber = GetProcessorIndex (CpuFeaturesData);
   CpuFeaturesData->BspNumber = OldBspNumber;
 
   Status = gBS->CreateEvent (
diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/PeiRegisterCpuFeaturesLib.c b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/PeiRegisterCpuFeaturesLib.c
index 0bb3dee8b6..41f4dd54e8 100644
--- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/PeiRegisterCpuFeaturesLib.c
+++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/PeiRegisterCpuFeaturesLib.c
@@ -72,8 +72,8 @@ GetCpuFeaturesData (
 
   @return PEI PPI service pointer.
 **/
-EFI_PEI_MP_SERVICES_PPI *
-GetMpPpi (
+VOID *
+GetMpService (
   VOID
   )
 {
@@ -96,20 +96,27 @@ GetMpPpi (
 /**
   Worker function to return processor index.
 
+  @param  CpuFeaturesData    Cpu Feature Data structure.
+
   @return  The processor index.
 **/
 UINTN
 GetProcessorIndex (
-  VOID
+  IN CPU_FEATURES_DATA        *CpuFeaturesData
   )
 {
   EFI_STATUS                 Status;
   EFI_PEI_MP_SERVICES_PPI    *CpuMpPpi;
   UINTN                      ProcessorIndex;
 
-  CpuMpPpi = GetMpPpi ();
+  CpuMpPpi = (EFI_PEI_MP_SERVICES_PPI *)CpuFeaturesData->MpService;
 
-  Status = CpuMpPpi->WhoAmI(GetPeiServicesTablePointer (), CpuMpPpi, &ProcessorIndex);
+  //
+  // For two reasons which use NULL for WhoAmI:
+  // 1. This function will be called by APs and AP should not use PeiServices Table
+  // 2. Check WhoAmI implementation, this parameter will not be used.
+  //
+  Status = CpuMpPpi->WhoAmI(NULL, CpuMpPpi, &ProcessorIndex);
   ASSERT_EFI_ERROR (Status);
   return ProcessorIndex;
 }
@@ -132,8 +139,11 @@ GetProcessorInformation (
 {
   EFI_PEI_MP_SERVICES_PPI    *CpuMpPpi;
   EFI_STATUS                 Status;
+  CPU_FEATURES_DATA          *CpuFeaturesData;
+
+  CpuFeaturesData = GetCpuFeaturesData ();
+  CpuMpPpi = (EFI_PEI_MP_SERVICES_PPI *)CpuFeaturesData->MpService;
 
-  CpuMpPpi = GetMpPpi ();
   Status = CpuMpPpi->GetProcessorInfo (
                GetPeiServicesTablePointer(),
                CpuMpPpi,
@@ -162,17 +172,7 @@ StartupAPsWorker (
   CPU_FEATURES_DATA                    *CpuFeaturesData;
 
   CpuFeaturesData = GetCpuFeaturesData ();
-
-  //
-  // Get MP Services Protocol
-  //
-  Status = PeiServicesLocatePpi (
-             &gEfiPeiMpServicesPpiGuid,
-             0,
-             NULL,
-             (VOID **)&CpuMpPpi
-             );
-  ASSERT_EFI_ERROR (Status);
+  CpuMpPpi = (EFI_PEI_MP_SERVICES_PPI *)CpuFeaturesData->MpService;
 
   //
   // Wakeup all APs for data collection.
@@ -244,17 +244,10 @@ SwitchNewBsp (
 {
   EFI_STATUS                           Status;
   EFI_PEI_MP_SERVICES_PPI              *CpuMpPpi;
+  CPU_FEATURES_DATA                    *CpuFeaturesData;
 
-  //
-  // Get MP Services Protocol
-  //
-  Status = PeiServicesLocatePpi (
-             &gEfiPeiMpServicesPpiGuid,
-             0,
-             NULL,
-             (VOID **)&CpuMpPpi
-             );
-  ASSERT_EFI_ERROR (Status);
+  CpuFeaturesData = GetCpuFeaturesData ();
+  CpuMpPpi = (EFI_PEI_MP_SERVICES_PPI *)CpuFeaturesData->MpService;
 
   //
   // Wakeup all APs for data collection.
@@ -286,17 +279,10 @@ GetNumberOfProcessor (
 {
   EFI_STATUS                 Status;
   EFI_PEI_MP_SERVICES_PPI    *CpuMpPpi;
+  CPU_FEATURES_DATA          *CpuFeaturesData;
 
-  //
-  // Get MP Services Protocol
-  //
-  Status = PeiServicesLocatePpi (
-             &gEfiPeiMpServicesPpiGuid,
-             0,
-             NULL,
-             (VOID **)&CpuMpPpi
-             );
-  ASSERT_EFI_ERROR (Status);
+  CpuFeaturesData = GetCpuFeaturesData ();
+  CpuMpPpi = (EFI_PEI_MP_SERVICES_PPI *)CpuFeaturesData->MpService;
 
   //
   // Get the number of CPUs
@@ -329,7 +315,7 @@ CpuFeaturesInitialize (
 
   CpuFeaturesData = GetCpuFeaturesData ();
 
-  OldBspNumber = GetProcessorIndex();
+  OldBspNumber = GetProcessorIndex (CpuFeaturesData);
   CpuFeaturesData->BspNumber = OldBspNumber;
 
   //
diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeatures.h b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeatures.h
index cf3da84837..ac0a2f9cf4 100644
--- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeatures.h
+++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeatures.h
@@ -85,6 +85,8 @@ typedef struct {
   UINTN                    BspNumber;
 
   PROGRAM_CPU_REGISTER_FLAGS  CpuFlags;
+
+  VOID                     *MpService;
 } CPU_FEATURES_DATA;
 
 #define CPU_FEATURE_ENTRY_FROM_LINK(a) \
@@ -108,11 +110,13 @@ GetCpuFeaturesData (
 /**
   Worker function to return processor index.
 
+  @param  CpuFeaturesData    Cpu Feature Data structure.
+
   @return  The processor index.
 **/
 UINTN
 GetProcessorIndex (
-  VOID
+  IN CPU_FEATURES_DATA        *CpuFeaturesData
   );
 
 /**
@@ -245,4 +249,14 @@ GetAcpiCpuData (
   VOID
   );
 
+/**
+  Worker function to get MP service pointer.
+
+  @return Mp service pointer.
+**/
+VOID *
+GetMpService (
+  VOID
+  );
+
 #endif
-- 
2.15.0.windows.1



  parent reply	other threads:[~2019-01-07  1:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-07  1:05 [Patch v2 0/2] Avoid AP calls PeiServices table Eric Dong
2019-01-07  1:05 ` [Patch v2 1/2] UefiCpuPkg/RegisterCpuFeaturesLib: Enhance debug message Eric Dong
2019-01-07  7:53   ` Ni, Ruiyu
2019-01-07  1:05 ` Eric Dong [this message]
     [not found]   ` <734D49CCEBEEF84792F5B80ED585239D5BFA53EB@SHSMSX103.ccr.corp.intel.com>
2019-01-07 19:17     ` [Patch 2/2] UefiCpuPkg/RegisterCpuFeaturesLib: Avoid AP calls PeiService Laszlo Ersek
2019-01-08  5:32       ` Dong, Eric

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=20190107010555.23264-3-eric.dong@intel.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