ghes.c 39.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * APEI Generic Hardware Error Source support
 *
 * Generic Hardware Error Source provides a way to report platform
 * hardware errors (such as that from chipset). It works in so called
 * "Firmware First" mode, that is, hardware errors are reported to
 * firmware firstly, then reported to Linux by firmware. This way,
 * some non-standard hardware error registers or non-standard hardware
 * link can be checked by firmware to produce more hardware error
 * information for Linux.
 *
 * For more information about Generic Hardware Error Source, please
 * refer to ACPI Specification version 4.0, section 17.3.2.6
 *
16
 * Copyright 2010,2011 Intel Corp.
17 18 19
 *   Author: Huang Ying <ying.huang@intel.com>
 */

20
#include <linux/arm_sdei.h>
21
#include <linux/kernel.h>
22
#include <linux/moduleparam.h>
23 24 25 26
#include <linux/init.h>
#include <linux/acpi.h>
#include <linux/io.h>
#include <linux/interrupt.h>
27
#include <linux/timer.h>
28
#include <linux/cper.h>
29 30
#include <linux/platform_device.h>
#include <linux/mutex.h>
31
#include <linux/ratelimit.h>
32
#include <linux/vmalloc.h>
33 34 35
#include <linux/irq_work.h>
#include <linux/llist.h>
#include <linux/genalloc.h>
36
#include <linux/pci.h>
37
#include <linux/pfn.h>
38
#include <linux/aer.h>
39
#include <linux/nmi.h>
40
#include <linux/sched/clock.h>
41 42
#include <linux/uuid.h>
#include <linux/ras.h>
43
#include <linux/task_work.h>
44

45
#include <acpi/actbl1.h>
46
#include <acpi/ghes.h>
47
#include <acpi/apei.h>
48
#include <asm/fixmap.h>
49
#include <asm/tlbflush.h>
50
#include <ras/ras_event.h>
51 52 53 54 55 56

#include "apei-internal.h"

#define GHES_PFX	"GHES: "

#define GHES_ESTATUS_MAX_SIZE		65536
57 58 59 60
#define GHES_ESOURCE_PREALLOC_MAX_SIZE	65536

#define GHES_ESTATUS_POOL_MIN_ALLOC_ORDER 3

61 62 63 64 65
/* This is just an estimation for memory pool allocation */
#define GHES_ESTATUS_CACHE_AVG_SIZE	512

#define GHES_ESTATUS_CACHES_SIZE	4

L
Len Brown 已提交
66
#define GHES_ESTATUS_IN_CACHE_MAX_NSEC	10000000000ULL
67 68 69 70 71 72
/* Prevent too many caches are allocated because of RCU */
#define GHES_ESTATUS_CACHE_ALLOCED_MAX	(GHES_ESTATUS_CACHES_SIZE * 3 / 2)

#define GHES_ESTATUS_CACHE_LEN(estatus_len)			\
	(sizeof(struct ghes_estatus_cache) + (estatus_len))
#define GHES_ESTATUS_FROM_CACHE(estatus_cache)			\
73
	((struct acpi_hest_generic_status *)				\
74 75
	 ((struct ghes_estatus_cache *)(estatus_cache) + 1))

76 77
#define GHES_ESTATUS_NODE_LEN(estatus_len)			\
	(sizeof(struct ghes_estatus_node) + (estatus_len))
C
Chen, Gong 已提交
78
#define GHES_ESTATUS_FROM_NODE(estatus_node)			\
79
	((struct acpi_hest_generic_status *)				\
80
	 ((struct ghes_estatus_node *)(estatus_node) + 1))
81

82 83 84 85 86 87
#define GHES_VENDOR_ENTRY_LEN(gdata_len)                               \
	(sizeof(struct ghes_vendor_record_entry) + (gdata_len))
#define GHES_GDATA_FROM_VENDOR_ENTRY(vendor_entry)                     \
	((struct acpi_hest_generic_data *)                              \
	((struct ghes_vendor_record_entry *)(vendor_entry) + 1))

88 89 90 91 92 93 94 95 96
/*
 *  NMI-like notifications vary by architecture, before the compiler can prune
 *  unused static functions it needs a value for these enums.
 */
#ifndef CONFIG_ARM_SDE_INTERFACE
#define FIX_APEI_GHES_SDEI_NORMAL	__end_of_fixed_addresses
#define FIX_APEI_GHES_SDEI_CRITICAL	__end_of_fixed_addresses
#endif

97 98 99 100 101
static inline bool is_hest_type_generic_v2(struct ghes *ghes)
{
	return ghes->generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
}

102 103 104 105 106
/*
 * This driver isn't really modular, however for the time being,
 * continuing to use module_param is the easiest way to remain
 * compatible with existing boot arg use cases.
 */
107
bool ghes_disable;
108 109
module_param_named(disable, ghes_disable, bool, 0);

110
/*
111 112 113
 * All error sources notified with HED (Hardware Error Device) share a
 * single notifier callback, so they need to be linked and checked one
 * by one. This holds true for NMI too.
114
 *
115 116
 * RCU is used for these lists, so ghes_list_mutex is only used for
 * list changing, not for traversing.
117
 */
118
static LIST_HEAD(ghes_hed);
119
static DEFINE_MUTEX(ghes_list_mutex);
120

121
#ifdef CONFIG_ACPI_APEI_GHES_TS_CORE
G
Guo Mengqi 已提交
122 123
BLOCKING_NOTIFIER_HEAD(ghes_ts_err_chain);
EXPORT_SYMBOL(ghes_ts_err_chain);
124
#endif
G
Guo Mengqi 已提交
125

126 127 128 129
/*
 * Because the memory area used to transfer hardware error information
 * from BIOS to Linux can be determined only in NMI, IRQ or timer
 * handler, but general ioremap can not be used in atomic context, so
130
 * the fixmap is used instead.
131
 *
132
 * This spinlock is used to prevent the fixmap entry from being used
133
 * simultaneously.
134
 */
135
static DEFINE_SPINLOCK(ghes_notify_lock_irq);
136

137 138 139 140 141 142
struct ghes_vendor_record_entry {
	struct work_struct work;
	int error_severity;
	char vendor_record[];
};

143 144 145
static struct gen_pool *ghes_estatus_pool;
static unsigned long ghes_estatus_pool_size_request;

146
static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
147 148
static atomic_t ghes_estatus_cache_alloced;

149 150
static int ghes_panic_timeout __read_mostly = 30;

151
static void __iomem *ghes_map(u64 pfn, enum fixed_addresses fixmap_idx)
152
{
153 154
	phys_addr_t paddr;
	pgprot_t prot;
155

156
	paddr = PFN_PHYS(pfn);
157
	prot = arch_apei_get_mem_attribute(paddr);
158
	__set_fixmap(fixmap_idx, paddr, prot);
159

160
	return (void __iomem *) __fix_to_virt(fixmap_idx);
161 162
}

