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