1 // SPDX-License-Identifier: GPL-2.0-only
3 * APEI Generic Hardware Error Source support
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.
13 * For more information about Generic Hardware Error Source, please
14 * refer to ACPI Specification version 4.0, section 17.3.2.6
16 * Copyright 2010,2011 Intel Corp.
17 * Author: Huang Ying <ying.huang@intel.com>
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>
26 #include <linux/interrupt.h>
27 #include <linux/timer.h>
28 #include <linux/cper.h>
29 #include <linux/platform_device.h>
30 #include <linux/mutex.h>
31 #include <linux/ratelimit.h>
32 #include <linux/vmalloc.h>
33 #include <linux/irq_work.h>
34 #include <linux/llist.h>
35 #include <linux/genalloc.h>
36 #include <linux/pci.h>
37 #include <linux/pfn.h>
38 #include <linux/aer.h>
39 #include <linux/nmi.h>
40 #include <linux/sched/clock.h>
41 #include <linux/uuid.h>
42 #include <linux/ras.h>
44 #include <acpi/actbl1.h>
45 #include <acpi/ghes.h>
46 #include <acpi/apei.h>
47 #include <asm/fixmap.h>
48 #include <asm/tlbflush.h>
49 #include <ras/ras_event.h>
51 #include "apei-internal.h"
53 #define GHES_PFX "GHES: "
55 #define GHES_ESTATUS_MAX_SIZE 65536
56 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
58 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
60 /* This is just an estimation for memory pool allocation */
61 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
63 #define GHES_ESTATUS_CACHES_SIZE 4
65 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
66 /* Prevent too many caches are allocated because of RCU */
67 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
69 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
70 (sizeof(struct ghes_estatus_cache) + (estatus_len))
71 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
72 ((struct acpi_hest_generic_status *) \
73 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
75 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
76 (sizeof(struct ghes_estatus_node) + (estatus_len))
77 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
78 ((struct acpi_hest_generic_status *) \
79 ((struct ghes_estatus_node *)(estatus_node) + 1))
82 * NMI-like notifications vary by architecture, before the compiler can prune
83 * unused static functions it needs a value for these enums.
85 #ifndef CONFIG_ARM_SDE_INTERFACE
86 #define FIX_APEI_GHES_SDEI_NORMAL __end_of_fixed_addresses
87 #define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses
90 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
92 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
96 * This driver isn't really modular, however for the time being,
97 * continuing to use module_param is the easiest way to remain
98 * compatible with existing boot arg use cases.
101 module_param_named(disable, ghes_disable, bool, 0);
104 * All error sources notified with HED (Hardware Error Device) share a
105 * single notifier callback, so they need to be linked and checked one
106 * by one. This holds true for NMI too.
108 * RCU is used for these lists, so ghes_list_mutex is only used for
109 * list changing, not for traversing.
111 static LIST_HEAD(ghes_hed);
112 static DEFINE_MUTEX(ghes_list_mutex);
115 * Because the memory area used to transfer hardware error information
116 * from BIOS to Linux can be determined only in NMI, IRQ or timer
117 * handler, but general ioremap can not be used in atomic context, so
118 * the fixmap is used instead.
120 * This spinlock is used to prevent the fixmap entry from being used
123 static DEFINE_SPINLOCK(ghes_notify_lock_irq);
125 static struct gen_pool *ghes_estatus_pool;
126 static unsigned long ghes_estatus_pool_size_request;
128 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
129 static atomic_t ghes_estatus_cache_alloced;
131 static int ghes_panic_timeout __read_mostly = 30;
133 static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
138 paddr = PFN_PHYS(pfn);
139 prot = arch_apei_get_mem_attribute(paddr);
140 __set_fixmap(fixmap_idx, paddr, prot);
142 return (void __iomem *) __fix_to_virt(fixmap_idx);
145 static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
147 int _idx = virt_to_fix((unsigned long)vaddr);
149 WARN_ON_ONCE(fixmap_idx != _idx);
150 clear_fixmap(fixmap_idx);
153 int ghes_estatus_pool_init(int num_ghes)
155 unsigned long addr, len;
157 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
158 if (!ghes_estatus_pool)
161 len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
162 len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
164 ghes_estatus_pool_size_request = PAGE_ALIGN(len);
165 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
170 * New allocation must be visible in all pgd before it can be found by
171 * an NMI allocating from the pool.
175 return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
178 static int map_gen_v2(struct ghes *ghes)
180 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
183 static void unmap_gen_v2(struct ghes *ghes)
185 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
188 static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
193 rc = apei_read(&val, &gv2->read_ack_register);
197 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
198 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
200 apei_write(val, &gv2->read_ack_register);
203 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
206 unsigned int error_block_length;
209 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
213 ghes->generic = generic;
214 if (is_hest_type_generic_v2(ghes)) {
215 rc = map_gen_v2(ghes);
220 rc = apei_map_generic_address(&generic->error_status_address);
222 goto err_unmap_read_ack_addr;
223 error_block_length = generic->error_block_length;
224 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
225 pr_warning(FW_WARN GHES_PFX
226 "Error status block length is too long: %u for "
227 "generic hardware error source: %d.\n",
228 error_block_length, generic->header.source_id);
229 error_block_length = GHES_ESTATUS_MAX_SIZE;
231 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
232 if (!ghes->estatus) {
234 goto err_unmap_status_addr;
239 err_unmap_status_addr:
240 apei_unmap_generic_address(&generic->error_status_address);
241 err_unmap_read_ack_addr:
242 if (is_hest_type_generic_v2(ghes))
249 static void ghes_fini(struct ghes *ghes)
251 kfree(ghes->estatus);
252 apei_unmap_generic_address(&ghes->generic->error_status_address);
253 if (is_hest_type_generic_v2(ghes))
257 static inline int ghes_severity(int severity)
260 case CPER_SEV_INFORMATIONAL:
262 case CPER_SEV_CORRECTED:
263 return GHES_SEV_CORRECTED;
264 case CPER_SEV_RECOVERABLE:
265 return GHES_SEV_RECOVERABLE;
267 return GHES_SEV_PANIC;
269 /* Unknown, go panic */
270 return GHES_SEV_PANIC;
274 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
276 enum fixed_addresses fixmap_idx)
283 offset = paddr - (paddr & PAGE_MASK);
284 vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
285 trunk = PAGE_SIZE - offset;
286 trunk = min(trunk, len);
288 memcpy_fromio(buffer, vaddr + offset, trunk);
290 memcpy_toio(vaddr + offset, buffer, trunk);
294 ghes_unmap(vaddr, fixmap_idx);
298 /* Check the top-level record header has an appropriate size. */
299 static int __ghes_check_estatus(struct ghes *ghes,
300 struct acpi_hest_generic_status *estatus)
302 u32 len = cper_estatus_len(estatus);
304 if (len < sizeof(*estatus)) {
305 pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
309 if (len > ghes->generic->error_block_length) {
310 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
314 if (cper_estatus_check_header(estatus)) {
315 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
322 /* Read the CPER block, returning its address, and header in estatus. */
323 static int __ghes_peek_estatus(struct ghes *ghes,
324 struct acpi_hest_generic_status *estatus,
325 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
327 struct acpi_hest_generic *g = ghes->generic;
330 rc = apei_read(buf_paddr, &g->error_status_address);
333 pr_warn_ratelimited(FW_WARN GHES_PFX
334 "Failed to read error status block address for hardware error source: %d.\n",
335 g->header.source_id);
341 ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
343 if (!estatus->block_status) {
348 return __ghes_check_estatus(ghes, estatus);
351 static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
352 u64 buf_paddr, enum fixed_addresses fixmap_idx,
355 ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
356 if (cper_estatus_check(estatus)) {
357 pr_warn_ratelimited(FW_WARN GHES_PFX
358 "Failed to read error status block!\n");
365 static int ghes_read_estatus(struct ghes *ghes,
366 struct acpi_hest_generic_status *estatus,
367 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
371 rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
375 rc = __ghes_check_estatus(ghes, estatus);
379 return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
380 cper_estatus_len(estatus));
383 static void ghes_clear_estatus(struct ghes *ghes,
384 struct acpi_hest_generic_status *estatus,
385 u64 buf_paddr, enum fixed_addresses fixmap_idx)
387 estatus->block_status = 0;
392 ghes_copy_tofrom_phys(estatus, buf_paddr,
393 sizeof(estatus->block_status), 0,
397 * GHESv2 type HEST entries introduce support for error acknowledgment,
398 * so only acknowledge the error if this support is present.
400 if (is_hest_type_generic_v2(ghes))
401 ghes_ack_error(ghes->generic_v2);
404 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
406 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
409 int sec_sev = ghes_severity(gdata->error_severity);
410 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
412 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
415 pfn = mem_err->physical_addr >> PAGE_SHIFT;
416 if (!pfn_valid(pfn)) {
417 pr_warn_ratelimited(FW_WARN GHES_PFX
418 "Invalid address in generic error data: %#llx\n",
419 mem_err->physical_addr);
423 /* iff following two events can be handled properly by now */
424 if (sec_sev == GHES_SEV_CORRECTED &&
425 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
426 flags = MF_SOFT_OFFLINE;
427 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
431 memory_failure_queue(pfn, flags);
436 * PCIe AER errors need to be sent to the AER driver for reporting and
437 * recovery. The GHES severities map to the following AER severities and
438 * require the following handling:
440 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
441 * These need to be reported by the AER driver but no recovery is
443 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
444 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
445 * These both need to be reported and recovered from by the AER driver.
446 * GHES_SEV_PANIC does not make it to this handling since the kernel must
449 static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
451 #ifdef CONFIG_ACPI_APEI_PCIEAER
452 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
454 if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
455 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
459 devfn = PCI_DEVFN(pcie_err->device_id.device,
460 pcie_err->device_id.function);
461 aer_severity = cper_severity_to_aer(gdata->error_severity);
464 * If firmware reset the component to contain
465 * the error, we must reinitialize it before
466 * use, so treat it as a fatal AER error.
468 if (gdata->flags & CPER_SEC_RESET)
469 aer_severity = AER_FATAL;
471 aer_recover_queue(pcie_err->device_id.segment,
472 pcie_err->device_id.bus,
474 (struct aer_capability_regs *)
480 static void ghes_do_proc(struct ghes *ghes,
481 const struct acpi_hest_generic_status *estatus)
484 struct acpi_hest_generic_data *gdata;
486 guid_t *fru_id = &NULL_UUID_LE;
489 sev = ghes_severity(estatus->error_severity);
490 apei_estatus_for_each_section(estatus, gdata) {
491 sec_type = (guid_t *)gdata->section_type;
492 sec_sev = ghes_severity(gdata->error_severity);
493 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
494 fru_id = (guid_t *)gdata->fru_id;
496 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
497 fru_text = gdata->fru_text;
499 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
500 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
502 ghes_edac_report_mem_error(sev, mem_err);
504 arch_apei_report_mem_error(sev, mem_err);
505 ghes_handle_memory_failure(gdata, sev);
507 else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
508 ghes_handle_aer(gdata);
510 else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
511 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
513 log_arm_hw_error(err);
515 void *err = acpi_hest_get_payload(gdata);
517 log_non_standard_event(sec_type, fru_id, fru_text,
519 gdata->error_data_length);
524 static void __ghes_print_estatus(const char *pfx,
525 const struct acpi_hest_generic *generic,
526 const struct acpi_hest_generic_status *estatus)
528 static atomic_t seqno;
529 unsigned int curr_seqno;
533 if (ghes_severity(estatus->error_severity) <=
539 curr_seqno = atomic_inc_return(&seqno);
540 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
541 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
542 pfx_seq, generic->header.source_id);
543 cper_estatus_print(pfx_seq, estatus);
546 static int ghes_print_estatus(const char *pfx,
547 const struct acpi_hest_generic *generic,
548 const struct acpi_hest_generic_status *estatus)
550 /* Not more than 2 messages every 5 seconds */
551 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
552 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
553 struct ratelimit_state *ratelimit;
555 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
556 ratelimit = &ratelimit_corrected;
558 ratelimit = &ratelimit_uncorrected;
559 if (__ratelimit(ratelimit)) {
560 __ghes_print_estatus(pfx, generic, estatus);
567 * GHES error status reporting throttle, to report more kinds of
568 * errors, instead of just most frequently occurred errors.
570 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
574 unsigned long long now;
575 struct ghes_estatus_cache *cache;
576 struct acpi_hest_generic_status *cache_estatus;
578 len = cper_estatus_len(estatus);
580 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
581 cache = rcu_dereference(ghes_estatus_caches[i]);
584 if (len != cache->estatus_len)
586 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
587 if (memcmp(estatus, cache_estatus, len))
589 atomic_inc(&cache->count);
591 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
599 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
600 struct acpi_hest_generic *generic,
601 struct acpi_hest_generic_status *estatus)
605 struct ghes_estatus_cache *cache;
606 struct acpi_hest_generic_status *cache_estatus;
608 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
609 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
610 atomic_dec(&ghes_estatus_cache_alloced);
613 len = cper_estatus_len(estatus);
614 cache_len = GHES_ESTATUS_CACHE_LEN(len);
615 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
617 atomic_dec(&ghes_estatus_cache_alloced);
620 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
621 memcpy(cache_estatus, estatus, len);
622 cache->estatus_len = len;
623 atomic_set(&cache->count, 0);
624 cache->generic = generic;
625 cache->time_in = sched_clock();
629 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
633 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
634 len = GHES_ESTATUS_CACHE_LEN(len);
635 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
636 atomic_dec(&ghes_estatus_cache_alloced);
639 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
641 struct ghes_estatus_cache *cache;
643 cache = container_of(head, struct ghes_estatus_cache, rcu);
644 ghes_estatus_cache_free(cache);
647 static void ghes_estatus_cache_add(
648 struct acpi_hest_generic *generic,
649 struct acpi_hest_generic_status *estatus)
651 int i, slot = -1, count;
652 unsigned long long now, duration, period, max_period = 0;
653 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
655 new_cache = ghes_estatus_cache_alloc(generic, estatus);
656 if (new_cache == NULL)
660 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
661 cache = rcu_dereference(ghes_estatus_caches[i]);
667 duration = now - cache->time_in;
668 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
673 count = atomic_read(&cache->count);
675 do_div(period, (count + 1));
676 if (period > max_period) {
682 /* new_cache must be put into array after its contents are written */
684 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
685 slot_cache, new_cache) == slot_cache) {
687 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
689 ghes_estatus_cache_free(new_cache);
693 static void __ghes_panic(struct ghes *ghes,
694 struct acpi_hest_generic_status *estatus,
695 u64 buf_paddr, enum fixed_addresses fixmap_idx)
697 __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
699 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
701 /* reboot to log the error! */
703 panic_timeout = ghes_panic_timeout;
704 panic("Fatal hardware error!");
707 static int ghes_proc(struct ghes *ghes)
709 struct acpi_hest_generic_status *estatus = ghes->estatus;
713 rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
717 if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
718 __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
720 if (!ghes_estatus_cached(estatus)) {
721 if (ghes_print_estatus(NULL, ghes->generic, estatus))
722 ghes_estatus_cache_add(ghes->generic, estatus);
724 ghes_do_proc(ghes, estatus);
727 ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
732 static void ghes_add_timer(struct ghes *ghes)
734 struct acpi_hest_generic *g = ghes->generic;
735 unsigned long expire;
737 if (!g->notify.poll_interval) {
738 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
739 g->header.source_id);
742 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
743 ghes->timer.expires = round_jiffies_relative(expire);
744 add_timer(&ghes->timer);
747 static void ghes_poll_func(struct timer_list *t)
749 struct ghes *ghes = from_timer(ghes, t, timer);
752 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
754 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
755 if (!(ghes->flags & GHES_EXITING))
756 ghes_add_timer(ghes);
759 static irqreturn_t ghes_irq_func(int irq, void *data)
761 struct ghes *ghes = data;
765 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
766 rc = ghes_proc(ghes);
767 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
774 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
779 int ret = NOTIFY_DONE;
781 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
783 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
784 if (!ghes_proc(ghes))
788 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
793 static struct notifier_block ghes_notifier_hed = {
794 .notifier_call = ghes_notify_hed,
798 * Handlers for CPER records may not be NMI safe. For example,
799 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
800 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
801 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
802 * ghes_proc_in_irq() to run in IRQ context where each estatus in
803 * ghes_estatus_llist is processed.
805 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
806 * to suppress frequent messages.
808 static struct llist_head ghes_estatus_llist;
809 static struct irq_work ghes_proc_irq_work;
811 static void ghes_proc_in_irq(struct irq_work *irq_work)
813 struct llist_node *llnode, *next;
814 struct ghes_estatus_node *estatus_node;
815 struct acpi_hest_generic *generic;
816 struct acpi_hest_generic_status *estatus;
819 llnode = llist_del_all(&ghes_estatus_llist);
821 * Because the time order of estatus in list is reversed,
822 * revert it back to proper order.
824 llnode = llist_reverse_order(llnode);
827 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
829 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
830 len = cper_estatus_len(estatus);
831 node_len = GHES_ESTATUS_NODE_LEN(len);
832 ghes_do_proc(estatus_node->ghes, estatus);
833 if (!ghes_estatus_cached(estatus)) {
834 generic = estatus_node->generic;
835 if (ghes_print_estatus(NULL, generic, estatus))
836 ghes_estatus_cache_add(generic, estatus);
838 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
844 static void ghes_print_queued_estatus(void)
846 struct llist_node *llnode;
847 struct ghes_estatus_node *estatus_node;
848 struct acpi_hest_generic *generic;
849 struct acpi_hest_generic_status *estatus;
851 llnode = llist_del_all(&ghes_estatus_llist);
853 * Because the time order of estatus in list is reversed,
854 * revert it back to proper order.
856 llnode = llist_reverse_order(llnode);
858 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
860 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
861 generic = estatus_node->generic;
862 ghes_print_estatus(NULL, generic, estatus);
863 llnode = llnode->next;
867 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
868 enum fixed_addresses fixmap_idx)
870 struct acpi_hest_generic_status *estatus, tmp_header;
871 struct ghes_estatus_node *estatus_node;
876 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
879 rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
881 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
885 rc = __ghes_check_estatus(ghes, &tmp_header);
887 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
891 len = cper_estatus_len(&tmp_header);
892 node_len = GHES_ESTATUS_NODE_LEN(len);
893 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
897 estatus_node->ghes = ghes;
898 estatus_node->generic = ghes->generic;
899 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
901 if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
902 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
907 sev = ghes_severity(estatus->error_severity);
908 if (sev >= GHES_SEV_PANIC) {
909 ghes_print_queued_estatus();
910 __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
913 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
915 /* This error has been reported before, don't process it again. */
916 if (ghes_estatus_cached(estatus))
919 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
924 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
930 static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
931 enum fixed_addresses fixmap_idx)
937 list_for_each_entry_rcu(ghes, rcu_list, list) {
938 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
943 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
944 irq_work_queue(&ghes_proc_irq_work);
949 #ifdef CONFIG_ACPI_APEI_SEA
950 static LIST_HEAD(ghes_sea);
953 * Return 0 only if one of the SEA error sources successfully reported an error
954 * record sent from the firmware.
956 int ghes_notify_sea(void)
958 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
961 raw_spin_lock(&ghes_notify_lock_sea);
962 rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
963 raw_spin_unlock(&ghes_notify_lock_sea);
968 static void ghes_sea_add(struct ghes *ghes)
970 mutex_lock(&ghes_list_mutex);
971 list_add_rcu(&ghes->list, &ghes_sea);
972 mutex_unlock(&ghes_list_mutex);
975 static void ghes_sea_remove(struct ghes *ghes)
977 mutex_lock(&ghes_list_mutex);
978 list_del_rcu(&ghes->list);
979 mutex_unlock(&ghes_list_mutex);
982 #else /* CONFIG_ACPI_APEI_SEA */
983 static inline void ghes_sea_add(struct ghes *ghes) { }
984 static inline void ghes_sea_remove(struct ghes *ghes) { }
985 #endif /* CONFIG_ACPI_APEI_SEA */
987 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
989 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
990 * having only one concurrent reader.
992 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
994 static LIST_HEAD(ghes_nmi);
996 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
998 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1001 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1004 raw_spin_lock(&ghes_notify_lock_nmi);
1005 if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1007 raw_spin_unlock(&ghes_notify_lock_nmi);
1009 atomic_dec(&ghes_in_nmi);
1013 static void ghes_nmi_add(struct ghes *ghes)
1015 mutex_lock(&ghes_list_mutex);
1016 if (list_empty(&ghes_nmi))
1017 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1018 list_add_rcu(&ghes->list, &ghes_nmi);
1019 mutex_unlock(&ghes_list_mutex);
1022 static void ghes_nmi_remove(struct ghes *ghes)
1024 mutex_lock(&ghes_list_mutex);
1025 list_del_rcu(&ghes->list);
1026 if (list_empty(&ghes_nmi))
1027 unregister_nmi_handler(NMI_LOCAL, "ghes");
1028 mutex_unlock(&ghes_list_mutex);
1030 * To synchronize with NMI handler, ghes can only be
1031 * freed after NMI handler finishes.
1035 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1036 static inline void ghes_nmi_add(struct ghes *ghes) { }
1037 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1038 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1040 static void ghes_nmi_init_cxt(void)
1042 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1045 static int __ghes_sdei_callback(struct ghes *ghes,
1046 enum fixed_addresses fixmap_idx)
1048 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1049 irq_work_queue(&ghes_proc_irq_work);
1057 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1060 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1061 struct ghes *ghes = arg;
1064 raw_spin_lock(&ghes_notify_lock_sdei_normal);
1065 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1066 raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1071 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1074 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1075 struct ghes *ghes = arg;
1078 raw_spin_lock(&ghes_notify_lock_sdei_critical);
1079 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1080 raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1085 static int apei_sdei_register_ghes(struct ghes *ghes)
1087 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1090 return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1091 ghes_sdei_critical_callback);
1094 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1096 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1099 return sdei_unregister_ghes(ghes);
1102 static int ghes_probe(struct platform_device *ghes_dev)
1104 struct acpi_hest_generic *generic;
1105 struct ghes *ghes = NULL;
1106 unsigned long flags;
1110 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1111 if (!generic->enabled)
1114 switch (generic->notify.type) {
1115 case ACPI_HEST_NOTIFY_POLLED:
1116 case ACPI_HEST_NOTIFY_EXTERNAL:
1117 case ACPI_HEST_NOTIFY_SCI:
1118 case ACPI_HEST_NOTIFY_GSIV:
1119 case ACPI_HEST_NOTIFY_GPIO:
1122 case ACPI_HEST_NOTIFY_SEA:
1123 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1124 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1125 generic->header.source_id);
1130 case ACPI_HEST_NOTIFY_NMI:
1131 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1132 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1133 generic->header.source_id);
1137 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1138 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1139 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1140 generic->header.source_id);
1144 case ACPI_HEST_NOTIFY_LOCAL:
1145 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1146 generic->header.source_id);
1149 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1150 generic->notify.type, generic->header.source_id);
1155 if (generic->error_block_length <
1156 sizeof(struct acpi_hest_generic_status)) {
1157 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1158 generic->error_block_length,
1159 generic->header.source_id);
1162 ghes = ghes_new(generic);
1169 switch (generic->notify.type) {
1170 case ACPI_HEST_NOTIFY_POLLED:
1171 timer_setup(&ghes->timer, ghes_poll_func, TIMER_DEFERRABLE);
1172 ghes_add_timer(ghes);
1174 case ACPI_HEST_NOTIFY_EXTERNAL:
1175 /* External interrupt vector is GSI */
1176 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1178 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1179 generic->header.source_id);
1182 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1185 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1186 generic->header.source_id);
1191 case ACPI_HEST_NOTIFY_SCI:
1192 case ACPI_HEST_NOTIFY_GSIV:
1193 case ACPI_HEST_NOTIFY_GPIO:
1194 mutex_lock(&ghes_list_mutex);
1195 if (list_empty(&ghes_hed))
1196 register_acpi_hed_notifier(&ghes_notifier_hed);
1197 list_add_rcu(&ghes->list, &ghes_hed);
1198 mutex_unlock(&ghes_list_mutex);
1201 case ACPI_HEST_NOTIFY_SEA:
1204 case ACPI_HEST_NOTIFY_NMI:
1207 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1208 rc = apei_sdei_register_ghes(ghes);
1216 platform_set_drvdata(ghes_dev, ghes);
1218 ghes_edac_register(ghes, &ghes_dev->dev);
1220 /* Handle any pending errors right away */
1221 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1223 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1235 static int ghes_remove(struct platform_device *ghes_dev)
1239 struct acpi_hest_generic *generic;
1241 ghes = platform_get_drvdata(ghes_dev);
1242 generic = ghes->generic;
1244 ghes->flags |= GHES_EXITING;
1245 switch (generic->notify.type) {
1246 case ACPI_HEST_NOTIFY_POLLED:
1247 del_timer_sync(&ghes->timer);
1249 case ACPI_HEST_NOTIFY_EXTERNAL:
1250 free_irq(ghes->irq, ghes);
1253 case ACPI_HEST_NOTIFY_SCI:
1254 case ACPI_HEST_NOTIFY_GSIV:
1255 case ACPI_HEST_NOTIFY_GPIO:
1256 mutex_lock(&ghes_list_mutex);
1257 list_del_rcu(&ghes->list);
1258 if (list_empty(&ghes_hed))
1259 unregister_acpi_hed_notifier(&ghes_notifier_hed);
1260 mutex_unlock(&ghes_list_mutex);
1264 case ACPI_HEST_NOTIFY_SEA:
1265 ghes_sea_remove(ghes);
1267 case ACPI_HEST_NOTIFY_NMI:
1268 ghes_nmi_remove(ghes);
1270 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1271 rc = apei_sdei_unregister_ghes(ghes);
1282 ghes_edac_unregister(ghes);
1286 platform_set_drvdata(ghes_dev, NULL);
1291 static struct platform_driver ghes_platform_driver = {
1295 .probe = ghes_probe,
1296 .remove = ghes_remove,
1299 static int __init ghes_init(void)
1306 switch (hest_disable) {
1307 case HEST_NOT_FOUND:
1310 pr_info(GHES_PFX "HEST is not enabled!\n");
1317 pr_info(GHES_PFX "GHES is not enabled!\n");
1321 ghes_nmi_init_cxt();
1323 rc = platform_driver_register(&ghes_platform_driver);
1327 rc = apei_osc_setup();
1328 if (rc == 0 && osc_sb_apei_support_acked)
1329 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1330 else if (rc == 0 && !osc_sb_apei_support_acked)
1331 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1332 else if (rc && osc_sb_apei_support_acked)
1333 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1335 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1341 device_initcall(ghes_init);