acpi_extlog.c 8.72 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12
/*
 * Extended Error Log driver
 *
 * Copyright (C) 2013 Intel Corp.
 * Author: Chen, Gong <gong.chen@intel.com>
 */

#include <linux/module.h>
#include <linux/acpi.h>
#include <linux/cper.h>
#include <linux/ratelimit.h>
13
#include <linux/edac.h>
14
#include <linux/ras.h>
15 16 17 18
#include <asm/cpu.h>
#include <asm/mce.h>

#include "apei/apei-internal.h"
19
#include <ras/ras_event.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#define EXT_ELOG_ENTRY_MASK	GENMASK_ULL(51, 0) /* elog entry address mask */

#define EXTLOG_DSM_REV		0x0
#define	EXTLOG_FN_ADDR		0x1

#define FLAG_OS_OPTIN		BIT(0)
#define ELOG_ENTRY_VALID	(1ULL<<63)
#define ELOG_ENTRY_LEN		0x1000

#define EMCA_BUG \
	"Can not request iomem region <0x%016llx-0x%016llx> - eMCA disabled\n"

struct extlog_l1_head {
	u32 ver;	/* Header Version */
	u32 hdr_len;	/* Header Length */
	u64 total_len;	/* entire L1 Directory length including this header */
	u64 elog_base;	/* MCA Error Log Directory base address */
	u64 elog_len;	/* MCA Error Log Directory length */
	u32 flags;	/* bit 0 - OS/VMM Opt-in */
	u8  rev0[12];
	u32 entries;	/* Valid L1 Directory entries per logical processor */
	u8  rev1[12];
};

45 46
static int old_edac_report_status;

47
static u8 extlog_dsm_uuid[] __initdata = "663E35AF-CC10-41A4-88EA-5470AF055295";
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

/* L1 table related physical address */
static u64 elog_base;
static size_t elog_size;
static u64 l1_dirbase;
static size_t l1_size;

/* L1 table related virtual address */
static void __iomem *extlog_l1_addr;
static void __iomem *elog_addr;

static void *elog_buf;

static u64 *l1_entry_base;
static u32 l1_percpu_entry;

#define ELOG_IDX(cpu, bank) \
	(cpu_physical_id(cpu) * l1_percpu_entry + (bank))

#define ELOG_ENTRY_DATA(idx) \
	(*(l1_entry_base + (idx)))

#define ELOG_ENTRY_ADDR(phyaddr) \
	(phyaddr - elog_base + (u8 *)elog_addr)

73
static struct acpi_hest_generic_status *extlog_elog_entry_check(int cpu, int bank)
74 75 76
{
	int idx;
	u64 data;
77
	struct acpi_hest_generic_status *estatus;
78 79 80 81 82 83 84 85

	WARN_ON(cpu < 0);
	idx = ELOG_IDX(cpu, bank);
	data = ELOG_ENTRY_DATA(idx);
	if ((data & ELOG_ENTRY_VALID) == 0)
		return NULL;

	data &= EXT_ELOG_ENTRY_MASK;
86
	estatus = (struct acpi_hest_generic_status *)ELOG_ENTRY_ADDR(data);
87 88 89 90 91 92 93 94 95

	/* if no valid data in elog entry, just return */
	if (estatus->block_status == 0)
		return NULL;

	return estatus;
}

static void __print_extlog_rcd(const char *pfx,
96
			       struct acpi_hest_generic_status *estatus, int cpu)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
	static atomic_t seqno;
	unsigned int curr_seqno;
	char pfx_seq[64];

	if (!pfx) {
		if (estatus->error_severity <= CPER_SEV_CORRECTED)
			pfx = KERN_INFO;
		else
			pfx = KERN_ERR;
	}
	curr_seqno = atomic_inc_return(&seqno);
	snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}", pfx, curr_seqno);
	printk("%s""Hardware error detected on CPU%d\n", pfx_seq, cpu);
	cper_estatus_print(pfx_seq, estatus);
}

