apei.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Bridge between MCE and APEI
  4. *
  5. * On some machine, corrected memory errors are reported via APEI
  6. * generic hardware error source (GHES) instead of corrected Machine
  7. * Check. These corrected memory errors can be reported to user space
  8. * through /dev/mcelog via faking a corrected Machine Check, so that
  9. * the error memory page can be offlined by /sbin/mcelog if the error
  10. * count for one page is beyond the threshold.
  11. *
  12. * For fatal MCE, save MCE record into persistent storage via ERST, so
  13. * that the MCE record can be logged after reboot via ERST.
  14. *
  15. * Copyright 2010 Intel Corp.
  16. * Author: Huang Ying <[email protected]>
  17. */
  18. #include <linux/export.h>
  19. #include <linux/kernel.h>
  20. #include <linux/acpi.h>
  21. #include <linux/cper.h>
  22. #include <acpi/apei.h>
  23. #include <acpi/ghes.h>
  24. #include <asm/mce.h>
  25. #include "internal.h"
  26. void apei_mce_report_mem_error(int severity, struct cper_sec_mem_err *mem_err)
  27. {
  28. struct mce m;
  29. int lsb;
  30. if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
  31. return;
  32. /*
  33. * Even if the ->validation_bits are set for address mask,
  34. * to be extra safe, check and reject an error radius '0',
  35. * and fall back to the default page size.
  36. */
  37. if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
  38. lsb = find_first_bit((void *)&mem_err->physical_addr_mask, PAGE_SHIFT);
  39. else
  40. lsb = PAGE_SHIFT;
  41. mce_setup(&m);
  42. m.bank = -1;
  43. /* Fake a memory read error with unknown channel */
  44. m.status = MCI_STATUS_VAL | MCI_STATUS_EN | MCI_STATUS_ADDRV | MCI_STATUS_MISCV | 0x9f;
  45. m.misc = (MCI_MISC_ADDR_PHYS << 6) | lsb;
  46. if (severity >= GHES_SEV_RECOVERABLE)
  47. m.status |= MCI_STATUS_UC;
  48. if (severity >= GHES_SEV_PANIC) {
  49. m.status |= MCI_STATUS_PCC;
  50. m.tsc = rdtsc();
  51. }
  52. m.addr = mem_err->physical_addr;
  53. mce_log(&m);
  54. }
  55. EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
  56. int apei_smca_report_x86_error(struct cper_ia_proc_ctx *ctx_info, u64 lapic_id)
  57. {
  58. const u64 *i_mce = ((const u64 *) (ctx_info + 1));
  59. unsigned int cpu;
  60. struct mce m;
  61. if (!boot_cpu_has(X86_FEATURE_SMCA))
  62. return -EINVAL;
  63. /*
  64. * The starting address of the register array extracted from BERT must
  65. * match with the first expected register in the register layout of
  66. * SMCA address space. This address corresponds to banks's MCA_STATUS
  67. * register.
  68. *
  69. * Match any MCi_STATUS register by turning off bank numbers.
  70. */
  71. if ((ctx_info->msr_addr & MSR_AMD64_SMCA_MC0_STATUS) !=
  72. MSR_AMD64_SMCA_MC0_STATUS)
  73. return -EINVAL;
  74. /*
  75. * The register array size must be large enough to include all the
  76. * SMCA registers which need to be extracted.
  77. *
  78. * The number of registers in the register array is determined by
  79. * Register Array Size/8 as defined in UEFI spec v2.8, sec N.2.4.2.2.
  80. * The register layout is fixed and currently the raw data in the
  81. * register array includes 6 SMCA registers which the kernel can
  82. * extract.
  83. */
  84. if (ctx_info->reg_arr_size < 48)
  85. return -EINVAL;
  86. mce_setup(&m);
  87. m.extcpu = -1;
  88. m.socketid = -1;
  89. for_each_possible_cpu(cpu) {
  90. if (cpu_data(cpu).initial_apicid == lapic_id) {
  91. m.extcpu = cpu;
  92. m.socketid = cpu_data(m.extcpu).phys_proc_id;
  93. break;
  94. }
  95. }
  96. m.apicid = lapic_id;
  97. m.bank = (ctx_info->msr_addr >> 4) & 0xFF;
  98. m.status = *i_mce;
  99. m.addr = *(i_mce + 1);
  100. m.misc = *(i_mce + 2);
  101. /* Skipping MCA_CONFIG */
  102. m.ipid = *(i_mce + 4);
  103. m.synd = *(i_mce + 5);
  104. mce_log(&m);
  105. return 0;
  106. }
  107. #define CPER_CREATOR_MCE \
  108. GUID_INIT(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  109. 0x64, 0x90, 0xb8, 0x9d)
  110. #define CPER_SECTION_TYPE_MCE \
  111. GUID_INIT(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  112. 0x04, 0x4a, 0x38, 0xfc)
  113. /*
  114. * CPER specification (in UEFI specification 2.3 appendix N) requires
  115. * byte-packed.
  116. */
  117. struct cper_mce_record {
  118. struct cper_record_header hdr;
  119. struct cper_section_descriptor sec_hdr;
  120. struct mce mce;
  121. } __packed;
  122. int apei_write_mce(struct mce *m)
  123. {
  124. struct cper_mce_record rcd;
  125. memset(&rcd, 0, sizeof(rcd));
  126. memcpy(rcd.hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  127. rcd.hdr.revision = CPER_RECORD_REV;
  128. rcd.hdr.signature_end = CPER_SIG_END;
  129. rcd.hdr.section_count = 1;
  130. rcd.hdr.error_severity = CPER_SEV_FATAL;
  131. /* timestamp, platform_id, partition_id are all invalid */
  132. rcd.hdr.validation_bits = 0;
  133. rcd.hdr.record_length = sizeof(rcd);
  134. rcd.hdr.creator_id = CPER_CREATOR_MCE;
  135. rcd.hdr.notification_type = CPER_NOTIFY_MCE;
  136. rcd.hdr.record_id = cper_next_record_id();
  137. rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  138. rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
  139. rcd.sec_hdr.section_length = sizeof(rcd.mce);
  140. rcd.sec_hdr.revision = CPER_SEC_REV;
  141. /* fru_id and fru_text is invalid */
  142. rcd.sec_hdr.validation_bits = 0;
  143. rcd.sec_hdr.flags = CPER_SEC_PRIMARY;
  144. rcd.sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  145. rcd.sec_hdr.section_severity = CPER_SEV_FATAL;
  146. memcpy(&rcd.mce, m, sizeof(*m));
  147. return erst_write(&rcd.hdr);
  148. }
  149. ssize_t apei_read_mce(struct mce *m, u64 *record_id)
  150. {
  151. struct cper_mce_record rcd;
  152. int rc, pos;
  153. rc = erst_get_record_id_begin(&pos);
  154. if (rc)
  155. return rc;
  156. retry:
  157. rc = erst_get_record_id_next(&pos, record_id);
  158. if (rc)
  159. goto out;
  160. /* no more record */
  161. if (*record_id == APEI_ERST_INVALID_RECORD_ID)
  162. goto out;
  163. rc = erst_read_record(*record_id, &rcd.hdr, sizeof(rcd), sizeof(rcd),
  164. &CPER_CREATOR_MCE);
  165. /* someone else has cleared the record, try next one */
  166. if (rc == -ENOENT)
  167. goto retry;
  168. else if (rc < 0)
  169. goto out;
  170. memcpy(m, &rcd.mce, sizeof(*m));
  171. rc = sizeof(*m);
  172. out:
  173. erst_get_record_id_end();
  174. return rc;
  175. }
  176. /* Check whether there is record in ERST */
  177. int apei_check_mce(void)
  178. {
  179. return erst_get_record_count();
  180. }
  181. int apei_clear_mce(u64 record_id)
  182. {
  183. return erst_clear(record_id);
  184. }