From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web12.8499.1611640220106690392 for ; Mon, 25 Jan 2021 21:50:20 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: ray.ni@intel.com) IronPort-SDR: EY99ozFP7LO3QBhK0sxJU8+KG/VgNBBug7ZCaQPZse8F9uojpemWHvfu0w6qZFP2ZXZz2dsa3Z qpv4U9fqVXkg== X-IronPort-AV: E=McAfee;i="6000,8403,9875"; a="264676090" X-IronPort-AV: E=Sophos;i="5.79,375,1602572400"; d="scan'208";a="264676090" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2021 21:50:18 -0800 IronPort-SDR: 2ZcmBJ5i/mHR9fike95WKQdeArxCSMDs+vzM8x42SNKk2n+CvzPYPfAWLiAZKBwqK193n/IRGi Zo9J5Corkk7A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,375,1602572400"; d="scan'208";a="429578385" Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by orsmga001.jf.intel.com with ESMTP; 25 Jan 2021 21:50:17 -0800 From: "Ni, Ray" To: devel@edk2.groups.io Subject: [PATCH] UefiCpuPkg/MpInitLib: Don't increase CpuCount in ApWakeupFunction Date: Tue, 26 Jan 2021 13:50:15 +0800 Message-Id: <20210126055015.633-1-ray.ni@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3179 When BSP first time wakes all APs, each AP atomically increases CpuMpData->CpuCount and CpuMpData->FinishedCount. Each AP atomically increases CpuMpData->NumApsExecuting in early assembly code and decreases it before it enters to HLT or MWAIT state. Putting them together, the 3 variables are changed in the following order: 1. NumApsExecuting++ // in assembly 2. CpuCpunt++ 4. FinishedCount++ 3. NumApsExecuting-- // in C BSP waits for a certain timeout and then polls NumApsExecuting until it drops to zero. It assumes all APs are waken up concurrently and NumApsExecuting only drops to zero when all APs have checked in. Then it additionally waits for FinishedCount =3D=3D CpuCount - 1. (FinishedCount doesn't include BSP while CpuCount includes BSP.) There is no need to additionally wait for FinishedCount =3D=3D CpuCount - 1 because when NumApsExecuting =3D=3D 0, the number of increments of FinishedCount and CpuCount should equal. This patch simplifies the code to remove "CpuCount++" in ApWakeupFunction() and assigns FinishedCount + 1 to CpuCount after WakeUpAP(). Signed-off-by: Ray Ni --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 8b1f7f84ba..2568986d8c 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -1,7 +1,7 @@ /** @file=0D CPU MP Initialize Library common functions.=0D =0D - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
=0D + Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
=0D Copyright (c) 2020, AMD Inc. All rights reserved.
=0D =0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -485,14 +485,12 @@ CollectProcessorCount ( CpuMpData->InitFlag =3D ApInitConfig;=0D WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);=0D CpuMpData->InitFlag =3D ApInitDone;=0D - ASSERT (CpuMpData->CpuCount <=3D PcdGet32 (PcdCpuMaxLogicalProcessorNumb= er));=0D //=0D - // Wait for all APs finished the initialization=0D + // When InitFlag =3D=3D ApInitConfig, WakeUpAP () guarantees all APs are= checked in.=0D + // FinishedCount is the number of check-in APs.=0D //=0D - while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {=0D - CpuPause ();=0D - }=0D -=0D + CpuMpData->CpuCount =3D CpuMpData->FinishedCount + 1;=0D + ASSERT (CpuMpData->CpuCount <=3D PcdGet32 (PcdCpuMaxLogicalProcessorNumb= er));=0D =0D //=0D // Enable x2APIC mode if=0D @@ -751,10 +749,6 @@ ApWakeupFunction ( CurrentApicMode =3D GetApicMode ();=0D while (TRUE) {=0D if (CpuMpData->InitFlag =3D=3D ApInitConfig) {=0D - //=0D - // Add CPU number=0D - //=0D - InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);=0D ProcessorNumber =3D ApIndex;=0D //=0D // This is first time AP wakeup, get BIST information from AP stack= =0D --=20 2.27.0.windows.1