MiscMemoryDeviceFunction.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*++
  2. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. Module Name:
  5. MiscMemoryDeviceFunction.c
  6. Abstract:
  7. Memory Device
  8. Misc. subclass type 17.
  9. SMBIOS type 17.
  10. --*/
  11. #include "CommonHeader.h"
  12. #include "MiscSubclassDriver.h"
  13. #include <Protocol/MemInfo.h>
  14. #define FREQ_800 0x00
  15. #define FREQ_1066 0x01
  16. #define FREQ_1333 0x02
  17. #define FREQ_1600 0x03
  18. #define MAX_SOCKETS 2
  19. #define EfiMemoryTypeDdr3 0x18
  20. enum {
  21. DDRType_DDR3 = 0,
  22. DDRType_DDR3L = 1,
  23. DDRType_DDR3U = 2,
  24. DDRType_DDR3All = 3,
  25. DDRType_LPDDR2 = 4,
  26. DDRType_LPDDR3 = 5,
  27. DDRType_DDR4 = 6
  28. };
  29. /**
  30. This function makes boot time changes to the contents of the
  31. MiscBiosVendor (Type 0).
  32. @param RecordData Pointer to copy of RecordData from the Data Table.
  33. @retval EFI_SUCCESS All parameters were valid.
  34. @retval EFI_UNSUPPORTED Unexpected RecordType value.
  35. @retval EFI_INVALID_PARAMETER Invalid parameter was found.
  36. **/
  37. VOID
  38. GetType16Hndl (
  39. IN EFI_SMBIOS_PROTOCOL *Smbios,
  40. OUT EFI_SMBIOS_HANDLE *Handle
  41. )
  42. {
  43. EFI_STATUS Status;
  44. EFI_SMBIOS_TYPE RecordType;
  45. EFI_SMBIOS_TABLE_HEADER *Buffer;
  46. *Handle = 0;
  47. RecordType = EFI_SMBIOS_TYPE_PHYSICAL_MEMORY_ARRAY;
  48. Status = Smbios->GetNext (
  49. Smbios,
  50. Handle,
  51. &RecordType,
  52. &Buffer,
  53. NULL
  54. );
  55. if (!EFI_ERROR(Status)) {
  56. return;
  57. }
  58. *Handle = 0xFFFF;
  59. }
  60. MISC_SMBIOS_TABLE_FUNCTION( MiscMemoryDevice )
  61. {
  62. CHAR8 *OptionalStrStart;
  63. UINTN MemDeviceStrLen;
  64. UINTN MemBankLocatorStrLen;
  65. UINTN MemManufacturerStrLen;
  66. UINTN MemSerialNumberStrLen;
  67. UINTN MemAssetTagStrLen;
  68. UINTN MemPartNumberStrLen;
  69. CHAR16 *MemDevice;
  70. CHAR16 *MemBankLocator;
  71. CHAR16 *MemManufacturer;
  72. CHAR16 *MemSerialNumber;
  73. CHAR16 *MemAssetTag;
  74. CHAR16 *MemPartNumber;
  75. EFI_STATUS Status;
  76. STRING_REF TokenToGet;
  77. SMBIOS_TABLE_TYPE17 *SmbiosRecord;
  78. SMBIOS_TABLE_TYPE19 *SmbiosRecord19;
  79. EFI_SMBIOS_HANDLE SmbiosHandle;
  80. EFI_MEMORY_ARRAY_LINK_DATA *ForType17InputData;
  81. UINT16 DdrFreq=0;
  82. UINT16 Type16Handle=0;
  83. MEM_INFO_PROTOCOL *MemInfoHob;
  84. UINT8 MemoryType;
  85. UINT8 Dimm;
  86. UINT8 NumSlots;
  87. UINT64 TotalMemorySize;
  88. STRING_REF DevLocator[] = {
  89. STRING_TOKEN(STR_MISC_MEM_DEV_LOCATOR0), STRING_TOKEN(STR_MISC_MEM_DEV_LOCATOR1)
  90. };
  91. STRING_REF BankLocator[] = {
  92. STRING_TOKEN(STR_MISC_MEM_BANK_LOCATOR0), STRING_TOKEN(STR_MISC_MEM_BANK_LOCATOR1)
  93. };
  94. //
  95. // First check for invalid parameters.
  96. //
  97. if (RecordData == NULL) {
  98. return EFI_INVALID_PARAMETER;
  99. }
  100. ForType17InputData = (EFI_MEMORY_ARRAY_LINK_DATA *)RecordData;
  101. //
  102. // Get Memory size parameters for each rank from the chipset registers
  103. //
  104. Status = gBS->LocateProtocol (
  105. &gMemInfoProtocolGuid,
  106. NULL,
  107. (void **)&MemInfoHob
  108. );
  109. ASSERT_EFI_ERROR (Status);
  110. NumSlots = (UINT8)(MAX_SOCKETS);
  111. //
  112. // Memory Freq
  113. //
  114. switch (MemInfoHob->MemInfoData.ddrFreq){
  115. case FREQ_800:
  116. DdrFreq = 800;
  117. break;
  118. case FREQ_1066:
  119. DdrFreq = 1066;
  120. break;
  121. case FREQ_1333:
  122. DdrFreq = 1333;
  123. break;
  124. case FREQ_1600:
  125. DdrFreq = 1600;
  126. break;
  127. default:
  128. DdrFreq = 0;
  129. break;
  130. }
  131. //
  132. // Memory Type
  133. //
  134. switch (MemInfoHob->MemInfoData.ddrType) {
  135. case DDRType_LPDDR2:
  136. MemoryType = EfiMemoryTypeDdr2;
  137. break;
  138. case DDRType_DDR3:
  139. case DDRType_DDR3L:
  140. case DDRType_DDR3U:
  141. case DDRType_LPDDR3:
  142. MemoryType = EfiMemoryTypeDdr3;
  143. break;
  144. default:
  145. MemoryType = EfiMemoryTypeUnknown;
  146. break;
  147. }
  148. GetType16Hndl( Smbios, &Type16Handle);
  149. TotalMemorySize = 0;
  150. for (Dimm = 0; Dimm < NumSlots; Dimm++) {
  151. //
  152. // Memory Device Locator
  153. //
  154. TokenToGet = DevLocator[Dimm];
  155. MemDevice = SmbiosMiscGetString (TokenToGet);
  156. MemDeviceStrLen = StrLen(MemDevice);
  157. if (MemDeviceStrLen > SMBIOS_STRING_MAX_LENGTH) {
  158. return EFI_UNSUPPORTED;
  159. }
  160. TokenToGet = DevLocator[Dimm];
  161. MemDevice = SmbiosMiscGetString (TokenToGet);
  162. MemDeviceStrLen = StrLen(MemDevice);
  163. if (MemDeviceStrLen > SMBIOS_STRING_MAX_LENGTH) {
  164. return EFI_UNSUPPORTED;
  165. }
  166. //
  167. // Memory Bank Locator
  168. //
  169. TokenToGet = BankLocator[Dimm];
  170. MemBankLocator = SmbiosMiscGetString (TokenToGet);
  171. MemBankLocatorStrLen = StrLen(MemBankLocator);
  172. if (MemBankLocatorStrLen > SMBIOS_STRING_MAX_LENGTH) {
  173. return EFI_UNSUPPORTED;
  174. }
  175. //
  176. // Memory Manufacturer
  177. //
  178. TokenToGet = STRING_TOKEN (STR_MISC_MEM_MANUFACTURER);
  179. MemManufacturer = SmbiosMiscGetString (TokenToGet);
  180. MemManufacturerStrLen = StrLen(MemManufacturer);
  181. if (MemManufacturerStrLen > SMBIOS_STRING_MAX_LENGTH) {
  182. return EFI_UNSUPPORTED;
  183. }
  184. //
  185. // Memory Serial Number
  186. //
  187. TokenToGet = STRING_TOKEN (STR_MISC_MEM_SERIAL_NO);
  188. MemSerialNumber = SmbiosMiscGetString (TokenToGet);
  189. MemSerialNumberStrLen = StrLen(MemSerialNumber);
  190. if (MemSerialNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
  191. return EFI_UNSUPPORTED;
  192. }
  193. //
  194. // Memory Asset Tag Number
  195. //
  196. TokenToGet = STRING_TOKEN (STR_MISC_MEM_ASSET_TAG);
  197. MemAssetTag = SmbiosMiscGetString (TokenToGet);
  198. MemAssetTagStrLen = StrLen(MemAssetTag);
  199. if (MemAssetTagStrLen > SMBIOS_STRING_MAX_LENGTH) {
  200. return EFI_UNSUPPORTED;
  201. }
  202. //
  203. // Memory Part Number
  204. //
  205. TokenToGet = STRING_TOKEN (STR_MISC_MEM_PART_NUMBER);
  206. MemPartNumber = SmbiosMiscGetString (TokenToGet);
  207. MemPartNumberStrLen = StrLen(MemPartNumber);
  208. if (MemPartNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
  209. return EFI_UNSUPPORTED;
  210. }
  211. //
  212. // Two zeros following the last string.
  213. //
  214. SmbiosRecord = AllocatePool(sizeof (SMBIOS_TABLE_TYPE17) + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1 + MemSerialNumberStrLen + 1 + MemAssetTagStrLen+1 + MemPartNumberStrLen + 1 + 1);
  215. ZeroMem(SmbiosRecord, sizeof (SMBIOS_TABLE_TYPE17) + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1 + MemSerialNumberStrLen + 1 + MemAssetTagStrLen+1 + MemPartNumberStrLen + 1 + 1);
  216. SmbiosRecord->Hdr.Type = EFI_SMBIOS_TYPE_MEMORY_DEVICE;
  217. SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE17);
  218. //
  219. // Make handle chosen by smbios protocol.add automatically.
  220. //
  221. SmbiosRecord->Hdr.Handle = 0;
  222. //
  223. // Memory Array Handle will be the 3rd optional string following the formatted structure.
  224. //
  225. SmbiosRecord->MemoryArrayHandle = Type16Handle;
  226. //
  227. // Memory Size
  228. //
  229. if ((MemInfoHob->MemInfoData.dimmSize[Dimm])!=0){
  230. SmbiosRecord->TotalWidth = 32;
  231. SmbiosRecord->DataWidth = 32;
  232. SmbiosRecord->Size = MemInfoHob->MemInfoData.dimmSize[Dimm];
  233. SmbiosRecord->Speed = DdrFreq;
  234. SmbiosRecord->ConfiguredMemoryClockSpeed = DdrFreq;
  235. SmbiosRecord->FormFactor = EfiMemoryFormFactorDimm;
  236. }
  237. SmbiosRecord->DeviceSet =(UINT8) ForType17InputData->MemoryDeviceSet;
  238. SmbiosRecord->DeviceLocator= 1;
  239. SmbiosRecord->BankLocator = 2;
  240. SmbiosRecord->Manufacturer = 3;
  241. SmbiosRecord->SerialNumber= 4;
  242. SmbiosRecord->AssetTag= 5;
  243. SmbiosRecord->PartNumber= 6;
  244. SmbiosRecord->Attributes = (UINT8) ForType17InputData->MemoryState;
  245. SmbiosRecord->MemoryType = MemoryType;
  246. OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
  247. UnicodeStrToAsciiStr(MemDevice, OptionalStrStart);
  248. UnicodeStrToAsciiStr(MemBankLocator, OptionalStrStart + MemDeviceStrLen + 1);
  249. UnicodeStrToAsciiStr(MemManufacturer, OptionalStrStart + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1);
  250. UnicodeStrToAsciiStr(MemSerialNumber, OptionalStrStart + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1);
  251. UnicodeStrToAsciiStr(MemAssetTag, OptionalStrStart + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1 + MemSerialNumberStrLen + 1);
  252. UnicodeStrToAsciiStr(MemPartNumber, OptionalStrStart + MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1 + MemSerialNumberStrLen + 1+ MemAssetTagStrLen+1 );
  253. //
  254. // Now we have got the full smbios record, call smbios protocol to add this record.
  255. //
  256. SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
  257. Status = Smbios-> Add(
  258. Smbios,
  259. NULL,
  260. &SmbiosHandle,
  261. (EFI_SMBIOS_TABLE_HEADER *) SmbiosRecord
  262. );
  263. if ((SmbiosRecord->Size & BIT15) != 0) {
  264. //
  265. // Size is in KB
  266. //
  267. TotalMemorySize = TotalMemorySize + LShiftU64 (SmbiosRecord->Size, 10);
  268. } else {
  269. //
  270. // Size is in MB
  271. //
  272. TotalMemorySize = TotalMemorySize + LShiftU64 (SmbiosRecord->Size, 20);
  273. }
  274. FreePool(SmbiosRecord);
  275. }
  276. //
  277. // Allocate and zero SMBIOS TYPE 19 Record
  278. //
  279. SmbiosRecord19 = AllocateZeroPool (sizeof (SMBIOS_TABLE_TYPE19));
  280. ASSERT (SmbiosRecord19 != NULL);
  281. //
  282. // Fill in SMBIOS type 19 information
  283. //
  284. SmbiosRecord19->Hdr.Type = SMBIOS_TYPE_MEMORY_ARRAY_MAPPED_ADDRESS;
  285. SmbiosRecord19->Hdr.Length = (UINT8) sizeof (SMBIOS_TABLE_TYPE19);
  286. SmbiosRecord19->Hdr.Handle = 0;
  287. SmbiosRecord19->MemoryArrayHandle = Type16Handle;
  288. SmbiosRecord19->PartitionWidth = NumSlots;
  289. if (TotalMemorySize <= SIZE_4TB) {
  290. SmbiosRecord19->StartingAddress = 0x0;
  291. //
  292. // Convert bytes to KB
  293. //
  294. SmbiosRecord19->EndingAddress = (UINT32)RShiftU64 (TotalMemorySize, 10) - 1;
  295. SmbiosRecord19->ExtendedStartingAddress = 0;
  296. SmbiosRecord19->ExtendedEndingAddress = 0;
  297. } else {
  298. SmbiosRecord19->StartingAddress = 0xffffffff;
  299. SmbiosRecord19->EndingAddress = 0xffffffff;
  300. SmbiosRecord19->ExtendedStartingAddress = 0;
  301. SmbiosRecord19->ExtendedEndingAddress = TotalMemorySize - 1;
  302. }
  303. //
  304. // Add SMBIOS type 19 record to SMBIOS table.
  305. //
  306. SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
  307. Smbios->Add(
  308. Smbios,
  309. NULL,
  310. &SmbiosHandle,
  311. (EFI_SMBIOS_TABLE_HEADER *)SmbiosRecord19
  312. );
  313. return Status;
  314. }