1 | /** @file
|
---|
2 | Provides cache info for each package, core type, cache level and cache type.
|
---|
3 |
|
---|
4 | Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include "InternalCpuCacheInfoLib.h"
|
---|
10 |
|
---|
11 | /**
|
---|
12 | Print CpuCacheInfo array.
|
---|
13 |
|
---|
14 | @param[in] CpuCacheInfo Pointer to the CpuCacheInfo array.
|
---|
15 | @param[in] CpuCacheInfoCount The length of CpuCacheInfo array.
|
---|
16 |
|
---|
17 | **/
|
---|
18 | VOID
|
---|
19 | CpuCacheInfoPrintCpuCacheInfoTable (
|
---|
20 | IN CPU_CACHE_INFO *CpuCacheInfo,
|
---|
21 | IN UINTN CpuCacheInfoCount
|
---|
22 | )
|
---|
23 | {
|
---|
24 | UINTN Index;
|
---|
25 |
|
---|
26 | DEBUG ((DEBUG_INFO, "+-------+--------------------------------------------------------------------------------------+\n"));
|
---|
27 | DEBUG ((DEBUG_INFO, "| Index | Packge CoreType CacheLevel CacheType CacheWays (FA|DM) CacheSizeinKB CacheCount |\n"));
|
---|
28 | DEBUG ((DEBUG_INFO, "+-------+--------------------------------------------------------------------------------------+\n"));
|
---|
29 |
|
---|
30 | for (Index = 0; Index < CpuCacheInfoCount; Index++) {
|
---|
31 | DEBUG ((
|
---|
32 | DEBUG_INFO,
|
---|
33 | "| %4x | %4x %2x %2x %2x %4x ( %x| %x) %8x %4x |\n",
|
---|
34 | Index,
|
---|
35 | CpuCacheInfo[Index].Package,
|
---|
36 | CpuCacheInfo[Index].CoreType,
|
---|
37 | CpuCacheInfo[Index].CacheLevel,
|
---|
38 | CpuCacheInfo[Index].CacheType,
|
---|
39 | CpuCacheInfo[Index].CacheWays,
|
---|
40 | CpuCacheInfo[Index].FullyAssociativeCache,
|
---|
41 | CpuCacheInfo[Index].DirectMappedCache,
|
---|
42 | CpuCacheInfo[Index].CacheSizeinKB,
|
---|
43 | CpuCacheInfo[Index].CacheCount
|
---|
44 | ));
|
---|
45 | }
|
---|
46 |
|
---|
47 | DEBUG ((DEBUG_INFO, "+-------+--------------------------------------------------------------------------------------+\n"));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | Function to compare CPU package ID, core type, cache level and cache type for use in QuickSort.
|
---|
52 |
|
---|
53 | @param[in] Buffer1 pointer to CPU_CACHE_INFO poiner to compare
|
---|
54 | @param[in] Buffer2 pointer to second CPU_CACHE_INFO pointer to compare
|
---|
55 |
|
---|
56 | @retval 0 Buffer1 equal to Buffer2
|
---|
57 | @retval 1 Buffer1 is greater than Buffer2
|
---|
58 | @retval -1 Buffer1 is less than Buffer2
|
---|
59 | **/
|
---|
60 | INTN
|
---|
61 | EFIAPI
|
---|
62 | CpuCacheInfoCompare (
|
---|
63 | IN CONST VOID *Buffer1,
|
---|
64 | IN CONST VOID *Buffer2
|
---|
65 | )
|
---|
66 | {
|
---|
67 | CPU_CACHE_INFO_COMPARATOR Comparator1, Comparator2;
|
---|
68 |
|
---|
69 | ZeroMem (&Comparator1, sizeof (Comparator1));
|
---|
70 | ZeroMem (&Comparator2, sizeof (Comparator2));
|
---|
71 |
|
---|
72 | Comparator1.Bits.Package = ((CPU_CACHE_INFO *)Buffer1)->Package;
|
---|
73 | Comparator1.Bits.CoreType = ((CPU_CACHE_INFO *)Buffer1)->CoreType;
|
---|
74 | Comparator1.Bits.CacheLevel = ((CPU_CACHE_INFO *)Buffer1)->CacheLevel;
|
---|
75 | Comparator1.Bits.CacheType = ((CPU_CACHE_INFO *)Buffer1)->CacheType;
|
---|
76 |
|
---|
77 | Comparator2.Bits.Package = ((CPU_CACHE_INFO *)Buffer2)->Package;
|
---|
78 | Comparator2.Bits.CoreType = ((CPU_CACHE_INFO *)Buffer2)->CoreType;
|
---|
79 | Comparator2.Bits.CacheLevel = ((CPU_CACHE_INFO *)Buffer2)->CacheLevel;
|
---|
80 | Comparator2.Bits.CacheType = ((CPU_CACHE_INFO *)Buffer2)->CacheType;
|
---|
81 |
|
---|
82 | if (Comparator1.Uint64 == Comparator2.Uint64) {
|
---|
83 | return 0;
|
---|
84 | } else if (Comparator1.Uint64 > Comparator2.Uint64) {
|
---|
85 | return 1;
|
---|
86 | } else {
|
---|
87 | return -1;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | Get the total number of package and package ID in the platform.
|
---|
93 |
|
---|
94 | @param[in] ProcessorInfo Pointer to the ProcessorInfo array.
|
---|
95 | @param[in] NumberOfProcessors Total number of logical processors in the platform.
|
---|
96 | @param[in, out] Package Pointer to the Package array.
|
---|
97 |
|
---|
98 | @retval Return the total number of package and package ID in the platform.
|
---|
99 | **/
|
---|
100 | UINT32
|
---|
101 | CpuCacheInfoGetNumberOfPackages (
|
---|
102 | IN CPUID_PROCESSOR_INFO *ProcessorInfo,
|
---|
103 | IN UINTN NumberOfProcessors,
|
---|
104 | IN OUT UINT32 *Package
|
---|
105 | )
|
---|
106 | {
|
---|
107 | UINTN ProcessorIndex;
|
---|
108 | UINT32 PackageIndex;
|
---|
109 | UINT32 PackageCount;
|
---|
110 | UINT32 CurrentPackage;
|
---|
111 |
|
---|
112 | PackageCount = 0;
|
---|
113 |
|
---|
114 | for (ProcessorIndex = 0; ProcessorIndex < NumberOfProcessors; ProcessorIndex++) {
|
---|
115 | CurrentPackage = ProcessorInfo[ProcessorIndex].Package;
|
---|
116 |
|
---|
117 | //
|
---|
118 | // For the package that already exists in Package array, break out the loop.
|
---|
119 | //
|
---|
120 | for (PackageIndex = 0; PackageIndex < PackageCount; PackageIndex++) {
|
---|
121 | if (CurrentPackage == Package[PackageIndex]) {
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // For the new package, save it in Package array.
|
---|
128 | //
|
---|
129 | if (PackageIndex == PackageCount) {
|
---|
130 | ASSERT (PackageCount < MAX_NUM_OF_PACKAGE);
|
---|
131 | Package[PackageCount++] = CurrentPackage;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return PackageCount;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | Get the number of CoreType of requested package.
|
---|
140 |
|
---|
141 | @param[in] ProcessorInfo Pointer to the ProcessorInfo array.
|
---|
142 | @param[in] NumberOfProcessors Total number of logical processors in the platform.
|
---|
143 | @param[in] Package The requested package number.
|
---|
144 |
|
---|
145 | @retval Return the number of CoreType of requested package.
|
---|
146 | **/
|
---|
147 | UINTN
|
---|
148 | CpuCacheInfoGetNumberOfCoreTypePerPackage (
|
---|
149 | IN CPUID_PROCESSOR_INFO *ProcessorInfo,
|
---|
150 | IN UINTN NumberOfProcessors,
|
---|
151 | IN UINTN Package
|
---|
152 | )
|
---|
153 | {
|
---|
154 | UINTN ProcessorIndex;
|
---|
155 | //
|
---|
156 | // Core Type value comes from CPUID.1Ah.EAX[31:24].
|
---|
157 | // So max number of core types should be MAX_UINT8.
|
---|
158 | //
|
---|
159 | UINT8 CoreType[MAX_UINT8];
|
---|
160 | UINTN CoreTypeIndex;
|
---|
161 | UINTN CoreTypeCount;
|
---|
162 | UINT8 CurrentCoreType;
|
---|
163 |
|
---|
164 | //
|
---|
165 | // CoreType array is empty.
|
---|
166 | //
|
---|
167 | CoreTypeCount = 0;
|
---|
168 |
|
---|
169 | for (ProcessorIndex = 0; ProcessorIndex < NumberOfProcessors; ProcessorIndex++) {
|
---|
170 | CurrentCoreType = ProcessorInfo[ProcessorIndex].CoreType;
|
---|
171 |
|
---|
172 | if (ProcessorInfo[ProcessorIndex].Package != Package) {
|
---|
173 | continue;
|
---|
174 | }
|
---|
175 |
|
---|
176 | //
|
---|
177 | // For the type that already exists in CoreType array, break out the loop.
|
---|
178 | //
|
---|
179 | for (CoreTypeIndex = 0; CoreTypeIndex < CoreTypeCount; CoreTypeIndex++) {
|
---|
180 | if (CurrentCoreType == CoreType[CoreTypeIndex]) {
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | //
|
---|
186 | // For the new type, save it in CoreType array.
|
---|
187 | //
|
---|
188 | if (CoreTypeIndex == CoreTypeCount) {
|
---|
189 | ASSERT (CoreTypeCount < MAX_UINT8);
|
---|
190 | CoreType[CoreTypeCount++] = CurrentCoreType;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | return CoreTypeCount;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | Collect core and cache information of calling processor via CPUID instructions.
|
---|
199 |
|
---|
200 | @param[in, out] Buffer The pointer to private data buffer.
|
---|
201 | **/
|
---|
202 | VOID
|
---|
203 | EFIAPI
|
---|
204 | CpuCacheInfoCollectCoreAndCacheData (
|
---|
205 | IN OUT VOID *Buffer
|
---|
206 | )
|
---|
207 | {
|
---|
208 | UINTN ProcessorIndex;
|
---|
209 | UINT32 CpuidMaxInput;
|
---|
210 | UINT8 CacheParamLeafIndex;
|
---|
211 | CPUID_CACHE_PARAMS_EAX CacheParamEax;
|
---|
212 | CPUID_CACHE_PARAMS_EBX CacheParamEbx;
|
---|
213 | UINT32 CacheParamEcx;
|
---|
214 | CPUID_CACHE_PARAMS_EDX CacheParamEdx;
|
---|
215 | CPUID_NATIVE_MODEL_ID_AND_CORE_TYPE_EAX NativeModelIdAndCoreTypeEax;
|
---|
216 | COLLECT_CPUID_CACHE_DATA_CONTEXT *Context;
|
---|
217 | CPUID_CACHE_DATA *CacheData;
|
---|
218 |
|
---|
219 | Context = (COLLECT_CPUID_CACHE_DATA_CONTEXT *)Buffer;
|
---|
220 | ProcessorIndex = CpuCacheInfoWhoAmI (Context->MpServices);
|
---|
221 | CacheData = &Context->CacheData[MAX_NUM_OF_CACHE_PARAMS_LEAF * ProcessorIndex];
|
---|
222 |
|
---|
223 | AsmCpuid (CPUID_SIGNATURE, &CpuidMaxInput, NULL, NULL, NULL);
|
---|
224 |
|
---|
225 | //
|
---|
226 | // get CoreType if CPUID_HYBRID_INFORMATION leaf is supported.
|
---|
227 | //
|
---|
228 | Context->ProcessorInfo[ProcessorIndex].CoreType = 0;
|
---|
229 | if (CpuidMaxInput >= CPUID_HYBRID_INFORMATION) {
|
---|
230 | AsmCpuidEx (CPUID_HYBRID_INFORMATION, CPUID_HYBRID_INFORMATION_MAIN_LEAF, &NativeModelIdAndCoreTypeEax.Uint32, NULL, NULL, NULL);
|
---|
231 | Context->ProcessorInfo[ProcessorIndex].CoreType = (UINT8)NativeModelIdAndCoreTypeEax.Bits.CoreType;
|
---|
232 | }
|
---|
233 |
|
---|
234 | //
|
---|
235 | // cache hierarchy starts with an index value of 0.
|
---|
236 | //
|
---|
237 | CacheParamLeafIndex = 0;
|
---|
238 |
|
---|
239 | while (CacheParamLeafIndex < MAX_NUM_OF_CACHE_PARAMS_LEAF) {
|
---|
240 | AsmCpuidEx (CPUID_CACHE_PARAMS, CacheParamLeafIndex, &CacheParamEax.Uint32, &CacheParamEbx.Uint32, &CacheParamEcx, &CacheParamEdx.Uint32);
|
---|
241 |
|
---|
242 | if (CacheParamEax.Bits.CacheType == 0) {
|
---|
243 | break;
|
---|
244 | }
|
---|
245 |
|
---|
246 | CacheData[CacheParamLeafIndex].CacheLevel = (UINT8)CacheParamEax.Bits.CacheLevel;
|
---|
247 | CacheData[CacheParamLeafIndex].CacheType = (UINT8)CacheParamEax.Bits.CacheType;
|
---|
248 | CacheData[CacheParamLeafIndex].CacheWays = (UINT16)CacheParamEbx.Bits.Ways;
|
---|
249 | CacheData[CacheParamLeafIndex].FullyAssociativeCache = (UINT8)CacheParamEax.Bits.FullyAssociativeCache;
|
---|
250 | CacheData[CacheParamLeafIndex].DirectMappedCache = (UINT8)(CacheParamEdx.Bits.ComplexCacheIndexing == 0);
|
---|
251 | CacheData[CacheParamLeafIndex].CacheShareBits = (UINT16)CacheParamEax.Bits.MaximumAddressableIdsForLogicalProcessors;
|
---|
252 | CacheData[CacheParamLeafIndex].CacheSizeinKB = (CacheParamEbx.Bits.Ways + 1) *
|
---|
253 | (CacheParamEbx.Bits.LinePartitions + 1) * (CacheParamEbx.Bits.LineSize + 1) * (CacheParamEcx + 1) / SIZE_1KB;
|
---|
254 |
|
---|
255 | CacheParamLeafIndex++;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | Collect CacheInfo data from the CacheData.
|
---|
261 |
|
---|
262 | @param[in] CacheData Pointer to the CacheData array.
|
---|
263 | @param[in] ProcessorInfo Pointer to the ProcessorInfo array.
|
---|
264 | @param[in] NumberOfProcessors Total number of logical processors in the platform.
|
---|
265 | @param[in, out] CacheInfo Pointer to the CacheInfo array.
|
---|
266 | @param[in, out] CacheInfoCount As input, point to the length of response CacheInfo array.
|
---|
267 | As output, point to the actual length of response CacheInfo array.
|
---|
268 |
|
---|
269 | @retval EFI_SUCCESS Function completed successfully.
|
---|
270 | @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
|
---|
271 | @retval EFI_BUFFER_TOO_SMALL CacheInfoCount is too small to hold the response CacheInfo
|
---|
272 | array. CacheInfoCount has been updated with the length needed
|
---|
273 | to complete the request.
|
---|
274 | **/
|
---|
275 | EFI_STATUS
|
---|
276 | CpuCacheInfoCollectCpuCacheInfoData (
|
---|
277 | IN CPUID_CACHE_DATA *CacheData,
|
---|
278 | IN CPUID_PROCESSOR_INFO *ProcessorInfo,
|
---|
279 | IN UINTN NumberOfProcessors,
|
---|
280 | IN OUT CPU_CACHE_INFO *CacheInfo,
|
---|
281 | IN OUT UINTN *CacheInfoCount
|
---|
282 | )
|
---|
283 | {
|
---|
284 | EFI_STATUS Status;
|
---|
285 | UINT32 NumberOfPackage;
|
---|
286 | UINT32 Package[MAX_NUM_OF_PACKAGE];
|
---|
287 | UINTN PackageIndex;
|
---|
288 | UINTN TotalNumberOfCoreType;
|
---|
289 | UINTN MaxCacheInfoCount;
|
---|
290 | CPU_CACHE_INFO *LocalCacheInfo;
|
---|
291 | UINTN CacheInfoIndex;
|
---|
292 | UINTN LocalCacheInfoCount;
|
---|
293 | UINTN Index;
|
---|
294 | UINTN NextIndex;
|
---|
295 | CPU_CACHE_INFO SortBuffer;
|
---|
296 |
|
---|
297 | //
|
---|
298 | // Get number of Packages and Package ID.
|
---|
299 | //
|
---|
300 | NumberOfPackage = CpuCacheInfoGetNumberOfPackages (ProcessorInfo, NumberOfProcessors, Package);
|
---|
301 |
|
---|
302 | //
|
---|
303 | // Get number of core types for each package and count the total number.
|
---|
304 | // E.g. If Package1 and Package2 both have 2 core types, the total number is 4.
|
---|
305 | //
|
---|
306 | TotalNumberOfCoreType = 0;
|
---|
307 | for (PackageIndex = 0; PackageIndex < NumberOfPackage; PackageIndex++) {
|
---|
308 | TotalNumberOfCoreType += CpuCacheInfoGetNumberOfCoreTypePerPackage (ProcessorInfo, NumberOfProcessors, Package[PackageIndex]);
|
---|
309 | }
|
---|
310 |
|
---|
311 | MaxCacheInfoCount = TotalNumberOfCoreType * MAX_NUM_OF_CACHE_PARAMS_LEAF;
|
---|
312 | LocalCacheInfo = AllocatePages (EFI_SIZE_TO_PAGES (MaxCacheInfoCount * sizeof (*LocalCacheInfo)));
|
---|
313 | ASSERT (LocalCacheInfo != NULL);
|
---|
314 | if (LocalCacheInfo == NULL) {
|
---|
315 | return EFI_OUT_OF_RESOURCES;
|
---|
316 | }
|
---|
317 |
|
---|
318 | LocalCacheInfoCount = 0;
|
---|
319 |
|
---|
320 | for (Index = 0; Index < NumberOfProcessors * MAX_NUM_OF_CACHE_PARAMS_LEAF; Index++) {
|
---|
321 | if (CacheData[Index].CacheSizeinKB == 0) {
|
---|
322 | continue;
|
---|
323 | }
|
---|
324 |
|
---|
325 | //
|
---|
326 | // For the sharing caches, clear their CacheSize.
|
---|
327 | //
|
---|
328 | for (NextIndex = Index + 1; NextIndex < NumberOfProcessors * MAX_NUM_OF_CACHE_PARAMS_LEAF; NextIndex++) {
|
---|
329 | if (CacheData[NextIndex].CacheSizeinKB == 0) {
|
---|
330 | continue;
|
---|
331 | }
|
---|
332 |
|
---|
333 | if ((CacheData[Index].CacheLevel == CacheData[NextIndex].CacheLevel) &&
|
---|
334 | (CacheData[Index].CacheType == CacheData[NextIndex].CacheType) &&
|
---|
335 | (ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].Package == ProcessorInfo[NextIndex / MAX_NUM_OF_CACHE_PARAMS_LEAF].Package) &&
|
---|
336 | (ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].CoreType == ProcessorInfo[NextIndex / MAX_NUM_OF_CACHE_PARAMS_LEAF].CoreType) &&
|
---|
337 | ((ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].ApicId & ~CacheData[Index].CacheShareBits) ==
|
---|
338 | (ProcessorInfo[NextIndex / MAX_NUM_OF_CACHE_PARAMS_LEAF].ApicId & ~CacheData[NextIndex].CacheShareBits)))
|
---|
339 | {
|
---|
340 | CacheData[NextIndex].CacheSizeinKB = 0; // uses the sharing cache
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | //
|
---|
345 | // For the cache that already exists in LocalCacheInfo, increase its CacheCount.
|
---|
346 | //
|
---|
347 | for (CacheInfoIndex = 0; CacheInfoIndex < LocalCacheInfoCount; CacheInfoIndex++) {
|
---|
348 | if ((LocalCacheInfo[CacheInfoIndex].Package == ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].Package) &&
|
---|
349 | (LocalCacheInfo[CacheInfoIndex].CoreType == ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].CoreType) &&
|
---|
350 | (LocalCacheInfo[CacheInfoIndex].CacheLevel == CacheData[Index].CacheLevel) &&
|
---|
351 | (LocalCacheInfo[CacheInfoIndex].CacheType == CacheData[Index].CacheType) &&
|
---|
352 | (LocalCacheInfo[CacheInfoIndex].CacheSizeinKB == CacheData[Index].CacheSizeinKB))
|
---|
353 | {
|
---|
354 | LocalCacheInfo[CacheInfoIndex].CacheCount++;
|
---|
355 | break;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | //
|
---|
360 | // For the new cache with different Package, CoreType, CacheLevel or CacheType, copy its
|
---|
361 | // data into LocalCacheInfo buffer.
|
---|
362 | //
|
---|
363 | if (CacheInfoIndex == LocalCacheInfoCount) {
|
---|
364 | ASSERT (LocalCacheInfoCount < MaxCacheInfoCount);
|
---|
365 |
|
---|
366 | LocalCacheInfo[LocalCacheInfoCount].Package = ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].Package;
|
---|
367 | LocalCacheInfo[LocalCacheInfoCount].CoreType = ProcessorInfo[Index / MAX_NUM_OF_CACHE_PARAMS_LEAF].CoreType;
|
---|
368 | LocalCacheInfo[LocalCacheInfoCount].CacheLevel = CacheData[Index].CacheLevel;
|
---|
369 | LocalCacheInfo[LocalCacheInfoCount].CacheType = CacheData[Index].CacheType;
|
---|
370 | LocalCacheInfo[LocalCacheInfoCount].CacheWays = CacheData[Index].CacheWays;
|
---|
371 | LocalCacheInfo[LocalCacheInfoCount].FullyAssociativeCache = CacheData[Index].FullyAssociativeCache;
|
---|
372 | LocalCacheInfo[LocalCacheInfoCount].DirectMappedCache = CacheData[Index].DirectMappedCache;
|
---|
373 | LocalCacheInfo[LocalCacheInfoCount].CacheSizeinKB = CacheData[Index].CacheSizeinKB;
|
---|
374 | LocalCacheInfo[LocalCacheInfoCount].CacheCount = 1;
|
---|
375 |
|
---|
376 | LocalCacheInfoCount++;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | if (*CacheInfoCount < LocalCacheInfoCount) {
|
---|
381 | Status = EFI_BUFFER_TOO_SMALL;
|
---|
382 | } else {
|
---|
383 | //
|
---|
384 | // Sort LocalCacheInfo array by CPU package ID, core type, cache level and cache type.
|
---|
385 | //
|
---|
386 | QuickSort (LocalCacheInfo, LocalCacheInfoCount, sizeof (*LocalCacheInfo), CpuCacheInfoCompare, (VOID *)&SortBuffer);
|
---|
387 | CopyMem (CacheInfo, LocalCacheInfo, sizeof (*CacheInfo) * LocalCacheInfoCount);
|
---|
388 | DEBUG_CODE (
|
---|
389 | CpuCacheInfoPrintCpuCacheInfoTable (CacheInfo, LocalCacheInfoCount);
|
---|
390 | );
|
---|
391 | Status = EFI_SUCCESS;
|
---|
392 | }
|
---|
393 |
|
---|
394 | *CacheInfoCount = LocalCacheInfoCount;
|
---|
395 |
|
---|
396 | FreePages (LocalCacheInfo, EFI_SIZE_TO_PAGES (MaxCacheInfoCount * sizeof (*LocalCacheInfo)));
|
---|
397 |
|
---|
398 | return Status;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | Get CpuCacheInfo data array. The array is sorted by CPU package ID, core type, cache level and cache type.
|
---|
403 |
|
---|
404 | @param[in, out] CpuCacheInfo Pointer to the CpuCacheInfo array.
|
---|
405 | @param[in, out] CpuCacheInfoCount As input, point to the length of response CpuCacheInfo array.
|
---|
406 | As output, point to the actual length of response CpuCacheInfo array.
|
---|
407 |
|
---|
408 | @retval EFI_SUCCESS Function completed successfully.
|
---|
409 | @retval EFI_INVALID_PARAMETER CpuCacheInfoCount is NULL.
|
---|
410 | @retval EFI_INVALID_PARAMETER CpuCacheInfo is NULL while CpuCacheInfoCount contains the value
|
---|
411 | greater than zero.
|
---|
412 | @retval EFI_UNSUPPORTED Processor does not support CPUID_CACHE_PARAMS Leaf.
|
---|
413 | @retval EFI_OUT_OF_RESOURCES Required resources could not be allocated.
|
---|
414 | @retval EFI_BUFFER_TOO_SMALL CpuCacheInfoCount is too small to hold the response CpuCacheInfo
|
---|
415 | array. CpuCacheInfoCount has been updated with the length needed
|
---|
416 | to complete the request.
|
---|
417 | **/
|
---|
418 | EFI_STATUS
|
---|
419 | EFIAPI
|
---|
420 | GetCpuCacheInfo (
|
---|
421 | IN OUT CPU_CACHE_INFO *CpuCacheInfo,
|
---|
422 | IN OUT UINTN *CpuCacheInfoCount
|
---|
423 | )
|
---|
424 | {
|
---|
425 | EFI_STATUS Status;
|
---|
426 | UINT32 CpuidMaxInput;
|
---|
427 | UINT32 NumberOfProcessors;
|
---|
428 | UINTN CacheDataCount;
|
---|
429 | UINTN ProcessorIndex;
|
---|
430 | EFI_PROCESSOR_INFORMATION ProcessorInfo;
|
---|
431 | COLLECT_CPUID_CACHE_DATA_CONTEXT Context;
|
---|
432 |
|
---|
433 | if (CpuCacheInfoCount == NULL) {
|
---|
434 | return EFI_INVALID_PARAMETER;
|
---|
435 | }
|
---|
436 |
|
---|
437 | if ((*CpuCacheInfoCount != 0) && (CpuCacheInfo == NULL)) {
|
---|
438 | return EFI_INVALID_PARAMETER;
|
---|
439 | }
|
---|
440 |
|
---|
441 | AsmCpuid (CPUID_SIGNATURE, &CpuidMaxInput, NULL, NULL, NULL);
|
---|
442 | if (CpuidMaxInput < CPUID_CACHE_PARAMS) {
|
---|
443 | return EFI_UNSUPPORTED;
|
---|
444 | }
|
---|
445 |
|
---|
446 | //
|
---|
447 | // Initialize COLLECT_CPUID_CACHE_DATA_CONTEXT.MpServices.
|
---|
448 | //
|
---|
449 | CpuCacheInfoGetMpServices (&Context.MpServices);
|
---|
450 |
|
---|
451 | NumberOfProcessors = CpuCacheInfoGetNumberOfProcessors (Context.MpServices);
|
---|
452 |
|
---|
453 | //
|
---|
454 | // Initialize COLLECT_CPUID_CACHE_DATA_CONTEXT.ProcessorInfo.
|
---|
455 | //
|
---|
456 | Context.ProcessorInfo = AllocatePages (EFI_SIZE_TO_PAGES (NumberOfProcessors * sizeof (*Context.ProcessorInfo)));
|
---|
457 | ASSERT (Context.ProcessorInfo != NULL);
|
---|
458 | if (Context.ProcessorInfo == NULL) {
|
---|
459 | return EFI_OUT_OF_RESOURCES;
|
---|
460 | }
|
---|
461 |
|
---|
462 | //
|
---|
463 | // Initialize COLLECT_CPUID_CACHE_DATA_CONTEXT.CacheData.
|
---|
464 | // CacheData array consists of CPUID_CACHE_DATA data structure for each Cpuid Cache Parameter Leaf
|
---|
465 | // per logical processor. The array begin with data of each Cache Parameter Leaf of processor 0, followed
|
---|
466 | // by data of each Cache Parameter Leaf of processor 1 ...
|
---|
467 | //
|
---|
468 | CacheDataCount = NumberOfProcessors * MAX_NUM_OF_CACHE_PARAMS_LEAF;
|
---|
469 | Context.CacheData = AllocatePages (EFI_SIZE_TO_PAGES (CacheDataCount * sizeof (*Context.CacheData)));
|
---|
470 | ASSERT (Context.CacheData != NULL);
|
---|
471 | if (Context.CacheData == NULL) {
|
---|
472 | FreePages (Context.ProcessorInfo, EFI_SIZE_TO_PAGES (NumberOfProcessors * sizeof (*Context.ProcessorInfo)));
|
---|
473 | return EFI_OUT_OF_RESOURCES;
|
---|
474 | }
|
---|
475 |
|
---|
476 | ZeroMem (Context.CacheData, CacheDataCount * sizeof (*Context.CacheData));
|
---|
477 |
|
---|
478 | //
|
---|
479 | // Collect Package ID and APIC ID of all processors.
|
---|
480 | //
|
---|
481 | for (ProcessorIndex = 0; ProcessorIndex < NumberOfProcessors; ProcessorIndex++) {
|
---|
482 | CpuCacheInfoGetProcessorInfo (Context.MpServices, ProcessorIndex, &ProcessorInfo);
|
---|
483 | Context.ProcessorInfo[ProcessorIndex].Package = ProcessorInfo.Location.Package;
|
---|
484 | Context.ProcessorInfo[ProcessorIndex].ApicId = (UINT32)ProcessorInfo.ProcessorId;
|
---|
485 | }
|
---|
486 |
|
---|
487 | //
|
---|
488 | // Wakeup all processors for CacheData(core type and cache data) collection.
|
---|
489 | //
|
---|
490 | CpuCacheInfoStartupAllCPUs (Context.MpServices, CpuCacheInfoCollectCoreAndCacheData, &Context);
|
---|
491 |
|
---|
492 | //
|
---|
493 | // Collect CpuCacheInfo data from CacheData.
|
---|
494 | //
|
---|
495 | Status = CpuCacheInfoCollectCpuCacheInfoData (Context.CacheData, Context.ProcessorInfo, NumberOfProcessors, CpuCacheInfo, CpuCacheInfoCount);
|
---|
496 |
|
---|
497 | FreePages (Context.CacheData, EFI_SIZE_TO_PAGES (CacheDataCount * sizeof (*Context.CacheData)));
|
---|
498 | FreePages (Context.ProcessorInfo, EFI_SIZE_TO_PAGES (NumberOfProcessors * sizeof (*Context.ProcessorInfo)));
|
---|
499 |
|
---|
500 | return Status;
|
---|
501 | }
|
---|