einj.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 <[email protected]>
  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 SLEEP_UNIT_MIN 1000 /* 1ms */
  28. #define SLEEP_UNIT_MAX 5000 /* 5ms */
  29. /* Firmware should respond within 1 seconds */
  30. #define FIRMWARE_TIMEOUT (1 * USEC_PER_SEC)
  31. #define ACPI5_VENDOR_BIT BIT(31)
  32. #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
  33. ACPI_EINJ_MEMORY_UNCORRECTABLE | \
  34. ACPI_EINJ_MEMORY_FATAL)
  35. /*
  36. * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
  37. */
  38. static int acpi5;
  39. struct set_error_type_with_address {
  40. u32 type;
  41. u32 vendor_extension;
  42. u32 flags;
  43. u32 apicid;
  44. u64 memory_address;
  45. u64 memory_address_range;
  46. u32 pcie_sbdf;
  47. };
  48. enum {
  49. SETWA_FLAGS_APICID = 1,
  50. SETWA_FLAGS_MEM = 2,
  51. SETWA_FLAGS_PCIE_SBDF = 4,
  52. };
  53. /*
  54. * Vendor extensions for platform specific operations
  55. */
  56. struct vendor_error_type_extension {
  57. u32 length;
  58. u32 pcie_sbdf;
  59. u16 vendor_id;
  60. u16 device_id;
  61. u8 rev_id;
  62. u8 reserved[3];
  63. };
  64. static u32 notrigger;
  65. static u32 vendor_flags;
  66. static struct debugfs_blob_wrapper vendor_blob;
  67. static char vendor_dev[64];
  68. /*
  69. * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
  70. * EINJ table through an unpublished extension. Use with caution as
  71. * most will ignore the parameter and make their own choice of address
  72. * for error injection. This extension is used only if
  73. * param_extension module parameter is specified.
  74. */
  75. struct einj_parameter {
  76. u64 type;
  77. u64 reserved1;
  78. u64 reserved2;
  79. u64 param1;
  80. u64 param2;
  81. };
  82. #define EINJ_OP_BUSY 0x1
  83. #define EINJ_STATUS_SUCCESS 0x0
  84. #define EINJ_STATUS_FAIL 0x1
  85. #define EINJ_STATUS_INVAL 0x2
  86. #define EINJ_TAB_ENTRY(tab) \
  87. ((struct acpi_whea_header *)((char *)(tab) + \
  88. sizeof(struct acpi_table_einj)))
  89. static bool param_extension;
  90. module_param(param_extension, bool, 0);
  91. static struct acpi_table_einj *einj_tab;
  92. static struct apei_resources einj_resources;
  93. static struct apei_exec_ins_type einj_ins_type[] = {
  94. [ACPI_EINJ_READ_REGISTER] = {
  95. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  96. .run = apei_exec_read_register,
  97. },
  98. [ACPI_EINJ_READ_REGISTER_VALUE] = {
  99. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  100. .run = apei_exec_read_register_value,
  101. },
  102. [ACPI_EINJ_WRITE_REGISTER] = {
  103. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  104. .run = apei_exec_write_register,
  105. },
  106. [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
  107. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  108. .run = apei_exec_write_register_value,
  109. },
  110. [ACPI_EINJ_NOOP] = {
  111. .flags = 0,
  112. .run = apei_exec_noop,
  113. },
  114. };
  115. /*
  116. * Prevent EINJ interpreter to run simultaneously, because the
  117. * corresponding firmware implementation may not work properly when
  118. * invoked simultaneously.
  119. */
  120. static DEFINE_MUTEX(einj_mutex);
  121. static void *einj_param;
  122. static void einj_exec_ctx_init(struct apei_exec_context *ctx)
  123. {
  124. apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
  125. EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
  126. }
  127. static int __einj_get_available_error_type(u32 *type)
  128. {
  129. struct apei_exec_context ctx;
  130. int rc;
  131. einj_exec_ctx_init(&ctx);
  132. rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
  133. if (rc)
  134. return rc;
  135. *type = apei_exec_ctx_get_output(&ctx);
  136. return 0;
  137. }
  138. /* Get error injection capabilities of the platform */
  139. static int einj_get_available_error_type(u32 *type)
  140. {
  141. int rc;
  142. mutex_lock(&einj_mutex);
  143. rc = __einj_get_available_error_type(type);
  144. mutex_unlock(&einj_mutex);
  145. return rc;
  146. }
  147. static int einj_timedout(u64 *t)
  148. {
  149. if ((s64)*t < SLEEP_UNIT_MIN) {
  150. pr_warn(FW_WARN "Firmware does not respond in time\n");
  151. return 1;
  152. }
  153. *t -= SLEEP_UNIT_MIN;
  154. usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX);
  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. (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED)
  492. != REGION_INTERSECTS) &&
  493. !arch_is_platform_page(base_addr)))
  494. return -EINVAL;
  495. if (is_zero_pfn(base_addr >> PAGE_SHIFT))
  496. return -EADDRINUSE;
  497. inject:
  498. mutex_lock(&einj_mutex);
  499. rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
  500. mutex_unlock(&einj_mutex);
  501. return rc;
  502. }
  503. static u32 error_type;
  504. static u32 error_flags;
  505. static u64 error_param1;
  506. static u64 error_param2;
  507. static u64 error_param3;
  508. static u64 error_param4;
  509. static struct dentry *einj_debug_dir;
  510. static int available_error_type_show(struct seq_file *m, void *v)
  511. {
  512. int rc;
  513. u32 available_error_type = 0;
  514. rc = einj_get_available_error_type(&available_error_type);
  515. if (rc)
  516. return rc;
  517. if (available_error_type & 0x0001)
  518. seq_printf(m, "0x00000001\tProcessor Correctable\n");
  519. if (available_error_type & 0x0002)
  520. seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
  521. if (available_error_type & 0x0004)
  522. seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
  523. if (available_error_type & 0x0008)
  524. seq_printf(m, "0x00000008\tMemory Correctable\n");
  525. if (available_error_type & 0x0010)
  526. seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
  527. if (available_error_type & 0x0020)
  528. seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
  529. if (available_error_type & 0x0040)
  530. seq_printf(m, "0x00000040\tPCI Express Correctable\n");
  531. if (available_error_type & 0x0080)
  532. seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
  533. if (available_error_type & 0x0100)
  534. seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
  535. if (available_error_type & 0x0200)
  536. seq_printf(m, "0x00000200\tPlatform Correctable\n");
  537. if (available_error_type & 0x0400)
  538. seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
  539. if (available_error_type & 0x0800)
  540. seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
  541. return 0;
  542. }
  543. DEFINE_SHOW_ATTRIBUTE(available_error_type);
  544. static int error_type_get(void *data, u64 *val)
  545. {
  546. *val = error_type;
  547. return 0;
  548. }
  549. static int error_type_set(void *data, u64 val)
  550. {
  551. int rc;
  552. u32 available_error_type = 0;
  553. u32 tval, vendor;
  554. /*
  555. * Vendor defined types have 0x80000000 bit set, and
  556. * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
  557. */
  558. vendor = val & ACPI5_VENDOR_BIT;
  559. tval = val & 0x7fffffff;
  560. /* Only one error type can be specified */
  561. if (tval & (tval - 1))
  562. return -EINVAL;
  563. if (!vendor) {
  564. rc = einj_get_available_error_type(&available_error_type);
  565. if (rc)
  566. return rc;
  567. if (!(val & available_error_type))
  568. return -EINVAL;
  569. }
  570. error_type = val;
  571. return 0;
  572. }
  573. DEFINE_DEBUGFS_ATTRIBUTE(error_type_fops, error_type_get, error_type_set,
  574. "0x%llx\n");
  575. static int error_inject_set(void *data, u64 val)
  576. {
  577. if (!error_type)
  578. return -EINVAL;
  579. return einj_error_inject(error_type, error_flags, error_param1, error_param2,
  580. error_param3, error_param4);
  581. }
  582. DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
  583. static int einj_check_table(struct acpi_table_einj *einj_tab)
  584. {
  585. if ((einj_tab->header_length !=
  586. (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
  587. && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
  588. return -EINVAL;
  589. if (einj_tab->header.length < sizeof(struct acpi_table_einj))
  590. return -EINVAL;
  591. if (einj_tab->entries !=
  592. (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
  593. sizeof(struct acpi_einj_entry))
  594. return -EINVAL;
  595. return 0;
  596. }
  597. static int __init einj_init(void)
  598. {
  599. int rc;
  600. acpi_status status;
  601. struct apei_exec_context ctx;
  602. if (acpi_disabled) {
  603. pr_info("ACPI disabled.\n");
  604. return -ENODEV;
  605. }
  606. status = acpi_get_table(ACPI_SIG_EINJ, 0,
  607. (struct acpi_table_header **)&einj_tab);
  608. if (status == AE_NOT_FOUND) {
  609. pr_warn("EINJ table not found.\n");
  610. return -ENODEV;
  611. }
  612. else if (ACPI_FAILURE(status)) {
  613. pr_err("Failed to get EINJ table: %s\n",
  614. acpi_format_exception(status));
  615. return -EINVAL;
  616. }
  617. rc = einj_check_table(einj_tab);
  618. if (rc) {
  619. pr_warn(FW_BUG "Invalid EINJ table.\n");
  620. goto err_put_table;
  621. }
  622. rc = -ENOMEM;
  623. einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
  624. debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
  625. NULL, &available_error_type_fops);
  626. debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
  627. NULL, &error_type_fops);
  628. debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
  629. NULL, &error_inject_fops);
  630. apei_resources_init(&einj_resources);
  631. einj_exec_ctx_init(&ctx);
  632. rc = apei_exec_collect_resources(&ctx, &einj_resources);
  633. if (rc) {
  634. pr_err("Error collecting EINJ resources.\n");
  635. goto err_fini;
  636. }
  637. rc = apei_resources_request(&einj_resources, "APEI EINJ");
  638. if (rc) {
  639. pr_err("Error requesting memory/port resources.\n");
  640. goto err_fini;
  641. }
  642. rc = apei_exec_pre_map_gars(&ctx);
  643. if (rc) {
  644. pr_err("Error pre-mapping GARs.\n");
  645. goto err_release;
  646. }
  647. einj_param = einj_get_parameter_address();
  648. if ((param_extension || acpi5) && einj_param) {
  649. debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
  650. &error_flags);
  651. debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
  652. &error_param1);
  653. debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
  654. &error_param2);
  655. debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
  656. &error_param3);
  657. debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
  658. &error_param4);
  659. debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
  660. einj_debug_dir, &notrigger);
  661. }
  662. if (vendor_dev[0]) {
  663. vendor_blob.data = vendor_dev;
  664. vendor_blob.size = strlen(vendor_dev);
  665. debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
  666. &vendor_blob);
  667. debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
  668. einj_debug_dir, &vendor_flags);
  669. }
  670. pr_info("Error INJection is initialized.\n");
  671. return 0;
  672. err_release:
  673. apei_resources_release(&einj_resources);
  674. err_fini:
  675. apei_resources_fini(&einj_resources);
  676. debugfs_remove_recursive(einj_debug_dir);
  677. err_put_table:
  678. acpi_put_table((struct acpi_table_header *)einj_tab);
  679. return rc;
  680. }
  681. static void __exit einj_exit(void)
  682. {
  683. struct apei_exec_context ctx;
  684. if (einj_param) {
  685. acpi_size size = (acpi5) ?
  686. sizeof(struct set_error_type_with_address) :
  687. sizeof(struct einj_parameter);
  688. acpi_os_unmap_iomem(einj_param, size);
  689. }
  690. einj_exec_ctx_init(&ctx);
  691. apei_exec_post_unmap_gars(&ctx);
  692. apei_resources_release(&einj_resources);
  693. apei_resources_fini(&einj_resources);
  694. debugfs_remove_recursive(einj_debug_dir);
  695. acpi_put_table((struct acpi_table_header *)einj_tab);
  696. }
  697. module_init(einj_init);
  698. module_exit(einj_exit);
  699. MODULE_AUTHOR("Huang Ying");
  700. MODULE_DESCRIPTION("APEI Error INJection support");
  701. MODULE_LICENSE("GPL");