163
static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
164
{
165
	int _idx = virt_to_fix((unsigned long)vaddr);
166

167 168
	WARN_ON_ONCE(fixmap_idx != _idx);
	clear_fixmap(fixmap_idx);
169 170
}

171
int ghes_estatus_pool_init(int num_ghes)
172
{
173
	unsigned long addr, len;
174
	int rc;
175

176 177 178 179
	ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
	if (!ghes_estatus_pool)
		return -ENOMEM;

180 181
	len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
	len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
182

183
	ghes_estatus_pool_size_request = PAGE_ALIGN(len);
184 185
	addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
	if (!addr)
186
		goto err_pool_alloc;
187

188 189 190 191 192 193 194 195 196 197 198 199 200
	rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
	if (rc)
		goto err_pool_add;

	return 0;

err_pool_add:
	vfree((void *)addr);

err_pool_alloc:
	gen_pool_destroy(ghes_estatus_pool);

	return -ENOMEM;
201 202
}

203 204 205 206 207 208 209 210 211 212
static int map_gen_v2(struct ghes *ghes)
{
	return apei_map_generic_address(&ghes->generic_v2->read_ack_register);
}

static void unmap_gen_v2(struct ghes *ghes)
{
	apei_unmap_generic_address(&ghes->generic_v2->read_ack_register);
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
static void ghes_ack_error(struct acpi_hest_generic_v2 *gv2)
{
	int rc;
	u64 val = 0;

	rc = apei_read(&val, &gv2->read_ack_register);
	if (rc)
		return;

	val &= gv2->read_ack_preserve << gv2->read_ack_register.bit_offset;
	val |= gv2->read_ack_write    << gv2->read_ack_register.bit_offset;

	apei_write(val, &gv2->read_ack_register);
}

228 229 230 231 232 233 234 235 236
static struct ghes *ghes_new(struct acpi_hest_generic *generic)
{
	struct ghes *ghes;
	unsigned int error_block_length;
	int rc;

	ghes = kzalloc(sizeof(*ghes), GFP_KERNEL);
	if (!ghes)
		return ERR_PTR(-ENOMEM);
237

238
	ghes->generic = generic;
239 240 241 242 243 244
	if (is_hest_type_generic_v2(ghes)) {
		rc = map_gen_v2(ghes);
		if (rc)
			goto err_free;
	}

245
	rc = apei_map_generic_address(&generic->error_status_address);
246
	if (rc)
247
		goto err_unmap_read_ack_addr;
248 249
	error_block_length = generic->error_block_length;
	if (error_block_length > GHES_ESTATUS_MAX_SIZE) {
250 251 252 253
		pr_warn(FW_WARN GHES_PFX
			"Error status block length is too long: %u for "
			"generic hardware error source: %d.\n",
			error_block_length, generic->header.source_id);
254 255 256 257 258
		error_block_length = GHES_ESTATUS_MAX_SIZE;
	}
	ghes->estatus = kmalloc(error_block_length, GFP_KERNEL);
	if (!ghes->estatus) {
		rc = -ENOMEM;
259
		goto err_unmap_status_addr;
260 261 262 263
	}

	return ghes;

264
err_unmap_status_addr:
265
	apei_unmap_generic_address(&generic->error_status_address);
266 267 268
err_unmap_read_ack_addr:
	if (is_hest_type_generic_v2(ghes))
		unmap_gen_v2(ghes);
269 270 271 272 273 274 275 276
err_free:
	kfree(ghes);
	return ERR_PTR(rc);
}

static void ghes_fini(struct ghes *ghes)
{
	kfree(ghes->estatus);
277
	apei_unmap_generic_address(&ghes->generic->error_status_address);
278 279
	if (is_hest_type_generic_v2(ghes))
		unmap_gen_v2(ghes);
280 281 282 283 284
}

static inline int ghes_severity(int severity)
{
	switch (severity) {
285 286 287 288 289 290 291 292
	case CPER_SEV_INFORMATIONAL:
		return GHES_SEV_NO;
	case CPER_SEV_CORRECTED:
		return GHES_SEV_CORRECTED;
	case CPER_SEV_RECOVERABLE:
		return GHES_SEV_RECOVERABLE;
	case CPER_SEV_FATAL:
		return GHES_SEV_PANIC;
293
	default:
L
Lucas De Marchi 已提交
294
		/* Unknown, go panic */
295
		return GHES_SEV_PANIC;
296 297 298
	}
}

299
static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
300 301
				  int from_phys,
				  enum fixed_addresses fixmap_idx)
