Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * APEI Generic Hardware Error Source support
0004  *
0005  * Generic Hardware Error Source provides a way to report platform
0006  * hardware errors (such as that from chipset). It works in so called
0007  * "Firmware First" mode, that is, hardware errors are reported to
0008  * firmware firstly, then reported to Linux by firmware. This way,
0009  * some non-standard hardware error registers or non-standard hardware
0010  * link can be checked by firmware to produce more hardware error
0011  * information for Linux.
0012  *
0013  * For more information about Generic Hardware Error Source, please
0014  * refer to ACPI Specification version 4.0, section 17.3.2.6
0015  *
0016  * Copyright 2010,2011 Intel Corp.
0017  *   Author: Huang Ying <ying.huang@intel.com>
0018  */
0019 
0020 #include <linux/arm_sdei.h>
0021 #include <linux/kernel.h>
0022 #include <linux/moduleparam.h>
0023 #include <linux/init.h>
0024 #include <linux/acpi.h>
0025 #include <linux/io.h>
0026 #include <linux/interrupt.h>
0027 #include <linux/timer.h>
0028 #include <linux/cper.h>
0029 #include <linux/platform_device.h>
0030 #include <linux/mutex.h>
0031 #include <linux/ratelimit.h>
0032 #include <linux/vmalloc.h>
0033 #include <linux/irq_work.h>
0034 #include <linux/llist.h>
0035 #include <linux/genalloc.h>
0036 #include <linux/pci.h>
0037 #include <linux/pfn.h>
0038 #include <linux/aer.h>
0039 #include <linux/nmi.h>
0040 #include <linux/sched/clock.h>
0041 #include <linux/uuid.h>
0042 #include <linux/ras.h>
0043 #include <linux/task_work.h>
0044 
0045 #include <acpi/actbl1.h>
0046 #include <acpi/ghes.h>
0047 #include <acpi/apei.h>
0048 #include <asm/fixmap.h>
0049 #include <asm/tlbflush.h>
0050 #include <ras/ras_event.h>
0051 
0052 #include "apei-internal.h"
0053 
0054 #define GHES_PFX    "GHES: "
0055 
0056 #define GHES_ESTATUS_MAX_SIZE       65536
0057 #define GHES_ESOURCE_PREALLOC_MAX_SIZE  65536
0058 
0059 #define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3
0060 
0061 /* This is just an estimation for memory pool allocation */
0062 #define GHES_ESTATUS_CACHE_AVG_SIZE 512
0063 
0064 #define GHES_ESTATUS_CACHES_SIZE    4
0065 
0066 #define GHES_ESTATUS_IN_CACHE_MAX_NSEC  10000000000ULL
0067 /* Prevent too many caches are allocated because of RCU */
0068 #define GHES_ESTATUS_CACHE_ALLOCED_MAX  (GHES_ESTATUS_CACHES_SIZE * 3 / 2)
0069 
0070 #define GHES_ESTATUS_CACHE_LEN(estatus_len)         \
0071     (sizeof(struct ghes_estatus_cache) + (estatus_len))
0072 #define GHES_ESTATUS_FROM_CACHE(estatus_cache)          \
0073     ((struct acpi_hest_generic_status *)                \
0074      ((struct ghes_estatus_cache *)(estatus_cache) + 1))
0075 
0076 #define GHES_ESTATUS_NODE_LEN(estatus_len)          \
0077     (sizeof(struct ghes_estatus_node) + (estatus_len))
0078 #define GHES_ESTATUS_FROM_NODE(estatus_node)            \
0079     ((struct acpi_hest_generic_status *)                \
0080      ((struct ghes_estatus_node *)(estatus_node) + 1))
0081 
0082 #define GHES_VENDOR_ENTRY_LEN(gdata_len)                               \
0083     (sizeof(struct ghes_vendor_record_entry) + (gdata_len))
0084 #define GHES_GDATA_FROM_VENDOR_ENTRY(vendor_entry)                     \
0085     ((struct acpi_hest_generic_data *)                              \
0086     ((struct ghes_vendor_record_entry *)(vendor_entry) + 1))
0087 
0088 /*
0089  *  NMI-like notifications vary by architecture, before the compiler can prune
0090  *  unused static functions it needs a value for these enums.
0091  */
0092 #ifndef CONFIG_ARM_SDE_INTERFACE
0093 #define FIX_APEI_GHES_SDEI_NORMAL   __end_of_fixed_addresses
0094 #define FIX_APEI_GHES_SDEI_CRITICAL __end_of_fixed_addresses
0095 #endif
0096 
0097 static inline bool is_hest_type_generic_v2(struct ghes *ghes)
0098 {
0099     return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
0100 }
0101 
0102 /*
0103  * This driver isn't really modular, however for the time being,
0104  * continuing to use module_param is the easiest way to remain
0105  * compatible with existing boot arg use cases.
0106  */
0107 bool ghes_disable;
0108 module_param_named(disable, ghes_disable, bool, 0);
0109 
0110 /*
0111  * All error sources notified with HED (Hardware Error Device) share a
0112  * single notifier callback, so they need to be linked and checked one
0113  * by one. This holds true for NMI too.
0114  *
0115  * RCU is used for these lists, so ghes_list_mutex is only used for
0116  * list changing, not for traversing.
0117  */
0118 static LIST_HEAD(ghes_hed);
0119 static DEFINE_MUTEX(ghes_list_mutex);
0120 
0121 /*
0122  * Because the memory area used to transfer hardware error information
0123  * from BIOS to Linux can be determined only in NMI, IRQ or timer
0124  * handler, but general ioremap can not be used in atomic context, so
0125  * the fixmap is used instead.
0126  *
0127  * This spinlock is used to prevent the fixmap entry from being used
0128  * simultaneously.
0129  */
0130 static DEFINE_SPINLOCK(ghes_notify_lock_irq);
0131 
0132 struct ghes_vendor_record_entry {
0133     struct work_struct work;
0134     int error_severity;
0135     char vendor_record[];
0136 };
0137 
0138 static struct gen_pool *ghes_estatus_pool;
0139 static unsigned long ghes_estatus_pool_size_request;
0140 
0141 static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
0142 static atomic_t ghes_estatus_cache_alloced;
0143 
0144 static int ghes_panic_timeout __read_mostly = 30;
0145 
0146 static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
0147 {
0148     phys_addr_t paddr;
0149     pgprot_t prot;
0150 
0151     paddr = PFN_PHYS(pfn);
0152     prot = arch_apei_get_mem_attribute(paddr);
0153     __set_fixmap(fixmap_idx, paddr, prot);
0154 
0155     return (void __iomem *) __fix_to_virt(fixmap_idx);
0156 }
0157 
0158 static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
0159 {
0160     int _idx = virt_to_fix((unsigned long)vaddr);
0161 
0162     WARN_ON_ONCE(fixmap_idx != _idx);
0163     clear_fixmap(fixmap_idx);
0164 }
0165 
0166 int ghes_estatus_pool_init(int num_ghes)
0167 {
0168     unsigned long addr, len;
0169     int rc;
0170 
0171     ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
0172     if (!ghes_estatus_pool)
0173         return -ENOMEM;
0174 
0175     len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
0176     len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
0177 
0178     ghes_estatus_pool_size_request = PAGE_ALIGN(len);
0179     addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
0180     if (!addr)
0181         goto err_pool_alloc;
0182 
0183     rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
0184     if (rc)
0185         goto err_pool_add;
0186 
0187     return 0;
0188 
0189 err_pool_add:
0190     vfree((void *)addr);
0191 
0192 err_pool_alloc:
0193     gen_pool_destroy(ghes_estatus_pool);
0194 
0195     return -ENOMEM;
0196 }
0197 
0198 static int map_gen_v2(struct ghes *ghes)
0199 {
0200     return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
0201 }
0202 
0203 static void unmap_gen_v2(struct ghes *ghes)
0204 {
0205     apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
0206 }
0207 
0208 static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
0209 {
0210     int rc;
0211     u64 val = 0;
0212 
0213     rc = apei_read(&val, &gv2->read_ack_register);
0214     if (rc)
0215         return;
0216 
0217     val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
0218     val |= gv2->read_ack_write    << gv2->read_ack_register.bit_offset;
0219 
0220     apei_write(val, &gv2->read_ack_register);
0221 }
0222 
0223 static struct ghes *ghes_new(struct acpi_hest_generic *generic)
0224 {
0225     struct ghes *ghes;
0226     unsigned int error_block_length;
0227     int rc;
0228 
0229     ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
0230     if (!ghes)
0231         return ERR_PTR(-ENOMEM);
0232 
0233     ghes->generic = generic;
0234     if (is_hest_type_generic_v2(ghes)) {
0235         rc = map_gen_v2(ghes);
0236         if (rc)
0237             goto err_free;
0238     }
0239 
0240     rc = apei_map_generic_address(&generic->error_status_address);
0241     if (rc)
0242         goto err_unmap_read_ack_addr;
0243     error_block_length = generic->error_block_length;
0244     if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
0245         pr_warn(FW_WARN GHES_PFX
0246             "Error status block length is too long: %u for "
0247             "generic hardware error source: %d.\n",
0248             error_block_length, generic->header.source_id);
0249         error_block_length = GHES_ESTATUS_MAX_SIZE;
0250     }
0251     ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
0252     if (!ghes->estatus) {
0253         rc = -ENOMEM;
0254         goto err_unmap_status_addr;
0255     }
0256 
0257     return ghes;
0258 
0259 err_unmap_status_addr:
0260     apei_unmap_generic_address(&generic->error_status_address);
0261 err_unmap_read_ack_addr:
0262     if (is_hest_type_generic_v2(ghes))
0263         unmap_gen_v2(ghes);
0264 err_free:
0265     kfree(ghes);
0266     return ERR_PTR(rc);
0267 }
0268 
0269 static void ghes_fini(struct ghes *ghes)
0270 {
0271     kfree(ghes->estatus);
0272     apei_unmap_generic_address(&ghes->generic->error_status_address);
0273     if (is_hest_type_generic_v2(ghes))
0274         unmap_gen_v2(ghes);
0275 }
0276 
0277 static inline int ghes_severity(int severity)
0278 {
0279     switch (severity) {
0280     case CPER_SEV_INFORMATIONAL:
0281         return GHES_SEV_NO;
0282     case CPER_SEV_CORRECTED:
0283         return GHES_SEV_CORRECTED;
0284     case CPER_SEV_RECOVERABLE:
0285         return GHES_SEV_RECOVERABLE;
0286     case CPER_SEV_FATAL:
0287         return GHES_SEV_PANIC;
0288     default:
0289         /* Unknown, go panic */
0290         return GHES_SEV_PANIC;
0291     }
0292 }
0293 
0294 static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
0295                   int from_phys,
0296                   enum fixed_addresses fixmap_idx)
0297 {
0298     void __iomem *vaddr;
0299     u64 offset;
0300     u32 trunk;
0301 
0302     while (len > 0) {
0303         offset = paddr - (paddr & PAGE_MASK);
0304         vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
0305         trunk = PAGE_SIZE - offset;
0306         trunk = min(trunk, len);
0307         if (from_phys)
0308             memcpy_fromio(buffer, vaddr + offset, trunk);
0309         else
0310             memcpy_toio(vaddr + offset, buffer, trunk);
0311         len -= trunk;
0312         paddr += trunk;
0313         buffer += trunk;
0314         ghes_unmap(vaddr, fixmap_idx);
0315     }
0316 }
0317 
0318 /* Check the top-level record header has an appropriate size. */
0319 static int __ghes_check_estatus(struct ghes *ghes,
0320                 struct acpi_hest_generic_status *estatus)
0321 {
0322     u32 len = cper_estatus_len(estatus);
0323 
0324     if (len < sizeof(*estatus)) {
0325         pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
0326         return -EIO;
0327     }
0328 
0329     if (len > ghes->generic->error_block_length) {
0330         pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
0331         return -EIO;
0332     }
0333 
0334     if (cper_estatus_check_header(estatus)) {
0335         pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
0336         return -EIO;
0337     }
0338 
0339     return 0;
0340 }
0341 
0342 /* Read the CPER block, returning its address, and header in estatus. */
0343 static int __ghes_peek_estatus(struct ghes *ghes,
0344                    struct acpi_hest_generic_status *estatus,
0345                    u64 *buf_paddr, enum fixed_addresses fixmap_idx)
0346 {
0347     struct acpi_hest_generic *g = ghes->generic;
0348     int rc;
0349 
0350     rc = apei_read(buf_paddr, &g->error_status_address);
0351     if (rc) {
0352         *buf_paddr = 0;
0353         pr_warn_ratelimited(FW_WARN GHES_PFX
0354 "Failed to read error status block address for hardware error source: %d.\n",
0355                    g->header.source_id);
0356         return -EIO;
0357     }
0358     if (!*buf_paddr)
0359         return -ENOENT;
0360 
0361     ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
0362                   fixmap_idx);
0363     if (!estatus->block_status) {
0364         *buf_paddr = 0;
0365         return -ENOENT;
0366     }
0367 
0368     return 0;
0369 }
0370 
0371 static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
0372                    u64 buf_paddr, enum fixed_addresses fixmap_idx,
0373                    size_t buf_len)
0374 {
0375     ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
0376     if (cper_estatus_check(estatus)) {
0377         pr_warn_ratelimited(FW_WARN GHES_PFX
0378                     "Failed to read error status block!\n");
0379         return -EIO;
0380     }
0381 
0382     return 0;
0383 }
0384 
0385 static int ghes_read_estatus(struct ghes *ghes,
0386                  struct acpi_hest_generic_status *estatus,
0387                  u64 *buf_paddr, enum fixed_addresses fixmap_idx)
0388 {
0389     int rc;
0390 
0391     rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
0392     if (rc)
0393         return rc;
0394 
0395     rc = __ghes_check_estatus(ghes, estatus);
0396     if (rc)
0397         return rc;
0398 
0399     return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
0400                    cper_estatus_len(estatus));
0401 }
0402 
0403 static void ghes_clear_estatus(struct ghes *ghes,
0404                    struct acpi_hest_generic_status *estatus,
0405                    u64 buf_paddr, enum fixed_addresses fixmap_idx)
0406 {
0407     estatus->block_status = 0;
0408 
0409     if (!buf_paddr)
0410         return;
0411 
0412     ghes_copy_tofrom_phys(estatus, buf_paddr,
0413                   sizeof(estatus->block_status), 0,
0414                   fixmap_idx);
0415 
0416     /*
0417      * GHESv2 type HEST entries introduce support for error acknowledgment,
0418      * so only acknowledge the error if this support is present.
0419      */
0420     if (is_hest_type_generic_v2(ghes))
0421         ghes_ack_error(ghes->generic_v2);
0422 }
0423 
0424 /*
0425  * Called as task_work before returning to user-space.
0426  * Ensure any queued work has been done before we return to the context that
0427  * triggered the notification.
0428  */
0429 static void ghes_kick_task_work(struct callback_head *head)
0430 {
0431     struct acpi_hest_generic_status *estatus;
0432     struct ghes_estatus_node *estatus_node;
0433     u32 node_len;
0434 
0435     estatus_node = container_of(head, struct ghes_estatus_node, task_work);
0436     if (IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))
0437         memory_failure_queue_kick(estatus_node->task_work_cpu);
0438 
0439     estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
0440     node_len = GHES_ESTATUS_NODE_LEN(cper_estatus_len(estatus));
0441     gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node, node_len);
0442 }
0443 
0444 static bool ghes_do_memory_failure(u64 physical_addr, int flags)
0445 {
0446     unsigned long pfn;
0447 
0448     if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))
0449         return false;
0450 
0451     pfn = PHYS_PFN(physical_addr);
0452     if (!pfn_valid(pfn) && !arch_is_platform_page(physical_addr)) {
0453         pr_warn_ratelimited(FW_WARN GHES_PFX
0454         "Invalid address in generic error data: %#llx\n",
0455         physical_addr);
0456         return false;
0457     }
0458 
0459     memory_failure_queue(pfn, flags);
0460     return true;
0461 }
0462 
0463 static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
0464                        int sev)
0465 {
0466     int flags = -1;
0467     int sec_sev = ghes_severity(gdata->error_severity);
0468     struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
0469 
0470     if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
0471         return false;
0472 
0473     /* iff following two events can be handled properly by now */
0474     if (sec_sev == GHES_SEV_CORRECTED &&
0475         (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
0476         flags = MF_SOFT_OFFLINE;
0477     if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
0478         flags = 0;
0479 
0480     if (flags != -1)
0481         return ghes_do_memory_failure(mem_err->physical_addr, flags);
0482 
0483     return false;
0484 }
0485 
0486 static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata, int sev)
0487 {
0488     struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
0489     bool queued = false;
0490     int sec_sev, i;
0491     char *p;
0492 
0493     log_arm_hw_error(err);
0494 
0495     sec_sev = ghes_severity(gdata->error_severity);
0496     if (sev != GHES_SEV_RECOVERABLE || sec_sev != GHES_SEV_RECOVERABLE)
0497         return false;
0498 
0499     p = (char *)(err + 1);
0500     for (i = 0; i < err->err_info_num; i++) {
0501         struct cper_arm_err_info *err_info = (struct cper_arm_err_info *)p;
0502         bool is_cache = (err_info->type == CPER_ARM_CACHE_ERROR);
0503         bool has_pa = (err_info->validation_bits & CPER_ARM_INFO_VALID_PHYSICAL_ADDR);
0504         const char *error_type = "unknown error";
0505 
0506         /*
0507          * The field (err_info->error_info & BIT(26)) is fixed to set to
0508          * 1 in some old firmware of HiSilicon Kunpeng920. We assume that
0509          * firmware won't mix corrected errors in an uncorrected section,
0510          * and don't filter out 'corrected' error here.
0511          */
0512         if (is_cache && has_pa) {
0513             queued = ghes_do_memory_failure(err_info->physical_fault_addr, 0);
0514             p += err_info->length;
0515             continue;
0516         }
0517 
0518         if (err_info->type < ARRAY_SIZE(cper_proc_error_type_strs))
0519             error_type = cper_proc_error_type_strs[err_info->type];
0520 
0521         pr_warn_ratelimited(FW_WARN GHES_PFX
0522                     "Unhandled processor error type: %s\n",
0523                     error_type);
0524         p += err_info->length;
0525     }
0526 
0527     return queued;
0528 }
0529 
0530 /*
0531  * PCIe AER errors need to be sent to the AER driver for reporting and
0532  * recovery. The GHES severities map to the following AER severities and
0533  * require the following handling:
0534  *
0535  * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
0536  *     These need to be reported by the AER driver but no recovery is
0537  *     necessary.
0538  * GHES_SEV_RECOVERABLE -> AER_NONFATAL
0539  * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
0540  *     These both need to be reported and recovered from by the AER driver.
0541  * GHES_SEV_PANIC does not make it to this handling since the kernel must
0542  *     panic.
0543  */
0544 static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
0545 {
0546 #ifdef CONFIG_ACPI_APEI_PCIEAER
0547     struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);
0548 
0549     if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
0550         pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
0551         unsigned int devfn;
0552         int aer_severity;
0553 
0554         devfn = PCI_DEVFN(pcie_err->device_id.device,
0555                   pcie_err->device_id.function);
0556         aer_severity = cper_severity_to_aer(gdata->error_severity);
0557 
0558         /*
0559          * If firmware reset the component to contain
0560          * the error, we must reinitialize it before
0561          * use, so treat it as a fatal AER error.
0562          */
0563         if (gdata->flags & CPER_SEC_RESET)
0564             aer_severity = AER_FATAL;
0565 
0566         aer_recover_queue(pcie_err->device_id.segment,
0567                   pcie_err->device_id.bus,
0568                   devfn, aer_severity,
0569                   (struct aer_capability_regs *)
0570                   pcie_err->aer_info);
0571     }
0572 #endif
0573 }
0574 
0575 static BLOCKING_NOTIFIER_HEAD(vendor_record_notify_list);
0576 
0577 int ghes_register_vendor_record_notifier(struct notifier_block *nb)
0578 {
0579     return blocking_notifier_chain_register(&vendor_record_notify_list, nb);
0580 }
0581 EXPORT_SYMBOL_GPL(ghes_register_vendor_record_notifier);
0582 
0583 void ghes_unregister_vendor_record_notifier(struct notifier_block *nb)
0584 {
0585     blocking_notifier_chain_unregister(&vendor_record_notify_list, nb);
0586 }
0587 EXPORT_SYMBOL_GPL(ghes_unregister_vendor_record_notifier);
0588 
0589 static void ghes_vendor_record_work_func(struct work_struct *work)
0590 {
0591     struct ghes_vendor_record_entry *entry;
0592     struct acpi_hest_generic_data *gdata;
0593     u32 len;
0594 
0595     entry = container_of(work, struct ghes_vendor_record_entry, work);
0596     gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);
0597 
0598     blocking_notifier_call_chain(&vendor_record_notify_list,
0599                      entry->error_severity, gdata);
0600 
0601     len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
0602     gen_pool_free(ghes_estatus_pool, (unsigned long)entry, len);
0603 }
0604 
0605 static void ghes_defer_non_standard_event(struct acpi_hest_generic_data *gdata,
0606                       int sev)
0607 {
0608     struct acpi_hest_generic_data *copied_gdata;
0609     struct ghes_vendor_record_entry *entry;
0610     u32 len;
0611 
0612     len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
0613     entry = (void *)gen_pool_alloc(ghes_estatus_pool, len);
0614     if (!entry)
0615         return;
0616 
0617     copied_gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);
0618     memcpy(copied_gdata, gdata, acpi_hest_get_record_size(gdata));
0619     entry->error_severity = sev;
0620 
0621     INIT_WORK(&entry->work, ghes_vendor_record_work_func);
0622     schedule_work(&entry->work);
0623 }
0624 
0625 static bool ghes_do_proc(struct ghes *ghes,
0626              const struct acpi_hest_generic_status *estatus)
0627 {
0628     int sev, sec_sev;
0629     struct acpi_hest_generic_data *gdata;
0630     guid_t *sec_type;
0631     const guid_t *fru_id = &guid_null;
0632     char *fru_text = "";
0633     bool queued = false;
0634 
0635     sev = ghes_severity(estatus->error_severity);
0636     apei_estatus_for_each_section(estatus, gdata) {
0637         sec_type = (guid_t *)gdata->section_type;
0638         sec_sev = ghes_severity(gdata->error_severity);
0639         if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
0640             fru_id = (guid_t *)gdata->fru_id;
0641 
0642         if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
0643             fru_text = gdata->fru_text;
0644 
0645         if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
0646             struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);
0647 
0648             ghes_edac_report_mem_error(sev, mem_err);
0649 
0650             arch_apei_report_mem_error(sev, mem_err);
0651             queued = ghes_handle_memory_failure(gdata, sev);
0652         }
0653         else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
0654             ghes_handle_aer(gdata);
0655         }
0656         else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
0657             queued = ghes_handle_arm_hw_error(gdata, sev);
0658         } else {
0659             void *err = acpi_hest_get_payload(gdata);
0660 
0661             ghes_defer_non_standard_event(gdata, sev);
0662             log_non_standard_event(sec_type, fru_id, fru_text,
0663                            sec_sev, err,
0664                            gdata->error_data_length);
0665         }
0666     }
0667 
0668     return queued;
0669 }
0670 
0671 static void __ghes_print_estatus(const char *pfx,
0672                  const struct acpi_hest_generic *generic,
0673                  const struct acpi_hest_generic_status *estatus)
0674 {
0675     static atomic_t seqno;
0676     unsigned int curr_seqno;
0677     char pfx_seq[64];
0678 
0679     if (pfx == NULL) {
0680         if (ghes_severity(estatus->error_severity) <=
0681             GHES_SEV_CORRECTED)
0682             pfx = KERN_WARNING;
0683         else
0684             pfx = KERN_ERR;
0685     }
0686     curr_seqno = atomic_inc_return(&seqno);
0687     snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
0688     printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
0689            pfx_seq, generic->header.source_id);
0690     cper_estatus_print(pfx_seq, estatus);
0691 }
0692 
0693 static int ghes_print_estatus(const char *pfx,
0694                   const struct acpi_hest_generic *generic,
0695                   const struct acpi_hest_generic_status *estatus)
0696 {
0697     /* Not more than 2 messages every 5 seconds */
0698     static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
0699     static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
0700     struct ratelimit_state *ratelimit;
0701 
0702     if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
0703         ratelimit = &ratelimit_corrected;
0704     else
0705         ratelimit = &ratelimit_uncorrected;
0706     if (__ratelimit(ratelimit)) {
0707         __ghes_print_estatus(pfx, generic, estatus);
0708         return 1;
0709     }
0710     return 0;
0711 }
0712 
0713 /*
0714  * GHES error status reporting throttle, to report more kinds of
0715  * errors, instead of just most frequently occurred errors.
0716  */
0717 static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
0718 {
0719     u32 len;
0720     int i, cached = 0;
0721     unsigned long long now;
0722     struct ghes_estatus_cache *cache;
0723     struct acpi_hest_generic_status *cache_estatus;
0724 
0725     len = cper_estatus_len(estatus);
0726     rcu_read_lock();
0727     for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
0728         cache = rcu_dereference(ghes_estatus_caches[i]);
0729         if (cache == NULL)
0730             continue;
0731         if (len != cache->estatus_len)
0732             continue;
0733         cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
0734         if (memcmp(estatus, cache_estatus, len))
0735             continue;
0736         atomic_inc(&cache->count);
0737         now = sched_clock();
0738         if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
0739             cached = 1;
0740         break;
0741     }
0742     rcu_read_unlock();
0743     return cached;
0744 }
0745 
0746 static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
0747     struct acpi_hest_generic *generic,
0748     struct acpi_hest_generic_status *estatus)
0749 {
0750     int alloced;
0751     u32 len, cache_len;
0752     struct ghes_estatus_cache *cache;
0753     struct acpi_hest_generic_status *cache_estatus;
0754 
0755     alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
0756     if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
0757         atomic_dec(&ghes_estatus_cache_alloced);
0758         return NULL;
0759     }
0760     len = cper_estatus_len(estatus);
0761     cache_len = GHES_ESTATUS_CACHE_LEN(len);
0762     cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
0763     if (!cache) {
0764         atomic_dec(&ghes_estatus_cache_alloced);
0765         return NULL;
0766     }
0767     cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
0768     memcpy(cache_estatus, estatus, len);
0769     cache->estatus_len = len;
0770     atomic_set(&cache->count, 0);
0771     cache->generic = generic;
0772     cache->time_in = sched_clock();
0773     return cache;
0774 }
0775 
0776 static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
0777 {
0778     u32 len;
0779 
0780     len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
0781     len = GHES_ESTATUS_CACHE_LEN(len);
0782     gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
0783     atomic_dec(&ghes_estatus_cache_alloced);
0784 }
0785 
0786 static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
0787 {
0788     struct ghes_estatus_cache *cache;
0789 
0790     cache = container_of(head, struct ghes_estatus_cache, rcu);
0791     ghes_estatus_cache_free(cache);
0792 }
0793 
0794 static void ghes_estatus_cache_add(
0795     struct acpi_hest_generic *generic,
0796     struct acpi_hest_generic_status *estatus)
0797 {
0798     int i, slot = -1, count;
0799     unsigned long long now, duration, period, max_period = 0;
0800     struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;
0801 
0802     new_cache = ghes_estatus_cache_alloc(generic, estatus);
0803     if (new_cache == NULL)
0804         return;
0805     rcu_read_lock();
0806     now = sched_clock();
0807     for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
0808         cache = rcu_dereference(ghes_estatus_caches[i]);
0809         if (cache == NULL) {
0810             slot = i;
0811             slot_cache = NULL;
0812             break;
0813         }
0814         duration = now - cache->time_in;
0815         if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
0816             slot = i;
0817             slot_cache = cache;
0818             break;
0819         }
0820         count = atomic_read(&cache->count);
0821         period = duration;
0822         do_div(period, (count + 1));
0823         if (period > max_period) {
0824             max_period = period;
0825             slot = i;
0826             slot_cache = cache;
0827         }
0828     }
0829     /* new_cache must be put into array after its contents are written */
0830     smp_wmb();
0831     if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
0832                   slot_cache, new_cache) == slot_cache) {
0833         if (slot_cache)
0834             call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
0835     } else
0836         ghes_estatus_cache_free(new_cache);
0837     rcu_read_unlock();
0838 }
0839 
0840 static void __ghes_panic(struct ghes *ghes,
0841              struct acpi_hest_generic_status *estatus,
0842              u64 buf_paddr, enum fixed_addresses fixmap_idx)
0843 {
0844     __ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
0845 
0846     ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
0847 
0848     /* reboot to log the error! */
0849     if (!panic_timeout)
0850         panic_timeout = ghes_panic_timeout;
0851     panic("Fatal hardware error!");
0852 }
0853 
0854 static int ghes_proc(struct ghes *ghes)
0855 {
0856     struct acpi_hest_generic_status *estatus = ghes->estatus;
0857     u64 buf_paddr;
0858     int rc;
0859 
0860     rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
0861     if (rc)
0862         goto out;
0863 
0864     if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
0865         __ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
0866 
0867     if (!ghes_estatus_cached(estatus)) {
0868         if (ghes_print_estatus(NULL, ghes->generic, estatus))
0869             ghes_estatus_cache_add(ghes->generic, estatus);
0870     }
0871     ghes_do_proc(ghes, estatus);
0872 
0873 out:
0874     ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
0875 
0876     return rc;
0877 }
0878 
0879 static void ghes_add_timer(struct ghes *ghes)
0880 {
0881     struct acpi_hest_generic *g = ghes->generic;
0882     unsigned long expire;
0883 
0884     if (!g->notify.poll_interval) {
0885         pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
0886             g->header.source_id);
0887         return;
0888     }
0889     expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
0890     ghes->timer.expires = round_jiffies_relative(expire);
0891     add_timer(&ghes->timer);
0892 }
0893 
0894 static void ghes_poll_func(struct timer_list *t)
0895 {
0896     struct ghes *ghes = from_timer(ghes, t, timer);
0897     unsigned long flags;
0898 
0899     spin_lock_irqsave(&ghes_notify_lock_irq, flags);
0900     ghes_proc(ghes);
0901     spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
0902     if (!(ghes->flags & GHES_EXITING))
0903         ghes_add_timer(ghes);
0904 }
0905 
0906 static irqreturn_t ghes_irq_func(int irq, void *data)
0907 {
0908     struct ghes *ghes = data;
0909     unsigned long flags;
0910     int rc;
0911 
0912     spin_lock_irqsave(&ghes_notify_lock_irq, flags);
0913     rc = ghes_proc(ghes);
0914     spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
0915     if (rc)
0916         return IRQ_NONE;
0917 
0918     return IRQ_HANDLED;
0919 }
0920 
0921 static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
0922                void *data)
0923 {
0924     struct ghes *ghes;
0925     unsigned long flags;
0926     int ret = NOTIFY_DONE;
0927 
0928     spin_lock_irqsave(&ghes_notify_lock_irq, flags);
0929     rcu_read_lock();
0930     list_for_each_entry_rcu(ghes, &ghes_hed, list) {
0931         if (!ghes_proc(ghes))
0932             ret = NOTIFY_OK;
0933     }
0934     rcu_read_unlock();
0935     spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
0936 
0937     return ret;
0938 }
0939 
0940 static struct notifier_block ghes_notifier_hed = {
0941     .notifier_call = ghes_notify_hed,
0942 };
0943 
0944 /*
0945  * Handlers for CPER records may not be NMI safe. For example,
0946  * memory_failure_queue() takes spinlocks and calls schedule_work_on().
0947  * In any NMI-like handler, memory from ghes_estatus_pool is used to save
0948  * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
0949  * ghes_proc_in_irq() to run in IRQ context where each estatus in
0950  * ghes_estatus_llist is processed.
0951  *
0952  * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
0953  * to suppress frequent messages.
0954  */
0955 static struct llist_head ghes_estatus_llist;
0956 static struct irq_work ghes_proc_irq_work;
0957 
0958 static void ghes_proc_in_irq(struct irq_work *irq_work)
0959 {
0960     struct llist_node *llnode, *next;
0961     struct ghes_estatus_node *estatus_node;
0962     struct acpi_hest_generic *generic;
0963     struct acpi_hest_generic_status *estatus;
0964     bool task_work_pending;
0965     u32 len, node_len;
0966     int ret;
0967 
0968     llnode = llist_del_all(&ghes_estatus_llist);
0969     /*
0970      * Because the time order of estatus in list is reversed,
0971      * revert it back to proper order.
0972      */
0973     llnode = llist_reverse_order(llnode);
0974     while (llnode) {
0975         next = llnode->next;
0976         estatus_node = llist_entry(llnode, struct ghes_estatus_node,
0977                        llnode);
0978         estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
0979         len = cper_estatus_len(estatus);
0980         node_len = GHES_ESTATUS_NODE_LEN(len);
0981         task_work_pending = ghes_do_proc(estatus_node->ghes, estatus);
0982         if (!ghes_estatus_cached(estatus)) {
0983             generic = estatus_node->generic;
0984             if (ghes_print_estatus(NULL, generic, estatus))
0985                 ghes_estatus_cache_add(generic, estatus);
0986         }
0987 
0988         if (task_work_pending && current->mm != &init_mm) {
0989             estatus_node->task_work.func = ghes_kick_task_work;
0990             estatus_node->task_work_cpu = smp_processor_id();
0991             ret = task_work_add(current, &estatus_node->task_work,
0992                         TWA_RESUME);
0993             if (ret)
0994                 estatus_node->task_work.func = NULL;
0995         }
0996 
0997         if (!estatus_node->task_work.func)
0998             gen_pool_free(ghes_estatus_pool,
0999                       (unsigned long)estatus_node, node_len);
1000 
1001         llnode = next;
1002     }
1003 }
1004 
1005 static void ghes_print_queued_estatus(void)
1006 {
1007     struct llist_node *llnode;
1008     struct ghes_estatus_node *estatus_node;
1009     struct acpi_hest_generic *generic;
1010     struct acpi_hest_generic_status *estatus;
1011 
1012     llnode = llist_del_all(&ghes_estatus_llist);
1013     /*
1014      * Because the time order of estatus in list is reversed,
1015      * revert it back to proper order.
1016      */
1017     llnode = llist_reverse_order(llnode);
1018     while (llnode) {
1019         estatus_node = llist_entry(llnode, struct ghes_estatus_node,
1020                        llnode);
1021         estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1022         generic = estatus_node->generic;
1023         ghes_print_estatus(NULL, generic, estatus);
1024         llnode = llnode->next;
1025     }
1026 }
1027 
1028 static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
1029                        enum fixed_addresses fixmap_idx)
1030 {
1031     struct acpi_hest_generic_status *estatus, tmp_header;
1032     struct ghes_estatus_node *estatus_node;
1033     u32 len, node_len;
1034     u64 buf_paddr;
1035     int sev, rc;
1036 
1037     if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
1038         return -EOPNOTSUPP;
1039 
1040     rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
1041     if (rc) {
1042         ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1043         return rc;
1044     }
1045 
1046     rc = __ghes_check_estatus(ghes, &tmp_header);
1047     if (rc) {
1048         ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1049         return rc;
1050     }
1051 
1052     len = cper_estatus_len(&tmp_header);
1053     node_len = GHES_ESTATUS_NODE_LEN(len);
1054     estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
1055     if (!estatus_node)
1056         return -ENOMEM;
1057 
1058     estatus_node->ghes = ghes;
1059     estatus_node->generic = ghes->generic;
1060     estatus_node->task_work.func = NULL;
1061     estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
1062 
1063     if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
1064         ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1065         rc = -ENOENT;
1066         goto no_work;
1067     }
1068 
1069     sev = ghes_severity(estatus->error_severity);
1070     if (sev >= GHES_SEV_PANIC) {
1071         ghes_print_queued_estatus();
1072         __ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
1073     }
1074 
1075     ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1076 
1077     /* This error has been reported before, don't process it again. */
1078     if (ghes_estatus_cached(estatus))
1079         goto no_work;
1080 
1081     llist_add(&estatus_node->llnode, &ghes_estatus_llist);
1082 
1083     return rc;
1084 
1085 no_work:
1086     gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
1087               node_len);
1088 
1089     return rc;
1090 }
1091 
1092 static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
1093                        enum fixed_addresses fixmap_idx)
1094 {
1095     int ret = -ENOENT;
1096     struct ghes *ghes;
1097 
1098     rcu_read_lock();
1099     list_for_each_entry_rcu(ghes, rcu_list, list) {
1100         if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
1101             ret = 0;
1102     }
1103     rcu_read_unlock();
1104 
1105     if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
1106         irq_work_queue(&ghes_proc_irq_work);
1107 
1108     return ret;
1109 }
1110 
1111 #ifdef CONFIG_ACPI_APEI_SEA
1112 static LIST_HEAD(ghes_sea);
1113 
1114 /*
1115  * Return 0 only if one of the SEA error sources successfully reported an error
1116  * record sent from the firmware.
1117  */
1118 int ghes_notify_sea(void)
1119 {
1120     static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
1121     int rv;
1122 
1123     raw_spin_lock(&ghes_notify_lock_sea);
1124     rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
1125     raw_spin_unlock(&ghes_notify_lock_sea);
1126 
1127     return rv;
1128 }
1129 
1130 static void ghes_sea_add(struct ghes *ghes)
1131 {
1132     mutex_lock(&ghes_list_mutex);
1133     list_add_rcu(&ghes->list, &ghes_sea);
1134     mutex_unlock(&ghes_list_mutex);
1135 }
1136 
1137 static void ghes_sea_remove(struct ghes *ghes)
1138 {
1139     mutex_lock(&ghes_list_mutex);
1140     list_del_rcu(&ghes->list);
1141     mutex_unlock(&ghes_list_mutex);
1142     synchronize_rcu();
1143 }
1144 #else /* CONFIG_ACPI_APEI_SEA */
1145 static inline void ghes_sea_add(struct ghes *ghes) { }
1146 static inline void ghes_sea_remove(struct ghes *ghes) { }
1147 #endif /* CONFIG_ACPI_APEI_SEA */
1148 
1149 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
1150 /*
1151  * NMI may be triggered on any CPU, so ghes_in_nmi is used for
1152  * having only one concurrent reader.
1153  */
1154 static atomic_t ghes_in_nmi = ATOMIC_INIT(0);
1155 
1156 static LIST_HEAD(ghes_nmi);
1157 
1158 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
1159 {
1160     static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1161     int ret = NMI_DONE;
1162 
1163     if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
1164         return ret;
1165 
1166     raw_spin_lock(&ghes_notify_lock_nmi);
1167     if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1168         ret = NMI_HANDLED;
1169     raw_spin_unlock(&ghes_notify_lock_nmi);
1170 
1171     atomic_dec(&ghes_in_nmi);
1172     return ret;
1173 }
1174 
1175 static void ghes_nmi_add(struct ghes *ghes)
1176 {
1177     mutex_lock(&ghes_list_mutex);
1178     if (list_empty(&ghes_nmi))
1179         register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
1180     list_add_rcu(&ghes->list, &ghes_nmi);
1181     mutex_unlock(&ghes_list_mutex);
1182 }
1183 
1184 static void ghes_nmi_remove(struct ghes *ghes)
1185 {
1186     mutex_lock(&ghes_list_mutex);
1187     list_del_rcu(&ghes->list);
1188     if (list_empty(&ghes_nmi))
1189         unregister_nmi_handler(NMI_LOCAL, "ghes");
1190     mutex_unlock(&ghes_list_mutex);
1191     /*
1192      * To synchronize with NMI handler, ghes can only be
1193      * freed after NMI handler finishes.
1194      */
1195     synchronize_rcu();
1196 }
1197 #else /* CONFIG_HAVE_ACPI_APEI_NMI */
1198 static inline void ghes_nmi_add(struct ghes *ghes) { }
1199 static inline void ghes_nmi_remove(struct ghes *ghes) { }
1200 #endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1201 
1202 static void ghes_nmi_init_cxt(void)
1203 {
1204     init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
1205 }
1206 
1207 static int __ghes_sdei_callback(struct ghes *ghes,
1208                 enum fixed_addresses fixmap_idx)
1209 {
1210     if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
1211         irq_work_queue(&ghes_proc_irq_work);
1212 
1213         return 0;
1214     }
1215 
1216     return -ENOENT;
1217 }
1218 
1219 static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
1220                       void *arg)
1221 {
1222     static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
1223     struct ghes *ghes = arg;
1224     int err;
1225 
1226     raw_spin_lock(&ghes_notify_lock_sdei_normal);
1227     err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
1228     raw_spin_unlock(&ghes_notify_lock_sdei_normal);
1229 
1230     return err;
1231 }
1232 
1233 static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
1234                        void *arg)
1235 {
1236     static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
1237     struct ghes *ghes = arg;
1238     int err;
1239 
1240     raw_spin_lock(&ghes_notify_lock_sdei_critical);
1241     err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
1242     raw_spin_unlock(&ghes_notify_lock_sdei_critical);
1243 
1244     return err;
1245 }
1246 
1247 static int apei_sdei_register_ghes(struct ghes *ghes)
1248 {
1249     if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1250         return -EOPNOTSUPP;
1251 
1252     return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
1253                  ghes_sdei_critical_callback);
1254 }
1255 
1256 static int apei_sdei_unregister_ghes(struct ghes *ghes)
1257 {
1258     if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
1259         return -EOPNOTSUPP;
1260 
1261     return sdei_unregister_ghes(ghes);
1262 }
1263 
1264 static int ghes_probe(struct platform_device *ghes_dev)
1265 {
1266     struct acpi_hest_generic *generic;
1267     struct ghes *ghes = NULL;
1268     unsigned long flags;
1269 
1270     int rc = -EINVAL;
1271 
1272     generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1273     if (!generic->enabled)
1274         return -ENODEV;
1275 
1276     switch (generic->notify.type) {
1277     case ACPI_HEST_NOTIFY_POLLED:
1278     case ACPI_HEST_NOTIFY_EXTERNAL:
1279     case ACPI_HEST_NOTIFY_SCI:
1280     case ACPI_HEST_NOTIFY_GSIV:
1281     case ACPI_HEST_NOTIFY_GPIO:
1282         break;
1283 
1284     case ACPI_HEST_NOTIFY_SEA:
1285         if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
1286             pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
1287                 generic->header.source_id);
1288             rc = -ENOTSUPP;
1289             goto err;
1290         }
1291         break;
1292     case ACPI_HEST_NOTIFY_NMI:
1293         if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
1294             pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
1295                 generic->header.source_id);
1296             goto err;
1297         }
1298         break;
1299     case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1300         if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
1301             pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
1302                 generic->header.source_id);
1303             goto err;
1304         }
1305         break;
1306     case ACPI_HEST_NOTIFY_LOCAL:
1307         pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
1308             generic->header.source_id);
1309         goto err;
1310     default:
1311         pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
1312             generic->notify.type, generic->header.source_id);
1313         goto err;
1314     }
1315 
1316     rc = -EIO;
1317     if (generic->error_block_length <
1318         sizeof(struct acpi_hest_generic_status)) {
1319         pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
1320             generic->error_block_length, generic->header.source_id);
1321         goto err;
1322     }
1323     ghes = ghes_new(generic);
1324     if (IS_ERR(ghes)) {
1325         rc = PTR_ERR(ghes);
1326         ghes = NULL;
1327         goto err;
1328     }
1329 
1330     switch (generic->notify.type) {
1331     case ACPI_HEST_NOTIFY_POLLED:
1332         timer_setup(&ghes->timer, ghes_poll_func, 0);
1333         ghes_add_timer(ghes);
1334         break;
1335     case ACPI_HEST_NOTIFY_EXTERNAL:
1336         /* External interrupt vector is GSI */
1337         rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
1338         if (rc) {
1339             pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
1340                    generic->header.source_id);
1341             goto err;
1342         }
1343         rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
1344                  "GHES IRQ", ghes);
1345         if (rc) {
1346             pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
1347                    generic->header.source_id);
1348             goto err;
1349         }
1350         break;
1351 
1352     case ACPI_HEST_NOTIFY_SCI:
1353     case ACPI_HEST_NOTIFY_GSIV:
1354     case ACPI_HEST_NOTIFY_GPIO:
1355         mutex_lock(&ghes_list_mutex);
1356         if (list_empty(&ghes_hed))
1357             register_acpi_hed_notifier(&ghes_notifier_hed);
1358         list_add_rcu(&ghes->list, &ghes_hed);
1359         mutex_unlock(&ghes_list_mutex);
1360         break;
1361 
1362     case ACPI_HEST_NOTIFY_SEA:
1363         ghes_sea_add(ghes);
1364         break;
1365     case ACPI_HEST_NOTIFY_NMI:
1366         ghes_nmi_add(ghes);
1367         break;
1368     case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1369         rc = apei_sdei_register_ghes(ghes);
1370         if (rc)
1371             goto err;
1372         break;
1373     default:
1374         BUG();
1375     }
1376 
1377     platform_set_drvdata(ghes_dev, ghes);
1378 
1379     ghes_edac_register(ghes, &ghes_dev->dev);
1380 
1381     /* Handle any pending errors right away */
1382     spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1383     ghes_proc(ghes);
1384     spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1385 
1386     return 0;
1387 
1388 err:
1389     if (ghes) {
1390         ghes_fini(ghes);
1391         kfree(ghes);
1392     }
1393     return rc;
1394 }
1395 
1396 static int ghes_remove(struct platform_device *ghes_dev)
1397 {
1398     int rc;
1399     struct ghes *ghes;
1400     struct acpi_hest_generic *generic;
1401 
1402     ghes = platform_get_drvdata(ghes_dev);
1403     generic = ghes->generic;
1404 
1405     ghes->flags |= GHES_EXITING;
1406     switch (generic->notify.type) {
1407     case ACPI_HEST_NOTIFY_POLLED:
1408         del_timer_sync(&ghes->timer);
1409         break;
1410     case ACPI_HEST_NOTIFY_EXTERNAL:
1411         free_irq(ghes->irq, ghes);
1412         break;
1413 
1414     case ACPI_HEST_NOTIFY_SCI:
1415     case ACPI_HEST_NOTIFY_GSIV:
1416     case ACPI_HEST_NOTIFY_GPIO:
1417         mutex_lock(&ghes_list_mutex);
1418         list_del_rcu(&ghes->list);
1419         if (list_empty(&ghes_hed))
1420             unregister_acpi_hed_notifier(&ghes_notifier_hed);
1421         mutex_unlock(&ghes_list_mutex);
1422         synchronize_rcu();
1423         break;
1424 
1425     case ACPI_HEST_NOTIFY_SEA:
1426         ghes_sea_remove(ghes);
1427         break;
1428     case ACPI_HEST_NOTIFY_NMI:
1429         ghes_nmi_remove(ghes);
1430         break;
1431     case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
1432         rc = apei_sdei_unregister_ghes(ghes);
1433         if (rc)
1434             return rc;
1435         break;
1436     default:
1437         BUG();
1438         break;
1439     }
1440 
1441     ghes_fini(ghes);
1442 
1443     ghes_edac_unregister(ghes);
1444 
1445     kfree(ghes);
1446 
1447     platform_set_drvdata(ghes_dev, NULL);
1448 
1449     return 0;
1450 }
1451 
1452 static struct platform_driver ghes_platform_driver = {
1453     .driver     = {
1454         .name   = "GHES",
1455     },
1456     .probe      = ghes_probe,
1457     .remove     = ghes_remove,
1458 };
1459 
1460 void __init acpi_ghes_init(void)
1461 {
1462     int rc;
1463 
1464     sdei_init();
1465 
1466     if (acpi_disabled)
1467         return;
1468 
1469     switch (hest_disable) {
1470     case HEST_NOT_FOUND:
1471         return;
1472     case HEST_DISABLED:
1473         pr_info(GHES_PFX "HEST is not enabled!\n");
1474         return;
1475     default:
1476         break;
1477     }
1478 
1479     if (ghes_disable) {
1480         pr_info(GHES_PFX "GHES is not enabled!\n");
1481         return;
1482     }
1483 
1484     ghes_nmi_init_cxt();
1485 
1486     rc = platform_driver_register(&ghes_platform_driver);
1487     if (rc)
1488         return;
1489 
1490     rc = apei_osc_setup();
1491     if (rc == 0 && osc_sb_apei_support_acked)
1492         pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
1493     else if (rc == 0 && !osc_sb_apei_support_acked)
1494         pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
1495     else if (rc && osc_sb_apei_support_acked)
1496         pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
1497     else
1498         pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");
1499 }