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>
35 #include <acpi/acpi.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)
46 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
47 * EINJ table through an unpublished extension. Use with caution as
48 * most will ignore the parameter and make their own choice of address
49 * for error injection.
51 struct einj_parameter {
59 #define EINJ_OP_BUSY 0x1
60 #define EINJ_STATUS_SUCCESS 0x0
61 #define EINJ_STATUS_FAIL 0x1
62 #define EINJ_STATUS_INVAL 0x2
64 #define EINJ_TAB_ENTRY(tab) \
65 ((struct acpi_whea_header *)((char *)(tab) + \
66 sizeof(struct acpi_table_einj)))
68 static struct acpi_table_einj *einj_tab;
70 static struct apei_resources einj_resources;
72 static struct apei_exec_ins_type einj_ins_type[] = {
73 [ACPI_EINJ_READ_REGISTER] = {
74 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
75 .run = apei_exec_read_register,
77 [ACPI_EINJ_READ_REGISTER_VALUE] = {
78 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
79 .run = apei_exec_read_register_value,
81 [ACPI_EINJ_WRITE_REGISTER] = {
82 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
83 .run = apei_exec_write_register,
85 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
86 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
87 .run = apei_exec_write_register_value,
91 .run = apei_exec_noop,
96 * Prevent EINJ interpreter to run simultaneously, because the
97 * corresponding firmware implementation may not work properly when
98 * invoked simultaneously.
100 static DEFINE_MUTEX(einj_mutex);
102 static struct einj_parameter *einj_param;
105 static inline void writeq(__u64 val, volatile void __iomem *addr)
108 writel(val >> 32, addr+4);
112 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
114 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
115 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
118 static int __einj_get_available_error_type(u32 *type)
120 struct apei_exec_context ctx;
123 einj_exec_ctx_init(&ctx);
124 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
127 *type = apei_exec_ctx_get_output(&ctx);
132 /* Get error injection capabilities of the platform */
133 static int einj_get_available_error_type(u32 *type)
137 mutex_lock(&einj_mutex);
138 rc = __einj_get_available_error_type(type);
139 mutex_unlock(&einj_mutex);
144 static int einj_timedout(u64 *t)
146 if ((s64)*t < SPIN_UNIT) {
147 pr_warning(FW_WARN EINJ_PFX
148 "Firmware does not respond in time\n");
153 touch_nmi_watchdog();
157 static u64 einj_get_parameter_address(void)
161 struct acpi_whea_header *entry;
163 entry = EINJ_TAB_ENTRY(einj_tab);
164 for (i = 0; i < einj_tab->entries; i++) {
165 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
166 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
167 entry->register_region.space_id ==
168 ACPI_ADR_SPACE_SYSTEM_MEMORY)
169 memcpy(&paddr, &entry->register_region.address,
177 /* do sanity check to trigger table */
178 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
180 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
182 if (trigger_tab->table_size > PAGE_SIZE ||
183 trigger_tab->table_size <= trigger_tab->header_size)
185 if (trigger_tab->entry_count !=
186 (trigger_tab->table_size - trigger_tab->header_size) /
187 sizeof(struct acpi_einj_entry))
193 /* Execute instructions in trigger error action table */
194 static int __einj_error_trigger(u64 trigger_paddr)
196 struct acpi_einj_trigger *trigger_tab = NULL;
197 struct apei_exec_context trigger_ctx;
198 struct apei_resources trigger_resources;
199 struct acpi_whea_header *trigger_entry;
204 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
205 "APEI EINJ Trigger Table");
208 "Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
209 (unsigned long long)trigger_paddr,
210 (unsigned long long)trigger_paddr+sizeof(*trigger_tab));
213 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
215 pr_err(EINJ_PFX "Failed to map trigger table!\n");
218 rc = einj_check_trigger_header(trigger_tab);
220 pr_warning(FW_BUG EINJ_PFX
221 "The trigger error action table is invalid\n");
225 table_size = trigger_tab->table_size;
226 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
227 table_size - sizeof(*trigger_tab),
228 "APEI EINJ Trigger Table");
231 "Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
232 (unsigned long long)trigger_paddr+sizeof(*trigger_tab),
233 (unsigned long long)trigger_paddr + table_size);
236 iounmap(trigger_tab);
237 trigger_tab = ioremap_cache(trigger_paddr, table_size);
239 pr_err(EINJ_PFX "Failed to map trigger table!\n");
242 trigger_entry = (struct acpi_whea_header *)
243 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
244 apei_resources_init(&trigger_resources);
245 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
246 ARRAY_SIZE(einj_ins_type),
247 trigger_entry, trigger_tab->entry_count);
248 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
251 rc = apei_resources_sub(&trigger_resources, &einj_resources);
254 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
257 rc = apei_exec_pre_map_gars(&trigger_ctx);
261 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
263 apei_exec_post_unmap_gars(&trigger_ctx);
265 apei_resources_release(&trigger_resources);
267 apei_resources_fini(&trigger_resources);
269 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
270 table_size - sizeof(*trigger_tab));
272 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
275 iounmap(trigger_tab);
280 static int __einj_error_inject(u32 type, u64 param1, u64 param2)
282 struct apei_exec_context ctx;
283 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
286 einj_exec_ctx_init(&ctx);
288 rc = apei_exec_run(&ctx, ACPI_EINJ_BEGIN_OPERATION);
291 apei_exec_ctx_set_input(&ctx, type);
292 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
296 writeq(param1, &einj_param->param1);
297 writeq(param2, &einj_param->param2);
299 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
303 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
306 val = apei_exec_ctx_get_output(&ctx);
307 if (!(val & EINJ_OP_BUSY))
309 if (einj_timedout(&timeout))
312 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
315 val = apei_exec_ctx_get_output(&ctx);
316 if (val != EINJ_STATUS_SUCCESS)
319 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
322 trigger_paddr = apei_exec_ctx_get_output(&ctx);
323 rc = __einj_error_trigger(trigger_paddr);
326 rc = apei_exec_run(&ctx, ACPI_EINJ_END_OPERATION);
331 /* Inject the specified hardware error */
332 static int einj_error_inject(u32 type, u64 param1, u64 param2)
336 mutex_lock(&einj_mutex);
337 rc = __einj_error_inject(type, param1, param2);
338 mutex_unlock(&einj_mutex);
343 static u32 error_type;
344 static u64 error_param1;
345 static u64 error_param2;
346 static struct dentry *einj_debug_dir;
348 static int available_error_type_show(struct seq_file *m, void *v)
351 u32 available_error_type = 0;
353 rc = einj_get_available_error_type(&available_error_type);
356 if (available_error_type & 0x0001)
357 seq_printf(m, "0x00000001\tProcessor Correctable\n");
358 if (available_error_type & 0x0002)
359 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
360 if (available_error_type & 0x0004)
361 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
362 if (available_error_type & 0x0008)
363 seq_printf(m, "0x00000008\tMemory Correctable\n");
364 if (available_error_type & 0x0010)
365 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
366 if (available_error_type & 0x0020)
367 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
368 if (available_error_type & 0x0040)
369 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
370 if (available_error_type & 0x0080)
371 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
372 if (available_error_type & 0x0100)
373 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
374 if (available_error_type & 0x0200)
375 seq_printf(m, "0x00000200\tPlatform Correctable\n");
376 if (available_error_type & 0x0400)
377 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
378 if (available_error_type & 0x0800)
379 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
384 static int available_error_type_open(struct inode *inode, struct file *file)
386 return single_open(file, available_error_type_show, NULL);
389 static const struct file_operations available_error_type_fops = {
390 .open = available_error_type_open,
393 .release = single_release,
396 static int error_type_get(void *data, u64 *val)
403 static int error_type_set(void *data, u64 val)
406 u32 available_error_type = 0;
408 /* Only one error type can be specified */
411 rc = einj_get_available_error_type(&available_error_type);
414 if (!(val & available_error_type))
421 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
422 error_type_set, "0x%llx\n");
424 static int error_inject_set(void *data, u64 val)
429 return einj_error_inject(error_type, error_param1, error_param2);
432 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
433 error_inject_set, "%llu\n");
435 static int einj_check_table(struct acpi_table_einj *einj_tab)
437 if ((einj_tab->header_length !=
438 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
439 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
441 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
443 if (einj_tab->entries !=
444 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
445 sizeof(struct acpi_einj_entry))
451 static int __init einj_init(void)
456 struct dentry *fentry;
457 struct apei_exec_context ctx;
462 status = acpi_get_table(ACPI_SIG_EINJ, 0,
463 (struct acpi_table_header **)&einj_tab);
464 if (status == AE_NOT_FOUND) {
465 pr_info(EINJ_PFX "Table is not found!\n");
467 } else if (ACPI_FAILURE(status)) {
468 const char *msg = acpi_format_exception(status);
469 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
473 rc = einj_check_table(einj_tab);
475 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
480 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
483 fentry = debugfs_create_file("available_error_type", S_IRUSR,
484 einj_debug_dir, NULL,
485 &available_error_type_fops);
488 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
489 einj_debug_dir, NULL, &error_type_fops);
492 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
493 einj_debug_dir, &error_param1);
496 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
497 einj_debug_dir, &error_param2);
500 fentry = debugfs_create_file("error_inject", S_IWUSR,
501 einj_debug_dir, NULL, &error_inject_fops);
505 apei_resources_init(&einj_resources);
506 einj_exec_ctx_init(&ctx);
507 rc = apei_exec_collect_resources(&ctx, &einj_resources);
510 rc = apei_resources_request(&einj_resources, "APEI EINJ");
513 rc = apei_exec_pre_map_gars(&ctx);
516 param_paddr = einj_get_parameter_address();
518 einj_param = ioremap(param_paddr, sizeof(*einj_param));
524 pr_info(EINJ_PFX "Error INJection is initialized.\n");
529 apei_exec_post_unmap_gars(&ctx);
531 apei_resources_release(&einj_resources);
533 apei_resources_fini(&einj_resources);
535 debugfs_remove_recursive(einj_debug_dir);
540 static void __exit einj_exit(void)
542 struct apei_exec_context ctx;
546 einj_exec_ctx_init(&ctx);
547 apei_exec_post_unmap_gars(&ctx);
548 apei_resources_release(&einj_resources);
549 apei_resources_fini(&einj_resources);
550 debugfs_remove_recursive(einj_debug_dir);
553 module_init(einj_init);
554 module_exit(einj_exit);
556 MODULE_AUTHOR("Huang Ying");
557 MODULE_DESCRIPTION("APEI Error INJection support");
558 MODULE_LICENSE("GPL");