einj.c 20 KB

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