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/module.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>
48 #include <acpi/ghes.h>
49 #include <acpi/apei.h>
50 #include <asm/tlbflush.h>
52 #include "apei-internal.h"
54 #define GHES_PFX "GHES: "
56 #define GHES_ESTATUS_MAX_SIZE 65536
57 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
59 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
61 /* This is just an estimation for memory pool allocation */
62 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
64 #define GHES_ESTATUS_CACHES_SIZE 4
66 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
67 /* Prevent too many caches are allocated because of RCU */
68 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
70 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
71 (sizeof(struct ghes_estatus_cache) + (estatus_len))
72 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
73 ((struct acpi_hest_generic_status *) \
74 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
76 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
77 (sizeof(struct ghes_estatus_node) + (estatus_len))
78 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
79 ((struct acpi_hest_generic_status *) \
80 ((struct ghes_estatus_node *)(estatus_node) + 1))
83 module_param_named(disable, ghes_disable, bool, 0);
86 * All error sources notified with SCI shares one notifier function,
87 * so they need to be linked and checked one by one. This is applied
90 * RCU is used for these lists, so ghes_list_mutex is only used for
91 * list changing, not for traversing.
93 static LIST_HEAD(ghes_sci);
94 static DEFINE_MUTEX(ghes_list_mutex);
97 * Because the memory area used to transfer hardware error information
98 * from BIOS to Linux can be determined only in NMI, IRQ or timer
99 * handler, but general ioremap can not be used in atomic context, so
100 * a special version of atomic ioremap is implemented for that.
104 * Two virtual pages are used, one for IRQ/PROCESS context, the other for
105 * NMI context (optionally).
107 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
108 #define GHES_IOREMAP_PAGES 2
110 #define GHES_IOREMAP_PAGES 1
112 #define GHES_IOREMAP_IRQ_PAGE(base) (base)
113 #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
115 /* virtual memory area for atomic ioremap */
116 static struct vm_struct *ghes_ioremap_area;
118 * These 2 spinlock is used to prevent atomic ioremap virtual memory
119 * area from being mapped simultaneously.
121 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
122 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
124 static struct gen_pool *ghes_estatus_pool;
125 static unsigned long ghes_estatus_pool_size_request;
127 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
128 static atomic_t ghes_estatus_cache_alloced;
130 static int ghes_ioremap_init(void)
132 ghes_ioremap_area = __get_vm_area(PAGE_SIZE * GHES_IOREMAP_PAGES,
133 VM_IOREMAP, VMALLOC_START, VMALLOC_END);
134 if (!ghes_ioremap_area) {
135 pr_err(GHES_PFX "Failed to allocate virtual memory area for atomic ioremap.\n");
142 static void ghes_ioremap_exit(void)
144 free_vm_area(ghes_ioremap_area);
147 static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
151 vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
152 ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
153 pfn << PAGE_SHIFT, PAGE_KERNEL);
155 return (void __iomem *)vaddr;
158 static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
160 unsigned long vaddr, paddr;
163 vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
165 paddr = pfn << PAGE_SHIFT;
166 prot = arch_apei_get_mem_attribute(paddr);
168 ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
170 return (void __iomem *)vaddr;
173 static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
175 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
176 void *base = ghes_ioremap_area->addr;
178 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
179 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
180 arch_apei_flush_tlb_one(vaddr);
183 static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
185 unsigned long vaddr = (unsigned long __force)vaddr_ptr;
186 void *base = ghes_ioremap_area->addr;
188 BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
189 unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
190 arch_apei_flush_tlb_one(vaddr);
193 static int ghes_estatus_pool_init(void)
195 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
196 if (!ghes_estatus_pool)
201 static void ghes_estatus_pool_free_chunk_page(struct gen_pool *pool,
202 struct gen_pool_chunk *chunk,
205 free_page(chunk->start_addr);
208 static void ghes_estatus_pool_exit(void)
210 gen_pool_for_each_chunk(ghes_estatus_pool,
211 ghes_estatus_pool_free_chunk_page, NULL);
212 gen_pool_destroy(ghes_estatus_pool);
215 static int ghes_estatus_pool_expand(unsigned long len)
217 unsigned long i, pages, size, addr;
220 ghes_estatus_pool_size_request += PAGE_ALIGN(len);
221 size = gen_pool_size(ghes_estatus_pool);
222 if (size >= ghes_estatus_pool_size_request)
224 pages = (ghes_estatus_pool_size_request - size) / PAGE_SIZE;
225 for (i = 0; i < pages; i++) {
226 addr = __get_free_page(GFP_KERNEL);
229 ret = gen_pool_add(ghes_estatus_pool, addr, PAGE_SIZE, -1);
237 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
240 unsigned int error_block_length;
243 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
245 return ERR_PTR(-ENOMEM);
246 ghes->generic = generic;
247 rc = apei_map_generic_address(&generic->error_status_address);
250 error_block_length = generic->error_block_length;
251 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
252 pr_warning(FW_WARN GHES_PFX
253 "Error status block length is too long: %u for "
254 "generic hardware error source: %d.\n",
255 error_block_length, generic->header.source_id);
256 error_block_length = GHES_ESTATUS_MAX_SIZE;
258 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
259 if (!ghes->estatus) {
267 apei_unmap_generic_address(&generic->error_status_address);
273 static void ghes_fini(struct ghes *ghes)
275 kfree(ghes->estatus);
276 apei_unmap_generic_address(&ghes->generic->error_status_address);
279 static inline int ghes_severity(int severity)
282 case CPER_SEV_INFORMATIONAL:
284 case CPER_SEV_CORRECTED:
285 return GHES_SEV_CORRECTED;
286 case CPER_SEV_RECOVERABLE:
287 return GHES_SEV_RECOVERABLE;
289 return GHES_SEV_PANIC;
291 /* Unknown, go panic */
292 return GHES_SEV_PANIC;
296 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
300 unsigned long flags = 0;
301 int in_nmi = in_nmi();
306 offset = paddr - (paddr & PAGE_MASK);
308 raw_spin_lock(&ghes_ioremap_lock_nmi);
309 vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
311 spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
312 vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
314 trunk = PAGE_SIZE - offset;
315 trunk = min(trunk, len);
317 memcpy_fromio(buffer, vaddr + offset, trunk);
319 memcpy_toio(vaddr + offset, buffer, trunk);
324 ghes_iounmap_nmi(vaddr);
325 raw_spin_unlock(&ghes_ioremap_lock_nmi);
327 ghes_iounmap_irq(vaddr);
328 spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
333 static int ghes_read_estatus(struct ghes *ghes, int silent)
335 struct acpi_hest_generic *g = ghes->generic;
340 rc = apei_read(&buf_paddr, &g->error_status_address);
342 if (!silent && printk_ratelimit())
343 pr_warning(FW_WARN GHES_PFX
344 "Failed to read error status block address for hardware error source: %d.\n",
345 g->header.source_id);
351 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
352 sizeof(*ghes->estatus), 1);
353 if (!ghes->estatus->block_status)
356 ghes->buffer_paddr = buf_paddr;
357 ghes->flags |= GHES_TO_CLEAR;
360 len = cper_estatus_len(ghes->estatus);
361 if (len < sizeof(*ghes->estatus))
363 if (len > ghes->generic->error_block_length)
365 if (cper_estatus_check_header(ghes->estatus))
367 ghes_copy_tofrom_phys(ghes->estatus + 1,
368 buf_paddr + sizeof(*ghes->estatus),
369 len - sizeof(*ghes->estatus), 1);
370 if (cper_estatus_check(ghes->estatus))
375 if (rc && !silent && printk_ratelimit())
376 pr_warning(FW_WARN GHES_PFX
377 "Failed to read error status block!\n");
381 static void ghes_clear_estatus(struct ghes *ghes)
383 ghes->estatus->block_status = 0;
384 if (!(ghes->flags & GHES_TO_CLEAR))
386 ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
387 sizeof(ghes->estatus->block_status), 0);
388 ghes->flags &= ~GHES_TO_CLEAR;
391 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
393 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
396 int sec_sev = ghes_severity(gdata->error_severity);
397 struct cper_sec_mem_err *mem_err;
398 mem_err = (struct cper_sec_mem_err *)(gdata + 1);
400 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
403 pfn = mem_err->physical_addr >> PAGE_SHIFT;
404 if (!pfn_valid(pfn)) {
405 pr_warn_ratelimited(FW_WARN GHES_PFX
406 "Invalid address in generic error data: %#llx\n",
407 mem_err->physical_addr);
411 /* iff following two events can be handled properly by now */
412 if (sec_sev == GHES_SEV_CORRECTED &&
413 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
414 flags = MF_SOFT_OFFLINE;
415 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
419 memory_failure_queue(pfn, 0, flags);
423 static void ghes_do_proc(struct ghes *ghes,
424 const struct acpi_hest_generic_status *estatus)
427 struct acpi_hest_generic_data *gdata;
429 sev = ghes_severity(estatus->error_severity);
430 apei_estatus_for_each_section(estatus, gdata) {
431 sec_sev = ghes_severity(gdata->error_severity);
432 if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
433 CPER_SEC_PLATFORM_MEM)) {
434 struct cper_sec_mem_err *mem_err;
435 mem_err = (struct cper_sec_mem_err *)(gdata+1);
436 ghes_edac_report_mem_error(ghes, sev, mem_err);
438 arch_apei_report_mem_error(sev, mem_err);
439 ghes_handle_memory_failure(gdata, sev);
441 #ifdef CONFIG_ACPI_APEI_PCIEAER
442 else if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
444 struct cper_sec_pcie *pcie_err;
445 pcie_err = (struct cper_sec_pcie *)(gdata+1);
446 if (sev == GHES_SEV_RECOVERABLE &&
447 sec_sev == GHES_SEV_RECOVERABLE &&
448 pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
449 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
453 devfn = PCI_DEVFN(pcie_err->device_id.device,
454 pcie_err->device_id.function);
455 aer_severity = cper_severity_to_aer(sev);
458 * If firmware reset the component to contain
459 * the error, we must reinitialize it before
460 * use, so treat it as a fatal AER error.
462 if (gdata->flags & CPER_SEC_RESET)
463 aer_severity = AER_FATAL;
465 aer_recover_queue(pcie_err->device_id.segment,
466 pcie_err->device_id.bus,
468 (struct aer_capability_regs *)
477 static void __ghes_print_estatus(const char *pfx,
478 const struct acpi_hest_generic *generic,
479 const struct acpi_hest_generic_status *estatus)
481 static atomic_t seqno;
482 unsigned int curr_seqno;
486 if (ghes_severity(estatus->error_severity) <=
492 curr_seqno = atomic_inc_return(&seqno);
493 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
494 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
495 pfx_seq, generic->header.source_id);
496 cper_estatus_print(pfx_seq, estatus);
499 static int ghes_print_estatus(const char *pfx,
500 const struct acpi_hest_generic *generic,
501 const struct acpi_hest_generic_status *estatus)
503 /* Not more than 2 messages every 5 seconds */
504 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
505 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
506 struct ratelimit_state *ratelimit;
508 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
509 ratelimit = &ratelimit_corrected;
511 ratelimit = &ratelimit_uncorrected;
512 if (__ratelimit(ratelimit)) {
513 __ghes_print_estatus(pfx, generic, estatus);
520 * GHES error status reporting throttle, to report more kinds of
521 * errors, instead of just most frequently occurred errors.
523 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
527 unsigned long long now;
528 struct ghes_estatus_cache *cache;
529 struct acpi_hest_generic_status *cache_estatus;
531 len = cper_estatus_len(estatus);
533 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
534 cache = rcu_dereference(ghes_estatus_caches[i]);
537 if (len != cache->estatus_len)
539 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
540 if (memcmp(estatus, cache_estatus, len))
542 atomic_inc(&cache->count);
544 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
552 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
553 struct acpi_hest_generic *generic,
554 struct acpi_hest_generic_status *estatus)
558 struct ghes_estatus_cache *cache;
559 struct acpi_hest_generic_status *cache_estatus;
561 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
562 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
563 atomic_dec(&ghes_estatus_cache_alloced);
566 len = cper_estatus_len(estatus);
567 cache_len = GHES_ESTATUS_CACHE_LEN(len);
568 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
570 atomic_dec(&ghes_estatus_cache_alloced);
573 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
574 memcpy(cache_estatus, estatus, len);
575 cache->estatus_len = len;
576 atomic_set(&cache->count, 0);
577 cache->generic = generic;
578 cache->time_in = sched_clock();
582 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
586 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
587 len = GHES_ESTATUS_CACHE_LEN(len);
588 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
589 atomic_dec(&ghes_estatus_cache_alloced);
592 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
594 struct ghes_estatus_cache *cache;
596 cache = container_of(head, struct ghes_estatus_cache, rcu);
597 ghes_estatus_cache_free(cache);
600 static void ghes_estatus_cache_add(
601 struct acpi_hest_generic *generic,
602 struct acpi_hest_generic_status *estatus)
604 int i, slot = -1, count;
605 unsigned long long now, duration, period, max_period = 0;
606 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
608 new_cache = ghes_estatus_cache_alloc(generic, estatus);
609 if (new_cache == NULL)
613 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
614 cache = rcu_dereference(ghes_estatus_caches[i]);
620 duration = now - cache->time_in;
621 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
626 count = atomic_read(&cache->count);
628 do_div(period, (count + 1));
629 if (period > max_period) {
635 /* new_cache must be put into array after its contents are written */
637 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
638 slot_cache, new_cache) == slot_cache) {
640 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
642 ghes_estatus_cache_free(new_cache);
646 static int ghes_proc(struct ghes *ghes)
650 rc = ghes_read_estatus(ghes, 0);
653 if (!ghes_estatus_cached(ghes->estatus)) {
654 if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
655 ghes_estatus_cache_add(ghes->generic, ghes->estatus);
657 ghes_do_proc(ghes, ghes->estatus);
659 ghes_clear_estatus(ghes);
663 static void ghes_add_timer(struct ghes *ghes)
665 struct acpi_hest_generic *g = ghes->generic;
666 unsigned long expire;
668 if (!g->notify.poll_interval) {
669 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
670 g->header.source_id);
673 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
674 ghes->timer.expires = round_jiffies_relative(expire);
675 add_timer(&ghes->timer);
678 static void ghes_poll_func(unsigned long data)
680 struct ghes *ghes = (void *)data;
683 if (!(ghes->flags & GHES_EXITING))
684 ghes_add_timer(ghes);
687 static irqreturn_t ghes_irq_func(int irq, void *data)
689 struct ghes *ghes = data;
692 rc = ghes_proc(ghes);
699 static int ghes_notify_sci(struct notifier_block *this,
700 unsigned long event, void *data)
703 int ret = NOTIFY_DONE;
706 list_for_each_entry_rcu(ghes, &ghes_sci, list) {
707 if (!ghes_proc(ghes))
715 static struct notifier_block ghes_notifier_sci = {
716 .notifier_call = ghes_notify_sci,
719 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
721 * printk is not safe in NMI context. So in NMI handler, we allocate
722 * required memory from lock-less memory allocator
723 * (ghes_estatus_pool), save estatus into it, put them into lock-less
724 * list (ghes_estatus_llist), then delay printk into IRQ context via
725 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
726 * required pool size by all NMI error source.
728 static struct llist_head ghes_estatus_llist;
729 static struct irq_work ghes_proc_irq_work;
732 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
733 * having only one concurrent reader.
735 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
737 static LIST_HEAD(ghes_nmi);
739 static int ghes_panic_timeout __read_mostly = 30;
741 static void ghes_proc_in_irq(struct irq_work *irq_work)
743 struct llist_node *llnode, *next;
744 struct ghes_estatus_node *estatus_node;
745 struct acpi_hest_generic *generic;
746 struct acpi_hest_generic_status *estatus;
749 llnode = llist_del_all(&ghes_estatus_llist);
751 * Because the time order of estatus in list is reversed,
752 * revert it back to proper order.
754 llnode = llist_reverse_order(llnode);
757 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
759 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
760 len = cper_estatus_len(estatus);
761 node_len = GHES_ESTATUS_NODE_LEN(len);
762 ghes_do_proc(estatus_node->ghes, estatus);
763 if (!ghes_estatus_cached(estatus)) {
764 generic = estatus_node->generic;
765 if (ghes_print_estatus(NULL, generic, estatus))
766 ghes_estatus_cache_add(generic, estatus);
768 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
774 static void ghes_print_queued_estatus(void)
776 struct llist_node *llnode;
777 struct ghes_estatus_node *estatus_node;
778 struct acpi_hest_generic *generic;
779 struct acpi_hest_generic_status *estatus;
782 llnode = llist_del_all(&ghes_estatus_llist);
784 * Because the time order of estatus in list is reversed,
785 * revert it back to proper order.
787 llnode = llist_reverse_order(llnode);
789 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
791 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
792 len = cper_estatus_len(estatus);
793 node_len = GHES_ESTATUS_NODE_LEN(len);
794 generic = estatus_node->generic;
795 ghes_print_estatus(NULL, generic, estatus);
796 llnode = llnode->next;
800 /* Save estatus for further processing in IRQ context */
801 static void __process_error(struct ghes *ghes)
803 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
805 struct ghes_estatus_node *estatus_node;
806 struct acpi_hest_generic_status *estatus;
808 if (ghes_estatus_cached(ghes->estatus))
811 len = cper_estatus_len(ghes->estatus);
812 node_len = GHES_ESTATUS_NODE_LEN(len);
814 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
818 estatus_node->ghes = ghes;
819 estatus_node->generic = ghes->generic;
820 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
821 memcpy(estatus, ghes->estatus, len);
822 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
826 static void __ghes_panic(struct ghes *ghes)
829 ghes_print_queued_estatus();
830 __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
832 /* reboot to log the error! */
833 if (panic_timeout == 0)
834 panic_timeout = ghes_panic_timeout;
835 panic("Fatal hardware error!");
838 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
841 int sev, ret = NMI_DONE;
843 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
846 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
847 if (ghes_read_estatus(ghes, 1)) {
848 ghes_clear_estatus(ghes);
852 sev = ghes_severity(ghes->estatus->error_severity);
853 if (sev >= GHES_SEV_PANIC)
856 if (!(ghes->flags & GHES_TO_CLEAR))
859 __process_error(ghes);
860 ghes_clear_estatus(ghes);
865 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
866 irq_work_queue(&ghes_proc_irq_work);
868 atomic_dec(&ghes_in_nmi);
872 static unsigned long ghes_esource_prealloc_size(
873 const struct acpi_hest_generic *generic)
875 unsigned long block_length, prealloc_records, prealloc_size;
877 block_length = min_t(unsigned long, generic->error_block_length,
878 GHES_ESTATUS_MAX_SIZE);
879 prealloc_records = max_t(unsigned long,
880 generic->records_to_preallocate, 1);
881 prealloc_size = min_t(unsigned long, block_length * prealloc_records,
882 GHES_ESOURCE_PREALLOC_MAX_SIZE);
884 return prealloc_size;
887 static void ghes_estatus_pool_shrink(unsigned long len)
889 ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
892 static void ghes_nmi_add(struct ghes *ghes)
896 len = ghes_esource_prealloc_size(ghes->generic);
897 ghes_estatus_pool_expand(len);
898 mutex_lock(&ghes_list_mutex);
899 if (list_empty(&ghes_nmi))
900 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
901 list_add_rcu(&ghes->list, &ghes_nmi);
902 mutex_unlock(&ghes_list_mutex);
905 static void ghes_nmi_remove(struct ghes *ghes)
909 mutex_lock(&ghes_list_mutex);
910 list_del_rcu(&ghes->list);
911 if (list_empty(&ghes_nmi))
912 unregister_nmi_handler(NMI_LOCAL, "ghes");
913 mutex_unlock(&ghes_list_mutex);
915 * To synchronize with NMI handler, ghes can only be
916 * freed after NMI handler finishes.
919 len = ghes_esource_prealloc_size(ghes->generic);
920 ghes_estatus_pool_shrink(len);
923 static void ghes_nmi_init_cxt(void)
925 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
927 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
928 static inline void ghes_nmi_add(struct ghes *ghes)
930 pr_err(GHES_PFX "ID: %d, trying to add NMI notification which is not supported!\n",
931 ghes->generic->header.source_id);
935 static inline void ghes_nmi_remove(struct ghes *ghes)
937 pr_err(GHES_PFX "ID: %d, trying to remove NMI notification which is not supported!\n",
938 ghes->generic->header.source_id);
942 static inline void ghes_nmi_init_cxt(void)
945 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
947 static int ghes_probe(struct platform_device *ghes_dev)
949 struct acpi_hest_generic *generic;
950 struct ghes *ghes = NULL;
954 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
955 if (!generic->enabled)
958 switch (generic->notify.type) {
959 case ACPI_HEST_NOTIFY_POLLED:
960 case ACPI_HEST_NOTIFY_EXTERNAL:
961 case ACPI_HEST_NOTIFY_SCI:
963 case ACPI_HEST_NOTIFY_NMI:
964 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
965 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
966 generic->header.source_id);
970 case ACPI_HEST_NOTIFY_LOCAL:
971 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
972 generic->header.source_id);
975 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
976 generic->notify.type, generic->header.source_id);
981 if (generic->error_block_length <
982 sizeof(struct acpi_hest_generic_status)) {
983 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
984 generic->error_block_length,
985 generic->header.source_id);
988 ghes = ghes_new(generic);
995 rc = ghes_edac_register(ghes, &ghes_dev->dev);
999 switch (generic->notify.type) {
1000 case ACPI_HEST_NOTIFY_POLLED:
1001 ghes->timer.function = ghes_poll_func;
1002 ghes->timer.data = (unsigned long)ghes;
1003 init_timer_deferrable(&ghes->timer);
1004 ghes_add_timer(ghes);
1006 case ACPI_HEST_NOTIFY_EXTERNAL:
1007 /* External interrupt vector is GSI */
1008 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1010 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1011 generic->header.source_id);
1012 goto err_edac_unreg;
1014 rc = request_irq(ghes->irq, ghes_irq_func, 0, "GHES IRQ", ghes);
1016 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1017 generic->header.source_id);
1018 goto err_edac_unreg;
1021 case ACPI_HEST_NOTIFY_SCI:
1022 mutex_lock(&ghes_list_mutex);
1023 if (list_empty(&ghes_sci))
1024 register_acpi_hed_notifier(&ghes_notifier_sci);
1025 list_add_rcu(&ghes->list, &ghes_sci);
1026 mutex_unlock(&ghes_list_mutex);
1028 case ACPI_HEST_NOTIFY_NMI:
1034 platform_set_drvdata(ghes_dev, ghes);
1038 ghes_edac_unregister(ghes);
1047 static int ghes_remove(struct platform_device *ghes_dev)
1050 struct acpi_hest_generic *generic;
1052 ghes = platform_get_drvdata(ghes_dev);
1053 generic = ghes->generic;
1055 ghes->flags |= GHES_EXITING;
1056 switch (generic->notify.type) {
1057 case ACPI_HEST_NOTIFY_POLLED:
1058 del_timer_sync(&ghes->timer);
1060 case ACPI_HEST_NOTIFY_EXTERNAL:
1061 free_irq(ghes->irq, ghes);
1063 case ACPI_HEST_NOTIFY_SCI:
1064 mutex_lock(&ghes_list_mutex);
1065 list_del_rcu(&ghes->list);
1066 if (list_empty(&ghes_sci))
1067 unregister_acpi_hed_notifier(&ghes_notifier_sci);
1068 mutex_unlock(&ghes_list_mutex);
1070 case ACPI_HEST_NOTIFY_NMI:
1071 ghes_nmi_remove(ghes);
1080 ghes_edac_unregister(ghes);
1084 platform_set_drvdata(ghes_dev, NULL);
1089 static struct platform_driver ghes_platform_driver = {
1093 .probe = ghes_probe,
1094 .remove = ghes_remove,
1097 static int __init ghes_init(void)
1105 pr_info(GHES_PFX "HEST is not enabled!\n");
1110 pr_info(GHES_PFX "GHES is not enabled!\n");
1114 ghes_nmi_init_cxt();
1116 rc = ghes_ioremap_init();
1120 rc = ghes_estatus_pool_init();
1122 goto err_ioremap_exit;
1124 rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
1125 GHES_ESTATUS_CACHE_ALLOCED_MAX);
1129 rc = platform_driver_register(&ghes_platform_driver);
1133 rc = apei_osc_setup();
1134 if (rc == 0 && osc_sb_apei_support_acked)
1135 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1136 else if (rc == 0 && !osc_sb_apei_support_acked)
1137 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1138 else if (rc && osc_sb_apei_support_acked)
1139 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1141 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1145 ghes_estatus_pool_exit();
1147 ghes_ioremap_exit();
1152 static void __exit ghes_exit(void)
1154 platform_driver_unregister(&ghes_platform_driver);
1155 ghes_estatus_pool_exit();
1156 ghes_ioremap_exit();
1159 module_init(ghes_init);
1160 module_exit(ghes_exit);
1162 MODULE_AUTHOR("Huang Ying");
1163 MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
1164 MODULE_LICENSE("GPL");
1165 MODULE_ALIAS("platform:GHES");