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>
49 #include <acpi/ghes.h>
50 #include <acpi/apei.h>
51 #include <asm/tlbflush.h>
53 #include "apei-internal.h"
55 #define GHES_PFX "GHES: "
57 #define GHES_ESTATUS_MAX_SIZE 65536
58 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
60 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
62 /* This is just an estimation for memory pool allocation */
63 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
65 #define GHES_ESTATUS_CACHES_SIZE 4
67 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
68 /* Prevent too many caches are allocated because of RCU */
69 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
71 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
72 (sizeof(struct ghes_estatus_cache) + (estatus_len))
73 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
74 ((struct acpi_hest_generic_status *) \
75 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
77 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
78 (sizeof(struct ghes_estatus_node) + (estatus_len))
79 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
80 ((struct acpi_hest_generic_status *) \
81 ((struct ghes_estatus_node *)(estatus_node) + 1))
84 * This driver isn't really modular, however for the time being,
85 * continuing to use module_param is the easiest way to remain
86 * compatible with existing boot arg use cases.
89 module_param_named(disable
, ghes_disable
, bool, 0);
92 * All error sources notified with SCI shares one notifier function,
93 * so they need to be linked and checked one by one. This is applied
96 * RCU is used for these lists, so ghes_list_mutex is only used for
97 * list changing, not for traversing.
99 static LIST_HEAD(ghes_sci
);
100 static DEFINE_MUTEX(ghes_list_mutex
);
103 * Because the memory area used to transfer hardware error information
104 * from BIOS to Linux can be determined only in NMI, IRQ or timer
105 * handler, but general ioremap can not be used in atomic context, so
106 * a special version of atomic ioremap is implemented for that.
110 * Two virtual pages are used, one for IRQ/PROCESS context, the other for
111 * NMI context (optionally).
113 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
114 #define GHES_IOREMAP_PAGES 2
116 #define GHES_IOREMAP_PAGES 1
118 #define GHES_IOREMAP_IRQ_PAGE(base) (base)
119 #define GHES_IOREMAP_NMI_PAGE(base) ((base) + PAGE_SIZE)
121 /* virtual memory area for atomic ioremap */
122 static struct vm_struct
*ghes_ioremap_area
;
124 * These 2 spinlock is used to prevent atomic ioremap virtual memory
125 * area from being mapped simultaneously.
127 static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi
);
128 static DEFINE_SPINLOCK(ghes_ioremap_lock_irq
);
130 static struct gen_pool
*ghes_estatus_pool
;
131 static unsigned long ghes_estatus_pool_size_request
;
133 static struct ghes_estatus_cache
*ghes_estatus_caches
[GHES_ESTATUS_CACHES_SIZE
];
134 static atomic_t ghes_estatus_cache_alloced
;
136 static int ghes_ioremap_init(void)
138 ghes_ioremap_area
= __get_vm_area(PAGE_SIZE
* GHES_IOREMAP_PAGES
,
139 VM_IOREMAP
, VMALLOC_START
, VMALLOC_END
);
140 if (!ghes_ioremap_area
) {
141 pr_err(GHES_PFX
"Failed to allocate virtual memory area for atomic ioremap.\n");
148 static void ghes_ioremap_exit(void)
150 free_vm_area(ghes_ioremap_area
);
153 static void __iomem
*ghes_ioremap_pfn_nmi(u64 pfn
)
157 vaddr
= (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area
->addr
);
158 ioremap_page_range(vaddr
, vaddr
+ PAGE_SIZE
,
159 pfn
<< PAGE_SHIFT
, PAGE_KERNEL
);
161 return (void __iomem
*)vaddr
;
164 static void __iomem
*ghes_ioremap_pfn_irq(u64 pfn
)
166 unsigned long vaddr
, paddr
;
169 vaddr
= (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area
->addr
);
171 paddr
= pfn
<< PAGE_SHIFT
;
172 prot
= arch_apei_get_mem_attribute(paddr
);
174 ioremap_page_range(vaddr
, vaddr
+ PAGE_SIZE
, paddr
, prot
);
176 return (void __iomem
*)vaddr
;
179 static void ghes_iounmap_nmi(void __iomem
*vaddr_ptr
)
181 unsigned long vaddr
= (unsigned long __force
)vaddr_ptr
;
182 void *base
= ghes_ioremap_area
->addr
;
184 BUG_ON(vaddr
!= (unsigned long)GHES_IOREMAP_NMI_PAGE(base
));
185 unmap_kernel_range_noflush(vaddr
, PAGE_SIZE
);
186 arch_apei_flush_tlb_one(vaddr
);
189 static void ghes_iounmap_irq(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_IRQ_PAGE(base
));
195 unmap_kernel_range_noflush(vaddr
, PAGE_SIZE
);
196 arch_apei_flush_tlb_one(vaddr
);
199 static int ghes_estatus_pool_init(void)
201 ghes_estatus_pool
= gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER
, -1);
202 if (!ghes_estatus_pool
)
207 static void ghes_estatus_pool_free_chunk_page(struct gen_pool
*pool
,
208 struct gen_pool_chunk
*chunk
,
211 free_page(chunk
->start_addr
);
214 static void ghes_estatus_pool_exit(void)
216 gen_pool_for_each_chunk(ghes_estatus_pool
,
217 ghes_estatus_pool_free_chunk_page
, NULL
);
218 gen_pool_destroy(ghes_estatus_pool
);
221 static int ghes_estatus_pool_expand(unsigned long len
)
223 unsigned long i
, pages
, size
, addr
;
226 ghes_estatus_pool_size_request
+= PAGE_ALIGN(len
);
227 size
= gen_pool_size(ghes_estatus_pool
);
228 if (size
>= ghes_estatus_pool_size_request
)
230 pages
= (ghes_estatus_pool_size_request
- size
) / PAGE_SIZE
;
231 for (i
= 0; i
< pages
; i
++) {
232 addr
= __get_free_page(GFP_KERNEL
);
235 ret
= gen_pool_add(ghes_estatus_pool
, addr
, PAGE_SIZE
, -1);
243 static struct ghes
*ghes_new(struct acpi_hest_generic
*generic
)
246 unsigned int error_block_length
;
249 ghes
= kzalloc(sizeof(*ghes
), GFP_KERNEL
);
251 return ERR_PTR(-ENOMEM
);
252 ghes
->generic
= generic
;
253 rc
= apei_map_generic_address(&generic
->error_status_address
);
256 error_block_length
= generic
->error_block_length
;
257 if (error_block_length
> GHES_ESTATUS_MAX_SIZE
) {
258 pr_warning(FW_WARN GHES_PFX
259 "Error status block length is too long: %u for "
260 "generic hardware error source: %d.\n",
261 error_block_length
, generic
->header
.source_id
);
262 error_block_length
= GHES_ESTATUS_MAX_SIZE
;
264 ghes
->estatus
= kmalloc(error_block_length
, GFP_KERNEL
);
265 if (!ghes
->estatus
) {
273 apei_unmap_generic_address(&generic
->error_status_address
);
279 static void ghes_fini(struct ghes
*ghes
)
281 kfree(ghes
->estatus
);
282 apei_unmap_generic_address(&ghes
->generic
->error_status_address
);
285 static inline int ghes_severity(int severity
)
288 case CPER_SEV_INFORMATIONAL
:
290 case CPER_SEV_CORRECTED
:
291 return GHES_SEV_CORRECTED
;
292 case CPER_SEV_RECOVERABLE
:
293 return GHES_SEV_RECOVERABLE
;
295 return GHES_SEV_PANIC
;
297 /* Unknown, go panic */
298 return GHES_SEV_PANIC
;
302 static void ghes_copy_tofrom_phys(void *buffer
, u64 paddr
, u32 len
,
306 unsigned long flags
= 0;
307 int in_nmi
= in_nmi();
312 offset
= paddr
- (paddr
& PAGE_MASK
);
314 raw_spin_lock(&ghes_ioremap_lock_nmi
);
315 vaddr
= ghes_ioremap_pfn_nmi(paddr
>> PAGE_SHIFT
);
317 spin_lock_irqsave(&ghes_ioremap_lock_irq
, flags
);
318 vaddr
= ghes_ioremap_pfn_irq(paddr
>> PAGE_SHIFT
);
320 trunk
= PAGE_SIZE
- offset
;
321 trunk
= min(trunk
, len
);
323 memcpy_fromio(buffer
, vaddr
+ offset
, trunk
);
325 memcpy_toio(vaddr
+ offset
, buffer
, trunk
);
330 ghes_iounmap_nmi(vaddr
);
331 raw_spin_unlock(&ghes_ioremap_lock_nmi
);
333 ghes_iounmap_irq(vaddr
);
334 spin_unlock_irqrestore(&ghes_ioremap_lock_irq
, flags
);
339 static int ghes_read_estatus(struct ghes
*ghes
, int silent
)
341 struct acpi_hest_generic
*g
= ghes
->generic
;
346 rc
= apei_read(&buf_paddr
, &g
->error_status_address
);
348 if (!silent
&& printk_ratelimit())
349 pr_warning(FW_WARN GHES_PFX
350 "Failed to read error status block address for hardware error source: %d.\n",
351 g
->header
.source_id
);
357 ghes_copy_tofrom_phys(ghes
->estatus
, buf_paddr
,
358 sizeof(*ghes
->estatus
), 1);
359 if (!ghes
->estatus
->block_status
)
362 ghes
->buffer_paddr
= buf_paddr
;
363 ghes
->flags
|= GHES_TO_CLEAR
;
366 len
= cper_estatus_len(ghes
->estatus
);
367 if (len
< sizeof(*ghes
->estatus
))
369 if (len
> ghes
->generic
->error_block_length
)
371 if (cper_estatus_check_header(ghes
->estatus
))
373 ghes_copy_tofrom_phys(ghes
->estatus
+ 1,
374 buf_paddr
+ sizeof(*ghes
->estatus
),
375 len
- sizeof(*ghes
->estatus
), 1);
376 if (cper_estatus_check(ghes
->estatus
))
381 if (rc
&& !silent
&& printk_ratelimit())
382 pr_warning(FW_WARN GHES_PFX
383 "Failed to read error status block!\n");
387 static void ghes_clear_estatus(struct ghes
*ghes
)
389 ghes
->estatus
->block_status
= 0;
390 if (!(ghes
->flags
& GHES_TO_CLEAR
))
392 ghes_copy_tofrom_phys(ghes
->estatus
, ghes
->buffer_paddr
,
393 sizeof(ghes
->estatus
->block_status
), 0);
394 ghes
->flags
&= ~GHES_TO_CLEAR
;
397 static void ghes_handle_memory_failure(struct acpi_hest_generic_data
*gdata
, int sev
)
399 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
402 int sec_sev
= ghes_severity(gdata
->error_severity
);
403 struct cper_sec_mem_err
*mem_err
;
404 mem_err
= (struct cper_sec_mem_err
*)(gdata
+ 1);
406 if (!(mem_err
->validation_bits
& CPER_MEM_VALID_PA
))
409 pfn
= mem_err
->physical_addr
>> PAGE_SHIFT
;
410 if (!pfn_valid(pfn
)) {
411 pr_warn_ratelimited(FW_WARN GHES_PFX
412 "Invalid address in generic error data: %#llx\n",
413 mem_err
->physical_addr
);
417 /* iff following two events can be handled properly by now */
418 if (sec_sev
== GHES_SEV_CORRECTED
&&
419 (gdata
->flags
& CPER_SEC_ERROR_THRESHOLD_EXCEEDED
))
420 flags
= MF_SOFT_OFFLINE
;
421 if (sev
== GHES_SEV_RECOVERABLE
&& sec_sev
== GHES_SEV_RECOVERABLE
)
425 memory_failure_queue(pfn
, 0, flags
);
429 static void ghes_do_proc(struct ghes
*ghes
,
430 const struct acpi_hest_generic_status
*estatus
)
433 struct acpi_hest_generic_data
*gdata
;
435 sev
= ghes_severity(estatus
->error_severity
);
436 apei_estatus_for_each_section(estatus
, gdata
) {
437 sec_sev
= ghes_severity(gdata
->error_severity
);
438 if (!uuid_le_cmp(*(uuid_le
*)gdata
->section_type
,
439 CPER_SEC_PLATFORM_MEM
)) {
440 struct cper_sec_mem_err
*mem_err
;
441 mem_err
= (struct cper_sec_mem_err
*)(gdata
+1);
442 ghes_edac_report_mem_error(ghes
, sev
, mem_err
);
444 arch_apei_report_mem_error(sev
, mem_err
);
445 ghes_handle_memory_failure(gdata
, sev
);
447 #ifdef CONFIG_ACPI_APEI_PCIEAER
448 else if (!uuid_le_cmp(*(uuid_le
*)gdata
->section_type
,
450 struct cper_sec_pcie
*pcie_err
;
451 pcie_err
= (struct cper_sec_pcie
*)(gdata
+1);
452 if (sev
== GHES_SEV_RECOVERABLE
&&
453 sec_sev
== GHES_SEV_RECOVERABLE
&&
454 pcie_err
->validation_bits
& CPER_PCIE_VALID_DEVICE_ID
&&
455 pcie_err
->validation_bits
& CPER_PCIE_VALID_AER_INFO
) {
459 devfn
= PCI_DEVFN(pcie_err
->device_id
.device
,
460 pcie_err
->device_id
.function
);
461 aer_severity
= cper_severity_to_aer(gdata
->error_severity
);
464 * If firmware reset the component to contain
465 * the error, we must reinitialize it before
466 * use, so treat it as a fatal AER error.
468 if (gdata
->flags
& CPER_SEC_RESET
)
469 aer_severity
= AER_FATAL
;
471 aer_recover_queue(pcie_err
->device_id
.segment
,
472 pcie_err
->device_id
.bus
,
474 (struct aer_capability_regs
*)
483 static void __ghes_print_estatus(const char *pfx
,
484 const struct acpi_hest_generic
*generic
,
485 const struct acpi_hest_generic_status
*estatus
)
487 static atomic_t seqno
;
488 unsigned int curr_seqno
;
492 if (ghes_severity(estatus
->error_severity
) <=
498 curr_seqno
= atomic_inc_return(&seqno
);
499 snprintf(pfx_seq
, sizeof(pfx_seq
), "%s{%u}" HW_ERR
, pfx
, curr_seqno
);
500 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
501 pfx_seq
, generic
->header
.source_id
);
502 cper_estatus_print(pfx_seq
, estatus
);
505 static int ghes_print_estatus(const char *pfx
,
506 const struct acpi_hest_generic
*generic
,
507 const struct acpi_hest_generic_status
*estatus
)
509 /* Not more than 2 messages every 5 seconds */
510 static DEFINE_RATELIMIT_STATE(ratelimit_corrected
, 5*HZ
, 2);
511 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected
, 5*HZ
, 2);
512 struct ratelimit_state
*ratelimit
;
514 if (ghes_severity(estatus
->error_severity
) <= GHES_SEV_CORRECTED
)
515 ratelimit
= &ratelimit_corrected
;
517 ratelimit
= &ratelimit_uncorrected
;
518 if (__ratelimit(ratelimit
)) {
519 __ghes_print_estatus(pfx
, generic
, estatus
);
526 * GHES error status reporting throttle, to report more kinds of
527 * errors, instead of just most frequently occurred errors.
529 static int ghes_estatus_cached(struct acpi_hest_generic_status
*estatus
)
533 unsigned long long now
;
534 struct ghes_estatus_cache
*cache
;
535 struct acpi_hest_generic_status
*cache_estatus
;
537 len
= cper_estatus_len(estatus
);
539 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
540 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
543 if (len
!= cache
->estatus_len
)
545 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
546 if (memcmp(estatus
, cache_estatus
, len
))
548 atomic_inc(&cache
->count
);
550 if (now
- cache
->time_in
< GHES_ESTATUS_IN_CACHE_MAX_NSEC
)
558 static struct ghes_estatus_cache
*ghes_estatus_cache_alloc(
559 struct acpi_hest_generic
*generic
,
560 struct acpi_hest_generic_status
*estatus
)
564 struct ghes_estatus_cache
*cache
;
565 struct acpi_hest_generic_status
*cache_estatus
;
567 alloced
= atomic_add_return(1, &ghes_estatus_cache_alloced
);
568 if (alloced
> GHES_ESTATUS_CACHE_ALLOCED_MAX
) {
569 atomic_dec(&ghes_estatus_cache_alloced
);
572 len
= cper_estatus_len(estatus
);
573 cache_len
= GHES_ESTATUS_CACHE_LEN(len
);
574 cache
= (void *)gen_pool_alloc(ghes_estatus_pool
, cache_len
);
576 atomic_dec(&ghes_estatus_cache_alloced
);
579 cache_estatus
= GHES_ESTATUS_FROM_CACHE(cache
);
580 memcpy(cache_estatus
, estatus
, len
);
581 cache
->estatus_len
= len
;
582 atomic_set(&cache
->count
, 0);
583 cache
->generic
= generic
;
584 cache
->time_in
= sched_clock();
588 static void ghes_estatus_cache_free(struct ghes_estatus_cache
*cache
)
592 len
= cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache
));
593 len
= GHES_ESTATUS_CACHE_LEN(len
);
594 gen_pool_free(ghes_estatus_pool
, (unsigned long)cache
, len
);
595 atomic_dec(&ghes_estatus_cache_alloced
);
598 static void ghes_estatus_cache_rcu_free(struct rcu_head
*head
)
600 struct ghes_estatus_cache
*cache
;
602 cache
= container_of(head
, struct ghes_estatus_cache
, rcu
);
603 ghes_estatus_cache_free(cache
);
606 static void ghes_estatus_cache_add(
607 struct acpi_hest_generic
*generic
,
608 struct acpi_hest_generic_status
*estatus
)
610 int i
, slot
= -1, count
;
611 unsigned long long now
, duration
, period
, max_period
= 0;
612 struct ghes_estatus_cache
*cache
, *slot_cache
= NULL
, *new_cache
;
614 new_cache
= ghes_estatus_cache_alloc(generic
, estatus
);
615 if (new_cache
== NULL
)
619 for (i
= 0; i
< GHES_ESTATUS_CACHES_SIZE
; i
++) {
620 cache
= rcu_dereference(ghes_estatus_caches
[i
]);
626 duration
= now
- cache
->time_in
;
627 if (duration
>= GHES_ESTATUS_IN_CACHE_MAX_NSEC
) {
632 count
= atomic_read(&cache
->count
);
634 do_div(period
, (count
+ 1));
635 if (period
> max_period
) {
641 /* new_cache must be put into array after its contents are written */
643 if (slot
!= -1 && cmpxchg(ghes_estatus_caches
+ slot
,
644 slot_cache
, new_cache
) == slot_cache
) {
646 call_rcu(&slot_cache
->rcu
, ghes_estatus_cache_rcu_free
);
648 ghes_estatus_cache_free(new_cache
);
652 static int ghes_proc(struct ghes
*ghes
)
656 rc
= ghes_read_estatus(ghes
, 0);
659 if (!ghes_estatus_cached(ghes
->estatus
)) {
660 if (ghes_print_estatus(NULL
, ghes
->generic
, ghes
->estatus
))
661 ghes_estatus_cache_add(ghes
->generic
, ghes
->estatus
);
663 ghes_do_proc(ghes
, ghes
->estatus
);
665 ghes_clear_estatus(ghes
);
669 static void ghes_add_timer(struct ghes
*ghes
)
671 struct acpi_hest_generic
*g
= ghes
->generic
;
672 unsigned long expire
;
674 if (!g
->notify
.poll_interval
) {
675 pr_warning(FW_WARN GHES_PFX
"Poll interval is 0 for generic hardware error source: %d, disabled.\n",
676 g
->header
.source_id
);
679 expire
= jiffies
+ msecs_to_jiffies(g
->notify
.poll_interval
);
680 ghes
->timer
.expires
= round_jiffies_relative(expire
);
681 add_timer(&ghes
->timer
);
684 static void ghes_poll_func(unsigned long data
)
686 struct ghes
*ghes
= (void *)data
;
689 if (!(ghes
->flags
& GHES_EXITING
))
690 ghes_add_timer(ghes
);
693 static irqreturn_t
ghes_irq_func(int irq
, void *data
)
695 struct ghes
*ghes
= data
;
698 rc
= ghes_proc(ghes
);
705 static int ghes_notify_sci(struct notifier_block
*this,
706 unsigned long event
, void *data
)
709 int ret
= NOTIFY_DONE
;
712 list_for_each_entry_rcu(ghes
, &ghes_sci
, list
) {
713 if (!ghes_proc(ghes
))
721 static struct notifier_block ghes_notifier_sci
= {
722 .notifier_call
= ghes_notify_sci
,
725 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
727 * printk is not safe in NMI context. So in NMI handler, we allocate
728 * required memory from lock-less memory allocator
729 * (ghes_estatus_pool), save estatus into it, put them into lock-less
730 * list (ghes_estatus_llist), then delay printk into IRQ context via
731 * irq_work (ghes_proc_irq_work). ghes_estatus_size_request record
732 * required pool size by all NMI error source.
734 static struct llist_head ghes_estatus_llist
;
735 static struct irq_work ghes_proc_irq_work
;
738 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
739 * having only one concurrent reader.
741 static atomic_t ghes_in_nmi
= ATOMIC_INIT(0);
743 static LIST_HEAD(ghes_nmi
);
745 static int ghes_panic_timeout __read_mostly
= 30;
747 static void ghes_proc_in_irq(struct irq_work
*irq_work
)
749 struct llist_node
*llnode
, *next
;
750 struct ghes_estatus_node
*estatus_node
;
751 struct acpi_hest_generic
*generic
;
752 struct acpi_hest_generic_status
*estatus
;
755 llnode
= llist_del_all(&ghes_estatus_llist
);
757 * Because the time order of estatus in list is reversed,
758 * revert it back to proper order.
760 llnode
= llist_reverse_order(llnode
);
763 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
765 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
766 len
= cper_estatus_len(estatus
);
767 node_len
= GHES_ESTATUS_NODE_LEN(len
);
768 ghes_do_proc(estatus_node
->ghes
, estatus
);
769 if (!ghes_estatus_cached(estatus
)) {
770 generic
= estatus_node
->generic
;
771 if (ghes_print_estatus(NULL
, generic
, estatus
))
772 ghes_estatus_cache_add(generic
, estatus
);
774 gen_pool_free(ghes_estatus_pool
, (unsigned long)estatus_node
,
780 static void ghes_print_queued_estatus(void)
782 struct llist_node
*llnode
;
783 struct ghes_estatus_node
*estatus_node
;
784 struct acpi_hest_generic
*generic
;
785 struct acpi_hest_generic_status
*estatus
;
788 llnode
= llist_del_all(&ghes_estatus_llist
);
790 * Because the time order of estatus in list is reversed,
791 * revert it back to proper order.
793 llnode
= llist_reverse_order(llnode
);
795 estatus_node
= llist_entry(llnode
, struct ghes_estatus_node
,
797 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
798 len
= cper_estatus_len(estatus
);
799 node_len
= GHES_ESTATUS_NODE_LEN(len
);
800 generic
= estatus_node
->generic
;
801 ghes_print_estatus(NULL
, generic
, estatus
);
802 llnode
= llnode
->next
;
806 /* Save estatus for further processing in IRQ context */
807 static void __process_error(struct ghes
*ghes
)
809 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
811 struct ghes_estatus_node
*estatus_node
;
812 struct acpi_hest_generic_status
*estatus
;
814 if (ghes_estatus_cached(ghes
->estatus
))
817 len
= cper_estatus_len(ghes
->estatus
);
818 node_len
= GHES_ESTATUS_NODE_LEN(len
);
820 estatus_node
= (void *)gen_pool_alloc(ghes_estatus_pool
, node_len
);
824 estatus_node
->ghes
= ghes
;
825 estatus_node
->generic
= ghes
->generic
;
826 estatus
= GHES_ESTATUS_FROM_NODE(estatus_node
);
827 memcpy(estatus
, ghes
->estatus
, len
);
828 llist_add(&estatus_node
->llnode
, &ghes_estatus_llist
);
832 static void __ghes_panic(struct ghes
*ghes
)
835 ghes_print_queued_estatus();
836 __ghes_print_estatus(KERN_EMERG
, ghes
->generic
, ghes
->estatus
);
838 /* reboot to log the error! */
839 if (panic_timeout
== 0)
840 panic_timeout
= ghes_panic_timeout
;
841 panic("Fatal hardware error!");
844 static int ghes_notify_nmi(unsigned int cmd
, struct pt_regs
*regs
)
847 int sev
, ret
= NMI_DONE
;
849 if (!atomic_add_unless(&ghes_in_nmi
, 1, 1))
852 list_for_each_entry_rcu(ghes
, &ghes_nmi
, list
) {
853 if (ghes_read_estatus(ghes
, 1)) {
854 ghes_clear_estatus(ghes
);
860 sev
= ghes_severity(ghes
->estatus
->error_severity
);
861 if (sev
>= GHES_SEV_PANIC
)
864 if (!(ghes
->flags
& GHES_TO_CLEAR
))
867 __process_error(ghes
);
868 ghes_clear_estatus(ghes
);
871 #ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
872 if (ret
== NMI_HANDLED
)
873 irq_work_queue(&ghes_proc_irq_work
);
875 atomic_dec(&ghes_in_nmi
);
879 static unsigned long ghes_esource_prealloc_size(
880 const struct acpi_hest_generic
*generic
)
882 unsigned long block_length
, prealloc_records
, prealloc_size
;
884 block_length
= min_t(unsigned long, generic
->error_block_length
,
885 GHES_ESTATUS_MAX_SIZE
);
886 prealloc_records
= max_t(unsigned long,
887 generic
->records_to_preallocate
, 1);
888 prealloc_size
= min_t(unsigned long, block_length
* prealloc_records
,
889 GHES_ESOURCE_PREALLOC_MAX_SIZE
);
891 return prealloc_size
;
894 static void ghes_estatus_pool_shrink(unsigned long len
)
896 ghes_estatus_pool_size_request
-= PAGE_ALIGN(len
);
899 static void ghes_nmi_add(struct ghes
*ghes
)
903 len
= ghes_esource_prealloc_size(ghes
->generic
);
904 ghes_estatus_pool_expand(len
);
905 mutex_lock(&ghes_list_mutex
);
906 if (list_empty(&ghes_nmi
))
907 register_nmi_handler(NMI_LOCAL
, ghes_notify_nmi
, 0, "ghes");
908 list_add_rcu(&ghes
->list
, &ghes_nmi
);
909 mutex_unlock(&ghes_list_mutex
);
912 static void ghes_nmi_remove(struct ghes
*ghes
)
916 mutex_lock(&ghes_list_mutex
);
917 list_del_rcu(&ghes
->list
);
918 if (list_empty(&ghes_nmi
))
919 unregister_nmi_handler(NMI_LOCAL
, "ghes");
920 mutex_unlock(&ghes_list_mutex
);
922 * To synchronize with NMI handler, ghes can only be
923 * freed after NMI handler finishes.
926 len
= ghes_esource_prealloc_size(ghes
->generic
);
927 ghes_estatus_pool_shrink(len
);
930 static void ghes_nmi_init_cxt(void)
932 init_irq_work(&ghes_proc_irq_work
, ghes_proc_in_irq
);
934 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
935 static inline void ghes_nmi_add(struct ghes
*ghes
)
937 pr_err(GHES_PFX
"ID: %d, trying to add NMI notification which is not supported!\n",
938 ghes
->generic
->header
.source_id
);
942 static inline void ghes_nmi_remove(struct ghes
*ghes
)
944 pr_err(GHES_PFX
"ID: %d, trying to remove NMI notification which is not supported!\n",
945 ghes
->generic
->header
.source_id
);
949 static inline void ghes_nmi_init_cxt(void)
952 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
954 static int ghes_probe(struct platform_device
*ghes_dev
)
956 struct acpi_hest_generic
*generic
;
957 struct ghes
*ghes
= NULL
;
961 generic
= *(struct acpi_hest_generic
**)ghes_dev
->dev
.platform_data
;
962 if (!generic
->enabled
)
965 switch (generic
->notify
.type
) {
966 case ACPI_HEST_NOTIFY_POLLED
:
967 case ACPI_HEST_NOTIFY_EXTERNAL
:
968 case ACPI_HEST_NOTIFY_SCI
:
970 case ACPI_HEST_NOTIFY_NMI
:
971 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI
)) {
972 pr_warn(GHES_PFX
"Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
973 generic
->header
.source_id
);
977 case ACPI_HEST_NOTIFY_LOCAL
:
978 pr_warning(GHES_PFX
"Generic hardware error source: %d notified via local interrupt is not supported!\n",
979 generic
->header
.source_id
);
982 pr_warning(FW_WARN GHES_PFX
"Unknown notification type: %u for generic hardware error source: %d\n",
983 generic
->notify
.type
, generic
->header
.source_id
);
988 if (generic
->error_block_length
<
989 sizeof(struct acpi_hest_generic_status
)) {
990 pr_warning(FW_BUG GHES_PFX
"Invalid error block length: %u for generic hardware error source: %d\n",
991 generic
->error_block_length
,
992 generic
->header
.source_id
);
995 ghes
= ghes_new(generic
);
1002 rc
= ghes_edac_register(ghes
, &ghes_dev
->dev
);
1006 switch (generic
->notify
.type
) {
1007 case ACPI_HEST_NOTIFY_POLLED
:
1008 ghes
->timer
.function
= ghes_poll_func
;
1009 ghes
->timer
.data
= (unsigned long)ghes
;
1010 init_timer_deferrable(&ghes
->timer
);
1011 ghes_add_timer(ghes
);
1013 case ACPI_HEST_NOTIFY_EXTERNAL
:
1014 /* External interrupt vector is GSI */
1015 rc
= acpi_gsi_to_irq(generic
->notify
.vector
, &ghes
->irq
);
1017 pr_err(GHES_PFX
"Failed to map GSI to IRQ for generic hardware error source: %d\n",
1018 generic
->header
.source_id
);
1019 goto err_edac_unreg
;
1021 rc
= request_irq(ghes
->irq
, ghes_irq_func
, 0, "GHES IRQ", ghes
);
1023 pr_err(GHES_PFX
"Failed to register IRQ for generic hardware error source: %d\n",
1024 generic
->header
.source_id
);
1025 goto err_edac_unreg
;
1028 case ACPI_HEST_NOTIFY_SCI
:
1029 mutex_lock(&ghes_list_mutex
);
1030 if (list_empty(&ghes_sci
))
1031 register_acpi_hed_notifier(&ghes_notifier_sci
);
1032 list_add_rcu(&ghes
->list
, &ghes_sci
);
1033 mutex_unlock(&ghes_list_mutex
);
1035 case ACPI_HEST_NOTIFY_NMI
:
1041 platform_set_drvdata(ghes_dev
, ghes
);
1045 ghes_edac_unregister(ghes
);
1054 static int ghes_remove(struct platform_device
*ghes_dev
)
1057 struct acpi_hest_generic
*generic
;
1059 ghes
= platform_get_drvdata(ghes_dev
);
1060 generic
= ghes
->generic
;
1062 ghes
->flags
|= GHES_EXITING
;
1063 switch (generic
->notify
.type
) {
1064 case ACPI_HEST_NOTIFY_POLLED
:
1065 del_timer_sync(&ghes
->timer
);
1067 case ACPI_HEST_NOTIFY_EXTERNAL
:
1068 free_irq(ghes
->irq
, ghes
);
1070 case ACPI_HEST_NOTIFY_SCI
:
1071 mutex_lock(&ghes_list_mutex
);
1072 list_del_rcu(&ghes
->list
);
1073 if (list_empty(&ghes_sci
))
1074 unregister_acpi_hed_notifier(&ghes_notifier_sci
);
1075 mutex_unlock(&ghes_list_mutex
);
1077 case ACPI_HEST_NOTIFY_NMI
:
1078 ghes_nmi_remove(ghes
);
1087 ghes_edac_unregister(ghes
);
1091 platform_set_drvdata(ghes_dev
, NULL
);
1096 static struct platform_driver ghes_platform_driver
= {
1100 .probe
= ghes_probe
,
1101 .remove
= ghes_remove
,
1104 static int __init
ghes_init(void)
1112 pr_info(GHES_PFX
"HEST is not enabled!\n");
1117 pr_info(GHES_PFX
"GHES is not enabled!\n");
1121 ghes_nmi_init_cxt();
1123 rc
= ghes_ioremap_init();
1127 rc
= ghes_estatus_pool_init();
1129 goto err_ioremap_exit
;
1131 rc
= ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE
*
1132 GHES_ESTATUS_CACHE_ALLOCED_MAX
);
1136 rc
= platform_driver_register(&ghes_platform_driver
);
1140 rc
= apei_osc_setup();
1141 if (rc
== 0 && osc_sb_apei_support_acked
)
1142 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1143 else if (rc
== 0 && !osc_sb_apei_support_acked
)
1144 pr_info(GHES_PFX
"APEI firmware first mode is enabled by WHEA _OSC.\n");
1145 else if (rc
&& osc_sb_apei_support_acked
)
1146 pr_info(GHES_PFX
"APEI firmware first mode is enabled by APEI bit.\n");
1148 pr_info(GHES_PFX
"Failed to enable APEI firmware first mode.\n");
1152 ghes_estatus_pool_exit();
1154 ghes_ioremap_exit();
1158 device_initcall(ghes_init
);