hest.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/acpi.h>
  33. #include <linux/kdebug.h>
  34. #include <linux/highmem.h>
  35. #include <linux/io.h>
  36. #include <linux/platform_device.h>
  37. #include <acpi/apei.h>
  38. #include <asm/mce.h>
  39. #include "apei-internal.h"
  40. #define HEST_PFX "HEST: "
  41. bool hest_disable;
  42. EXPORT_SYMBOL_GPL(hest_disable);
  43. /* HEST table parsing */
  44. static struct acpi_table_hest *__read_mostly hest_tab;
  45. static const int hest_esrc_len_tab[ACPI_HEST_TYPE_RESERVED] = {
  46. [ACPI_HEST_TYPE_IA32_CHECK] = -1, /* need further calculation */
  47. [ACPI_HEST_TYPE_IA32_CORRECTED_CHECK] = -1,
  48. [ACPI_HEST_TYPE_IA32_NMI] = sizeof(struct acpi_hest_ia_nmi),
  49. [ACPI_HEST_TYPE_AER_ROOT_PORT] = sizeof(struct acpi_hest_aer_root),
  50. [ACPI_HEST_TYPE_AER_ENDPOINT] = sizeof(struct acpi_hest_aer),
  51. [ACPI_HEST_TYPE_AER_BRIDGE] = sizeof(struct acpi_hest_aer_bridge),
  52. [ACPI_HEST_TYPE_GENERIC_ERROR] = sizeof(struct acpi_hest_generic),
  53. };
  54. static int hest_esrc_len(struct acpi_hest_header *hest_hdr)
  55. {
  56. u16 hest_type = hest_hdr->type;
  57. int len;
  58. if (hest_type >= ACPI_HEST_TYPE_RESERVED)
  59. return 0;
  60. len = hest_esrc_len_tab[hest_type];
  61. if (hest_type == ACPI_HEST_TYPE_IA32_CORRECTED_CHECK) {
  62. struct acpi_hest_ia_corrected *cmc;
  63. cmc = (struct acpi_hest_ia_corrected *)hest_hdr;
  64. len = sizeof(*cmc) + cmc->num_hardware_banks *
  65. sizeof(struct acpi_hest_ia_error_bank);
  66. } else if (hest_type == ACPI_HEST_TYPE_IA32_CHECK) {
  67. struct acpi_hest_ia_machine_check *mc;
  68. mc = (struct acpi_hest_ia_machine_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. int apei_hest_parse(apei_hest_func_t func, void *data)
  76. {
  77. struct acpi_hest_header *hest_hdr;
  78. int i, rc, len;
  79. if (hest_disable || !hest_tab)
  80. return -EINVAL;
  81. hest_hdr = (struct acpi_hest_header *)(hest_tab + 1);
  82. for (i = 0; i < hest_tab->error_source_count; i++) {
  83. len = hest_esrc_len(hest_hdr);
  84. if (!len) {
  85. pr_warning(FW_WARN HEST_PFX
  86. "Unknown or unused hardware error source "
  87. "type: %d for hardware error source: %d.\n",
  88. hest_hdr->type, hest_hdr->source_id);
  89. return -EINVAL;
  90. }
  91. if ((void *)hest_hdr + len >
  92. (void *)hest_tab + hest_tab->header.length) {
  93. pr_warning(FW_BUG HEST_PFX
  94. "Table contents overflow for hardware error source: %d.\n",
  95. hest_hdr->source_id);
  96. return -EINVAL;
  97. }
  98. rc = func(hest_hdr, data);
  99. if (rc)
  100. return rc;
  101. hest_hdr = (void *)hest_hdr + len;
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(apei_hest_parse);
  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. #ifdef CONFIG_X86_MCE
  113. int i;
  114. struct acpi_hest_ia_corrected *cmc;
  115. struct acpi_hest_ia_error_bank *mc_bank;
  116. if (hest_hdr->type != ACPI_HEST_TYPE_IA32_CORRECTED_CHECK)
  117. return 0;
  118. cmc = (struct acpi_hest_ia_corrected *)hest_hdr;
  119. if (!cmc->enabled)
  120. return 0;
  121. /*
  122. * We expect HEST to provide a list of MC banks that report errors
  123. * in firmware first mode. Otherwise, return non-zero value to
  124. * indicate that we are done parsing HEST.
  125. */
  126. if (!(cmc->flags & ACPI_HEST_FIRMWARE_FIRST) || !cmc->num_hardware_banks)
  127. return 1;
  128. pr_info(HEST_PFX "Enabling Firmware First mode for corrected errors.\n");
  129. mc_bank = (struct acpi_hest_ia_error_bank *)(cmc + 1);
  130. for (i = 0; i < cmc->num_hardware_banks; i++, mc_bank++)
  131. mce_disable_bank(mc_bank->bank_number);
  132. #endif
  133. return 1;
  134. }
  135. struct ghes_arr {
  136. struct platform_device **ghes_devs;
  137. unsigned int count;
  138. };
  139. static int __init hest_parse_ghes_count(struct acpi_hest_header *hest_hdr, void *data)
  140. {
  141. int *count = data;
  142. if (hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR)
  143. (*count)++;
  144. return 0;
  145. }
  146. static int __init hest_parse_ghes(struct acpi_hest_header *hest_hdr, void *data)
  147. {
  148. struct platform_device *ghes_dev;
  149. struct ghes_arr *ghes_arr = data;
  150. int rc, i;
  151. if (hest_hdr->type != ACPI_HEST_TYPE_GENERIC_ERROR)
  152. return 0;
  153. if (!((struct acpi_hest_generic *)hest_hdr)->enabled)
  154. return 0;
  155. for (i = 0; i < ghes_arr->count; i++) {
  156. struct acpi_hest_header *hdr;
  157. ghes_dev = ghes_arr->ghes_devs[i];
  158. hdr = *(struct acpi_hest_header **)ghes_dev->dev.platform_data;
  159. if (hdr->source_id == hest_hdr->source_id) {
  160. pr_warning(FW_WARN HEST_PFX "Duplicated hardware error source ID: %d.\n",
  161. hdr->source_id);
  162. return -EIO;
  163. }
  164. }
  165. ghes_dev = platform_device_alloc("GHES", hest_hdr->source_id);
  166. if (!ghes_dev)
  167. return -ENOMEM;
  168. rc = platform_device_add_data(ghes_dev, &hest_hdr, sizeof(void *));
  169. if (rc)
  170. goto err;
  171. rc = platform_device_add(ghes_dev);
  172. if (rc)
  173. goto err;
  174. ghes_arr->ghes_devs[ghes_arr->count++] = ghes_dev;
  175. return 0;
  176. err:
  177. platform_device_put(ghes_dev);
  178. return rc;
  179. }
  180. static int __init hest_ghes_dev_register(unsigned int ghes_count)
  181. {
  182. int rc, i;
  183. struct ghes_arr ghes_arr;
  184. ghes_arr.count = 0;
  185. ghes_arr.ghes_devs = kmalloc(sizeof(void *) * ghes_count, GFP_KERNEL);
  186. if (!ghes_arr.ghes_devs)
  187. return -ENOMEM;
  188. rc = apei_hest_parse(hest_parse_ghes, &ghes_arr);
  189. if (rc)
  190. goto err;
  191. out:
  192. kfree(ghes_arr.ghes_devs);
  193. return rc;
  194. err:
  195. for (i = 0; i < ghes_arr.count; i++)
  196. platform_device_unregister(ghes_arr.ghes_devs[i]);
  197. goto out;
  198. }
  199. static int __init setup_hest_disable(char *str)
  200. {
  201. hest_disable = 1;
  202. return 0;
  203. }
  204. __setup("hest_disable", setup_hest_disable);
  205. void __init acpi_hest_init(void)
  206. {
  207. acpi_status status;
  208. int rc = -ENODEV;
  209. unsigned int ghes_count = 0;
  210. if (hest_disable) {
  211. pr_info(HEST_PFX "Table parsing disabled.\n");
  212. return;
  213. }
  214. status = acpi_get_table(ACPI_SIG_HEST, 0,
  215. (struct acpi_table_header **)&hest_tab);
  216. if (status == AE_NOT_FOUND)
  217. goto err;
  218. else if (ACPI_FAILURE(status)) {
  219. const char *msg = acpi_format_exception(status);
  220. pr_err(HEST_PFX "Failed to get table, %s\n", msg);
  221. rc = -EINVAL;
  222. goto err;
  223. }
  224. if (!acpi_disable_cmcff)
  225. apei_hest_parse(hest_parse_cmc, NULL);
  226. if (!ghes_disable) {
  227. rc = apei_hest_parse(hest_parse_ghes_count, &ghes_count);
  228. if (rc)
  229. goto err;
  230. rc = hest_ghes_dev_register(ghes_count);
  231. if (rc)
  232. goto err;
  233. }
  234. pr_info(HEST_PFX "Table parsing has been initialized.\n");
  235. return;
  236. err:
  237. hest_disable = 1;
  238. }