einj.c 15 KB

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