2 * apei-base.c - ACPI Platform Error Interface (APEI) supporting
5 * APEI allows to report errors (for example from the chipset) to the
6 * the operating system. This improves NMI handling especially. In
7 * addition it supports error serialization and error injection.
9 * For more information about APEI, please refer to ACPI Specification
10 * version 4.0, chapter 17.
12 * This file has Common functions used by more than one APEI table,
13 * including framework of interpreter for ERST and EINJ; resource
14 * management for APEI registers.
16 * Copyright (C) 2009, Intel Corp.
17 * Author: Huang Ying <ying.huang@intel.com>
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License version
21 * 2 as published by the Free Software Foundation.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/acpi.h>
33 #include <linux/slab.h>
35 #include <linux/kref.h>
36 #include <linux/rculist.h>
37 #include <linux/interrupt.h>
38 #include <linux/debugfs.h>
39 #include <asm/unaligned.h>
41 #include "apei-internal.h"
43 #define APEI_PFX "APEI: "
46 * APEI ERST (Error Record Serialization Table) and EINJ (Error
47 * INJection) interpreter framework.
50 #define APEI_EXEC_PRESERVE_REGISTER 0x1
52 void apei_exec_ctx_init(struct apei_exec_context
*ctx
,
53 struct apei_exec_ins_type
*ins_table
,
55 struct acpi_whea_header
*action_table
,
58 ctx
->ins_table
= ins_table
;
59 ctx
->instructions
= instructions
;
60 ctx
->action_table
= action_table
;
61 ctx
->entries
= entries
;
63 EXPORT_SYMBOL_GPL(apei_exec_ctx_init
);
65 int __apei_exec_read_register(struct acpi_whea_header
*entry
, u64
*val
)
69 rc
= apei_read(val
, &entry
->register_region
);
72 *val
>>= entry
->register_region
.bit_offset
;
78 int apei_exec_read_register(struct apei_exec_context
*ctx
,
79 struct acpi_whea_header
*entry
)
84 rc
= __apei_exec_read_register(entry
, &val
);
91 EXPORT_SYMBOL_GPL(apei_exec_read_register
);
93 int apei_exec_read_register_value(struct apei_exec_context
*ctx
,
94 struct acpi_whea_header
*entry
)
98 rc
= apei_exec_read_register(ctx
, entry
);
101 ctx
->value
= (ctx
->value
== entry
->value
);
105 EXPORT_SYMBOL_GPL(apei_exec_read_register_value
);
107 int __apei_exec_write_register(struct acpi_whea_header
*entry
, u64 val
)
112 val
<<= entry
->register_region
.bit_offset
;
113 if (entry
->flags
& APEI_EXEC_PRESERVE_REGISTER
) {
115 rc
= apei_read(&valr
, &entry
->register_region
);
118 valr
&= ~(entry
->mask
<< entry
->register_region
.bit_offset
);
121 rc
= apei_write(val
, &entry
->register_region
);
126 int apei_exec_write_register(struct apei_exec_context
*ctx
,
127 struct acpi_whea_header
*entry
)
129 return __apei_exec_write_register(entry
, ctx
->value
);
131 EXPORT_SYMBOL_GPL(apei_exec_write_register
);
133 int apei_exec_write_register_value(struct apei_exec_context
*ctx
,
134 struct acpi_whea_header
*entry
)
138 ctx
->value
= entry
->value
;
139 rc
= apei_exec_write_register(ctx
, entry
);
143 EXPORT_SYMBOL_GPL(apei_exec_write_register_value
);
145 int apei_exec_noop(struct apei_exec_context
*ctx
,
146 struct acpi_whea_header
*entry
)
150 EXPORT_SYMBOL_GPL(apei_exec_noop
);
153 * Interpret the specified action. Go through whole action table,
154 * execute all instructions belong to the action.
156 int __apei_exec_run(struct apei_exec_context
*ctx
, u8 action
,
161 struct acpi_whea_header
*entry
;
162 apei_exec_ins_func_t run
;
167 * "ip" is the instruction pointer of current instruction,
168 * "ctx->ip" specifies the next instruction to executed,
169 * instruction "run" function may change the "ctx->ip" to
170 * implement "goto" semantics.
174 for (i
= 0; i
< ctx
->entries
; i
++) {
175 entry
= &ctx
->action_table
[i
];
176 if (entry
->action
!= action
)
179 if (entry
->instruction
>= ctx
->instructions
||
180 !ctx
->ins_table
[entry
->instruction
].run
) {
181 pr_warning(FW_WARN APEI_PFX
182 "Invalid action table, unknown instruction type: %d\n",
186 run
= ctx
->ins_table
[entry
->instruction
].run
;
187 rc
= run(ctx
, entry
);
190 else if (rc
!= APEI_EXEC_SET_IP
)
198 return !optional
&& rc
< 0 ? rc
: 0;
200 EXPORT_SYMBOL_GPL(__apei_exec_run
);
202 typedef int (*apei_exec_entry_func_t
)(struct apei_exec_context
*ctx
,
203 struct acpi_whea_header
*entry
,
206 static int apei_exec_for_each_entry(struct apei_exec_context
*ctx
,
207 apei_exec_entry_func_t func
,
213 struct acpi_whea_header
*entry
;
214 struct apei_exec_ins_type
*ins_table
= ctx
->ins_table
;
216 for (i
= 0; i
< ctx
->entries
; i
++) {
217 entry
= ctx
->action_table
+ i
;
218 ins
= entry
->instruction
;
221 if (ins
>= ctx
->instructions
|| !ins_table
[ins
].run
) {
222 pr_warning(FW_WARN APEI_PFX
223 "Invalid action table, unknown instruction type: %d\n",
227 rc
= func(ctx
, entry
, data
);
235 static int pre_map_gar_callback(struct apei_exec_context
*ctx
,
236 struct acpi_whea_header
*entry
,
239 u8 ins
= entry
->instruction
;
241 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
242 return apei_map_generic_address(&entry
->register_region
);
248 * Pre-map all GARs in action table to make it possible to access them
251 int apei_exec_pre_map_gars(struct apei_exec_context
*ctx
)
255 rc
= apei_exec_for_each_entry(ctx
, pre_map_gar_callback
,
258 struct apei_exec_context ctx_unmap
;
259 memcpy(&ctx_unmap
, ctx
, sizeof(*ctx
));
260 ctx_unmap
.entries
= end
;
261 apei_exec_post_unmap_gars(&ctx_unmap
);
266 EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars
);
268 static int post_unmap_gar_callback(struct apei_exec_context
*ctx
,
269 struct acpi_whea_header
*entry
,
272 u8 ins
= entry
->instruction
;
274 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
275 apei_unmap_generic_address(&entry
->register_region
);
280 /* Post-unmap all GAR in action table. */
281 int apei_exec_post_unmap_gars(struct apei_exec_context
*ctx
)
283 return apei_exec_for_each_entry(ctx
, post_unmap_gar_callback
,
286 EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars
);
289 * Resource management for GARs in APEI
292 struct list_head list
;
297 /* Collect all resources requested, to avoid conflict */
298 struct apei_resources apei_resources_all
= {
299 .iomem
= LIST_HEAD_INIT(apei_resources_all
.iomem
),
300 .ioport
= LIST_HEAD_INIT(apei_resources_all
.ioport
),
303 static int apei_res_add(struct list_head
*res_list
,
304 unsigned long start
, unsigned long size
)
306 struct apei_res
*res
, *resn
, *res_ins
= NULL
;
307 unsigned long end
= start
+ size
;
312 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
313 if (res
->start
> end
|| res
->end
< start
)
315 else if (end
<= res
->end
&& start
>= res
->start
) {
319 list_del(&res
->list
);
320 res
->start
= start
= min(res
->start
, start
);
321 res
->end
= end
= max(res
->end
, end
);
328 list_add(&res_ins
->list
, res_list
);
330 res_ins
= kmalloc(sizeof(*res
), GFP_KERNEL
);
333 res_ins
->start
= start
;
335 list_add(&res_ins
->list
, res_list
);
341 static int apei_res_sub(struct list_head
*res_list1
,
342 struct list_head
*res_list2
)
344 struct apei_res
*res1
, *resn1
, *res2
, *res
;
345 res1
= list_entry(res_list1
->next
, struct apei_res
, list
);
346 resn1
= list_entry(res1
->list
.next
, struct apei_res
, list
);
347 while (&res1
->list
!= res_list1
) {
348 list_for_each_entry(res2
, res_list2
, list
) {
349 if (res1
->start
>= res2
->end
||
350 res1
->end
<= res2
->start
)
352 else if (res1
->end
<= res2
->end
&&
353 res1
->start
>= res2
->start
) {
354 list_del(&res1
->list
);
357 } else if (res1
->end
> res2
->end
&&
358 res1
->start
< res2
->start
) {
359 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
362 res
->start
= res2
->end
;
363 res
->end
= res1
->end
;
364 res1
->end
= res2
->start
;
365 list_add(&res
->list
, &res1
->list
);
368 if (res1
->start
< res2
->start
)
369 res1
->end
= res2
->start
;
371 res1
->start
= res2
->end
;
375 resn1
= list_entry(resn1
->list
.next
, struct apei_res
, list
);
381 static void apei_res_clean(struct list_head
*res_list
)
383 struct apei_res
*res
, *resn
;
385 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
386 list_del(&res
->list
);
391 void apei_resources_fini(struct apei_resources
*resources
)
393 apei_res_clean(&resources
->iomem
);
394 apei_res_clean(&resources
->ioport
);
396 EXPORT_SYMBOL_GPL(apei_resources_fini
);
398 static int apei_resources_merge(struct apei_resources
*resources1
,
399 struct apei_resources
*resources2
)
402 struct apei_res
*res
;
404 list_for_each_entry(res
, &resources2
->iomem
, list
) {
405 rc
= apei_res_add(&resources1
->iomem
, res
->start
,
406 res
->end
- res
->start
);
410 list_for_each_entry(res
, &resources2
->ioport
, list
) {
411 rc
= apei_res_add(&resources1
->ioport
, res
->start
,
412 res
->end
- res
->start
);
420 int apei_resources_add(struct apei_resources
*resources
,
421 unsigned long start
, unsigned long size
,
425 return apei_res_add(&resources
->iomem
, start
, size
);
427 return apei_res_add(&resources
->ioport
, start
, size
);
429 EXPORT_SYMBOL_GPL(apei_resources_add
);
432 * EINJ has two groups of GARs (EINJ table entry and trigger table
433 * entry), so common resources are subtracted from the trigger table
434 * resources before the second requesting.
436 int apei_resources_sub(struct apei_resources
*resources1
,
437 struct apei_resources
*resources2
)
441 rc
= apei_res_sub(&resources1
->iomem
, &resources2
->iomem
);
444 return apei_res_sub(&resources1
->ioport
, &resources2
->ioport
);
446 EXPORT_SYMBOL_GPL(apei_resources_sub
);
448 static int apei_get_res_callback(__u64 start
, __u64 size
, void *data
)
450 struct apei_resources
*resources
= data
;
451 return apei_res_add(&resources
->iomem
, start
, size
);
454 static int apei_get_nvs_resources(struct apei_resources
*resources
)
456 return acpi_nvs_for_each_region(apei_get_res_callback
, resources
);
459 int (*arch_apei_filter_addr
)(int (*func
)(__u64 start
, __u64 size
,
460 void *data
), void *data
);
461 static int apei_get_arch_resources(struct apei_resources
*resources
)
464 return arch_apei_filter_addr(apei_get_res_callback
, resources
);
468 * IO memory/port resource management mechanism is used to check
469 * whether memory/port area used by GARs conflicts with normal memory
470 * or IO memory/port of devices.
472 int apei_resources_request(struct apei_resources
*resources
,
475 struct apei_res
*res
, *res_bak
= NULL
;
477 struct apei_resources nvs_resources
, arch_res
;
480 rc
= apei_resources_sub(resources
, &apei_resources_all
);
485 * Some firmware uses ACPI NVS region, that has been marked as
486 * busy, so exclude it from APEI resources to avoid false
489 apei_resources_init(&nvs_resources
);
490 rc
= apei_get_nvs_resources(&nvs_resources
);
493 rc
= apei_resources_sub(resources
, &nvs_resources
);
497 if (arch_apei_filter_addr
) {
498 apei_resources_init(&arch_res
);
499 rc
= apei_get_arch_resources(&arch_res
);
502 rc
= apei_resources_sub(resources
, &arch_res
);
508 list_for_each_entry(res
, &resources
->iomem
, list
) {
509 r
= request_mem_region(res
->start
, res
->end
- res
->start
,
513 "Can not request [mem %#010llx-%#010llx] for %s registers\n",
514 (unsigned long long)res
->start
,
515 (unsigned long long)res
->end
- 1, desc
);
517 goto err_unmap_iomem
;
521 list_for_each_entry(res
, &resources
->ioport
, list
) {
522 r
= request_region(res
->start
, res
->end
- res
->start
, desc
);
525 "Can not request [io %#06llx-%#06llx] for %s registers\n",
526 (unsigned long long)res
->start
,
527 (unsigned long long)res
->end
- 1, desc
);
529 goto err_unmap_ioport
;
533 rc
= apei_resources_merge(&apei_resources_all
, resources
);
535 pr_err(APEI_PFX
"Fail to merge resources!\n");
536 goto err_unmap_ioport
;
542 list_for_each_entry(res
, &resources
->ioport
, list
) {
545 release_region(res
->start
, res
->end
- res
->start
);
549 list_for_each_entry(res
, &resources
->iomem
, list
) {
552 release_mem_region(res
->start
, res
->end
- res
->start
);
555 if (arch_apei_filter_addr
)
556 apei_resources_fini(&arch_res
);
558 apei_resources_fini(&nvs_resources
);
561 EXPORT_SYMBOL_GPL(apei_resources_request
);
563 void apei_resources_release(struct apei_resources
*resources
)
566 struct apei_res
*res
;
568 list_for_each_entry(res
, &resources
->iomem
, list
)
569 release_mem_region(res
->start
, res
->end
- res
->start
);
570 list_for_each_entry(res
, &resources
->ioport
, list
)
571 release_region(res
->start
, res
->end
- res
->start
);
573 rc
= apei_resources_sub(&apei_resources_all
, resources
);
575 pr_err(APEI_PFX
"Fail to sub resources!\n");
577 EXPORT_SYMBOL_GPL(apei_resources_release
);
579 static int apei_check_gar(struct acpi_generic_address
*reg
, u64
*paddr
,
580 u32
*access_bit_width
)
582 u32 bit_width
, bit_offset
, access_size_code
, space_id
;
584 bit_width
= reg
->bit_width
;
585 bit_offset
= reg
->bit_offset
;
586 access_size_code
= reg
->access_width
;
587 space_id
= reg
->space_id
;
588 *paddr
= get_unaligned(®
->address
);
590 pr_warning(FW_BUG APEI_PFX
591 "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
592 *paddr
, bit_width
, bit_offset
, access_size_code
,
597 if (access_size_code
< 1 || access_size_code
> 4) {
598 pr_warning(FW_BUG APEI_PFX
599 "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
600 *paddr
, bit_width
, bit_offset
, access_size_code
,
604 *access_bit_width
= 1UL << (access_size_code
+ 2);
606 /* Fixup common BIOS bug */
607 if (bit_width
== 32 && bit_offset
== 0 && (*paddr
& 0x03) == 0 &&
608 *access_bit_width
< 32)
609 *access_bit_width
= 32;
610 else if (bit_width
== 64 && bit_offset
== 0 && (*paddr
& 0x07) == 0 &&
611 *access_bit_width
< 64)
612 *access_bit_width
= 64;
614 if ((bit_width
+ bit_offset
) > *access_bit_width
) {
615 pr_warning(FW_BUG APEI_PFX
616 "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
617 *paddr
, bit_width
, bit_offset
, access_size_code
,
622 if (space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
&&
623 space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
) {
624 pr_warning(FW_BUG APEI_PFX
625 "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
626 *paddr
, bit_width
, bit_offset
, access_size_code
,
634 int apei_map_generic_address(struct acpi_generic_address
*reg
)
637 u32 access_bit_width
;
640 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
643 return acpi_os_map_generic_address(reg
);
645 EXPORT_SYMBOL_GPL(apei_map_generic_address
);
647 /* read GAR in interrupt (including NMI) or process context */
648 int apei_read(u64
*val
, struct acpi_generic_address
*reg
)
651 u32 access_bit_width
;
655 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
660 switch(reg
->space_id
) {
661 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
662 status
= acpi_os_read_memory((acpi_physical_address
) address
,
663 val
, access_bit_width
);
664 if (ACPI_FAILURE(status
))
667 case ACPI_ADR_SPACE_SYSTEM_IO
:
668 status
= acpi_os_read_port(address
, (u32
*)val
,
670 if (ACPI_FAILURE(status
))
679 EXPORT_SYMBOL_GPL(apei_read
);
681 /* write GAR in interrupt (including NMI) or process context */
682 int apei_write(u64 val
, struct acpi_generic_address
*reg
)
685 u32 access_bit_width
;
689 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
693 switch (reg
->space_id
) {
694 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
695 status
= acpi_os_write_memory((acpi_physical_address
) address
,
696 val
, access_bit_width
);
697 if (ACPI_FAILURE(status
))
700 case ACPI_ADR_SPACE_SYSTEM_IO
:
701 status
= acpi_os_write_port(address
, val
, access_bit_width
);
702 if (ACPI_FAILURE(status
))
711 EXPORT_SYMBOL_GPL(apei_write
);
713 static int collect_res_callback(struct apei_exec_context
*ctx
,
714 struct acpi_whea_header
*entry
,
717 struct apei_resources
*resources
= data
;
718 struct acpi_generic_address
*reg
= &entry
->register_region
;
719 u8 ins
= entry
->instruction
;
720 u32 access_bit_width
;
724 if (!(ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
))
727 rc
= apei_check_gar(reg
, &paddr
, &access_bit_width
);
731 switch (reg
->space_id
) {
732 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
733 return apei_res_add(&resources
->iomem
, paddr
,
734 access_bit_width
/ 8);
735 case ACPI_ADR_SPACE_SYSTEM_IO
:
736 return apei_res_add(&resources
->ioport
, paddr
,
737 access_bit_width
/ 8);
744 * Same register may be used by multiple instructions in GARs, so
745 * resources are collected before requesting.
747 int apei_exec_collect_resources(struct apei_exec_context
*ctx
,
748 struct apei_resources
*resources
)
750 return apei_exec_for_each_entry(ctx
, collect_res_callback
,
753 EXPORT_SYMBOL_GPL(apei_exec_collect_resources
);
755 struct dentry
*apei_get_debugfs_dir(void)
757 static struct dentry
*dapei
;
760 dapei
= debugfs_create_dir("apei", NULL
);
764 EXPORT_SYMBOL_GPL(apei_get_debugfs_dir
);
766 int __weak
arch_apei_enable_cmcff(struct acpi_hest_header
*hest_hdr
,
771 EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff
);
773 void __weak
arch_apei_report_mem_error(int sev
,
774 struct cper_sec_mem_err
*mem_err
)
777 EXPORT_SYMBOL_GPL(arch_apei_report_mem_error
);
779 int apei_osc_setup(void)
781 static u8 whea_uuid_str
[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
784 struct acpi_osc_context context
= {
785 .uuid_str
= whea_uuid_str
,
787 .cap
.length
= sizeof(capbuf
),
788 .cap
.pointer
= capbuf
,
791 capbuf
[OSC_QUERY_DWORD
] = OSC_QUERY_ENABLE
;
792 capbuf
[OSC_SUPPORT_DWORD
] = 1;
793 capbuf
[OSC_CONTROL_DWORD
] = 0;
795 if (ACPI_FAILURE(acpi_get_handle(NULL
, "\\_SB", &handle
))
796 || ACPI_FAILURE(acpi_run_osc(handle
, &context
)))
799 kfree(context
.ret
.pointer
);
803 EXPORT_SYMBOL_GPL(apei_osc_setup
);