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, u64 param1, u64 param2)
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;
450 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
451 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
452 case ACPI_EINJ_PROCESSOR_FATAL:
453 v5param->apicid = param1;
454 v5param->flags = SETWA_FLAGS_APICID;
456 case ACPI_EINJ_MEMORY_CORRECTABLE:
457 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
458 case ACPI_EINJ_MEMORY_FATAL:
459 v5param->memory_address = param1;
460 v5param->memory_address_range = param2;
461 v5param->flags = SETWA_FLAGS_MEM;
463 case ACPI_EINJ_PCIX_CORRECTABLE:
464 case ACPI_EINJ_PCIX_UNCORRECTABLE:
465 case ACPI_EINJ_PCIX_FATAL:
466 v5param->pcie_sbdf = param1;
467 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
472 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
476 struct einj_parameter *v4param = einj_param;
477 v4param->param1 = param1;
478 v4param->param2 = param2;
481 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
485 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
488 val = apei_exec_ctx_get_output(&ctx);
489 if (!(val & EINJ_OP_BUSY))
491 if (einj_timedout(&timeout))
494 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
497 val = apei_exec_ctx_get_output(&ctx);
498 if (val != EINJ_STATUS_SUCCESS)
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, u64 param1, u64 param2)
522 * We need extra sanity checks for memory errors.
523 * Other types leap directly to injection.
526 /* ensure param1/param2 existed */
527 if (!(param_extension || acpi5))
530 /* ensure injection is memory related */
531 if (type & ACPI5_VENDOR_BIT) {
532 if (vendor_flags != SETWA_FLAGS_MEM)
534 } else if (!(type & MEM_ERROR_MASK))
538 * Disallow crazy address masks that give BIOS leeway to pick
539 * injection address almost anywhere. Insist on page or
540 * better granularity and that target address is normal RAM.
542 pfn = PFN_DOWN(param1 & param2);
543 if (!page_is_ram(pfn) || ((param2 & PAGE_MASK) != PAGE_MASK))
547 mutex_lock(&einj_mutex);
548 rc = __einj_error_inject(type, param1, param2);
549 mutex_unlock(&einj_mutex);
554 static u32 error_type;
555 static u64 error_param1;
556 static u64 error_param2;
557 static struct dentry *einj_debug_dir;
559 static int available_error_type_show(struct seq_file *m, void *v)
562 u32 available_error_type = 0;
564 rc = einj_get_available_error_type(&available_error_type);
567 if (available_error_type & 0x0001)
568 seq_printf(m, "0x00000001\tProcessor Correctable\n");
569 if (available_error_type & 0x0002)
570 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
571 if (available_error_type & 0x0004)
572 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
573 if (available_error_type & 0x0008)
574 seq_printf(m, "0x00000008\tMemory Correctable\n");
575 if (available_error_type & 0x0010)
576 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
577 if (available_error_type & 0x0020)
578 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
579 if (available_error_type & 0x0040)
580 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
581 if (available_error_type & 0x0080)
582 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
583 if (available_error_type & 0x0100)
584 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
585 if (available_error_type & 0x0200)
586 seq_printf(m, "0x00000200\tPlatform Correctable\n");
587 if (available_error_type & 0x0400)
588 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
589 if (available_error_type & 0x0800)
590 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
595 static int available_error_type_open(struct inode *inode, struct file *file)
597 return single_open(file, available_error_type_show, NULL);
600 static const struct file_operations available_error_type_fops = {
601 .open = available_error_type_open,
604 .release = single_release,
607 static int error_type_get(void *data, u64 *val)
614 static int error_type_set(void *data, u64 val)
617 u32 available_error_type = 0;
621 * Vendor defined types have 0x80000000 bit set, and
622 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
624 vendor = val & ACPI5_VENDOR_BIT;
625 tval = val & 0x7fffffff;
627 /* Only one error type can be specified */
628 if (tval & (tval - 1))
631 rc = einj_get_available_error_type(&available_error_type);
634 if (!(val & available_error_type))
642 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
643 error_type_set, "0x%llx\n");
645 static int error_inject_set(void *data, u64 val)
650 return einj_error_inject(error_type, error_param1, error_param2);
653 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
654 error_inject_set, "%llu\n");
656 static int einj_check_table(struct acpi_table_einj *einj_tab)
658 if ((einj_tab->header_length !=
659 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
660 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
662 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
664 if (einj_tab->entries !=
665 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
666 sizeof(struct acpi_einj_entry))
672 static int __init einj_init(void)
676 struct dentry *fentry;
677 struct apei_exec_context ctx;
682 status = acpi_get_table(ACPI_SIG_EINJ, 0,
683 (struct acpi_table_header **)&einj_tab);
684 if (status == AE_NOT_FOUND)
686 else if (ACPI_FAILURE(status)) {
687 const char *msg = acpi_format_exception(status);
688 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
692 rc = einj_check_table(einj_tab);
694 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
699 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
702 fentry = debugfs_create_file("available_error_type", S_IRUSR,
703 einj_debug_dir, NULL,
704 &available_error_type_fops);
707 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
708 einj_debug_dir, NULL, &error_type_fops);
711 fentry = debugfs_create_file("error_inject", S_IWUSR,
712 einj_debug_dir, NULL, &error_inject_fops);
716 apei_resources_init(&einj_resources);
717 einj_exec_ctx_init(&ctx);
718 rc = apei_exec_collect_resources(&ctx, &einj_resources);
721 rc = apei_resources_request(&einj_resources, "APEI EINJ");
724 rc = apei_exec_pre_map_gars(&ctx);
729 einj_param = einj_get_parameter_address();
730 if ((param_extension || acpi5) && einj_param) {
731 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
732 einj_debug_dir, &error_param1);
735 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
736 einj_debug_dir, &error_param2);
740 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
741 einj_debug_dir, ¬rigger);
747 vendor_blob.data = vendor_dev;
748 vendor_blob.size = strlen(vendor_dev);
749 fentry = debugfs_create_blob("vendor", S_IRUSR,
750 einj_debug_dir, &vendor_blob);
753 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
754 einj_debug_dir, &vendor_flags);
759 pr_info(EINJ_PFX "Error INJection is initialized.\n");
765 acpi_size size = (acpi5) ?
766 sizeof(struct set_error_type_with_address) :
767 sizeof(struct einj_parameter);
769 acpi_os_unmap_memory(einj_param, size);
771 apei_exec_post_unmap_gars(&ctx);
773 apei_resources_release(&einj_resources);
775 apei_resources_fini(&einj_resources);
777 debugfs_remove_recursive(einj_debug_dir);
782 static void __exit einj_exit(void)
784 struct apei_exec_context ctx;
787 acpi_size size = (acpi5) ?
788 sizeof(struct set_error_type_with_address) :
789 sizeof(struct einj_parameter);
791 acpi_os_unmap_memory(einj_param, size);
793 einj_exec_ctx_init(&ctx);
794 apei_exec_post_unmap_gars(&ctx);
795 apei_resources_release(&einj_resources);
796 apei_resources_fini(&einj_resources);
797 debugfs_remove_recursive(einj_debug_dir);
800 module_init(einj_init);
801 module_exit(einj_exit);
803 MODULE_AUTHOR("Huang Ying");
804 MODULE_DESCRIPTION("APEI Error INJection support");
805 MODULE_LICENSE("GPL");