summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SmbiosMemory/Dxe/SmbiosMemory.c
blob: 910c58a60a58e742c55423363e581aeaaa123383 (plain)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/** @file
  This driver will determine memory configuration information from the chipset
  and memory and create SMBIOS memory structures appropriately.

@copyright
  Copyright (c) 1999 - 2013 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.
  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement

**/
#include "SmbiosMemory.h"
EFI_DRIVER_ENTRY_POINT (SmbiosMemoryEntryPoint)

extern UINT8                    SmBiosMemoryStrings[];
MEMORY_MODULE_MANUFACTURE_LIST  MemoryModuleManufactureList[] = {
  {
    0,
    0x2c,
    L"Micron"
  },
  {
    0,
    0xad,
    L"Hynix/Hyundai"
  },
  {
    0,
    0xce,
    L"Samsung"
  },
  {
    1,
    0x4f,
    L"Transcend"
  },
  {
    1,
    0x98,
    L"Kingston"
  },
  {
    2,
    0xfe,
    L"Elpida"
  },
  {
    0xff,
    0xff,
    0
  }
};

///
/// Even SPD Addresses only as we read Words
///
const UINT8
  SpdAddress[] = { 2, 8, 116, 118, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144 };

/**
    This driver will determine memory configuration information from the chipset
    and memory and report the memory configuration info to the DataHub.

    @param[in] ImageHandle   - Handle for the image of this driver
    @param[in] SystemTable   - Pointer to the EFI System Table

    @retval EFI_SUCCESS          - if the data is successfully reported
    @retval EFI_NOT_FOUND        - if the HOB list could not be located.
    @retval EFI_OUT_OF_RESOURCES - if not able to get resouces.
**/
EFI_STATUS
SmbiosMemoryEntryPoint (
  IN EFI_HANDLE       ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
  )
{
  BOOLEAN                         Populated;
  CHAR16                          StringBuffer2[64];
  EFI_DATA_HUB_PROTOCOL           *DataHub;
  DDR_ROW_CONFIG                  RowConfArray[SA_MC_MAX_SOCKETS];
  EFI_MEMORY_SUBCLASS_DRIVER_DATA MemorySubClassData;
  EFI_SMBUS_DEVICE_ADDRESS        SmbusSlaveAddress;
  EFI_SMBUS_HC_PROTOCOL           *SmbusController;
  EFI_STATUS                      Status;
  EFI_STRING                      StringBuffer;
  EFI_PHYSICAL_ADDRESS            BaseAddress;
  UINT8                           Index;
  UINT8                           *SmbusBuffer;
  UINT16                          ArrayInstance;
  UINT64                          DimmMemorySize;
  UINT64                          TotalMemorySize;
  UINT8                           Dimm;
  UINTN                           SmbusBufferSize;
  UINTN                           SmbusLength;
  UINTN                           SmbusOffset;
  UINTN                           StringBufferSize;
  UINT8                           IndexCounter;
  UINTN                           IdListIndex;
  MEM_INFO_PROTOCOL               *MemInfoHob;
  EFI_GUID                        MemInfoProtocolGuid = MEM_INFO_PROTOCOL_GUID;
  UINT8                           ChannelASlotMap;
  UINT8                           ChannelBSlotMap;
  UINT8                           BitIndex;
  UINT16                          MaxSockets;
  UINT8                           WidthCount=0;  //AMI_OVERRIDE [EIP344598]
  UINT8                           ChannelASlotNum;
  UINT8                           ChannelBSlotNum;
  BOOLEAN                         SlotPresent;
  UINT16                          MemoryTotalWidth;
  UINT16                          MemoryDataWidth;
  UINT8                           i;

#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
  EFI_HANDLE                      DriverHandle;
  EFI_HII_DATABASE_PROTOCOL       *HiiDatabase;
  EFI_HII_PACKAGE_LIST_HEADER     *PackageList;
  EFI_HII_HANDLE                  StringPackHandle;
#else
  EFI_HII_PROTOCOL                *Hii;
  EFI_HII_HANDLE                  HiiHandle;
  EFI_HII_PACKAGES                *PackageList;
#endif
  STRING_REF                      DimmToDevLocator[] = {
    STRING_TOKEN (STR_MEMORY_SUBCLASS_DEVICE_LOCATOR_0),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_DEVICE_LOCATOR_1),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_DEVICE_LOCATOR_2),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_DEVICE_LOCATOR_3)
  };

  STRING_REF                      DimmToBankLocator[] = {
    STRING_TOKEN (STR_MEMORY_SUBCLASS_BANK_LOCATOR_0),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_BANK_LOCATOR_1),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_BANK_LOCATOR_2),
    STRING_TOKEN (STR_MEMORY_SUBCLASS_BANK_LOCATOR_3)
  };

  EFI_GUID                        gEfiMemorySubClassDriverGuid = EFI_MEMORY_SUBCLASS_DRIVER_GUID;
  DXE_PLATFORM_SA_POLICY_PROTOCOL *DxePlatformSaPolicy;

  DxePlatformSaPolicy           = NULL;

  ///
  /// Get the platform setup policy.
  ///
  Status = gBS->LocateProtocol (&gDxePlatformSaPolicyGuid, NULL, (VOID **) &DxePlatformSaPolicy);
  ASSERT_EFI_ERROR (Status);

  StringBufferSize  = (sizeof (CHAR16)) * 100;
  StringBuffer      = AllocateZeroPool (StringBufferSize);
  if (StringBuffer == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  SmbusBuffer     = NULL;
  SmbusBufferSize = 0x100;
  SmbusBuffer     = AllocatePool (SmbusBufferSize);
  if (SmbusBuffer == NULL) {
    (gBS->FreePool) (StringBuffer);
    return EFI_OUT_OF_RESOURCES;
  }

  Status = gBS->LocateProtocol (&gEfiDataHubProtocolGuid, NULL, (VOID **) &DataHub);
  ASSERT_EFI_ERROR (Status);

  Status = gBS->LocateProtocol (&gEfiSmbusProtocolGuid, NULL, (VOID **) &SmbusController);
  ASSERT_EFI_ERROR (Status);

#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)

  Status = gBS->LocateProtocol (
                  &gEfiHiiDatabaseProtocolGuid,
                  NULL,
                  (VOID **) &HiiDatabase
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  ///
  /// Create driver handle used by HII database
  ///
  Status = CreateHiiDriverHandle (&DriverHandle);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  ///
  /// Publish our HII data
  ///
  PackageList = PreparePackageList (1, &gEfiMemorySubClassDriverGuid, SmBiosMemoryStrings);
  if (PackageList == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Status = HiiDatabase->NewPackageList (
                          HiiDatabase,
                          PackageList,
                          DriverHandle,
                          &StringPackHandle
                          );
  ASSERT_EFI_ERROR (Status);

#else
  ///
  /// There should only be one HII protocol
  ///
  Status = gBS->LocateProtocol (&gEfiHiiProtocolGuid, NULL, (VOID **) &Hii);
  ASSERT_EFI_ERROR (Status);

  ///
  /// Add our default strings to the HII database. They will be modified later.
  ///
  PackageList = PreparePackages (1, &gEfiMemorySubClassDriverGuid, SmBiosMemoryStrings);
  Status      = Hii->NewPack (Hii, PackageList, &HiiHandle);

#endif

  (gBS->FreePool) (PackageList);

  Status = gBS->LocateProtocol (&MemInfoProtocolGuid, NULL, (VOID **) &MemInfoHob);
  ASSERT_EFI_ERROR (Status);
  ///
  /// Data for TYPE 16 SMBIOS Structure
  ///
  ///
  /// Create physical array and associated data for all mainboard memory
  ///
  ArrayInstance                         = 1;
  TotalMemorySize                       = 0;
  MemorySubClassData.Header.Version     = EFI_MEMORY_SUBCLASS_VERSION;
  MemorySubClassData.Header.HeaderSize  = sizeof (EFI_SUBCLASS_TYPE1_HEADER);
  MemorySubClassData.Header.Instance    = ArrayInstance;
  MemorySubClassData.Header.SubInstance = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
  MemorySubClassData.Header.RecordType  = EFI_MEMORY_ARRAY_LOCATION_RECORD_NUMBER;

  MemorySubClassData.Record.ArrayLocationData.MemoryArrayLocation = EfiMemoryArrayLocationSystemBoard;
  MemorySubClassData.Record.ArrayLocationData.MemoryArrayUse      = EfiMemoryArrayUseSystemMemory;

  ///
  /// Detect ECC
  ///
  if (MemInfoHob->MemInfoData.EccSupport) {
    MemorySubClassData.Record.ArrayLocationData.MemoryErrorCorrection = EfiMemoryErrorCorrectionSingleBitEcc;
  } else {
    MemorySubClassData.Record.ArrayLocationData.MemoryErrorCorrection = EfiMemoryErrorCorrectionNone;
  }
  ///
  /// Get the Memory DIMM info from platform policy protocols
  ///
  ChannelASlotMap = DxePlatformSaPolicy->MemoryConfig->ChannelASlotMap;
  ChannelBSlotMap = DxePlatformSaPolicy->MemoryConfig->ChannelBSlotMap;
  ChannelASlotNum = 0;
  ChannelBSlotNum = 0;
  for (BitIndex = 0; BitIndex < 8; BitIndex++) {
    if ((ChannelASlotMap >> BitIndex) & BIT0) {
      ChannelASlotNum++;
    }

    if ((ChannelBSlotMap >> BitIndex) & BIT0) {
      ChannelBSlotNum++;
    }
  }

  MaxSockets  = ChannelASlotNum + ChannelBSlotNum;
  MemorySubClassData.Record.ArrayLocationData.MaximumMemoryCapacity = MAX_RANK_CAPACITY * SA_MC_MAX_SIDES * MaxSockets;
  MemorySubClassData.Record.ArrayLocationData.NumberMemoryDevices   = (UINT16) (MaxSockets);

  ///
  /// Report top level physical array to datahub
  /// This will translate into a Type 16 SMBIOS Record
  ///
  Status = DataHub->LogData (
                      DataHub,
                      &gEfiMemorySubClassGuid,
                      &gEfiMemorySubClassDriverGuid,
                      EFI_DATA_RECORD_CLASS_DATA,
                      &MemorySubClassData,
                      sizeof (EFI_SUBCLASS_TYPE1_HEADER) + sizeof (EFI_MEMORY_ARRAY_LOCATION_DATA)
                      );
  if (EFI_ERROR (Status)) {
    goto CleanAndExit;
  }
  ///
  /// Get Memory size parameters for each rank
  ///
  ///
  /// We start from a base address of 0 for rank 0. We calculate the base address based on the DIMM size.
  ///
  BaseAddress = 0;

  for (Dimm = 0; Dimm < SA_MC_MAX_SLOTS; Dimm++) {
    ///
    /// Channel 0
    ///
    RowConfArray[Dimm].BaseAddress  = BaseAddress;
    RowConfArray[Dimm].RowLength    = LShiftU64 (MemInfoHob->MemInfoData.dimmSize[Dimm], 20);
    BaseAddress += RowConfArray[Dimm].RowLength;
    ///
    /// Channel 1
    ///
    RowConfArray[Dimm + SA_MC_MAX_SLOTS].BaseAddress  = BaseAddress;
    RowConfArray[Dimm + SA_MC_MAX_SLOTS].RowLength    = LShiftU64 (MemInfoHob->MemInfoData.dimmSize[Dimm + SA_MC_MAX_SLOTS], 20);
    BaseAddress += RowConfArray[Dimm + SA_MC_MAX_SLOTS].RowLength;

  }
  ///
  /// For each existed socket whether it is populated or not generate Type 17.
  /// Type 20 is optional for existed and populated socket.
  ///
  /// The Desktop and mobile only support 2 channels * 2 slots per channel = 4 sockets totally
  /// So there is rule here for Desktop and mobile that there are no more 4 DIMMS totally in a system:
  ///  Channel A/ Slot 0 --> SpdAddressTable[0] --> DimmToDevLocator[0] --> MemInfoHobProtocol.MemInfoData.dimmSize[0]
  ///  Channel A/ Slot 1 --> SpdAddressTable[1] --> DimmToDevLocator[1] --> MemInfoHobProtocol.MemInfoData.dimmSize[1]
  ///  Channel B/ Slot 0 --> SpdAddressTable[2] --> DimmToDevLocator[2] --> MemInfoHobProtocol.MemInfoData.dimmSize[2]
  ///  Channel B/ Slot 1 --> SpdAddressTable[3] --> DimmToDevLocator[3] --> MemInfoHobProtocol.MemInfoData.dimmSize[3]
  ///
  for (Dimm = 0; Dimm < SA_MC_MAX_SOCKETS; Dimm++) {
    ///
    /// Use channel slot map to check whether the Socket is supported in this SKU, some SKU only has 2 Sockets totally
    ///
    SlotPresent = FALSE;
    if (Dimm < 2) {
      if (ChannelASlotMap & (1 << Dimm)) {
        SlotPresent = TRUE;
      }
    } else {
      if (ChannelBSlotMap & (1 << (Dimm - 2))) {
        SlotPresent = TRUE;
      }
    }
    ///
    /// Don't create Type 17 and Type 20 items for non-existing socket
    ///
    if (!SlotPresent) {
      continue;
    }
    ///
    /// Generate Memory Device info (Type 17)
    ///
    ZeroMem (SmbusBuffer, SmbusBufferSize);
    ///
    /// Only read the SPD data if the DIMM is populated in the slot.
    ///
    Populated = MemInfoHob->MemInfoData.DimmExist[Dimm];
    if (Populated) {
      WidthCount++;           //AMI_OVERRIDE [EIP344598]
      ///
      /// Read the SPD for this DIMM
      ///
      SmbusSlaveAddress.SmbusDeviceAddress = (DxePlatformSaPolicy->MemoryConfig->SpdAddressTable[Dimm]) >> 1;

      ///
      /// Read only needed values from SMBus or DimmsSpdData pointer to improve performance.
      ///
      for (i = 0; i < sizeof SpdAddress; i++) {
        SmbusOffset = SpdAddress[i];
        if (MemInfoHob->MemInfoData.DimmsSpdData[Dimm] == NULL) {
          SmbusLength = 2;
          Status = SmbusController->Execute (
                                      SmbusController,
                                      SmbusSlaveAddress,
                                      SmbusOffset,
                                      EfiSmbusReadWord,
                                      FALSE,
                                      &SmbusLength,
                                      &SmbusBuffer[SmbusOffset]
                                      );
          if (EFI_ERROR (Status)) {
            Populated = FALSE;
            break;
          }
        } else {
          *(UINT16 *) (SmbusBuffer + SmbusOffset) = *(UINT16 *) (MemInfoHob->MemInfoData.DimmsSpdData[Dimm] + SmbusOffset);
        }
      }
    }

    ZeroMem (&MemorySubClassData, sizeof (EFI_MEMORY_SUBCLASS_DRIVER_DATA));

    ///
    /// Use SPD data to generate Device Type info
    ///
    MemorySubClassData.Header.Version                       = EFI_MEMORY_SUBCLASS_VERSION;
    MemorySubClassData.Header.HeaderSize                    = sizeof (EFI_SUBCLASS_TYPE1_HEADER);
    MemorySubClassData.Header.Instance                      = ArrayInstance;
    MemorySubClassData.Header.SubInstance                   = (UINT16) (Dimm + 1);
    MemorySubClassData.Header.RecordType                    = EFI_MEMORY_ARRAY_LINK_RECORD_NUMBER;

    MemorySubClassData.Record.ArrayLink.MemoryDeviceLocator = DimmToDevLocator[Dimm];
    MemorySubClassData.Record.ArrayLink.MemoryBankLocator   = DimmToBankLocator[Dimm];

#ifdef MEMORY_ASSET_TAG
    StrCpy (StringBuffer, MEMORY_ASSET_TAG);
    MemorySubClassData.Record.ArrayLink.MemoryAssetTag = (STRING_REF) 0;

#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
    Status = IfrLibNewString (StringPackHandle, &MemorySubClassData.Record.ArrayLink.MemoryAssetTag, StringBuffer);
    ASSERT_EFI_ERROR (Status);
#else
    Status = Hii->NewString (
                    Hii,
                    NULL,
                    HiiHandle,
                    &MemorySubClassData.Record.ArrayLink.MemoryAssetTag,
                    StringBuffer
                    );
    ASSERT_EFI_ERROR (Status);
#endif

#else
    MemorySubClassData.Record.ArrayLink.MemoryAssetTag = STRING_TOKEN (STR_MEMORY_SUBCLASS_DEFAULT_ASSET_TAG);
#endif

    MemorySubClassData.Record.ArrayLink.MemoryArrayLink.ProducerName    = gEfiMemorySubClassDriverGuid;
    MemorySubClassData.Record.ArrayLink.MemoryArrayLink.Instance        = ArrayInstance;
    MemorySubClassData.Record.ArrayLink.MemoryArrayLink.SubInstance     = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
    MemorySubClassData.Record.ArrayLink.MemorySubArrayLink.ProducerName = gEfiMemorySubClassDriverGuid;
    MemorySubClassData.Record.ArrayLink.MemorySubArrayLink.SubInstance  = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
    ///
    /// Set MemoryType value to DDR3 (0x18)
    ///
    MemorySubClassData.Record.ArrayLink.MemoryType = EfiMemoryTypeDdr3;

    ///
    /// According to SMBIOS 2.6.1 Specification - Appendix A Conformance Guidelines
    /// 4.8.7:  Form Factor is not 00h (Reserved) or 02h (Unknown).
    ///
    if (Populated) {
      switch (SmbusBuffer[DDR_MTYPE_SPD_OFFSET] & DDR_MTYPE_SPD_MASK) {
      case DDR_MTYPE_SODIMM:
        MemorySubClassData.Record.ArrayLink.MemoryFormFactor = EfiMemoryFormFactorSodimm;
        break;

      case DDR_MTYPE_RDIMM:
      case DDR_MTYPE_MINI_RDIMM:
        MemorySubClassData.Record.ArrayLink.MemoryFormFactor = EfiMemoryFormFactorRimm;
        break;

      case DDR_MTYPE_UDIMM:
      case DDR_MTYPE_MICRO_DIMM:
      case DDR_MTYPE_MINI_UDIMM:
      default:
        MemorySubClassData.Record.ArrayLink.MemoryFormFactor = EfiMemoryFormFactorDimm;
      }
      ///
      /// Show name for known manufacturer or ID for unknown manufacturer
      ///
      StrCpy (StringBuffer, L"");

      ///
      /// Calculate index counter
      /// Clearing Bit7 as it is the Parity Bit for Byte 117
      ///
      IndexCounter = SmbusBuffer[117] & (~0x80);

      ///
      /// Converter memory manufacturer ID to string
      ///
      for (IdListIndex = 0; MemoryModuleManufactureList[IdListIndex].Index != 0xff; IdListIndex++) {
        if (MemoryModuleManufactureList[IdListIndex].Index == IndexCounter &&
            MemoryModuleManufactureList[IdListIndex].ManufactureId == SmbusBuffer[118]
            ) {
          StrCpy (StringBuffer, MemoryModuleManufactureList[IdListIndex].ManufactureName);
          break;
        }
      }
      ///
      /// Use original data if no conversion information in conversion table
      ///
      if (!(*StringBuffer)) {
        for (Index = 117; Index < 119; Index++) {
          ///
          /// --cr--         EfiValueToHexStr(StringBuffer2, SmbusBuffer[Index], PREFIX_ZERO, 2);
          ///
          UnicodeValueToString (StringBuffer2, PREFIX_ZERO, SmbusBuffer[Index], 2);
          StrCat (StringBuffer, StringBuffer2);
        }
      }

      MemorySubClassData.Record.ArrayLink.MemoryManufacturer = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (
                StringPackHandle,
                &MemorySubClassData.Record.ArrayLink.MemoryManufacturer,
                StringBuffer
                );
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemoryManufacturer,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif
      StrCpy (StringBuffer, L"");
      for (Index = 122; Index < 126; Index++) {
        EfiValueToHexStr(StringBuffer2, SmbusBuffer[Index], PREFIX_ZERO, 2);
        StrCat (StringBuffer, StringBuffer2);
      }

      MemorySubClassData.Record.ArrayLink.MemorySerialNumber = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (
                StringPackHandle,
                &MemorySubClassData.Record.ArrayLink.MemorySerialNumber,
                StringBuffer
                );
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemorySerialNumber,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif
      StrCpy (StringBuffer, L"");
      for (Index = 128; Index < 146; Index++) {
        UnicodeSPrint (
          StringBuffer2,
          4,
          L"%c",
          SmbusBuffer[Index]
          );
        StrCat (StringBuffer, StringBuffer2);
      }

      MemorySubClassData.Record.ArrayLink.MemoryPartNumber = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (StringPackHandle, &MemorySubClassData.Record.ArrayLink.MemoryPartNumber, StringBuffer);
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemoryPartNumber,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif
      ///
      /// Get the Memory TotalWidth and DataWidth info for DDR3
      /// refer to DDR3 SPD 1.0 spec, Byte 8: Module Memory Bus Width
      /// SPD Offset 8 Bits [2:0] DataWidth aka Primary Bus Width
      /// SPD Offset 8 Bits [4:3] Bus Width extension for ECC
      ///
      MemoryDataWidth   = 8 * (1 << (SmbusBuffer[8] & 0x07));
      MemoryTotalWidth  = MemoryDataWidth + 8 * (SmbusBuffer[8] & 0x18);
      MemorySubClassData.Record.ArrayLink.MemoryTotalWidth  = MemoryTotalWidth;
      MemorySubClassData.Record.ArrayLink.MemoryDataWidth   = MemoryDataWidth;
      DimmMemorySize = RowConfArray[Dimm].RowLength;

      TotalMemorySize += DimmMemorySize;
      MemorySubClassData.Record.ArrayLink.MemoryDeviceSize              = DimmMemorySize;
      MemorySubClassData.Record.ArrayLink.MemoryTypeDetail.Synchronous  = 1;
      if (MemorySubClassData.Record.ArrayLink.MemoryFormFactor == EfiMemoryFormFactorRimm) {
        MemorySubClassData.Record.ArrayLink.MemoryTypeDetail.Rambus = 1;
      }

      MemorySubClassData.Record.ArrayLink.MemorySpeed       = MemInfoHob->MemInfoData.ddrFreq;
      MemorySubClassData.Record.ArrayLink.MemoryAttributes  = MemInfoHob->MemInfoData.RankInDimm[Dimm] & 0x0F;
    } else {
      ///
      /// Memory is not Populated in this slot.
      ///
      StrCpy (StringBuffer, L"");
      MemorySubClassData.Record.ArrayLink.MemoryManufacturer = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (
                StringPackHandle,
                &MemorySubClassData.Record.ArrayLink.MemoryManufacturer,
                StringBuffer
                );
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemoryManufacturer,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif
      MemorySubClassData.Record.ArrayLink.MemorySerialNumber = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (
                StringPackHandle,
                &MemorySubClassData.Record.ArrayLink.MemorySerialNumber,
                StringBuffer
                );
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemorySerialNumber,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif

      MemorySubClassData.Record.ArrayLink.MemoryPartNumber = (STRING_REF) 0;
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
      Status = IfrLibNewString (StringPackHandle, &MemorySubClassData.Record.ArrayLink.MemoryPartNumber, StringBuffer);
      ASSERT_EFI_ERROR (Status);
#else
      Status = Hii->NewString (
                      Hii,
                      NULL,
                      HiiHandle,
                      &MemorySubClassData.Record.ArrayLink.MemoryPartNumber,
                      StringBuffer
                      );
      ASSERT_EFI_ERROR (Status);
#endif
      DimmMemorySize  = 0;
      MemorySubClassData.Record.ArrayLink.MemorySpeed       = 0;
      MemorySubClassData.Record.ArrayLink.MemoryDeviceSize  = 0;
      MemorySubClassData.Record.ArrayLink.MemoryType        = EfiMemoryTypeUnknown;
      MemorySubClassData.Record.ArrayLink.MemoryFormFactor  = EfiMemoryFormFactorDimm;
    }
    ///
    /// Generate Memory Device info (Type 17)
    ///
    Status = DataHub->LogData (
                        DataHub,
                        &gEfiMemorySubClassGuid,
                        &gEfiMemorySubClassDriverGuid,
                        EFI_DATA_RECORD_CLASS_DATA,
                        &MemorySubClassData,
                        sizeof (EFI_SUBCLASS_TYPE1_HEADER) + sizeof (EFI_MEMORY_ARRAY_LINK)
                        );
    if (EFI_ERROR (Status)) {
      goto CleanAndExit;
    }
    ///
    /// Memory Device Mapped Address (Type 20) is optional and it will not be generated by RC
    ///
// ...AMI_OVERRIDE... support Type 20
    if ((Populated) && (DimmMemorySize != 0)) {
      ///
      /// Generate Memory Device Mapped Address info (Type 20)
      ///
      MemorySubClassData.Header.Instance    = ArrayInstance;
      MemorySubClassData.Header.SubInstance = (UINT16) (Dimm + 1);
      MemorySubClassData.Header.RecordType  = EFI_MEMORY_DEVICE_START_ADDRESS_RECORD_NUMBER;

      MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceStartAddress = RowConfArray[Dimm].BaseAddress;
      MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceEndAddress = MemorySubClassData.Record.
          DeviceStartAddress.MemoryDeviceStartAddress +
        DimmMemorySize -
        1;
      ///
      /// 1 or 2 will be applicable for lock step mode
      ///
      MemorySubClassData.Record.DeviceStartAddress.MemoryDevicePartitionRowPosition       = 0xFF;

      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryDeviceLink.ProducerName  = gEfiMemorySubClassDriverGuid;
      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryDeviceLink.Instance      = ArrayInstance;
      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryDeviceLink.SubInstance   = (UINT16) (Dimm + 1);
      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryArrayLink.ProducerName   = gEfiMemorySubClassDriverGuid;
      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryArrayLink.Instance       = ArrayInstance;
      MemorySubClassData.Record.DeviceStartAddress.PhysicalMemoryArrayLink.SubInstance = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;

      ///
      /// Set Interleave Data Depth and Position.
      ///
      if (((Dimm >= SA_MC_MAX_SLOTS) && (MemInfoHob->MemInfoData.DimmExist[Dimm - SA_MC_MAX_SLOTS])) ||
          ((Dimm < SA_MC_MAX_SLOTS) && (MemInfoHob->MemInfoData.DimmExist[Dimm + SA_MC_MAX_SLOTS]))
          ) {
        ///
        /// If DIMMs on both channels are populated then Interleaved Data Depth is 2 and Interleave Position is 1 or 2
        ///
        MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceInterleaveDataDepth = (UINT8) (MaxSockets >> 1);
        MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceInterleavePosition = (UINT8) (1 << (Dimm >= (MaxSockets >> 1)));
      } else {
        ///
        /// Interleaved Data Depth and Position is 1
        ///
        MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceInterleaveDataDepth  = 1;
        MemorySubClassData.Record.DeviceStartAddress.MemoryDeviceInterleavePosition   = 1;
      }
      ///
      /// Generate Memory Device Mapped Address info (Type 20)
      ///
      Status = DataHub->LogData (
                          DataHub,
                          &gEfiMemorySubClassGuid,
                          &gEfiMemorySubClassDriverGuid,
                          EFI_DATA_RECORD_CLASS_DATA,
                          &MemorySubClassData,
                          sizeof (EFI_SUBCLASS_TYPE1_HEADER) + sizeof (EFI_MEMORY_DEVICE_START_ADDRESS)
                          );
      if (EFI_ERROR (Status)) {
        goto CleanAndExit;
      }
    }
// ...AMI_OVERRIDE... support Type 20 end
  }
  ///
  /// Generate Memory Array Mapped Address info (TYPE 19)
  ///
  MemorySubClassData.Header.Instance    = ArrayInstance;
  MemorySubClassData.Header.SubInstance = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
  MemorySubClassData.Header.RecordType  = EFI_MEMORY_ARRAY_START_ADDRESS_RECORD_NUMBER;

  MemorySubClassData.Record.ArrayStartAddress.MemoryArrayStartAddress               = 0;
  MemorySubClassData.Record.ArrayStartAddress.MemoryArrayEndAddress                 = TotalMemorySize - 1;
  MemorySubClassData.Record.ArrayStartAddress.PhysicalMemoryArrayLink.ProducerName  = gEfiMemorySubClassDriverGuid;
  MemorySubClassData.Record.ArrayStartAddress.PhysicalMemoryArrayLink.Instance      = ArrayInstance;
  MemorySubClassData.Record.ArrayStartAddress.PhysicalMemoryArrayLink.SubInstance = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
  //MemorySubClassData.Record.ArrayStartAddress.MemoryArrayPartitionWidth = (UINT16) (MaxSockets);  //AMI_OVERRIDE [EIP344598]
  MemorySubClassData.Record.ArrayStartAddress.MemoryArrayPartitionWidth = WidthCount;  //AMI_OVERRIDE [EIP344598]
  ///
  /// Generate Memory Array Mapped Address info (TYPE 19)
  ///
  Status = DataHub->LogData (
                      DataHub,
                      &gEfiMemorySubClassGuid,
                      &gEfiMemorySubClassDriverGuid,
                      EFI_DATA_RECORD_CLASS_DATA,
                      &MemorySubClassData,
                      sizeof (EFI_SUBCLASS_TYPE1_HEADER) + sizeof (EFI_MEMORY_ARRAY_START_ADDRESS)
                      );
CleanAndExit:
  (gBS->FreePool) (SmbusBuffer);
  (gBS->FreePool) (StringBuffer);
  return Status;
}