FILE
ghes.c
File preview with syntax highlighting.
linux-6.19.14/drivers/acpi/apei/ghes.c
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * APEI Generic Hardware Error Source support 4 * 5 * Generic Hardware Error Source provides a way to report platform 6 * hardware errors (such as that from chipset). It works in so called 7 * "Firmware First" mode, that is, hardware errors are reported to 8 * firmware firstly, then reported to Linux by firmware. This way, 9 * some non-standard hardware error registers or non-standard hardware 10 * link can be checked by firmware to produce more hardware error 11 * information for Linux. 12 * 13 * For more information about Generic Hardware Error Source, please 14 * refer to ACPI Specification version 4.0, section 17.3.2.6 15 * 16 * Copyright 2010,2011 Intel Corp. 17 * Author: Huang Ying <ying.huang@intel.com> 18 */ 19 20#include <linux/arm_sdei.h> 21#include <linux/kernel.h> 22#include <linux/moduleparam.h> 23#include <linux/init.h> 24#include <linux/acpi.h> 25#include <linux/bitfield.h> 26#include <linux/io.h> 27#include <linux/interrupt.h> 28#include <linux/timer.h> 29#include <linux/cper.h> 30#include <linux/cleanup.h> 31#include <linux/platform_device.h> 32#include <linux/minmax.h> 33#include <linux/mutex.h> 34#include <linux/ratelimit.h> 35#include <linux/vmalloc.h> 36#include <linux/irq_work.h> 37#include <linux/llist.h> 38#include <linux/genalloc.h> 39#include <linux/kfifo.h> 40#include <linux/pci.h> 41#include <linux/pfn.h> 42#include <linux/aer.h> 43#include <linux/nmi.h> 44#include <linux/sched/clock.h> 45#include <linux/uuid.h> 46#include <linux/ras.h> 47#include <linux/task_work.h> 48#include <linux/vmcore_info.h> 49 50#include <acpi/actbl1.h> 51#include <acpi/ghes.h> 52#include <acpi/apei.h> 53#include <asm/fixmap.h> 54#include <asm/tlbflush.h> 55#include <cxl/event.h> 56#include <ras/ras_event.h> 57 58#include "apei-internal.h" 59 60#define GHES_PFX "GHES: " 61 62#define GHES_ESTATUS_MAX_SIZE 65536 63#define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536 64 65#define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3 66 67/* This is just an estimation for memory pool allocation */ 68#define GHES_ESTATUS_CACHE_AVG_SIZE 512 69 70#define GHES_ESTATUS_CACHES_SIZE 4 71 72#define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL 73/* Prevent too many caches are allocated because of RCU */ 74#define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2) 75 76#define GHES_ESTATUS_CACHE_LEN(estatus_len) \ 77 (sizeof(struct ghes_estatus_cache) + (estatus_len)) 78#define GHES_ESTATUS_FROM_CACHE(estatus_cache) \ 79 ((struct acpi_hest_generic_status *) \ 80 ((struct ghes_estatus_cache *)(estatus_cache) + 1)) 81 82#define GHES_ESTATUS_NODE_LEN(estatus_len) \ 83 (sizeof(struct ghes_estatus_node) + (estatus_len)) 84#define GHES_ESTATUS_FROM_NODE(estatus_node) \ 85 ((struct acpi_hest_generic_status *) \ 86 ((struct ghes_estatus_node *)(estatus_node) + 1)) 87 88#define GHES_VENDOR_ENTRY_LEN(gdata_len) \ 89 (sizeof(struct ghes_vendor_record_entry) + (gdata_len)) 90#define GHES_GDATA_FROM_VENDOR_ENTRY(vendor_entry) \ 91 ((struct acpi_hest_generic_data *) \ 92 ((struct ghes_vendor_record_entry *)(vendor_entry) + 1)) 93 94/* 95 * NMI-like notifications vary by architecture, before the compiler can prune 96 * unused static functions it needs a value for these enums. 97 */ 98#ifndef CONFIG_ARM_SDE_INTERFACE 99#define FIX_APEI_GHES_SDEI_NORMAL __end_of_fixed_addresses 100#define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses 101#endif 102 103static ATOMIC_NOTIFIER_HEAD(ghes_report_chain); 104 105static inline bool is_hest_type_generic_v2(struct ghes *ghes) 106{ 107 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2; 108} 109 110/* 111 * A platform may describe one error source for the handling of synchronous 112 * errors (e.g. MCE or SEA), or for handling asynchronous errors (e.g. SCI 113 * or External Interrupt). On x86, the HEST notifications are always 114 * asynchronous, so only SEA on ARM is delivered as a synchronous 115 * notification. 116 */ 117static inline bool is_hest_sync_notify(struct ghes *ghes) 118{ 119 u8 notify_type = ghes->generic->notify.type; 120 121 return notify_type == ACPI_HEST_NOTIFY_SEA; 122} 123 124/* 125 * This driver isn't really modular, however for the time being, 126 * continuing to use module_param is the easiest way to remain 127 * compatible with existing boot arg use cases. 128 */ 129bool ghes_disable; 130module_param_named(disable, ghes_disable, bool, 0); 131 132/* 133 * "ghes.edac_force_enable" forcibly enables ghes_edac and skips the platform 134 * check. 135 */ 136static bool ghes_edac_force_enable; 137module_param_named(edac_force_enable, ghes_edac_force_enable, bool, 0); 138 139/* 140 * All error sources notified with HED (Hardware Error Device) share a 141 * single notifier callback, so they need to be linked and checked one 142 * by one. This holds true for NMI too. 143 * 144 * RCU is used for these lists, so ghes_list_mutex is only used for 145 * list changing, not for traversing. 146 */ 147static LIST_HEAD(ghes_hed); 148static DEFINE_MUTEX(ghes_list_mutex); 149 150/* 151 * A list of GHES devices which are given to the corresponding EDAC driver 152 * ghes_edac for further use. 153 */ 154static LIST_HEAD(ghes_devs); 155static DEFINE_MUTEX(ghes_devs_mutex); 156 157/* 158 * Because the memory area used to transfer hardware error information 159 * from BIOS to Linux can be determined only in NMI, IRQ or timer 160 * handler, but general ioremap can not be used in atomic context, so 161 * the fixmap is used instead. 162 * 163 * This spinlock is used to prevent the fixmap entry from being used 164 * simultaneously. 165 */ 166static DEFINE_SPINLOCK(ghes_notify_lock_irq); 167 168struct ghes_vendor_record_entry { 169 struct work_struct work; 170 int error_severity; 171 char vendor_record[]; 172}; 173 174static struct gen_pool *ghes_estatus_pool; 175 176static struct ghes_estatus_cache __rcu *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE]; 177static atomic_t ghes_estatus_cache_alloced; 178 179static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx) 180{ 181 phys_addr_t paddr; 182 pgprot_t prot; 183 184 paddr = PFN_PHYS(pfn); 185 prot = arch_apei_get_mem_attribute(paddr); 186 __set_fixmap(fixmap_idx, paddr, prot); 187 188 return (void __iomem *) __fix_to_virt(fixmap_idx); 189} 190 191static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx) 192{ 193 int _idx = virt_to_fix((unsigned long)vaddr); 194 195 WARN_ON_ONCE(fixmap_idx != _idx); 196 clear_fixmap(fixmap_idx); 197} 198 199int ghes_estatus_pool_init(unsigned int num_ghes) 200{ 201 unsigned long addr, len; 202 int rc; 203 204 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1); 205 if (!ghes_estatus_pool) 206 return -ENOMEM; 207 208 len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX; 209 len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE); 210 211 addr = (unsigned long)vmalloc(PAGE_ALIGN(len)); 212 if (!addr) 213 goto err_pool_alloc; 214 215 rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1); 216 if (rc) 217 goto err_pool_add; 218 219 return 0; 220 221err_pool_add: 222 vfree((void *)addr); 223 224err_pool_alloc: 225 gen_pool_destroy(ghes_estatus_pool); 226 227 return -ENOMEM; 228} 229 230/** 231 * ghes_estatus_pool_region_free - free previously allocated memory 232 * from the ghes_estatus_pool. 233 * @addr: address of memory to free. 234 * @size: size of memory to free. 235 * 236 * Returns none. 237 */ 238void ghes_estatus_pool_region_free(unsigned long addr, u32 size) 239{ 240 gen_pool_free(ghes_estatus_pool, addr, size); 241} 242EXPORT_SYMBOL_GPL(ghes_estatus_pool_region_free); 243 244static int map_gen_v2(struct ghes *ghes) 245{ 246 return apei_map_generic_address(&ghes->generic_v2->read_ack_register); 247} 248 249static void unmap_gen_v2(struct ghes *ghes) 250{ 251 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register); 252} 253 254static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2) 255{ 256 int rc; 257 u64 val = 0; 258 259 rc = apei_read(&val, &gv2->read_ack_register); 260 if (rc) 261 return; 262 263 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset; 264 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset; 265 266 apei_write(val, &gv2->read_ack_register); 267} 268 269static struct ghes *ghes_new(struct acpi_hest_generic *generic) 270{ 271 struct ghes *ghes; 272 unsigned int error_block_length; 273 int rc; 274 275 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL); 276 if (!ghes) 277 return ERR_PTR(-ENOMEM); 278 279 ghes->generic = generic; 280 if (is_hest_type_generic_v2(ghes)) { 281 rc = map_gen_v2(ghes); 282 if (rc) 283 goto err_free; 284 } 285 286 rc = apei_map_generic_address(&generic->error_status_address); 287 if (rc) 288 goto err_unmap_read_ack_addr; 289 error_block_length = generic->error_block_length; 290 if (error_block_length > GHES_ESTATUS_MAX_SIZE) { 291 pr_warn(FW_WARN GHES_PFX 292 "Error status block length is too long: %u for " 293 "generic hardware error source: %d.\n", 294 error_block_length, generic->header.source_id); 295 error_block_length = GHES_ESTATUS_MAX_SIZE; 296 } 297 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL); 298 ghes->estatus_length = error_block_length; 299 if (!ghes->estatus) { 300 rc = -ENOMEM; 301 goto err_unmap_status_addr; 302 } 303 304 return ghes; 305 306err_unmap_status_addr: 307 apei_unmap_generic_address(&generic->error_status_address); 308err_unmap_read_ack_addr: 309 if (is_hest_type_generic_v2(ghes)) 310 unmap_gen_v2(ghes); 311err_free: 312 kfree(ghes); 313 return ERR_PTR(rc); 314} 315 316static void ghes_fini(struct ghes *ghes) 317{ 318 kfree(ghes->estatus); 319 apei_unmap_generic_address(&ghes->generic->error_status_address); 320 if (is_hest_type_generic_v2(ghes)) 321 unmap_gen_v2(ghes); 322} 323 324static inline int ghes_severity(int severity) 325{ 326 switch (severity) { 327 case CPER_SEV_INFORMATIONAL: 328 return GHES_SEV_NO; 329 case CPER_SEV_CORRECTED: 330 return GHES_SEV_CORRECTED; 331 case CPER_SEV_RECOVERABLE: 332 return GHES_SEV_RECOVERABLE; 333 case CPER_SEV_FATAL: 334 return GHES_SEV_PANIC; 335 default: 336 /* Unknown, go panic */ 337 return GHES_SEV_PANIC; 338 } 339} 340 341static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len, 342 int from_phys, 343 enum fixed_addresses fixmap_idx) 344{ 345 void __iomem *vaddr; 346 u64 offset; 347 u32 trunk; 348 349 while (len > 0) { 350 offset = paddr - (paddr & PAGE_MASK); 351 vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx); 352 trunk = PAGE_SIZE - offset; 353 trunk = min(trunk, len); 354 if (from_phys) 355 memcpy_fromio(buffer, vaddr + offset, trunk); 356 else 357 memcpy_toio(vaddr + offset, buffer, trunk); 358 len -= trunk; 359 paddr += trunk; 360 buffer += trunk; 361 ghes_unmap(vaddr, fixmap_idx); 362 } 363} 364 365/* Check the top-level record header has an appropriate size. */ 366static int __ghes_check_estatus(struct ghes *ghes, 367 struct acpi_hest_generic_status *estatus) 368{ 369 u32 len = cper_estatus_len(estatus); 370 u32 max_len = min(ghes->generic->error_block_length, 371 ghes->estatus_length); 372 373 if (len < sizeof(*estatus)) { 374 pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n"); 375 return -EIO; 376 } 377 378 if (!len || len > max_len) { 379 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n"); 380 return -EIO; 381 } 382 383 if (cper_estatus_check_header(estatus)) { 384 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n"); 385 return -EIO; 386 } 387 388 return 0; 389} 390 391/* Read the CPER block, returning its address, and header in estatus. */ 392static int __ghes_peek_estatus(struct ghes *ghes, 393 struct acpi_hest_generic_status *estatus, 394 u64 *buf_paddr, enum fixed_addresses fixmap_idx) 395{ 396 struct acpi_hest_generic *g = ghes->generic; 397 int rc; 398 399 rc = apei_read(buf_paddr, &g->error_status_address); 400 if (rc) { 401 *buf_paddr = 0; 402 pr_warn_ratelimited(FW_WARN GHES_PFX 403"Failed to read error status block address for hardware error source: %d.\n", 404 g->header.source_id); 405 return -EIO; 406 } 407 if (!*buf_paddr) 408 return -ENOENT; 409 410 ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1, 411 fixmap_idx); 412 if (!estatus->block_status) { 413 *buf_paddr = 0; 414 return -ENOENT; 415 } 416 417 return 0; 418} 419 420static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus, 421 u64 buf_paddr, enum fixed_addresses fixmap_idx, 422 size_t buf_len) 423{ 424 ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx); 425 if (cper_estatus_check(estatus)) { 426 pr_warn_ratelimited(FW_WARN GHES_PFX 427 "Failed to read error status block!\n"); 428 return -EIO; 429 } 430 431 return 0; 432} 433 434static int ghes_read_estatus(struct ghes *ghes, 435 struct acpi_hest_generic_status *estatus, 436 u64 *buf_paddr, enum fixed_addresses fixmap_idx) 437{ 438 int rc; 439 440 rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx); 441 if (rc) 442 return rc; 443 444 rc = __ghes_check_estatus(ghes, estatus); 445 if (rc) 446 return rc; 447 448 return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx, 449 cper_estatus_len(estatus)); 450} 451 452static void ghes_clear_estatus(struct ghes *ghes, 453 struct acpi_hest_generic_status *estatus, 454 u64 buf_paddr, enum fixed_addresses fixmap_idx) 455{ 456 estatus->block_status = 0; 457 458 if (!buf_paddr) 459 return; 460 461 ghes_copy_tofrom_phys(estatus, buf_paddr, 462 sizeof(estatus->block_status), 0, 463 fixmap_idx); 464 465 /* 466 * GHESv2 type HEST entries introduce support for error acknowledgment, 467 * so only acknowledge the error if this support is present. 468 */ 469 if (is_hest_type_generic_v2(ghes)) 470 ghes_ack_error(ghes->generic_v2); 471} 472 473/** 474 * struct ghes_task_work - for synchronous RAS event 475 * 476 * @twork: callback_head for task work 477 * @pfn: page frame number of corrupted page 478 * @flags: work control flags 479 * 480 * Structure to pass task work to be handled before 481 * returning to user-space via task_work_add(). 482 */ 483struct ghes_task_work { 484 struct callback_head twork; 485 u64 pfn; 486 int flags; 487}; 488 489static void memory_failure_cb(struct callback_head *twork) 490{ 491 struct ghes_task_work *twcb = container_of(twork, struct ghes_task_work, twork); 492 int ret; 493 494 ret = memory_failure(twcb->pfn, twcb->flags); 495 gen_pool_free(ghes_estatus_pool, (unsigned long)twcb, sizeof(*twcb)); 496 497 if (!ret || ret == -EHWPOISON || ret == -EOPNOTSUPP) 498 return; 499 500 pr_err("%#llx: Sending SIGBUS to %s:%d due to hardware memory corruption\n", 501 twcb->pfn, current->comm, task_pid_nr(current)); 502 force_sig(SIGBUS); 503} 504 505static bool ghes_do_memory_failure(u64 physical_addr, int flags) 506{ 507 struct ghes_task_work *twcb; 508 unsigned long pfn; 509 510 if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE)) 511 return false; 512 513 pfn = PHYS_PFN(physical_addr); 514 515 if (flags == MF_ACTION_REQUIRED && current->mm) { 516 twcb = (void *)gen_pool_alloc(ghes_estatus_pool, sizeof(*twcb)); 517 if (!twcb) 518 return false; 519 520 twcb->pfn = pfn; 521 twcb->flags = flags; 522 init_task_work(&twcb->twork, memory_failure_cb); 523 task_work_add(current, &twcb->twork, TWA_RESUME); 524 return true; 525 } 526 527 memory_failure_queue(pfn, flags); 528 return true; 529} 530 531static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, 532 int sev, bool sync) 533{ 534 int flags = -1; 535 int sec_sev = ghes_severity(gdata->error_severity); 536 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata); 537 538 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA)) 539 return false; 540 541 /* iff following two events can be handled properly by now */ 542 if (sec_sev == GHES_SEV_CORRECTED && 543 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED)) 544 flags = MF_SOFT_OFFLINE; 545 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE) 546 flags = sync ? MF_ACTION_REQUIRED : 0; 547 548 if (flags != -1) 549 return ghes_do_memory_failure(mem_err->physical_addr, flags); 550 551 return false; 552} 553 554static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata, 555 int sev, bool sync) 556{ 557 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata); 558 int flags = sync ? MF_ACTION_REQUIRED : 0; 559 int length = gdata->error_data_length; 560 char error_type[120]; 561 bool queued = false; 562 int sec_sev, i; 563 char *p; 564 565 sec_sev = ghes_severity(gdata->error_severity); 566 if (length >= sizeof(*err)) { 567 log_arm_hw_error(err, sec_sev); 568 } else { 569 pr_warn(FW_BUG "arm error length: %d\n", length); 570 pr_warn(FW_BUG "length is too small\n"); 571 pr_warn(FW_BUG "firmware-generated error record is incorrect\n"); 572 return false; 573 } 574 575 if (sev != GHES_SEV_RECOVERABLE || sec_sev != GHES_SEV_RECOVERABLE) 576 return false; 577 578 p = (char *)(err + 1); 579 length -= sizeof(err); 580 581 for (i = 0; i < err->err_info_num; i++) { 582 struct cper_arm_err_info *err_info; 583 bool is_cache, has_pa; 584 585 /* Ensure we have enough data for the error info header */ 586 if (length < sizeof(*err_info)) 587 break; 588 589 err_info = (struct cper_arm_err_info *)p; 590 591 /* Validate the claimed length before using it */ 592 length -= err_info->length; 593 if (length < 0) 594 break; 595 596 is_cache = err_info->type & CPER_ARM_CACHE_ERROR; 597 has_pa = (err_info->validation_bits & CPER_ARM_INFO_VALID_PHYSICAL_ADDR); 598 599 /* 600 * The field (err_info->error_info & BIT(26)) is fixed to set to 601 * 1 in some old firmware of HiSilicon Kunpeng920. We assume that 602 * firmware won't mix corrected errors in an uncorrected section, 603 * and don't filter out 'corrected' error here. 604 */ 605 if (is_cache && has_pa) { 606 queued = ghes_do_memory_failure(err_info->physical_fault_addr, flags); 607 p += err_info->length; 608 continue; 609 } 610 611 cper_bits_to_str(error_type, sizeof(error_type), 612 FIELD_GET(CPER_ARM_ERR_TYPE_MASK, err_info->type), 613 cper_proc_error_type_strs, 614 ARRAY_SIZE(cper_proc_error_type_strs)); 615 616 pr_warn_ratelimited(FW_WARN GHES_PFX 617 "Unhandled processor error type 0x%02x: %s%s\n", 618 err_info->type, error_type, 619 (err_info->type & ~CPER_ARM_ERR_TYPE_MASK) ? " with reserved bit(s)" : ""); 620 p += err_info->length; 621 } 622 623 return queued; 624} 625 626/* 627 * PCIe AER errors need to be sent to the AER driver for reporting and 628 * recovery. The GHES severities map to the following AER severities and 629 * require the following handling: 630 * 631 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE 632 * These need to be reported by the AER driver but no recovery is 633 * necessary. 634 * GHES_SEV_RECOVERABLE -> AER_NONFATAL 635 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL 636 * These both need to be reported and recovered from by the AER driver. 637 * GHES_SEV_PANIC does not make it to this handling since the kernel must 638 * panic. 639 */ 640static void ghes_handle_aer(struct acpi_hest_generic_data *gdata) 641{ 642#ifdef CONFIG_ACPI_APEI_PCIEAER 643 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata); 644 645 if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID && 646 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) { 647 unsigned int devfn; 648 int aer_severity; 649 u8 *aer_info; 650 651 devfn = PCI_DEVFN(pcie_err->device_id.device, 652 pcie_err->device_id.function); 653 aer_severity = cper_severity_to_aer(gdata->error_severity); 654 655 /* 656 * If firmware reset the component to contain 657 * the error, we must reinitialize it before 658 * use, so treat it as a fatal AER error. 659 */ 660 if (gdata->flags & CPER_SEC_RESET) 661 aer_severity = AER_FATAL; 662 663 aer_info = (void *)gen_pool_alloc(ghes_estatus_pool, 664 sizeof(struct aer_capability_regs)); 665 if (!aer_info) 666 return; 667 memcpy(aer_info, pcie_err->aer_info, sizeof(struct aer_capability_regs)); 668 669 aer_recover_queue(pcie_err->device_id.segment, 670 pcie_err->device_id.bus, 671 devfn, aer_severity, 672 (struct aer_capability_regs *) 673 aer_info); 674 } 675#endif 676} 677 678static BLOCKING_NOTIFIER_HEAD(vendor_record_notify_list); 679 680int ghes_register_vendor_record_notifier(struct notifier_block *nb) 681{ 682 return blocking_notifier_chain_register(&vendor_record_notify_list, nb); 683} 684EXPORT_SYMBOL_GPL(ghes_register_vendor_record_notifier); 685 686void ghes_unregister_vendor_record_notifier(struct notifier_block *nb) 687{ 688 blocking_notifier_chain_unregister(&vendor_record_notify_list, nb); 689} 690EXPORT_SYMBOL_GPL(ghes_unregister_vendor_record_notifier); 691 692static void ghes_vendor_record_work_func(struct work_struct *work) 693{ 694 struct ghes_vendor_record_entry *entry; 695 struct acpi_hest_generic_data *gdata; 696 u32 len; 697 698 entry = container_of(work, struct ghes_vendor_record_entry, work); 699 gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry); 700 701 blocking_notifier_call_chain(&vendor_record_notify_list, 702 entry->error_severity, gdata); 703 704 len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata)); 705 gen_pool_free(ghes_estatus_pool, (unsigned long)entry, len); 706} 707 708static void ghes_defer_non_standard_event(struct acpi_hest_generic_data *gdata, 709 int sev) 710{ 711 struct acpi_hest_generic_data *copied_gdata; 712 struct ghes_vendor_record_entry *entry; 713 u32 len; 714 715 len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata)); 716 entry = (void *)gen_pool_alloc(ghes_estatus_pool, len); 717 if (!entry) 718 return; 719 720 copied_gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry); 721 memcpy(copied_gdata, gdata, acpi_hest_get_record_size(gdata)); 722 entry->error_severity = sev; 723 724 INIT_WORK(&entry->work, ghes_vendor_record_work_func); 725 schedule_work(&entry->work); 726} 727 728/* Room for 8 entries */ 729#define CXL_CPER_PROT_ERR_FIFO_DEPTH 8 730static DEFINE_KFIFO(cxl_cper_prot_err_fifo, struct cxl_cper_prot_err_work_data, 731 CXL_CPER_PROT_ERR_FIFO_DEPTH); 732 733/* Synchronize schedule_work() with cxl_cper_prot_err_work changes */ 734static DEFINE_SPINLOCK(cxl_cper_prot_err_work_lock); 735struct work_struct *cxl_cper_prot_err_work; 736 737static void cxl_cper_post_prot_err(struct cxl_cper_sec_prot_err *prot_err, 738 int severity) 739{ 740#ifdef CONFIG_ACPI_APEI_PCIEAER 741 struct cxl_cper_prot_err_work_data wd; 742 u8 *dvsec_start, *cap_start; 743 744 if (cxl_cper_sec_prot_err_valid(prot_err)) 745 return; 746 747 guard(spinlock_irqsave)(&cxl_cper_prot_err_work_lock); 748 749 if (!cxl_cper_prot_err_work) 750 return; 751 752 switch (prot_err->agent_type) { 753 case RCD: 754 case DEVICE: 755 case LD: 756 case FMLD: 757 case RP: 758 case DSP: 759 case USP: 760 memcpy(&wd.prot_err, prot_err, sizeof(wd.prot_err)); 761 762 dvsec_start = (u8 *)(prot_err + 1); 763 cap_start = dvsec_start + prot_err->dvsec_len; 764 765 memcpy(&wd.ras_cap, cap_start, sizeof(wd.ras_cap)); 766 wd.severity = cper_severity_to_aer(severity); 767 break; 768 default: 769 pr_err_ratelimited("CXL CPER invalid agent type: %d\n", 770 prot_err->agent_type); 771 return; 772 } 773 774 if (!kfifo_put(&cxl_cper_prot_err_fifo, wd)) { 775 pr_err_ratelimited("CXL CPER kfifo overflow\n"); 776 return; 777 } 778 779 schedule_work(cxl_cper_prot_err_work); 780#endif 781} 782 783int cxl_cper_register_prot_err_work(struct work_struct *work) 784{ 785 if (cxl_cper_prot_err_work) 786 return -EINVAL; 787 788 guard(spinlock)(&cxl_cper_prot_err_work_lock); 789 cxl_cper_prot_err_work = work; 790 return 0; 791} 792EXPORT_SYMBOL_NS_GPL(cxl_cper_register_prot_err_work, "CXL"); 793 794int cxl_cper_unregister_prot_err_work(struct work_struct *work) 795{ 796 if (cxl_cper_prot_err_work != work) 797 return -EINVAL; 798 799 guard(spinlock)(&cxl_cper_prot_err_work_lock); 800 cxl_cper_prot_err_work = NULL; 801 return 0; 802} 803EXPORT_SYMBOL_NS_GPL(cxl_cper_unregister_prot_err_work, "CXL"); 804 805int cxl_cper_prot_err_kfifo_get(struct cxl_cper_prot_err_work_data *wd) 806{ 807 return kfifo_get(&cxl_cper_prot_err_fifo, wd); 808} 809EXPORT_SYMBOL_NS_GPL(cxl_cper_prot_err_kfifo_get, "CXL"); 810 811/* Room for 8 entries for each of the 4 event log queues */ 812#define CXL_CPER_FIFO_DEPTH 32 813DEFINE_KFIFO(cxl_cper_fifo, struct cxl_cper_work_data, CXL_CPER_FIFO_DEPTH); 814 815/* Synchronize schedule_work() with cxl_cper_work changes */ 816static DEFINE_SPINLOCK(cxl_cper_work_lock); 817struct work_struct *cxl_cper_work; 818 819static void cxl_cper_post_event(enum cxl_event_type event_type, 820 struct cxl_cper_event_rec *rec) 821{ 822 struct cxl_cper_work_data wd; 823 824 if (rec->hdr.length <= sizeof(rec->hdr) || 825 rec->hdr.length > sizeof(*rec)) { 826 pr_err(FW_WARN "CXL CPER Invalid section length (%u)\n", 827 rec->hdr.length); 828 return; 829 } 830 831 if (!(rec->hdr.validation_bits & CPER_CXL_COMP_EVENT_LOG_VALID)) { 832 pr_err(FW_WARN "CXL CPER invalid event\n"); 833 return; 834 } 835 836 guard(spinlock_irqsave)(&cxl_cper_work_lock); 837 838 if (!cxl_cper_work) 839 return; 840 841 wd.event_type = event_type; 842 memcpy(&wd.rec, rec, sizeof(wd.rec)); 843 844 if (!kfifo_put(&cxl_cper_fifo, wd)) { 845 pr_err_ratelimited("CXL CPER kfifo overflow\n"); 846 return; 847 } 848 849 schedule_work(cxl_cper_work); 850} 851 852int cxl_cper_register_work(struct work_struct *work) 853{ 854 if (cxl_cper_work) 855 return -EINVAL; 856 857 guard(spinlock)(&cxl_cper_work_lock); 858 cxl_cper_work = work; 859 return 0; 860} 861EXPORT_SYMBOL_NS_GPL(cxl_cper_register_work, "CXL"); 862 863int cxl_cper_unregister_work(struct work_struct *work) 864{ 865 if (cxl_cper_work != work) 866 return -EINVAL; 867 868 guard(spinlock)(&cxl_cper_work_lock); 869 cxl_cper_work = NULL; 870 return 0; 871} 872EXPORT_SYMBOL_NS_GPL(cxl_cper_unregister_work, "CXL"); 873 874int cxl_cper_kfifo_get(struct cxl_cper_work_data *wd) 875{ 876 return kfifo_get(&cxl_cper_fifo, wd); 877} 878EXPORT_SYMBOL_NS_GPL(cxl_cper_kfifo_get, "CXL"); 879 880static void ghes_log_hwerr(int sev, guid_t *sec_type) 881{ 882 if (sev != CPER_SEV_RECOVERABLE) 883 return; 884 885 if (guid_equal(sec_type, &CPER_SEC_PROC_ARM) || 886 guid_equal(sec_type, &CPER_SEC_PROC_GENERIC) || 887 guid_equal(sec_type, &CPER_SEC_PROC_IA)) { 888 hwerr_log_error_type(HWERR_RECOV_CPU); 889 return; 890 } 891 892 if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR) || 893 guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID) || 894 guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID) || 895 guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) { 896 hwerr_log_error_type(HWERR_RECOV_CXL); 897 return; 898 } 899 900 if (guid_equal(sec_type, &CPER_SEC_PCIE) || 901 guid_equal(sec_type, &CPER_SEC_PCI_X_BUS)) { 902 hwerr_log_error_type(HWERR_RECOV_PCI); 903 return; 904 } 905 906 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) { 907 hwerr_log_error_type(HWERR_RECOV_MEMORY); 908 return; 909 } 910 911 hwerr_log_error_type(HWERR_RECOV_OTHERS); 912} 913 914static void ghes_do_proc(struct ghes *ghes, 915 const struct acpi_hest_generic_status *estatus) 916{ 917 int sev, sec_sev; 918 struct acpi_hest_generic_data *gdata; 919 guid_t *sec_type; 920 const guid_t *fru_id = &guid_null; 921 char *fru_text = ""; 922 bool queued = false; 923 bool sync = is_hest_sync_notify(ghes); 924 925 sev = ghes_severity(estatus->error_severity); 926 apei_estatus_for_each_section(estatus, gdata) { 927 sec_type = (guid_t *)gdata->section_type; 928 sec_sev = ghes_severity(gdata->error_severity); 929 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID) 930 fru_id = (guid_t *)gdata->fru_id; 931 932 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT) 933 fru_text = gdata->fru_text; 934 935 ghes_log_hwerr(sev, sec_type); 936 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) { 937 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata); 938 939 atomic_notifier_call_chain(&ghes_report_chain, sev, mem_err); 940 941 arch_apei_report_mem_error(sev, mem_err); 942 queued = ghes_handle_memory_failure(gdata, sev, sync); 943 } else if (guid_equal(sec_type, &CPER_SEC_PCIE)) { 944 ghes_handle_aer(gdata); 945 } else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) { 946 queued = ghes_handle_arm_hw_error(gdata, sev, sync); 947 } else if (guid_equal(sec_type, &CPER_SEC_CXL_PROT_ERR)) { 948 struct cxl_cper_sec_prot_err *prot_err = acpi_hest_get_payload(gdata); 949 950 cxl_cper_post_prot_err(prot_err, gdata->error_severity); 951 } else if (guid_equal(sec_type, &CPER_SEC_CXL_GEN_MEDIA_GUID)) { 952 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata); 953 954 cxl_cper_post_event(CXL_CPER_EVENT_GEN_MEDIA, rec); 955 } else if (guid_equal(sec_type, &CPER_SEC_CXL_DRAM_GUID)) { 956 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata); 957 958 cxl_cper_post_event(CXL_CPER_EVENT_DRAM, rec); 959 } else if (guid_equal(sec_type, &CPER_SEC_CXL_MEM_MODULE_GUID)) { 960 struct cxl_cper_event_rec *rec = acpi_hest_get_payload(gdata); 961 962 cxl_cper_post_event(CXL_CPER_EVENT_MEM_MODULE, rec); 963 } else { 964 void *err = acpi_hest_get_payload(gdata); 965 966 ghes_defer_non_standard_event(gdata, sev); 967 log_non_standard_event(sec_type, fru_id, fru_text, 968 sec_sev, err, 969 gdata->error_data_length); 970 } 971 } 972 973 /* 974 * If no memory failure work is queued for abnormal synchronous 975 * errors, do a force kill. 976 */ 977 if (sync && !queued) { 978 dev_err(ghes->dev, 979 HW_ERR GHES_PFX "%s:%d: synchronous unrecoverable error (SIGBUS)\n", 980 current->comm, task_pid_nr(current)); 981 force_sig(SIGBUS); 982 } 983} 984 985static void __ghes_print_estatus(const char *pfx, 986 const struct acpi_hest_generic *generic, 987 const struct acpi_hest_generic_status *estatus) 988{ 989 static atomic_t seqno; 990 unsigned int curr_seqno; 991 char pfx_seq[64]; 992 993 if (pfx == NULL) { 994 if (ghes_severity(estatus->error_severity) <= 995 GHES_SEV_CORRECTED) 996 pfx = KERN_WARNING; 997 else 998 pfx = KERN_ERR; 999 } 1000 curr_seqno = atomic_inc_return(&seqno); 1001 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno); 1002 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n", 1003 pfx_seq, generic->header.source_id); 1004 cper_estatus_print(pfx_seq, estatus); 1005} 1006 1007static int ghes_print_estatus(const char *pfx, 1008 const struct acpi_hest_generic *generic, 1009 const struct acpi_hest_generic_status *estatus) 1010{ 1011 /* Not more than 2 messages every 5 seconds */ 1012 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2); 1013 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2); 1014 struct ratelimit_state *ratelimit; 1015 1016 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED) 1017 ratelimit = &ratelimit_corrected; 1018 else 1019 ratelimit = &ratelimit_uncorrected; 1020 if (__ratelimit(ratelimit)) { 1021 __ghes_print_estatus(pfx, generic, estatus); 1022 return 1; 1023 } 1024 return 0; 1025} 1026 1027/* 1028 * GHES error status reporting throttle, to report more kinds of 1029 * errors, instead of just most frequently occurred errors. 1030 */ 1031static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus) 1032{ 1033 u32 len; 1034 int i, cached = 0; 1035 unsigned long long now; 1036 struct ghes_estatus_cache *cache; 1037 struct acpi_hest_generic_status *cache_estatus; 1038 1039 len = cper_estatus_len(estatus); 1040 rcu_read_lock(); 1041 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) { 1042 cache = rcu_dereference(ghes_estatus_caches[i]); 1043 if (cache == NULL) 1044 continue; 1045 if (len != cache->estatus_len) 1046 continue; 1047 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache); 1048 if (memcmp(estatus, cache_estatus, len)) 1049 continue; 1050 atomic_inc(&cache->count); 1051 now = sched_clock(); 1052 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC) 1053 cached = 1; 1054 break; 1055 } 1056 rcu_read_unlock(); 1057 return cached; 1058} 1059 1060static struct ghes_estatus_cache *ghes_estatus_cache_alloc( 1061 struct acpi_hest_generic *generic, 1062 struct acpi_hest_generic_status *estatus) 1063{ 1064 int alloced; 1065 u32 len, cache_len; 1066 struct ghes_estatus_cache *cache; 1067 struct acpi_hest_generic_status *cache_estatus; 1068 1069 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced); 1070 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) { 1071 atomic_dec(&ghes_estatus_cache_alloced); 1072 return NULL; 1073 } 1074 len = cper_estatus_len(estatus); 1075 cache_len = GHES_ESTATUS_CACHE_LEN(len); 1076 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len); 1077 if (!cache) { 1078 atomic_dec(&ghes_estatus_cache_alloced); 1079 return NULL; 1080 } 1081 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache); 1082 memcpy(cache_estatus, estatus, len); 1083 cache->estatus_len = len; 1084 atomic_set(&cache->count, 0); 1085 cache->generic = generic; 1086 cache->time_in = sched_clock(); 1087 return cache; 1088} 1089 1090static void ghes_estatus_cache_rcu_free(struct rcu_head *head) 1091{ 1092 struct ghes_estatus_cache *cache; 1093 u32 len; 1094 1095 cache = container_of(head, struct ghes_estatus_cache, rcu); 1096 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache)); 1097 len = GHES_ESTATUS_CACHE_LEN(len); 1098 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len); 1099 atomic_dec(&ghes_estatus_cache_alloced); 1100} 1101 1102static void 1103ghes_estatus_cache_add(struct acpi_hest_generic *generic, 1104 struct acpi_hest_generic_status *estatus) 1105{ 1106 unsigned long long now, duration, period, max_period = 0; 1107 struct ghes_estatus_cache *cache, *new_cache; 1108 struct ghes_estatus_cache __rcu *victim; 1109 int i, slot = -1, count; 1110 1111 new_cache = ghes_estatus_cache_alloc(generic, estatus); 1112 if (!new_cache) 1113 return; 1114 1115 rcu_read_lock(); 1116 now = sched_clock(); 1117 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) { 1118 cache = rcu_dereference(ghes_estatus_caches[i]); 1119 if (cache == NULL) { 1120 slot = i; 1121 break; 1122 } 1123 duration = now - cache->time_in; 1124 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) { 1125 slot = i; 1126 break; 1127 } 1128 count = atomic_read(&cache->count); 1129 period = duration; 1130 do_div(period, (count + 1)); 1131 if (period > max_period) { 1132 max_period = period; 1133 slot = i; 1134 } 1135 } 1136 rcu_read_unlock(); 1137 1138 if (slot != -1) { 1139 /* 1140 * Use release semantics to ensure that ghes_estatus_cached() 1141 * running on another CPU will see the updated cache fields if 1142 * it can see the new value of the pointer. 1143 */ 1144 victim = xchg_release(&ghes_estatus_caches[slot], 1145 RCU_INITIALIZER(new_cache)); 1146 1147 /* 1148 * At this point, victim may point to a cached item different 1149 * from the one based on which we selected the slot. Instead of 1150 * going to the loop again to pick another slot, let's just 1151 * drop the other item anyway: this may cause a false cache 1152 * miss later on, but that won't cause any problems. 1153 */ 1154 if (victim) 1155 call_rcu(&unrcu_pointer(victim)->rcu, 1156 ghes_estatus_cache_rcu_free); 1157 } 1158} 1159 1160static void __ghes_panic(struct ghes *ghes, 1161 struct acpi_hest_generic_status *estatus, 1162 u64 buf_paddr, enum fixed_addresses fixmap_idx) 1163{ 1164 const char *msg = GHES_PFX "Fatal hardware error"; 1165 1166 __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus); 1167 1168 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK); 1169 1170 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx); 1171 1172 if (!panic_timeout) 1173 pr_emerg("%s but panic disabled\n", msg); 1174 1175 panic(msg); 1176} 1177 1178static int ghes_proc(struct ghes *ghes) 1179{ 1180 struct acpi_hest_generic_status *estatus = ghes->estatus; 1181 u64 buf_paddr; 1182 int rc; 1183 1184 rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ); 1185 if (rc) 1186 goto out; 1187 1188 if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC) 1189 __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ); 1190 1191 if (!ghes_estatus_cached(estatus)) { 1192 if (ghes_print_estatus(NULL, ghes->generic, estatus)) 1193 ghes_estatus_cache_add(ghes->generic, estatus); 1194 } 1195 ghes_do_proc(ghes, estatus); 1196 1197out: 1198 ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ); 1199 1200 return rc; 1201} 1202 1203static void ghes_add_timer(struct ghes *ghes) 1204{ 1205 struct acpi_hest_generic *g = ghes->generic; 1206 unsigned long expire; 1207 1208 if (!g->notify.poll_interval) { 1209 pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n", 1210 g->header.source_id); 1211 return; 1212 } 1213 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval); 1214 ghes->timer.expires = round_jiffies_relative(expire); 1215 add_timer(&ghes->timer); 1216} 1217 1218static void ghes_poll_func(struct timer_list *t) 1219{ 1220 struct ghes *ghes = timer_container_of(ghes, t, timer); 1221 unsigned long flags; 1222 1223 spin_lock_irqsave(&ghes_notify_lock_irq, flags); 1224 ghes_proc(ghes); 1225 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags); 1226 if (!(ghes->flags & GHES_EXITING)) 1227 ghes_add_timer(ghes); 1228} 1229 1230static irqreturn_t ghes_irq_func(int irq, void *data) 1231{ 1232 struct ghes *ghes = data; 1233 unsigned long flags; 1234 int rc; 1235 1236 spin_lock_irqsave(&ghes_notify_lock_irq, flags); 1237 rc = ghes_proc(ghes); 1238 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags); 1239 if (rc) 1240 return IRQ_NONE; 1241 1242 return IRQ_HANDLED; 1243} 1244 1245static int ghes_notify_hed(struct notifier_block *this, unsigned long event, 1246 void *data) 1247{ 1248 struct ghes *ghes; 1249 unsigned long flags; 1250 int ret = NOTIFY_DONE; 1251 1252 spin_lock_irqsave(&ghes_notify_lock_irq, flags); 1253 list_for_each_entry_rcu(ghes, &ghes_hed, list) { 1254 if (!ghes_proc(ghes)) 1255 ret = NOTIFY_OK; 1256 } 1257 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags); 1258 1259 return ret; 1260} 1261 1262static struct notifier_block ghes_notifier_hed = { 1263 .notifier_call = ghes_notify_hed, 1264}; 1265 1266/* 1267 * Handlers for CPER records may not be NMI safe. For example, 1268 * memory_failure_queue() takes spinlocks and calls schedule_work_on(). 1269 * In any NMI-like handler, memory from ghes_estatus_pool is used to save 1270 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes 1271 * ghes_proc_in_irq() to run in IRQ context where each estatus in 1272 * ghes_estatus_llist is processed. 1273 * 1274 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache 1275 * to suppress frequent messages. 1276 */ 1277static struct llist_head ghes_estatus_llist; 1278static struct irq_work ghes_proc_irq_work; 1279 1280static void ghes_proc_in_irq(struct irq_work *irq_work) 1281{ 1282 struct llist_node *llnode, *next; 1283 struct ghes_estatus_node *estatus_node; 1284 struct acpi_hest_generic *generic; 1285 struct acpi_hest_generic_status *estatus; 1286 u32 len, node_len; 1287 1288 llnode = llist_del_all(&ghes_estatus_llist); 1289 /* 1290 * Because the time order of estatus in list is reversed, 1291 * revert it back to proper order. 1292 */ 1293 llnode = llist_reverse_order(llnode); 1294 while (llnode) { 1295 next = llnode->next; 1296 estatus_node = llist_entry(llnode, struct ghes_estatus_node, 1297 llnode); 1298 estatus = GHES_ESTATUS_FROM_NODE(estatus_node); 1299 len = cper_estatus_len(estatus); 1300 node_len = GHES_ESTATUS_NODE_LEN(len); 1301 1302 ghes_do_proc(estatus_node->ghes, estatus); 1303 1304 if (!ghes_estatus_cached(estatus)) { 1305 generic = estatus_node->generic; 1306 if (ghes_print_estatus(NULL, generic, estatus)) 1307 ghes_estatus_cache_add(generic, estatus); 1308 } 1309 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node, 1310 node_len); 1311 1312 llnode = next; 1313 } 1314} 1315 1316static void ghes_print_queued_estatus(void) 1317{ 1318 struct llist_node *llnode; 1319 struct ghes_estatus_node *estatus_node; 1320 struct acpi_hest_generic *generic; 1321 struct acpi_hest_generic_status *estatus; 1322 1323 llnode = llist_del_all(&ghes_estatus_llist); 1324 /* 1325 * Because the time order of estatus in list is reversed, 1326 * revert it back to proper order. 1327 */ 1328 llnode = llist_reverse_order(llnode); 1329 while (llnode) { 1330 estatus_node = llist_entry(llnode, struct ghes_estatus_node, 1331 llnode); 1332 estatus = GHES_ESTATUS_FROM_NODE(estatus_node); 1333 generic = estatus_node->generic; 1334 ghes_print_estatus(NULL, generic, estatus); 1335 llnode = llnode->next; 1336 } 1337} 1338 1339static int ghes_in_nmi_queue_one_entry(struct ghes *ghes, 1340 enum fixed_addresses fixmap_idx) 1341{ 1342 struct acpi_hest_generic_status *estatus, tmp_header; 1343 struct ghes_estatus_node *estatus_node; 1344 u32 len, node_len; 1345 u64 buf_paddr; 1346 int sev, rc; 1347 1348 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG)) 1349 return -EOPNOTSUPP; 1350 1351 rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx); 1352 if (rc) { 1353 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx); 1354 return rc; 1355 } 1356 1357 rc = __ghes_check_estatus(ghes, &tmp_header); 1358 if (rc) { 1359 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx); 1360 return rc; 1361 } 1362 1363 len = cper_estatus_len(&tmp_header); 1364 node_len = GHES_ESTATUS_NODE_LEN(len); 1365 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len); 1366 if (!estatus_node) 1367 return -ENOMEM; 1368 1369 estatus_node->ghes = ghes; 1370 estatus_node->generic = ghes->generic; 1371 estatus = GHES_ESTATUS_FROM_NODE(estatus_node); 1372 1373 if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) { 1374 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx); 1375 rc = -ENOENT; 1376 goto no_work; 1377 } 1378 1379 sev = ghes_severity(estatus->error_severity); 1380 if (sev >= GHES_SEV_PANIC) { 1381 ghes_print_queued_estatus(); 1382 __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx); 1383 } 1384 1385 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx); 1386 1387 /* This error has been reported before, don't process it again. */ 1388 if (ghes_estatus_cached(estatus)) 1389 goto no_work; 1390 1391 llist_add(&estatus_node->llnode, &ghes_estatus_llist); 1392 1393 return rc; 1394 1395no_work: 1396 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node, 1397 node_len); 1398 1399 return rc; 1400} 1401 1402static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list, 1403 enum fixed_addresses fixmap_idx) 1404{ 1405 int ret = -ENOENT; 1406 struct ghes *ghes; 1407 1408 rcu_read_lock(); 1409 list_for_each_entry_rcu(ghes, rcu_list, list) { 1410 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) 1411 ret = 0; 1412 } 1413 rcu_read_unlock(); 1414 1415 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret) 1416 irq_work_queue(&ghes_proc_irq_work); 1417 1418 return ret; 1419} 1420 1421#ifdef CONFIG_ACPI_APEI_SEA 1422static LIST_HEAD(ghes_sea); 1423 1424/* 1425 * Return 0 only if one of the SEA error sources successfully reported an error 1426 * record sent from the firmware. 1427 */ 1428int ghes_notify_sea(void) 1429{ 1430 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea); 1431 int rv; 1432 1433 raw_spin_lock(&ghes_notify_lock_sea); 1434 rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA); 1435 raw_spin_unlock(&ghes_notify_lock_sea); 1436 1437 return rv; 1438} 1439 1440static void ghes_sea_add(struct ghes *ghes) 1441{ 1442 mutex_lock(&ghes_list_mutex); 1443 list_add_rcu(&ghes->list, &ghes_sea); 1444 mutex_unlock(&ghes_list_mutex); 1445} 1446 1447static void ghes_sea_remove(struct ghes *ghes) 1448{ 1449 mutex_lock(&ghes_list_mutex); 1450 list_del_rcu(&ghes->list); 1451 mutex_unlock(&ghes_list_mutex); 1452 synchronize_rcu(); 1453} 1454#else /* CONFIG_ACPI_APEI_SEA */ 1455static inline void ghes_sea_add(struct ghes *ghes) { } 1456static inline void ghes_sea_remove(struct ghes *ghes) { } 1457#endif /* CONFIG_ACPI_APEI_SEA */ 1458 1459#ifdef CONFIG_HAVE_ACPI_APEI_NMI 1460/* 1461 * NMI may be triggered on any CPU, so ghes_in_nmi is used for 1462 * having only one concurrent reader. 1463 */ 1464static atomic_t ghes_in_nmi = ATOMIC_INIT(0); 1465 1466static LIST_HEAD(ghes_nmi); 1467 1468static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs) 1469{ 1470 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi); 1471 int ret = NMI_DONE; 1472 1473 if (!atomic_add_unless(&ghes_in_nmi, 1, 1)) 1474 return ret; 1475 1476 raw_spin_lock(&ghes_notify_lock_nmi); 1477 if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI)) 1478 ret = NMI_HANDLED; 1479 raw_spin_unlock(&ghes_notify_lock_nmi); 1480 1481 atomic_dec(&ghes_in_nmi); 1482 return ret; 1483} 1484 1485static void ghes_nmi_add(struct ghes *ghes) 1486{ 1487 mutex_lock(&ghes_list_mutex); 1488 if (list_empty(&ghes_nmi)) 1489 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes"); 1490 list_add_rcu(&ghes->list, &ghes_nmi); 1491 mutex_unlock(&ghes_list_mutex); 1492} 1493 1494static void ghes_nmi_remove(struct ghes *ghes) 1495{ 1496 mutex_lock(&ghes_list_mutex); 1497 list_del_rcu(&ghes->list); 1498 if (list_empty(&ghes_nmi)) 1499 unregister_nmi_handler(NMI_LOCAL, "ghes"); 1500 mutex_unlock(&ghes_list_mutex); 1501 /* 1502 * To synchronize with NMI handler, ghes can only be 1503 * freed after NMI handler finishes. 1504 */ 1505 synchronize_rcu(); 1506} 1507#else /* CONFIG_HAVE_ACPI_APEI_NMI */ 1508static inline void ghes_nmi_add(struct ghes *ghes) { } 1509static inline void ghes_nmi_remove(struct ghes *ghes) { } 1510#endif /* CONFIG_HAVE_ACPI_APEI_NMI */ 1511 1512static void ghes_nmi_init_cxt(void) 1513{ 1514 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq); 1515} 1516 1517static int __ghes_sdei_callback(struct ghes *ghes, 1518 enum fixed_addresses fixmap_idx) 1519{ 1520 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) { 1521 irq_work_queue(&ghes_proc_irq_work); 1522 1523 return 0; 1524 } 1525 1526 return -ENOENT; 1527} 1528 1529static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs, 1530 void *arg) 1531{ 1532 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal); 1533 struct ghes *ghes = arg; 1534 int err; 1535 1536 raw_spin_lock(&ghes_notify_lock_sdei_normal); 1537 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL); 1538 raw_spin_unlock(&ghes_notify_lock_sdei_normal); 1539 1540 return err; 1541} 1542 1543static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs, 1544 void *arg) 1545{ 1546 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical); 1547 struct ghes *ghes = arg; 1548 int err; 1549 1550 raw_spin_lock(&ghes_notify_lock_sdei_critical); 1551 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL); 1552 raw_spin_unlock(&ghes_notify_lock_sdei_critical); 1553 1554 return err; 1555} 1556 1557static int apei_sdei_register_ghes(struct ghes *ghes) 1558{ 1559 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) 1560 return -EOPNOTSUPP; 1561 1562 return sdei_register_ghes(ghes, ghes_sdei_normal_callback, 1563 ghes_sdei_critical_callback); 1564} 1565 1566static int apei_sdei_unregister_ghes(struct ghes *ghes) 1567{ 1568 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) 1569 return -EOPNOTSUPP; 1570 1571 return sdei_unregister_ghes(ghes); 1572} 1573 1574static int ghes_probe(struct platform_device *ghes_dev) 1575{ 1576 struct acpi_hest_generic *generic; 1577 struct ghes *ghes = NULL; 1578 unsigned long flags; 1579 1580 int rc = -EINVAL; 1581 1582 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data; 1583 if (!generic->enabled) 1584 return -ENODEV; 1585 1586 switch (generic->notify.type) { 1587 case ACPI_HEST_NOTIFY_POLLED: 1588 case ACPI_HEST_NOTIFY_EXTERNAL: 1589 case ACPI_HEST_NOTIFY_SCI: 1590 case ACPI_HEST_NOTIFY_GSIV: 1591 case ACPI_HEST_NOTIFY_GPIO: 1592 break; 1593 1594 case ACPI_HEST_NOTIFY_SEA: 1595 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) { 1596 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n", 1597 generic->header.source_id); 1598 rc = -ENOTSUPP; 1599 goto err; 1600 } 1601 break; 1602 case ACPI_HEST_NOTIFY_NMI: 1603 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) { 1604 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n", 1605 generic->header.source_id); 1606 goto err; 1607 } 1608 break; 1609 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED: 1610 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) { 1611 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n", 1612 generic->header.source_id); 1613 goto err; 1614 } 1615 break; 1616 case ACPI_HEST_NOTIFY_LOCAL: 1617 pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n", 1618 generic->header.source_id); 1619 goto err; 1620 default: 1621 pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n", 1622 generic->notify.type, generic->header.source_id); 1623 goto err; 1624 } 1625 1626 rc = -EIO; 1627 if (generic->error_block_length < 1628 sizeof(struct acpi_hest_generic_status)) { 1629 pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n", 1630 generic->error_block_length, generic->header.source_id); 1631 goto err; 1632 } 1633 ghes = ghes_new(generic); 1634 if (IS_ERR(ghes)) { 1635 rc = PTR_ERR(ghes); 1636 ghes = NULL; 1637 goto err; 1638 } 1639 1640 switch (generic->notify.type) { 1641 case ACPI_HEST_NOTIFY_POLLED: 1642 timer_setup(&ghes->timer, ghes_poll_func, 0); 1643 ghes_add_timer(ghes); 1644 break; 1645 case ACPI_HEST_NOTIFY_EXTERNAL: 1646 /* External interrupt vector is GSI */ 1647 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq); 1648 if (rc) { 1649 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n", 1650 generic->header.source_id); 1651 goto err; 1652 } 1653 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED, 1654 "GHES IRQ", ghes); 1655 if (rc) { 1656 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n", 1657 generic->header.source_id); 1658 goto err; 1659 } 1660 break; 1661 1662 case ACPI_HEST_NOTIFY_SCI: 1663 case ACPI_HEST_NOTIFY_GSIV: 1664 case ACPI_HEST_NOTIFY_GPIO: 1665 mutex_lock(&ghes_list_mutex); 1666 if (list_empty(&ghes_hed)) 1667 register_acpi_hed_notifier(&ghes_notifier_hed); 1668 list_add_rcu(&ghes->list, &ghes_hed); 1669 mutex_unlock(&ghes_list_mutex); 1670 break; 1671 1672 case ACPI_HEST_NOTIFY_SEA: 1673 ghes_sea_add(ghes); 1674 break; 1675 case ACPI_HEST_NOTIFY_NMI: 1676 ghes_nmi_add(ghes); 1677 break; 1678 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED: 1679 rc = apei_sdei_register_ghes(ghes); 1680 if (rc) 1681 goto err; 1682 break; 1683 default: 1684 BUG(); 1685 } 1686 1687 platform_set_drvdata(ghes_dev, ghes); 1688 1689 ghes->dev = &ghes_dev->dev; 1690 1691 mutex_lock(&ghes_devs_mutex); 1692 list_add_tail(&ghes->elist, &ghes_devs); 1693 mutex_unlock(&ghes_devs_mutex); 1694 1695 /* Handle any pending errors right away */ 1696 spin_lock_irqsave(&ghes_notify_lock_irq, flags); 1697 ghes_proc(ghes); 1698 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags); 1699 1700 return 0; 1701 1702err: 1703 if (ghes) { 1704 ghes_fini(ghes); 1705 kfree(ghes); 1706 } 1707 return rc; 1708} 1709 1710static void ghes_remove(struct platform_device *ghes_dev) 1711{ 1712 int rc; 1713 struct ghes *ghes; 1714 struct acpi_hest_generic *generic; 1715 1716 ghes = platform_get_drvdata(ghes_dev); 1717 generic = ghes->generic; 1718 1719 ghes->flags |= GHES_EXITING; 1720 switch (generic->notify.type) { 1721 case ACPI_HEST_NOTIFY_POLLED: 1722 timer_shutdown_sync(&ghes->timer); 1723 break; 1724 case ACPI_HEST_NOTIFY_EXTERNAL: 1725 free_irq(ghes->irq, ghes); 1726 break; 1727 1728 case ACPI_HEST_NOTIFY_SCI: 1729 case ACPI_HEST_NOTIFY_GSIV: 1730 case ACPI_HEST_NOTIFY_GPIO: 1731 mutex_lock(&ghes_list_mutex); 1732 list_del_rcu(&ghes->list); 1733 if (list_empty(&ghes_hed)) 1734 unregister_acpi_hed_notifier(&ghes_notifier_hed); 1735 mutex_unlock(&ghes_list_mutex); 1736 synchronize_rcu(); 1737 break; 1738 1739 case ACPI_HEST_NOTIFY_SEA: 1740 ghes_sea_remove(ghes); 1741 break; 1742 case ACPI_HEST_NOTIFY_NMI: 1743 ghes_nmi_remove(ghes); 1744 break; 1745 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED: 1746 rc = apei_sdei_unregister_ghes(ghes); 1747 if (rc) { 1748 /* 1749 * Returning early results in a resource leak, but we're 1750 * only here if stopping the hardware failed. 1751 */ 1752 dev_err(&ghes_dev->dev, "Failed to unregister ghes (%pe)\n", 1753 ERR_PTR(rc)); 1754 return; 1755 } 1756 break; 1757 default: 1758 BUG(); 1759 break; 1760 } 1761 1762 ghes_fini(ghes); 1763 1764 mutex_lock(&ghes_devs_mutex); 1765 list_del(&ghes->elist); 1766 mutex_unlock(&ghes_devs_mutex); 1767 1768 kfree(ghes); 1769} 1770 1771static struct platform_driver ghes_platform_driver = { 1772 .driver = { 1773 .name = "GHES", 1774 }, 1775 .probe = ghes_probe, 1776 .remove = ghes_remove, 1777}; 1778 1779void __init acpi_ghes_init(void) 1780{ 1781 int rc; 1782 1783 acpi_sdei_init(); 1784 1785 if (acpi_disabled) 1786 return; 1787 1788 switch (hest_disable) { 1789 case HEST_NOT_FOUND: 1790 return; 1791 case HEST_DISABLED: 1792 pr_info(GHES_PFX "HEST is not enabled!\n"); 1793 return; 1794 default: 1795 break; 1796 } 1797 1798 if (ghes_disable) { 1799 pr_info(GHES_PFX "GHES is not enabled!\n"); 1800 return; 1801 } 1802 1803 ghes_nmi_init_cxt(); 1804 1805 rc = platform_driver_register(&ghes_platform_driver); 1806 if (rc) 1807 return; 1808 1809 rc = apei_osc_setup(); 1810 if (rc == 0 && osc_sb_apei_support_acked) 1811 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n"); 1812 else if (rc == 0 && !osc_sb_apei_support_acked) 1813 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n"); 1814 else if (rc && osc_sb_apei_support_acked) 1815 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n"); 1816 else 1817 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n"); 1818} 1819 1820/* 1821 * Known x86 systems that prefer GHES error reporting: 1822 */ 1823static struct acpi_platform_list plat_list[] = { 1824 {"HPE ", "Server ", 0, ACPI_SIG_FADT, all_versions}, 1825 { } /* End */ 1826}; 1827 1828struct list_head *ghes_get_devices(void) 1829{ 1830 int idx = -1; 1831 1832 if (IS_ENABLED(CONFIG_X86)) { 1833 idx = acpi_match_platform_list(plat_list); 1834 if (idx < 0) { 1835 if (!ghes_edac_force_enable) 1836 return NULL; 1837 1838 pr_warn_once("Force-loading ghes_edac on an unsupported platform. You're on your own!\n"); 1839 } 1840 } else if (list_empty(&ghes_devs)) { 1841 return NULL; 1842 } 1843 1844 return &ghes_devs; 1845} 1846EXPORT_SYMBOL_GPL(ghes_get_devices); 1847 1848void ghes_register_report_chain(struct notifier_block *nb) 1849{ 1850 atomic_notifier_chain_register(&ghes_report_chain, nb); 1851} 1852EXPORT_SYMBOL_GPL(ghes_register_report_chain); 1853 1854void ghes_unregister_report_chain(struct notifier_block *nb) 1855{ 1856 atomic_notifier_chain_unregister(&ghes_report_chain, nb); 1857} 1858EXPORT_SYMBOL_GPL(ghes_unregister_report_chain);
~/
Read-only file preview.