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