acpi_extlog.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <acpi/acpi_bus.h>
  12. #include <linux/cper.h>
  13. #include <linux/ratelimit.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_QUERY 0x0
  20. #define EXTLOG_FN_ADDR 0x1
  21. #define FLAG_OS_OPTIN BIT(0)
  22. #define EXTLOG_QUERY_L1_EXIST BIT(1)
  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 u8 extlog_dsm_uuid[] = "663E35AF-CC10-41A4-88EA-5470AF055295";
  39. /* L1 table related physical address */
  40. static u64 elog_base;
  41. static size_t elog_size;
  42. static u64 l1_dirbase;
  43. static size_t l1_size;
  44. /* L1 table related virtual address */
  45. static void __iomem *extlog_l1_addr;
  46. static void __iomem *elog_addr;
  47. static void *elog_buf;
  48. static u64 *l1_entry_base;
  49. static u32 l1_percpu_entry;
  50. #define ELOG_IDX(cpu, bank) \
  51. (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
  52. #define ELOG_ENTRY_DATA(idx) \
  53. (*(l1_entry_base + (idx)))
  54. #define ELOG_ENTRY_ADDR(phyaddr) \
  55. (phyaddr - elog_base + (u8 *)elog_addr)
  56. static struct acpi_generic_status *extlog_elog_entry_check(int cpu, int bank)
  57. {
  58. int idx;
  59. u64 data;
  60. struct acpi_generic_status *estatus;
  61. WARN_ON(cpu < 0);
  62. idx = ELOG_IDX(cpu, bank);
  63. data = ELOG_ENTRY_DATA(idx);
  64. if ((data & ELOG_ENTRY_VALID) == 0)
  65. return NULL;
  66. data &= EXT_ELOG_ENTRY_MASK;
  67. estatus = (struct acpi_generic_status *)ELOG_ENTRY_ADDR(data);
  68. /* if no valid data in elog entry, just return */
  69. if (estatus->block_status == 0)
  70. return NULL;
  71. return estatus;
  72. }
  73. static void __print_extlog_rcd(const char *pfx,
  74. struct acpi_generic_status *estatus, int cpu)
  75. {
  76. static atomic_t seqno;
  77. unsigned int curr_seqno;
  78. char pfx_seq[64];
  79. if (!pfx) {
  80. if (estatus->error_severity <= CPER_SEV_CORRECTED)
  81. pfx = KERN_INFO;
  82. else
  83. pfx = KERN_ERR;
  84. }
  85. curr_seqno = atomic_inc_return(&seqno);
  86. snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
  87. printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
  88. cper_estatus_print(pfx_seq, estatus);
  89. }
  90. static int print_extlog_rcd(const char *pfx,
  91. struct acpi_generic_status *estatus, int cpu)
  92. {
  93. /* Not more than 2 messages every 5 seconds */
  94. static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
  95. static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
  96. struct ratelimit_state *ratelimit;
  97. if (estatus->error_severity == CPER_SEV_CORRECTED ||
  98. (estatus->error_severity == CPER_SEV_INFORMATIONAL))
  99. ratelimit = &ratelimit_corrected;
  100. else
  101. ratelimit = &ratelimit_uncorrected;
  102. if (__ratelimit(ratelimit)) {
  103. __print_extlog_rcd(pfx, estatus, cpu);
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static int extlog_print(struct notifier_block *nb, unsigned long val,
  109. void *data)
  110. {
  111. struct mce *mce = (struct mce *)data;
  112. int bank = mce->bank;
  113. int cpu = mce->extcpu;
  114. struct acpi_generic_status *estatus;
  115. int rc;
  116. estatus = extlog_elog_entry_check(cpu, bank);
  117. if (estatus == NULL)
  118. return NOTIFY_DONE;
  119. memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
  120. /* clear record status to enable BIOS to update it again */
  121. estatus->block_status = 0;
  122. rc = print_extlog_rcd(NULL, (struct acpi_generic_status *)elog_buf, cpu);
  123. return NOTIFY_DONE;
  124. }
  125. static int extlog_get_dsm(acpi_handle handle, int rev, int func, u64 *ret)
  126. {
  127. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  128. struct acpi_object_list input;
  129. union acpi_object params[4], *obj;
  130. u8 uuid[16];
  131. int i;
  132. acpi_str_to_uuid(extlog_dsm_uuid, uuid);
  133. input.count = 4;
  134. input.pointer = params;
  135. params[0].type = ACPI_TYPE_BUFFER;
  136. params[0].buffer.length = 16;
  137. params[0].buffer.pointer = uuid;
  138. params[1].type = ACPI_TYPE_INTEGER;
  139. params[1].integer.value = rev;
  140. params[2].type = ACPI_TYPE_INTEGER;
  141. params[2].integer.value = func;
  142. params[3].type = ACPI_TYPE_PACKAGE;
  143. params[3].package.count = 0;
  144. params[3].package.elements = NULL;
  145. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf)))
  146. return -1;
  147. *ret = 0;
  148. obj = (union acpi_object *)buf.pointer;
  149. if (obj->type == ACPI_TYPE_INTEGER) {
  150. *ret = obj->integer.value;
  151. } else if (obj->type == ACPI_TYPE_BUFFER) {
  152. if (obj->buffer.length <= 8) {
  153. for (i = 0; i < obj->buffer.length; i++)
  154. *ret |= (obj->buffer.pointer[i] << (i * 8));
  155. }
  156. }
  157. kfree(buf.pointer);
  158. return 0;
  159. }
  160. static bool extlog_get_l1addr(void)
  161. {
  162. acpi_handle handle;
  163. u64 ret;
  164. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  165. return false;
  166. if (extlog_get_dsm(handle, EXTLOG_DSM_REV, EXTLOG_FN_QUERY, &ret) ||
  167. !(ret & EXTLOG_QUERY_L1_EXIST))
  168. return false;
  169. if (extlog_get_dsm(handle, EXTLOG_DSM_REV, EXTLOG_FN_ADDR, &ret))
  170. return false;
  171. l1_dirbase = ret;
  172. /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
  173. if (l1_dirbase & ((1 << 12) - 1)) {
  174. pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
  175. l1_dirbase);
  176. return false;
  177. }
  178. return true;
  179. }
  180. static struct notifier_block extlog_mce_dec = {
  181. .notifier_call = extlog_print,
  182. };
  183. static int __init extlog_init(void)
  184. {
  185. struct extlog_l1_head *l1_head;
  186. void __iomem *extlog_l1_hdr;
  187. size_t l1_hdr_size;
  188. struct resource *r;
  189. u64 cap;
  190. int rc;
  191. rc = -ENODEV;
  192. rdmsrl(MSR_IA32_MCG_CAP, cap);
  193. if (!(cap & MCG_ELOG_P))
  194. return rc;
  195. if (!extlog_get_l1addr())
  196. return rc;
  197. rc = -EINVAL;
  198. /* get L1 header to fetch necessary information */
  199. l1_hdr_size = sizeof(struct extlog_l1_head);
  200. r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
  201. if (!r) {
  202. pr_warn(FW_BUG EMCA_BUG,
  203. (unsigned long long)l1_dirbase,
  204. (unsigned long long)l1_dirbase + l1_hdr_size);
  205. goto err;
  206. }
  207. extlog_l1_hdr = acpi_os_map_memory(l1_dirbase, l1_hdr_size);
  208. l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
  209. l1_size = l1_head->total_len;
  210. l1_percpu_entry = l1_head->entries;
  211. elog_base = l1_head->elog_base;
  212. elog_size = l1_head->elog_len;
  213. acpi_os_unmap_memory(extlog_l1_hdr, l1_hdr_size);
  214. release_mem_region(l1_dirbase, l1_hdr_size);
  215. /* remap L1 header again based on completed information */
  216. r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
  217. if (!r) {
  218. pr_warn(FW_BUG EMCA_BUG,
  219. (unsigned long long)l1_dirbase,
  220. (unsigned long long)l1_dirbase + l1_size);
  221. goto err;
  222. }
  223. extlog_l1_addr = acpi_os_map_memory(l1_dirbase, l1_size);
  224. l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
  225. /* remap elog table */
  226. r = request_mem_region(elog_base, elog_size, "Elog Table");
  227. if (!r) {
  228. pr_warn(FW_BUG EMCA_BUG,
  229. (unsigned long long)elog_base,
  230. (unsigned long long)elog_base + elog_size);
  231. goto err_release_l1_dir;
  232. }
  233. elog_addr = acpi_os_map_memory(elog_base, elog_size);
  234. rc = -ENOMEM;
  235. /* allocate buffer to save elog record */
  236. elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
  237. if (elog_buf == NULL)
  238. goto err_release_elog;
  239. mce_register_decode_chain(&extlog_mce_dec);
  240. /* enable OS to be involved to take over management from BIOS */
  241. ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
  242. return 0;
  243. err_release_elog:
  244. if (elog_addr)
  245. acpi_os_unmap_memory(elog_addr, elog_size);
  246. release_mem_region(elog_base, elog_size);
  247. err_release_l1_dir:
  248. if (extlog_l1_addr)
  249. acpi_os_unmap_memory(extlog_l1_addr, l1_size);
  250. release_mem_region(l1_dirbase, l1_size);
  251. err:
  252. pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
  253. return rc;
  254. }
  255. static void __exit extlog_exit(void)
  256. {
  257. mce_unregister_decode_chain(&extlog_mce_dec);
  258. ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
  259. if (extlog_l1_addr)
  260. acpi_os_unmap_memory(extlog_l1_addr, l1_size);
  261. if (elog_addr)
  262. acpi_os_unmap_memory(elog_addr, elog_size);
  263. release_mem_region(elog_base, elog_size);
  264. release_mem_region(l1_dirbase, l1_size);
  265. kfree(elog_buf);
  266. }
  267. module_init(extlog_init);
  268. module_exit(extlog_exit);
  269. MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
  270. MODULE_DESCRIPTION("Extended MCA Error Log Driver");
  271. MODULE_LICENSE("GPL");