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