drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / acpi / apei / ghes.c
blob0b5ae91fd0fb5469cabd2d7cf54a6129b2e1b501
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/arm_sdei.h>
29 #include <linux/kernel.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/acpi.h>
33 #include <linux/io.h>
34 #include <linux/interrupt.h>
35 #include <linux/timer.h>
36 #include <linux/cper.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/pfn.h>
46 #include <linux/aer.h>
47 #include <linux/nmi.h>
48 #include <linux/sched/clock.h>
49 #include <linux/uuid.h>
50 #include <linux/ras.h>
52 #include <acpi/actbl1.h>
53 #include <acpi/ghes.h>
54 #include <acpi/apei.h>
55 #include <asm/fixmap.h>
56 #include <asm/tlbflush.h>
57 #include <ras/ras_event.h>
59 #include "apei-internal.h"
61 #define GHES_PFX "GHES: "
63 #define GHES_ESTATUS_MAX_SIZE 65536
64 #define GHES_ESOURCE_PREALLOC_MAX_SIZE 65536
66 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
68 /* This is just an estimation for memory pool allocation */
69 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
71 #define GHES_ESTATUS_CACHES_SIZE 4
73 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC 10000000000ULL
74 /* Prevent too many caches are allocated because of RCU */
75 #define GHES_ESTATUS_CACHE_ALLOCED_MAX (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
77 #define GHES_ESTATUS_CACHE_LEN(estatus_len) \
78 (sizeof(struct ghes_estatus_cache) + (estatus_len))
79 #define GHES_ESTATUS_FROM_CACHE(estatus_cache) \
80 ((struct acpi_hest_generic_status *) \
81 ((struct ghes_estatus_cache *)(estatus_cache) + 1))
83 #define GHES_ESTATUS_NODE_LEN(estatus_len) \
84 (sizeof(struct ghes_estatus_node) + (estatus_len))
85 #define GHES_ESTATUS_FROM_NODE(estatus_node) \
86 ((struct acpi_hest_generic_status *) \
87 ((struct ghes_estatus_node *)(estatus_node) + 1))
90 * NMI-like notifications vary by architecture, before the compiler can prune
91 * unused static functions it needs a value for these enums.
93 #ifndef CONFIG_ARM_SDE_INTERFACE
94 #define FIX_APEI_GHES_SDEI_NORMAL __end_of_fixed_addresses
95 #define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses
96 #endif
98 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
100 return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
104 * This driver isn't really modular, however for the time being,
105 * continuing to use module_param is the easiest way to remain
106 * compatible with existing boot arg use cases.
108 bool ghes_disable;
109 module_param_named(disable, ghes_disable, bool, 0);
112 * All error sources notified with HED (Hardware Error Device) share a
113 * single notifier callback, so they need to be linked and checked one
114 * by one. This holds true for NMI too.
116 * RCU is used for these lists, so ghes_list_mutex is only used for
117 * list changing, not for traversing.
119 static LIST_HEAD(ghes_hed);
120 static DEFINE_MUTEX(ghes_list_mutex);
123 * Because the memory area used to transfer hardware error information
124 * from BIOS to Linux can be determined only in NMI, IRQ or timer
125 * handler, but general ioremap can not be used in atomic context, so
126 * the fixmap is used instead.
128 * This spinlock is used to prevent the fixmap entry from being used
129 * simultaneously.
131 static DEFINE_SPINLOCK(ghes_notify_lock_irq);
133 static struct gen_pool *ghes_estatus_pool;
134 static unsigned long ghes_estatus_pool_size_request;
136 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
137 static atomic_t ghes_estatus_cache_alloced;
139 static int ghes_panic_timeout __read_mostly = 30;
141 static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
143 phys_addr_t paddr;
144 pgprot_t prot;
146 paddr = PFN_PHYS(pfn);
147 prot = arch_apei_get_mem_attribute(paddr);
148 __set_fixmap(fixmap_idx, paddr, prot);
150 return (void __iomem *) __fix_to_virt(fixmap_idx);
153 static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
155 int _idx = virt_to_fix((unsigned long)vaddr);
157 WARN_ON_ONCE(fixmap_idx != _idx);
158 clear_fixmap(fixmap_idx);
161 int ghes_estatus_pool_init(int num_ghes)
163 unsigned long addr, len;
165 ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
166 if (!ghes_estatus_pool)
167 return -ENOMEM;
169 len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
170 len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
172 ghes_estatus_pool_size_request = PAGE_ALIGN(len);
173 addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
174 if (!addr)
175 return -ENOMEM;
178 * New allocation must be visible in all pgd before it can be found by
179 * an NMI allocating from the pool.
181 vmalloc_sync_all();
183 return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
186 static int map_gen_v2(struct ghes *ghes)
188 return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
191 static void unmap_gen_v2(struct ghes *ghes)
193 apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
196 static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
198 int rc;
199 u64 val = 0;
201 rc = apei_read(&val, &gv2->read_ack_register);
202 if (rc)
203 return;
205 val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
206 val |= gv2->read_ack_write << gv2->read_ack_register.bit_offset;
208 apei_write(val, &gv2->read_ack_register);
211 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
213 struct ghes *ghes;
214 unsigned int error_block_length;
215 int rc;
217 ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
218 if (!ghes)
219 return ERR_PTR(-ENOMEM);
221 ghes->generic = generic;
222 if (is_hest_type_generic_v2(ghes)) {
223 rc = map_gen_v2(ghes);
224 if (rc)
225 goto err_free;
228 rc = apei_map_generic_address(&generic->error_status_address);
229 if (rc)
230 goto err_unmap_read_ack_addr;
231 error_block_length = generic->error_block_length;
232 if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
233 pr_warning(FW_WARN GHES_PFX
234 "Error status block length is too long: %u for "
235 "generic hardware error source: %d.\n",
236 error_block_length, generic->header.source_id);
237 error_block_length = GHES_ESTATUS_MAX_SIZE;
239 ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
240 if (!ghes->estatus) {
241 rc = -ENOMEM;
242 goto err_unmap_status_addr;
245 return ghes;
247 err_unmap_status_addr:
248 apei_unmap_generic_address(&generic->error_status_address);
249 err_unmap_read_ack_addr:
250 if (is_hest_type_generic_v2(ghes))
251 unmap_gen_v2(ghes);
252 err_free:
253 kfree(ghes);
254 return ERR_PTR(rc);
257 static void ghes_fini(struct ghes *ghes)
259 kfree(ghes->estatus);
260 apei_unmap_generic_address(&ghes->generic->error_status_address);
261 if (is_hest_type_generic_v2(ghes))
262 unmap_gen_v2(ghes);
265 static inline int ghes_severity(int severity)
267 switch (severity) {
268 case CPER_SEV_INFORMATIONAL:
269 return GHES_SEV_NO;
270 case CPER_SEV_CORRECTED:
271 return GHES_SEV_CORRECTED;
272 case CPER_SEV_RECOVERABLE:
273 return GHES_SEV_RECOVERABLE;
274 case CPER_SEV_FATAL:
275 return GHES_SEV_PANIC;
276 default:
277 /* Unknown, go panic */
278 return GHES_SEV_PANIC;
282 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
283 int from_phys,
284 enum fixed_addresses fixmap_idx)
286 void __iomem *vaddr;
287 u64 offset;
288 u32 trunk;
290 while (len > 0) {
291 offset = paddr - (paddr & PAGE_MASK);
292 vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
293 trunk = PAGE_SIZE - offset;
294 trunk = min(trunk, len);
295 if (from_phys)
296 memcpy_fromio(buffer, vaddr + offset, trunk);
297 else
298 memcpy_toio(vaddr + offset, buffer, trunk);
299 len -= trunk;
300 paddr += trunk;
301 buffer += trunk;
302 ghes_unmap(vaddr, fixmap_idx);
306 /* Check the top-level record header has an appropriate size. */
307 static int __ghes_check_estatus(struct ghes *ghes,
308 struct acpi_hest_generic_status *estatus)
310 u32 len = cper_estatus_len(estatus);
312 if (len < sizeof(*estatus)) {
313 pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
314 return -EIO;
317 if (len > ghes->generic->error_block_length) {
318 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
319 return -EIO;
322 if (cper_estatus_check_header(estatus)) {
323 pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
324 return -EIO;
327 return 0;
330 /* Read the CPER block, returning its address, and header in estatus. */
331 static int __ghes_peek_estatus(struct ghes *ghes,
332 struct acpi_hest_generic_status *estatus,
333 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
335 struct acpi_hest_generic *g = ghes->generic;
336 int rc;
338 rc = apei_read(buf_paddr, &g->error_status_address);
339 if (rc) {
340 *buf_paddr = 0;
341 pr_warn_ratelimited(FW_WARN GHES_PFX
342 "Failed to read error status block address for hardware error source: %d.\n",
343 g->header.source_id);
344 return -EIO;
346 if (!*buf_paddr)
347 return -ENOENT;
349 ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
350 fixmap_idx);
351 if (!estatus->block_status) {
352 *buf_paddr = 0;
353 return -ENOENT;
356 return __ghes_check_estatus(ghes, estatus);
359 static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
360 u64 buf_paddr, enum fixed_addresses fixmap_idx,
361 size_t buf_len)
363 ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
364 if (cper_estatus_check(estatus)) {
365 pr_warn_ratelimited(FW_WARN GHES_PFX
366 "Failed to read error status block!\n");
367 return -EIO;
370 return 0;
373 static int ghes_read_estatus(struct ghes *ghes,
374 struct acpi_hest_generic_status *estatus,
375 u64 *buf_paddr, enum fixed_addresses fixmap_idx)
377 int rc;
379 rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
380 if (rc)
381 return rc;
383 rc = __ghes_check_estatus(ghes, estatus);
384 if (rc)
385 return rc;
387 return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
388 cper_estatus_len(estatus));
391 static void ghes_clear_estatus(struct ghes *ghes,
392 struct acpi_hest_generic_status *estatus,
393 u64 buf_paddr, enum fixed_addresses fixmap_idx)
395 estatus->block_status = 0;
397 if (!buf_paddr)
398 return;
400 ghes_copy_tofrom_phys(estatus, buf_paddr,
401 sizeof(estatus->block_status), 0,
402 fixmap_idx);
405 * GHESv2 type HEST entries introduce support for error acknowledgment,
406 * so only acknowledge the error if this support is present.
408 if (is_hest_type_generic_v2(ghes))
409 ghes_ack_error(ghes->generic_v2);
412 static void ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata, int sev)
414 #ifdef CONFIG_ACPI_APEI_MEMORY_FAILURE
415 unsigned long pfn;
416 int flags = -1;
417 int sec_sev = ghes_severity(gdata->error_severity);
418 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
420 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
421 return;
423 pfn = mem_err->physical_addr >> PAGE_SHIFT;
424 if (!pfn_valid(pfn)) {
425 pr_warn_ratelimited(FW_WARN GHES_PFX
426 "Invalid address in generic error data: %#llx\n",
427 mem_err->physical_addr);
428 return;
431 /* iff following two events can be handled properly by now */
432 if (sec_sev == GHES_SEV_CORRECTED &&
433 (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
434 flags = MF_SOFT_OFFLINE;
435 if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
436 flags = 0;
438 if (flags != -1)
439 memory_failure_queue(pfn, flags);
440 #endif
444 * PCIe AER errors need to be sent to the AER driver for reporting and
445 * recovery. The GHES severities map to the following AER severities and
446 * require the following handling:
448 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
449 * These need to be reported by the AER driver but no recovery is
450 * necessary.
451 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
452 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
453 * These both need to be reported and recovered from by the AER driver.
454 * GHES_SEV_PANIC does not make it to this handling since the kernel must
455 * panic.
457 static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
459 #ifdef CONFIG_ACPI_APEI_PCIEAER
460 struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
462 if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
463 pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
464 unsigned int devfn;
465 int aer_severity;
467 devfn = PCI_DEVFN(pcie_err->device_id.device,
468 pcie_err->device_id.function);
469 aer_severity = cper_severity_to_aer(gdata->error_severity);
472 * If firmware reset the component to contain
473 * the error, we must reinitialize it before
474 * use, so treat it as a fatal AER error.
476 if (gdata->flags & CPER_SEC_RESET)
477 aer_severity = AER_FATAL;
479 aer_recover_queue(pcie_err->device_id.segment,
480 pcie_err->device_id.bus,
481 devfn, aer_severity,
482 (struct aer_capability_regs *)
483 pcie_err->aer_info);
485 #endif
488 static void ghes_do_proc(struct ghes *ghes,
489 const struct acpi_hest_generic_status *estatus)
491 int sev, sec_sev;
492 struct acpi_hest_generic_data *gdata;
493 guid_t *sec_type;
494 guid_t *fru_id = &NULL_UUID_LE;
495 char *fru_text = "";
497 sev = ghes_severity(estatus->error_severity);
498 apei_estatus_for_each_section(estatus, gdata) {
499 sec_type = (guid_t *)gdata->section_type;
500 sec_sev = ghes_severity(gdata->error_severity);
501 if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
502 fru_id = (guid_t *)gdata->fru_id;
504 if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
505 fru_text = gdata->fru_text;
507 if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
508 struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
510 ghes_edac_report_mem_error(sev, mem_err);
512 arch_apei_report_mem_error(sev, mem_err);
513 ghes_handle_memory_failure(gdata, sev);
515 else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
516 ghes_handle_aer(gdata);
518 else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
519 struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
521 log_arm_hw_error(err);
522 } else {
523 void *err = acpi_hest_get_payload(gdata);
525 log_non_standard_event(sec_type, fru_id, fru_text,
526 sec_sev, err,
527 gdata->error_data_length);
532 static void __ghes_print_estatus(const char *pfx,
533 const struct acpi_hest_generic *generic,
534 const struct acpi_hest_generic_status *estatus)
536 static atomic_t seqno;
537 unsigned int curr_seqno;
538 char pfx_seq[64];
540 if (pfx == NULL) {
541 if (ghes_severity(estatus->error_severity) <=
542 GHES_SEV_CORRECTED)
543 pfx = KERN_WARNING;
544 else
545 pfx = KERN_ERR;
547 curr_seqno = atomic_inc_return(&seqno);
548 snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
549 printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
550 pfx_seq, generic->header.source_id);
551 cper_estatus_print(pfx_seq, estatus);
554 static int ghes_print_estatus(const char *pfx,
555 const struct acpi_hest_generic *generic,
556 const struct acpi_hest_generic_status *estatus)
558 /* Not more than 2 messages every 5 seconds */
559 static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
560 static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
561 struct ratelimit_state *ratelimit;
563 if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
564 ratelimit = &ratelimit_corrected;
565 else
566 ratelimit = &ratelimit_uncorrected;
567 if (__ratelimit(ratelimit)) {
568 __ghes_print_estatus(pfx, generic, estatus);
569 return 1;
571 return 0;
575 * GHES error status reporting throttle, to report more kinds of
576 * errors, instead of just most frequently occurred errors.
578 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
580 u32 len;
581 int i, cached = 0;
582 unsigned long long now;
583 struct ghes_estatus_cache *cache;
584 struct acpi_hest_generic_status *cache_estatus;
586 len = cper_estatus_len(estatus);
587 rcu_read_lock();
588 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
589 cache = rcu_dereference(ghes_estatus_caches[i]);
590 if (cache == NULL)
591 continue;
592 if (len != cache->estatus_len)
593 continue;
594 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
595 if (memcmp(estatus, cache_estatus, len))
596 continue;
597 atomic_inc(&cache->count);
598 now = sched_clock();
599 if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
600 cached = 1;
601 break;
603 rcu_read_unlock();
604 return cached;
607 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
608 struct acpi_hest_generic *generic,
609 struct acpi_hest_generic_status *estatus)
611 int alloced;
612 u32 len, cache_len;
613 struct ghes_estatus_cache *cache;
614 struct acpi_hest_generic_status *cache_estatus;
616 alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
617 if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
618 atomic_dec(&ghes_estatus_cache_alloced);
619 return NULL;
621 len = cper_estatus_len(estatus);
622 cache_len = GHES_ESTATUS_CACHE_LEN(len);
623 cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
624 if (!cache) {
625 atomic_dec(&ghes_estatus_cache_alloced);
626 return NULL;
628 cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
629 memcpy(cache_estatus, estatus, len);
630 cache->estatus_len = len;
631 atomic_set(&cache->count, 0);
632 cache->generic = generic;
633 cache->time_in = sched_clock();
634 return cache;
637 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
639 u32 len;
641 len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
642 len = GHES_ESTATUS_CACHE_LEN(len);
643 gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
644 atomic_dec(&ghes_estatus_cache_alloced);
647 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
649 struct ghes_estatus_cache *cache;
651 cache = container_of(head, struct ghes_estatus_cache, rcu);
652 ghes_estatus_cache_free(cache);
655 static void ghes_estatus_cache_add(
656 struct acpi_hest_generic *generic,
657 struct acpi_hest_generic_status *estatus)
659 int i, slot = -1, count;
660 unsigned long long now, duration, period, max_period = 0;
661 struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
663 new_cache = ghes_estatus_cache_alloc(generic, estatus);
664 if (new_cache == NULL)
665 return;
666 rcu_read_lock();
667 now = sched_clock();
668 for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
669 cache = rcu_dereference(ghes_estatus_caches[i]);
670 if (cache == NULL) {
671 slot = i;
672 slot_cache = NULL;
673 break;
675 duration = now - cache->time_in;
676 if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
677 slot = i;
678 slot_cache = cache;
679 break;
681 count = atomic_read(&cache->count);
682 period = duration;
683 do_div(period, (count + 1));
684 if (period > max_period) {
685 max_period = period;
686 slot = i;
687 slot_cache = cache;
690 /* new_cache must be put into array after its contents are written */
691 smp_wmb();
692 if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
693 slot_cache, new_cache) == slot_cache) {
694 if (slot_cache)
695 call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
696 } else
697 ghes_estatus_cache_free(new_cache);
698 rcu_read_unlock();
701 static void __ghes_panic(struct ghes *ghes,
702 struct acpi_hest_generic_status *estatus,
703 u64 buf_paddr, enum fixed_addresses fixmap_idx)
705 __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
707 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
709 /* reboot to log the error! */
710 if (!panic_timeout)
711 panic_timeout = ghes_panic_timeout;
712 panic("Fatal hardware error!");
715 static int ghes_proc(struct ghes *ghes)
717 struct acpi_hest_generic_status *estatus = ghes->estatus;
718 u64 buf_paddr;
719 int rc;
721 rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
722 if (rc)
723 goto out;
725 if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
726 __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
728 if (!ghes_estatus_cached(estatus)) {
729 if (ghes_print_estatus(NULL, ghes->generic, estatus))
730 ghes_estatus_cache_add(ghes->generic, estatus);
732 ghes_do_proc(ghes, estatus);
734 out:
735 ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
737 return rc;
740 static void ghes_add_timer(struct ghes *ghes)
742 struct acpi_hest_generic *g = ghes->generic;
743 unsigned long expire;
745 if (!g->notify.poll_interval) {
746 pr_warning(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
747 g->header.source_id);
748 return;
750 expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
751 ghes->timer.expires = round_jiffies_relative(expire);
752 add_timer(&ghes->timer);
755 static void ghes_poll_func(struct timer_list *t)
757 struct ghes *ghes = from_timer(ghes, t, timer);
758 unsigned long flags;
760 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
761 ghes_proc(ghes);
762 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
763 if (!(ghes->flags & GHES_EXITING))
764 ghes_add_timer(ghes);
767 static irqreturn_t ghes_irq_func(int irq, void *data)
769 struct ghes *ghes = data;
770 unsigned long flags;
771 int rc;
773 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
774 rc = ghes_proc(ghes);
775 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
776 if (rc)
777 return IRQ_NONE;
779 return IRQ_HANDLED;
782 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
783 void *data)
785 struct ghes *ghes;
786 unsigned long flags;
787 int ret = NOTIFY_DONE;
789 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
790 rcu_read_lock();
791 list_for_each_entry_rcu(ghes, &ghes_hed, list) {
792 if (!ghes_proc(ghes))
793 ret = NOTIFY_OK;
795 rcu_read_unlock();
796 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
798 return ret;
801 static struct notifier_block ghes_notifier_hed = {
802 .notifier_call = ghes_notify_hed,
806 * Handlers for CPER records may not be NMI safe. For example,
807 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
808 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
809 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
810 * ghes_proc_in_irq() to run in IRQ context where each estatus in
811 * ghes_estatus_llist is processed.
813 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
814 * to suppress frequent messages.
816 static struct llist_head ghes_estatus_llist;
817 static struct irq_work ghes_proc_irq_work;
819 static void ghes_proc_in_irq(struct irq_work *irq_work)
821 struct llist_node *llnode, *next;
822 struct ghes_estatus_node *estatus_node;
823 struct acpi_hest_generic *generic;
824 struct acpi_hest_generic_status *estatus;
825 u32 len, node_len;
827 llnode = llist_del_all(&ghes_estatus_llist);
829 * Because the time order of estatus in list is reversed,
830 * revert it back to proper order.
832 llnode = llist_reverse_order(llnode);
833 while (llnode) {
834 next = llnode->next;
835 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
836 llnode);
837 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
838 len = cper_estatus_len(estatus);
839 node_len = GHES_ESTATUS_NODE_LEN(len);
840 ghes_do_proc(estatus_node->ghes, estatus);
841 if (!ghes_estatus_cached(estatus)) {
842 generic = estatus_node->generic;
843 if (ghes_print_estatus(NULL, generic, estatus))
844 ghes_estatus_cache_add(generic, estatus);
846 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
847 node_len);
848 llnode = next;
852 static void ghes_print_queued_estatus(void)
854 struct llist_node *llnode;
855 struct ghes_estatus_node *estatus_node;
856 struct acpi_hest_generic *generic;
857 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);
865 while (llnode) {
866 estatus_node = llist_entry(llnode, struct ghes_estatus_node,
867 llnode);
868 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
869 generic = estatus_node->generic;
870 ghes_print_estatus(NULL, generic, estatus);
871 llnode = llnode->next;
875 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
876 enum fixed_addresses fixmap_idx)
878 struct acpi_hest_generic_status *estatus, tmp_header;
879 struct ghes_estatus_node *estatus_node;
880 u32 len, node_len;
881 u64 buf_paddr;
882 int sev, rc;
884 if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
885 return -EOPNOTSUPP;
887 rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
888 if (rc) {
889 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
890 return rc;
893 rc = __ghes_check_estatus(ghes, &tmp_header);
894 if (rc) {
895 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
896 return rc;
899 len = cper_estatus_len(&tmp_header);
900 node_len = GHES_ESTATUS_NODE_LEN(len);
901 estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
902 if (!estatus_node)
903 return -ENOMEM;
905 estatus_node->ghes = ghes;
906 estatus_node->generic = ghes->generic;
907 estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
909 if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
910 ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
911 rc = -ENOENT;
912 goto no_work;
915 sev = ghes_severity(estatus->error_severity);
916 if (sev >= GHES_SEV_PANIC) {
917 ghes_print_queued_estatus();
918 __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
921 ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
923 /* This error has been reported before, don't process it again. */
924 if (ghes_estatus_cached(estatus))
925 goto no_work;
927 llist_add(&estatus_node->llnode, &ghes_estatus_llist);
929 return rc;
931 no_work:
932 gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
933 node_len);
935 return rc;
938 static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
939 enum fixed_addresses fixmap_idx)
941 int ret = -ENOENT;
942 struct ghes *ghes;
944 rcu_read_lock();
945 list_for_each_entry_rcu(ghes, rcu_list, list) {
946 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
947 ret = 0;
949 rcu_read_unlock();
951 if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
952 irq_work_queue(&ghes_proc_irq_work);
954 return ret;
957 #ifdef CONFIG_ACPI_APEI_SEA
958 static LIST_HEAD(ghes_sea);
961 * Return 0 only if one of the SEA error sources successfully reported an error
962 * record sent from the firmware.
964 int ghes_notify_sea(void)
966 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
967 int rv;
969 raw_spin_lock(&ghes_notify_lock_sea);
970 rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
971 raw_spin_unlock(&ghes_notify_lock_sea);
973 return rv;
976 static void ghes_sea_add(struct ghes *ghes)
978 mutex_lock(&ghes_list_mutex);
979 list_add_rcu(&ghes->list, &ghes_sea);
980 mutex_unlock(&ghes_list_mutex);
983 static void ghes_sea_remove(struct ghes *ghes)
985 mutex_lock(&ghes_list_mutex);
986 list_del_rcu(&ghes->list);
987 mutex_unlock(&ghes_list_mutex);
988 synchronize_rcu();
990 #else /* CONFIG_ACPI_APEI_SEA */
991 static inline void ghes_sea_add(struct ghes *ghes) { }
992 static inline void ghes_sea_remove(struct ghes *ghes) { }
993 #endif /* CONFIG_ACPI_APEI_SEA */
995 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
997 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
998 * having only one concurrent reader.
1000 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1002 static LIST_HEAD(ghes_nmi);
1004 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1006 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1007 int ret = NMI_DONE;
1009 if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1010 return ret;
1012 raw_spin_lock(&ghes_notify_lock_nmi);
1013 if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1014 ret = NMI_HANDLED;
1015 raw_spin_unlock(&ghes_notify_lock_nmi);
1017 atomic_dec(&ghes_in_nmi);
1018 return ret;
1021 static void ghes_nmi_add(struct ghes *ghes)
1023 mutex_lock(&ghes_list_mutex);
1024 if (list_empty(&ghes_nmi))
1025 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1026 list_add_rcu(&ghes->list, &ghes_nmi);
1027 mutex_unlock(&ghes_list_mutex);
1030 static void ghes_nmi_remove(struct ghes *ghes)
1032 mutex_lock(&ghes_list_mutex);
1033 list_del_rcu(&ghes->list);
1034 if (list_empty(&ghes_nmi))
1035 unregister_nmi_handler(NMI_LOCAL, "ghes");
1036 mutex_unlock(&ghes_list_mutex);
1038 * To synchronize with NMI handler, ghes can only be
1039 * freed after NMI handler finishes.
1041 synchronize_rcu();
1043 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1044 static inline void ghes_nmi_add(struct ghes *ghes) { }
1045 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1046 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1048 static void ghes_nmi_init_cxt(void)
1050 init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1053 static int __ghes_sdei_callback(struct ghes *ghes,
1054 enum fixed_addresses fixmap_idx)
1056 if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1057 irq_work_queue(&ghes_proc_irq_work);
1059 return 0;
1062 return -ENOENT;
1065 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1066 void *arg)
1068 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1069 struct ghes *ghes = arg;
1070 int err;
1072 raw_spin_lock(&ghes_notify_lock_sdei_normal);
1073 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1074 raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1076 return err;
1079 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1080 void *arg)
1082 static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1083 struct ghes *ghes = arg;
1084 int err;
1086 raw_spin_lock(&ghes_notify_lock_sdei_critical);
1087 err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1088 raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1090 return err;
1093 static int apei_sdei_register_ghes(struct ghes *ghes)
1095 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1096 return -EOPNOTSUPP;
1098 return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1099 ghes_sdei_critical_callback);
1102 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1104 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1105 return -EOPNOTSUPP;
1107 return sdei_unregister_ghes(ghes);
1110 static int ghes_probe(struct platform_device *ghes_dev)
1112 struct acpi_hest_generic *generic;
1113 struct ghes *ghes = NULL;
1114 unsigned long flags;
1116 int rc = -EINVAL;
1118 generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1119 if (!generic->enabled)
1120 return -ENODEV;
1122 switch (generic->notify.type) {
1123 case ACPI_HEST_NOTIFY_POLLED:
1124 case ACPI_HEST_NOTIFY_EXTERNAL:
1125 case ACPI_HEST_NOTIFY_SCI:
1126 case ACPI_HEST_NOTIFY_GSIV:
1127 case ACPI_HEST_NOTIFY_GPIO:
1128 break;
1130 case ACPI_HEST_NOTIFY_SEA:
1131 if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1132 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1133 generic->header.source_id);
1134 rc = -ENOTSUPP;
1135 goto err;
1137 break;
1138 case ACPI_HEST_NOTIFY_NMI:
1139 if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1140 pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1141 generic->header.source_id);
1142 goto err;
1144 break;
1145 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1146 if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1147 pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1148 generic->header.source_id);
1149 goto err;
1151 break;
1152 case ACPI_HEST_NOTIFY_LOCAL:
1153 pr_warning(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1154 generic->header.source_id);
1155 goto err;
1156 default:
1157 pr_warning(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1158 generic->notify.type, generic->header.source_id);
1159 goto err;
1162 rc = -EIO;
1163 if (generic->error_block_length <
1164 sizeof(struct acpi_hest_generic_status)) {
1165 pr_warning(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1166 generic->error_block_length,
1167 generic->header.source_id);
1168 goto err;
1170 ghes = ghes_new(generic);
1171 if (IS_ERR(ghes)) {
1172 rc = PTR_ERR(ghes);
1173 ghes = NULL;
1174 goto err;
1177 switch (generic->notify.type) {
1178 case ACPI_HEST_NOTIFY_POLLED:
1179 timer_setup(&ghes->timer, ghes_poll_func, TIMER_DEFERRABLE);
1180 ghes_add_timer(ghes);
1181 break;
1182 case ACPI_HEST_NOTIFY_EXTERNAL:
1183 /* External interrupt vector is GSI */
1184 rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1185 if (rc) {
1186 pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1187 generic->header.source_id);
1188 goto err;
1190 rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1191 "GHES IRQ", ghes);
1192 if (rc) {
1193 pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1194 generic->header.source_id);
1195 goto err;
1197 break;
1199 case ACPI_HEST_NOTIFY_SCI:
1200 case ACPI_HEST_NOTIFY_GSIV:
1201 case ACPI_HEST_NOTIFY_GPIO:
1202 mutex_lock(&ghes_list_mutex);
1203 if (list_empty(&ghes_hed))
1204 register_acpi_hed_notifier(&ghes_notifier_hed);
1205 list_add_rcu(&ghes->list, &ghes_hed);
1206 mutex_unlock(&ghes_list_mutex);
1207 break;
1209 case ACPI_HEST_NOTIFY_SEA:
1210 ghes_sea_add(ghes);
1211 break;
1212 case ACPI_HEST_NOTIFY_NMI:
1213 ghes_nmi_add(ghes);
1214 break;
1215 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1216 rc = apei_sdei_register_ghes(ghes);
1217 if (rc)
1218 goto err;
1219 break;
1220 default:
1221 BUG();
1224 platform_set_drvdata(ghes_dev, ghes);
1226 ghes_edac_register(ghes, &ghes_dev->dev);
1228 /* Handle any pending errors right away */
1229 spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1230 ghes_proc(ghes);
1231 spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1233 return 0;
1235 err:
1236 if (ghes) {
1237 ghes_fini(ghes);
1238 kfree(ghes);
1240 return rc;
1243 static int ghes_remove(struct platform_device *ghes_dev)
1245 int rc;
1246 struct ghes *ghes;
1247 struct acpi_hest_generic *generic;
1249 ghes = platform_get_drvdata(ghes_dev);
1250 generic = ghes->generic;
1252 ghes->flags |= GHES_EXITING;
1253 switch (generic->notify.type) {
1254 case ACPI_HEST_NOTIFY_POLLED:
1255 del_timer_sync(&ghes->timer);
1256 break;
1257 case ACPI_HEST_NOTIFY_EXTERNAL:
1258 free_irq(ghes->irq, ghes);
1259 break;
1261 case ACPI_HEST_NOTIFY_SCI:
1262 case ACPI_HEST_NOTIFY_GSIV:
1263 case ACPI_HEST_NOTIFY_GPIO:
1264 mutex_lock(&ghes_list_mutex);
1265 list_del_rcu(&ghes->list);
1266 if (list_empty(&ghes_hed))
1267 unregister_acpi_hed_notifier(&ghes_notifier_hed);
1268 mutex_unlock(&ghes_list_mutex);
1269 synchronize_rcu();
1270 break;
1272 case ACPI_HEST_NOTIFY_SEA:
1273 ghes_sea_remove(ghes);
1274 break;
1275 case ACPI_HEST_NOTIFY_NMI:
1276 ghes_nmi_remove(ghes);
1277 break;
1278 case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1279 rc = apei_sdei_unregister_ghes(ghes);
1280 if (rc)
1281 return rc;
1282 break;
1283 default:
1284 BUG();
1285 break;
1288 ghes_fini(ghes);
1290 ghes_edac_unregister(ghes);
1292 kfree(ghes);
1294 platform_set_drvdata(ghes_dev, NULL);
1296 return 0;
1299 static struct platform_driver ghes_platform_driver = {
1300 .driver = {
1301 .name = "GHES",
1303 .probe = ghes_probe,
1304 .remove = ghes_remove,
1307 static int __init ghes_init(void)
1309 int rc;
1311 if (acpi_disabled)
1312 return -ENODEV;
1314 switch (hest_disable) {
1315 case HEST_NOT_FOUND:
1316 return -ENODEV;
1317 case HEST_DISABLED:
1318 pr_info(GHES_PFX "HEST is not enabled!\n");
1319 return -EINVAL;
1320 default:
1321 break;
1324 if (ghes_disable) {
1325 pr_info(GHES_PFX "GHES is not enabled!\n");
1326 return -EINVAL;
1329 ghes_nmi_init_cxt();
1331 rc = platform_driver_register(&ghes_platform_driver);
1332 if (rc)
1333 goto err;
1335 rc = apei_osc_setup();
1336 if (rc == 0 && osc_sb_apei_support_acked)
1337 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1338 else if (rc == 0 && !osc_sb_apei_support_acked)
1339 pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1340 else if (rc && osc_sb_apei_support_acked)
1341 pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1342 else
1343 pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1345 return 0;
1346 err:
1347 return rc;
1349 device_initcall(ghes_init);