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 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)
45 #define EINJ_OP_BUSY 0x1
46 #define EINJ_STATUS_SUCCESS 0x0
47 #define EINJ_STATUS_FAIL 0x1
48 #define EINJ_STATUS_INVAL 0x2
50 #define EINJ_TAB_ENTRY(tab) \
51 ((struct acpi_whea_header *)((char *)(tab) + \
52 sizeof(struct acpi_table_einj)))
54 static struct acpi_table_einj *einj_tab;
56 static struct apei_resources einj_resources;
58 static struct apei_exec_ins_type einj_ins_type[] = {
59 [ACPI_EINJ_READ_REGISTER] = {
60 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
61 .run = apei_exec_read_register,
63 [ACPI_EINJ_READ_REGISTER_VALUE] = {
64 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
65 .run = apei_exec_read_register_value,
67 [ACPI_EINJ_WRITE_REGISTER] = {
68 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
69 .run = apei_exec_write_register,
71 [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
72 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
73 .run = apei_exec_write_register_value,
77 .run = apei_exec_noop,
82 * Prevent EINJ interpreter to run simultaneously, because the
83 * corresponding firmware implementation may not work properly when
84 * invoked simultaneously.
86 static DEFINE_MUTEX(einj_mutex);
88 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
90 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
91 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
94 static int __einj_get_available_error_type(u32 *type)
96 struct apei_exec_context ctx;
99 einj_exec_ctx_init(&ctx);
100 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
103 *type = apei_exec_ctx_get_output(&ctx);
108 /* Get error injection capabilities of the platform */
109 static int einj_get_available_error_type(u32 *type)
113 mutex_lock(&einj_mutex);
114 rc = __einj_get_available_error_type(type);
115 mutex_unlock(&einj_mutex);
120 static int einj_timedout(u64 *t)
122 if ((s64)*t < SPIN_UNIT) {
123 pr_warning(FW_WARN EINJ_PFX
124 "Firmware does not respond in time\n");
129 touch_nmi_watchdog();
133 /* do sanity check to trigger table */
134 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
136 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
138 if (trigger_tab->table_size > PAGE_SIZE ||
139 trigger_tab->table_size <= trigger_tab->header_size)
141 if (trigger_tab->entry_count !=
142 (trigger_tab->table_size - trigger_tab->header_size) /
143 sizeof(struct acpi_einj_entry))
149 /* Execute instructions in trigger error action table */
150 static int __einj_error_trigger(u64 trigger_paddr)
152 struct acpi_einj_trigger *trigger_tab = NULL;
153 struct apei_exec_context trigger_ctx;
154 struct apei_resources trigger_resources;
155 struct acpi_whea_header *trigger_entry;
160 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
161 "APEI EINJ Trigger Table");
164 "Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
165 (unsigned long long)trigger_paddr,
166 (unsigned long long)trigger_paddr+sizeof(*trigger_tab));
169 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
171 pr_err(EINJ_PFX "Failed to map trigger table!\n");
174 rc = einj_check_trigger_header(trigger_tab);
176 pr_warning(FW_BUG EINJ_PFX
177 "The trigger error action table is invalid\n");
181 table_size = trigger_tab->table_size;
182 r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
183 table_size - sizeof(*trigger_tab),
184 "APEI EINJ Trigger Table");
187 "Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
188 (unsigned long long)trigger_paddr+sizeof(*trigger_tab),
189 (unsigned long long)trigger_paddr + table_size);
192 iounmap(trigger_tab);
193 trigger_tab = ioremap_cache(trigger_paddr, table_size);
195 pr_err(EINJ_PFX "Failed to map trigger table!\n");
198 trigger_entry = (struct acpi_whea_header *)
199 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
200 apei_resources_init(&trigger_resources);
201 apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
202 ARRAY_SIZE(einj_ins_type),
203 trigger_entry, trigger_tab->entry_count);
204 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
207 rc = apei_resources_sub(&trigger_resources, &einj_resources);
210 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
213 rc = apei_exec_pre_map_gars(&trigger_ctx);
217 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
219 apei_exec_post_unmap_gars(&trigger_ctx);
221 apei_resources_release(&trigger_resources);
223 apei_resources_fini(&trigger_resources);
225 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
226 table_size - sizeof(*trigger_tab));
228 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
231 iounmap(trigger_tab);
236 static int __einj_error_inject(u32 type)
238 struct apei_exec_context ctx;
239 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
242 einj_exec_ctx_init(&ctx);
244 rc = apei_exec_run(&ctx, ACPI_EINJ_BEGIN_OPERATION);
247 apei_exec_ctx_set_input(&ctx, type);
248 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
251 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
255 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
258 val = apei_exec_ctx_get_output(&ctx);
259 if (!(val & EINJ_OP_BUSY))
261 if (einj_timedout(&timeout))
264 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
267 val = apei_exec_ctx_get_output(&ctx);
268 if (val != EINJ_STATUS_SUCCESS)
271 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
274 trigger_paddr = apei_exec_ctx_get_output(&ctx);
275 rc = __einj_error_trigger(trigger_paddr);
278 rc = apei_exec_run(&ctx, ACPI_EINJ_END_OPERATION);
283 /* Inject the specified hardware error */
284 static int einj_error_inject(u32 type)
288 mutex_lock(&einj_mutex);
289 rc = __einj_error_inject(type);
290 mutex_unlock(&einj_mutex);
295 static u32 error_type;
296 static struct dentry *einj_debug_dir;
298 static int available_error_type_show(struct seq_file *m, void *v)
301 u32 available_error_type = 0;
303 rc = einj_get_available_error_type(&available_error_type);
306 if (available_error_type & 0x0001)
307 seq_printf(m, "0x00000001\tProcessor Correctable\n");
308 if (available_error_type & 0x0002)
309 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
310 if (available_error_type & 0x0004)
311 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
312 if (available_error_type & 0x0008)
313 seq_printf(m, "0x00000008\tMemory Correctable\n");
314 if (available_error_type & 0x0010)
315 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
316 if (available_error_type & 0x0020)
317 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
318 if (available_error_type & 0x0040)
319 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
320 if (available_error_type & 0x0080)
321 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
322 if (available_error_type & 0x0100)
323 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
324 if (available_error_type & 0x0200)
325 seq_printf(m, "0x00000200\tPlatform Correctable\n");
326 if (available_error_type & 0x0400)
327 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
328 if (available_error_type & 0x0800)
329 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
334 static int available_error_type_open(struct inode *inode, struct file *file)
336 return single_open(file, available_error_type_show, NULL);
339 static const struct file_operations available_error_type_fops = {
340 .open = available_error_type_open,
343 .release = single_release,
346 static int error_type_get(void *data, u64 *val)
353 static int error_type_set(void *data, u64 val)
356 u32 available_error_type = 0;
358 /* Only one error type can be specified */
361 rc = einj_get_available_error_type(&available_error_type);
364 if (!(val & available_error_type))
371 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
372 error_type_set, "0x%llx\n");
374 static int error_inject_set(void *data, u64 val)
379 return einj_error_inject(error_type);
382 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
383 error_inject_set, "%llu\n");
385 static int einj_check_table(struct acpi_table_einj *einj_tab)
387 if (einj_tab->header_length != sizeof(struct acpi_table_einj))
389 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
391 if (einj_tab->entries !=
392 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
393 sizeof(struct acpi_einj_entry))
399 static int __init einj_init(void)
403 struct dentry *fentry;
404 struct apei_exec_context ctx;
409 status = acpi_get_table(ACPI_SIG_EINJ, 0,
410 (struct acpi_table_header **)&einj_tab);
411 if (status == AE_NOT_FOUND) {
412 pr_info(EINJ_PFX "Table is not found!\n");
414 } else if (ACPI_FAILURE(status)) {
415 const char *msg = acpi_format_exception(status);
416 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
420 rc = einj_check_table(einj_tab);
422 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
427 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
430 fentry = debugfs_create_file("available_error_type", S_IRUSR,
431 einj_debug_dir, NULL,
432 &available_error_type_fops);
435 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
436 einj_debug_dir, NULL, &error_type_fops);
439 fentry = debugfs_create_file("error_inject", S_IWUSR,
440 einj_debug_dir, NULL, &error_inject_fops);
444 apei_resources_init(&einj_resources);
445 einj_exec_ctx_init(&ctx);
446 rc = apei_exec_collect_resources(&ctx, &einj_resources);
449 rc = apei_resources_request(&einj_resources, "APEI EINJ");
452 rc = apei_exec_pre_map_gars(&ctx);
456 pr_info(EINJ_PFX "Error INJection is initialized.\n");
461 apei_resources_release(&einj_resources);
463 apei_resources_fini(&einj_resources);
465 debugfs_remove_recursive(einj_debug_dir);
470 static void __exit einj_exit(void)
472 struct apei_exec_context ctx;
474 einj_exec_ctx_init(&ctx);
475 apei_exec_post_unmap_gars(&ctx);
476 apei_resources_release(&einj_resources);
477 apei_resources_fini(&einj_resources);
478 debugfs_remove_recursive(einj_debug_dir);
481 module_init(einj_init);
482 module_exit(einj_exit);
484 MODULE_AUTHOR("Huang Ying");
485 MODULE_DESCRIPTION("APEI Error INJection support");
486 MODULE_LICENSE("GPL");