2 * APEI Generic Hardware Error Source support
4 * Generic Hardware Error Source provides a way to report platform
5 * hardware errors (such as that from chipset). It works in so called
6 * "Firmware First" mode, that is, hardware errors are reported to
7 * firmware firstly, then reported to Linux by firmware. This way,
8 * some non-standard hardware error registers or non-standard hardware
9 * link can be checked by firmware to produce more hardware error
10 * information for Linux.
12 * For more information about Generic Hardware Error Source, please
13 * refer to ACPI Specification version 4.0, section 17.3.2.6
15 * Copyright 2010,2011 Intel Corp.
16 * Author: Huang Ying <ying.huang@intel.com>
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License version
20 * 2 as published by the Free Software Foundation;
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
28 #include <linux/kernel.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/acpi.h>
33 #include <linux/interrupt.h>
34 #include <linux/timer.h>
35 #include <linux/cper.h>
36 #include <linux/platform_device.h>
37 #include <linux/mutex.h>
38 #include <linux/ratelimit.h>
39 #include <linux/vmalloc.h>
40 #include <linux/irq_work.h>
41 #include <linux/llist.h>
42 #include <linux/genalloc.h>
43 #include <linux/pci.h>
44 #include <linux/aer.h>
45 #include <linux/nmi.h>
46 #include <linux/sched/clock.h>
47 #include <linux/uuid.h>
48 #include <linux/ras.h>
50 #include <acpi/actbl1.h>
51 #include <acpi/ghes.h>
52 #include <acpi/apei.h>
53 #include <asm/fixmap.h>
54 #include <asm/tlbflush.h>
55 #include <ras/ras_event.h>
57 #include "apei-internal.h"
59 #define GHES_PFX "GHES: "
61 #define GHES_ESTATUS_MAX_SIZE 65536
62 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
64 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
66 /* This is just an estimation for memory pool allocation */
67 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
69 #define GHES_ESTATUS_CACHES_SIZE 4
71 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
72 /* Prevent too many caches are allocated because of RCU */
73 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
75 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
76 (sizeof(struct ghes_estatus_cache) + (estatus_len))
77 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
78 ((struct acpi_hest_generic_status *) \
79 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
81 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
82 (sizeof(struct ghes_estatus_node) + (estatus_len))
83 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
84 ((struct acpi_hest_generic_status *) \
85 ((struct ghes_estatus_node *)(estatus_node) + 1))
87 static inline bool is_hest_type_generic_v2(struct ghes
*ghes
)
89 return ghes
->generic
->header
.type
== ACPI_HEST_TYPE_GENERIC_ERROR_V2
;
93 * This driver isn't really modular, however for the time being,
94 * continuing to use module_param is the easiest way to remain
95 * compatible with existing boot arg use cases.
98 module_param_named(disable
, ghes_disable
, bool, 0);
101 * All error sources notified with HED (Hardware Error Device) share a
102 * single notifier callback, so they need to be linked and checked one
103 * by one. This holds true for NMI too.
105 * RCU is used for these lists, so ghes_list_mutex is only used for
106 * list changing, not for traversing.
108 static LIST_HEAD(ghes_hed
);
109 static DEFINE_MUTEX(ghes_list_mutex
);
112 * Because the memory area used to transfer hardware error information
113 * from BIOS to Linux can be determined only in NMI, IRQ or timer
114 * handler, but general ioremap can not be used in atomic context, so
115 * the fixmap is used instead.
117 * These 2 spinlocks are used to prevent the fixmap entries from being used
120 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi
);
121 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq
);
123 static struct gen_pool
*ghes_estatus_pool
;
124 static unsigned long ghes_estatus_pool_size_request
;
126 static struct ghes_estatus_cache
*ghes_estatus_caches
[GHES_ESTATUS_CACHES_SIZE
];
127 static atomic_t ghes_estatus_cache_alloced
;
129 static int ghes_panic_timeout __read_mostly
= 30;
131 static void __iomem
*ghes_ioremap_pfn_nmi(u64 pfn
)
136 paddr
= pfn
<< PAGE_SHIFT
;
137 prot
= arch_apei_get_mem_attribute(paddr
);
138 __set_fixmap(FIX_APEI_GHES_NMI
, paddr
, prot
);
140 return (void __iomem
*) fix_to_virt(FIX_APEI_GHES_NMI
);
143 static void __iomem
*ghes_ioremap_pfn_irq(u64 pfn
)
148 paddr
= pfn
<< PAGE_SHIFT
;
149 prot
= arch_apei_get_mem_attribute(paddr
);
150 __set_fixmap(FIX_APEI_GHES_IRQ
, paddr
, prot
);
152 return (void __iomem
*) fix_to_virt(FIX_APEI_GHES_IRQ
);
155 static void ghes_iounmap_nmi(void)
157 clear_fixmap(FIX_APEI_GHES_NMI
);
160 static void ghes_iounmap_irq(void)
162 clear_fixmap(FIX_APEI_GHES_IRQ
);
165 static int ghes_estatus_pool_init(void)
167 ghes_estatus_pool
= gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER
, -1);
168 if (!ghes_estatus_pool
)
173 static void ghes_estatus_pool_free_chunk(struct gen_pool
*pool
,
174 struct gen_pool_chunk
*chunk
,
177 vfree((void *)chunk
->start_addr
);
180 static void ghes_estatus_pool_exit(void)
182 gen_pool_for_each_chunk(ghes_estatus_pool
,
183 ghes_estatus_pool_free_chunk
, NULL
);
184 gen_pool_destroy(ghes_estatus_pool
);
187 static int ghes_estatus_pool_expand(unsigned long len
)
189 unsigned long size
, addr
;
191 ghes_estatus_pool_size_request
+= PAGE_ALIGN(len
);
192 size
= gen_pool_size(ghes_estatus_pool
);
193 if (size
>= ghes_estatus_pool_size_request
)
196 addr
= (unsigned long)vmalloc(PAGE_ALIGN(len
));
201 * New allocation must be visible in all pgd before it can be found by
202 * an NMI allocating from the pool.
204 vmalloc_sync_mappings();
206 return gen_pool_add(ghes_estatus_pool
, addr
, PAGE_ALIGN(len
), -1);
209 static int map_gen_v2(struct ghes
*ghes
)
211 return apei_map_generic_address(&ghes
->generic_v2
->read_ack_register
);
214 static void unmap_gen_v2(struct ghes
*ghes
)
216 apei_unmap_generic_address(&ghes
->generic_v2
->read_ack_register
);
219 static struct ghes
*ghes_new(struct acpi_hest_generic
*generic
)
222 unsigned int error_block_length
;
225 ghes
= kzalloc(sizeof(*ghes
), GFP_KERNEL
);
227 return ERR_PTR(-ENOMEM
);
229 ghes
->generic
= generic
;
230 if (is_hest_type_generic_v2(ghes
)) {
231 rc
= map_gen_v2(ghes
);
236 rc
= apei_map_generic_address(&generic
->error_status_address
);
238 goto err_unmap_read_ack_addr
;
239 error_block_length
= generic
->error_block_length
;
240 if (error_block_length
> GHES_ESTATUS_MAX_SIZE
) {
241 pr_warning(FW_WARN GHES_PFX
242 "Error status block length is too long: %u for "
243 "generic hardware error source: %d.\n",
244 error_block_length
, generic
->header
.source_id
);
245 error_block_length
= GHES_ESTATUS_MAX_SIZE
;
247 ghes
->estatus
= kmalloc(error_block_length
, GFP_KERNEL
);
248 if (!ghes
->estatus
) {
250 goto err_unmap_status_addr
;
255 err_unmap_status_addr
:
256 apei_unmap_generic_address(&generic
->error_status_address
);
257 err_unmap_read_ack_addr
:
258 if (is_hest_type_generic_v2(ghes
))
265 static void ghes_fini(struct ghes
*ghes
)
267 kfree(ghes
->estatus
);
268 apei_unmap_generic_address(&ghes
->generic
->error_status_address
);
269 if (is_hest_type_generic_v2(ghes
))
273 static inline int ghes_severity(int severity
)
276 case CPER_SEV_INFORMATIONAL
:
278 case CPER_SEV_CORRECTED
:
279 return GHES_SEV_CORRECTED
;
280 case CPER_SEV_RECOVERABLE
:
281 return GHES_SEV_RECOVERABLE
;
283 return GHES_SEV_PANIC
;
285 /* Unknown, go panic */
286 return GHES_SEV_PANIC
;
290 static void ghes_copy_tofrom_phys(void *buffer
, u64 paddr
, u32 len
,
294 unsigned long flags
= 0;
295 int in_nmi
= in_nmi();
300 offset
= paddr
- (paddr
& PAGE_MASK
);
302 raw_spin_lock(&ghes_ioremap_lock_nmi
);
303 vaddr
= ghes_ioremap_pfn_nmi(paddr
>> PAGE_SHIFT
);
305 spin_lock_irqsave(&ghes_ioremap_lock_irq
, flags
);
306 vaddr
= ghes_ioremap_pfn_irq(paddr
>> PAGE_SHIFT
);
308 trunk
= PAGE_SIZE
- offset
;
309 trunk
= min(trunk
, len
);
311 memcpy_fromio(buffer
, vaddr
+ offset
, trunk
);
313 memcpy_toio(vaddr
+ offset
, buffer
, trunk
);
319 raw_spin_unlock(&ghes_ioremap_lock_nmi
);
322 spin_unlock_irqrestore(&ghes_ioremap_lock_irq
, flags
);
327 static int ghes_read_estatus(struct ghes
*ghes
, int silent
)
329 struct acpi_hest_generic
*g
= ghes
->generic
;
334 rc
= apei_read(&buf_paddr
, &g
->error_status_address
);
336 if (!silent
&& printk_ratelimit())
337 pr_warning(FW_WARN GHES_PFX
338 "Failed to read error status block address for hardware error source: %d.\n",
339 g
->header
.source_id
);
345 ghes_copy_tofrom_phys(ghes
->estatus
, buf_paddr
,
346 sizeof(*ghes
->estatus
), 1);
347 if (!ghes
->estatus
->block_status
)
350 ghes
->buffer_paddr
= buf_paddr
;
351 ghes
->flags
|= GHES_TO_CLEAR
;
354 len
= cper_estatus_len(ghes
->estatus
);
355 if (len
< sizeof(*ghes
->estatus
))
357 if (len
> ghes
->generic
->error_block_length
)
359 if (cper_estatus_check_header(ghes
->estatus
))
361 ghes_copy_tofrom_phys(ghes
->estatus
+ 1,
362 buf_paddr
+ sizeof(*ghes
->estatus
),
363 len
- sizeof(*ghes
->estatus
), 1);
364 if (cper_estatus_check(ghes
->estatus
))
369 if (rc
&& !silent
&& printk_ratelimit())
370 pr_warning(FW_WARN GHES_PFX
371 "Failed to read error status block!\n");
375 static void ghes_clear_estatus(struct ghes
*ghes
)
377 ghes
->estatus
->block_status
= 0;
378 if (!(ghes
->flags
& GHES_TO_CLEAR
))
380 ghes_copy_tofrom_phys(ghes
->estatus
, ghes
->buffer_paddr
,
381 sizeof(ghes
->estatus
->block_status
), 0);
382 ghes
->flags
&= ~GHES_TO_CLEAR
;
385 static void ghes_handle_memory_failure(struct acpi_hest_generic_data
*gdata
, int sev
)
387 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
390 int sec_sev
= ghes_severity(gdata
->error_severity
);
391 struct cper_sec_mem_err
*mem_err
= acpi_hest_get_payload(gdata
);
393 if (!(mem_err
->validation_bits
& CPER_MEM_VALID_PA
))
396 pfn
= mem_err
->physical_addr
>> PAGE_SHIFT
;
397 if (!pfn_valid(pfn
)) {
398 pr_warn_ratelimited(FW_WARN GHES_PFX
399 "Invalid address in generic error data: %#llx\n",
400 mem_err
->physical_addr
);
404 /* iff following two events can be handled properly by now */
405 if (sec_sev
== GHES_SEV_CORRECTED
&&
406 (gdata
->flags
& CPER_SEC_ERROR_THRESHOLD_EXCEEDED
))
407 flags
= MF_SOFT_OFFLINE
;
408 if (sev
== GHES_SEV_RECOVERABLE
&& sec_sev
== GHES_SEV_RECOVERABLE
)
412 memory_failure_queue(pfn
, flags
);
417 * PCIe AER errors need to be sent to the AER driver for reporting and
418 * recovery. The GHES severities map to the following AER severities and
419 * require the following handling:
421 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
422 * These need to be reported by the AER driver but no recovery is
424 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
425 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
426 * These both need to be reported and recovered from by the AER driver.
427 * GHES_SEV_PANIC does not make it to this handling since the kernel must
430 static void ghes_handle_aer(struct acpi_hest_generic_data
*gdata
)
432 #ifdef CONFIG_ACPI_APEI_PCIEAER
433 struct cper_sec_pcie
*pcie_err
= acpi_hest_get_payload(gdata
);
435 if (pcie_err
->validation_bits
& CPER_PCIE_VALID_DEVICE_ID
&&
436 pcie_err
->validation_bits
& CPER_PCIE_VALID_AER_INFO
) {
440 devfn
= PCI_DEVFN(pcie_err
->device_id
.device
,
441 pcie_err
->device_id
.function
);
442 aer_severity
= cper_severity_to_aer(gdata
->error_severity
);
445 * If firmware reset the component to contain
446 * the error, we must reinitialize it before
447 * use, so treat it as a fatal AER error.
449 if (gdata
->flags
& CPER_SEC_RESET
)
450 aer_severity
= AER_FATAL
;
452 aer_recover_queue(pcie_err
->device_id
.segment
,
453 pcie_err
->device_id
.bus
,
455 (struct aer_capability_regs
*)
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(sev
, mem_err
);
485 arch_apei_report_mem_error(sev
, mem_err
);
486 ghes_handle_memory_failure(gdata
, sev
);
488 else if (guid_equal(sec_type
, &CPER_SEC_PCIE
)) {
489 ghes_handle_aer(gdata
);
491 else if (guid_equal(sec_type
, &CPER_SEC_PROC_ARM
)) {
492 struct cper_sec_proc_arm
*err
= acpi_hest_get_payload(gdata
);
494 log_arm_hw_error(err
);
496 void *err
= acpi_hest_get_payload(gdata
);
498 log_non_standard_event(sec_type
, fru_id
, fru_text
,
500 gdata
->error_data_length
);
505 static void __ghes_print_estatus(const char *pfx
,
506 const struct acpi_hest_generic
*generic
,
507 const struct acpi_hest_generic_status
*estatus
)
509 static atomic_t seqno
;
510 unsigned int curr_seqno
;
514 if (ghes_severity(estatus
->error_severity
) <=
520 curr_seqno
= atomic_inc_return(&seqno
);
521 snprintf(pfx_seq
, sizeof(pfx_seq
), "%s{%u}" HW_ERR
, pfx
, curr_seqno
);
522 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
523 pfx_seq
, generic
->header
.source_id
);
524 cper_estatus_print(pfx_seq
, estatus
);
527 static int ghes_print_estatus(const char *pfx
,
528 const struct acpi_hest_generic
*generic
,
529 const struct acpi_hest_generic_status
*estatus
)
531 /* Not more than 2 messages every 5 seconds */
532 static DEFINE_RATELIMIT_STATE(ratelimit_corrected
, 5*HZ
, 2);
533 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected
, 5*HZ
, 2);
534 struct ratelimit_state
*ratelimit
;
536 if (ghes_severity(estatus
->error_severity
) <= GHES_SEV_CORRECTED
)
537 ratelimit
= &ratelimit_corrected
;
539 ratelimit
= &ratelimit_uncorrected
;
540 if (__ratelimit(ratelimit
)) {
541 __ghes_print_estatus(pfx
, generic
, estatus
);
548 * GHES error status reporting throttle, to report more kinds of
549 * errors, instead of just most frequently occurred errors.
551 static int ghes_estatus_cached(struct acpi_hest_generic_status
*estatus
)
555 unsigned long long now
;
556 struct ghes_estatus_cache
*cache
;
557 struct acpi_hest_generic_status
*cache_estatus
;
559 len
= cper_estatus_len(estatus
);
561 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
562 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
565 if (len
!= cache
->estatus_len
)
567 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
568 if (memcmp(estatus
, cache_estatus
, len
))
570 atomic_inc(&cache
->count
);
572 if (now
- cache
->time_in
< GHES_ESTATUS_IN_CACHE_MAX_NSEC
)
580 static struct ghes_estatus_cache
*ghes_estatus_cache_alloc(
581 struct acpi_hest_generic
*generic
,
582 struct acpi_hest_generic_status
*estatus
)
586 struct ghes_estatus_cache
*cache
;
587 struct acpi_hest_generic_status
*cache_estatus
;
589 alloced
= atomic_add_return(1, &ghes_estatus_cache_alloced
);
590 if (alloced
> GHES_ESTATUS_CACHE_ALLOCED_MAX
) {
591 atomic_dec(&ghes_estatus_cache_alloced
);
594 len
= cper_estatus_len(estatus
);
595 cache_len
= GHES_ESTATUS_CACHE_LEN(len
);
596 cache
= (void *)gen_pool_alloc(ghes_estatus_pool
, cache_len
);
598 atomic_dec(&ghes_estatus_cache_alloced
);
601 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
602 memcpy(cache_estatus
, estatus
, len
);
603 cache
->estatus_len
= len
;
604 atomic_set(&cache
->count
, 0);
605 cache
->generic
= generic
;
606 cache
->time_in
= sched_clock();
610 static void ghes_estatus_cache_free(struct ghes_estatus_cache
*cache
)
614 len
= cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache
));
615 len
= GHES_ESTATUS_CACHE_LEN(len
);
616 gen_pool_free(ghes_estatus_pool
, (unsigned long)cache
, len
);
617 atomic_dec(&ghes_estatus_cache_alloced
);
620 static void ghes_estatus_cache_rcu_free(struct rcu_head
*head
)
622 struct ghes_estatus_cache
*cache
;
624 cache
= container_of(head
, struct ghes_estatus_cache
, rcu
);
625 ghes_estatus_cache_free(cache
);
628 static void ghes_estatus_cache_add(
629 struct acpi_hest_generic
*generic
,
630 struct acpi_hest_generic_status
*estatus
)
632 int i
, slot
= -1, count
;
633 unsigned long long now
, duration
, period
, max_period
= 0;
634 struct ghes_estatus_cache
*cache
, *slot_cache
= NULL
, *new_cache
;
636 new_cache
= ghes_estatus_cache_alloc(generic
, estatus
);
637 if (new_cache
== NULL
)
641 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
642 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
648 duration
= now
- cache
->time_in
;
649 if (duration
>= GHES_ESTATUS_IN_CACHE_MAX_NSEC
) {
654 count
= atomic_read(&cache
->count
);
656 do_div(period
, (count
+ 1));
657 if (period
> max_period
) {
663 /* new_cache must be put into array after its contents are written */
665 if (slot
!= -1 && cmpxchg(ghes_estatus_caches
+ slot
,
666 slot_cache
, new_cache
) == slot_cache
) {
668 call_rcu(&slot_cache
->rcu
, ghes_estatus_cache_rcu_free
);
670 ghes_estatus_cache_free(new_cache
);
674 static int ghes_ack_error(struct acpi_hest_generic_v2
*gv2
)
679 rc
= apei_read(&val
, &gv2
->read_ack_register
);
683 val
&= gv2
->read_ack_preserve
<< gv2
->read_ack_register
.bit_offset
;
684 val
|= gv2
->read_ack_write
<< gv2
->read_ack_register
.bit_offset
;
686 return apei_write(val
, &gv2
->read_ack_register
);
689 static void __ghes_panic(struct ghes
*ghes
)
691 __ghes_print_estatus(KERN_EMERG
, ghes
->generic
, ghes
->estatus
);
693 ghes_clear_estatus(ghes
);
695 /* reboot to log the error! */
697 panic_timeout
= ghes_panic_timeout
;
698 panic("Fatal hardware error!");
701 static int ghes_proc(struct ghes
*ghes
)
705 rc
= ghes_read_estatus(ghes
, 0);
709 if (ghes_severity(ghes
->estatus
->error_severity
) >= GHES_SEV_PANIC
) {
713 if (!ghes_estatus_cached(ghes
->estatus
)) {
714 if (ghes_print_estatus(NULL
, ghes
->generic
, ghes
->estatus
))
715 ghes_estatus_cache_add(ghes
->generic
, ghes
->estatus
);
717 ghes_do_proc(ghes
, ghes
->estatus
);
720 ghes_clear_estatus(ghes
);
726 * GHESv2 type HEST entries introduce support for error acknowledgment,
727 * so only acknowledge the error if this support is present.
729 if (is_hest_type_generic_v2(ghes
))
730 return ghes_ack_error(ghes
->generic_v2
);
735 static void ghes_add_timer(struct ghes
*ghes
)
737 struct acpi_hest_generic
*g
= ghes
->generic
;
738 unsigned long expire
;
740 if (!g
->notify
.poll_interval
) {
741 pr_warning(FW_WARN GHES_PFX
"Poll interval is 0 for generic hardware error source: %d, disabled.\n",
742 g
->header
.source_id
);
745 expire
= jiffies
+ msecs_to_jiffies(g
->notify
.poll_interval
);
746 ghes
->timer
.expires
= round_jiffies_relative(expire
);
747 add_timer(&ghes
->timer
);
750 static void ghes_poll_func(struct timer_list
*t
)
752 struct ghes
*ghes
= from_timer(ghes
, t
, timer
);
755 if (!(ghes
->flags
& GHES_EXITING
))
756 ghes_add_timer(ghes
);
759 static irqreturn_t
ghes_irq_func(int irq
, void *data
)
761 struct ghes
*ghes
= data
;
764 rc
= ghes_proc(ghes
);
771 static int ghes_notify_hed(struct notifier_block
*this, unsigned long event
,
775 int ret
= NOTIFY_DONE
;
778 list_for_each_entry_rcu(ghes
, &ghes_hed
, list
) {
779 if (!ghes_proc(ghes
))
787 static struct notifier_block ghes_notifier_hed
= {
788 .notifier_call
= ghes_notify_hed
,
791 #ifdef CONFIG_ACPI_APEI_SEA
792 static LIST_HEAD(ghes_sea
);
795 * Return 0 only if one of the SEA error sources successfully reported an error
796 * record sent from the firmware.
798 int ghes_notify_sea(void)
804 list_for_each_entry_rcu(ghes
, &ghes_sea
, list
) {
805 if (!ghes_proc(ghes
))
812 static void ghes_sea_add(struct ghes
*ghes
)
814 mutex_lock(&ghes_list_mutex
);
815 list_add_rcu(&ghes
->list
, &ghes_sea
);
816 mutex_unlock(&ghes_list_mutex
);
819 static void ghes_sea_remove(struct ghes
*ghes
)
821 mutex_lock(&ghes_list_mutex
);
822 list_del_rcu(&ghes
->list
);
823 mutex_unlock(&ghes_list_mutex
);
826 #else /* CONFIG_ACPI_APEI_SEA */
827 static inline void ghes_sea_add(struct ghes
*ghes
) { }
828 static inline void ghes_sea_remove(struct ghes
*ghes
) { }
829 #endif /* CONFIG_ACPI_APEI_SEA */
831 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
833 * printk is not safe in NMI context. So in NMI handler, we allocate
834 * required memory from lock-less memory allocator
835 * (ghes_estatus_pool), save estatus into it, put them into lock-less
836 * list (ghes_estatus_llist), then delay printk into IRQ context via
837 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
838 * required pool size by all NMI error source.
840 static struct llist_head ghes_estatus_llist
;
841 static struct irq_work ghes_proc_irq_work
;
844 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
845 * having only one concurrent reader.
847 static atomic_t ghes_in_nmi
= ATOMIC_INIT(0);
849 static LIST_HEAD(ghes_nmi
);
851 static void ghes_proc_in_irq(struct irq_work
*irq_work
)
853 struct llist_node
*llnode
, *next
;
854 struct ghes_estatus_node
*estatus_node
;
855 struct acpi_hest_generic
*generic
;
856 struct acpi_hest_generic_status
*estatus
;
859 llnode
= llist_del_all(&ghes_estatus_llist
);
861 * Because the time order of estatus in list is reversed,
862 * revert it back to proper order.
864 llnode
= llist_reverse_order(llnode
);
867 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
869 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
870 len
= cper_estatus_len(estatus
);
871 node_len
= GHES_ESTATUS_NODE_LEN(len
);
872 ghes_do_proc(estatus_node
->ghes
, estatus
);
873 if (!ghes_estatus_cached(estatus
)) {
874 generic
= estatus_node
->generic
;
875 if (ghes_print_estatus(NULL
, generic
, estatus
))
876 ghes_estatus_cache_add(generic
, estatus
);
878 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
884 static void ghes_print_queued_estatus(void)
886 struct llist_node
*llnode
;
887 struct ghes_estatus_node
*estatus_node
;
888 struct acpi_hest_generic
*generic
;
889 struct acpi_hest_generic_status
*estatus
;
891 llnode
= llist_del_all(&ghes_estatus_llist
);
893 * Because the time order of estatus in list is reversed,
894 * revert it back to proper order.
896 llnode
= llist_reverse_order(llnode
);
898 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
900 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
901 generic
= estatus_node
->generic
;
902 ghes_print_estatus(NULL
, generic
, estatus
);
903 llnode
= llnode
->next
;
907 /* Save estatus for further processing in IRQ context */
908 static void __process_error(struct ghes
*ghes
)
910 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
912 struct ghes_estatus_node
*estatus_node
;
913 struct acpi_hest_generic_status
*estatus
;
915 if (ghes_estatus_cached(ghes
->estatus
))
918 len
= cper_estatus_len(ghes
->estatus
);
919 node_len
= GHES_ESTATUS_NODE_LEN(len
);
921 estatus_node
= (void *)gen_pool_alloc(ghes_estatus_pool
, node_len
);
925 estatus_node
->ghes
= ghes
;
926 estatus_node
->generic
= ghes
->generic
;
927 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
928 memcpy(estatus
, ghes
->estatus
, len
);
929 llist_add(&estatus_node
->llnode
, &ghes_estatus_llist
);
933 static int ghes_notify_nmi(unsigned int cmd
, struct pt_regs
*regs
)
936 int sev
, ret
= NMI_DONE
;
938 if (!atomic_add_unless(&ghes_in_nmi
, 1, 1))
941 list_for_each_entry_rcu(ghes
, &ghes_nmi
, list
) {
942 if (ghes_read_estatus(ghes
, 1)) {
943 ghes_clear_estatus(ghes
);
949 sev
= ghes_severity(ghes
->estatus
->error_severity
);
950 if (sev
>= GHES_SEV_PANIC
) {
951 ghes_print_queued_estatus();
955 if (!(ghes
->flags
& GHES_TO_CLEAR
))
958 __process_error(ghes
);
959 ghes_clear_estatus(ghes
);
962 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
963 if (ret
== NMI_HANDLED
)
964 irq_work_queue(&ghes_proc_irq_work
);
966 atomic_dec(&ghes_in_nmi
);
970 static unsigned long ghes_esource_prealloc_size(
971 const struct acpi_hest_generic
*generic
)
973 unsigned long block_length
, prealloc_records
, prealloc_size
;
975 block_length
= min_t(unsigned long, generic
->error_block_length
,
976 GHES_ESTATUS_MAX_SIZE
);
977 prealloc_records
= max_t(unsigned long,
978 generic
->records_to_preallocate
, 1);
979 prealloc_size
= min_t(unsigned long, block_length
* prealloc_records
,
980 GHES_ESOURCE_PREALLOC_MAX_SIZE
);
982 return prealloc_size
;
985 static void ghes_estatus_pool_shrink(unsigned long len
)
987 ghes_estatus_pool_size_request
-= PAGE_ALIGN(len
);
990 static void ghes_nmi_add(struct ghes
*ghes
)
994 len
= ghes_esource_prealloc_size(ghes
->generic
);
995 ghes_estatus_pool_expand(len
);
996 mutex_lock(&ghes_list_mutex
);
997 if (list_empty(&ghes_nmi
))
998 register_nmi_handler(NMI_LOCAL
, ghes_notify_nmi
, 0, "ghes");
999 list_add_rcu(&ghes
->list
, &ghes_nmi
);
1000 mutex_unlock(&ghes_list_mutex
);
1003 static void ghes_nmi_remove(struct ghes
*ghes
)
1007 mutex_lock(&ghes_list_mutex
);
1008 list_del_rcu(&ghes
->list
);
1009 if (list_empty(&ghes_nmi
))
1010 unregister_nmi_handler(NMI_LOCAL
, "ghes");
1011 mutex_unlock(&ghes_list_mutex
);
1013 * To synchronize with NMI handler, ghes can only be
1014 * freed after NMI handler finishes.
1017 len
= ghes_esource_prealloc_size(ghes
->generic
);
1018 ghes_estatus_pool_shrink(len
);
1021 static void ghes_nmi_init_cxt(void)
1023 init_irq_work(&ghes_proc_irq_work
, ghes_proc_in_irq
);
1025 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1026 static inline void ghes_nmi_add(struct ghes
*ghes
) { }
1027 static inline void ghes_nmi_remove(struct ghes
*ghes
) { }
1028 static inline void ghes_nmi_init_cxt(void) { }
1029 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1031 static int ghes_probe(struct platform_device
*ghes_dev
)
1033 struct acpi_hest_generic
*generic
;
1034 struct ghes
*ghes
= NULL
;
1038 generic
= *(struct acpi_hest_generic
**)ghes_dev
->dev
.platform_data
;
1039 if (!generic
->enabled
)
1042 switch (generic
->notify
.type
) {
1043 case ACPI_HEST_NOTIFY_POLLED
:
1044 case ACPI_HEST_NOTIFY_EXTERNAL
:
1045 case ACPI_HEST_NOTIFY_SCI
:
1046 case ACPI_HEST_NOTIFY_GSIV
:
1047 case ACPI_HEST_NOTIFY_GPIO
:
1050 case ACPI_HEST_NOTIFY_SEA
:
1051 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA
)) {
1052 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via SEA is not supported\n",
1053 generic
->header
.source_id
);
1058 case ACPI_HEST_NOTIFY_NMI
:
1059 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI
)) {
1060 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1061 generic
->header
.source_id
);
1065 case ACPI_HEST_NOTIFY_LOCAL
:
1066 pr_warning(GHES_PFX
"Generic hardware error source: %d notified via local interrupt is not supported!\n",
1067 generic
->header
.source_id
);
1070 pr_warning(FW_WARN GHES_PFX
"Unknown notification type: %u for generic hardware error source: %d\n",
1071 generic
->notify
.type
, generic
->header
.source_id
);
1076 if (generic
->error_block_length
<
1077 sizeof(struct acpi_hest_generic_status
)) {
1078 pr_warning(FW_BUG GHES_PFX
"Invalid error block length: %u for generic hardware error source: %d\n",
1079 generic
->error_block_length
,
1080 generic
->header
.source_id
);
1083 ghes
= ghes_new(generic
);
1090 switch (generic
->notify
.type
) {
1091 case ACPI_HEST_NOTIFY_POLLED
:
1092 timer_setup(&ghes
->timer
, ghes_poll_func
, TIMER_DEFERRABLE
);
1093 ghes_add_timer(ghes
);
1095 case ACPI_HEST_NOTIFY_EXTERNAL
:
1096 /* External interrupt vector is GSI */
1097 rc
= acpi_gsi_to_irq(generic
->notify
.vector
, &ghes
->irq
);
1099 pr_err(GHES_PFX
"Failed to map GSI to IRQ for generic hardware error source: %d\n",
1100 generic
->header
.source_id
);
1103 rc
= request_irq(ghes
->irq
, ghes_irq_func
, IRQF_SHARED
,
1106 pr_err(GHES_PFX
"Failed to register IRQ for generic hardware error source: %d\n",
1107 generic
->header
.source_id
);
1112 case ACPI_HEST_NOTIFY_SCI
:
1113 case ACPI_HEST_NOTIFY_GSIV
:
1114 case ACPI_HEST_NOTIFY_GPIO
:
1115 mutex_lock(&ghes_list_mutex
);
1116 if (list_empty(&ghes_hed
))
1117 register_acpi_hed_notifier(&ghes_notifier_hed
);
1118 list_add_rcu(&ghes
->list
, &ghes_hed
);
1119 mutex_unlock(&ghes_list_mutex
);
1122 case ACPI_HEST_NOTIFY_SEA
:
1125 case ACPI_HEST_NOTIFY_NMI
:
1132 platform_set_drvdata(ghes_dev
, ghes
);
1134 ghes_edac_register(ghes
, &ghes_dev
->dev
);
1136 /* Handle any pending errors right away */
1149 static int ghes_remove(struct platform_device
*ghes_dev
)
1152 struct acpi_hest_generic
*generic
;
1154 ghes
= platform_get_drvdata(ghes_dev
);
1155 generic
= ghes
->generic
;
1157 ghes
->flags
|= GHES_EXITING
;
1158 switch (generic
->notify
.type
) {
1159 case ACPI_HEST_NOTIFY_POLLED
:
1160 del_timer_sync(&ghes
->timer
);
1162 case ACPI_HEST_NOTIFY_EXTERNAL
:
1163 free_irq(ghes
->irq
, ghes
);
1166 case ACPI_HEST_NOTIFY_SCI
:
1167 case ACPI_HEST_NOTIFY_GSIV
:
1168 case ACPI_HEST_NOTIFY_GPIO
:
1169 mutex_lock(&ghes_list_mutex
);
1170 list_del_rcu(&ghes
->list
);
1171 if (list_empty(&ghes_hed
))
1172 unregister_acpi_hed_notifier(&ghes_notifier_hed
);
1173 mutex_unlock(&ghes_list_mutex
);
1177 case ACPI_HEST_NOTIFY_SEA
:
1178 ghes_sea_remove(ghes
);
1180 case ACPI_HEST_NOTIFY_NMI
:
1181 ghes_nmi_remove(ghes
);
1190 ghes_edac_unregister(ghes
);
1194 platform_set_drvdata(ghes_dev
, NULL
);
1199 static struct platform_driver ghes_platform_driver
= {
1203 .probe
= ghes_probe
,
1204 .remove
= ghes_remove
,
1207 static int __init
ghes_init(void)
1214 switch (hest_disable
) {
1215 case HEST_NOT_FOUND
:
1218 pr_info(GHES_PFX
"HEST is not enabled!\n");
1225 pr_info(GHES_PFX
"GHES is not enabled!\n");
1229 ghes_nmi_init_cxt();
1231 rc
= ghes_estatus_pool_init();
1235 rc
= ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE
*
1236 GHES_ESTATUS_CACHE_ALLOCED_MAX
);
1240 rc
= platform_driver_register(&ghes_platform_driver
);
1244 rc
= apei_osc_setup();
1245 if (rc
== 0 && osc_sb_apei_support_acked
)
1246 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1247 else if (rc
== 0 && !osc_sb_apei_support_acked
)
1248 pr_info(GHES_PFX
"APEI firmware first mode is enabled by WHEA _OSC.\n");
1249 else if (rc
&& osc_sb_apei_support_acked
)
1250 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit.\n");
1252 pr_info(GHES_PFX
"Failed to enable APEI firmware first mode.\n");
1256 ghes_estatus_pool_exit();
1260 device_initcall(ghes_init
);