1 // SPDX-License-Identifier: GPL-2.0-only
3 * APEI Error INJection support
5 * EINJ provides a hardware error injection mechanism, this is useful
6 * for debugging and testing of other APEI and RAS features.
8 * For more information about EINJ, please refer to ACPI Specification
9 * version 4.0, section 17.5.
11 * Copyright 2009-2010 Intel Corp.
12 * Author: Huang Ying <ying.huang@intel.com>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/nmi.h>
22 #include <linux/delay.h>
24 #include <asm/unaligned.h>
26 #include "apei-internal.h"
29 #define pr_fmt(fmt) "EINJ: " fmt
31 #define SLEEP_UNIT_MIN 1000 /* 1ms */
32 #define SLEEP_UNIT_MAX 5000 /* 5ms */
33 /* Firmware should respond within 1 seconds */
34 #define FIRMWARE_TIMEOUT (1 * USEC_PER_SEC)
35 #define ACPI5_VENDOR_BIT BIT(31)
36 #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
37 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
38 ACPI_EINJ_MEMORY_FATAL)
41 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
45 struct set_error_type_with_address {
51 u64 memory_address_range;
55 SETWA_FLAGS_APICID = 1,
57 SETWA_FLAGS_PCIE_SBDF = 4,
61 * Vendor extensions for platform specific operations
63 struct vendor_error_type_extension {
74 static u32 vendor_flags;
75 static struct debugfs_blob_wrapper vendor_blob;
76 static char vendor_dev[64];
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.
85 struct einj_parameter {
93 #define EINJ_OP_BUSY 0x1
94 #define EINJ_STATUS_SUCCESS 0x0
95 #define EINJ_STATUS_FAIL 0x1
96 #define EINJ_STATUS_INVAL 0x2
98 #define EINJ_TAB_ENTRY(tab) \
99 ((struct acpi_whea_header *)((char *)(tab) + \
100 sizeof(struct acpi_table_einj)))
102 static bool param_extension;
103 module_param(param_extension, bool, 0);
105 static struct acpi_table_einj *einj_tab;
107 static struct apei_resources einj_resources;
109 static struct apei_exec_ins_type einj_ins_type[] = {
110 [ACPI_EINJ_READ_REGISTER] = {
111 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
112 .run = apei_exec_read_register,
114 [ACPI_EINJ_READ_REGISTER_VALUE] = {
115 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
116 .run = apei_exec_read_register_value,
118 [ACPI_EINJ_WRITE_REGISTER] = {
119 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
120 .run = apei_exec_write_register,
122 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
123 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
124 .run = apei_exec_write_register_value,
128 .run = apei_exec_noop,
133 * Prevent EINJ interpreter to run simultaneously, because the
134 * corresponding firmware implementation may not work properly when
135 * invoked simultaneously.
137 static DEFINE_MUTEX(einj_mutex);
139 static void *einj_param;
141 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
143 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
144 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
147 static int __einj_get_available_error_type(u32 *type)
149 struct apei_exec_context ctx;
152 einj_exec_ctx_init(&ctx);
153 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
156 *type = apei_exec_ctx_get_output(&ctx);
161 /* Get error injection capabilities of the platform */
162 static int einj_get_available_error_type(u32 *type)
166 mutex_lock(&einj_mutex);
167 rc = __einj_get_available_error_type(type);
168 mutex_unlock(&einj_mutex);
173 static int einj_timedout(u64 *t)
175 if ((s64)*t < SLEEP_UNIT_MIN) {
176 pr_warn(FW_WARN "Firmware does not respond in time\n");
179 *t -= SLEEP_UNIT_MIN;
180 usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX);
185 static void check_vendor_extension(u64 paddr,
186 struct set_error_type_with_address *v5param)
188 int offset = v5param->vendor_extension;
189 struct vendor_error_type_extension *v;
194 v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
198 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
199 sbdf >> 24, (sbdf >> 16) & 0xff,
200 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
201 v->vendor_id, v->device_id, v->rev_id);
202 acpi_os_unmap_iomem(v, sizeof(*v));
205 static void *einj_get_parameter_address(void)
208 u64 pa_v4 = 0, pa_v5 = 0;
209 struct acpi_whea_header *entry;
211 entry = EINJ_TAB_ENTRY(einj_tab);
212 for (i = 0; i < einj_tab->entries; i++) {
213 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
214 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
215 entry->register_region.space_id ==
216 ACPI_ADR_SPACE_SYSTEM_MEMORY)
217 pa_v4 = get_unaligned(&entry->register_region.address);
218 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
219 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
220 entry->register_region.space_id ==
221 ACPI_ADR_SPACE_SYSTEM_MEMORY)
222 pa_v5 = get_unaligned(&entry->register_region.address);
226 struct set_error_type_with_address *v5param;
228 v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
231 check_vendor_extension(pa_v5, v5param);
235 if (param_extension && pa_v4) {
236 struct einj_parameter *v4param;
238 v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
241 if (v4param->reserved1 || v4param->reserved2) {
242 acpi_os_unmap_iomem(v4param, sizeof(*v4param));
251 /* do sanity check to trigger table */
252 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
254 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
256 if (trigger_tab->table_size > PAGE_SIZE ||
257 trigger_tab->table_size < trigger_tab->header_size)
259 if (trigger_tab->entry_count !=
260 (trigger_tab->table_size - trigger_tab->header_size) /
261 sizeof(struct acpi_einj_entry))
267 static struct acpi_generic_address *einj_get_trigger_parameter_region(
268 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
271 struct acpi_whea_header *entry;
273 entry = (struct acpi_whea_header *)
274 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
275 for (i = 0; i < trigger_tab->entry_count; i++) {
276 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
277 entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE &&
278 entry->register_region.space_id ==
279 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
280 (entry->register_region.address & param2) == (param1 & param2))
281 return &entry->register_region;
287 /* Execute instructions in trigger error action table */
288 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
289 u64 param1, u64 param2)
291 struct acpi_einj_trigger *trigger_tab = NULL;
292 struct apei_exec_context trigger_ctx;
293 struct apei_resources trigger_resources;
294 struct acpi_whea_header *trigger_entry;
298 struct acpi_generic_address *trigger_param_region = NULL;
300 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
301 "APEI EINJ Trigger Table");
303 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
304 (unsigned long long)trigger_paddr,
305 (unsigned long long)trigger_paddr +
306 sizeof(*trigger_tab) - 1);
309 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
311 pr_err("Failed to map trigger table!\n");
314 rc = einj_check_trigger_header(trigger_tab);
316 pr_warn(FW_BUG "Invalid trigger error action table.\n");
320 /* No action structures in the TRIGGER_ERROR table, nothing to do */
321 if (!trigger_tab->entry_count)
325 table_size = trigger_tab->table_size;
326 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
327 table_size - sizeof(*trigger_tab),
328 "APEI EINJ Trigger Table");
330 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
331 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
332 (unsigned long long)trigger_paddr + table_size - 1);
335 iounmap(trigger_tab);
336 trigger_tab = ioremap_cache(trigger_paddr, table_size);
338 pr_err("Failed to map trigger table!\n");
341 trigger_entry = (struct acpi_whea_header *)
342 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
343 apei_resources_init(&trigger_resources);
344 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
345 ARRAY_SIZE(einj_ins_type),
346 trigger_entry, trigger_tab->entry_count);
347 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
350 rc = apei_resources_sub(&trigger_resources, &einj_resources);
354 * Some firmware will access target address specified in
355 * param1 to trigger the error when injecting memory error.
356 * This will cause resource conflict with regular memory. So
357 * remove it from trigger table resources.
359 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
360 struct apei_resources addr_resources;
362 apei_resources_init(&addr_resources);
363 trigger_param_region = einj_get_trigger_parameter_region(
364 trigger_tab, param1, param2);
365 if (trigger_param_region) {
366 rc = apei_resources_add(&addr_resources,
367 trigger_param_region->address,
368 trigger_param_region->bit_width/8, true);
371 rc = apei_resources_sub(&trigger_resources,
374 apei_resources_fini(&addr_resources);
378 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
381 rc = apei_exec_pre_map_gars(&trigger_ctx);
385 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
387 apei_exec_post_unmap_gars(&trigger_ctx);
389 apei_resources_release(&trigger_resources);
391 apei_resources_fini(&trigger_resources);
393 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
394 table_size - sizeof(*trigger_tab));
396 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
399 iounmap(trigger_tab);
404 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
405 u64 param3, u64 param4)
407 struct apei_exec_context ctx;
408 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
411 einj_exec_ctx_init(&ctx);
413 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
416 apei_exec_ctx_set_input(&ctx, type);
418 struct set_error_type_with_address *v5param = einj_param;
420 v5param->type = type;
421 if (type & ACPI5_VENDOR_BIT) {
422 switch (vendor_flags) {
423 case SETWA_FLAGS_APICID:
424 v5param->apicid = param1;
426 case SETWA_FLAGS_MEM:
427 v5param->memory_address = param1;
428 v5param->memory_address_range = param2;
430 case SETWA_FLAGS_PCIE_SBDF:
431 v5param->pcie_sbdf = param1;
434 v5param->flags = vendor_flags;
436 v5param->flags = flags;
437 v5param->memory_address = param1;
438 v5param->memory_address_range = param2;
439 v5param->apicid = param3;
440 v5param->pcie_sbdf = param4;
443 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
444 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
445 case ACPI_EINJ_PROCESSOR_FATAL:
446 v5param->apicid = param1;
447 v5param->flags = SETWA_FLAGS_APICID;
449 case ACPI_EINJ_MEMORY_CORRECTABLE:
450 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
451 case ACPI_EINJ_MEMORY_FATAL:
452 v5param->memory_address = param1;
453 v5param->memory_address_range = param2;
454 v5param->flags = SETWA_FLAGS_MEM;
456 case ACPI_EINJ_PCIX_CORRECTABLE:
457 case ACPI_EINJ_PCIX_UNCORRECTABLE:
458 case ACPI_EINJ_PCIX_FATAL:
459 v5param->pcie_sbdf = param1;
460 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
465 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
469 struct einj_parameter *v4param = einj_param;
471 v4param->param1 = param1;
472 v4param->param2 = param2;
475 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
479 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
482 val = apei_exec_ctx_get_output(&ctx);
483 if (!(val & EINJ_OP_BUSY))
485 if (einj_timedout(&timeout))
488 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
491 val = apei_exec_ctx_get_output(&ctx);
492 if (val == EINJ_STATUS_FAIL)
494 else if (val == EINJ_STATUS_INVAL)
498 * The error is injected into the platform successfully, then it needs
499 * to trigger the error.
501 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
504 trigger_paddr = apei_exec_ctx_get_output(&ctx);
505 if (notrigger == 0) {
506 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
510 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
515 /* Inject the specified hardware error */
516 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
517 u64 param3, u64 param4)
522 /* If user manually set "flags", make sure it is legal */
523 if (flags && (flags &
524 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
528 * We need extra sanity checks for memory errors.
529 * Other types leap directly to injection.
532 /* ensure param1/param2 existed */
533 if (!(param_extension || acpi5))
536 /* ensure injection is memory related */
537 if (type & ACPI5_VENDOR_BIT) {
538 if (vendor_flags != SETWA_FLAGS_MEM)
540 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
544 * Disallow crazy address masks that give BIOS leeway to pick
545 * injection address almost anywhere. Insist on page or
546 * better granularity and that target address is normal RAM or
549 base_addr = param1 & param2;
552 if (((param2 & PAGE_MASK) != PAGE_MASK) ||
553 ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
554 != REGION_INTERSECTS) &&
555 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
556 != REGION_INTERSECTS) &&
557 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED)
558 != REGION_INTERSECTS) &&
559 !arch_is_platform_page(base_addr)))
562 if (is_zero_pfn(base_addr >> PAGE_SHIFT))
566 mutex_lock(&einj_mutex);
567 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
568 mutex_unlock(&einj_mutex);
573 static u32 error_type;
574 static u32 error_flags;
575 static u64 error_param1;
576 static u64 error_param2;
577 static u64 error_param3;
578 static u64 error_param4;
579 static struct dentry *einj_debug_dir;
580 static const char * const einj_error_type_string[] = {
581 "0x00000001\tProcessor Correctable\n",
582 "0x00000002\tProcessor Uncorrectable non-fatal\n",
583 "0x00000004\tProcessor Uncorrectable fatal\n",
584 "0x00000008\tMemory Correctable\n",
585 "0x00000010\tMemory Uncorrectable non-fatal\n",
586 "0x00000020\tMemory Uncorrectable fatal\n",
587 "0x00000040\tPCI Express Correctable\n",
588 "0x00000080\tPCI Express Uncorrectable non-fatal\n",
589 "0x00000100\tPCI Express Uncorrectable fatal\n",
590 "0x00000200\tPlatform Correctable\n",
591 "0x00000400\tPlatform Uncorrectable non-fatal\n",
592 "0x00000800\tPlatform Uncorrectable fatal\n",
593 "0x00001000\tCXL.cache Protocol Correctable\n",
594 "0x00002000\tCXL.cache Protocol Uncorrectable non-fatal\n",
595 "0x00004000\tCXL.cache Protocol Uncorrectable fatal\n",
596 "0x00008000\tCXL.mem Protocol Correctable\n",
597 "0x00010000\tCXL.mem Protocol Uncorrectable non-fatal\n",
598 "0x00020000\tCXL.mem Protocol Uncorrectable fatal\n",
601 static int available_error_type_show(struct seq_file *m, void *v)
604 u32 available_error_type = 0;
606 rc = einj_get_available_error_type(&available_error_type);
609 for (int pos = 0; pos < ARRAY_SIZE(einj_error_type_string); pos++)
610 if (available_error_type & BIT(pos))
611 seq_puts(m, einj_error_type_string[pos]);
616 DEFINE_SHOW_ATTRIBUTE(available_error_type);
618 static int error_type_get(void *data, u64 *val)
625 static int error_type_set(void *data, u64 val)
628 u32 available_error_type = 0;
631 /* Only low 32 bits for error type are valid */
632 if (val & GENMASK_ULL(63, 32))
636 * Vendor defined types have 0x80000000 bit set, and
637 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
639 vendor = val & ACPI5_VENDOR_BIT;
640 tval = val & 0x7fffffff;
642 /* Only one error type can be specified */
643 if (tval & (tval - 1))
646 rc = einj_get_available_error_type(&available_error_type);
649 if (!(val & available_error_type))
657 DEFINE_DEBUGFS_ATTRIBUTE(error_type_fops, error_type_get, error_type_set,
660 static int error_inject_set(void *data, u64 val)
665 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
666 error_param3, error_param4);
669 DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
671 static int einj_check_table(struct acpi_table_einj *einj_tab)
673 if ((einj_tab->header_length !=
674 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
675 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
677 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
679 if (einj_tab->entries !=
680 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
681 sizeof(struct acpi_einj_entry))
687 static int __init einj_init(void)
691 struct apei_exec_context ctx;
694 pr_info("ACPI disabled.\n");
698 status = acpi_get_table(ACPI_SIG_EINJ, 0,
699 (struct acpi_table_header **)&einj_tab);
700 if (status == AE_NOT_FOUND) {
701 pr_warn("EINJ table not found.\n");
703 } else if (ACPI_FAILURE(status)) {
704 pr_err("Failed to get EINJ table: %s\n",
705 acpi_format_exception(status));
709 rc = einj_check_table(einj_tab);
711 pr_warn(FW_BUG "Invalid EINJ table.\n");
716 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
718 debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
719 NULL, &available_error_type_fops);
720 debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
721 NULL, &error_type_fops);
722 debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
723 NULL, &error_inject_fops);
725 apei_resources_init(&einj_resources);
726 einj_exec_ctx_init(&ctx);
727 rc = apei_exec_collect_resources(&ctx, &einj_resources);
729 pr_err("Error collecting EINJ resources.\n");
733 rc = apei_resources_request(&einj_resources, "APEI EINJ");
735 pr_err("Error requesting memory/port resources.\n");
739 rc = apei_exec_pre_map_gars(&ctx);
741 pr_err("Error pre-mapping GARs.\n");
745 einj_param = einj_get_parameter_address();
746 if ((param_extension || acpi5) && einj_param) {
747 debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
749 debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
751 debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
753 debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
755 debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
757 debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
758 einj_debug_dir, ¬rigger);
762 vendor_blob.data = vendor_dev;
763 vendor_blob.size = strlen(vendor_dev);
764 debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
766 debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
767 einj_debug_dir, &vendor_flags);
770 pr_info("Error INJection is initialized.\n");
775 apei_resources_release(&einj_resources);
777 apei_resources_fini(&einj_resources);
778 debugfs_remove_recursive(einj_debug_dir);
780 acpi_put_table((struct acpi_table_header *)einj_tab);
785 static void __exit einj_exit(void)
787 struct apei_exec_context ctx;
790 acpi_size size = (acpi5) ?
791 sizeof(struct set_error_type_with_address) :
792 sizeof(struct einj_parameter);
794 acpi_os_unmap_iomem(einj_param, size);
796 einj_exec_ctx_init(&ctx);
797 apei_exec_post_unmap_gars(&ctx);
798 apei_resources_release(&einj_resources);
799 apei_resources_fini(&einj_resources);
800 debugfs_remove_recursive(einj_debug_dir);
801 acpi_put_table((struct acpi_table_header *)einj_tab);
804 module_init(einj_init);
805 module_exit(einj_exit);
807 MODULE_AUTHOR("Huang Ying");
808 MODULE_DESCRIPTION("APEI Error INJection support");
809 MODULE_LICENSE("GPL");