acpi_extlog.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Extended Error Log driver
  3. *
  4. * Copyright (C) 2013 Intel Corp.
  5. * Author: Chen, Gong <gong.chen@intel.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/acpi.h>
  11. #include <linux/cper.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/edac.h>
  14. #include <asm/cpu.h>
  15. #include <asm/mce.h>
  16. #include "apei/apei-internal.h"
  17. #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
  18. #define EXTLOG_DSM_REV 0x0
  19. #define EXTLOG_FN_ADDR 0x1
  20. #define FLAG_OS_OPTIN BIT(0)
  21. #define ELOG_ENTRY_VALID (1ULL<<63)
  22. #define ELOG_ENTRY_LEN 0x1000
  23. #define EMCA_BUG \
  24. "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
  25. struct extlog_l1_head {
  26. u32 ver; /* Header Version */
  27. u32 hdr_len; /* Header Length */
  28. u64 total_len; /* entire L1 Directory length including this header */
  29. u64 elog_base; /* MCA Error Log Directory base address */
  30. u64 elog_len; /* MCA Error Log Directory length */
  31. u32 flags; /* bit 0 - OS/VMM Opt-in */
  32. u8 rev0[12];
  33. u32 entries; /* Valid L1 Directory entries per logical processor */
  34. u8 rev1[12];
  35. };
  36. static int old_edac_report_status;
  37. static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
  38. /* L1 table related physical address */
  39. static u64 elog_base;
  40. static size_t elog_size;
  41. static u64 l1_dirbase;
  42. static size_t l1_size;
  43. /* L1 table related virtual address */
  44. static void __iomem *extlog_l1_addr;
  45. static void __iomem *elog_addr;
  46. static void *elog_buf;
  47. static u64 *l1_entry_base;
  48. static u32 l1_percpu_entry;
  49. #define ELOG_IDX(cpu, bank) \
  50. (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
  51. #define ELOG_ENTRY_DATA(idx) \
  52. (*(l1_entry_base + (idx)))
  53. #define ELOG_ENTRY_ADDR(phyaddr) \
  54. (phyaddr - elog_base + (u8 *)elog_addr)
  55. static struct acpi_generic_status *extlog_elog_entry_check(int cpu, int bank)
  56. {
  57. int idx;
  58. u64 data;
  59. struct acpi_generic_status *estatus;
  60. WARN_ON(cpu < 0);
  61. idx = ELOG_IDX(cpu, bank);
  62. data = ELOG_ENTRY_DATA(idx);
  63. if ((data & ELOG_ENTRY_VALID) == 0)
  64. return NULL;
  65. data &= EXT_ELOG_ENTRY_MASK;
  66. estatus = (struct acpi_generic_status *)ELOG_ENTRY_ADDR(data);
  67. /* if no valid data in elog entry, just return */
  68. if (estatus->block_status == 0)
  69. return NULL;
  70. return estatus;
  71. }
  72. static void __print_extlog_rcd(const char *pfx,
  73. struct acpi_generic_status *estatus, int cpu)
  74. {
  75. static atomic_t seqno;
  76. unsigned int curr_seqno;
  77. char pfx_seq[64];
  78. if (!pfx) {
  79. if (estatus->error_severity <= CPER_SEV_CORRECTED)
  80. pfx = KERN_INFO;
  81. else
  82. pfx = KERN_ERR;
  83. }
  84. curr_seqno = atomic_inc_return(&seqno);
  85. snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
  86. printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
  87. cper_estatus_print(pfx_seq, estatus);
  88. }
  89. static int print_extlog_rcd(const char *pfx,
  90. struct acpi_generic_status *estatus, int cpu)
  91. {
  92. /* Not more than 2 messages every 5 seconds */
  93. static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
  94. static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
  95. struct ratelimit_state *ratelimit;
  96. if (estatus->error_severity == CPER_SEV_CORRECTED ||
  97. (estatus->error_severity == CPER_SEV_INFORMATIONAL))
  98. ratelimit = &ratelimit_corrected;
  99. else
  100. ratelimit = &ratelimit_uncorrected;
  101. if (__ratelimit(ratelimit)) {
  102. __print_extlog_rcd(pfx, estatus, cpu);
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. static int extlog_print(struct notifier_block *nb, unsigned long val,
  108. void *data)
  109. {
  110. struct mce *mce = (struct mce *)data;
  111. int bank = mce->bank;
  112. int cpu = mce->extcpu;
  113. struct acpi_generic_status *estatus;
  114. int rc;
  115. estatus = extlog_elog_entry_check(cpu, bank);
  116. if (estatus == NULL)
  117. return NOTIFY_DONE;
  118. memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
  119. /* clear record status to enable BIOS to update it again */
  120. estatus->block_status = 0;
  121. rc = print_extlog_rcd(NULL, (struct acpi_generic_status *)elog_buf, cpu);
  122. return NOTIFY_STOP;
  123. }
  124. static bool __init extlog_get_l1addr(void)
  125. {
  126. u8 uuid[16];
  127. acpi_handle handle;
  128. union acpi_object *obj;
  129. acpi_str_to_uuid(extlog_dsm_uuid, uuid);
  130. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  131. return false;
  132. if (!acpi_check_dsm(handle, uuid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
  133. return false;
  134. obj = acpi_evaluate_dsm_typed(handle, uuid, EXTLOG_DSM_REV,
  135. EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
  136. if (!obj) {
  137. return false;
  138. } else {
  139. l1_dirbase = obj->integer.value;
  140. ACPI_FREE(obj);
  141. }
  142. /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
  143. if (l1_dirbase & ((1 << 12) - 1)) {
  144. pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
  145. l1_dirbase);
  146. return false;
  147. }
  148. return true;
  149. }
  150. static struct notifier_block extlog_mce_dec = {
  151. .notifier_call = extlog_print,
  152. };
  153. static int __init extlog_init(void)
  154. {
  155. struct extlog_l1_head *l1_head;
  156. void __iomem *extlog_l1_hdr;
  157. size_t l1_hdr_size;
  158. struct resource *r;
  159. u64 cap;
  160. int rc;
  161. if (get_edac_report_status() == EDAC_REPORTING_FORCE) {
  162. pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
  163. return -EPERM;
  164. }
  165. rc = -ENODEV;
  166. rdmsrl(MSR_IA32_MCG_CAP, cap);
  167. if (!(cap & MCG_ELOG_P))
  168. return rc;
  169. if (!extlog_get_l1addr())
  170. return rc;
  171. rc = -EINVAL;
  172. /* get L1 header to fetch necessary information */
  173. l1_hdr_size = sizeof(struct extlog_l1_head);
  174. r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
  175. if (!r) {
  176. pr_warn(FW_BUG EMCA_BUG,
  177. (unsigned long long)l1_dirbase,
  178. (unsigned long long)l1_dirbase + l1_hdr_size);
  179. goto err;
  180. }
  181. extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
  182. l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
  183. l1_size = l1_head->total_len;
  184. l1_percpu_entry = l1_head->entries;
  185. elog_base = l1_head->elog_base;
  186. elog_size = l1_head->elog_len;
  187. acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
  188. release_mem_region(l1_dirbase, l1_hdr_size);
  189. /* remap L1 header again based on completed information */
  190. r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
  191. if (!r) {
  192. pr_warn(FW_BUG EMCA_BUG,
  193. (unsigned long long)l1_dirbase,
  194. (unsigned long long)l1_dirbase + l1_size);
  195. goto err;
  196. }
  197. extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
  198. l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
  199. /* remap elog table */
  200. r = request_mem_region(elog_base, elog_size, "Elog Table");
  201. if (!r) {
  202. pr_warn(FW_BUG EMCA_BUG,
  203. (unsigned long long)elog_base,
  204. (unsigned long long)elog_base + elog_size);
  205. goto err_release_l1_dir;
  206. }
  207. elog_addr = acpi_os_map_iomem(elog_base, elog_size);
  208. rc = -ENOMEM;
  209. /* allocate buffer to save elog record */
  210. elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
  211. if (elog_buf == NULL)
  212. goto err_release_elog;
  213. /*
  214. * eMCA event report method has higher priority than EDAC method,
  215. * unless EDAC event report method is mandatory.
  216. */
  217. old_edac_report_status = get_edac_report_status();
  218. set_edac_report_status(EDAC_REPORTING_DISABLED);
  219. mce_register_decode_chain(&extlog_mce_dec);
  220. /* enable OS to be involved to take over management from BIOS */
  221. ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
  222. return 0;
  223. err_release_elog:
  224. if (elog_addr)
  225. acpi_os_unmap_iomem(elog_addr, elog_size);
  226. release_mem_region(elog_base, elog_size);
  227. err_release_l1_dir:
  228. if (extlog_l1_addr)
  229. acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
  230. release_mem_region(l1_dirbase, l1_size);
  231. err:
  232. pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
  233. return rc;
  234. }
  235. static void __exit extlog_exit(void)
  236. {
  237. set_edac_report_status(old_edac_report_status);
  238. mce_unregister_decode_chain(&extlog_mce_dec);
  239. ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
  240. if (extlog_l1_addr)
  241. acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
  242. if (elog_addr)
  243. acpi_os_unmap_iomem(elog_addr, elog_size);
  244. release_mem_region(elog_base, elog_size);
  245. release_mem_region(l1_dirbase, l1_size);
  246. kfree(elog_buf);
  247. }
  248. module_init(extlog_init);
  249. module_exit(extlog_exit);
  250. MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
  251. MODULE_DESCRIPTION("Extended MCA Error Log Driver");
  252. MODULE_LICENSE("GPL");