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
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/interrupt.h>
29 #include <linux/debugfs.h>
30 #include <acpi/apei.h>
31 #include <linux/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
)
128 ctx
->value
= entry
->value
;
130 return apei_exec_write_register(ctx
, entry
);
132 EXPORT_SYMBOL_GPL(apei_exec_write_register_value
);
134 int apei_exec_noop(struct apei_exec_context
*ctx
,
135 struct acpi_whea_header
*entry
)
139 EXPORT_SYMBOL_GPL(apei_exec_noop
);
142 * Interpret the specified action. Go through whole action table,
143 * execute all instructions belong to the action.
145 int __apei_exec_run(struct apei_exec_context
*ctx
, u8 action
,
150 struct acpi_whea_header
*entry
;
151 apei_exec_ins_func_t run
;
156 * "ip" is the instruction pointer of current instruction,
157 * "ctx->ip" specifies the next instruction to executed,
158 * instruction "run" function may change the "ctx->ip" to
159 * implement "goto" semantics.
163 for (i
= 0; i
< ctx
->entries
; i
++) {
164 entry
= &ctx
->action_table
[i
];
165 if (entry
->action
!= action
)
168 if (entry
->instruction
>= ctx
->instructions
||
169 !ctx
->ins_table
[entry
->instruction
].run
) {
170 pr_warn(FW_WARN APEI_PFX
171 "Invalid action table, unknown instruction type: %d\n",
175 run
= ctx
->ins_table
[entry
->instruction
].run
;
176 rc
= run(ctx
, entry
);
179 else if (rc
!= APEI_EXEC_SET_IP
)
187 return !optional
&& rc
< 0 ? rc
: 0;
189 EXPORT_SYMBOL_GPL(__apei_exec_run
);
191 typedef int (*apei_exec_entry_func_t
)(struct apei_exec_context
*ctx
,
192 struct acpi_whea_header
*entry
,
195 static int apei_exec_for_each_entry(struct apei_exec_context
*ctx
,
196 apei_exec_entry_func_t func
,
202 struct acpi_whea_header
*entry
;
203 struct apei_exec_ins_type
*ins_table
= ctx
->ins_table
;
205 for (i
= 0; i
< ctx
->entries
; i
++) {
206 entry
= ctx
->action_table
+ i
;
207 ins
= entry
->instruction
;
210 if (ins
>= ctx
->instructions
|| !ins_table
[ins
].run
) {
211 pr_warn(FW_WARN APEI_PFX
212 "Invalid action table, unknown instruction type: %d\n",
216 rc
= func(ctx
, entry
, data
);
224 static int pre_map_gar_callback(struct apei_exec_context
*ctx
,
225 struct acpi_whea_header
*entry
,
228 u8 ins
= entry
->instruction
;
230 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
231 return apei_map_generic_address(&entry
->register_region
);
237 * Pre-map all GARs in action table to make it possible to access them
240 int apei_exec_pre_map_gars(struct apei_exec_context
*ctx
)
244 rc
= apei_exec_for_each_entry(ctx
, pre_map_gar_callback
,
247 struct apei_exec_context ctx_unmap
;
248 memcpy(&ctx_unmap
, ctx
, sizeof(*ctx
));
249 ctx_unmap
.entries
= end
;
250 apei_exec_post_unmap_gars(&ctx_unmap
);
255 EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars
);
257 static int post_unmap_gar_callback(struct apei_exec_context
*ctx
,
258 struct acpi_whea_header
*entry
,
261 u8 ins
= entry
->instruction
;
263 if (ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
)
264 apei_unmap_generic_address(&entry
->register_region
);
269 /* Post-unmap all GAR in action table. */
270 int apei_exec_post_unmap_gars(struct apei_exec_context
*ctx
)
272 return apei_exec_for_each_entry(ctx
, post_unmap_gar_callback
,
275 EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars
);
278 * Resource management for GARs in APEI
281 struct list_head list
;
286 /* Collect all resources requested, to avoid conflict */
287 static struct apei_resources apei_resources_all
= {
288 .iomem
= LIST_HEAD_INIT(apei_resources_all
.iomem
),
289 .ioport
= LIST_HEAD_INIT(apei_resources_all
.ioport
),
292 static int apei_res_add(struct list_head
*res_list
,
293 unsigned long start
, unsigned long size
)
295 struct apei_res
*res
, *resn
, *res_ins
= NULL
;
296 unsigned long end
= start
+ size
;
301 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
302 if (res
->start
> end
|| res
->end
< start
)
304 else if (end
<= res
->end
&& start
>= res
->start
) {
308 list_del(&res
->list
);
309 res
->start
= start
= min(res
->start
, start
);
310 res
->end
= end
= max(res
->end
, end
);
317 list_add(&res_ins
->list
, res_list
);
319 res_ins
= kmalloc(sizeof(*res_ins
), GFP_KERNEL
);
322 res_ins
->start
= start
;
324 list_add(&res_ins
->list
, res_list
);
330 static int apei_res_sub(struct list_head
*res_list1
,
331 struct list_head
*res_list2
)
333 struct apei_res
*res1
, *resn1
, *res2
, *res
;
334 res1
= list_entry(res_list1
->next
, struct apei_res
, list
);
335 resn1
= list_entry(res1
->list
.next
, struct apei_res
, list
);
336 while (&res1
->list
!= res_list1
) {
337 list_for_each_entry(res2
, res_list2
, list
) {
338 if (res1
->start
>= res2
->end
||
339 res1
->end
<= res2
->start
)
341 else if (res1
->end
<= res2
->end
&&
342 res1
->start
>= res2
->start
) {
343 list_del(&res1
->list
);
346 } else if (res1
->end
> res2
->end
&&
347 res1
->start
< res2
->start
) {
348 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
351 res
->start
= res2
->end
;
352 res
->end
= res1
->end
;
353 res1
->end
= res2
->start
;
354 list_add(&res
->list
, &res1
->list
);
357 if (res1
->start
< res2
->start
)
358 res1
->end
= res2
->start
;
360 res1
->start
= res2
->end
;
364 resn1
= list_entry(resn1
->list
.next
, struct apei_res
, list
);
370 static void apei_res_clean(struct list_head
*res_list
)
372 struct apei_res
*res
, *resn
;
374 list_for_each_entry_safe(res
, resn
, res_list
, list
) {
375 list_del(&res
->list
);
380 void apei_resources_fini(struct apei_resources
*resources
)
382 apei_res_clean(&resources
->iomem
);
383 apei_res_clean(&resources
->ioport
);
385 EXPORT_SYMBOL_GPL(apei_resources_fini
);
387 static int apei_resources_merge(struct apei_resources
*resources1
,
388 struct apei_resources
*resources2
)
391 struct apei_res
*res
;
393 list_for_each_entry(res
, &resources2
->iomem
, list
) {
394 rc
= apei_res_add(&resources1
->iomem
, res
->start
,
395 res
->end
- res
->start
);
399 list_for_each_entry(res
, &resources2
->ioport
, list
) {
400 rc
= apei_res_add(&resources1
->ioport
, res
->start
,
401 res
->end
- res
->start
);
409 int apei_resources_add(struct apei_resources
*resources
,
410 unsigned long start
, unsigned long size
,
414 return apei_res_add(&resources
->iomem
, start
, size
);
416 return apei_res_add(&resources
->ioport
, start
, size
);
418 EXPORT_SYMBOL_GPL(apei_resources_add
);
421 * EINJ has two groups of GARs (EINJ table entry and trigger table
422 * entry), so common resources are subtracted from the trigger table
423 * resources before the second requesting.
425 int apei_resources_sub(struct apei_resources
*resources1
,
426 struct apei_resources
*resources2
)
430 rc
= apei_res_sub(&resources1
->iomem
, &resources2
->iomem
);
433 return apei_res_sub(&resources1
->ioport
, &resources2
->ioport
);
435 EXPORT_SYMBOL_GPL(apei_resources_sub
);
437 static int apei_get_res_callback(__u64 start
, __u64 size
, void *data
)
439 struct apei_resources
*resources
= data
;
440 return apei_res_add(&resources
->iomem
, start
, size
);
443 static int apei_get_nvs_resources(struct apei_resources
*resources
)
445 return acpi_nvs_for_each_region(apei_get_res_callback
, resources
);
448 int (*arch_apei_filter_addr
)(int (*func
)(__u64 start
, __u64 size
,
449 void *data
), void *data
);
450 static int apei_get_arch_resources(struct apei_resources
*resources
)
453 return arch_apei_filter_addr(apei_get_res_callback
, resources
);
457 * IO memory/port resource management mechanism is used to check
458 * whether memory/port area used by GARs conflicts with normal memory
459 * or IO memory/port of devices.
461 int apei_resources_request(struct apei_resources
*resources
,
464 struct apei_res
*res
, *res_bak
= NULL
;
466 struct apei_resources nvs_resources
, arch_res
;
469 rc
= apei_resources_sub(resources
, &apei_resources_all
);
474 * Some firmware uses ACPI NVS region, that has been marked as
475 * busy, so exclude it from APEI resources to avoid false
478 apei_resources_init(&nvs_resources
);
479 rc
= apei_get_nvs_resources(&nvs_resources
);
482 rc
= apei_resources_sub(resources
, &nvs_resources
);
486 if (arch_apei_filter_addr
) {
487 apei_resources_init(&arch_res
);
488 rc
= apei_get_arch_resources(&arch_res
);
491 rc
= apei_resources_sub(resources
, &arch_res
);
497 list_for_each_entry(res
, &resources
->iomem
, list
) {
498 r
= request_mem_region(res
->start
, res
->end
- res
->start
,
502 "Can not request [mem %#010llx-%#010llx] for %s registers\n",
503 (unsigned long long)res
->start
,
504 (unsigned long long)res
->end
- 1, desc
);
506 goto err_unmap_iomem
;
510 list_for_each_entry(res
, &resources
->ioport
, list
) {
511 r
= request_region(res
->start
, res
->end
- res
->start
, desc
);
514 "Can not request [io %#06llx-%#06llx] for %s registers\n",
515 (unsigned long long)res
->start
,
516 (unsigned long long)res
->end
- 1, desc
);
518 goto err_unmap_ioport
;
522 rc
= apei_resources_merge(&apei_resources_all
, resources
);
524 pr_err(APEI_PFX
"Fail to merge resources!\n");
525 goto err_unmap_ioport
;
531 list_for_each_entry(res
, &resources
->ioport
, list
) {
534 release_region(res
->start
, res
->end
- res
->start
);
538 list_for_each_entry(res
, &resources
->iomem
, list
) {
541 release_mem_region(res
->start
, res
->end
- res
->start
);
544 if (arch_apei_filter_addr
)
545 apei_resources_fini(&arch_res
);
547 apei_resources_fini(&nvs_resources
);
550 EXPORT_SYMBOL_GPL(apei_resources_request
);
552 void apei_resources_release(struct apei_resources
*resources
)
555 struct apei_res
*res
;
557 list_for_each_entry(res
, &resources
->iomem
, list
)
558 release_mem_region(res
->start
, res
->end
- res
->start
);
559 list_for_each_entry(res
, &resources
->ioport
, list
)
560 release_region(res
->start
, res
->end
- res
->start
);
562 rc
= apei_resources_sub(&apei_resources_all
, resources
);
564 pr_err(APEI_PFX
"Fail to sub resources!\n");
566 EXPORT_SYMBOL_GPL(apei_resources_release
);
568 static int apei_check_gar(struct acpi_generic_address
*reg
, u64
*paddr
,
569 u32
*access_bit_width
)
571 u32 bit_width
, bit_offset
, access_size_code
, space_id
;
573 bit_width
= reg
->bit_width
;
574 bit_offset
= reg
->bit_offset
;
575 access_size_code
= reg
->access_width
;
576 space_id
= reg
->space_id
;
577 *paddr
= get_unaligned(®
->address
);
579 pr_warn(FW_BUG APEI_PFX
580 "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
581 *paddr
, bit_width
, bit_offset
, access_size_code
,
586 if (access_size_code
< 1 || access_size_code
> 4) {
587 pr_warn(FW_BUG APEI_PFX
588 "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
589 *paddr
, bit_width
, bit_offset
, access_size_code
,
593 *access_bit_width
= 1UL << (access_size_code
+ 2);
595 /* Fixup common BIOS bug */
596 if (bit_width
== 32 && bit_offset
== 0 && (*paddr
& 0x03) == 0 &&
597 *access_bit_width
< 32)
598 *access_bit_width
= 32;
599 else if (bit_width
== 64 && bit_offset
== 0 && (*paddr
& 0x07) == 0 &&
600 *access_bit_width
< 64)
601 *access_bit_width
= 64;
603 if ((bit_width
+ bit_offset
) > *access_bit_width
) {
604 pr_warn(FW_BUG APEI_PFX
605 "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
606 *paddr
, bit_width
, bit_offset
, access_size_code
,
611 if (space_id
!= ACPI_ADR_SPACE_SYSTEM_MEMORY
&&
612 space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
) {
613 pr_warn(FW_BUG APEI_PFX
614 "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
615 *paddr
, bit_width
, bit_offset
, access_size_code
,
623 int apei_map_generic_address(struct acpi_generic_address
*reg
)
626 u32 access_bit_width
;
629 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
633 /* IO space doesn't need mapping */
634 if (reg
->space_id
== ACPI_ADR_SPACE_SYSTEM_IO
)
637 if (!acpi_os_map_generic_address(reg
))
642 EXPORT_SYMBOL_GPL(apei_map_generic_address
);
644 /* read GAR in interrupt (including NMI) or process context */
645 int apei_read(u64
*val
, struct acpi_generic_address
*reg
)
648 u32 access_bit_width
;
652 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
657 switch(reg
->space_id
) {
658 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
659 status
= acpi_os_read_memory((acpi_physical_address
) address
,
660 val
, access_bit_width
);
661 if (ACPI_FAILURE(status
))
664 case ACPI_ADR_SPACE_SYSTEM_IO
:
665 status
= acpi_os_read_port(address
, (u32
*)val
,
667 if (ACPI_FAILURE(status
))
676 EXPORT_SYMBOL_GPL(apei_read
);
678 /* write GAR in interrupt (including NMI) or process context */
679 int apei_write(u64 val
, struct acpi_generic_address
*reg
)
682 u32 access_bit_width
;
686 rc
= apei_check_gar(reg
, &address
, &access_bit_width
);
690 switch (reg
->space_id
) {
691 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
692 status
= acpi_os_write_memory((acpi_physical_address
) address
,
693 val
, access_bit_width
);
694 if (ACPI_FAILURE(status
))
697 case ACPI_ADR_SPACE_SYSTEM_IO
:
698 status
= acpi_os_write_port(address
, val
, access_bit_width
);
699 if (ACPI_FAILURE(status
))
708 EXPORT_SYMBOL_GPL(apei_write
);
710 static int collect_res_callback(struct apei_exec_context
*ctx
,
711 struct acpi_whea_header
*entry
,
714 struct apei_resources
*resources
= data
;
715 struct acpi_generic_address
*reg
= &entry
->register_region
;
716 u8 ins
= entry
->instruction
;
717 u32 access_bit_width
;
721 if (!(ctx
->ins_table
[ins
].flags
& APEI_EXEC_INS_ACCESS_REGISTER
))
724 rc
= apei_check_gar(reg
, &paddr
, &access_bit_width
);
728 switch (reg
->space_id
) {
729 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
730 return apei_res_add(&resources
->iomem
, paddr
,
731 access_bit_width
/ 8);
732 case ACPI_ADR_SPACE_SYSTEM_IO
:
733 return apei_res_add(&resources
->ioport
, paddr
,
734 access_bit_width
/ 8);
741 * Same register may be used by multiple instructions in GARs, so
742 * resources are collected before requesting.
744 int apei_exec_collect_resources(struct apei_exec_context
*ctx
,
745 struct apei_resources
*resources
)
747 return apei_exec_for_each_entry(ctx
, collect_res_callback
,
750 EXPORT_SYMBOL_GPL(apei_exec_collect_resources
);
752 struct dentry
*apei_get_debugfs_dir(void)
754 static struct dentry
*dapei
;
757 dapei
= debugfs_create_dir("apei", NULL
);
761 EXPORT_SYMBOL_GPL(apei_get_debugfs_dir
);
763 int __weak
arch_apei_enable_cmcff(struct acpi_hest_header
*hest_hdr
,
768 EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff
);
770 void __weak
arch_apei_report_mem_error(int sev
,
771 struct cper_sec_mem_err
*mem_err
)
774 EXPORT_SYMBOL_GPL(arch_apei_report_mem_error
);
776 int apei_osc_setup(void)
778 static u8 whea_uuid_str
[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
781 struct acpi_osc_context context
= {
782 .uuid_str
= whea_uuid_str
,
784 .cap
.length
= sizeof(capbuf
),
785 .cap
.pointer
= capbuf
,
788 capbuf
[OSC_QUERY_DWORD
] = OSC_QUERY_ENABLE
;
789 capbuf
[OSC_SUPPORT_DWORD
] = 1;
790 capbuf
[OSC_CONTROL_DWORD
] = 0;
792 if (ACPI_FAILURE(acpi_get_handle(NULL
, "\\_SB", &handle
))
793 || ACPI_FAILURE(acpi_run_osc(handle
, &context
)))
796 kfree(context
.ret
.pointer
);
800 EXPORT_SYMBOL_GPL(apei_osc_setup
);