AcpiPlatform.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /** @file
  2. ACPI Platform Driver
  3. Copyright (c) 2019 Intel Corporation. All rights reserved. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "AcpiPlatform.h"
  7. #define MAX_CPU_NUM (FixedPcdGet32(PcdMaxCpuThreadCount) * FixedPcdGet32(PcdMaxCpuCoreCount) * FixedPcdGet32(PcdMaxCpuSocketCount))
  8. #pragma pack(1)
  9. typedef struct {
  10. UINT32 AcpiProcessorId;
  11. UINT32 ApicId;
  12. UINT32 Flags;
  13. UINT32 SwProcApicId;
  14. UINT32 SocketNum;
  15. } EFI_CPU_ID_ORDER_MAP;
  16. //
  17. // Private Driver Data
  18. //
  19. //
  20. // Define Union of IO APIC & Local APIC structure;
  21. //
  22. typedef union {
  23. EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE AcpiLocalApic;
  24. EFI_ACPI_4_0_IO_APIC_STRUCTURE AcpiIoApic;
  25. EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE AcpiLocalx2Apic;
  26. struct {
  27. UINT8 Type;
  28. UINT8 Length;
  29. } AcpiApicCommon;
  30. } ACPI_APIC_STRUCTURE_PTR;
  31. #pragma pack()
  32. extern EFI_ACPI_5_0_FIRMWARE_ACPI_CONTROL_STRUCTURE Facs;
  33. extern EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE Fadt;
  34. extern EFI_ACPI_HIGH_PRECISION_EVENT_TIMER_TABLE_HEADER Hpet;
  35. extern EFI_ACPI_WSMT_TABLE Wsmt;
  36. VOID *mLocalTable[] = {
  37. &Facs,
  38. &Fadt,
  39. &Hpet,
  40. &Wsmt,
  41. };
  42. EFI_ACPI_TABLE_PROTOCOL *mAcpiTable;
  43. UINT32 mNumOfBitShift = 6;
  44. BOOLEAN mForceX2ApicId;
  45. BOOLEAN mX2ApicEnabled;
  46. EFI_MP_SERVICES_PROTOCOL *mMpService;
  47. BOOLEAN mCpuOrderSorted;
  48. EFI_CPU_ID_ORDER_MAP mCpuApicIdOrderTable[MAX_CPU_NUM];
  49. UINTN mNumberOfCPUs = 0;
  50. UINTN mNumberOfEnabledCPUs = 0;
  51. //
  52. // following are possible APICID Map for SKX
  53. //
  54. static const UINT32 ApicIdMapA[] = { //for SKUs have number of core > 16
  55. //it is 14 + 14 + 14 + 14 format
  56. 0x00000000, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
  57. 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x00000010, 0x00000011,
  58. 0x00000012, 0x00000013, 0x00000014, 0x00000015, 0x00000016, 0x00000017, 0x00000018, 0x00000019,
  59. 0x0000001A, 0x0000001B, 0x0000001C, 0x0000001D, 0x00000020, 0x00000021, 0x00000022, 0x00000023,
  60. 0x00000024, 0x00000025, 0x00000026, 0x00000027, 0x00000028, 0x00000029, 0x0000002A, 0x0000002B,
  61. 0x0000002C, 0x0000002D, 0x00000030, 0x00000031, 0x00000032, 0x00000033, 0x00000034, 0x00000035,
  62. 0x00000036, 0x00000037, 0x00000038, 0x00000039, 0x0000003A, 0x0000003B, 0x0000003C, 0x0000003D
  63. };
  64. static const UINT32 ApicIdMapB[] = { //for SKUs have number of cores <= 16 use 32 ID space
  65. //
  66. //it is 16+16 format
  67. //
  68. 0x00000000, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
  69. 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E, 0x0000000F,
  70. 0x00000010, 0x00000011, 0x00000012, 0x00000013, 0x00000014, 0x00000015, 0x00000016, 0x00000017,
  71. 0x00000018, 0x00000019, 0x0000001A, 0x0000001B, 0x0000001C, 0x0000001D, 0x0000001E, 0x0000001F,
  72. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  73. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  74. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  75. };
  76. static const UINT32 ApicIdMapC[] = { //for SKUs have number of cores <= 16 use 64 ID space
  77. //
  78. //it is 16+0+16+0 format
  79. //
  80. 0x00000000, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
  81. 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E, 0x0000000F,
  82. 0x00000020, 0x00000021, 0x00000022, 0x00000023, 0x00000024, 0x00000025, 0x00000026, 0x00000027,
  83. 0x00000028, 0x00000029, 0x0000002A, 0x0000002B, 0x0000002C, 0x0000002D, 0x0000002E, 0x0000002F,
  84. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  85. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  86. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  87. };
  88. static const UINT32 ApicIdMapD[] = { //for SKUs have number of cores <= 8 use 16 ID space
  89. //
  90. //it is 16 format
  91. //
  92. 0x00000000, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
  93. 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E, 0x0000000F,
  94. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  95. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  96. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  97. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  98. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  99. };
  100. const UINT32 *mApicIdMap = NULL;
  101. /**
  102. This function detect the APICID map and update ApicID Map pointer
  103. @param None
  104. @retval VOID
  105. **/
  106. VOID DetectApicIdMap(VOID)
  107. {
  108. UINTN CoreCount;
  109. CoreCount = 0;
  110. if(mApicIdMap != NULL) {
  111. return; //aleady initialized
  112. }
  113. mApicIdMap = ApicIdMapA; // default to > 16C SKUs
  114. CoreCount = mNumberOfEnabledCPUs / 2;
  115. DEBUG ((DEBUG_INFO, "CoreCount - %d\n", CoreCount));
  116. if(CoreCount <= 16) {
  117. if(mNumOfBitShift == 4) {
  118. mApicIdMap = ApicIdMapD;
  119. }
  120. if(mNumOfBitShift == 5) {
  121. mApicIdMap = ApicIdMapB;
  122. }
  123. if(mNumOfBitShift == 6) {
  124. mApicIdMap = ApicIdMapC;
  125. }
  126. }
  127. return;
  128. }
  129. /**
  130. This function return the CoreThreadId of ApicId from ACPI ApicId Map array
  131. @param ApicId
  132. @retval Index of ACPI ApicId Map array
  133. **/
  134. UINT32
  135. GetIndexFromApicId (
  136. UINT32 ApicId
  137. )
  138. {
  139. UINT32 CoreThreadId;
  140. UINT32 i;
  141. ASSERT (mApicIdMap != NULL);
  142. CoreThreadId = ApicId & ((1 << mNumOfBitShift) - 1);
  143. for(i = 0; i < (FixedPcdGet32(PcdMaxCpuCoreCount) * FixedPcdGet32(PcdMaxCpuThreadCount)); i++) {
  144. if(mApicIdMap[i] == CoreThreadId) {
  145. break;
  146. }
  147. }
  148. ASSERT (i <= (FixedPcdGet32(PcdMaxCpuCoreCount) * FixedPcdGet32(PcdMaxCpuThreadCount)));
  149. return i;
  150. }
  151. UINT32
  152. ApicId2SwProcApicId (
  153. UINT32 ApicId
  154. )
  155. {
  156. UINT32 Index;
  157. for (Index = 0; Index < MAX_CPU_NUM; Index++) {
  158. if ((mCpuApicIdOrderTable[Index].Flags == 1) && (mCpuApicIdOrderTable[Index].ApicId == ApicId)) {
  159. return Index;
  160. }
  161. }
  162. return (UINT32) -1;
  163. }
  164. VOID
  165. DebugDisplayReOrderTable(
  166. VOID
  167. )
  168. {
  169. UINT32 Index;
  170. DEBUG ((EFI_D_ERROR, "Index AcpiProcId ApicId Flags SwApicId Skt\n"));
  171. for (Index=0; Index<MAX_CPU_NUM; Index++) {
  172. DEBUG ((EFI_D_ERROR, " %02d 0x%02X 0x%02X %d 0x%02X %d\n",
  173. Index, mCpuApicIdOrderTable[Index].AcpiProcessorId,
  174. mCpuApicIdOrderTable[Index].ApicId,
  175. mCpuApicIdOrderTable[Index].Flags,
  176. mCpuApicIdOrderTable[Index].SwProcApicId,
  177. mCpuApicIdOrderTable[Index].SocketNum));
  178. }
  179. }
  180. EFI_STATUS
  181. AppendCpuMapTableEntry (
  182. IN VOID *ApicPtr,
  183. IN UINT32 LocalApicCounter
  184. )
  185. {
  186. EFI_STATUS Status;
  187. EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE *LocalApicPtr;
  188. EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE *LocalX2ApicPtr;
  189. UINT8 Type;
  190. Status = EFI_SUCCESS;
  191. Type = ((ACPI_APIC_STRUCTURE_PTR *)ApicPtr)->AcpiApicCommon.Type;
  192. LocalApicPtr = (EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE *)(&((ACPI_APIC_STRUCTURE_PTR *)ApicPtr)->AcpiLocalApic);
  193. LocalX2ApicPtr = (EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE *)(&((ACPI_APIC_STRUCTURE_PTR *)ApicPtr)->AcpiLocalx2Apic);
  194. if(Type == EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC) {
  195. if(!mX2ApicEnabled) {
  196. LocalApicPtr->Flags = (UINT8)mCpuApicIdOrderTable[LocalApicCounter].Flags;
  197. LocalApicPtr->ApicId = (UINT8)mCpuApicIdOrderTable[LocalApicCounter].ApicId;
  198. LocalApicPtr->AcpiProcessorId = (UINT8)mCpuApicIdOrderTable[LocalApicCounter].AcpiProcessorId;
  199. } else {
  200. LocalApicPtr->Flags = 0;
  201. LocalApicPtr->ApicId = 0xFF;
  202. LocalApicPtr->AcpiProcessorId = (UINT8)0xFF;
  203. Status = EFI_UNSUPPORTED;
  204. }
  205. } else if(Type == EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC) {
  206. if(mX2ApicEnabled) {
  207. LocalX2ApicPtr->Flags = (UINT8)mCpuApicIdOrderTable[LocalApicCounter].Flags;
  208. LocalX2ApicPtr->X2ApicId = mCpuApicIdOrderTable[LocalApicCounter].ApicId;
  209. LocalX2ApicPtr->AcpiProcessorUid = mCpuApicIdOrderTable[LocalApicCounter].AcpiProcessorId;
  210. } else {
  211. LocalX2ApicPtr->Flags = 0;
  212. LocalX2ApicPtr->X2ApicId = (UINT32)-1;
  213. LocalX2ApicPtr->AcpiProcessorUid = (UINT32)-1;
  214. Status = EFI_UNSUPPORTED;
  215. }
  216. } else {
  217. Status = EFI_UNSUPPORTED;
  218. }
  219. return Status;
  220. }
  221. EFI_STATUS
  222. SortCpuLocalApicInTable (
  223. VOID
  224. )
  225. {
  226. EFI_STATUS Status;
  227. EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;
  228. UINT32 Index;
  229. UINT32 CurrProcessor;
  230. UINT32 BspApicId;
  231. UINT32 TempVal = 0;
  232. EFI_CPU_ID_ORDER_MAP *CpuIdMapPtr;
  233. UINT32 CoreThreadMask;
  234. Index = 0;
  235. Status = EFI_SUCCESS;
  236. CoreThreadMask = (UINT32) ((1 << mNumOfBitShift) - 1);
  237. if(!mCpuOrderSorted) {
  238. Index = 0;
  239. for (CurrProcessor = 0; CurrProcessor < mNumberOfCPUs; CurrProcessor++) {
  240. Status = mMpService->GetProcessorInfo (
  241. mMpService,
  242. CurrProcessor,
  243. &ProcessorInfoBuffer
  244. );
  245. if ((ProcessorInfoBuffer.StatusFlag & PROCESSOR_ENABLED_BIT) != 0) {
  246. if(ProcessorInfoBuffer.ProcessorId & 1) { //is 2nd thread
  247. CpuIdMapPtr = (EFI_CPU_ID_ORDER_MAP *)&mCpuApicIdOrderTable[(Index - 1) + MAX_CPU_NUM / 2];
  248. } else { //is primary thread
  249. CpuIdMapPtr = (EFI_CPU_ID_ORDER_MAP *)&mCpuApicIdOrderTable[Index];
  250. Index++;
  251. }
  252. CpuIdMapPtr->ApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;
  253. CpuIdMapPtr->Flags = ((ProcessorInfoBuffer.StatusFlag & PROCESSOR_ENABLED_BIT) != 0);
  254. CpuIdMapPtr->SocketNum = (UINT32)ProcessorInfoBuffer.Location.Package;
  255. CpuIdMapPtr->AcpiProcessorId = (CpuIdMapPtr->SocketNum * FixedPcdGet32(PcdMaxCpuCoreCount) * FixedPcdGet32(PcdMaxCpuThreadCount)) + GetIndexFromApicId(CpuIdMapPtr->ApicId); //CpuIdMapPtr->ApicId;
  256. CpuIdMapPtr->SwProcApicId = ((UINT32)(ProcessorInfoBuffer.Location.Package << mNumOfBitShift) + (((UINT32)ProcessorInfoBuffer.ProcessorId) & CoreThreadMask));
  257. if(mX2ApicEnabled) { //if X2Apic, re-order the socket # so it starts from base 0 and contiguous
  258. //may not necessory!!!!!
  259. }
  260. //update processorbitMask
  261. if (CpuIdMapPtr->Flags == 1) {
  262. if(mForceX2ApicId) {
  263. CpuIdMapPtr->SocketNum &= 0x7;
  264. CpuIdMapPtr->AcpiProcessorId &= 0xFF; //keep lower 8bit due to use Proc obj in dsdt
  265. CpuIdMapPtr->SwProcApicId &= 0xFF;
  266. }
  267. }
  268. } else { //not enabled
  269. CpuIdMapPtr = (EFI_CPU_ID_ORDER_MAP *)&mCpuApicIdOrderTable[Index];
  270. CpuIdMapPtr->ApicId = (UINT32)-1;
  271. CpuIdMapPtr->Flags = 0;
  272. CpuIdMapPtr->AcpiProcessorId = (UINT32)-1;
  273. CpuIdMapPtr->SwProcApicId = (UINT32)-1;
  274. CpuIdMapPtr->SocketNum = (UINT32)-1;
  275. } //end if PROC ENABLE
  276. } //end for CurrentProcessor
  277. //
  278. //keep for debug purpose
  279. //
  280. DEBUG(( EFI_D_ERROR, "::ACPI:: APIC ID Order Table Init. CoreThreadMask = %x, mNumOfBitShift = %x\n", CoreThreadMask, mNumOfBitShift));
  281. DebugDisplayReOrderTable();
  282. //
  283. //make sure 1st entry is BSP
  284. //
  285. if(mX2ApicEnabled) {
  286. BspApicId = (UINT32)AsmReadMsr64(0x802);
  287. } else {
  288. BspApicId = (*(volatile UINT32 *)(UINTN)0xFEE00020) >> 24;
  289. }
  290. DEBUG ((EFI_D_INFO, "BspApicId - 0x%x\n", BspApicId));
  291. if(mCpuApicIdOrderTable[0].ApicId != BspApicId) {
  292. //
  293. //check to see if 1st entry is BSP, if not swap it
  294. //
  295. Index = ApicId2SwProcApicId(BspApicId);
  296. if(MAX_CPU_NUM <= Index) {
  297. DEBUG ((EFI_D_ERROR, "Asserting the SortCpuLocalApicInTable Index Bufferflow\n"));
  298. ASSERT_EFI_ERROR(EFI_INVALID_PARAMETER);
  299. }
  300. TempVal = mCpuApicIdOrderTable[Index].ApicId;
  301. mCpuApicIdOrderTable[Index].ApicId = mCpuApicIdOrderTable[0].ApicId;
  302. mCpuApicIdOrderTable[0].ApicId = TempVal;
  303. mCpuApicIdOrderTable[Index].Flags = mCpuApicIdOrderTable[0].Flags;
  304. mCpuApicIdOrderTable[0].Flags = 1;
  305. TempVal = mCpuApicIdOrderTable[Index].SwProcApicId;
  306. mCpuApicIdOrderTable[Index].SwProcApicId = mCpuApicIdOrderTable[0].SwProcApicId;
  307. mCpuApicIdOrderTable[0].SwProcApicId = TempVal;
  308. //
  309. //swap AcpiProcId
  310. //
  311. TempVal = mCpuApicIdOrderTable[Index].AcpiProcessorId;
  312. mCpuApicIdOrderTable[Index].AcpiProcessorId = mCpuApicIdOrderTable[0].AcpiProcessorId;
  313. mCpuApicIdOrderTable[0].AcpiProcessorId = TempVal;
  314. }
  315. //
  316. //Make sure no holes between enabled threads
  317. //
  318. for(CurrProcessor = 0; CurrProcessor < MAX_CPU_NUM; CurrProcessor++) {
  319. if(mCpuApicIdOrderTable[CurrProcessor].Flags == 0) {
  320. //
  321. //make sure disabled entry has ProcId set to FFs
  322. //
  323. mCpuApicIdOrderTable[CurrProcessor].ApicId = (UINT32)-1;
  324. mCpuApicIdOrderTable[CurrProcessor].AcpiProcessorId = (UINT32)-1;
  325. mCpuApicIdOrderTable[CurrProcessor].SwProcApicId = (UINT32)-1;
  326. for(Index = CurrProcessor+1; Index < MAX_CPU_NUM; Index++) {
  327. if(mCpuApicIdOrderTable[Index].Flags == 1) {
  328. //
  329. //move enabled entry up
  330. //
  331. mCpuApicIdOrderTable[CurrProcessor].Flags = 1;
  332. mCpuApicIdOrderTable[CurrProcessor].ApicId = mCpuApicIdOrderTable[Index].ApicId;
  333. mCpuApicIdOrderTable[CurrProcessor].AcpiProcessorId = mCpuApicIdOrderTable[Index].AcpiProcessorId;
  334. mCpuApicIdOrderTable[CurrProcessor].SwProcApicId = mCpuApicIdOrderTable[Index].SwProcApicId;
  335. mCpuApicIdOrderTable[CurrProcessor].SocketNum = mCpuApicIdOrderTable[Index].SocketNum;
  336. //
  337. //disable moved entry
  338. //
  339. mCpuApicIdOrderTable[Index].Flags = 0;
  340. mCpuApicIdOrderTable[Index].ApicId = (UINT32)-1;
  341. mCpuApicIdOrderTable[Index].AcpiProcessorId = (UINT32)-1;
  342. mCpuApicIdOrderTable[Index].SwProcApicId = (UINT32)-1;
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. //
  349. //keep for debug purpose
  350. //
  351. DEBUG ((EFI_D_ERROR, "APIC ID Order Table ReOrdered\n"));
  352. DebugDisplayReOrderTable();
  353. mCpuOrderSorted = TRUE;
  354. }
  355. return Status;
  356. }
  357. /** Structure of a sub-structure of the ACPI header.
  358. This structure contains the type and length fields, which are common to every
  359. sub-structure of the ACPI tables. A pointer to any structure can be cast as this.
  360. **/
  361. typedef struct {
  362. UINT8 Type;
  363. UINT8 Length;
  364. } STRUCTURE_HEADER;
  365. STRUCTURE_HEADER mMadtStructureTable[] = {
  366. {EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC, sizeof (EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE)},
  367. {EFI_ACPI_4_0_IO_APIC, sizeof (EFI_ACPI_4_0_IO_APIC_STRUCTURE)},
  368. {EFI_ACPI_4_0_INTERRUPT_SOURCE_OVERRIDE, sizeof (EFI_ACPI_4_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE)},
  369. {EFI_ACPI_4_0_NON_MASKABLE_INTERRUPT_SOURCE, sizeof (EFI_ACPI_4_0_NON_MASKABLE_INTERRUPT_SOURCE_STRUCTURE)},
  370. {EFI_ACPI_4_0_LOCAL_APIC_NMI, sizeof (EFI_ACPI_4_0_LOCAL_APIC_NMI_STRUCTURE)},
  371. {EFI_ACPI_4_0_LOCAL_APIC_ADDRESS_OVERRIDE, sizeof (EFI_ACPI_4_0_LOCAL_APIC_ADDRESS_OVERRIDE_STRUCTURE)},
  372. {EFI_ACPI_4_0_IO_SAPIC, sizeof (EFI_ACPI_4_0_IO_SAPIC_STRUCTURE)},
  373. {EFI_ACPI_4_0_LOCAL_SAPIC, sizeof (EFI_ACPI_4_0_PROCESSOR_LOCAL_SAPIC_STRUCTURE)},
  374. {EFI_ACPI_4_0_PLATFORM_INTERRUPT_SOURCES, sizeof (EFI_ACPI_4_0_PLATFORM_INTERRUPT_SOURCES_STRUCTURE)},
  375. {EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC, sizeof (EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE)},
  376. {EFI_ACPI_4_0_LOCAL_X2APIC_NMI, sizeof (EFI_ACPI_4_0_LOCAL_X2APIC_NMI_STRUCTURE)}
  377. };
  378. /**
  379. Get the size of the ACPI table.
  380. This function calculates the size needed for the ACPI Table based on the number and
  381. size of the sub-structures that will compose it.
  382. @param[in] TableSpecificHdrLength Size of the table specific header, not the ACPI standard header size.
  383. @param[in] Structures Pointer to an array of sub-structure pointers.
  384. @param[in] StructureCount Number of structure pointers in the array.
  385. @return Total size needed for the ACPI table.
  386. **/
  387. UINT32
  388. GetTableSize (
  389. IN UINTN TableSpecificHdrLength,
  390. IN STRUCTURE_HEADER **Structures,
  391. IN UINTN StructureCount
  392. )
  393. {
  394. UINT32 TableLength;
  395. UINT32 Index;
  396. //
  397. // Compute size of the ACPI table; header plus all structures needed.
  398. //
  399. TableLength = (UINT32) TableSpecificHdrLength;
  400. for (Index = 0; Index < StructureCount; Index++) {
  401. ASSERT (Structures[Index] != NULL);
  402. if (Structures[Index] == NULL) {
  403. return 0;
  404. }
  405. TableLength += Structures[Index]->Length;
  406. }
  407. return TableLength;
  408. }
  409. /**
  410. Allocate the ACPI Table.
  411. This function allocates space for the ACPI table based on the number and size of
  412. the sub-structures that will compose it.
  413. @param[in] TableSpecificHdrLength Size of the table specific header, not the ACPI standard header size.
  414. @param[in] Structures Pointer to an array of sub-structure pointers.
  415. @param[in] StructureCount Number of structure pointers in the array.
  416. @param[out] Table Newly allocated ACPI Table pointer.
  417. @retval EFI_SUCCESS Successfully allocated the Table.
  418. @retval EFI_OUT_OF_RESOURCES Space for the Table could not be allocated.
  419. **/
  420. EFI_STATUS
  421. AllocateTable (
  422. IN UINTN TableSpecificHdrLength,
  423. IN STRUCTURE_HEADER **Structures,
  424. IN UINTN StructureCount,
  425. OUT EFI_ACPI_DESCRIPTION_HEADER **Table
  426. )
  427. {
  428. EFI_STATUS Status;
  429. UINT32 Size;
  430. EFI_ACPI_DESCRIPTION_HEADER *InternalTable;
  431. //
  432. // Get the size of the ACPI table and allocate memory.
  433. //
  434. Size = GetTableSize (TableSpecificHdrLength, Structures, StructureCount);
  435. InternalTable = (EFI_ACPI_DESCRIPTION_HEADER *) AllocatePool (Size);
  436. if (InternalTable == NULL) {
  437. Status = EFI_OUT_OF_RESOURCES;
  438. DEBUG ((
  439. DEBUG_ERROR,
  440. "Failed to allocate %d bytes for ACPI Table\n",
  441. Size
  442. ));
  443. } else {
  444. Status = EFI_SUCCESS;
  445. DEBUG ((
  446. DEBUG_INFO,
  447. "Successfully allocated %d bytes for ACPI Table at 0x%p\n",
  448. Size,
  449. InternalTable
  450. ));
  451. *Table = InternalTable;
  452. }
  453. return Status;
  454. }
  455. /**
  456. Initialize the header.
  457. This function fills in the standard table header with correct values,
  458. except for the length and checksum fields, which are filled in later.
  459. @param[in,out] Header Pointer to the header structure.
  460. @retval EFI_SUCCESS Successfully initialized the header.
  461. @retval EFI_INVALID_PARAMETER Pointer parameter was null.
  462. **/
  463. EFI_STATUS
  464. InitializeHeader (
  465. IN OUT EFI_ACPI_DESCRIPTION_HEADER *Header,
  466. IN UINT32 Signature,
  467. IN UINT8 Revision,
  468. IN UINT32 OemRevision
  469. )
  470. {
  471. UINT64 AcpiTableOemId;
  472. if (Header == NULL) {
  473. DEBUG ((DEBUG_ERROR, "Header pointer is NULL\n"));
  474. return EFI_INVALID_PARAMETER;
  475. }
  476. Header->Signature = Signature;
  477. Header->Length = 0; // filled in by Build function
  478. Header->Revision = Revision;
  479. Header->Checksum = 0; // filled in by InstallAcpiTable
  480. CopyMem (
  481. (VOID *) &Header->OemId,
  482. PcdGetPtr (PcdAcpiDefaultOemId),
  483. sizeof (Header->OemId)
  484. );
  485. AcpiTableOemId = PcdGet64 (PcdAcpiDefaultOemTableId);
  486. CopyMem (
  487. (VOID *) &Header->OemTableId,
  488. (VOID *) &AcpiTableOemId,
  489. sizeof (Header->OemTableId)
  490. );
  491. Header->OemRevision = OemRevision;
  492. Header->CreatorId = 0;
  493. Header->CreatorRevision = 0;
  494. return EFI_SUCCESS;
  495. }
  496. /**
  497. Initialize the MADT header.
  498. This function fills in the MADT's standard table header with correct values,
  499. except for the length and checksum fields, which are filled in later.
  500. @param[in,out] MadtHeader Pointer to the MADT header structure.
  501. @retval EFI_SUCCESS Successfully initialized the MADT header.
  502. @retval EFI_INVALID_PARAMETER Pointer parameter was null.
  503. **/
  504. EFI_STATUS
  505. InitializeMadtHeader (
  506. IN OUT EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER *MadtHeader
  507. )
  508. {
  509. EFI_STATUS Status;
  510. if (MadtHeader == NULL) {
  511. DEBUG ((DEBUG_ERROR, "MADT header pointer is NULL\n"));
  512. return EFI_INVALID_PARAMETER;
  513. }
  514. Status = InitializeHeader (
  515. &MadtHeader->Header,
  516. EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE,
  517. EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION,
  518. 0
  519. );
  520. if (EFI_ERROR (Status)) {
  521. return Status;
  522. }
  523. MadtHeader->LocalApicAddress = PcdGet32(PcdLocalApicAddress);
  524. MadtHeader->Flags = EFI_ACPI_4_0_PCAT_COMPAT;
  525. return EFI_SUCCESS;
  526. }
  527. /**
  528. Copy an ACPI sub-structure; MADT and SRAT supported
  529. This function validates the structure type and size of a sub-structure
  530. and returns a newly allocated copy of it.
  531. @param[in] Header Pointer to the header of the table.
  532. @param[in] Structure Pointer to the structure to copy.
  533. @param[in] NewStructure Newly allocated copy of the structure.
  534. @retval EFI_SUCCESS Successfully copied the structure.
  535. @retval EFI_INVALID_PARAMETER Pointer parameter was null.
  536. @retval EFI_INVALID_PARAMETER Structure type was unknown.
  537. @retval EFI_INVALID_PARAMETER Structure length was wrong for its type.
  538. @retval EFI_UNSUPPORTED Header passed in is not supported.
  539. **/
  540. EFI_STATUS
  541. CopyStructure (
  542. IN EFI_ACPI_DESCRIPTION_HEADER *Header,
  543. IN STRUCTURE_HEADER *Structure,
  544. OUT STRUCTURE_HEADER **NewStructure
  545. )
  546. {
  547. STRUCTURE_HEADER *NewStructureInternal;
  548. STRUCTURE_HEADER *StructureTable;
  549. UINTN TableNumEntries;
  550. BOOLEAN EntryFound;
  551. UINT8 Index;
  552. //
  553. // Initialize the number of table entries and the table based on the table header passed in.
  554. //
  555. if (Header->Signature == EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE) {
  556. TableNumEntries = sizeof (mMadtStructureTable) / sizeof (STRUCTURE_HEADER);
  557. StructureTable = mMadtStructureTable;
  558. } else {
  559. return EFI_UNSUPPORTED;
  560. }
  561. //
  562. // Check the incoming structure against the table of supported structures.
  563. //
  564. EntryFound = FALSE;
  565. for (Index = 0; Index < TableNumEntries; Index++) {
  566. if (Structure->Type == StructureTable[Index].Type) {
  567. if (Structure->Length == StructureTable[Index].Length) {
  568. EntryFound = TRUE;
  569. } else {
  570. DEBUG ((
  571. DEBUG_ERROR,
  572. "Invalid length for structure type %d: expected %d, actually %d\n",
  573. Structure->Type,
  574. StructureTable[Index].Length,
  575. Structure->Length
  576. ));
  577. return EFI_INVALID_PARAMETER;
  578. }
  579. }
  580. }
  581. //
  582. // If no entry in the table matches the structure type and length passed in
  583. // then return invalid parameter.
  584. //
  585. if (!EntryFound) {
  586. DEBUG ((
  587. DEBUG_ERROR,
  588. "Unknown structure type: %d\n",
  589. Structure->Type
  590. ));
  591. return EFI_INVALID_PARAMETER;
  592. }
  593. NewStructureInternal = (STRUCTURE_HEADER *) AllocatePool (Structure->Length);
  594. if (NewStructureInternal == NULL) {
  595. DEBUG ((
  596. DEBUG_ERROR,
  597. "Failed to allocate %d bytes for type %d structure\n",
  598. Structure->Length,
  599. Structure->Type
  600. ));
  601. return EFI_OUT_OF_RESOURCES;
  602. } else {
  603. DEBUG ((
  604. DEBUG_INFO,
  605. "Successfully allocated %d bytes for type %d structure at 0x%p\n",
  606. Structure->Length,
  607. Structure->Type,
  608. NewStructureInternal
  609. ));
  610. }
  611. CopyMem (
  612. (VOID *) NewStructureInternal,
  613. (VOID *) Structure,
  614. Structure->Length
  615. );
  616. *NewStructure = NewStructureInternal;
  617. return EFI_SUCCESS;
  618. }
  619. /**
  620. Build ACPI Table. MADT tables supported.
  621. This function builds the ACPI table from the header plus the list of sub-structures
  622. passed in. The table returned by this function is ready to be installed using
  623. the ACPI table protocol's InstallAcpiTable function, which copies it into
  624. ACPI memory. After that, the caller should free the memory returned by this
  625. function.
  626. @param[in] AcpiHeader Pointer to the header structure.
  627. @param[in] TableSpecificHdrLength Size of the table specific header, not the ACPI standard header size.
  628. @param[in] Structures Pointer to an array of sub-structure pointers.
  629. @param[in] StructureCount Number of structure pointers in the array.
  630. @param[out] NewTable Newly allocated and initialized pointer to the ACPI Table.
  631. @retval EFI_SUCCESS Successfully built the ACPI table.
  632. @retval EFI_INVALID_PARAMETER Pointer parameter was null.
  633. @retval EFI_INVALID_PARAMETER Header parameter had the wrong signature.
  634. @retval EFI_OUT_OF_RESOURCES Space for the ACPI Table could not be allocated.
  635. **/
  636. EFI_STATUS
  637. BuildAcpiTable (
  638. IN EFI_ACPI_DESCRIPTION_HEADER *AcpiHeader,
  639. IN UINTN TableSpecificHdrLength,
  640. IN STRUCTURE_HEADER **Structures,
  641. IN UINTN StructureCount,
  642. OUT UINT8 **NewTable
  643. )
  644. {
  645. EFI_STATUS Status;
  646. EFI_ACPI_DESCRIPTION_HEADER *InternalTable;
  647. UINTN Index;
  648. UINT8 *CurrPtr;
  649. UINT8 *EndOfTablePtr;
  650. if (AcpiHeader == NULL) {
  651. DEBUG ((DEBUG_ERROR, "AcpiHeader pointer is NULL\n"));
  652. return EFI_INVALID_PARAMETER;
  653. }
  654. if (AcpiHeader->Signature != EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE) {
  655. DEBUG ((
  656. DEBUG_ERROR,
  657. "MADT header signature is expected, actually 0x%08x\n",
  658. AcpiHeader->Signature
  659. ));
  660. return EFI_INVALID_PARAMETER;
  661. }
  662. if (Structures == NULL) {
  663. DEBUG ((DEBUG_ERROR, "Structure array pointer is NULL\n"));
  664. return EFI_INVALID_PARAMETER;
  665. }
  666. for (Index = 0; Index < StructureCount; Index++) {
  667. if (Structures[Index] == NULL) {
  668. DEBUG ((DEBUG_ERROR, "Structure pointer %d is NULL\n", Index));
  669. return EFI_INVALID_PARAMETER;
  670. }
  671. }
  672. //
  673. // Allocate the memory needed for the table.
  674. //
  675. Status = AllocateTable (
  676. TableSpecificHdrLength,
  677. Structures,
  678. StructureCount,
  679. &InternalTable
  680. );
  681. if (EFI_ERROR (Status)) {
  682. return Status;
  683. }
  684. //
  685. // Copy Header and patch in structure length, checksum is programmed later
  686. // after all structures are populated.
  687. //
  688. CopyMem (
  689. (VOID *) InternalTable,
  690. (VOID *) AcpiHeader,
  691. TableSpecificHdrLength
  692. );
  693. InternalTable->Length = GetTableSize (TableSpecificHdrLength, Structures, StructureCount);
  694. //
  695. // Copy all the sub structures to the table.
  696. //
  697. CurrPtr = ((UINT8 *) InternalTable) + TableSpecificHdrLength;
  698. EndOfTablePtr = ((UINT8 *) InternalTable) + InternalTable->Length;
  699. for (Index = 0; Index < StructureCount; Index++) {
  700. ASSERT (Structures[Index] != NULL);
  701. if (Structures[Index] == NULL) {
  702. break;
  703. }
  704. CopyMem (
  705. (VOID *) CurrPtr,
  706. (VOID *) Structures[Index],
  707. Structures[Index]->Length
  708. );
  709. CurrPtr += Structures[Index]->Length;
  710. ASSERT (CurrPtr <= EndOfTablePtr);
  711. if (CurrPtr > EndOfTablePtr) {
  712. break;
  713. }
  714. }
  715. //
  716. // Update the return pointer.
  717. //
  718. *NewTable = (UINT8 *) InternalTable;
  719. return EFI_SUCCESS;
  720. }
  721. /**
  722. Build from scratch and install the MADT.
  723. @retval EFI_SUCCESS The MADT was installed successfully.
  724. @retval EFI_OUT_OF_RESOURCES Could not allocate required structures.
  725. **/
  726. EFI_STATUS
  727. InstallMadtFromScratch (
  728. VOID
  729. )
  730. {
  731. EFI_STATUS Status;
  732. UINTN Index;
  733. EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER *NewMadtTable;
  734. UINTN TableHandle;
  735. EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER MadtTableHeader;
  736. EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE ProcLocalApicStruct;
  737. EFI_ACPI_4_0_IO_APIC_STRUCTURE IoApicStruct;
  738. EFI_ACPI_4_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE IntSrcOverrideStruct;
  739. EFI_ACPI_4_0_LOCAL_APIC_NMI_STRUCTURE LocalApciNmiStruct;
  740. EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE ProcLocalX2ApicStruct;
  741. EFI_ACPI_4_0_LOCAL_X2APIC_NMI_STRUCTURE LocalX2ApicNmiStruct;
  742. STRUCTURE_HEADER **MadtStructs;
  743. UINTN MaxMadtStructCount;
  744. UINTN MadtStructsIndex;
  745. UINT32 CurrentIoApicAddress = (UINT32)(PcdGet32(PcdPcIoApicAddressBase));
  746. UINT32 PcIoApicEnable;
  747. UINT32 PcIoApicMask;
  748. UINTN PcIoApicIndex;
  749. DetectApicIdMap();
  750. // Call for Local APIC ID Reorder
  751. SortCpuLocalApicInTable ();
  752. NewMadtTable = NULL;
  753. MaxMadtStructCount = (UINT32) (
  754. MAX_CPU_NUM + // processor local APIC structures
  755. MAX_CPU_NUM + // processor local x2APIC structures
  756. 1 + PcdGet8(PcdPcIoApicCount) + // I/O APIC structures
  757. 2 + // interrupt source override structures
  758. 1 + // local APIC NMI structures
  759. 1 // local x2APIC NMI structures
  760. ); // other structures are not used
  761. MadtStructs = (STRUCTURE_HEADER **) AllocateZeroPool (MaxMadtStructCount * sizeof (STRUCTURE_HEADER *));
  762. if (MadtStructs == NULL) {
  763. DEBUG ((DEBUG_ERROR, "Could not allocate MADT structure pointer array\n"));
  764. return EFI_OUT_OF_RESOURCES;
  765. }
  766. //
  767. // Initialize the next index into the structure pointer array. It is
  768. // incremented every time a structure of any type is copied to the array.
  769. //
  770. MadtStructsIndex = 0;
  771. //
  772. // Initialize MADT Header Structure
  773. //
  774. Status = InitializeMadtHeader (&MadtTableHeader);
  775. if (EFI_ERROR (Status)) {
  776. DEBUG ((EFI_D_ERROR, "InitializeMadtHeader failed: %r\n", Status));
  777. goto Done;
  778. }
  779. DEBUG ((EFI_D_INFO, "Number of CPUs detected = %d \n", mNumberOfCPUs));
  780. //
  781. // Build Processor Local APIC Structures and Processor Local X2APIC Structures
  782. //
  783. ProcLocalApicStruct.Type = EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC;
  784. ProcLocalApicStruct.Length = sizeof (EFI_ACPI_4_0_PROCESSOR_LOCAL_APIC_STRUCTURE);
  785. ProcLocalX2ApicStruct.Type = EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC;
  786. ProcLocalX2ApicStruct.Length = sizeof (EFI_ACPI_4_0_PROCESSOR_LOCAL_X2APIC_STRUCTURE);
  787. ProcLocalX2ApicStruct.Reserved[0] = 0;
  788. ProcLocalX2ApicStruct.Reserved[1] = 0;
  789. for (Index = 0; Index < MAX_CPU_NUM; Index++) {
  790. //
  791. // If x2APIC mode is not enabled, and if it is possible to express the
  792. // APIC ID as a UINT8, use a processor local APIC structure. Otherwise,
  793. // use a processor local x2APIC structure.
  794. //
  795. if (!mX2ApicEnabled && mCpuApicIdOrderTable[Index].ApicId < MAX_UINT8) {
  796. ProcLocalApicStruct.Flags = (UINT8) mCpuApicIdOrderTable[Index].Flags;
  797. ProcLocalApicStruct.ApicId = (UINT8) mCpuApicIdOrderTable[Index].ApicId;
  798. ProcLocalApicStruct.AcpiProcessorId = (UINT8) mCpuApicIdOrderTable[Index].AcpiProcessorId;
  799. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  800. Status = CopyStructure (
  801. &MadtTableHeader.Header,
  802. (STRUCTURE_HEADER *) &ProcLocalApicStruct,
  803. &MadtStructs[MadtStructsIndex++]
  804. );
  805. } else if (mCpuApicIdOrderTable[Index].ApicId != 0xFFFFFFFF) {
  806. ProcLocalX2ApicStruct.Flags = (UINT8) mCpuApicIdOrderTable[Index].Flags;
  807. ProcLocalX2ApicStruct.X2ApicId = mCpuApicIdOrderTable[Index].ApicId;
  808. ProcLocalX2ApicStruct.AcpiProcessorUid = mCpuApicIdOrderTable[Index].AcpiProcessorId;
  809. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  810. Status = CopyStructure (
  811. &MadtTableHeader.Header,
  812. (STRUCTURE_HEADER *) &ProcLocalX2ApicStruct,
  813. &MadtStructs[MadtStructsIndex++]
  814. );
  815. }
  816. if (EFI_ERROR (Status)) {
  817. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (local APIC/x2APIC) failed: %r\n", Status));
  818. goto Done;
  819. }
  820. }
  821. //
  822. // Build I/O APIC Structures
  823. //
  824. IoApicStruct.Type = EFI_ACPI_4_0_IO_APIC;
  825. IoApicStruct.Length = sizeof (EFI_ACPI_4_0_IO_APIC_STRUCTURE);
  826. IoApicStruct.Reserved = 0;
  827. PcIoApicEnable = PcdGet32(PcdPcIoApicEnable);
  828. if (FixedPcdGet32(PcdMaxCpuSocketCount) <= 4) {
  829. IoApicStruct.IoApicId = PcdGet8(PcdIoApicId);
  830. IoApicStruct.IoApicAddress = PcdGet32(PcdIoApicAddress);
  831. IoApicStruct.GlobalSystemInterruptBase = 0;
  832. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  833. Status = CopyStructure (
  834. &MadtTableHeader.Header,
  835. (STRUCTURE_HEADER *) &IoApicStruct,
  836. &MadtStructs[MadtStructsIndex++]
  837. );
  838. if (EFI_ERROR (Status)) {
  839. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (I/O APIC) failed: %r\n", Status));
  840. goto Done;
  841. }
  842. }
  843. for (PcIoApicIndex = 0; PcIoApicIndex < PcdGet8(PcdPcIoApicCount); PcIoApicIndex++) {
  844. PcIoApicMask = (1 << PcIoApicIndex);
  845. if ((PcIoApicEnable & PcIoApicMask) == 0) {
  846. continue;
  847. }
  848. IoApicStruct.IoApicId = (UINT8)(PcdGet8(PcdPcIoApicIdBase) + PcIoApicIndex);
  849. IoApicStruct.IoApicAddress = CurrentIoApicAddress;
  850. CurrentIoApicAddress = (CurrentIoApicAddress & 0xFFFF8000) + 0x8000;
  851. IoApicStruct.GlobalSystemInterruptBase = (UINT32)(24 + (PcIoApicIndex * 8));
  852. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  853. Status = CopyStructure (
  854. &MadtTableHeader.Header,
  855. (STRUCTURE_HEADER *) &IoApicStruct,
  856. &MadtStructs[MadtStructsIndex++]
  857. );
  858. if (EFI_ERROR (Status)) {
  859. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (I/O APIC) failed: %r\n", Status));
  860. goto Done;
  861. }
  862. }
  863. //
  864. // Build Interrupt Source Override Structures
  865. //
  866. IntSrcOverrideStruct.Type = EFI_ACPI_4_0_INTERRUPT_SOURCE_OVERRIDE;
  867. IntSrcOverrideStruct.Length = sizeof (EFI_ACPI_4_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE);
  868. //
  869. // IRQ0=>IRQ2 Interrupt Source Override Structure
  870. //
  871. IntSrcOverrideStruct.Bus = 0x0; // Bus - ISA
  872. IntSrcOverrideStruct.Source = 0x0; // Source - IRQ0
  873. IntSrcOverrideStruct.GlobalSystemInterrupt = 0x2; // Global System Interrupt - IRQ2
  874. IntSrcOverrideStruct.Flags = 0x0; // Flags - Conforms to specifications of the bus
  875. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  876. Status = CopyStructure (
  877. &MadtTableHeader.Header,
  878. (STRUCTURE_HEADER *) &IntSrcOverrideStruct,
  879. &MadtStructs[MadtStructsIndex++]
  880. );
  881. if (EFI_ERROR (Status)) {
  882. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (IRQ2 source override) failed: %r\n", Status));
  883. goto Done;
  884. }
  885. //
  886. // IRQ9 (SCI Active High) Interrupt Source Override Structure
  887. //
  888. IntSrcOverrideStruct.Bus = 0x0; // Bus - ISA
  889. IntSrcOverrideStruct.Source = 0x9; // Source - IRQ9
  890. IntSrcOverrideStruct.GlobalSystemInterrupt = 0x9; // Global System Interrupt - IRQ9
  891. IntSrcOverrideStruct.Flags = 0xD; // Flags - Level-tiggered, Active High
  892. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  893. Status = CopyStructure (
  894. &MadtTableHeader.Header,
  895. (STRUCTURE_HEADER *) &IntSrcOverrideStruct,
  896. &MadtStructs[MadtStructsIndex++]
  897. );
  898. if (EFI_ERROR (Status)) {
  899. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (IRQ9 source override) failed: %r\n", Status));
  900. goto Done;
  901. }
  902. //
  903. // Build Local APIC NMI Structures
  904. //
  905. LocalApciNmiStruct.Type = EFI_ACPI_4_0_LOCAL_APIC_NMI;
  906. LocalApciNmiStruct.Length = sizeof (EFI_ACPI_4_0_LOCAL_APIC_NMI_STRUCTURE);
  907. LocalApciNmiStruct.AcpiProcessorId = 0xFF; // Applies to all processors
  908. LocalApciNmiStruct.Flags = 0x000D; // Flags - Level-tiggered, Active High
  909. LocalApciNmiStruct.LocalApicLint = 0x1;
  910. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  911. Status = CopyStructure (
  912. &MadtTableHeader.Header,
  913. (STRUCTURE_HEADER *) &LocalApciNmiStruct,
  914. &MadtStructs[MadtStructsIndex++]
  915. );
  916. if (EFI_ERROR (Status)) {
  917. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (APIC NMI) failed: %r\n", Status));
  918. goto Done;
  919. }
  920. //
  921. // Build Local x2APIC NMI Structure
  922. //
  923. LocalX2ApicNmiStruct.Type = EFI_ACPI_4_0_LOCAL_X2APIC_NMI;
  924. LocalX2ApicNmiStruct.Length = sizeof (EFI_ACPI_4_0_LOCAL_X2APIC_NMI_STRUCTURE);
  925. LocalX2ApicNmiStruct.Flags = 0x000D; // Flags - Level-tiggered, Active High
  926. LocalX2ApicNmiStruct.AcpiProcessorUid = 0xFFFFFFFF; // Applies to all processors
  927. LocalX2ApicNmiStruct.LocalX2ApicLint = 0x01;
  928. LocalX2ApicNmiStruct.Reserved[0] = 0x00;
  929. LocalX2ApicNmiStruct.Reserved[1] = 0x00;
  930. LocalX2ApicNmiStruct.Reserved[2] = 0x00;
  931. ASSERT (MadtStructsIndex < MaxMadtStructCount);
  932. Status = CopyStructure (
  933. &MadtTableHeader.Header,
  934. (STRUCTURE_HEADER *) &LocalX2ApicNmiStruct,
  935. &MadtStructs[MadtStructsIndex++]
  936. );
  937. if (EFI_ERROR (Status)) {
  938. DEBUG ((EFI_D_ERROR, "CopyMadtStructure (x2APIC NMI) failed: %r\n", Status));
  939. goto Done;
  940. }
  941. //
  942. // Build Madt Structure from the Madt Header and collection of pointers in MadtStructs[]
  943. //
  944. Status = BuildAcpiTable (
  945. (EFI_ACPI_DESCRIPTION_HEADER *) &MadtTableHeader,
  946. sizeof (EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER),
  947. MadtStructs,
  948. MadtStructsIndex,
  949. (UINT8 **)&NewMadtTable
  950. );
  951. if (EFI_ERROR (Status)) {
  952. DEBUG ((EFI_D_ERROR, "BuildAcpiTable failed: %r\n", Status));
  953. goto Done;
  954. }
  955. //
  956. // Publish Madt Structure to ACPI
  957. //
  958. Status = mAcpiTable->InstallAcpiTable (
  959. mAcpiTable,
  960. NewMadtTable,
  961. NewMadtTable->Header.Length,
  962. &TableHandle
  963. );
  964. Done:
  965. //
  966. // Free memory
  967. //
  968. for (MadtStructsIndex = 0; MadtStructsIndex < MaxMadtStructCount; MadtStructsIndex++) {
  969. if (MadtStructs[MadtStructsIndex] != NULL) {
  970. FreePool (MadtStructs[MadtStructsIndex]);
  971. }
  972. }
  973. FreePool (MadtStructs);
  974. if (NewMadtTable != NULL) {
  975. FreePool (NewMadtTable);
  976. }
  977. return Status;
  978. }
  979. EFI_STATUS
  980. InstallMcfgFromScratch (
  981. VOID
  982. )
  983. {
  984. EFI_STATUS Status;
  985. EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_BASE_ADDRESS_TABLE_HEADER *McfgTable;
  986. EFI_ACPI_MEMORY_MAPPED_ENHANCED_CONFIGURATION_SPACE_BASE_ADDRESS_ALLOCATION_STRUCTURE *Segment;
  987. UINTN Index;
  988. UINTN SegmentCount;
  989. PCI_SEGMENT_INFO *PciSegmentInfo;
  990. UINTN TableHandle;
  991. PciSegmentInfo = GetPciSegmentInfo (&SegmentCount);
  992. McfgTable = AllocateZeroPool (
  993. sizeof(EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_BASE_ADDRESS_TABLE_HEADER) +
  994. sizeof(EFI_ACPI_MEMORY_MAPPED_ENHANCED_CONFIGURATION_SPACE_BASE_ADDRESS_ALLOCATION_STRUCTURE) * SegmentCount
  995. );
  996. if (McfgTable == NULL) {
  997. DEBUG ((DEBUG_ERROR, "Could not allocate MCFG structure\n"));
  998. return EFI_OUT_OF_RESOURCES;
  999. }
  1000. Status = InitializeHeader (
  1001. &McfgTable->Header,
  1002. EFI_ACPI_3_0_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE,
  1003. EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION,
  1004. 0
  1005. );
  1006. if (EFI_ERROR (Status)) {
  1007. return Status;
  1008. }
  1009. //
  1010. // Set MCFG table "Length" field based on the number of PCIe segments enumerated so far
  1011. //
  1012. McfgTable->Header.Length = (UINT32)(sizeof (EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_BASE_ADDRESS_TABLE_HEADER) +
  1013. sizeof (EFI_ACPI_MEMORY_MAPPED_ENHANCED_CONFIGURATION_SPACE_BASE_ADDRESS_ALLOCATION_STRUCTURE) * SegmentCount);
  1014. Segment = (VOID *)(McfgTable + 1);
  1015. for (Index = 0; Index < SegmentCount; Index++) {
  1016. Segment[Index].PciSegmentGroupNumber = PciSegmentInfo[Index].SegmentNumber;
  1017. Segment[Index].BaseAddress = PciSegmentInfo[Index].BaseAddress;
  1018. Segment[Index].StartBusNumber = PciSegmentInfo[Index].StartBusNumber;
  1019. Segment[Index].EndBusNumber = PciSegmentInfo[Index].EndBusNumber;
  1020. }
  1021. //
  1022. // Publish Madt Structure to ACPI
  1023. //
  1024. Status = mAcpiTable->InstallAcpiTable (
  1025. mAcpiTable,
  1026. McfgTable,
  1027. McfgTable->Header.Length,
  1028. &TableHandle
  1029. );
  1030. return Status;
  1031. }
  1032. /**
  1033. This function will update any runtime platform specific information.
  1034. This currently includes:
  1035. Setting OEM table values, ID, table ID, creator ID and creator revision.
  1036. Enabling the proper processor entries in the APIC tables
  1037. It also indicates with which ACPI table version the table belongs.
  1038. @param[in] Table The table to update
  1039. @param[in] Version Where to install this table
  1040. @retval EFI_SUCCESS Updated tables commplete.
  1041. **/
  1042. EFI_STATUS
  1043. PlatformUpdateTables (
  1044. IN OUT EFI_ACPI_COMMON_HEADER *Table,
  1045. IN OUT EFI_ACPI_TABLE_VERSION *Version
  1046. )
  1047. {
  1048. EFI_ACPI_DESCRIPTION_HEADER *TableHeader;
  1049. UINT8 *TempOemId;
  1050. UINT64 TempOemTableId;
  1051. EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *FadtHeader;
  1052. EFI_ACPI_HIGH_PRECISION_EVENT_TIMER_TABLE_HEADER *HpetTable;
  1053. UINT32 HpetBaseAddress;
  1054. EFI_ACPI_HIGH_PRECISION_EVENT_TIMER_BLOCK_ID HpetBlockId;
  1055. UINT32 HpetCapabilitiesData;
  1056. HPET_GENERAL_CAPABILITIES_ID_REGISTER HpetCapabilities;
  1057. TableHeader = NULL;
  1058. //
  1059. // By default, a table belongs in all ACPI table versions published.
  1060. // Some tables will override this because they have different versions of the table.
  1061. //
  1062. TableHeader = (EFI_ACPI_DESCRIPTION_HEADER *) Table;
  1063. //
  1064. // Update the OEM and creator information for every table except FACS.
  1065. //
  1066. if (Table->Signature != EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) {
  1067. TempOemId = (UINT8 *)PcdGetPtr(PcdAcpiDefaultOemId);
  1068. CopyMem (&TableHeader->OemId, TempOemId, 6);
  1069. //
  1070. // Skip OEM table ID and creator information for DSDT, SSDT and PSDT tables, since these are
  1071. // created by an ASL compiler and the creator information is useful.
  1072. //
  1073. if (Table->Signature != EFI_ACPI_1_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE &&
  1074. Table->Signature != EFI_ACPI_1_0_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE &&
  1075. Table->Signature != EFI_ACPI_1_0_PERSISTENT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
  1076. ) {
  1077. TempOemTableId = PcdGet64(PcdAcpiDefaultOemTableId);
  1078. CopyMem (&TableHeader->OemTableId, &TempOemTableId, 8);
  1079. //
  1080. // Update the creator ID
  1081. //
  1082. TableHeader->CreatorId = PcdGet32(PcdAcpiDefaultCreatorId);
  1083. //
  1084. // Update the creator revision
  1085. //
  1086. TableHeader->CreatorRevision = PcdGet32(PcdAcpiDefaultCreatorRevision);
  1087. }
  1088. }
  1089. //
  1090. // By default, a table belongs in all ACPI table versions published.
  1091. // Some tables will override this because they have different versions of the table.
  1092. //
  1093. *Version = EFI_ACPI_TABLE_VERSION_1_0B | EFI_ACPI_TABLE_VERSION_2_0 | EFI_ACPI_TABLE_VERSION_3_0;
  1094. //
  1095. // Update the various table types with the necessary updates
  1096. //
  1097. switch (Table->Signature) {
  1098. case EFI_ACPI_4_0_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE:
  1099. ASSERT(FALSE);
  1100. break;
  1101. case EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE:
  1102. FadtHeader = (EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *) Table;
  1103. FadtHeader->PreferredPmProfile = PcdGet8 (PcdFadtPreferredPmProfile);
  1104. FadtHeader->IaPcBootArch = PcdGet16 (PcdFadtIaPcBootArch);
  1105. FadtHeader->Flags = PcdGet32 (PcdFadtFlags);
  1106. FadtHeader->AcpiEnable = PcdGet8 (PcdAcpiEnableSwSmi);
  1107. FadtHeader->AcpiDisable = PcdGet8 (PcdAcpiDisableSwSmi);
  1108. FadtHeader->Pm1aEvtBlk = PcdGet16 (PcdAcpiPm1AEventBlockAddress);
  1109. FadtHeader->Pm1bEvtBlk = PcdGet16 (PcdAcpiPm1BEventBlockAddress);
  1110. FadtHeader->Pm1aCntBlk = PcdGet16 (PcdAcpiPm1AControlBlockAddress);
  1111. FadtHeader->Pm1bCntBlk = PcdGet16 (PcdAcpiPm1BControlBlockAddress);
  1112. FadtHeader->Pm2CntBlk = PcdGet16 (PcdAcpiPm2ControlBlockAddress);
  1113. FadtHeader->PmTmrBlk = PcdGet16 (PcdAcpiPmTimerBlockAddress);
  1114. FadtHeader->Gpe0Blk = PcdGet16 (PcdAcpiGpe0BlockAddress);
  1115. FadtHeader->Gpe1Blk = PcdGet16 (PcdAcpiGpe1BlockAddress);
  1116. FadtHeader->XPm1aEvtBlk.Address = PcdGet16 (PcdAcpiPm1AEventBlockAddress);
  1117. FadtHeader->XPm1bEvtBlk.Address = PcdGet16 (PcdAcpiPm1BEventBlockAddress);
  1118. if (FadtHeader->XPm1bEvtBlk.Address == 0) {
  1119. FadtHeader->XPm1bEvtBlk.AccessSize = 0;
  1120. }
  1121. FadtHeader->XPm1aCntBlk.Address = PcdGet16 (PcdAcpiPm1AControlBlockAddress);
  1122. FadtHeader->XPm1bCntBlk.Address = PcdGet16 (PcdAcpiPm1BControlBlockAddress);
  1123. if (FadtHeader->XPm1bCntBlk.Address == 0) {
  1124. FadtHeader->XPm1bCntBlk.AccessSize = 0;
  1125. }
  1126. FadtHeader->XPm2CntBlk.Address = PcdGet16 (PcdAcpiPm2ControlBlockAddress);
  1127. //if (FadtHeader->XPm2CntBlk.Address == 0) {
  1128. FadtHeader->XPm2CntBlk.AccessSize = 0;
  1129. //}
  1130. FadtHeader->XPmTmrBlk.Address = PcdGet16 (PcdAcpiPmTimerBlockAddress);
  1131. FadtHeader->XGpe0Blk.Address = PcdGet16 (PcdAcpiGpe0BlockAddress);
  1132. FadtHeader->XGpe1Blk.Address = PcdGet16 (PcdAcpiGpe1BlockAddress);
  1133. if (FadtHeader->XGpe1Blk.Address == 0) {
  1134. FadtHeader->XGpe1Blk.AccessSize = 0;
  1135. }
  1136. DEBUG(( EFI_D_ERROR, "ACPI FADT table @ address 0x%x\n", Table ));
  1137. DEBUG(( EFI_D_ERROR, " IaPcBootArch 0x%x\n", FadtHeader->IaPcBootArch ));
  1138. DEBUG(( EFI_D_ERROR, " Flags 0x%x\n", FadtHeader->Flags ));
  1139. break;
  1140. case EFI_ACPI_3_0_HIGH_PRECISION_EVENT_TIMER_TABLE_SIGNATURE:
  1141. HpetTable = (EFI_ACPI_HIGH_PRECISION_EVENT_TIMER_TABLE_HEADER *)Table;
  1142. HpetBaseAddress = PcdGet32 (PcdHpetBaseAddress);
  1143. HpetTable->BaseAddressLower32Bit.Address = HpetBaseAddress;
  1144. HpetTable->BaseAddressLower32Bit.RegisterBitWidth = 0;
  1145. HpetCapabilitiesData = MmioRead32 (HpetBaseAddress + HPET_GENERAL_CAPABILITIES_ID_OFFSET);
  1146. HpetCapabilities.Uint64 = HpetCapabilitiesData;
  1147. HpetCapabilitiesData = MmioRead32 (HpetBaseAddress + HPET_GENERAL_CAPABILITIES_ID_OFFSET + 4);
  1148. HpetCapabilities.Uint64 |= LShiftU64 (HpetCapabilitiesData, 32);
  1149. HpetBlockId.Bits.Revision = HpetCapabilities.Bits.Revision;
  1150. HpetBlockId.Bits.NumberOfTimers = HpetCapabilities.Bits.NumberOfTimers;
  1151. HpetBlockId.Bits.CounterSize = HpetCapabilities.Bits.CounterSize;
  1152. HpetBlockId.Bits.Reserved = 0;
  1153. HpetBlockId.Bits.LegacyRoute = HpetCapabilities.Bits.LegacyRoute;
  1154. HpetBlockId.Bits.VendorId = HpetCapabilities.Bits.VendorId;
  1155. HpetTable->EventTimerBlockId = HpetBlockId.Uint32;
  1156. HpetTable->MainCounterMinimumClockTickInPeriodicMode = (UINT16)HpetCapabilities.Bits.CounterClockPeriod;
  1157. DEBUG(( EFI_D_ERROR, "ACPI HPET table @ address 0x%x\n", Table ));
  1158. DEBUG(( EFI_D_ERROR, " HPET base 0x%x\n", PcdGet32 (PcdHpetBaseAddress) ));
  1159. break;
  1160. case EFI_ACPI_3_0_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE:
  1161. ASSERT(FALSE);
  1162. break;
  1163. default:
  1164. break;
  1165. }
  1166. return EFI_SUCCESS;
  1167. }
  1168. /**
  1169. This function calculates RCR based on PCI Device ID and Vendor ID from the devices
  1170. available on the platform.
  1171. It also includes other instances of BIOS change to calculate CRC and provides as
  1172. HWSignature filed in FADT table.
  1173. **/
  1174. VOID
  1175. IsHardwareChange (
  1176. VOID
  1177. )
  1178. {
  1179. EFI_STATUS Status;
  1180. UINTN Index;
  1181. UINTN HandleCount;
  1182. EFI_HANDLE *HandleBuffer;
  1183. EFI_PCI_IO_PROTOCOL *PciIo;
  1184. UINT32 CRC;
  1185. UINT32 *HWChange;
  1186. UINTN HWChangeSize;
  1187. UINT32 PciId;
  1188. UINTN Handle;
  1189. EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *FacsPtr;
  1190. EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE *pFADT;
  1191. HandleCount = 0;
  1192. HandleBuffer = NULL;
  1193. Status = gBS->LocateHandleBuffer (
  1194. ByProtocol,
  1195. &gEfiPciIoProtocolGuid,
  1196. NULL,
  1197. &HandleCount,
  1198. &HandleBuffer
  1199. );
  1200. if (EFI_ERROR (Status)) {
  1201. return; // PciIO protocol not installed yet!
  1202. }
  1203. //
  1204. // Allocate memory for HWChange and add additional entrie for
  1205. // pFADT->XDsdt
  1206. //
  1207. HWChangeSize = HandleCount + 1;
  1208. HWChange = AllocateZeroPool( sizeof(UINT32) * HWChangeSize );
  1209. ASSERT( HWChange != NULL );
  1210. if (HWChange == NULL) return;
  1211. //
  1212. // add HWChange inputs: PCI devices
  1213. //
  1214. for (Index = 0; HandleCount > 0; HandleCount--) {
  1215. PciId = 0;
  1216. Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID **) &PciIo);
  1217. if (!EFI_ERROR (Status)) {
  1218. Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, 1, &PciId);
  1219. if (EFI_ERROR (Status)) {
  1220. continue;
  1221. }
  1222. HWChange[Index++] = PciId;
  1223. }
  1224. }
  1225. //
  1226. // Locate FACP Table
  1227. //
  1228. Handle = 0;
  1229. Status = LocateAcpiTableBySignature (
  1230. EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
  1231. (EFI_ACPI_DESCRIPTION_HEADER **) &pFADT,
  1232. &Handle
  1233. );
  1234. if (EFI_ERROR (Status) || (pFADT == NULL)) {
  1235. return; //Table not found or out of memory resource for pFADT table
  1236. }
  1237. //
  1238. // add HWChange inputs: others
  1239. //
  1240. HWChange[Index++] = (UINT32)pFADT->XDsdt;
  1241. //
  1242. // Calculate CRC value with HWChange data.
  1243. //
  1244. Status = gBS->CalculateCrc32(HWChange, HWChangeSize, &CRC);
  1245. DEBUG((DEBUG_INFO, "CRC = %x and Status = %r\n", CRC, Status));
  1246. //
  1247. // Set HardwareSignature value based on CRC value.
  1248. //
  1249. FacsPtr = (EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)pFADT->FirmwareCtrl;
  1250. FacsPtr->HardwareSignature = CRC;
  1251. FreePool( HWChange );
  1252. }
  1253. VOID
  1254. UpdateLocalTable (
  1255. VOID
  1256. )
  1257. {
  1258. EFI_STATUS Status;
  1259. EFI_ACPI_COMMON_HEADER *CurrentTable;
  1260. EFI_ACPI_TABLE_VERSION Version;
  1261. UINTN TableHandle;
  1262. UINTN Index;
  1263. for (Index = 0; Index < sizeof(mLocalTable)/sizeof(mLocalTable[0]); Index++) {
  1264. CurrentTable = mLocalTable[Index];
  1265. PlatformUpdateTables (CurrentTable, &Version);
  1266. TableHandle = 0;
  1267. if (Version != EFI_ACPI_TABLE_VERSION_NONE) {
  1268. Status = mAcpiTable->InstallAcpiTable (
  1269. mAcpiTable,
  1270. CurrentTable,
  1271. CurrentTable->Length,
  1272. &TableHandle
  1273. );
  1274. ASSERT_EFI_ERROR (Status);
  1275. }
  1276. }
  1277. }
  1278. VOID
  1279. EFIAPI
  1280. AcpiEndOfDxeEvent (
  1281. EFI_EVENT Event,
  1282. VOID *ParentImageHandle
  1283. )
  1284. {
  1285. if (Event != NULL) {
  1286. gBS->CloseEvent(Event);
  1287. }
  1288. //
  1289. // Calculate Hardware Signature value based on current platform configurations
  1290. //
  1291. IsHardwareChange();
  1292. }
  1293. /**
  1294. ACPI Platform driver installation function.
  1295. @param[in] ImageHandle Handle for this drivers loaded image protocol.
  1296. @param[in] SystemTable EFI system table.
  1297. @retval EFI_SUCCESS The driver installed without error.
  1298. @retval EFI_ABORTED The driver encountered an error and could not complete installation of
  1299. the ACPI tables.
  1300. **/
  1301. EFI_STATUS
  1302. EFIAPI
  1303. InstallAcpiPlatform (
  1304. IN EFI_HANDLE ImageHandle,
  1305. IN EFI_SYSTEM_TABLE *SystemTable
  1306. )
  1307. {
  1308. EFI_STATUS Status;
  1309. EFI_EVENT EndOfDxeEvent;
  1310. Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&mMpService);
  1311. ASSERT_EFI_ERROR (Status);
  1312. Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&mAcpiTable);
  1313. ASSERT_EFI_ERROR (Status);
  1314. //
  1315. // Create an End of DXE event.
  1316. //
  1317. Status = gBS->CreateEventEx (
  1318. EVT_NOTIFY_SIGNAL,
  1319. TPL_CALLBACK,
  1320. AcpiEndOfDxeEvent,
  1321. NULL,
  1322. &gEfiEndOfDxeEventGroupGuid,
  1323. &EndOfDxeEvent
  1324. );
  1325. ASSERT_EFI_ERROR (Status);
  1326. //
  1327. // Determine the number of processors
  1328. //
  1329. mMpService->GetNumberOfProcessors (
  1330. mMpService,
  1331. &mNumberOfCPUs,
  1332. &mNumberOfEnabledCPUs
  1333. );
  1334. ASSERT (mNumberOfCPUs <= MAX_CPU_NUM && mNumberOfEnabledCPUs >= 1);
  1335. DEBUG ((DEBUG_INFO, "mNumberOfCPUs - %d\n", mNumberOfCPUs));
  1336. DEBUG ((DEBUG_INFO, "mNumberOfEnabledCPUs - %d\n", mNumberOfEnabledCPUs));
  1337. DEBUG ((DEBUG_INFO, "mX2ApicEnabled - 0x%x\n", mX2ApicEnabled));
  1338. DEBUG ((DEBUG_INFO, "mForceX2ApicId - 0x%x\n", mForceX2ApicId));
  1339. // support up to 64 threads/socket
  1340. AsmCpuidEx(CPUID_EXTENDED_TOPOLOGY, 1, &mNumOfBitShift, NULL, NULL, NULL);
  1341. mNumOfBitShift &= 0x1F;
  1342. DEBUG ((DEBUG_INFO, "mNumOfBitShift - 0x%x\n", mNumOfBitShift));
  1343. UpdateLocalTable ();
  1344. InstallMadtFromScratch ();
  1345. InstallMcfgFromScratch ();
  1346. return EFI_SUCCESS;
  1347. }