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/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/tlbflush.h>
54 #include <ras/ras_event.h>
56 #include "apei-internal.h"
58 #define GHES_PFX "GHES: "
60 #define GHES_ESTATUS_MAX_SIZE 65536
61 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
63 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
65 /* This is just an estimation for memory pool allocation */
66 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
68 #define GHES_ESTATUS_CACHES_SIZE 4
70 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
71 /* Prevent too many caches are allocated because of RCU */
72 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
74 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
75 (sizeof(struct ghes_estatus_cache) + (estatus_len))
76 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
77 ((struct acpi_hest_generic_status *) \
78 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
80 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
81 (sizeof(struct ghes_estatus_node) + (estatus_len))
82 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
83 ((struct acpi_hest_generic_status *) \
84 ((struct ghes_estatus_node *)(estatus_node) + 1))
86 static inline bool is_hest_type_generic_v2(struct ghes
*ghes
)
88 return ghes
->generic
->header
.type
== ACPI_HEST_TYPE_GENERIC_ERROR_V2
;
92 * This driver isn't really modular, however for the time being,
93 * continuing to use module_param is the easiest way to remain
94 * compatible with existing boot arg use cases.
97 module_param_named(disable
, ghes_disable
, bool, 0);
100 * All error sources notified with SCI shares one notifier function,
101 * so they need to be linked and checked one by one. This is applied
104 * RCU is used for these lists, so ghes_list_mutex is only used for
105 * list changing, not for traversing.
107 static LIST_HEAD(ghes_sci
);
108 static DEFINE_MUTEX(ghes_list_mutex
);
111 * Because the memory area used to transfer hardware error information
112 * from BIOS to Linux can be determined only in NMI, IRQ or timer
113 * handler, but general ioremap can not be used in atomic context, so
114 * a special version of atomic ioremap is implemented for that.
118 * Two virtual pages are used, one for IRQ/PROCESS context, the other for
119 * NMI context (optionally).
121 #define GHES_IOREMAP_PAGES 2
122 #define GHES_IOREMAP_IRQ_PAGE(base) (base)
123 #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
125 /* virtual memory area for atomic ioremap */
126 static struct vm_struct
*ghes_ioremap_area
;
128 * These 2 spinlock is used to prevent atomic ioremap virtual memory
129 * area from being mapped simultaneously.
131 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi
);
132 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq
);
134 static struct gen_pool
*ghes_estatus_pool
;
135 static unsigned long ghes_estatus_pool_size_request
;
137 static struct ghes_estatus_cache
*ghes_estatus_caches
[GHES_ESTATUS_CACHES_SIZE
];
138 static atomic_t ghes_estatus_cache_alloced
;
140 static int ghes_panic_timeout __read_mostly
= 30;
142 static int ghes_ioremap_init(void)
144 ghes_ioremap_area
= __get_vm_area(PAGE_SIZE
* GHES_IOREMAP_PAGES
,
145 VM_IOREMAP
, VMALLOC_START
, VMALLOC_END
);
146 if (!ghes_ioremap_area
) {
147 pr_err(GHES_PFX
"Failed to allocate virtual memory area for atomic ioremap.\n");
154 static void ghes_ioremap_exit(void)
156 free_vm_area(ghes_ioremap_area
);
159 static void __iomem
*ghes_ioremap_pfn_nmi(u64 pfn
)
165 vaddr
= (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area
->addr
);
167 paddr
= pfn
<< PAGE_SHIFT
;
168 prot
= arch_apei_get_mem_attribute(paddr
);
169 ioremap_page_range(vaddr
, vaddr
+ PAGE_SIZE
, paddr
, prot
);
171 return (void __iomem
*)vaddr
;
174 static void __iomem
*ghes_ioremap_pfn_irq(u64 pfn
)
176 unsigned long vaddr
, paddr
;
179 vaddr
= (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area
->addr
);
181 paddr
= pfn
<< PAGE_SHIFT
;
182 prot
= arch_apei_get_mem_attribute(paddr
);
184 ioremap_page_range(vaddr
, vaddr
+ PAGE_SIZE
, paddr
, prot
);
186 return (void __iomem
*)vaddr
;
189 static void ghes_iounmap_nmi(void __iomem
*vaddr_ptr
)
191 unsigned long vaddr
= (unsigned long __force
)vaddr_ptr
;
192 void *base
= ghes_ioremap_area
->addr
;
194 BUG_ON(vaddr
!= (unsigned long)GHES_IOREMAP_NMI_PAGE(base
));
195 unmap_kernel_range_noflush(vaddr
, PAGE_SIZE
);
196 arch_apei_flush_tlb_one(vaddr
);
199 static void ghes_iounmap_irq(void __iomem
*vaddr_ptr
)
201 unsigned long vaddr
= (unsigned long __force
)vaddr_ptr
;
202 void *base
= ghes_ioremap_area
->addr
;
204 BUG_ON(vaddr
!= (unsigned long)GHES_IOREMAP_IRQ_PAGE(base
));
205 unmap_kernel_range_noflush(vaddr
, PAGE_SIZE
);
206 arch_apei_flush_tlb_one(vaddr
);
209 static int ghes_estatus_pool_init(void)
211 ghes_estatus_pool
= gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER
, -1);
212 if (!ghes_estatus_pool
)
217 static void ghes_estatus_pool_free_chunk_page(struct gen_pool
*pool
,
218 struct gen_pool_chunk
*chunk
,
221 free_page(chunk
->start_addr
);
224 static void ghes_estatus_pool_exit(void)
226 gen_pool_for_each_chunk(ghes_estatus_pool
,
227 ghes_estatus_pool_free_chunk_page
, NULL
);
228 gen_pool_destroy(ghes_estatus_pool
);
231 static int ghes_estatus_pool_expand(unsigned long len
)
233 unsigned long i
, pages
, size
, addr
;
236 ghes_estatus_pool_size_request
+= PAGE_ALIGN(len
);
237 size
= gen_pool_size(ghes_estatus_pool
);
238 if (size
>= ghes_estatus_pool_size_request
)
240 pages
= (ghes_estatus_pool_size_request
- size
) / PAGE_SIZE
;
241 for (i
= 0; i
< pages
; i
++) {
242 addr
= __get_free_page(GFP_KERNEL
);
245 ret
= gen_pool_add(ghes_estatus_pool
, addr
, PAGE_SIZE
, -1);
253 static int map_gen_v2(struct ghes
*ghes
)
255 return apei_map_generic_address(&ghes
->generic_v2
->read_ack_register
);
258 static void unmap_gen_v2(struct ghes
*ghes
)
260 apei_unmap_generic_address(&ghes
->generic_v2
->read_ack_register
);
263 static struct ghes
*ghes_new(struct acpi_hest_generic
*generic
)
266 unsigned int error_block_length
;
269 ghes
= kzalloc(sizeof(*ghes
), GFP_KERNEL
);
271 return ERR_PTR(-ENOMEM
);
273 ghes
->generic
= generic
;
274 if (is_hest_type_generic_v2(ghes
)) {
275 rc
= map_gen_v2(ghes
);
280 rc
= apei_map_generic_address(&generic
->error_status_address
);
282 goto err_unmap_read_ack_addr
;
283 error_block_length
= generic
->error_block_length
;
284 if (error_block_length
> GHES_ESTATUS_MAX_SIZE
) {
285 pr_warning(FW_WARN GHES_PFX
286 "Error status block length is too long: %u for "
287 "generic hardware error source: %d.\n",
288 error_block_length
, generic
->header
.source_id
);
289 error_block_length
= GHES_ESTATUS_MAX_SIZE
;
291 ghes
->estatus
= kmalloc(error_block_length
, GFP_KERNEL
);
292 if (!ghes
->estatus
) {
294 goto err_unmap_status_addr
;
299 err_unmap_status_addr
:
300 apei_unmap_generic_address(&generic
->error_status_address
);
301 err_unmap_read_ack_addr
:
302 if (is_hest_type_generic_v2(ghes
))
309 static void ghes_fini(struct ghes
*ghes
)
311 kfree(ghes
->estatus
);
312 apei_unmap_generic_address(&ghes
->generic
->error_status_address
);
313 if (is_hest_type_generic_v2(ghes
))
317 static inline int ghes_severity(int severity
)
320 case CPER_SEV_INFORMATIONAL
:
322 case CPER_SEV_CORRECTED
:
323 return GHES_SEV_CORRECTED
;
324 case CPER_SEV_RECOVERABLE
:
325 return GHES_SEV_RECOVERABLE
;
327 return GHES_SEV_PANIC
;
329 /* Unknown, go panic */
330 return GHES_SEV_PANIC
;
334 static void ghes_copy_tofrom_phys(void *buffer
, u64 paddr
, u32 len
,
338 unsigned long flags
= 0;
339 int in_nmi
= in_nmi();
344 offset
= paddr
- (paddr
& PAGE_MASK
);
346 raw_spin_lock(&ghes_ioremap_lock_nmi
);
347 vaddr
= ghes_ioremap_pfn_nmi(paddr
>> PAGE_SHIFT
);
349 spin_lock_irqsave(&ghes_ioremap_lock_irq
, flags
);
350 vaddr
= ghes_ioremap_pfn_irq(paddr
>> PAGE_SHIFT
);
352 trunk
= PAGE_SIZE
- offset
;
353 trunk
= min(trunk
, len
);
355 memcpy_fromio(buffer
, vaddr
+ offset
, trunk
);
357 memcpy_toio(vaddr
+ offset
, buffer
, trunk
);
362 ghes_iounmap_nmi(vaddr
);
363 raw_spin_unlock(&ghes_ioremap_lock_nmi
);
365 ghes_iounmap_irq(vaddr
);
366 spin_unlock_irqrestore(&ghes_ioremap_lock_irq
, flags
);
371 static int ghes_read_estatus(struct ghes
*ghes
, int silent
)
373 struct acpi_hest_generic
*g
= ghes
->generic
;
378 rc
= apei_read(&buf_paddr
, &g
->error_status_address
);
380 if (!silent
&& printk_ratelimit())
381 pr_warning(FW_WARN GHES_PFX
382 "Failed to read error status block address for hardware error source: %d.\n",
383 g
->header
.source_id
);
389 ghes_copy_tofrom_phys(ghes
->estatus
, buf_paddr
,
390 sizeof(*ghes
->estatus
), 1);
391 if (!ghes
->estatus
->block_status
)
394 ghes
->buffer_paddr
= buf_paddr
;
395 ghes
->flags
|= GHES_TO_CLEAR
;
398 len
= cper_estatus_len(ghes
->estatus
);
399 if (len
< sizeof(*ghes
->estatus
))
401 if (len
> ghes
->generic
->error_block_length
)
403 if (cper_estatus_check_header(ghes
->estatus
))
405 ghes_copy_tofrom_phys(ghes
->estatus
+ 1,
406 buf_paddr
+ sizeof(*ghes
->estatus
),
407 len
- sizeof(*ghes
->estatus
), 1);
408 if (cper_estatus_check(ghes
->estatus
))
413 if (rc
&& !silent
&& printk_ratelimit())
414 pr_warning(FW_WARN GHES_PFX
415 "Failed to read error status block!\n");
419 static void ghes_clear_estatus(struct ghes
*ghes
)
421 ghes
->estatus
->block_status
= 0;
422 if (!(ghes
->flags
& GHES_TO_CLEAR
))
424 ghes_copy_tofrom_phys(ghes
->estatus
, ghes
->buffer_paddr
,
425 sizeof(ghes
->estatus
->block_status
), 0);
426 ghes
->flags
&= ~GHES_TO_CLEAR
;
429 static void ghes_handle_memory_failure(struct acpi_hest_generic_data
*gdata
, int sev
)
431 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
434 int sec_sev
= ghes_severity(gdata
->error_severity
);
435 struct cper_sec_mem_err
*mem_err
= acpi_hest_get_payload(gdata
);
437 if (!(mem_err
->validation_bits
& CPER_MEM_VALID_PA
))
440 pfn
= mem_err
->physical_addr
>> PAGE_SHIFT
;
441 if (!pfn_valid(pfn
)) {
442 pr_warn_ratelimited(FW_WARN GHES_PFX
443 "Invalid address in generic error data: %#llx\n",
444 mem_err
->physical_addr
);
448 /* iff following two events can be handled properly by now */
449 if (sec_sev
== GHES_SEV_CORRECTED
&&
450 (gdata
->flags
& CPER_SEC_ERROR_THRESHOLD_EXCEEDED
))
451 flags
= MF_SOFT_OFFLINE
;
452 if (sev
== GHES_SEV_RECOVERABLE
&& sec_sev
== GHES_SEV_RECOVERABLE
)
456 memory_failure_queue(pfn
, 0, flags
);
460 static void ghes_do_proc(struct ghes
*ghes
,
461 const struct acpi_hest_generic_status
*estatus
)
464 struct acpi_hest_generic_data
*gdata
;
466 uuid_le
*fru_id
= &NULL_UUID_LE
;
469 sev
= ghes_severity(estatus
->error_severity
);
470 apei_estatus_for_each_section(estatus
, gdata
) {
471 sec_sev
= ghes_severity(gdata
->error_severity
);
472 sec_type
= *(uuid_le
*)gdata
->section_type
;
474 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_ID
)
475 fru_id
= (uuid_le
*)gdata
->fru_id
;
477 if (gdata
->validation_bits
& CPER_SEC_VALID_FRU_TEXT
)
478 fru_text
= gdata
->fru_text
;
480 if (!uuid_le_cmp(*(uuid_le
*)gdata
->section_type
,
481 CPER_SEC_PLATFORM_MEM
)) {
482 struct cper_sec_mem_err
*mem_err
= acpi_hest_get_payload(gdata
);
484 ghes_edac_report_mem_error(ghes
, sev
, mem_err
);
486 arch_apei_report_mem_error(sev
, mem_err
);
487 ghes_handle_memory_failure(gdata
, sev
);
489 #ifdef CONFIG_ACPI_APEI_PCIEAER
490 else if (!uuid_le_cmp(*(uuid_le
*)gdata
->section_type
,
492 struct cper_sec_pcie
*pcie_err
= acpi_hest_get_payload(gdata
);
494 if (sev
== GHES_SEV_RECOVERABLE
&&
495 sec_sev
== GHES_SEV_RECOVERABLE
&&
496 pcie_err
->validation_bits
& CPER_PCIE_VALID_DEVICE_ID
&&
497 pcie_err
->validation_bits
& CPER_PCIE_VALID_AER_INFO
) {
501 devfn
= PCI_DEVFN(pcie_err
->device_id
.device
,
502 pcie_err
->device_id
.function
);
503 aer_severity
= cper_severity_to_aer(gdata
->error_severity
);
506 * If firmware reset the component to contain
507 * the error, we must reinitialize it before
508 * use, so treat it as a fatal AER error.
510 if (gdata
->flags
& CPER_SEC_RESET
)
511 aer_severity
= AER_FATAL
;
513 aer_recover_queue(pcie_err
->device_id
.segment
,
514 pcie_err
->device_id
.bus
,
516 (struct aer_capability_regs
*)
523 void *err
= acpi_hest_get_payload(gdata
);
525 log_non_standard_event(&sec_type
, fru_id
, fru_text
,
527 gdata
->error_data_length
);
532 static void __ghes_print_estatus(const char *pfx
,
533 const struct acpi_hest_generic
*generic
,
534 const struct acpi_hest_generic_status
*estatus
)
536 static atomic_t seqno
;
537 unsigned int curr_seqno
;
541 if (ghes_severity(estatus
->error_severity
) <=
547 curr_seqno
= atomic_inc_return(&seqno
);
548 snprintf(pfx_seq
, sizeof(pfx_seq
), "%s{%u}" HW_ERR
, pfx
, curr_seqno
);
549 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
550 pfx_seq
, generic
->header
.source_id
);
551 cper_estatus_print(pfx_seq
, estatus
);
554 static int ghes_print_estatus(const char *pfx
,
555 const struct acpi_hest_generic
*generic
,
556 const struct acpi_hest_generic_status
*estatus
)
558 /* Not more than 2 messages every 5 seconds */
559 static DEFINE_RATELIMIT_STATE(ratelimit_corrected
, 5*HZ
, 2);
560 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected
, 5*HZ
, 2);
561 struct ratelimit_state
*ratelimit
;
563 if (ghes_severity(estatus
->error_severity
) <= GHES_SEV_CORRECTED
)
564 ratelimit
= &ratelimit_corrected
;
566 ratelimit
= &ratelimit_uncorrected
;
567 if (__ratelimit(ratelimit
)) {
568 __ghes_print_estatus(pfx
, generic
, estatus
);
575 * GHES error status reporting throttle, to report more kinds of
576 * errors, instead of just most frequently occurred errors.
578 static int ghes_estatus_cached(struct acpi_hest_generic_status
*estatus
)
582 unsigned long long now
;
583 struct ghes_estatus_cache
*cache
;
584 struct acpi_hest_generic_status
*cache_estatus
;
586 len
= cper_estatus_len(estatus
);
588 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
589 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
592 if (len
!= cache
->estatus_len
)
594 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
595 if (memcmp(estatus
, cache_estatus
, len
))
597 atomic_inc(&cache
->count
);
599 if (now
- cache
->time_in
< GHES_ESTATUS_IN_CACHE_MAX_NSEC
)
607 static struct ghes_estatus_cache
*ghes_estatus_cache_alloc(
608 struct acpi_hest_generic
*generic
,
609 struct acpi_hest_generic_status
*estatus
)
613 struct ghes_estatus_cache
*cache
;
614 struct acpi_hest_generic_status
*cache_estatus
;
616 alloced
= atomic_add_return(1, &ghes_estatus_cache_alloced
);
617 if (alloced
> GHES_ESTATUS_CACHE_ALLOCED_MAX
) {
618 atomic_dec(&ghes_estatus_cache_alloced
);
621 len
= cper_estatus_len(estatus
);
622 cache_len
= GHES_ESTATUS_CACHE_LEN(len
);
623 cache
= (void *)gen_pool_alloc(ghes_estatus_pool
, cache_len
);
625 atomic_dec(&ghes_estatus_cache_alloced
);
628 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
629 memcpy(cache_estatus
, estatus
, len
);
630 cache
->estatus_len
= len
;
631 atomic_set(&cache
->count
, 0);
632 cache
->generic
= generic
;
633 cache
->time_in
= sched_clock();
637 static void ghes_estatus_cache_free(struct ghes_estatus_cache
*cache
)
641 len
= cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache
));
642 len
= GHES_ESTATUS_CACHE_LEN(len
);
643 gen_pool_free(ghes_estatus_pool
, (unsigned long)cache
, len
);
644 atomic_dec(&ghes_estatus_cache_alloced
);
647 static void ghes_estatus_cache_rcu_free(struct rcu_head
*head
)
649 struct ghes_estatus_cache
*cache
;
651 cache
= container_of(head
, struct ghes_estatus_cache
, rcu
);
652 ghes_estatus_cache_free(cache
);
655 static void ghes_estatus_cache_add(
656 struct acpi_hest_generic
*generic
,
657 struct acpi_hest_generic_status
*estatus
)
659 int i
, slot
= -1, count
;
660 unsigned long long now
, duration
, period
, max_period
= 0;
661 struct ghes_estatus_cache
*cache
, *slot_cache
= NULL
, *new_cache
;
663 new_cache
= ghes_estatus_cache_alloc(generic
, estatus
);
664 if (new_cache
== NULL
)
668 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
669 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
675 duration
= now
- cache
->time_in
;
676 if (duration
>= GHES_ESTATUS_IN_CACHE_MAX_NSEC
) {
681 count
= atomic_read(&cache
->count
);
683 do_div(period
, (count
+ 1));
684 if (period
> max_period
) {
690 /* new_cache must be put into array after its contents are written */
692 if (slot
!= -1 && cmpxchg(ghes_estatus_caches
+ slot
,
693 slot_cache
, new_cache
) == slot_cache
) {
695 call_rcu(&slot_cache
->rcu
, ghes_estatus_cache_rcu_free
);
697 ghes_estatus_cache_free(new_cache
);
701 static int ghes_ack_error(struct acpi_hest_generic_v2
*gv2
)
706 rc
= apei_read(&val
, &gv2
->read_ack_register
);
710 val
&= gv2
->read_ack_preserve
<< gv2
->read_ack_register
.bit_offset
;
711 val
|= gv2
->read_ack_write
<< gv2
->read_ack_register
.bit_offset
;
713 return apei_write(val
, &gv2
->read_ack_register
);
716 static void __ghes_panic(struct ghes
*ghes
)
718 __ghes_print_estatus(KERN_EMERG
, ghes
->generic
, ghes
->estatus
);
720 /* reboot to log the error! */
722 panic_timeout
= ghes_panic_timeout
;
723 panic("Fatal hardware error!");
726 static int ghes_proc(struct ghes
*ghes
)
730 rc
= ghes_read_estatus(ghes
, 0);
734 if (ghes_severity(ghes
->estatus
->error_severity
) >= GHES_SEV_PANIC
) {
738 if (!ghes_estatus_cached(ghes
->estatus
)) {
739 if (ghes_print_estatus(NULL
, ghes
->generic
, ghes
->estatus
))
740 ghes_estatus_cache_add(ghes
->generic
, ghes
->estatus
);
742 ghes_do_proc(ghes
, ghes
->estatus
);
745 * GHESv2 type HEST entries introduce support for error acknowledgment,
746 * so only acknowledge the error if this support is present.
748 if (is_hest_type_generic_v2(ghes
)) {
749 rc
= ghes_ack_error(ghes
->generic_v2
);
754 ghes_clear_estatus(ghes
);
758 static void ghes_add_timer(struct ghes
*ghes
)
760 struct acpi_hest_generic
*g
= ghes
->generic
;
761 unsigned long expire
;
763 if (!g
->notify
.poll_interval
) {
764 pr_warning(FW_WARN GHES_PFX
"Poll interval is 0 for generic hardware error source: %d, disabled.\n",
765 g
->header
.source_id
);
768 expire
= jiffies
+ msecs_to_jiffies(g
->notify
.poll_interval
);
769 ghes
->timer
.expires
= round_jiffies_relative(expire
);
770 add_timer(&ghes
->timer
);
773 static void ghes_poll_func(unsigned long data
)
775 struct ghes
*ghes
= (void *)data
;
778 if (!(ghes
->flags
& GHES_EXITING
))
779 ghes_add_timer(ghes
);
782 static irqreturn_t
ghes_irq_func(int irq
, void *data
)
784 struct ghes
*ghes
= data
;
787 rc
= ghes_proc(ghes
);
794 static int ghes_notify_sci(struct notifier_block
*this,
795 unsigned long event
, void *data
)
798 int ret
= NOTIFY_DONE
;
801 list_for_each_entry_rcu(ghes
, &ghes_sci
, list
) {
802 if (!ghes_proc(ghes
))
810 static struct notifier_block ghes_notifier_sci
= {
811 .notifier_call
= ghes_notify_sci
,
814 #ifdef CONFIG_ACPI_APEI_SEA
815 static LIST_HEAD(ghes_sea
);
817 void ghes_notify_sea(void)
822 * synchronize_rcu() will wait for nmi_exit(), so no need to
825 list_for_each_entry_rcu(ghes
, &ghes_sea
, list
) {
830 static void ghes_sea_add(struct ghes
*ghes
)
832 mutex_lock(&ghes_list_mutex
);
833 list_add_rcu(&ghes
->list
, &ghes_sea
);
834 mutex_unlock(&ghes_list_mutex
);
837 static void ghes_sea_remove(struct ghes
*ghes
)
839 mutex_lock(&ghes_list_mutex
);
840 list_del_rcu(&ghes
->list
);
841 mutex_unlock(&ghes_list_mutex
);
844 #else /* CONFIG_ACPI_APEI_SEA */
845 static inline void ghes_sea_add(struct ghes
*ghes
)
847 pr_err(GHES_PFX
"ID: %d, trying to add SEA notification which is not supported\n",
848 ghes
->generic
->header
.source_id
);
851 static inline void ghes_sea_remove(struct ghes
*ghes
)
853 pr_err(GHES_PFX
"ID: %d, trying to remove SEA notification which is not supported\n",
854 ghes
->generic
->header
.source_id
);
856 #endif /* CONFIG_ACPI_APEI_SEA */
858 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
860 * printk is not safe in NMI context. So in NMI handler, we allocate
861 * required memory from lock-less memory allocator
862 * (ghes_estatus_pool), save estatus into it, put them into lock-less
863 * list (ghes_estatus_llist), then delay printk into IRQ context via
864 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
865 * required pool size by all NMI error source.
867 static struct llist_head ghes_estatus_llist
;
868 static struct irq_work ghes_proc_irq_work
;
871 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
872 * having only one concurrent reader.
874 static atomic_t ghes_in_nmi
= ATOMIC_INIT(0);
876 static LIST_HEAD(ghes_nmi
);
878 static void ghes_proc_in_irq(struct irq_work
*irq_work
)
880 struct llist_node
*llnode
, *next
;
881 struct ghes_estatus_node
*estatus_node
;
882 struct acpi_hest_generic
*generic
;
883 struct acpi_hest_generic_status
*estatus
;
886 llnode
= llist_del_all(&ghes_estatus_llist
);
888 * Because the time order of estatus in list is reversed,
889 * revert it back to proper order.
891 llnode
= llist_reverse_order(llnode
);
894 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
896 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
897 len
= cper_estatus_len(estatus
);
898 node_len
= GHES_ESTATUS_NODE_LEN(len
);
899 ghes_do_proc(estatus_node
->ghes
, estatus
);
900 if (!ghes_estatus_cached(estatus
)) {
901 generic
= estatus_node
->generic
;
902 if (ghes_print_estatus(NULL
, generic
, estatus
))
903 ghes_estatus_cache_add(generic
, estatus
);
905 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
911 static void ghes_print_queued_estatus(void)
913 struct llist_node
*llnode
;
914 struct ghes_estatus_node
*estatus_node
;
915 struct acpi_hest_generic
*generic
;
916 struct acpi_hest_generic_status
*estatus
;
919 llnode
= llist_del_all(&ghes_estatus_llist
);
921 * Because the time order of estatus in list is reversed,
922 * revert it back to proper order.
924 llnode
= llist_reverse_order(llnode
);
926 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
928 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
929 len
= cper_estatus_len(estatus
);
930 node_len
= GHES_ESTATUS_NODE_LEN(len
);
931 generic
= estatus_node
->generic
;
932 ghes_print_estatus(NULL
, generic
, estatus
);
933 llnode
= llnode
->next
;
937 /* Save estatus for further processing in IRQ context */
938 static void __process_error(struct ghes
*ghes
)
940 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
942 struct ghes_estatus_node
*estatus_node
;
943 struct acpi_hest_generic_status
*estatus
;
945 if (ghes_estatus_cached(ghes
->estatus
))
948 len
= cper_estatus_len(ghes
->estatus
);
949 node_len
= GHES_ESTATUS_NODE_LEN(len
);
951 estatus_node
= (void *)gen_pool_alloc(ghes_estatus_pool
, node_len
);
955 estatus_node
->ghes
= ghes
;
956 estatus_node
->generic
= ghes
->generic
;
957 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
958 memcpy(estatus
, ghes
->estatus
, len
);
959 llist_add(&estatus_node
->llnode
, &ghes_estatus_llist
);
963 static int ghes_notify_nmi(unsigned int cmd
, struct pt_regs
*regs
)
966 int sev
, ret
= NMI_DONE
;
968 if (!atomic_add_unless(&ghes_in_nmi
, 1, 1))
971 list_for_each_entry_rcu(ghes
, &ghes_nmi
, list
) {
972 if (ghes_read_estatus(ghes
, 1)) {
973 ghes_clear_estatus(ghes
);
979 sev
= ghes_severity(ghes
->estatus
->error_severity
);
980 if (sev
>= GHES_SEV_PANIC
) {
982 ghes_print_queued_estatus();
986 if (!(ghes
->flags
& GHES_TO_CLEAR
))
989 __process_error(ghes
);
990 ghes_clear_estatus(ghes
);
993 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
994 if (ret
== NMI_HANDLED
)
995 irq_work_queue(&ghes_proc_irq_work
);
997 atomic_dec(&ghes_in_nmi
);
1001 static unsigned long ghes_esource_prealloc_size(
1002 const struct acpi_hest_generic
*generic
)
1004 unsigned long block_length
, prealloc_records
, prealloc_size
;
1006 block_length
= min_t(unsigned long, generic
->error_block_length
,
1007 GHES_ESTATUS_MAX_SIZE
);
1008 prealloc_records
= max_t(unsigned long,
1009 generic
->records_to_preallocate
, 1);
1010 prealloc_size
= min_t(unsigned long, block_length
* prealloc_records
,
1011 GHES_ESOURCE_PREALLOC_MAX_SIZE
);
1013 return prealloc_size
;
1016 static void ghes_estatus_pool_shrink(unsigned long len
)
1018 ghes_estatus_pool_size_request
-= PAGE_ALIGN(len
);
1021 static void ghes_nmi_add(struct ghes
*ghes
)
1025 len
= ghes_esource_prealloc_size(ghes
->generic
);
1026 ghes_estatus_pool_expand(len
);
1027 mutex_lock(&ghes_list_mutex
);
1028 if (list_empty(&ghes_nmi
))
1029 register_nmi_handler(NMI_LOCAL
, ghes_notify_nmi
, 0, "ghes");
1030 list_add_rcu(&ghes
->list
, &ghes_nmi
);
1031 mutex_unlock(&ghes_list_mutex
);
1034 static void ghes_nmi_remove(struct ghes
*ghes
)
1038 mutex_lock(&ghes_list_mutex
);
1039 list_del_rcu(&ghes
->list
);
1040 if (list_empty(&ghes_nmi
))
1041 unregister_nmi_handler(NMI_LOCAL
, "ghes");
1042 mutex_unlock(&ghes_list_mutex
);
1044 * To synchronize with NMI handler, ghes can only be
1045 * freed after NMI handler finishes.
1048 len
= ghes_esource_prealloc_size(ghes
->generic
);
1049 ghes_estatus_pool_shrink(len
);
1052 static void ghes_nmi_init_cxt(void)
1054 init_irq_work(&ghes_proc_irq_work
, ghes_proc_in_irq
);
1056 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1057 static inline void ghes_nmi_add(struct ghes
*ghes
)
1059 pr_err(GHES_PFX
"ID: %d, trying to add NMI notification which is not supported!\n",
1060 ghes
->generic
->header
.source_id
);
1064 static inline void ghes_nmi_remove(struct ghes
*ghes
)
1066 pr_err(GHES_PFX
"ID: %d, trying to remove NMI notification which is not supported!\n",
1067 ghes
->generic
->header
.source_id
);
1071 static inline void ghes_nmi_init_cxt(void)
1074 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1076 static int ghes_probe(struct platform_device
*ghes_dev
)
1078 struct acpi_hest_generic
*generic
;
1079 struct ghes
*ghes
= NULL
;
1083 generic
= *(struct acpi_hest_generic
**)ghes_dev
->dev
.platform_data
;
1084 if (!generic
->enabled
)
1087 switch (generic
->notify
.type
) {
1088 case ACPI_HEST_NOTIFY_POLLED
:
1089 case ACPI_HEST_NOTIFY_EXTERNAL
:
1090 case ACPI_HEST_NOTIFY_SCI
:
1092 case ACPI_HEST_NOTIFY_SEA
:
1093 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA
)) {
1094 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via SEA is not supported\n",
1095 generic
->header
.source_id
);
1100 case ACPI_HEST_NOTIFY_NMI
:
1101 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI
)) {
1102 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1103 generic
->header
.source_id
);
1107 case ACPI_HEST_NOTIFY_LOCAL
:
1108 pr_warning(GHES_PFX
"Generic hardware error source: %d notified via local interrupt is not supported!\n",
1109 generic
->header
.source_id
);
1112 pr_warning(FW_WARN GHES_PFX
"Unknown notification type: %u for generic hardware error source: %d\n",
1113 generic
->notify
.type
, generic
->header
.source_id
);
1118 if (generic
->error_block_length
<
1119 sizeof(struct acpi_hest_generic_status
)) {
1120 pr_warning(FW_BUG GHES_PFX
"Invalid error block length: %u for generic hardware error source: %d\n",
1121 generic
->error_block_length
,
1122 generic
->header
.source_id
);
1125 ghes
= ghes_new(generic
);
1132 rc
= ghes_edac_register(ghes
, &ghes_dev
->dev
);
1136 switch (generic
->notify
.type
) {
1137 case ACPI_HEST_NOTIFY_POLLED
:
1138 ghes
->timer
.function
= ghes_poll_func
;
1139 ghes
->timer
.data
= (unsigned long)ghes
;
1140 init_timer_deferrable(&ghes
->timer
);
1141 ghes_add_timer(ghes
);
1143 case ACPI_HEST_NOTIFY_EXTERNAL
:
1144 /* External interrupt vector is GSI */
1145 rc
= acpi_gsi_to_irq(generic
->notify
.vector
, &ghes
->irq
);
1147 pr_err(GHES_PFX
"Failed to map GSI to IRQ for generic hardware error source: %d\n",
1148 generic
->header
.source_id
);
1149 goto err_edac_unreg
;
1151 rc
= request_irq(ghes
->irq
, ghes_irq_func
, 0, "GHES IRQ", ghes
);
1153 pr_err(GHES_PFX
"Failed to register IRQ for generic hardware error source: %d\n",
1154 generic
->header
.source_id
);
1155 goto err_edac_unreg
;
1158 case ACPI_HEST_NOTIFY_SCI
:
1159 mutex_lock(&ghes_list_mutex
);
1160 if (list_empty(&ghes_sci
))
1161 register_acpi_hed_notifier(&ghes_notifier_sci
);
1162 list_add_rcu(&ghes
->list
, &ghes_sci
);
1163 mutex_unlock(&ghes_list_mutex
);
1165 case ACPI_HEST_NOTIFY_SEA
:
1168 case ACPI_HEST_NOTIFY_NMI
:
1174 platform_set_drvdata(ghes_dev
, ghes
);
1178 ghes_edac_unregister(ghes
);
1187 static int ghes_remove(struct platform_device
*ghes_dev
)
1190 struct acpi_hest_generic
*generic
;
1192 ghes
= platform_get_drvdata(ghes_dev
);
1193 generic
= ghes
->generic
;
1195 ghes
->flags
|= GHES_EXITING
;
1196 switch (generic
->notify
.type
) {
1197 case ACPI_HEST_NOTIFY_POLLED
:
1198 del_timer_sync(&ghes
->timer
);
1200 case ACPI_HEST_NOTIFY_EXTERNAL
:
1201 free_irq(ghes
->irq
, ghes
);
1203 case ACPI_HEST_NOTIFY_SCI
:
1204 mutex_lock(&ghes_list_mutex
);
1205 list_del_rcu(&ghes
->list
);
1206 if (list_empty(&ghes_sci
))
1207 unregister_acpi_hed_notifier(&ghes_notifier_sci
);
1208 mutex_unlock(&ghes_list_mutex
);
1210 case ACPI_HEST_NOTIFY_SEA
:
1211 ghes_sea_remove(ghes
);
1213 case ACPI_HEST_NOTIFY_NMI
:
1214 ghes_nmi_remove(ghes
);
1223 ghes_edac_unregister(ghes
);
1227 platform_set_drvdata(ghes_dev
, NULL
);
1232 static struct platform_driver ghes_platform_driver
= {
1236 .probe
= ghes_probe
,
1237 .remove
= ghes_remove
,
1240 static int __init
ghes_init(void)
1248 pr_info(GHES_PFX
"HEST is not enabled!\n");
1253 pr_info(GHES_PFX
"GHES is not enabled!\n");
1257 ghes_nmi_init_cxt();
1259 rc
= ghes_ioremap_init();
1263 rc
= ghes_estatus_pool_init();
1265 goto err_ioremap_exit
;
1267 rc
= ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE
*
1268 GHES_ESTATUS_CACHE_ALLOCED_MAX
);
1272 rc
= platform_driver_register(&ghes_platform_driver
);
1276 rc
= apei_osc_setup();
1277 if (rc
== 0 && osc_sb_apei_support_acked
)
1278 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1279 else if (rc
== 0 && !osc_sb_apei_support_acked
)
1280 pr_info(GHES_PFX
"APEI firmware first mode is enabled by WHEA _OSC.\n");
1281 else if (rc
&& osc_sb_apei_support_acked
)
1282 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit.\n");
1284 pr_info(GHES_PFX
"Failed to enable APEI firmware first mode.\n");
1288 ghes_estatus_pool_exit();
1290 ghes_ioremap_exit();
1294 device_initcall(ghes_init
);