2 * APEI Error INJection support
4 * EINJ provides a hardware error injection mechanism, this is useful
5 * for debugging and testing of other APEI and RAS features.
7 * For more information about EINJ, please refer to ACPI Specification
8 * version 4.0, section 17.5.
10 * Copyright 2009-2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
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.
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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
27 #include <linux/debugfs.h>
28 #include <linux/seq_file.h>
29 #include <linux/nmi.h>
30 #include <linux/delay.h>
32 #include <asm/unaligned.h>
34 #include "apei-internal.h"
36 #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)
47 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
51 struct set_error_type_with_address {
57 u64 memory_address_range;
61 SETWA_FLAGS_APICID = 1,
63 SETWA_FLAGS_PCIE_SBDF = 4,
67 * Vendor extensions for platform specific operations
69 struct vendor_error_type_extension {
80 static u32 vendor_flags;
81 static struct debugfs_blob_wrapper vendor_blob;
82 static char vendor_dev[64];
85 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
86 * EINJ table through an unpublished extension. Use with caution as
87 * most will ignore the parameter and make their own choice of address
88 * for error injection. This extension is used only if
89 * param_extension module parameter is specified.
91 struct einj_parameter {
99 #define EINJ_OP_BUSY 0x1
100 #define EINJ_STATUS_SUCCESS 0x0
101 #define EINJ_STATUS_FAIL 0x1
102 #define EINJ_STATUS_INVAL 0x2
104 #define EINJ_TAB_ENTRY(tab) \
105 ((struct acpi_whea_header *)((char *)(tab) + \
106 sizeof(struct acpi_table_einj)))
108 static bool param_extension;
109 module_param(param_extension, bool, 0);
111 static struct acpi_table_einj *einj_tab;
113 static struct apei_resources einj_resources;
115 static struct apei_exec_ins_type einj_ins_type[] = {
116 [ACPI_EINJ_READ_REGISTER] = {
117 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
118 .run = apei_exec_read_register,
120 [ACPI_EINJ_READ_REGISTER_VALUE] = {
121 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
122 .run = apei_exec_read_register_value,
124 [ACPI_EINJ_WRITE_REGISTER] = {
125 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
126 .run = apei_exec_write_register,
128 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
129 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
130 .run = apei_exec_write_register_value,
134 .run = apei_exec_noop,
139 * Prevent EINJ interpreter to run simultaneously, because the
140 * corresponding firmware implementation may not work properly when
141 * invoked simultaneously.
143 static DEFINE_MUTEX(einj_mutex);
145 static void *einj_param;
147 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
149 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
150 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
153 static int __einj_get_available_error_type(u32 *type)
155 struct apei_exec_context ctx;
158 einj_exec_ctx_init(&ctx);
159 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
162 *type = apei_exec_ctx_get_output(&ctx);
167 /* Get error injection capabilities of the platform */
168 static int einj_get_available_error_type(u32 *type)
172 mutex_lock(&einj_mutex);
173 rc = __einj_get_available_error_type(type);
174 mutex_unlock(&einj_mutex);
179 static int einj_timedout(u64 *t)
181 if ((s64)*t < SPIN_UNIT) {
182 pr_warning(FW_WARN EINJ_PFX
183 "Firmware does not respond in time\n");
188 touch_nmi_watchdog();
192 static void check_vendor_extension(u64 paddr,
193 struct set_error_type_with_address *v5param)
195 int offset = v5param->vendor_extension;
196 struct vendor_error_type_extension *v;
201 v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
205 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
206 sbdf >> 24, (sbdf >> 16) & 0xff,
207 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
208 v->vendor_id, v->device_id, v->rev_id);
209 acpi_os_unmap_iomem(v, sizeof(*v));
212 static void *einj_get_parameter_address(void)
215 u64 pa_v4 = 0, pa_v5 = 0;
216 struct acpi_whea_header *entry;
218 entry = EINJ_TAB_ENTRY(einj_tab);
219 for (i = 0; i < einj_tab->entries; i++) {
220 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
221 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
222 entry->register_region.space_id ==
223 ACPI_ADR_SPACE_SYSTEM_MEMORY)
224 pa_v4 = get_unaligned(&entry->register_region.address);
225 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
226 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
227 entry->register_region.space_id ==
228 ACPI_ADR_SPACE_SYSTEM_MEMORY)
229 pa_v5 = get_unaligned(&entry->register_region.address);
233 struct set_error_type_with_address *v5param;
235 v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
238 check_vendor_extension(pa_v5, v5param);
242 if (param_extension && pa_v4) {
243 struct einj_parameter *v4param;
245 v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
248 if (v4param->reserved1 || v4param->reserved2) {
249 acpi_os_unmap_iomem(v4param, sizeof(*v4param));
258 /* do sanity check to trigger table */
259 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
261 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
263 if (trigger_tab->table_size > PAGE_SIZE ||
264 trigger_tab->table_size < trigger_tab->header_size)
266 if (trigger_tab->entry_count !=
267 (trigger_tab->table_size - trigger_tab->header_size) /
268 sizeof(struct acpi_einj_entry))
274 static struct acpi_generic_address *einj_get_trigger_parameter_region(
275 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
278 struct acpi_whea_header *entry;
280 entry = (struct acpi_whea_header *)
281 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
282 for (i = 0; i < trigger_tab->entry_count; i++) {
283 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
284 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
285 entry->register_region.space_id ==
286 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
287 (entry->register_region.address & param2) == (param1 & param2))
288 return &entry->register_region;
294 /* Execute instructions in trigger error action table */
295 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
296 u64 param1, u64 param2)
298 struct acpi_einj_trigger *trigger_tab = NULL;
299 struct apei_exec_context trigger_ctx;
300 struct apei_resources trigger_resources;
301 struct acpi_whea_header *trigger_entry;
305 struct acpi_generic_address *trigger_param_region = NULL;
307 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
308 "APEI EINJ Trigger Table");
311 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
312 (unsigned long long)trigger_paddr,
313 (unsigned long long)trigger_paddr +
314 sizeof(*trigger_tab) - 1);
317 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
319 pr_err(EINJ_PFX "Failed to map trigger table!\n");
322 rc = einj_check_trigger_header(trigger_tab);
324 pr_warning(FW_BUG EINJ_PFX
325 "The trigger error action table is invalid\n");
329 /* No action structures in the TRIGGER_ERROR table, nothing to do */
330 if (!trigger_tab->entry_count)
334 table_size = trigger_tab->table_size;
335 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
336 table_size - sizeof(*trigger_tab),
337 "APEI EINJ Trigger Table");
340 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
341 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
342 (unsigned long long)trigger_paddr + table_size - 1);
345 iounmap(trigger_tab);
346 trigger_tab = ioremap_cache(trigger_paddr, table_size);
348 pr_err(EINJ_PFX "Failed to map trigger table!\n");
351 trigger_entry = (struct acpi_whea_header *)
352 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
353 apei_resources_init(&trigger_resources);
354 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
355 ARRAY_SIZE(einj_ins_type),
356 trigger_entry, trigger_tab->entry_count);
357 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
360 rc = apei_resources_sub(&trigger_resources, &einj_resources);
364 * Some firmware will access target address specified in
365 * param1 to trigger the error when injecting memory error.
366 * This will cause resource conflict with regular memory. So
367 * remove it from trigger table resources.
369 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
370 struct apei_resources addr_resources;
371 apei_resources_init(&addr_resources);
372 trigger_param_region = einj_get_trigger_parameter_region(
373 trigger_tab, param1, param2);
374 if (trigger_param_region) {
375 rc = apei_resources_add(&addr_resources,
376 trigger_param_region->address,
377 trigger_param_region->bit_width/8, true);
380 rc = apei_resources_sub(&trigger_resources,
383 apei_resources_fini(&addr_resources);
387 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
390 rc = apei_exec_pre_map_gars(&trigger_ctx);
394 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
396 apei_exec_post_unmap_gars(&trigger_ctx);
398 apei_resources_release(&trigger_resources);
400 apei_resources_fini(&trigger_resources);
402 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
403 table_size - sizeof(*trigger_tab));
405 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
408 iounmap(trigger_tab);
413 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
414 u64 param3, u64 param4)
416 struct apei_exec_context ctx;
417 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
420 einj_exec_ctx_init(&ctx);
422 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
425 apei_exec_ctx_set_input(&ctx, type);
427 struct set_error_type_with_address *v5param = einj_param;
429 v5param->type = type;
430 if (type & ACPI5_VENDOR_BIT) {
431 switch (vendor_flags) {
432 case SETWA_FLAGS_APICID:
433 v5param->apicid = param1;
435 case SETWA_FLAGS_MEM:
436 v5param->memory_address = param1;
437 v5param->memory_address_range = param2;
439 case SETWA_FLAGS_PCIE_SBDF:
440 v5param->pcie_sbdf = param1;
443 v5param->flags = vendor_flags;
445 v5param->flags = flags;
446 v5param->memory_address = param1;
447 v5param->memory_address_range = param2;
448 v5param->apicid = param3;
449 v5param->pcie_sbdf = param4;
452 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
453 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
454 case ACPI_EINJ_PROCESSOR_FATAL:
455 v5param->apicid = param1;
456 v5param->flags = SETWA_FLAGS_APICID;
458 case ACPI_EINJ_MEMORY_CORRECTABLE:
459 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
460 case ACPI_EINJ_MEMORY_FATAL:
461 v5param->memory_address = param1;
462 v5param->memory_address_range = param2;
463 v5param->flags = SETWA_FLAGS_MEM;
465 case ACPI_EINJ_PCIX_CORRECTABLE:
466 case ACPI_EINJ_PCIX_UNCORRECTABLE:
467 case ACPI_EINJ_PCIX_FATAL:
468 v5param->pcie_sbdf = param1;
469 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
474 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
478 struct einj_parameter *v4param = einj_param;
479 v4param->param1 = param1;
480 v4param->param2 = param2;
483 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
487 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
490 val = apei_exec_ctx_get_output(&ctx);
491 if (!(val & EINJ_OP_BUSY))
493 if (einj_timedout(&timeout))
496 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
499 val = apei_exec_ctx_get_output(&ctx);
500 if (val != EINJ_STATUS_SUCCESS)
503 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
506 trigger_paddr = apei_exec_ctx_get_output(&ctx);
507 if (notrigger == 0) {
508 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
512 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
517 /* Inject the specified hardware error */
518 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
519 u64 param3, u64 param4)
524 /* If user manually set "flags", make sure it is legal */
525 if (flags && (flags &
526 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
530 * We need extra sanity checks for memory errors.
531 * Other types leap directly to injection.
534 /* ensure param1/param2 existed */
535 if (!(param_extension || acpi5))
538 /* ensure injection is memory related */
539 if (type & ACPI5_VENDOR_BIT) {
540 if (vendor_flags != SETWA_FLAGS_MEM)
542 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
546 * Disallow crazy address masks that give BIOS leeway to pick
547 * injection address almost anywhere. Insist on page or
548 * better granularity and that target address is normal RAM.
550 pfn = PFN_DOWN(param1 & param2);
551 if (!page_is_ram(pfn) || ((param2 & PAGE_MASK) != PAGE_MASK))
555 mutex_lock(&einj_mutex);
556 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
557 mutex_unlock(&einj_mutex);
562 static u32 error_type;
563 static u32 error_flags;
564 static u64 error_param1;
565 static u64 error_param2;
566 static u64 error_param3;
567 static u64 error_param4;
568 static struct dentry *einj_debug_dir;
570 static int available_error_type_show(struct seq_file *m, void *v)
573 u32 available_error_type = 0;
575 rc = einj_get_available_error_type(&available_error_type);
578 if (available_error_type & 0x0001)
579 seq_printf(m, "0x00000001\tProcessor Correctable\n");
580 if (available_error_type & 0x0002)
581 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
582 if (available_error_type & 0x0004)
583 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
584 if (available_error_type & 0x0008)
585 seq_printf(m, "0x00000008\tMemory Correctable\n");
586 if (available_error_type & 0x0010)
587 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
588 if (available_error_type & 0x0020)
589 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
590 if (available_error_type & 0x0040)
591 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
592 if (available_error_type & 0x0080)
593 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
594 if (available_error_type & 0x0100)
595 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
596 if (available_error_type & 0x0200)
597 seq_printf(m, "0x00000200\tPlatform Correctable\n");
598 if (available_error_type & 0x0400)
599 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
600 if (available_error_type & 0x0800)
601 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
606 static int available_error_type_open(struct inode *inode, struct file *file)
608 return single_open(file, available_error_type_show, NULL);
611 static const struct file_operations available_error_type_fops = {
612 .open = available_error_type_open,
615 .release = single_release,
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;
632 * Vendor defined types have 0x80000000 bit set, and
633 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
635 vendor = val & ACPI5_VENDOR_BIT;
636 tval = val & 0x7fffffff;
638 /* Only one error type can be specified */
639 if (tval & (tval - 1))
642 rc = einj_get_available_error_type(&available_error_type);
645 if (!(val & available_error_type))
653 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
654 error_type_set, "0x%llx\n");
656 static int error_inject_set(void *data, u64 val)
661 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
662 error_param3, error_param4);
665 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
666 error_inject_set, "%llu\n");
668 static int einj_check_table(struct acpi_table_einj *einj_tab)
670 if ((einj_tab->header_length !=
671 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
672 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
674 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
676 if (einj_tab->entries !=
677 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
678 sizeof(struct acpi_einj_entry))
684 static int __init einj_init(void)
688 struct dentry *fentry;
689 struct apei_exec_context ctx;
694 status = acpi_get_table(ACPI_SIG_EINJ, 0,
695 (struct acpi_table_header **)&einj_tab);
696 if (status == AE_NOT_FOUND)
698 else if (ACPI_FAILURE(status)) {
699 const char *msg = acpi_format_exception(status);
700 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
704 rc = einj_check_table(einj_tab);
706 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
711 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
714 fentry = debugfs_create_file("available_error_type", S_IRUSR,
715 einj_debug_dir, NULL,
716 &available_error_type_fops);
719 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
720 einj_debug_dir, NULL, &error_type_fops);
723 fentry = debugfs_create_file("error_inject", S_IWUSR,
724 einj_debug_dir, NULL, &error_inject_fops);
728 apei_resources_init(&einj_resources);
729 einj_exec_ctx_init(&ctx);
730 rc = apei_exec_collect_resources(&ctx, &einj_resources);
733 rc = apei_resources_request(&einj_resources, "APEI EINJ");
736 rc = apei_exec_pre_map_gars(&ctx);
741 einj_param = einj_get_parameter_address();
742 if ((param_extension || acpi5) && einj_param) {
743 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
744 einj_debug_dir, &error_flags);
747 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
748 einj_debug_dir, &error_param1);
751 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
752 einj_debug_dir, &error_param2);
755 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
756 einj_debug_dir, &error_param3);
759 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
760 einj_debug_dir, &error_param4);
764 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
765 einj_debug_dir, ¬rigger);
771 vendor_blob.data = vendor_dev;
772 vendor_blob.size = strlen(vendor_dev);
773 fentry = debugfs_create_blob("vendor", S_IRUSR,
774 einj_debug_dir, &vendor_blob);
777 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
778 einj_debug_dir, &vendor_flags);
783 pr_info(EINJ_PFX "Error INJection is initialized.\n");
789 acpi_size size = (acpi5) ?
790 sizeof(struct set_error_type_with_address) :
791 sizeof(struct einj_parameter);
793 acpi_os_unmap_iomem(einj_param, size);
795 apei_exec_post_unmap_gars(&ctx);
797 apei_resources_release(&einj_resources);
799 apei_resources_fini(&einj_resources);
801 debugfs_remove_recursive(einj_debug_dir);
806 static void __exit einj_exit(void)
808 struct apei_exec_context ctx;
811 acpi_size size = (acpi5) ?
812 sizeof(struct set_error_type_with_address) :
813 sizeof(struct einj_parameter);
815 acpi_os_unmap_iomem(einj_param, size);
817 einj_exec_ctx_init(&ctx);
818 apei_exec_post_unmap_gars(&ctx);
819 apei_resources_release(&einj_resources);
820 apei_resources_fini(&einj_resources);
821 debugfs_remove_recursive(einj_debug_dir);
824 module_init(einj_init);
825 module_exit(einj_exit);
827 MODULE_AUTHOR("Huang Ying");
828 MODULE_DESCRIPTION("APEI Error INJection support");
829 MODULE_LICENSE("GPL");