xref: /linux/drivers/acpi/apei/einj-core.c (revision f70e1e7980f3611039d7b9a1b34beaaba1054af7)
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 <linux/device/faux.h>
25 #include <linux/unaligned.h>
26 
27 #include "apei-internal.h"
28 
29 #undef pr_fmt
30 #define pr_fmt(fmt) "EINJ: " fmt
31 
32 #define SLEEP_UNIT_MIN		1000			/* 1ms */
33 #define SLEEP_UNIT_MAX		5000			/* 5ms */
34 /* Firmware should respond within 1 seconds */
35 #define FIRMWARE_TIMEOUT	(1 * USEC_PER_SEC)
36 #define COMPONENT_LEN		16
37 #define ACPI65_EINJV2_SUPP	BIT(30)
38 #define ACPI5_VENDOR_BIT	BIT(31)
39 #define MEM_ERROR_MASK		(ACPI_EINJ_MEMORY_CORRECTABLE | \
40 				ACPI_EINJ_MEMORY_UNCORRECTABLE | \
41 				ACPI_EINJ_MEMORY_FATAL)
42 #define CXL_ERROR_MASK		(ACPI_EINJ_CXL_CACHE_CORRECTABLE | \
43 				ACPI_EINJ_CXL_CACHE_UNCORRECTABLE | \
44 				ACPI_EINJ_CXL_CACHE_FATAL | \
45 				ACPI_EINJ_CXL_MEM_CORRECTABLE | \
46 				ACPI_EINJ_CXL_MEM_UNCORRECTABLE | \
47 				ACPI_EINJ_CXL_MEM_FATAL)
48 
49 /*
50  * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
51  */
52 static int acpi5;
53 
54 struct syndrome_array {
55 	union {
56 		u8	acpi_id[COMPONENT_LEN];
57 		u8	device_id[COMPONENT_LEN];
58 		u8	pcie_sbdf[COMPONENT_LEN];
59 		u8	vendor_id[COMPONENT_LEN];
60 	} comp_id;
61 	union {
62 		u8	proc_synd[COMPONENT_LEN];
63 		u8	mem_synd[COMPONENT_LEN];
64 		u8	pcie_synd[COMPONENT_LEN];
65 		u8	vendor_synd[COMPONENT_LEN];
66 	} comp_synd;
67 };
68 
69 struct einjv2_extension_struct {
70 	u32 length;
71 	u16 revision;
72 	u16 component_arr_count;
73 	struct syndrome_array component_arr[] __counted_by(component_arr_count);
74 };
75 
76 struct set_error_type_with_address {
77 	u32	type;
78 	u32	vendor_extension;
79 	u32	flags;
80 	u32	apicid;
81 	u64	memory_address;
82 	u64	memory_address_range;
83 	u32	pcie_sbdf;
84 	struct	einjv2_extension_struct einjv2_struct;
85 };
86 enum {
87 	SETWA_FLAGS_APICID = 1,
88 	SETWA_FLAGS_MEM = 2,
89 	SETWA_FLAGS_PCIE_SBDF = 4,
90 	SETWA_FLAGS_EINJV2 = 8,
91 };
92 
93 /*
94  * Vendor extensions for platform specific operations
95  */
96 struct vendor_error_type_extension {
97 	u32	length;
98 	u32	pcie_sbdf;
99 	u16	vendor_id;
100 	u16	device_id;
101 	u8	rev_id;
102 	u8	reserved[3];
103 };
104 
105 static u32 notrigger;
106 
107 static u32 vendor_flags;
108 static struct debugfs_blob_wrapper vendor_blob;
109 static struct debugfs_blob_wrapper vendor_errors;
110 static char vendor_dev[64];
111 
112 static u32 max_nr_components;
113 static u32 available_error_type;
114 static u32 available_error_type_v2;
115 static struct syndrome_array *syndrome_data;
116 
117 /*
118  * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
119  * EINJ table through an unpublished extension. Use with caution as
120  * most will ignore the parameter and make their own choice of address
121  * for error injection.  This extension is used only if
122  * param_extension module parameter is specified.
123  */
124 struct einj_parameter {
125 	u64 type;
126 	u64 reserved1;
127 	u64 reserved2;
128 	u64 param1;
129 	u64 param2;
130 };
131 
132 #define EINJ_OP_BUSY			0x1
133 #define EINJ_STATUS_SUCCESS		0x0
134 #define EINJ_STATUS_FAIL		0x1
135 #define EINJ_STATUS_INVAL		0x2
136 
137 #define EINJ_TAB_ENTRY(tab)						\
138 	((struct acpi_whea_header *)((char *)(tab) +			\
139 				    sizeof(struct acpi_table_einj)))
140 
141 static bool param_extension;
142 module_param(param_extension, bool, 0);
143 
144 static struct acpi_table_einj *einj_tab;
145 
146 static struct apei_resources einj_resources;
147 
148 static struct apei_exec_ins_type einj_ins_type[] = {
149 	[ACPI_EINJ_READ_REGISTER] = {
150 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
151 		.run   = apei_exec_read_register,
152 	},
153 	[ACPI_EINJ_READ_REGISTER_VALUE] = {
154 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
155 		.run   = apei_exec_read_register_value,
156 	},
157 	[ACPI_EINJ_WRITE_REGISTER] = {
158 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
159 		.run   = apei_exec_write_register,
160 	},
161 	[ACPI_EINJ_WRITE_REGISTER_VALUE] = {
162 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
163 		.run   = apei_exec_write_register_value,
164 	},
165 	[ACPI_EINJ_NOOP] = {
166 		.flags = 0,
167 		.run   = apei_exec_noop,
168 	},
169 };
170 
171 /*
172  * Prevent EINJ interpreter to run simultaneously, because the
173  * corresponding firmware implementation may not work properly when
174  * invoked simultaneously.
175  */
176 static DEFINE_MUTEX(einj_mutex);
177 
178 /*
179  * Exported APIs use this flag to exit early if einj_probe() failed.
180  */
181 bool einj_initialized __ro_after_init;
182 
183 static void __iomem *einj_param;
184 static u32 v5param_size;
185 static bool is_v2;
186 
einj_exec_ctx_init(struct apei_exec_context * ctx)187 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
188 {
189 	apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
190 			   EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
191 }
192 
__einj_get_available_error_type(u32 * type,int einj_action)193 static int __einj_get_available_error_type(u32 *type, int einj_action)
194 {
195 	struct apei_exec_context ctx;
196 	int rc;
197 
198 	einj_exec_ctx_init(&ctx);
199 	rc = apei_exec_run(&ctx, einj_action);
200 	if (rc)
201 		return rc;
202 	*type = apei_exec_ctx_get_output(&ctx);
203 
204 	return 0;
205 }
206 
207 /* Get error injection capabilities of the platform */
einj_get_available_error_type(u32 * type,int einj_action)208 int einj_get_available_error_type(u32 *type, int einj_action)
209 {
210 	int rc;
211 
212 	mutex_lock(&einj_mutex);
213 	rc = __einj_get_available_error_type(type, einj_action);
214 	mutex_unlock(&einj_mutex);
215 
216 	return rc;
217 }
218 
einj_get_available_error_types(u32 * type1,u32 * type2)219 static int einj_get_available_error_types(u32 *type1, u32 *type2)
220 {
221 	int rc;
222 
223 	rc = einj_get_available_error_type(type1, ACPI_EINJ_GET_ERROR_TYPE);
224 	if (rc)
225 		return rc;
226 	if (*type1 & ACPI65_EINJV2_SUPP) {
227 		rc = einj_get_available_error_type(type2,
228 						   ACPI_EINJV2_GET_ERROR_TYPE);
229 		if (rc)
230 			return rc;
231 	}
232 
233 	return 0;
234 }
235 
einj_timedout(u64 * t)236 static int einj_timedout(u64 *t)
237 {
238 	if ((s64)*t < SLEEP_UNIT_MIN) {
239 		pr_warn(FW_WARN "Firmware does not respond in time\n");
240 		return 1;
241 	}
242 	*t -= SLEEP_UNIT_MIN;
243 	usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX);
244 
245 	return 0;
246 }
247 
get_oem_vendor_struct(u64 paddr,int offset,struct vendor_error_type_extension * v)248 static void get_oem_vendor_struct(u64 paddr, int offset,
249 				  struct vendor_error_type_extension *v)
250 {
251 	unsigned long vendor_size;
252 	u64 target_pa = paddr + offset + sizeof(struct vendor_error_type_extension);
253 
254 	vendor_size = v->length - sizeof(struct vendor_error_type_extension);
255 
256 	if (vendor_size)
257 		vendor_errors.data = acpi_os_map_memory(target_pa, vendor_size);
258 
259 	if (vendor_errors.data)
260 		vendor_errors.size = vendor_size;
261 }
262 
check_vendor_extension(u64 paddr,struct set_error_type_with_address * v5param)263 static void check_vendor_extension(u64 paddr,
264 				   struct set_error_type_with_address *v5param)
265 {
266 	int	offset = v5param->vendor_extension;
267 	struct	vendor_error_type_extension v;
268 	struct vendor_error_type_extension __iomem *p;
269 	u32	sbdf;
270 
271 	if (!offset)
272 		return;
273 	p = acpi_os_map_iomem(paddr + offset, sizeof(*p));
274 	if (!p)
275 		return;
276 	memcpy_fromio(&v, p, sizeof(v));
277 	get_oem_vendor_struct(paddr, offset, &v);
278 	sbdf = v.pcie_sbdf;
279 	sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
280 		sbdf >> 24, (sbdf >> 16) & 0xff,
281 		(sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
282 		 v.vendor_id, v.device_id, v.rev_id);
283 	acpi_os_unmap_iomem(p, sizeof(v));
284 }
285 
einj_get_parameter_address(void)286 static void __iomem *einj_get_parameter_address(void)
287 {
288 	int i;
289 	u64 pa_v4 = 0, pa_v5 = 0;
290 	struct acpi_whea_header *entry;
291 
292 	entry = EINJ_TAB_ENTRY(einj_tab);
293 	for (i = 0; i < einj_tab->entries; i++) {
294 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
295 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
296 		    entry->register_region.space_id ==
297 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
298 			pa_v4 = get_unaligned(&entry->register_region.address);
299 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
300 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
301 		    entry->register_region.space_id ==
302 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
303 			pa_v5 = get_unaligned(&entry->register_region.address);
304 		entry++;
305 	}
306 	if (pa_v5) {
307 		struct set_error_type_with_address v5param;
308 		struct set_error_type_with_address __iomem *p;
309 
310 		v5param_size = sizeof(v5param);
311 		p = acpi_os_map_iomem(pa_v5, sizeof(*p));
312 		if (p) {
313 			int offset, len;
314 
315 			memcpy_fromio(&v5param, p, v5param_size);
316 			acpi5 = 1;
317 			check_vendor_extension(pa_v5, &v5param);
318 			if (is_v2 && available_error_type & ACPI65_EINJV2_SUPP) {
319 				len = v5param.einjv2_struct.length;
320 				offset = offsetof(struct einjv2_extension_struct, component_arr);
321 				max_nr_components = (len - offset) /
322 						sizeof(v5param.einjv2_struct.component_arr[0]);
323 				/*
324 				 * The first call to acpi_os_map_iomem above does not include the
325 				 * component array, instead it is used to read and calculate maximum
326 				 * number of components supported by the system. Below, the mapping
327 				 * is expanded to include the component array.
328 				 */
329 				acpi_os_unmap_iomem(p, v5param_size);
330 				offset = offsetof(struct set_error_type_with_address, einjv2_struct);
331 				v5param_size = offset + struct_size(&v5param.einjv2_struct,
332 					component_arr, max_nr_components);
333 				p = acpi_os_map_iomem(pa_v5, v5param_size);
334 			}
335 			return p;
336 		}
337 	}
338 	if (param_extension && pa_v4) {
339 		struct einj_parameter v4param;
340 		struct einj_parameter __iomem *p;
341 
342 		p = acpi_os_map_iomem(pa_v4, sizeof(*p));
343 		if (!p)
344 			return NULL;
345 		memcpy_fromio(&v4param, p, sizeof(v4param));
346 		if (v4param.reserved1 || v4param.reserved2) {
347 			acpi_os_unmap_iomem(p, sizeof(v4param));
348 			return NULL;
349 		}
350 		return p;
351 	}
352 
353 	return NULL;
354 }
355 
356 /* do sanity check to trigger table */
einj_check_trigger_header(struct acpi_einj_trigger * trigger_tab)357 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
358 {
359 	if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
360 		return -EINVAL;
361 	if (trigger_tab->table_size > PAGE_SIZE ||
362 	    trigger_tab->table_size < trigger_tab->header_size)
363 		return -EINVAL;
364 	if (trigger_tab->entry_count !=
365 	    (trigger_tab->table_size - trigger_tab->header_size) /
366 	    sizeof(struct acpi_einj_entry))
367 		return -EINVAL;
368 
369 	return 0;
370 }
371 
einj_get_trigger_parameter_region(struct acpi_einj_trigger * trigger_tab,u64 param1,u64 param2)372 static struct acpi_generic_address *einj_get_trigger_parameter_region(
373 	struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
374 {
375 	int i;
376 	struct acpi_whea_header *entry;
377 
378 	entry = (struct acpi_whea_header *)
379 		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
380 	for (i = 0; i < trigger_tab->entry_count; i++) {
381 		if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
382 		entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE &&
383 		entry->register_region.space_id ==
384 			ACPI_ADR_SPACE_SYSTEM_MEMORY &&
385 		(entry->register_region.address & param2) == (param1 & param2))
386 			return &entry->register_region;
387 		entry++;
388 	}
389 
390 	return NULL;
391 }
392 /* Execute instructions in trigger error action table */
__einj_error_trigger(u64 trigger_paddr,u32 type,u64 param1,u64 param2)393 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
394 				u64 param1, u64 param2)
395 {
396 	struct acpi_einj_trigger trigger_tab;
397 	struct acpi_einj_trigger *full_trigger_tab;
398 	struct apei_exec_context trigger_ctx;
399 	struct apei_resources trigger_resources;
400 	struct acpi_whea_header *trigger_entry;
401 	struct resource *r;
402 	u32 table_size;
403 	int rc = -EIO;
404 	struct acpi_generic_address *trigger_param_region = NULL;
405 	struct acpi_einj_trigger __iomem *p = NULL;
406 
407 	r = request_mem_region(trigger_paddr, sizeof(trigger_tab),
408 			       "APEI EINJ Trigger Table");
409 	if (!r) {
410 		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
411 		       (unsigned long long)trigger_paddr,
412 		       (unsigned long long)trigger_paddr +
413 			    sizeof(trigger_tab) - 1);
414 		goto out;
415 	}
416 	p = ioremap_cache(trigger_paddr, sizeof(*p));
417 	if (!p) {
418 		pr_err("Failed to map trigger table!\n");
419 		goto out_rel_header;
420 	}
421 	memcpy_fromio(&trigger_tab, p, sizeof(trigger_tab));
422 	rc = einj_check_trigger_header(&trigger_tab);
423 	if (rc) {
424 		pr_warn(FW_BUG "Invalid trigger error action table.\n");
425 		goto out_rel_header;
426 	}
427 
428 	/* No action structures in the TRIGGER_ERROR table, nothing to do */
429 	if (!trigger_tab.entry_count)
430 		goto out_rel_header;
431 
432 	rc = -EIO;
433 	table_size = trigger_tab.table_size;
434 	full_trigger_tab = kmalloc(table_size, GFP_KERNEL);
435 	if (!full_trigger_tab)
436 		goto out_rel_header;
437 	r = request_mem_region(trigger_paddr + sizeof(trigger_tab),
438 			       table_size - sizeof(trigger_tab),
439 			       "APEI EINJ Trigger Table");
440 	if (!r) {
441 		pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
442 		       (unsigned long long)trigger_paddr + sizeof(trigger_tab),
443 		       (unsigned long long)trigger_paddr + table_size - 1);
444 		goto out_free_trigger_tab;
445 	}
446 	iounmap(p);
447 	p = ioremap_cache(trigger_paddr, table_size);
448 	if (!p) {
449 		pr_err("Failed to map trigger table!\n");
450 		goto out_rel_entry;
451 	}
452 	memcpy_fromio(full_trigger_tab, p, table_size);
453 	trigger_entry = (struct acpi_whea_header *)
454 		((char *)full_trigger_tab + sizeof(struct acpi_einj_trigger));
455 	apei_resources_init(&trigger_resources);
456 	apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
457 			   ARRAY_SIZE(einj_ins_type),
458 			   trigger_entry, trigger_tab.entry_count);
459 	rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
460 	if (rc)
461 		goto out_fini;
462 	rc = apei_resources_sub(&trigger_resources, &einj_resources);
463 	if (rc)
464 		goto out_fini;
465 	/*
466 	 * Some firmware will access target address specified in
467 	 * param1 to trigger the error when injecting memory error.
468 	 * This will cause resource conflict with regular memory.  So
469 	 * remove it from trigger table resources.
470 	 */
471 	if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
472 		struct apei_resources addr_resources;
473 
474 		apei_resources_init(&addr_resources);
475 		trigger_param_region = einj_get_trigger_parameter_region(
476 			full_trigger_tab, param1, param2);
477 		if (trigger_param_region) {
478 			rc = apei_resources_add(&addr_resources,
479 				trigger_param_region->address,
480 				trigger_param_region->bit_width/8, true);
481 			if (rc)
482 				goto out_fini;
483 			rc = apei_resources_sub(&trigger_resources,
484 					&addr_resources);
485 		}
486 		apei_resources_fini(&addr_resources);
487 		if (rc)
488 			goto out_fini;
489 	}
490 	rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
491 	if (rc)
492 		goto out_fini;
493 	rc = apei_exec_pre_map_gars(&trigger_ctx);
494 	if (rc)
495 		goto out_release;
496 
497 	rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
498 
499 	apei_exec_post_unmap_gars(&trigger_ctx);
500 out_release:
501 	apei_resources_release(&trigger_resources);
502 out_fini:
503 	apei_resources_fini(&trigger_resources);
504 out_rel_entry:
505 	release_mem_region(trigger_paddr + sizeof(trigger_tab),
506 			   table_size - sizeof(trigger_tab));
507 out_free_trigger_tab:
508 	kfree(full_trigger_tab);
509 out_rel_header:
510 	release_mem_region(trigger_paddr, sizeof(trigger_tab));
511 out:
512 	if (p)
513 		iounmap(p);
514 
515 	return rc;
516 }
517 
is_end_of_list(u8 * val)518 static bool is_end_of_list(u8 *val)
519 {
520 	for (int i = 0; i < COMPONENT_LEN; ++i) {
521 		if (val[i] != 0xFF)
522 			return false;
523 	}
524 	return true;
525 }
__einj_error_inject(u32 type,u32 flags,u64 param1,u64 param2,u64 param3,u64 param4)526 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
527 			       u64 param3, u64 param4)
528 {
529 	struct apei_exec_context ctx;
530 	u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
531 	int i, rc;
532 
533 	einj_exec_ctx_init(&ctx);
534 
535 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
536 	if (rc)
537 		return rc;
538 	apei_exec_ctx_set_input(&ctx, type);
539 	if (acpi5) {
540 		struct set_error_type_with_address *v5param;
541 
542 		v5param = kmalloc(v5param_size, GFP_KERNEL);
543 		if (!v5param)
544 			return -ENOMEM;
545 
546 		memcpy_fromio(v5param, einj_param, v5param_size);
547 		v5param->type = type;
548 		if (type & ACPI5_VENDOR_BIT) {
549 			switch (vendor_flags) {
550 			case SETWA_FLAGS_APICID:
551 				v5param->apicid = param1;
552 				break;
553 			case SETWA_FLAGS_MEM:
554 				v5param->memory_address = param1;
555 				v5param->memory_address_range = param2;
556 				break;
557 			case SETWA_FLAGS_PCIE_SBDF:
558 				v5param->pcie_sbdf = param1;
559 				break;
560 			}
561 			v5param->flags = vendor_flags;
562 		} else if (flags) {
563 			v5param->flags = flags;
564 			v5param->memory_address = param1;
565 			v5param->memory_address_range = param2;
566 
567 			if (is_v2) {
568 				for (i = 0; i < max_nr_components; i++) {
569 					if (is_end_of_list(syndrome_data[i].comp_id.acpi_id))
570 						break;
571 					v5param->einjv2_struct.component_arr[i].comp_id =
572 						syndrome_data[i].comp_id;
573 					v5param->einjv2_struct.component_arr[i].comp_synd =
574 						syndrome_data[i].comp_synd;
575 				}
576 				v5param->einjv2_struct.component_arr_count = i;
577 			} else {
578 				v5param->apicid = param3;
579 				v5param->pcie_sbdf = param4;
580 			}
581 		} else {
582 			switch (type) {
583 			case ACPI_EINJ_PROCESSOR_CORRECTABLE:
584 			case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
585 			case ACPI_EINJ_PROCESSOR_FATAL:
586 				v5param->apicid = param1;
587 				v5param->flags = SETWA_FLAGS_APICID;
588 				break;
589 			case ACPI_EINJ_MEMORY_CORRECTABLE:
590 			case ACPI_EINJ_MEMORY_UNCORRECTABLE:
591 			case ACPI_EINJ_MEMORY_FATAL:
592 				v5param->memory_address = param1;
593 				v5param->memory_address_range = param2;
594 				v5param->flags = SETWA_FLAGS_MEM;
595 				break;
596 			case ACPI_EINJ_PCIX_CORRECTABLE:
597 			case ACPI_EINJ_PCIX_UNCORRECTABLE:
598 			case ACPI_EINJ_PCIX_FATAL:
599 				v5param->pcie_sbdf = param1;
600 				v5param->flags = SETWA_FLAGS_PCIE_SBDF;
601 				break;
602 			}
603 		}
604 		memcpy_toio(einj_param, v5param, v5param_size);
605 		kfree(v5param);
606 	} else {
607 		rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
608 		if (rc)
609 			return rc;
610 		if (einj_param) {
611 			struct einj_parameter v4param;
612 
613 			memcpy_fromio(&v4param, einj_param, sizeof(v4param));
614 			v4param.param1 = param1;
615 			v4param.param2 = param2;
616 			memcpy_toio(einj_param, &v4param, sizeof(v4param));
617 		}
618 	}
619 	rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
620 	if (rc)
621 		return rc;
622 	for (;;) {
623 		rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
624 		if (rc)
625 			return rc;
626 		val = apei_exec_ctx_get_output(&ctx);
627 		if (!(val & EINJ_OP_BUSY))
628 			break;
629 		if (einj_timedout(&timeout))
630 			return -EIO;
631 	}
632 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
633 	if (rc)
634 		return rc;
635 	val = apei_exec_ctx_get_output(&ctx);
636 	if (val == EINJ_STATUS_FAIL)
637 		return -EBUSY;
638 	else if (val == EINJ_STATUS_INVAL)
639 		return -EINVAL;
640 
641 	/*
642 	 * The error is injected into the platform successfully, then it needs
643 	 * to trigger the error.
644 	 */
645 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
646 	if (rc)
647 		return rc;
648 	trigger_paddr = apei_exec_ctx_get_output(&ctx);
649 	if (notrigger == 0) {
650 		rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
651 		if (rc)
652 			return rc;
653 	}
654 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
655 
656 	return rc;
657 }
658 
659 /* Inject the specified hardware error */
einj_error_inject(u32 type,u32 flags,u64 param1,u64 param2,u64 param3,u64 param4)660 int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2, u64 param3,
661 		      u64 param4)
662 {
663 	int rc;
664 	u64 base_addr, size;
665 
666 	/* If user manually set "flags", make sure it is legal */
667 	if (flags && (flags & ~(SETWA_FLAGS_APICID | SETWA_FLAGS_MEM |
668 		      SETWA_FLAGS_PCIE_SBDF | SETWA_FLAGS_EINJV2)))
669 		return -EINVAL;
670 
671 	/* check if type is a valid EINJv2 error type */
672 	if (is_v2) {
673 		if (!(type & available_error_type_v2))
674 			return -EINVAL;
675 	}
676 	/*
677 	 * We need extra sanity checks for memory errors.
678 	 * Other types leap directly to injection.
679 	 */
680 
681 	/* ensure param1/param2 existed */
682 	if (!(param_extension || acpi5))
683 		goto inject;
684 
685 	/* ensure injection is memory related */
686 	if (type & ACPI5_VENDOR_BIT) {
687 		if (vendor_flags != SETWA_FLAGS_MEM)
688 			goto inject;
689 	} else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM)) {
690 		goto inject;
691 	}
692 
693 	/*
694 	 * Injections targeting a CXL 1.0/1.1 port have to be injected
695 	 * via the einj_cxl_rch_error_inject() path as that does the proper
696 	 * validation of the given RCRB base (MMIO) address.
697 	 */
698 	if (einj_is_cxl_error_type(type) && (flags & SETWA_FLAGS_MEM))
699 		return -EINVAL;
700 
701 	/*
702 	 * Disallow crazy address masks that give BIOS leeway to pick
703 	 * injection address almost anywhere. Insist on page or
704 	 * better granularity and that target address is normal RAM or
705 	 * NVDIMM.
706 	 */
707 	base_addr = param1 & param2;
708 	size = ~param2 + 1;
709 
710 	if (((param2 & PAGE_MASK) != PAGE_MASK) ||
711 	    ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
712 				!= REGION_INTERSECTS) &&
713 	     (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
714 				!= REGION_INTERSECTS) &&
715 	     (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED)
716 				!= REGION_INTERSECTS) &&
717 	     !arch_is_platform_page(base_addr)))
718 		return -EINVAL;
719 
720 	if (is_zero_pfn(base_addr >> PAGE_SHIFT))
721 		return -EADDRINUSE;
722 
723 inject:
724 	mutex_lock(&einj_mutex);
725 	rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
726 	mutex_unlock(&einj_mutex);
727 
728 	return rc;
729 }
730 
einj_cxl_rch_error_inject(u32 type,u32 flags,u64 param1,u64 param2,u64 param3,u64 param4)731 int einj_cxl_rch_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
732 			      u64 param3, u64 param4)
733 {
734 	int rc;
735 
736 	if (!(einj_is_cxl_error_type(type) && (flags & SETWA_FLAGS_MEM)))
737 		return -EINVAL;
738 
739 	mutex_lock(&einj_mutex);
740 	rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
741 	mutex_unlock(&einj_mutex);
742 
743 	return rc;
744 }
745 
746 static u32 error_type;
747 static u32 error_flags;
748 static u64 error_param1;
749 static u64 error_param2;
750 static u64 error_param3;
751 static u64 error_param4;
752 static struct dentry *einj_debug_dir;
753 static char einj_buf[32];
754 static bool einj_v2_enabled;
755 static struct { u32 mask; const char *str; } const einj_error_type_string[] = {
756 	{ BIT(0), "Processor Correctable" },
757 	{ BIT(1), "Processor Uncorrectable non-fatal" },
758 	{ BIT(2), "Processor Uncorrectable fatal" },
759 	{ BIT(3), "Memory Correctable" },
760 	{ BIT(4), "Memory Uncorrectable non-fatal" },
761 	{ BIT(5), "Memory Uncorrectable fatal" },
762 	{ BIT(6), "PCI Express Correctable" },
763 	{ BIT(7), "PCI Express Uncorrectable non-fatal" },
764 	{ BIT(8), "PCI Express Uncorrectable fatal" },
765 	{ BIT(9), "Platform Correctable" },
766 	{ BIT(10), "Platform Uncorrectable non-fatal" },
767 	{ BIT(11), "Platform Uncorrectable fatal"},
768 	{ BIT(31), "Vendor Defined Error Types" },
769 };
770 
771 static struct { u32 mask; const char *str; } const einjv2_error_type_string[] = {
772 	{ BIT(0), "EINJV2 Processor Error" },
773 	{ BIT(1), "EINJV2 Memory Error" },
774 	{ BIT(2), "EINJV2 PCI Express Error" },
775 };
776 
available_error_type_show(struct seq_file * m,void * v)777 static int available_error_type_show(struct seq_file *m, void *v)
778 {
779 
780 	for (int pos = 0; pos < ARRAY_SIZE(einj_error_type_string); pos++)
781 		if (available_error_type & einj_error_type_string[pos].mask)
782 			seq_printf(m, "0x%08x\t%s\n", einj_error_type_string[pos].mask,
783 				   einj_error_type_string[pos].str);
784 	if ((available_error_type & ACPI65_EINJV2_SUPP) && einj_v2_enabled) {
785 		for (int pos = 0; pos < ARRAY_SIZE(einjv2_error_type_string); pos++) {
786 			if (available_error_type_v2 & einjv2_error_type_string[pos].mask)
787 				seq_printf(m, "V2_0x%08x\t%s\n", einjv2_error_type_string[pos].mask,
788 					   einjv2_error_type_string[pos].str);
789 		}
790 	}
791 	return 0;
792 }
793 
794 DEFINE_SHOW_ATTRIBUTE(available_error_type);
795 
error_type_get(struct file * file,char __user * buf,size_t count,loff_t * ppos)796 static ssize_t error_type_get(struct file *file, char __user *buf,
797 				size_t count, loff_t *ppos)
798 {
799 	return simple_read_from_buffer(buf, count, ppos, einj_buf, strlen(einj_buf));
800 }
801 
einj_is_cxl_error_type(u64 type)802 bool einj_is_cxl_error_type(u64 type)
803 {
804 	return (type & CXL_ERROR_MASK) && (!(type & ACPI5_VENDOR_BIT));
805 }
806 
einj_validate_error_type(u64 type)807 int einj_validate_error_type(u64 type)
808 {
809 	u32 tval, vendor;
810 
811 	/* Only low 32 bits for error type are valid */
812 	if (type & GENMASK_ULL(63, 32))
813 		return -EINVAL;
814 
815 	/*
816 	 * Vendor defined types have 0x80000000 bit set, and
817 	 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
818 	 */
819 	vendor = type & ACPI5_VENDOR_BIT;
820 	tval = type & GENMASK(30, 0);
821 
822 	/* Only one error type can be specified */
823 	if (tval & (tval - 1))
824 		return -EINVAL;
825 	if (!vendor)
826 		if (!(type & (available_error_type | available_error_type_v2)))
827 			return -EINVAL;
828 
829 	return 0;
830 }
831 
error_type_set(struct file * file,const char __user * buf,size_t count,loff_t * ppos)832 static ssize_t error_type_set(struct file *file, const char __user *buf,
833 				size_t count, loff_t *ppos)
834 {
835 	int rc;
836 	u64 val;
837 
838 	/* Leave the last character for the NUL terminator */
839 	if (count > sizeof(einj_buf) - 1)
840 		return -EINVAL;
841 
842 	memset(einj_buf, 0, sizeof(einj_buf));
843 	if (copy_from_user(einj_buf, buf, count))
844 		return -EFAULT;
845 
846 	if (strncmp(einj_buf, "V2_", 3) == 0) {
847 		if (!sscanf(einj_buf, "V2_%llx", &val))
848 			return -EINVAL;
849 		is_v2 = true;
850 	} else {
851 		if (!sscanf(einj_buf, "%llx", &val))
852 			return -EINVAL;
853 		is_v2 = false;
854 	}
855 
856 	rc = einj_validate_error_type(val);
857 	if (rc)
858 		return rc;
859 
860 	error_type = val;
861 
862 	return count;
863 }
864 
865 static const struct file_operations error_type_fops = {
866 	.read		= error_type_get,
867 	.write		= error_type_set,
868 };
869 
error_inject_set(void * data,u64 val)870 static int error_inject_set(void *data, u64 val)
871 {
872 	if (!error_type)
873 		return -EINVAL;
874 
875 	if (is_v2)
876 		error_flags |= SETWA_FLAGS_EINJV2;
877 	else
878 		error_flags &= ~SETWA_FLAGS_EINJV2;
879 
880 	return einj_error_inject(error_type, error_flags, error_param1, error_param2,
881 		error_param3, error_param4);
882 }
883 
884 DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
885 
einj_check_table(struct acpi_table_einj * einj_tab)886 static int einj_check_table(struct acpi_table_einj *einj_tab)
887 {
888 	if ((einj_tab->header_length !=
889 	     (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
890 	    && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
891 		return -EINVAL;
892 	if (einj_tab->header.length < sizeof(struct acpi_table_einj))
893 		return -EINVAL;
894 	if (einj_tab->entries !=
895 	    (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
896 	    sizeof(struct acpi_einj_entry))
897 		return -EINVAL;
898 
899 	return 0;
900 }
901 
u128_read(struct file * f,char __user * buf,size_t count,loff_t * off)902 static ssize_t u128_read(struct file *f, char __user *buf, size_t count, loff_t *off)
903 {
904 	char output[2 * COMPONENT_LEN + 1];
905 	u8 *data = f->f_inode->i_private;
906 	int i;
907 
908 	if (*off >= sizeof(output))
909 		return 0;
910 
911 	for (i = 0; i < COMPONENT_LEN; i++)
912 		sprintf(output + 2 * i, "%.02x", data[COMPONENT_LEN - i - 1]);
913 	output[2 * COMPONENT_LEN] = '\n';
914 
915 	return simple_read_from_buffer(buf, count, off, output, sizeof(output));
916 }
917 
u128_write(struct file * f,const char __user * buf,size_t count,loff_t * off)918 static ssize_t u128_write(struct file *f, const char __user *buf, size_t count, loff_t *off)
919 {
920 	char input[2 + 2 * COMPONENT_LEN + 2];
921 	u8 *save = f->f_inode->i_private;
922 	u8 tmp[COMPONENT_LEN];
923 	char byte[3] = {};
924 	char *s, *e;
925 	ssize_t c;
926 	long val;
927 	int i;
928 
929 	/* Require that user supply whole input line in one write(2) syscall */
930 	if (*off)
931 		return -EINVAL;
932 
933 	c = simple_write_to_buffer(input, sizeof(input), off, buf, count);
934 	if (c < 0)
935 		return c;
936 
937 	if (c < 1 || input[c - 1] != '\n')
938 		return -EINVAL;
939 
940 	/* Empty line means invalidate this entry */
941 	if (c == 1) {
942 		memset(save, 0xff, COMPONENT_LEN);
943 		return c;
944 	}
945 
946 	if (input[0] == '0' && (input[1] == 'x' || input[1] == 'X'))
947 		s = input + 2;
948 	else
949 		s = input;
950 	e = input + c - 1;
951 
952 	for (i = 0; i < COMPONENT_LEN; i++) {
953 		byte[1] = *--e;
954 		byte[0] = e > s ? *--e : '0';
955 		if (kstrtol(byte, 16, &val))
956 			return -EINVAL;
957 		tmp[i] = val;
958 		if (e <= s)
959 			break;
960 	}
961 	while (++i < COMPONENT_LEN)
962 		tmp[i] = 0;
963 
964 	memcpy(save, tmp, COMPONENT_LEN);
965 
966 	return c;
967 }
968 
969 static const struct file_operations u128_fops = {
970 	.read	= u128_read,
971 	.write	= u128_write,
972 };
973 
setup_einjv2_component_files(void)974 static bool setup_einjv2_component_files(void)
975 {
976 	char name[32];
977 
978 	syndrome_data = kcalloc(max_nr_components, sizeof(syndrome_data[0]), GFP_KERNEL);
979 	if (!syndrome_data)
980 		return false;
981 
982 	for (int i = 0; i < max_nr_components; i++) {
983 		sprintf(name, "component_id%d", i);
984 		debugfs_create_file(name, 0600, einj_debug_dir,
985 				    &syndrome_data[i].comp_id, &u128_fops);
986 		sprintf(name, "component_syndrome%d", i);
987 		debugfs_create_file(name, 0600, einj_debug_dir,
988 				    &syndrome_data[i].comp_synd, &u128_fops);
989 	}
990 
991 	return true;
992 }
993 
einj_probe(struct faux_device * fdev)994 static int __init einj_probe(struct faux_device *fdev)
995 {
996 	int rc;
997 	acpi_status status;
998 	struct apei_exec_context ctx;
999 
1000 	status = acpi_get_table(ACPI_SIG_EINJ, 0,
1001 				(struct acpi_table_header **)&einj_tab);
1002 	if (status == AE_NOT_FOUND) {
1003 		pr_debug("EINJ table not found.\n");
1004 		return -ENODEV;
1005 	} else if (ACPI_FAILURE(status)) {
1006 		pr_err("Failed to get EINJ table: %s\n",
1007 				acpi_format_exception(status));
1008 		return -EINVAL;
1009 	}
1010 
1011 	rc = einj_check_table(einj_tab);
1012 	if (rc) {
1013 		pr_warn(FW_BUG "Invalid EINJ table.\n");
1014 		goto err_put_table;
1015 	}
1016 
1017 	rc = einj_get_available_error_types(&available_error_type, &available_error_type_v2);
1018 	if (rc)
1019 		goto err_put_table;
1020 
1021 	rc = -ENOMEM;
1022 	einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
1023 
1024 	debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
1025 			    NULL, &available_error_type_fops);
1026 	debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
1027 				   NULL, &error_type_fops);
1028 	debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
1029 				   NULL, &error_inject_fops);
1030 
1031 	apei_resources_init(&einj_resources);
1032 	einj_exec_ctx_init(&ctx);
1033 	rc = apei_exec_collect_resources(&ctx, &einj_resources);
1034 	if (rc) {
1035 		pr_err("Error collecting EINJ resources.\n");
1036 		goto err_fini;
1037 	}
1038 
1039 	rc = apei_resources_request(&einj_resources, "APEI EINJ");
1040 	if (rc) {
1041 		pr_err("Error requesting memory/port resources.\n");
1042 		goto err_fini;
1043 	}
1044 
1045 	rc = apei_exec_pre_map_gars(&ctx);
1046 	if (rc) {
1047 		pr_err("Error pre-mapping GARs.\n");
1048 		goto err_release;
1049 	}
1050 
1051 	einj_param = einj_get_parameter_address();
1052 	if ((param_extension || acpi5) && einj_param) {
1053 		debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
1054 				   &error_flags);
1055 		debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
1056 				   &error_param1);
1057 		debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
1058 				   &error_param2);
1059 		debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
1060 				   &error_param3);
1061 		debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
1062 				   &error_param4);
1063 		debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
1064 				   einj_debug_dir, &notrigger);
1065 		if (available_error_type & ACPI65_EINJV2_SUPP)
1066 			einj_v2_enabled = setup_einjv2_component_files();
1067 	}
1068 
1069 	if (vendor_dev[0]) {
1070 		vendor_blob.data = vendor_dev;
1071 		vendor_blob.size = strlen(vendor_dev);
1072 		debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
1073 				    &vendor_blob);
1074 		debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
1075 				   einj_debug_dir, &vendor_flags);
1076 	}
1077 
1078 	if (vendor_errors.size)
1079 		debugfs_create_blob("oem_error", 0600, einj_debug_dir,
1080 				    &vendor_errors);
1081 
1082 	pr_info("Error INJection is initialized.\n");
1083 
1084 	return 0;
1085 
1086 err_release:
1087 	apei_resources_release(&einj_resources);
1088 err_fini:
1089 	apei_resources_fini(&einj_resources);
1090 	debugfs_remove_recursive(einj_debug_dir);
1091 err_put_table:
1092 	acpi_put_table((struct acpi_table_header *)einj_tab);
1093 
1094 	return rc;
1095 }
1096 
einj_remove(struct faux_device * fdev)1097 static void einj_remove(struct faux_device *fdev)
1098 {
1099 	struct apei_exec_context ctx;
1100 
1101 	if (einj_param) {
1102 		acpi_size size = (acpi5) ?
1103 			v5param_size :
1104 			sizeof(struct einj_parameter);
1105 
1106 		acpi_os_unmap_iomem(einj_param, size);
1107 		if (vendor_errors.size)
1108 			acpi_os_unmap_memory(vendor_errors.data, vendor_errors.size);
1109 	}
1110 	einj_exec_ctx_init(&ctx);
1111 	apei_exec_post_unmap_gars(&ctx);
1112 	apei_resources_release(&einj_resources);
1113 	apei_resources_fini(&einj_resources);
1114 	debugfs_remove_recursive(einj_debug_dir);
1115 	kfree(syndrome_data);
1116 	acpi_put_table((struct acpi_table_header *)einj_tab);
1117 }
1118 
1119 static struct faux_device *einj_dev;
1120 static struct faux_device_ops einj_device_ops = {
1121 	.probe = einj_probe,
1122 	.remove = einj_remove,
1123 };
1124 
einj_init(void)1125 static int __init einj_init(void)
1126 {
1127 	if (acpi_disabled) {
1128 		pr_debug("ACPI disabled.\n");
1129 		return -ENODEV;
1130 	}
1131 
1132 	einj_dev = faux_device_create("acpi-einj", NULL, &einj_device_ops);
1133 
1134 	if (einj_dev)
1135 		einj_initialized = true;
1136 
1137 	return 0;
1138 }
1139 
einj_exit(void)1140 static void __exit einj_exit(void)
1141 {
1142 	faux_device_destroy(einj_dev);
1143 }
1144 
1145 module_init(einj_init);
1146 module_exit(einj_exit);
1147 
1148 MODULE_AUTHOR("Huang Ying");
1149 MODULE_DESCRIPTION("APEI Error INJection support");
1150 MODULE_LICENSE("GPL");
1151