302
{
303 304 305 306 307 308
	void __iomem *vaddr;
	u64 offset;
	u32 trunk;

	while (len > 0) {
		offset = paddr - (paddr & PAGE_MASK);
309
		vaddr = ghes_map(PHYS_PFN(paddr), fixmap_idx);
310 311 312 313 314 315 316 317 318
		trunk = PAGE_SIZE - offset;
		trunk = min(trunk, len);
		if (from_phys)
			memcpy_fromio(buffer, vaddr + offset, trunk);
		else
			memcpy_toio(vaddr + offset, buffer, trunk);
		len -= trunk;
		paddr += trunk;
		buffer += trunk;
319
		ghes_unmap(vaddr, fixmap_idx);
320
	}
321 322
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/* Check the top-level record header has an appropriate size. */
static int __ghes_check_estatus(struct ghes *ghes,
				struct acpi_hest_generic_status *estatus)
{
	u32 len = cper_estatus_len(estatus);

	if (len < sizeof(*estatus)) {
		pr_warn_ratelimited(FW_WARN GHES_PFX "Truncated error status block!\n");
		return -EIO;
	}

	if (len > ghes->generic->error_block_length) {
		pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid error status block length!\n");
		return -EIO;
	}

	if (cper_estatus_check_header(estatus)) {
		pr_warn_ratelimited(FW_WARN GHES_PFX "Invalid CPER header!\n");
		return -EIO;
	}

	return 0;
}

347 348 349 350
/* Read the CPER block, returning its address, and header in estatus. */
static int __ghes_peek_estatus(struct ghes *ghes,
			       struct acpi_hest_generic_status *estatus,
			       u64 *buf_paddr, enum fixed_addresses fixmap_idx)
351 352 353 354
{
	struct acpi_hest_generic *g = ghes->generic;
	int rc;

355
	rc = apei_read(buf_paddr, &g->error_status_address);
356
	if (rc) {
357
		*buf_paddr = 0;
358
		pr_warn_ratelimited(FW_WARN GHES_PFX
359 360 361 362
"Failed to read error status block address for hardware error source: %d.\n",
				   g->header.source_id);
		return -EIO;
	}
363
	if (!*buf_paddr)
364 365
		return -ENOENT;

366 367 368
	ghes_copy_tofrom_phys(estatus, *buf_paddr, sizeof(*estatus), 1,
			      fixmap_idx);
	if (!estatus->block_status) {
369
		*buf_paddr = 0;
370
		return -ENOENT;
371
	}
372

373
	return 0;
374
}
375

376 377 378 379 380
static int __ghes_read_estatus(struct acpi_hest_generic_status *estatus,
			       u64 buf_paddr, enum fixed_addresses fixmap_idx,
			       size_t buf_len)
{
	ghes_copy_tofrom_phys(estatus, buf_paddr, buf_len, 1, fixmap_idx);
381
	if (cper_estatus_check(estatus)) {
382 383
		pr_warn_ratelimited(FW_WARN GHES_PFX
				    "Failed to read error status block!\n");
384 385
		return -EIO;
	}
386

387
	return 0;
388 389
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
static int ghes_read_estatus(struct ghes *ghes,
			     struct acpi_hest_generic_status *estatus,
			     u64 *buf_paddr, enum fixed_addresses fixmap_idx)
{
	int rc;

	rc = __ghes_peek_estatus(ghes, estatus, buf_paddr, fixmap_idx);
	if (rc)
		return rc;

	rc = __ghes_check_estatus(ghes, estatus);
	if (rc)
		return rc;

	return __ghes_read_estatus(estatus, *buf_paddr, fixmap_idx,
				   cper_estatus_len(estatus));
}

408 409 410
static void ghes_clear_estatus(struct ghes *ghes,
			       struct acpi_hest_generic_status *estatus,
			       u64 buf_paddr, enum fixed_addresses fixmap_idx)
411
{
412
	estatus->block_status = 0;
413 414 415 416

	if (!buf_paddr)
		return;

417 418
	ghes_copy_tofrom_phys(estatus, buf_paddr,
			      sizeof(estatus->block_status), 0,
419
			      fixmap_idx);
420 421 422 423 424 425 426

	/*
	 * GHESv2 type HEST entries introduce support for error acknowledgment,
	 * so only acknowledge the error if this support is present.
	 */
	if (is_hest_type_generic_v2(ghes))
		ghes_ack_error(ghes->generic_v2);
427 428
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
/*
 * Called as task_work before returning to user-space.
 * Ensure any queued work has been done before we return to the context that
 * triggered the notification.
 */
static void ghes_kick_task_work(struct callback_head *head)
{
	struct acpi_hest_generic_status *estatus;
	struct ghes_estatus_node *estatus_node;
	u32 node_len;

	estatus_node = container_of(head, struct ghes_estatus_node, task_work);
	if (IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))
		memory_failure_queue_kick(estatus_node->task_work_cpu);

	estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
	node_len = GHES_ESTATUS_NODE_LEN(cper_estatus_len(estatus));
	gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node, node_len);
}

449
static bool ghes_do_memory_failure(u64 physical_addr, int flags)
450 451 452
{
	unsigned long pfn;

453 454 455
	if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))
		return false;

456
	pfn = PHYS_PFN(physical_addr);
457
	if (!pfn_valid(pfn) && !arch_is_platform_page(physical_addr)) {
458 459
		pr_warn_ratelimited(FW_WARN GHES_PFX
		"Invalid address in generic error data: %#llx\n",
460
		physical_addr);
461
		return false;
462
	}
463

464 465 466 467 468 469 470 471 472 473 474 475 476 477
	memory_failure_queue(pfn, flags);
	return true;
}

static bool ghes_handle_memory_failure(struct acpi_hest_generic_data *gdata,
				       int sev)
{
	int flags = -1;
	int sec_sev = ghes_severity(gdata->error_severity);
	struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);

	if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))
		return false;

478 479 480 481 482 483 484
	/* iff following two events can be handled properly by now */
	if (sec_sev == GHES_SEV_CORRECTED &&
	    (gdata->flags & CPER_SEC_ERROR_THRESHOLD_EXCEEDED))
		flags = MF_SOFT_OFFLINE;
	if (sev == GHES_SEV_RECOVERABLE && sec_sev == GHES_SEV_RECOVERABLE)
		flags = 0;

485 486
	if (flags != -1)
		return ghes_do_memory_failure(mem_err->physical_addr, flags);
487 488

	return false;
489 490
}

491 492 493 494 495 496 497 498
static bool ghes_handle_arm_hw_error(struct acpi_hest_generic_data *gdata, int sev)
{
	struct cper_sec_proc_arm *err = acpi_hest_get_payload(gdata);
	bool queued = false;
	int sec_sev, i;
	char *p;

	sec_sev = ghes_severity(gdata->error_severity);
499
	log_arm_hw_error(err, sec_sev);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	if (sev != GHES_SEV_RECOVERABLE || sec_sev != GHES_SEV_RECOVERABLE)
		return false;

	p = (char *)(err + 1);
	for (i = 0; i < err->err_info_num; i++) {
		struct cper_arm_err_info *err_info = (struct cper_arm_err_info *)p;
		bool is_cache = (err_info->type == CPER_ARM_CACHE_ERROR);
		bool has_pa = (err_info->validation_bits & CPER_ARM_INFO_VALID_PHYSICAL_ADDR);
		const char *error_type = "unknown error";

		/*
		 * The field (err_info->error_info & BIT(26)) is fixed to set to
		 * 1 in some old firmware of HiSilicon Kunpeng920. We assume that
		 * firmware won't mix corrected errors in an uncorrected section,
		 * and don't filter out 'corrected' error here.
		 */
		if (is_cache && has_pa) {
			queued = ghes_do_memory_failure(err_info->physical_fault_addr, 0);
			p += err_info->length;
			continue;
		}

		if (err_info->type < ARRAY_SIZE(cper_proc_error_type_strs))
			error_type = cper_proc_error_type_strs[err_info->type];

		pr_warn_ratelimited(FW_WARN GHES_PFX
				    "Unhandled processor error type: %s\n",
				    error_type);
		p += err_info->length;
	}

	return queued;
}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
/*
 * PCIe AER errors need to be sent to the AER driver for reporting and
 * recovery. The GHES severities map to the following AER severities and
 * require the following handling:
 *
 * GHES_SEV_CORRECTABLE -> AER_CORRECTABLE
 *     These need to be reported by the AER driver but no recovery is
 *     necessary.
 * GHES_SEV_RECOVERABLE -> AER_NONFATAL
 * GHES_SEV_RECOVERABLE && CPER_SEC_RESET -> AER_FATAL
 *     These both need to be reported and recovered from by the AER driver.
 * GHES_SEV_PANIC does not make it to this handling since the kernel must
 *     panic.
 */
