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