Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 1 | /* |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 2 | * A simple MCE injection facility for testing different aspects of the RAS |
| 3 | * code. This driver should be built as module so that it can be loaded |
| 4 | * on production kernels for testing purposes. |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 5 | * |
| 6 | * This file may be distributed under the terms of the GNU General Public |
| 7 | * License version 2. |
| 8 | * |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 9 | * Copyright (c) 2010-14: 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> |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 15 | #include <linux/device.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 | |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 21 | /* |
| 22 | * Collect all the MCi_XXX settings |
| 23 | */ |
| 24 | static struct mce i_mce; |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 25 | static struct dentry *dfs_inj; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 26 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 27 | #define MCE_INJECT_SET(reg) \ |
| 28 | static int inj_##reg##_set(void *data, u64 val) \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 29 | { \ |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 30 | struct mce *m = (struct mce *)data; \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 31 | \ |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 32 | m->reg = val; \ |
| 33 | return 0; \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 36 | MCE_INJECT_SET(status); |
| 37 | MCE_INJECT_SET(misc); |
| 38 | MCE_INJECT_SET(addr); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 39 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 40 | #define MCE_INJECT_GET(reg) \ |
| 41 | static int inj_##reg##_get(void *data, u64 *val) \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 42 | { \ |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 43 | struct mce *m = (struct mce *)data; \ |
| 44 | \ |
| 45 | *val = m->reg; \ |
| 46 | return 0; \ |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 49 | MCE_INJECT_GET(status); |
| 50 | MCE_INJECT_GET(misc); |
| 51 | MCE_INJECT_GET(addr); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 52 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 53 | DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n"); |
| 54 | DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n"); |
| 55 | DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n"); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 56 | |
| 57 | /* |
Borislav Petkov | 2169093 | 2014-11-22 11:22:35 +0100 | [diff] [blame] | 58 | * Caller needs to be make sure this cpu doesn't disappear |
| 59 | * from under us, i.e.: get_cpu/put_cpu. |
| 60 | */ |
| 61 | static int toggle_hw_mce_inject(unsigned int cpu, bool enable) |
| 62 | { |
| 63 | u32 l, h; |
| 64 | int err; |
| 65 | |
| 66 | err = rdmsr_on_cpu(cpu, MSR_K7_HWCR, &l, &h); |
| 67 | if (err) { |
| 68 | pr_err("%s: error reading HWCR\n", __func__); |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | enable ? (l |= BIT(18)) : (l &= ~BIT(18)); |
| 73 | |
| 74 | err = wrmsr_on_cpu(cpu, MSR_K7_HWCR, l, h); |
| 75 | if (err) |
| 76 | pr_err("%s: error writing HWCR\n", __func__); |
| 77 | |
| 78 | return err; |
| 79 | } |
| 80 | |
Borislav Petkov | b18f386 | 2014-11-22 11:35:26 +0100 | [diff] [blame^] | 81 | static int flags_get(void *data, u64 *val) |
| 82 | { |
| 83 | struct mce *m = (struct mce *)data; |
| 84 | |
| 85 | *val = m->inject_flags; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int flags_set(void *data, u64 val) |
| 91 | { |
| 92 | struct mce *m = (struct mce *)data; |
| 93 | |
| 94 | m->inject_flags = (u8)val; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | DEFINE_SIMPLE_ATTRIBUTE(flags_fops, flags_get, flags_set, "%llu\n"); |
| 99 | |
| 100 | /* |
| 101 | * On which CPU to inject? |
| 102 | */ |
| 103 | MCE_INJECT_GET(extcpu); |
| 104 | |
| 105 | static int inj_extcpu_set(void *data, u64 val) |
| 106 | { |
| 107 | struct mce *m = (struct mce *)data; |
| 108 | |
| 109 | if (val >= nr_cpu_ids || !cpu_online(val)) { |
| 110 | pr_err("%s: Invalid CPU: %llu\n", __func__, val); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | m->extcpu = val; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | DEFINE_SIMPLE_ATTRIBUTE(extcpu_fops, inj_extcpu_get, inj_extcpu_set, "%llu\n"); |
| 118 | |
Borislav Petkov | 2169093 | 2014-11-22 11:22:35 +0100 | [diff] [blame] | 119 | /* |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 120 | * This denotes into which bank we're injecting and triggers |
| 121 | * the injection, at the same time. |
| 122 | */ |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 123 | static int inj_bank_set(void *data, u64 val) |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 124 | { |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 125 | struct mce *m = (struct mce *)data; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 126 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 127 | if (val > 5) { |
| 128 | if (boot_cpu_data.x86 != 0x15 || val > 6) { |
| 129 | pr_err("Non-existent MCE bank: %llu\n", val); |
Borislav Petkov | 1b07ca4 | 2010-11-09 19:41:49 +0100 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | } |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 132 | } |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 133 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 134 | m->bank = val; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 135 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 136 | amd_decode_mce(NULL, 0, m); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 137 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 138 | return 0; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 141 | static int inj_bank_get(void *data, u64 *val) |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 142 | { |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 143 | struct mce *m = (struct mce *)data; |
| 144 | |
| 145 | *val = m->bank; |
| 146 | return 0; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 149 | DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n"); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 150 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 151 | struct dfs_node { |
| 152 | char *name; |
| 153 | struct dentry *d; |
| 154 | const struct file_operations *fops; |
| 155 | } dfs_fls[] = { |
| 156 | { .name = "status", .fops = &status_fops }, |
| 157 | { .name = "misc", .fops = &misc_fops }, |
| 158 | { .name = "addr", .fops = &addr_fops }, |
| 159 | { .name = "bank", .fops = &bank_fops }, |
Borislav Petkov | b18f386 | 2014-11-22 11:35:26 +0100 | [diff] [blame^] | 160 | { .name = "flags", .fops = &flags_fops }, |
| 161 | { .name = "cpu", .fops = &extcpu_fops }, |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 162 | }; |
| 163 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 164 | static int __init init_mce_inject(void) |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 165 | { |
| 166 | int i; |
| 167 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 168 | dfs_inj = debugfs_create_dir("mce-inject", NULL); |
| 169 | if (!dfs_inj) |
| 170 | return -EINVAL; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 171 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 172 | for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) { |
| 173 | dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name, |
| 174 | S_IRUSR | S_IWUSR, |
| 175 | dfs_inj, |
| 176 | &i_mce, |
| 177 | dfs_fls[i].fops); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 178 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 179 | if (!dfs_fls[i].d) |
| 180 | goto err_dfs_add; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | |
| 185 | err_dfs_add: |
| 186 | while (--i >= 0) |
| 187 | debugfs_remove(dfs_fls[i].d); |
| 188 | |
| 189 | debugfs_remove(dfs_inj); |
| 190 | dfs_inj = NULL; |
| 191 | |
| 192 | return -ENOMEM; |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 195 | static void __exit exit_mce_inject(void) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) |
| 200 | debugfs_remove(dfs_fls[i].d); |
| 201 | |
| 202 | memset(&dfs_fls, 0, sizeof(dfs_fls)); |
| 203 | |
| 204 | debugfs_remove(dfs_inj); |
| 205 | dfs_inj = NULL; |
| 206 | } |
| 207 | module_init(init_mce_inject); |
| 208 | module_exit(exit_mce_inject); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 209 | |
| 210 | MODULE_LICENSE("GPL"); |
Borislav Petkov | 43aff26 | 2012-10-29 18:40:09 +0100 | [diff] [blame] | 211 | MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>"); |
Borislav Petkov | 9cdeb40 | 2010-09-02 18:33:24 +0200 | [diff] [blame] | 212 | MODULE_AUTHOR("AMD Inc."); |
Borislav Petkov | fd19fcd | 2014-11-22 11:09:12 +0100 | [diff] [blame] | 213 | MODULE_DESCRIPTION("MCE injection facility for RAS testing"); |