static void ghes_handle_aer(struct acpi_hest_generic_data *gdata)
549 550 551 552
{
#ifdef CONFIG_ACPI_APEI_PCIEAER
	struct cper_sec_pcie *pcie_err = acpi_hest_get_payload(gdata);

553
	if (pcie_err->validation_bits & CPER_PCIE_VALID_DEVICE_ID &&
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	    pcie_err->validation_bits & CPER_PCIE_VALID_AER_INFO) {
		unsigned int devfn;
		int aer_severity;

		devfn = PCI_DEVFN(pcie_err->device_id.device,
				  pcie_err->device_id.function);
		aer_severity = cper_severity_to_aer(gdata->error_severity);

		/*
		 * If firmware reset the component to contain
		 * the error, we must reinitialize it before
		 * use, so treat it as a fatal AER error.
		 */
		if (gdata->flags & CPER_SEC_RESET)
			aer_severity = AER_FATAL;

		aer_recover_queue(pcie_err->device_id.segment,
				  pcie_err->device_id.bus,
				  devfn, aer_severity,
				  (struct aer_capability_regs *)
				  pcie_err->aer_info);
	}
#endif
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
static BLOCKING_NOTIFIER_HEAD(vendor_record_notify_list);

int ghes_register_vendor_record_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&vendor_record_notify_list, nb);
}
EXPORT_SYMBOL_GPL(ghes_register_vendor_record_notifier);

void ghes_unregister_vendor_record_notifier(struct notifier_block *nb)
{
	blocking_notifier_chain_unregister(&vendor_record_notify_list, nb);
}
EXPORT_SYMBOL_GPL(ghes_unregister_vendor_record_notifier);

static void ghes_vendor_record_work_func(struct work_struct *work)
{
	struct ghes_vendor_record_entry *entry;
	struct acpi_hest_generic_data *gdata;
	u32 len;

	entry = container_of(work, struct ghes_vendor_record_entry, work);
	gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);

	blocking_notifier_call_chain(&vendor_record_notify_list,
				     entry->error_severity, gdata);

	len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
	gen_pool_free(ghes_estatus_pool, (unsigned long)entry, len);
}

static void ghes_defer_non_standard_event(struct acpi_hest_generic_data *gdata,
					  int sev)
{
	struct acpi_hest_generic_data *copied_gdata;
	struct ghes_vendor_record_entry *entry;
	u32 len;

	len = GHES_VENDOR_ENTRY_LEN(acpi_hest_get_record_size(gdata));
	entry = (void *)gen_pool_alloc(ghes_estatus_pool, len);
	if (!entry)
		return;

	copied_gdata = GHES_GDATA_FROM_VENDOR_ENTRY(entry);
	memcpy(copied_gdata, gdata, acpi_hest_get_record_size(gdata));
	entry->error_severity = sev;

	INIT_WORK(&entry->work, ghes_vendor_record_work_func);
	schedule_work(&entry->work);
}

629
static bool ghes_do_proc(struct ghes *ghes,
630
			 const struct acpi_hest_generic_status *estatus)
631
{
632
	int sev, sec_sev;
633
	struct acpi_hest_generic_data *gdata;
634
	guid_t *sec_type;
635
	const guid_t *fru_id = &guid_null;
636
	char *fru_text = "";
637
	bool queued = false;
638

639 640
	sev = ghes_severity(estatus->error_severity);
	apei_estatus_for_each_section(estatus, gdata) {
641
		sec_type = (guid_t *)gdata->section_type;
642
		sec_sev = ghes_severity(gdata->error_severity);
643 644 645 646 647 648
		if (gdata->validation_bits & CPER_SEC_VALID_FRU_ID)
			fru_id = (guid_t *)gdata->fru_id;

		if (gdata->validation_bits & CPER_SEC_VALID_FRU_TEXT)
			fru_text = gdata->fru_text;

649
		if (guid_equal(sec_type, &CPER_SEC_PLATFORM_MEM)) {
650 651
			struct cper_sec_mem_err *mem_err = acpi_hest_get_payload(gdata);

652
			ghes_edac_report_mem_error(sev, mem_err);
653

654
			arch_apei_report_mem_error(sev, mem_err);
655
			queued = ghes_handle_memory_failure(gdata, sev);
656
		}
657
		else if (guid_equal(sec_type, &CPER_SEC_PCIE)) {
658
			ghes_handle_aer(gdata);
659
		}
660
		else if (guid_equal(sec_type, &CPER_SEC_PROC_ARM)) {
661
			queued = ghes_handle_arm_hw_error(gdata, sev);
662
#ifdef CONFIG_ACPI_APEI_GHES_TS_CORE
G
Guo Mengqi 已提交
663 664 665 666
		}
		else if (guid_equal(sec_type, &CPER_SEC_TS_CORE)) {
			blocking_notifier_call_chain(&ghes_ts_err_chain,
					0, acpi_hest_get_payload(gdata));
667
#endif
668
		} else {
669 670 671 672 673 674
			void *err = acpi_hest_get_payload(gdata);

			log_non_standard_event(sec_type, fru_id, fru_text,
					       sec_sev, err,
					       gdata->error_data_length);
		}
675 676 677

		/* Customization deliver all types error to driver. */
		ghes_defer_non_standard_event(gdata, sev);
678
	}
679 680

	return queued;
681
}
682

683 684
static void __ghes_print_estatus(const char *pfx,
				 const struct acpi_hest_generic *generic,
685
				 const struct acpi_hest_generic_status *estatus)
686
{
687 688 689 690
	static atomic_t seqno;
	unsigned int curr_seqno;
	char pfx_seq[64];

691
	if (pfx == NULL) {
692
		if (ghes_severity(estatus->error_severity) <=
693
		    GHES_SEV_CORRECTED)
694
			pfx = KERN_WARNING;
695
		else
696
			pfx = KERN_ERR;
697
	}
698 699
	curr_seqno = atomic_inc_return(&seqno);
	snprintf(pfx_seq, sizeof(pfx_seq), "%s{%u}" HW_ERR, pfx, curr_seqno);
700
	printk("%s""Hardware error from APEI Generic Hardware Error Source: %d\n",
701
	       pfx_seq, generic->header.source_id);
C
Chen, Gong 已提交
702
	cper_estatus_print(pfx_seq, estatus);
703 704
}

705 706
static int ghes_print_estatus(const char *pfx,
			      const struct acpi_hest_generic *generic,
707
			      const struct acpi_hest_generic_status *estatus)
708 709
{
	/* Not more than 2 messages every 5 seconds */
710 711 712
	static DEFINE_RATELIMIT_STATE(ratelimit_corrected, 5*HZ, 2);
	static DEFINE_RATELIMIT_STATE(ratelimit_uncorrected, 5*HZ, 2);
	struct ratelimit_state *ratelimit;
713

714 715 716 717
	if (ghes_severity(estatus->error_severity) <= GHES_SEV_CORRECTED)
		ratelimit = &ratelimit_corrected;
	else
		ratelimit = &ratelimit_uncorrected;
718
	if (__ratelimit(ratelimit)) {
719
		__ghes_print_estatus(pfx, generic, estatus);
720 721 722 723 724 725 726 727 728
		return 1;
	}
	return 0;
}

/*
 * GHES error status reporting throttle, to report more kinds of
 * errors, instead of just most frequently occurred errors.
 */
