OcCpuLib.h 4.6 KB
Newer Older
V
vit9696 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** @file
  Copyright (C) 2016 - 2017, The HermitCrabs Lab. All rights reserved.

  All rights reserved.

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

V
vit9696 已提交
15 16
#ifndef OC_CPU_LIB_H
#define OC_CPU_LIB_H
V
vit9696 已提交
17

18
#include <IndustryStandard/CpuId.h>
19
#include <IndustryStandard/AppleIntelCpuInfo.h>
20

21 22 23 24 25 26
//
// External clock value on Sandy Bridge and above (in MHz).
// Rounded FSB * 4.
//
#define OC_CPU_SNB_QPB_CLOCK 0x19U

V
vit9696 已提交
27
typedef struct {
V
vit9696 已提交
28 29 30
  //
  // Note, Vendor and BrandString are reordered for proper alignment.
  //
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  UINT32                  Vendor[4];
  CHAR8                   BrandString[48];

  CPUID_VERSION_INFO_EAX  CpuidVerEax;
  CPUID_VERSION_INFO_EBX  CpuidVerEbx;
  CPUID_VERSION_INFO_ECX  CpuidVerEcx;
  CPUID_VERSION_INFO_EDX  CpuidVerEdx;

  UINT32                  MicrocodeRevision;

  UINT8                   Type;
  UINT8                   Family;
  UINT8                   Model;
  UINT8                   ExtModel;
  UINT8                   ExtFamily;
  UINT8                   Stepping;
  UINT64                  Features;
  UINT64                  ExtFeatures;
  UINT32                  Signature;
  UINT8                   Brand;
  UINT16                  AppleProcessorType;
  BOOLEAN                 CstConfigLock;

54
  UINT32                  MaxId;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  UINT32                  MaxExtId;

  UINT8                   MaxDiv;
  UINT8                   CurBusRatio;  ///< Current Multiplier
  UINT8                   MinBusRatio;  ///< Min Bus Ratio
  UINT8                   MaxBusRatio;  ///< Max Bus Ratio

  UINT8                   TurboBusRatio1;
  UINT8                   TurboBusRatio2;
  UINT8                   TurboBusRatio3;
  UINT8                   TurboBusRatio4;

  UINT16                  PackageCount;
  UINT16                  CoreCount;
  UINT16                  ThreadCount;

71 72 73 74 75 76 77 78 79 80 81
  //
  // Platform-dependent frequency for the Always Running Timer (ART), normally
  // 24Mhz. Firmwares may choose to override this. Some CPUs like Xeon Scalable
  // use a different frequency. CPUs report the frequency through CPUID.15H.ECX.
  // If unreported, the frequency is looked up based on the model and family.
  //
  // Nominal Core Crystal Clock Frequency for known processor families:
  //   Intel Xeon Scalable with CPUID signature 0x0655: 25 Mhz
  //   6th and 7th generation Intel Core & Xeon W: 24 Mhz
  //   Nex Generation Intel Atom with CPUID 0x065C: 19.2 Mhz
  //
82
  UINT64                  ARTFrequency;
83 84 85 86 87 88 89 90 91

  //
  // The CPU frequency derived from either CPUFrequencyFromTSC (legacy) or
  // CPUFrequencyFromART (preferred for Skylake and presumably newer processors
  // that have an Always Running Timer).
  //
  // CPUFrequencyFromTSC should approximate equal CPUFrequencyFromART. If not,
  // there is likely a bug or miscalculation.
  //
92
  UINT64                  CPUFrequency;
93 94 95 96 97 98 99 100

  //
  // The CPU frequency as reported by the Time Stamp Counter (TSC).
  //
  UINT64                  CPUFrequencyFromTSC;

  //
  // The CPU frequency derived from the Always Running Timer (ART) frequency:
M
M. R. Miller 已提交
101
  //   TSC Freq = (ART Freq * CPUID.15H:EBX[31:0]) / CPUID.15H:EAX[31:0]
102 103 104 105 106 107 108 109 110
  //
  // 0 if ART is not present.
  //
  UINT64                  CPUFrequencyFromART;

  //
  // The Front Side Bus (FSB) frequency calculated from dividing the CPU
  // frequency by the Max Ratio.
  //
111
  UINT64                  FSBFrequency;
112
} OC_CPU_INFO;
V
vit9696 已提交
113

114 115
/**
  Scan the processor and fill the cpu info structure with results.
V
vit9696 已提交
116

117
  @param[in] Cpu  A pointer to the cpu info structure to fill with results.
V
vit9696 已提交
118
**/
119
VOID
V
vit9696 已提交
120
OcCpuScanProcessor (
121
  IN OUT OC_CPU_INFO  *Cpu
V
vit9696 已提交
122 123
  );

124 125 126 127 128 129 130 131 132 133 134
/**
  Disable flex ratio if it has invalid value.
  Commonly fixes early reboot on APTIO IV (Ivy/Haswell).

  @param[in] Cpu  A pointer to the cpu info.
**/
VOID
OcCpuCorrectFlexRatio (
  IN OC_CPU_INFO  *Cpu
  );

135 136 137 138 139 140 141
/**
  Converts CPUID Family and Model extracted from EAX
  CPUID (1) call to AppleFamily value. This implements
  cpuid_set_cpufamily functionality as it is in XNU.

  @param[in] VersionEax  CPUID (1) EAX value.

142
  @retval Apple Family (e.g. CPUFAMILY_UNKNOWN)
143 144 145
**/
UINT32
OcCpuModelToAppleFamily (
146
  IN CPUID_VERSION_INFO_EAX  VersionEax
147 148
  );

149 150 151 152 153 154 155 156 157 158
/**
  Special Intel Sandy Bridge and Ivy Bridge detection code.

  @retval TRUE when running with Sandy Bridge or Ivy Bridge CPU.
**/
BOOLEAN
OcIsSandyOrIvy (
  VOID
  );

V
vit9696 已提交
159
#endif // OC_CPU_LIB_H_