From nobody Sun Feb 8 18:53:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+49649+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49649+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1572429227; cv=none; d=zoho.com; s=zohoarc; b=GDg2z0bVUy6j7Tvq29wsQ/gM4XOEpRRTH7WPd0b/wTQqxbd5//vvrLvNFY4WEGj+plWODZ3d7vu4S/SwL7byQLWznyLCR9JcDRdbJimzKyXszfB5s1HBr4TTEJW+45cPNVcHmQ8Cj8VAzZ0A1lUctFUTf3O7c7nfQfbiTVrWs08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1572429227; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=Iy/LVti/1W5Xz/LbOc50OJBGKrnaDnvb8+6mR+r7M80=; b=jeLgpguQ3kmeMnZ4CG4C76kSpogPKwyGWGUxj+VRZVkmhx9y01pAeBmAoGRv8i+74jSHUd4JjT2a6+hrD36V4LXvfn6vLHu3lh5Awr5KZwDWiFHcklFMuwIyXZqBDKUm6LD1y033oh/FgyrdRwIXu0VfgxjsWghMI4xicNBqlS8= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49649+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1572429227892471.24994960907793; Wed, 30 Oct 2019 02:53:47 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id D8irYY1788612x3zXdyRCcNZ; Wed, 30 Oct 2019 02:53:47 -0700 X-Received: from mga11.intel.com (mga11.intel.com []) by mx.groups.io with SMTP id smtpd.web10.2357.1572429225602982517 for ; Wed, 30 Oct 2019 02:53:46 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Oct 2019 02:53:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,247,1569308400"; d="scan'208";a="230389670" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.9.9]) by fmsmga002.fm.intel.com with ESMTP; 30 Oct 2019 02:53:45 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Eric Dong , Laszlo Ersek Subject: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Set X2ApicEnable flag from BSP Date: Wed, 30 Oct 2019 17:52:32 +0800 Message-Id: <20191030095233.565420-2-ray.ni@intel.com> In-Reply-To: <20191030095233.565420-1-ray.ni@intel.com> References: <20191030095233.565420-1-ray.ni@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ray.ni@intel.com X-Gm-Message-State: yq5ik5cFCuQIrfHX7KLaUiv9x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1572429227; bh=50JklO0xx9KxhqlvdyYKnThmGDu/bEZKHNQ85I8B9z8=; h=Cc:Date:From:Reply-To:Subject:To; b=t6PCQuc7BwTQcrZ/Hk/8CuEUuNljOrFoU0tiNgAooVKCQ7ntYOaS8P9bhAmES8JBduj KKEq6+VrzSBP/hjcExQa0ZDVMsj8ekBr6lEdWzHH8iGhmsfVIJll1jHsarDqANVmzCHqj pLtrkJM8++mw5zvxKW+VnJs3czMBl5raGVQ= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" Today's logic sets X2ApicEnable flag in each AP's initialization path when InitFlag =3D=3D ApInitConfig. Since all CPUs update the same global data, a spin-lock is used to avoid modifications from multiple CPUs happen at the same time. The spin-lock causes two problems: 1. Potential performance downgrade. 2. Undefined behavior when improper timer lib is used. For example we saw certain platforms used AcpiTimerLib from PcAtChipsetPkg and that library depends on retrieving PeiServices from idtr. But in fact AP's (idtr - 4) doesn't point to PeiServices. The patch simplifies the code to let BSP set the X2ApicEnable flag so the spin-lock acquisition from AP is not needed any more. Signed-off-by: Ray Ni Cc: Eric Dong Cc: Laszlo Ersek Acked-by: Laszlo Ersek Reviewed-by: Eric Dong --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 622b70ca3c..8f62a8d965 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -458,6 +458,7 @@ CollectProcessorCount ( ) { UINTN Index; + CPU_INFO_IN_HOB *CpuInfoInHob; =20 // // Send 1st broadcast IPI to APs to wakeup APs @@ -474,12 +475,27 @@ CollectProcessorCount ( CpuPause (); } =20 + =20 + // + // Enable x2APIC mode if + // 1. Number of CPU is greater than 255; or + // 2. There are any logical processors reporting an Initial APIC ID of = 255 or greater. + // if (CpuMpData->CpuCount > 255) { // // If there are more than 255 processor found, force to enable X2APIC // CpuMpData->X2ApicEnable =3D TRUE; + } else { + CpuInfoInHob =3D (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + for (Index =3D 0; Index < CpuMpData->CpuCount; Index++) { + if (CpuInfoInHob[Index].InitialApicId >=3D 0xFF) { + CpuMpData->X2ApicEnable =3D TRUE; + break; + } + } } + if (CpuMpData->X2ApicEnable) { DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n")); // @@ -541,15 +557,6 @@ InitializeApData ( =20 CpuMpData->CpuData[ProcessorNumber].Waiting =3D FALSE; CpuMpData->CpuData[ProcessorNumber].CpuHealthy =3D (BistData =3D=3D 0) ?= TRUE : FALSE; - if (CpuInfoInHob[ProcessorNumber].InitialApicId >=3D 0xFF) { - // - // Set x2APIC mode if there are any logical processor reporting - // an Initial APIC ID of 255 or greater. - // - AcquireSpinLock(&CpuMpData->MpLock); - CpuMpData->X2ApicEnable =3D TRUE; - ReleaseSpinLock(&CpuMpData->MpLock); - } =20 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock); SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle); --=20 2.21.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#49649): https://edk2.groups.io/g/devel/message/49649 Mute This Topic: https://groups.io/mt/39769103/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sun Feb 8 18:53:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+49650+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49650+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1572429228; cv=none; d=zoho.com; s=zohoarc; b=IInYWALQ23+Hk3LrS6ewGI22vJiuiQboIm68O74esUEbtZYt/ns/WE0cq9Z/kgKE5Gt896A2mYC4/JvtDJ5rrImTpY0vBCd5/LDG3dFQI7+YkddQOPF4n5UneB/2htFngZQzNzptDpCboTTqajvoGXpaGc8GgX2X1djrTJH4zOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1572429228; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=ysqoqP38IOBI/oACcYoTfxUV7qvUmPACKVnC5Eb0pPk=; b=LFBYnDQtU0/73iqmuigrFD+zCwuqqQXUXGAzCRB8vfXIDzdQz5CzlYENsOARHOtGOmWO/ModiKkh7IFFvs1CuW8mdvVPfbZlJUL215fjDTJLEP+Qxw8WdTuZ23Z8a9a4dfS/RIUVqg0DOdnuSHuc/vSFSBZuJYFlQHCCop/j5bg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49650+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1572429228485392.89316222693935; Wed, 30 Oct 2019 02:53:48 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id T9pWYY1788612xe7piBXz3JU; Wed, 30 Oct 2019 02:53:48 -0700 X-Received: from mga11.intel.com (mga11.intel.com []) by mx.groups.io with SMTP id smtpd.web10.2357.1572429225602982517 for ; Wed, 30 Oct 2019 02:53:47 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Oct 2019 02:53:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,247,1569308400"; d="scan'208";a="230389690" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.9.9]) by fmsmga002.fm.intel.com with ESMTP; 30 Oct 2019 02:53:46 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Eric Dong , Laszlo Ersek Subject: [edk2-devel] [PATCH 2/2] UefiCpuPkg/MpInitLib: Remove global variable X2ApicEnable Date: Wed, 30 Oct 2019 17:52:33 +0800 Message-Id: <20191030095233.565420-3-ray.ni@intel.com> In-Reply-To: <20191030095233.565420-1-ray.ni@intel.com> References: <20191030095233.565420-1-ray.ni@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ray.ni@intel.com X-Gm-Message-State: zkQ3V1pzTV1JhpebZgxFLn4kx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1572429228; bh=juR8P9b8ymkrGNfS8QMRMwvC1HiiGfYRM6LIwCqnRFw=; h=Cc:Date:From:Reply-To:Subject:To; b=hVrMQUxyL4yeDjJlYiqlNPSFj8XjBL8XkNl7Dn2GdDnti46RpjECAKzbFhWkO509Sdb VgD+LjOxbXpg4EcLgaUgwlcMy0RsK89Ld4kjcc/L9WIBXB/p988uw65VHOo4mNyJWxPzb 8GMpVYBAungOciwrPiLJjQutcKK7cwE1R5I= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" MpInitLib sets X2ApicEnable in two places. 1. CollectProcessorCount() This function is called when MpInitLibInitialize() hasn't been called before. It sets X2ApicEnable and later in the same function it configures all CPUs to operate in X2 APIC mode. 2. MpInitLibInitialize() The X2ApicEnable setting happens when this function is called in second time. But after that setting, no code consumes that flag. With the above analysis and with the purpose of simplifying the code, the X2ApicEnable in #1 is changed to local variable and the #2 can be changed to remove the setting of X2ApicEnable. Signed-off-by: Ray Ni Cc: Eric Dong Cc: Laszlo Ersek Reviewed-by: Eric Dong Reviewed-by: Laszlo Ersek --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 14 ++++++-------- UefiCpuPkg/Library/MpInitLib/MpLib.h | 1 - 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 8f62a8d965..49be5d5385 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -459,12 +459,12 @@ CollectProcessorCount ( { UINTN Index; CPU_INFO_IN_HOB *CpuInfoInHob; + BOOLEAN X2Apic; =20 // // Send 1st broadcast IPI to APs to wakeup APs // - CpuMpData->InitFlag =3D ApInitConfig; - CpuMpData->X2ApicEnable =3D FALSE; + CpuMpData->InitFlag =3D ApInitConfig; WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE); CpuMpData->InitFlag =3D ApInitDone; ASSERT (CpuMpData->CpuCount <=3D PcdGet32 (PcdCpuMaxLogicalProcessorNumb= er)); @@ -481,22 +481,23 @@ CollectProcessorCount ( // 1. Number of CPU is greater than 255; or // 2. There are any logical processors reporting an Initial APIC ID of = 255 or greater. // + X2Apic =3D FALSE; if (CpuMpData->CpuCount > 255) { // // If there are more than 255 processor found, force to enable X2APIC // - CpuMpData->X2ApicEnable =3D TRUE; + X2Apic =3D TRUE; } else { CpuInfoInHob =3D (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; for (Index =3D 0; Index < CpuMpData->CpuCount; Index++) { if (CpuInfoInHob[Index].InitialApicId >=3D 0xFF) { - CpuMpData->X2ApicEnable =3D TRUE; + X2Apic =3D TRUE; break; } } } =20 - if (CpuMpData->X2ApicEnable) { + if (X2Apic) { DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n")); // // Wakeup all APs to enable x2APIC mode @@ -1780,9 +1781,6 @@ MpInitLibInitialize ( CpuInfoInHob =3D (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; for (Index =3D 0; Index < CpuMpData->CpuCount; Index++) { InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock); - if (CpuInfoInHob[Index].InitialApicId >=3D 255 || Index > 254) { - CpuMpData->X2ApicEnable =3D TRUE; - } CpuMpData->CpuData[Index].CpuHealthy =3D (CpuInfoInHob[Index].Health= =3D=3D 0)? TRUE:FALSE; CpuMpData->CpuData[Index].ApFunction =3D 0; CopyMem (&CpuMpData->CpuData[Index].VolatileRegisters, &VolatileRegi= sters, sizeof (CPU_VOLATILE_REGISTERS)); diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index 107872b367..8fa07b12c5 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -227,7 +227,6 @@ struct _CPU_MP_DATA { UINTN **FailedCpuList; =20 AP_INIT_STATE InitFlag; - BOOLEAN X2ApicEnable; BOOLEAN SwitchBspFlag; UINTN NewBspNumber; CPU_EXCHANGE_ROLE_INFO BSPInfo; --=20 2.21.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#49650): https://edk2.groups.io/g/devel/message/49650 Mute This Topic: https://groups.io/mt/39769104/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-