einj.c 22.4 KB
Newer Older
H
Huang Ying 已提交
1 2 3 4 5 6 7 8 9
/*
 * APEI Error INJection support
 *
 * EINJ provides a hardware error injection mechanism, this is useful
 * for debugging and testing of other APEI and RAS features.
 *
 * For more information about EINJ, please refer to ACPI Specification
 * version 4.0, section 17.5.
 *
10
 * Copyright 2009-2010 Intel Corp.
H
Huang Ying 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *   Author: Huang Ying <ying.huang@intel.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/nmi.h>
#include <linux/delay.h>
31
#include <linux/mm.h>
32
#include <asm/unaligned.h>
H
Huang Ying 已提交
33 34 35

#include "apei-internal.h"

36 37
#undef pr_fmt
#define pr_fmt(fmt) "EINJ: " fmt
H
Huang Ying 已提交
38 39

#define SPIN_UNIT		100			/* 100ns */
S
Stefan Weil 已提交
40
/* Firmware should respond within 1 milliseconds */
H
Huang Ying 已提交
41
#define FIRMWARE_TIMEOUT	(1 * NSEC_PER_MSEC)
42 43 44 45
#define ACPI5_VENDOR_BIT	BIT(31)
#define MEM_ERROR_MASK		(ACPI_EINJ_MEMORY_CORRECTABLE | \
				ACPI_EINJ_MEMORY_UNCORRECTABLE | \
				ACPI_EINJ_MEMORY_FATAL)
H
Huang Ying 已提交
46

47 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 73 74 75 76 77 78
/*
 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
 */
static int acpi5;

struct set_error_type_with_address {
	u32	type;
	u32	vendor_extension;
	u32	flags;
	u32	apicid;
	u64	memory_address;
	u64	memory_address_range;
	u32	pcie_sbdf;
};
enum {
	SETWA_FLAGS_APICID = 1,
	SETWA_FLAGS_MEM = 2,
	SETWA_FLAGS_PCIE_SBDF = 4,
};

/*
 * Vendor extensions for platform specific operations
 */
struct vendor_error_type_extension {
	u32	length;
	u32	pcie_sbdf;
	u16	vendor_id;
	u16	device_id;
	u8	rev_id;
	u8	reserved[3];
};

79 80
static u32 notrigger;

81 82 83 84
static u32 vendor_flags;
static struct debugfs_blob_wrapper vendor_blob;
static char vendor_dev[64];

85 86 87 88
/*
 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
 * EINJ table through an unpublished extension. Use with caution as
 * most will ignore the parameter and make their own choice of address
89 90
 * for error injection.  This extension is used only if
 * param_extension module parameter is specified.
91 92 93 94 95 96 97 98 99
 */
struct einj_parameter {
	u64 type;
	u64 reserved1;
	u64 reserved2;
	u64 param1;
	u64 param2;
};

H
Huang Ying 已提交
100 101 102 103 104 105 106 107 108
#define EINJ_OP_BUSY			0x1
#define EINJ_STATUS_SUCCESS		0x0
#define EINJ_STATUS_FAIL		0x1
#define EINJ_STATUS_INVAL		0x2

#define EINJ_TAB_ENTRY(tab)						\
	((struct acpi_whea_header *)((char *)(tab) +			\
				    sizeof(struct acpi_table_einj)))

109 110 111
static bool param_extension;
module_param(param_extension, bool, 0);

H
Huang Ying 已提交
112 113 114 115 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 141 142 143 144 145
static struct acpi_table_einj *einj_tab;

static struct apei_resources einj_resources;

static struct apei_exec_ins_type einj_ins_type[] = {
	[ACPI_EINJ_READ_REGISTER] = {
		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
		.run   = apei_exec_read_register,
	},
	[ACPI_EINJ_READ_REGISTER_VALUE] = {
		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
		.run   = apei_exec_read_register_value,
	},
	[ACPI_EINJ_WRITE_REGISTER] = {
		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
		.run   = apei_exec_write_register,
	},
	[ACPI_EINJ_WRITE_REGISTER_VALUE] = {
		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
		.run   = apei_exec_write_register_value,
	},
	[ACPI_EINJ_NOOP] = {
		.flags = 0,
		.run   = apei_exec_noop,
	},
};

