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