Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/drivers/acpi/apei/einj.c
15111 views
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 <[email protected]>
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>
35
#include <acpi/acpi.h>
36
37
#include "apei-internal.h"
38
39
#define EINJ_PFX "EINJ: "
40
41
#define SPIN_UNIT 100 /* 100ns */
42
/* Firmware should respond within 1 milliseconds */
43
#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
44
45
/*
46
* Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
47
* EINJ table through an unpublished extension. Use with caution as
48
* most will ignore the parameter and make their own choice of address
49
* for error injection.
50
*/
51
struct einj_parameter {
52
u64 type;
53
u64 reserved1;
54
u64 reserved2;
55
u64 param1;
56
u64 param2;
57
};
58
59
#define EINJ_OP_BUSY 0x1
60
#define EINJ_STATUS_SUCCESS 0x0
61
#define EINJ_STATUS_FAIL 0x1
62
#define EINJ_STATUS_INVAL 0x2
63
64
#define EINJ_TAB_ENTRY(tab) \
65
((struct acpi_whea_header *)((char *)(tab) + \
66
sizeof(struct acpi_table_einj)))
67
68
static struct acpi_table_einj *einj_tab;
69
70
static struct apei_resources einj_resources;
71
72
static struct apei_exec_ins_type einj_ins_type[] = {
73
[ACPI_EINJ_READ_REGISTER] = {
74
.flags = APEI_EXEC_INS_ACCESS_REGISTER,
75
.run = apei_exec_read_register,
76
},
77
[ACPI_EINJ_READ_REGISTER_VALUE] = {
78
.flags = APEI_EXEC_INS_ACCESS_REGISTER,
79
.run = apei_exec_read_register_value,
80
},
81
[ACPI_EINJ_WRITE_REGISTER] = {
82
.flags = APEI_EXEC_INS_ACCESS_REGISTER,
83
.run = apei_exec_write_register,
84
},
85
[ACPI_EINJ_WRITE_REGISTER_VALUE] = {
86
.flags = APEI_EXEC_INS_ACCESS_REGISTER,
87
.run = apei_exec_write_register_value,
88
},
89
[ACPI_EINJ_NOOP] = {
90
.flags = 0,
91
.run = apei_exec_noop,
92
},
93
};
94
95
/*
96
* Prevent EINJ interpreter to run simultaneously, because the
97
* corresponding firmware implementation may not work properly when
98
* invoked simultaneously.
99
*/
100
static DEFINE_MUTEX(einj_mutex);
101
102
static struct einj_parameter *einj_param;
103
104
#ifndef writeq
105
static inline void writeq(__u64 val, volatile void __iomem *addr)
106
{
107
writel(val, addr);
108
writel(val >> 32, addr+4);
109
}
110
#endif
111
112
static void einj_exec_ctx_init(struct apei_exec_context *ctx)
113
{
114
apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
115
EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
116
}
117
118
static int __einj_get_available_error_type(u32 *type)
119
{
120
struct apei_exec_context ctx;
121
int rc;
122
123
einj_exec_ctx_init(&ctx);
124
rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
125
if (rc)
126
return rc;
127
*type = apei_exec_ctx_get_output(&ctx);
128
129
return 0;
130
}
131
132
/* Get error injection capabilities of the platform */
133
static int einj_get_available_error_type(u32 *type)
134
{
135
int rc;
136
137
mutex_lock(&einj_mutex);
138
rc = __einj_get_available_error_type(type);
139
mutex_unlock(&einj_mutex);
140
141
return rc;
142
}
143
144
static int einj_timedout(u64 *t)
145
{
146
if ((s64)*t < SPIN_UNIT) {
147
pr_warning(FW_WARN EINJ_PFX
148
"Firmware does not respond in time\n");
149
return 1;
150
}
151
*t -= SPIN_UNIT;
152
ndelay(SPIN_UNIT);
153
touch_nmi_watchdog();
154
return 0;
155
}
156
157
static u64 einj_get_parameter_address(void)
158
{
159
int i;
160
u64 paddr = 0;
161
struct acpi_whea_header *entry;
162
163
entry = EINJ_TAB_ENTRY(einj_tab);
164
for (i = 0; i < einj_tab->entries; i++) {
165
if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
166
entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
167
entry->register_region.space_id ==
168
ACPI_ADR_SPACE_SYSTEM_MEMORY)
169
memcpy(&paddr, &entry->register_region.address,
170
sizeof(paddr));
171
entry++;
172
}
173
174
return paddr;
175
}
176
177
/* do sanity check to trigger table */
178
static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
179
{
180
if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
181
return -EINVAL;
182
if (trigger_tab->table_size > PAGE_SIZE ||
183
trigger_tab->table_size <= trigger_tab->header_size)
184
return -EINVAL;
185
if (trigger_tab->entry_count !=
186
(trigger_tab->table_size - trigger_tab->header_size) /
187
sizeof(struct acpi_einj_entry))
188
return -EINVAL;
189
190
return 0;
191
}
192
193
/* Execute instructions in trigger error action table */
194
static int __einj_error_trigger(u64 trigger_paddr)
195
{
196
struct acpi_einj_trigger *trigger_tab = NULL;
197
struct apei_exec_context trigger_ctx;
198
struct apei_resources trigger_resources;
199
struct acpi_whea_header *trigger_entry;
200
struct resource *r;
201
u32 table_size;
202
int rc = -EIO;
203
204
r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
205
"APEI EINJ Trigger Table");
206
if (!r) {
207
pr_err(EINJ_PFX
208
"Can not request iomem region <%016llx-%016llx> for Trigger table.\n",
209
(unsigned long long)trigger_paddr,
210
(unsigned long long)trigger_paddr+sizeof(*trigger_tab));
211
goto out;
212
}
213
trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
214
if (!trigger_tab) {
215
pr_err(EINJ_PFX "Failed to map trigger table!\n");
216
goto out_rel_header;
217
}
218
rc = einj_check_trigger_header(trigger_tab);
219
if (rc) {
220
pr_warning(FW_BUG EINJ_PFX
221
"The trigger error action table is invalid\n");
222
goto out_rel_header;
223
}
224
rc = -EIO;
225
table_size = trigger_tab->table_size;
226
r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
227
table_size - sizeof(*trigger_tab),
228
"APEI EINJ Trigger Table");
229
if (!r) {
230
pr_err(EINJ_PFX
231
"Can not request iomem region <%016llx-%016llx> for Trigger Table Entry.\n",
232
(unsigned long long)trigger_paddr+sizeof(*trigger_tab),
233
(unsigned long long)trigger_paddr + table_size);
234
goto out_rel_header;
235
}
236
iounmap(trigger_tab);
237
trigger_tab = ioremap_cache(trigger_paddr, table_size);
238
if (!trigger_tab) {
239
pr_err(EINJ_PFX "Failed to map trigger table!\n");
240
goto out_rel_entry;
241
}
242
trigger_entry = (struct acpi_whea_header *)
243
((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
244
apei_resources_init(&trigger_resources);
245
apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
246
ARRAY_SIZE(einj_ins_type),
247
trigger_entry, trigger_tab->entry_count);
248
rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
249
if (rc)
250
goto out_fini;
251
rc = apei_resources_sub(&trigger_resources, &einj_resources);
252
if (rc)
253
goto out_fini;
254
rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
255
if (rc)
256
goto out_fini;
257
rc = apei_exec_pre_map_gars(&trigger_ctx);
258
if (rc)
259
goto out_release;
260
261
rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
262
263
apei_exec_post_unmap_gars(&trigger_ctx);
264
out_release:
265
apei_resources_release(&trigger_resources);
266
out_fini:
267
apei_resources_fini(&trigger_resources);
268
out_rel_entry:
269
release_mem_region(trigger_paddr + sizeof(*trigger_tab),
270
table_size - sizeof(*trigger_tab));
271
out_rel_header:
272
release_mem_region(trigger_paddr, sizeof(*trigger_tab));
273
out:
274
if (trigger_tab)
275
iounmap(trigger_tab);
276
277
return rc;
278
}
279
280
static int __einj_error_inject(u32 type, u64 param1, u64 param2)
281
{
282
struct apei_exec_context ctx;
283
u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
284
int rc;
285
286
einj_exec_ctx_init(&ctx);
287
288
rc = apei_exec_run(&ctx, ACPI_EINJ_BEGIN_OPERATION);
289
if (rc)
290
return rc;
291
apei_exec_ctx_set_input(&ctx, type);
292
rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
293
if (rc)
294
return rc;
295
if (einj_param) {
296
writeq(param1, &einj_param->param1);
297
writeq(param2, &einj_param->param2);
298
}
299
rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
300
if (rc)
301
return rc;
302
for (;;) {
303
rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
304
if (rc)
305
return rc;
306
val = apei_exec_ctx_get_output(&ctx);
307
if (!(val & EINJ_OP_BUSY))
308
break;
309
if (einj_timedout(&timeout))
310
return -EIO;
311
}
312
rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
313
if (rc)
314
return rc;
315
val = apei_exec_ctx_get_output(&ctx);
316
if (val != EINJ_STATUS_SUCCESS)
317
return -EBUSY;
318
319
rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
320
if (rc)
321
return rc;
322
trigger_paddr = apei_exec_ctx_get_output(&ctx);
323
rc = __einj_error_trigger(trigger_paddr);
324
if (rc)
325
return rc;
326
rc = apei_exec_run(&ctx, ACPI_EINJ_END_OPERATION);
327
328
return rc;
329
}
330
331
/* Inject the specified hardware error */
332
static int einj_error_inject(u32 type, u64 param1, u64 param2)
333
{
334
int rc;
335
336
mutex_lock(&einj_mutex);
337
rc = __einj_error_inject(type, param1, param2);
338
mutex_unlock(&einj_mutex);
339
340
return rc;
341
}
342
343
static u32 error_type;
344
static u64 error_param1;
345
static u64 error_param2;
346
static struct dentry *einj_debug_dir;
347
348
static int available_error_type_show(struct seq_file *m, void *v)
349
{
350
int rc;
351
u32 available_error_type = 0;
352
353
rc = einj_get_available_error_type(&available_error_type);
354
if (rc)
355
return rc;
356
if (available_error_type & 0x0001)
357
seq_printf(m, "0x00000001\tProcessor Correctable\n");
358
if (available_error_type & 0x0002)
359
seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
360
if (available_error_type & 0x0004)
361
seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
362
if (available_error_type & 0x0008)
363
seq_printf(m, "0x00000008\tMemory Correctable\n");
364
if (available_error_type & 0x0010)
365
seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
366
if (available_error_type & 0x0020)
367
seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
368
if (available_error_type & 0x0040)
369
seq_printf(m, "0x00000040\tPCI Express Correctable\n");
370
if (available_error_type & 0x0080)
371
seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
372
if (available_error_type & 0x0100)
373
seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
374
if (available_error_type & 0x0200)
375
seq_printf(m, "0x00000200\tPlatform Correctable\n");
376
if (available_error_type & 0x0400)
377
seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
378
if (available_error_type & 0x0800)
379
seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
380
381
return 0;
382
}
383
384
static int available_error_type_open(struct inode *inode, struct file *file)
385
{
386
return single_open(file, available_error_type_show, NULL);
387
}
388
389
static const struct file_operations available_error_type_fops = {
390
.open = available_error_type_open,
391
.read = seq_read,
392
.llseek = seq_lseek,
393
.release = single_release,
394
};
395
396
static int error_type_get(void *data, u64 *val)
397
{
398
*val = error_type;
399
400
return 0;
401
}
402
403
static int error_type_set(void *data, u64 val)
404
{
405
int rc;
406
u32 available_error_type = 0;
407
408
/* Only one error type can be specified */
409
if (val & (val - 1))
410
return -EINVAL;
411
rc = einj_get_available_error_type(&available_error_type);
412
if (rc)
413
return rc;
414
if (!(val & available_error_type))
415
return -EINVAL;
416
error_type = val;
417
418
return 0;
419
}
420
421
DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
422
error_type_set, "0x%llx\n");
423
424
static int error_inject_set(void *data, u64 val)
425
{
426
if (!error_type)
427
return -EINVAL;
428
429
return einj_error_inject(error_type, error_param1, error_param2);
430
}
431
432
DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
433
error_inject_set, "%llu\n");
434
435
static int einj_check_table(struct acpi_table_einj *einj_tab)
436
{
437
if ((einj_tab->header_length !=
438
(sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
439
&& (einj_tab->header_length != sizeof(struct acpi_table_einj)))
440
return -EINVAL;
441
if (einj_tab->header.length < sizeof(struct acpi_table_einj))
442
return -EINVAL;
443
if (einj_tab->entries !=
444
(einj_tab->header.length - sizeof(struct acpi_table_einj)) /
445
sizeof(struct acpi_einj_entry))
446
return -EINVAL;
447
448
return 0;
449
}
450
451
static int __init einj_init(void)
452
{
453
int rc;
454
u64 param_paddr;
455
acpi_status status;
456
struct dentry *fentry;
457
struct apei_exec_context ctx;
458
459
if (acpi_disabled)
460
return -ENODEV;
461
462
status = acpi_get_table(ACPI_SIG_EINJ, 0,
463
(struct acpi_table_header **)&einj_tab);
464
if (status == AE_NOT_FOUND) {
465
pr_info(EINJ_PFX "Table is not found!\n");
466
return -ENODEV;
467
} else if (ACPI_FAILURE(status)) {
468
const char *msg = acpi_format_exception(status);
469
pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
470
return -EINVAL;
471
}
472
473
rc = einj_check_table(einj_tab);
474
if (rc) {
475
pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
476
return -EINVAL;
477
}
478
479
rc = -ENOMEM;
480
einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
481
if (!einj_debug_dir)
482
goto err_cleanup;
483
fentry = debugfs_create_file("available_error_type", S_IRUSR,
484
einj_debug_dir, NULL,
485
&available_error_type_fops);
486
if (!fentry)
487
goto err_cleanup;
488
fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
489
einj_debug_dir, NULL, &error_type_fops);
490
if (!fentry)
491
goto err_cleanup;
492
fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
493
einj_debug_dir, &error_param1);
494
if (!fentry)
495
goto err_cleanup;
496
fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
497
einj_debug_dir, &error_param2);
498
if (!fentry)
499
goto err_cleanup;
500
fentry = debugfs_create_file("error_inject", S_IWUSR,
501
einj_debug_dir, NULL, &error_inject_fops);
502
if (!fentry)
503
goto err_cleanup;
504
505
apei_resources_init(&einj_resources);
506
einj_exec_ctx_init(&ctx);
507
rc = apei_exec_collect_resources(&ctx, &einj_resources);
508
if (rc)
509
goto err_fini;
510
rc = apei_resources_request(&einj_resources, "APEI EINJ");
511
if (rc)
512
goto err_fini;
513
rc = apei_exec_pre_map_gars(&ctx);
514
if (rc)
515
goto err_release;
516
param_paddr = einj_get_parameter_address();
517
if (param_paddr) {
518
einj_param = ioremap(param_paddr, sizeof(*einj_param));
519
rc = -ENOMEM;
520
if (!einj_param)
521
goto err_unmap;
522
}
523
524
pr_info(EINJ_PFX "Error INJection is initialized.\n");
525
526
return 0;
527
528
err_unmap:
529
apei_exec_post_unmap_gars(&ctx);
530
err_release:
531
apei_resources_release(&einj_resources);
532
err_fini:
533
apei_resources_fini(&einj_resources);
534
err_cleanup:
535
debugfs_remove_recursive(einj_debug_dir);
536
537
return rc;
538
}
539
540
static void __exit einj_exit(void)
541
{
542
struct apei_exec_context ctx;
543
544
if (einj_param)
545
iounmap(einj_param);
546
einj_exec_ctx_init(&ctx);
547
apei_exec_post_unmap_gars(&ctx);
548
apei_resources_release(&einj_resources);
549
apei_resources_fini(&einj_resources);
550
debugfs_remove_recursive(einj_debug_dir);
551
}
552
553
module_init(einj_init);
554
module_exit(einj_exit);
555
556
MODULE_AUTHOR("Huang Ying");
557
MODULE_DESCRIPTION("APEI Error INJection support");
558
MODULE_LICENSE("GPL");
559
560