/*
 * Prevent EINJ interpreter to run simultaneously, because the
 * corresponding firmware implementation may not work properly when
 * invoked simultaneously.
 */
static DEFINE_MUTEX(einj_mutex);

146 147
static void *einj_param;

H
Huang Ying 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static void einj_exec_ctx_init(struct apei_exec_context *ctx)
{
	apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
			   EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
}

static int __einj_get_available_error_type(u32 *type)
{
	struct apei_exec_context ctx;
	int rc;

	einj_exec_ctx_init(&ctx);
	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
	if (rc)
		return rc;
	*type = apei_exec_ctx_get_output(&ctx);

	return 0;
}

/* Get error injection capabilities of the platform */
static int einj_get_available_error_type(u32 *type)
{
	int rc;

	mutex_lock(&einj_mutex);
	rc = __einj_get_available_error_type(type);
	mutex_unlock(&einj_mutex);

	return rc;
}

static int einj_timedout(u64 *t)
{
	if ((s64)*t < SPIN_UNIT) {
183
		pr_warning(FW_WARN "Firmware does not respond in time\n");
H
Huang Ying 已提交
184 185 186 187 188 189 190 191
		return 1;
	}
	*t -= SPIN_UNIT;
	ndelay(SPIN_UNIT);
	touch_nmi_watchdog();
	return 0;
}

192 193 194
static void check_vendor_extension(u64 paddr,
				   struct set_error_type_with_address *v5param)
{
195
	int	offset = v5param->vendor_extension;
196 197 198 199 200
	struct	vendor_error_type_extension *v;
	u32	sbdf;

	if (!offset)
		return;
201
	v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
202 203
	if (!v)
		return;
204
	sbdf = v->pcie_sbdf;
205 206 207
	sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
		sbdf >> 24, (sbdf >> 16) & 0xff,
		(sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
208
		 v->vendor_id, v->device_id, v->rev_id);
209
	acpi_os_unmap_iomem(v, sizeof(*v));
210 211 212
}

static void *einj_get_parameter_address(void)
213 214
{
	int i;
215
	u64 pa_v4 = 0, pa_v5 = 0;
216 217 218 219 220 221 222 223
	struct acpi_whea_header *entry;

	entry = EINJ_TAB_ENTRY(einj_tab);
	for (i = 0; i < einj_tab->entries; i++) {
		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
		    entry->register_region.space_id ==
		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
224
			pa_v4 = get_unaligned(&entry->register_region.address);
225 226 227 228
		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
		    entry->register_region.space_id ==
		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
229
			pa_v5 = get_unaligned(&entry->register_region.address);
230 231
		entry++;
	}
232
	if (pa_v5) {
233 234
		struct set_error_type_with_address *v5param;

235
		v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
236 237
		if (v5param) {
			acpi5 = 1;
238
			check_vendor_extension(pa_v5, v5param);
239 240 241
			return v5param;
		}
	}
242
	if (param_extension && pa_v4) {
243 244
		struct einj_parameter *v4param;

245
		v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
246
		if (!v4param)
247
			return NULL;
248
		if (v4param->reserved1 || v4param->reserved2) {
249
			acpi_os_unmap_iomem(v4param, sizeof(*v4param));
250
			return NULL;
251 252 253
		}
		return v4param;
	}
254

255
	return NULL;
256 257
}