729
static int ghes_estatus_cached(struct acpi_hest_generic_status *estatus)
730 731 732 733 734
{
	u32 len;
	int i, cached = 0;
	unsigned long long now;
	struct ghes_estatus_cache *cache;
735
	struct acpi_hest_generic_status *cache_estatus;
736

C
Chen, Gong 已提交
737
	len = cper_estatus_len(estatus);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	rcu_read_lock();
	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
		cache = rcu_dereference(ghes_estatus_caches[i]);
		if (cache == NULL)
			continue;
		if (len != cache->estatus_len)
			continue;
		cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
		if (memcmp(estatus, cache_estatus, len))
			continue;
		atomic_inc(&cache->count);
		now = sched_clock();
		if (now - cache->time_in < GHES_ESTATUS_IN_CACHE_MAX_NSEC)
			cached = 1;
		break;
	}
	rcu_read_unlock();
	return cached;
}

static struct ghes_estatus_cache *ghes_estatus_cache_alloc(
	struct acpi_hest_generic *generic,
760
	struct acpi_hest_generic_status *estatus)
761 762 763 764
{
	int alloced;
	u32 len, cache_len;
	struct ghes_estatus_cache *cache;
765
	struct acpi_hest_generic_status *cache_estatus;
766 767 768 769 770 771

	alloced = atomic_add_return(1, &ghes_estatus_cache_alloced);
	if (alloced > GHES_ESTATUS_CACHE_ALLOCED_MAX) {
		atomic_dec(&ghes_estatus_cache_alloced);
		return NULL;
	}
C
Chen, Gong 已提交
772
	len = cper_estatus_len(estatus);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	cache_len = GHES_ESTATUS_CACHE_LEN(len);
	cache = (void *)gen_pool_alloc(ghes_estatus_pool, cache_len);
	if (!cache) {
		atomic_dec(&ghes_estatus_cache_alloced);
		return NULL;
	}
	cache_estatus = GHES_ESTATUS_FROM_CACHE(cache);
	memcpy(cache_estatus, estatus, len);
	cache->estatus_len = len;
	atomic_set(&cache->count, 0);
	cache->generic = generic;
	cache->time_in = sched_clock();
	return cache;
}

static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache)
{
	u32 len;

C
Chen, Gong 已提交
792
	len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache));
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	len = GHES_ESTATUS_CACHE_LEN(len);
	gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len);
	atomic_dec(&ghes_estatus_cache_alloced);
}

static void ghes_estatus_cache_rcu_free(struct rcu_head *head)
{
	struct ghes_estatus_cache *cache;

	cache = container_of(head, struct ghes_estatus_cache, rcu);
	ghes_estatus_cache_free(cache);
}

static void ghes_estatus_cache_add(
	struct acpi_hest_generic *generic,
808
	struct acpi_hest_generic_status *estatus)
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
{
	int i, slot = -1, count;
	unsigned long long now, duration, period, max_period = 0;
	struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache;

	new_cache = ghes_estatus_cache_alloc(generic, estatus);
	if (new_cache == NULL)
		return;
	rcu_read_lock();
	now = sched_clock();
	for (i = 0; i < GHES_ESTATUS_CACHES_SIZE; i++) {
		cache = rcu_dereference(ghes_estatus_caches[i]);
		if (cache == NULL) {
			slot = i;
			slot_cache = NULL;
			break;
		}
		duration = now - cache->time_in;
		if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) {
			slot = i;
			slot_cache = cache;
			break;
		}
		count = atomic_read(&cache->count);
L
Len Brown 已提交
833 834
		period = duration;
		do_div(period, (count + 1));
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
		if (period > max_period) {
			max_period = period;
			slot = i;
			slot_cache = cache;
		}
	}
	/* new_cache must be put into array after its contents are written */
	smp_wmb();
	if (slot != -1 && cmpxchg(ghes_estatus_caches + slot,
				  slot_cache, new_cache) == slot_cache) {
		if (slot_cache)
			call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free);
	} else
		ghes_estatus_cache_free(new_cache);
	rcu_read_unlock();
850 851
}

852 853 854
static void __ghes_panic(struct ghes *ghes,
			 struct acpi_hest_generic_status *estatus,
			 u64 buf_paddr, enum fixed_addresses fixmap_idx)
855
{
856
	__ghes_print_estatus(KERN_EMERG, ghes->generic, estatus);
857

858
	ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
859

860 861 862 863 864 865
	/* reboot to log the error! */
	if (!panic_timeout)
		panic_timeout = ghes_panic_timeout;
	panic("Fatal hardware error!");
}

866 867
static int ghes_proc(struct ghes *ghes)
{
868
	struct acpi_hest_generic_status *estatus = ghes->estatus;
869
	u64 buf_paddr;
870 871
	int rc;

872
	rc = ghes_read_estatus(ghes, estatus, &buf_paddr, FIX_APEI_GHES_IRQ);
873 874
	if (rc)
		goto out;
875

876 877
	if (ghes_severity(estatus->error_severity) >= GHES_SEV_PANIC)
		__ghes_panic(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
878

879 880 881
	if (!ghes_estatus_cached(estatus)) {
		if (ghes_print_estatus(NULL, ghes->generic, estatus))
			ghes_estatus_cache_add(ghes->generic, estatus);
882
	}
883
	ghes_do_proc(ghes, estatus);
884

885
out:
886
	ghes_clear_estatus(ghes, estatus, buf_paddr, FIX_APEI_GHES_IRQ);
887

888
	return rc;
889 890
}

891 892 893 894 895 896
static void ghes_add_timer(struct ghes *ghes)
{
	struct acpi_hest_generic *g = ghes->generic;
	unsigned long expire;

	if (!g->notify.poll_interval) {
897 898
		pr_warn(FW_WARN GHES_PFX "Poll interval is 0 for generic hardware error source: %d, disabled.\n",
			g->header.source_id);
899 900 901 902 903 904 905
		return;
	}
	expire = jiffies + msecs_to_jiffies(g->notify.poll_interval);
	ghes->timer.expires = round_jiffies_relative(expire);
	add_timer(&ghes->timer);
}

906
static void ghes_poll_func(struct timer_list *t)
907
{
908
	struct ghes *ghes = from_timer(ghes, t, timer);
909
	unsigned long flags;
910

911
	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
912
	ghes_proc(ghes);
913
	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
914 915 916 917 918 919 920
	if (!(ghes->flags & GHES_EXITING))
		ghes_add_timer(ghes);
}

static irqreturn_t ghes_irq_func(int irq, void *data)
{
	struct ghes *ghes = data;
921
	unsigned long flags;
922 923
	int rc;

924
	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
925
	rc = ghes_proc(ghes);
926
	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
927 928 929 930 931 932
	if (rc)
		return IRQ_NONE;

	return IRQ_HANDLED;
}

933 934
static int ghes_notify_hed(struct notifier_block *this, unsigned long event,
			   void *data)
935 936
{
	struct ghes *ghes;
937
	unsigned long flags;
938 939
	int ret = NOTIFY_DONE;

940
	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
941
	rcu_read_lock();
942
	list_for_each_entry_rcu(ghes, &ghes_hed, list) {
943 944 945 946
		if (!ghes_proc(ghes))
			ret = NOTIFY_OK;
	}
	rcu_read_unlock();
947
	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
948 949 950 951

	return ret;
}

952 953
static struct notifier_block ghes_notifier_hed = {
	.notifier_call = ghes_notify_hed,
954 955 956
};

/*
957 958 959 960 961 962 963 964 965
 * Handlers for CPER records may not be NMI safe. For example,
 * memory_failure_queue() takes spinlocks and calls schedule_work_on().
 * In any NMI-like handler, memory from ghes_estatus_pool is used to save
 * estatus, and added to the ghes_estatus_llist. irq_work_queue() causes
 * ghes_proc_in_irq() to run in IRQ context where each estatus in
 * ghes_estatus_llist is processed.
 *
 * Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache
 * to suppress frequent messages.
966 967 968 969
 */
static struct llist_head ghes_estatus_llist;
static struct irq_work ghes_proc_irq_work;

970 971
static void ghes_proc_in_irq(struct irq_work *irq_work)
{
972
	struct llist_node *llnode, *next;
973
	struct ghes_estatus_node *estatus_node;
974
	struct acpi_hest_generic *generic;
975
	struct acpi_hest_generic_status *estatus;
976
	bool task_work_pending;
977
	u32 len, node_len;
978
	int ret;
979

980
	llnode = llist_del_all(&ghes_estatus_llist);
981 982 983 984
	/*
	 * Because the time order of estatus in list is reversed,
	 * revert it back to proper order.
	 */
985
	llnode = llist_reverse_order(llnode);
986 987 988 989 990
	while (llnode) {
		next = llnode->next;
		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
					   llnode);
		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
C
Chen, Gong 已提交
991
		len = cper_estatus_len(estatus);
992
		node_len = GHES_ESTATUS_NODE_LEN(len);
993
		task_work_pending = ghes_do_proc(estatus_node->ghes, estatus);
994 995 996 997 998
		if (!ghes_estatus_cached(estatus)) {
			generic = estatus_node->generic;
			if (ghes_print_estatus(NULL, generic, estatus))
				ghes_estatus_cache_add(generic, estatus);
		}
999 1000 1001 1002 1003

		if (task_work_pending && current->mm != &init_mm) {
			estatus_node->task_work.func = ghes_kick_task_work;
			estatus_node->task_work_cpu = smp_processor_id();
			ret = task_work_add(current, &estatus_node->task_work,
J
Jens Axboe 已提交
1004
					    TWA_RESUME);
1005 1006 1007 1008 1009 1010 1011 1012
			if (ret)
				estatus_node->task_work.func = NULL;
		}

		if (!estatus_node->task_work.func)
			gen_pool_free(ghes_estatus_pool,
				      (unsigned long)estatus_node, node_len);

1013 1014 1015 1016
		llnode = next;
	}
}

