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.
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
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/nmi.h>
34 #include <linux/delay.h>
36 #include <asm/unaligned.h>
38 #include "apei-internal.h"
40 #define EINJ_PFX "EINJ: "
42 #define SPIN_UNIT 100 /* 100ns */
43 /* Firmware should respond within 1 milliseconds */
44 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
45 #define ACPI5_VENDOR_BIT BIT(31)
46 #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
47 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
48 ACPI_EINJ_MEMORY_FATAL)
51 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
55 struct set_error_type_with_address {
61 u64 memory_address_range;
65 SETWA_FLAGS_APICID = 1,
67 SETWA_FLAGS_PCIE_SBDF = 4,
71 * Vendor extensions for platform specific operations
73 struct vendor_error_type_extension {
84 static u32 vendor_flags;
85 static struct debugfs_blob_wrapper vendor_blob;
86 static char vendor_dev[64];
89 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
90 * EINJ table through an unpublished extension. Use with caution as
91 * most will ignore the parameter and make their own choice of address
92 * for error injection. This extension is used only if
93 * param_extension module parameter is specified.
95 struct einj_parameter {
103 #define EINJ_OP_BUSY 0x1
104 #define EINJ_STATUS_SUCCESS 0x0
105 #define EINJ_STATUS_FAIL 0x1
106 #define EINJ_STATUS_INVAL 0x2
108 #define EINJ_TAB_ENTRY(tab) \
109 ((struct acpi_whea_header *)((char *)(tab) + \
110 sizeof(struct acpi_table_einj)))
112 static bool param_extension;
113 module_param(param_extension, bool, 0);
115 static struct acpi_table_einj *einj_tab;
117 static struct apei_resources einj_resources;
119 static struct apei_exec_ins_type einj_ins_type[] = {
120 [ACPI_EINJ_READ_REGISTER] = {
121 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
122 .run = apei_exec_read_register,
124 [ACPI_EINJ_READ_REGISTER_VALUE] = {
125 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
126 .run = apei_exec_read_register_value,
128 [ACPI_EINJ_WRITE_REGISTER] = {
129 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
130 .run = apei_exec_write_register,
132 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
133 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
134 .run = apei_exec_write_register_value,
138 .run = apei_exec_noop,
143 * Prevent EINJ interpreter to run simultaneously, because the
144 * corresponding firmware implementation may not work properly when
145 * invoked simultaneously.
147 static DEFINE_MUTEX(einj_mutex);
149 static void *einj_param;
151 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
153 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
154 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
157 static int __einj_get_available_error_type(u32 *type)
159 struct apei_exec_context ctx;
162 einj_exec_ctx_init(&ctx);
163 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
166 *type = apei_exec_ctx_get_output(&ctx);
171 /* Get error injection capabilities of the platform */
172 static int einj_get_available_error_type(u32 *type)
176 mutex_lock(&einj_mutex);
177 rc = __einj_get_available_error_type(type);
178 mutex_unlock(&einj_mutex);
183 static int einj_timedout(u64 *t)
185 if ((s64)*t < SPIN_UNIT) {
186 pr_warning(FW_WARN EINJ_PFX
187 "Firmware does not respond in time\n");
192 touch_nmi_watchdog();
196 static void check_vendor_extension(u64 paddr,
197 struct set_error_type_with_address *v5param)
199 int offset = v5param->vendor_extension;
200 struct vendor_error_type_extension *v;
205 v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
209 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
210 sbdf >> 24, (sbdf >> 16) & 0xff,
211 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
212 v->vendor_id, v->device_id, v->rev_id);
213 acpi_os_unmap_iomem(v, sizeof(*v));
216 static void *einj_get_parameter_address(void)
219 u64 pa_v4 = 0, pa_v5 = 0;
220 struct acpi_whea_header *entry;
222 entry = EINJ_TAB_ENTRY(einj_tab);
223 for (i = 0; i < einj_tab->entries; i++) {
224 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
225 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
226 entry->register_region.space_id ==
227 ACPI_ADR_SPACE_SYSTEM_MEMORY)
228 pa_v4 = get_unaligned(&entry->register_region.address);
229 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
230 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
231 entry->register_region.space_id ==
232 ACPI_ADR_SPACE_SYSTEM_MEMORY)
233 pa_v5 = get_unaligned(&entry->register_region.address);
237 struct set_error_type_with_address *v5param;
239 v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
242 check_vendor_extension(pa_v5, v5param);
246 if (param_extension && pa_v4) {
247 struct einj_parameter *v4param;
249 v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
252 if (v4param->reserved1 || v4param->reserved2) {
253 acpi_os_unmap_iomem(v4param, sizeof(*v4param));
262 /* do sanity check to trigger table */
263 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
265 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
267 if (trigger_tab->table_size > PAGE_SIZE ||
268 trigger_tab->table_size < trigger_tab->header_size)
270 if (trigger_tab->entry_count !=
271 (trigger_tab->table_size - trigger_tab->header_size) /
272 sizeof(struct acpi_einj_entry))
278 static struct acpi_generic_address *einj_get_trigger_parameter_region(
279 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
282 struct acpi_whea_header *entry;
284 entry = (struct acpi_whea_header *)
285 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
286 for (i = 0; i < trigger_tab->entry_count; i++) {
287 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
288 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
289 entry->register_region.space_id ==
290 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
291 (entry->register_region.address & param2) == (param1 & param2))
292 return &entry->register_region;
298 /* Execute instructions in trigger error action table */
299 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
300 u64 param1, u64 param2)
302 struct acpi_einj_trigger *trigger_tab = NULL;
303 struct apei_exec_context trigger_ctx;
304 struct apei_resources trigger_resources;
305 struct acpi_whea_header *trigger_entry;
309 struct acpi_generic_address *trigger_param_region = NULL;
311 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
312 "APEI EINJ Trigger Table");
315 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
316 (unsigned long long)trigger_paddr,
317 (unsigned long long)trigger_paddr +
318 sizeof(*trigger_tab) - 1);
321 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
323 pr_err(EINJ_PFX "Failed to map trigger table!\n");
326 rc = einj_check_trigger_header(trigger_tab);
328 pr_warning(FW_BUG EINJ_PFX
329 "The trigger error action table is invalid\n");
333 /* No action structures in the TRIGGER_ERROR table, nothing to do */
334 if (!trigger_tab->entry_count)
338 table_size = trigger_tab->table_size;
339 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
340 table_size - sizeof(*trigger_tab),
341 "APEI EINJ Trigger Table");
344 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
345 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
346 (unsigned long long)trigger_paddr + table_size - 1);
349 iounmap(trigger_tab);
350 trigger_tab = ioremap_cache(trigger_paddr, table_size);
352 pr_err(EINJ_PFX "Failed to map trigger table!\n");
355 trigger_entry = (struct acpi_whea_header *)
356 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
357 apei_resources_init(&trigger_resources);
358 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
359 ARRAY_SIZE(einj_ins_type),
360 trigger_entry, trigger_tab->entry_count);
361 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
364 rc = apei_resources_sub(&trigger_resources, &einj_resources);
368 * Some firmware will access target address specified in
369 * param1 to trigger the error when injecting memory error.
370 * This will cause resource conflict with regular memory. So
371 * remove it from trigger table resources.
373 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
374 struct apei_resources addr_resources;
375 apei_resources_init(&addr_resources);
376 trigger_param_region = einj_get_trigger_parameter_region(
377 trigger_tab, param1, param2);
378 if (trigger_param_region) {
379 rc = apei_resources_add(&addr_resources,
380 trigger_param_region->address,
381 trigger_param_region->bit_width/8, true);
384 rc = apei_resources_sub(&trigger_resources,
387 apei_resources_fini(&addr_resources);
391 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
394 rc = apei_exec_pre_map_gars(&trigger_ctx);
398 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
400 apei_exec_post_unmap_gars(&trigger_ctx);
402 apei_resources_release(&trigger_resources);
404 apei_resources_fini(&trigger_resources);
406 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
407 table_size - sizeof(*trigger_tab));
409 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
412 iounmap(trigger_tab);
417 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
418 u64 param3, u64 param4)
420 struct apei_exec_context ctx;
421 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
424 einj_exec_ctx_init(&ctx);
426 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
429 apei_exec_ctx_set_input(&ctx, type);
431 struct set_error_type_with_address *v5param = einj_param;
433 v5param->type = type;
434 if (type & ACPI5_VENDOR_BIT) {
435 switch (vendor_flags) {
436 case SETWA_FLAGS_APICID:
437 v5param->apicid = param1;
439 case SETWA_FLAGS_MEM:
440 v5param->memory_address = param1;
441 v5param->memory_address_range = param2;
443 case SETWA_FLAGS_PCIE_SBDF:
444 v5param->pcie_sbdf = param1;
447 v5param->flags = vendor_flags;
449 v5param->flags = flags;
450 v5param->memory_address = param1;
451 v5param->memory_address_range = param2;
452 v5param->apicid = param3;
453 v5param->pcie_sbdf = param4;
456 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
457 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
458 case ACPI_EINJ_PROCESSOR_FATAL:
459 v5param->apicid = param1;
460 v5param->flags = SETWA_FLAGS_APICID;
462 case ACPI_EINJ_MEMORY_CORRECTABLE:
463 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
464 case ACPI_EINJ_MEMORY_FATAL:
465 v5param->memory_address = param1;
466 v5param->memory_address_range = param2;
467 v5param->flags = SETWA_FLAGS_MEM;
469 case ACPI_EINJ_PCIX_CORRECTABLE:
470 case ACPI_EINJ_PCIX_UNCORRECTABLE:
471 case ACPI_EINJ_PCIX_FATAL:
472 v5param->pcie_sbdf = param1;
473 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
478 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
482 struct einj_parameter *v4param = einj_param;
483 v4param->param1 = param1;
484 v4param->param2 = param2;
487 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
491 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
494 val = apei_exec_ctx_get_output(&ctx);
495 if (!(val & EINJ_OP_BUSY))
497 if (einj_timedout(&timeout))
500 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
503 val = apei_exec_ctx_get_output(&ctx);
504 if (val != EINJ_STATUS_SUCCESS)
507 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
510 trigger_paddr = apei_exec_ctx_get_output(&ctx);
511 if (notrigger == 0) {
512 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
516 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
521 /* Inject the specified hardware error */
522 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
523 u64 param3, u64 param4)
528 /* If user manually set "flags", make sure it is legal */
529 if (flags && (flags &
530 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
534 * We need extra sanity checks for memory errors.
535 * Other types leap directly to injection.
538 /* ensure param1/param2 existed */
539 if (!(param_extension || acpi5))
542 /* ensure injection is memory related */
543 if (type & ACPI5_VENDOR_BIT) {
544 if (vendor_flags != SETWA_FLAGS_MEM)
546 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
550 * Disallow crazy address masks that give BIOS leeway to pick
551 * injection address almost anywhere. Insist on page or
552 * better granularity and that target address is normal RAM.
554 pfn = PFN_DOWN(param1 & param2);
555 if (!page_is_ram(pfn) || ((param2 & PAGE_MASK) != PAGE_MASK))
559 mutex_lock(&einj_mutex);
560 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
561 mutex_unlock(&einj_mutex);
566 static u32 error_type;
567 static u32 error_flags;
568 static u64 error_param1;
569 static u64 error_param2;
570 static u64 error_param3;
571 static u64 error_param4;
572 static struct dentry *einj_debug_dir;
574 static int available_error_type_show(struct seq_file *m, void *v)
577 u32 available_error_type = 0;
579 rc = einj_get_available_error_type(&available_error_type);
582 if (available_error_type & 0x0001)
583 seq_printf(m, "0x00000001\tProcessor Correctable\n");
584 if (available_error_type & 0x0002)
585 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
586 if (available_error_type & 0x0004)
587 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
588 if (available_error_type & 0x0008)
589 seq_printf(m, "0x00000008\tMemory Correctable\n");
590 if (available_error_type & 0x0010)
591 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
592 if (available_error_type & 0x0020)
593 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
594 if (available_error_type & 0x0040)
595 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
596 if (available_error_type & 0x0080)
597 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
598 if (available_error_type & 0x0100)
599 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
600 if (available_error_type & 0x0200)
601 seq_printf(m, "0x00000200\tPlatform Correctable\n");
602 if (available_error_type & 0x0400)
603 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
604 if (available_error_type & 0x0800)
605 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
610 static int available_error_type_open(struct inode *inode, struct file *file)
612 return single_open(file, available_error_type_show, NULL);
615 static const struct file_operations available_error_type_fops = {
616 .open = available_error_type_open,
619 .release = single_release,
622 static int error_type_get(void *data, u64 *val)
629 static int error_type_set(void *data, u64 val)
632 u32 available_error_type = 0;
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_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
658 error_type_set, "0x%llx\n");
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_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
670 error_inject_set, "%llu\n");
672 static int einj_check_table(struct acpi_table_einj *einj_tab)
674 if ((einj_tab->header_length !=
675 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
676 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
678 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
680 if (einj_tab->entries !=
681 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
682 sizeof(struct acpi_einj_entry))
688 static int __init einj_init(void)
692 struct dentry *fentry;
693 struct apei_exec_context ctx;
698 status = acpi_get_table(ACPI_SIG_EINJ, 0,
699 (struct acpi_table_header **)&einj_tab);
700 if (status == AE_NOT_FOUND)
702 else if (ACPI_FAILURE(status)) {
703 const char *msg = acpi_format_exception(status);
704 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
708 rc = einj_check_table(einj_tab);
710 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
715 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
718 fentry = debugfs_create_file("available_error_type", S_IRUSR,
719 einj_debug_dir, NULL,
720 &available_error_type_fops);
723 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
724 einj_debug_dir, NULL, &error_type_fops);
727 fentry = debugfs_create_file("error_inject", S_IWUSR,
728 einj_debug_dir, NULL, &error_inject_fops);
732 apei_resources_init(&einj_resources);
733 einj_exec_ctx_init(&ctx);
734 rc = apei_exec_collect_resources(&ctx, &einj_resources);
737 rc = apei_resources_request(&einj_resources, "APEI EINJ");
740 rc = apei_exec_pre_map_gars(&ctx);
745 einj_param = einj_get_parameter_address();
746 if ((param_extension || acpi5) && einj_param) {
747 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
748 einj_debug_dir, &error_flags);
751 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
752 einj_debug_dir, &error_param1);
755 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
756 einj_debug_dir, &error_param2);
759 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
760 einj_debug_dir, &error_param3);
763 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
764 einj_debug_dir, &error_param4);
768 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
769 einj_debug_dir, ¬rigger);
775 vendor_blob.data = vendor_dev;
776 vendor_blob.size = strlen(vendor_dev);
777 fentry = debugfs_create_blob("vendor", S_IRUSR,
778 einj_debug_dir, &vendor_blob);
781 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
782 einj_debug_dir, &vendor_flags);
787 pr_info(EINJ_PFX "Error INJection is initialized.\n");
793 acpi_size size = (acpi5) ?
794 sizeof(struct set_error_type_with_address) :
795 sizeof(struct einj_parameter);
797 acpi_os_unmap_iomem(einj_param, size);
799 apei_exec_post_unmap_gars(&ctx);
801 apei_resources_release(&einj_resources);
803 apei_resources_fini(&einj_resources);
805 debugfs_remove_recursive(einj_debug_dir);
810 static void __exit einj_exit(void)
812 struct apei_exec_context ctx;
815 acpi_size size = (acpi5) ?
816 sizeof(struct set_error_type_with_address) :
817 sizeof(struct einj_parameter);
819 acpi_os_unmap_iomem(einj_param, size);
821 einj_exec_ctx_init(&ctx);
822 apei_exec_post_unmap_gars(&ctx);
823 apei_resources_release(&einj_resources);
824 apei_resources_fini(&einj_resources);
825 debugfs_remove_recursive(einj_debug_dir);
828 module_init(einj_init);
829 module_exit(einj_exit);
831 MODULE_AUTHOR("Huang Ying");
832 MODULE_DESCRIPTION("APEI Error INJection support");
833 MODULE_LICENSE("GPL");