David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * APEI Boot Error Record Table (BERT) support |
| 4 | * |
| 5 | * Copyright 2011 Intel Corp. |
| 6 | * Author: Huang Ying <ying.huang@intel.com> |
| 7 | * |
| 8 | * Under normal circumstances, when a hardware error occurs, the error |
| 9 | * handler receives control and processes the error. This gives OSPM a |
| 10 | * chance to process the error condition, report it, and optionally attempt |
| 11 | * recovery. In some cases, the system is unable to process an error. |
| 12 | * For example, system firmware or a management controller may choose to |
| 13 | * reset the system or the system might experience an uncontrolled crash |
| 14 | * or reset.The boot error source is used to report unhandled errors that |
| 15 | * occurred in a previous boot. This mechanism is described in the BERT |
| 16 | * table. |
| 17 | * |
| 18 | * For more information about BERT, please refer to ACPI Specification |
| 19 | * version 4.0, section 17.3.1 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/acpi.h> |
| 26 | #include <linux/io.h> |
| 27 | |
| 28 | #include "apei-internal.h" |
| 29 | |
| 30 | #undef pr_fmt |
| 31 | #define pr_fmt(fmt) "BERT: " fmt |
| 32 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 33 | #define ACPI_BERT_PRINT_MAX_RECORDS 5 |
| 34 | #define ACPI_BERT_PRINT_MAX_LEN 1024 |
| 35 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | static int bert_disable; |
| 37 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 38 | /* |
| 39 | * Print "all" the error records in the BERT table, but avoid huge spam to |
| 40 | * the console if the BIOS included oversize records, or too many records. |
| 41 | * Skipping some records here does not lose anything because the full |
| 42 | * data is available to user tools in: |
| 43 | * /sys/firmware/acpi/tables/data/BERT |
| 44 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | static void __init bert_print_all(struct acpi_bert_region *region, |
| 46 | unsigned int region_len) |
| 47 | { |
| 48 | struct acpi_hest_generic_status *estatus = |
| 49 | (struct acpi_hest_generic_status *)region; |
| 50 | int remain = region_len; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 51 | int printed = 0, skipped = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | u32 estatus_len; |
| 53 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 54 | while (remain >= sizeof(struct acpi_bert_region)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 55 | estatus_len = cper_estatus_len(estatus); |
| 56 | if (remain < estatus_len) { |
| 57 | pr_err(FW_BUG "Truncated status block (length: %u).\n", |
| 58 | estatus_len); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 59 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 62 | /* No more error records. */ |
| 63 | if (!estatus->block_status) |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 64 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 65 | |
| 66 | if (cper_estatus_check(estatus)) { |
| 67 | pr_err(FW_BUG "Invalid error record.\n"); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 68 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 71 | if (estatus_len < ACPI_BERT_PRINT_MAX_LEN && |
| 72 | printed < ACPI_BERT_PRINT_MAX_RECORDS) { |
| 73 | pr_info_once("Error records from previous boot:\n"); |
| 74 | cper_estatus_print(KERN_INFO HW_ERR, estatus); |
| 75 | printed++; |
| 76 | } else { |
| 77 | skipped++; |
| 78 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Because the boot error source is "one-time polled" type, |
| 82 | * clear Block Status of current Generic Error Status Block, |
| 83 | * once it's printed. |
| 84 | */ |
| 85 | estatus->block_status = 0; |
| 86 | |
| 87 | estatus = (void *)estatus + estatus_len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 88 | remain -= estatus_len; |
| 89 | } |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 90 | |
| 91 | if (skipped) |
| 92 | pr_info(HW_ERR "Skipped %d error records\n", skipped); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int __init setup_bert_disable(char *str) |
| 96 | { |
| 97 | bert_disable = 1; |
| 98 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 99 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | } |
| 101 | __setup("bert_disable", setup_bert_disable); |
| 102 | |
| 103 | static int __init bert_check_table(struct acpi_table_bert *bert_tab) |
| 104 | { |
| 105 | if (bert_tab->header.length < sizeof(struct acpi_table_bert) || |
| 106 | bert_tab->region_length < sizeof(struct acpi_bert_region)) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int __init bert_init(void) |
| 113 | { |
| 114 | struct apei_resources bert_resources; |
| 115 | struct acpi_bert_region *boot_error_region; |
| 116 | struct acpi_table_bert *bert_tab; |
| 117 | unsigned int region_len; |
| 118 | acpi_status status; |
| 119 | int rc = 0; |
| 120 | |
| 121 | if (acpi_disabled) |
| 122 | return 0; |
| 123 | |
| 124 | if (bert_disable) { |
| 125 | pr_info("Boot Error Record Table support is disabled.\n"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | status = acpi_get_table(ACPI_SIG_BERT, 0, (struct acpi_table_header **)&bert_tab); |
| 130 | if (status == AE_NOT_FOUND) |
| 131 | return 0; |
| 132 | |
| 133 | if (ACPI_FAILURE(status)) { |
| 134 | pr_err("get table failed, %s.\n", acpi_format_exception(status)); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | rc = bert_check_table(bert_tab); |
| 139 | if (rc) { |
| 140 | pr_err(FW_BUG "table invalid.\n"); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 141 | goto out_put_bert_tab; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | region_len = bert_tab->region_length; |
| 145 | apei_resources_init(&bert_resources); |
| 146 | rc = apei_resources_add(&bert_resources, bert_tab->address, |
| 147 | region_len, true); |
| 148 | if (rc) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 149 | goto out_put_bert_tab; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 150 | rc = apei_resources_request(&bert_resources, "APEI BERT"); |
| 151 | if (rc) |
| 152 | goto out_fini; |
| 153 | boot_error_region = ioremap_cache(bert_tab->address, region_len); |
| 154 | if (boot_error_region) { |
| 155 | bert_print_all(boot_error_region, region_len); |
| 156 | iounmap(boot_error_region); |
| 157 | } else { |
| 158 | rc = -ENOMEM; |
| 159 | } |
| 160 | |
| 161 | apei_resources_release(&bert_resources); |
| 162 | out_fini: |
| 163 | apei_resources_fini(&bert_resources); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 164 | out_put_bert_tab: |
| 165 | acpi_put_table((struct acpi_table_header *)bert_tab); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 166 | |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | late_initcall(bert_init); |