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
{
79 static u32 vendor_flags
;
80 static struct debugfs_blob_wrapper vendor_blob
;
81 static char vendor_dev
[64];
84 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
85 * EINJ table through an unpublished extension. Use with caution as
86 * most will ignore the parameter and make their own choice of address
87 * for error injection. This extension is used only if
88 * param_extension module parameter is specified.
90 struct einj_parameter
{
98 #define EINJ_OP_BUSY 0x1
99 #define EINJ_STATUS_SUCCESS 0x0
100 #define EINJ_STATUS_FAIL 0x1
101 #define EINJ_STATUS_INVAL 0x2
103 #define EINJ_TAB_ENTRY(tab) \
104 ((struct acpi_whea_header *)((char *)(tab) + \
105 sizeof(struct acpi_table_einj)))
107 static bool param_extension
;
108 module_param(param_extension
, bool, 0);
110 static struct acpi_table_einj
*einj_tab
;
112 static struct apei_resources einj_resources
;
114 static struct apei_exec_ins_type einj_ins_type
[] = {
115 [ACPI_EINJ_READ_REGISTER
] = {
116 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
117 .run
= apei_exec_read_register
,
119 [ACPI_EINJ_READ_REGISTER_VALUE
] = {
120 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
121 .run
= apei_exec_read_register_value
,
123 [ACPI_EINJ_WRITE_REGISTER
] = {
124 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
125 .run
= apei_exec_write_register
,
127 [ACPI_EINJ_WRITE_REGISTER_VALUE
] = {
128 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
129 .run
= apei_exec_write_register_value
,
133 .run
= apei_exec_noop
,
138 * Prevent EINJ interpreter to run simultaneously, because the
139 * corresponding firmware implementation may not work properly when
140 * invoked simultaneously.
142 static DEFINE_MUTEX(einj_mutex
);
144 static void *einj_param
;
146 static void einj_exec_ctx_init(struct apei_exec_context
*ctx
)
148 apei_exec_ctx_init(ctx
, einj_ins_type
, ARRAY_SIZE(einj_ins_type
),
149 EINJ_TAB_ENTRY(einj_tab
), einj_tab
->entries
);
152 static int __einj_get_available_error_type(u32
*type
)
154 struct apei_exec_context ctx
;
157 einj_exec_ctx_init(&ctx
);
158 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_ERROR_TYPE
);
161 *type
= apei_exec_ctx_get_output(&ctx
);
166 /* Get error injection capabilities of the platform */
167 static int einj_get_available_error_type(u32
*type
)
171 mutex_lock(&einj_mutex
);
172 rc
= __einj_get_available_error_type(type
);
173 mutex_unlock(&einj_mutex
);
178 static int einj_timedout(u64
*t
)
180 if ((s64
)*t
< SPIN_UNIT
) {
181 pr_warning(FW_WARN EINJ_PFX
182 "Firmware does not respond in time\n");
187 touch_nmi_watchdog();
191 static void check_vendor_extension(u64 paddr
,
192 struct set_error_type_with_address
*v5param
)
194 int offset
= v5param
->vendor_extension
;
195 struct vendor_error_type_extension
*v
;
200 v
= acpi_os_map_memory(paddr
+ offset
, sizeof(*v
));
204 sprintf(vendor_dev
, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
205 sbdf
>> 24, (sbdf
>> 16) & 0xff,
206 (sbdf
>> 11) & 0x1f, (sbdf
>> 8) & 0x7,
207 v
->vendor_id
, v
->device_id
, v
->rev_id
);
208 acpi_os_unmap_memory(v
, sizeof(*v
));
211 static void *einj_get_parameter_address(void)
214 u64 paddrv4
= 0, paddrv5
= 0;
215 struct acpi_whea_header
*entry
;
217 entry
= EINJ_TAB_ENTRY(einj_tab
);
218 for (i
= 0; i
< einj_tab
->entries
; i
++) {
219 if (entry
->action
== ACPI_EINJ_SET_ERROR_TYPE
&&
220 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER
&&
221 entry
->register_region
.space_id
==
222 ACPI_ADR_SPACE_SYSTEM_MEMORY
)
223 memcpy(&paddrv4
, &entry
->register_region
.address
,
225 if (entry
->action
== ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS
&&
226 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER
&&
227 entry
->register_region
.space_id
==
228 ACPI_ADR_SPACE_SYSTEM_MEMORY
)
229 memcpy(&paddrv5
, &entry
->register_region
.address
,
234 struct set_error_type_with_address
*v5param
;
236 v5param
= acpi_os_map_memory(paddrv5
, sizeof(*v5param
));
239 check_vendor_extension(paddrv5
, v5param
);
243 if (param_extension
&& paddrv4
) {
244 struct einj_parameter
*v4param
;
246 v4param
= acpi_os_map_memory(paddrv4
, sizeof(*v4param
));
249 if (v4param
->reserved1
|| v4param
->reserved2
) {
250 acpi_os_unmap_memory(v4param
, sizeof(*v4param
));
259 /* do sanity check to trigger table */
260 static int einj_check_trigger_header(struct acpi_einj_trigger
*trigger_tab
)
262 if (trigger_tab
->header_size
!= sizeof(struct acpi_einj_trigger
))
264 if (trigger_tab
->table_size
> PAGE_SIZE
||
265 trigger_tab
->table_size
< trigger_tab
->header_size
)
267 if (trigger_tab
->entry_count
!=
268 (trigger_tab
->table_size
- trigger_tab
->header_size
) /
269 sizeof(struct acpi_einj_entry
))
275 static struct acpi_generic_address
*einj_get_trigger_parameter_region(
276 struct acpi_einj_trigger
*trigger_tab
, u64 param1
, u64 param2
)
279 struct acpi_whea_header
*entry
;
281 entry
= (struct acpi_whea_header
*)
282 ((char *)trigger_tab
+ sizeof(struct acpi_einj_trigger
));
283 for (i
= 0; i
< trigger_tab
->entry_count
; i
++) {
284 if (entry
->action
== ACPI_EINJ_TRIGGER_ERROR
&&
285 entry
->instruction
== ACPI_EINJ_WRITE_REGISTER_VALUE
&&
286 entry
->register_region
.space_id
==
287 ACPI_ADR_SPACE_SYSTEM_MEMORY
&&
288 (entry
->register_region
.address
& param2
) == (param1
& param2
))
289 return &entry
->register_region
;
295 /* Execute instructions in trigger error action table */
296 static int __einj_error_trigger(u64 trigger_paddr
, u32 type
,
297 u64 param1
, u64 param2
)
299 struct acpi_einj_trigger
*trigger_tab
= NULL
;
300 struct apei_exec_context trigger_ctx
;
301 struct apei_resources trigger_resources
;
302 struct acpi_whea_header
*trigger_entry
;
306 struct acpi_generic_address
*trigger_param_region
= NULL
;
308 r
= request_mem_region(trigger_paddr
, sizeof(*trigger_tab
),
309 "APEI EINJ Trigger Table");
312 "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
313 (unsigned long long)trigger_paddr
,
314 (unsigned long long)trigger_paddr
+
315 sizeof(*trigger_tab
) - 1);
318 trigger_tab
= ioremap_cache(trigger_paddr
, sizeof(*trigger_tab
));
320 pr_err(EINJ_PFX
"Failed to map trigger table!\n");
323 rc
= einj_check_trigger_header(trigger_tab
);
325 pr_warning(FW_BUG EINJ_PFX
326 "The trigger error action table is invalid\n");
330 /* No action structures in the TRIGGER_ERROR table, nothing to do */
331 if (!trigger_tab
->entry_count
)
335 table_size
= trigger_tab
->table_size
;
336 r
= request_mem_region(trigger_paddr
+ sizeof(*trigger_tab
),
337 table_size
- sizeof(*trigger_tab
),
338 "APEI EINJ Trigger Table");
341 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
342 (unsigned long long)trigger_paddr
+ sizeof(*trigger_tab
),
343 (unsigned long long)trigger_paddr
+ table_size
- 1);
346 iounmap(trigger_tab
);
347 trigger_tab
= ioremap_cache(trigger_paddr
, table_size
);
349 pr_err(EINJ_PFX
"Failed to map trigger table!\n");
352 trigger_entry
= (struct acpi_whea_header
*)
353 ((char *)trigger_tab
+ sizeof(struct acpi_einj_trigger
));
354 apei_resources_init(&trigger_resources
);
355 apei_exec_ctx_init(&trigger_ctx
, einj_ins_type
,
356 ARRAY_SIZE(einj_ins_type
),
357 trigger_entry
, trigger_tab
->entry_count
);
358 rc
= apei_exec_collect_resources(&trigger_ctx
, &trigger_resources
);
361 rc
= apei_resources_sub(&trigger_resources
, &einj_resources
);
365 * Some firmware will access target address specified in
366 * param1 to trigger the error when injecting memory error.
367 * This will cause resource conflict with regular memory. So
368 * remove it from trigger table resources.
370 if ((param_extension
|| acpi5
) && (type
& 0x0038) && param2
) {
371 struct apei_resources addr_resources
;
372 apei_resources_init(&addr_resources
);
373 trigger_param_region
= einj_get_trigger_parameter_region(
374 trigger_tab
, param1
, param2
);
375 if (trigger_param_region
) {
376 rc
= apei_resources_add(&addr_resources
,
377 trigger_param_region
->address
,
378 trigger_param_region
->bit_width
/8, true);
381 rc
= apei_resources_sub(&trigger_resources
,
384 apei_resources_fini(&addr_resources
);
388 rc
= apei_resources_request(&trigger_resources
, "APEI EINJ Trigger");
391 rc
= apei_exec_pre_map_gars(&trigger_ctx
);
395 rc
= apei_exec_run(&trigger_ctx
, ACPI_EINJ_TRIGGER_ERROR
);
397 apei_exec_post_unmap_gars(&trigger_ctx
);
399 apei_resources_release(&trigger_resources
);
401 apei_resources_fini(&trigger_resources
);
403 release_mem_region(trigger_paddr
+ sizeof(*trigger_tab
),
404 table_size
- sizeof(*trigger_tab
));
406 release_mem_region(trigger_paddr
, sizeof(*trigger_tab
));
409 iounmap(trigger_tab
);
414 static int __einj_error_inject(u32 type
, u64 param1
, u64 param2
)
416 struct apei_exec_context ctx
;
417 u64 val
, trigger_paddr
, timeout
= FIRMWARE_TIMEOUT
;
420 einj_exec_ctx_init(&ctx
);
422 rc
= apei_exec_run_optional(&ctx
, ACPI_EINJ_BEGIN_OPERATION
);
425 apei_exec_ctx_set_input(&ctx
, type
);
427 struct set_error_type_with_address
*v5param
= einj_param
;
429 v5param
->type
= type
;
430 if (type
& 0x80000000) {
431 switch (vendor_flags
) {
432 case SETWA_FLAGS_APICID
:
433 v5param
->apicid
= param1
;
435 case SETWA_FLAGS_MEM
:
436 v5param
->memory_address
= param1
;
437 v5param
->memory_address_range
= param2
;
439 case SETWA_FLAGS_PCIE_SBDF
:
440 v5param
->pcie_sbdf
= param1
;
443 v5param
->flags
= vendor_flags
;
446 case ACPI_EINJ_PROCESSOR_CORRECTABLE
:
447 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE
:
448 case ACPI_EINJ_PROCESSOR_FATAL
:
449 v5param
->apicid
= param1
;
450 v5param
->flags
= SETWA_FLAGS_APICID
;
452 case ACPI_EINJ_MEMORY_CORRECTABLE
:
453 case ACPI_EINJ_MEMORY_UNCORRECTABLE
:
454 case ACPI_EINJ_MEMORY_FATAL
:
455 v5param
->memory_address
= param1
;
456 v5param
->memory_address_range
= param2
;
457 v5param
->flags
= SETWA_FLAGS_MEM
;
459 case ACPI_EINJ_PCIX_CORRECTABLE
:
460 case ACPI_EINJ_PCIX_UNCORRECTABLE
:
461 case ACPI_EINJ_PCIX_FATAL
:
462 v5param
->pcie_sbdf
= param1
;
463 v5param
->flags
= SETWA_FLAGS_PCIE_SBDF
;
468 rc
= apei_exec_run(&ctx
, ACPI_EINJ_SET_ERROR_TYPE
);
472 struct einj_parameter
*v4param
= einj_param
;
473 v4param
->param1
= param1
;
474 v4param
->param2
= param2
;
477 rc
= apei_exec_run(&ctx
, ACPI_EINJ_EXECUTE_OPERATION
);
481 rc
= apei_exec_run(&ctx
, ACPI_EINJ_CHECK_BUSY_STATUS
);
484 val
= apei_exec_ctx_get_output(&ctx
);
485 if (!(val
& EINJ_OP_BUSY
))
487 if (einj_timedout(&timeout
))
490 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_COMMAND_STATUS
);
493 val
= apei_exec_ctx_get_output(&ctx
);
494 if (val
!= EINJ_STATUS_SUCCESS
)
497 rc
= apei_exec_run(&ctx
, ACPI_EINJ_GET_TRIGGER_TABLE
);
500 trigger_paddr
= apei_exec_ctx_get_output(&ctx
);
501 if (notrigger
== 0) {
502 rc
= __einj_error_trigger(trigger_paddr
, type
, param1
, param2
);
506 rc
= apei_exec_run_optional(&ctx
, ACPI_EINJ_END_OPERATION
);
511 /* Inject the specified hardware error */
512 static int einj_error_inject(u32 type
, u64 param1
, u64 param2
)
516 mutex_lock(&einj_mutex
);
517 rc
= __einj_error_inject(type
, param1
, param2
);
518 mutex_unlock(&einj_mutex
);
523 static u32 error_type
;
524 static u64 error_param1
;
525 static u64 error_param2
;
526 static struct dentry
*einj_debug_dir
;
528 static int available_error_type_show(struct seq_file
*m
, void *v
)
531 u32 available_error_type
= 0;
533 rc
= einj_get_available_error_type(&available_error_type
);
536 if (available_error_type
& 0x0001)
537 seq_printf(m
, "0x00000001\tProcessor Correctable\n");
538 if (available_error_type
& 0x0002)
539 seq_printf(m
, "0x00000002\tProcessor Uncorrectable non-fatal\n");
540 if (available_error_type
& 0x0004)
541 seq_printf(m
, "0x00000004\tProcessor Uncorrectable fatal\n");
542 if (available_error_type
& 0x0008)
543 seq_printf(m
, "0x00000008\tMemory Correctable\n");
544 if (available_error_type
& 0x0010)
545 seq_printf(m
, "0x00000010\tMemory Uncorrectable non-fatal\n");
546 if (available_error_type
& 0x0020)
547 seq_printf(m
, "0x00000020\tMemory Uncorrectable fatal\n");
548 if (available_error_type
& 0x0040)
549 seq_printf(m
, "0x00000040\tPCI Express Correctable\n");
550 if (available_error_type
& 0x0080)
551 seq_printf(m
, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
552 if (available_error_type
& 0x0100)
553 seq_printf(m
, "0x00000100\tPCI Express Uncorrectable fatal\n");
554 if (available_error_type
& 0x0200)
555 seq_printf(m
, "0x00000200\tPlatform Correctable\n");
556 if (available_error_type
& 0x0400)
557 seq_printf(m
, "0x00000400\tPlatform Uncorrectable non-fatal\n");
558 if (available_error_type
& 0x0800)
559 seq_printf(m
, "0x00000800\tPlatform Uncorrectable fatal\n");
564 static int available_error_type_open(struct inode
*inode
, struct file
*file
)
566 return single_open(file
, available_error_type_show
, NULL
);
569 static const struct file_operations available_error_type_fops
= {
570 .open
= available_error_type_open
,
573 .release
= single_release
,
576 static int error_type_get(void *data
, u64
*val
)
583 static int error_type_set(void *data
, u64 val
)
586 u32 available_error_type
= 0;
590 * Vendor defined types have 0x80000000 bit set, and
591 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
593 vendor
= val
& 0x80000000;
594 tval
= val
& 0x7fffffff;
596 /* Only one error type can be specified */
597 if (tval
& (tval
- 1))
600 rc
= einj_get_available_error_type(&available_error_type
);
603 if (!(val
& available_error_type
))
611 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops
, error_type_get
,
612 error_type_set
, "0x%llx\n");
614 static int error_inject_set(void *data
, u64 val
)
619 return einj_error_inject(error_type
, error_param1
, error_param2
);
622 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops
, NULL
,
623 error_inject_set
, "%llu\n");
625 static int einj_check_table(struct acpi_table_einj
*einj_tab
)
627 if ((einj_tab
->header_length
!=
628 (sizeof(struct acpi_table_einj
) - sizeof(einj_tab
->header
)))
629 && (einj_tab
->header_length
!= sizeof(struct acpi_table_einj
)))
631 if (einj_tab
->header
.length
< sizeof(struct acpi_table_einj
))
633 if (einj_tab
->entries
!=
634 (einj_tab
->header
.length
- sizeof(struct acpi_table_einj
)) /
635 sizeof(struct acpi_einj_entry
))
641 static int __init
einj_init(void)
645 struct dentry
*fentry
;
646 struct apei_exec_context ctx
;
651 status
= acpi_get_table(ACPI_SIG_EINJ
, 0,
652 (struct acpi_table_header
**)&einj_tab
);
653 if (status
== AE_NOT_FOUND
)
655 else if (ACPI_FAILURE(status
)) {
656 const char *msg
= acpi_format_exception(status
);
657 pr_err(EINJ_PFX
"Failed to get table, %s\n", msg
);
661 rc
= einj_check_table(einj_tab
);
663 pr_warning(FW_BUG EINJ_PFX
"EINJ table is invalid\n");
668 einj_debug_dir
= debugfs_create_dir("einj", apei_get_debugfs_dir());
671 fentry
= debugfs_create_file("available_error_type", S_IRUSR
,
672 einj_debug_dir
, NULL
,
673 &available_error_type_fops
);
676 fentry
= debugfs_create_file("error_type", S_IRUSR
| S_IWUSR
,
677 einj_debug_dir
, NULL
, &error_type_fops
);
680 fentry
= debugfs_create_file("error_inject", S_IWUSR
,
681 einj_debug_dir
, NULL
, &error_inject_fops
);
685 apei_resources_init(&einj_resources
);
686 einj_exec_ctx_init(&ctx
);
687 rc
= apei_exec_collect_resources(&ctx
, &einj_resources
);
690 rc
= apei_resources_request(&einj_resources
, "APEI EINJ");
693 rc
= apei_exec_pre_map_gars(&ctx
);
697 einj_param
= einj_get_parameter_address();
698 if ((param_extension
|| acpi5
) && einj_param
) {
699 fentry
= debugfs_create_x64("param1", S_IRUSR
| S_IWUSR
,
700 einj_debug_dir
, &error_param1
);
703 fentry
= debugfs_create_x64("param2", S_IRUSR
| S_IWUSR
,
704 einj_debug_dir
, &error_param2
);
708 fentry
= debugfs_create_x32("notrigger", S_IRUSR
| S_IWUSR
,
709 einj_debug_dir
, ¬rigger
);
715 vendor_blob
.data
= vendor_dev
;
716 vendor_blob
.size
= strlen(vendor_dev
);
717 fentry
= debugfs_create_blob("vendor", S_IRUSR
,
718 einj_debug_dir
, &vendor_blob
);
721 fentry
= debugfs_create_x32("vendor_flags", S_IRUSR
| S_IWUSR
,
722 einj_debug_dir
, &vendor_flags
);
727 pr_info(EINJ_PFX
"Error INJection is initialized.\n");
733 acpi_size size
= (acpi5
) ?
734 sizeof(struct set_error_type_with_address
) :
735 sizeof(struct einj_parameter
);
737 acpi_os_unmap_memory(einj_param
, size
);
739 apei_exec_post_unmap_gars(&ctx
);
741 apei_resources_release(&einj_resources
);
743 apei_resources_fini(&einj_resources
);
745 debugfs_remove_recursive(einj_debug_dir
);
750 static void __exit
einj_exit(void)
752 struct apei_exec_context ctx
;
755 acpi_size size
= (acpi5
) ?
756 sizeof(struct set_error_type_with_address
) :
757 sizeof(struct einj_parameter
);
759 acpi_os_unmap_memory(einj_param
, size
);
761 einj_exec_ctx_init(&ctx
);
762 apei_exec_post_unmap_gars(&ctx
);
763 apei_resources_release(&einj_resources
);
764 apei_resources_fini(&einj_resources
);
765 debugfs_remove_recursive(einj_debug_dir
);
768 module_init(einj_init
);
769 module_exit(einj_exit
);
771 MODULE_AUTHOR("Huang Ying");
772 MODULE_DESCRIPTION("APEI Error INJection support");
773 MODULE_LICENSE("GPL");