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/platform_device.h>
37 #include <linux/mutex.h>
38 #include <linux/ratelimit.h>
39 #include <linux/vmalloc.h>
40 #include <linux/irq_work.h>
41 #include <linux/llist.h>
42 #include <linux/genalloc.h>
43 #include <linux/pci.h>
44 #include <linux/aer.h>
45 #include <linux/nmi.h>
46 #include <linux/sched/clock.h>
47 #include <linux/uuid.h>
48 #include <linux/ras.h>
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 <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 * the fixmap is used instead.
117 * These 2 spinlocks are used to prevent the fixmap entries from being used
120 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
121 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
123 static struct gen_pool *ghes_estatus_pool;
124 static unsigned long ghes_estatus_pool_size_request;
126 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
127 static atomic_t ghes_estatus_cache_alloced;
129 static int ghes_panic_timeout __read_mostly = 30;
131 static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
136 paddr = pfn << PAGE_SHIFT;
137 prot = arch_apei_get_mem_attribute(paddr);
138 __set_fixmap(FIX_APEI_GHES_NMI, paddr, prot);
140 return (void __iomem *) fix_to_virt(FIX_APEI_GHES_NMI);
143 static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
148 paddr = pfn << PAGE_SHIFT;
149 prot = arch_apei_get_mem_attribute(paddr);
150 __set_fixmap(FIX_APEI_GHES_IRQ, paddr, prot);
152 return (void __iomem *) fix_to_virt(FIX_APEI_GHES_IRQ);
155 static void ghes_iounmap_nmi(void)
157 clear_fixmap(FIX_APEI_GHES_NMI);
160 static void ghes_iounmap_irq(void)
162 clear_fixmap(FIX_APEI_GHES_IRQ);
165 static int ghes_estatus_pool_init(void)
167 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
168 if (!ghes_estatus_pool)
173 static void ghes_estatus_pool_free_chunk(struct gen_pool *pool,
174 struct gen_pool_chunk *chunk,
177 vfree((void *)chunk->start_addr);
180 static void ghes_estatus_pool_exit(void)
182 gen_pool_for_each_chunk(ghes_estatus_pool,
183 ghes_estatus_pool_free_chunk, NULL);
184 gen_pool_destroy(ghes_estatus_pool);
187 static int ghes_estatus_pool_expand(unsigned long len)
189 unsigned long size, addr;
191 ghes_estatus_pool_size_request += PAGE_ALIGN(len);
192 size = gen_pool_size(ghes_estatus_pool);
193 if (size >= ghes_estatus_pool_size_request)
196 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
201 * New allocation must be visible in all pgd before it can be found by
202 * an NMI allocating from the pool.
204 vmalloc_sync_mappings();
206 return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
209 static int map_gen_v2(struct ghes *ghes)
211 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
214 static void unmap_gen_v2(struct ghes *ghes)
216 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
219 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
222 unsigned int error_block_length;
225 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
227 return ERR_PTR(-ENOMEM);
229 ghes->generic = generic;
230 if (is_hest_type_generic_v2(ghes)) {
231 rc = map_gen_v2(ghes);
236 rc = apei_map_generic_address(&generic->error_status_address);
238 goto err_unmap_read_ack_addr;
239 error_block_length = generic->error_block_length;
240 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
241 pr_warning(FW_WARN GHES_PFX
242 "Error status block length is too long: %u for "
243 "generic hardware error source: %d.\n",
244 error_block_length, generic->header.source_id);
245 error_block_length = GHES_ESTATUS_MAX_SIZE;
247 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
248 if (!ghes->estatus) {
250 goto err_unmap_status_addr;
255 err_unmap_status_addr:
256 apei_unmap_generic_address(&generic->error_status_address);
257 err_unmap_read_ack_addr:
258 if (is_hest_type_generic_v2(ghes))
265 static void ghes_fini(struct ghes *ghes)
267 kfree(ghes->estatus);
268 apei_unmap_generic_address(&ghes->generic->error_status_address);
269 if (is_hest_type_generic_v2(ghes))
273 static inline int ghes_severity(int severity)
276 case CPER_SEV_INFORMATIONAL:
278 case CPER_SEV_CORRECTED:
279 return GHES_SEV_CORRECTED;
280 case CPER_SEV_RECOVERABLE:
281 return GHES_SEV_RECOVERABLE;
283 return GHES_SEV_PANIC;
285 /* Unknown, go panic */
286 return GHES_SEV_PANIC;
290 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
294 unsigned long flags = 0;
295 int in_nmi = in_nmi();
300 offset = paddr - (paddr & PAGE_MASK);
302 raw_spin_lock(&ghes_ioremap_lock_nmi);
303 vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
305 spin_lock_irqsave(&ghes_ioremap_lock_irq, flags);
306 vaddr = ghes_ioremap_pfn_irq(paddr >> PAGE_SHIFT);
308 trunk = PAGE_SIZE - offset;
309 trunk = min(trunk, len);
311 memcpy_fromio(buffer, vaddr + offset, trunk);
313 memcpy_toio(vaddr + offset, buffer, trunk);
319 raw_spin_unlock(&ghes_ioremap_lock_nmi);
322 spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
327 static int ghes_read_estatus(struct ghes *ghes, int silent)
329 struct acpi_hest_generic *g = ghes->generic;
334 rc = apei_read(&buf_paddr, &g->error_status_address);
336 if (!silent && printk_ratelimit())
337 pr_warning(FW_WARN GHES_PFX
338 "Failed to read error status block address for hardware error source: %d.\n",
339 g->header.source_id);
345 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
346 sizeof(*ghes->estatus), 1);
347 if (!ghes->estatus->block_status)
350 ghes->buffer_paddr = buf_paddr;
351 ghes->flags |= GHES_TO_CLEAR;
354 len = cper_estatus_len(ghes->estatus);
355 if (len < sizeof(*ghes->estatus))
357 if (len > ghes->generic->error_block_length)
359 if (cper_estatus_check_header(ghes->estatus))
361 ghes_copy_tofrom_phys(ghes->estatus + 1,
362 buf_paddr + sizeof(*ghes->estatus),
363 len - sizeof(*ghes->estatus), 1);
364 if (cper_estatus_check(ghes->estatus))
369 if (rc && !silent && printk_ratelimit())
370 pr_warning(FW_WARN GHES_PFX
371 "Failed to read error status block!\n");
375 static void ghes_clear_estatus(struct ghes *ghes)
377 ghes->estatus->block_status = 0;
378 if (!(ghes->flags & GHES_TO_CLEAR))
380 ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
381 sizeof(ghes->estatus->block_status), 0);
382 ghes->flags &= ~GHES_TO_CLEAR;
385 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
387 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
390 int sec_sev = ghes_severity(gdata->error_severity);
391 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
393 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
396 pfn = mem_err->physical_addr >> PAGE_SHIFT;
397 if (!pfn_valid(pfn)) {
398 pr_warn_ratelimited(FW_WARN GHES_PFX
399 "Invalid address in generic error data: %#llx\n",
400 mem_err->physical_addr);
404 /* iff following two events can be handled properly by now */
405 if (sec_sev == GHES_SEV_CORRECTED &&
406 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
407 flags = MF_SOFT_OFFLINE;
408 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
412 memory_failure_queue(pfn, 0, flags);
416 static void ghes_do_proc(struct ghes *ghes,
417 const struct acpi_hest_generic_status *estatus)
420 struct acpi_hest_generic_data *gdata;
422 guid_t *fru_id = &NULL_UUID_LE;
425 sev = ghes_severity(estatus->error_severity);
426 apei_estatus_for_each_section(estatus, gdata) {
427 sec_type = (guid_t *)gdata->section_type;
428 sec_sev = ghes_severity(gdata->error_severity);
429 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
430 fru_id = (guid_t *)gdata->fru_id;
432 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
433 fru_text = gdata->fru_text;
435 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
436 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
438 ghes_edac_report_mem_error(ghes, sev, mem_err);
440 arch_apei_report_mem_error(sev, mem_err);
441 ghes_handle_memory_failure(gdata, sev);
443 #ifdef CONFIG_ACPI_APEI_PCIEAER
444 else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
445 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
447 if (sev == GHES_SEV_RECOVERABLE &&
448 sec_sev == GHES_SEV_RECOVERABLE &&
449 pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
450 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
454 devfn = PCI_DEVFN(pcie_err->device_id.device,
455 pcie_err->device_id.function);
456 aer_severity = cper_severity_to_aer(gdata->error_severity);
459 * If firmware reset the component to contain
460 * the error, we must reinitialize it before
461 * use, so treat it as a fatal AER error.
463 if (gdata->flags & CPER_SEC_RESET)
464 aer_severity = AER_FATAL;
466 aer_recover_queue(pcie_err->device_id.segment,
467 pcie_err->device_id.bus,
469 (struct aer_capability_regs *)
475 else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
476 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
478 log_arm_hw_error(err);
480 void *err = acpi_hest_get_payload(gdata);
482 log_non_standard_event(sec_type, fru_id, fru_text,
484 gdata->error_data_length);
489 static void __ghes_print_estatus(const char *pfx,
490 const struct acpi_hest_generic *generic,
491 const struct acpi_hest_generic_status *estatus)
493 static atomic_t seqno;
494 unsigned int curr_seqno;
498 if (ghes_severity(estatus->error_severity) <=
504 curr_seqno = atomic_inc_return(&seqno);
505 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
506 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
507 pfx_seq, generic->header.source_id);
508 cper_estatus_print(pfx_seq, estatus);
511 static int ghes_print_estatus(const char *pfx,
512 const struct acpi_hest_generic *generic,
513 const struct acpi_hest_generic_status *estatus)
515 /* Not more than 2 messages every 5 seconds */
516 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
517 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
518 struct ratelimit_state *ratelimit;
520 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
521 ratelimit = &ratelimit_corrected;
523 ratelimit = &ratelimit_uncorrected;
524 if (__ratelimit(ratelimit)) {
525 __ghes_print_estatus(pfx, generic, estatus);
532 * GHES error status reporting throttle, to report more kinds of
533 * errors, instead of just most frequently occurred errors.
535 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
539 unsigned long long now;
540 struct ghes_estatus_cache *cache;
541 struct acpi_hest_generic_status *cache_estatus;
543 len = cper_estatus_len(estatus);
545 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
546 cache = rcu_dereference(ghes_estatus_caches[i]);
549 if (len != cache->estatus_len)
551 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
552 if (memcmp(estatus, cache_estatus, len))
554 atomic_inc(&cache->count);
556 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
564 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
565 struct acpi_hest_generic *generic,
566 struct acpi_hest_generic_status *estatus)
570 struct ghes_estatus_cache *cache;
571 struct acpi_hest_generic_status *cache_estatus;
573 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
574 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
575 atomic_dec(&ghes_estatus_cache_alloced);
578 len = cper_estatus_len(estatus);
579 cache_len = GHES_ESTATUS_CACHE_LEN(len);
580 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
582 atomic_dec(&ghes_estatus_cache_alloced);
585 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
586 memcpy(cache_estatus, estatus, len);
587 cache->estatus_len = len;
588 atomic_set(&cache->count, 0);
589 cache->generic = generic;
590 cache->time_in = sched_clock();
594 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
598 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
599 len = GHES_ESTATUS_CACHE_LEN(len);
600 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
601 atomic_dec(&ghes_estatus_cache_alloced);
604 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
606 struct ghes_estatus_cache *cache;
608 cache = container_of(head, struct ghes_estatus_cache, rcu);
609 ghes_estatus_cache_free(cache);
612 static void ghes_estatus_cache_add(
613 struct acpi_hest_generic *generic,
614 struct acpi_hest_generic_status *estatus)
616 int i, slot = -1, count;
617 unsigned long long now, duration, period, max_period = 0;
618 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
620 new_cache = ghes_estatus_cache_alloc(generic, estatus);
621 if (new_cache == NULL)
625 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
626 cache = rcu_dereference(ghes_estatus_caches[i]);
632 duration = now - cache->time_in;
633 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
638 count = atomic_read(&cache->count);
640 do_div(period, (count + 1));
641 if (period > max_period) {
647 /* new_cache must be put into array after its contents are written */
649 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
650 slot_cache, new_cache) == slot_cache) {
652 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
654 ghes_estatus_cache_free(new_cache);
658 static int ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
663 rc = apei_read(&val, &gv2->read_ack_register);
667 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
668 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
670 return apei_write(val, &gv2->read_ack_register);
673 static void __ghes_panic(struct ghes *ghes)
675 __ghes_print_estatus(KERN_EMERG, ghes->generic, ghes->estatus);
677 ghes_clear_estatus(ghes);
679 /* reboot to log the error! */
681 panic_timeout = ghes_panic_timeout;
682 panic("Fatal hardware error!");
685 static int ghes_proc(struct ghes *ghes)
689 rc = ghes_read_estatus(ghes, 0);
693 if (ghes_severity(ghes->estatus->error_severity) >= GHES_SEV_PANIC) {
697 if (!ghes_estatus_cached(ghes->estatus)) {
698 if (ghes_print_estatus(NULL, ghes->generic, ghes->estatus))
699 ghes_estatus_cache_add(ghes->generic, ghes->estatus);
701 ghes_do_proc(ghes, ghes->estatus);
704 ghes_clear_estatus(ghes);
710 * GHESv2 type HEST entries introduce support for error acknowledgment,
711 * so only acknowledge the error if this support is present.
713 if (is_hest_type_generic_v2(ghes))
714 return ghes_ack_error(ghes->generic_v2);
719 static void ghes_add_timer(struct ghes *ghes)
721 struct acpi_hest_generic *g = ghes->generic;
722 unsigned long expire;
724 if (!g->notify.poll_interval) {
725 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
726 g->header.source_id);
729 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
730 ghes->timer.expires = round_jiffies_relative(expire);
731 add_timer(&ghes->timer);
734 static void ghes_poll_func(unsigned long data)
736 struct ghes *ghes = (void *)data;
739 if (!(ghes->flags & GHES_EXITING))
740 ghes_add_timer(ghes);
743 static irqreturn_t ghes_irq_func(int irq, void *data)
745 struct ghes *ghes = data;
748 rc = ghes_proc(ghes);
755 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
759 int ret = NOTIFY_DONE;
762 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
763 if (!ghes_proc(ghes))
771 static struct notifier_block ghes_notifier_hed = {
772 .notifier_call = ghes_notify_hed,
775 #ifdef CONFIG_ACPI_APEI_SEA
776 static LIST_HEAD(ghes_sea);
779 * Return 0 only if one of the SEA error sources successfully reported an error
780 * record sent from the firmware.
782 int ghes_notify_sea(void)
788 list_for_each_entry_rcu(ghes, &ghes_sea, list) {
789 if (!ghes_proc(ghes))
796 static void ghes_sea_add(struct ghes *ghes)
798 mutex_lock(&ghes_list_mutex);
799 list_add_rcu(&ghes->list, &ghes_sea);
800 mutex_unlock(&ghes_list_mutex);
803 static void ghes_sea_remove(struct ghes *ghes)
805 mutex_lock(&ghes_list_mutex);
806 list_del_rcu(&ghes->list);
807 mutex_unlock(&ghes_list_mutex);
810 #else /* CONFIG_ACPI_APEI_SEA */
811 static inline void ghes_sea_add(struct ghes *ghes) { }
812 static inline void ghes_sea_remove(struct ghes *ghes) { }
813 #endif /* CONFIG_ACPI_APEI_SEA */
815 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
817 * printk is not safe in NMI context. So in NMI handler, we allocate
818 * required memory from lock-less memory allocator
819 * (ghes_estatus_pool), save estatus into it, put them into lock-less
820 * list (ghes_estatus_llist), then delay printk into IRQ context via
821 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
822 * required pool size by all NMI error source.
824 static struct llist_head ghes_estatus_llist;
825 static struct irq_work ghes_proc_irq_work;
828 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
829 * having only one concurrent reader.
831 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
833 static LIST_HEAD(ghes_nmi);
835 static void ghes_proc_in_irq(struct irq_work *irq_work)
837 struct llist_node *llnode, *next;
838 struct ghes_estatus_node *estatus_node;
839 struct acpi_hest_generic *generic;
840 struct acpi_hest_generic_status *estatus;
843 llnode = llist_del_all(&ghes_estatus_llist);
845 * Because the time order of estatus in list is reversed,
846 * revert it back to proper order.
848 llnode = llist_reverse_order(llnode);
851 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
853 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
854 len = cper_estatus_len(estatus);
855 node_len = GHES_ESTATUS_NODE_LEN(len);
856 ghes_do_proc(estatus_node->ghes, estatus);
857 if (!ghes_estatus_cached(estatus)) {
858 generic = estatus_node->generic;
859 if (ghes_print_estatus(NULL, generic, estatus))
860 ghes_estatus_cache_add(generic, estatus);
862 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
868 static void ghes_print_queued_estatus(void)
870 struct llist_node *llnode;
871 struct ghes_estatus_node *estatus_node;
872 struct acpi_hest_generic *generic;
873 struct acpi_hest_generic_status *estatus;
876 llnode = llist_del_all(&ghes_estatus_llist);
878 * Because the time order of estatus in list is reversed,
879 * revert it back to proper order.
881 llnode = llist_reverse_order(llnode);
883 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
885 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
886 len = cper_estatus_len(estatus);
887 node_len = GHES_ESTATUS_NODE_LEN(len);
888 generic = estatus_node->generic;
889 ghes_print_estatus(NULL, generic, estatus);
890 llnode = llnode->next;
894 /* Save estatus for further processing in IRQ context */
895 static void __process_error(struct ghes *ghes)
897 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
899 struct ghes_estatus_node *estatus_node;
900 struct acpi_hest_generic_status *estatus;
902 if (ghes_estatus_cached(ghes->estatus))
905 len = cper_estatus_len(ghes->estatus);
906 node_len = GHES_ESTATUS_NODE_LEN(len);
908 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
912 estatus_node->ghes = ghes;
913 estatus_node->generic = ghes->generic;
914 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
915 memcpy(estatus, ghes->estatus, len);
916 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
920 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
923 int sev, ret = NMI_DONE;
925 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
928 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
929 if (ghes_read_estatus(ghes, 1)) {
930 ghes_clear_estatus(ghes);
936 sev = ghes_severity(ghes->estatus->error_severity);
937 if (sev >= GHES_SEV_PANIC) {
938 ghes_print_queued_estatus();
942 if (!(ghes->flags & GHES_TO_CLEAR))
945 __process_error(ghes);
946 ghes_clear_estatus(ghes);
949 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
950 if (ret == NMI_HANDLED)
951 irq_work_queue(&ghes_proc_irq_work);
953 atomic_dec(&ghes_in_nmi);
957 static unsigned long ghes_esource_prealloc_size(
958 const struct acpi_hest_generic *generic)
960 unsigned long block_length, prealloc_records, prealloc_size;
962 block_length = min_t(unsigned long, generic->error_block_length,
963 GHES_ESTATUS_MAX_SIZE);
964 prealloc_records = max_t(unsigned long,
965 generic->records_to_preallocate, 1);
966 prealloc_size = min_t(unsigned long, block_length * prealloc_records,
967 GHES_ESOURCE_PREALLOC_MAX_SIZE);
969 return prealloc_size;
972 static void ghes_estatus_pool_shrink(unsigned long len)
974 ghes_estatus_pool_size_request -= PAGE_ALIGN(len);
977 static void ghes_nmi_add(struct ghes *ghes)
981 len = ghes_esource_prealloc_size(ghes->generic);
982 ghes_estatus_pool_expand(len);
983 mutex_lock(&ghes_list_mutex);
984 if (list_empty(&ghes_nmi))
985 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
986 list_add_rcu(&ghes->list, &ghes_nmi);
987 mutex_unlock(&ghes_list_mutex);
990 static void ghes_nmi_remove(struct ghes *ghes)
994 mutex_lock(&ghes_list_mutex);
995 list_del_rcu(&ghes->list);
996 if (list_empty(&ghes_nmi))
997 unregister_nmi_handler(NMI_LOCAL, "ghes");
998 mutex_unlock(&ghes_list_mutex);
1000 * To synchronize with NMI handler, ghes can only be
1001 * freed after NMI handler finishes.
1004 len = ghes_esource_prealloc_size(ghes->generic);
1005 ghes_estatus_pool_shrink(len);
1008 static void ghes_nmi_init_cxt(void)
1010 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1012 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1013 static inline void ghes_nmi_add(struct ghes *ghes) { }
1014 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1015 static inline void ghes_nmi_init_cxt(void) { }
1016 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1018 static int ghes_probe(struct platform_device *ghes_dev)
1020 struct acpi_hest_generic *generic;
1021 struct ghes *ghes = NULL;
1025 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1026 if (!generic->enabled)
1029 switch (generic->notify.type) {
1030 case ACPI_HEST_NOTIFY_POLLED:
1031 case ACPI_HEST_NOTIFY_EXTERNAL:
1032 case ACPI_HEST_NOTIFY_SCI:
1033 case ACPI_HEST_NOTIFY_GSIV:
1034 case ACPI_HEST_NOTIFY_GPIO:
1037 case ACPI_HEST_NOTIFY_SEA:
1038 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1039 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1040 generic->header.source_id);
1045 case ACPI_HEST_NOTIFY_NMI:
1046 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1047 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1048 generic->header.source_id);
1052 case ACPI_HEST_NOTIFY_LOCAL:
1053 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1054 generic->header.source_id);
1057 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1058 generic->notify.type, generic->header.source_id);
1063 if (generic->error_block_length <
1064 sizeof(struct acpi_hest_generic_status)) {
1065 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1066 generic->error_block_length,
1067 generic->header.source_id);
1070 ghes = ghes_new(generic);
1077 rc = ghes_edac_register(ghes, &ghes_dev->dev);
1081 switch (generic->notify.type) {
1082 case ACPI_HEST_NOTIFY_POLLED:
1083 setup_deferrable_timer(&ghes->timer, ghes_poll_func,
1084 (unsigned long)ghes);
1085 ghes_add_timer(ghes);
1087 case ACPI_HEST_NOTIFY_EXTERNAL:
1088 /* External interrupt vector is GSI */
1089 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1091 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1092 generic->header.source_id);
1093 goto err_edac_unreg;
1095 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1098 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1099 generic->header.source_id);
1100 goto err_edac_unreg;
1104 case ACPI_HEST_NOTIFY_SCI:
1105 case ACPI_HEST_NOTIFY_GSIV:
1106 case ACPI_HEST_NOTIFY_GPIO:
1107 mutex_lock(&ghes_list_mutex);
1108 if (list_empty(&ghes_hed))
1109 register_acpi_hed_notifier(&ghes_notifier_hed);
1110 list_add_rcu(&ghes->list, &ghes_hed);
1111 mutex_unlock(&ghes_list_mutex);
1114 case ACPI_HEST_NOTIFY_SEA:
1117 case ACPI_HEST_NOTIFY_NMI:
1123 platform_set_drvdata(ghes_dev, ghes);
1125 /* Handle any pending errors right away */
1130 ghes_edac_unregister(ghes);
1139 static int ghes_remove(struct platform_device *ghes_dev)
1142 struct acpi_hest_generic *generic;
1144 ghes = platform_get_drvdata(ghes_dev);
1145 generic = ghes->generic;
1147 ghes->flags |= GHES_EXITING;
1148 switch (generic->notify.type) {
1149 case ACPI_HEST_NOTIFY_POLLED:
1150 del_timer_sync(&ghes->timer);
1152 case ACPI_HEST_NOTIFY_EXTERNAL:
1153 free_irq(ghes->irq, ghes);
1156 case ACPI_HEST_NOTIFY_SCI:
1157 case ACPI_HEST_NOTIFY_GSIV:
1158 case ACPI_HEST_NOTIFY_GPIO:
1159 mutex_lock(&ghes_list_mutex);
1160 list_del_rcu(&ghes->list);
1161 if (list_empty(&ghes_hed))
1162 unregister_acpi_hed_notifier(&ghes_notifier_hed);
1163 mutex_unlock(&ghes_list_mutex);
1167 case ACPI_HEST_NOTIFY_SEA:
1168 ghes_sea_remove(ghes);
1170 case ACPI_HEST_NOTIFY_NMI:
1171 ghes_nmi_remove(ghes);
1180 ghes_edac_unregister(ghes);
1184 platform_set_drvdata(ghes_dev, NULL);
1189 static struct platform_driver ghes_platform_driver = {
1193 .probe = ghes_probe,
1194 .remove = ghes_remove,
1197 static int __init ghes_init(void)
1204 switch (hest_disable) {
1205 case HEST_NOT_FOUND:
1208 pr_info(GHES_PFX "HEST is not enabled!\n");
1215 pr_info(GHES_PFX "GHES is not enabled!\n");
1219 ghes_nmi_init_cxt();
1221 rc = ghes_estatus_pool_init();
1225 rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
1226 GHES_ESTATUS_CACHE_ALLOCED_MAX);
1230 rc = platform_driver_register(&ghes_platform_driver);
1234 rc = apei_osc_setup();
1235 if (rc == 0 && osc_sb_apei_support_acked)
1236 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1237 else if (rc == 0 && !osc_sb_apei_support_acked)
1238 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1239 else if (rc && osc_sb_apei_support_acked)
1240 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1242 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1246 ghes_estatus_pool_exit();
1250 device_initcall(ghes_init);