Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 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 | * |
Borislav Petkov | 43aff26 | 2012-10-29 18:40:09 +0100 | [diff] [blame] | 9 | * Copyright (c) 2010: Borislav Petkov <bp@alien8.de> |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 10 | * Advanced Micro Devices Inc. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kobject.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 14 | #include <linux/device.h> |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 15 | #include <linux/edac.h> |
Paul Gortmaker | 80a2e2e | 2011-07-03 13:37:56 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 17 | #include <asm/mce.h> |
| 18 | |
Borislav Petkov | 47ca08a | 2010-09-27 15:30:39 +0200 | [diff] [blame] | 19 | #include "mce_amd.h" |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 20 | |
| 21 | struct 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) \ |
| 29 | static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store) |
| 30 | |
| 31 | static struct kobject *mce_kobj; |
| 32 | |
| 33 | /* |
| 34 | * Collect all the MCi_XXX settings |
| 35 | */ |
| 36 | static struct mce i_mce; |
| 37 | |
| 38 | #define MCE_INJECT_STORE(reg) \ |
| 39 | static 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 Han | c7f62fc | 2013-06-01 16:08:22 +0900 | [diff] [blame] | 46 | ret = kstrtoul(data, 16, &value); \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 47 | 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 | |
| 55 | MCE_INJECT_STORE(status); |
| 56 | MCE_INJECT_STORE(misc); |
| 57 | MCE_INJECT_STORE(addr); |
| 58 | |
| 59 | #define MCE_INJECT_SHOW(reg) \ |
| 60 | static 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 | |
| 67 | MCE_INJECT_SHOW(status); |
| 68 | MCE_INJECT_SHOW(misc); |
| 69 | MCE_INJECT_SHOW(addr); |
| 70 | |
| 71 | EDAC_MCE_ATTR(status, 0644, edac_inject_status_show, edac_inject_status_store); |
| 72 | EDAC_MCE_ATTR(misc, 0644, edac_inject_misc_show, edac_inject_misc_store); |
| 73 | EDAC_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 | */ |
| 79 | static 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 Han | c7f62fc | 2013-06-01 16:08:22 +0900 | [diff] [blame] | 86 | ret = kstrtoul(data, 10, &value); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 87 | if (ret < 0) { |
| 88 | printk(KERN_ERR "Invalid bank value!\n"); |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | |
Borislav Petkov | 1b07ca4 | 2010-11-09 19:41:49 +0100 | [diff] [blame] | 92 | if (value > 5) |
| 93 | if (boot_cpu_data.x86 != 0x15 || value > 6) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 94 | printk(KERN_ERR "Non-existent MCE bank: %lu\n", value); |
Borislav Petkov | 1b07ca4 | 2010-11-09 19:41:49 +0100 | [diff] [blame] | 95 | return -EINVAL; |
| 96 | } |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 97 | |
| 98 | i_mce.bank = value; |
| 99 | |
| 100 | amd_decode_mce(NULL, 0, &i_mce); |
| 101 | |
| 102 | return count; |
| 103 | } |
| 104 | |
| 105 | static 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 | |
| 111 | EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store); |
| 112 | |
| 113 | static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc, |
| 114 | &mce_attr_addr, &mce_attr_bank |
| 115 | }; |
| 116 | |
| 117 | static int __init edac_init_mce_inject(void) |
| 118 | { |
Kay Sievers | fe5ff8b | 2011-12-14 15:21:07 -0800 | [diff] [blame] | 119 | struct bus_type *edac_subsys = NULL; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 120 | int i, err = 0; |
| 121 | |
Kay Sievers | fe5ff8b | 2011-12-14 15:21:07 -0800 | [diff] [blame] | 122 | edac_subsys = edac_get_sysfs_subsys(); |
| 123 | if (!edac_subsys) |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | |
Kay Sievers | fe5ff8b | 2011-12-14 15:21:07 -0800 | [diff] [blame] | 126 | mce_kobj = kobject_create_and_add("mce", &edac_subsys->dev_root->kobj); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 127 | 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 | |
| 143 | err_sysfs_create: |
Axel Lin | df4b2a3 | 2010-11-18 22:05:43 -0500 | [diff] [blame] | 144 | while (--i >= 0) |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 145 | sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr); |
| 146 | |
| 147 | kobject_del(mce_kobj); |
| 148 | |
| 149 | err_mce_kobj: |
Kay Sievers | fe5ff8b | 2011-12-14 15:21:07 -0800 | [diff] [blame] | 150 | edac_put_sysfs_subsys(); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 151 | |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | static 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 Sievers | fe5ff8b | 2011-12-14 15:21:07 -0800 | [diff] [blame] | 164 | edac_put_sysfs_subsys(); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | module_init(edac_init_mce_inject); |
| 168 | module_exit(edac_exit_mce_inject); |
| 169 | |
| 170 | MODULE_LICENSE("GPL"); |
Borislav Petkov | 43aff26 | 2012-10-29 18:40:09 +0100 | [diff] [blame] | 171 | MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>"); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 172 | MODULE_AUTHOR("AMD Inc."); |
| 173 | MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding"); |