hest.c 6.7 KB

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