1/*
2 * Bridge between MCE and APEI
3 *
4 * On some machine, corrected memory errors are reported via APEI
5 * generic hardware error source (GHES) instead of corrected Machine
6 * Check. These corrected memory errors can be reported to user space
7 * through /dev/mcelog via faking a corrected Machine Check, so that
8 * the error memory page can be offlined by /sbin/mcelog if the error
9 * count for one page is beyond the threshold.
10 *
11 * For fatal MCE, save MCE record into persistent storage via ERST, so
12 * that the MCE record can be logged after reboot via ERST.
13 *
14 * Copyright 2010 Intel Corp.
15 *   Author: Huang Ying <ying.huang@intel.com>
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License version
19 * 2 as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 <asm/mce.h>
37
38#include "mce-internal.h"
39
40void apei_mce_report_mem_error(int corrected, struct cper_sec_mem_err *mem_err)
41{
42	struct mce m;
43
44	/* Only corrected MC is reported */
45	if (!corrected)
46		return;
47
48	mce_setup(&m);
49	m.bank = 1;
50	/* Fake a memory read corrected error with unknown channel */
51	m.status = MCI_STATUS_VAL | MCI_STATUS_EN | MCI_STATUS_ADDRV | 0x9f;
52	m.addr = mem_err->physical_addr;
53	mce_log(&m);
54	mce_notify_irq();
55}
56EXPORT_SYMBOL_GPL(apei_mce_report_mem_error);
57
58#define CPER_CREATOR_MCE						\
59	UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c,	\
60		0x64, 0x90, 0xb8, 0x9d)
61#define CPER_SECTION_TYPE_MCE						\
62	UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96,	\
63		0x04, 0x4a, 0x38, 0xfc)
64
65/*
66 * CPER specification (in UEFI specification 2.3 appendix N) requires
67 * byte-packed.
68 */
69struct cper_mce_record {
70	struct cper_record_header hdr;
71	struct cper_section_descriptor sec_hdr;
72	struct mce mce;
73} __packed;
74
75int apei_write_mce(struct mce *m)
76{
77	struct cper_mce_record rcd;
78
79	memset(&rcd, 0, sizeof(rcd));
80	memcpy(rcd.hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
81	rcd.hdr.revision = CPER_RECORD_REV;
82	rcd.hdr.signature_end = CPER_SIG_END;
83	rcd.hdr.section_count = 1;
84	rcd.hdr.error_severity = CPER_SEV_FATAL;
85	/* timestamp, platform_id, partition_id are all invalid */
86	rcd.hdr.validation_bits = 0;
87	rcd.hdr.record_length = sizeof(rcd);
88	rcd.hdr.creator_id = CPER_CREATOR_MCE;
89	rcd.hdr.notification_type = CPER_NOTIFY_MCE;
90	rcd.hdr.record_id = cper_next_record_id();
91	rcd.hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
92
93	rcd.sec_hdr.section_offset = (void *)&rcd.mce - (void *)&rcd;
94	rcd.sec_hdr.section_length = sizeof(rcd.mce);
95	rcd.sec_hdr.revision = CPER_SEC_REV;
96	/* fru_id and fru_text is invalid */
97	rcd.sec_hdr.validation_bits = 0;
98	rcd.sec_hdr.flags = CPER_SEC_PRIMARY;
99	rcd.sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
100	rcd.sec_hdr.section_severity = CPER_SEV_FATAL;
101
102	memcpy(&rcd.mce, m, sizeof(*m));
103
104	return erst_write(&rcd.hdr);
105}
106
107ssize_t apei_read_mce(struct mce *m, u64 *record_id)
108{
109	struct cper_mce_record rcd;
110	int rc, pos;
111
112	rc = erst_get_record_id_begin(&pos);
113	if (rc)
114		return rc;
115retry:
116	rc = erst_get_record_id_next(&pos, record_id);
117	if (rc)
118		goto out;
119	/* no more record */
120	if (*record_id == APEI_ERST_INVALID_RECORD_ID)
121		goto out;
122	rc = erst_read(*record_id, &rcd.hdr, sizeof(rcd));
123	/* someone else has cleared the record, try next one */
124	if (rc == -ENOENT)
125		goto retry;
126	else if (rc < 0)
127		goto out;
128	/* try to skip other type records in storage */
129	else if (rc != sizeof(rcd) ||
130		 uuid_le_cmp(rcd.hdr.creator_id, CPER_CREATOR_MCE))
131		goto retry;
132	memcpy(m, &rcd.mce, sizeof(*m));
133	rc = sizeof(*m);
134out:
135	erst_get_record_id_end();
136
137	return rc;
138}
139
140/* Check whether there is record in ERST */
141int apei_check_mce(void)
142{
143	return erst_get_record_count();
144}
145
146int apei_clear_mce(u64 record_id)
147{
148	return erst_clear(record_id);
149}
150