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