xref: /openbmc/linux/drivers/acpi/apei/einj.c (revision 3bf90eca)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * APEI Error INJection support
4  *
5  * EINJ provides a hardware error injection mechanism, this is useful
6  * for debugging and testing of other APEI and RAS features.
7  *
8  * For more information about EINJ, please refer to ACPI Specification
9  * version 4.0, section 17.5.
10  *
11  * Copyright 2009-2010 Intel Corp.
12  *   Author: Huang Ying <ying.huang@intel.com>
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/io.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/nmi.h>
22 #include <linux/delay.h>
23 #include <linux/mm.h>
24 #include <asm/unaligned.h>
25 
26 #include "apei-internal.h"
27 
28 #undef pr_fmt
29 #define pr_fmt(fmt) "EINJ: " fmt
30 
31 #define SLEEP_UNIT_MIN		1000			/* 1ms */
32 #define SLEEP_UNIT_MAX		5000			/* 5ms */
33 /* Firmware should respond within 1 seconds */
34 #define FIRMWARE_TIMEOUT	(1 * USEC_PER_SEC)
35 #define ACPI5_VENDOR_BIT	BIT(31)
36 #define MEM_ERROR_MASK		(ACPI_EINJ_MEMORY_CORRECTABLE | \
37 				ACPI_EINJ_MEMORY_UNCORRECTABLE | \
38 				ACPI_EINJ_MEMORY_FATAL)
39 
40 /*
41  * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
42  */
43 static int acpi5;
44 
45 struct set_error_type_with_address {
46 	u32	type;
47 	u32	vendor_extension;
48 	u32	flags;
49 	u32	apicid;
50 	u64	memory_address;
51 	u64	memory_address_range;
52 	u32	pcie_sbdf;
53 };
54 enum {
55 	SETWA_FLAGS_APICID = 1,
56 	SETWA_FLAGS_MEM = 2,
57 	SETWA_FLAGS_PCIE_SBDF = 4,
58 };
59 
60 /*
61  * Vendor extensions for platform specific operations
62  */
63 struct vendor_error_type_extension {
64 	u32	length;
65 	u32	pcie_sbdf;
66 	u16	vendor_id;
67 	u16	device_id;
68 	u8	rev_id;
69 	u8	reserved[3];
70 };
71 
72 static u32 notrigger;
73 
74 static u32 vendor_flags;
75 static struct debugfs_blob_wrapper vendor_blob;
76 static char vendor_dev[64];
77 
78 /*
79  * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
80  * EINJ table through an unpublished extension. Use with caution as
81  * most will ignore the parameter and make their own choice of address
82  * for error injection.  This extension is used only if
83  * param_extension module parameter is specified.
84  */
85 struct einj_parameter {
86 	u64 type;
87 	u64 reserved1;
88 	u64 reserved2;
89 	u64 param1;
90 	u64 param2;
91 };
92 
93 #define EINJ_OP_BUSY			0x1
94 #define EINJ_STATUS_SUCCESS		0x0
95 #define EINJ_STATUS_FAIL		0x1
96 #define EINJ_STATUS_INVAL		0x2
97 
98 #define EINJ_TAB_ENTRY(tab)						\
99 	((struct acpi_whea_header *)((char *)(tab) +			\
100 				    sizeof(struct acpi_table_einj)))
101 
102 static bool param_extension;
103 module_param(param_extension, bool, 0);
104 
105 static struct acpi_table_einj *einj_tab;
106 
107 static struct apei_resources einj_resources;
108 
109 static struct apei_exec_ins_type einj_ins_type[] = {
110 	[ACPI_EINJ_READ_REGISTER] = {
111 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
112 		.run   = apei_exec_read_register,
113 	},
114 	[ACPI_EINJ_READ_REGISTER_VALUE] = {
115 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
116 		.run   = apei_exec_read_register_value,
117 	},
118 	[ACPI_EINJ_WRITE_REGISTER] = {
119 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
120 		.run   = apei_exec_write_register,
121 	},
122 	[ACPI_EINJ_WRITE_REGISTER_VALUE] = {
123 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
124 		.run   = apei_exec_write_register_value,
125 	},
126 	[ACPI_EINJ_NOOP] = {
127 		.flags = 0,
128 		.run   = apei_exec_noop,
129 	},
130 };
131 
132 /*
133  * Prevent EINJ interpreter to run simultaneously, because the
134  * corresponding firmware implementation may not work properly when
135  * invoked simultaneously.
136  */
137 static DEFINE_MUTEX(einj_mutex);
138 
139 static void *einj_param;
140 
141 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
142 {
143 	apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
144 			   EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
145 }
146 
147 static int __einj_get_available_error_type(u32 *type)
148 {
149 	struct apei_exec_context ctx;
150 	int rc;
151 
152 	einj_exec_ctx_init(&ctx);
153 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
154 	if (rc)
155 		return rc;
156 	*type = apei_exec_ctx_get_output(&ctx);
157 
158 	return 0;
159 }
160 
161 /* Get error injection capabilities of the platform */
162 static int einj_get_available_error_type(u32 *type)
163 {
164 	int rc;
165 
166 	mutex_lock(&einj_mutex);
167 	rc = __einj_get_available_error_type(type);
168 	mutex_unlock(&einj_mutex);
169 
170 	return rc;
171 }
172 
173 static int einj_timedout(u64 *t)
174 {
175 	if ((s64)*t < SLEEP_UNIT_MIN) {
176 		pr_warn(FW_WARN "Firmware does not respond in time\n");
177 		return 1;
178 	}
179 	*t -= SLEEP_UNIT_MIN;
180 	usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX);
181 
182 	return 0;
183 }
184 
185 static void check_vendor_extension(u64 paddr,
186 				   struct set_error_type_with_address *v5param)
187 {
188 	int	offset = v5param->vendor_extension;
189 	struct	vendor_error_type_extension *v;
190 	u32	sbdf;
191 
192 	if (!offset)
193 		return;
194 	v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
195 	if (!v)
196 		return;
197 	sbdf = v->pcie_sbdf;
198 	sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
199 		sbdf >> 24, (sbdf >> 16) & 0xff,
200 		(sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
201 		 v->vendor_id, v->device_id, v->rev_id);
202 	acpi_os_unmap_iomem(v, sizeof(*v));
203 }
204 
205 static void *einj_get_parameter_address(void)
206 {
207 	int i;
208 	u64 pa_v4 = 0, pa_v5 = 0;
209 	struct acpi_whea_header *entry;
210 
211 	entry = EINJ_TAB_ENTRY(einj_tab);
212 	for (i = 0; i < einj_tab->entries; i++) {
213 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
214 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
215 		    entry->register_region.space_id ==
216 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
217 			pa_v4 = get_unaligned(&entry->register_region.address);
218 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
219 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
220 		    entry->register_region.space_id ==
221 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
222 			pa_v5 = get_unaligned(&entry->register_region.address);
223 		entry++;
224 	}
225 	if (pa_v5) {
226 		struct set_error_type_with_address *v5param;
227 
228 		v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
229 		if (v5param) {
230 			acpi5 = 1;
231 			check_vendor_extension(pa_v5, v5param);
232 			return v5param;
233 		}
234 	}
235 	if (param_extension && pa_v4) {
236 		struct einj_parameter *v4param;
237 
238 		v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
239 		if (!v4param)
240 			return NULL;
241 		if (v4param->reserved1 || v4param->reserved2) {
242 			acpi_os_unmap_iomem(v4param, sizeof(*v4param));
243 			return NULL;
244 		}
245 		return v4param;
246 	}
247 
248 	return NULL;
249 }
250 
251 /* do sanity check to trigger table */
252 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
253 {
254 	if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
255 		return -EINVAL;
256 	if (trigger_tab->table_size > PAGE_SIZE ||
257 	    trigger_tab->table_size < trigger_tab->header_size)
258 		return -EINVAL;
259 	if (trigger_tab->entry_count !=
260 	    (trigger_tab->table_size - trigger_tab->header_size) /
261 	    sizeof(struct acpi_einj_entry))
262 		return -EINVAL;
263 
264 	return 0;
265 }
266 
267 static struct acpi_generic_address *einj_get_trigger_parameter_region(
268 	struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
269 {
270 	int i;
271 	struct acpi_whea_header *entry;
272 
273 	entry = (struct acpi_whea_header *)
274 		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
275 	for (i = 0; i < trigger_tab->entry_count; i++) {
276 		if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
277 		entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE &&
278 		entry->register_region.space_id ==
279 			ACPI_ADR_SPACE_SYSTEM_MEMORY &&
280 		(entry->register_region.address & param2) == (param1 & param2))
281 			return &entry->register_region;
282 		entry++;
283 	}
284 
285 	return NULL;
286 }
287 /* Execute instructions in trigger error action table */
288 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
289 				u64 param1, u64 param2)
290 {
291 	struct acpi_einj_trigger *trigger_tab = NULL;
292 	struct apei_exec_context trigger_ctx;
293 	struct apei_resources trigger_resources;
294 	struct acpi_whea_header *trigger_entry;
295 	struct resource *r;
296 	u32 table_size;
297 	int rc = -EIO;
298 	struct acpi_generic_address *trigger_param_region = NULL;
299 
300 	r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
301 			       "APEI EINJ Trigger Table");
302 	if (!r) {
303 		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
304 		       (unsigned long long)trigger_paddr,
305 		       (unsigned long long)trigger_paddr +
306 			    sizeof(*trigger_tab) - 1);
307 		goto out;
308 	}
309 	trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
310 	if (!trigger_tab) {
311 		pr_err("Failed to map trigger table!\n");
312 		goto out_rel_header;
313 	}
314 	rc = einj_check_trigger_header(trigger_tab);
315 	if (rc) {
316 		pr_warn(FW_BUG "Invalid trigger error action table.\n");
317 		goto out_rel_header;
318 	}
319 
320 	/* No action structures in the TRIGGER_ERROR table, nothing to do */
321 	if (!trigger_tab->entry_count)
322 		goto out_rel_header;
323 
324 	rc = -EIO;
325 	table_size = trigger_tab->table_size;
326 	r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
327 			       table_size - sizeof(*trigger_tab),
328 			       "APEI EINJ Trigger Table");
329 	if (!r) {
330 		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
331 		       (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
332 		       (unsigned long long)trigger_paddr + table_size - 1);
333 		goto out_rel_header;
334 	}
335 	iounmap(trigger_tab);
336 	trigger_tab = ioremap_cache(trigger_paddr, table_size);
337 	if (!trigger_tab) {
338 		pr_err("Failed to map trigger table!\n");
339 		goto out_rel_entry;
340 	}
341 	trigger_entry = (struct acpi_whea_header *)
342 		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
343 	apei_resources_init(&trigger_resources);
344 	apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
345 			   ARRAY_SIZE(einj_ins_type),
346 			   trigger_entry, trigger_tab->entry_count);
347 	rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
348 	if (rc)
349 		goto out_fini;
350 	rc = apei_resources_sub(&trigger_resources, &einj_resources);
351 	if (rc)
352 		goto out_fini;
353 	/*
354 	 * Some firmware will access target address specified in
355 	 * param1 to trigger the error when injecting memory error.
356 	 * This will cause resource conflict with regular memory.  So
357 	 * remove it from trigger table resources.
358 	 */
359 	if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
360 		struct apei_resources addr_resources;
361 
362 		apei_resources_init(&addr_resources);
363 		trigger_param_region = einj_get_trigger_parameter_region(
364 			trigger_tab, param1, param2);
365 		if (trigger_param_region) {
366 			rc = apei_resources_add(&addr_resources,
367 				trigger_param_region->address,
368 				trigger_param_region->bit_width/8, true);
369 			if (rc)
370 				goto out_fini;
371 			rc = apei_resources_sub(&trigger_resources,
372 					&addr_resources);
373 		}
374 		apei_resources_fini(&addr_resources);
375 		if (rc)
376 			goto out_fini;
377 	}
378 	rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
379 	if (rc)
380 		goto out_fini;
381 	rc = apei_exec_pre_map_gars(&trigger_ctx);
382 	if (rc)
383 		goto out_release;
384 
385 	rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
386 
387 	apei_exec_post_unmap_gars(&trigger_ctx);
388 out_release:
389 	apei_resources_release(&trigger_resources);
390 out_fini:
391 	apei_resources_fini(&trigger_resources);
392 out_rel_entry:
393 	release_mem_region(trigger_paddr + sizeof(*trigger_tab),
394 			   table_size - sizeof(*trigger_tab));
395 out_rel_header:
396 	release_mem_region(trigger_paddr, sizeof(*trigger_tab));
397 out:
398 	if (trigger_tab)
399 		iounmap(trigger_tab);
400 
401 	return rc;
402 }
403 
404 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
405 			       u64 param3, u64 param4)
406 {
407 	struct apei_exec_context ctx;
408 	u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
409 	int rc;
410 
411 	einj_exec_ctx_init(&ctx);
412 
413 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
414 	if (rc)
415 		return rc;
416 	apei_exec_ctx_set_input(&ctx, type);
417 	if (acpi5) {
418 		struct set_error_type_with_address *v5param = einj_param;
419 
420 		v5param->type = type;
421 		if (type & ACPI5_VENDOR_BIT) {
422 			switch (vendor_flags) {
423 			case SETWA_FLAGS_APICID:
424 				v5param->apicid = param1;
425 				break;
426 			case SETWA_FLAGS_MEM:
427 				v5param->memory_address = param1;
428 				v5param->memory_address_range = param2;
429 				break;
430 			case SETWA_FLAGS_PCIE_SBDF:
431 				v5param->pcie_sbdf = param1;
432 				break;
433 			}
434 			v5param->flags = vendor_flags;
435 		} else if (flags) {
436 			v5param->flags = flags;
437 			v5param->memory_address = param1;
438 			v5param->memory_address_range = param2;
439 			v5param->apicid = param3;
440 			v5param->pcie_sbdf = param4;
441 		} else {
442 			switch (type) {
443 			case ACPI_EINJ_PROCESSOR_CORRECTABLE:
444 			case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
445 			case ACPI_EINJ_PROCESSOR_FATAL:
446 				v5param->apicid = param1;
447 				v5param->flags = SETWA_FLAGS_APICID;
448 				break;
449 			case ACPI_EINJ_MEMORY_CORRECTABLE:
450 			case ACPI_EINJ_MEMORY_UNCORRECTABLE:
451 			case ACPI_EINJ_MEMORY_FATAL:
452 				v5param->memory_address = param1;
453 				v5param->memory_address_range = param2;
454 				v5param->flags = SETWA_FLAGS_MEM;
455 				break;
456 			case ACPI_EINJ_PCIX_CORRECTABLE:
457 			case ACPI_EINJ_PCIX_UNCORRECTABLE:
458 			case ACPI_EINJ_PCIX_FATAL:
459 				v5param->pcie_sbdf = param1;
460 				v5param->flags = SETWA_FLAGS_PCIE_SBDF;
461 				break;
462 			}
463 		}
464 	} else {
465 		rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
466 		if (rc)
467 			return rc;
468 		if (einj_param) {
469 			struct einj_parameter *v4param = einj_param;
470 
471 			v4param->param1 = param1;
472 			v4param->param2 = param2;
473 		}
474 	}
475 	rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
476 	if (rc)
477 		return rc;
478 	for (;;) {
479 		rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
480 		if (rc)
481 			return rc;
482 		val = apei_exec_ctx_get_output(&ctx);
483 		if (!(val & EINJ_OP_BUSY))
484 			break;
485 		if (einj_timedout(&timeout))
486 			return -EIO;
487 	}
488 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
489 	if (rc)
490 		return rc;
491 	val = apei_exec_ctx_get_output(&ctx);
492 	if (val != EINJ_STATUS_SUCCESS)
493 		return -EBUSY;
494 
495 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
496 	if (rc)
497 		return rc;
498 	trigger_paddr = apei_exec_ctx_get_output(&ctx);
499 	if (notrigger == 0) {
500 		rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
501 		if (rc)
502 			return rc;
503 	}
504 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
505 
506 	return rc;
507 }
508 
509 /* Inject the specified hardware error */
510 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
511 			     u64 param3, u64 param4)
512 {
513 	int rc;
514 	u64 base_addr, size;
515 
516 	/* If user manually set "flags", make sure it is legal */
517 	if (flags && (flags &
518 		~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
519 		return -EINVAL;
520 
521 	/*
522 	 * We need extra sanity checks for memory errors.
523 	 * Other types leap directly to injection.
524 	 */
525 
526 	/* ensure param1/param2 existed */
527 	if (!(param_extension || acpi5))
528 		goto inject;
529 
530 	/* ensure injection is memory related */
531 	if (type & ACPI5_VENDOR_BIT) {
532 		if (vendor_flags != SETWA_FLAGS_MEM)
533 			goto inject;
534 	} else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
535 		goto inject;
536 
537 	/*
538 	 * Disallow crazy address masks that give BIOS leeway to pick
539 	 * injection address almost anywhere. Insist on page or
540 	 * better granularity and that target address is normal RAM or
541 	 * NVDIMM.
542 	 */
543 	base_addr = param1 & param2;
544 	size = ~param2 + 1;
545 
546 	if (((param2 & PAGE_MASK) != PAGE_MASK) ||
547 	    ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
548 				!= REGION_INTERSECTS) &&
549 	     (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
550 				!= REGION_INTERSECTS) &&
551 	     (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED)
552 				!= REGION_INTERSECTS) &&
553 	     !arch_is_platform_page(base_addr)))
554 		return -EINVAL;
555 
556 	if (is_zero_pfn(base_addr >> PAGE_SHIFT))
557 		return -EADDRINUSE;
558 
559 inject:
560 	mutex_lock(&einj_mutex);
561 	rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
562 	mutex_unlock(&einj_mutex);
563 
564 	return rc;
565 }
566 
567 static u32 error_type;
568 static u32 error_flags;
569 static u64 error_param1;
570 static u64 error_param2;
571 static u64 error_param3;
572 static u64 error_param4;
573 static struct dentry *einj_debug_dir;
574 static const char * const einj_error_type_string[] = {
575 	"0x00000001\tProcessor Correctable\n",
576 	"0x00000002\tProcessor Uncorrectable non-fatal\n",
577 	"0x00000004\tProcessor Uncorrectable fatal\n",
578 	"0x00000008\tMemory Correctable\n",
579 	"0x00000010\tMemory Uncorrectable non-fatal\n",
580 	"0x00000020\tMemory Uncorrectable fatal\n",
581 	"0x00000040\tPCI Express Correctable\n",
582 	"0x00000080\tPCI Express Uncorrectable non-fatal\n",
583 	"0x00000100\tPCI Express Uncorrectable fatal\n",
584 	"0x00000200\tPlatform Correctable\n",
585 	"0x00000400\tPlatform Uncorrectable non-fatal\n",
586 	"0x00000800\tPlatform Uncorrectable fatal\n",
587 };
588 
589 static int available_error_type_show(struct seq_file *m, void *v)
590 {
591 	int rc;
592 	u32 available_error_type = 0;
593 
594 	rc = einj_get_available_error_type(&available_error_type);
595 	if (rc)
596 		return rc;
597 	for (int pos = 0; pos < ARRAY_SIZE(einj_error_type_string); pos++)
598 		if (available_error_type & BIT(pos))
599 			seq_puts(m, einj_error_type_string[pos]);
600 
601 	return 0;
602 }
603 
604 DEFINE_SHOW_ATTRIBUTE(available_error_type);
605 
606 static int error_type_get(void *data, u64 *val)
607 {
608 	*val = error_type;
609 
610 	return 0;
611 }
612 
613 static int error_type_set(void *data, u64 val)
614 {
615 	int rc;
616 	u32 available_error_type = 0;
617 	u32 tval, vendor;
618 
619 	/*
620 	 * Vendor defined types have 0x80000000 bit set, and
621 	 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
622 	 */
623 	vendor = val & ACPI5_VENDOR_BIT;
624 	tval = val & 0x7fffffff;
625 
626 	/* Only one error type can be specified */
627 	if (tval & (tval - 1))
628 		return -EINVAL;
629 	if (!vendor) {
630 		rc = einj_get_available_error_type(&available_error_type);
631 		if (rc)
632 			return rc;
633 		if (!(val & available_error_type))
634 			return -EINVAL;
635 	}
636 	error_type = val;
637 
638 	return 0;
639 }
640 
641 DEFINE_DEBUGFS_ATTRIBUTE(error_type_fops, error_type_get, error_type_set,
642 			 "0x%llx\n");
643 
644 static int error_inject_set(void *data, u64 val)
645 {
646 	if (!error_type)
647 		return -EINVAL;
648 
649 	return einj_error_inject(error_type, error_flags, error_param1, error_param2,
650 		error_param3, error_param4);
651 }
652 
653 DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
654 
655 static int einj_check_table(struct acpi_table_einj *einj_tab)
656 {
657 	if ((einj_tab->header_length !=
658 	     (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
659 	    && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
660 		return -EINVAL;
661 	if (einj_tab->header.length < sizeof(struct acpi_table_einj))
662 		return -EINVAL;
663 	if (einj_tab->entries !=
664 	    (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
665 	    sizeof(struct acpi_einj_entry))
666 		return -EINVAL;
667 
668 	return 0;
669 }
670 
671 static int __init einj_init(void)
672 {
673 	int rc;
674 	acpi_status status;
675 	struct apei_exec_context ctx;
676 
677 	if (acpi_disabled) {
678 		pr_info("ACPI disabled.\n");
679 		return -ENODEV;
680 	}
681 
682 	status = acpi_get_table(ACPI_SIG_EINJ, 0,
683 				(struct acpi_table_header **)&einj_tab);
684 	if (status == AE_NOT_FOUND) {
685 		pr_warn("EINJ table not found.\n");
686 		return -ENODEV;
687 	} else if (ACPI_FAILURE(status)) {
688 		pr_err("Failed to get EINJ table: %s\n",
689 				acpi_format_exception(status));
690 		return -EINVAL;
691 	}
692 
693 	rc = einj_check_table(einj_tab);
694 	if (rc) {
695 		pr_warn(FW_BUG "Invalid EINJ table.\n");
696 		goto err_put_table;
697 	}
698 
699 	rc = -ENOMEM;
700 	einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
701 
702 	debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
703 			    NULL, &available_error_type_fops);
704 	debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
705 				   NULL, &error_type_fops);
706 	debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
707 				   NULL, &error_inject_fops);
708 
709 	apei_resources_init(&einj_resources);
710 	einj_exec_ctx_init(&ctx);
711 	rc = apei_exec_collect_resources(&ctx, &einj_resources);
712 	if (rc) {
713 		pr_err("Error collecting EINJ resources.\n");
714 		goto err_fini;
715 	}
716 
717 	rc = apei_resources_request(&einj_resources, "APEI EINJ");
718 	if (rc) {
719 		pr_err("Error requesting memory/port resources.\n");
720 		goto err_fini;
721 	}
722 
723 	rc = apei_exec_pre_map_gars(&ctx);
724 	if (rc) {
725 		pr_err("Error pre-mapping GARs.\n");
726 		goto err_release;
727 	}
728 
729 	einj_param = einj_get_parameter_address();
730 	if ((param_extension || acpi5) && einj_param) {
731 		debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
732 				   &error_flags);
733 		debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
734 				   &error_param1);
735 		debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
736 				   &error_param2);
737 		debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
738 				   &error_param3);
739 		debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
740 				   &error_param4);
741 		debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
742 				   einj_debug_dir, &notrigger);
743 	}
744 
745 	if (vendor_dev[0]) {
746 		vendor_blob.data = vendor_dev;
747 		vendor_blob.size = strlen(vendor_dev);
748 		debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
749 				    &vendor_blob);
750 		debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
751 				   einj_debug_dir, &vendor_flags);
752 	}
753 
754 	pr_info("Error INJection is initialized.\n");
755 
756 	return 0;
757 
758 err_release:
759 	apei_resources_release(&einj_resources);
760 err_fini:
761 	apei_resources_fini(&einj_resources);
762 	debugfs_remove_recursive(einj_debug_dir);
763 err_put_table:
764 	acpi_put_table((struct acpi_table_header *)einj_tab);
765 
766 	return rc;
767 }
768 
769 static void __exit einj_exit(void)
770 {
771 	struct apei_exec_context ctx;
772 
773 	if (einj_param) {
774 		acpi_size size = (acpi5) ?
775 			sizeof(struct set_error_type_with_address) :
776 			sizeof(struct einj_parameter);
777 
778 		acpi_os_unmap_iomem(einj_param, size);
779 	}
780 	einj_exec_ctx_init(&ctx);
781 	apei_exec_post_unmap_gars(&ctx);
782 	apei_resources_release(&einj_resources);
783 	apei_resources_fini(&einj_resources);
784 	debugfs_remove_recursive(einj_debug_dir);
785 	acpi_put_table((struct acpi_table_header *)einj_tab);
786 }
787 
788 module_init(einj_init);
789 module_exit(einj_exit);
790 
791 MODULE_AUTHOR("Huang Ying");
792 MODULE_DESCRIPTION("APEI Error INJection support");
793 MODULE_LICENSE("GPL");
794