1017 1018 1019 1020 1021
static void ghes_print_queued_estatus(void)
{
	struct llist_node *llnode;
	struct ghes_estatus_node *estatus_node;
	struct acpi_hest_generic *generic;
1022
	struct acpi_hest_generic_status *estatus;
1023 1024 1025 1026 1027 1028

	llnode = llist_del_all(&ghes_estatus_llist);
	/*
	 * Because the time order of estatus in list is reversed,
	 * revert it back to proper order.
	 */
1029
	llnode = llist_reverse_order(llnode);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	while (llnode) {
		estatus_node = llist_entry(llnode, struct ghes_estatus_node,
					   llnode);
		estatus = GHES_ESTATUS_FROM_NODE(estatus_node);
		generic = estatus_node->generic;
		ghes_print_estatus(NULL, generic, estatus);
		llnode = llnode->next;
	}
}

1040 1041
static int ghes_in_nmi_queue_one_entry(struct ghes *ghes,
				       enum fixed_addresses fixmap_idx)
1042
{
1043
	struct acpi_hest_generic_status *estatus, tmp_header;
1044
	struct ghes_estatus_node *estatus_node;
1045 1046 1047
	u32 len, node_len;
	u64 buf_paddr;
	int sev, rc;
1048

1049
	if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG))
1050
		return -EOPNOTSUPP;
1051

1052 1053 1054 1055 1056
	rc = __ghes_peek_estatus(ghes, &tmp_header, &buf_paddr, fixmap_idx);
	if (rc) {
		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
		return rc;
	}
1057

1058 1059 1060 1061 1062
	rc = __ghes_check_estatus(ghes, &tmp_header);
	if (rc) {
		ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
		return rc;
	}
1063

1064 1065
	len = cper_estatus_len(&tmp_header);
	node_len = GHES_ESTATUS_NODE_LEN(len);
1066 1067
	estatus_node = (void *)gen_pool_alloc(ghes_estatus_pool, node_len);
	if (!estatus_node)
1068
		return -ENOMEM;
1069 1070 1071

	estatus_node->ghes = ghes;
	estatus_node->generic = ghes->generic;
1072
	estatus_node->task_work.func = NULL;
1073 1074
	estatus = GHES_ESTATUS_FROM_NODE(estatus_node);

1075
	if (__ghes_read_estatus(estatus, buf_paddr, fixmap_idx, len)) {
1076
		ghes_clear_estatus(ghes, estatus, buf_paddr, fixmap_idx);
1077 1078
		rc = -ENOENT;
		goto no_work;
1079
	}
1080

1081
	sev = ghes_severity(estatus->error_severity);
1082 1083
	if (sev >= GHES_SEV_PANIC) {
		ghes_print_queued_estatus();
1084
		__ghes_panic(ghes, estatus, buf_paddr, fixmap_idx);
1085
	}
1086

1087
	ghes_clear_estatus(ghes, &tmp_header, buf_paddr, fixmap_idx);
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	/* This error has been reported before, don't process it again. */
	if (ghes_estatus_cached(estatus))
		goto no_work;

	llist_add(&estatus_node->llnode, &ghes_estatus_llist);

	return rc;

no_work:
	gen_pool_free(ghes_estatus_pool, (unsigned long)estatus_node,
		      node_len);

	return rc;
1102 1103
}

1104 1105
static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
				       enum fixed_addresses fixmap_idx)
1106 1107 1108 1109 1110 1111
{
	int ret = -ENOENT;
	struct ghes *ghes;

	rcu_read_lock();
	list_for_each_entry_rcu(ghes, rcu_list, list) {
1112
		if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx))
1113
			ret = 0;
1114
	}
1115
	rcu_read_unlock();
1116

1117
	if (IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && !ret)
1118
		irq_work_queue(&ghes_proc_irq_work);
1119 1120 1121

	return ret;
}
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

#ifdef CONFIG_ACPI_APEI_SEA
static LIST_HEAD(ghes_sea);

/*
 * Return 0 only if one of the SEA error sources successfully reported an error
 * record sent from the firmware.
 */
int ghes_notify_sea(void)
{
1132 1133 1134 1135
	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sea);
	int rv;

	raw_spin_lock(&ghes_notify_lock_sea);
1136
	rv = ghes_in_nmi_spool_from_list(&ghes_sea, FIX_APEI_GHES_SEA);