H
Huang Ying 已提交
258 259 260 261 262 263
/* do sanity check to trigger table */
static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
{
	if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
		return -EINVAL;
	if (trigger_tab->table_size > PAGE_SIZE ||
264
	    trigger_tab->table_size < trigger_tab->header_size)
H
Huang Ying 已提交
265 266 267 268 269 270 271 272 273
		return -EINVAL;
	if (trigger_tab->entry_count !=
	    (trigger_tab->table_size - trigger_tab->header_size) /
	    sizeof(struct acpi_einj_entry))
		return -EINVAL;

	return 0;
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static struct acpi_generic_address *einj_get_trigger_parameter_region(
	struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
{
	int i;
	struct acpi_whea_header *entry;

	entry = (struct acpi_whea_header *)
		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
	for (i = 0; i < trigger_tab->entry_count; i++) {
		if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
		entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
		entry->register_region.space_id ==
			ACPI_ADR_SPACE_SYSTEM_MEMORY &&
		(entry->register_region.address & param2) == (param1 & param2))
			return &entry->register_region;
		entry++;
	}

	return NULL;
}
H
Huang Ying 已提交
294
/* Execute instructions in trigger error action table */
295 296
static int __einj_error_trigger(u64 trigger_paddr, u32 type,
				u64 param1, u64 param2)
H
Huang Ying 已提交
297 298 299 300 301 302 303 304
{
	struct acpi_einj_trigger *trigger_tab = NULL;
	struct apei_exec_context trigger_ctx;
	struct apei_resources trigger_resources;
	struct acpi_whea_header *trigger_entry;
	struct resource *r;
	u32 table_size;
	int rc = -EIO;
305
	struct acpi_generic_address *trigger_param_region = NULL;
H
Huang Ying 已提交
306 307 308 309

	r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
			       "APEI EINJ Trigger Table");
	if (!r) {
310
		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
H
Huang Ying 已提交
311
		       (unsigned long long)trigger_paddr,
312 313
		       (unsigned long long)trigger_paddr +
			    sizeof(*trigger_tab) - 1);
H
Huang Ying 已提交
314 315 316 317
		goto out;
	}
	trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
	if (!trigger_tab) {
318
		pr_err("Failed to map trigger table!\n");
H
Huang Ying 已提交
319 320 321 322
		goto out_rel_header;
	}
	rc = einj_check_trigger_header(trigger_tab);
	if (rc) {
323
		pr_warning(FW_BUG "Invalid trigger error action table.\n");
H
Huang Ying 已提交
324 325
		goto out_rel_header;
	}
326 327 328 329 330

	/* No action structures in the TRIGGER_ERROR table, nothing to do */
	if (!trigger_tab->entry_count)
		goto out_rel_header;

H
Huang Ying 已提交
331 332 333 334 335 336
	rc = -EIO;
	table_size = trigger_tab->table_size;
	r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
			       table_size - sizeof(*trigger_tab),
			       "APEI EINJ Trigger Table");
	if (!r) {
337
		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
338 339
		       (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
		       (unsigned long long)trigger_paddr + table_size - 1);
H
Huang Ying 已提交
340 341 342 343 344
		goto out_rel_header;
	}
	iounmap(trigger_tab);
	trigger_tab = ioremap_cache(trigger_paddr, table_size);
	if (!trigger_tab) {
345
		pr_err("Failed to map trigger table!\n");
H
Huang Ying 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359
		goto out_rel_entry;
	}
	trigger_entry = (struct acpi_whea_header *)
		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
	apei_resources_init(&trigger_resources);
	apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
			   ARRAY_SIZE(einj_ins_type),
			   trigger_entry, trigger_tab->entry_count);
	rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
	if (rc)
		goto out_fini;
	rc = apei_resources_sub(&trigger_resources, &einj_resources);
	if (rc)
		goto out_fini;
360 361 362 363 364 365
	/*
	 * Some firmware will access target address specified in
	 * param1 to trigger the error when injecting memory error.
	 * This will cause resource conflict with regular memory.  So
	 * remove it from trigger table resources.
	 */
366
	if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
367 368
		struct apei_resources addr_resources;
		apei_resources_init(&addr_resources);