static int print_extlog_rcd(const char *pfx,
115
			    struct acpi_hest_generic_status *estatus, int cpu)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
	/* Not more than 2 messages every 5 seconds */
	static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
	static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
	struct ratelimit_state *ratelimit;

	if (estatus->error_severity == CPER_SEV_CORRECTED ||
	    (estatus->error_severity == CPER_SEV_INFORMATIONAL))
		ratelimit = &ratelimit_corrected;
	else
		ratelimit = &ratelimit_uncorrected;
	if (__ratelimit(ratelimit)) {
		__print_extlog_rcd(pfx, estatus, cpu);
		return 0;
	}

	return 1;
}

static int extlog_print(struct notifier_block *nb, unsigned long val,
			void *data)
{
	struct mce *mce = (struct mce *)data;
	int	bank = mce->bank;
	int	cpu = mce->extcpu;
141 142
	struct acpi_hest_generic_status *estatus, *tmp;
	struct acpi_hest_generic_data *gdata;
143
	const guid_t *fru_id = &guid_null;
144
	char *fru_text = "";
145
	guid_t *sec_type;
146
	static u32 err_seq;
147 148 149 150 151 152 153 154 155

	estatus = extlog_elog_entry_check(cpu, bank);
	if (estatus == NULL)
		return NOTIFY_DONE;

	memcpy(elog_buf, (void *)estatus, ELOG_ENTRY_LEN);
	/* clear record status to enable BIOS to update it again */
	estatus->block_status = 0;

156
	tmp = (struct acpi_hest_generic_status *)elog_buf;
157 158 159 160 161

	if (!ras_userspace_consumers()) {
		print_extlog_rcd(NULL, tmp, cpu);
		goto out;
	}
162 163 164

	/* log event via trace */
	err_seq++;
165
	gdata = (struct acpi_hest_generic_data *)(tmp + 1);
166
	if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
167
		fru_id = (guid_t *)gdata->fru_id;
168 169
	if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
		fru_text = gdata->fru_text;
170 171
	sec_type = (guid_t *)gdata->section_type;
	if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
172 173 174 175 176
		struct cper_sec_mem_err *mem = (void *)(gdata + 1);
		if (gdata->error_data_length >= sizeof(*mem))
			trace_extlog_mem_event(mem, err_seq, fru_id, fru_text,
					       (u8)gdata->error_severity);
	}
177

178
out:
179
	return NOTIFY_STOP;
180 181
}

182
static bool __init extlog_get_l1addr(void)
183
{
184
	guid_t guid;
185 186
	acpi_handle handle;
	union acpi_object *obj;
187

188 189
	if (guid_parse(extlog_dsm_uuid, &guid))
		return false;
190 191
	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
		return false;
192
	if (!acpi_check_dsm(handle, &guid, EXTLOG_DSM_REV, 1 << EXTLOG_FN_ADDR))
193
		return false;
194
	obj = acpi_evaluate_dsm_typed(handle, &guid, EXTLOG_DSM_REV,
195 196
				      EXTLOG_FN_ADDR, NULL, ACPI_TYPE_INTEGER);
	if (!obj) {
197
		return false;
198 199 200 201
	} else {
		l1_dirbase = obj->integer.value;
		ACPI_FREE(obj);
	}
202 203 204 205 206 207 208 209 210 211 212 213

	/* Spec says L1 directory must be 4K aligned, bail out if it isn't */
	if (l1_dirbase & ((1 << 12) - 1)) {
		pr_warn(FW_BUG "L1 Directory is invalid at physical %llx\n",
			l1_dirbase);
		return false;
	}

	return true;
}
static struct notifier_block extlog_mce_dec = {
	.notifier_call	= extlog_print,
214
	.priority	= MCE_PRIO_EXTLOG,
215 216 217 218 219 220 221 222 223 224 225
};