1137 1138 1139
	raw_spin_unlock(&ghes_notify_lock_sea);

	return rv;
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
}

static void ghes_sea_add(struct ghes *ghes)
{
	mutex_lock(&ghes_list_mutex);
	list_add_rcu(&ghes->list, &ghes_sea);
	mutex_unlock(&ghes_list_mutex);
}

static void ghes_sea_remove(struct ghes *ghes)
{
	mutex_lock(&ghes_list_mutex);
	list_del_rcu(&ghes->list);
	mutex_unlock(&ghes_list_mutex);
	synchronize_rcu();
}
#else /* CONFIG_ACPI_APEI_SEA */
static inline void ghes_sea_add(struct ghes *ghes) { }
static inline void ghes_sea_remove(struct ghes *ghes) { }
#endif /* CONFIG_ACPI_APEI_SEA */

#ifdef CONFIG_HAVE_ACPI_APEI_NMI
/*
 * NMI may be triggered on any CPU, so ghes_in_nmi is used for
 * having only one concurrent reader.
 */
static atomic_t ghes_in_nmi = ATOMIC_INIT(0);

static LIST_HEAD(ghes_nmi);
1169 1170 1171

static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
{
1172
	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
1173 1174 1175 1176 1177
	int ret = NMI_DONE;

	if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
		return ret;

1178
	raw_spin_lock(&ghes_notify_lock_nmi);
1179
	if (!ghes_in_nmi_spool_from_list(&ghes_nmi, FIX_APEI_GHES_NMI))
1180
		ret = NMI_HANDLED;
1181
	raw_spin_unlock(&ghes_notify_lock_nmi);
1182

1183
	atomic_dec(&ghes_in_nmi);
1184 1185 1186
	return ret;
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
static void ghes_nmi_add(struct ghes *ghes)
{
	mutex_lock(&ghes_list_mutex);
	if (list_empty(&ghes_nmi))
		register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
	list_add_rcu(&ghes->list, &ghes_nmi);
	mutex_unlock(&ghes_list_mutex);
}

static void ghes_nmi_remove(struct ghes *ghes)
{
	mutex_lock(&ghes_list_mutex);
	list_del_rcu(&ghes->list);
	if (list_empty(&ghes_nmi))
		unregister_nmi_handler(NMI_LOCAL, "ghes");
	mutex_unlock(&ghes_list_mutex);
	/*
	 * To synchronize with NMI handler, ghes can only be
	 * freed after NMI handler finishes.
	 */
	synchronize_rcu();
}
1209 1210 1211 1212
#else /* CONFIG_HAVE_ACPI_APEI_NMI */
static inline void ghes_nmi_add(struct ghes *ghes) { }
static inline void ghes_nmi_remove(struct ghes *ghes) { }
#endif /* CONFIG_HAVE_ACPI_APEI_NMI */
1213 1214 1215 1216 1217 1218

static void ghes_nmi_init_cxt(void)
{
	init_irq_work(&ghes_proc_irq_work, ghes_proc_in_irq);
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
static int __ghes_sdei_callback(struct ghes *ghes,
				enum fixed_addresses fixmap_idx)
{
	if (!ghes_in_nmi_queue_one_entry(ghes, fixmap_idx)) {
		irq_work_queue(&ghes_proc_irq_work);

		return 0;
	}

	return -ENOENT;
}

static int ghes_sdei_normal_callback(u32 event_num, struct pt_regs *regs,
				      void *arg)
{
	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_normal);
	struct ghes *ghes = arg;
	int err;

	raw_spin_lock(&ghes_notify_lock_sdei_normal);
	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_NORMAL);
	raw_spin_unlock(&ghes_notify_lock_sdei_normal);

	return err;
}

static int ghes_sdei_critical_callback(u32 event_num, struct pt_regs *regs,
				       void *arg)
{
	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_sdei_critical);
	struct ghes *ghes = arg;
	int err;

	raw_spin_lock(&ghes_notify_lock_sdei_critical);
	err = __ghes_sdei_callback(ghes, FIX_APEI_GHES_SDEI_CRITICAL);
	raw_spin_unlock(&ghes_notify_lock_sdei_critical);

	return err;
}

static int apei_sdei_register_ghes(struct ghes *ghes)
{
	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
		return -EOPNOTSUPP;

	return sdei_register_ghes(ghes, ghes_sdei_normal_callback,
				 ghes_sdei_critical_callback);
}

static int apei_sdei_unregister_ghes(struct ghes *ghes)
{
	if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
		return -EOPNOTSUPP;

	return sdei_unregister_ghes(ghes);
}

B
Bill Pemberton 已提交
1276
static int ghes_probe(struct platform_device *ghes_dev)
1277 1278 1279
{
	struct acpi_hest_generic *generic;
	struct ghes *ghes = NULL;
1280
	unsigned long flags;
1281

1282
	int rc = -EINVAL;
1283

1284
	generic = *(struct acpi_hest_generic **)ghes_dev->dev.platform_data;
1285
	if (!generic->enabled)
1286
		return -ENODEV;
1287

1288 1289 1290 1291
	switch (generic->notify.type) {
	case ACPI_HEST_NOTIFY_POLLED:
	case ACPI_HEST_NOTIFY_EXTERNAL:
	case ACPI_HEST_NOTIFY_SCI:
1292 1293
	case ACPI_HEST_NOTIFY_GSIV:
	case ACPI_HEST_NOTIFY_GPIO:
1294
		break;
1295

1296 1297 1298 1299 1300 1301 1302 1303
	case ACPI_HEST_NOTIFY_SEA:
		if (!IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SEA is not supported\n",
				generic->header.source_id);
			rc = -ENOTSUPP;
			goto err;
		}
		break;
1304
	case ACPI_HEST_NOTIFY_NMI:
1305 1306 1307 1308 1309
		if (!IS_ENABLED(CONFIG_HAVE_ACPI_APEI_NMI)) {
			pr_warn(GHES_PFX "Generic hardware error source: %d notified via NMI interrupt is not supported!\n",
				generic->header.source_id);
			goto err;
		}
1310
		break;
1311 1312 1313 1314 1315 1316 1317
	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
		if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) {
			pr_warn(GHES_PFX "Generic hardware error source: %d notified via SDE Interface is not supported!\n",
				generic->header.source_id);
			goto err;
		}
		break;
1318
	case ACPI_HEST_NOTIFY_LOCAL:
1319 1320
		pr_warn(GHES_PFX "Generic hardware error source: %d notified via local interrupt is not supported!\n",
			generic->header.source_id);
1321
		goto err;
1322
	default:
1323 1324
		pr_warn(FW_WARN GHES_PFX "Unknown notification type: %u for generic hardware error source: %d\n",
			generic->notify.type, generic->header.source_id);
1325
		goto err;
1326
	}
