[PATCH v3] UefiCpuPkg/CpuCacheInfoLib: Sort CpuCacheInfo array


Jason Lou
 

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3508

Sort the CpuCacheInfo array by CPU package ID, core type, cache level
and cache type.

Signed-off-by: Jason Lou <yun.lou@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
---
UefiCpuPkg/Library/CpuCacheInfoLib/CpuCacheInfoLib.c | 47 ++++++++=
+++++++++++-
UefiCpuPkg/Include/Library/CpuCacheInfoLib.h | 2 +-
UefiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf | 4 +-
UefiCpuPkg/Library/CpuCacheInfoLib/InternalCpuCacheInfoLib.h | 27 ++++++++=
+++
UefiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf | 4 +-
5 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuCacheInfoLib/CpuCacheInfoLib.c b/UefiCpu=
Pkg/Library/CpuCacheInfoLib/CpuCacheInfoLib.c
index 126ee0da86..ae81ea9ce2 100644
--- a/UefiCpuPkg/Library/CpuCacheInfoLib/CpuCacheInfoLib.c
+++ b/UefiCpuPkg/Library/CpuCacheInfoLib/CpuCacheInfoLib.c
@@ -37,6 +37,47 @@ CpuCacheInfoPrintCpuCacheInfoTable (
DEBUG ((DEBUG_INFO, "+-------+------------------------------------------=
--------------------------------------------+\n"));=0D
}=0D
=0D
+/**=0D
+ Function to compare CPU package ID, core type, cache level and cache typ=
e for use in QuickSort.=0D
+=0D
+ @param[in] Buffer1 pointer to CPU_CACHE_INFO poiner to comp=
are=0D
+ @param[in] Buffer2 pointer to second CPU_CACHE_INFO pointer=
to compare=0D
+=0D
+ @retval 0 Buffer1 equal to Buffer2=0D
+ @retval 1 Buffer1 is greater than Buffer2=0D
+ @retval -1 Buffer1 is less than Buffer2=0D
+**/=0D
+INTN=0D
+EFIAPI=0D
+CpuCacheInfoCompare (=0D
+ IN CONST VOID *Buffer1,=0D
+ IN CONST VOID *Buffer2=0D
+ )=0D
+{=0D
+ CPU_CACHE_INFO_COMPARATOR Comparator1, Comparator2;=0D
+=0D
+ ZeroMem (&Comparator1, sizeof (Comparator1));=0D
+ ZeroMem (&Comparator2, sizeof (Comparator2));=0D
+=0D
+ Comparator1.Bits.Package =3D ((CPU_CACHE_INFO*)Buffer1)->Package;=0D
+ Comparator1.Bits.CoreType =3D ((CPU_CACHE_INFO*)Buffer1)->CoreType;=0D
+ Comparator1.Bits.CacheLevel =3D ((CPU_CACHE_INFO*)Buffer1)->CacheLevel;=
=0D
+ Comparator1.Bits.CacheType =3D ((CPU_CACHE_INFO*)Buffer1)->CacheType;=0D
+=0D
+ Comparator2.Bits.Package =3D ((CPU_CACHE_INFO*)Buffer2)->Package;=0D
+ Comparator2.Bits.CoreType =3D ((CPU_CACHE_INFO*)Buffer2)->CoreType;=0D
+ Comparator2.Bits.CacheLevel =3D ((CPU_CACHE_INFO*)Buffer2)->CacheLevel;=
=0D
+ Comparator2.Bits.CacheType =3D ((CPU_CACHE_INFO*)Buffer2)->CacheType;=0D
+=0D
+ if (Comparator1.Uint64 =3D=3D Comparator2.Uint64) {=0D
+ return 0;=0D
+ } else if (Comparator1.Uint64 > Comparator2.Uint64) {=0D
+ return 1;=0D
+ } else {=0D
+ return -1;=0D
+ }=0D
+}=0D
+=0D
/**=0D
Get the total number of package and package ID in the platform.=0D
=0D
@@ -325,6 +366,10 @@ CpuCacheInfoCollectCpuCacheInfoData (
if (*CacheInfoCount < LocalCacheInfoCount) {=0D
Status =3D EFI_BUFFER_TOO_SMALL;=0D
} else {=0D
+ //=0D
+ // Sort LocalCacheInfo array by CPU package ID, core type, cache level=
and cache type.=0D
+ //=0D
+ PerformQuickSort (LocalCacheInfo, LocalCacheInfoCount, sizeof (*LocalC=
acheInfo), (SORT_COMPARE) CpuCacheInfoCompare);=0D
CopyMem (CacheInfo, LocalCacheInfo, sizeof (*CacheInfo) * LocalCacheIn=
foCount);=0D
DEBUG_CODE (=0D
CpuCacheInfoPrintCpuCacheInfoTable (CacheInfo, LocalCacheInfoCount);=
=0D
@@ -340,7 +385,7 @@ CpuCacheInfoCollectCpuCacheInfoData (
}=0D
=0D
/**=0D
- Get CpuCacheInfo data array.=0D
+ Get CpuCacheInfo data array. The array is sorted by CPU package ID, core=
type, cache level and cache type.=0D
=0D
@param[in, out] CpuCacheInfo Pointer to the CpuCacheInfo array.=0D
@param[in, out] CpuCacheInfoCount As input, point to the length of res=
ponse CpuCacheInfo array.=0D
diff --git a/UefiCpuPkg/Include/Library/CpuCacheInfoLib.h b/UefiCpuPkg/Incl=
ude/Library/CpuCacheInfoLib.h
index a66152bce0..3422997f54 100644
--- a/UefiCpuPkg/Include/Library/CpuCacheInfoLib.h
+++ b/UefiCpuPkg/Include/Library/CpuCacheInfoLib.h
@@ -59,7 +59,7 @@ typedef struct {
} CPU_CACHE_INFO;=0D
=0D
/**=0D
- Get CpuCacheInfo data array.=0D
+ Get CpuCacheInfo data array. The array is sorted by CPU package ID, core=
type, cache level and cache type.=0D
=0D
@param[in, out] CpuCacheInfo Pointer to the CpuCacheInfo array.=0D
@param[in, out] CpuCacheInfoCount As input, point to the length of res=
ponse CpuCacheInfo array.=0D
diff --git a/UefiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf b/Ue=
fiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf
index c481080e49..c3d3f1e799 100644
--- a/UefiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf
+++ b/UefiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf
@@ -3,7 +3,7 @@
#=0D
# Provides cache info for each package, core type, cache level and cache =
type.=0D
#=0D
-# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>=0D
+# Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>=
=0D
#=0D
# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
#=0D
@@ -25,6 +25,7 @@
=0D
[Packages]=0D
MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
UefiCpuPkg/UefiCpuPkg.dec=0D
=0D
[LibraryClasses]=0D
@@ -33,6 +34,7 @@
BaseMemoryLib=0D
MemoryAllocationLib=0D
UefiBootServicesTableLib=0D
+ SortLib=0D
=0D
[Protocols]=0D
gEfiMpServiceProtocolGuid=0D
diff --git a/UefiCpuPkg/Library/CpuCacheInfoLib/InternalCpuCacheInfoLib.h b=
/UefiCpuPkg/Library/CpuCacheInfoLib/InternalCpuCacheInfoLib.h
index b6e6ae5bc5..26e1f46516 100644
--- a/UefiCpuPkg/Library/CpuCacheInfoLib/InternalCpuCacheInfoLib.h
+++ b/UefiCpuPkg/Library/CpuCacheInfoLib/InternalCpuCacheInfoLib.h
@@ -17,8 +17,35 @@
#include <Library/DebugLib.h>=0D
#include <Library/BaseMemoryLib.h>=0D
#include <Library/MemoryAllocationLib.h>=0D
+#include <Library/SortLib.h>=0D
#include <Library/CpuCacheInfoLib.h>=0D
=0D
+typedef union {=0D
+ struct {=0D
+ //=0D
+ // Type of the cache that this package's this type of logical processo=
r corresponds to.=0D
+ // Value =3D CPUID.04h:EAX[04:00]=0D
+ //=0D
+ UINT32 CacheType : 5;=0D
+ //=0D
+ // Level of the cache that this package's this type of logical process=
or corresponds to.=0D
+ // Value =3D CPUID.04h:EAX[07:05]=0D
+ //=0D
+ UINT32 CacheLevel : 3;=0D
+ //=0D
+ // Core type of logical processor.=0D
+ // Value =3D CPUID.1Ah:EAX[31:24]=0D
+ //=0D
+ UINT32 CoreType : 8;=0D
+ UINT32 Reserved : 16;=0D
+ //=0D
+ // Package number.=0D
+ //=0D
+ UINT32 Package;=0D
+ } Bits;=0D
+ UINT64 Uint64;=0D
+} CPU_CACHE_INFO_COMPARATOR;=0D
+=0D
typedef struct {=0D
//=0D
// Package ID, the information comes from=0D
diff --git a/UefiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf b/Ue=
fiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf
index 0c73015cac..0864497849 100644
--- a/UefiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf
+++ b/UefiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf
@@ -3,7 +3,7 @@
#=0D
# Provides cache info for each package, core type, cache level and cache =
type.=0D
#=0D
-# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>=0D
+# Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>=
=0D
#=0D
# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
#=0D
@@ -25,6 +25,7 @@
=0D
[Packages]=0D
MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
UefiCpuPkg/UefiCpuPkg.dec=0D
=0D
[LibraryClasses]=0D
@@ -33,6 +34,7 @@
BaseMemoryLib=0D
MemoryAllocationLib=0D
PeiServicesTablePointerLib=0D
+ SortLib=0D
=0D
[Ppis]=0D
gEdkiiPeiMpServices2PpiGuid=0D
--=20
2.28.0.windows.1

Join devel@edk2.groups.io to automatically receive all group messages.