162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * APEI Boot Error Record Table (BERT) support
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2011 Intel Corp.
662306a36Sopenharmony_ci *   Author: Huang Ying <ying.huang@intel.com>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Under normal circumstances, when a hardware error occurs, the error
962306a36Sopenharmony_ci * handler receives control and processes the error. This gives OSPM a
1062306a36Sopenharmony_ci * chance to process the error condition, report it, and optionally attempt
1162306a36Sopenharmony_ci * recovery. In some cases, the system is unable to process an error.
1262306a36Sopenharmony_ci * For example, system firmware or a management controller may choose to
1362306a36Sopenharmony_ci * reset the system or the system might experience an uncontrolled crash
1462306a36Sopenharmony_ci * or reset.The boot error source is used to report unhandled errors that
1562306a36Sopenharmony_ci * occurred in a previous boot. This mechanism is described in the BERT
1662306a36Sopenharmony_ci * table.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * For more information about BERT, please refer to ACPI Specification
1962306a36Sopenharmony_ci * version 4.0, section 17.3.1
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include <linux/kernel.h>
2362306a36Sopenharmony_ci#include <linux/module.h>
2462306a36Sopenharmony_ci#include <linux/init.h>
2562306a36Sopenharmony_ci#include <linux/acpi.h>
2662306a36Sopenharmony_ci#include <linux/cper.h>
2762306a36Sopenharmony_ci#include <linux/io.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include "apei-internal.h"
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#undef pr_fmt
3262306a36Sopenharmony_ci#define pr_fmt(fmt) "BERT: " fmt
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define ACPI_BERT_PRINT_MAX_RECORDS 5
3562306a36Sopenharmony_ci#define ACPI_BERT_PRINT_MAX_LEN 1024
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistatic int bert_disable __initdata;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/*
4062306a36Sopenharmony_ci * Print "all" the error records in the BERT table, but avoid huge spam to
4162306a36Sopenharmony_ci * the console if the BIOS included oversize records, or too many records.
4262306a36Sopenharmony_ci * Skipping some records here does not lose anything because the full
4362306a36Sopenharmony_ci * data is available to user tools in:
4462306a36Sopenharmony_ci *	/sys/firmware/acpi/tables/data/BERT
4562306a36Sopenharmony_ci */
4662306a36Sopenharmony_cistatic void __init bert_print_all(struct acpi_bert_region *region,
4762306a36Sopenharmony_ci				  unsigned int region_len)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	struct acpi_hest_generic_status *estatus =
5062306a36Sopenharmony_ci		(struct acpi_hest_generic_status *)region;
5162306a36Sopenharmony_ci	int remain = region_len;
5262306a36Sopenharmony_ci	int printed = 0, skipped = 0;
5362306a36Sopenharmony_ci	u32 estatus_len;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	while (remain >= sizeof(struct acpi_bert_region)) {
5662306a36Sopenharmony_ci		estatus_len = cper_estatus_len(estatus);
5762306a36Sopenharmony_ci		if (remain < estatus_len) {
5862306a36Sopenharmony_ci			pr_err(FW_BUG "Truncated status block (length: %u).\n",
5962306a36Sopenharmony_ci			       estatus_len);
6062306a36Sopenharmony_ci			break;
6162306a36Sopenharmony_ci		}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci		/* No more error records. */
6462306a36Sopenharmony_ci		if (!estatus->block_status)
6562306a36Sopenharmony_ci			break;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci		if (cper_estatus_check(estatus)) {
6862306a36Sopenharmony_ci			pr_err(FW_BUG "Invalid error record.\n");
6962306a36Sopenharmony_ci			break;
7062306a36Sopenharmony_ci		}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci		if (estatus_len < ACPI_BERT_PRINT_MAX_LEN &&
7362306a36Sopenharmony_ci		    printed < ACPI_BERT_PRINT_MAX_RECORDS) {
7462306a36Sopenharmony_ci			pr_info_once("Error records from previous boot:\n");
7562306a36Sopenharmony_ci			cper_estatus_print(KERN_INFO HW_ERR, estatus);
7662306a36Sopenharmony_ci			printed++;
7762306a36Sopenharmony_ci		} else {
7862306a36Sopenharmony_ci			skipped++;
7962306a36Sopenharmony_ci		}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci		/*
8262306a36Sopenharmony_ci		 * Because the boot error source is "one-time polled" type,
8362306a36Sopenharmony_ci		 * clear Block Status of current Generic Error Status Block,
8462306a36Sopenharmony_ci		 * once it's printed.
8562306a36Sopenharmony_ci		 */
8662306a36Sopenharmony_ci		estatus->block_status = 0;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		estatus = (void *)estatus + estatus_len;
8962306a36Sopenharmony_ci		remain -= estatus_len;
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	if (skipped)
9362306a36Sopenharmony_ci		pr_info(HW_ERR "Skipped %d error records\n", skipped);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	if (printed + skipped)
9662306a36Sopenharmony_ci		pr_info("Total records found: %d\n", printed + skipped);
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic int __init setup_bert_disable(char *str)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	bert_disable = 1;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return 1;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci__setup("bert_disable", setup_bert_disable);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic int __init bert_check_table(struct acpi_table_bert *bert_tab)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	if (bert_tab->header.length < sizeof(struct acpi_table_bert) ||
11062306a36Sopenharmony_ci	    bert_tab->region_length < sizeof(struct acpi_bert_region))
11162306a36Sopenharmony_ci		return -EINVAL;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	return 0;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic int __init bert_init(void)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	struct apei_resources bert_resources;
11962306a36Sopenharmony_ci	struct acpi_bert_region *boot_error_region;
12062306a36Sopenharmony_ci	struct acpi_table_bert *bert_tab;
12162306a36Sopenharmony_ci	unsigned int region_len;
12262306a36Sopenharmony_ci	acpi_status status;
12362306a36Sopenharmony_ci	int rc = 0;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	if (acpi_disabled)
12662306a36Sopenharmony_ci		return 0;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	if (bert_disable) {
12962306a36Sopenharmony_ci		pr_info("Boot Error Record Table support is disabled.\n");
13062306a36Sopenharmony_ci		return 0;
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	status = acpi_get_table(ACPI_SIG_BERT, 0, (struct acpi_table_header **)&bert_tab);
13462306a36Sopenharmony_ci	if (status == AE_NOT_FOUND)
13562306a36Sopenharmony_ci		return 0;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
13862306a36Sopenharmony_ci		pr_err("get table failed, %s.\n", acpi_format_exception(status));
13962306a36Sopenharmony_ci		return -EINVAL;
14062306a36Sopenharmony_ci	}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	rc = bert_check_table(bert_tab);
14362306a36Sopenharmony_ci	if (rc) {
14462306a36Sopenharmony_ci		pr_err(FW_BUG "table invalid.\n");
14562306a36Sopenharmony_ci		goto out_put_bert_tab;
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	region_len = bert_tab->region_length;
14962306a36Sopenharmony_ci	apei_resources_init(&bert_resources);
15062306a36Sopenharmony_ci	rc = apei_resources_add(&bert_resources, bert_tab->address,
15162306a36Sopenharmony_ci				region_len, true);
15262306a36Sopenharmony_ci	if (rc)
15362306a36Sopenharmony_ci		goto out_put_bert_tab;
15462306a36Sopenharmony_ci	rc = apei_resources_request(&bert_resources, "APEI BERT");
15562306a36Sopenharmony_ci	if (rc)
15662306a36Sopenharmony_ci		goto out_fini;
15762306a36Sopenharmony_ci	boot_error_region = ioremap_cache(bert_tab->address, region_len);
15862306a36Sopenharmony_ci	if (boot_error_region) {
15962306a36Sopenharmony_ci		bert_print_all(boot_error_region, region_len);
16062306a36Sopenharmony_ci		iounmap(boot_error_region);
16162306a36Sopenharmony_ci	} else {
16262306a36Sopenharmony_ci		rc = -ENOMEM;
16362306a36Sopenharmony_ci	}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	apei_resources_release(&bert_resources);
16662306a36Sopenharmony_ciout_fini:
16762306a36Sopenharmony_ci	apei_resources_fini(&bert_resources);
16862306a36Sopenharmony_ciout_put_bert_tab:
16962306a36Sopenharmony_ci	acpi_put_table((struct acpi_table_header *)bert_tab);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	return rc;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cilate_initcall(bert_init);
175