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 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
50 struct set_error_type_with_address
{
56 u64 memory_address_range
;
60 SETWA_FLAGS_APICID
= 1,
62 SETWA_FLAGS_PCIE_SBDF
= 4,
66 * Vendor extensions for platform specific operations
68 struct vendor_error_type_extension
{
77 static u32 vendor_flags
;
78 static struct debugfs_blob_wrapper vendor_blob
;
79 static char vendor_dev
[64];
82 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
83 * EINJ table through an unpublished extension. Use with caution as
84 * most will ignore the parameter and make their own choice of address
85 * for error injection. This extension is used only if
86 * param_extension module parameter is specified.
88 struct einj_parameter
{
96 #define EINJ_OP_BUSY 0x1
97 #define EINJ_STATUS_SUCCESS 0x0
98 #define EINJ_STATUS_FAIL 0x1
99 #define EINJ_STATUS_INVAL 0x2
101 #define EINJ_TAB_ENTRY(tab) \
102 ((struct acpi_whea_header *)((char *)(tab) + \
103 sizeof(struct acpi_table_einj)))
105 static bool param_extension
;
106 module_param(param_extension
, bool, 0);
108 static struct acpi_table_einj
*einj_tab
;
110 static struct apei_resources einj_resources
;
112 static struct apei_exec_ins_type einj_ins_type
[] = {
113 [ACPI_EINJ_READ_REGISTER
] = {
114 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
115 .run
= apei_exec_read_register
,
117 [ACPI_EINJ_READ_REGISTER_VALUE
] = {
118 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
119 .run
= apei_exec_read_register_value
,
121 [ACPI_EINJ_WRITE_REGISTER
] = {
122 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
123 .run
= apei_exec_write_register
,
125 [ACPI_EINJ_WRITE_REGISTER_VALUE
] = {
126 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
127 .run
= apei_exec_write_register_value
,
131 .run
= apei_exec_noop
,
136 * Prevent EINJ interpreter to run simultaneously, because the
137 * corresponding firmware implementation may not work properly when
138 * invoked simultaneously.
140 static DEFINE_MUTEX(einj_mutex
);
142 static void *einj_param
;
145 static inline __u64
readq(volatile void __iomem
*addr
)
147 return ((__u64
)readl(addr
+4) << 32) + readl(addr
);
152 static inline void writeq(__u64 val
, volatile void __iomem
*addr
)
155 writel(val
>> 32, addr
+4);
159 static void einj_exec_ctx_init(struct apei_exec_context
*ctx
)
161 apei_exec_ctx_init(ctx
, einj_ins_type
, ARRAY_SIZE(einj_ins_type
),
162 EINJ_TAB_ENTRY(einj_tab
), einj_tab
->entries
);
165 static int __einj_get_available_error_type(u32
*type
)
167 struct apei_exec_context ctx
;
170 einj_exec_ctx_init(&ctx
);
171 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_ERROR_TYPE
);
174 *type
= apei_exec_ctx_get_output(&ctx
);
179 /* Get error injection capabilities of the platform */
180 static int einj_get_available_error_type(u32
*type
)
184 mutex_lock(&einj_mutex
);
185 rc
= __einj_get_available_error_type(type
);
186 mutex_unlock(&einj_mutex
);
191 static int einj_timedout(u64
*t
)
193 if ((s64
)*t
< SPIN_UNIT
) {
194 pr_warning(FW_WARN EINJ_PFX
195 "Firmware does not respond in time\n");
200 touch_nmi_watchdog();
204 static void check_vendor_extension(u64 paddr
,
205 struct set_error_type_with_address
*v5param
)
207 int offset
= readl(&v5param
->vendor_extension
);
208 struct vendor_error_type_extension
*v
;
213 v
= ioremap(paddr
+ offset
, sizeof(*v
));
216 sbdf
= readl(&v
->pcie_sbdf
);
217 sprintf(vendor_dev
, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
218 sbdf
>> 24, (sbdf
>> 16) & 0xff,
219 (sbdf
>> 11) & 0x1f, (sbdf
>> 8) & 0x7,
220 readw(&v
->vendor_id
), readw(&v
->device_id
),
225 static void *einj_get_parameter_address(void)
228 u64 paddrv4
= 0, paddrv5
= 0;
229 struct acpi_whea_header
*entry
;
231 entry
= EINJ_TAB_ENTRY(einj_tab
);
232 for (i
= 0; i
< einj_tab
->entries
; i
++) {
233 if (entry
->action
== ACPI_EINJ_SET_ERROR_TYPE
&&
234 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER
&&
235 entry
->register_region
.space_id
==
236 ACPI_ADR_SPACE_SYSTEM_MEMORY
)
237 memcpy(&paddrv4
, &entry
->register_region
.address
,
239 if (entry
->action
== ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS
&&
240 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER
&&
241 entry
->register_region
.space_id
==
242 ACPI_ADR_SPACE_SYSTEM_MEMORY
)
243 memcpy(&paddrv5
, &entry
->register_region
.address
,
248 struct set_error_type_with_address
*v5param
;
250 v5param
= ioremap(paddrv5
, sizeof(*v5param
));
253 check_vendor_extension(paddrv5
, v5param
);
258 struct einj_parameter
*v4param
;
260 v4param
= ioremap(paddrv4
, sizeof(*v4param
));
263 if (readq(&v4param
->reserved1
) || readq(&v4param
->reserved2
)) {
273 /* do sanity check to trigger table */
274 static int einj_check_trigger_header(struct acpi_einj_trigger
*trigger_tab
)
276 if (trigger_tab
->header_size
!= sizeof(struct acpi_einj_trigger
))
278 if (trigger_tab
->table_size
> PAGE_SIZE
||
279 trigger_tab
->table_size
< trigger_tab
->header_size
)
281 if (trigger_tab
->entry_count
!=
282 (trigger_tab
->table_size
- trigger_tab
->header_size
) /
283 sizeof(struct acpi_einj_entry
))
289 static struct acpi_generic_address
*einj_get_trigger_parameter_region(
290 struct acpi_einj_trigger
*trigger_tab
, u64 param1
, u64 param2
)
293 struct acpi_whea_header
*entry
;
295 entry
= (struct acpi_whea_header
*)
296 ((char *)trigger_tab
+ sizeof(struct acpi_einj_trigger
));
297 for (i
= 0; i
< trigger_tab
->entry_count
; i
++) {
298 if (entry
->action
== ACPI_EINJ_TRIGGER_ERROR
&&
299 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER_VALUE
&&
300 entry
->register_region
.space_id
==
301 ACPI_ADR_SPACE_SYSTEM_MEMORY
&&
302 (entry
->register_region
.address
& param2
) == (param1
& param2
))
303 return &entry
->register_region
;
309 /* Execute instructions in trigger error action table */
310 static int __einj_error_trigger(u64 trigger_paddr
, u32 type
,
311 u64 param1
, u64 param2
)
313 struct acpi_einj_trigger
*trigger_tab
= NULL
;
314 struct apei_exec_context trigger_ctx
;
315 struct apei_resources trigger_resources
;
316 struct acpi_whea_header
*trigger_entry
;
320 struct acpi_generic_address
*trigger_param_region
= NULL
;
322 r
= request_mem_region(trigger_paddr
, sizeof(*trigger_tab
),
323 "APEI EINJ Trigger Table");
326 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
327 (unsigned long long)trigger_paddr
,
328 (unsigned long long)trigger_paddr
+
329 sizeof(*trigger_tab
) - 1);
332 trigger_tab
= ioremap_cache(trigger_paddr
, sizeof(*trigger_tab
));
334 pr_err(EINJ_PFX
"Failed to map trigger table!\n");
337 rc
= einj_check_trigger_header(trigger_tab
);
339 pr_warning(FW_BUG EINJ_PFX
340 "The trigger error action table is invalid\n");
344 /* No action structures in the TRIGGER_ERROR table, nothing to do */
345 if (!trigger_tab
->entry_count
)
349 table_size
= trigger_tab
->table_size
;
350 r
= request_mem_region(trigger_paddr
+ sizeof(*trigger_tab
),
351 table_size
- sizeof(*trigger_tab
),
352 "APEI EINJ Trigger Table");
355 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
356 (unsigned long long)trigger_paddr
+ sizeof(*trigger_tab
),
357 (unsigned long long)trigger_paddr
+ table_size
- 1);
360 iounmap(trigger_tab
);
361 trigger_tab
= ioremap_cache(trigger_paddr
, table_size
);
363 pr_err(EINJ_PFX
"Failed to map trigger table!\n");
366 trigger_entry
= (struct acpi_whea_header
*)
367 ((char *)trigger_tab
+ sizeof(struct acpi_einj_trigger
));
368 apei_resources_init(&trigger_resources
);
369 apei_exec_ctx_init(&trigger_ctx
, einj_ins_type
,
370 ARRAY_SIZE(einj_ins_type
),
371 trigger_entry
, trigger_tab
->entry_count
);
372 rc
= apei_exec_collect_resources(&trigger_ctx
, &trigger_resources
);
375 rc
= apei_resources_sub(&trigger_resources
, &einj_resources
);
379 * Some firmware will access target address specified in
380 * param1 to trigger the error when injecting memory error.
381 * This will cause resource conflict with regular memory. So
382 * remove it from trigger table resources.
384 if (param_extension
&& (type
& 0x0038) && param2
) {
385 struct apei_resources addr_resources
;
386 apei_resources_init(&addr_resources
);
387 trigger_param_region
= einj_get_trigger_parameter_region(
388 trigger_tab
, param1
, param2
);
389 if (trigger_param_region
) {
390 rc
= apei_resources_add(&addr_resources
,
391 trigger_param_region
->address
,
392 trigger_param_region
->bit_width
/8, true);
395 rc
= apei_resources_sub(&trigger_resources
,
398 apei_resources_fini(&addr_resources
);
402 rc
= apei_resources_request(&trigger_resources
, "APEI EINJ Trigger");
405 rc
= apei_exec_pre_map_gars(&trigger_ctx
);
409 rc
= apei_exec_run(&trigger_ctx
, ACPI_EINJ_TRIGGER_ERROR
);
411 apei_exec_post_unmap_gars(&trigger_ctx
);
413 apei_resources_release(&trigger_resources
);
415 apei_resources_fini(&trigger_resources
);
417 release_mem_region(trigger_paddr
+ sizeof(*trigger_tab
),
418 table_size
- sizeof(*trigger_tab
));
420 release_mem_region(trigger_paddr
, sizeof(*trigger_tab
));
423 iounmap(trigger_tab
);
428 static int __einj_error_inject(u32 type
, u64 param1
, u64 param2
)
430 struct apei_exec_context ctx
;
431 u64 val
, trigger_paddr
, timeout
= FIRMWARE_TIMEOUT
;
434 einj_exec_ctx_init(&ctx
);
436 rc
= apei_exec_run_optional(&ctx
, ACPI_EINJ_BEGIN_OPERATION
);
439 apei_exec_ctx_set_input(&ctx
, type
);
441 struct set_error_type_with_address
*v5param
= einj_param
;
443 writel(type
, &v5param
->type
);
444 if (type
& 0x80000000) {
445 switch (vendor_flags
) {
446 case SETWA_FLAGS_APICID
:
447 writel(param1
, &v5param
->apicid
);
449 case SETWA_FLAGS_MEM
:
450 writeq(param1
, &v5param
->memory_address
);
451 writeq(param2
, &v5param
->memory_address_range
);
453 case SETWA_FLAGS_PCIE_SBDF
:
454 writel(param1
, &v5param
->pcie_sbdf
);
457 writel(vendor_flags
, &v5param
->flags
);
460 case ACPI_EINJ_PROCESSOR_CORRECTABLE
:
461 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE
:
462 case ACPI_EINJ_PROCESSOR_FATAL
:
463 writel(param1
, &v5param
->apicid
);
464 writel(SETWA_FLAGS_APICID
, &v5param
->flags
);
466 case ACPI_EINJ_MEMORY_CORRECTABLE
:
467 case ACPI_EINJ_MEMORY_UNCORRECTABLE
:
468 case ACPI_EINJ_MEMORY_FATAL
:
469 writeq(param1
, &v5param
->memory_address
);
470 writeq(param2
, &v5param
->memory_address_range
);
471 writel(SETWA_FLAGS_MEM
, &v5param
->flags
);
473 case ACPI_EINJ_PCIX_CORRECTABLE
:
474 case ACPI_EINJ_PCIX_UNCORRECTABLE
:
475 case ACPI_EINJ_PCIX_FATAL
:
476 writel(param1
, &v5param
->pcie_sbdf
);
477 writel(SETWA_FLAGS_PCIE_SBDF
, &v5param
->flags
);
482 rc
= apei_exec_run(&ctx
, ACPI_EINJ_SET_ERROR_TYPE
);
486 struct einj_parameter
*v4param
= einj_param
;
487 writeq(param1
, &v4param
->param1
);
488 writeq(param2
, &v4param
->param2
);
491 rc
= apei_exec_run(&ctx
, ACPI_EINJ_EXECUTE_OPERATION
);
495 rc
= apei_exec_run(&ctx
, ACPI_EINJ_CHECK_BUSY_STATUS
);
498 val
= apei_exec_ctx_get_output(&ctx
);
499 if (!(val
& EINJ_OP_BUSY
))
501 if (einj_timedout(&timeout
))
504 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_COMMAND_STATUS
);
507 val
= apei_exec_ctx_get_output(&ctx
);
508 if (val
!= EINJ_STATUS_SUCCESS
)
511 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_TRIGGER_TABLE
);
514 trigger_paddr
= apei_exec_ctx_get_output(&ctx
);
515 rc
= __einj_error_trigger(trigger_paddr
, type
, param1
, param2
);
518 rc
= apei_exec_run_optional(&ctx
, ACPI_EINJ_END_OPERATION
);
523 /* Inject the specified hardware error */
524 static int einj_error_inject(u32 type
, u64 param1
, u64 param2
)
528 mutex_lock(&einj_mutex
);
529 rc
= __einj_error_inject(type
, param1
, param2
);
530 mutex_unlock(&einj_mutex
);
535 static u32 error_type
;
536 static u64 error_param1
;
537 static u64 error_param2
;
538 static struct dentry
*einj_debug_dir
;
540 static int available_error_type_show(struct seq_file
*m
, void *v
)
543 u32 available_error_type
= 0;
545 rc
= einj_get_available_error_type(&available_error_type
);
548 if (available_error_type
& 0x0001)
549 seq_printf(m
, "0x00000001\tProcessor Correctable\n");
550 if (available_error_type
& 0x0002)
551 seq_printf(m
, "0x00000002\tProcessor Uncorrectable non-fatal\n");
552 if (available_error_type
& 0x0004)
553 seq_printf(m
, "0x00000004\tProcessor Uncorrectable fatal\n");
554 if (available_error_type
& 0x0008)
555 seq_printf(m
, "0x00000008\tMemory Correctable\n");
556 if (available_error_type
& 0x0010)
557 seq_printf(m
, "0x00000010\tMemory Uncorrectable non-fatal\n");
558 if (available_error_type
& 0x0020)
559 seq_printf(m
, "0x00000020\tMemory Uncorrectable fatal\n");
560 if (available_error_type
& 0x0040)
561 seq_printf(m
, "0x00000040\tPCI Express Correctable\n");
562 if (available_error_type
& 0x0080)
563 seq_printf(m
, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
564 if (available_error_type
& 0x0100)
565 seq_printf(m
, "0x00000100\tPCI Express Uncorrectable fatal\n");
566 if (available_error_type
& 0x0200)
567 seq_printf(m
, "0x00000200\tPlatform Correctable\n");
568 if (available_error_type
& 0x0400)
569 seq_printf(m
, "0x00000400\tPlatform Uncorrectable non-fatal\n");
570 if (available_error_type
& 0x0800)
571 seq_printf(m
, "0x00000800\tPlatform Uncorrectable fatal\n");
576 static int available_error_type_open(struct inode
*inode
, struct file
*file
)
578 return single_open(file
, available_error_type_show
, NULL
);
581 static const struct file_operations available_error_type_fops
= {
582 .open
= available_error_type_open
,
585 .release
= single_release
,
588 static int error_type_get(void *data
, u64
*val
)
595 static int error_type_set(void *data
, u64 val
)
598 u32 available_error_type
= 0;
602 * Vendor defined types have 0x80000000 bit set, and
603 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
605 vendor
= val
& 0x80000000;
606 tval
= val
& 0x7fffffff;
608 /* Only one error type can be specified */
609 if (tval
& (tval
- 1))
612 rc
= einj_get_available_error_type(&available_error_type
);
615 if (!(val
& available_error_type
))
623 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops
, error_type_get
,
624 error_type_set
, "0x%llx\n");
626 static int error_inject_set(void *data
, u64 val
)
631 return einj_error_inject(error_type
, error_param1
, error_param2
);
634 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops
, NULL
,
635 error_inject_set
, "%llu\n");
637 static int einj_check_table(struct acpi_table_einj
*einj_tab
)
639 if ((einj_tab
->header_length
!=
640 (sizeof(struct acpi_table_einj
) - sizeof(einj_tab
->header
)))
641 && (einj_tab
->header_length
!= sizeof(struct acpi_table_einj
)))
643 if (einj_tab
->header
.length
< sizeof(struct acpi_table_einj
))
645 if (einj_tab
->entries
!=
646 (einj_tab
->header
.length
- sizeof(struct acpi_table_einj
)) /
647 sizeof(struct acpi_einj_entry
))
653 static int __init
einj_init(void)
657 struct dentry
*fentry
;
658 struct apei_exec_context ctx
;
663 status
= acpi_get_table(ACPI_SIG_EINJ
, 0,
664 (struct acpi_table_header
**)&einj_tab
);
665 if (status
== AE_NOT_FOUND
)
667 else if (ACPI_FAILURE(status
)) {
668 const char *msg
= acpi_format_exception(status
);
669 pr_err(EINJ_PFX
"Failed to get table, %s\n", msg
);
673 rc
= einj_check_table(einj_tab
);
675 pr_warning(FW_BUG EINJ_PFX
"EINJ table is invalid\n");
680 einj_debug_dir
= debugfs_create_dir("einj", apei_get_debugfs_dir());
683 fentry
= debugfs_create_file("available_error_type", S_IRUSR
,
684 einj_debug_dir
, NULL
,
685 &available_error_type_fops
);
688 fentry
= debugfs_create_file("error_type", S_IRUSR
| S_IWUSR
,
689 einj_debug_dir
, NULL
, &error_type_fops
);
692 fentry
= debugfs_create_file("error_inject", S_IWUSR
,
693 einj_debug_dir
, NULL
, &error_inject_fops
);
697 apei_resources_init(&einj_resources
);
698 einj_exec_ctx_init(&ctx
);
699 rc
= apei_exec_collect_resources(&ctx
, &einj_resources
);
702 rc
= apei_resources_request(&einj_resources
, "APEI EINJ");
705 rc
= apei_exec_pre_map_gars(&ctx
);
709 einj_param
= einj_get_parameter_address();
710 if ((param_extension
|| acpi5
) && einj_param
) {
711 fentry
= debugfs_create_x64("param1", S_IRUSR
| S_IWUSR
,
712 einj_debug_dir
, &error_param1
);
715 fentry
= debugfs_create_x64("param2", S_IRUSR
| S_IWUSR
,
716 einj_debug_dir
, &error_param2
);
722 vendor_blob
.data
= vendor_dev
;
723 vendor_blob
.size
= strlen(vendor_dev
);
724 fentry
= debugfs_create_blob("vendor", S_IRUSR
,
725 einj_debug_dir
, &vendor_blob
);
728 fentry
= debugfs_create_x32("vendor_flags", S_IRUSR
| S_IWUSR
,
729 einj_debug_dir
, &vendor_flags
);
734 pr_info(EINJ_PFX
"Error INJection is initialized.\n");
741 apei_exec_post_unmap_gars(&ctx
);
743 apei_resources_release(&einj_resources
);
745 apei_resources_fini(&einj_resources
);
747 debugfs_remove_recursive(einj_debug_dir
);
752 static void __exit
einj_exit(void)
754 struct apei_exec_context ctx
;
758 einj_exec_ctx_init(&ctx
);
759 apei_exec_post_unmap_gars(&ctx
);
760 apei_resources_release(&einj_resources
);
761 apei_resources_fini(&einj_resources
);
762 debugfs_remove_recursive(einj_debug_dir
);
765 module_init(einj_init
);
766 module_exit(einj_exit
);
768 MODULE_AUTHOR("Huang Ying");
769 MODULE_DESCRIPTION("APEI Error INJection support");
770 MODULE_LICENSE("GPL");