hest.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * APEI Hardware Error Source Table support
  4. *
  5. * HEST describes error sources in detail; communicates operational
  6. * parameters (i.e. severity levels, masking bits, and threshold
  7. * values) to Linux as necessary. It also allows the BIOS to report
  8. * non-standard error sources to Linux (for example, chipset-specific
  9. * error registers).
  10. *
  11. * For more information about HEST, please refer to ACPI Specification
  12. * version 4.0, section 17.3.2.
  13. *
  14. * Copyright 2009 Intel Corp.
  15. * Author: Huang Ying <[email protected]>
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/acpi.h>
  21. #include <linux/kdebug.h>
  22. #include <linux/highmem.h>
  23. #include <linux/io.h>
  24. #include <linux/platform_device.h>
  25. #include <acpi/apei.h>
  26. #include <acpi/ghes.h>
  27. #include "apei-internal.h"
  28. #define HEST_PFX "HEST: "
  29. int hest_disable;
  30. EXPORT_SYMBOL_GPL(hest_disable);
  31. /* HEST table parsing */
  32. static struct acpi_table_hest *__read_mostly hest_tab;
  33. static const int hest_esrc_len_tab[ACPI_HEST_TYPE_RESERVED] = {
  34. [ACPI_HEST_TYPE_IA32_CHECK] = -1, /* need further calculation */
  35. [ACPI_HEST_TYPE_IA32_CORRECTED_CHECK] = -1,
  36. [ACPI_HEST_TYPE_IA32_NMI] = sizeof(struct acpi_hest_ia_nmi),
  37. [ACPI_HEST_TYPE_AER_ROOT_PORT] = sizeof(struct acpi_hest_aer_root),
  38. [ACPI_HEST_TYPE_AER_ENDPOINT] = sizeof(struct acpi_hest_aer),
  39. [ACPI_HEST_TYPE_AER_BRIDGE] = sizeof(struct acpi_hest_aer_bridge),
  40. [ACPI_HEST_TYPE_GENERIC_ERROR] = sizeof(struct acpi_hest_generic),
  41. [ACPI_HEST_TYPE_GENERIC_ERROR_V2] = sizeof(struct acpi_hest_generic_v2),
  42. [ACPI_HEST_TYPE_IA32_DEFERRED_CHECK] = -1,
  43. };
  44. static inline bool is_generic_error(struct acpi_hest_header *hest_hdr)
  45. {
  46. return hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR ||
  47. hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
  48. }
  49. static int hest_esrc_len(struct acpi_hest_header *hest_hdr)
  50. {
  51. u16 hest_type = hest_hdr->type;
  52. int len;
  53. if (hest_type >= ACPI_HEST_TYPE_RESERVED)
  54. return 0;
  55. len = hest_esrc_len_tab[hest_type];
  56. if (hest_type == ACPI_HEST_TYPE_IA32_CORRECTED_CHECK) {
  57. struct acpi_hest_ia_corrected *cmc;
  58. cmc = (struct acpi_hest_ia_corrected *)hest_hdr;
  59. len = sizeof(*cmc) + cmc->num_hardware_banks *
  60. sizeof(struct acpi_hest_ia_error_bank);
  61. } else if (hest_type == ACPI_HEST_TYPE_IA32_CHECK) {
  62. struct acpi_hest_ia_machine_check *mc;
  63. mc = (struct acpi_hest_ia_machine_check *)hest_hdr;
  64. len = sizeof(*mc) + mc->num_hardware_banks *
  65. sizeof(struct acpi_hest_ia_error_bank);
  66. } else if (hest_type == ACPI_HEST_TYPE_IA32_DEFERRED_CHECK) {
  67. struct acpi_hest_ia_deferred_check *mc;
  68. mc = (struct acpi_hest_ia_deferred_check *)hest_hdr;
  69. len = sizeof(*mc) + mc->num_hardware_banks *
  70. sizeof(struct acpi_hest_ia_error_bank);
  71. }
  72. BUG_ON(len == -1);
  73. return len;
  74. };
  75. typedef int (*apei_hest_func_t)(struct acpi_hest_header *hest_hdr, void *data);
  76. static int apei_hest_parse(apei_hest_func_t func, void *data)
  77. {
  78. struct acpi_hest_header *hest_hdr;
  79. int i, rc, len;
  80. if (hest_disable || !hest_tab)
  81. return -EINVAL;
  82. hest_hdr = (struct acpi_hest_header *)(hest_tab + 1);
  83. for (i = 0; i < hest_tab->error_source_count; i++) {
  84. len = hest_esrc_len(hest_hdr);
  85. if (!len) {
  86. pr_warn(FW_WARN HEST_PFX
  87. "Unknown or unused hardware error source "
  88. "type: %d for hardware error source: %d.\n",
  89. hest_hdr->type, hest_hdr->source_id);
  90. return -EINVAL;
  91. }
  92. if ((void *)hest_hdr + len >
  93. (void *)hest_tab + hest_tab->header.length) {
  94. pr_warn(FW_BUG HEST_PFX
  95. "Table contents overflow for hardware error source: %d.\n",
  96. hest_hdr->source_id);
  97. return -EINVAL;
  98. }
  99. rc = func(hest_hdr, data);
  100. if (rc)
  101. return rc;
  102. hest_hdr = (void *)hest_hdr + len;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * Check if firmware advertises firmware first mode. We need FF bit to be set
  108. * along with a set of MC banks which work in FF mode.
  109. */
  110. static int __init hest_parse_cmc(struct acpi_hest_header *hest_hdr, void *data)
  111. {
  112. if (hest_hdr->type != ACPI_HEST_TYPE_IA32_CORRECTED_CHECK)
  113. return 0;
  114. if (!acpi_disable_cmcff)
  115. return !arch_apei_enable_cmcff(hest_hdr, data);
  116. return 0;
  117. }
  118. struct ghes_arr {
  119. struct platform_device **ghes_devs;
  120. unsigned int count;
  121. };
  122. static int __init hest_parse_ghes_count(struct acpi_hest_header *hest_hdr, void *data)
  123. {
  124. int *count = data;
  125. if (is_generic_error(hest_hdr))
  126. (*count)++;
  127. return 0;
  128. }
  129. static int __init hest_parse_ghes(struct acpi_hest_header *hest_hdr, void *data)
  130. {
  131. struct platform_device *ghes_dev;
  132. struct ghes_arr *ghes_arr = data;
  133. int rc, i;
  134. if (!is_generic_error(hest_hdr))
  135. return 0;
  136. if (!((struct acpi_hest_generic *)hest_hdr)->enabled)
  137. return 0;
  138. for (i = 0; i < ghes_arr->count; i++) {
  139. struct acpi_hest_header *hdr;
  140. ghes_dev = ghes_arr->ghes_devs[i];
  141. hdr = *(struct acpi_hest_header **)ghes_dev->dev.platform_data;
  142. if (hdr->source_id == hest_hdr->source_id) {
  143. pr_warn(FW_WARN HEST_PFX "Duplicated hardware error source ID: %d.\n",
  144. hdr->source_id);
  145. return -EIO;
  146. }
  147. }
  148. ghes_dev = platform_device_alloc("GHES", hest_hdr->source_id);
  149. if (!ghes_dev)
  150. return -ENOMEM;
  151. rc = platform_device_add_data(ghes_dev, &hest_hdr, sizeof(void *));
  152. if (rc)
  153. goto err;
  154. rc = platform_device_add(ghes_dev);
  155. if (rc)
  156. goto err;
  157. ghes_arr->ghes_devs[ghes_arr->count++] = ghes_dev;
  158. return 0;
  159. err:
  160. platform_device_put(ghes_dev);
  161. return rc;
  162. }
  163. static int __init hest_ghes_dev_register(unsigned int ghes_count)
  164. {
  165. int rc, i;
  166. struct ghes_arr ghes_arr;
  167. ghes_arr.count = 0;
  168. ghes_arr.ghes_devs = kmalloc_array(ghes_count, sizeof(void *),
  169. GFP_KERNEL);
  170. if (!ghes_arr.ghes_devs)
  171. return -ENOMEM;
  172. rc = apei_hest_parse(hest_parse_ghes, &ghes_arr);
  173. if (rc)
  174. goto err;
  175. rc = ghes_estatus_pool_init(ghes_count);
  176. if (rc)
  177. goto err;
  178. out:
  179. kfree(ghes_arr.ghes_devs);
  180. return rc;
  181. err:
  182. for (i = 0; i < ghes_arr.count; i++)
  183. platform_device_unregister(ghes_arr.ghes_devs[i]);
  184. goto out;
  185. }
  186. static int __init setup_hest_disable(char *str)
  187. {
  188. hest_disable = HEST_DISABLED;
  189. return 1;
  190. }
  191. __setup("hest_disable", setup_hest_disable);
  192. void __init acpi_hest_init(void)
  193. {
  194. acpi_status status;
  195. int rc;
  196. unsigned int ghes_count = 0;
  197. if (hest_disable) {
  198. pr_info(HEST_PFX "Table parsing disabled.\n");
  199. return;
  200. }
  201. status = acpi_get_table(ACPI_SIG_HEST, 0,
  202. (struct acpi_table_header **)&hest_tab);
  203. if (status == AE_NOT_FOUND) {
  204. hest_disable = HEST_NOT_FOUND;
  205. return;
  206. } else if (ACPI_FAILURE(status)) {
  207. const char *msg = acpi_format_exception(status);
  208. pr_err(HEST_PFX "Failed to get table, %s\n", msg);
  209. hest_disable = HEST_DISABLED;
  210. return;
  211. }
  212. rc = apei_hest_parse(hest_parse_cmc, NULL);
  213. if (rc)
  214. goto err;
  215. if (!ghes_disable) {
  216. rc = apei_hest_parse(hest_parse_ghes_count, &ghes_count);
  217. if (rc)
  218. goto err;
  219. if (ghes_count)
  220. rc = hest_ghes_dev_register(ghes_count);
  221. if (rc)
  222. goto err;
  223. }
  224. pr_info(HEST_PFX "Table parsing has been initialized.\n");
  225. return;
  226. err:
  227. hest_disable = HEST_DISABLED;
  228. acpi_put_table((struct acpi_table_header *)hest_tab);
  229. }