]> Pileus Git - ~andy/linux/blob - drivers/acpi/apei/einj.c
Merge branch 'atomicio-remove' into release
[~andy/linux] / drivers / acpi / apei / einj.c
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 <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 */
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.  This extension is used only if
50  * param_extension module parameter is specified.
51  */
52 struct einj_parameter {
53         u64 type;
54         u64 reserved1;
55         u64 reserved2;
56         u64 param1;
57         u64 param2;
58 };
59
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
69 static bool param_extension;
70 module_param(param_extension, bool, 0);
71
72 static struct acpi_table_einj *einj_tab;
73
74 static struct apei_resources einj_resources;
75
76 static 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  */
104 static DEFINE_MUTEX(einj_mutex);
105
106 static struct einj_parameter *einj_param;
107
108 #ifndef writeq
109 static inline void writeq(__u64 val, volatile void __iomem *addr)
110 {
111         writel(val, addr);
112         writel(val >> 32, addr+4);
113 }
114 #endif
115
116 static 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
122 static 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 */
137 static 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
148 static 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
161 static 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
181 /* do sanity check to trigger table */
182 static 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 static struct acpi_generic_address *einj_get_trigger_parameter_region(
198         struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
199 {
200         int i;
201         struct acpi_whea_header *entry;
202
203         entry = (struct acpi_whea_header *)
204                 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
205         for (i = 0; i < trigger_tab->entry_count; i++) {
206                 if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
207                 entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
208                 entry->register_region.space_id ==
209                         ACPI_ADR_SPACE_SYSTEM_MEMORY &&
210                 (entry->register_region.address & param2) == (param1 & param2))
211                         return &entry->register_region;
212                 entry++;
213         }
214
215         return NULL;
216 }
217 /* Execute instructions in trigger error action table */
218 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
219                                 u64 param1, u64 param2)
220 {
221         struct acpi_einj_trigger *trigger_tab = NULL;
222         struct apei_exec_context trigger_ctx;
223         struct apei_resources trigger_resources;
224         struct acpi_whea_header *trigger_entry;
225         struct resource *r;
226         u32 table_size;
227         int rc = -EIO;
228         struct acpi_generic_address *trigger_param_region = NULL;
229
230         r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
231                                "APEI EINJ Trigger Table");
232         if (!r) {
233                 pr_err(EINJ_PFX
234         "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
235                        (unsigned long long)trigger_paddr,
236                        (unsigned long long)trigger_paddr +
237                             sizeof(*trigger_tab) - 1);
238                 goto out;
239         }
240         trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
241         if (!trigger_tab) {
242                 pr_err(EINJ_PFX "Failed to map trigger table!\n");
243                 goto out_rel_header;
244         }
245         rc = einj_check_trigger_header(trigger_tab);
246         if (rc) {
247                 pr_warning(FW_BUG EINJ_PFX
248                            "The trigger error action table is invalid\n");
249                 goto out_rel_header;
250         }
251         rc = -EIO;
252         table_size = trigger_tab->table_size;
253         r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
254                                table_size - sizeof(*trigger_tab),
255                                "APEI EINJ Trigger Table");
256         if (!r) {
257                 pr_err(EINJ_PFX
258 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
259                        (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
260                        (unsigned long long)trigger_paddr + table_size - 1);
261                 goto out_rel_header;
262         }
263         iounmap(trigger_tab);
264         trigger_tab = ioremap_cache(trigger_paddr, table_size);
265         if (!trigger_tab) {
266                 pr_err(EINJ_PFX "Failed to map trigger table!\n");
267                 goto out_rel_entry;
268         }
269         trigger_entry = (struct acpi_whea_header *)
270                 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
271         apei_resources_init(&trigger_resources);
272         apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
273                            ARRAY_SIZE(einj_ins_type),
274                            trigger_entry, trigger_tab->entry_count);
275         rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
276         if (rc)
277                 goto out_fini;
278         rc = apei_resources_sub(&trigger_resources, &einj_resources);
279         if (rc)
280                 goto out_fini;
281         /*
282          * Some firmware will access target address specified in
283          * param1 to trigger the error when injecting memory error.
284          * This will cause resource conflict with regular memory.  So
285          * remove it from trigger table resources.
286          */
287         if (param_extension && (type & 0x0038) && param2) {
288                 struct apei_resources addr_resources;
289                 apei_resources_init(&addr_resources);
290                 trigger_param_region = einj_get_trigger_parameter_region(
291                         trigger_tab, param1, param2);
292                 if (trigger_param_region) {
293                         rc = apei_resources_add(&addr_resources,
294                                 trigger_param_region->address,
295                                 trigger_param_region->bit_width/8, true);
296                         if (rc)
297                                 goto out_fini;
298                         rc = apei_resources_sub(&trigger_resources,
299                                         &addr_resources);
300                 }
301                 apei_resources_fini(&addr_resources);
302                 if (rc)
303                         goto out_fini;
304         }
305         rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
306         if (rc)
307                 goto out_fini;
308         rc = apei_exec_pre_map_gars(&trigger_ctx);
309         if (rc)
310                 goto out_release;
311
312         rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
313
314         apei_exec_post_unmap_gars(&trigger_ctx);
315 out_release:
316         apei_resources_release(&trigger_resources);
317 out_fini:
318         apei_resources_fini(&trigger_resources);
319 out_rel_entry:
320         release_mem_region(trigger_paddr + sizeof(*trigger_tab),
321                            table_size - sizeof(*trigger_tab));
322 out_rel_header:
323         release_mem_region(trigger_paddr, sizeof(*trigger_tab));
324 out:
325         if (trigger_tab)
326                 iounmap(trigger_tab);
327
328         return rc;
329 }
330
331 static int __einj_error_inject(u32 type, u64 param1, u64 param2)
332 {
333         struct apei_exec_context ctx;
334         u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
335         int rc;
336
337         einj_exec_ctx_init(&ctx);
338
339         rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
340         if (rc)
341                 return rc;
342         apei_exec_ctx_set_input(&ctx, type);
343         rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
344         if (rc)
345                 return rc;
346         if (einj_param) {
347                 writeq(param1, &einj_param->param1);
348                 writeq(param2, &einj_param->param2);
349         }
350         rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
351         if (rc)
352                 return rc;
353         for (;;) {
354                 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
355                 if (rc)
356                         return rc;
357                 val = apei_exec_ctx_get_output(&ctx);
358                 if (!(val & EINJ_OP_BUSY))
359                         break;
360                 if (einj_timedout(&timeout))
361                         return -EIO;
362         }
363         rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
364         if (rc)
365                 return rc;
366         val = apei_exec_ctx_get_output(&ctx);
367         if (val != EINJ_STATUS_SUCCESS)
368                 return -EBUSY;
369
370         rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
371         if (rc)
372                 return rc;
373         trigger_paddr = apei_exec_ctx_get_output(&ctx);
374         rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
375         if (rc)
376                 return rc;
377         rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
378
379         return rc;
380 }
381
382 /* Inject the specified hardware error */
383 static int einj_error_inject(u32 type, u64 param1, u64 param2)
384 {
385         int rc;
386
387         mutex_lock(&einj_mutex);
388         rc = __einj_error_inject(type, param1, param2);
389         mutex_unlock(&einj_mutex);
390
391         return rc;
392 }
393
394 static u32 error_type;
395 static u64 error_param1;
396 static u64 error_param2;
397 static struct dentry *einj_debug_dir;
398
399 static int available_error_type_show(struct seq_file *m, void *v)
400 {
401         int rc;
402         u32 available_error_type = 0;
403
404         rc = einj_get_available_error_type(&available_error_type);
405         if (rc)
406                 return rc;
407         if (available_error_type & 0x0001)
408                 seq_printf(m, "0x00000001\tProcessor Correctable\n");
409         if (available_error_type & 0x0002)
410                 seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
411         if (available_error_type & 0x0004)
412                 seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
413         if (available_error_type & 0x0008)
414                 seq_printf(m, "0x00000008\tMemory Correctable\n");
415         if (available_error_type & 0x0010)
416                 seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
417         if (available_error_type & 0x0020)
418                 seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
419         if (available_error_type & 0x0040)
420                 seq_printf(m, "0x00000040\tPCI Express Correctable\n");
421         if (available_error_type & 0x0080)
422                 seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
423         if (available_error_type & 0x0100)
424                 seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
425         if (available_error_type & 0x0200)
426                 seq_printf(m, "0x00000200\tPlatform Correctable\n");
427         if (available_error_type & 0x0400)
428                 seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
429         if (available_error_type & 0x0800)
430                 seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
431
432         return 0;
433 }
434
435 static int available_error_type_open(struct inode *inode, struct file *file)
436 {
437         return single_open(file, available_error_type_show, NULL);
438 }
439
440 static const struct file_operations available_error_type_fops = {
441         .open           = available_error_type_open,
442         .read           = seq_read,
443         .llseek         = seq_lseek,
444         .release        = single_release,
445 };
446
447 static int error_type_get(void *data, u64 *val)
448 {
449         *val = error_type;
450
451         return 0;
452 }
453
454 static int error_type_set(void *data, u64 val)
455 {
456         int rc;
457         u32 available_error_type = 0;
458
459         /* Only one error type can be specified */
460         if (val & (val - 1))
461                 return -EINVAL;
462         rc = einj_get_available_error_type(&available_error_type);
463         if (rc)
464                 return rc;
465         if (!(val & available_error_type))
466                 return -EINVAL;
467         error_type = val;
468
469         return 0;
470 }
471
472 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
473                         error_type_set, "0x%llx\n");
474
475 static int error_inject_set(void *data, u64 val)
476 {
477         if (!error_type)
478                 return -EINVAL;
479
480         return einj_error_inject(error_type, error_param1, error_param2);
481 }
482
483 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
484                         error_inject_set, "%llu\n");
485
486 static int einj_check_table(struct acpi_table_einj *einj_tab)
487 {
488         if ((einj_tab->header_length !=
489              (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
490             && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
491                 return -EINVAL;
492         if (einj_tab->header.length < sizeof(struct acpi_table_einj))
493                 return -EINVAL;
494         if (einj_tab->entries !=
495             (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
496             sizeof(struct acpi_einj_entry))
497                 return -EINVAL;
498
499         return 0;
500 }
501
502 static int __init einj_init(void)
503 {
504         int rc;
505         u64 param_paddr;
506         acpi_status status;
507         struct dentry *fentry;
508         struct apei_exec_context ctx;
509
510         if (acpi_disabled)
511                 return -ENODEV;
512
513         status = acpi_get_table(ACPI_SIG_EINJ, 0,
514                                 (struct acpi_table_header **)&einj_tab);
515         if (status == AE_NOT_FOUND)
516                 return -ENODEV;
517         else if (ACPI_FAILURE(status)) {
518                 const char *msg = acpi_format_exception(status);
519                 pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
520                 return -EINVAL;
521         }
522
523         rc = einj_check_table(einj_tab);
524         if (rc) {
525                 pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
526                 return -EINVAL;
527         }
528
529         rc = -ENOMEM;
530         einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
531         if (!einj_debug_dir)
532                 goto err_cleanup;
533         fentry = debugfs_create_file("available_error_type", S_IRUSR,
534                                      einj_debug_dir, NULL,
535                                      &available_error_type_fops);
536         if (!fentry)
537                 goto err_cleanup;
538         fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
539                                      einj_debug_dir, NULL, &error_type_fops);
540         if (!fentry)
541                 goto err_cleanup;
542         fentry = debugfs_create_file("error_inject", S_IWUSR,
543                                      einj_debug_dir, NULL, &error_inject_fops);
544         if (!fentry)
545                 goto err_cleanup;
546
547         apei_resources_init(&einj_resources);
548         einj_exec_ctx_init(&ctx);
549         rc = apei_exec_collect_resources(&ctx, &einj_resources);
550         if (rc)
551                 goto err_fini;
552         rc = apei_resources_request(&einj_resources, "APEI EINJ");
553         if (rc)
554                 goto err_fini;
555         rc = apei_exec_pre_map_gars(&ctx);
556         if (rc)
557                 goto err_release;
558         if (param_extension) {
559                 param_paddr = einj_get_parameter_address();
560                 if (param_paddr) {
561                         einj_param = ioremap(param_paddr, sizeof(*einj_param));
562                         rc = -ENOMEM;
563                         if (!einj_param)
564                                 goto err_unmap;
565                         fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
566                                                     einj_debug_dir, &error_param1);
567                         if (!fentry)
568                                 goto err_unmap;
569                         fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
570                                                     einj_debug_dir, &error_param2);
571                         if (!fentry)
572                                 goto err_unmap;
573                 } else
574                         pr_warn(EINJ_PFX "Parameter extension is not supported.\n");
575         }
576
577         pr_info(EINJ_PFX "Error INJection is initialized.\n");
578
579         return 0;
580
581 err_unmap:
582         if (einj_param)
583                 iounmap(einj_param);
584         apei_exec_post_unmap_gars(&ctx);
585 err_release:
586         apei_resources_release(&einj_resources);
587 err_fini:
588         apei_resources_fini(&einj_resources);
589 err_cleanup:
590         debugfs_remove_recursive(einj_debug_dir);
591
592         return rc;
593 }
594
595 static void __exit einj_exit(void)
596 {
597         struct apei_exec_context ctx;
598
599         if (einj_param)
600                 iounmap(einj_param);
601         einj_exec_ctx_init(&ctx);
602         apei_exec_post_unmap_gars(&ctx);
603         apei_resources_release(&einj_resources);
604         apei_resources_fini(&einj_resources);
605         debugfs_remove_recursive(einj_debug_dir);
606 }
607
608 module_init(einj_init);
609 module_exit(einj_exit);
610
611 MODULE_AUTHOR("Huang Ying");
612 MODULE_DESCRIPTION("APEI Error INJection support");
613 MODULE_LICENSE("GPL");