/drivers/acpi/apei/einj.c

http://github.com/mirrors/linux · C · 790 lines · 633 code · 96 blank · 61 comment · 118 complexity · af2426b78184d67cf60de0efef410775 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * APEI Error INJection support
  4. *
  5. * EINJ provides a hardware error injection mechanism, this is useful
  6. * for debugging and testing of other APEI and RAS features.
  7. *
  8. * For more information about EINJ, please refer to ACPI Specification
  9. * version 4.0, section 17.5.
  10. *
  11. * Copyright 2009-2010 Intel Corp.
  12. * Author: Huang Ying <ying.huang@intel.com>
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/nmi.h>
  21. #include <linux/delay.h>
  22. #include <linux/mm.h>
  23. #include <asm/unaligned.h>
  24. #include "apei-internal.h"
  25. #undef pr_fmt
  26. #define pr_fmt(fmt) "EINJ: " fmt
  27. #define SPIN_UNIT 100 /* 100ns */
  28. /* Firmware should respond within 1 milliseconds */
  29. #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
  30. #define ACPI5_VENDOR_BIT BIT(31)
  31. #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
  32. ACPI_EINJ_MEMORY_UNCORRECTABLE | \
  33. ACPI_EINJ_MEMORY_FATAL)
  34. /*
  35. * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
  36. */
  37. static int acpi5;
  38. struct set_error_type_with_address {
  39. u32 type;
  40. u32 vendor_extension;
  41. u32 flags;
  42. u32 apicid;
  43. u64 memory_address;
  44. u64 memory_address_range;
  45. u32 pcie_sbdf;
  46. };
  47. enum {
  48. SETWA_FLAGS_APICID = 1,
  49. SETWA_FLAGS_MEM = 2,
  50. SETWA_FLAGS_PCIE_SBDF = 4,
  51. };
  52. /*
  53. * Vendor extensions for platform specific operations
  54. */
  55. struct vendor_error_type_extension {
  56. u32 length;
  57. u32 pcie_sbdf;
  58. u16 vendor_id;
  59. u16 device_id;
  60. u8 rev_id;
  61. u8 reserved[3];
  62. };
  63. static u32 notrigger;
  64. static u32 vendor_flags;
  65. static struct debugfs_blob_wrapper vendor_blob;
  66. static char vendor_dev[64];
  67. /*
  68. * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
  69. * EINJ table through an unpublished extension. Use with caution as
  70. * most will ignore the parameter and make their own choice of address
  71. * for error injection. This extension is used only if
  72. * param_extension module parameter is specified.
  73. */
  74. struct einj_parameter {
  75. u64 type;
  76. u64 reserved1;
  77. u64 reserved2;
  78. u64 param1;
  79. u64 param2;
  80. };
  81. #define EINJ_OP_BUSY 0x1
  82. #define EINJ_STATUS_SUCCESS 0x0
  83. #define EINJ_STATUS_FAIL 0x1
  84. #define EINJ_STATUS_INVAL 0x2
  85. #define EINJ_TAB_ENTRY(tab) \
  86. ((struct acpi_whea_header *)((char *)(tab) + \
  87. sizeof(struct acpi_table_einj)))
  88. static bool param_extension;
  89. module_param(param_extension, bool, 0);
  90. static struct acpi_table_einj *einj_tab;
  91. static struct apei_resources einj_resources;
  92. static struct apei_exec_ins_type einj_ins_type[] = {
  93. [ACPI_EINJ_READ_REGISTER] = {
  94. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  95. .run = apei_exec_read_register,
  96. },
  97. [ACPI_EINJ_READ_REGISTER_VALUE] = {
  98. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  99. .run = apei_exec_read_register_value,
  100. },
  101. [ACPI_EINJ_WRITE_REGISTER] = {
  102. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  103. .run = apei_exec_write_register,
  104. },
  105. [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
  106. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  107. .run = apei_exec_write_register_value,
  108. },
  109. [ACPI_EINJ_NOOP] = {
  110. .flags = 0,
  111. .run = apei_exec_noop,
  112. },
  113. };
  114. /*
  115. * Prevent EINJ interpreter to run simultaneously, because the
  116. * corresponding firmware implementation may not work properly when
  117. * invoked simultaneously.
  118. */
  119. static DEFINE_MUTEX(einj_mutex);
  120. static void *einj_param;
  121. static void einj_exec_ctx_init(struct apei_exec_context *ctx)
  122. {
  123. apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
  124. EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
  125. }
  126. static int __einj_get_available_error_type(u32 *type)
  127. {
  128. struct apei_exec_context ctx;
  129. int rc;
  130. einj_exec_ctx_init(&ctx);
  131. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
  132. if (rc)
  133. return rc;
  134. *type = apei_exec_ctx_get_output(&ctx);
  135. return 0;
  136. }
  137. /* Get error injection capabilities of the platform */
  138. static int einj_get_available_error_type(u32 *type)
  139. {
  140. int rc;
  141. mutex_lock(&einj_mutex);
  142. rc = __einj_get_available_error_type(type);
  143. mutex_unlock(&einj_mutex);
  144. return rc;
  145. }
  146. static int einj_timedout(u64 *t)
  147. {
  148. if ((s64)*t < SPIN_UNIT) {
  149. pr_warn(FW_WARN "Firmware does not respond in time\n");
  150. return 1;
  151. }
  152. *t -= SPIN_UNIT;
  153. ndelay(SPIN_UNIT);
  154. touch_nmi_watchdog();
  155. return 0;
  156. }
  157. static void check_vendor_extension(u64 paddr,
  158. struct set_error_type_with_address *v5param)
  159. {
  160. int offset = v5param->vendor_extension;
  161. struct vendor_error_type_extension *v;
  162. u32 sbdf;
  163. if (!offset)
  164. return;
  165. v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
  166. if (!v)
  167. return;
  168. sbdf = v->pcie_sbdf;
  169. sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
  170. sbdf >> 24, (sbdf >> 16) & 0xff,
  171. (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
  172. v->vendor_id, v->device_id, v->rev_id);
  173. acpi_os_unmap_iomem(v, sizeof(*v));
  174. }
  175. static void *einj_get_parameter_address(void)
  176. {
  177. int i;
  178. u64 pa_v4 = 0, pa_v5 = 0;
  179. struct acpi_whea_header *entry;
  180. entry = EINJ_TAB_ENTRY(einj_tab);
  181. for (i = 0; i < einj_tab->entries; i++) {
  182. if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
  183. entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
  184. entry->register_region.space_id ==
  185. ACPI_ADR_SPACE_SYSTEM_MEMORY)
  186. pa_v4 = get_unaligned(&entry->register_region.address);
  187. if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
  188. entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
  189. entry->register_region.space_id ==
  190. ACPI_ADR_SPACE_SYSTEM_MEMORY)
  191. pa_v5 = get_unaligned(&entry->register_region.address);
  192. entry++;
  193. }
  194. if (pa_v5) {
  195. struct set_error_type_with_address *v5param;
  196. v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
  197. if (v5param) {
  198. acpi5 = 1;
  199. check_vendor_extension(pa_v5, v5param);
  200. return v5param;
  201. }
  202. }
  203. if (param_extension && pa_v4) {
  204. struct einj_parameter *v4param;
  205. v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
  206. if (!v4param)
  207. return NULL;
  208. if (v4param->reserved1 || v4param->reserved2) {
  209. acpi_os_unmap_iomem(v4param, sizeof(*v4param));
  210. return NULL;
  211. }
  212. return v4param;
  213. }
  214. return NULL;
  215. }
  216. /* do sanity check to trigger table */
  217. static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
  218. {
  219. if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
  220. return -EINVAL;
  221. if (trigger_tab->table_size > PAGE_SIZE ||
  222. trigger_tab->table_size < trigger_tab->header_size)
  223. return -EINVAL;
  224. if (trigger_tab->entry_count !=
  225. (trigger_tab->table_size - trigger_tab->header_size) /
  226. sizeof(struct acpi_einj_entry))
  227. return -EINVAL;
  228. return 0;
  229. }
  230. static struct acpi_generic_address *einj_get_trigger_parameter_region(
  231. struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
  232. {
  233. int i;
  234. struct acpi_whea_header *entry;
  235. entry = (struct acpi_whea_header *)
  236. ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
  237. for (i = 0; i < trigger_tab->entry_count; i++) {
  238. if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
  239. entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE &&
  240. entry->register_region.space_id ==
  241. ACPI_ADR_SPACE_SYSTEM_MEMORY &&
  242. (entry->register_region.address & param2) == (param1 & param2))
  243. return &entry->register_region;
  244. entry++;
  245. }
  246. return NULL;
  247. }
  248. /* Execute instructions in trigger error action table */
  249. static int __einj_error_trigger(u64 trigger_paddr, u32 type,
  250. u64 param1, u64 param2)
  251. {
  252. struct acpi_einj_trigger *trigger_tab = NULL;
  253. struct apei_exec_context trigger_ctx;
  254. struct apei_resources trigger_resources;
  255. struct acpi_whea_header *trigger_entry;
  256. struct resource *r;
  257. u32 table_size;
  258. int rc = -EIO;
  259. struct acpi_generic_address *trigger_param_region = NULL;
  260. r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
  261. "APEI EINJ Trigger Table");
  262. if (!r) {
  263. pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
  264. (unsigned long long)trigger_paddr,
  265. (unsigned long long)trigger_paddr +
  266. sizeof(*trigger_tab) - 1);
  267. goto out;
  268. }
  269. trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
  270. if (!trigger_tab) {
  271. pr_err("Failed to map trigger table!\n");
  272. goto out_rel_header;
  273. }
  274. rc = einj_check_trigger_header(trigger_tab);
  275. if (rc) {
  276. pr_warn(FW_BUG "Invalid trigger error action table.\n");
  277. goto out_rel_header;
  278. }
  279. /* No action structures in the TRIGGER_ERROR table, nothing to do */
  280. if (!trigger_tab->entry_count)
  281. goto out_rel_header;
  282. rc = -EIO;
  283. table_size = trigger_tab->table_size;
  284. r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
  285. table_size - sizeof(*trigger_tab),
  286. "APEI EINJ Trigger Table");
  287. if (!r) {
  288. pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
  289. (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
  290. (unsigned long long)trigger_paddr + table_size - 1);
  291. goto out_rel_header;
  292. }
  293. iounmap(trigger_tab);
  294. trigger_tab = ioremap_cache(trigger_paddr, table_size);
  295. if (!trigger_tab) {
  296. pr_err("Failed to map trigger table!\n");
  297. goto out_rel_entry;
  298. }
  299. trigger_entry = (struct acpi_whea_header *)
  300. ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
  301. apei_resources_init(&trigger_resources);
  302. apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
  303. ARRAY_SIZE(einj_ins_type),
  304. trigger_entry, trigger_tab->entry_count);
  305. rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
  306. if (rc)
  307. goto out_fini;
  308. rc = apei_resources_sub(&trigger_resources, &einj_resources);
  309. if (rc)
  310. goto out_fini;
  311. /*
  312. * Some firmware will access target address specified in
  313. * param1 to trigger the error when injecting memory error.
  314. * This will cause resource conflict with regular memory. So
  315. * remove it from trigger table resources.
  316. */
  317. if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
  318. struct apei_resources addr_resources;
  319. apei_resources_init(&addr_resources);
  320. trigger_param_region = einj_get_trigger_parameter_region(
  321. trigger_tab, param1, param2);
  322. if (trigger_param_region) {
  323. rc = apei_resources_add(&addr_resources,
  324. trigger_param_region->address,
  325. trigger_param_region->bit_width/8, true);
  326. if (rc)
  327. goto out_fini;
  328. rc = apei_resources_sub(&trigger_resources,
  329. &addr_resources);
  330. }
  331. apei_resources_fini(&addr_resources);
  332. if (rc)
  333. goto out_fini;
  334. }
  335. rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
  336. if (rc)
  337. goto out_fini;
  338. rc = apei_exec_pre_map_gars(&trigger_ctx);
  339. if (rc)
  340. goto out_release;
  341. rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
  342. apei_exec_post_unmap_gars(&trigger_ctx);
  343. out_release:
  344. apei_resources_release(&trigger_resources);
  345. out_fini:
  346. apei_resources_fini(&trigger_resources);
  347. out_rel_entry:
  348. release_mem_region(trigger_paddr + sizeof(*trigger_tab),
  349. table_size - sizeof(*trigger_tab));
  350. out_rel_header:
  351. release_mem_region(trigger_paddr, sizeof(*trigger_tab));
  352. out:
  353. if (trigger_tab)
  354. iounmap(trigger_tab);
  355. return rc;
  356. }
  357. static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
  358. u64 param3, u64 param4)
  359. {
  360. struct apei_exec_context ctx;
  361. u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
  362. int rc;
  363. einj_exec_ctx_init(&ctx);
  364. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
  365. if (rc)
  366. return rc;
  367. apei_exec_ctx_set_input(&ctx, type);
  368. if (acpi5) {
  369. struct set_error_type_with_address *v5param = einj_param;
  370. v5param->type = type;
  371. if (type & ACPI5_VENDOR_BIT) {
  372. switch (vendor_flags) {
  373. case SETWA_FLAGS_APICID:
  374. v5param->apicid = param1;
  375. break;
  376. case SETWA_FLAGS_MEM:
  377. v5param->memory_address = param1;
  378. v5param->memory_address_range = param2;
  379. break;
  380. case SETWA_FLAGS_PCIE_SBDF:
  381. v5param->pcie_sbdf = param1;
  382. break;
  383. }
  384. v5param->flags = vendor_flags;
  385. } else if (flags) {
  386. v5param->flags = flags;
  387. v5param->memory_address = param1;
  388. v5param->memory_address_range = param2;
  389. v5param->apicid = param3;
  390. v5param->pcie_sbdf = param4;
  391. } else {
  392. switch (type) {
  393. case ACPI_EINJ_PROCESSOR_CORRECTABLE:
  394. case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
  395. case ACPI_EINJ_PROCESSOR_FATAL:
  396. v5param->apicid = param1;
  397. v5param->flags = SETWA_FLAGS_APICID;
  398. break;
  399. case ACPI_EINJ_MEMORY_CORRECTABLE:
  400. case ACPI_EINJ_MEMORY_UNCORRECTABLE:
  401. case ACPI_EINJ_MEMORY_FATAL:
  402. v5param->memory_address = param1;
  403. v5param->memory_address_range = param2;
  404. v5param->flags = SETWA_FLAGS_MEM;
  405. break;
  406. case ACPI_EINJ_PCIX_CORRECTABLE:
  407. case ACPI_EINJ_PCIX_UNCORRECTABLE:
  408. case ACPI_EINJ_PCIX_FATAL:
  409. v5param->pcie_sbdf = param1;
  410. v5param->flags = SETWA_FLAGS_PCIE_SBDF;
  411. break;
  412. }
  413. }
  414. } else {
  415. rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
  416. if (rc)
  417. return rc;
  418. if (einj_param) {
  419. struct einj_parameter *v4param = einj_param;
  420. v4param->param1 = param1;
  421. v4param->param2 = param2;
  422. }
  423. }
  424. rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
  425. if (rc)
  426. return rc;
  427. for (;;) {
  428. rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
  429. if (rc)
  430. return rc;
  431. val = apei_exec_ctx_get_output(&ctx);
  432. if (!(val & EINJ_OP_BUSY))
  433. break;
  434. if (einj_timedout(&timeout))
  435. return -EIO;
  436. }
  437. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
  438. if (rc)
  439. return rc;
  440. val = apei_exec_ctx_get_output(&ctx);
  441. if (val != EINJ_STATUS_SUCCESS)
  442. return -EBUSY;
  443. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
  444. if (rc)
  445. return rc;
  446. trigger_paddr = apei_exec_ctx_get_output(&ctx);
  447. if (notrigger == 0) {
  448. rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
  449. if (rc)
  450. return rc;
  451. }
  452. rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
  453. return rc;
  454. }
  455. /* Inject the specified hardware error */
  456. static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
  457. u64 param3, u64 param4)
  458. {
  459. int rc;
  460. u64 base_addr, size;
  461. /* If user manually set "flags", make sure it is legal */
  462. if (flags && (flags &
  463. ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
  464. return -EINVAL;
  465. /*
  466. * We need extra sanity checks for memory errors.
  467. * Other types leap directly to injection.
  468. */
  469. /* ensure param1/param2 existed */
  470. if (!(param_extension || acpi5))
  471. goto inject;
  472. /* ensure injection is memory related */
  473. if (type & ACPI5_VENDOR_BIT) {
  474. if (vendor_flags != SETWA_FLAGS_MEM)
  475. goto inject;
  476. } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
  477. goto inject;
  478. /*
  479. * Disallow crazy address masks that give BIOS leeway to pick
  480. * injection address almost anywhere. Insist on page or
  481. * better granularity and that target address is normal RAM or
  482. * NVDIMM.
  483. */
  484. base_addr = param1 & param2;
  485. size = ~param2 + 1;
  486. if (((param2 & PAGE_MASK) != PAGE_MASK) ||
  487. ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
  488. != REGION_INTERSECTS) &&
  489. (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
  490. != REGION_INTERSECTS)))
  491. return -EINVAL;
  492. inject:
  493. mutex_lock(&einj_mutex);
  494. rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
  495. mutex_unlock(&einj_mutex);
  496. return rc;
  497. }
  498. static u32 error_type;
  499. static u32 error_flags;
  500. static u64 error_param1;
  501. static u64 error_param2;
  502. static u64 error_param3;
  503. static u64 error_param4;
  504. static struct dentry *einj_debug_dir;
  505. static int available_error_type_show(struct seq_file *m, void *v)
  506. {
  507. int rc;
  508. u32 available_error_type = 0;
  509. rc = einj_get_available_error_type(&available_error_type);
  510. if (rc)
  511. return rc;
  512. if (available_error_type & 0x0001)
  513. seq_printf(m, "0x00000001\tProcessor Correctable\n");
  514. if (available_error_type & 0x0002)
  515. seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
  516. if (available_error_type & 0x0004)
  517. seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
  518. if (available_error_type & 0x0008)
  519. seq_printf(m, "0x00000008\tMemory Correctable\n");
  520. if (available_error_type & 0x0010)
  521. seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
  522. if (available_error_type & 0x0020)
  523. seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
  524. if (available_error_type & 0x0040)
  525. seq_printf(m, "0x00000040\tPCI Express Correctable\n");
  526. if (available_error_type & 0x0080)
  527. seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
  528. if (available_error_type & 0x0100)
  529. seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
  530. if (available_error_type & 0x0200)
  531. seq_printf(m, "0x00000200\tPlatform Correctable\n");
  532. if (available_error_type & 0x0400)
  533. seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
  534. if (available_error_type & 0x0800)
  535. seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
  536. return 0;
  537. }
  538. DEFINE_SHOW_ATTRIBUTE(available_error_type);
  539. static int error_type_get(void *data, u64 *val)
  540. {
  541. *val = error_type;
  542. return 0;
  543. }
  544. static int error_type_set(void *data, u64 val)
  545. {
  546. int rc;
  547. u32 available_error_type = 0;
  548. u32 tval, vendor;
  549. /*
  550. * Vendor defined types have 0x80000000 bit set, and
  551. * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
  552. */
  553. vendor = val & ACPI5_VENDOR_BIT;
  554. tval = val & 0x7fffffff;
  555. /* Only one error type can be specified */
  556. if (tval & (tval - 1))
  557. return -EINVAL;
  558. if (!vendor) {
  559. rc = einj_get_available_error_type(&available_error_type);
  560. if (rc)
  561. return rc;
  562. if (!(val & available_error_type))
  563. return -EINVAL;
  564. }
  565. error_type = val;
  566. return 0;
  567. }
  568. DEFINE_DEBUGFS_ATTRIBUTE(error_type_fops, error_type_get, error_type_set,
  569. "0x%llx\n");
  570. static int error_inject_set(void *data, u64 val)
  571. {
  572. if (!error_type)
  573. return -EINVAL;
  574. return einj_error_inject(error_type, error_flags, error_param1, error_param2,
  575. error_param3, error_param4);
  576. }
  577. DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
  578. static int einj_check_table(struct acpi_table_einj *einj_tab)
  579. {
  580. if ((einj_tab->header_length !=
  581. (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
  582. && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
  583. return -EINVAL;
  584. if (einj_tab->header.length < sizeof(struct acpi_table_einj))
  585. return -EINVAL;
  586. if (einj_tab->entries !=
  587. (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
  588. sizeof(struct acpi_einj_entry))
  589. return -EINVAL;
  590. return 0;
  591. }
  592. static int __init einj_init(void)
  593. {
  594. int rc;
  595. acpi_status status;
  596. struct apei_exec_context ctx;
  597. if (acpi_disabled) {
  598. pr_warn("ACPI disabled.\n");
  599. return -ENODEV;
  600. }
  601. status = acpi_get_table(ACPI_SIG_EINJ, 0,
  602. (struct acpi_table_header **)&einj_tab);
  603. if (status == AE_NOT_FOUND) {
  604. pr_warn("EINJ table not found.\n");
  605. return -ENODEV;
  606. }
  607. else if (ACPI_FAILURE(status)) {
  608. pr_err("Failed to get EINJ table: %s\n",
  609. acpi_format_exception(status));
  610. return -EINVAL;
  611. }
  612. rc = einj_check_table(einj_tab);
  613. if (rc) {
  614. pr_warn(FW_BUG "Invalid EINJ table.\n");
  615. return -EINVAL;
  616. }
  617. rc = -ENOMEM;
  618. einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
  619. debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
  620. NULL, &available_error_type_fops);
  621. debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
  622. NULL, &error_type_fops);
  623. debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
  624. NULL, &error_inject_fops);
  625. apei_resources_init(&einj_resources);
  626. einj_exec_ctx_init(&ctx);
  627. rc = apei_exec_collect_resources(&ctx, &einj_resources);
  628. if (rc) {
  629. pr_err("Error collecting EINJ resources.\n");
  630. goto err_fini;
  631. }
  632. rc = apei_resources_request(&einj_resources, "APEI EINJ");
  633. if (rc) {
  634. pr_err("Error requesting memory/port resources.\n");
  635. goto err_fini;
  636. }
  637. rc = apei_exec_pre_map_gars(&ctx);
  638. if (rc) {
  639. pr_err("Error pre-mapping GARs.\n");
  640. goto err_release;
  641. }
  642. rc = -ENOMEM;
  643. einj_param = einj_get_parameter_address();
  644. if ((param_extension || acpi5) && einj_param) {
  645. debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
  646. &error_flags);
  647. debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
  648. &error_param1);
  649. debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
  650. &error_param2);
  651. debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
  652. &error_param3);
  653. debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
  654. &error_param4);
  655. debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
  656. einj_debug_dir, &notrigger);
  657. }
  658. if (vendor_dev[0]) {
  659. vendor_blob.data = vendor_dev;
  660. vendor_blob.size = strlen(vendor_dev);
  661. debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
  662. &vendor_blob);
  663. debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
  664. einj_debug_dir, &vendor_flags);
  665. }
  666. pr_info("Error INJection is initialized.\n");
  667. return 0;
  668. err_release:
  669. apei_resources_release(&einj_resources);
  670. err_fini:
  671. apei_resources_fini(&einj_resources);
  672. debugfs_remove_recursive(einj_debug_dir);
  673. return rc;
  674. }
  675. static void __exit einj_exit(void)
  676. {
  677. struct apei_exec_context ctx;
  678. if (einj_param) {
  679. acpi_size size = (acpi5) ?
  680. sizeof(struct set_error_type_with_address) :
  681. sizeof(struct einj_parameter);
  682. acpi_os_unmap_iomem(einj_param, size);
  683. }
  684. einj_exec_ctx_init(&ctx);
  685. apei_exec_post_unmap_gars(&ctx);
  686. apei_resources_release(&einj_resources);
  687. apei_resources_fini(&einj_resources);
  688. debugfs_remove_recursive(einj_debug_dir);
  689. }
  690. module_init(einj_init);
  691. module_exit(einj_exit);
  692. MODULE_AUTHOR("Huang Ying");
  693. MODULE_DESCRIPTION("APEI Error INJection support");
  694. MODULE_LICENSE("GPL");