2 * APEI Generic Hardware Error Source support
4 * Generic Hardware Error Source provides a way to report platform
5 * hardware errors (such as that from chipset). It works in so called
6 * "Firmware First" mode, that is, hardware errors are reported to
7 * firmware firstly, then reported to Linux by firmware. This way,
8 * some non-standard hardware error registers or non-standard hardware
9 * link can be checked by firmware to produce more hardware error
10 * information for Linux.
12 * For more information about Generic Hardware Error Source, please
13 * refer to ACPI Specification version 4.0, section 17.3.2.6
15 * Copyright 2010,2011 Intel Corp.
16 * Author: Huang Ying <ying.huang@intel.com>
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License version
20 * 2 as published by the Free Software Foundation;
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
28 #include <linux/kernel.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/acpi.h>
33 #include <linux/interrupt.h>
34 #include <linux/timer.h>
35 #include <linux/cper.h>
36 #include <linux/kdebug.h>
37 #include <linux/platform_device.h>
38 #include <linux/mutex.h>
39 #include <linux/ratelimit.h>
40 #include <linux/vmalloc.h>
41 #include <linux/irq_work.h>
42 #include <linux/llist.h>
43 #include <linux/genalloc.h>
44 #include <linux/pci.h>
45 #include <linux/aer.h>
46 #include <linux/nmi.h>
47 #include <linux/sched/clock.h>
48 #include <linux/uuid.h>
49 #include <linux/ras.h>
51 #include <acpi/actbl1.h>
52 #include <acpi/ghes.h>
53 #include <acpi/apei.h>
54 #include <asm/tlbflush.h>
55 #include <ras/ras_event.h>
57 #include "apei-internal.h"
59 #define GHES_PFX "GHES: "
61 #define GHES_ESTATUS_MAX_SIZE 65536
62 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
64 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
66 /* This is just an estimation for memory pool allocation */
67 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
69 #define GHES_ESTATUS_CACHES_SIZE 4
71 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
72 /* Prevent too many caches are allocated because of RCU */
73 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
75 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
76 (sizeof(struct ghes_estatus_cache) + (estatus_len))
77 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
78 ((struct acpi_hest_generic_status *) \
79 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
81 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
82 (sizeof(struct ghes_estatus_node) + (estatus_len))
83 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
84 ((struct acpi_hest_generic_status *) \
85 ((struct ghes_estatus_node *)(estatus_node) + 1))
87 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
89 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
93 * This driver isn't really modular, however for the time being,
94 * continuing to use module_param is the easiest way to remain
95 * compatible with existing boot arg use cases.
98 module_param_named(disable, ghes_disable, bool, 0);
101 * All error sources notified with HED (Hardware Error Device) share a
102 * single notifier callback, so they need to be linked and checked one
103 * by one. This holds true for NMI too.
105 * RCU is used for these lists, so ghes_list_mutex is only used for
106 * list changing, not for traversing.
108 static LIST_HEAD(ghes_hed);
109 static DEFINE_MUTEX(ghes_list_mutex);
112 * Because the memory area used to transfer hardware error information
113 * from BIOS to Linux can be determined only in NMI, IRQ or timer
114 * handler, but general ioremap can not be used in atomic context, so
115 * a special version of atomic ioremap is implemented for that.
119 * Two virtual pages are used, one for IRQ/PROCESS context, the other for
120 * NMI context (optionally).
122 #define GHES_IOREMAP_PAGES 2
123 #define GHES_IOREMAP_IRQ_PAGE(base) (base)
124 #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
126 /* virtual memory area for atomic ioremap */
127 static struct vm_struct *ghes_ioremap_area;
129 * These 2 spinlock is used to prevent atomic ioremap virtual memory
130 * area from being mapped simultaneously.
132 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
133 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
135 static struct gen_pool *ghes_estatus_pool;
136 static unsigned long ghes_estatus_pool_size_request;
138 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
139 static atomic_t ghes_estatus_cache_alloced;
141 static int ghes_panic_timeout __read_mostly = 30;
143 static int ghes_ioremap_init(void)
145 ghes_ioremap_area = __get_vm_area(PAGE_SIZE * GHES_IOREMAP_PAGES,
146 VM_IOREMAP, VMALLOC_START, VMALLOC_END);
147 if (!ghes_ioremap_area) {
148 pr_err(GHES_PFX "Failed to allocate virtual memory area for atomic ioremap.\n");
155 static void ghes_ioremap_exit(void)
157 free_vm_area(ghes_ioremap_area);
160 static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
166 vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
168 paddr = pfn << PAGE_SHIFT;
169 prot = arch_apei_get_mem_attribute(paddr);
170 ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
172 return (void __iomem *)vaddr;
175 static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
177 unsigned long vaddr, paddr;
180 vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
182 paddr = pfn << PAGE_SHIFT;
183 prot = arch_apei_get_mem_attribute(paddr);
185 ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
187 return (void __iomem *)vaddr;
190 static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
192 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
193 void *base = ghes_ioremap_area->addr;
195 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
196 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
197 arch_apei_flush_tlb_one(vaddr);
200 static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
202 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
203 void *base = ghes_ioremap_area->addr;
205 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
206 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
207 arch_apei_flush_tlb_one(vaddr);
210 static int ghes_estatus_pool_init(void)
212 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
213 if (!ghes_estatus_pool)
218 static void ghes_estatus_pool_free_chunk_page(struct gen_pool *pool,
219 struct gen_pool_chunk *chunk,
222 free_page(chunk->start_addr);
225 static void ghes_estatus_pool_exit(void)
227 gen_pool_for_each_chunk(ghes_estatus_pool,
228 ghes_estatus_pool_free_chunk_page, NULL);
229 gen_pool_destroy(ghes_estatus_pool);
232 static int ghes_estatus_pool_expand(unsigned long len)
234 unsigned long i, pages, size, addr;
237 ghes_estatus_pool_size_request += PAGE_ALIGN(len);
238 size = gen_pool_size(ghes_estatus_pool);
239 if (size >= ghes_estatus_pool_size_request)
241 pages = (ghes_estatus_pool_size_request - size) / PAGE_SIZE;
242 for (i = 0; i < pages; i++) {
243 addr = __get_free_page(GFP_KERNEL);
246 ret = gen_pool_add(ghes_estatus_pool, addr, PAGE_SIZE, -1);
254 static int map_gen_v2(struct ghes *ghes)
256 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
259 static void unmap_gen_v2(struct ghes *ghes)
261 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
264 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
267 unsigned int error_block_length;
270 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
272 return ERR_PTR(-ENOMEM);
274 ghes->generic = generic;
275 if (is_hest_type_generic_v2(ghes)) {
276 rc = map_gen_v2(ghes);
281 rc = apei_map_generic_address(&generic->error_status_address);
283 goto err_unmap_read_ack_addr;
284 error_block_length = generic->error_block_length;
285 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
286 pr_warning(FW_WARN GHES_PFX
287 "Error status block length is too long: %u for "
288 "generic hardware error source: %d.\n",
289 error_block_length, generic->header.source_id);
290 error_block_length = GHES_ESTATUS_MAX_SIZE;
292 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
293 if (!ghes->estatus) {
295 goto err_unmap_status_addr;
300 err_unmap_status_addr:
301 apei_unmap_generic_address(&generic->error_status_address);
302 err_unmap_read_ack_addr:
303 if (is_hest_type_generic_v2(ghes))
310 static void ghes_fini(struct ghes *ghes)
312 kfree(ghes->estatus);
313 apei_unmap_generic_address(&ghes->generic->error_status_address);
314 if (is_hest_type_generic_v2(ghes))
318 static inline int ghes_severity(int severity)
321 case CPER_SEV_INFORMATIONAL:
323 case CPER_SEV_CORRECTED:
324 return GHES_SEV_CORRECTED;
325 case CPER_SEV_RECOVERABLE:
326 return GHES_SEV_RECOVERABLE;
328 return GHES_SEV_PANIC;
330 /* Unknown, go panic */
331 return GHES_SEV_PANIC;
335 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
339 unsigned long flags = 0;
340 int in_nmi = in_nmi();
345 offset = paddr - (paddr & PAGE_MASK);
347 raw_spin_lock(&ghes_ioremap_lock_nmi);
348 vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
350 spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
351 vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
353 trunk = PAGE_SIZE - offset;
354 trunk = min(trunk, len);
356 memcpy_fromio(buffer, vaddr + offset, trunk);
358 memcpy_toio(vaddr + offset, buffer, trunk);
363 ghes_iounmap_nmi(vaddr);
364 raw_spin_unlock(&ghes_ioremap_lock_nmi);
366 ghes_iounmap_irq(vaddr);
367 spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
372 static int ghes_read_estatus(struct ghes *ghes, int silent)
374 struct acpi_hest_generic *g = ghes->generic;
379 rc = apei_read(&buf_paddr, &g->error_status_address);
381 if (!silent && printk_ratelimit())
382 pr_warning(FW_WARN GHES_PFX
383 "Failed to read error status block address for hardware error source: %d.\n",
384 g->header.source_id);
390 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
391 sizeof(*ghes->estatus), 1);
392 if (!ghes->estatus->block_status)
395 ghes->buffer_paddr = buf_paddr;
396 ghes->flags |= GHES_TO_CLEAR;
399 len = cper_estatus_len(ghes->estatus);
400 if (len < sizeof(*ghes->estatus))
402 if (len > ghes->generic->error_block_length)
404 if (cper_estatus_check_header(ghes->estatus))
406 ghes_copy_tofrom_phys(ghes->estatus + 1,
407 buf_paddr + sizeof(*ghes->estatus),
408 len - sizeof(*ghes->estatus), 1);
409 if (cper_estatus_check(ghes->estatus))
414 if (rc && !silent && printk_ratelimit())
415 pr_warning(FW_WARN GHES_PFX
416 "Failed to read error status block!\n");
420 static void ghes_clear_estatus(struct ghes *ghes)
422 ghes->estatus->block_status = 0;
423 if (!(ghes->flags & GHES_TO_CLEAR))
425 ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
426 sizeof(ghes->estatus->block_status), 0);
427 ghes->flags &= ~GHES_TO_CLEAR;
430 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
432 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
435 int sec_sev = ghes_severity(gdata->error_severity);
436 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
438 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
441 pfn = mem_err->physical_addr >> PAGE_SHIFT;
442 if (!pfn_valid(pfn)) {
443 pr_warn_ratelimited(FW_WARN GHES_PFX
444 "Invalid address in generic error data: %#llx\n",
445 mem_err->physical_addr);
449 /* iff following two events can be handled properly by now */
450 if (sec_sev == GHES_SEV_CORRECTED &&
451 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
452 flags = MF_SOFT_OFFLINE;
453 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
457 memory_failure_queue(pfn, 0, flags);
461 static void ghes_do_proc(struct ghes *ghes,
462 const struct acpi_hest_generic_status *estatus)
465 struct acpi_hest_generic_data *gdata;
467 guid_t *fru_id = &NULL_UUID_LE;
470 sev = ghes_severity(estatus->error_severity);
471 apei_estatus_for_each_section(estatus, gdata) {
472 sec_type = (guid_t *)gdata->section_type;
473 sec_sev = ghes_severity(gdata->error_severity);
474 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
475 fru_id = (guid_t *)gdata->fru_id;
477 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
478 fru_text = gdata->fru_text;
480 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
481 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
483 ghes_edac_report_mem_error(ghes, sev, mem_err);
485 arch_apei_report_mem_error(sev, mem_err);
486 ghes_handle_memory_failure(gdata, sev);
488 #ifdef CONFIG_ACPI_APEI_PCIEAER
489 else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
490 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
492 if (sev == GHES_SEV_RECOVERABLE &&
493 sec_sev == GHES_SEV_RECOVERABLE &&
494 pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
495 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
499 devfn = PCI_DEVFN(pcie_err->device_id.device,
500 pcie_err->device_id.function);
501 aer_severity = cper_severity_to_aer(gdata->error_severity);
504 * If firmware reset the component to contain
505 * the error, we must reinitialize it before
506 * use, so treat it as a fatal AER error.
508 if (gdata->flags & CPER_SEC_RESET)
509 aer_severity = AER_FATAL;
511 aer_recover_queue(pcie_err->device_id.segment,
512 pcie_err->device_id.bus,
514 (struct aer_capability_regs *)
520 else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
521 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
523 log_arm_hw_error(err);
525 void *err = acpi_hest_get_payload(gdata);
527 log_non_standard_event(sec_type, fru_id, fru_text,
529 gdata->error_data_length);
534 static void __ghes_print_estatus(const char *pfx,
535 const struct acpi_hest_generic *generic,
536 const struct acpi_hest_generic_status *estatus)
538 static atomic_t seqno;
539 unsigned int curr_seqno;
543 if (ghes_severity(estatus->error_severity) <=
549 curr_seqno = atomic_inc_return(&seqno);
550 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
551 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
552 pfx_seq, generic->header.source_id);
553 cper_estatus_print(pfx_seq, estatus);
556 static int ghes_print_estatus(const char *pfx,
557 const struct acpi_hest_generic *generic,
558 const struct acpi_hest_generic_status *estatus)
560 /* Not more than 2 messages every 5 seconds */
561 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
562 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
563 struct ratelimit_state *ratelimit;
565 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
566 ratelimit = &ratelimit_corrected;
568 ratelimit = &ratelimit_uncorrected;
569 if (__ratelimit(ratelimit)) {
570 __ghes_print_estatus(pfx, generic, estatus);
577 * GHES error status reporting throttle, to report more kinds of
578 * errors, instead of just most frequently occurred errors.
580 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
584 unsigned long long now;
585 struct ghes_estatus_cache *cache;
586 struct acpi_hest_generic_status *cache_estatus;
588 len = cper_estatus_len(estatus);
590 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
591 cache = rcu_dereference(ghes_estatus_caches[i]);
594 if (len != cache->estatus_len)
596 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
597 if (memcmp(estatus, cache_estatus, len))
599 atomic_inc(&cache->count);
601 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
609 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
610 struct acpi_hest_generic *generic,
611 struct acpi_hest_generic_status *estatus)
615 struct ghes_estatus_cache *cache;
616 struct acpi_hest_generic_status *cache_estatus;
618 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
619 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
620 atomic_dec(&ghes_estatus_cache_alloced);
623 len = cper_estatus_len(estatus);
624 cache_len = GHES_ESTATUS_CACHE_LEN(len);
625 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
627 atomic_dec(&ghes_estatus_cache_alloced);
630 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
631 memcpy(cache_estatus, estatus, len);
632 cache->estatus_len = len;
633 atomic_set(&cache->count, 0);
634 cache->generic = generic;
635 cache->time_in = sched_clock();
639 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
643 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
644 len = GHES_ESTATUS_CACHE_LEN(len);
645 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
646 atomic_dec(&ghes_estatus_cache_alloced);
649 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
651 struct ghes_estatus_cache *cache;
653 cache = container_of(head, struct ghes_estatus_cache, rcu);
654 ghes_estatus_cache_free(cache);
657 static void ghes_estatus_cache_add(
658 struct acpi_hest_generic *generic,
659 struct acpi_hest_generic_status *estatus)
661 int i, slot = -1, count;
662 unsigned long long now, duration, period, max_period = 0;
663 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
665 new_cache = ghes_estatus_cache_alloc(generic, estatus);
666 if (new_cache == NULL)
670 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
671 cache = rcu_dereference(ghes_estatus_caches[i]);
677 duration = now - cache->time_in;
678 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
683 count = atomic_read(&cache->count);
685 do_div(period, (count + 1));
686 if (period > max_period) {
692 /* new_cache must be put into array after its contents are written */
694 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
695 slot_cache, new_cache) == slot_cache) {
697 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
699 ghes_estatus_cache_free(new_cache);
703 static int ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
708 rc = apei_read(&val, &gv2->read_ack_register);
712 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
713 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
715 return apei_write(val, &gv2->read_ack_register);
718 static void __ghes_panic(struct ghes *ghes)
720 __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
722 /* reboot to log the error! */
724 panic_timeout = ghes_panic_timeout;
725 panic("Fatal hardware error!");
728 static int ghes_proc(struct ghes *ghes)
732 rc = ghes_read_estatus(ghes, 0);
736 if (ghes_severity(ghes->estatus->error_severity) >= GHES_SEV_PANIC) {
740 if (!ghes_estatus_cached(ghes->estatus)) {
741 if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
742 ghes_estatus_cache_add(ghes->generic, ghes->estatus);
744 ghes_do_proc(ghes, ghes->estatus);
747 ghes_clear_estatus(ghes);
753 * GHESv2 type HEST entries introduce support for error acknowledgment,
754 * so only acknowledge the error if this support is present.
756 if (is_hest_type_generic_v2(ghes))
757 return ghes_ack_error(ghes->generic_v2);
762 static void ghes_add_timer(struct ghes *ghes)
764 struct acpi_hest_generic *g = ghes->generic;
765 unsigned long expire;
767 if (!g->notify.poll_interval) {
768 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
769 g->header.source_id);
772 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
773 ghes->timer.expires = round_jiffies_relative(expire);
774 add_timer(&ghes->timer);
777 static void ghes_poll_func(unsigned long data)
779 struct ghes *ghes = (void *)data;
782 if (!(ghes->flags & GHES_EXITING))
783 ghes_add_timer(ghes);
786 static irqreturn_t ghes_irq_func(int irq, void *data)
788 struct ghes *ghes = data;
791 rc = ghes_proc(ghes);
798 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
802 int ret = NOTIFY_DONE;
805 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
806 if (!ghes_proc(ghes))
814 static struct notifier_block ghes_notifier_hed = {
815 .notifier_call = ghes_notify_hed,
818 #ifdef CONFIG_ACPI_APEI_SEA
819 static LIST_HEAD(ghes_sea);
822 * Return 0 only if one of the SEA error sources successfully reported an error
823 * record sent from the firmware.
825 int ghes_notify_sea(void)
831 list_for_each_entry_rcu(ghes, &ghes_sea, list) {
832 if (!ghes_proc(ghes))
839 static void ghes_sea_add(struct ghes *ghes)
841 mutex_lock(&ghes_list_mutex);
842 list_add_rcu(&ghes->list, &ghes_sea);
843 mutex_unlock(&ghes_list_mutex);
846 static void ghes_sea_remove(struct ghes *ghes)
848 mutex_lock(&ghes_list_mutex);
849 list_del_rcu(&ghes->list);
850 mutex_unlock(&ghes_list_mutex);
853 #else /* CONFIG_ACPI_APEI_SEA */
854 static inline void ghes_sea_add(struct ghes *ghes)
856 pr_err(GHES_PFX "ID: %d, trying to add SEA notification which is not supported\n",
857 ghes->generic->header.source_id);
860 static inline void ghes_sea_remove(struct ghes *ghes)
862 pr_err(GHES_PFX "ID: %d, trying to remove SEA notification which is not supported\n",
863 ghes->generic->header.source_id);
865 #endif /* CONFIG_ACPI_APEI_SEA */
867 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
869 * printk is not safe in NMI context. So in NMI handler, we allocate
870 * required memory from lock-less memory allocator
871 * (ghes_estatus_pool), save estatus into it, put them into lock-less
872 * list (ghes_estatus_llist), then delay printk into IRQ context via
873 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
874 * required pool size by all NMI error source.
876 static struct llist_head ghes_estatus_llist;
877 static struct irq_work ghes_proc_irq_work;
880 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
881 * having only one concurrent reader.
883 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
885 static LIST_HEAD(ghes_nmi);
887 static void ghes_proc_in_irq(struct irq_work *irq_work)
889 struct llist_node *llnode, *next;
890 struct ghes_estatus_node *estatus_node;
891 struct acpi_hest_generic *generic;
892 struct acpi_hest_generic_status *estatus;
895 llnode = llist_del_all(&ghes_estatus_llist);
897 * Because the time order of estatus in list is reversed,
898 * revert it back to proper order.
900 llnode = llist_reverse_order(llnode);
903 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
905 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
906 len = cper_estatus_len(estatus);
907 node_len = GHES_ESTATUS_NODE_LEN(len);
908 ghes_do_proc(estatus_node->ghes, estatus);
909 if (!ghes_estatus_cached(estatus)) {
910 generic = estatus_node->generic;
911 if (ghes_print_estatus(NULL, generic, estatus))
912 ghes_estatus_cache_add(generic, estatus);
914 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
920 static void ghes_print_queued_estatus(void)
922 struct llist_node *llnode;
923 struct ghes_estatus_node *estatus_node;
924 struct acpi_hest_generic *generic;
925 struct acpi_hest_generic_status *estatus;
928 llnode = llist_del_all(&ghes_estatus_llist);
930 * Because the time order of estatus in list is reversed,
931 * revert it back to proper order.
933 llnode = llist_reverse_order(llnode);
935 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
937 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
938 len = cper_estatus_len(estatus);
939 node_len = GHES_ESTATUS_NODE_LEN(len);
940 generic = estatus_node->generic;
941 ghes_print_estatus(NULL, generic, estatus);
942 llnode = llnode->next;
946 /* Save estatus for further processing in IRQ context */
947 static void __process_error(struct ghes *ghes)
949 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
951 struct ghes_estatus_node *estatus_node;
952 struct acpi_hest_generic_status *estatus;
954 if (ghes_estatus_cached(ghes->estatus))
957 len = cper_estatus_len(ghes->estatus);
958 node_len = GHES_ESTATUS_NODE_LEN(len);
960 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
964 estatus_node->ghes = ghes;
965 estatus_node->generic = ghes->generic;
966 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
967 memcpy(estatus, ghes->estatus, len);
968 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
972 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
975 int sev, ret = NMI_DONE;
977 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
980 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
981 if (ghes_read_estatus(ghes, 1)) {
982 ghes_clear_estatus(ghes);
988 sev = ghes_severity(ghes->estatus->error_severity);
989 if (sev >= GHES_SEV_PANIC) {
991 ghes_print_queued_estatus();
995 if (!(ghes->flags & GHES_TO_CLEAR))
998 __process_error(ghes);
999 ghes_clear_estatus(ghes);
1002 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
1003 if (ret == NMI_HANDLED)
1004 irq_work_queue(&ghes_proc_irq_work);
1006 atomic_dec(&ghes_in_nmi);
1010 static unsigned long ghes_esource_prealloc_size(
1011 const struct acpi_hest_generic *generic)
1013 unsigned long block_length, prealloc_records, prealloc_size;
1015 block_length = min_t(unsigned long, generic->error_block_length,
1016 GHES_ESTATUS_MAX_SIZE);
1017 prealloc_records = max_t(unsigned long,
1018 generic->records_to_preallocate, 1);
1019 prealloc_size = min_t(unsigned long, block_length * prealloc_records,
1020 GHES_ESOURCE_PREALLOC_MAX_SIZE);
1022 return prealloc_size;
1025 static void ghes_estatus_pool_shrink(unsigned long len)
1027 ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
1030 static void ghes_nmi_add(struct ghes *ghes)
1034 len = ghes_esource_prealloc_size(ghes->generic);
1035 ghes_estatus_pool_expand(len);
1036 mutex_lock(&ghes_list_mutex);
1037 if (list_empty(&ghes_nmi))
1038 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1039 list_add_rcu(&ghes->list, &ghes_nmi);
1040 mutex_unlock(&ghes_list_mutex);
1043 static void ghes_nmi_remove(struct ghes *ghes)
1047 mutex_lock(&ghes_list_mutex);
1048 list_del_rcu(&ghes->list);
1049 if (list_empty(&ghes_nmi))
1050 unregister_nmi_handler(NMI_LOCAL, "ghes");
1051 mutex_unlock(&ghes_list_mutex);
1053 * To synchronize with NMI handler, ghes can only be
1054 * freed after NMI handler finishes.
1057 len = ghes_esource_prealloc_size(ghes->generic);
1058 ghes_estatus_pool_shrink(len);
1061 static void ghes_nmi_init_cxt(void)
1063 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1065 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1066 static inline void ghes_nmi_add(struct ghes *ghes)
1068 pr_err(GHES_PFX "ID: %d, trying to add NMI notification which is not supported!\n",
1069 ghes->generic->header.source_id);
1073 static inline void ghes_nmi_remove(struct ghes *ghes)
1075 pr_err(GHES_PFX "ID: %d, trying to remove NMI notification which is not supported!\n",
1076 ghes->generic->header.source_id);
1080 static inline void ghes_nmi_init_cxt(void)
1083 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1085 static int ghes_probe(struct platform_device *ghes_dev)
1087 struct acpi_hest_generic *generic;
1088 struct ghes *ghes = NULL;
1092 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1093 if (!generic->enabled)
1096 switch (generic->notify.type) {
1097 case ACPI_HEST_NOTIFY_POLLED:
1098 case ACPI_HEST_NOTIFY_EXTERNAL:
1099 case ACPI_HEST_NOTIFY_SCI:
1100 case ACPI_HEST_NOTIFY_GSIV:
1101 case ACPI_HEST_NOTIFY_GPIO:
1104 case ACPI_HEST_NOTIFY_SEA:
1105 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1106 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1107 generic->header.source_id);
1112 case ACPI_HEST_NOTIFY_NMI:
1113 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1114 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1115 generic->header.source_id);
1119 case ACPI_HEST_NOTIFY_LOCAL:
1120 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1121 generic->header.source_id);
1124 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1125 generic->notify.type, generic->header.source_id);
1130 if (generic->error_block_length <
1131 sizeof(struct acpi_hest_generic_status)) {
1132 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1133 generic->error_block_length,
1134 generic->header.source_id);
1137 ghes = ghes_new(generic);
1144 rc = ghes_edac_register(ghes, &ghes_dev->dev);
1148 switch (generic->notify.type) {
1149 case ACPI_HEST_NOTIFY_POLLED:
1150 setup_deferrable_timer(&ghes->timer, ghes_poll_func,
1151 (unsigned long)ghes);
1152 ghes_add_timer(ghes);
1154 case ACPI_HEST_NOTIFY_EXTERNAL:
1155 /* External interrupt vector is GSI */
1156 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1158 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1159 generic->header.source_id);
1160 goto err_edac_unreg;
1162 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1165 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1166 generic->header.source_id);
1167 goto err_edac_unreg;
1171 case ACPI_HEST_NOTIFY_SCI:
1172 case ACPI_HEST_NOTIFY_GSIV:
1173 case ACPI_HEST_NOTIFY_GPIO:
1174 mutex_lock(&ghes_list_mutex);
1175 if (list_empty(&ghes_hed))
1176 register_acpi_hed_notifier(&ghes_notifier_hed);
1177 list_add_rcu(&ghes->list, &ghes_hed);
1178 mutex_unlock(&ghes_list_mutex);
1181 case ACPI_HEST_NOTIFY_SEA:
1184 case ACPI_HEST_NOTIFY_NMI:
1190 platform_set_drvdata(ghes_dev, ghes);
1192 /* Handle any pending errors right away */
1197 ghes_edac_unregister(ghes);
1206 static int ghes_remove(struct platform_device *ghes_dev)
1209 struct acpi_hest_generic *generic;
1211 ghes = platform_get_drvdata(ghes_dev);
1212 generic = ghes->generic;
1214 ghes->flags |= GHES_EXITING;
1215 switch (generic->notify.type) {
1216 case ACPI_HEST_NOTIFY_POLLED:
1217 del_timer_sync(&ghes->timer);
1219 case ACPI_HEST_NOTIFY_EXTERNAL:
1220 free_irq(ghes->irq, ghes);
1223 case ACPI_HEST_NOTIFY_SCI:
1224 case ACPI_HEST_NOTIFY_GSIV:
1225 case ACPI_HEST_NOTIFY_GPIO:
1226 mutex_lock(&ghes_list_mutex);
1227 list_del_rcu(&ghes->list);
1228 if (list_empty(&ghes_hed))
1229 unregister_acpi_hed_notifier(&ghes_notifier_hed);
1230 mutex_unlock(&ghes_list_mutex);
1234 case ACPI_HEST_NOTIFY_SEA:
1235 ghes_sea_remove(ghes);
1237 case ACPI_HEST_NOTIFY_NMI:
1238 ghes_nmi_remove(ghes);
1247 ghes_edac_unregister(ghes);
1251 platform_set_drvdata(ghes_dev, NULL);
1256 static struct platform_driver ghes_platform_driver = {
1260 .probe = ghes_probe,
1261 .remove = ghes_remove,
1264 static int __init ghes_init(void)
1271 switch (hest_disable) {
1272 case HEST_NOT_FOUND:
1275 pr_info(GHES_PFX "HEST is not enabled!\n");
1282 pr_info(GHES_PFX "GHES is not enabled!\n");
1286 ghes_nmi_init_cxt();
1288 rc = ghes_ioremap_init();
1292 rc = ghes_estatus_pool_init();
1294 goto err_ioremap_exit;
1296 rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
1297 GHES_ESTATUS_CACHE_ALLOCED_MAX);
1301 rc = platform_driver_register(&ghes_platform_driver);
1305 rc = apei_osc_setup();
1306 if (rc == 0 && osc_sb_apei_support_acked)
1307 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1308 else if (rc == 0 && !osc_sb_apei_support_acked)
1309 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1310 else if (rc && osc_sb_apei_support_acked)
1311 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1313 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1317 ghes_estatus_pool_exit();
1319 ghes_ioremap_exit();
1323 device_initcall(ghes_init);