hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*++
 
Copyright (c) 2006  - 2021, Intel Corporation. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
Module Name:
 
  MiscProcessorInformationFunction.c
 
Abstract:
 
  Onboard processor information boot time changes.
  SMBIOS type 4.
 
--*/
 
#include "CommonHeader.h"
 
#include "MiscSubclassDriver.h"
 
#include <Protocol/MpService.h>
#include <Library/TimerLib.h>
#include <Register/Cpuid.h>
 
#define EfiProcessorFamilyIntelAtomProcessor    0x2B
 
EFI_GUID                        mProcessorProducerGuid;
 
 
/**
  Get cache SMBIOS record handle.
 
  @param  Smbios        Pointer to SMBIOS protocol instance.
  @param  CacheLevel    Level of cache, starting from one.
  @param  Handle        Returned record handle.
 
**/
 
VOID
GetCacheHandle (
  IN  EFI_SMBIOS_PROTOCOL      *Smbios,
  IN  UINT8                    CacheLevel,
  OUT  EFI_SMBIOS_HANDLE       *Handle
  )
{
  UINT16                     CacheConfig;
  EFI_STATUS                 Status;
  EFI_SMBIOS_TYPE            RecordType;
  EFI_SMBIOS_TABLE_HEADER    *Buffer;
 
  *Handle = 0;
  RecordType = EFI_SMBIOS_TYPE_CACHE_INFORMATION;
 
  do {
    Status = Smbios->GetNext (
                       Smbios,
                       Handle,
                       &RecordType,
                       &Buffer,
                       NULL
                       );
    if (!EFI_ERROR(Status)) {
      CacheConfig = *(UINT16*)((UINT8*)Buffer + 5);
      if ((CacheConfig & 0x7) == (CacheLevel -1) ) {
        return;
      }
    }
  } while (!EFI_ERROR(Status));
 
  *Handle = 0xFFFF;
}
 
#define BSEL_CR_OVERCLOCK_CONTROL    0xCD
#define    FUSE_BSEL_MASK                0x03
 
 
 
UINT16 miFSBFrequencyTable[4] = {
  83,              // 83.3MHz
  100,          // 100MHz
  133,          // 133MHz
  117           // 116.7MHz
};
 
/**
  Determine the processor core frequency
 
  @param None
 
  @retval Processor core frequency multiplied by 3
 
 
**/
UINT16
DetermineiFsbFromMsr (
  VOID
  )
{
  //
  // Determine the processor core frequency
  //
  UINT64    Temp;
  Temp = (AsmReadMsr64 (BSEL_CR_OVERCLOCK_CONTROL)) & FUSE_BSEL_MASK;
  return miFSBFrequencyTable[(UINT32)(Temp)];
 
}
 
CHAR16 *
CpuidSocVendorBrandString (
  VOID
  )
{
  UINT32  MaximumExtendedFunction;
  //
  // Array to store brand string from 3 brand string leafs with
  // 4 32-bit brand string values per leaf and an extra value to
  // null terminate the string.
  //
  UINT32  BrandString[3 * 4 + 1];
  CHAR8   *AsciiBrandString;
  CHAR16  *UnicodeBrandString;
  UINTN   Length;
 
  AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaximumExtendedFunction, NULL, NULL, NULL);
 
  ZeroMem (&BrandString, sizeof (BrandString));
  if (CPUID_BRAND_STRING1 <= MaximumExtendedFunction) {
    AsmCpuid (
      CPUID_BRAND_STRING1,
      &BrandString[0],
      &BrandString[1],
      &BrandString[2],
      &BrandString[3]
      );
  }
  if (CPUID_BRAND_STRING2 <= MaximumExtendedFunction) {
    AsmCpuid (
      CPUID_BRAND_STRING2,
      &BrandString[4],
      &BrandString[5],
      &BrandString[6],
      &BrandString[7]
      );
  }
  if (CPUID_BRAND_STRING3 <= MaximumExtendedFunction) {
    AsmCpuid (
      CPUID_BRAND_STRING3,
      &BrandString[8],
      &BrandString[9],
      &BrandString[10],
      &BrandString[11]
      );
  }
 
  //
  // Skip spaces at the beginning of the brand string
  //
  for (AsciiBrandString = (CHAR8 *)BrandString; *AsciiBrandString == ' '; AsciiBrandString++);
 
  DEBUG ((DEBUG_INFO, "Processor Brand String = %a\n", AsciiBrandString));
 
  //
  // Convert ASCII brand string to an allocated Unicode brand string
  //
  Length = AsciiStrLen (AsciiBrandString) + 1;
  UnicodeBrandString = AllocatePool (Length * sizeof (CHAR16));
  AsciiStrToUnicodeStrS (AsciiBrandString, UnicodeBrandString, Length);
 
  DEBUG ((DEBUG_INFO, "Processor Unicode Brand String = %s\n", UnicodeBrandString));
 
  return UnicodeBrandString;
}
 
