acpi_extlog.c 8.7 KB

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