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.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/acpi.h>
37 #include <linux/slab.h>
39 #include <linux/kref.h>
40 #include <linux/rculist.h>
41 #include <linux/interrupt.h>
42 #include <linux/debugfs.h>
43 #include <asm/unaligned.h>
45 #include "apei-internal.h"
47 #define APEI_PFX "APEI: "
50 * APEI ERST (Error Record Serialization Table) and EINJ (Error
51 * INJection) interpreter framework.
54 #define APEI_EXEC_PRESERVE_REGISTER 0x1
56 void apei_exec_ctx_init(struct apei_exec_context *ctx,
57 struct apei_exec_ins_type *ins_table,
59 struct acpi_whea_header *action_table,
62 ctx->ins_table = ins_table;
63 ctx->instructions = instructions;
64 ctx->action_table = action_table;
65 ctx->entries = entries;
67 EXPORT_SYMBOL_GPL(apei_exec_ctx_init);
69 int __apei_exec_read_register(struct acpi_whea_header *entry, u64 *val)
73 rc = apei_read(val, &entry->register_region);
76 *val >>= entry->register_region.bit_offset;
82 int apei_exec_read_register(struct apei_exec_context *ctx,
83 struct acpi_whea_header *entry)
88 rc = __apei_exec_read_register(entry, &val);
95 EXPORT_SYMBOL_GPL(apei_exec_read_register);
97 int apei_exec_read_register_value(struct apei_exec_context *ctx,
98 struct acpi_whea_header *entry)
102 rc = apei_exec_read_register(ctx, entry);
105 ctx->value = (ctx->value == entry->value);
109 EXPORT_SYMBOL_GPL(apei_exec_read_register_value);
111 int __apei_exec_write_register(struct acpi_whea_header *entry, u64 val)
116 val <<= entry->register_region.bit_offset;
117 if (entry->flags & APEI_EXEC_PRESERVE_REGISTER) {
119 rc = apei_read(&valr, &entry->register_region);
122 valr &= ~(entry->mask << entry->register_region.bit_offset);
125 rc = apei_write(val, &entry->register_region);
130 int apei_exec_write_register(struct apei_exec_context *ctx,
131 struct acpi_whea_header *entry)
133 return __apei_exec_write_register(entry, ctx->value);
135 EXPORT_SYMBOL_GPL(apei_exec_write_register);
137 int apei_exec_write_register_value(struct apei_exec_context *ctx,
138 struct acpi_whea_header *entry)
142 ctx->value = entry->value;
143 rc = apei_exec_write_register(ctx, entry);
147 EXPORT_SYMBOL_GPL(apei_exec_write_register_value);
149 int apei_exec_noop(struct apei_exec_context *ctx,
150 struct acpi_whea_header *entry)
154 EXPORT_SYMBOL_GPL(apei_exec_noop);
157 * Interpret the specified action. Go through whole action table,
158 * execute all instructions belong to the action.
160 int __apei_exec_run(struct apei_exec_context *ctx, u8 action,
165 struct acpi_whea_header *entry;
166 apei_exec_ins_func_t run;
171 * "ip" is the instruction pointer of current instruction,
172 * "ctx->ip" specifies the next instruction to executed,
173 * instruction "run" function may change the "ctx->ip" to
174 * implement "goto" semantics.
178 for (i = 0; i < ctx->entries; i++) {
179 entry = &ctx->action_table[i];
180 if (entry->action != action)
183 if (entry->instruction >= ctx->instructions ||
184 !ctx->ins_table[entry->instruction].run) {
185 pr_warning(FW_WARN APEI_PFX
186 "Invalid action table, unknown instruction type: %d\n",
190 run = ctx->ins_table[entry->instruction].run;
191 rc = run(ctx, entry);
194 else if (rc != APEI_EXEC_SET_IP)
202 return !optional && rc < 0 ? rc : 0;
204 EXPORT_SYMBOL_GPL(__apei_exec_run);
206 typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
207 struct acpi_whea_header *entry,
210 static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
211 apei_exec_entry_func_t func,
217 struct acpi_whea_header *entry;
218 struct apei_exec_ins_type *ins_table = ctx->ins_table;
220 for (i = 0; i < ctx->entries; i++) {
221 entry = ctx->action_table + i;
222 ins = entry->instruction;
225 if (ins >= ctx->instructions || !ins_table[ins].run) {
226 pr_warning(FW_WARN APEI_PFX
227 "Invalid action table, unknown instruction type: %d\n",
231 rc = func(ctx, entry, data);
239 static int pre_map_gar_callback(struct apei_exec_context *ctx,
240 struct acpi_whea_header *entry,
243 u8 ins = entry->instruction;
245 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
246 return apei_map_generic_address(&entry->register_region);
252 * Pre-map all GARs in action table to make it possible to access them
255 int apei_exec_pre_map_gars(struct apei_exec_context *ctx)
259 rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
262 struct apei_exec_context ctx_unmap;
263 memcpy(&ctx_unmap, ctx, sizeof(*ctx));
264 ctx_unmap.entries = end;
265 apei_exec_post_unmap_gars(&ctx_unmap);
270 EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars);
272 static int post_unmap_gar_callback(struct apei_exec_context *ctx,
273 struct acpi_whea_header *entry,
276 u8 ins = entry->instruction;
278 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
279 apei_unmap_generic_address(&entry->register_region);
284 /* Post-unmap all GAR in action table. */
285 int apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
287 return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
290 EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars);
293 * Resource management for GARs in APEI
296 struct list_head list;
301 /* Collect all resources requested, to avoid conflict */
302 struct apei_resources apei_resources_all = {
303 .iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
304 .ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
307 static int apei_res_add(struct list_head *res_list,
308 unsigned long start, unsigned long size)
310 struct apei_res *res, *resn, *res_ins = NULL;
311 unsigned long end = start + size;
316 list_for_each_entry_safe(res, resn, res_list, list) {
317 if (res->start > end || res->end < start)
319 else if (end <= res->end && start >= res->start) {
323 list_del(&res->list);
324 res->start = start = min(res->start, start);
325 res->end = end = max(res->end, end);
332 list_add(&res_ins->list, res_list);
334 res_ins = kmalloc(sizeof(*res), GFP_KERNEL);
337 res_ins->start = start;
339 list_add(&res_ins->list, res_list);
345 static int apei_res_sub(struct list_head *res_list1,
346 struct list_head *res_list2)
348 struct apei_res *res1, *resn1, *res2, *res;
349 res1 = list_entry(res_list1->next, struct apei_res, list);
350 resn1 = list_entry(res1->list.next, struct apei_res, list);
351 while (&res1->list != res_list1) {
352 list_for_each_entry(res2, res_list2, list) {
353 if (res1->start >= res2->end ||
354 res1->end <= res2->start)
356 else if (res1->end <= res2->end &&
357 res1->start >= res2->start) {
358 list_del(&res1->list);
361 } else if (res1->end > res2->end &&
362 res1->start < res2->start) {
363 res = kmalloc(sizeof(*res), GFP_KERNEL);
366 res->start = res2->end;
367 res->end = res1->end;
368 res1->end = res2->start;
369 list_add(&res->list, &res1->list);
372 if (res1->start < res2->start)
373 res1->end = res2->start;
375 res1->start = res2->end;
379 resn1 = list_entry(resn1->list.next, struct apei_res, list);
385 static void apei_res_clean(struct list_head *res_list)
387 struct apei_res *res, *resn;
389 list_for_each_entry_safe(res, resn, res_list, list) {
390 list_del(&res->list);
395 void apei_resources_fini(struct apei_resources *resources)
397 apei_res_clean(&resources->iomem);
398 apei_res_clean(&resources->ioport);
400 EXPORT_SYMBOL_GPL(apei_resources_fini);
402 static int apei_resources_merge(struct apei_resources *resources1,
403 struct apei_resources *resources2)
406 struct apei_res *res;
408 list_for_each_entry(res, &resources2->iomem, list) {
409 rc = apei_res_add(&resources1->iomem, res->start,
410 res->end - res->start);
414 list_for_each_entry(res, &resources2->ioport, list) {
415 rc = apei_res_add(&resources1->ioport, res->start,
416 res->end - res->start);
424 int apei_resources_add(struct apei_resources *resources,
425 unsigned long start, unsigned long size,
429 return apei_res_add(&resources->iomem, start, size);
431 return apei_res_add(&resources->ioport, start, size);
433 EXPORT_SYMBOL_GPL(apei_resources_add);
436 * EINJ has two groups of GARs (EINJ table entry and trigger table
437 * entry), so common resources are subtracted from the trigger table
438 * resources before the second requesting.
440 int apei_resources_sub(struct apei_resources *resources1,
441 struct apei_resources *resources2)
445 rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
448 return apei_res_sub(&resources1->ioport, &resources2->ioport);
450 EXPORT_SYMBOL_GPL(apei_resources_sub);
452 static int apei_get_res_callback(__u64 start, __u64 size, void *data)
454 struct apei_resources *resources = data;
455 return apei_res_add(&resources->iomem, start, size);
458 static int apei_get_nvs_resources(struct apei_resources *resources)
460 return acpi_nvs_for_each_region(apei_get_res_callback, resources);
463 int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
464 void *data), void *data);
465 static int apei_get_arch_resources(struct apei_resources *resources)
468 return arch_apei_filter_addr(apei_get_res_callback, resources);
472 * IO memory/port resource management mechanism is used to check
473 * whether memory/port area used by GARs conflicts with normal memory
474 * or IO memory/port of devices.
476 int apei_resources_request(struct apei_resources *resources,
479 struct apei_res *res, *res_bak = NULL;
481 struct apei_resources nvs_resources, arch_res;
484 rc = apei_resources_sub(resources, &apei_resources_all);
489 * Some firmware uses ACPI NVS region, that has been marked as
490 * busy, so exclude it from APEI resources to avoid false
493 apei_resources_init(&nvs_resources);
494 rc = apei_get_nvs_resources(&nvs_resources);
497 rc = apei_resources_sub(resources, &nvs_resources);
501 if (arch_apei_filter_addr) {
502 apei_resources_init(&arch_res);
503 rc = apei_get_arch_resources(&arch_res);
506 rc = apei_resources_sub(resources, &arch_res);
512 list_for_each_entry(res, &resources->iomem, list) {
513 r = request_mem_region(res->start, res->end - res->start,
517 "Can not request [mem %#010llx-%#010llx] for %s registers\n",
518 (unsigned long long)res->start,
519 (unsigned long long)res->end - 1, desc);
521 goto err_unmap_iomem;
525 list_for_each_entry(res, &resources->ioport, list) {
526 r = request_region(res->start, res->end - res->start, desc);
529 "Can not request [io %#06llx-%#06llx] for %s registers\n",
530 (unsigned long long)res->start,
531 (unsigned long long)res->end - 1, desc);
533 goto err_unmap_ioport;
537 rc = apei_resources_merge(&apei_resources_all, resources);
539 pr_err(APEI_PFX "Fail to merge resources!\n");
540 goto err_unmap_ioport;
545 list_for_each_entry(res, &resources->ioport, list) {
548 release_region(res->start, res->end - res->start);
552 list_for_each_entry(res, &resources->iomem, list) {
555 release_mem_region(res->start, res->end - res->start);
558 apei_resources_fini(&arch_res);
560 apei_resources_fini(&nvs_resources);
563 EXPORT_SYMBOL_GPL(apei_resources_request);
565 void apei_resources_release(struct apei_resources *resources)
568 struct apei_res *res;
570 list_for_each_entry(res, &resources->iomem, list)
571 release_mem_region(res->start, res->end - res->start);
572 list_for_each_entry(res, &resources->ioport, list)
573 release_region(res->start, res->end - res->start);
575 rc = apei_resources_sub(&apei_resources_all, resources);
577 pr_err(APEI_PFX "Fail to sub resources!\n");
579 EXPORT_SYMBOL_GPL(apei_resources_release);
581 static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr,
582 u32 *access_bit_width)
584 u32 bit_width, bit_offset, access_size_code, space_id;
586 bit_width = reg->bit_width;
587 bit_offset = reg->bit_offset;
588 access_size_code = reg->access_width;
589 space_id = reg->space_id;
590 *paddr = get_unaligned(®->address);
592 pr_warning(FW_BUG APEI_PFX
593 "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
594 *paddr, bit_width, bit_offset, access_size_code,
599 if (access_size_code < 1 || access_size_code > 4) {
600 pr_warning(FW_BUG APEI_PFX
601 "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
602 *paddr, bit_width, bit_offset, access_size_code,
606 *access_bit_width = 1UL << (access_size_code + 2);
608 /* Fixup common BIOS bug */
609 if (bit_width == 32 && bit_offset == 0 && (*paddr & 0x03) == 0 &&
610 *access_bit_width < 32)
611 *access_bit_width = 32;
612 else if (bit_width == 64 && bit_offset == 0 && (*paddr & 0x07) == 0 &&
613 *access_bit_width < 64)
614 *access_bit_width = 64;
616 if ((bit_width + bit_offset) > *access_bit_width) {
617 pr_warning(FW_BUG APEI_PFX
618 "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
619 *paddr, bit_width, bit_offset, access_size_code,
624 if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
625 space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
626 pr_warning(FW_BUG APEI_PFX
627 "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
628 *paddr, bit_width, bit_offset, access_size_code,
636 int apei_map_generic_address(struct acpi_generic_address *reg)
639 u32 access_bit_width;
642 rc = apei_check_gar(reg, &address, &access_bit_width);
645 return acpi_os_map_generic_address(reg);
647 EXPORT_SYMBOL_GPL(apei_map_generic_address);
649 /* read GAR in interrupt (including NMI) or process context */
650 int apei_read(u64 *val, struct acpi_generic_address *reg)
653 u32 access_bit_width;
657 rc = apei_check_gar(reg, &address, &access_bit_width);
662 switch(reg->space_id) {
663 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
664 status = acpi_os_read_memory((acpi_physical_address) address,
665 val, access_bit_width);
666 if (ACPI_FAILURE(status))
669 case ACPI_ADR_SPACE_SYSTEM_IO:
670 status = acpi_os_read_port(address, (u32 *)val,
672 if (ACPI_FAILURE(status))
681 EXPORT_SYMBOL_GPL(apei_read);
683 /* write GAR in interrupt (including NMI) or process context */
684 int apei_write(u64 val, struct acpi_generic_address *reg)
687 u32 access_bit_width;
691 rc = apei_check_gar(reg, &address, &access_bit_width);
695 switch (reg->space_id) {
696 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
697 status = acpi_os_write_memory((acpi_physical_address) address,
698 val, access_bit_width);
699 if (ACPI_FAILURE(status))
702 case ACPI_ADR_SPACE_SYSTEM_IO:
703 status = acpi_os_write_port(address, val, access_bit_width);
704 if (ACPI_FAILURE(status))
713 EXPORT_SYMBOL_GPL(apei_write);
715 static int collect_res_callback(struct apei_exec_context *ctx,
716 struct acpi_whea_header *entry,
719 struct apei_resources *resources = data;
720 struct acpi_generic_address *reg = &entry->register_region;
721 u8 ins = entry->instruction;
722 u32 access_bit_width;
726 if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
729 rc = apei_check_gar(reg, &paddr, &access_bit_width);
733 switch (reg->space_id) {
734 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
735 return apei_res_add(&resources->iomem, paddr,
736 access_bit_width / 8);
737 case ACPI_ADR_SPACE_SYSTEM_IO:
738 return apei_res_add(&resources->ioport, paddr,
739 access_bit_width / 8);
746 * Same register may be used by multiple instructions in GARs, so
747 * resources are collected before requesting.
749 int apei_exec_collect_resources(struct apei_exec_context *ctx,
750 struct apei_resources *resources)
752 return apei_exec_for_each_entry(ctx, collect_res_callback,
755 EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
757 struct dentry *apei_get_debugfs_dir(void)
759 static struct dentry *dapei;
762 dapei = debugfs_create_dir("apei", NULL);
766 EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
768 int __weak arch_apei_enable_cmcff(struct acpi_hest_header *hest_hdr,
773 EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff);
775 void __weak arch_apei_report_mem_error(int sev,
776 struct cper_sec_mem_err *mem_err)
779 EXPORT_SYMBOL_GPL(arch_apei_report_mem_error);
781 int apei_osc_setup(void)
783 static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
786 struct acpi_osc_context context = {
787 .uuid_str = whea_uuid_str,
789 .cap.length = sizeof(capbuf),
790 .cap.pointer = capbuf,
793 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
794 capbuf[OSC_SUPPORT_DWORD] = 1;
795 capbuf[OSC_CONTROL_DWORD] = 0;
797 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
798 || ACPI_FAILURE(acpi_run_osc(handle, &context)))
801 kfree(context.ret.pointer);
805 EXPORT_SYMBOL_GPL(apei_osc_setup);