Commit | Line | Data |
---|---|---|
9cdeb404 BP |
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 | ||
47ca08a4 | 18 | #include "mce_amd.h" |
9cdeb404 BP |
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 | */ | |
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 | printk(KERN_ERR "Non-existant MCE bank: %lu\n", value); | |
93 | return -EINVAL; | |
94 | } | |
95 | ||
96 | i_mce.bank = value; | |
97 | ||
98 | amd_decode_mce(NULL, 0, &i_mce); | |
99 | ||
100 | return count; | |
101 | } | |
102 | ||
103 | static ssize_t edac_inject_bank_show(struct kobject *kobj, | |
104 | struct edac_mce_attr *attr, char *buf) | |
105 | { | |
106 | return sprintf(buf, "%d\n", i_mce.bank); | |
107 | } | |
108 | ||
109 | EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store); | |
110 | ||
111 | static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc, | |
112 | &mce_attr_addr, &mce_attr_bank | |
113 | }; | |
114 | ||
115 | static int __init edac_init_mce_inject(void) | |
116 | { | |
117 | struct sysdev_class *edac_class = NULL; | |
118 | int i, err = 0; | |
119 | ||
120 | edac_class = edac_get_sysfs_class(); | |
121 | if (!edac_class) | |
122 | return -EINVAL; | |
123 | ||
124 | mce_kobj = kobject_create_and_add("mce", &edac_class->kset.kobj); | |
125 | if (!mce_kobj) { | |
126 | printk(KERN_ERR "Error creating a mce kset.\n"); | |
127 | err = -ENOMEM; | |
128 | goto err_mce_kobj; | |
129 | } | |
130 | ||
131 | for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++) { | |
132 | err = sysfs_create_file(mce_kobj, &sysfs_attrs[i]->attr); | |
133 | if (err) { | |
134 | printk(KERN_ERR "Error creating %s in sysfs.\n", | |
135 | sysfs_attrs[i]->attr.name); | |
136 | goto err_sysfs_create; | |
137 | } | |
138 | } | |
139 | return 0; | |
140 | ||
141 | err_sysfs_create: | |
142 | while (i-- >= 0) | |
143 | sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr); | |
144 | ||
145 | kobject_del(mce_kobj); | |
146 | ||
147 | err_mce_kobj: | |
148 | edac_put_sysfs_class(); | |
149 | ||
150 | return err; | |
151 | } | |
152 | ||
153 | static void __exit edac_exit_mce_inject(void) | |
154 | { | |
155 | int i; | |
156 | ||
157 | for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++) | |
158 | sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr); | |
159 | ||
160 | kobject_del(mce_kobj); | |
161 | ||
162 | edac_put_sysfs_class(); | |
163 | } | |
164 | ||
165 | module_init(edac_init_mce_inject); | |
166 | module_exit(edac_exit_mce_inject); | |
167 | ||
168 | MODULE_LICENSE("GPL"); | |
169 | MODULE_AUTHOR("Borislav Petkov <borislav.petkov@amd.com>"); | |
170 | MODULE_AUTHOR("AMD Inc."); | |
171 | MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding"); |