i5100_edac: connect fault injection to debugfs node
[linux-3.10.git] / drivers / edac / mce_amd_inj.c
1 /*
2  * A simple MCE injection facility for testing the MCE decoding code. This
3  * driver should be built as module so that it can be loaded on production
4  * kernels for testing purposes.
5  *
6  * This file may be distributed under the terms of the GNU General Public
7  * License version 2.
8  *
9  * Copyright (c) 2010:  Borislav Petkov <bp@alien8.de>
10  *                      Advanced Micro Devices Inc.
11  */
12
13 #include <linux/kobject.h>
14 #include <linux/device.h>
15 #include <linux/edac.h>
16 #include <linux/module.h>
17 #include <asm/mce.h>
18
19 #include "mce_amd.h"
20
21 struct edac_mce_attr {
22         struct attribute attr;
23         ssize_t (*show) (struct kobject *kobj, struct edac_mce_attr *attr, char *buf);
24         ssize_t (*store)(struct kobject *kobj, struct edac_mce_attr *attr,
25                          const char *buf, size_t count);
26 };
27
28 #define EDAC_MCE_ATTR(_name, _mode, _show, _store)                      \
29 static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store)
30
31 static struct kobject *mce_kobj;
32
33 /*
34  * Collect all the MCi_XXX settings
35  */
36 static struct mce i_mce;
37
38 #define MCE_INJECT_STORE(reg)                                           \
39 static ssize_t edac_inject_##reg##_store(struct kobject *kobj,          \
40                                          struct edac_mce_attr *attr,    \
41                                          const char *data, size_t count)\
42 {                                                                       \
43         int ret = 0;                                                    \
44         unsigned long value;                                            \
45                                                                         \
46         ret = strict_strtoul(data, 16, &value);                         \
47         if (ret < 0)                                                    \
48                 printk(KERN_ERR "Error writing MCE " #reg " field.\n"); \
49                                                                         \
50         i_mce.reg = value;                                              \
51                                                                         \
52         return count;                                                   \
53 }
54
55 MCE_INJECT_STORE(status);
56 MCE_INJECT_STORE(misc);
57 MCE_INJECT_STORE(addr);
58
59 #define MCE_INJECT_SHOW(reg)                                            \
60 static ssize_t edac_inject_##reg##_show(struct kobject *kobj,           \
61                                         struct edac_mce_attr *attr,     \
62                                         char *buf)                      \
63 {                                                                       \
64         return sprintf(buf, "0x%016llx\n", i_mce.reg);                  \
65 }
66
67 MCE_INJECT_SHOW(status);
68 MCE_INJECT_SHOW(misc);
69 MCE_INJECT_SHOW(addr);
70
71 EDAC_MCE_ATTR(status, 0644, edac_inject_status_show, edac_inject_status_store);
72 EDAC_MCE_ATTR(misc, 0644, edac_inject_misc_show, edac_inject_misc_store);
73 EDAC_MCE_ATTR(addr, 0644, edac_inject_addr_show, edac_inject_addr_store);
74
75 /*
76  * This denotes into which bank we're injecting and triggers
77  * the injection, at the same time.
78  */
79 static ssize_t edac_inject_bank_store(struct kobject *kobj,
80                                       struct edac_mce_attr *attr,
81                                       const char *data, size_t count)
82 {
83         int ret = 0;
84         unsigned long value;
85
86         ret = strict_strtoul(data, 10, &value);
87         if (ret < 0) {
88                 printk(KERN_ERR "Invalid bank value!\n");
89                 return -EINVAL;
90         }
91
92         if (value > 5)
93                 if (boot_cpu_data.x86 != 0x15 || value > 6) {
94                         printk(KERN_ERR "Non-existent MCE bank: %lu\n", value);
95                         return -EINVAL;
96                 }
97
98         i_mce.bank = value;
99
100         amd_decode_mce(NULL, 0, &i_mce);
101
102         return count;
103 }
104
105 static ssize_t edac_inject_bank_show(struct kobject *kobj,
106                                      struct edac_mce_attr *attr, char *buf)
107 {
108         return sprintf(buf, "%d\n", i_mce.bank);
109 }
110
111 EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store);
112
113 static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc,
114                                                &mce_attr_addr, &mce_attr_bank
115 };
116
117 static int __init edac_init_mce_inject(void)
118 {
119         struct bus_type *edac_subsys = NULL;
120         int i, err = 0;
121
122         edac_subsys = edac_get_sysfs_subsys();
123         if (!edac_subsys)
124                 return -EINVAL;
125
126         mce_kobj = kobject_create_and_add("mce", &edac_subsys->dev_root->kobj);
127         if (!mce_kobj) {
128                 printk(KERN_ERR "Error creating a mce kset.\n");
129                 err = -ENOMEM;
130                 goto err_mce_kobj;
131         }
132
133         for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++) {
134                 err = sysfs_create_file(mce_kobj, &sysfs_attrs[i]->attr);
135                 if (err) {
136                         printk(KERN_ERR "Error creating %s in sysfs.\n",
137                                         sysfs_attrs[i]->attr.name);
138                         goto err_sysfs_create;
139                 }
140         }
141         return 0;
142
143 err_sysfs_create:
144         while (--i >= 0)
145                 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
146
147         kobject_del(mce_kobj);
148
149 err_mce_kobj:
150         edac_put_sysfs_subsys();
151
152         return err;
153 }
154
155 static void __exit edac_exit_mce_inject(void)
156 {
157         int i;
158
159         for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++)
160                 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
161
162         kobject_del(mce_kobj);
163
164         edac_put_sysfs_subsys();
165 }
166
167 module_init(edac_init_mce_inject);
168 module_exit(edac_exit_mce_inject);
169
170 MODULE_LICENSE("GPL");
171 MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>");
172 MODULE_AUTHOR("AMD Inc.");
173 MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding");