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.
6 * This file may be distributed under the terms of the GNU General Public
9 * Copyright (c) 2010: Borislav Petkov <borislav.petkov@amd.com>
10 * Advanced Micro Devices Inc.
13 #include <linux/kobject.h>
14 #include <linux/sysdev.h>
15 #include <linux/edac.h>
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);
27 #define EDAC_MCE_ATTR(_name, _mode, _show, _store) \
28 static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store)
30 static struct kobject *mce_kobj;
33 * Collect all the MCi_XXX settings
35 static struct mce i_mce;
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)\
43 unsigned long value; \
45 ret = strict_strtoul(data, 16, &value); \
47 printk(KERN_ERR "Error writing MCE " #reg " field.\n"); \
54 MCE_INJECT_STORE(status);
55 MCE_INJECT_STORE(misc);
56 MCE_INJECT_STORE(addr);
58 #define MCE_INJECT_SHOW(reg) \
59 static ssize_t edac_inject_##reg##_show(struct kobject *kobj, \
60 struct edac_mce_attr *attr, \
63 return sprintf(buf, "0x%016llx\n", i_mce.reg); \
66 MCE_INJECT_SHOW(status);
67 MCE_INJECT_SHOW(misc);
68 MCE_INJECT_SHOW(addr);
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);
75 * This denotes into which bank we're injecting and triggers
76 * the injection, at the same time.
78 static ssize_t edac_inject_bank_store(struct kobject *kobj,
79 struct edac_mce_attr *attr,
80 const char *data, size_t count)
85 ret = strict_strtoul(data, 10, &value);
87 printk(KERN_ERR "Invalid bank value!\n");
92 if (boot_cpu_data.x86 != 0x15 || value > 6) {
93 printk(KERN_ERR "Non-existant MCE bank: %lu\n", value);
99 amd_decode_mce(NULL, 0, &i_mce);
104 static ssize_t edac_inject_bank_show(struct kobject *kobj,
105 struct edac_mce_attr *attr, char *buf)
107 return sprintf(buf, "%d\n", i_mce.bank);
110 EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store);
112 static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc,
113 &mce_attr_addr, &mce_attr_bank
116 static int __init edac_init_mce_inject(void)
118 struct sysdev_class *edac_class = NULL;
121 edac_class = edac_get_sysfs_class();
125 mce_kobj = kobject_create_and_add("mce", &edac_class->kset.kobj);
127 printk(KERN_ERR "Error creating a mce kset.\n");
132 for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++) {
133 err = sysfs_create_file(mce_kobj, &sysfs_attrs[i]->attr);
135 printk(KERN_ERR "Error creating %s in sysfs.\n",
136 sysfs_attrs[i]->attr.name);
137 goto err_sysfs_create;
144 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
146 kobject_del(mce_kobj);
149 edac_put_sysfs_class();
154 static void __exit edac_exit_mce_inject(void)
158 for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++)
159 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
161 kobject_del(mce_kobj);
163 edac_put_sysfs_class();
166 module_init(edac_init_mce_inject);
167 module_exit(edac_exit_mce_inject);
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");