1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/export.h>
32#include <linux/kernel.h>
33#include <linux/acpi.h>
34#include <linux/cper.h>
35#include <acpi/apei.h>
36#include <acpi/ghes.h>
37#include <asm/mce.h>
38
39#include "mce-internal.h"
40
41void apei_mce_report_mem_error(int severity, struct cper_sec_mem_err *mem_err)
42{
43 struct mce m;
44
45 if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
46 return;
47
48 mce_setup(&m);
49 m.bank = 1;
50
51 m.status = MCI_STATUS_VAL | MCI_STATUS_EN | MCI_STATUS_ADDRV | 0x9f;
52
53 if (severity >= GHES_SEV_RECOVERABLE)
54 m.status |= MCI_STATUS_UC;
55 if (severity >= GHES_SEV_PANIC)
56 m.status |= MCI_STATUS_PCC;
57
58 m.addr = mem_err->physical_addr;
59 mce_log(&m);
60}
61EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
62
63#define CPER_CREATOR_MCE \
64 UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
65 0x64, 0x90, 0xb8, 0x9d)
66#define CPER_SECTION_TYPE_MCE \
67 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
68 0x04, 0x4a, 0x38, 0xfc)
69
70
71
72
73
74struct cper_mce_record {
75 struct cper_record_header hdr;
76 struct cper_section_descriptor sec_hdr;
77 struct mce mce;
78} __packed;
79
80int apei_write_mce(struct mce *m)
81{
82 struct cper_mce_record rcd;
83
84 memset(&rcd, 0, sizeof(rcd));
85 memcpy(rcd.hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
86 rcd.hdr.revision = CPER_RECORD_REV;
87 rcd.hdr.signature_end = CPER_SIG_END;
88 rcd.hdr.section_count = 1;
89 rcd.hdr.error_severity = CPER_SEV_FATAL;
90
91 rcd.hdr.validation_bits = 0;
92 rcd.hdr.record_length = sizeof(rcd);
93 rcd.hdr.creator_id = CPER_CREATOR_MCE;
94 rcd.hdr.notification_type = CPER_NOTIFY_MCE;
95 rcd.hdr.record_id = cper_next_record_id();
96 rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
97
98 rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
99 rcd.sec_hdr.section_length = sizeof(rcd.mce);
100 rcd.sec_hdr.revision = CPER_SEC_REV;
101
102 rcd.sec_hdr.validation_bits = 0;
103 rcd.sec_hdr.flags = CPER_SEC_PRIMARY;
104 rcd.sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
105 rcd.sec_hdr.section_severity = CPER_SEV_FATAL;
106
107 memcpy(&rcd.mce, m, sizeof(*m));
108
109 return erst_write(&rcd.hdr);
110}
111
112ssize_t apei_read_mce(struct mce *m, u64 *record_id)
113{
114 struct cper_mce_record rcd;
115 int rc, pos;
116
117 rc = erst_get_record_id_begin(&pos);
118 if (rc)
119 return rc;
120retry:
121 rc = erst_get_record_id_next(&pos, record_id);
122 if (rc)
123 goto out;
124
125 if (*record_id == APEI_ERST_INVALID_RECORD_ID)
126 goto out;
127 rc = erst_read(*record_id, &rcd.hdr, sizeof(rcd));
128
129 if (rc == -ENOENT)
130 goto retry;
131 else if (rc < 0)
132 goto out;
133
134 else if (rc != sizeof(rcd) ||
135 uuid_le_cmp(rcd.hdr.creator_id, CPER_CREATOR_MCE))
136 goto retry;
137 memcpy(m, &rcd.mce, sizeof(*m));
138 rc = sizeof(*m);
139out:
140 erst_get_record_id_end();
141
142 return rc;
143}
144
145
146int apei_check_mce(void)
147{
148 return erst_get_record_count();
149}
150
151int apei_clear_mce(u64 record_id)
152{
153 return erst_clear(record_id);
154}
155