internal.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __X86_MCE_INTERNAL_H__
  3. #define __X86_MCE_INTERNAL_H__
  4. #undef pr_fmt
  5. #define pr_fmt(fmt) "mce: " fmt
  6. #include <linux/device.h>
  7. #include <asm/mce.h>
  8. enum severity_level {
  9. MCE_NO_SEVERITY,
  10. MCE_DEFERRED_SEVERITY,
  11. MCE_UCNA_SEVERITY = MCE_DEFERRED_SEVERITY,
  12. MCE_KEEP_SEVERITY,
  13. MCE_SOME_SEVERITY,
  14. MCE_AO_SEVERITY,
  15. MCE_UC_SEVERITY,
  16. MCE_AR_SEVERITY,
  17. MCE_PANIC_SEVERITY,
  18. };
  19. extern struct blocking_notifier_head x86_mce_decoder_chain;
  20. #define INITIAL_CHECK_INTERVAL 5 * 60 /* 5 minutes */
  21. struct mce_evt_llist {
  22. struct llist_node llnode;
  23. struct mce mce;
  24. };
  25. void mce_gen_pool_process(struct work_struct *__unused);
  26. bool mce_gen_pool_empty(void);
  27. int mce_gen_pool_add(struct mce *mce);
  28. int mce_gen_pool_init(void);
  29. struct llist_node *mce_gen_pool_prepare_records(void);
  30. int mce_severity(struct mce *a, struct pt_regs *regs, char **msg, bool is_excp);
  31. struct dentry *mce_get_debugfs_dir(void);
  32. extern mce_banks_t mce_banks_ce_disabled;
  33. #ifdef CONFIG_X86_MCE_INTEL
  34. void mce_intel_handle_storm(int bank, bool on);
  35. void cmci_disable_bank(int bank);
  36. void intel_init_cmci(void);
  37. void intel_init_lmce(void);
  38. void intel_clear_lmce(void);
  39. bool intel_filter_mce(struct mce *m);
  40. bool intel_mce_usable_address(struct mce *m);
  41. #else
  42. static inline void mce_intel_handle_storm(int bank, bool on) { }
  43. static inline void cmci_disable_bank(int bank) { }
  44. static inline void intel_init_cmci(void) { }
  45. static inline void intel_init_lmce(void) { }
  46. static inline void intel_clear_lmce(void) { }
  47. static inline bool intel_filter_mce(struct mce *m) { return false; }
  48. static inline bool intel_mce_usable_address(struct mce *m) { return false; }
  49. #endif
  50. void mce_timer_kick(bool storm);
  51. #ifdef CONFIG_X86_MCE_THRESHOLD
  52. void cmci_storm_begin(unsigned int bank);
  53. void cmci_storm_end(unsigned int bank);
  54. void mce_track_storm(struct mce *mce);
  55. void mce_inherit_storm(unsigned int bank);
  56. bool mce_get_storm_mode(void);
  57. void mce_set_storm_mode(bool storm);
  58. #else
  59. static inline void cmci_storm_begin(unsigned int bank) {}
  60. static inline void cmci_storm_end(unsigned int bank) {}
  61. static inline void mce_track_storm(struct mce *mce) {}
  62. static inline void mce_inherit_storm(unsigned int bank) {}
  63. static inline bool mce_get_storm_mode(void) { return false; }
  64. static inline void mce_set_storm_mode(bool storm) {}
  65. #endif
  66. /*
  67. * history: Bitmask tracking errors occurrence. Each set bit
  68. * represents an error seen.
  69. *
  70. * timestamp: Last time (in jiffies) that the bank was polled.
  71. * in_storm_mode: Is this bank in storm mode?
  72. * poll_only: Bank does not support CMCI, skip storm tracking.
  73. */
  74. struct storm_bank {
  75. u64 history;
  76. u64 timestamp;
  77. bool in_storm_mode;
  78. bool poll_only;
  79. };
  80. #define NUM_HISTORY_BITS (sizeof(u64) * BITS_PER_BYTE)
  81. /* How many errors within the history buffer mark the start of a storm. */
  82. #define STORM_BEGIN_THRESHOLD 5
  83. /*
  84. * How many polls of machine check bank without an error before declaring
  85. * the storm is over. Since it is tracked by the bitmasks in the history
  86. * field of struct storm_bank the mask is 30 bits [0 ... 29].
  87. */
  88. #define STORM_END_POLL_THRESHOLD 29
  89. /*
  90. * banks: per-cpu, per-bank details
  91. * stormy_bank_count: count of MC banks in storm state
  92. * poll_mode: CPU is in poll mode
  93. */
  94. struct mca_storm_desc {
  95. struct storm_bank banks[MAX_NR_BANKS];
  96. u8 stormy_bank_count;
  97. bool poll_mode;
  98. };
  99. DECLARE_PER_CPU(struct mca_storm_desc, storm_desc);
  100. #ifdef CONFIG_ACPI_APEI
  101. int apei_write_mce(struct mce *m);
  102. ssize_t apei_read_mce(struct mce *m, u64 *record_id);
  103. int apei_check_mce(void);
  104. int apei_clear_mce(u64 record_id);
  105. #else
  106. static inline int apei_write_mce(struct mce *m)
  107. {
  108. return -EINVAL;
  109. }
  110. static inline ssize_t apei_read_mce(struct mce *m, u64 *record_id)
  111. {
  112. return 0;
  113. }
  114. static inline int apei_check_mce(void)
  115. {
  116. return 0;
  117. }
  118. static inline int apei_clear_mce(u64 record_id)
  119. {
  120. return -EINVAL;
  121. }
  122. #endif
  123. /*
  124. * We consider records to be equivalent if bank+status+addr+misc all match.
  125. * This is only used when the system is going down because of a fatal error
  126. * to avoid cluttering the console log with essentially repeated information.
  127. * In normal processing all errors seen are logged.
  128. */
  129. static inline bool mce_cmp(struct mce *m1, struct mce *m2)
  130. {
  131. return m1->bank != m2->bank ||
  132. m1->status != m2->status ||
  133. m1->addr != m2->addr ||
  134. m1->misc != m2->misc;
  135. }
  136. extern struct device_attribute dev_attr_trigger;
  137. #ifdef CONFIG_X86_MCELOG_LEGACY
  138. void mce_work_trigger(void);
  139. void mce_register_injector_chain(struct notifier_block *nb);
  140. void mce_unregister_injector_chain(struct notifier_block *nb);
  141. #else
  142. static inline void mce_work_trigger(void) { }
  143. static inline void mce_register_injector_chain(struct notifier_block *nb) { }
  144. static inline void mce_unregister_injector_chain(struct notifier_block *nb) { }
  145. #endif
  146. struct mca_config {
  147. __u64 lmce_disabled : 1,
  148. disabled : 1,
  149. ser : 1,
  150. recovery : 1,
  151. bios_cmci_threshold : 1,
  152. /* Proper #MC exception handler is set */
  153. initialized : 1,
  154. __reserved : 58;
  155. bool dont_log_ce;
  156. bool cmci_disabled;
  157. bool ignore_ce;
  158. bool print_all;
  159. int monarch_timeout;
  160. int panic_timeout;
  161. u32 rip_msr;
  162. s8 bootlog;
  163. };
  164. extern struct mca_config mca_cfg;
  165. DECLARE_PER_CPU_READ_MOSTLY(unsigned int, mce_num_banks);
  166. struct mce_vendor_flags {
  167. /*
  168. * Indicates that overflow conditions are not fatal, when set.
  169. */
  170. __u64 overflow_recov : 1,
  171. /*
  172. * (AMD) SUCCOR stands for S/W UnCorrectable error COntainment and
  173. * Recovery. It indicates support for data poisoning in HW and deferred
  174. * error interrupts.
  175. */
  176. succor : 1,
  177. /*
  178. * (AMD) SMCA: This bit indicates support for Scalable MCA which expands
  179. * the register space for each MCA bank and also increases number of
  180. * banks. Also, to accommodate the new banks and registers, the MCA
  181. * register space is moved to a new MSR range.
  182. */
  183. smca : 1,
  184. /* Zen IFU quirk */
  185. zen_ifu_quirk : 1,
  186. /* AMD-style error thresholding banks present. */
  187. amd_threshold : 1,
  188. /* Pentium, family 5-style MCA */
  189. p5 : 1,
  190. /* Centaur Winchip C6-style MCA */
  191. winchip : 1,
  192. /* SandyBridge IFU quirk */
  193. snb_ifu_quirk : 1,
  194. /* Skylake, Cascade Lake, Cooper Lake REP;MOVS* quirk */
  195. skx_repmov_quirk : 1,
  196. __reserved_0 : 55;
  197. };
  198. extern struct mce_vendor_flags mce_flags;
  199. struct mce_bank {
  200. /* subevents to enable */
  201. u64 ctl;
  202. /* initialise bank? */
  203. __u64 init : 1,
  204. /*
  205. * (AMD) MCA_CONFIG[McaLsbInStatusSupported]: When set, this bit indicates
  206. * the LSB field is found in MCA_STATUS and not in MCA_ADDR.
  207. */
  208. lsb_in_status : 1,
  209. __reserved_1 : 62;
  210. };
  211. DECLARE_PER_CPU_READ_MOSTLY(struct mce_bank[MAX_NR_BANKS], mce_banks_array);
  212. enum mca_msr {
  213. MCA_CTL,
  214. MCA_STATUS,
  215. MCA_ADDR,
  216. MCA_MISC,
  217. };
  218. /* Decide whether to add MCE record to MCE event pool or filter it out. */
  219. extern bool filter_mce(struct mce *m);
  220. void mce_prep_record_common(struct mce *m);
  221. void mce_prep_record_per_cpu(unsigned int cpu, struct mce *m);
  222. #ifdef CONFIG_X86_MCE_AMD
  223. extern bool amd_filter_mce(struct mce *m);
  224. bool amd_mce_usable_address(struct mce *m);
  225. /*
  226. * If MCA_CONFIG[McaLsbInStatusSupported] is set, extract ErrAddr in bits
  227. * [56:0] of MCA_STATUS, else in bits [55:0] of MCA_ADDR.
  228. */
  229. static __always_inline void smca_extract_err_addr(struct mce *m)
  230. {
  231. u8 lsb;
  232. if (!mce_flags.smca)
  233. return;
  234. if (this_cpu_ptr(mce_banks_array)[m->bank].lsb_in_status) {
  235. lsb = (m->status >> 24) & 0x3f;
  236. m->addr &= GENMASK_ULL(56, lsb);
  237. return;
  238. }
  239. lsb = (m->addr >> 56) & 0x3f;
  240. m->addr &= GENMASK_ULL(55, lsb);
  241. }
  242. #else
  243. static inline bool amd_filter_mce(struct mce *m) { return false; }
  244. static inline bool amd_mce_usable_address(struct mce *m) { return false; }
  245. static inline void smca_extract_err_addr(struct mce *m) { }
  246. #endif
  247. #ifdef CONFIG_X86_ANCIENT_MCE
  248. void intel_p5_mcheck_init(struct cpuinfo_x86 *c);
  249. void winchip_mcheck_init(struct cpuinfo_x86 *c);
  250. noinstr void pentium_machine_check(struct pt_regs *regs);
  251. noinstr void winchip_machine_check(struct pt_regs *regs);
  252. static inline void enable_p5_mce(void) { mce_p5_enabled = 1; }
  253. #else
  254. static __always_inline void intel_p5_mcheck_init(struct cpuinfo_x86 *c) {}
  255. static __always_inline void winchip_mcheck_init(struct cpuinfo_x86 *c) {}
  256. static __always_inline void enable_p5_mce(void) {}
  257. static __always_inline void pentium_machine_check(struct pt_regs *regs) {}
  258. static __always_inline void winchip_machine_check(struct pt_regs *regs) {}
  259. #endif
  260. noinstr u64 mce_rdmsrl(u32 msr);
  261. static __always_inline u32 mca_msr_reg(int bank, enum mca_msr reg)
  262. {
  263. if (cpu_feature_enabled(X86_FEATURE_SMCA)) {
  264. switch (reg) {
  265. case MCA_CTL: return MSR_AMD64_SMCA_MCx_CTL(bank);
  266. case MCA_ADDR: return MSR_AMD64_SMCA_MCx_ADDR(bank);
  267. case MCA_MISC: return MSR_AMD64_SMCA_MCx_MISC(bank);
  268. case MCA_STATUS: return MSR_AMD64_SMCA_MCx_STATUS(bank);
  269. }
  270. }
  271. switch (reg) {
  272. case MCA_CTL: return MSR_IA32_MCx_CTL(bank);
  273. case MCA_ADDR: return MSR_IA32_MCx_ADDR(bank);
  274. case MCA_MISC: return MSR_IA32_MCx_MISC(bank);
  275. case MCA_STATUS: return MSR_IA32_MCx_STATUS(bank);
  276. }
  277. return 0;
  278. }
  279. extern void (*mc_poll_banks)(void);
  280. #endif /* __X86_MCE_INTERNAL_H__ */