acpi_extlog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Extended Error Log driver
  4. *
  5. * Copyright (C) 2013 Intel Corp.
  6. * Author: Chen, Gong <gong.chen@intel.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/acpi.h>
  10. #include <linux/cper.h>
  11. #include <linux/ratelimit.h>
  12. #include <linux/edac.h>
  13. #include <linux/ras.h>
  14. #include <cxl/event.h>
  15. #include <acpi/ghes.h>
  16. #include <asm/cpu.h>
  17. #include <asm/mce.h>
  18. #include <asm/msr.h>
  19. #include "apei/apei-internal.h"
  20. #include <ras/ras_event.h>
  21. #define EXT_ELOG_ENTRY_MASK GENMASK_ULL(51, 0) /* elog entry address mask */
  22. #define EXTLOG_DSM_REV 0x0
  23. #define EXTLOG_FN_ADDR 0x1
  24. #define FLAG_OS_OPTIN BIT(0)
  25. #define ELOG_ENTRY_VALID (1ULL<<63)
  26. #define ELOG_ENTRY_LEN 0x1000
  27. #define EMCA_BUG \
  28. "Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"
  29. struct extlog_l1_head {
  30. u32 ver; /* Header Version */
  31. u32 hdr_len; /* Header Length */
  32. u64 total_len; /* entire L1 Directory length including this header */
  33. u64 elog_base; /* MCA Error Log Directory base address */
  34. u64 elog_len; /* MCA Error Log Directory length */
  35. u32 flags; /* bit 0 - OS/VMM Opt-in */
  36. u8 rev0[12];
  37. u32 entries; /* Valid L1 Directory entries per logical processor */
  38. u8 rev1[12];
  39. };
  40. static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
  41. /* L1 table related physical address */
  42. static u64 elog_base;
  43. static size_t elog_size;
  44. static u64 l1_dirbase;
  45. static size_t l1_size;
  46. /* L1 table related virtual address */
  47. static void __iomem *extlog_l1_addr;
  48. static void __iomem *elog_addr;
  49. static void *elog_buf;
  50. static u64 *l1_entry_base;
  51. static u32 l1_percpu_entry;
  52. #define ELOG_IDX(cpu, bank) \
  53. (cpu_physical_id(cpu) * l1_percpu_entry + (bank))
  54. #define ELOG_ENTRY_DATA(idx) \
  55. (*(l1_entry_base + (idx)))
  56. #define ELOG_ENTRY_ADDR(phyaddr) \
  57. (phyaddr - elog_base + (u8 *)elog_addr)
  58. static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank)
  59. {
  60. int idx;
  61. u64 data;
  62. struct acpi_hest_generic_status *estatus;
  63. WARN_ON(cpu < 0);
  64. idx = ELOG_IDX(cpu, bank);
  65. data = ELOG_ENTRY_DATA(idx);
  66. if ((data & ELOG_ENTRY_VALID) == 0)
  67. return NULL;
  68. data &= EXT_ELOG_ENTRY_MASK;
  69. estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data);
  70. /* if no valid data in elog entry, just return */
  71. if (estatus->block_status == 0)
  72. return NULL;
  73. return estatus;
  74. }
  75. static void __print_extlog_rcd(const char *pfx,
  76. struct acpi_hest_generic_status *estatus, int cpu)
  77. {
  78. static atomic_t seqno;
  79. unsigned int curr_seqno;
  80. char pfx_seq[64];
  81. if (!pfx) {
  82. if (estatus->error_severity <= CPER_SEV_CORRECTED)
  83. pfx = KERN_INFO;
  84. else
  85. pfx = KERN_ERR;
  86. }
  87. curr_seqno = atomic_inc_return(&seqno);
  88. snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
  89. printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
  90. cper_estatus_print(pfx_seq, estatus);
  91. }
  92. static int print_extlog_rcd(const char *pfx,
  93. struct acpi_hest_generic_status *estatus, int cpu)
  94. {
  95. /* Not more than 2 messages every 5 seconds */
  96. static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
  97. static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
  98. struct ratelimit_state *ratelimit;
  99. if (estatus->error_severity == CPER_SEV_CORRECTED ||
  100. (estatus->error_severity == CPER_SEV_INFORMATIONAL))
  101. ratelimit = &ratelimit_corrected;
  102. else
  103. ratelimit = &ratelimit_uncorrected;
  104. if (__ratelimit(ratelimit)) {
  105. __print_extlog_rcd(pfx, estatus, cpu);
  106. return 0;
  107. }
  108. return 1;
  109. }
  110. static void extlog_print_pcie(struct cper_sec_pcie *pcie_err,
  111. int severity)
  112. {
  113. #ifdef ACPI_APEI_PCIEAER
  114. struct aer_capability_regs *aer;
  115. struct pci_dev *pdev;
  116. unsigned int devfn;
  117. unsigned int bus;
  118. int aer_severity;
  119. int domain;
  120. if (!(pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
  121. pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO))
  122. return;
  123. aer_severity = cper_severity_to_aer(severity);
  124. aer = (struct aer_capability_regs *)pcie_err->aer_info;
  125. domain = pcie_err->device_id.segment;
  126. bus = pcie_err->device_id.bus;
  127. devfn = PCI_DEVFN(pcie_err->device_id.device,
  128. pcie_err->device_id.function);
  129. pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
  130. if (!pdev)
  131. return;
  132. pci_print_aer(pdev, aer_severity, aer);
  133. pci_dev_put(pdev);
  134. #endif
  135. }
  136. static void
  137. extlog_cxl_cper_handle_prot_err(struct cxl_cper_sec_prot_err *prot_err,
  138. int severity)
  139. {
  140. #ifdef ACPI_APEI_PCIEAER
  141. struct cxl_cper_prot_err_work_data wd;
  142. if (cxl_cper_sec_prot_err_valid(prot_err))
  143. return;
  144. if (cxl_cper_setup_prot_err_work_data(&wd, prot_err, severity))
  145. return;
  146. cxl_cper_handle_prot_err(&wd);
  147. #endif
  148. }
  149. static int extlog_print(struct notifier_block *nb, unsigned long val,
  150. void *data)
  151. {
  152. struct mce *mce = (struct mce *)data;
  153. int bank = mce->bank;
  154. int cpu = mce->extcpu;
  155. struct acpi_hest_generic_status *estatus, *tmp;
  156. struct acpi_hest_generic_data *gdata;
  157. const guid_t *fru_id;
  158. char *fru_text;
  159. guid_t *sec_type;
  160. static u32 err_seq;
  161. estatus = extlog_elog_entry_check(cpu, bank);
  162. if (!estatus)
  163. return NOTIFY_DONE;
  164. if (mce->kflags & MCE_HANDLED_CEC) {
  165. estatus->block_status = 0;
  166. return NOTIFY_DONE;
  167. }
  168. memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
  169. /* clear record status to enable BIOS to update it again */
  170. estatus->block_status = 0;
  171. tmp = (struct acpi_hest_generic_status *)elog_buf;
  172. if (!ras_userspace_consumers()) {
  173. print_extlog_rcd(NULL, tmp, cpu);
  174. goto out;
  175. }
  176. /* log event via trace */
  177. err_seq++;
  178. apei_estatus_for_each_section(tmp, gdata) {
  179. if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
  180. fru_id = (guid_t *)gdata->fru_id;
  181. else
  182. fru_id = &guid_null;
  183. if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
  184. fru_text = gdata->fru_text;
  185. else
  186. fru_text = "";
  187. sec_type = (guid_t *)gdata->section_type;
  188. if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
  189. struct cper_sec_mem_err *mem = acpi_hest_get_payload(gdata);
  190. if (gdata->error_data_length >= sizeof(*mem))
  191. trace_extlog_mem_event(mem, err_seq, fru_id, fru_text,
  192. (u8)gdata->error_severity);
  193. } else if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR)) {
  194. struct cxl_cper_sec_prot_err *prot_err =
  195. acpi_hest_get_payload(gdata);
  196. extlog_cxl_cper_handle_prot_err(prot_err,
  197. gdata->error_severity);
  198. } else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
  199. struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
  200. extlog_print_pcie(pcie_err, gdata->error_severity);
  201. } else {
  202. void *err = acpi_hest_get_payload(gdata);
  203. log_non_standard_event(sec_type, fru_id, fru_text,
  204. gdata->error_severity, err,
  205. gdata->error_data_length);
  206. }
  207. }
  208. out:
  209. mce->kflags |= MCE_HANDLED_EXTLOG;
  210. return NOTIFY_OK;
  211. }
  212. static bool __init extlog_get_l1addr(void)
  213. {
  214. guid_t guid;
  215. acpi_handle handle;
  216. union acpi_object *obj;
  217. if (guid_parse(extlog_dsm_uuid, &guid))
  218. return false;
  219. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  220. return false;
  221. if (!acpi_check_dsm(handle, &guid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
  222. return false;
  223. obj = acpi_evaluate_dsm_typed(handle, &guid, EXTLOG_DSM_REV,
  224. EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
  225. if (!obj) {
  226. return false;
  227. } else {
  228. l1_dirbase = obj->integer.value;
  229. ACPI_FREE(obj);
  230. }
  231. /* Spec says L1 directory must be 4K aligned, bail out if it isn't */
  232. if (l1_dirbase & ((1 << 12) - 1)) {
  233. pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
  234. l1_dirbase);
  235. return false;
  236. }
  237. return true;
  238. }
  239. static struct notifier_block extlog_mce_dec = {
  240. .notifier_call = extlog_print,
  241. .priority = MCE_PRIO_EXTLOG,
  242. };
  243. static int __init extlog_init(void)
  244. {
  245. struct extlog_l1_head *l1_head;
  246. void __iomem *extlog_l1_hdr;
  247. size_t l1_hdr_size;
  248. struct resource *r;
  249. u64 cap;
  250. int rc;
  251. if (rdmsrq_safe(MSR_IA32_MCG_CAP, &cap) ||
  252. !(cap & MCG_ELOG_P) ||
  253. !extlog_get_l1addr())
  254. return -ENODEV;
  255. rc = -EINVAL;
  256. /* get L1 header to fetch necessary information */
  257. l1_hdr_size = sizeof(struct extlog_l1_head);
  258. r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
  259. if (!r) {
  260. pr_warn(FW_BUG EMCA_BUG,
  261. (unsigned long long)l1_dirbase,
  262. (unsigned long long)l1_dirbase + l1_hdr_size);
  263. goto err;
  264. }
  265. extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
  266. if (!extlog_l1_hdr) {
  267. rc = -ENOMEM;
  268. goto err_release_l1_hdr;
  269. }
  270. l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
  271. l1_size = l1_head->total_len;
  272. l1_percpu_entry = l1_head->entries;
  273. elog_base = l1_head->elog_base;
  274. elog_size = l1_head->elog_len;
  275. acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
  276. release_mem_region(l1_dirbase, l1_hdr_size);
  277. /* remap L1 header again based on completed information */
  278. r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
  279. if (!r) {
  280. pr_warn(FW_BUG EMCA_BUG,
  281. (unsigned long long)l1_dirbase,
  282. (unsigned long long)l1_dirbase + l1_size);
  283. goto err;
  284. }
  285. extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
  286. if (!extlog_l1_addr) {
  287. rc = -ENOMEM;
  288. goto err_release_l1_dir;
  289. }
  290. l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);
  291. /* remap elog table */
  292. r = request_mem_region(elog_base, elog_size, "Elog Table");
  293. if (!r) {
  294. pr_warn(FW_BUG EMCA_BUG,
  295. (unsigned long long)elog_base,
  296. (unsigned long long)elog_base + elog_size);
  297. goto err_release_l1_dir;
  298. }
  299. elog_addr = acpi_os_map_iomem(elog_base, elog_size);
  300. if (!elog_addr) {
  301. rc = -ENOMEM;
  302. goto err_release_elog;
  303. }
  304. rc = -ENOMEM;
  305. /* allocate buffer to save elog record */
  306. elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
  307. if (elog_buf == NULL)
  308. goto err_release_elog;
  309. mce_register_decode_chain(&extlog_mce_dec);
  310. /* enable OS to be involved to take over management from BIOS */
  311. ((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;
  312. return 0;
  313. err_release_elog:
  314. if (elog_addr)
  315. acpi_os_unmap_iomem(elog_addr, elog_size);
  316. release_mem_region(elog_base, elog_size);
  317. err_release_l1_dir:
  318. if (extlog_l1_addr)
  319. acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
  320. release_mem_region(l1_dirbase, l1_size);
  321. err_release_l1_hdr:
  322. release_mem_region(l1_dirbase, l1_hdr_size);
  323. err:
  324. pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
  325. return rc;
  326. }
  327. static void __exit extlog_exit(void)
  328. {
  329. mce_unregister_decode_chain(&extlog_mce_dec);
  330. if (extlog_l1_addr) {
  331. ((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
  332. acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
  333. }
  334. if (elog_addr)
  335. acpi_os_unmap_iomem(elog_addr, elog_size);
  336. release_mem_region(elog_base, elog_size);
  337. release_mem_region(l1_dirbase, l1_size);
  338. kfree(elog_buf);
  339. }
  340. module_init(extlog_init);
  341. module_exit(extlog_exit);
  342. MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
  343. MODULE_DESCRIPTION("Extended MCA Error Log Driver");
  344. MODULE_LICENSE("GPL");