369 370 371 372 373 374 375 376 377 378 379
		trigger_param_region = einj_get_trigger_parameter_region(
			trigger_tab, param1, param2);
		if (trigger_param_region) {
			rc = apei_resources_add(&addr_resources,
				trigger_param_region->address,
				trigger_param_region->bit_width/8, true);
			if (rc)
				goto out_fini;
			rc = apei_resources_sub(&trigger_resources,
					&addr_resources);
		}
380 381 382 383
		apei_resources_fini(&addr_resources);
		if (rc)
			goto out_fini;
	}
H
Huang Ying 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
	if (rc)
		goto out_fini;
	rc = apei_exec_pre_map_gars(&trigger_ctx);
	if (rc)
		goto out_release;

	rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);

	apei_exec_post_unmap_gars(&trigger_ctx);
out_release:
	apei_resources_release(&trigger_resources);
out_fini:
	apei_resources_fini(&trigger_resources);
out_rel_entry:
	release_mem_region(trigger_paddr + sizeof(*trigger_tab),
			   table_size - sizeof(*trigger_tab));
out_rel_header:
	release_mem_region(trigger_paddr, sizeof(*trigger_tab));
out:
	if (trigger_tab)
		iounmap(trigger_tab);

	return rc;
}

410 411
static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
			       u64 param3, u64 param4)
H
Huang Ying 已提交
412 413 414 415 416 417 418
{
	struct apei_exec_context ctx;
	u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
	int rc;

	einj_exec_ctx_init(&ctx);

419
	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
H
Huang Ying 已提交
420 421 422
	if (rc)
		return rc;
	apei_exec_ctx_set_input(&ctx, type);
423 424 425
	if (acpi5) {
		struct set_error_type_with_address *v5param = einj_param;

426
		v5param->type = type;
427
		if (type & ACPI5_VENDOR_BIT) {
428 429
			switch (vendor_flags) {
			case SETWA_FLAGS_APICID:
430
				v5param->apicid = param1;
431 432
				break;
			case SETWA_FLAGS_MEM:
433 434
				v5param->memory_address = param1;
				v5param->memory_address_range = param2;
435 436
				break;
			case SETWA_FLAGS_PCIE_SBDF:
437
				v5param->pcie_sbdf = param1;
438 439
				break;
			}
440
			v5param->flags = vendor_flags;
441 442 443 444 445 446
		} else if (flags) {
				v5param->flags = flags;
				v5param->memory_address = param1;
				v5param->memory_address_range = param2;
				v5param->apicid = param3;
				v5param->pcie_sbdf = param4;
447 448 449 450 451
		} else {
			switch (type) {
			case ACPI_EINJ_PROCESSOR_CORRECTABLE:
			case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
			case ACPI_EINJ_PROCESSOR_FATAL:
452 453
				v5param->apicid = param1;
				v5param->flags = SETWA_FLAGS_APICID;
454 455 456 457
				break;
			case ACPI_EINJ_MEMORY_CORRECTABLE:
			case ACPI_EINJ_MEMORY_UNCORRECTABLE:
			case ACPI_EINJ_MEMORY_FATAL:
458 459 460
				v5param->memory_address = param1;
				v5param->memory_address_range = param2;
				v5param->flags = SETWA_FLAGS_MEM;
461 462 463 464
				break;
			case ACPI_EINJ_PCIX_CORRECTABLE:
			case ACPI_EINJ_PCIX_UNCORRECTABLE:
			case ACPI_EINJ_PCIX_FATAL:
465 466
				v5param->pcie_sbdf = param1;
				v5param->flags = SETWA_FLAGS_PCIE_SBDF;
467 468 469 470 471 472 473 474 475
				break;
			}
		}
	} else {
		rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
		if (rc)
			return rc;
		if (einj_param) {
			struct einj_parameter *v4param = einj_param;
476 477
			v4param->param1 = param1;
			v4param->param2 = param2;
478
		}
479
	}
H
Huang Ying 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
	if (rc)
		return rc;
	for (;;) {
		rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
		if (rc)
			return rc;
		val = apei_exec_ctx_get_output(&ctx);
		if (!(val & EINJ_OP_BUSY))
			break;
		if (einj_timedout(&timeout))
			return -EIO;
	}
	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
	if (rc)
		return rc;
	val = apei_exec_ctx_get_output(&ctx);
	if (val != EINJ_STATUS_SUCCESS)
		return -EBUSY;

	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
	if (rc)
		return rc;
	trigger_paddr = apei_exec_ctx_get_output(&ctx);
504 505 506 507 508
	if (notrigger == 0) {
		rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
		if (rc)
			return rc;
	}
509
	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
H
Huang Ying 已提交
510 511 512 513 514

	return rc;
}

