bert.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * APEI Boot Error Record Table (BERT) support
  4. *
  5. * Copyright 2011 Intel Corp.
  6. * Author: Huang Ying <[email protected]>
  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
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/acpi.h>
  25. #include <linux/io.h>
  26. #include "apei-internal.h"
  27. #undef pr_fmt
  28. #define pr_fmt(fmt) "BERT: " fmt
  29. #define ACPI_BERT_PRINT_MAX_RECORDS 5
  30. #define ACPI_BERT_PRINT_MAX_LEN 1024
  31. static int bert_disable;
  32. /*
  33. * Print "all" the error records in the BERT table, but avoid huge spam to
  34. * the console if the BIOS included oversize records, or too many records.
  35. * Skipping some records here does not lose anything because the full
  36. * data is available to user tools in:
  37. * /sys/firmware/acpi/tables/data/BERT
  38. */
  39. static void __init bert_print_all(struct acpi_bert_region *region,
  40. unsigned int region_len)
  41. {
  42. struct acpi_hest_generic_status *estatus =
  43. (struct acpi_hest_generic_status *)region;
  44. int remain = region_len;
  45. int printed = 0, skipped = 0;
  46. u32 estatus_len;
  47. while (remain >= sizeof(struct acpi_bert_region)) {
  48. estatus_len = cper_estatus_len(estatus);
  49. if (remain < estatus_len) {
  50. pr_err(FW_BUG "Truncated status block (length: %u).\n",
  51. estatus_len);
  52. break;
  53. }
  54. /* No more error records. */
  55. if (!estatus->block_status)
  56. break;
  57. if (cper_estatus_check(estatus)) {
  58. pr_err(FW_BUG "Invalid error record.\n");
  59. break;
  60. }
  61. if (estatus_len < ACPI_BERT_PRINT_MAX_LEN &&
  62. printed < ACPI_BERT_PRINT_MAX_RECORDS) {
  63. pr_info_once("Error records from previous boot:\n");
  64. cper_estatus_print(KERN_INFO HW_ERR, estatus);
  65. printed++;
  66. } else {
  67. skipped++;
  68. }
  69. /*
  70. * Because the boot error source is "one-time polled" type,
  71. * clear Block Status of current Generic Error Status Block,
  72. * once it's printed.
  73. */
  74. estatus->block_status = 0;
  75. estatus = (void *)estatus + estatus_len;
  76. remain -= estatus_len;
  77. }
  78. if (skipped)
  79. pr_info(HW_ERR "Skipped %d error records\n", skipped);
  80. if (printed + skipped)
  81. pr_info("Total records found: %d\n", printed + skipped);
  82. }
  83. static int __init setup_bert_disable(char *str)
  84. {
  85. bert_disable = 1;
  86. return 1;
  87. }
  88. __setup("bert_disable", setup_bert_disable);
  89. static int __init bert_check_table(struct acpi_table_bert *bert_tab)
  90. {
  91. if (bert_tab->header.length < sizeof(struct acpi_table_bert) ||
  92. bert_tab->region_length < sizeof(struct acpi_bert_region))
  93. return -EINVAL;
  94. return 0;
  95. }
  96. static int __init bert_init(void)
  97. {
  98. struct apei_resources bert_resources;
  99. struct acpi_bert_region *boot_error_region;
  100. struct acpi_table_bert *bert_tab;
  101. unsigned int region_len;
  102. acpi_status status;
  103. int rc = 0;
  104. if (acpi_disabled)
  105. return 0;
  106. if (bert_disable) {
  107. pr_info("Boot Error Record Table support is disabled.\n");
  108. return 0;
  109. }
  110. status = acpi_get_table(ACPI_SIG_BERT, 0, (struct acpi_table_header **)&bert_tab);
  111. if (status == AE_NOT_FOUND)
  112. return 0;
  113. if (ACPI_FAILURE(status)) {
  114. pr_err("get table failed, %s.\n", acpi_format_exception(status));
  115. return -EINVAL;
  116. }
  117. rc = bert_check_table(bert_tab);
  118. if (rc) {
  119. pr_err(FW_BUG "table invalid.\n");
  120. goto out_put_bert_tab;
  121. }
  122. region_len = bert_tab->region_length;
  123. apei_resources_init(&bert_resources);
  124. rc = apei_resources_add(&bert_resources, bert_tab->address,
  125. region_len, true);
  126. if (rc)
  127. goto out_put_bert_tab;
  128. rc = apei_resources_request(&bert_resources, "APEI BERT");
  129. if (rc)
  130. goto out_fini;
  131. boot_error_region = ioremap_cache(bert_tab->address, region_len);
  132. if (boot_error_region) {
  133. bert_print_all(boot_error_region, region_len);
  134. iounmap(boot_error_region);
  135. } else {
  136. rc = -ENOMEM;
  137. }
  138. apei_resources_release(&bert_resources);
  139. out_fini:
  140. apei_resources_fini(&bert_resources);
  141. out_put_bert_tab:
  142. acpi_put_table((struct acpi_table_header *)bert_tab);
  143. return rc;
  144. }
  145. late_initcall(bert_init);