blob: 885e8ad8fdcf94679c3631db3c319d0fff7ac618 [file] [log] [blame]
Borislav Petkov9cdeb402010-09-02 18:33:24 +02001/*
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>
Borislav Petkov9cdeb402010-09-02 18:33:24 +020014#include <linux/edac.h>
Paul Gortmaker80a2e2e2011-07-03 13:37:56 -040015#include <linux/module.h>
Borislav Petkov9cdeb402010-09-02 18:33:24 +020016#include <asm/mce.h>
17
Borislav Petkov47ca08a2010-09-27 15:30:39 +020018#include "mce_amd.h"
Borislav Petkov9cdeb402010-09-02 18:33:24 +020019
20struct 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) \
28static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store)
29
30static struct kobject *mce_kobj;
31
32/*
33 * Collect all the MCi_XXX settings
34 */
35static struct mce i_mce;
36
37#define MCE_INJECT_STORE(reg) \
38static 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
54MCE_INJECT_STORE(status);
55MCE_INJECT_STORE(misc);
56MCE_INJECT_STORE(addr);
57
58#define MCE_INJECT_SHOW(reg) \
59static 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
66MCE_INJECT_SHOW(status);
67MCE_INJECT_SHOW(misc);
68MCE_INJECT_SHOW(addr);
69
70EDAC_MCE_ATTR(status, 0644, edac_inject_status_show, edac_inject_status_store);
71EDAC_MCE_ATTR(misc, 0644, edac_inject_misc_show, edac_inject_misc_store);
72EDAC_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 */
78static 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
Borislav Petkov1b07ca42010-11-09 19:41:49 +010091 if (value > 5)
92 if (boot_cpu_data.x86 != 0x15 || value > 6) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -030093 printk(KERN_ERR "Non-existent MCE bank: %lu\n", value);
Borislav Petkov1b07ca42010-11-09 19:41:49 +010094 return -EINVAL;
95 }
Borislav Petkov9cdeb402010-09-02 18:33:24 +020096
97 i_mce.bank = value;
98
99 amd_decode_mce(NULL, 0, &i_mce);
100
101 return count;
102}
103
104static 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
110EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store);
111
112static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc,
113 &mce_attr_addr, &mce_attr_bank
114};
115
116static int __init edac_init_mce_inject(void)
117{
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800118 struct bus_type *edac_subsys = NULL;
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200119 int i, err = 0;
120
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800121 edac_subsys = edac_get_sysfs_subsys();
122 if (!edac_subsys)
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200123 return -EINVAL;
124
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800125 mce_kobj = kobject_create_and_add("mce", &edac_subsys->dev_root->kobj);
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200126 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
142err_sysfs_create:
Axel Lindf4b2a32010-11-18 22:05:43 -0500143 while (--i >= 0)
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200144 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
145
146 kobject_del(mce_kobj);
147
148err_mce_kobj:
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800149 edac_put_sysfs_subsys();
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200150
151 return err;
152}
153
154static 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
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800163 edac_put_sysfs_subsys();
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200164}
165
166module_init(edac_init_mce_inject);
167module_exit(edac_exit_mce_inject);
168
169MODULE_LICENSE("GPL");
170MODULE_AUTHOR("Borislav Petkov <borislav.petkov@amd.com>");
171MODULE_AUTHOR("AMD Inc.");
172MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding");