MiscMemoryDeviceFunction.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*++
  2. Copyright (c) 2006 - 2020, 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 OptionalStrSize;
  64. UINTN MemDeviceStrLen;
  65. UINTN MemBankLocatorStrLen;
  66. UINTN MemManufacturerStrLen;
  67. UINTN MemSerialNumberStrLen;
  68. UINTN MemAssetTagStrLen;
  69. UINTN MemPartNumberStrLen;
  70. CHAR16 *MemDevice;
  71. CHAR16 *MemBankLocator;
  72. CHAR16 *MemManufacturer;
  73. CHAR16 *MemSerialNumber;
  74. CHAR16 *MemAssetTag;
  75. CHAR16 *MemPartNumber;
  76. EFI_STATUS Status;
  77. STRING_REF TokenToGet;
  78. SMBIOS_TABLE_TYPE17 *SmbiosRecord;
  79. SMBIOS_TABLE_TYPE19 *SmbiosRecord19;
  80. EFI_SMBIOS_HANDLE SmbiosHandle;
  81. EFI_MEMORY_ARRAY_LINK_DATA *ForType17InputData;
  82. UINT16 DdrFreq=0;
  83. UINT16 Type16Handle=0;
  84. MEM_INFO_PROTOCOL *MemInfoHob;
  85. UINT8 MemoryType;
  86. UINT8 Dimm;
  87. UINT8 NumSlots;
  88. UINT64 TotalMemorySize;
  89. STRING_REF DevLocator[] = {
  90. STRING_TOKEN(STR_MISC_MEM_DEV_LOCATOR0), STRING_TOKEN(STR_MISC_MEM_DEV_LOCATOR1)
  91. };
  92. STRING_REF BankLocator[] = {
  93. STRING_TOKEN(STR_MISC_MEM_BANK_LOCATOR0), STRING_TOKEN(STR_MISC_MEM_BANK_LOCATOR1)
  94. };
  95. //
  96. // First check for invalid parameters.
  97. //
  98. if (RecordData == NULL) {
  99. return EFI_INVALID_PARAMETER;
  100. }
  101. ForType17InputData = (EFI_MEMORY_ARRAY_LINK_DATA *)RecordData;
  102. //
  103. // Get Memory size parameters for each rank from the chipset registers
  104. //
  105. Status = gBS->LocateProtocol (
  106. &gMemInfoProtocolGuid,
  107. NULL,
  108. (void **)&MemInfoHob
  109. );
  110. ASSERT_EFI_ERROR (Status);
  111. NumSlots = (UINT8)(MAX_SOCKETS);
  112. //
  113. // Memory Freq
  114. //
  115. switch (MemInfoHob->MemInfoData.ddrFreq){
  116. case FREQ_800:
  117. DdrFreq = 800;
  118. break;
  119. case FREQ_1066:
  120. DdrFreq = 1066;
  121. break;
  122. case FREQ_1333:
  123. DdrFreq = 1333;
  124. break;
  125. case FREQ_1600:
  126. DdrFreq = 1600;
  127. break;
  128. default:
  129. DdrFreq = 0;
  130. break;
  131. }
  132. //
  133. // Memory Type
  134. //
  135. switch (MemInfoHob->MemInfoData.ddrType) {
  136. case DDRType_LPDDR2:
  137. MemoryType = EfiMemoryTypeDdr2;
  138. break;
  139. case DDRType_DDR3:
  140. case DDRType_DDR3L:
  141. case DDRType_DDR3U:
  142. case DDRType_LPDDR3:
  143. MemoryType = EfiMemoryTypeDdr3;
  144. break;
  145. default:
  146. MemoryType = EfiMemoryTypeUnknown;
  147. break;
  148. }
  149. GetType16Hndl( Smbios, &Type16Handle);
  150. TotalMemorySize = 0;
  151. for (Dimm = 0; Dimm < NumSlots; Dimm++) {
  152. //
  153. // Memory Device Locator
  154. //
  155. TokenToGet = DevLocator[Dimm];
  156. MemDevice = SmbiosMiscGetString (TokenToGet);
  157. MemDeviceStrLen = StrLen(MemDevice);
  158. if (MemDeviceStrLen > SMBIOS_STRING_MAX_LENGTH) {
  159. return EFI_UNSUPPORTED;
  160. }
  161. TokenToGet = DevLocator[Dimm];
  162. MemDevice = SmbiosMiscGetString (TokenToGet);
  163. MemDeviceStrLen = StrLen(MemDevice);
  164. if (MemDeviceStrLen > SMBIOS_STRING_MAX_LENGTH) {
  165. return EFI_UNSUPPORTED;
  166. }
  167. //
  168. // Memory Bank Locator
  169. //
  170. TokenToGet = BankLocator[Dimm];
  171. MemBankLocator = SmbiosMiscGetString (TokenToGet);
  172. MemBankLocatorStrLen = StrLen(MemBankLocator);
  173. if (MemBankLocatorStrLen > SMBIOS_STRING_MAX_LENGTH) {
  174. return EFI_UNSUPPORTED;
  175. }
  176. //
  177. // Memory Manufacturer
  178. //
  179. TokenToGet = STRING_TOKEN (STR_MISC_MEM_MANUFACTURER);
  180. MemManufacturer = SmbiosMiscGetString (TokenToGet);
  181. MemManufacturerStrLen = StrLen(MemManufacturer);
  182. if (MemManufacturerStrLen > SMBIOS_STRING_MAX_LENGTH) {
  183. return EFI_UNSUPPORTED;
  184. }
  185. //
  186. // Memory Serial Number
  187. //
  188. TokenToGet = STRING_TOKEN (STR_MISC_MEM_SERIAL_NO);
  189. MemSerialNumber = SmbiosMiscGetString (TokenToGet);
  190. MemSerialNumberStrLen = StrLen(MemSerialNumber);
  191. if (MemSerialNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
  192. return EFI_UNSUPPORTED;
  193. }
  194. //
  195. // Memory Asset Tag Number
  196. //
  197. TokenToGet = STRING_TOKEN (STR_MISC_MEM_ASSET_TAG);
  198. MemAssetTag = SmbiosMiscGetString (TokenToGet);
  199. MemAssetTagStrLen = StrLen(MemAssetTag);
  200. if (MemAssetTagStrLen > SMBIOS_STRING_MAX_LENGTH) {
  201. return EFI_UNSUPPORTED;
  202. }
  203. //
  204. // Memory Part Number
  205. //
  206. TokenToGet = STRING_TOKEN (STR_MISC_MEM_PART_NUMBER);
  207. MemPartNumber = SmbiosMiscGetString (TokenToGet);
  208. MemPartNumberStrLen = StrLen(MemPartNumber);
  209. if (MemPartNumberStrLen > SMBIOS_STRING_MAX_LENGTH) {
  210. return EFI_UNSUPPORTED;
  211. }
  212. //
  213. // Two zeros following the last string.
  214. //
  215. OptionalStrSize = MemDeviceStrLen + 1 + MemBankLocatorStrLen + 1 + MemManufacturerStrLen + 1 + MemSerialNumberStrLen + 1 + MemAssetTagStrLen+1 + MemPartNumberStrLen + 1 + 1;
  216. SmbiosRecord = AllocatePool(sizeof (SMBIOS_TABLE_TYPE17) + OptionalStrSize);
  217. ZeroMem(SmbiosRecord, sizeof (SMBIOS_TABLE_TYPE17) + OptionalStrSize);
  218. SmbiosRecord->Hdr.Type = EFI_SMBIOS_TYPE_MEMORY_DEVICE;
  219. SmbiosRecord->Hdr.Length = sizeof (SMBIOS_TABLE_TYPE17);
  220. //
  221. // Make handle chosen by smbios protocol.add automatically.
  222. //
  223. SmbiosRecord->Hdr.Handle = 0;
  224. //
  225. // Memory Array Handle will be the 3rd optional string following the formatted structure.
  226. //
  227. SmbiosRecord->MemoryArrayHandle = Type16Handle;
  228. //
  229. // Memory Size
  230. //
  231. if ((MemInfoHob->MemInfoData.dimmSize[Dimm])!=0){
  232. SmbiosRecord->TotalWidth = 32;
  233. SmbiosRecord->DataWidth = 32;
  234. SmbiosRecord->Size = MemInfoHob->MemInfoData.dimmSize[Dimm];
  235. SmbiosRecord->Speed = DdrFreq;
  236. SmbiosRecord->ConfiguredMemoryClockSpeed = DdrFreq;
  237. SmbiosRecord->FormFactor = EfiMemoryFormFactorDimm;
  238. }
  239. SmbiosRecord->DeviceSet =(UINT8) ForType17InputData->MemoryDeviceSet;
  240. SmbiosRecord->DeviceLocator= 1;
  241. SmbiosRecord->BankLocator = 2;
  242. SmbiosRecord->Manufacturer = 3;
  243. SmbiosRecord->SerialNumber= 4;
  244. SmbiosRecord->AssetTag= 5;
  245. SmbiosRecord->PartNumber= 6;
  246. SmbiosRecord->Attributes = (UINT8) ForType17InputData->MemoryState;
  247. SmbiosRecord->MemoryType = MemoryType;
  248. OptionalStrStart = (CHAR8 *)(SmbiosRecord + 1);
  249. UnicodeStrToAsciiStrS (MemDevice, OptionalStrStart, OptionalStrSize);
  250. OptionalStrStart += (MemDeviceStrLen + 1);
  251. OptionalStrSize -= (MemDeviceStrLen + 1);
  252. UnicodeStrToAsciiStrS (MemBankLocator, OptionalStrStart, OptionalStrSize);
  253. OptionalStrStart += (MemBankLocatorStrLen + 1);
  254. OptionalStrSize -= (MemBankLocatorStrLen + 1);
  255. UnicodeStrToAsciiStrS (MemManufacturer, OptionalStrStart, OptionalStrSize);
  256. OptionalStrStart += (MemManufacturerStrLen + 1);
  257. OptionalStrSize -= (MemManufacturerStrLen + 1);
  258. UnicodeStrToAsciiStrS (MemSerialNumber, OptionalStrStart, OptionalStrSize);
  259. OptionalStrStart += (MemSerialNumberStrLen + 1);
  260. OptionalStrSize -= (MemSerialNumberStrLen + 1);
  261. UnicodeStrToAsciiStrS (MemAssetTag, OptionalStrStart, OptionalStrSize);
  262. OptionalStrStart += (MemAssetTagStrLen + 1);
  263. OptionalStrSize -= (MemAssetTagStrLen + 1);
  264. UnicodeStrToAsciiStrS (MemPartNumber, OptionalStrStart, OptionalStrSize);
  265. //
  266. // Now we have got the full smbios record, call smbios protocol to add this record.
  267. //
  268. SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
  269. Status = Smbios-> Add(
  270. Smbios,
  271. NULL,
  272. &SmbiosHandle,
  273. (EFI_SMBIOS_TABLE_HEADER *) SmbiosRecord
  274. );
  275. if ((SmbiosRecord->Size & BIT15) != 0) {
  276. //
  277. // Size is in KB
  278. //
  279. TotalMemorySize = TotalMemorySize + LShiftU64 (SmbiosRecord->Size, 10);
  280. } else {
  281. //
  282. // Size is in MB
  283. //
  284. TotalMemorySize = TotalMemorySize + LShiftU64 (SmbiosRecord->Size, 20);
  285. }
  286. FreePool(SmbiosRecord);
  287. }
  288. //
  289. // Allocate and zero SMBIOS TYPE 19 Record
  290. //
  291. SmbiosRecord19 = AllocateZeroPool (sizeof (SMBIOS_TABLE_TYPE19));
  292. ASSERT (SmbiosRecord19 != NULL);
  293. //
  294. // Fill in SMBIOS type 19 information
  295. //
  296. SmbiosRecord19->Hdr.Type = SMBIOS_TYPE_MEMORY_ARRAY_MAPPED_ADDRESS;
  297. SmbiosRecord19->Hdr.Length = (UINT8) sizeof (SMBIOS_TABLE_TYPE19);
  298. SmbiosRecord19->Hdr.Handle = 0;
  299. SmbiosRecord19->MemoryArrayHandle = Type16Handle;
  300. SmbiosRecord19->PartitionWidth = NumSlots;
  301. if (TotalMemorySize <= SIZE_4TB) {
  302. SmbiosRecord19->StartingAddress = 0x0;
  303. //
  304. // Convert bytes to KB
  305. //
  306. SmbiosRecord19->EndingAddress = (UINT32)RShiftU64 (TotalMemorySize, 10) - 1;
  307. SmbiosRecord19->ExtendedStartingAddress = 0;
  308. SmbiosRecord19->ExtendedEndingAddress = 0;
  309. } else {
  310. SmbiosRecord19->StartingAddress = 0xffffffff;
  311. SmbiosRecord19->EndingAddress = 0xffffffff;
  312. SmbiosRecord19->ExtendedStartingAddress = 0;
  313. SmbiosRecord19->ExtendedEndingAddress = TotalMemorySize - 1;
  314. }
  315. //
  316. // Add SMBIOS type 19 record to SMBIOS table.
  317. //
  318. SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
  319. Smbios->Add(
  320. Smbios,
  321. NULL,
  322. &SmbiosHandle,
  323. (EFI_SMBIOS_TABLE_HEADER *)SmbiosRecord19
  324. );
  325. return Status;
  326. }