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 miliseconds */
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;
104 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
106 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
107 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
110 static int __einj_get_available_error_type(u32 *type)
112 struct apei_exec_context ctx;
115 einj_exec_ctx_init(&ctx);
116 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
119 *type = apei_exec_ctx_get_output(&ctx);
124 /* Get error injection capabilities of the platform */
125 static int einj_get_available_error_type(u32 *type)
129 mutex_lock(&einj_mutex);
130 rc = __einj_get_available_error_type(type);
131 mutex_unlock(&einj_mutex);
136 static int einj_timedout(u64 *t)
138 if ((s64)*t < SPIN_UNIT) {
139 pr_warning(FW_WARN EINJ_PFX
140 "Firmware does not respond in time\n");
145 touch_nmi_watchdog();
149 static u64 einj_get_parameter_address(void)
153 struct acpi_whea_header *entry;
155 entry = EINJ_TAB_ENTRY(einj_tab);
156 for (i = 0; i < einj_tab->entries; i++) {
157 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
158 entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
159 entry->register_region.space_id ==
160 ACPI_ADR_SPACE_SYSTEM_MEMORY)
161 memcpy(&paddr, &entry->register_region.address,
169 /* do sanity check to trigger table */
170 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
172 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
174 if (trigger_tab->table_size > PAGE_SIZE ||
175 trigger_tab->table_size <= trigger_tab->header_size)
177 if (trigger_tab->entry_count !=
178 (trigger_tab->table_size - trigger_tab->header_size) /
179 sizeof(struct acpi_einj_entry))
185 /* Execute instructions in trigger error action table */
186 static int __einj_error_trigger(u64 trigger_paddr)
188 struct acpi_einj_trigger *trigger_tab = NULL;
189 struct apei_exec_context trigger_ctx;
190 struct apei_resources trigger_resources;
191 struct acpi_whea_header *trigger_entry;
196 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
197 "APEI EINJ Trigger Table");
200 "Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
201 (unsigned long long)trigger_paddr,
202 (unsigned long long)trigger_paddr+sizeof(*trigger_tab));
205 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
207 pr_err(EINJ_PFX "Failed to map trigger table!\n");
210 rc = einj_check_trigger_header(trigger_tab);
212 pr_warning(FW_BUG EINJ_PFX
213 "The trigger error action table is invalid\n");
217 table_size = trigger_tab->table_size;
218 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
219 table_size - sizeof(*trigger_tab),
220 "APEI EINJ Trigger Table");
223 "Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
224 (unsigned long long)trigger_paddr+sizeof(*trigger_tab),
225 (unsigned long long)trigger_paddr + table_size);
228 iounmap(trigger_tab);
229 trigger_tab = ioremap_cache(trigger_paddr, table_size);
231 pr_err(EINJ_PFX "Failed to map trigger table!\n");
234 trigger_entry = (struct acpi_whea_header *)
235 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
236 apei_resources_init(&trigger_resources);
237 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
238 ARRAY_SIZE(einj_ins_type),
239 trigger_entry, trigger_tab->entry_count);
240 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
243 rc = apei_resources_sub(&trigger_resources, &einj_resources);
246 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
249 rc = apei_exec_pre_map_gars(&trigger_ctx);
253 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
255 apei_exec_post_unmap_gars(&trigger_ctx);
257 apei_resources_release(&trigger_resources);
259 apei_resources_fini(&trigger_resources);
261 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
262 table_size - sizeof(*trigger_tab));
264 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
267 iounmap(trigger_tab);
272 static int __einj_error_inject(u32 type, u64 param1, u64 param2)
274 struct apei_exec_context ctx;
275 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
278 einj_exec_ctx_init(&ctx);
280 rc = apei_exec_run(&ctx, ACPI_EINJ_BEGIN_OPERATION);
283 apei_exec_ctx_set_input(&ctx, type);
284 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
288 writeq(param1, &einj_param->param1);
289 writeq(param2, &einj_param->param2);
291 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
295 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
298 val = apei_exec_ctx_get_output(&ctx);
299 if (!(val & EINJ_OP_BUSY))
301 if (einj_timedout(&timeout))
304 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
307 val = apei_exec_ctx_get_output(&ctx);
308 if (val != EINJ_STATUS_SUCCESS)
311 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
314 trigger_paddr = apei_exec_ctx_get_output(&ctx);
315 rc = __einj_error_trigger(trigger_paddr);
318 rc = apei_exec_run(&ctx, ACPI_EINJ_END_OPERATION);
323 /* Inject the specified hardware error */
324 static int einj_error_inject(u32 type, u64 param1, u64 param2)
328 mutex_lock(&einj_mutex);
329 rc = __einj_error_inject(type, param1, param2);
330 mutex_unlock(&einj_mutex);
335 static u32 error_type;
336 static u64 error_param1;
337 static u64 error_param2;
338 static struct dentry *einj_debug_dir;
340 static int available_error_type_show(struct seq_file *m, void *v)
343 u32 available_error_type = 0;
345 rc = einj_get_available_error_type(&available_error_type);
348 if (available_error_type & 0x0001)
349 seq_printf(m, "0x00000001\tProcessor Correctable\n");
350 if (available_error_type & 0x0002)
351 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
352 if (available_error_type & 0x0004)
353 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
354 if (available_error_type & 0x0008)
355 seq_printf(m, "0x00000008\tMemory Correctable\n");
356 if (available_error_type & 0x0010)
357 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
358 if (available_error_type & 0x0020)
359 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
360 if (available_error_type & 0x0040)
361 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
362 if (available_error_type & 0x0080)
363 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
364 if (available_error_type & 0x0100)
365 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
366 if (available_error_type & 0x0200)
367 seq_printf(m, "0x00000200\tPlatform Correctable\n");
368 if (available_error_type & 0x0400)
369 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
370 if (available_error_type & 0x0800)
371 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
376 static int available_error_type_open(struct inode *inode, struct file *file)
378 return single_open(file, available_error_type_show, NULL);
381 static const struct file_operations available_error_type_fops = {
382 .open = available_error_type_open,
385 .release = single_release,
388 static int error_type_get(void *data, u64 *val)
395 static int error_type_set(void *data, u64 val)
398 u32 available_error_type = 0;
400 /* Only one error type can be specified */
403 rc = einj_get_available_error_type(&available_error_type);
406 if (!(val & available_error_type))
413 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
414 error_type_set, "0x%llx\n");
416 static int error_inject_set(void *data, u64 val)
421 return einj_error_inject(error_type, error_param1, error_param2);
424 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
425 error_inject_set, "%llu\n");
427 static int einj_check_table(struct acpi_table_einj *einj_tab)
429 if (einj_tab->header_length != sizeof(struct acpi_table_einj))
431 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
433 if (einj_tab->entries !=
434 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
435 sizeof(struct acpi_einj_entry))
441 static int __init einj_init(void)
446 struct dentry *fentry;
447 struct apei_exec_context ctx;
452 status = acpi_get_table(ACPI_SIG_EINJ, 0,
453 (struct acpi_table_header **)&einj_tab);
454 if (status == AE_NOT_FOUND) {
455 pr_info(EINJ_PFX "Table is not found!\n");
457 } else if (ACPI_FAILURE(status)) {
458 const char *msg = acpi_format_exception(status);
459 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
463 rc = einj_check_table(einj_tab);
465 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
470 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
473 fentry = debugfs_create_file("available_error_type", S_IRUSR,
474 einj_debug_dir, NULL,
475 &available_error_type_fops);
478 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
479 einj_debug_dir, NULL, &error_type_fops);
482 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
483 einj_debug_dir, &error_param1);
486 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
487 einj_debug_dir, &error_param2);
490 fentry = debugfs_create_file("error_inject", S_IWUSR,
491 einj_debug_dir, NULL, &error_inject_fops);
495 apei_resources_init(&einj_resources);
496 einj_exec_ctx_init(&ctx);
497 rc = apei_exec_collect_resources(&ctx, &einj_resources);
500 rc = apei_resources_request(&einj_resources, "APEI EINJ");
503 rc = apei_exec_pre_map_gars(&ctx);
506 param_paddr = einj_get_parameter_address();
508 einj_param = ioremap(param_paddr, sizeof(*einj_param));
514 pr_info(EINJ_PFX "Error INJection is initialized.\n");
519 apei_exec_post_unmap_gars(&ctx);
521 apei_resources_release(&einj_resources);
523 apei_resources_fini(&einj_resources);
525 debugfs_remove_recursive(einj_debug_dir);
530 static void __exit einj_exit(void)
532 struct apei_exec_context ctx;
536 einj_exec_ctx_init(&ctx);
537 apei_exec_post_unmap_gars(&ctx);
538 apei_resources_release(&einj_resources);
539 apei_resources_fini(&einj_resources);
540 debugfs_remove_recursive(einj_debug_dir);
543 module_init(einj_init);
544 module_exit(einj_exit);
546 MODULE_AUTHOR("Huang Ying");
547 MODULE_DESCRIPTION("APEI Error INJection support");
548 MODULE_LICENSE("GPL");