mce.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_MCE_H
  3. #define _ASM_X86_MCE_H
  4. #include <uapi/asm/mce.h>
  5. /*
  6. * Machine Check support for x86
  7. */
  8. /* MCG_CAP register defines */
  9. #define MCG_BANKCNT_MASK 0xff /* Number of Banks */
  10. #define MCG_CTL_P (1ULL<<8) /* MCG_CTL register available */
  11. #define MCG_EXT_P (1ULL<<9) /* Extended registers available */
  12. #define MCG_CMCI_P (1ULL<<10) /* CMCI supported */
  13. #define MCG_EXT_CNT_MASK 0xff0000 /* Number of Extended registers */
  14. #define MCG_EXT_CNT_SHIFT 16
  15. #define MCG_EXT_CNT(c) (((c) & MCG_EXT_CNT_MASK) >> MCG_EXT_CNT_SHIFT)
  16. #define MCG_SER_P (1ULL<<24) /* MCA recovery/new status bits */
  17. #define MCG_ELOG_P (1ULL<<26) /* Extended error log supported */
  18. #define MCG_LMCE_P (1ULL<<27) /* Local machine check supported */
  19. /* MCG_STATUS register defines */
  20. #define MCG_STATUS_RIPV (1ULL<<0) /* restart ip valid */
  21. #define MCG_STATUS_EIPV (1ULL<<1) /* ip points to correct instruction */
  22. #define MCG_STATUS_MCIP (1ULL<<2) /* machine check in progress */
  23. #define MCG_STATUS_LMCES (1ULL<<3) /* LMCE signaled */
  24. /* MCG_EXT_CTL register defines */
  25. #define MCG_EXT_CTL_LMCE_EN (1ULL<<0) /* Enable LMCE */
  26. /* MCi_STATUS register defines */
  27. #define MCI_STATUS_VAL (1ULL<<63) /* valid error */
  28. #define MCI_STATUS_OVER (1ULL<<62) /* previous errors lost */
  29. #define MCI_STATUS_UC (1ULL<<61) /* uncorrected error */
  30. #define MCI_STATUS_EN (1ULL<<60) /* error enabled */
  31. #define MCI_STATUS_MISCV (1ULL<<59) /* misc error reg. valid */
  32. #define MCI_STATUS_ADDRV (1ULL<<58) /* addr reg. valid */
  33. #define MCI_STATUS_PCC (1ULL<<57) /* processor context corrupt */
  34. #define MCI_STATUS_S (1ULL<<56) /* Signaled machine check */
  35. #define MCI_STATUS_AR (1ULL<<55) /* Action required */
  36. /* AMD-specific bits */
  37. #define MCI_STATUS_TCC (1ULL<<55) /* Task context corrupt */
  38. #define MCI_STATUS_SYNDV (1ULL<<53) /* synd reg. valid */
  39. #define MCI_STATUS_DEFERRED (1ULL<<44) /* uncorrected error, deferred exception */
  40. #define MCI_STATUS_POISON (1ULL<<43) /* access poisonous data */
  41. /*
  42. * McaX field if set indicates a given bank supports MCA extensions:
  43. * - Deferred error interrupt type is specifiable by bank.
  44. * - MCx_MISC0[BlkPtr] field indicates presence of extended MISC registers,
  45. * But should not be used to determine MSR numbers.
  46. * - TCC bit is present in MCx_STATUS.
  47. */
  48. #define MCI_CONFIG_MCAX 0x1
  49. #define MCI_IPID_MCATYPE 0xFFFF0000
  50. #define MCI_IPID_HWID 0xFFF
  51. /*
  52. * Note that the full MCACOD field of IA32_MCi_STATUS MSR is
  53. * bits 15:0. But bit 12 is the 'F' bit, defined for corrected
  54. * errors to indicate that errors are being filtered by hardware.
  55. * We should mask out bit 12 when looking for specific signatures
  56. * of uncorrected errors - so the F bit is deliberately skipped
  57. * in this #define.
  58. */
  59. #define MCACOD 0xefff /* MCA Error Code */
  60. /* Architecturally defined codes from SDM Vol. 3B Chapter 15 */
  61. #define MCACOD_SCRUB 0x00C0 /* 0xC0-0xCF Memory Scrubbing */
  62. #define MCACOD_SCRUBMSK 0xeff0 /* Skip bit 12 ('F' bit) */
  63. #define MCACOD_L3WB 0x017A /* L3 Explicit Writeback */
  64. #define MCACOD_DATA 0x0134 /* Data Load */
  65. #define MCACOD_INSTR 0x0150 /* Instruction Fetch */
  66. /* MCi_MISC register defines */
  67. #define MCI_MISC_ADDR_LSB(m) ((m) & 0x3f)
  68. #define MCI_MISC_ADDR_MODE(m) (((m) >> 6) & 7)
  69. #define MCI_MISC_ADDR_SEGOFF 0 /* segment offset */
  70. #define MCI_MISC_ADDR_LINEAR 1 /* linear address */
  71. #define MCI_MISC_ADDR_PHYS 2 /* physical address */
  72. #define MCI_MISC_ADDR_MEM 3 /* memory address */
  73. #define MCI_MISC_ADDR_GENERIC 7 /* generic */
  74. /* CTL2 register defines */
  75. #define MCI_CTL2_CMCI_EN (1ULL << 30)
  76. #define MCI_CTL2_CMCI_THRESHOLD_MASK 0x7fffULL
  77. #define MCJ_CTX_MASK 3
  78. #define MCJ_CTX(flags) ((flags) & MCJ_CTX_MASK)
  79. #define MCJ_CTX_RANDOM 0 /* inject context: random */
  80. #define MCJ_CTX_PROCESS 0x1 /* inject context: process */
  81. #define MCJ_CTX_IRQ 0x2 /* inject context: IRQ */
  82. #define MCJ_NMI_BROADCAST 0x4 /* do NMI broadcasting */
  83. #define MCJ_EXCEPTION 0x8 /* raise as exception */
  84. #define MCJ_IRQ_BROADCAST 0x10 /* do IRQ broadcasting */
  85. #define MCE_OVERFLOW 0 /* bit 0 in flags means overflow */
  86. #define MCE_LOG_LEN 32
  87. #define MCE_LOG_SIGNATURE "MACHINECHECK"
  88. /* AMD Scalable MCA */
  89. #define MSR_AMD64_SMCA_MC0_CTL 0xc0002000
  90. #define MSR_AMD64_SMCA_MC0_STATUS 0xc0002001
  91. #define MSR_AMD64_SMCA_MC0_ADDR 0xc0002002
  92. #define MSR_AMD64_SMCA_MC0_MISC0 0xc0002003
  93. #define MSR_AMD64_SMCA_MC0_CONFIG 0xc0002004
  94. #define MSR_AMD64_SMCA_MC0_IPID 0xc0002005
  95. #define MSR_AMD64_SMCA_MC0_SYND 0xc0002006
  96. #define MSR_AMD64_SMCA_MC0_DESTAT 0xc0002008
  97. #define MSR_AMD64_SMCA_MC0_DEADDR 0xc0002009
  98. #define MSR_AMD64_SMCA_MC0_MISC1 0xc000200a
  99. #define MSR_AMD64_SMCA_MCx_CTL(x) (MSR_AMD64_SMCA_MC0_CTL + 0x10*(x))
  100. #define MSR_AMD64_SMCA_MCx_STATUS(x) (MSR_AMD64_SMCA_MC0_STATUS + 0x10*(x))
  101. #define MSR_AMD64_SMCA_MCx_ADDR(x) (MSR_AMD64_SMCA_MC0_ADDR + 0x10*(x))
  102. #define MSR_AMD64_SMCA_MCx_MISC(x) (MSR_AMD64_SMCA_MC0_MISC0 + 0x10*(x))
  103. #define MSR_AMD64_SMCA_MCx_CONFIG(x) (MSR_AMD64_SMCA_MC0_CONFIG + 0x10*(x))
  104. #define MSR_AMD64_SMCA_MCx_IPID(x) (MSR_AMD64_SMCA_MC0_IPID + 0x10*(x))
  105. #define MSR_AMD64_SMCA_MCx_SYND(x) (MSR_AMD64_SMCA_MC0_SYND + 0x10*(x))
  106. #define MSR_AMD64_SMCA_MCx_DESTAT(x) (MSR_AMD64_SMCA_MC0_DESTAT + 0x10*(x))
  107. #define MSR_AMD64_SMCA_MCx_DEADDR(x) (MSR_AMD64_SMCA_MC0_DEADDR + 0x10*(x))
  108. #define MSR_AMD64_SMCA_MCx_MISCy(x, y) ((MSR_AMD64_SMCA_MC0_MISC1 + y) + (0x10*(x)))
  109. /*
  110. * This structure contains all data related to the MCE log. Also
  111. * carries a signature to make it easier to find from external
  112. * debugging tools. Each entry is only valid when its finished flag
  113. * is set.
  114. */
  115. struct mce_log_buffer {
  116. char signature[12]; /* "MACHINECHECK" */
  117. unsigned len; /* = MCE_LOG_LEN */
  118. unsigned next;
  119. unsigned flags;
  120. unsigned recordlen; /* length of struct mce */
  121. struct mce entry[MCE_LOG_LEN];
  122. };
  123. struct mca_config {
  124. bool dont_log_ce;
  125. bool cmci_disabled;
  126. bool lmce_disabled;
  127. bool ignore_ce;
  128. bool disabled;
  129. bool ser;
  130. bool recovery;
  131. bool bios_cmci_threshold;
  132. u8 banks;
  133. s8 bootlog;
  134. int tolerant;
  135. int monarch_timeout;
  136. int panic_timeout;
  137. u32 rip_msr;
  138. };
  139. struct mce_vendor_flags {
  140. /*
  141. * Indicates that overflow conditions are not fatal, when set.
  142. */
  143. __u64 overflow_recov : 1,
  144. /*
  145. * (AMD) SUCCOR stands for S/W UnCorrectable error COntainment and
  146. * Recovery. It indicates support for data poisoning in HW and deferred
  147. * error interrupts.
  148. */
  149. succor : 1,
  150. /*
  151. * (AMD) SMCA: This bit indicates support for Scalable MCA which expands
  152. * the register space for each MCA bank and also increases number of
  153. * banks. Also, to accommodate the new banks and registers, the MCA
  154. * register space is moved to a new MSR range.
  155. */
  156. smca : 1,
  157. __reserved_0 : 61;
  158. };
  159. struct mca_msr_regs {
  160. u32 (*ctl) (int bank);
  161. u32 (*status) (int bank);
  162. u32 (*addr) (int bank);
  163. u32 (*misc) (int bank);
  164. };
  165. extern struct mce_vendor_flags mce_flags;
  166. extern struct mca_msr_regs msr_ops;
  167. enum mce_notifier_prios {
  168. MCE_PRIO_FIRST = INT_MAX,
  169. MCE_PRIO_SRAO = INT_MAX - 1,
  170. MCE_PRIO_EXTLOG = INT_MAX - 2,
  171. MCE_PRIO_NFIT = INT_MAX - 3,
  172. MCE_PRIO_EDAC = INT_MAX - 4,
  173. MCE_PRIO_MCELOG = 1,
  174. MCE_PRIO_LOWEST = 0,
  175. };
  176. extern void mce_register_decode_chain(struct notifier_block *nb);
  177. extern void mce_unregister_decode_chain(struct notifier_block *nb);
  178. #include <linux/percpu.h>
  179. #include <linux/atomic.h>
  180. extern int mce_p5_enabled;
  181. #ifdef CONFIG_X86_MCE
  182. int mcheck_init(void);
  183. void mcheck_cpu_init(struct cpuinfo_x86 *c);
  184. void mcheck_cpu_clear(struct cpuinfo_x86 *c);
  185. void mcheck_vendor_init_severity(void);
  186. #else
  187. static inline int mcheck_init(void) { return 0; }
  188. static inline void mcheck_cpu_init(struct cpuinfo_x86 *c) {}
  189. static inline void mcheck_cpu_clear(struct cpuinfo_x86 *c) {}
  190. static inline void mcheck_vendor_init_severity(void) {}
  191. #endif
  192. #ifdef CONFIG_X86_ANCIENT_MCE
  193. void intel_p5_mcheck_init(struct cpuinfo_x86 *c);
  194. void winchip_mcheck_init(struct cpuinfo_x86 *c);
  195. static inline void enable_p5_mce(void) { mce_p5_enabled = 1; }
  196. #else
  197. static inline void intel_p5_mcheck_init(struct cpuinfo_x86 *c) {}
  198. static inline void winchip_mcheck_init(struct cpuinfo_x86 *c) {}
  199. static inline void enable_p5_mce(void) {}
  200. #endif
  201. void mce_setup(struct mce *m);
  202. void mce_log(struct mce *m);
  203. DECLARE_PER_CPU(struct device *, mce_device);
  204. /*
  205. * Maximum banks number.
  206. * This is the limit of the current register layout on
  207. * Intel CPUs.
  208. */
  209. #define MAX_NR_BANKS 32
  210. #ifdef CONFIG_X86_MCE_INTEL
  211. void mce_intel_feature_init(struct cpuinfo_x86 *c);
  212. void mce_intel_feature_clear(struct cpuinfo_x86 *c);
  213. void cmci_clear(void);
  214. void cmci_reenable(void);
  215. void cmci_rediscover(void);
  216. void cmci_recheck(void);
  217. #else
  218. static inline void mce_intel_feature_init(struct cpuinfo_x86 *c) { }
  219. static inline void mce_intel_feature_clear(struct cpuinfo_x86 *c) { }
  220. static inline void cmci_clear(void) {}
  221. static inline void cmci_reenable(void) {}
  222. static inline void cmci_rediscover(void) {}
  223. static inline void cmci_recheck(void) {}
  224. #endif
  225. #ifdef CONFIG_X86_MCE_AMD
  226. void mce_amd_feature_init(struct cpuinfo_x86 *c);
  227. int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr);
  228. #else
  229. static inline void mce_amd_feature_init(struct cpuinfo_x86 *c) { }
  230. static inline int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr) { return -EINVAL; };
  231. #endif
  232. int mce_available(struct cpuinfo_x86 *c);
  233. bool mce_is_memory_error(struct mce *m);
  234. DECLARE_PER_CPU(unsigned, mce_exception_count);
  235. DECLARE_PER_CPU(unsigned, mce_poll_count);
  236. typedef DECLARE_BITMAP(mce_banks_t, MAX_NR_BANKS);
  237. DECLARE_PER_CPU(mce_banks_t, mce_poll_banks);
  238. enum mcp_flags {
  239. MCP_TIMESTAMP = BIT(0), /* log time stamp */
  240. MCP_UC = BIT(1), /* log uncorrected errors */
  241. MCP_DONTLOG = BIT(2), /* only clear, don't log */
  242. };
  243. bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b);
  244. int mce_notify_irq(void);
  245. DECLARE_PER_CPU(struct mce, injectm);
  246. /* Disable CMCI/polling for MCA bank claimed by firmware */
  247. extern void mce_disable_bank(int bank);
  248. /*
  249. * Exception handler
  250. */
  251. /* Call the installed machine check handler for this CPU setup. */
  252. extern void (*machine_check_vector)(struct pt_regs *, long error_code);
  253. void do_machine_check(struct pt_regs *, long);
  254. /*
  255. * Threshold handler
  256. */
  257. extern void (*mce_threshold_vector)(void);
  258. /* Deferred error interrupt handler */
  259. extern void (*deferred_error_int_vector)(void);
  260. /*
  261. * Thermal handler
  262. */
  263. void intel_init_thermal(struct cpuinfo_x86 *c);
  264. /* Interrupt Handler for core thermal thresholds */
  265. extern int (*platform_thermal_notify)(__u64 msr_val);
  266. /* Interrupt Handler for package thermal thresholds */
  267. extern int (*platform_thermal_package_notify)(__u64 msr_val);
  268. /* Callback support of rate control, return true, if
  269. * callback has rate control */
  270. extern bool (*platform_thermal_package_rate_control)(void);
  271. #ifdef CONFIG_X86_THERMAL_VECTOR
  272. extern void mcheck_intel_therm_init(void);
  273. #else
  274. static inline void mcheck_intel_therm_init(void) { }
  275. #endif
  276. /*
  277. * Used by APEI to report memory error via /dev/mcelog
  278. */
  279. struct cper_sec_mem_err;
  280. extern void apei_mce_report_mem_error(int corrected,
  281. struct cper_sec_mem_err *mem_err);
  282. /*
  283. * Enumerate new IP types and HWID values in AMD processors which support
  284. * Scalable MCA.
  285. */
  286. #ifdef CONFIG_X86_MCE_AMD
  287. /* These may be used by multiple smca_hwid_mcatypes */
  288. enum smca_bank_types {
  289. SMCA_LS = 0, /* Load Store */
  290. SMCA_IF, /* Instruction Fetch */
  291. SMCA_L2_CACHE, /* L2 Cache */
  292. SMCA_DE, /* Decoder Unit */
  293. SMCA_EX, /* Execution Unit */
  294. SMCA_FP, /* Floating Point */
  295. SMCA_L3_CACHE, /* L3 Cache */
  296. SMCA_CS, /* Coherent Slave */
  297. SMCA_PIE, /* Power, Interrupts, etc. */
  298. SMCA_UMC, /* Unified Memory Controller */
  299. SMCA_PB, /* Parameter Block */
  300. SMCA_PSP, /* Platform Security Processor */
  301. SMCA_SMU, /* System Management Unit */
  302. N_SMCA_BANK_TYPES
  303. };
  304. #define HWID_MCATYPE(hwid, mcatype) (((hwid) << 16) | (mcatype))
  305. struct smca_hwid {
  306. unsigned int bank_type; /* Use with smca_bank_types for easy indexing. */
  307. u32 hwid_mcatype; /* (hwid,mcatype) tuple */
  308. u32 xec_bitmap; /* Bitmap of valid ExtErrorCodes; current max is 21. */
  309. u8 count; /* Number of instances. */
  310. };
  311. struct smca_bank {
  312. struct smca_hwid *hwid;
  313. u32 id; /* Value of MCA_IPID[InstanceId]. */
  314. u8 sysfs_id; /* Value used for sysfs name. */
  315. };
  316. extern struct smca_bank smca_banks[MAX_NR_BANKS];
  317. extern const char *smca_get_long_name(enum smca_bank_types t);
  318. extern bool amd_mce_is_memory_error(struct mce *m);
  319. extern int mce_threshold_create_device(unsigned int cpu);
  320. extern int mce_threshold_remove_device(unsigned int cpu);
  321. #else
  322. static inline int mce_threshold_create_device(unsigned int cpu) { return 0; };
  323. static inline int mce_threshold_remove_device(unsigned int cpu) { return 0; };
  324. static inline bool amd_mce_is_memory_error(struct mce *m) { return false; };
  325. #endif
  326. #endif /* _ASM_X86_MCE_H */