PeiCpuPolicyUpdatePreMem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /** @file
  2. This file is SampleCode of the library for Intel CPU PEI Policy initialization.
  3. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PeiCpuPolicyUpdate.h"
  7. #include <Library/ConfigBlockLib.h>
  8. #include <Library/PmcLib.h>
  9. #include <Library/PchCycleDecodingLib.h>
  10. #include <Library/PciSegmentLib.h>
  11. #include <Library/SpiLib.h>
  12. /**
  13. Check on the processor if SGX is supported.
  14. @retval True if SGX supported or FALSE if not
  15. **/
  16. BOOLEAN
  17. IsSgxCapSupported (
  18. VOID
  19. )
  20. {
  21. EFI_CPUID_REGISTER CpuidRegs;
  22. ///
  23. /// Processor support SGX feature by reading CPUID.(EAX=7,ECX=0):EBX[2]
  24. ///
  25. AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS, 0, &CpuidRegs.RegEax,&CpuidRegs.RegEbx,&CpuidRegs.RegEcx,&CpuidRegs.RegEdx);
  26. ///
  27. /// SGX feature is supported only on WHL and later,
  28. /// with CPUID.(EAX=7,ECX=0):EBX[2]=1
  29. /// PRMRR configuration enabled, MSR IA32_MTRRCAP (FEh) [12] == 1
  30. ///
  31. if ((CpuidRegs.RegEbx & BIT2) && (AsmReadMsr64 (MSR_IA32_MTRRCAP) & BIT12)) {
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. /**
  37. This function performs CPU PEI Policy initialization in Pre-memory.
  38. @param[in] SiPreMemPolicyPpi The SI Pre-Mem Policy PPI instance
  39. @retval EFI_SUCCESS The PPI is installed and initialized.
  40. @retval EFI ERRORS The PPI is not successfully installed.
  41. @retval EFI_OUT_OF_RESOURCES Do not have enough resources to initialize the driver
  42. **/
  43. EFI_STATUS
  44. EFIAPI
  45. UpdatePeiCpuPolicyPreMem (
  46. IN OUT SI_PREMEM_POLICY_PPI *SiPreMemPolicyPpi
  47. )
  48. {
  49. EFI_STATUS Status;
  50. EFI_BOOT_MODE BootMode;
  51. CPU_CONFIG_LIB_PREMEM_CONFIG *CpuConfigLibPreMemConfig;
  52. CPU_OVERCLOCKING_PREMEM_CONFIG *CpuOverClockingPreMemConfig;
  53. UINT32 PchSpiBar0;
  54. UINT32 MaxLogicProcessors;
  55. Status = GetConfigBlock ((VOID *) SiPreMemPolicyPpi, &gCpuConfigLibPreMemConfigGuid, (VOID *) &CpuConfigLibPreMemConfig);
  56. ASSERT_EFI_ERROR (Status);
  57. Status = GetConfigBlock ((VOID *) SiPreMemPolicyPpi, &gCpuOverclockingPreMemConfigGuid, (VOID *) &CpuOverClockingPreMemConfig);
  58. ASSERT_EFI_ERROR (Status);
  59. DEBUG ((DEBUG_INFO, "UpdatePeiCpuPolicyPreMem Start\n"));
  60. //
  61. // Get current boot mode
  62. //
  63. Status = PeiServicesGetBootMode (&BootMode);
  64. ASSERT_EFI_ERROR (Status);
  65. SpiServiceInit ();
  66. PchSpiBar0 = PciSegmentRead32 (PCI_SEGMENT_LIB_ADDRESS (
  67. DEFAULT_PCI_SEGMENT_NUMBER_PCH,
  68. DEFAULT_PCI_BUS_NUMBER_PCH,
  69. PCI_DEVICE_NUMBER_PCH_SPI,
  70. PCI_FUNCTION_NUMBER_PCH_SPI,
  71. R_SPI_CFG_BAR0
  72. ));
  73. PchSpiBar0 &= ~(B_SPI_CFG_BAR0_MASK);
  74. if (PchSpiBar0 == 0) {
  75. DEBUG ((DEBUG_ERROR, "ERROR : PchSpiBar0 is invalid!\n"));
  76. ASSERT (FALSE);
  77. }
  78. CpuConfigLibPreMemConfig->PeciC10Reset = 0;
  79. CpuConfigLibPreMemConfig->CpuRatio = 0;
  80. ///
  81. /// Set PcdCpuMaxLogicalProcessorNumber to max number of logical processors enabled
  82. /// Read MSR_CORE_THREAD_COUNT (0x35) to check the total active Threads
  83. ///
  84. MaxLogicProcessors = (UINT32) (AsmReadMsr64 (MSR_CORE_THREAD_COUNT) & B_THREAD_COUNT_MASK);
  85. DEBUG ((DEBUG_INFO, "MaxLogicProcessors = %d\n", MaxLogicProcessors));
  86. PcdSet32S (PcdCpuMaxLogicalProcessorNumber, MaxLogicProcessors);
  87. return EFI_SUCCESS;
  88. }