einj.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * APEI Error INJection support
  3. *
  4. * EINJ provides a hardware error injection mechanism, this is useful
  5. * for debugging and testing of other APEI and RAS features.
  6. *
  7. * For more information about EINJ, please refer to ACPI Specification
  8. * version 4.0, section 17.5.
  9. *
  10. * Copyright 2009-2010 Intel Corp.
  11. * Author: Huang Ying <ying.huang@intel.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version
  15. * 2 as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/nmi.h>
  33. #include <linux/delay.h>
  34. #include <acpi/acpi.h>
  35. #include "apei-internal.h"
  36. #define EINJ_PFX "EINJ: "
  37. #define SPIN_UNIT 100 /* 100ns */
  38. /* Firmware should respond within 1 miliseconds */
  39. #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
  40. /*
  41. * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
  42. * EINJ table through an unpublished extension. Use with caution as
  43. * most will ignore the parameter and make their own choice of address
  44. * for error injection.
  45. */
  46. struct einj_parameter {
  47. u64 type;
  48. u64 reserved1;
  49. u64 reserved2;
  50. u64 param1;
  51. u64 param2;
  52. };
  53. #define EINJ_OP_BUSY 0x1
  54. #define EINJ_STATUS_SUCCESS 0x0
  55. #define EINJ_STATUS_FAIL 0x1
  56. #define EINJ_STATUS_INVAL 0x2
  57. #define EINJ_TAB_ENTRY(tab) \
  58. ((struct acpi_whea_header *)((char *)(tab) + \
  59. sizeof(struct acpi_table_einj)))
  60. static struct acpi_table_einj *einj_tab;
  61. static struct apei_resources einj_resources;
  62. static struct apei_exec_ins_type einj_ins_type[] = {
  63. [ACPI_EINJ_READ_REGISTER] = {
  64. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  65. .run = apei_exec_read_register,
  66. },
  67. [ACPI_EINJ_READ_REGISTER_VALUE] = {
  68. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  69. .run = apei_exec_read_register_value,
  70. },
  71. [ACPI_EINJ_WRITE_REGISTER] = {
  72. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  73. .run = apei_exec_write_register,
  74. },
  75. [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
  76. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  77. .run = apei_exec_write_register_value,
  78. },
  79. [ACPI_EINJ_NOOP] = {
  80. .flags = 0,
  81. .run = apei_exec_noop,
  82. },
  83. };
  84. /*
  85. * Prevent EINJ interpreter to run simultaneously, because the
  86. * corresponding firmware implementation may not work properly when
  87. * invoked simultaneously.
  88. */
  89. static DEFINE_MUTEX(einj_mutex);
  90. static struct einj_parameter *einj_param;
  91. static void einj_exec_ctx_init(struct apei_exec_context *ctx)
  92. {
  93. apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
  94. EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
  95. }
  96. static int __einj_get_available_error_type(u32 *type)
  97. {
  98. struct apei_exec_context ctx;
  99. int rc;
  100. einj_exec_ctx_init(&ctx);
  101. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
  102. if (rc)
  103. return rc;
  104. *type = apei_exec_ctx_get_output(&ctx);
  105. return 0;
  106. }
  107. /* Get error injection capabilities of the platform */
  108. static int einj_get_available_error_type(u32 *type)
  109. {
  110. int rc;
  111. mutex_lock(&einj_mutex);
  112. rc = __einj_get_available_error_type(type);
  113. mutex_unlock(&einj_mutex);
  114. return rc;
  115. }
  116. static int einj_timedout(u64 *t)
  117. {
  118. if ((s64)*t < SPIN_UNIT) {
  119. pr_warning(FW_WARN EINJ_PFX
  120. "Firmware does not respond in time\n");
  121. return 1;
  122. }
  123. *t -= SPIN_UNIT;
  124. ndelay(SPIN_UNIT);
  125. touch_nmi_watchdog();
  126. return 0;
  127. }
  128. static u64 einj_get_parameter_address(void)
  129. {
  130. int i;
  131. u64 paddr = 0;
  132. struct acpi_whea_header *entry;
  133. entry = EINJ_TAB_ENTRY(einj_tab);
  134. for (i = 0; i < einj_tab->entries; i++) {
  135. if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
  136. entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
  137. entry->register_region.space_id ==
  138. ACPI_ADR_SPACE_SYSTEM_MEMORY)
  139. memcpy(&paddr, &entry->register_region.address,
  140. sizeof(paddr));
  141. entry++;
  142. }
  143. return paddr;
  144. }
  145. /* do sanity check to trigger table */
  146. static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
  147. {
  148. if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
  149. return -EINVAL;
  150. if (trigger_tab->table_size > PAGE_SIZE ||
  151. trigger_tab->table_size <= trigger_tab->header_size)
  152. return -EINVAL;
  153. if (trigger_tab->entry_count !=
  154. (trigger_tab->table_size - trigger_tab->header_size) /
  155. sizeof(struct acpi_einj_entry))
  156. return -EINVAL;
  157. return 0;
  158. }
  159. /* Execute instructions in trigger error action table */
  160. static int __einj_error_trigger(u64 trigger_paddr)
  161. {
  162. struct acpi_einj_trigger *trigger_tab = NULL;
  163. struct apei_exec_context trigger_ctx;
  164. struct apei_resources trigger_resources;
  165. struct acpi_whea_header *trigger_entry;
  166. struct resource *r;
  167. u32 table_size;
  168. int rc = -EIO;
  169. r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
  170. "APEI EINJ Trigger Table");
  171. if (!r) {
  172. pr_err(EINJ_PFX
  173. "Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
  174. (unsigned long long)trigger_paddr,
  175. (unsigned long long)trigger_paddr+sizeof(*trigger_tab));
  176. goto out;
  177. }
  178. trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
  179. if (!trigger_tab) {
  180. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  181. goto out_rel_header;
  182. }
  183. rc = einj_check_trigger_header(trigger_tab);
  184. if (rc) {
  185. pr_warning(FW_BUG EINJ_PFX
  186. "The trigger error action table is invalid\n");
  187. goto out_rel_header;
  188. }
  189. rc = -EIO;
  190. table_size = trigger_tab->table_size;
  191. r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
  192. table_size - sizeof(*trigger_tab),
  193. "APEI EINJ Trigger Table");
  194. if (!r) {
  195. pr_err(EINJ_PFX
  196. "Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
  197. (unsigned long long)trigger_paddr+sizeof(*trigger_tab),
  198. (unsigned long long)trigger_paddr + table_size);
  199. goto out_rel_header;
  200. }
  201. iounmap(trigger_tab);
  202. trigger_tab = ioremap_cache(trigger_paddr, table_size);
  203. if (!trigger_tab) {
  204. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  205. goto out_rel_entry;
  206. }
  207. trigger_entry = (struct acpi_whea_header *)
  208. ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
  209. apei_resources_init(&trigger_resources);
  210. apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
  211. ARRAY_SIZE(einj_ins_type),
  212. trigger_entry, trigger_tab->entry_count);
  213. rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
  214. if (rc)
  215. goto out_fini;
  216. rc = apei_resources_sub(&trigger_resources, &einj_resources);
  217. if (rc)
  218. goto out_fini;
  219. rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
  220. if (rc)
  221. goto out_fini;
  222. rc = apei_exec_pre_map_gars(&trigger_ctx);
  223. if (rc)
  224. goto out_release;
  225. rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
  226. apei_exec_post_unmap_gars(&trigger_ctx);
  227. out_release:
  228. apei_resources_release(&trigger_resources);
  229. out_fini:
  230. apei_resources_fini(&trigger_resources);
  231. out_rel_entry:
  232. release_mem_region(trigger_paddr + sizeof(*trigger_tab),
  233. table_size - sizeof(*trigger_tab));
  234. out_rel_header:
  235. release_mem_region(trigger_paddr, sizeof(*trigger_tab));
  236. out:
  237. if (trigger_tab)
  238. iounmap(trigger_tab);
  239. return rc;
  240. }
  241. static int __einj_error_inject(u32 type, u64 param1, u64 param2)
  242. {
  243. struct apei_exec_context ctx;
  244. u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
  245. int rc;
  246. einj_exec_ctx_init(&ctx);
  247. rc = apei_exec_run(&ctx, ACPI_EINJ_BEGIN_OPERATION);
  248. if (rc)
  249. return rc;
  250. apei_exec_ctx_set_input(&ctx, type);
  251. rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
  252. if (rc)
  253. return rc;
  254. if (einj_param) {
  255. writeq(param1, &einj_param->param1);
  256. writeq(param2, &einj_param->param2);
  257. }
  258. rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
  259. if (rc)
  260. return rc;
  261. for (;;) {
  262. rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
  263. if (rc)
  264. return rc;
  265. val = apei_exec_ctx_get_output(&ctx);
  266. if (!(val & EINJ_OP_BUSY))
  267. break;
  268. if (einj_timedout(&timeout))
  269. return -EIO;
  270. }
  271. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
  272. if (rc)
  273. return rc;
  274. val = apei_exec_ctx_get_output(&ctx);
  275. if (val != EINJ_STATUS_SUCCESS)
  276. return -EBUSY;
  277. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
  278. if (rc)
  279. return rc;
  280. trigger_paddr = apei_exec_ctx_get_output(&ctx);
  281. rc = __einj_error_trigger(trigger_paddr);
  282. if (rc)
  283. return rc;
  284. rc = apei_exec_run(&ctx, ACPI_EINJ_END_OPERATION);
  285. return rc;
  286. }
  287. /* Inject the specified hardware error */
  288. static int einj_error_inject(u32 type, u64 param1, u64 param2)
  289. {
  290. int rc;
  291. mutex_lock(&einj_mutex);
  292. rc = __einj_error_inject(type, param1, param2);
  293. mutex_unlock(&einj_mutex);
  294. return rc;
  295. }
  296. static u32 error_type;
  297. static u64 error_param1;
  298. static u64 error_param2;
  299. static struct dentry *einj_debug_dir;
  300. static int available_error_type_show(struct seq_file *m, void *v)
  301. {
  302. int rc;
  303. u32 available_error_type = 0;
  304. rc = einj_get_available_error_type(&available_error_type);
  305. if (rc)
  306. return rc;
  307. if (available_error_type & 0x0001)
  308. seq_printf(m, "0x00000001\tProcessor Correctable\n");
  309. if (available_error_type & 0x0002)
  310. seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
  311. if (available_error_type & 0x0004)
  312. seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
  313. if (available_error_type & 0x0008)
  314. seq_printf(m, "0x00000008\tMemory Correctable\n");
  315. if (available_error_type & 0x0010)
  316. seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
  317. if (available_error_type & 0x0020)
  318. seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
  319. if (available_error_type & 0x0040)
  320. seq_printf(m, "0x00000040\tPCI Express Correctable\n");
  321. if (available_error_type & 0x0080)
  322. seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
  323. if (available_error_type & 0x0100)
  324. seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
  325. if (available_error_type & 0x0200)
  326. seq_printf(m, "0x00000200\tPlatform Correctable\n");
  327. if (available_error_type & 0x0400)
  328. seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
  329. if (available_error_type & 0x0800)
  330. seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
  331. return 0;
  332. }
  333. static int available_error_type_open(struct inode *inode, struct file *file)
  334. {
  335. return single_open(file, available_error_type_show, NULL);
  336. }
  337. static const struct file_operations available_error_type_fops = {
  338. .open = available_error_type_open,
  339. .read = seq_read,
  340. .llseek = seq_lseek,
  341. .release = single_release,
  342. };
  343. static int error_type_get(void *data, u64 *val)
  344. {
  345. *val = error_type;
  346. return 0;
  347. }
  348. static int error_type_set(void *data, u64 val)
  349. {
  350. int rc;
  351. u32 available_error_type = 0;
  352. /* Only one error type can be specified */
  353. if (val & (val - 1))
  354. return -EINVAL;
  355. rc = einj_get_available_error_type(&available_error_type);
  356. if (rc)
  357. return rc;
  358. if (!(val & available_error_type))
  359. return -EINVAL;
  360. error_type = val;
  361. return 0;
  362. }
  363. DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
  364. error_type_set, "0x%llx\n");
  365. static int error_inject_set(void *data, u64 val)
  366. {
  367. if (!error_type)
  368. return -EINVAL;
  369. return einj_error_inject(error_type, error_param1, error_param2);
  370. }
  371. DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
  372. error_inject_set, "%llu\n");
  373. static int einj_check_table(struct acpi_table_einj *einj_tab)
  374. {
  375. if ((einj_tab->header_length !=
  376. (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
  377. && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
  378. return -EINVAL;
  379. if (einj_tab->header.length < sizeof(struct acpi_table_einj))
  380. return -EINVAL;
  381. if (einj_tab->entries !=
  382. (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
  383. sizeof(struct acpi_einj_entry))
  384. return -EINVAL;
  385. return 0;
  386. }
  387. static int __init einj_init(void)
  388. {
  389. int rc;
  390. u64 param_paddr;
  391. acpi_status status;
  392. struct dentry *fentry;
  393. struct apei_exec_context ctx;
  394. if (acpi_disabled)
  395. return -ENODEV;
  396. status = acpi_get_table(ACPI_SIG_EINJ, 0,
  397. (struct acpi_table_header **)&einj_tab);
  398. if (status == AE_NOT_FOUND) {
  399. pr_info(EINJ_PFX "Table is not found!\n");
  400. return -ENODEV;
  401. } else if (ACPI_FAILURE(status)) {
  402. const char *msg = acpi_format_exception(status);
  403. pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
  404. return -EINVAL;
  405. }
  406. rc = einj_check_table(einj_tab);
  407. if (rc) {
  408. pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
  409. return -EINVAL;
  410. }
  411. rc = -ENOMEM;
  412. einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
  413. if (!einj_debug_dir)
  414. goto err_cleanup;
  415. fentry = debugfs_create_file("available_error_type", S_IRUSR,
  416. einj_debug_dir, NULL,
  417. &available_error_type_fops);
  418. if (!fentry)
  419. goto err_cleanup;
  420. fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
  421. einj_debug_dir, NULL, &error_type_fops);
  422. if (!fentry)
  423. goto err_cleanup;
  424. fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
  425. einj_debug_dir, &error_param1);
  426. if (!fentry)
  427. goto err_cleanup;
  428. fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
  429. einj_debug_dir, &error_param2);
  430. if (!fentry)
  431. goto err_cleanup;
  432. fentry = debugfs_create_file("error_inject", S_IWUSR,
  433. einj_debug_dir, NULL, &error_inject_fops);
  434. if (!fentry)
  435. goto err_cleanup;
  436. apei_resources_init(&einj_resources);
  437. einj_exec_ctx_init(&ctx);
  438. rc = apei_exec_collect_resources(&ctx, &einj_resources);
  439. if (rc)
  440. goto err_fini;
  441. rc = apei_resources_request(&einj_resources, "APEI EINJ");
  442. if (rc)
  443. goto err_fini;
  444. rc = apei_exec_pre_map_gars(&ctx);
  445. if (rc)
  446. goto err_release;
  447. param_paddr = einj_get_parameter_address();
  448. if (param_paddr) {
  449. einj_param = ioremap(param_paddr, sizeof(*einj_param));
  450. rc = -ENOMEM;
  451. if (!einj_param)
  452. goto err_unmap;
  453. }
  454. pr_info(EINJ_PFX "Error INJection is initialized.\n");
  455. return 0;
  456. err_unmap:
  457. apei_exec_post_unmap_gars(&ctx);
  458. err_release:
  459. apei_resources_release(&einj_resources);
  460. err_fini:
  461. apei_resources_fini(&einj_resources);
  462. err_cleanup:
  463. debugfs_remove_recursive(einj_debug_dir);
  464. return rc;
  465. }
  466. static void __exit einj_exit(void)
  467. {
  468. struct apei_exec_context ctx;
  469. if (einj_param)
  470. iounmap(einj_param);
  471. einj_exec_ctx_init(&ctx);
  472. apei_exec_post_unmap_gars(&ctx);
  473. apei_resources_release(&einj_resources);
  474. apei_resources_fini(&einj_resources);
  475. debugfs_remove_recursive(einj_debug_dir);
  476. }
  477. module_init(einj_init);
  478. module_exit(einj_exit);
  479. MODULE_AUTHOR("Huang Ying");
  480. MODULE_DESCRIPTION("APEI Error INJection support");
  481. MODULE_LICENSE("GPL");