ghes.c 11 KB

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