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>
37 #include "apei-internal.h"
39 #define EINJ_PFX "EINJ: "
41 #define SPIN_UNIT 100 /* 100ns */
42 /* Firmware should respond within 1 milliseconds */
43 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
44 #define ACPI5_VENDOR_BIT BIT(31)
45 #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \
46 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
47 ACPI_EINJ_MEMORY_FATAL)
50 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
54 struct set_error_type_with_address {
60 u64 memory_address_range;
64 SETWA_FLAGS_APICID = 1,
66 SETWA_FLAGS_PCIE_SBDF = 4,
70 * Vendor extensions for platform specific operations
72 struct vendor_error_type_extension {
83 static u32 vendor_flags;
84 static struct debugfs_blob_wrapper vendor_blob;
85 static char vendor_dev[64];
88 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
89 * EINJ table through an unpublished extension. Use with caution as
90 * most will ignore the parameter and make their own choice of address
91 * for error injection. This extension is used only if
92 * param_extension module parameter is specified.
94 struct einj_parameter {
102 #define EINJ_OP_BUSY 0x1
103 #define EINJ_STATUS_SUCCESS 0x0
104 #define EINJ_STATUS_FAIL 0x1
105 #define EINJ_STATUS_INVAL 0x2
107 #define EINJ_TAB_ENTRY(tab) \
108 ((struct acpi_whea_header *)((char *)(tab) + \
109 sizeof(struct acpi_table_einj)))
111 static bool param_extension;
112 module_param(param_extension, bool, 0);
114 static struct acpi_table_einj *einj_tab;
116 static struct apei_resources einj_resources;
118 static struct apei_exec_ins_type einj_ins_type[] = {
119 [ACPI_EINJ_READ_REGISTER] = {
120 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
121 .run = apei_exec_read_register,
123 [ACPI_EINJ_READ_REGISTER_VALUE] = {
124 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
125 .run = apei_exec_read_register_value,
127 [ACPI_EINJ_WRITE_REGISTER] = {
128 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
129 .run = apei_exec_write_register,
131 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
132 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
133 .run = apei_exec_write_register_value,
137 .run = apei_exec_noop,
142 * Prevent EINJ interpreter to run simultaneously, because the
143 * corresponding firmware implementation may not work properly when
144 * invoked simultaneously.
146 static DEFINE_MUTEX(einj_mutex);
148 static void *einj_param;
150 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
152 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
153 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
156 static int __einj_get_available_error_type(u32 *type)
158 struct apei_exec_context ctx;
161 einj_exec_ctx_init(&ctx);
162 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
165 *type = apei_exec_ctx_get_output(&ctx);
170 /* Get error injection capabilities of the platform */
171 static int einj_get_available_error_type(u32 *type)
175 mutex_lock(&einj_mutex);
176 rc = __einj_get_available_error_type(type);
177 mutex_unlock(&einj_mutex);
182 static int einj_timedout(u64 *t)
184 if ((s64)*t < SPIN_UNIT) {
185 pr_warning(FW_WARN EINJ_PFX
186 "Firmware does not respond in time\n");
191 touch_nmi_watchdog();
195 static void check_vendor_extension(u64 paddr,
196 struct set_error_type_with_address *v5param)
198 int offset = v5param->vendor_extension;
199 struct vendor_error_type_extension *v;
204 v = acpi_os_map_memory(paddr + offset, sizeof(*v));
208 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
209 sbdf >> 24, (sbdf >> 16) & 0xff,
210 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
211 v->vendor_id, v->device_id, v->rev_id);
212 acpi_os_unmap_memory(v, sizeof(*v));
215 static void *einj_get_parameter_address(void)
218 u64 paddrv4 = 0, paddrv5 = 0;
219 struct acpi_whea_header *entry;
221 entry = EINJ_TAB_ENTRY(einj_tab);
222 for (i = 0; i < einj_tab->entries; i++) {
223 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
224 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
225 entry->register_region.space_id ==
226 ACPI_ADR_SPACE_SYSTEM_MEMORY)
227 memcpy(&paddrv4, &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 memcpy(&paddrv5, &entry->register_region.address,
238 struct set_error_type_with_address *v5param;
240 v5param = acpi_os_map_memory(paddrv5, sizeof(*v5param));
243 check_vendor_extension(paddrv5, v5param);
247 if (param_extension && paddrv4) {
248 struct einj_parameter *v4param;
250 v4param = acpi_os_map_memory(paddrv4, sizeof(*v4param));
253 if (v4param->reserved1 || v4param->reserved2) {
254 acpi_os_unmap_memory(v4param, sizeof(*v4param));
263 /* do sanity check to trigger table */
264 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
266 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
268 if (trigger_tab->table_size > PAGE_SIZE ||
269 trigger_tab->table_size < trigger_tab->header_size)
271 if (trigger_tab->entry_count !=
272 (trigger_tab->table_size - trigger_tab->header_size) /
273 sizeof(struct acpi_einj_entry))
279 static struct acpi_generic_address *einj_get_trigger_parameter_region(
280 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
283 struct acpi_whea_header *entry;
285 entry = (struct acpi_whea_header *)
286 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
287 for (i = 0; i < trigger_tab->entry_count; i++) {
288 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
289 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
290 entry->register_region.space_id ==
291 ACPI_ADR_SPACE_SYSTEM_MEMORY &&
292 (entry->register_region.address & param2) == (param1 & param2))
293 return &entry->register_region;
299 /* Execute instructions in trigger error action table */
300 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
301 u64 param1, u64 param2)
303 struct acpi_einj_trigger *trigger_tab = NULL;
304 struct apei_exec_context trigger_ctx;
305 struct apei_resources trigger_resources;
306 struct acpi_whea_header *trigger_entry;
310 struct acpi_generic_address *trigger_param_region = NULL;
312 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
313 "APEI EINJ Trigger Table");
316 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
317 (unsigned long long)trigger_paddr,
318 (unsigned long long)trigger_paddr +
319 sizeof(*trigger_tab) - 1);
322 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
324 pr_err(EINJ_PFX "Failed to map trigger table!\n");
327 rc = einj_check_trigger_header(trigger_tab);
329 pr_warning(FW_BUG EINJ_PFX
330 "The trigger error action table is invalid\n");
334 /* No action structures in the TRIGGER_ERROR table, nothing to do */
335 if (!trigger_tab->entry_count)
339 table_size = trigger_tab->table_size;
340 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
341 table_size - sizeof(*trigger_tab),
342 "APEI EINJ Trigger Table");
345 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
346 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
347 (unsigned long long)trigger_paddr + table_size - 1);
350 iounmap(trigger_tab);
351 trigger_tab = ioremap_cache(trigger_paddr, table_size);
353 pr_err(EINJ_PFX "Failed to map trigger table!\n");
356 trigger_entry = (struct acpi_whea_header *)
357 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
358 apei_resources_init(&trigger_resources);
359 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
360 ARRAY_SIZE(einj_ins_type),
361 trigger_entry, trigger_tab->entry_count);
362 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
365 rc = apei_resources_sub(&trigger_resources, &einj_resources);
369 * Some firmware will access target address specified in
370 * param1 to trigger the error when injecting memory error.
371 * This will cause resource conflict with regular memory. So
372 * remove it from trigger table resources.
374 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
375 struct apei_resources addr_resources;
376 apei_resources_init(&addr_resources);
377 trigger_param_region = einj_get_trigger_parameter_region(
378 trigger_tab, param1, param2);
379 if (trigger_param_region) {
380 rc = apei_resources_add(&addr_resources,
381 trigger_param_region->address,
382 trigger_param_region->bit_width/8, true);
385 rc = apei_resources_sub(&trigger_resources,
388 apei_resources_fini(&addr_resources);
392 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
395 rc = apei_exec_pre_map_gars(&trigger_ctx);
399 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
401 apei_exec_post_unmap_gars(&trigger_ctx);
403 apei_resources_release(&trigger_resources);
405 apei_resources_fini(&trigger_resources);
407 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
408 table_size - sizeof(*trigger_tab));
410 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
413 iounmap(trigger_tab);
418 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
419 u64 param3, u64 param4)
421 struct apei_exec_context ctx;
422 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
425 einj_exec_ctx_init(&ctx);
427 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
430 apei_exec_ctx_set_input(&ctx, type);
432 struct set_error_type_with_address *v5param = einj_param;
434 v5param->type = type;
435 if (type & ACPI5_VENDOR_BIT) {
436 switch (vendor_flags) {
437 case SETWA_FLAGS_APICID:
438 v5param->apicid = param1;
440 case SETWA_FLAGS_MEM:
441 v5param->memory_address = param1;
442 v5param->memory_address_range = param2;
444 case SETWA_FLAGS_PCIE_SBDF:
445 v5param->pcie_sbdf = param1;
448 v5param->flags = vendor_flags;
450 v5param->flags = flags;
451 v5param->memory_address = param1;
452 v5param->memory_address_range = param2;
453 v5param->apicid = param3;
454 v5param->pcie_sbdf = param4;
457 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
458 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
459 case ACPI_EINJ_PROCESSOR_FATAL:
460 v5param->apicid = param1;
461 v5param->flags = SETWA_FLAGS_APICID;
463 case ACPI_EINJ_MEMORY_CORRECTABLE:
464 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
465 case ACPI_EINJ_MEMORY_FATAL:
466 v5param->memory_address = param1;
467 v5param->memory_address_range = param2;
468 v5param->flags = SETWA_FLAGS_MEM;
470 case ACPI_EINJ_PCIX_CORRECTABLE:
471 case ACPI_EINJ_PCIX_UNCORRECTABLE:
472 case ACPI_EINJ_PCIX_FATAL:
473 v5param->pcie_sbdf = param1;
474 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
479 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
483 struct einj_parameter *v4param = einj_param;
484 v4param->param1 = param1;
485 v4param->param2 = param2;
488 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
492 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
495 val = apei_exec_ctx_get_output(&ctx);
496 if (!(val & EINJ_OP_BUSY))
498 if (einj_timedout(&timeout))
501 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
504 val = apei_exec_ctx_get_output(&ctx);
505 if (val != EINJ_STATUS_SUCCESS)
508 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
511 trigger_paddr = apei_exec_ctx_get_output(&ctx);
512 if (notrigger == 0) {
513 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
517 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
522 /* Inject the specified hardware error */
523 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
524 u64 param3, u64 param4)
529 /* If user manually set "flags", make sure it is legal */
530 if (flags && (flags &
531 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
535 * We need extra sanity checks for memory errors.
536 * Other types leap directly to injection.
539 /* ensure param1/param2 existed */
540 if (!(param_extension || acpi5))
543 /* ensure injection is memory related */
544 if (type & ACPI5_VENDOR_BIT) {
545 if (vendor_flags != SETWA_FLAGS_MEM)
547 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
551 * Disallow crazy address masks that give BIOS leeway to pick
552 * injection address almost anywhere. Insist on page or
553 * better granularity and that target address is normal RAM.
555 pfn = PFN_DOWN(param1 & param2);
556 if (!page_is_ram(pfn) || ((param2 & PAGE_MASK) != PAGE_MASK))
560 mutex_lock(&einj_mutex);
561 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
562 mutex_unlock(&einj_mutex);
567 static u32 error_type;
568 static u32 error_flags;
569 static u64 error_param1;
570 static u64 error_param2;
571 static u64 error_param3;
572 static u64 error_param4;
573 static struct dentry *einj_debug_dir;
575 static int available_error_type_show(struct seq_file *m, void *v)
578 u32 available_error_type = 0;
580 rc = einj_get_available_error_type(&available_error_type);
583 if (available_error_type & 0x0001)
584 seq_printf(m, "0x00000001\tProcessor Correctable\n");
585 if (available_error_type & 0x0002)
586 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
587 if (available_error_type & 0x0004)
588 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
589 if (available_error_type & 0x0008)
590 seq_printf(m, "0x00000008\tMemory Correctable\n");
591 if (available_error_type & 0x0010)
592 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
593 if (available_error_type & 0x0020)
594 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
595 if (available_error_type & 0x0040)
596 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
597 if (available_error_type & 0x0080)
598 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
599 if (available_error_type & 0x0100)
600 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
601 if (available_error_type & 0x0200)
602 seq_printf(m, "0x00000200\tPlatform Correctable\n");
603 if (available_error_type & 0x0400)
604 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
605 if (available_error_type & 0x0800)
606 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
611 static int available_error_type_open(struct inode *inode, struct file *file)
613 return single_open(file, available_error_type_show, NULL);
616 static const struct file_operations available_error_type_fops = {
617 .open = available_error_type_open,
620 .release = single_release,
623 static int error_type_get(void *data, u64 *val)
630 static int error_type_set(void *data, u64 val)
633 u32 available_error_type = 0;
637 * Vendor defined types have 0x80000000 bit set, and
638 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
640 vendor = val & ACPI5_VENDOR_BIT;
641 tval = val & 0x7fffffff;
643 /* Only one error type can be specified */
644 if (tval & (tval - 1))
647 rc = einj_get_available_error_type(&available_error_type);
650 if (!(val & available_error_type))
658 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
659 error_type_set, "0x%llx\n");
661 static int error_inject_set(void *data, u64 val)
666 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
667 error_param3, error_param4);
670 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
671 error_inject_set, "%llu\n");
673 static int einj_check_table(struct acpi_table_einj *einj_tab)
675 if ((einj_tab->header_length !=
676 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
677 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
679 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
681 if (einj_tab->entries !=
682 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
683 sizeof(struct acpi_einj_entry))
689 static int __init einj_init(void)
693 struct dentry *fentry;
694 struct apei_exec_context ctx;
699 status = acpi_get_table(ACPI_SIG_EINJ, 0,
700 (struct acpi_table_header **)&einj_tab);
701 if (status == AE_NOT_FOUND)
703 else if (ACPI_FAILURE(status)) {
704 const char *msg = acpi_format_exception(status);
705 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
709 rc = einj_check_table(einj_tab);
711 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
716 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
719 fentry = debugfs_create_file("available_error_type", S_IRUSR,
720 einj_debug_dir, NULL,
721 &available_error_type_fops);
724 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
725 einj_debug_dir, NULL, &error_type_fops);
728 fentry = debugfs_create_file("error_inject", S_IWUSR,
729 einj_debug_dir, NULL, &error_inject_fops);
733 apei_resources_init(&einj_resources);
734 einj_exec_ctx_init(&ctx);
735 rc = apei_exec_collect_resources(&ctx, &einj_resources);
738 rc = apei_resources_request(&einj_resources, "APEI EINJ");
741 rc = apei_exec_pre_map_gars(&ctx);
746 einj_param = einj_get_parameter_address();
747 if ((param_extension || acpi5) && einj_param) {
748 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
749 einj_debug_dir, &error_flags);
752 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
753 einj_debug_dir, &error_param1);
756 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
757 einj_debug_dir, &error_param2);
760 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
761 einj_debug_dir, &error_param3);
764 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
765 einj_debug_dir, &error_param4);
769 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
770 einj_debug_dir, ¬rigger);
776 vendor_blob.data = vendor_dev;
777 vendor_blob.size = strlen(vendor_dev);
778 fentry = debugfs_create_blob("vendor", S_IRUSR,
779 einj_debug_dir, &vendor_blob);
782 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
783 einj_debug_dir, &vendor_flags);
788 pr_info(EINJ_PFX "Error INJection is initialized.\n");
794 acpi_size size = (acpi5) ?
795 sizeof(struct set_error_type_with_address) :
796 sizeof(struct einj_parameter);
798 acpi_os_unmap_memory(einj_param, size);
800 apei_exec_post_unmap_gars(&ctx);
802 apei_resources_release(&einj_resources);
804 apei_resources_fini(&einj_resources);
806 debugfs_remove_recursive(einj_debug_dir);
811 static void __exit einj_exit(void)
813 struct apei_exec_context ctx;
816 acpi_size size = (acpi5) ?
817 sizeof(struct set_error_type_with_address) :
818 sizeof(struct einj_parameter);
820 acpi_os_unmap_memory(einj_param, size);
822 einj_exec_ctx_init(&ctx);
823 apei_exec_post_unmap_gars(&ctx);
824 apei_resources_release(&einj_resources);
825 apei_resources_fini(&einj_resources);
826 debugfs_remove_recursive(einj_debug_dir);
829 module_init(einj_init);
830 module_exit(einj_exit);
832 MODULE_AUTHOR("Huang Ying");
833 MODULE_DESCRIPTION("APEI Error INJection support");
834 MODULE_LICENSE("GPL");