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