/* Inject the specified hardware error */
515 516
static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
			     u64 param3, u64 param4)
H
Huang Ying 已提交
517 518
{
	int rc;
519
	u64 base_addr, size;
H
Huang Ying 已提交
520

521 522 523 524 525
	/* If user manually set "flags", make sure it is legal */
	if (flags && (flags &
		~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
		return -EINVAL;

526 527 528 529 530 531 532 533 534 535 536 537 538
	/*
	 * We need extra sanity checks for memory errors.
	 * Other types leap directly to injection.
	 */

	/* ensure param1/param2 existed */
	if (!(param_extension || acpi5))
		goto inject;

	/* ensure injection is memory related */
	if (type & ACPI5_VENDOR_BIT) {
		if (vendor_flags != SETWA_FLAGS_MEM)
			goto inject;
539
	} else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
540 541 542 543 544
		goto inject;

	/*
	 * Disallow crazy address masks that give BIOS leeway to pick
	 * injection address almost anywhere. Insist on page or
545 546
	 * better granularity and that target address is normal RAM or
	 * NVDIMM.
547
	 */
548 549 550 551 552 553 554 555
	base_addr = param1 & param2;
	size = ~param2 + 1;

	if (((param2 & PAGE_MASK) != PAGE_MASK) ||
	    ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
				!= REGION_INTERSECTS) &&
	     (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
				!= REGION_INTERSECTS)))
556 557 558
		return -EINVAL;

inject:
H
Huang Ying 已提交
559
	mutex_lock(&einj_mutex);
560
	rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
H
Huang Ying 已提交
561 562 563 564 565 566
	mutex_unlock(&einj_mutex);

	return rc;
}

static u32 error_type;
567
static u32 error_flags;
568 569
static u64 error_param1;
static u64 error_param2;
570 571
static u64 error_param3;
static u64 error_param4;
H
Huang Ying 已提交
572 573 574 575 576 577 578 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 629 630 631 632
static struct dentry *einj_debug_dir;

static int available_error_type_show(struct seq_file *m, void *v)
{
	int rc;
	u32 available_error_type = 0;

	rc = einj_get_available_error_type(&available_error_type);
	if (rc)
		return rc;
	if (available_error_type & 0x0001)
		seq_printf(m, "0x00000001\tProcessor Correctable\n");
	if (available_error_type & 0x0002)
		seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
	if (available_error_type & 0x0004)
		seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
	if (available_error_type & 0x0008)
		seq_printf(m, "0x00000008\tMemory Correctable\n");
	if (available_error_type & 0x0010)
		seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
	if (available_error_type & 0x0020)
		seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
	if (available_error_type & 0x0040)
		seq_printf(m, "0x00000040\tPCI Express Correctable\n");
	if (available_error_type & 0x0080)
		seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
	if (available_error_type & 0x0100)
		seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
	if (available_error_type & 0x0200)
		seq_printf(m, "0x00000200\tPlatform Correctable\n");
	if (available_error_type & 0x0400)
		seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
	if (available_error_type & 0x0800)
		seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");

	return 0;
}