UINT64
MeasureTscFrequency (
  VOID
  )
{
  EFI_TPL  CurrentTpl;
  UINT64   BeginValue;
  UINT64   EndValue;
  UINT64   Frequency;
 
  //
  // Wait for 10000us = 10ms for the calculation
  // It needs a precise timer to calculate the ticks
  //
  CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  BeginValue = AsmReadTsc ();
  MicroSecondDelay (10000);
  EndValue   = AsmReadTsc ();
  gBS->RestoreTPL (CurrentTpl);
  Frequency = MultU64x32 (EndValue - BeginValue, 1000);
  Frequency = DivU64x32 (Frequency, 10);
  return Frequency;
}
 
MISC_SMBIOS_TABLE_FUNCTION (MiscProcessorInformation)
{
    CHAR8                           *OptionalStrStart;
    UINTN                           OptionalStrSize;
    EFI_STRING                      SerialNumber;
    CHAR16                          *Version=NULL;
    CHAR16                          *Manufacturer=NULL;
    CHAR16                          *Socket=NULL;
    CHAR16                          *AssetTag=NULL;
    CHAR16                          *PartNumber=NULL;
    UINTN                           SerialNumberStrLen=0;
    UINTN                           VersionStrLen=0;
    UINTN                           ManufacturerStrLen=0;
    UINTN                           SocketStrLen=0;
    UINTN                           AssetTagStrLen=0;
    UINTN                           PartNumberStrLen=0;
    UINTN                           ProcessorVoltage=(BIT7 | 9);
    UINT32                          Eax01;
    UINT32                          Ebx01;
    UINT32                          Ecx01;
    UINT32                          Edx01;
    STRING_REF                      TokenToGet;
    EFI_STATUS                      Status;
    EFI_SMBIOS_HANDLE               SmbiosHandle;
    SMBIOS_TABLE_TYPE4              *SmbiosRecord;
    EFI_CPU_DATA_RECORD             *ForType4InputData;
    UINT16                          L1CacheHandle=0;
    UINT16                          L2CacheHandle=0;
    UINT16                          L3CacheHandle=0;
    UINTN                           NumberOfEnabledProcessors=0 ;
    UINTN                           NumberOfProcessors=0;
    UINT64                          Frequency = 0;
    EFI_MP_SERVICES_PROTOCOL        *MpService;
    PROCESSOR_ID_DATA               *ProcessorId = NULL;
 
    //
    // First check for invalid parameters.
    //
    if (RecordData == NULL) {
        return EFI_INVALID_PARAMETER;
    }
 
    ForType4InputData = (EFI_CPU_DATA_RECORD *)RecordData;
 
    ProcessorId = AllocateZeroPool(sizeof(PROCESSOR_ID_DATA));
    if (ProcessorId == NULL) {
      return EFI_INVALID_PARAMETER;
    }
 
    //
    // Token to get for Socket Name
    //
    TokenToGet = STRING_TOKEN (STR_MISC_SOCKET_NAME);
    Socket = SmbiosMiscGetString (TokenToGet);
    SocketStrLen = StrLen(Socket);
    if (SocketStrLen > SMBIOS_STRING_MAX_LENGTH) {
         return EFI_UNSUPPORTED;
    }
 
    //
    // Token to get for Processor Manufacturer
    //
    TokenToGet = STRING_TOKEN (STR_MISC_PROCESSOR_MAUFACTURER);
    Manufacturer = SmbiosMiscGetString (TokenToGet);
    ManufacturerStrLen = StrLen(Manufacturer);
    if (ManufacturerStrLen > SMBIOS_STRING_MAX_LENGTH) {
      return EFI_UNSUPPORTED;
    }
 
    //
    // Token to get for Processor Version
    //
    Version = CpuidSocVendorBrandString ();
    VersionStrLen = StrLen(Version);
    if (VersionStrLen > SMBIOS_STRING_MAX_LENGTH) {
        return EFI_UNSUPPORTED;
    }
 
    //
    // Token to get for Serial Number
    //
    TokenToGet = STRING_TOKEN (STR_MISC_PROCESSOR_SERIAL_NUMBER);
    SerialNumber = SmbiosMiscGetString (TokenToGet);
    SerialNumberStrLen = StrLen(SerialNumber);
    if (SerialNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
        return EFI_UNSUPPORTED;
    }
 
    //
    // Token to get for Assert Tag Information
    //
    TokenToGet = STRING_TOKEN (STR_MISC_ASSERT_TAG_DATA);
    AssetTag = SmbiosMiscGetString (TokenToGet);
    AssetTagStrLen = StrLen(AssetTag);
    if (AssetTagStrLen > SMBIOS_STRING_MAX_LENGTH) {
        return EFI_UNSUPPORTED;
    }
 
    //
    // Token to get for part number Information
    //
    TokenToGet = STRING_TOKEN (STR_MISC_PART_NUMBER);
    PartNumber = SmbiosMiscGetString (TokenToGet);
    PartNumberStrLen = StrLen(PartNumber);
    if (PartNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
         return EFI_UNSUPPORTED;
    }
 
    //
    // Two zeros following the last string.
    //
    OptionalStrSize = AssetTagStrLen + 1 + SocketStrLen + 1+ ManufacturerStrLen +1 + VersionStrLen+ 1+ SerialNumberStrLen + 1 + PartNumberStrLen+ 1 + 1;
    SmbiosRecord = AllocateZeroPool(sizeof (SMBIOS_TABLE_TYPE4) + OptionalStrSize);
 
    SmbiosRecord->Hdr.Type = EFI_SMBIOS_TYPE_PROCESSOR_INFORMATION;
    SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE4);
 
    //
    // Make handle chosen by smbios protocol.add automatically.
    //
    SmbiosRecord->Hdr.Handle = 0;
 
    SmbiosRecord-> Socket= 1;
    SmbiosRecord -> ProcessorManufacturer = 2;
    SmbiosRecord -> ProcessorVersion = 3;
    SmbiosRecord ->SerialNumber =4;
 
    SmbiosRecord-> AssetTag= 5;
    SmbiosRecord-> PartNumber= 6;
 
    //
    // Processor Type
    //
    ForType4InputData-> VariableRecord.ProcessorType= EfiCentralProcessor;
    SmbiosRecord -> ProcessorType = ForType4InputData-> VariableRecord.ProcessorType;
 
    //
    // Processor Family
    //
    ForType4InputData-> VariableRecord.ProcessorFamily= EfiProcessorFamilyIntelAtomProcessor; //0x2B;;
    SmbiosRecord -> ProcessorFamily = ForType4InputData-> VariableRecord.ProcessorFamily;
    SmbiosRecord -> ExternalClock = DetermineiFsbFromMsr();
 
    //
    // Processor ID
    //
    AsmCpuid(0x001, &Eax01, &Ebx01, &Ecx01, &Edx01);
    ProcessorId->Signature = *(PROCESSOR_SIGNATURE *)&Eax01;
    ProcessorId->FeatureFlags = *(PROCESSOR_FEATURE_FLAGS *)&Edx01;
    SmbiosRecord -> ProcessorId = *(PROCESSOR_ID_DATA *)ProcessorId;
 
    //
    // Processor Voltage
    //
    ForType4InputData-> VariableRecord.ProcessorVoltage= *(EFI_PROCESSOR_VOLTAGE_DATA *)&ProcessorVoltage;
    SmbiosRecord -> Voltage = *(PROCESSOR_VOLTAGE *) &(ForType4InputData-> VariableRecord.ProcessorVoltage);
 
    //
    // Status
    //
    ForType4InputData-> VariableRecord.ProcessorHealthStatus= 0x41;//0x41;
    SmbiosRecord -> Status = ForType4InputData-> VariableRecord.ProcessorHealthStatus;
 
    //
    // Processor Upgrade
    //
    SmbiosRecord -> ProcessorUpgrade = 0x008;
 
    //
    // Processor Family 2
    //
    SmbiosRecord -> ProcessorFamily2 = ForType4InputData-> VariableRecord.ProcessorFamily;
 
    //
    // Processor speed in MHz
    //
    Frequency = MeasureTscFrequency ();
    Frequency = DivU64x32 (Frequency, 1000000);
    SmbiosRecord-> CurrentSpeed = (UINT16)Frequency;
    SmbiosRecord-> MaxSpeed = (UINT16)Frequency;
 
    //
    // Processor Characteristics
    //
    AsmCpuid(0x8000000, NULL, NULL, NULL, &Edx01);
    Edx01= Edx01 >> 28;
    Edx01 &= 0x01;
    SmbiosRecord-> ProcessorCharacteristics= (UINT16)Edx01;
 
    //
    // Processor Core Count and Enabled core count
    //
    Status = gBS->LocateProtocol (
                    &gEfiMpServiceProtocolGuid,
                    NULL,
                    (void **)&MpService
                    );
    if (!EFI_ERROR (Status)) {
    //
    // Determine the number of processors
    //
    MpService->GetNumberOfProcessors (
                 MpService,
                 &NumberOfProcessors,
                 &NumberOfEnabledProcessors
                 );
    }
    SmbiosRecord-> CoreCount= (UINT8)NumberOfProcessors;
    SmbiosRecord-> EnabledCoreCount= (UINT8)NumberOfEnabledProcessors;
    SmbiosRecord-> ThreadCount= (UINT8)NumberOfEnabledProcessors;
    SmbiosRecord-> ProcessorCharacteristics = 0x2; // Unknown
 
    //
    // Processor Cache Handle
    //
    GetCacheHandle( Smbios,1, &L1CacheHandle);
    GetCacheHandle( Smbios,2, &L2CacheHandle);
    GetCacheHandle( Smbios,3, &L3CacheHandle);
 
    //
    // Updating Cache Handle Information
    //
    SmbiosRecord->L1CacheHandle  = L1CacheHandle;
    SmbiosRecord->L2CacheHandle  = L2CacheHandle;
    SmbiosRecord->L3CacheHandle  = L3CacheHandle;
 
    OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
    UnicodeStrToAsciiStrS (Socket, OptionalStrStart, OptionalStrSize);
    OptionalStrStart += (SocketStrLen + 1);
    OptionalStrSize  -= (SocketStrLen + 1);
    UnicodeStrToAsciiStrS (Manufacturer, OptionalStrStart, OptionalStrSize);
    OptionalStrStart += (ManufacturerStrLen + 1);
    OptionalStrSize  -= (ManufacturerStrLen + 1);
    UnicodeStrToAsciiStrS (Version, OptionalStrStart, OptionalStrSize);
    OptionalStrStart += (VersionStrLen + 1);
    OptionalStrSize  -= (VersionStrLen + 1);
    UnicodeStrToAsciiStrS (SerialNumber, OptionalStrStart, OptionalStrSize);
    OptionalStrStart += (SerialNumberStrLen + 1);
    OptionalStrSize  -= (SerialNumberStrLen + 1);
    UnicodeStrToAsciiStrS (AssetTag, OptionalStrStart, OptionalStrSize);
    OptionalStrStart += (AssetTagStrLen + 1);
    OptionalStrSize  -= (AssetTagStrLen + 1);
    UnicodeStrToAsciiStrS (PartNumber, OptionalStrStart, OptionalStrSize);
 
    //
    // Now we have got the full Smbios record, call Smbios protocol to add this record.
    //
    SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
    Status = Smbios-> Add(
                        Smbios,
                        NULL,
                        &SmbiosHandle,
                        (EFI_SMBIOS_TABLE_HEADER *) SmbiosRecord
                        );
    if (EFI_ERROR (Status)) return Status;
    FreePool(SmbiosRecord);
    return Status;
 
}