Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * APEI Boot Error Record Table (BERT) support
0004  *
0005  * Copyright 2011 Intel Corp.
0006  *   Author: Huang Ying <ying.huang@intel.com>
0007  *
0008  * Under normal circumstances, when a hardware error occurs, the error
0009  * handler receives control and processes the error. This gives OSPM a
0010  * chance to process the error condition, report it, and optionally attempt
0011  * recovery. In some cases, the system is unable to process an error.
0012  * For example, system firmware or a management controller may choose to
0013  * reset the system or the system might experience an uncontrolled crash
0014  * or reset.The boot error source is used to report unhandled errors that
0015  * occurred in a previous boot. This mechanism is described in the BERT
0016  * table.
0017  *
0018  * For more information about BERT, please refer to ACPI Specification
0019  * version 4.0, section 17.3.1
0020  */
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/module.h>
0024 #include <linux/init.h>
0025 #include <linux/acpi.h>
0026 #include <linux/io.h>
0027 
0028 #include "apei-internal.h"
0029 
0030 #undef pr_fmt
0031 #define pr_fmt(fmt) "BERT: " fmt
0032 
0033 #define ACPI_BERT_PRINT_MAX_RECORDS 5
0034 #define ACPI_BERT_PRINT_MAX_LEN 1024
0035 
0036 static int bert_disable;
0037 
0038 /*
0039  * Print "all" the error records in the BERT table, but avoid huge spam to
0040  * the console if the BIOS included oversize records, or too many records.
0041  * Skipping some records here does not lose anything because the full
0042  * data is available to user tools in:
0043  *  /sys/firmware/acpi/tables/data/BERT
0044  */
0045 static void __init bert_print_all(struct acpi_bert_region *region,
0046                   unsigned int region_len)
0047 {
0048     struct acpi_hest_generic_status *estatus =
0049         (struct acpi_hest_generic_status *)region;
0050     int remain = region_len;
0051     int printed = 0, skipped = 0;
0052     u32 estatus_len;
0053 
0054     while (remain >= sizeof(struct acpi_bert_region)) {
0055         estatus_len = cper_estatus_len(estatus);
0056         if (remain < estatus_len) {
0057             pr_err(FW_BUG "Truncated status block (length: %u).\n",
0058                    estatus_len);
0059             break;
0060         }
0061 
0062         /* No more error records. */
0063         if (!estatus->block_status)
0064             break;
0065 
0066         if (cper_estatus_check(estatus)) {
0067             pr_err(FW_BUG "Invalid error record.\n");
0068             break;
0069         }
0070 
0071         if (estatus_len < ACPI_BERT_PRINT_MAX_LEN &&
0072             printed < ACPI_BERT_PRINT_MAX_RECORDS) {
0073             pr_info_once("Error records from previous boot:\n");
0074             cper_estatus_print(KERN_INFO HW_ERR, estatus);
0075             printed++;
0076         } else {
0077             skipped++;
0078         }
0079 
0080         /*
0081          * Because the boot error source is "one-time polled" type,
0082          * clear Block Status of current Generic Error Status Block,
0083          * once it's printed.
0084          */
0085         estatus->block_status = 0;
0086 
0087         estatus = (void *)estatus + estatus_len;
0088         remain -= estatus_len;
0089     }
0090 
0091     if (skipped)
0092         pr_info(HW_ERR "Skipped %d error records\n", skipped);
0093 }
0094 
0095 static int __init setup_bert_disable(char *str)
0096 {
0097     bert_disable = 1;
0098 
0099     return 1;
0100 }
0101 __setup("bert_disable", setup_bert_disable);
0102 
0103 static int __init bert_check_table(struct acpi_table_bert *bert_tab)
0104 {
0105     if (bert_tab->header.length < sizeof(struct acpi_table_bert) ||
0106         bert_tab->region_length < sizeof(struct acpi_bert_region))
0107         return -EINVAL;
0108 
0109     return 0;
0110 }
0111 
0112 static int __init bert_init(void)
0113 {
0114     struct apei_resources bert_resources;
0115     struct acpi_bert_region *boot_error_region;
0116     struct acpi_table_bert *bert_tab;
0117     unsigned int region_len;
0118     acpi_status status;
0119     int rc = 0;
0120 
0121     if (acpi_disabled)
0122         return 0;
0123 
0124     if (bert_disable) {
0125         pr_info("Boot Error Record Table support is disabled.\n");
0126         return 0;
0127     }
0128 
0129     status = acpi_get_table(ACPI_SIG_BERT, 0, (struct acpi_table_header **)&bert_tab);
0130     if (status == AE_NOT_FOUND)
0131         return 0;
0132 
0133     if (ACPI_FAILURE(status)) {
0134         pr_err("get table failed, %s.\n", acpi_format_exception(status));
0135         return -EINVAL;
0136     }
0137 
0138     rc = bert_check_table(bert_tab);
0139     if (rc) {
0140         pr_err(FW_BUG "table invalid.\n");
0141         goto out_put_bert_tab;
0142     }
0143 
0144     region_len = bert_tab->region_length;
0145     apei_resources_init(&bert_resources);
0146     rc = apei_resources_add(&bert_resources, bert_tab->address,
0147                 region_len, true);
0148     if (rc)
0149         goto out_put_bert_tab;
0150     rc = apei_resources_request(&bert_resources, "APEI BERT");
0151     if (rc)
0152         goto out_fini;
0153     boot_error_region = ioremap_cache(bert_tab->address, region_len);
0154     if (boot_error_region) {
0155         bert_print_all(boot_error_region, region_len);
0156         iounmap(boot_error_region);
0157     } else {
0158         rc = -ENOMEM;
0159     }
0160 
0161     apei_resources_release(&bert_resources);
0162 out_fini:
0163     apei_resources_fini(&bert_resources);
0164 out_put_bert_tab:
0165     acpi_put_table((struct acpi_table_header *)bert_tab);
0166 
0167     return rc;
0168 }
0169 
0170 late_initcall(bert_init);