Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * APEI Hardware Error Source Table support
0004  *
0005  * HEST describes error sources in detail; communicates operational
0006  * parameters (i.e. severity levels, masking bits, and threshold
0007  * values) to Linux as necessary. It also allows the BIOS to report
0008  * non-standard error sources to Linux (for example, chipset-specific
0009  * error registers).
0010  *
0011  * For more information about HEST, please refer to ACPI Specification
0012  * version 4.0, section 17.3.2.
0013  *
0014  * Copyright 2009 Intel Corp.
0015  *   Author: Huang Ying <ying.huang@intel.com>
0016  */
0017 
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <linux/init.h>
0021 #include <linux/acpi.h>
0022 #include <linux/kdebug.h>
0023 #include <linux/highmem.h>
0024 #include <linux/io.h>
0025 #include <linux/platform_device.h>
0026 #include <acpi/apei.h>
0027 #include <acpi/ghes.h>
0028 
0029 #include "apei-internal.h"
0030 
0031 #define HEST_PFX "HEST: "
0032 
0033 int hest_disable;
0034 EXPORT_SYMBOL_GPL(hest_disable);
0035 
0036 /* HEST table parsing */
0037 
0038 static struct acpi_table_hest *__read_mostly hest_tab;
0039 
0040 static const int hest_esrc_len_tab[ACPI_HEST_TYPE_RESERVED] = {
0041     [ACPI_HEST_TYPE_IA32_CHECK] = -1,   /* need further calculation */
0042     [ACPI_HEST_TYPE_IA32_CORRECTED_CHECK] = -1,
0043     [ACPI_HEST_TYPE_IA32_NMI] = sizeof(struct acpi_hest_ia_nmi),
0044     [ACPI_HEST_TYPE_AER_ROOT_PORT] = sizeof(struct acpi_hest_aer_root),
0045     [ACPI_HEST_TYPE_AER_ENDPOINT] = sizeof(struct acpi_hest_aer),
0046     [ACPI_HEST_TYPE_AER_BRIDGE] = sizeof(struct acpi_hest_aer_bridge),
0047     [ACPI_HEST_TYPE_GENERIC_ERROR] = sizeof(struct acpi_hest_generic),
0048     [ACPI_HEST_TYPE_GENERIC_ERROR_V2] = sizeof(struct acpi_hest_generic_v2),
0049     [ACPI_HEST_TYPE_IA32_DEFERRED_CHECK] = -1,
0050 };
0051 
0052 static inline bool is_generic_error(struct acpi_hest_header *hest_hdr)
0053 {
0054     return hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR ||
0055            hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
0056 }
0057 
0058 static int hest_esrc_len(struct acpi_hest_header *hest_hdr)
0059 {
0060     u16 hest_type = hest_hdr->type;
0061     int len;
0062 
0063     if (hest_type >= ACPI_HEST_TYPE_RESERVED)
0064         return 0;
0065 
0066     len = hest_esrc_len_tab[hest_type];
0067 
0068     if (hest_type == ACPI_HEST_TYPE_IA32_CORRECTED_CHECK) {
0069         struct acpi_hest_ia_corrected *cmc;
0070         cmc = (struct acpi_hest_ia_corrected *)hest_hdr;
0071         len = sizeof(*cmc) + cmc->num_hardware_banks *
0072             sizeof(struct acpi_hest_ia_error_bank);
0073     } else if (hest_type == ACPI_HEST_TYPE_IA32_CHECK) {
0074         struct acpi_hest_ia_machine_check *mc;
0075         mc = (struct acpi_hest_ia_machine_check *)hest_hdr;
0076         len = sizeof(*mc) + mc->num_hardware_banks *
0077             sizeof(struct acpi_hest_ia_error_bank);
0078     } else if (hest_type == ACPI_HEST_TYPE_IA32_DEFERRED_CHECK) {
0079         struct acpi_hest_ia_deferred_check *mc;
0080         mc = (struct acpi_hest_ia_deferred_check *)hest_hdr;
0081         len = sizeof(*mc) + mc->num_hardware_banks *
0082             sizeof(struct acpi_hest_ia_error_bank);
0083     }
0084     BUG_ON(len == -1);
0085 
0086     return len;
0087 };
0088 
0089 typedef int (*apei_hest_func_t)(struct acpi_hest_header *hest_hdr, void *data);
0090 
0091 static int apei_hest_parse(apei_hest_func_t func, void *data)
0092 {
0093     struct acpi_hest_header *hest_hdr;
0094     int i, rc, len;
0095 
0096     if (hest_disable || !hest_tab)
0097         return -EINVAL;
0098 
0099     hest_hdr = (struct acpi_hest_header *)(hest_tab + 1);
0100     for (i = 0; i < hest_tab->error_source_count; i++) {
0101         len = hest_esrc_len(hest_hdr);
0102         if (!len) {
0103             pr_warn(FW_WARN HEST_PFX
0104                 "Unknown or unused hardware error source "
0105                 "type: %d for hardware error source: %d.\n",
0106                 hest_hdr->type, hest_hdr->source_id);
0107             return -EINVAL;
0108         }
0109         if ((void *)hest_hdr + len >
0110             (void *)hest_tab + hest_tab->header.length) {
0111             pr_warn(FW_BUG HEST_PFX
0112         "Table contents overflow for hardware error source: %d.\n",
0113                 hest_hdr->source_id);
0114             return -EINVAL;
0115         }
0116 
0117         rc = func(hest_hdr, data);
0118         if (rc)
0119             return rc;
0120 
0121         hest_hdr = (void *)hest_hdr + len;
0122     }
0123 
0124     return 0;
0125 }
0126 
0127 /*
0128  * Check if firmware advertises firmware first mode. We need FF bit to be set
0129  * along with a set of MC banks which work in FF mode.
0130  */
0131 static int __init hest_parse_cmc(struct acpi_hest_header *hest_hdr, void *data)
0132 {
0133     if (hest_hdr->type != ACPI_HEST_TYPE_IA32_CORRECTED_CHECK)
0134         return 0;
0135 
0136     if (!acpi_disable_cmcff)
0137         return !arch_apei_enable_cmcff(hest_hdr, data);
0138 
0139     return 0;
0140 }
0141 
0142 struct ghes_arr {
0143     struct platform_device **ghes_devs;
0144     unsigned int count;
0145 };
0146 
0147 static int __init hest_parse_ghes_count(struct acpi_hest_header *hest_hdr, void *data)
0148 {
0149     int *count = data;
0150 
0151     if (is_generic_error(hest_hdr))
0152         (*count)++;
0153     return 0;
0154 }
0155 
0156 static int __init hest_parse_ghes(struct acpi_hest_header *hest_hdr, void *data)
0157 {
0158     struct platform_device *ghes_dev;
0159     struct ghes_arr *ghes_arr = data;
0160     int rc, i;
0161 
0162     if (!is_generic_error(hest_hdr))
0163         return 0;
0164 
0165     if (!((struct acpi_hest_generic *)hest_hdr)->enabled)
0166         return 0;
0167     for (i = 0; i < ghes_arr->count; i++) {
0168         struct acpi_hest_header *hdr;
0169         ghes_dev = ghes_arr->ghes_devs[i];
0170         hdr = *(struct acpi_hest_header **)ghes_dev->dev.platform_data;
0171         if (hdr->source_id == hest_hdr->source_id) {
0172             pr_warn(FW_WARN HEST_PFX "Duplicated hardware error source ID: %d.\n",
0173                 hdr->source_id);
0174             return -EIO;
0175         }
0176     }
0177     ghes_dev = platform_device_alloc("GHES", hest_hdr->source_id);
0178     if (!ghes_dev)
0179         return -ENOMEM;
0180 
0181     rc = platform_device_add_data(ghes_dev, &hest_hdr, sizeof(void *));
0182     if (rc)
0183         goto err;
0184 
0185     rc = platform_device_add(ghes_dev);
0186     if (rc)
0187         goto err;
0188     ghes_arr->ghes_devs[ghes_arr->count++] = ghes_dev;
0189 
0190     return 0;
0191 err:
0192     platform_device_put(ghes_dev);
0193     return rc;
0194 }
0195 
0196 static int __init hest_ghes_dev_register(unsigned int ghes_count)
0197 {
0198     int rc, i;
0199     struct ghes_arr ghes_arr;
0200 
0201     ghes_arr.count = 0;
0202     ghes_arr.ghes_devs = kmalloc_array(ghes_count, sizeof(void *),
0203                        GFP_KERNEL);
0204     if (!ghes_arr.ghes_devs)
0205         return -ENOMEM;
0206 
0207     rc = apei_hest_parse(hest_parse_ghes, &ghes_arr);
0208     if (rc)
0209         goto err;
0210 
0211     rc = ghes_estatus_pool_init(ghes_count);
0212     if (rc)
0213         goto err;
0214 
0215 out:
0216     kfree(ghes_arr.ghes_devs);
0217     return rc;
0218 err:
0219     for (i = 0; i < ghes_arr.count; i++)
0220         platform_device_unregister(ghes_arr.ghes_devs[i]);
0221     goto out;
0222 }
0223 
0224 static int __init setup_hest_disable(char *str)
0225 {
0226     hest_disable = HEST_DISABLED;
0227     return 1;
0228 }
0229 
0230 __setup("hest_disable", setup_hest_disable);
0231 
0232 void __init acpi_hest_init(void)
0233 {
0234     acpi_status status;
0235     int rc;
0236     unsigned int ghes_count = 0;
0237 
0238     if (hest_disable) {
0239         pr_info(HEST_PFX "Table parsing disabled.\n");
0240         return;
0241     }
0242 
0243     status = acpi_get_table(ACPI_SIG_HEST, 0,
0244                 (struct acpi_table_header **)&hest_tab);
0245     if (status == AE_NOT_FOUND) {
0246         hest_disable = HEST_NOT_FOUND;
0247         return;
0248     } else if (ACPI_FAILURE(status)) {
0249         const char *msg = acpi_format_exception(status);
0250         pr_err(HEST_PFX "Failed to get table, %s\n", msg);
0251         hest_disable = HEST_DISABLED;
0252         return;
0253     }
0254 
0255     rc = apei_hest_parse(hest_parse_cmc, NULL);
0256     if (rc)
0257         goto err;
0258 
0259     if (!ghes_disable) {
0260         rc = apei_hest_parse(hest_parse_ghes_count, &ghes_count);
0261         if (rc)
0262             goto err;
0263 
0264         if (ghes_count)
0265             rc = hest_ghes_dev_register(ghes_count);
0266         if (rc)
0267             goto err;
0268     }
0269 
0270     pr_info(HEST_PFX "Table parsing has been initialized.\n");
0271     return;
0272 err:
0273     hest_disable = HEST_DISABLED;
0274     acpi_put_table((struct acpi_table_header *)hest_tab);
0275 }