einj.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 [mem %#010llx-%#010llx] for Trigger table\n",
  184. (unsigned long long)trigger_paddr,
  185. (unsigned long long)trigger_paddr +
  186. sizeof(*trigger_tab) - 1);
  187. goto out;
  188. }
  189. trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
  190. if (!trigger_tab) {
  191. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  192. goto out_rel_header;
  193. }
  194. rc = einj_check_trigger_header(trigger_tab);
  195. if (rc) {
  196. pr_warning(FW_BUG EINJ_PFX
  197. "The trigger error action table is invalid\n");
  198. goto out_rel_header;
  199. }
  200. rc = -EIO;
  201. table_size = trigger_tab->table_size;
  202. r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
  203. table_size - sizeof(*trigger_tab),
  204. "APEI EINJ Trigger Table");
  205. if (!r) {
  206. pr_err(EINJ_PFX
  207. "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
  208. (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
  209. (unsigned long long)trigger_paddr + table_size - 1);
  210. goto out_rel_header;
  211. }
  212. iounmap(trigger_tab);
  213. trigger_tab = ioremap_cache(trigger_paddr, table_size);
  214. if (!trigger_tab) {
  215. pr_err(EINJ_PFX "Failed to map trigger table!\n");
  216. goto out_rel_entry;
  217. }
  218. trigger_entry = (struct acpi_whea_header *)
  219. ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
  220. apei_resources_init(&trigger_resources);
  221. apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
  222. ARRAY_SIZE(einj_ins_type),
  223. trigger_entry, trigger_tab->entry_count);
  224. rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
  225. if (rc)
  226. goto out_fini;
  227. rc = apei_resources_sub(&trigger_resources, &einj_resources);
  228. if (rc)
  229. goto out_fini;
  230. rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
  231. if (rc)
  232. goto out_fini;
  233. rc = apei_exec_pre_map_gars(&trigger_ctx);
  234. if (rc)
  235. goto out_release;
  236. rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
  237. apei_exec_post_unmap_gars(&trigger_ctx);
  238. out_release:
  239. apei_resources_release(&trigger_resources);
  240. out_fini:
  241. apei_resources_fini(&trigger_resources);
  242. out_rel_entry:
  243. release_mem_region(trigger_paddr + sizeof(*trigger_tab),
  244. table_size - sizeof(*trigger_tab));
  245. out_rel_header:
  246. release_mem_region(trigger_paddr, sizeof(*trigger_tab));
  247. out:
  248. if (trigger_tab)
  249. iounmap(trigger_tab);
  250. return rc;
  251. }
  252. static int __einj_error_inject(u32 type, u64 param1, u64 param2)
  253. {
  254. struct apei_exec_context ctx;
  255. u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
  256. int rc;
  257. einj_exec_ctx_init(&ctx);
  258. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
  259. if (rc)
  260. return rc;
  261. apei_exec_ctx_set_input(&ctx, type);
  262. rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
  263. if (rc)
  264. return rc;
  265. if (einj_param) {
  266. writeq(param1, &einj_param->param1);
  267. writeq(param2, &einj_param->param2);
  268. }
  269. rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
  270. if (rc)
  271. return rc;
  272. for (;;) {
  273. rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
  274. if (rc)
  275. return rc;
  276. val = apei_exec_ctx_get_output(&ctx);
  277. if (!(val & EINJ_OP_BUSY))
  278. break;
  279. if (einj_timedout(&timeout))
  280. return -EIO;
  281. }
  282. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
  283. if (rc)
  284. return rc;
  285. val = apei_exec_ctx_get_output(&ctx);
  286. if (val != EINJ_STATUS_SUCCESS)
  287. return -EBUSY;
  288. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
  289. if (rc)
  290. return rc;
  291. trigger_paddr = apei_exec_ctx_get_output(&ctx);
  292. rc = __einj_error_trigger(trigger_paddr);
  293. if (rc)
  294. return rc;
  295. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
  296. return rc;
  297. }
  298. /* Inject the specified hardware error */
  299. static int einj_error_inject(u32 type, u64 param1, u64 param2)
  300. {
  301. int rc;
  302. mutex_lock(&einj_mutex);
  303. rc = __einj_error_inject(type, param1, param2);
  304. mutex_unlock(&einj_mutex);
  305. return rc;
  306. }
  307. static u32 error_type;
  308. static u64 error_param1;
  309. static u64 error_param2;
  310. static struct dentry *einj_debug_dir;
  311. static int available_error_type_show(struct seq_file *m, void *v)
  312. {
  313. int rc;
  314. u32 available_error_type = 0;
  315. rc = einj_get_available_error_type(&available_error_type);
  316. if (rc)
  317. return rc;
  318. if (available_error_type & 0x0001)
  319. seq_printf(m, "0x00000001\tProcessor Correctable\n");
  320. if (available_error_type & 0x0002)
  321. seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
  322. if (available_error_type & 0x0004)
  323. seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
  324. if (available_error_type & 0x0008)
  325. seq_printf(m, "0x00000008\tMemory Correctable\n");
  326. if (available_error_type & 0x0010)
  327. seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
  328. if (available_error_type & 0x0020)
  329. seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
  330. if (available_error_type & 0x0040)
  331. seq_printf(m, "0x00000040\tPCI Express Correctable\n");
  332. if (available_error_type & 0x0080)
  333. seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
  334. if (available_error_type & 0x0100)
  335. seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
  336. if (available_error_type & 0x0200)
  337. seq_printf(m, "0x00000200\tPlatform Correctable\n");
  338. if (available_error_type & 0x0400)
  339. seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
  340. if (available_error_type & 0x0800)
  341. seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
  342. return 0;
  343. }
  344. static int available_error_type_open(struct inode *inode, struct file *file)
  345. {
  346. return single_open(file, available_error_type_show, NULL);
  347. }
  348. static const struct file_operations available_error_type_fops = {
  349. .open = available_error_type_open,
  350. .read = seq_read,
  351. .llseek = seq_lseek,
  352. .release = single_release,
  353. };
  354. static int error_type_get(void *data, u64 *val)
  355. {
  356. *val = error_type;
  357. return 0;
  358. }
  359. static int error_type_set(void *data, u64 val)
  360. {
  361. int rc;
  362. u32 available_error_type = 0;
  363. /* Only one error type can be specified */
  364. if (val & (val - 1))
  365. return -EINVAL;
  366. rc = einj_get_available_error_type(&available_error_type);
  367. if (rc)
  368. return rc;
  369. if (!(val & available_error_type))
  370. return -EINVAL;
  371. error_type = val;
  372. return 0;
  373. }
  374. DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
  375. error_type_set, "0x%llx\n");
  376. static int error_inject_set(void *data, u64 val)
  377. {
  378. if (!error_type)
  379. return -EINVAL;
  380. return einj_error_inject(error_type, error_param1, error_param2);
  381. }
  382. DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
  383. error_inject_set, "%llu\n");
  384. static int einj_check_table(struct acpi_table_einj *einj_tab)
  385. {
  386. if ((einj_tab->header_length !=
  387. (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
  388. && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
  389. return -EINVAL;
  390. if (einj_tab->header.length < sizeof(struct acpi_table_einj))
  391. return -EINVAL;
  392. if (einj_tab->entries !=
  393. (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
  394. sizeof(struct acpi_einj_entry))
  395. return -EINVAL;
  396. return 0;
  397. }
  398. static int __init einj_init(void)
  399. {
  400. int rc;
  401. u64 param_paddr;
  402. acpi_status status;
  403. struct dentry *fentry;
  404. struct apei_exec_context ctx;
  405. if (acpi_disabled)
  406. return -ENODEV;
  407. status = acpi_get_table(ACPI_SIG_EINJ, 0,
  408. (struct acpi_table_header **)&einj_tab);
  409. if (status == AE_NOT_FOUND) {
  410. pr_info(EINJ_PFX "Table is not found!\n");
  411. return -ENODEV;
  412. } else if (ACPI_FAILURE(status)) {
  413. const char *msg = acpi_format_exception(status);
  414. pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
  415. return -EINVAL;
  416. }
  417. rc = einj_check_table(einj_tab);
  418. if (rc) {
  419. pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
  420. return -EINVAL;
  421. }
  422. rc = -ENOMEM;
  423. einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
  424. if (!einj_debug_dir)
  425. goto err_cleanup;
  426. fentry = debugfs_create_file("available_error_type", S_IRUSR,
  427. einj_debug_dir, NULL,
  428. &available_error_type_fops);
  429. if (!fentry)
  430. goto err_cleanup;
  431. fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
  432. einj_debug_dir, NULL, &error_type_fops);
  433. if (!fentry)
  434. goto err_cleanup;
  435. fentry = debugfs_create_file("error_inject", S_IWUSR,
  436. einj_debug_dir, NULL, &error_inject_fops);
  437. if (!fentry)
  438. goto err_cleanup;
  439. apei_resources_init(&einj_resources);
  440. einj_exec_ctx_init(&ctx);
  441. rc = apei_exec_collect_resources(&ctx, &einj_resources);
  442. if (rc)
  443. goto err_fini;
  444. rc = apei_resources_request(&einj_resources, "APEI EINJ");
  445. if (rc)
  446. goto err_fini;
  447. rc = apei_exec_pre_map_gars(&ctx);
  448. if (rc)
  449. goto err_release;
  450. if (param_extension) {
  451. param_paddr = einj_get_parameter_address();
  452. if (param_paddr) {
  453. einj_param = ioremap(param_paddr, sizeof(*einj_param));
  454. rc = -ENOMEM;
  455. if (!einj_param)
  456. goto err_unmap;
  457. fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
  458. einj_debug_dir, &error_param1);
  459. if (!fentry)
  460. goto err_unmap;
  461. fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
  462. einj_debug_dir, &error_param2);
  463. if (!fentry)
  464. goto err_unmap;
  465. } else
  466. pr_warn(EINJ_PFX "Parameter extension is not supported.\n");
  467. }
  468. pr_info(EINJ_PFX "Error INJection is initialized.\n");
  469. return 0;
  470. err_unmap:
  471. if (einj_param)
  472. iounmap(einj_param);
  473. apei_exec_post_unmap_gars(&ctx);
  474. err_release:
  475. apei_resources_release(&einj_resources);
  476. err_fini:
  477. apei_resources_fini(&einj_resources);
  478. err_cleanup:
  479. debugfs_remove_recursive(einj_debug_dir);
  480. return rc;
  481. }
  482. static void __exit einj_exit(void)
  483. {
  484. struct apei_exec_context ctx;
  485. if (einj_param)
  486. iounmap(einj_param);
  487. einj_exec_ctx_init(&ctx);
  488. apei_exec_post_unmap_gars(&ctx);
  489. apei_resources_release(&einj_resources);
  490. apei_resources_fini(&einj_resources);
  491. debugfs_remove_recursive(einj_debug_dir);
  492. }
  493. module_init(einj_init);
  494. module_exit(einj_exit);
  495. MODULE_AUTHOR("Huang Ying");
  496. MODULE_DESCRIPTION("APEI Error INJection support");
  497. MODULE_LICENSE("GPL");