]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/kernel/cpu/mce/apei.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / cpu / mce / apei.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
d334a491
HY
2/*
3 * Bridge between MCE and APEI
4 *
5 * On some machine, corrected memory errors are reported via APEI
6 * generic hardware error source (GHES) instead of corrected Machine
7 * Check. These corrected memory errors can be reported to user space
8 * through /dev/mcelog via faking a corrected Machine Check, so that
9 * the error memory page can be offlined by /sbin/mcelog if the error
10 * count for one page is beyond the threshold.
11 *
482908b4
HY
12 * For fatal MCE, save MCE record into persistent storage via ERST, so
13 * that the MCE record can be logged after reboot via ERST.
14 *
d334a491
HY
15 * Copyright 2010 Intel Corp.
16 * Author: Huang Ying <ying.huang@intel.com>
d334a491
HY
17 */
18
69c60c88 19#include <linux/export.h>
d334a491
HY
20#include <linux/kernel.h>
21#include <linux/acpi.h>
22#include <linux/cper.h>
23#include <acpi/apei.h>
addccbb2 24#include <acpi/ghes.h>
d334a491
HY
25#include <asm/mce.h>
26
21afaf18 27#include "internal.h"
d334a491 28
addccbb2 29void apei_mce_report_mem_error(int severity, struct cper_sec_mem_err *mem_err)
d334a491
HY
30{
31 struct mce m;
32
addccbb2 33 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
d334a491
HY
34 return;
35
36 mce_setup(&m);
b2de4360 37 m.bank = -1;
addccbb2 38 /* Fake a memory read error with unknown channel */
d334a491 39 m.status = MCI_STATUS_VAL | MCI_STATUS_EN | MCI_STATUS_ADDRV | 0x9f;
addccbb2
CG
40
41 if (severity >= GHES_SEV_RECOVERABLE)
42 m.status |= MCI_STATUS_UC;
669c00f0
BP
43
44 if (severity >= GHES_SEV_PANIC) {
addccbb2 45 m.status |= MCI_STATUS_PCC;
669c00f0
BP
46 m.tsc = rdtsc();
47 }
addccbb2 48
d334a491
HY
49 m.addr = mem_err->physical_addr;
50 mce_log(&m);
d334a491
HY
51}
52EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
482908b4
HY
53
54#define CPER_CREATOR_MCE \
b62928ff
AS
55 GUID_INIT(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
56 0x64, 0x90, 0xb8, 0x9d)
482908b4 57#define CPER_SECTION_TYPE_MCE \
b62928ff
AS
58 GUID_INIT(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
59 0x04, 0x4a, 0x38, 0xfc)
482908b4
HY
60
61/*
62 * CPER specification (in UEFI specification 2.3 appendix N) requires
63 * byte-packed.
64 */
65struct cper_mce_record {
66 struct cper_record_header hdr;
67 struct cper_section_descriptor sec_hdr;
68 struct mce mce;
69} __packed;
70
71int apei_write_mce(struct mce *m)
72{
73 struct cper_mce_record rcd;
74
75 memset(&rcd, 0, sizeof(rcd));
76 memcpy(rcd.hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
77 rcd.hdr.revision = CPER_RECORD_REV;
78 rcd.hdr.signature_end = CPER_SIG_END;
79 rcd.hdr.section_count = 1;
ad4ecef2 80 rcd.hdr.error_severity = CPER_SEV_FATAL;
482908b4
HY
81 /* timestamp, platform_id, partition_id are all invalid */
82 rcd.hdr.validation_bits = 0;
83 rcd.hdr.record_length = sizeof(rcd);
84 rcd.hdr.creator_id = CPER_CREATOR_MCE;
85 rcd.hdr.notification_type = CPER_NOTIFY_MCE;
86 rcd.hdr.record_id = cper_next_record_id();
87 rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
88
89 rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
90 rcd.sec_hdr.section_length = sizeof(rcd.mce);
91 rcd.sec_hdr.revision = CPER_SEC_REV;
92 /* fru_id and fru_text is invalid */
93 rcd.sec_hdr.validation_bits = 0;
94 rcd.sec_hdr.flags = CPER_SEC_PRIMARY;
95 rcd.sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
ad4ecef2 96 rcd.sec_hdr.section_severity = CPER_SEV_FATAL;
482908b4
HY
97
98 memcpy(&rcd.mce, m, sizeof(*m));
99
100 return erst_write(&rcd.hdr);
101}
102
103ssize_t apei_read_mce(struct mce *m, u64 *record_id)
104{
105 struct cper_mce_record rcd;
885b976f
HY
106 int rc, pos;
107
108 rc = erst_get_record_id_begin(&pos);
109 if (rc)
110 return rc;
111retry:
112 rc = erst_get_record_id_next(&pos, record_id);
113 if (rc)
114 goto out;
115 /* no more record */
116 if (*record_id == APEI_ERST_INVALID_RECORD_ID)
117 goto out;
118 rc = erst_read(*record_id, &rcd.hdr, sizeof(rcd));
119 /* someone else has cleared the record, try next one */
120 if (rc == -ENOENT)
121 goto retry;
122 else if (rc < 0)
123 goto out;
124 /* try to skip other type records in storage */
125 else if (rc != sizeof(rcd) ||
b62928ff 126 !guid_equal(&rcd.hdr.creator_id, &CPER_CREATOR_MCE))
885b976f 127 goto retry;
482908b4 128 memcpy(m, &rcd.mce, sizeof(*m));
885b976f
HY
129 rc = sizeof(*m);
130out:
131 erst_get_record_id_end();
482908b4 132
885b976f 133 return rc;
482908b4
HY
134}
135
136/* Check whether there is record in ERST */
137int apei_check_mce(void)
138{
139 return erst_get_record_count();
140}
141
142int apei_clear_mce(u64 record_id)
143{
144 return erst_clear(record_id);
145}