static int __init extlog_init(void)
{
	struct extlog_l1_head *l1_head;
	void __iomem *extlog_l1_hdr;
	size_t l1_hdr_size;
	struct resource *r;
	u64 cap;
	int rc;

226 227 228 229 230
	rdmsrl(MSR_IA32_MCG_CAP, cap);

	if (!(cap & MCG_ELOG_P) || !extlog_get_l1addr())
		return -ENODEV;

231
	if (edac_get_report_status() == EDAC_REPORTING_FORCE) {
232 233 234
		pr_warn("Not loading eMCA, error reporting force-enabled through EDAC.\n");
		return -EPERM;
	}
235 236 237 238 239 240 241 242 243 244 245 246

	rc = -EINVAL;
	/* get L1 header to fetch necessary information */
	l1_hdr_size = sizeof(struct extlog_l1_head);
	r = request_mem_region(l1_dirbase, l1_hdr_size, "L1 DIR HDR");
	if (!r) {
		pr_warn(FW_BUG EMCA_BUG,
			(unsigned long long)l1_dirbase,
			(unsigned long long)l1_dirbase + l1_hdr_size);
		goto err;
	}

247
	extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size);
248 249 250 251 252
	l1_head = (struct extlog_l1_head *)extlog_l1_hdr;
	l1_size = l1_head->total_len;
	l1_percpu_entry = l1_head->entries;
	elog_base = l1_head->elog_base;
	elog_size = l1_head->elog_len;
253
	acpi_os_unmap_iomem(extlog_l1_hdr, l1_hdr_size);
254 255 256 257 258 259 260 261 262 263
	release_mem_region(l1_dirbase, l1_hdr_size);

	/* remap L1 header again based on completed information */
	r = request_mem_region(l1_dirbase, l1_size, "L1 Table");
	if (!r) {
		pr_warn(FW_BUG EMCA_BUG,
			(unsigned long long)l1_dirbase,
			(unsigned long long)l1_dirbase + l1_size);
		goto err;
	}
264
	extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size);
265 266 267 268 269 270 271 272 273 274
	l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size);

	/* remap elog table */
	r = request_mem_region(elog_base, elog_size, "Elog Table");
	if (!r) {
		pr_warn(FW_BUG EMCA_BUG,
			(unsigned long long)elog_base,
			(unsigned long long)elog_base + elog_size);
		goto err_release_l1_dir;
	}
275
	elog_addr = acpi_os_map_iomem(elog_base, elog_size);
276 277 278 279 280 281 282

	rc = -ENOMEM;
	/* allocate buffer to save elog record */
	elog_buf = kmalloc(ELOG_ENTRY_LEN, GFP_KERNEL);
	if (elog_buf == NULL)
		goto err_release_elog;

283 284 285 286
	/*
	 * eMCA event report method has higher priority than EDAC method,
	 * unless EDAC event report method is mandatory.
	 */
287 288
	old_edac_report_status = edac_get_report_status();
	edac_set_report_status(EDAC_REPORTING_DISABLED);
289 290 291 292 293 294 295 296
	mce_register_decode_chain(&extlog_mce_dec);
	/* enable OS to be involved to take over management from BIOS */
	((struct extlog_l1_head *)extlog_l1_addr)->flags |= FLAG_OS_OPTIN;

	return 0;

err_release_elog:
	if (elog_addr)
297
		acpi_os_unmap_iomem(elog_addr, elog_size);
298 299 300
	release_mem_region(elog_base, elog_size);
err_release_l1_dir:
	if (extlog_l1_addr)
301
		acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
302 303 304 305 306 307 308 309
	release_mem_region(l1_dirbase, l1_size);
err:
	pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n");
	return rc;
}

static void __exit extlog_exit(void)
{
310
	edac_set_report_status(old_edac_report_status);
311 312 313
	mce_unregister_decode_chain(&extlog_mce_dec);
	((struct extlog_l1_head *)extlog_l1_addr)->flags &= ~FLAG_OS_OPTIN;
	if (extlog_l1_addr)
314
		acpi_os_unmap_iomem(extlog_l1_addr, l1_size);
315
	if (elog_addr)
316
		acpi_os_unmap_iomem(elog_addr, elog_size);
317 318 319 320 321 322 323 324 325 326 327
	release_mem_region(elog_base, elog_size);
	release_mem_region(l1_dirbase, l1_size);
	kfree(elog_buf);
}

module_init(extlog_init);
module_exit(extlog_exit);

MODULE_AUTHOR("Chen, Gong <gong.chen@intel.com>");
MODULE_DESCRIPTION("Extended MCA Error Log Driver");
MODULE_LICENSE("GPL");