1 // SPDX-License-Identifier: GPL-2.0-only
3 * apei-base.c - ACPI Platform Error Interface (APEI) supporting
6 * APEI allows to report errors (for example from the chipset) to the
7 * the operating system. This improves NMI handling especially. In
8 * addition it supports error serialization and error injection.
10 * For more information about APEI, please refer to ACPI Specification
11 * version 4.0, chapter 17.
13 * This file has Common functions used by more than one APEI table,
14 * including framework of interpreter for ERST and EINJ; resource
15 * management for APEI registers.
17 * Copyright (C) 2009, Intel Corp.
18 * Author: Huang Ying <ying.huang@intel.com>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/acpi.h>
25 #include <linux/slab.h>
27 #include <linux/kref.h>
28 #include <linux/rculist.h>
29 #include <linux/interrupt.h>
30 #include <linux/debugfs.h>
31 #include <asm/unaligned.h>
33 #include "apei-internal.h"
35 #define APEI_PFX "APEI: "
38 * APEI ERST (Error Record Serialization Table) and EINJ (Error
39 * INJection) interpreter framework.
42 #define APEI_EXEC_PRESERVE_REGISTER 0x1
44 void apei_exec_ctx_init(struct apei_exec_context
*ctx
,
45 struct apei_exec_ins_type
*ins_table
,
47 struct acpi_whea_header
*action_table
,
50 ctx
->ins_table
= ins_table
;
51 ctx
->instructions
= instructions
;
52 ctx
->action_table
= action_table
;
53 ctx
->entries
= entries
;
55 EXPORT_SYMBOL_GPL(apei_exec_ctx_init
);
57 int __apei_exec_read_register(struct acpi_whea_header
*entry
, u64
*val
)
61 rc
= apei_read(val
, &entry
->register_region
);
64 *val
>>= entry
->register_region
.bit_offset
;
70 int apei_exec_read_register(struct apei_exec_context
*ctx
,
71 struct acpi_whea_header
*entry
)
76 rc
= __apei_exec_read_register(entry
, &val
);
83 EXPORT_SYMBOL_GPL(apei_exec_read_register
);
85 int apei_exec_read_register_value(struct apei_exec_context
*ctx
,
86 struct acpi_whea_header
*entry
)
90 rc
= apei_exec_read_register(ctx
, entry
);
93 ctx
->value
= (ctx
->value
== entry
->value
);
97 EXPORT_SYMBOL_GPL(apei_exec_read_register_value
);
99 int __apei_exec_write_register(struct acpi_whea_header
*entry
, u64 val
)
104 val
<<= entry
->register_region
.bit_offset
;
105 if (entry
->flags
& APEI_EXEC_PRESERVE_REGISTER
) {
107 rc
= apei_read(&valr
, &entry
->register_region
);
110 valr
&= ~(entry
->mask
<< entry
->register_region
.bit_offset
);
113 rc
= apei_write(val
, &entry
->register_region
);
118 int apei_exec_write_register(struct apei_exec_context
*ctx
,
119 struct acpi_whea_header
*entry
)
121 return __apei_exec_write_register(entry
, ctx
->value
);
123 EXPORT_SYMBOL_GPL(apei_exec_write_register
);
125 int apei_exec_write_register_value(struct apei_exec_context
*ctx
,
126 struct acpi_whea_header
*entry
)
130 ctx
->value
= entry
->value
;
131 rc
= apei_exec_write_register(ctx
, entry
);
135 EXPORT_SYMBOL_GPL(apei_exec_write_register_value
);
137 int apei_exec_noop(struct apei_exec_context
*ctx
,
138 struct acpi_whea_header
*entry
)
142 EXPORT_SYMBOL_GPL(apei_exec_noop
);
145 * Interpret the specified action. Go through whole action table,
146 * execute all instructions belong to the action.
148 int __apei_exec_run(struct apei_exec_context
*ctx
, u8 action
,
153 struct acpi_whea_header
*entry
;
154 apei_exec_ins_func_t run
;
159 * "ip" is the instruction pointer of current instruction,
160 * "ctx->ip" specifies the next instruction to executed,
161 * instruction "run" function may change the "ctx->ip" to
162 * implement "goto" semantics.
166 for (i
= 0; i
< ctx
->entries
; i
++) {
167 entry
= &ctx
->action_table
[i
];
168 if (entry
->action
!= action
)
171 if (entry
->instruction
>= ctx
->instructions
||
172 !ctx
->ins_table
[entry
->instruction
].run
) {
173 pr_warn(FW_WARN APEI_PFX
174 "Invalid action table, unknown instruction type: %d\n",
178 run
= ctx
->ins_table
[entry
->instruction
].run
;
179 rc
= run(ctx
, entry
);
182 else if (rc
!= APEI_EXEC_SET_IP
)
190 return !optional
&& rc
< 0 ? rc
: 0;
192 EXPORT_SYMBOL_GPL(__apei_exec_run
);
194 typedef int (*apei_exec_entry_func_t
)(struct apei_exec_context
*ctx
,
195 struct acpi_whea_header
*entry
,
198 static int apei_exec_for_each_entry(struct apei_exec_context
*ctx
,
199 apei_exec_entry_func_t func
,
205 struct acpi_whea_header
*entry
;
206 struct apei_exec_ins_type
*ins_table
= ctx
->ins_table
;
208 for (i
= 0; i
< ctx
->entries
; i
++) {
209 entry
= ctx
->action_table
+ i
;
210 ins
= entry
->instruction
;
213 if (ins
>= ctx
->instructions
|| !ins_table
[ins
].run
) {
214 pr_warn(FW_WARN APEI_PFX
215 "Invalid action table, unknown instruction type: %d\n",
219 rc
= func(ctx
, entry
, data
);
227 static int pre_map_gar_callback(struct apei_exec_context
*ctx
,
228 struct acpi_whea_header
*entry
,
231 u8 ins
= entry
->instruction
;
233 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
234 return apei_map_generic_address(&entry
->register_region
);
240 * Pre-map all GARs in action table to make it possible to access them
243 int apei_exec_pre_map_gars(struct apei_exec_context
*ctx
)
247 rc
= apei_exec_for_each_entry(ctx
, pre_map_gar_callback
,
250 struct apei_exec_context ctx_unmap
;
251 memcpy(&ctx_unmap
, ctx
, sizeof(*ctx
));
252 ctx_unmap
.entries
= end
;
253 apei_exec_post_unmap_gars(&ctx_unmap
);
258 EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars
);
260 static int post_unmap_gar_callback(struct apei_exec_context
*ctx
,
261 struct acpi_whea_header
*entry
,
264 u8 ins
= entry
->instruction
;
266 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
267 apei_unmap_generic_address(&entry
->register_region
);
272 /* Post-unmap all GAR in action table. */
273 int apei_exec_post_unmap_gars(struct apei_exec_context
*ctx
)
275 return apei_exec_for_each_entry(ctx
, post_unmap_gar_callback
,
278 EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars
);
281 * Resource management for GARs in APEI
284 struct list_head list
;
289 /* Collect all resources requested, to avoid conflict */
290 struct apei_resources apei_resources_all
= {
291 .iomem
= LIST_HEAD_INIT(apei_resources_all
.iomem
),
292 .ioport
= LIST_HEAD_INIT(apei_resources_all
.ioport
),
295 static int apei_res_add(struct list_head
*res_list
,
296 unsigned long start
, unsigned long size
)
298 struct apei_res
*res
, *resn
, *res_ins
= NULL
;
299 unsigned long end
= start
+ size
;
304 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
305 if (res
->start
> end
|| res
->end
< start
)
307 else if (end
<= res
->end
&& start
>= res
->start
) {
311 list_del(&res
->list
);
312 res
->start
= start
= min(res
->start
, start
);
313 res
->end
= end
= max(res
->end
, end
);
320 list_add(&res_ins
->list
, res_list
);
322 res_ins
= kmalloc(sizeof(*res
), GFP_KERNEL
);
325 res_ins
->start
= start
;
327 list_add(&res_ins
->list
, res_list
);
333 static int apei_res_sub(struct list_head
*res_list1
,
334 struct list_head
*res_list2
)
336 struct apei_res
*res1
, *resn1
, *res2
, *res
;
337 res1
= list_entry(res_list1
->next
, struct apei_res
, list
);
338 resn1
= list_entry(res1
->list
.next
, struct apei_res
, list
);
339 while (&res1
->list
!= res_list1
) {
340 list_for_each_entry(res2
, res_list2
, list
) {
341 if (res1
->start
>= res2
->end
||
342 res1
->end
<= res2
->start
)
344 else if (res1
->end
<= res2
->end
&&
345 res1
->start
>= res2
->start
) {
346 list_del(&res1
->list
);
349 } else if (res1
->end
> res2
->end
&&
350 res1
->start
< res2
->start
) {
351 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
354 res
->start
= res2
->end
;
355 res
->end
= res1
->end
;
356 res1
->end
= res2
->start
;
357 list_add(&res
->list
, &res1
->list
);
360 if (res1
->start
< res2
->start
)
361 res1
->end
= res2
->start
;
363 res1
->start
= res2
->end
;
367 resn1
= list_entry(resn1
->list
.next
, struct apei_res
, list
);
373 static void apei_res_clean(struct list_head
*res_list
)
375 struct apei_res
*res
, *resn
;
377 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
378 list_del(&res
->list
);
383 void apei_resources_fini(struct apei_resources
*resources
)
385 apei_res_clean(&resources
->iomem
);
386 apei_res_clean(&resources
->ioport
);
388 EXPORT_SYMBOL_GPL(apei_resources_fini
);
390 static int apei_resources_merge(struct apei_resources
*resources1
,
391 struct apei_resources
*resources2
)
394 struct apei_res
*res
;
396 list_for_each_entry(res
, &resources2
->iomem
, list
) {
397 rc
= apei_res_add(&resources1
->iomem
, res
->start
,
398 res
->end
- res
->start
);
402 list_for_each_entry(res
, &resources2
->ioport
, list
) {
403 rc
= apei_res_add(&resources1
->ioport
, res
->start
,
404 res
->end
- res
->start
);
412 int apei_resources_add(struct apei_resources
*resources
,
413 unsigned long start
, unsigned long size
,
417 return apei_res_add(&resources
->iomem
, start
, size
);
419 return apei_res_add(&resources
->ioport
, start
, size
);
421 EXPORT_SYMBOL_GPL(apei_resources_add
);
424 * EINJ has two groups of GARs (EINJ table entry and trigger table
425 * entry), so common resources are subtracted from the trigger table
426 * resources before the second requesting.
428 int apei_resources_sub(struct apei_resources
*resources1
,
429 struct apei_resources
*resources2
)
433 rc
= apei_res_sub(&resources1
->iomem
, &resources2
->iomem
);
436 return apei_res_sub(&resources1
->ioport
, &resources2
->ioport
);
438 EXPORT_SYMBOL_GPL(apei_resources_sub
);
440 static int apei_get_res_callback(__u64 start
, __u64 size
, void *data
)
442 struct apei_resources
*resources
= data
;
443 return apei_res_add(&resources
->iomem
, start
, size
);
446 static int apei_get_nvs_resources(struct apei_resources
*resources
)
448 return acpi_nvs_for_each_region(apei_get_res_callback
, resources
);
451 int (*arch_apei_filter_addr
)(int (*func
)(__u64 start
, __u64 size
,
452 void *data
), void *data
);
453 static int apei_get_arch_resources(struct apei_resources
*resources
)
456 return arch_apei_filter_addr(apei_get_res_callback
, resources
);
460 * IO memory/port resource management mechanism is used to check
461 * whether memory/port area used by GARs conflicts with normal memory
462 * or IO memory/port of devices.
464 int apei_resources_request(struct apei_resources
*resources
,
467 struct apei_res
*res
, *res_bak
= NULL
;
469 struct apei_resources nvs_resources
, arch_res
;
472 rc
= apei_resources_sub(resources
, &apei_resources_all
);
477 * Some firmware uses ACPI NVS region, that has been marked as
478 * busy, so exclude it from APEI resources to avoid false
481 apei_resources_init(&nvs_resources
);
482 rc
= apei_get_nvs_resources(&nvs_resources
);
485 rc
= apei_resources_sub(resources
, &nvs_resources
);
489 if (arch_apei_filter_addr
) {
490 apei_resources_init(&arch_res
);
491 rc
= apei_get_arch_resources(&arch_res
);
494 rc
= apei_resources_sub(resources
, &arch_res
);
500 list_for_each_entry(res
, &resources
->iomem
, list
) {
501 r
= request_mem_region(res
->start
, res
->end
- res
->start
,
505 "Can not request [mem %#010llx-%#010llx] for %s registers\n",
506 (unsigned long long)res
->start
,
507 (unsigned long long)res
->end
- 1, desc
);
509 goto err_unmap_iomem
;
513 list_for_each_entry(res
, &resources
->ioport
, list
) {
514 r
= request_region(res
->start
, res
->end
- res
->start
, desc
);
517 "Can not request [io %#06llx-%#06llx] for %s registers\n",
518 (unsigned long long)res
->start
,
519 (unsigned long long)res
->end
- 1, desc
);
521 goto err_unmap_ioport
;
525 rc
= apei_resources_merge(&apei_resources_all
, resources
);
527 pr_err(APEI_PFX
"Fail to merge resources!\n");
528 goto err_unmap_ioport
;
534 list_for_each_entry(res
, &resources
->ioport
, list
) {
537 release_region(res
->start
, res
->end
- res
->start
);
541 list_for_each_entry(res
, &resources
->iomem
, list
) {
544 release_mem_region(res
->start
, res
->end
- res
->start
);
547 if (arch_apei_filter_addr
)
548 apei_resources_fini(&arch_res
);
550 apei_resources_fini(&nvs_resources
);
553 EXPORT_SYMBOL_GPL(apei_resources_request
);
555 void apei_resources_release(struct apei_resources
*resources
)
558 struct apei_res
*res
;
560 list_for_each_entry(res
, &resources
->iomem
, list
)
561 release_mem_region(res
->start
, res
->end
- res
->start
);
562 list_for_each_entry(res
, &resources
->ioport
, list
)
563 release_region(res
->start
, res
->end
- res
->start
);
565 rc
= apei_resources_sub(&apei_resources_all
, resources
);
567 pr_err(APEI_PFX
"Fail to sub resources!\n");
569 EXPORT_SYMBOL_GPL(apei_resources_release
);
571 static int apei_check_gar(struct acpi_generic_address
*reg
, u64
*paddr
,
572 u32
*access_bit_width
)
574 u32 bit_width
, bit_offset
, access_size_code
, space_id
;
576 bit_width
= reg
->bit_width
;
577 bit_offset
= reg
->bit_offset
;
578 access_size_code
= reg
->access_width
;
579 space_id
= reg
->space_id
;
580 *paddr
= get_unaligned(®
->address
);
582 pr_warn(FW_BUG APEI_PFX
583 "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
584 *paddr
, bit_width
, bit_offset
, access_size_code
,
589 if (access_size_code
< 1 || access_size_code
> 4) {
590 pr_warn(FW_BUG APEI_PFX
591 "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
592 *paddr
, bit_width
, bit_offset
, access_size_code
,
596 *access_bit_width
= 1UL << (access_size_code
+ 2);
598 /* Fixup common BIOS bug */
599 if (bit_width
== 32 && bit_offset
== 0 && (*paddr
& 0x03) == 0 &&
600 *access_bit_width
< 32)
601 *access_bit_width
= 32;
602 else if (bit_width
== 64 && bit_offset
== 0 && (*paddr
& 0x07) == 0 &&
603 *access_bit_width
< 64)
604 *access_bit_width
= 64;
606 if ((bit_width
+ bit_offset
) > *access_bit_width
) {
607 pr_warn(FW_BUG APEI_PFX
608 "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
609 *paddr
, bit_width
, bit_offset
, access_size_code
,
614 if (space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
&&
615 space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
) {
616 pr_warn(FW_BUG APEI_PFX
617 "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
618 *paddr
, bit_width
, bit_offset
, access_size_code
,
626 int apei_map_generic_address(struct acpi_generic_address
*reg
)
629 u32 access_bit_width
;
632 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
635 return acpi_os_map_generic_address(reg
);
637 EXPORT_SYMBOL_GPL(apei_map_generic_address
);
639 /* read GAR in interrupt (including NMI) or process context */
640 int apei_read(u64
*val
, struct acpi_generic_address
*reg
)
643 u32 access_bit_width
;
647 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
652 switch(reg
->space_id
) {
653 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
654 status
= acpi_os_read_memory((acpi_physical_address
) address
,
655 val
, access_bit_width
);
656 if (ACPI_FAILURE(status
))
659 case ACPI_ADR_SPACE_SYSTEM_IO
:
660 status
= acpi_os_read_port(address
, (u32
*)val
,
662 if (ACPI_FAILURE(status
))
671 EXPORT_SYMBOL_GPL(apei_read
);
673 /* write GAR in interrupt (including NMI) or process context */
674 int apei_write(u64 val
, struct acpi_generic_address
*reg
)
677 u32 access_bit_width
;
681 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
685 switch (reg
->space_id
) {
686 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
687 status
= acpi_os_write_memory((acpi_physical_address
) address
,
688 val
, access_bit_width
);
689 if (ACPI_FAILURE(status
))
692 case ACPI_ADR_SPACE_SYSTEM_IO
:
693 status
= acpi_os_write_port(address
, val
, access_bit_width
);
694 if (ACPI_FAILURE(status
))
703 EXPORT_SYMBOL_GPL(apei_write
);
705 static int collect_res_callback(struct apei_exec_context
*ctx
,
706 struct acpi_whea_header
*entry
,
709 struct apei_resources
*resources
= data
;
710 struct acpi_generic_address
*reg
= &entry
->register_region
;
711 u8 ins
= entry
->instruction
;
712 u32 access_bit_width
;
716 if (!(ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
))
719 rc
= apei_check_gar(reg
, &paddr
, &access_bit_width
);
723 switch (reg
->space_id
) {
724 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
725 return apei_res_add(&resources
->iomem
, paddr
,
726 access_bit_width
/ 8);
727 case ACPI_ADR_SPACE_SYSTEM_IO
:
728 return apei_res_add(&resources
->ioport
, paddr
,
729 access_bit_width
/ 8);
736 * Same register may be used by multiple instructions in GARs, so
737 * resources are collected before requesting.
739 int apei_exec_collect_resources(struct apei_exec_context
*ctx
,
740 struct apei_resources
*resources
)
742 return apei_exec_for_each_entry(ctx
, collect_res_callback
,
745 EXPORT_SYMBOL_GPL(apei_exec_collect_resources
);
747 struct dentry
*apei_get_debugfs_dir(void)
749 static struct dentry
*dapei
;
752 dapei
= debugfs_create_dir("apei", NULL
);
756 EXPORT_SYMBOL_GPL(apei_get_debugfs_dir
);
758 int __weak
arch_apei_enable_cmcff(struct acpi_hest_header
*hest_hdr
,
763 EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff
);
765 void __weak
arch_apei_report_mem_error(int sev
,
766 struct cper_sec_mem_err
*mem_err
)
769 EXPORT_SYMBOL_GPL(arch_apei_report_mem_error
);
771 int apei_osc_setup(void)
773 static u8 whea_uuid_str
[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
776 struct acpi_osc_context context
= {
777 .uuid_str
= whea_uuid_str
,
779 .cap
.length
= sizeof(capbuf
),
780 .cap
.pointer
= capbuf
,
783 capbuf
[OSC_QUERY_DWORD
] = OSC_QUERY_ENABLE
;
784 capbuf
[OSC_SUPPORT_DWORD
] = 1;
785 capbuf
[OSC_CONTROL_DWORD
] = 0;
787 if (ACPI_FAILURE(acpi_get_handle(NULL
, "\\_SB", &handle
))
788 || ACPI_FAILURE(acpi_run_osc(handle
, &context
)))
791 kfree(context
.ret
.pointer
);
795 EXPORT_SYMBOL_GPL(apei_osc_setup
);