Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / apei / ghes.c
blobbe6d233fdb9fb7156eb57a862d8f59f33639fd6c
1 /*
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>
32 #include <linux/io.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.
97 bool ghes_disable;
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
118 * simultaneously.
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)
133 phys_addr_t paddr;
134 pgprot_t prot;
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)
145 phys_addr_t paddr;
146 pgprot_t prot;
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)
169 return -ENOMEM;
170 return 0;
173 static void ghes_estatus_pool_free_chunk(struct gen_pool *pool,
174 struct gen_pool_chunk *chunk,
175 void *data)
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)
194 return 0;
196 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
197 if (!addr)
198 return -ENOMEM;
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)
221 struct ghes *ghes;
222 unsigned int error_block_length;
223 int rc;
225 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
226 if (!ghes)
227 return ERR_PTR(-ENOMEM);
229 ghes->generic = generic;
230 if (is_hest_type_generic_v2(ghes)) {
231 rc = map_gen_v2(ghes);
232 if (rc)
233 goto err_free;
236 rc = apei_map_generic_address(&generic->error_status_address);
237 if (rc)
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) {
249 rc = -ENOMEM;
250 goto err_unmap_status_addr;
253 return ghes;
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))
259 unmap_gen_v2(ghes);
260 err_free:
261 kfree(ghes);
262 return ERR_PTR(rc);
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))
270 unmap_gen_v2(ghes);
273 static inline int ghes_severity(int severity)
275 switch (severity) {
276 case CPER_SEV_INFORMATIONAL:
277 return GHES_SEV_NO;
278 case CPER_SEV_CORRECTED:
279 return GHES_SEV_CORRECTED;
280 case CPER_SEV_RECOVERABLE:
281 return GHES_SEV_RECOVERABLE;
282 case CPER_SEV_FATAL:
283 return GHES_SEV_PANIC;
284 default:
285 /* Unknown, go panic */
286 return GHES_SEV_PANIC;
290 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
291 int from_phys)
293 void __iomem *vaddr;
294 unsigned long flags = 0;
295 int in_nmi = in_nmi();
296 u64 offset;
297 u32 trunk;
299 while (len > 0) {
300 offset = paddr - (paddr & PAGE_MASK);
301 if (in_nmi) {
302 raw_spin_lock(&ghes_ioremap_lock_nmi);
303 vaddr = ghes_ioremap_pfn_nmi(paddr >> PAGE_SHIFT);
304 } else {
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);
310 if (from_phys)
311 memcpy_fromio(buffer, vaddr + offset, trunk);
312 else
313 memcpy_toio(vaddr + offset, buffer, trunk);
314 len -= trunk;
315 paddr += trunk;
316 buffer += trunk;
317 if (in_nmi) {
318 ghes_iounmap_nmi();
319 raw_spin_unlock(&ghes_ioremap_lock_nmi);
320 } else {
321 ghes_iounmap_irq();
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;
330 u64 buf_paddr;
331 u32 len;
332 int rc;
334 rc = apei_read(&buf_paddr, &g->error_status_address);
335 if (rc) {
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);
340 return -EIO;
342 if (!buf_paddr)
343 return -ENOENT;
345 ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
346 sizeof(*ghes->estatus), 1);
347 if (!ghes->estatus->block_status)
348 return -ENOENT;
350 ghes->buffer_paddr = buf_paddr;
351 ghes->flags |= GHES_TO_CLEAR;
353 rc = -EIO;
354 len = cper_estatus_len(ghes->estatus);
355 if (len < sizeof(*ghes->estatus))
356 goto err_read_block;
357 if (len > ghes->generic->error_block_length)
358 goto err_read_block;
359 if (cper_estatus_check_header(ghes->estatus))
360 goto err_read_block;
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))
365 goto err_read_block;
366 rc = 0;
368 err_read_block:
369 if (rc && !silent && printk_ratelimit())
370 pr_warning(FW_WARN GHES_PFX
371 "Failed to read error status block!\n");
372 return rc;
375 static void ghes_clear_estatus(struct ghes *ghes)
377 ghes->estatus->block_status = 0;
378 if (!(ghes->flags & GHES_TO_CLEAR))
379 return;
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
388 unsigned long pfn;
389 int flags = -1;
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))
394 return;
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);
401 return;
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)
409 flags = 0;
411 if (flags != -1)
412 memory_failure_queue(pfn, flags);
413 #endif
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
423 * necessary.
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
428 * panic.
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) {
437 unsigned int devfn;
438 int aer_severity;
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,
454 devfn, aer_severity,
455 (struct aer_capability_regs *)
456 pcie_err->aer_info);
458 #endif
461 static void ghes_do_proc(struct ghes *ghes,
462 const struct acpi_hest_generic_status *estatus)
464 int sev, sec_sev;
465 struct acpi_hest_generic_data *gdata;
466 guid_t *sec_type;
467 guid_t *fru_id = &NULL_UUID_LE;
468 char *fru_text = "";
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);
495 } else {
496 void *err = acpi_hest_get_payload(gdata);
498 log_non_standard_event(sec_type, fru_id, fru_text,
499 sec_sev, err,
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;
511 char pfx_seq[64];
513 if (pfx == NULL) {
514 if (ghes_severity(estatus->error_severity) <=
515 GHES_SEV_CORRECTED)
516 pfx = KERN_WARNING;
517 else
518 pfx = KERN_ERR;
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;
538 else
539 ratelimit = &ratelimit_uncorrected;
540 if (__ratelimit(ratelimit)) {
541 __ghes_print_estatus(pfx, generic, estatus);
542 return 1;
544 return 0;
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)
553 u32 len;
554 int i, cached = 0;
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);
560 rcu_read_lock();
561 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
562 cache = rcu_dereference(ghes_estatus_caches[i]);
563 if (cache == NULL)
564 continue;
565 if (len != cache->estatus_len)
566 continue;
567 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
568 if (memcmp(estatus, cache_estatus, len))
569 continue;
570 atomic_inc(&cache->count);
571 now = sched_clock();
572 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
573 cached = 1;
574 break;
576 rcu_read_unlock();
577 return cached;
580 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
581 struct acpi_hest_generic *generic,
582 struct acpi_hest_generic_status *estatus)
584 int alloced;
585 u32 len, cache_len;
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);
592 return NULL;
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);
597 if (!cache) {
598 atomic_dec(&ghes_estatus_cache_alloced);
599 return NULL;
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();
607 return cache;
610 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
612 u32 len;
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)
638 return;
639 rcu_read_lock();
640 now = sched_clock();
641 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
642 cache = rcu_dereference(ghes_estatus_caches[i]);
643 if (cache == NULL) {
644 slot = i;
645 slot_cache = NULL;
646 break;
648 duration = now - cache->time_in;
649 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
650 slot = i;
651 slot_cache = cache;
652 break;
654 count = atomic_read(&cache->count);
655 period = duration;
656 do_div(period, (count + 1));
657 if (period > max_period) {
658 max_period = period;
659 slot = i;
660 slot_cache = cache;
663 /* new_cache must be put into array after its contents are written */
664 smp_wmb();
665 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
666 slot_cache, new_cache) == slot_cache) {
667 if (slot_cache)
668 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
669 } else
670 ghes_estatus_cache_free(new_cache);
671 rcu_read_unlock();
674 static int ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
676 int rc;
677 u64 val = 0;
679 rc = apei_read(&val, &gv2->read_ack_register);
680 if (rc)
681 return rc;
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! */
696 if (!panic_timeout)
697 panic_timeout = ghes_panic_timeout;
698 panic("Fatal hardware error!");
701 static int ghes_proc(struct ghes *ghes)
703 int rc;
705 rc = ghes_read_estatus(ghes, 0);
706 if (rc)
707 goto out;
709 if (ghes_severity(ghes->estatus->error_severity) >= GHES_SEV_PANIC) {
710 __ghes_panic(ghes);
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);
719 out:
720 ghes_clear_estatus(ghes);
722 if (rc == -ENOENT)
723 return rc;
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);
732 return rc;
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);
743 return;
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);
754 ghes_proc(ghes);
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;
762 int rc;
764 rc = ghes_proc(ghes);
765 if (rc)
766 return IRQ_NONE;
768 return IRQ_HANDLED;
771 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
772 void *data)
774 struct ghes *ghes;
775 int ret = NOTIFY_DONE;
777 rcu_read_lock();
778 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
779 if (!ghes_proc(ghes))
780 ret = NOTIFY_OK;
782 rcu_read_unlock();
784 return ret;
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)
800 struct ghes *ghes;
801 int ret = -ENOENT;
803 rcu_read_lock();
804 list_for_each_entry_rcu(ghes, &ghes_sea, list) {
805 if (!ghes_proc(ghes))
806 ret = 0;
808 rcu_read_unlock();
809 return ret;
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);
824 synchronize_rcu();
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;
857 u32 len, node_len;
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);
865 while (llnode) {
866 next = llnode->next;
867 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
868 llnode);
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,
879 node_len);
880 llnode = next;
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);
897 while (llnode) {
898 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
899 llnode);
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
911 u32 len, node_len;
912 struct ghes_estatus_node *estatus_node;
913 struct acpi_hest_generic_status *estatus;
915 if (ghes_estatus_cached(ghes->estatus))
916 return;
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);
922 if (!estatus_node)
923 return;
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);
930 #endif
933 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
935 struct ghes *ghes;
936 int sev, ret = NMI_DONE;
938 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
939 return ret;
941 list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
942 if (ghes_read_estatus(ghes, 1)) {
943 ghes_clear_estatus(ghes);
944 continue;
945 } else {
946 ret = NMI_HANDLED;
949 sev = ghes_severity(ghes->estatus->error_severity);
950 if (sev >= GHES_SEV_PANIC) {
951 ghes_print_queued_estatus();
952 __ghes_panic(ghes);
955 if (!(ghes->flags & GHES_TO_CLEAR))
956 continue;
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);
965 #endif
966 atomic_dec(&ghes_in_nmi);
967 return ret;
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)
992 unsigned long len;
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)
1005 unsigned long len;
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.
1016 synchronize_rcu();
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;
1036 int rc = -EINVAL;
1038 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1039 if (!generic->enabled)
1040 return -ENODEV;
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:
1048 break;
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);
1054 rc = -ENOTSUPP;
1055 goto err;
1057 break;
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);
1062 goto err;
1064 break;
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);
1068 goto err;
1069 default:
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);
1072 goto err;
1075 rc = -EIO;
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);
1081 goto err;
1083 ghes = ghes_new(generic);
1084 if (IS_ERR(ghes)) {
1085 rc = PTR_ERR(ghes);
1086 ghes = NULL;
1087 goto err;
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);
1094 break;
1095 case ACPI_HEST_NOTIFY_EXTERNAL:
1096 /* External interrupt vector is GSI */
1097 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1098 if (rc) {
1099 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1100 generic->header.source_id);
1101 goto err;
1103 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1104 "GHES IRQ", ghes);
1105 if (rc) {
1106 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1107 generic->header.source_id);
1108 goto err;
1110 break;
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);
1120 break;
1122 case ACPI_HEST_NOTIFY_SEA:
1123 ghes_sea_add(ghes);
1124 break;
1125 case ACPI_HEST_NOTIFY_NMI:
1126 ghes_nmi_add(ghes);
1127 break;
1128 default:
1129 BUG();
1132 platform_set_drvdata(ghes_dev, ghes);
1134 ghes_edac_register(ghes, &ghes_dev->dev);
1136 /* Handle any pending errors right away */
1137 ghes_proc(ghes);
1139 return 0;
1141 err:
1142 if (ghes) {
1143 ghes_fini(ghes);
1144 kfree(ghes);
1146 return rc;
1149 static int ghes_remove(struct platform_device *ghes_dev)
1151 struct ghes *ghes;
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);
1161 break;
1162 case ACPI_HEST_NOTIFY_EXTERNAL:
1163 free_irq(ghes->irq, ghes);
1164 break;
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);
1174 synchronize_rcu();
1175 break;
1177 case ACPI_HEST_NOTIFY_SEA:
1178 ghes_sea_remove(ghes);
1179 break;
1180 case ACPI_HEST_NOTIFY_NMI:
1181 ghes_nmi_remove(ghes);
1182 break;
1183 default:
1184 BUG();
1185 break;
1188 ghes_fini(ghes);
1190 ghes_edac_unregister(ghes);
1192 kfree(ghes);
1194 platform_set_drvdata(ghes_dev, NULL);
1196 return 0;
1199 static struct platform_driver ghes_platform_driver = {
1200 .driver = {
1201 .name = "GHES",
1203 .probe = ghes_probe,
1204 .remove = ghes_remove,
1207 static int __init ghes_init(void)
1209 int rc;
1211 if (acpi_disabled)
1212 return -ENODEV;
1214 switch (hest_disable) {
1215 case HEST_NOT_FOUND:
1216 return -ENODEV;
1217 case HEST_DISABLED:
1218 pr_info(GHES_PFX "HEST is not enabled!\n");
1219 return -EINVAL;
1220 default:
1221 break;
1224 if (ghes_disable) {
1225 pr_info(GHES_PFX "GHES is not enabled!\n");
1226 return -EINVAL;
1229 ghes_nmi_init_cxt();
1231 rc = ghes_estatus_pool_init();
1232 if (rc)
1233 goto err;
1235 rc = ghes_estatus_pool_expand(GHES_ESTATUS_CACHE_AVG_SIZE *
1236 GHES_ESTATUS_CACHE_ALLOCED_MAX);
1237 if (rc)
1238 goto err_pool_exit;
1240 rc = platform_driver_register(&ghes_platform_driver);
1241 if (rc)
1242 goto err_pool_exit;
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");
1251 else
1252 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1254 return 0;
1255 err_pool_exit:
1256 ghes_estatus_pool_exit();
1257 err:
1258 return rc;
1260 device_initcall(ghes_init);