blob: 5e46a9fea31bef134bb9f1d4685bec3c2f108250 [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 *
Borislav Petkov43aff262012-10-29 18:40:09 +01009 * Copyright (c) 2010: Borislav Petkov <bp@alien8.de>
Borislav Petkov9cdeb402010-09-02 18:33:24 +020010 * Advanced Micro Devices Inc.
11 */
12
13#include <linux/kobject.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050014#include <linux/device.h>
Borislav Petkov9cdeb402010-09-02 18:33:24 +020015#include <linux/edac.h>
Paul Gortmaker80a2e2e2011-07-03 13:37:56 -040016#include <linux/module.h>
Borislav Petkov9cdeb402010-09-02 18:33:24 +020017#include <asm/mce.h>
18
Borislav Petkov47ca08a2010-09-27 15:30:39 +020019#include "mce_amd.h"
Borislav Petkov9cdeb402010-09-02 18:33:24 +020020
21struct 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) \
29static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store)
30
31static struct kobject *mce_kobj;
32
33/*
34 * Collect all the MCi_XXX settings
35 */
36static struct mce i_mce;
37
38#define MCE_INJECT_STORE(reg) \
39static 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 \
Jingoo Hanc7f62fc2013-06-01 16:08:22 +090046 ret = kstrtoul(data, 16, &value); \
Borislav Petkov9cdeb402010-09-02 18:33:24 +020047 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
55MCE_INJECT_STORE(status);
56MCE_INJECT_STORE(misc);
57MCE_INJECT_STORE(addr);
58
59#define MCE_INJECT_SHOW(reg) \
60static 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
67MCE_INJECT_SHOW(status);
68MCE_INJECT_SHOW(misc);
69MCE_INJECT_SHOW(addr);
70
71EDAC_MCE_ATTR(status, 0644, edac_inject_status_show, edac_inject_status_store);
72EDAC_MCE_ATTR(misc, 0644, edac_inject_misc_show, edac_inject_misc_store);
73EDAC_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 */
79static 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
Jingoo Hanc7f62fc2013-06-01 16:08:22 +090086 ret = kstrtoul(data, 10, &value);
Borislav Petkov9cdeb402010-09-02 18:33:24 +020087 if (ret < 0) {
88 printk(KERN_ERR "Invalid bank value!\n");
89 return -EINVAL;
90 }
91
Borislav Petkov1b07ca42010-11-09 19:41:49 +010092 if (value > 5)
93 if (boot_cpu_data.x86 != 0x15 || value > 6) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -030094 printk(KERN_ERR "Non-existent MCE bank: %lu\n", value);
Borislav Petkov1b07ca42010-11-09 19:41:49 +010095 return -EINVAL;
96 }
Borislav Petkov9cdeb402010-09-02 18:33:24 +020097
98 i_mce.bank = value;
99
100 amd_decode_mce(NULL, 0, &i_mce);
101
102 return count;
103}
104
105static 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
111EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store);
112
113static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc,
114 &mce_attr_addr, &mce_attr_bank
115};
116
117static int __init edac_init_mce_inject(void)
118{
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800119 struct bus_type *edac_subsys = NULL;
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200120 int i, err = 0;
121
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800122 edac_subsys = edac_get_sysfs_subsys();
123 if (!edac_subsys)
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200124 return -EINVAL;
125
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800126 mce_kobj = kobject_create_and_add("mce", &edac_subsys->dev_root->kobj);
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200127 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
143err_sysfs_create:
Axel Lindf4b2a32010-11-18 22:05:43 -0500144 while (--i >= 0)
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200145 sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
146
147 kobject_del(mce_kobj);
148
149err_mce_kobj:
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800150 edac_put_sysfs_subsys();
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200151
152 return err;
153}
154
155static 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
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800164 edac_put_sysfs_subsys();
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200165}
166
167module_init(edac_init_mce_inject);
168module_exit(edac_exit_mce_inject);
169
170MODULE_LICENSE("GPL");
Borislav Petkov43aff262012-10-29 18:40:09 +0100171MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>");
Borislav Petkov9cdeb402010-09-02 18:33:24 +0200172MODULE_AUTHOR("AMD Inc.");
173MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding");