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 * GHESv2 type HEST entries introduce support for error acknowledgment,
748 * so only acknowledge the error if this support is present.
750 if (is_hest_type_generic_v2(ghes
)) {
751 rc
= ghes_ack_error(ghes
->generic_v2
);
756 ghes_clear_estatus(ghes
);
760 static void ghes_add_timer(struct ghes
*ghes
)
762 struct acpi_hest_generic
*g
= ghes
->generic
;
763 unsigned long expire
;
765 if (!g
->notify
.poll_interval
) {
766 pr_warning(FW_WARN GHES_PFX
"Poll interval is 0 for generic hardware error source: %d, disabled.\n",
767 g
->header
.source_id
);
770 expire
= jiffies
+ msecs_to_jiffies(g
->notify
.poll_interval
);
771 ghes
->timer
.expires
= round_jiffies_relative(expire
);
772 add_timer(&ghes
->timer
);
775 static void ghes_poll_func(unsigned long data
)
777 struct ghes
*ghes
= (void *)data
;
780 if (!(ghes
->flags
& GHES_EXITING
))
781 ghes_add_timer(ghes
);
784 static irqreturn_t
ghes_irq_func(int irq
, void *data
)
786 struct ghes
*ghes
= data
;
789 rc
= ghes_proc(ghes
);
796 static int ghes_notify_hed(struct notifier_block
*this, unsigned long event
,
800 int ret
= NOTIFY_DONE
;
803 list_for_each_entry_rcu(ghes
, &ghes_hed
, list
) {
804 if (!ghes_proc(ghes
))
812 static struct notifier_block ghes_notifier_hed
= {
813 .notifier_call
= ghes_notify_hed
,
816 #ifdef CONFIG_ACPI_APEI_SEA
817 static LIST_HEAD(ghes_sea
);
820 * Return 0 only if one of the SEA error sources successfully reported an error
821 * record sent from the firmware.
823 int ghes_notify_sea(void)
829 list_for_each_entry_rcu(ghes
, &ghes_sea
, list
) {
830 if (!ghes_proc(ghes
))
837 static void ghes_sea_add(struct ghes
*ghes
)
839 mutex_lock(&ghes_list_mutex
);
840 list_add_rcu(&ghes
->list
, &ghes_sea
);
841 mutex_unlock(&ghes_list_mutex
);
844 static void ghes_sea_remove(struct ghes
*ghes
)
846 mutex_lock(&ghes_list_mutex
);
847 list_del_rcu(&ghes
->list
);
848 mutex_unlock(&ghes_list_mutex
);
851 #else /* CONFIG_ACPI_APEI_SEA */
852 static inline void ghes_sea_add(struct ghes
*ghes
)
854 pr_err(GHES_PFX
"ID: %d, trying to add SEA notification which is not supported\n",
855 ghes
->generic
->header
.source_id
);
858 static inline void ghes_sea_remove(struct ghes
*ghes
)
860 pr_err(GHES_PFX
"ID: %d, trying to remove SEA notification which is not supported\n",
861 ghes
->generic
->header
.source_id
);
863 #endif /* CONFIG_ACPI_APEI_SEA */
865 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
867 * printk is not safe in NMI context. So in NMI handler, we allocate
868 * required memory from lock-less memory allocator
869 * (ghes_estatus_pool), save estatus into it, put them into lock-less
870 * list (ghes_estatus_llist), then delay printk into IRQ context via
871 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
872 * required pool size by all NMI error source.
874 static struct llist_head ghes_estatus_llist
;
875 static struct irq_work ghes_proc_irq_work
;
878 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
879 * having only one concurrent reader.
881 static atomic_t ghes_in_nmi
= ATOMIC_INIT(0);
883 static LIST_HEAD(ghes_nmi
);
885 static void ghes_proc_in_irq(struct irq_work
*irq_work
)
887 struct llist_node
*llnode
, *next
;
888 struct ghes_estatus_node
*estatus_node
;
889 struct acpi_hest_generic
*generic
;
890 struct acpi_hest_generic_status
*estatus
;
893 llnode
= llist_del_all(&ghes_estatus_llist
);
895 * Because the time order of estatus in list is reversed,
896 * revert it back to proper order.
898 llnode
= llist_reverse_order(llnode
);
901 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
903 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
904 len
= cper_estatus_len(estatus
);
905 node_len
= GHES_ESTATUS_NODE_LEN(len
);
906 ghes_do_proc(estatus_node
->ghes
, estatus
);
907 if (!ghes_estatus_cached(estatus
)) {
908 generic
= estatus_node
->generic
;
909 if (ghes_print_estatus(NULL
, generic
, estatus
))
910 ghes_estatus_cache_add(generic
, estatus
);
912 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
918 static void ghes_print_queued_estatus(void)
920 struct llist_node
*llnode
;
921 struct ghes_estatus_node
*estatus_node
;
922 struct acpi_hest_generic
*generic
;
923 struct acpi_hest_generic_status
*estatus
;
926 llnode
= llist_del_all(&ghes_estatus_llist
);
928 * Because the time order of estatus in list is reversed,
929 * revert it back to proper order.
931 llnode
= llist_reverse_order(llnode
);
933 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
935 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
936 len
= cper_estatus_len(estatus
);
937 node_len
= GHES_ESTATUS_NODE_LEN(len
);
938 generic
= estatus_node
->generic
;
939 ghes_print_estatus(NULL
, generic
, estatus
);
940 llnode
= llnode
->next
;
944 /* Save estatus for further processing in IRQ context */
945 static void __process_error(struct ghes
*ghes
)
947 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
949 struct ghes_estatus_node
*estatus_node
;
950 struct acpi_hest_generic_status
*estatus
;
952 if (ghes_estatus_cached(ghes
->estatus
))
955 len
= cper_estatus_len(ghes
->estatus
);
956 node_len
= GHES_ESTATUS_NODE_LEN(len
);
958 estatus_node
= (void *)gen_pool_alloc(ghes_estatus_pool
, node_len
);
962 estatus_node
->ghes
= ghes
;
963 estatus_node
->generic
= ghes
->generic
;
964 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
965 memcpy(estatus
, ghes
->estatus
, len
);
966 llist_add(&estatus_node
->llnode
, &ghes_estatus_llist
);
970 static int ghes_notify_nmi(unsigned int cmd
, struct pt_regs
*regs
)
973 int sev
, ret
= NMI_DONE
;
975 if (!atomic_add_unless(&ghes_in_nmi
, 1, 1))
978 list_for_each_entry_rcu(ghes
, &ghes_nmi
, list
) {
979 if (ghes_read_estatus(ghes
, 1)) {
980 ghes_clear_estatus(ghes
);
986 sev
= ghes_severity(ghes
->estatus
->error_severity
);
987 if (sev
>= GHES_SEV_PANIC
) {
989 ghes_print_queued_estatus();
993 if (!(ghes
->flags
& GHES_TO_CLEAR
))
996 __process_error(ghes
);
997 ghes_clear_estatus(ghes
);
1000 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
1001 if (ret
== NMI_HANDLED
)
1002 irq_work_queue(&ghes_proc_irq_work
);
1004 atomic_dec(&ghes_in_nmi
);
1008 static unsigned long ghes_esource_prealloc_size(
1009 const struct acpi_hest_generic
*generic
)
1011 unsigned long block_length
, prealloc_records
, prealloc_size
;
1013 block_length
= min_t(unsigned long, generic
->error_block_length
,
1014 GHES_ESTATUS_MAX_SIZE
);
1015 prealloc_records
= max_t(unsigned long,
1016 generic
->records_to_preallocate
, 1);
1017 prealloc_size
= min_t(unsigned long, block_length
* prealloc_records
,
1018 GHES_ESOURCE_PREALLOC_MAX_SIZE
);
1020 return prealloc_size
;
1023 static void ghes_estatus_pool_shrink(unsigned long len
)
1025 ghes_estatus_pool_size_request
-= PAGE_ALIGN(len
);
1028 static void ghes_nmi_add(struct ghes
*ghes
)
1032 len
= ghes_esource_prealloc_size(ghes
->generic
);
1033 ghes_estatus_pool_expand(len
);
1034 mutex_lock(&ghes_list_mutex
);
1035 if (list_empty(&ghes_nmi
))
1036 register_nmi_handler(NMI_LOCAL
, ghes_notify_nmi
, 0, "ghes");
1037 list_add_rcu(&ghes
->list
, &ghes_nmi
);
1038 mutex_unlock(&ghes_list_mutex
);
1041 static void ghes_nmi_remove(struct ghes
*ghes
)
1045 mutex_lock(&ghes_list_mutex
);
1046 list_del_rcu(&ghes
->list
);
1047 if (list_empty(&ghes_nmi
))
1048 unregister_nmi_handler(NMI_LOCAL
, "ghes");
1049 mutex_unlock(&ghes_list_mutex
);
1051 * To synchronize with NMI handler, ghes can only be
1052 * freed after NMI handler finishes.
1055 len
= ghes_esource_prealloc_size(ghes
->generic
);
1056 ghes_estatus_pool_shrink(len
);
1059 static void ghes_nmi_init_cxt(void)
1061 init_irq_work(&ghes_proc_irq_work
, ghes_proc_in_irq
);
1063 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1064 static inline void ghes_nmi_add(struct ghes
*ghes
)
1066 pr_err(GHES_PFX
"ID: %d, trying to add NMI notification which is not supported!\n",
1067 ghes
->generic
->header
.source_id
);
1071 static inline void ghes_nmi_remove(struct ghes
*ghes
)
1073 pr_err(GHES_PFX
"ID: %d, trying to remove NMI notification which is not supported!\n",
1074 ghes
->generic
->header
.source_id
);
1078 static inline void ghes_nmi_init_cxt(void)
1081 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1083 static int ghes_probe(struct platform_device
*ghes_dev
)
1085 struct acpi_hest_generic
*generic
;
1086 struct ghes
*ghes
= NULL
;
1090 generic
= *(struct acpi_hest_generic
**)ghes_dev
->dev
.platform_data
;
1091 if (!generic
->enabled
)
1094 switch (generic
->notify
.type
) {
1095 case ACPI_HEST_NOTIFY_POLLED
:
1096 case ACPI_HEST_NOTIFY_EXTERNAL
:
1097 case ACPI_HEST_NOTIFY_SCI
:
1098 case ACPI_HEST_NOTIFY_GSIV
:
1099 case ACPI_HEST_NOTIFY_GPIO
:
1102 case ACPI_HEST_NOTIFY_SEA
:
1103 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA
)) {
1104 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via SEA is not supported\n",
1105 generic
->header
.source_id
);
1110 case ACPI_HEST_NOTIFY_NMI
:
1111 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI
)) {
1112 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1113 generic
->header
.source_id
);
1117 case ACPI_HEST_NOTIFY_LOCAL
:
1118 pr_warning(GHES_PFX
"Generic hardware error source: %d notified via local interrupt is not supported!\n",
1119 generic
->header
.source_id
);
1122 pr_warning(FW_WARN GHES_PFX
"Unknown notification type: %u for generic hardware error source: %d\n",
1123 generic
->notify
.type
, generic
->header
.source_id
);
1128 if (generic
->error_block_length
<
1129 sizeof(struct acpi_hest_generic_status
)) {
1130 pr_warning(FW_BUG GHES_PFX
"Invalid error block length: %u for generic hardware error source: %d\n",
1131 generic
->error_block_length
,
1132 generic
->header
.source_id
);
1135 ghes
= ghes_new(generic
);
1142 rc
= ghes_edac_register(ghes
, &ghes_dev
->dev
);
1146 switch (generic
->notify
.type
) {
1147 case ACPI_HEST_NOTIFY_POLLED
:
1148 setup_deferrable_timer(&ghes
->timer
, ghes_poll_func
,
1149 (unsigned long)ghes
);
1150 ghes_add_timer(ghes
);
1152 case ACPI_HEST_NOTIFY_EXTERNAL
:
1153 /* External interrupt vector is GSI */
1154 rc
= acpi_gsi_to_irq(generic
->notify
.vector
, &ghes
->irq
);
1156 pr_err(GHES_PFX
"Failed to map GSI to IRQ for generic hardware error source: %d\n",
1157 generic
->header
.source_id
);
1158 goto err_edac_unreg
;
1160 rc
= request_irq(ghes
->irq
, ghes_irq_func
, IRQF_SHARED
,
1163 pr_err(GHES_PFX
"Failed to register IRQ for generic hardware error source: %d\n",
1164 generic
->header
.source_id
);
1165 goto err_edac_unreg
;
1169 case ACPI_HEST_NOTIFY_SCI
:
1170 case ACPI_HEST_NOTIFY_GSIV
:
1171 case ACPI_HEST_NOTIFY_GPIO
:
1172 mutex_lock(&ghes_list_mutex
);
1173 if (list_empty(&ghes_hed
))
1174 register_acpi_hed_notifier(&ghes_notifier_hed
);
1175 list_add_rcu(&ghes
->list
, &ghes_hed
);
1176 mutex_unlock(&ghes_list_mutex
);
1179 case ACPI_HEST_NOTIFY_SEA
:
1182 case ACPI_HEST_NOTIFY_NMI
:
1188 platform_set_drvdata(ghes_dev
, ghes
);
1190 /* Handle any pending errors right away */
1195 ghes_edac_unregister(ghes
);
1204 static int ghes_remove(struct platform_device
*ghes_dev
)
1207 struct acpi_hest_generic
*generic
;
1209 ghes
= platform_get_drvdata(ghes_dev
);
1210 generic
= ghes
->generic
;
1212 ghes
->flags
|= GHES_EXITING
;
1213 switch (generic
->notify
.type
) {
1214 case ACPI_HEST_NOTIFY_POLLED
:
1215 del_timer_sync(&ghes
->timer
);
1217 case ACPI_HEST_NOTIFY_EXTERNAL
:
1218 free_irq(ghes
->irq
, ghes
);
1221 case ACPI_HEST_NOTIFY_SCI
:
1222 case ACPI_HEST_NOTIFY_GSIV
:
1223 case ACPI_HEST_NOTIFY_GPIO
:
1224 mutex_lock(&ghes_list_mutex
);
1225 list_del_rcu(&ghes
->list
);
1226 if (list_empty(&ghes_hed
))
1227 unregister_acpi_hed_notifier(&ghes_notifier_hed
);
1228 mutex_unlock(&ghes_list_mutex
);
1232 case ACPI_HEST_NOTIFY_SEA
:
1233 ghes_sea_remove(ghes
);
1235 case ACPI_HEST_NOTIFY_NMI
:
1236 ghes_nmi_remove(ghes
);
1245 ghes_edac_unregister(ghes
);
1249 platform_set_drvdata(ghes_dev
, NULL
);
1254 static struct platform_driver ghes_platform_driver
= {
1258 .probe
= ghes_probe
,
1259 .remove
= ghes_remove
,
1262 static int __init
ghes_init(void)
1269 switch (hest_disable
) {
1270 case HEST_NOT_FOUND
:
1273 pr_info(GHES_PFX
"HEST is not enabled!\n");
1280 pr_info(GHES_PFX
"GHES is not enabled!\n");
1284 ghes_nmi_init_cxt();
1286 rc
= ghes_ioremap_init();
1290 rc
= ghes_estatus_pool_init();
1292 goto err_ioremap_exit
;
1294 rc
= ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE
*
1295 GHES_ESTATUS_CACHE_ALLOCED_MAX
);
1299 rc
= platform_driver_register(&ghes_platform_driver
);
1303 rc
= apei_osc_setup();
1304 if (rc
== 0 && osc_sb_apei_support_acked
)
1305 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1306 else if (rc
== 0 && !osc_sb_apei_support_acked
)
1307 pr_info(GHES_PFX
"APEI firmware first mode is enabled by WHEA _OSC.\n");
1308 else if (rc
&& osc_sb_apei_support_acked
)
1309 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit.\n");
1311 pr_info(GHES_PFX
"Failed to enable APEI firmware first mode.\n");
1315 ghes_estatus_pool_exit();
1317 ghes_ioremap_exit();
1321 device_initcall(ghes_init
);