]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/acpi/apei/einj.c
Merge remote-tracking branch 'tip/auto-latest'
[karo-tx-linux.git] / 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 <linux/mm.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 #define ACPI5_VENDOR_BIT        BIT(31)
45 #define MEM_ERROR_MASK          (ACPI_EINJ_MEMORY_CORRECTABLE | \
46                                 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
47                                 ACPI_EINJ_MEMORY_FATAL)
48
49 /*
50  * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
51  */
52 static int acpi5;
53
54 struct set_error_type_with_address {
55         u32     type;
56         u32     vendor_extension;
57         u32     flags;
58         u32     apicid;
59         u64     memory_address;
60         u64     memory_address_range;
61         u32     pcie_sbdf;
62 };
63 enum {
64         SETWA_FLAGS_APICID = 1,
65         SETWA_FLAGS_MEM = 2,
66         SETWA_FLAGS_PCIE_SBDF = 4,
67 };
68
69 /*
70  * Vendor extensions for platform specific operations
71  */
72 struct vendor_error_type_extension {
73         u32     length;
74         u32     pcie_sbdf;
75         u16     vendor_id;
76         u16     device_id;
77         u8      rev_id;
78         u8      reserved[3];
79 };
80
81 static u32 notrigger;
82
83 static u32 vendor_flags;
84 static struct debugfs_blob_wrapper vendor_blob;
85 static char vendor_dev[64];
86
87 /*
88  * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
89  * EINJ table through an unpublished extension. Use with caution as
90  * most will ignore the parameter and make their own choice of address
91  * for error injection.  This extension is used only if
92  * param_extension module parameter is specified.
93  */
94 struct einj_parameter {
95         u64 type;
96         u64 reserved1;
97         u64 reserved2;
98         u64 param1;
99         u64 param2;
100 };
101
102 #define EINJ_OP_BUSY                    0x1
103 #define EINJ_STATUS_SUCCESS             0x0
104 #define EINJ_STATUS_FAIL                0x1
105 #define EINJ_STATUS_INVAL               0x2
106
107 #define EINJ_TAB_ENTRY(tab)                                             \
108         ((struct acpi_whea_header *)((char *)(tab) +                    \
109                                     sizeof(struct acpi_table_einj)))
110
111 static bool param_extension;
112 module_param(param_extension, bool, 0);
113
114 static struct acpi_table_einj *einj_tab;
115
116 static struct apei_resources einj_resources;
117
118 static struct apei_exec_ins_type einj_ins_type[] = {
119         [ACPI_EINJ_READ_REGISTER] = {
120                 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
121                 .run   = apei_exec_read_register,
122         },
123         [ACPI_EINJ_READ_REGISTER_VALUE] = {
124                 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
125                 .run   = apei_exec_read_register_value,
126         },
127         [ACPI_EINJ_WRITE_REGISTER] = {
128                 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
129                 .run   = apei_exec_write_register,
130         },
131         [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
132                 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
133                 .run   = apei_exec_write_register_value,
134         },
135         [ACPI_EINJ_NOOP] = {
136                 .flags = 0,
137                 .run   = apei_exec_noop,
138         },
139 };
140
141 /*
142  * Prevent EINJ interpreter to run simultaneously, because the
143  * corresponding firmware implementation may not work properly when
144  * invoked simultaneously.
145  */
146 static DEFINE_MUTEX(einj_mutex);
147
148 static void *einj_param;
149
150 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
151 {
152         apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
153                            EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
154 }
155
156 static int __einj_get_available_error_type(u32 *type)
157 {
158         struct apei_exec_context ctx;
159         int rc;
160
161         einj_exec_ctx_init(&ctx);
162         rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
163         if (rc)
164                 return rc;
165         *type = apei_exec_ctx_get_output(&ctx);
166
167         return 0;
168 }
169
170 /* Get error injection capabilities of the platform */
171 static int einj_get_available_error_type(u32 *type)
172 {
173         int rc;
174
175         mutex_lock(&einj_mutex);
176         rc = __einj_get_available_error_type(type);
177         mutex_unlock(&einj_mutex);
178
179         return rc;
180 }
181
182 static int einj_timedout(u64 *t)
183 {
184         if ((s64)*t < SPIN_UNIT) {
185                 pr_warning(FW_WARN EINJ_PFX
186                            "Firmware does not respond in time\n");
187                 return 1;
188         }
189         *t -= SPIN_UNIT;
190         ndelay(SPIN_UNIT);
191         touch_nmi_watchdog();
192         return 0;
193 }
194
195 static void check_vendor_extension(u64 paddr,
196                                    struct set_error_type_with_address *v5param)
197 {
198         int     offset = v5param->vendor_extension;
199         struct  vendor_error_type_extension *v;
200         u32     sbdf;
201
202         if (!offset)
203                 return;
204         v = acpi_os_map_memory(paddr + offset, sizeof(*v));
205         if (!v)
206                 return;
207         sbdf = v->pcie_sbdf;
208         sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
209                 sbdf >> 24, (sbdf >> 16) & 0xff,
210                 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
211                  v->vendor_id, v->device_id, v->rev_id);
212         acpi_os_unmap_memory(v, sizeof(*v));
213 }
214
215 static void *einj_get_parameter_address(void)
216 {
217         int i;
218         u64 paddrv4 = 0, paddrv5 = 0;
219         struct acpi_whea_header *entry;
220
221         entry = EINJ_TAB_ENTRY(einj_tab);
222         for (i = 0; i < einj_tab->entries; i++) {
223                 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
224                     entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
225                     entry->register_region.space_id ==
226                     ACPI_ADR_SPACE_SYSTEM_MEMORY)
227                         memcpy(&paddrv4, &entry->register_region.address,
228                                sizeof(paddrv4));
229                 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
230                     entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
231                     entry->register_region.space_id ==
232                     ACPI_ADR_SPACE_SYSTEM_MEMORY)
233                         memcpy(&paddrv5, &entry->register_region.address,
234                                sizeof(paddrv5));
235                 entry++;
236         }
237         if (paddrv5) {
238                 struct set_error_type_with_address *v5param;
239
240                 v5param = acpi_os_map_memory(paddrv5, sizeof(*v5param));
241                 if (v5param) {
242                         acpi5 = 1;
243                         check_vendor_extension(paddrv5, v5param);
244                         return v5param;
245                 }
246         }
247         if (param_extension && paddrv4) {
248                 struct einj_parameter *v4param;
249
250                 v4param = acpi_os_map_memory(paddrv4, sizeof(*v4param));
251                 if (!v4param)
252                         return NULL;
253                 if (v4param->reserved1 || v4param->reserved2) {
254                         acpi_os_unmap_memory(v4param, sizeof(*v4param));
255                         return NULL;
256                 }
257                 return v4param;
258         }
259
260         return NULL;
261 }
262
263 /* do sanity check to trigger table */
264 static 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 ||
269             trigger_tab->table_size < trigger_tab->header_size)
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
279 static 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 }
299 /* Execute instructions in trigger error action table */
300 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
301                                 u64 param1, u64 param2)
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;
310         struct acpi_generic_address *trigger_param_region = NULL;
311
312         r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
313                                "APEI EINJ Trigger Table");
314         if (!r) {
315                 pr_err(EINJ_PFX
316         "Can not request [mem %#010llx-%#010llx] for Trigger table\n",
317                        (unsigned long long)trigger_paddr,
318                        (unsigned long long)trigger_paddr +
319                             sizeof(*trigger_tab) - 1);
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         }
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
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
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);
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;
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          */
374         if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
375                 struct apei_resources addr_resources;
376                 apei_resources_init(&addr_resources);
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                 }
388                 apei_resources_fini(&addr_resources);
389                 if (rc)
390                         goto out_fini;
391         }
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);
402 out_release:
403         apei_resources_release(&trigger_resources);
404 out_fini:
405         apei_resources_fini(&trigger_resources);
406 out_rel_entry:
407         release_mem_region(trigger_paddr + sizeof(*trigger_tab),
408                            table_size - sizeof(*trigger_tab));
409 out_rel_header:
410         release_mem_region(trigger_paddr, sizeof(*trigger_tab));
411 out:
412         if (trigger_tab)
413                 iounmap(trigger_tab);
414
415         return rc;
416 }
417
418 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
419                                u64 param3, u64 param4)
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
427         rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
428         if (rc)
429                 return rc;
430         apei_exec_ctx_set_input(&ctx, type);
431         if (acpi5) {
432                 struct set_error_type_with_address *v5param = einj_param;
433
434                 v5param->type = type;
435                 if (type & ACPI5_VENDOR_BIT) {
436                         switch (vendor_flags) {
437                         case SETWA_FLAGS_APICID:
438                                 v5param->apicid = param1;
439                                 break;
440                         case SETWA_FLAGS_MEM:
441                                 v5param->memory_address = param1;
442                                 v5param->memory_address_range = param2;
443                                 break;
444                         case SETWA_FLAGS_PCIE_SBDF:
445                                 v5param->pcie_sbdf = param1;
446                                 break;
447                         }
448                         v5param->flags = vendor_flags;
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;
455                 } else {
456                         switch (type) {
457                         case ACPI_EINJ_PROCESSOR_CORRECTABLE:
458                         case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
459                         case ACPI_EINJ_PROCESSOR_FATAL:
460                                 v5param->apicid = param1;
461                                 v5param->flags = SETWA_FLAGS_APICID;
462                                 break;
463                         case ACPI_EINJ_MEMORY_CORRECTABLE:
464                         case ACPI_EINJ_MEMORY_UNCORRECTABLE:
465                         case ACPI_EINJ_MEMORY_FATAL:
466                                 v5param->memory_address = param1;
467                                 v5param->memory_address_range = param2;
468                                 v5param->flags = SETWA_FLAGS_MEM;
469                                 break;
470                         case ACPI_EINJ_PCIX_CORRECTABLE:
471                         case ACPI_EINJ_PCIX_UNCORRECTABLE:
472                         case ACPI_EINJ_PCIX_FATAL:
473                                 v5param->pcie_sbdf = param1;
474                                 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
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;
484                         v4param->param1 = param1;
485                         v4param->param2 = param2;
486                 }
487         }
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);
512         if (notrigger == 0) {
513                 rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
514                 if (rc)
515                         return rc;
516         }
517         rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
518
519         return rc;
520 }
521
522 /* Inject the specified hardware error */
523 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
524                              u64 param3, u64 param4)
525 {
526         int rc;
527         unsigned long pfn;
528
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
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;
547         } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
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
559 inject:
560         mutex_lock(&einj_mutex);
561         rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
562         mutex_unlock(&einj_mutex);
563
564         return rc;
565 }
566
567 static u32 error_type;
568 static u32 error_flags;
569 static u64 error_param1;
570 static u64 error_param2;
571 static u64 error_param3;
572 static u64 error_param4;
573 static struct dentry *einj_debug_dir;
574
575 static 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
611 static int available_error_type_open(struct inode *inode, struct file *file)
612 {
613         return single_open(file, available_error_type_show, NULL);
614 }
615
616 static 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
623 static int error_type_get(void *data, u64 *val)
624 {
625         *val = error_type;
626
627         return 0;
628 }
629
630 static int error_type_set(void *data, u64 val)
631 {
632         int rc;
633         u32 available_error_type = 0;
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          */
640         vendor = val & ACPI5_VENDOR_BIT;
641         tval = val & 0x7fffffff;
642
643         /* Only one error type can be specified */
644         if (tval & (tval - 1))
645                 return -EINVAL;
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         }
653         error_type = val;
654
655         return 0;
656 }
657
658 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
659                         error_type_set, "0x%llx\n");
660
661 static int error_inject_set(void *data, u64 val)
662 {
663         if (!error_type)
664                 return -EINVAL;
665
666         return einj_error_inject(error_type, error_flags, error_param1, error_param2,
667                 error_param3, error_param4);
668 }
669
670 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
671                         error_inject_set, "%llu\n");
672
673 static int einj_check_table(struct acpi_table_einj *einj_tab)
674 {
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)))
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
689 static 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);
701         if (status == AE_NOT_FOUND)
702                 return -ENODEV;
703         else if (ACPI_FAILURE(status)) {
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;
744
745         rc = -ENOMEM;
746         einj_param = einj_get_parameter_address();
747         if ((param_extension || acpi5) && einj_param) {
748                 fentry = debugfs_create_x32("flags", S_IRUSR | S_IWUSR,
749                                             einj_debug_dir, &error_flags);
750                 if (!fentry)
751                         goto err_unmap;
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;
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;
768
769                 fentry = debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
770                                             einj_debug_dir, &notrigger);
771                 if (!fentry)
772                         goto err_unmap;
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;
786         }
787
788         pr_info(EINJ_PFX "Error INJection is initialized.\n");
789
790         return 0;
791
792 err_unmap:
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         }
800         apei_exec_post_unmap_gars(&ctx);
801 err_release:
802         apei_resources_release(&einj_resources);
803 err_fini:
804         apei_resources_fini(&einj_resources);
805 err_cleanup:
806         debugfs_remove_recursive(einj_debug_dir);
807
808         return rc;
809 }
810
811 static void __exit einj_exit(void)
812 {
813         struct apei_exec_context ctx;
814
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         }
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
829 module_init(einj_init);
830 module_exit(einj_exit);
831
832 MODULE_AUTHOR("Huang Ying");
833 MODULE_DESCRIPTION("APEI Error INJection support");
834 MODULE_LICENSE("GPL");