static int available_error_type_open(struct inode *inode, struct file *file)
{
	return single_open(file, available_error_type_show, NULL);
}

static const struct file_operations available_error_type_fops = {
	.open		= available_error_type_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int error_type_get(void *data, u64 *val)
{
	*val = error_type;

	return 0;
}

static int error_type_set(void *data, u64 val)
{
	int rc;
	u32 available_error_type = 0;
633 634 635 636 637 638
	u32 tval, vendor;

	/*
	 * Vendor defined types have 0x80000000 bit set, and
	 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
	 */
639
	vendor = val & ACPI5_VENDOR_BIT;
640
	tval = val & 0x7fffffff;
H
Huang Ying 已提交
641 642

	/* Only one error type can be specified */
643
	if (tval & (tval - 1))
H
Huang Ying 已提交
644
		return -EINVAL;
645 646 647 648 649 650 651
	if (!vendor) {
		rc = einj_get_available_error_type(&available_error_type);
		if (rc)
			return rc;
		if (!(val & available_error_type))
			return -EINVAL;
	}
H
Huang Ying 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664
	error_type = val;

	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
			error_type_set, "0x%llx\n");

static int error_inject_set(void *data, u64 val)
{
	if (!error_type)
		return -EINVAL;

665 666
	return einj_error_inject(error_type, error_flags, error_param1, error_param2,
		error_param3, error_param4);
H
Huang Ying 已提交
667 668 669 670 671 672 673
}

DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
			error_inject_set, "%llu\n");

static int einj_check_table(struct acpi_table_einj *einj_tab)
{
674 675 676
	if ((einj_tab->header_length !=
	     (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
	    && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
H
Huang Ying 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		return -EINVAL;
	if (einj_tab->header.length < sizeof(struct acpi_table_einj))
		return -EINVAL;
	if (einj_tab->entries !=
	    (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
	    sizeof(struct acpi_einj_entry))
		return -EINVAL;

	return 0;
}

static int __init einj_init(void)
{
	int rc;
	acpi_status status;
	struct dentry *fentry;
	struct apei_exec_context ctx;

695 696
	if (acpi_disabled) {
		pr_warn("ACPI disabled.\n");
H
Huang Ying 已提交
697
		return -ENODEV;
698
	}
H
Huang Ying 已提交
699 700 701

	status = acpi_get_table(ACPI_SIG_EINJ, 0,
				(struct acpi_table_header **)&einj_tab);
702 703
	if (status == AE_NOT_FOUND) {
		pr_warn("EINJ table not found.\n");
H
Huang Ying 已提交
704
		return -ENODEV;
705
	}
706
	else if (ACPI_FAILURE(status)) {
707 708
		pr_err("Failed to get EINJ table: %s\n",
				acpi_format_exception(status));
H
Huang Ying 已提交
709 710 711 712 713
		return -EINVAL;
	}

	rc = einj_check_table(einj_tab);
	if (rc) {
714
		pr_warn(FW_BUG "Invalid EINJ table.\n");
H
Huang Ying 已提交
715 716 717 718 719
		return -EINVAL;
	}

	rc = -ENOMEM;
	einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
720 721
	if (!einj_debug_dir) {
		pr_err("Error creating debugfs node.\n");
H
Huang Ying 已提交
722
		goto err_cleanup;
723 724
	}

H
Huang Ying 已提交
725 726 727 728 729
	fentry = debugfs_create_file("available_error_type", S_IRUSR,
				     einj_debug_dir, NULL,
				     &available_error_type_fops);
	if (!fentry)
		goto err_cleanup;
730

H
Huang Ying 已提交
731 732 733 734 735 736 737 738 739 740 741 742
	fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
				     einj_debug_dir, NULL, &error_type_fops);
	if (!fentry)
		goto err_cleanup;
	fentry = debugfs_create_file("error_inject", S_IWUSR,
				     einj_debug_dir, NULL, &error_inject_fops);
	if (!fentry)
		goto err_cleanup;

	apei_resources_init(&einj_resources);
	einj_exec_ctx_init(&ctx);
	rc = apei_exec_collect_resources(&ctx, &einj_resources);
743 744
	if (rc) {
		pr_err("Error collecting EINJ resources.\n");
H
Huang Ying 已提交
745
		goto err_fini;
746 747
	}

H
Huang Ying 已提交
748
	rc = apei_resources_request(&einj_resources, "APEI EINJ");
749 750
	if (rc) {
		pr_err("Error requesting memory/port resources.\n");
H
Huang Ying 已提交
751
		goto err_fini;
752 753
	}

H
Huang Ying 已提交
754
	rc = apei_exec_pre_map_gars(&ctx);
755 756
	if (rc) {
		pr_err("Error pre-mapping GARs.\n");
H
Huang Ying 已提交
757
		goto err_release;
758
	}
759

760
	rc = -ENOMEM;
761 762
	einj_param = einj_get_parameter_address();
	if ((param_extension || acpi5) && einj_param) {
763 764 765 766
		fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &error_flags);
		if (!fentry)
			goto err_unmap;
767 768 769 770 771 772 773 774
		fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &error_param1);
		if (!fentry)
			goto err_unmap;
		fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &error_param2);
		if (!fentry)
			goto err_unmap;
775 776 777 778 779 780 781 782
		fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &error_param3);
		if (!fentry)
			goto err_unmap;
		fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &error_param4);
		if (!fentry)
			goto err_unmap;
