einj.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 milliseconds */
  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. This extension is used only if
  45. * param_extension module parameter is specified.
  46. */
  47. struct einj_parameter {
  48. u64 type;
  49. u64 reserved1;
  50. u64 reserved2;
  51. u64 param1;
  52. u64 param2;
  53. };
  54. #define EINJ_OP_BUSY 0x1
  55. #define EINJ_STATUS_SUCCESS 0x0
  56. #define EINJ_STATUS_FAIL 0x1
  57. #define EINJ_STATUS_INVAL 0x2
  58. #define EINJ_TAB_ENTRY(tab) \
  59. ((struct acpi_whea_header *)((char *)(tab) + \
  60. sizeof(struct acpi_table_einj)))
  61. static bool param_extension;
  62. module_param(param_extension, bool, 0);
  63. static struct acpi_table_einj *einj_tab;
  64. static struct apei_resources einj_resources;
  65. static struct apei_exec_ins_type einj_ins_type[] = {
  66. [ACPI_EINJ_READ_REGISTER] = {
  67. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  68. .run = apei_exec_read_register,
  69. },
  70. [ACPI_EINJ_READ_REGISTER_VALUE] = {
  71. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  72. .run = apei_exec_read_register_value,
  73. },
  74. [ACPI_EINJ_WRITE_REGISTER] = {
  75. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  76. .run = apei_exec_write_register,
  77. },
  78. [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
  79. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  80. .run = apei_exec_write_register_value,
  81. },
  82. [ACPI_EINJ_NOOP] = {
  83. .flags = 0,
  84. .run = apei_exec_noop,
  85. },
  86. };
  87. /*
  88. * Prevent EINJ interpreter to run simultaneously, because the
  89. * corresponding firmware implementation may not work properly when
  90. * invoked simultaneously.
  91. */
  92. static DEFINE_MUTEX(einj_mutex);
  93. static struct einj_parameter *einj_param;
  94. #ifndef writeq
  95. static inline void writeq(__u64 val, volatile void __iomem *addr)
  96. {
  97. writel(val, addr);
  98. writel(val >> 32, addr+4);
  99. }
  100. #endif
  101. static void einj_exec_ctx_init(struct apei_exec_context *ctx)
  102. {
  103. apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
  104. EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
  105. }
  106. static int __einj_get_available_error_type(u32 *type)
  107. {
  108. struct apei_exec_context ctx;
  109. int rc;
  110. einj_exec_ctx_init(&ctx);
  111. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
  112. if (rc)
  113. return rc;
  114. *type = apei_exec_ctx_get_output(&ctx);
  115. return 0;
  116. }
  117. /* Get error injection capabilities of the platform */
  118. static int einj_get_available_error_type(u32 *type)
  119. {
  120. int rc;
  121. mutex_lock(&einj_mutex);
  122. rc = __einj_get_available_error_type(type);
  123. mutex_unlock(&einj_mutex);
  124. return rc;
  125. }
  126. static int einj_timedout(u64 *t)
  127. {
  128. if ((s64)*t < SPIN_UNIT) {
  129. pr_warning(FW_WARN EINJ_PFX
  130. "Firmware does not respond in time\n");
  131. return 1;
  132. }
  133. *t -= SPIN_UNIT;
  134. ndelay(SPIN_UNIT);
  135. touch_nmi_watchdog();
  136. return 0;
  137. }
  138. static u64 einj_get_parameter_address(void)
  139. {
  140. int i;
  141. u64 paddr = 0;
  142. struct acpi_whea_header *entry;
  143. entry = EINJ_TAB_ENTRY(einj_tab);
  144. for (i = 0; i < einj_tab->entries; i++) {
  145. if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
  146. entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
  147. entry->register_region.space_id ==
  148. ACPI_ADR_SPACE_SYSTEM_MEMORY)
  149. memcpy(&paddr, &entry->register_region.address,
  150. sizeof(paddr));
  151. entry++;
  152. }
  153. return paddr;
  154. }
  155. /* do sanity check to trigger table */
  156. static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
  157. {
  158. if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
  159. return -EINVAL;
  160. if (trigger_tab->table_size > PAGE_SIZE ||
  161. trigger_tab->table_size <= trigger_tab->header_size)
  162. return -EINVAL;
  163. if (trigger_tab->entry_count !=
  164. (trigger_tab->table_size - trigger_tab->header_size) /
  165. sizeof(struct acpi_einj_entry))
  166. return -EINVAL;
  167. return 0;
  168. }
  169. /* Execute instructions in trigger error action table */
  170. static int __einj_error_trigger(u64 trigger_paddr)
  171. {
  172. struct acpi_einj_trigger *trigger_tab = NULL;
  173. struct apei_exec_context trigger_ctx;
  174. struct apei_resources trigger_resources;
  175. struct acpi_whea_header *trigger_entry;
  176. struct resource *r;
  177. u32 table_size;
  178. int rc = -EIO;
  179. r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
  180. "APEI EINJ Trigger Table");
  181. if (!r) {
  182. pr_err(EINJ_PFX
  183. "Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
  184. (unsigned long long)trigger_paddr,
  185. (unsigned long long)trigger_paddr+sizeof(*trigger_tab));
  186. goto out;
  187. }
  188. trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
  189. if (!trigger_tab) {
  190. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  191. goto out_rel_header;
  192. }
  193. rc = einj_check_trigger_header(trigger_tab);
  194. if (rc) {
  195. pr_warning(FW_BUG EINJ_PFX
  196. "The trigger error action table is invalid\n");
  197. goto out_rel_header;
  198. }
  199. rc = -EIO;
  200. table_size = trigger_tab->table_size;
  201. r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
  202. table_size - sizeof(*trigger_tab),
  203. "APEI EINJ Trigger Table");
  204. if (!r) {
  205. pr_err(EINJ_PFX
  206. "Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
  207. (unsigned long long)trigger_paddr+sizeof(*trigger_tab),
  208. (unsigned long long)trigger_paddr + table_size);
  209. goto out_rel_header;
  210. }
  211. iounmap(trigger_tab);
  212. trigger_tab = ioremap_cache(trigger_paddr, table_size);
  213. if (!trigger_tab) {
  214. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  215. goto out_rel_entry;
  216. }
  217. trigger_entry = (struct acpi_whea_header *)
  218. ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
  219. apei_resources_init(&trigger_resources);
  220. apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
  221. ARRAY_SIZE(einj_ins_type),
  222. trigger_entry, trigger_tab->entry_count);
  223. rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
  224. if (rc)
  225. goto out_fini;
  226. rc = apei_resources_sub(&trigger_resources, &einj_resources);
  227. if (rc)
  228. goto out_fini;
  229. rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
  230. if (rc)
  231. goto out_fini;
  232. rc = apei_exec_pre_map_gars(&trigger_ctx);
  233. if (rc)
  234. goto out_release;
  235. rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
  236. apei_exec_post_unmap_gars(&trigger_ctx);
  237. out_release:
  238. apei_resources_release(&trigger_resources);
  239. out_fini:
  240. apei_resources_fini(&trigger_resources);
  241. out_rel_entry:
  242. release_mem_region(trigger_paddr + sizeof(*trigger_tab),
  243. table_size - sizeof(*trigger_tab));
  244. out_rel_header:
  245. release_mem_region(trigger_paddr, sizeof(*trigger_tab));
  246. out:
  247. if (trigger_tab)
  248. iounmap(trigger_tab);
  249. return rc;
  250. }
  251. static int __einj_error_inject(u32 type, u64 param1, u64 param2)
  252. {
  253. struct apei_exec_context ctx;
  254. u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
  255. int rc;
  256. einj_exec_ctx_init(&ctx);
  257. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
  258. if (rc)
  259. return rc;
  260. apei_exec_ctx_set_input(&ctx, type);
  261. rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
  262. if (rc)
  263. return rc;
  264. if (einj_param) {
  265. writeq(param1, &einj_param->param1);
  266. writeq(param2, &einj_param->param2);
  267. }
  268. rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
  269. if (rc)
  270. return rc;
  271. for (;;) {
  272. rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
  273. if (rc)
  274. return rc;
  275. val = apei_exec_ctx_get_output(&ctx);
  276. if (!(val & EINJ_OP_BUSY))
  277. break;
  278. if (einj_timedout(&timeout))
  279. return -EIO;
  280. }
  281. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
  282. if (rc)
  283. return rc;
  284. val = apei_exec_ctx_get_output(&ctx);
  285. if (val != EINJ_STATUS_SUCCESS)
  286. return -EBUSY;
  287. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
  288. if (rc)
  289. return rc;
  290. trigger_paddr = apei_exec_ctx_get_output(&ctx);
  291. rc = __einj_error_trigger(trigger_paddr);
  292. if (rc)
  293. return rc;
  294. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
  295. return rc;
  296. }
  297. /* Inject the specified hardware error */
  298. static int einj_error_inject(u32 type, u64 param1, u64 param2)
  299. {
  300. int rc;
  301. mutex_lock(&einj_mutex);
  302. rc = __einj_error_inject(type, param1, param2);
  303. mutex_unlock(&einj_mutex);
  304. return rc;
  305. }
  306. static u32 error_type;
  307. static u64 error_param1;
  308. static u64 error_param2;
  309. static struct dentry *einj_debug_dir;
  310. static int available_error_type_show(struct seq_file *m, void *v)
  311. {
  312. int rc;
  313. u32 available_error_type = 0;
  314. rc = einj_get_available_error_type(&available_error_type);
  315. if (rc)
  316. return rc;
  317. if (available_error_type & 0x0001)
  318. seq_printf(m, "0x00000001\tProcessor Correctable\n");
  319. if (available_error_type & 0x0002)
  320. seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
  321. if (available_error_type & 0x0004)
  322. seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
  323. if (available_error_type & 0x0008)
  324. seq_printf(m, "0x00000008\tMemory Correctable\n");
  325. if (available_error_type & 0x0010)
  326. seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
  327. if (available_error_type & 0x0020)
  328. seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
  329. if (available_error_type & 0x0040)
  330. seq_printf(m, "0x00000040\tPCI Express Correctable\n");
  331. if (available_error_type & 0x0080)
  332. seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
  333. if (available_error_type & 0x0100)
  334. seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
  335. if (available_error_type & 0x0200)
  336. seq_printf(m, "0x00000200\tPlatform Correctable\n");
  337. if (available_error_type & 0x0400)
  338. seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
  339. if (available_error_type & 0x0800)
  340. seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
  341. return 0;
  342. }
  343. static int available_error_type_open(struct inode *inode, struct file *file)
  344. {
  345. return single_open(file, available_error_type_show, NULL);
  346. }
  347. static const struct file_operations available_error_type_fops = {
  348. .open = available_error_type_open,
  349. .read = seq_read,
  350. .llseek = seq_lseek,
  351. .release = single_release,
  352. };
  353. static int error_type_get(void *data, u64 *val)
  354. {
  355. *val = error_type;
  356. return 0;
  357. }
  358. static int error_type_set(void *data, u64 val)
  359. {
  360. int rc;
  361. u32 available_error_type = 0;
  362. /* Only one error type can be specified */
  363. if (val & (val - 1))
  364. return -EINVAL;
  365. rc = einj_get_available_error_type(&available_error_type);
  366. if (rc)
  367. return rc;
  368. if (!(val & available_error_type))
  369. return -EINVAL;
  370. error_type = val;
  371. return 0;
  372. }
  373. DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
  374. error_type_set, "0x%llx\n");
  375. static int error_inject_set(void *data, u64 val)
  376. {
  377. if (!error_type)
  378. return -EINVAL;
  379. return einj_error_inject(error_type, error_param1, error_param2);
  380. }
  381. DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
  382. error_inject_set, "%llu\n");
  383. static int einj_check_table(struct acpi_table_einj *einj_tab)
  384. {
  385. if ((einj_tab->header_length !=
  386. (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
  387. && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
  388. return -EINVAL;
  389. if (einj_tab->header.length < sizeof(struct acpi_table_einj))
  390. return -EINVAL;
  391. if (einj_tab->entries !=
  392. (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
  393. sizeof(struct acpi_einj_entry))
  394. return -EINVAL;
  395. return 0;
  396. }
  397. static int __init einj_init(void)
  398. {
  399. int rc;
  400. u64 param_paddr;
  401. acpi_status status;
  402. struct dentry *fentry;
  403. struct apei_exec_context ctx;
  404. if (acpi_disabled)
  405. return -ENODEV;
  406. status = acpi_get_table(ACPI_SIG_EINJ, 0,
  407. (struct acpi_table_header **)&einj_tab);
  408. if (status == AE_NOT_FOUND) {
  409. pr_info(EINJ_PFX "Table is not found!\n");
  410. return -ENODEV;
  411. } else if (ACPI_FAILURE(status)) {
  412. const char *msg = acpi_format_exception(status);
  413. pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
  414. return -EINVAL;
  415. }
  416. rc = einj_check_table(einj_tab);
  417. if (rc) {
  418. pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
  419. return -EINVAL;
  420. }
  421. rc = -ENOMEM;
  422. einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
  423. if (!einj_debug_dir)
  424. goto err_cleanup;
  425. fentry = debugfs_create_file("available_error_type", S_IRUSR,
  426. einj_debug_dir, NULL,
  427. &available_error_type_fops);
  428. if (!fentry)
  429. goto err_cleanup;
  430. fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
  431. einj_debug_dir, NULL, &error_type_fops);
  432. if (!fentry)
  433. goto err_cleanup;
  434. fentry = debugfs_create_file("error_inject", S_IWUSR,
  435. einj_debug_dir, NULL, &error_inject_fops);
  436. if (!fentry)
  437. goto err_cleanup;
  438. apei_resources_init(&einj_resources);
  439. einj_exec_ctx_init(&ctx);
  440. rc = apei_exec_collect_resources(&ctx, &einj_resources);
  441. if (rc)
  442. goto err_fini;
  443. rc = apei_resources_request(&einj_resources, "APEI EINJ");
  444. if (rc)
  445. goto err_fini;
  446. rc = apei_exec_pre_map_gars(&ctx);
  447. if (rc)
  448. goto err_release;
  449. if (param_extension) {
  450. param_paddr = einj_get_parameter_address();
  451. if (param_paddr) {
  452. einj_param = ioremap(param_paddr, sizeof(*einj_param));
  453. rc = -ENOMEM;
  454. if (!einj_param)
  455. goto err_unmap;
  456. fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
  457. einj_debug_dir, &error_param1);
  458. if (!fentry)
  459. goto err_unmap;
  460. fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
  461. einj_debug_dir, &error_param2);
  462. if (!fentry)
  463. goto err_unmap;
  464. } else
  465. pr_warn(EINJ_PFX "Parameter extension is not supported.\n");
  466. }
  467. pr_info(EINJ_PFX "Error INJection is initialized.\n");
  468. return 0;
  469. err_unmap:
  470. if (einj_param)
  471. iounmap(einj_param);
  472. apei_exec_post_unmap_gars(&ctx);
  473. err_release:
  474. apei_resources_release(&einj_resources);
  475. err_fini:
  476. apei_resources_fini(&einj_resources);
  477. err_cleanup:
  478. debugfs_remove_recursive(einj_debug_dir);
  479. return rc;
  480. }
  481. static void __exit einj_exit(void)
  482. {
  483. struct apei_exec_context ctx;
  484. if (einj_param)
  485. iounmap(einj_param);
  486. einj_exec_ctx_init(&ctx);
  487. apei_exec_post_unmap_gars(&ctx);
  488. apei_resources_release(&einj_resources);
  489. apei_resources_fini(&einj_resources);
  490. debugfs_remove_recursive(einj_debug_dir);
  491. }
  492. module_init(einj_init);
  493. module_exit(einj_exit);
  494. MODULE_AUTHOR("Huang Ying");
  495. MODULE_DESCRIPTION("APEI Error INJection support");
  496. MODULE_LICENSE("GPL");