coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
msr.h
Go to the documentation of this file.
1 #ifndef CPU_X86_MSR_H
2 #define CPU_X86_MSR_H
3 
4 /* Intel SDM: Table 2-1
5  * IA-32 architectural MSR: Extended Feature Enable Register
6  *
7  * AMD64 Programmers Manual vol2 Revision 3.30 and/or the device's BKDG
8  */
9 
10 #define IA32_EFER 0xC0000080
11 #define EFER_NXE (1 << 11)
12 #define EFER_LMA (1 << 10)
13 #define EFER_LME (1 << 8)
14 #define EFER_SCE (1 << 0)
15 
16 /* Page attribute type MSR */
17 #define TSC_MSR 0x10
18 #define IA32_PLATFORM_ID 0x17
19 #define IA32_APIC_BASE_MSR_INDEX 0x1B
20 #define IA32_FEATURE_CONTROL 0x3a
21 #define FEATURE_CONTROL_LOCK_BIT (1 << 0)
22 #define FEATURE_ENABLE_VMX (1 << 2)
23 #define SMRR_ENABLE (1 << 3)
24 #define CPUID_VMX (1 << 5)
25 #define CPUID_SMX (1 << 6)
26 #define CPUID_DCA (1 << 18)
27 #define CPUID_X2APIC (1 << 21)
28 #define CPUID_AES (1 << 25)
29 #define SGX_GLOBAL_ENABLE (1 << 18)
30 #define PLATFORM_INFO_SET_TDP (1 << 29)
31 #define IA32_BIOS_UPDT_TRIG 0x79
32 #define IA32_BIOS_SIGN_ID 0x8b
33 #define IA32_MPERF 0xe7
34 #define IA32_APERF 0xe8
35 /* STM */
36 #define IA32_SMM_MONITOR_CTL_MSR 0x9B
37 #define SMBASE_RO_MSR 0x98
38 #define IA32_SMM_MONITOR_VALID (1 << 0)
39 #define IA32_MCG_CAP 0x179
40 #define MCG_CTL_P (1 << 8)
41 #define MCA_BANKS_MASK 0xff
42 #define IA32_PERF_STATUS 0x198
43 #define IA32_PERF_CTL 0x199
44 #define IA32_THERM_INTERRUPT 0x19b
45 #define IA32_MISC_ENABLE 0x1a0
46 #define FAST_STRINGS_ENABLE_BIT (1 << 0)
47 #define SPEED_STEP_ENABLE_BIT (1 << 16)
48 #define IA32_ENERGY_PERF_BIAS 0x1b0
49 #define ENERGY_POLICY_PERFORMANCE 0
50 #define ENERGY_POLICY_NORMAL 6
51 #define ENERGY_POLICY_POWERSAVE 15
52 #define ENERGY_POLICY_MASK 0xf
53 #define IA32_PACKAGE_THERM_INTERRUPT 0x1b2
54 #define SMRR_PHYSBASE_MSR 0x1F2
55 #define SMRR_PHYSMASK_MSR 0x1F3
56 #define IA32_PLATFORM_DCA_CAP 0x1f8
57 #define DCA_TYPE0_EN (1 << 0)
58 #define IA32_PAT 0x277
59 #define IA32_MC0_CTL 0x400
60 #define IA32_MC_CTL(bank) (IA32_MC0_CTL + 4 * (bank))
61 #define IA32_MC0_STATUS 0x401
62 #define IA32_MC_STATUS(bank) (IA32_MC0_STATUS + 4 * (bank))
63 #define MCA_STATUS_HI_VAL (1UL << (63 - 32))
64 #define MCA_STATUS_HI_OVERFLOW (1UL << (62 - 32))
65 #define MCA_STATUS_HI_UC (1UL << (61 - 32))
66 #define MCA_STATUS_HI_EN (1UL << (60 - 32))
67 #define MCA_STATUS_HI_MISCV (1UL << (59 - 32))
68 #define MCA_STATUS_HI_ADDRV (1UL << (58 - 32))
69 #define MCA_STATUS_HI_PCC (1UL << (57 - 32))
70 #define MCA_STATUS_HI_COREID_VAL (1UL << (56 - 32))
71 #define MCA_STATUS_HI_CECC (1UL << (46 - 32))
72 #define MCA_STATUS_HI_UECC (1UL << (45 - 32))
73 #define MCA_STATUS_HI_DEFERRED (1UL << (44 - 32))
74 #define MCA_STATUS_HI_POISON (1UL << (43 - 32))
75 #define MCA_STATUS_HI_SUBLINK (1UL << (41 - 32))
76 #define MCA_STATUS_HI_ERRCOREID_MASK (0xf << 0)
77 #define MCA_STATUS_LO_ERRCODE_EXT_SH 16
78 #define MCA_STATUS_LO_ERRCODE_EXT_MASK (0x3f << MCA_STATUS_LO_ERRCODE_EXT_SH)
79 #define MCA_STATUS_LO_ERRCODE_MASK (0xffff << 0)
80 #define IA32_MC0_ADDR 0x402
81 #define IA32_MC_ADDR(bank) (IA32_MC0_ADDR + 4 * (bank))
82 #define IA32_MC0_MISC 0x403
83 #define IA32_MC_MISC(bank) (IA32_MC0_MISC + 4 * (bank))
84 #define IA32_VMX_BASIC_MSR 0x480
85 #define VMX_BASIC_HI_DUAL_MONITOR (1UL << (49 - 32))
86 #define IA32_VMX_MISC_MSR 0x485
87 
88 #define IA32_PM_ENABLE 0x770
89 #define HWP_ENABLE 0x1
90 #define IA32_HWP_CAPABILITIES 0x771
91 #define IA32_HWP_REQUEST 0x774
92 #define IA32_HWP_REQUEST_EPP_MASK 0xff000000
93 #define IA32_HWP_REQUEST_EPP_SHIFT 24
94 #define IA32_HWP_STATUS 0x777
95 #define IA32_L3_PROTECTED_WAYS 0xc85
96 #define IA32_SF_QOS_INFO 0xc87
97 #define IA32_SF_WAY_COUNT_MASK 0x3f
98 #define IA32_PQR_ASSOC 0xc8f
99 /* MSR bits 33:32 encode slot number 0-3 */
100 #define IA32_PQR_ASSOC_MASK (1 << 0 | 1 << 1)
101 #define IA32_L3_MASK_1 0xc91
102 #define IA32_L3_MASK_2 0xc92
103 
104 #define IA32_CR_SF_QOS_MASK_1 0x1891
105 #define IA32_CR_SF_QOS_MASK_2 0x1892
106 
107 #ifndef __ASSEMBLER__
108 #include <types.h>
109 
110 typedef struct msr_struct {
111  unsigned int lo;
112  unsigned int hi;
114 
115 typedef struct msrinit_struct {
116  unsigned int index;
119 
120 #if CONFIG(SOC_SETS_MSRS)
121 msr_t soc_msr_read(unsigned int index);
122 void soc_msr_write(unsigned int index, msr_t msr);
123 
124 /* Handle MSR references in the other source code */
125 static __always_inline msr_t rdmsr(unsigned int index)
126 {
127  return soc_msr_read(index);
128 }
129 
130 static __always_inline void wrmsr(unsigned int index, msr_t msr)
131 {
132  soc_msr_write(index, msr);
133 }
134 #else /* CONFIG_SOC_SETS_MSRS */
135 
136 /* The following functions require the __always_inline due to AMD
137  * function STOP_CAR_AND_CPU that disables cache as
138  * RAM, the cache as RAM stack can no longer be used. Called
139  * functions must be inlined to avoid stack usage. Also, the
140  * compiler must keep local variables register based and not
141  * allocated them from the stack. With gcc 4.5.0, some functions
142  * declared as inline are not being inlined. This patch forces
143  * these functions to always be inlined by adding the qualifier
144  * __always_inline to their declaration.
145  */
146 static __always_inline msr_t rdmsr(unsigned int index)
147 {
148  msr_t result;
149  __asm__ __volatile__ (
150  "rdmsr"
151  : "=a" (result.lo), "=d" (result.hi)
152  : "c" (index)
153  );
154  return result;
155 }
156 
157 static __always_inline void wrmsr(unsigned int index, msr_t msr)
158 {
159  __asm__ __volatile__ (
160  "wrmsr"
161  : /* No outputs */
162  : "c" (index), "a" (msr.lo), "d" (msr.hi)
163  );
164 }
165 
166 #endif /* CONFIG_SOC_SETS_MSRS */
167 
168 /* Get MCA bank count from MSR */
169 static inline unsigned int mca_get_bank_count(void)
170 {
171  msr_t msr = rdmsr(IA32_MCG_CAP);
172  return msr.lo & MCA_BANKS_MASK;
173 }
174 
175 /* Clear all MCA status registers */
176 static inline void mca_clear_status(void)
177 {
178  const unsigned int num_banks = mca_get_bank_count();
179  const msr_t msr = {.lo = 0, .hi = 0};
180 
181  for (unsigned int i = 0 ; i < num_banks ; i++)
182  wrmsr(IA32_MC_STATUS(i), msr);
183 }
184 
185 /* Helpers for interpreting MC[i]_STATUS */
186 
187 static inline int mca_valid(msr_t msr)
188 {
189  return !!(msr.hi & MCA_STATUS_HI_VAL);
190 }
191 
192 static inline int mca_over(msr_t msr)
193 {
194  return !!(msr.hi & MCA_STATUS_HI_OVERFLOW);
195 }
196 
197 static inline int mca_uc(msr_t msr)
198 {
199  return !!(msr.hi & MCA_STATUS_HI_UC);
200 }
201 
202 static inline int mca_en(msr_t msr)
203 {
204  return !!(msr.hi & MCA_STATUS_HI_EN);
205 }
206 
207 static inline int mca_miscv(msr_t msr)
208 {
209  return !!(msr.hi & MCA_STATUS_HI_MISCV);
210 }
211 
212 static inline int mca_addrv(msr_t msr)
213 {
214  return !!(msr.hi & MCA_STATUS_HI_ADDRV);
215 }
216 
217 static inline int mca_pcc(msr_t msr)
218 {
219  return !!(msr.hi & MCA_STATUS_HI_PCC);
220 }
221 
222 static inline int mca_idv(msr_t msr)
223 {
224  return !!(msr.hi & MCA_STATUS_HI_COREID_VAL);
225 }
226 
227 static inline int mca_cecc(msr_t msr)
228 {
229  return !!(msr.hi & MCA_STATUS_HI_CECC);
230 }
231 
232 static inline int mca_uecc(msr_t msr)
233 {
234  return !!(msr.hi & MCA_STATUS_HI_UECC);
235 }
236 
237 static inline int mca_defd(msr_t msr)
238 {
239  return !!(msr.hi & MCA_STATUS_HI_DEFERRED);
240 }
241 
242 static inline int mca_poison(msr_t msr)
243 {
244  return !!(msr.hi & MCA_STATUS_HI_POISON);
245 }
246 
247 static inline int mca_sublink(msr_t msr)
248 {
249  return !!(msr.hi & MCA_STATUS_HI_SUBLINK);
250 }
251 
252 static inline uint16_t mca_err_code(msr_t reg)
253 {
254  return reg.lo & MCA_STATUS_LO_ERRCODE_MASK;
255 }
256 
257 static inline uint16_t mca_err_extcode(msr_t reg)
258 {
259  return reg.lo & MCA_STATUS_LO_ERRCODE_EXT_MASK;
260 }
261 
262 /* Machine Check errors may be categorized by type, as determined by the
263  * Error Code field of MC[i]_STATUS. The definitions below can typically
264  * be found by searching the BKDG for a table called "Error Code Types".
265  */
266 /* TLB Errors 0000 0000 0001 TTLL */
267 #define MCA_ERRCODE_TLB_DETECT 0xfff0
268 #define MCA_ERRCODE_TLB_TT_SH 2 /* Transaction Type */
269 #define MCA_ERRCODE_TLB_TT_MASK (0x3 << MCA_ERRCODE_TLB_TT_SH)
270 #define MCA_ERRCODE_TLB_LL_SH 0 /* Cache Level */
271 #define MCA_ERRCODE_TLB_LL_MASK (0x3 << MCA_ERRCODE_TLB_LL_SH)
272 
273 /* Memory Errors 0000 0001 RRRR TTLL */
274 #define MCA_ERRCODE_MEM_DETECT 0xff00
275 #define MCA_ERRCODE_MEM_RRRR_SH 4 /* Memory Transaction Type */
276 #define MCA_ERRCODE_MEM_RRRR_MASK (0xf << MCA_ERRCODE_MEM_RRRR_MASK)
277 #define MCA_ERRCODE_MEM_TT_SH 2 /* Transaction Type */
278 #define MCA_ERRCODE_MEM_TT_MASK (0x3 << MCA_ERRCODE_MEM_TT_SH)
279 #define MCA_ERRCODE_MEM_LL_SH 0 /* Cache Level */
280 #define MCA_ERRCODE_MEM_LL_MASK (0x3 << MCA_ERRCODE_MEM_LL_SH)
281 
282 /* Bus Errors 0000 1PPT RRRR IILL */
283 #define MCA_ERRCODE_BUS_DETECT 0xf800
284 #define MCA_ERRCODE_BUS_PP_SH 9 /* Participation Processor */
285 #define MCA_ERRCODE_BUS_PP_MASK (0x3 << MCA_ERRCODE_BUS_PP_SH)
286 #define MCA_ERRCODE_BUS_T_SH 8 /* Timeout */
287 #define MCA_ERRCODE_BUS_T_MASK (0x1 << MCA_ERRCODE_BUS_T_SH)
288 #define MCA_ERRCODE_BUS_RRRR_SH 4 /* Memory Transaction Type */
289 #define MCA_ERRCODE_BUS_RRRR_MASK (0xf << MCA_ERRCODE_BUS_RRRR_SH)
290 #define MCA_ERRCODE_BUS_II_SH 2 /* Memory or IO */
291 #define MCA_ERRCODE_BUS_II_MASK (0x3 << MCA_ERRCODE_BUS_II_SH)
292 #define MCA_ERRCODE_BUS_LL_SH 0 /* Cache Level */
293 #define MCA_ERRCODE_BUS_LL_MASK (0x3 << MCA_ERRCODE_BUS_LL_SH)
294 
295 /* Int. Unclassified Errors 0000 01UU 0000 0000 */
296 #define MCA_ERRCODE_INT_DETECT 0xfc00
297 #define MCA_ERRCODE_INT_UU_SH 8 /* Internal Error Type */
298 #define MCA_ERRCODE_INT_UU_MASK (0x3 << MCA_ERRCODE_INT_UU_SH)
299 
300 #define MCA_BANK_LS 0 /* Load-store, including DC */
301 #define MCA_BANK_IF 1 /* Instruction Fetch, including IC */
302 #define MCA_BANK_CU 2 /* Combined Unit, including L2 */
303 /* bank 3 reserved */
304 #define MCA_BANK_NB 4 /* Northbridge, including IO link */
305 #define MCA_BANK_EX 5 /* Execution Unit */
306 #define MCA_BANK_FP 6 /* Floating Point */
307 
314 };
315 
316 static inline enum mca_err_code_types mca_err_type(msr_t reg)
317 {
318  uint16_t error = mca_err_code(reg);
319  if (error & MCA_ERRCODE_BUS_DETECT) /* this order must be maintained */
320  return MCA_ERRTYPE_BUS;
321  if (error & MCA_ERRCODE_INT_DETECT)
322  return MCA_ERRTYPE_INT;
323  if (error & MCA_ERRCODE_MEM_DETECT)
324  return MCA_ERRTYPE_MEM;
325  if (error & MCA_ERRCODE_TLB_DETECT)
326  return MCA_ERRTYPE_TLB;
327  return MCA_ERRTYPE_UNKNOWN;
328 }
329 
330 /**
331  * Helper for reading a MSR
332  *
333  * @param[in] reg The MSR.
334  */
335 static inline uint64_t msr_read(unsigned int reg)
336 {
337  msr_t msr = rdmsr(reg);
338  return (((uint64_t)msr.hi << 32) | msr.lo);
339 }
340 
341 /**
342  * Helper for writing a MSR
343  *
344  * @param[in] reg The MSR.
345  * @param[in] value The value to be written to the MSR.
346  */
347 static inline void msr_write(unsigned int reg, uint64_t value)
348 {
349  msr_t msr = {
350  .lo = (unsigned int)value,
351  .hi = (unsigned int)(value >> 32)
352  };
353  wrmsr(reg, msr);
354 }
355 
356 /**
357  * Helper for (un)setting MSR bitmasks
358  *
359  * @param[in] reg The MSR.
360  * @param[in] unset Bitmask with ones to the bits to unset from the MSR.
361  * @param[in] set Bitmask with ones to the bits to set from the MSR.
362  */
363 static inline void msr_unset_and_set(unsigned int reg, uint64_t unset, uint64_t set)
364 {
365  msr_t msr;
366 
367  msr = rdmsr(reg);
368  msr.lo &= (unsigned int)~unset;
369  msr.hi &= (unsigned int)~(unset >> 32);
370  msr.lo |= (unsigned int)set;
371  msr.hi |= (unsigned int)(set >> 32);
372  wrmsr(reg, msr);
373 }
374 
375 /**
376  * Helper for setting MSR bitmasks
377  *
378  * @param[in] reg The MSR.
379  * @param[in] set Bitmask with ones to the bits to set from the MSR.
380  */
381 static inline void msr_set(unsigned int reg, uint64_t set)
382 {
383  msr_unset_and_set(reg, 0, set);
384 }
385 
386 /**
387  * Helper for unsetting MSR bitmasks
388  *
389  * @param[in] reg The MSR.
390  * @param[in] unset Bitmask with ones to the bits to unset from the MSR.
391  */
392 static inline void msr_unset(unsigned int reg, uint64_t unset)
393 {
394  msr_unset_and_set(reg, unset, 0);
395 }
396 
397 #endif /* __ASSEMBLER__ */
398 #endif /* CPU_X86_MSR_H */
pte_t value
Definition: mmu.c:91
#define __always_inline
Definition: compiler.h:35
#define MCA_STATUS_HI_UC
Definition: msr.h:65
#define MCA_STATUS_HI_PCC
Definition: msr.h:69
static unsigned int mca_get_bank_count(void)
Definition: msr.h:169
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
static int mca_uecc(msr_t msr)
Definition: msr.h:232
static enum mca_err_code_types mca_err_type(msr_t reg)
Definition: msr.h:316
#define MCA_STATUS_HI_MISCV
Definition: msr.h:67
#define IA32_MC_STATUS(bank)
Definition: msr.h:62
static void msr_unset(unsigned int reg, uint64_t unset)
Helper for unsetting MSR bitmasks.
Definition: msr.h:392
static int mca_valid(msr_t msr)
Definition: msr.h:187
static int mca_defd(msr_t msr)
Definition: msr.h:237
#define MCA_STATUS_HI_UECC
Definition: msr.h:72
#define MCA_STATUS_HI_EN
Definition: msr.h:66
#define MCA_STATUS_HI_OVERFLOW
Definition: msr.h:64
static void msr_write(unsigned int reg, uint64_t value)
Helper for writing a MSR.
Definition: msr.h:347
static void mca_clear_status(void)
Definition: msr.h:176
static int mca_idv(msr_t msr)
Definition: msr.h:222
#define MCA_STATUS_HI_CECC
Definition: msr.h:71
static int mca_sublink(msr_t msr)
Definition: msr.h:247
static int mca_en(msr_t msr)
Definition: msr.h:202
#define MCA_BANKS_MASK
Definition: msr.h:41
#define MCA_STATUS_HI_POISON
Definition: msr.h:74
#define MCA_STATUS_LO_ERRCODE_MASK
Definition: msr.h:79
struct msr_struct msr_t
#define MCA_ERRCODE_MEM_DETECT
Definition: msr.h:274
static void msr_set(unsigned int reg, uint64_t set)
Helper for setting MSR bitmasks.
Definition: msr.h:381
static uint64_t msr_read(unsigned int reg)
Helper for reading a MSR.
Definition: msr.h:335
#define MCA_STATUS_LO_ERRCODE_EXT_MASK
Definition: msr.h:78
static int mca_over(msr_t msr)
Definition: msr.h:192
#define MCA_STATUS_HI_VAL
Definition: msr.h:63
static int mca_uc(msr_t msr)
Definition: msr.h:197
#define MCA_STATUS_HI_ADDRV
Definition: msr.h:68
static uint16_t mca_err_code(msr_t reg)
Definition: msr.h:252
#define MCA_STATUS_HI_SUBLINK
Definition: msr.h:75
#define MCA_ERRCODE_BUS_DETECT
Definition: msr.h:283
static int mca_poison(msr_t msr)
Definition: msr.h:242
static void msr_unset_and_set(unsigned int reg, uint64_t unset, uint64_t set)
Helper for (un)setting MSR bitmasks.
Definition: msr.h:363
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
struct msrinit_struct msrinit_t
#define IA32_MCG_CAP
Definition: msr.h:39
static uint16_t mca_err_extcode(msr_t reg)
Definition: msr.h:257
#define MCA_ERRCODE_INT_DETECT
Definition: msr.h:296
static int mca_miscv(msr_t msr)
Definition: msr.h:207
static int mca_pcc(msr_t msr)
Definition: msr.h:217
#define MCA_STATUS_HI_COREID_VAL
Definition: msr.h:70
mca_err_code_types
Definition: msr.h:308
@ MCA_ERRTYPE_UNKNOWN
Definition: msr.h:309
@ MCA_ERRTYPE_INT
Definition: msr.h:313
@ MCA_ERRTYPE_BUS
Definition: msr.h:312
@ MCA_ERRTYPE_TLB
Definition: msr.h:310
@ MCA_ERRTYPE_MEM
Definition: msr.h:311
#define MCA_ERRCODE_TLB_DETECT
Definition: msr.h:267
static int mca_cecc(msr_t msr)
Definition: msr.h:227
#define MCA_STATUS_HI_DEFERRED
Definition: msr.h:73
static int mca_addrv(msr_t msr)
Definition: msr.h:212
result
Definition: mrc_cache.c:35
msr_t soc_msr_read(unsigned int index)
Definition: reg_access.c:430
void soc_msr_write(unsigned int index, msr_t msr)
Definition: reg_access.c:452
unsigned short uint16_t
Definition: stdint.h:11
unsigned long long uint64_t
Definition: stdint.h:17
unsigned int hi
Definition: msr.h:112
unsigned int lo
Definition: msr.h:111
msr_t msr
Definition: msr.h:117
unsigned int index
Definition: msr.h:116