]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/apei/einj.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / apei / einj.c
CommitLineData
e4021345
HY
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 *
6e320ec1 10 * Copyright 2009-2010 Intel Corp.
e4021345
HY
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.
e4021345
HY
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>
c5a13032 31#include <linux/mm.h>
d3ab3edc 32#include <asm/unaligned.h>
e4021345
HY
33
34#include "apei-internal.h"
35
b2f740ba
BP
36#undef pr_fmt
37#define pr_fmt(fmt) "EINJ: " fmt
e4021345
HY
38
39#define SPIN_UNIT 100 /* 100ns */
e8a8b252 40/* Firmware should respond within 1 milliseconds */
e4021345 41#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
c5a13032
CG
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)
e4021345 46
c130bd6f
TL
47/*
48 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
49 */
50static int acpi5;
51
52struct 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};
61enum {
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 */
70struct 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
ee49089d
CG
79static u32 notrigger;
80
c130bd6f
TL
81static u32 vendor_flags;
82static struct debugfs_blob_wrapper vendor_blob;
83static char vendor_dev[64];
84
6e320ec1
HY
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
c3e6088e
HY
89 * for error injection. This extension is used only if
90 * param_extension module parameter is specified.
6e320ec1
HY
91 */
92struct einj_parameter {
93 u64 type;
94 u64 reserved1;
95 u64 reserved2;
96 u64 param1;
97 u64 param2;
98};
99
e4021345
HY
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
c3e6088e
HY
109static bool param_extension;
110module_param(param_extension, bool, 0);
111
e4021345
HY
112static struct acpi_table_einj *einj_tab;
113
114static struct apei_resources einj_resources;
115
116static 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 */
144static DEFINE_MUTEX(einj_mutex);
145
c130bd6f
TL
146static void *einj_param;
147
e4021345
HY
148static 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
154static 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 */
169static 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
180static int einj_timedout(u64 *t)
181{
182 if ((s64)*t < SPIN_UNIT) {
b2f740ba 183 pr_warning(FW_WARN "Firmware does not respond in time\n");
e4021345
HY
184 return 1;
185 }
186 *t -= SPIN_UNIT;
187 ndelay(SPIN_UNIT);
188 touch_nmi_watchdog();
189 return 0;
190}
191
c130bd6f
TL
192static void check_vendor_extension(u64 paddr,
193 struct set_error_type_with_address *v5param)
194{
459413db 195 int offset = v5param->vendor_extension;
c130bd6f
TL
196 struct vendor_error_type_extension *v;
197 u32 sbdf;
198
199 if (!offset)
200 return;
a238317c 201 v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
c130bd6f
TL
202 if (!v)
203 return;
459413db 204 sbdf = v->pcie_sbdf;
c130bd6f
TL
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,
459413db 208 v->vendor_id, v->device_id, v->rev_id);
a238317c 209 acpi_os_unmap_iomem(v, sizeof(*v));
c130bd6f
TL
210}
211
212static void *einj_get_parameter_address(void)
6e320ec1
HY
213{
214 int i;
d3ab3edc 215 u64 pa_v4 = 0, pa_v5 = 0;
6e320ec1
HY
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)
d3ab3edc 224 pa_v4 = get_unaligned(&entry->register_region.address);
c130bd6f
TL
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)
d3ab3edc 229 pa_v5 = get_unaligned(&entry->register_region.address);
6e320ec1
HY
230 entry++;
231 }
d3ab3edc 232 if (pa_v5) {
c130bd6f
TL
233 struct set_error_type_with_address *v5param;
234
a238317c 235 v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
c130bd6f
TL
236 if (v5param) {
237 acpi5 = 1;
d3ab3edc 238 check_vendor_extension(pa_v5, v5param);
c130bd6f
TL
239 return v5param;
240 }
241 }
d3ab3edc 242 if (param_extension && pa_v4) {
c130bd6f
TL
243 struct einj_parameter *v4param;
244
a238317c 245 v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
c130bd6f 246 if (!v4param)
29924b9f 247 return NULL;
459413db 248 if (v4param->reserved1 || v4param->reserved2) {
a238317c 249 acpi_os_unmap_iomem(v4param, sizeof(*v4param));
29924b9f 250 return NULL;
c130bd6f
TL
251 }
252 return v4param;
253 }
6e320ec1 254
29924b9f 255 return NULL;
6e320ec1
HY
256}
257
e4021345
HY
258/* do sanity check to trigger table */
259static 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 ||
4c40aed8 264 trigger_tab->table_size < trigger_tab->header_size)
e4021345
HY
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
b4e008dc
XH
274static 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}
e4021345 294/* Execute instructions in trigger error action table */
fdea163d
HY
295static int __einj_error_trigger(u64 trigger_paddr, u32 type,
296 u64 param1, u64 param2)
e4021345
HY
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;
b4e008dc 305 struct acpi_generic_address *trigger_param_region = NULL;
e4021345
HY
306
307 r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
308 "APEI EINJ Trigger Table");
309 if (!r) {
b2f740ba 310 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
e4021345 311 (unsigned long long)trigger_paddr,
46b91e37
BH
312 (unsigned long long)trigger_paddr +
313 sizeof(*trigger_tab) - 1);
e4021345
HY
314 goto out;
315 }
316 trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
317 if (!trigger_tab) {
b2f740ba 318 pr_err("Failed to map trigger table!\n");
e4021345
HY
319 goto out_rel_header;
320 }
321 rc = einj_check_trigger_header(trigger_tab);
322 if (rc) {
b2f740ba 323 pr_warning(FW_BUG "Invalid trigger error action table.\n");
e4021345
HY
324 goto out_rel_header;
325 }
4c40aed8
NS
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
e4021345
HY
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) {
b2f740ba 337 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
46b91e37
BH
338 (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
339 (unsigned long long)trigger_paddr + table_size - 1);
e4021345
HY
340 goto out_rel_header;
341 }
342 iounmap(trigger_tab);
343 trigger_tab = ioremap_cache(trigger_paddr, table_size);
344 if (!trigger_tab) {
b2f740ba 345 pr_err("Failed to map trigger table!\n");
e4021345
HY
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;
fdea163d
HY
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 */
c5a13032 366 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
fdea163d
HY
367 struct apei_resources addr_resources;
368 apei_resources_init(&addr_resources);
b4e008dc
XH
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 }
fdea163d
HY
380 apei_resources_fini(&addr_resources);
381 if (rc)
382 goto out_fini;
383 }
e4021345
HY
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);
394out_release:
395 apei_resources_release(&trigger_resources);
396out_fini:
397 apei_resources_fini(&trigger_resources);
398out_rel_entry:
399 release_mem_region(trigger_paddr + sizeof(*trigger_tab),
400 table_size - sizeof(*trigger_tab));
401out_rel_header:
402 release_mem_region(trigger_paddr, sizeof(*trigger_tab));
403out:
404 if (trigger_tab)
405 iounmap(trigger_tab);
406
407 return rc;
408}
409
3482fb5e
TL
410static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
411 u64 param3, u64 param4)
e4021345
HY
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
392913de 419 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
e4021345
HY
420 if (rc)
421 return rc;
422 apei_exec_ctx_set_input(&ctx, type);
c130bd6f
TL
423 if (acpi5) {
424 struct set_error_type_with_address *v5param = einj_param;
425
459413db 426 v5param->type = type;
c5a13032 427 if (type & ACPI5_VENDOR_BIT) {
c130bd6f
TL
428 switch (vendor_flags) {
429 case SETWA_FLAGS_APICID:
459413db 430 v5param->apicid = param1;
c130bd6f
TL
431 break;
432 case SETWA_FLAGS_MEM:
459413db
TL
433 v5param->memory_address = param1;
434 v5param->memory_address_range = param2;
c130bd6f
TL
435 break;
436 case SETWA_FLAGS_PCIE_SBDF:
459413db 437 v5param->pcie_sbdf = param1;
c130bd6f
TL
438 break;
439 }
459413db 440 v5param->flags = vendor_flags;
3482fb5e
TL
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;
c130bd6f
TL
447 } else {
448 switch (type) {
449 case ACPI_EINJ_PROCESSOR_CORRECTABLE:
450 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
451 case ACPI_EINJ_PROCESSOR_FATAL:
459413db
TL
452 v5param->apicid = param1;
453 v5param->flags = SETWA_FLAGS_APICID;
c130bd6f
TL
454 break;
455 case ACPI_EINJ_MEMORY_CORRECTABLE:
456 case ACPI_EINJ_MEMORY_UNCORRECTABLE:
457 case ACPI_EINJ_MEMORY_FATAL:
459413db
TL
458 v5param->memory_address = param1;
459 v5param->memory_address_range = param2;
460 v5param->flags = SETWA_FLAGS_MEM;
c130bd6f
TL
461 break;
462 case ACPI_EINJ_PCIX_CORRECTABLE:
463 case ACPI_EINJ_PCIX_UNCORRECTABLE:
464 case ACPI_EINJ_PCIX_FATAL:
459413db
TL
465 v5param->pcie_sbdf = param1;
466 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
c130bd6f
TL
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;
459413db
TL
476 v4param->param1 = param1;
477 v4param->param2 = param2;
c130bd6f 478 }
6e320ec1 479 }
e4021345
HY
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);
ee49089d
CG
504 if (notrigger == 0) {
505 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
506 if (rc)
507 return rc;
508 }
392913de 509 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
e4021345
HY
510
511 return rc;
512}
513
514/* Inject the specified hardware error */
3482fb5e
TL
515static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
516 u64 param3, u64 param4)
e4021345
HY
517{
518 int rc;
4650bac1 519 u64 base_addr, size;
e4021345 520
3482fb5e
TL
521 /* If user manually set "flags", make sure it is legal */
522 if (flags && (flags &
523 ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
524 return -EINVAL;
525
c5a13032
CG
526 /*
527 * We need extra sanity checks for memory errors.
528 * Other types leap directly to injection.
529 */
530
531 /* ensure param1/param2 existed */
532 if (!(param_extension || acpi5))
533 goto inject;
534
535 /* ensure injection is memory related */
536 if (type & ACPI5_VENDOR_BIT) {
537 if (vendor_flags != SETWA_FLAGS_MEM)
538 goto inject;
3482fb5e 539 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
c5a13032
CG
540 goto inject;
541
542 /*
543 * Disallow crazy address masks that give BIOS leeway to pick
544 * injection address almost anywhere. Insist on page or
4650bac1
TK
545 * better granularity and that target address is normal RAM or
546 * NVDIMM.
c5a13032 547 */
4650bac1
TK
548 base_addr = param1 & param2;
549 size = ~param2 + 1;
550
551 if (((param2 & PAGE_MASK) != PAGE_MASK) ||
552 ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
553 != REGION_INTERSECTS) &&
554 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
555 != REGION_INTERSECTS)))
c5a13032
CG
556 return -EINVAL;
557
558inject:
e4021345 559 mutex_lock(&einj_mutex);
3482fb5e 560 rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
e4021345
HY
561 mutex_unlock(&einj_mutex);
562
563 return rc;
564}
565
566static u32 error_type;
3482fb5e 567static u32 error_flags;
6e320ec1
HY
568static u64 error_param1;
569static u64 error_param2;
3482fb5e
TL
570static u64 error_param3;
571static u64 error_param4;
e4021345
HY
572static struct dentry *einj_debug_dir;
573
574static int available_error_type_show(struct seq_file *m, void *v)
575{
576 int rc;
577 u32 available_error_type = 0;
578
579 rc = einj_get_available_error_type(&available_error_type);
580 if (rc)
581 return rc;
582 if (available_error_type & 0x0001)
583 seq_printf(m, "0x00000001\tProcessor Correctable\n");
584 if (available_error_type & 0x0002)
585 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
586 if (available_error_type & 0x0004)
587 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
588 if (available_error_type & 0x0008)
589 seq_printf(m, "0x00000008\tMemory Correctable\n");
590 if (available_error_type & 0x0010)
591 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
592 if (available_error_type & 0x0020)
593 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
594 if (available_error_type & 0x0040)
595 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
596 if (available_error_type & 0x0080)
597 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
598 if (available_error_type & 0x0100)
599 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
600 if (available_error_type & 0x0200)
601 seq_printf(m, "0x00000200\tPlatform Correctable\n");
602 if (available_error_type & 0x0400)
603 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
604 if (available_error_type & 0x0800)
605 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
606
607 return 0;
608}
609
610static int available_error_type_open(struct inode *inode, struct file *file)
611{
612 return single_open(file, available_error_type_show, NULL);
613}
614
615static const struct file_operations available_error_type_fops = {
616 .open = available_error_type_open,
617 .read = seq_read,
618 .llseek = seq_lseek,
619 .release = single_release,
620};
621
622static int error_type_get(void *data, u64 *val)
623{
624 *val = error_type;
625
626 return 0;
627}
628
629static int error_type_set(void *data, u64 val)
630{
631 int rc;
632 u32 available_error_type = 0;
c130bd6f
TL
633 u32 tval, vendor;
634
635 /*
636 * Vendor defined types have 0x80000000 bit set, and
637 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
638 */
c5a13032 639 vendor = val & ACPI5_VENDOR_BIT;
c130bd6f 640 tval = val & 0x7fffffff;
e4021345
HY
641
642 /* Only one error type can be specified */
c130bd6f 643 if (tval & (tval - 1))
e4021345 644 return -EINVAL;
c130bd6f
TL
645 if (!vendor) {
646 rc = einj_get_available_error_type(&available_error_type);
647 if (rc)
648 return rc;
649 if (!(val & available_error_type))
650 return -EINVAL;
651 }
e4021345
HY
652 error_type = val;
653
654 return 0;
655}
656
657DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
658 error_type_set, "0x%llx\n");
659
660static int error_inject_set(void *data, u64 val)
661{
662 if (!error_type)
663 return -EINVAL;
664
3482fb5e
TL
665 return einj_error_inject(error_type, error_flags, error_param1, error_param2,
666 error_param3, error_param4);
e4021345
HY
667}
668
669DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
670 error_inject_set, "%llu\n");
671
672static int einj_check_table(struct acpi_table_einj *einj_tab)
673{
3a78f965
HY
674 if ((einj_tab->header_length !=
675 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
676 && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
e4021345
HY
677 return -EINVAL;
678 if (einj_tab->header.length < sizeof(struct acpi_table_einj))
679 return -EINVAL;
680 if (einj_tab->entries !=
681 (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
682 sizeof(struct acpi_einj_entry))
683 return -EINVAL;
684
685 return 0;
686}
687
688static int __init einj_init(void)
689{
690 int rc;
691 acpi_status status;
692 struct dentry *fentry;
693 struct apei_exec_context ctx;
694
dba64830
BP
695 if (acpi_disabled) {
696 pr_warn("ACPI disabled.\n");
e4021345 697 return -ENODEV;
dba64830 698 }
e4021345
HY
699
700 status = acpi_get_table(ACPI_SIG_EINJ, 0,
701 (struct acpi_table_header **)&einj_tab);
dba64830
BP
702 if (status == AE_NOT_FOUND) {
703 pr_warn("EINJ table not found.\n");
e4021345 704 return -ENODEV;
dba64830 705 }
ad686154 706 else if (ACPI_FAILURE(status)) {
dba64830
BP
707 pr_err("Failed to get EINJ table: %s\n",
708 acpi_format_exception(status));
e4021345
HY
709 return -EINVAL;
710 }
711
712 rc = einj_check_table(einj_tab);
713 if (rc) {
d2226784 714 pr_warn(FW_BUG "Invalid EINJ table.\n");
e4021345
HY
715 return -EINVAL;
716 }
717
718 rc = -ENOMEM;
719 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
dba64830
BP
720 if (!einj_debug_dir) {
721 pr_err("Error creating debugfs node.\n");
e4021345 722 goto err_cleanup;
dba64830
BP
723 }
724
e4021345
HY
725 fentry = debugfs_create_file("available_error_type", S_IRUSR,
726 einj_debug_dir, NULL,
727 &available_error_type_fops);
728 if (!fentry)
729 goto err_cleanup;
dba64830 730
e4021345
HY
731 fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
732 einj_debug_dir, NULL, &error_type_fops);
733 if (!fentry)
734 goto err_cleanup;
735 fentry = debugfs_create_file("error_inject", S_IWUSR,
736 einj_debug_dir, NULL, &error_inject_fops);
737 if (!fentry)
738 goto err_cleanup;
739
740 apei_resources_init(&einj_resources);
741 einj_exec_ctx_init(&ctx);
742 rc = apei_exec_collect_resources(&ctx, &einj_resources);
dba64830
BP
743 if (rc) {
744 pr_err("Error collecting EINJ resources.\n");
e4021345 745 goto err_fini;
dba64830
BP
746 }
747
e4021345 748 rc = apei_resources_request(&einj_resources, "APEI EINJ");
dba64830
BP
749 if (rc) {
750 pr_err("Error requesting memory/port resources.\n");
e4021345 751 goto err_fini;
dba64830
BP
752 }
753
e4021345 754 rc = apei_exec_pre_map_gars(&ctx);
dba64830
BP
755 if (rc) {
756 pr_err("Error pre-mapping GARs.\n");
e4021345 757 goto err_release;
dba64830 758 }
c130bd6f 759
b8edb641 760 rc = -ENOMEM;
c130bd6f
TL
761 einj_param = einj_get_parameter_address();
762 if ((param_extension || acpi5) && einj_param) {
3482fb5e
TL
763 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
764 einj_debug_dir, &error_flags);
765 if (!fentry)
766 goto err_unmap;
c130bd6f
TL
767 fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
768 einj_debug_dir, &error_param1);
769 if (!fentry)
770 goto err_unmap;
771 fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
772 einj_debug_dir, &error_param2);
773 if (!fentry)
774 goto err_unmap;
3482fb5e
TL
775 fentry = debugfs_create_x64("param3", S_IRUSR | S_IWUSR,
776 einj_debug_dir, &error_param3);
777 if (!fentry)
778 goto err_unmap;
779 fentry = debugfs_create_x64("param4", S_IRUSR | S_IWUSR,
780 einj_debug_dir, &error_param4);
781 if (!fentry)
782 goto err_unmap;
ee49089d
CG
783
784 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
785 einj_debug_dir, &notrigger);
786 if (!fentry)
787 goto err_unmap;
c130bd6f
TL
788 }
789
790 if (vendor_dev[0]) {
791 vendor_blob.data = vendor_dev;
792 vendor_blob.size = strlen(vendor_dev);
793 fentry = debugfs_create_blob("vendor", S_IRUSR,
794 einj_debug_dir, &vendor_blob);
795 if (!fentry)
796 goto err_unmap;
797 fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
798 einj_debug_dir, &vendor_flags);
799 if (!fentry)
800 goto err_unmap;
6e320ec1 801 }
e4021345 802
b2f740ba 803 pr_info("Error INJection is initialized.\n");
e4021345
HY
804
805 return 0;
806
6e320ec1 807err_unmap:
459413db
TL
808 if (einj_param) {
809 acpi_size size = (acpi5) ?
810 sizeof(struct set_error_type_with_address) :
811 sizeof(struct einj_parameter);
812
a238317c 813 acpi_os_unmap_iomem(einj_param, size);
dba64830 814 pr_err("Error creating param extension debugfs nodes.\n");
459413db 815 }
6e320ec1 816 apei_exec_post_unmap_gars(&ctx);
e4021345
HY
817err_release:
818 apei_resources_release(&einj_resources);
819err_fini:
820 apei_resources_fini(&einj_resources);
821err_cleanup:
dba64830 822 pr_err("Error creating primary debugfs nodes.\n");
e4021345
HY
823 debugfs_remove_recursive(einj_debug_dir);
824
825 return rc;
826}
827
828static void __exit einj_exit(void)
829{
830 struct apei_exec_context ctx;
831
459413db
TL
832 if (einj_param) {
833 acpi_size size = (acpi5) ?
834 sizeof(struct set_error_type_with_address) :
835 sizeof(struct einj_parameter);
836
a238317c 837 acpi_os_unmap_iomem(einj_param, size);
459413db 838 }
e4021345
HY
839 einj_exec_ctx_init(&ctx);
840 apei_exec_post_unmap_gars(&ctx);
841 apei_resources_release(&einj_resources);
842 apei_resources_fini(&einj_resources);
843 debugfs_remove_recursive(einj_debug_dir);
844}
845
846module_init(einj_init);
847module_exit(einj_exit);
848
849MODULE_AUTHOR("Huang Ying");
850MODULE_DESCRIPTION("APEI Error INJection support");
851MODULE_LICENSE("GPL");