einj.c 22 KB

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