1327 1328 1329

	rc = -EIO;
	if (generic->error_block_length <
1330
	    sizeof(struct acpi_hest_generic_status)) {
1331 1332
		pr_warn(FW_BUG GHES_PFX "Invalid error block length: %u for generic hardware error source: %d\n",
			generic->error_block_length, generic->header.source_id);
1333 1334 1335 1336 1337 1338 1339 1340
		goto err;
	}
	ghes = ghes_new(generic);
	if (IS_ERR(ghes)) {
		rc = PTR_ERR(ghes);
		ghes = NULL;
		goto err;
	}
1341

1342 1343
	switch (generic->notify.type) {
	case ACPI_HEST_NOTIFY_POLLED:
1344
		timer_setup(&ghes->timer, ghes_poll_func, 0);
1345 1346 1347 1348
		ghes_add_timer(ghes);
		break;
	case ACPI_HEST_NOTIFY_EXTERNAL:
		/* External interrupt vector is GSI */
1349 1350
		rc = acpi_gsi_to_irq(generic->notify.vector, &ghes->irq);
		if (rc) {
1351 1352
			pr_err(GHES_PFX "Failed to map GSI to IRQ for generic hardware error source: %d\n",
			       generic->header.source_id);
1353
			goto err;
1354
		}
1355 1356
		rc = request_irq(ghes->irq, ghes_irq_func, IRQF_SHARED,
				 "GHES IRQ", ghes);
1357
		if (rc) {
1358 1359
			pr_err(GHES_PFX "Failed to register IRQ for generic hardware error source: %d\n",
			       generic->header.source_id);
1360
			goto err;
1361 1362
		}
		break;
1363

1364
	case ACPI_HEST_NOTIFY_SCI:
1365 1366
	case ACPI_HEST_NOTIFY_GSIV:
	case ACPI_HEST_NOTIFY_GPIO:
1367
		mutex_lock(&ghes_list_mutex);
1368 1369 1370
		if (list_empty(&ghes_hed))
			register_acpi_hed_notifier(&ghes_notifier_hed);
		list_add_rcu(&ghes->list, &ghes_hed);
1371
		mutex_unlock(&ghes_list_mutex);
1372
		break;
1373

1374 1375 1376
	case ACPI_HEST_NOTIFY_SEA:
		ghes_sea_add(ghes);
		break;
1377
	case ACPI_HEST_NOTIFY_NMI:
1378
		ghes_nmi_add(ghes);
1379
		break;
1380 1381 1382 1383 1384
	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
		rc = apei_sdei_register_ghes(ghes);
		if (rc)
			goto err;
		break;
1385 1386
	default:
		BUG();
1387
	}
1388

1389
	platform_set_drvdata(ghes_dev, ghes);
1390

1391 1392
	ghes_edac_register(ghes, &ghes_dev->dev);

1393
	/* Handle any pending errors right away */
1394
	spin_lock_irqsave(&ghes_notify_lock_irq, flags);
1395
	ghes_proc(ghes);
1396
	spin_unlock_irqrestore(&ghes_notify_lock_irq, flags);
1397

1398
	return 0;
1399

1400
err:
1401
	if (ghes) {
1402
		ghes_fini(ghes);
1403 1404
		kfree(ghes);
	}
1405 1406 1407
	return rc;
}

B
Bill Pemberton 已提交
1408
static int ghes_remove(struct platform_device *ghes_dev)
1409
{
1410
	int rc;
1411 1412
	struct ghes *ghes;
	struct acpi_hest_generic *generic;
1413

1414 1415 1416
	ghes = platform_get_drvdata(ghes_dev);
	generic = ghes->generic;

1417
	ghes->flags |= GHES_EXITING;
1418
	switch (generic->notify.type) {
1419 1420 1421 1422 1423 1424
	case ACPI_HEST_NOTIFY_POLLED:
		del_timer_sync(&ghes->timer);
		break;
	case ACPI_HEST_NOTIFY_EXTERNAL:
		free_irq(ghes->irq, ghes);
		break;
1425

1426
	case ACPI_HEST_NOTIFY_SCI:
1427 1428
	case ACPI_HEST_NOTIFY_GSIV:
	case ACPI_HEST_NOTIFY_GPIO:
1429 1430
		mutex_lock(&ghes_list_mutex);
		list_del_rcu(&ghes->list);
1431 1432
		if (list_empty(&ghes_hed))
			unregister_acpi_hed_notifier(&ghes_notifier_hed);
1433
		mutex_unlock(&ghes_list_mutex);
1434
		synchronize_rcu();
1435
		break;
1436

1437 1438 1439
	case ACPI_HEST_NOTIFY_SEA:
		ghes_sea_remove(ghes);
		break;
1440
	case ACPI_HEST_NOTIFY_NMI:
1441
		ghes_nmi_remove(ghes);
1442
		break;
1443 1444 1445 1446 1447
	case ACPI_HEST_NOTIFY_SOFTWARE_DELEGATED:
		rc = apei_sdei_unregister_ghes(ghes);
		if (rc)
			return rc;
		break;
1448 1449 1450 1451
	default:
		BUG();
		break;
	}
1452

1453
	ghes_fini(ghes);
1454 1455 1456

	ghes_edac_unregister(ghes);

1457
	kfree(ghes);
1458

1459 1460 1461
	platform_set_drvdata(ghes_dev, NULL);

	return 0;
1462 1463
}

1464 1465 1466 1467 1468 1469 1470 1471
static struct platform_driver ghes_platform_driver = {
	.driver		= {
		.name	= "GHES",
	},
	.probe		= ghes_probe,
	.remove		= ghes_remove,
};

1472 1473
static int __init ghes_init(void)
{
1474 1475
	int rc;

1476 1477 1478
	if (acpi_disabled)
		return -ENODEV;

1479 1480 1481 1482
	switch (hest_disable) {
	case HEST_NOT_FOUND:
		return -ENODEV;
	case HEST_DISABLED:
1483 1484
		pr_info(GHES_PFX "HEST is not enabled!\n");
		return -EINVAL;
1485 1486
	default:
		break;
1487 1488
	}

1489 1490 1491 1492 1493
	if (ghes_disable) {
		pr_info(GHES_PFX "GHES is not enabled!\n");
		return -EINVAL;
	}

1494
	ghes_nmi_init_cxt();
1495 1496 1497

	rc = platform_driver_register(&ghes_platform_driver);
	if (rc)
1498
		goto err;
1499

H
Huang Ying 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	rc = apei_osc_setup();
	if (rc == 0 && osc_sb_apei_support_acked)
		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit and WHEA _OSC.\n");
	else if (rc == 0 && !osc_sb_apei_support_acked)
		pr_info(GHES_PFX "APEI firmware first mode is enabled by WHEA _OSC.\n");
	else if (rc && osc_sb_apei_support_acked)
		pr_info(GHES_PFX "APEI firmware first mode is enabled by APEI bit.\n");
	else
		pr_info(GHES_PFX "Failed to enable APEI firmware first mode.\n");

1510 1511 1512
	return 0;
err:
	return rc;
1513
}
1514
device_initcall(ghes_init);