783 784 785 786 787

		fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &notrigger);
		if (!fentry)
			goto err_unmap;
788 789 790 791 792 793 794 795 796 797 798 799 800
	}

	if (vendor_dev[0]) {
		vendor_blob.data = vendor_dev;
		vendor_blob.size = strlen(vendor_dev);
		fentry = debugfs_create_blob("vendor", S_IRUSR,
					     einj_debug_dir, &vendor_blob);
		if (!fentry)
			goto err_unmap;
		fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
					    einj_debug_dir, &vendor_flags);
		if (!fentry)
			goto err_unmap;
801
	}
H
Huang Ying 已提交
802

803
	pr_info("Error INJection is initialized.\n");
H
Huang Ying 已提交
804 805 806

	return 0;

807
err_unmap:
808 809 810 811 812
	if (einj_param) {
		acpi_size size = (acpi5) ?
			sizeof(struct set_error_type_with_address) :
			sizeof(struct einj_parameter);

813
		acpi_os_unmap_iomem(einj_param, size);
814
		pr_err("Error creating param extension debugfs nodes.\n");
815
	}
816
	apei_exec_post_unmap_gars(&ctx);
H
Huang Ying 已提交
817 818 819 820 821
err_release:
	apei_resources_release(&einj_resources);
err_fini:
	apei_resources_fini(&einj_resources);
err_cleanup:
822
	pr_err("Error creating primary debugfs nodes.\n");
H
Huang Ying 已提交
823 824 825 826 827 828 829 830 831
	debugfs_remove_recursive(einj_debug_dir);

	return rc;
}

static void __exit einj_exit(void)
{
	struct apei_exec_context ctx;

832 833 834 835 836
	if (einj_param) {
		acpi_size size = (acpi5) ?
			sizeof(struct set_error_type_with_address) :
			sizeof(struct einj_parameter);

837
		acpi_os_unmap_iomem(einj_param, size);
838
	}
H
Huang Ying 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851
	einj_exec_ctx_init(&ctx);
	apei_exec_post_unmap_gars(&ctx);
	apei_resources_release(&einj_resources);
	apei_resources_fini(&einj_resources);
	debugfs_remove_recursive(einj_debug_dir);
}

module_init(einj_init);
module_exit(einj_exit);

MODULE_AUTHOR("Huang Ying");
MODULE_DESCRIPTION("APEI Error INJection support");
MODULE_LICENSE("GPL");