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
;
158 ghes_estatus_pool
= gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER
, -1);
159 if (!ghes_estatus_pool
)
162 len
= GHES_ESTATUS_CACHE_AVG_SIZE
* GHES_ESTATUS_CACHE_ALLOCED_MAX
;
163 len
+= (num_ghes
* GHES_ESOURCE_PREALLOC_MAX_SIZE
);
165 ghes_estatus_pool_size_request
= PAGE_ALIGN(len
);
166 addr
= (unsigned long)vmalloc(PAGE_ALIGN(len
));
171 * New allocation must be visible in all pgd before it can be found by
172 * an NMI allocating from the pool.
176 rc
= gen_pool_add(ghes_estatus_pool
, addr
, PAGE_ALIGN(len
), -1);
186 gen_pool_destroy(ghes_estatus_pool
);
191 static int map_gen_v2(struct ghes
*ghes
)
193 return apei_map_generic_address(&ghes
->generic_v2
->read_ack_register
);
196 static void unmap_gen_v2(struct ghes
*ghes
)
198 apei_unmap_generic_address(&ghes
->generic_v2
->read_ack_register
);
201 static void ghes_ack_error(struct acpi_hest_generic_v2
*gv2
)
206 rc
= apei_read(&val
, &gv2
->read_ack_register
);
210 val
&= gv2
->read_ack_preserve
<< gv2
->read_ack_register
.bit_offset
;
211 val
|= gv2
->read_ack_write
<< gv2
->read_ack_register
.bit_offset
;
213 apei_write(val
, &gv2
->read_ack_register
);
216 static struct ghes
*ghes_new(struct acpi_hest_generic
*generic
)
219 unsigned int error_block_length
;
222 ghes
= kzalloc(sizeof(*ghes
), GFP_KERNEL
);
224 return ERR_PTR(-ENOMEM
);
226 ghes
->generic
= generic
;
227 if (is_hest_type_generic_v2(ghes
)) {
228 rc
= map_gen_v2(ghes
);
233 rc
= apei_map_generic_address(&generic
->error_status_address
);
235 goto err_unmap_read_ack_addr
;
236 error_block_length
= generic
->error_block_length
;
237 if (error_block_length
> GHES_ESTATUS_MAX_SIZE
) {
238 pr_warn(FW_WARN GHES_PFX
239 "Error status block length is too long: %u for "
240 "generic hardware error source: %d.\n",
241 error_block_length
, generic
->header
.source_id
);
242 error_block_length
= GHES_ESTATUS_MAX_SIZE
;
244 ghes
->estatus
= kmalloc(error_block_length
, GFP_KERNEL
);
245 if (!ghes
->estatus
) {
247 goto err_unmap_status_addr
;
252 err_unmap_status_addr
:
253 apei_unmap_generic_address(&generic
->error_status_address
);
254 err_unmap_read_ack_addr
:
255 if (is_hest_type_generic_v2(ghes
))
262 static void ghes_fini(struct ghes
*ghes
)
264 kfree(ghes
->estatus
);
265 apei_unmap_generic_address(&ghes
->generic
->error_status_address
);
266 if (is_hest_type_generic_v2(ghes
))
270 static inline int ghes_severity(int severity
)
273 case CPER_SEV_INFORMATIONAL
:
275 case CPER_SEV_CORRECTED
:
276 return GHES_SEV_CORRECTED
;
277 case CPER_SEV_RECOVERABLE
:
278 return GHES_SEV_RECOVERABLE
;
280 return GHES_SEV_PANIC
;
282 /* Unknown, go panic */
283 return GHES_SEV_PANIC
;
287 static void ghes_copy_tofrom_phys(void *buffer
, u64 paddr
, u32 len
,
289 enum fixed_addresses fixmap_idx
)
296 offset
= paddr
- (paddr
& PAGE_MASK
);
297 vaddr
= ghes_map(PHYS_PFN(paddr
), fixmap_idx
);
298 trunk
= PAGE_SIZE
- offset
;
299 trunk
= min(trunk
, len
);
301 memcpy_fromio(buffer
, vaddr
+ offset
, trunk
);
303 memcpy_toio(vaddr
+ offset
, buffer
, trunk
);
307 ghes_unmap(vaddr
, fixmap_idx
);
311 /* Check the top-level record header has an appropriate size. */
312 static int __ghes_check_estatus(struct ghes
*ghes
,
313 struct acpi_hest_generic_status
*estatus
)
315 u32 len
= cper_estatus_len(estatus
);
317 if (len
< sizeof(*estatus
)) {
318 pr_warn_ratelimited(FW_WARN GHES_PFX
"Truncated error status block!\n");
322 if (len
> ghes
->generic
->error_block_length
) {
323 pr_warn_ratelimited(FW_WARN GHES_PFX
"Invalid error status block length!\n");
327 if (cper_estatus_check_header(estatus
)) {
328 pr_warn_ratelimited(FW_WARN GHES_PFX
"Invalid CPER header!\n");
335 /* Read the CPER block, returning its address, and header in estatus. */
336 static int __ghes_peek_estatus(struct ghes
*ghes
,
337 struct acpi_hest_generic_status
*estatus
,
338 u64
*buf_paddr
, enum fixed_addresses fixmap_idx
)
340 struct acpi_hest_generic
*g
= ghes
->generic
;
343 rc
= apei_read(buf_paddr
, &g
->error_status_address
);
346 pr_warn_ratelimited(FW_WARN GHES_PFX
347 "Failed to read error status block address for hardware error source: %d.\n",
348 g
->header
.source_id
);
354 ghes_copy_tofrom_phys(estatus
, *buf_paddr
, sizeof(*estatus
), 1,
356 if (!estatus
->block_status
) {
364 static int __ghes_read_estatus(struct acpi_hest_generic_status
*estatus
,
365 u64 buf_paddr
, enum fixed_addresses fixmap_idx
,
368 ghes_copy_tofrom_phys(estatus
, buf_paddr
, buf_len
, 1, fixmap_idx
);
369 if (cper_estatus_check(estatus
)) {
370 pr_warn_ratelimited(FW_WARN GHES_PFX
371 "Failed to read error status block!\n");
378 static int ghes_read_estatus(struct ghes
*ghes
,
379 struct acpi_hest_generic_status
*estatus
,
380 u64
*buf_paddr
, enum fixed_addresses fixmap_idx
)
384 rc
= __ghes_peek_estatus(ghes
, estatus
, buf_paddr
, fixmap_idx
);
388 rc
= __ghes_check_estatus(ghes
, estatus
);
392 return __ghes_read_estatus(estatus
, *buf_paddr
, fixmap_idx
,
393 cper_estatus_len(estatus
));
396 static void ghes_clear_estatus(struct ghes
*ghes
,
397 struct acpi_hest_generic_status
*estatus
,
398 u64 buf_paddr
, enum fixed_addresses fixmap_idx
)
400 estatus
->block_status
= 0;
405 ghes_copy_tofrom_phys(estatus
, buf_paddr
,
406 sizeof(estatus
->block_status
), 0,
410 * GHESv2 type HEST entries introduce support for error acknowledgment,
411 * so only acknowledge the error if this support is present.
413 if (is_hest_type_generic_v2(ghes
))
414 ghes_ack_error(ghes
->generic_v2
);
417 static void ghes_handle_memory_failure(struct acpi_hest_generic_data
*gdata
, int sev
)
419 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
422 int sec_sev
= ghes_severity(gdata
->error_severity
);
423 struct cper_sec_mem_err
*mem_err
= acpi_hest_get_payload(gdata
);
425 if (!(mem_err
->validation_bits
& CPER_MEM_VALID_PA
))
428 pfn
= mem_err
->physical_addr
>> PAGE_SHIFT
;
429 if (!pfn_valid(pfn
)) {
430 pr_warn_ratelimited(FW_WARN GHES_PFX
431 "Invalid address in generic error data: %#llx\n",
432 mem_err
->physical_addr
);
436 /* iff following two events can be handled properly by now */
437 if (sec_sev
== GHES_SEV_CORRECTED
&&
438 (gdata
->flags
& CPER_SEC_ERROR_THRESHOLD_EXCEEDED
))
439 flags
= MF_SOFT_OFFLINE
;
440 if (sev
== GHES_SEV_RECOVERABLE
&& sec_sev
== GHES_SEV_RECOVERABLE
)
444 memory_failure_queue(pfn
, flags
);
449 * PCIe AER errors need to be sent to the AER driver for reporting and
450 * recovery. The GHES severities map to the following AER severities and
451 * require the following handling:
453 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
454 * These need to be reported by the AER driver but no recovery is
456 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
457 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
458 * These both need to be reported and recovered from by the AER driver.
459 * GHES_SEV_PANIC does not make it to this handling since the kernel must
462 static void ghes_handle_aer(struct acpi_hest_generic_data
*gdata
)
464 #ifdef CONFIG_ACPI_APEI_PCIEAER
465 struct cper_sec_pcie
*pcie_err
= acpi_hest_get_payload(gdata
);
467 if (pcie_err
->validation_bits
& CPER_PCIE_VALID_DEVICE_ID
&&
468 pcie_err
->validation_bits
& CPER_PCIE_VALID_AER_INFO
) {
472 devfn
= PCI_DEVFN(pcie_err
->device_id
.device
,
473 pcie_err
->device_id
.function
);
474 aer_severity
= cper_severity_to_aer(gdata
->error_severity
);
477 * If firmware reset the component to contain
478 * the error, we must reinitialize it before
479 * use, so treat it as a fatal AER error.
481 if (gdata
->flags
& CPER_SEC_RESET
)
482 aer_severity
= AER_FATAL
;
484 aer_recover_queue(pcie_err
->device_id
.segment
,
485 pcie_err
->device_id
.bus
,
487 (struct aer_capability_regs
*)
493 static void ghes_do_proc(struct ghes
*ghes
,
494 const struct acpi_hest_generic_status
*estatus
)
497 struct acpi_hest_generic_data
*gdata
;
499 const guid_t
*fru_id
= &guid_null
;
502 sev
= ghes_severity(estatus
->error_severity
);
503 apei_estatus_for_each_section(estatus
, gdata
) {
504 sec_type
= (guid_t
*)gdata
->section_type
;
505 sec_sev
= ghes_severity(gdata
->error_severity
);
506 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_ID
)
507 fru_id
= (guid_t
*)gdata
->fru_id
;
509 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_TEXT
)
510 fru_text
= gdata
->fru_text
;
512 if (guid_equal(sec_type
, &CPER_SEC_PLATFORM_MEM
)) {
513 struct cper_sec_mem_err
*mem_err
= acpi_hest_get_payload(gdata
);
515 ghes_edac_report_mem_error(sev
, mem_err
);
517 arch_apei_report_mem_error(sev
, mem_err
);
518 ghes_handle_memory_failure(gdata
, sev
);
520 else if (guid_equal(sec_type
, &CPER_SEC_PCIE
)) {
521 ghes_handle_aer(gdata
);
523 else if (guid_equal(sec_type
, &CPER_SEC_PROC_ARM
)) {
524 struct cper_sec_proc_arm
*err
= acpi_hest_get_payload(gdata
);
526 log_arm_hw_error(err
);
528 void *err
= acpi_hest_get_payload(gdata
);
530 log_non_standard_event(sec_type
, fru_id
, fru_text
,
532 gdata
->error_data_length
);
537 static void __ghes_print_estatus(const char *pfx
,
538 const struct acpi_hest_generic
*generic
,
539 const struct acpi_hest_generic_status
*estatus
)
541 static atomic_t seqno
;
542 unsigned int curr_seqno
;
546 if (ghes_severity(estatus
->error_severity
) <=
552 curr_seqno
= atomic_inc_return(&seqno
);
553 snprintf(pfx_seq
, sizeof(pfx_seq
), "%s{%u}" HW_ERR
, pfx
, curr_seqno
);
554 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
555 pfx_seq
, generic
->header
.source_id
);
556 cper_estatus_print(pfx_seq
, estatus
);
559 static int ghes_print_estatus(const char *pfx
,
560 const struct acpi_hest_generic
*generic
,
561 const struct acpi_hest_generic_status
*estatus
)
563 /* Not more than 2 messages every 5 seconds */
564 static DEFINE_RATELIMIT_STATE(ratelimit_corrected
, 5*HZ
, 2);
565 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected
, 5*HZ
, 2);
566 struct ratelimit_state
*ratelimit
;
568 if (ghes_severity(estatus
->error_severity
) <= GHES_SEV_CORRECTED
)
569 ratelimit
= &ratelimit_corrected
;
571 ratelimit
= &ratelimit_uncorrected
;
572 if (__ratelimit(ratelimit
)) {
573 __ghes_print_estatus(pfx
, generic
, estatus
);
580 * GHES error status reporting throttle, to report more kinds of
581 * errors, instead of just most frequently occurred errors.
583 static int ghes_estatus_cached(struct acpi_hest_generic_status
*estatus
)
587 unsigned long long now
;
588 struct ghes_estatus_cache
*cache
;
589 struct acpi_hest_generic_status
*cache_estatus
;
591 len
= cper_estatus_len(estatus
);
593 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
594 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
597 if (len
!= cache
->estatus_len
)
599 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
600 if (memcmp(estatus
, cache_estatus
, len
))
602 atomic_inc(&cache
->count
);
604 if (now
- cache
->time_in
< GHES_ESTATUS_IN_CACHE_MAX_NSEC
)
612 static struct ghes_estatus_cache
*ghes_estatus_cache_alloc(
613 struct acpi_hest_generic
*generic
,
614 struct acpi_hest_generic_status
*estatus
)
618 struct ghes_estatus_cache
*cache
;
619 struct acpi_hest_generic_status
*cache_estatus
;
621 alloced
= atomic_add_return(1, &ghes_estatus_cache_alloced
);
622 if (alloced
> GHES_ESTATUS_CACHE_ALLOCED_MAX
) {
623 atomic_dec(&ghes_estatus_cache_alloced
);
626 len
= cper_estatus_len(estatus
);
627 cache_len
= GHES_ESTATUS_CACHE_LEN(len
);
628 cache
= (void *)gen_pool_alloc(ghes_estatus_pool
, cache_len
);
630 atomic_dec(&ghes_estatus_cache_alloced
);
633 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
634 memcpy(cache_estatus
, estatus
, len
);
635 cache
->estatus_len
= len
;
636 atomic_set(&cache
->count
, 0);
637 cache
->generic
= generic
;
638 cache
->time_in
= sched_clock();
642 static void ghes_estatus_cache_free(struct ghes_estatus_cache
*cache
)
646 len
= cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache
));
647 len
= GHES_ESTATUS_CACHE_LEN(len
);
648 gen_pool_free(ghes_estatus_pool
, (unsigned long)cache
, len
);
649 atomic_dec(&ghes_estatus_cache_alloced
);
652 static void ghes_estatus_cache_rcu_free(struct rcu_head
*head
)
654 struct ghes_estatus_cache
*cache
;
656 cache
= container_of(head
, struct ghes_estatus_cache
, rcu
);
657 ghes_estatus_cache_free(cache
);
660 static void ghes_estatus_cache_add(
661 struct acpi_hest_generic
*generic
,
662 struct acpi_hest_generic_status
*estatus
)
664 int i
, slot
= -1, count
;
665 unsigned long long now
, duration
, period
, max_period
= 0;
666 struct ghes_estatus_cache
*cache
, *slot_cache
= NULL
, *new_cache
;
668 new_cache
= ghes_estatus_cache_alloc(generic
, estatus
);
669 if (new_cache
== NULL
)
673 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
674 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
680 duration
= now
- cache
->time_in
;
681 if (duration
>= GHES_ESTATUS_IN_CACHE_MAX_NSEC
) {
686 count
= atomic_read(&cache
->count
);
688 do_div(period
, (count
+ 1));
689 if (period
> max_period
) {
695 /* new_cache must be put into array after its contents are written */
697 if (slot
!= -1 && cmpxchg(ghes_estatus_caches
+ slot
,
698 slot_cache
, new_cache
) == slot_cache
) {
700 call_rcu(&slot_cache
->rcu
, ghes_estatus_cache_rcu_free
);
702 ghes_estatus_cache_free(new_cache
);
706 static void __ghes_panic(struct ghes
*ghes
,
707 struct acpi_hest_generic_status
*estatus
,
708 u64 buf_paddr
, enum fixed_addresses fixmap_idx
)
710 __ghes_print_estatus(KERN_EMERG
, ghes
->generic
, estatus
);
712 ghes_clear_estatus(ghes
, estatus
, buf_paddr
, fixmap_idx
);
714 /* reboot to log the error! */
716 panic_timeout
= ghes_panic_timeout
;
717 panic("Fatal hardware error!");
720 static int ghes_proc(struct ghes
*ghes
)
722 struct acpi_hest_generic_status
*estatus
= ghes
->estatus
;
726 rc
= ghes_read_estatus(ghes
, estatus
, &buf_paddr
, FIX_APEI_GHES_IRQ
);
730 if (ghes_severity(estatus
->error_severity
) >= GHES_SEV_PANIC
)
731 __ghes_panic(ghes
, estatus
, buf_paddr
, FIX_APEI_GHES_IRQ
);
733 if (!ghes_estatus_cached(estatus
)) {
734 if (ghes_print_estatus(NULL
, ghes
->generic
, estatus
))
735 ghes_estatus_cache_add(ghes
->generic
, estatus
);
737 ghes_do_proc(ghes
, estatus
);
740 ghes_clear_estatus(ghes
, estatus
, buf_paddr
, FIX_APEI_GHES_IRQ
);
745 static void ghes_add_timer(struct ghes
*ghes
)
747 struct acpi_hest_generic
*g
= ghes
->generic
;
748 unsigned long expire
;
750 if (!g
->notify
.poll_interval
) {
751 pr_warn(FW_WARN GHES_PFX
"Poll interval is 0 for generic hardware error source: %d, disabled.\n",
752 g
->header
.source_id
);
755 expire
= jiffies
+ msecs_to_jiffies(g
->notify
.poll_interval
);
756 ghes
->timer
.expires
= round_jiffies_relative(expire
);
757 add_timer(&ghes
->timer
);
760 static void ghes_poll_func(struct timer_list
*t
)
762 struct ghes
*ghes
= from_timer(ghes
, t
, timer
);
765 spin_lock_irqsave(&ghes_notify_lock_irq
, flags
);
767 spin_unlock_irqrestore(&ghes_notify_lock_irq
, flags
);
768 if (!(ghes
->flags
& GHES_EXITING
))
769 ghes_add_timer(ghes
);
772 static irqreturn_t
ghes_irq_func(int irq
, void *data
)
774 struct ghes
*ghes
= data
;
778 spin_lock_irqsave(&ghes_notify_lock_irq
, flags
);
779 rc
= ghes_proc(ghes
);
780 spin_unlock_irqrestore(&ghes_notify_lock_irq
, flags
);
787 static int ghes_notify_hed(struct notifier_block
*this, unsigned long event
,
792 int ret
= NOTIFY_DONE
;
794 spin_lock_irqsave(&ghes_notify_lock_irq
, flags
);
796 list_for_each_entry_rcu(ghes
, &ghes_hed
, list
) {
797 if (!ghes_proc(ghes
))
801 spin_unlock_irqrestore(&ghes_notify_lock_irq
, flags
);
806 static struct notifier_block ghes_notifier_hed
= {
807 .notifier_call
= ghes_notify_hed
,
811 * Handlers for CPER records may not be NMI safe. For example,
812 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
813 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
814 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
815 * ghes_proc_in_irq() to run in IRQ context where each estatus in
816 * ghes_estatus_llist is processed.
818 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
819 * to suppress frequent messages.
821 static struct llist_head ghes_estatus_llist
;
822 static struct irq_work ghes_proc_irq_work
;
824 static void ghes_proc_in_irq(struct irq_work
*irq_work
)
826 struct llist_node
*llnode
, *next
;
827 struct ghes_estatus_node
*estatus_node
;
828 struct acpi_hest_generic
*generic
;
829 struct acpi_hest_generic_status
*estatus
;
832 llnode
= llist_del_all(&ghes_estatus_llist
);
834 * Because the time order of estatus in list is reversed,
835 * revert it back to proper order.
837 llnode
= llist_reverse_order(llnode
);
840 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
842 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
843 len
= cper_estatus_len(estatus
);
844 node_len
= GHES_ESTATUS_NODE_LEN(len
);
845 ghes_do_proc(estatus_node
->ghes
, estatus
);
846 if (!ghes_estatus_cached(estatus
)) {
847 generic
= estatus_node
->generic
;
848 if (ghes_print_estatus(NULL
, generic
, estatus
))
849 ghes_estatus_cache_add(generic
, estatus
);
851 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
857 static void ghes_print_queued_estatus(void)
859 struct llist_node
*llnode
;
860 struct ghes_estatus_node
*estatus_node
;
861 struct acpi_hest_generic
*generic
;
862 struct acpi_hest_generic_status
*estatus
;
864 llnode
= llist_del_all(&ghes_estatus_llist
);
866 * Because the time order of estatus in list is reversed,
867 * revert it back to proper order.
869 llnode
= llist_reverse_order(llnode
);
871 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
873 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
874 generic
= estatus_node
->generic
;
875 ghes_print_estatus(NULL
, generic
, estatus
);
876 llnode
= llnode
->next
;
880 static int ghes_in_nmi_queue_one_entry(struct ghes
*ghes
,
881 enum fixed_addresses fixmap_idx
)
883 struct acpi_hest_generic_status
*estatus
, tmp_header
;
884 struct ghes_estatus_node
*estatus_node
;
889 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
))
892 rc
= __ghes_peek_estatus(ghes
, &tmp_header
, &buf_paddr
, fixmap_idx
);
894 ghes_clear_estatus(ghes
, &tmp_header
, buf_paddr
, fixmap_idx
);
898 rc
= __ghes_check_estatus(ghes
, &tmp_header
);
900 ghes_clear_estatus(ghes
, &tmp_header
, buf_paddr
, fixmap_idx
);
904 len
= cper_estatus_len(&tmp_header
);
905 node_len
= GHES_ESTATUS_NODE_LEN(len
);
906 estatus_node
= (void *)gen_pool_alloc(ghes_estatus_pool
, node_len
);
910 estatus_node
->ghes
= ghes
;
911 estatus_node
->generic
= ghes
->generic
;
912 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
914 if (__ghes_read_estatus(estatus
, buf_paddr
, fixmap_idx
, len
)) {
915 ghes_clear_estatus(ghes
, estatus
, buf_paddr
, fixmap_idx
);
920 sev
= ghes_severity(estatus
->error_severity
);
921 if (sev
>= GHES_SEV_PANIC
) {
922 ghes_print_queued_estatus();
923 __ghes_panic(ghes
, estatus
, buf_paddr
, fixmap_idx
);
926 ghes_clear_estatus(ghes
, &tmp_header
, buf_paddr
, fixmap_idx
);
928 /* This error has been reported before, don't process it again. */
929 if (ghes_estatus_cached(estatus
))
932 llist_add(&estatus_node
->llnode
, &ghes_estatus_llist
);
937 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
943 static int ghes_in_nmi_spool_from_list(struct list_head
*rcu_list
,
944 enum fixed_addresses fixmap_idx
)
950 list_for_each_entry_rcu(ghes
, rcu_list
, list
) {
951 if (!ghes_in_nmi_queue_one_entry(ghes
, fixmap_idx
))
956 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
) && !ret
)
957 irq_work_queue(&ghes_proc_irq_work
);
962 #ifdef CONFIG_ACPI_APEI_SEA
963 static LIST_HEAD(ghes_sea
);
966 * Return 0 only if one of the SEA error sources successfully reported an error
967 * record sent from the firmware.
969 int ghes_notify_sea(void)
971 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea
);
974 raw_spin_lock(&ghes_notify_lock_sea
);
975 rv
= ghes_in_nmi_spool_from_list(&ghes_sea
, FIX_APEI_GHES_SEA
);
976 raw_spin_unlock(&ghes_notify_lock_sea
);
981 static void ghes_sea_add(struct ghes
*ghes
)
983 mutex_lock(&ghes_list_mutex
);
984 list_add_rcu(&ghes
->list
, &ghes_sea
);
985 mutex_unlock(&ghes_list_mutex
);
988 static void ghes_sea_remove(struct ghes
*ghes
)
990 mutex_lock(&ghes_list_mutex
);
991 list_del_rcu(&ghes
->list
);
992 mutex_unlock(&ghes_list_mutex
);
995 #else /* CONFIG_ACPI_APEI_SEA */
996 static inline void ghes_sea_add(struct ghes
*ghes
) { }
997 static inline void ghes_sea_remove(struct ghes
*ghes
) { }
998 #endif /* CONFIG_ACPI_APEI_SEA */
1000 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
1002 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
1003 * having only one concurrent reader.
1005 static atomic_t ghes_in_nmi
= ATOMIC_INIT(0);
1007 static LIST_HEAD(ghes_nmi
);
1009 static int ghes_notify_nmi(unsigned int cmd
, struct pt_regs
*regs
)
1011 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi
);
1014 if (!atomic_add_unless(&ghes_in_nmi
, 1, 1))
1017 raw_spin_lock(&ghes_notify_lock_nmi
);
1018 if (!ghes_in_nmi_spool_from_list(&ghes_nmi
, FIX_APEI_GHES_NMI
))
1020 raw_spin_unlock(&ghes_notify_lock_nmi
);
1022 atomic_dec(&ghes_in_nmi
);
1026 static void ghes_nmi_add(struct ghes
*ghes
)
1028 mutex_lock(&ghes_list_mutex
);
1029 if (list_empty(&ghes_nmi
))
1030 register_nmi_handler(NMI_LOCAL
, ghes_notify_nmi
, 0, "ghes");
1031 list_add_rcu(&ghes
->list
, &ghes_nmi
);
1032 mutex_unlock(&ghes_list_mutex
);
1035 static void ghes_nmi_remove(struct ghes
*ghes
)
1037 mutex_lock(&ghes_list_mutex
);
1038 list_del_rcu(&ghes
->list
);
1039 if (list_empty(&ghes_nmi
))
1040 unregister_nmi_handler(NMI_LOCAL
, "ghes");
1041 mutex_unlock(&ghes_list_mutex
);
1043 * To synchronize with NMI handler, ghes can only be
1044 * freed after NMI handler finishes.
1048 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1049 static inline void ghes_nmi_add(struct ghes
*ghes
) { }
1050 static inline void ghes_nmi_remove(struct ghes
*ghes
) { }
1051 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1053 static void ghes_nmi_init_cxt(void)
1055 init_irq_work(&ghes_proc_irq_work
, ghes_proc_in_irq
);
1058 static int __ghes_sdei_callback(struct ghes
*ghes
,
1059 enum fixed_addresses fixmap_idx
)
1061 if (!ghes_in_nmi_queue_one_entry(ghes
, fixmap_idx
)) {
1062 irq_work_queue(&ghes_proc_irq_work
);
1070 static int ghes_sdei_normal_callback(u32 event_num
, struct pt_regs
*regs
,
1073 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal
);
1074 struct ghes
*ghes
= arg
;
1077 raw_spin_lock(&ghes_notify_lock_sdei_normal
);
1078 err
= __ghes_sdei_callback(ghes
, FIX_APEI_GHES_SDEI_NORMAL
);
1079 raw_spin_unlock(&ghes_notify_lock_sdei_normal
);
1084 static int ghes_sdei_critical_callback(u32 event_num
, struct pt_regs
*regs
,
1087 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical
);
1088 struct ghes
*ghes
= arg
;
1091 raw_spin_lock(&ghes_notify_lock_sdei_critical
);
1092 err
= __ghes_sdei_callback(ghes
, FIX_APEI_GHES_SDEI_CRITICAL
);
1093 raw_spin_unlock(&ghes_notify_lock_sdei_critical
);
1098 static int apei_sdei_register_ghes(struct ghes
*ghes
)
1100 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE
))
1103 return sdei_register_ghes(ghes
, ghes_sdei_normal_callback
,
1104 ghes_sdei_critical_callback
);
1107 static int apei_sdei_unregister_ghes(struct ghes
*ghes
)
1109 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE
))
1112 return sdei_unregister_ghes(ghes
);
1115 static int ghes_probe(struct platform_device
*ghes_dev
)
1117 struct acpi_hest_generic
*generic
;
1118 struct ghes
*ghes
= NULL
;
1119 unsigned long flags
;
1123 generic
= *(struct acpi_hest_generic
**)ghes_dev
->dev
.platform_data
;
1124 if (!generic
->enabled
)
1127 switch (generic
->notify
.type
) {
1128 case ACPI_HEST_NOTIFY_POLLED
:
1129 case ACPI_HEST_NOTIFY_EXTERNAL
:
1130 case ACPI_HEST_NOTIFY_SCI
:
1131 case ACPI_HEST_NOTIFY_GSIV
:
1132 case ACPI_HEST_NOTIFY_GPIO
:
1135 case ACPI_HEST_NOTIFY_SEA
:
1136 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA
)) {
1137 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via SEA is not supported\n",
1138 generic
->header
.source_id
);
1143 case ACPI_HEST_NOTIFY_NMI
:
1144 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI
)) {
1145 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1146 generic
->header
.source_id
);
1150 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED
:
1151 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE
)) {
1152 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1153 generic
->header
.source_id
);
1157 case ACPI_HEST_NOTIFY_LOCAL
:
1158 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via local interrupt is not supported!\n",
1159 generic
->header
.source_id
);
1162 pr_warn(FW_WARN GHES_PFX
"Unknown notification type: %u for generic hardware error source: %d\n",
1163 generic
->notify
.type
, generic
->header
.source_id
);
1168 if (generic
->error_block_length
<
1169 sizeof(struct acpi_hest_generic_status
)) {
1170 pr_warn(FW_BUG GHES_PFX
"Invalid error block length: %u for generic hardware error source: %d\n",
1171 generic
->error_block_length
, generic
->header
.source_id
);
1174 ghes
= ghes_new(generic
);
1181 switch (generic
->notify
.type
) {
1182 case ACPI_HEST_NOTIFY_POLLED
:
1183 timer_setup(&ghes
->timer
, ghes_poll_func
, 0);
1184 ghes_add_timer(ghes
);
1186 case ACPI_HEST_NOTIFY_EXTERNAL
:
1187 /* External interrupt vector is GSI */
1188 rc
= acpi_gsi_to_irq(generic
->notify
.vector
, &ghes
->irq
);
1190 pr_err(GHES_PFX
"Failed to map GSI to IRQ for generic hardware error source: %d\n",
1191 generic
->header
.source_id
);
1194 rc
= request_irq(ghes
->irq
, ghes_irq_func
, IRQF_SHARED
,
1197 pr_err(GHES_PFX
"Failed to register IRQ for generic hardware error source: %d\n",
1198 generic
->header
.source_id
);
1203 case ACPI_HEST_NOTIFY_SCI
:
1204 case ACPI_HEST_NOTIFY_GSIV
:
1205 case ACPI_HEST_NOTIFY_GPIO
:
1206 mutex_lock(&ghes_list_mutex
);
1207 if (list_empty(&ghes_hed
))
1208 register_acpi_hed_notifier(&ghes_notifier_hed
);
1209 list_add_rcu(&ghes
->list
, &ghes_hed
);
1210 mutex_unlock(&ghes_list_mutex
);
1213 case ACPI_HEST_NOTIFY_SEA
:
1216 case ACPI_HEST_NOTIFY_NMI
:
1219 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED
:
1220 rc
= apei_sdei_register_ghes(ghes
);
1228 platform_set_drvdata(ghes_dev
, ghes
);
1230 ghes_edac_register(ghes
, &ghes_dev
->dev
);
1232 /* Handle any pending errors right away */
1233 spin_lock_irqsave(&ghes_notify_lock_irq
, flags
);
1235 spin_unlock_irqrestore(&ghes_notify_lock_irq
, flags
);
1247 static int ghes_remove(struct platform_device
*ghes_dev
)
1251 struct acpi_hest_generic
*generic
;
1253 ghes
= platform_get_drvdata(ghes_dev
);
1254 generic
= ghes
->generic
;
1256 ghes
->flags
|= GHES_EXITING
;
1257 switch (generic
->notify
.type
) {
1258 case ACPI_HEST_NOTIFY_POLLED
:
1259 del_timer_sync(&ghes
->timer
);
1261 case ACPI_HEST_NOTIFY_EXTERNAL
:
1262 free_irq(ghes
->irq
, ghes
);
1265 case ACPI_HEST_NOTIFY_SCI
:
1266 case ACPI_HEST_NOTIFY_GSIV
:
1267 case ACPI_HEST_NOTIFY_GPIO
:
1268 mutex_lock(&ghes_list_mutex
);
1269 list_del_rcu(&ghes
->list
);
1270 if (list_empty(&ghes_hed
))
1271 unregister_acpi_hed_notifier(&ghes_notifier_hed
);
1272 mutex_unlock(&ghes_list_mutex
);
1276 case ACPI_HEST_NOTIFY_SEA
:
1277 ghes_sea_remove(ghes
);
1279 case ACPI_HEST_NOTIFY_NMI
:
1280 ghes_nmi_remove(ghes
);
1282 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED
:
1283 rc
= apei_sdei_unregister_ghes(ghes
);
1294 ghes_edac_unregister(ghes
);
1298 platform_set_drvdata(ghes_dev
, NULL
);
1303 static struct platform_driver ghes_platform_driver
= {
1307 .probe
= ghes_probe
,
1308 .remove
= ghes_remove
,
1311 static int __init
ghes_init(void)
1318 switch (hest_disable
) {
1319 case HEST_NOT_FOUND
:
1322 pr_info(GHES_PFX
"HEST is not enabled!\n");
1329 pr_info(GHES_PFX
"GHES is not enabled!\n");
1333 ghes_nmi_init_cxt();
1335 rc
= platform_driver_register(&ghes_platform_driver
);
1339 rc
= apei_osc_setup();
1340 if (rc
== 0 && osc_sb_apei_support_acked
)
1341 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1342 else if (rc
== 0 && !osc_sb_apei_support_acked
)
1343 pr_info(GHES_PFX
"APEI firmware first mode is enabled by WHEA _OSC.\n");
1344 else if (rc
&& osc_sb_apei_support_acked
)
1345 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit.\n");
1347 pr_info(GHES_PFX
"Failed to enable APEI firmware first mode.\n");
1353 device_initcall(ghes_init
);