einj.c 21 KB

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