mce-apei.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Bridge between MCE and APEI
  3. *
  4. * On some machine, corrected memory errors are reported via APEI
  5. * generic hardware error source (GHES) instead of corrected Machine
  6. * Check. These corrected memory errors can be reported to user space
  7. * through /dev/mcelog via faking a corrected Machine Check, so that
  8. * the error memory page can be offlined by /sbin/mcelog if the error
  9. * count for one page is beyond the threshold.
  10. *
  11. * For fatal MCE, save MCE record into persistent storage via ERST, so
  12. * that the MCE record can be logged after reboot via ERST.
  13. *
  14. * Copyright 2010 Intel Corp.
  15. * Author: Huang Ying <ying.huang@intel.com>
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License version
  19. * 2 as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <linux/export.h>
  31. #include <linux/kernel.h>
  32. #include <linux/acpi.h>
  33. #include <linux/cper.h>
  34. #include <acpi/apei.h>
  35. #include <acpi/ghes.h>
  36. #include <asm/mce.h>
  37. #include "mce-internal.h"
  38. void apei_mce_report_mem_error(int severity, struct cper_sec_mem_err *mem_err)
  39. {
  40. struct mce m;
  41. if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
  42. return;
  43. mce_setup(&m);
  44. m.bank = -1;
  45. /* Fake a memory read error with unknown channel */
  46. m.status = MCI_STATUS_VAL | MCI_STATUS_EN | MCI_STATUS_ADDRV | 0x9f;
  47. if (severity >= GHES_SEV_RECOVERABLE)
  48. m.status |= MCI_STATUS_UC;
  49. if (severity >= GHES_SEV_PANIC)
  50. m.status |= MCI_STATUS_PCC;
  51. m.addr = mem_err->physical_addr;
  52. mce_log(&m);
  53. }
  54. EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
  55. #define CPER_CREATOR_MCE \
  56. UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  57. 0x64, 0x90, 0xb8, 0x9d)
  58. #define CPER_SECTION_TYPE_MCE \
  59. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  60. 0x04, 0x4a, 0x38, 0xfc)
  61. /*
  62. * CPER specification (in UEFI specification 2.3 appendix N) requires
  63. * byte-packed.
  64. */
  65. struct cper_mce_record {
  66. struct cper_record_header hdr;
  67. struct cper_section_descriptor sec_hdr;
  68. struct mce mce;
  69. } __packed;
  70. int apei_write_mce(struct mce *m)
  71. {
  72. struct cper_mce_record rcd;
  73. memset(&rcd, 0, sizeof(rcd));
  74. memcpy(rcd.hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  75. rcd.hdr.revision = CPER_RECORD_REV;
  76. rcd.hdr.signature_end = CPER_SIG_END;
  77. rcd.hdr.section_count = 1;
  78. rcd.hdr.error_severity = CPER_SEV_FATAL;
  79. /* timestamp, platform_id, partition_id are all invalid */
  80. rcd.hdr.validation_bits = 0;
  81. rcd.hdr.record_length = sizeof(rcd);
  82. rcd.hdr.creator_id = CPER_CREATOR_MCE;
  83. rcd.hdr.notification_type = CPER_NOTIFY_MCE;
  84. rcd.hdr.record_id = cper_next_record_id();
  85. rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  86. rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
  87. rcd.sec_hdr.section_length = sizeof(rcd.mce);
  88. rcd.sec_hdr.revision = CPER_SEC_REV;
  89. /* fru_id and fru_text is invalid */
  90. rcd.sec_hdr.validation_bits = 0;
  91. rcd.sec_hdr.flags = CPER_SEC_PRIMARY;
  92. rcd.sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  93. rcd.sec_hdr.section_severity = CPER_SEV_FATAL;
  94. memcpy(&rcd.mce, m, sizeof(*m));
  95. return erst_write(&rcd.hdr);
  96. }
  97. ssize_t apei_read_mce(struct mce *m, u64 *record_id)
  98. {
  99. struct cper_mce_record rcd;
  100. int rc, pos;
  101. rc = erst_get_record_id_begin(&pos);
  102. if (rc)
  103. return rc;
  104. retry:
  105. rc = erst_get_record_id_next(&pos, record_id);
  106. if (rc)
  107. goto out;
  108. /* no more record */
  109. if (*record_id == APEI_ERST_INVALID_RECORD_ID)
  110. goto out;
  111. rc = erst_read(*record_id, &rcd.hdr, sizeof(rcd));
  112. /* someone else has cleared the record, try next one */
  113. if (rc == -ENOENT)
  114. goto retry;
  115. else if (rc < 0)
  116. goto out;
  117. /* try to skip other type records in storage */
  118. else if (rc != sizeof(rcd) ||
  119. uuid_le_cmp(rcd.hdr.creator_id, CPER_CREATOR_MCE))
  120. goto retry;
  121. memcpy(m, &rcd.mce, sizeof(*m));
  122. rc = sizeof(*m);
  123. out:
  124. erst_get_record_id_end();
  125. return rc;
  126. }
  127. /* Check whether there is record in ERST */
  128. int apei_check_mce(void)
  129. {
  130. return erst_get_record_count();
  131. }
  132. int apei_clear_mce(u64 record_id)
  133. {
  134. return erst_clear(record_id);
  135. }