einj.c 20 KB

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