einj.c 15 KB

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