ghes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * APEI Generic Hardware Error Source support
  3. *
  4. * Generic Hardware Error Source provides a way to report platform
  5. * hardware errors (such as that from chipset). It works in so called
  6. * "Firmware First" mode, that is, hardware errors are reported to
  7. * firmware firstly, then reported to Linux by firmware. This way,
  8. * some non-standard hardware error registers or non-standard hardware
  9. * link can be checked by firmware to produce more hardware error
  10. * information for Linux.
  11. *
  12. * For more information about Generic Hardware Error Source, please
  13. * refer to ACPI Specification version 4.0, section 17.3.2.6
  14. *
  15. * Now, only SCI notification type and memory errors are
  16. * supported. More notification type and hardware error type will be
  17. * added later.
  18. *
  19. * Copyright 2010 Intel Corp.
  20. * Author: Huang Ying <ying.huang@intel.com>
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License version
  24. * 2 as published by the Free Software Foundation;
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/acpi.h>
  39. #include <linux/io.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/cper.h>
  42. #include <linux/kdebug.h>
  43. #include <acpi/apei.h>
  44. #include <acpi/atomicio.h>
  45. #include <acpi/hed.h>
  46. #include <asm/mce.h>
  47. #include "apei-internal.h"
  48. #define GHES_PFX "GHES: "
  49. #define GHES_ESTATUS_MAX_SIZE 65536
  50. /*
  51. * One struct ghes is created for each generic hardware error
  52. * source.
  53. *
  54. * It provides the context for APEI hardware error timer/IRQ/SCI/NMI
  55. * handler. Handler for one generic hardware error source is only
  56. * triggered after the previous one is done. So handler can uses
  57. * struct ghes without locking.
  58. *
  59. * estatus: memory buffer for error status block, allocated during
  60. * HEST parsing.
  61. */
  62. #define GHES_TO_CLEAR 0x0001
  63. struct ghes {
  64. struct acpi_hest_generic *generic;
  65. struct acpi_hest_generic_status *estatus;
  66. struct list_head list;
  67. u64 buffer_paddr;
  68. unsigned long flags;
  69. };
  70. /*
  71. * Error source lists, one list for each notification method. The
  72. * members in lists are struct ghes.
  73. *
  74. * The list members are only added in HEST parsing and deleted during
  75. * module_exit, that is, single-threaded. So no lock is needed for
  76. * that.
  77. *
  78. * But the mutual exclusion is needed between members adding/deleting
  79. * and timer/IRQ/SCI/NMI handler, which may traverse the list. RCU is
  80. * used for that.
  81. */
  82. static LIST_HEAD(ghes_sci);
  83. static struct ghes *ghes_new(struct acpi_hest_generic *generic)
  84. {
  85. struct ghes *ghes;
  86. unsigned int error_block_length;
  87. int rc;
  88. ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
  89. if (!ghes)
  90. return ERR_PTR(-ENOMEM);
  91. ghes->generic = generic;
  92. INIT_LIST_HEAD(&ghes->list);
  93. rc = acpi_pre_map_gar(&generic->error_status_address);
  94. if (rc)
  95. goto err_free;
  96. error_block_length = generic->error_block_length;
  97. if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
  98. pr_warning(FW_WARN GHES_PFX
  99. "Error status block length is too long: %u for "
  100. "generic hardware error source: %d.\n",
  101. error_block_length, generic->header.source_id);
  102. error_block_length = GHES_ESTATUS_MAX_SIZE;
  103. }
  104. ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
  105. if (!ghes->estatus) {
  106. rc = -ENOMEM;
  107. goto err_unmap;
  108. }
  109. return ghes;
  110. err_unmap:
  111. acpi_post_unmap_gar(&generic->error_status_address);
  112. err_free:
  113. kfree(ghes);
  114. return ERR_PTR(rc);
  115. }
  116. static void ghes_fini(struct ghes *ghes)
  117. {
  118. kfree(ghes->estatus);
  119. acpi_post_unmap_gar(&ghes->generic->error_status_address);
  120. }
  121. enum {
  122. GHES_SER_NO = 0x0,
  123. GHES_SER_CORRECTED = 0x1,
  124. GHES_SER_RECOVERABLE = 0x2,
  125. GHES_SER_PANIC = 0x3,
  126. };
  127. static inline int ghes_severity(int severity)
  128. {
  129. switch (severity) {
  130. case CPER_SER_INFORMATIONAL:
  131. return GHES_SER_NO;
  132. case CPER_SER_CORRECTED:
  133. return GHES_SER_CORRECTED;
  134. case CPER_SER_RECOVERABLE:
  135. return GHES_SER_RECOVERABLE;
  136. case CPER_SER_FATAL:
  137. return GHES_SER_PANIC;
  138. default:
  139. /* Unkown, go panic */
  140. return GHES_SER_PANIC;
  141. }
  142. }
  143. /* SCI handler run in work queue, so ioremap can be used here */
  144. static int ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
  145. int from_phys)
  146. {
  147. void *vaddr;
  148. vaddr = ioremap_cache(paddr, len);
  149. if (!vaddr)
  150. return -ENOMEM;
  151. if (from_phys)
  152. memcpy(buffer, vaddr, len);
  153. else
  154. memcpy(vaddr, buffer, len);
  155. iounmap(vaddr);
  156. return 0;
  157. }
  158. static int ghes_read_estatus(struct ghes *ghes, int silent)
  159. {
  160. struct acpi_hest_generic *g = ghes->generic;
  161. u64 buf_paddr;
  162. u32 len;
  163. int rc;
  164. rc = acpi_atomic_read(&buf_paddr, &g->error_status_address);
  165. if (rc) {
  166. if (!silent && printk_ratelimit())
  167. pr_warning(FW_WARN GHES_PFX
  168. "Failed to read error status block address for hardware error source: %d.\n",
  169. g->header.source_id);
  170. return -EIO;
  171. }
  172. if (!buf_paddr)
  173. return -ENOENT;
  174. rc = ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
  175. sizeof(*ghes->estatus), 1);
  176. if (rc)
  177. return rc;
  178. if (!ghes->estatus->block_status)
  179. return -ENOENT;
  180. ghes->buffer_paddr = buf_paddr;
  181. ghes->flags |= GHES_TO_CLEAR;
  182. rc = -EIO;
  183. len = apei_estatus_len(ghes->estatus);
  184. if (len < sizeof(*ghes->estatus))
  185. goto err_read_block;
  186. if (len > ghes->generic->error_block_length)
  187. goto err_read_block;
  188. if (apei_estatus_check_header(ghes->estatus))
  189. goto err_read_block;
  190. rc = ghes_copy_tofrom_phys(ghes->estatus + 1,
  191. buf_paddr + sizeof(*ghes->estatus),
  192. len - sizeof(*ghes->estatus), 1);
  193. if (rc)
  194. return rc;
  195. if (apei_estatus_check(ghes->estatus))
  196. goto err_read_block;
  197. rc = 0;
  198. err_read_block:
  199. if (rc && !silent)
  200. pr_warning(FW_WARN GHES_PFX
  201. "Failed to read error status block!\n");
  202. return rc;
  203. }
  204. static void ghes_clear_estatus(struct ghes *ghes)
  205. {
  206. ghes->estatus->block_status = 0;
  207. if (!(ghes->flags & GHES_TO_CLEAR))
  208. return;
  209. ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
  210. sizeof(ghes->estatus->block_status), 0);
  211. ghes->flags &= ~GHES_TO_CLEAR;
  212. }
  213. static void ghes_do_proc(struct ghes *ghes)
  214. {
  215. int ser, processed = 0;
  216. struct acpi_hest_generic_data *gdata;
  217. ser = ghes_severity(ghes->estatus->error_severity);
  218. apei_estatus_for_each_section(ghes->estatus, gdata) {
  219. #ifdef CONFIG_X86_MCE
  220. if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  221. CPER_SEC_PLATFORM_MEM)) {
  222. apei_mce_report_mem_error(
  223. ser == GHES_SER_CORRECTED,
  224. (struct cper_sec_mem_err *)(gdata+1));
  225. processed = 1;
  226. }
  227. #endif
  228. }
  229. if (!processed && printk_ratelimit())
  230. pr_warning(GHES_PFX
  231. "Unknown error record from generic hardware error source: %d\n",
  232. ghes->generic->header.source_id);
  233. }
  234. static int ghes_proc(struct ghes *ghes)
  235. {
  236. int rc;
  237. rc = ghes_read_estatus(ghes, 0);
  238. if (rc)
  239. goto out;
  240. ghes_do_proc(ghes);
  241. out:
  242. ghes_clear_estatus(ghes);
  243. return 0;
  244. }
  245. static int ghes_notify_sci(struct notifier_block *this,
  246. unsigned long event, void *data)
  247. {
  248. struct ghes *ghes;
  249. int ret = NOTIFY_DONE;
  250. rcu_read_lock();
  251. list_for_each_entry_rcu(ghes, &ghes_sci, list) {
  252. if (!ghes_proc(ghes))
  253. ret = NOTIFY_OK;
  254. }
  255. rcu_read_unlock();
  256. return ret;
  257. }
  258. static struct notifier_block ghes_notifier_sci = {
  259. .notifier_call = ghes_notify_sci,
  260. };
  261. static int hest_ghes_parse(struct acpi_hest_header *hest_hdr, void *data)
  262. {
  263. struct acpi_hest_generic *generic;
  264. struct ghes *ghes = NULL;
  265. int rc = 0;
  266. if (hest_hdr->type != ACPI_HEST_TYPE_GENERIC_ERROR)
  267. return 0;
  268. generic = (struct acpi_hest_generic *)hest_hdr;
  269. if (!generic->enabled)
  270. return 0;
  271. if (generic->error_block_length <
  272. sizeof(struct acpi_hest_generic_status)) {
  273. pr_warning(FW_BUG GHES_PFX
  274. "Invalid error block length: %u for generic hardware error source: %d\n",
  275. generic->error_block_length,
  276. generic->header.source_id);
  277. goto err;
  278. }
  279. if (generic->records_to_preallocate == 0) {
  280. pr_warning(FW_BUG GHES_PFX
  281. "Invalid records to preallocate: %u for generic hardware error source: %d\n",
  282. generic->records_to_preallocate,
  283. generic->header.source_id);
  284. goto err;
  285. }
  286. ghes = ghes_new(generic);
  287. if (IS_ERR(ghes)) {
  288. rc = PTR_ERR(ghes);
  289. ghes = NULL;
  290. goto err;
  291. }
  292. switch (generic->notify.type) {
  293. case ACPI_HEST_NOTIFY_POLLED:
  294. pr_warning(GHES_PFX
  295. "Generic hardware error source: %d notified via POLL is not supported!\n",
  296. generic->header.source_id);
  297. break;
  298. case ACPI_HEST_NOTIFY_EXTERNAL:
  299. case ACPI_HEST_NOTIFY_LOCAL:
  300. pr_warning(GHES_PFX
  301. "Generic hardware error source: %d notified via IRQ is not supported!\n",
  302. generic->header.source_id);
  303. break;
  304. case ACPI_HEST_NOTIFY_SCI:
  305. if (list_empty(&ghes_sci))
  306. register_acpi_hed_notifier(&ghes_notifier_sci);
  307. list_add_rcu(&ghes->list, &ghes_sci);
  308. break;
  309. case ACPI_HEST_NOTIFY_NMI:
  310. pr_warning(GHES_PFX
  311. "Generic hardware error source: %d notified via NMI is not supported!\n",
  312. generic->header.source_id);
  313. break;
  314. default:
  315. pr_warning(FW_WARN GHES_PFX
  316. "Unknown notification type: %u for generic hardware error source: %d\n",
  317. generic->notify.type, generic->header.source_id);
  318. break;
  319. }
  320. return 0;
  321. err:
  322. if (ghes)
  323. ghes_fini(ghes);
  324. return rc;
  325. }
  326. static void ghes_cleanup(void)
  327. {
  328. struct ghes *ghes, *nghes;
  329. if (!list_empty(&ghes_sci))
  330. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  331. synchronize_rcu();
  332. list_for_each_entry_safe(ghes, nghes, &ghes_sci, list) {
  333. list_del(&ghes->list);
  334. ghes_fini(ghes);
  335. kfree(ghes);
  336. }
  337. }
  338. static int __init ghes_init(void)
  339. {
  340. int rc;
  341. if (acpi_disabled)
  342. return -ENODEV;
  343. if (hest_disable) {
  344. pr_info(GHES_PFX "HEST is not enabled!\n");
  345. return -EINVAL;
  346. }
  347. rc = apei_hest_parse(hest_ghes_parse, NULL);
  348. if (rc) {
  349. pr_err(GHES_PFX
  350. "Error during parsing HEST generic hardware error sources.\n");
  351. goto err_cleanup;
  352. }
  353. if (list_empty(&ghes_sci)) {
  354. pr_info(GHES_PFX
  355. "No functional generic hardware error sources.\n");
  356. rc = -ENODEV;
  357. goto err_cleanup;
  358. }
  359. pr_info(GHES_PFX
  360. "Generic Hardware Error Source support is initialized.\n");
  361. return 0;
  362. err_cleanup:
  363. ghes_cleanup();
  364. return rc;
  365. }
  366. static void __exit ghes_exit(void)
  367. {
  368. ghes_cleanup();
  369. }
  370. module_init(ghes_init);
  371. module_exit(ghes_exit);
  372. MODULE_AUTHOR("Huang Ying");
  373. MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
  374. MODULE_LICENSE("GPL");