ghes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 <linux/platform_device.h>
  44. #include <linux/mutex.h>
  45. #include <acpi/apei.h>
  46. #include <acpi/atomicio.h>
  47. #include <acpi/hed.h>
  48. #include <asm/mce.h>
  49. #include "apei-internal.h"
  50. #define GHES_PFX "GHES: "
  51. #define GHES_ESTATUS_MAX_SIZE 65536
  52. /*
  53. * One struct ghes is created for each generic hardware error
  54. * source.
  55. *
  56. * It provides the context for APEI hardware error timer/IRQ/SCI/NMI
  57. * handler. Handler for one generic hardware error source is only
  58. * triggered after the previous one is done. So handler can uses
  59. * struct ghes without locking.
  60. *
  61. * estatus: memory buffer for error status block, allocated during
  62. * HEST parsing.
  63. */
  64. #define GHES_TO_CLEAR 0x0001
  65. struct ghes {
  66. struct acpi_hest_generic *generic;
  67. struct acpi_hest_generic_status *estatus;
  68. struct list_head list;
  69. u64 buffer_paddr;
  70. unsigned long flags;
  71. };
  72. /*
  73. * Error source lists, one list for each notification method. The
  74. * members in lists are struct ghes.
  75. *
  76. * The list members are only added in HEST parsing and deleted during
  77. * module_exit, that is, single-threaded. So no lock is needed for
  78. * that.
  79. *
  80. * But the mutual exclusion is needed between members adding/deleting
  81. * and timer/IRQ/SCI/NMI handler, which may traverse the list. RCU is
  82. * used for that.
  83. */
  84. static LIST_HEAD(ghes_sci);
  85. static DEFINE_MUTEX(ghes_list_mutex);
  86. static struct ghes *ghes_new(struct acpi_hest_generic *generic)
  87. {
  88. struct ghes *ghes;
  89. unsigned int error_block_length;
  90. int rc;
  91. ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
  92. if (!ghes)
  93. return ERR_PTR(-ENOMEM);
  94. ghes->generic = generic;
  95. INIT_LIST_HEAD(&ghes->list);
  96. rc = acpi_pre_map_gar(&generic->error_status_address);
  97. if (rc)
  98. goto err_free;
  99. error_block_length = generic->error_block_length;
  100. if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
  101. pr_warning(FW_WARN GHES_PFX
  102. "Error status block length is too long: %u for "
  103. "generic hardware error source: %d.\n",
  104. error_block_length, generic->header.source_id);
  105. error_block_length = GHES_ESTATUS_MAX_SIZE;
  106. }
  107. ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
  108. if (!ghes->estatus) {
  109. rc = -ENOMEM;
  110. goto err_unmap;
  111. }
  112. return ghes;
  113. err_unmap:
  114. acpi_post_unmap_gar(&generic->error_status_address);
  115. err_free:
  116. kfree(ghes);
  117. return ERR_PTR(rc);
  118. }
  119. static void ghes_fini(struct ghes *ghes)
  120. {
  121. kfree(ghes->estatus);
  122. acpi_post_unmap_gar(&ghes->generic->error_status_address);
  123. }
  124. enum {
  125. GHES_SEV_NO = 0x0,
  126. GHES_SEV_CORRECTED = 0x1,
  127. GHES_SEV_RECOVERABLE = 0x2,
  128. GHES_SEV_PANIC = 0x3,
  129. };
  130. static inline int ghes_severity(int severity)
  131. {
  132. switch (severity) {
  133. case CPER_SEV_INFORMATIONAL:
  134. return GHES_SEV_NO;
  135. case CPER_SEV_CORRECTED:
  136. return GHES_SEV_CORRECTED;
  137. case CPER_SEV_RECOVERABLE:
  138. return GHES_SEV_RECOVERABLE;
  139. case CPER_SEV_FATAL:
  140. return GHES_SEV_PANIC;
  141. default:
  142. /* Unkown, go panic */
  143. return GHES_SEV_PANIC;
  144. }
  145. }
  146. /* SCI handler run in work queue, so ioremap can be used here */
  147. static int ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
  148. int from_phys)
  149. {
  150. void *vaddr;
  151. vaddr = ioremap_cache(paddr, len);
  152. if (!vaddr)
  153. return -ENOMEM;
  154. if (from_phys)
  155. memcpy(buffer, vaddr, len);
  156. else
  157. memcpy(vaddr, buffer, len);
  158. iounmap(vaddr);
  159. return 0;
  160. }
  161. static int ghes_read_estatus(struct ghes *ghes, int silent)
  162. {
  163. struct acpi_hest_generic *g = ghes->generic;
  164. u64 buf_paddr;
  165. u32 len;
  166. int rc;
  167. rc = acpi_atomic_read(&buf_paddr, &g->error_status_address);
  168. if (rc) {
  169. if (!silent && printk_ratelimit())
  170. pr_warning(FW_WARN GHES_PFX
  171. "Failed to read error status block address for hardware error source: %d.\n",
  172. g->header.source_id);
  173. return -EIO;
  174. }
  175. if (!buf_paddr)
  176. return -ENOENT;
  177. rc = ghes_copy_tofrom_phys(ghes->estatus, buf_paddr,
  178. sizeof(*ghes->estatus), 1);
  179. if (rc)
  180. return rc;
  181. if (!ghes->estatus->block_status)
  182. return -ENOENT;
  183. ghes->buffer_paddr = buf_paddr;
  184. ghes->flags |= GHES_TO_CLEAR;
  185. rc = -EIO;
  186. len = apei_estatus_len(ghes->estatus);
  187. if (len < sizeof(*ghes->estatus))
  188. goto err_read_block;
  189. if (len > ghes->generic->error_block_length)
  190. goto err_read_block;
  191. if (apei_estatus_check_header(ghes->estatus))
  192. goto err_read_block;
  193. rc = ghes_copy_tofrom_phys(ghes->estatus + 1,
  194. buf_paddr + sizeof(*ghes->estatus),
  195. len - sizeof(*ghes->estatus), 1);
  196. if (rc)
  197. return rc;
  198. if (apei_estatus_check(ghes->estatus))
  199. goto err_read_block;
  200. rc = 0;
  201. err_read_block:
  202. if (rc && !silent)
  203. pr_warning(FW_WARN GHES_PFX
  204. "Failed to read error status block!\n");
  205. return rc;
  206. }
  207. static void ghes_clear_estatus(struct ghes *ghes)
  208. {
  209. ghes->estatus->block_status = 0;
  210. if (!(ghes->flags & GHES_TO_CLEAR))
  211. return;
  212. ghes_copy_tofrom_phys(ghes->estatus, ghes->buffer_paddr,
  213. sizeof(ghes->estatus->block_status), 0);
  214. ghes->flags &= ~GHES_TO_CLEAR;
  215. }
  216. static void ghes_do_proc(struct ghes *ghes)
  217. {
  218. int sev, processed = 0;
  219. struct acpi_hest_generic_data *gdata;
  220. sev = ghes_severity(ghes->estatus->error_severity);
  221. apei_estatus_for_each_section(ghes->estatus, gdata) {
  222. #ifdef CONFIG_X86_MCE
  223. if (!uuid_le_cmp(*(uuid_le *)gdata->section_type,
  224. CPER_SEC_PLATFORM_MEM)) {
  225. apei_mce_report_mem_error(
  226. sev == GHES_SEV_CORRECTED,
  227. (struct cper_sec_mem_err *)(gdata+1));
  228. processed = 1;
  229. }
  230. #endif
  231. }
  232. if (!processed && printk_ratelimit())
  233. pr_warning(GHES_PFX
  234. "Unknown error record from generic hardware error source: %d\n",
  235. ghes->generic->header.source_id);
  236. }
  237. static int ghes_proc(struct ghes *ghes)
  238. {
  239. int rc;
  240. rc = ghes_read_estatus(ghes, 0);
  241. if (rc)
  242. goto out;
  243. ghes_do_proc(ghes);
  244. out:
  245. ghes_clear_estatus(ghes);
  246. return 0;
  247. }
  248. static int ghes_notify_sci(struct notifier_block *this,
  249. unsigned long event, void *data)
  250. {
  251. struct ghes *ghes;
  252. int ret = NOTIFY_DONE;
  253. rcu_read_lock();
  254. list_for_each_entry_rcu(ghes, &ghes_sci, list) {
  255. if (!ghes_proc(ghes))
  256. ret = NOTIFY_OK;
  257. }
  258. rcu_read_unlock();
  259. return ret;
  260. }
  261. static struct notifier_block ghes_notifier_sci = {
  262. .notifier_call = ghes_notify_sci,
  263. };
  264. static int __devinit ghes_probe(struct platform_device *ghes_dev)
  265. {
  266. struct acpi_hest_generic *generic;
  267. struct ghes *ghes = NULL;
  268. int rc = -EINVAL;
  269. generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
  270. if (!generic->enabled)
  271. return -ENODEV;
  272. if (generic->error_block_length <
  273. sizeof(struct acpi_hest_generic_status)) {
  274. pr_warning(FW_BUG GHES_PFX
  275. "Invalid error block length: %u for generic hardware error source: %d\n",
  276. generic->error_block_length,
  277. generic->header.source_id);
  278. goto err;
  279. }
  280. if (generic->records_to_preallocate == 0) {
  281. pr_warning(FW_BUG GHES_PFX
  282. "Invalid records to preallocate: %u for generic hardware error source: %d\n",
  283. generic->records_to_preallocate,
  284. generic->header.source_id);
  285. goto err;
  286. }
  287. ghes = ghes_new(generic);
  288. if (IS_ERR(ghes)) {
  289. rc = PTR_ERR(ghes);
  290. ghes = NULL;
  291. goto err;
  292. }
  293. if (generic->notify.type == ACPI_HEST_NOTIFY_SCI) {
  294. mutex_lock(&ghes_list_mutex);
  295. if (list_empty(&ghes_sci))
  296. register_acpi_hed_notifier(&ghes_notifier_sci);
  297. list_add_rcu(&ghes->list, &ghes_sci);
  298. mutex_unlock(&ghes_list_mutex);
  299. } else {
  300. unsigned char *notify = NULL;
  301. switch (generic->notify.type) {
  302. case ACPI_HEST_NOTIFY_POLLED:
  303. notify = "POLL";
  304. break;
  305. case ACPI_HEST_NOTIFY_EXTERNAL:
  306. case ACPI_HEST_NOTIFY_LOCAL:
  307. notify = "IRQ";
  308. break;
  309. case ACPI_HEST_NOTIFY_NMI:
  310. notify = "NMI";
  311. break;
  312. }
  313. if (notify) {
  314. pr_warning(GHES_PFX
  315. "Generic hardware error source: %d notified via %s is not supported!\n",
  316. generic->header.source_id, notify);
  317. } else {
  318. pr_warning(FW_WARN GHES_PFX
  319. "Unknown notification type: %u for generic hardware error source: %d\n",
  320. generic->notify.type, generic->header.source_id);
  321. }
  322. rc = -ENODEV;
  323. goto err;
  324. }
  325. platform_set_drvdata(ghes_dev, ghes);
  326. return 0;
  327. err:
  328. if (ghes) {
  329. ghes_fini(ghes);
  330. kfree(ghes);
  331. }
  332. return rc;
  333. }
  334. static int __devexit ghes_remove(struct platform_device *ghes_dev)
  335. {
  336. struct ghes *ghes;
  337. struct acpi_hest_generic *generic;
  338. ghes = platform_get_drvdata(ghes_dev);
  339. generic = ghes->generic;
  340. switch (generic->notify.type) {
  341. case ACPI_HEST_NOTIFY_SCI:
  342. mutex_lock(&ghes_list_mutex);
  343. list_del_rcu(&ghes->list);
  344. if (list_empty(&ghes_sci))
  345. unregister_acpi_hed_notifier(&ghes_notifier_sci);
  346. mutex_unlock(&ghes_list_mutex);
  347. break;
  348. default:
  349. BUG();
  350. break;
  351. }
  352. synchronize_rcu();
  353. ghes_fini(ghes);
  354. kfree(ghes);
  355. platform_set_drvdata(ghes_dev, NULL);
  356. return 0;
  357. }
  358. static struct platform_driver ghes_platform_driver = {
  359. .driver = {
  360. .name = "GHES",
  361. .owner = THIS_MODULE,
  362. },
  363. .probe = ghes_probe,
  364. .remove = ghes_remove,
  365. };
  366. static int __init ghes_init(void)
  367. {
  368. if (acpi_disabled)
  369. return -ENODEV;
  370. if (hest_disable) {
  371. pr_info(GHES_PFX "HEST is not enabled!\n");
  372. return -EINVAL;
  373. }
  374. return platform_driver_register(&ghes_platform_driver);
  375. }
  376. static void __exit ghes_exit(void)
  377. {
  378. platform_driver_unregister(&ghes_platform_driver);
  379. }
  380. module_init(ghes_init);
  381. module_exit(ghes_exit);
  382. MODULE_AUTHOR("Huang Ying");
  383. MODULE_DESCRIPTION("APEI Generic Hardware Error Source support");
  384. MODULE_LICENSE("GPL");
  385. MODULE_ALIAS("platform:GHES");