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.
6 * This file may be distributed under the terms of the GNU General Public
9 * Copyright (c) 2010-15: Borislav Petkov <bp@alien8.de>
10 * Advanced Micro Devices Inc.
13 #include <linux/kobject.h>
14 #include <linux/debugfs.h>
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/cpu.h>
18 #include <linux/string.h>
19 #include <linux/uaccess.h>
20 #include <linux/pci.h>
24 #include <asm/amd_nb.h>
25 #include <asm/irq_vectors.h>
27 #include "../kernel/cpu/mcheck/mce-internal.h"
30 * Collect all the MCi_XXX settings
32 static struct mce i_mce;
33 static struct dentry *dfs_inj;
37 #define MAX_FLAG_OPT_SIZE 3
41 SW_INJ = 0, /* SW injection, simply decode the error */
42 HW_INJ, /* Trigger a #MC */
43 DFR_INT_INJ, /* Trigger Deferred error interrupt */
44 THR_INT_INJ, /* Trigger threshold interrupt */
48 static const char * const flags_options[] = {
56 /* Set default injection to SW_INJ */
57 static enum injection_type inj_type = SW_INJ;
59 #define MCE_INJECT_SET(reg) \
60 static int inj_##reg##_set(void *data, u64 val) \
62 struct mce *m = (struct mce *)data; \
68 MCE_INJECT_SET(status);
73 #define MCE_INJECT_GET(reg) \
74 static int inj_##reg##_get(void *data, u64 *val) \
76 struct mce *m = (struct mce *)data; \
82 MCE_INJECT_GET(status);
87 DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n");
88 DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n");
89 DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n");
90 DEFINE_SIMPLE_ATTRIBUTE(synd_fops, inj_synd_get, inj_synd_set, "%llx\n");
93 * Caller needs to be make sure this cpu doesn't disappear
94 * from under us, i.e.: get_cpu/put_cpu.
96 static int toggle_hw_mce_inject(unsigned int cpu, bool enable)
101 err = rdmsr_on_cpu(cpu, MSR_K7_HWCR, &l, &h);
103 pr_err("%s: error reading HWCR\n", __func__);
107 enable ? (l |= BIT(18)) : (l &= ~BIT(18));
109 err = wrmsr_on_cpu(cpu, MSR_K7_HWCR, l, h);
111 pr_err("%s: error writing HWCR\n", __func__);
116 static int __set_inj(const char *buf)
120 for (i = 0; i < N_INJ_TYPES; i++) {
121 if (!strncmp(flags_options[i], buf, strlen(flags_options[i]))) {
129 static ssize_t flags_read(struct file *filp, char __user *ubuf,
130 size_t cnt, loff_t *ppos)
132 char buf[MAX_FLAG_OPT_SIZE];
135 n = sprintf(buf, "%s\n", flags_options[inj_type]);
137 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
140 static ssize_t flags_write(struct file *filp, const char __user *ubuf,
141 size_t cnt, loff_t *ppos)
143 char buf[MAX_FLAG_OPT_SIZE], *__buf;
146 if (cnt > MAX_FLAG_OPT_SIZE)
149 if (copy_from_user(&buf, ubuf, cnt))
154 /* strip whitespace */
155 __buf = strstrip(buf);
157 err = __set_inj(__buf);
159 pr_err("%s: Invalid flags value: %s\n", __func__, __buf);
168 static const struct file_operations flags_fops = {
170 .write = flags_write,
171 .llseek = generic_file_llseek,
175 * On which CPU to inject?
177 MCE_INJECT_GET(extcpu);
179 static int inj_extcpu_set(void *data, u64 val)
181 struct mce *m = (struct mce *)data;
183 if (val >= nr_cpu_ids || !cpu_online(val)) {
184 pr_err("%s: Invalid CPU: %llu\n", __func__, val);
191 DEFINE_SIMPLE_ATTRIBUTE(extcpu_fops, inj_extcpu_get, inj_extcpu_set, "%llu\n");
193 static void trigger_mce(void *info)
195 asm volatile("int $18");
198 static void trigger_dfr_int(void *info)
200 asm volatile("int %0" :: "i" (DEFERRED_ERROR_VECTOR));
203 static void trigger_thr_int(void *info)
205 asm volatile("int %0" :: "i" (THRESHOLD_APIC_VECTOR));
208 static u32 get_nbc_for_node(int node_id)
210 struct cpuinfo_x86 *c = &boot_cpu_data;
213 cores_per_node = (c->x86_max_cores * smp_num_siblings) / amd_get_nodes_per_socket();
215 return cores_per_node * node_id;
218 static void toggle_nb_mca_mst_cpu(u16 nid)
220 struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
227 err = pci_read_config_dword(F3, NBCFG, &val);
229 pr_err("%s: Error reading F%dx%03x.\n",
230 __func__, PCI_FUNC(F3->devfn), NBCFG);
237 pr_err("%s: Set D18F3x44[NbMcaToMstCpuEn] which BIOS hasn't done.\n",
241 err = pci_write_config_dword(F3, NBCFG, val);
243 pr_err("%s: Error writing F%dx%03x.\n",
244 __func__, PCI_FUNC(F3->devfn), NBCFG);
247 static void prepare_msrs(void *info)
249 struct mce i_mce = *(struct mce *)info;
252 wrmsrl(MSR_IA32_MCG_STATUS, i_mce.mcgstatus);
254 if (boot_cpu_has(X86_FEATURE_SMCA)) {
255 if (i_mce.inject_flags == DFR_INT_INJ) {
256 wrmsrl(MSR_AMD64_SMCA_MCx_DESTAT(b), i_mce.status);
257 wrmsrl(MSR_AMD64_SMCA_MCx_DEADDR(b), i_mce.addr);
259 wrmsrl(MSR_AMD64_SMCA_MCx_STATUS(b), i_mce.status);
260 wrmsrl(MSR_AMD64_SMCA_MCx_ADDR(b), i_mce.addr);
263 wrmsrl(MSR_AMD64_SMCA_MCx_MISC(b), i_mce.misc);
264 wrmsrl(MSR_AMD64_SMCA_MCx_SYND(b), i_mce.synd);
266 wrmsrl(MSR_IA32_MCx_STATUS(b), i_mce.status);
267 wrmsrl(MSR_IA32_MCx_ADDR(b), i_mce.addr);
268 wrmsrl(MSR_IA32_MCx_MISC(b), i_mce.misc);
273 static void do_inject(void)
276 unsigned int cpu = i_mce.extcpu;
280 i_mce.status |= MCI_STATUS_MISCV;
283 i_mce.status |= MCI_STATUS_SYNDV;
285 if (inj_type == SW_INJ) {
286 mce_inject_log(&i_mce);
290 /* prep MCE global settings for the injection */
291 mcg_status = MCG_STATUS_MCIP | MCG_STATUS_EIPV;
293 if (!(i_mce.status & MCI_STATUS_PCC))
294 mcg_status |= MCG_STATUS_RIPV;
297 * Ensure necessary status bits for deferred errors:
298 * - MCx_STATUS[Deferred]: make sure it is a deferred error
299 * - MCx_STATUS[UC] cleared: deferred errors are _not_ UC
301 if (inj_type == DFR_INT_INJ) {
302 i_mce.status |= MCI_STATUS_DEFERRED;
303 i_mce.status |= (i_mce.status & ~MCI_STATUS_UC);
307 * For multi node CPUs, logging and reporting of bank 4 errors happens
308 * only on the node base core. Refer to D18F3x44[NbMcaToMstCpuEn] for
309 * Fam10h and later BKDGs.
311 if (static_cpu_has(X86_FEATURE_AMD_DCM) &&
313 boot_cpu_data.x86 < 0x17) {
314 toggle_nb_mca_mst_cpu(amd_get_nb_id(cpu));
315 cpu = get_nbc_for_node(amd_get_nb_id(cpu));
319 if (!cpu_online(cpu))
322 toggle_hw_mce_inject(cpu, true);
324 i_mce.mcgstatus = mcg_status;
325 i_mce.inject_flags = inj_type;
326 smp_call_function_single(cpu, prepare_msrs, &i_mce, 0);
328 toggle_hw_mce_inject(cpu, false);
332 smp_call_function_single(cpu, trigger_dfr_int, NULL, 0);
335 smp_call_function_single(cpu, trigger_thr_int, NULL, 0);
338 smp_call_function_single(cpu, trigger_mce, NULL, 0);
347 * This denotes into which bank we're injecting and triggers
348 * the injection, at the same time.
350 static int inj_bank_set(void *data, u64 val)
352 struct mce *m = (struct mce *)data;
354 if (val >= n_banks) {
355 pr_err("Non-existent MCE bank: %llu\n", val);
365 MCE_INJECT_GET(bank);
367 DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n");
369 static const char readme_msg[] =
370 "Description of the files and their usages:\n"
372 "Note1: i refers to the bank number below.\n"
373 "Note2: See respective BKDGs for the exact bit definitions of the files below\n"
374 "as they mirror the hardware registers.\n"
376 "status:\t Set MCi_STATUS: the bits in that MSR control the error type and\n"
377 "\t attributes of the error which caused the MCE.\n"
379 "misc:\t Set MCi_MISC: provide auxiliary info about the error. It is mostly\n"
380 "\t used for error thresholding purposes and its validity is indicated by\n"
381 "\t MCi_STATUS[MiscV].\n"
383 "synd:\t Set MCi_SYND: provide syndrome info about the error. Only valid on\n"
384 "\t Scalable MCA systems, and its validity is indicated by MCi_STATUS[SyndV].\n"
386 "addr:\t Error address value to be written to MCi_ADDR. Log address information\n"
387 "\t associated with the error.\n"
389 "cpu:\t The CPU to inject the error on.\n"
391 "bank:\t Specify the bank you want to inject the error into: the number of\n"
392 "\t banks in a processor varies and is family/model-specific, therefore, the\n"
393 "\t supplied value is sanity-checked. Setting the bank value also triggers the\n"
396 "flags:\t Injection type to be performed. Writing to this file will trigger a\n"
397 "\t real machine check, an APIC interrupt or invoke the error decoder routines\n"
398 "\t for AMD processors.\n"
400 "\t Allowed error injection types:\n"
401 "\t - \"sw\": Software error injection. Decode error to a human-readable \n"
402 "\t format only. Safe to use.\n"
403 "\t - \"hw\": Hardware error injection. Causes the #MC exception handler to \n"
404 "\t handle the error. Be warned: might cause system panic if MCi_STATUS[PCC] \n"
405 "\t is set. Therefore, consider setting (debugfs_mountpoint)/mce/fake_panic \n"
406 "\t before injecting.\n"
407 "\t - \"df\": Trigger APIC interrupt for Deferred error. Causes deferred \n"
408 "\t error APIC interrupt handler to handle the error if the feature is \n"
409 "\t is present in hardware. \n"
410 "\t - \"th\": Trigger APIC interrupt for Threshold errors. Causes threshold \n"
411 "\t APIC interrupt handler to handle the error. \n"
415 inj_readme_read(struct file *filp, char __user *ubuf,
416 size_t cnt, loff_t *ppos)
418 return simple_read_from_buffer(ubuf, cnt, ppos,
419 readme_msg, strlen(readme_msg));
422 static const struct file_operations readme_fops = {
423 .read = inj_readme_read,
426 static struct dfs_node {
429 const struct file_operations *fops;
432 { .name = "status", .fops = &status_fops, .perm = S_IRUSR | S_IWUSR },
433 { .name = "misc", .fops = &misc_fops, .perm = S_IRUSR | S_IWUSR },
434 { .name = "addr", .fops = &addr_fops, .perm = S_IRUSR | S_IWUSR },
435 { .name = "synd", .fops = &synd_fops, .perm = S_IRUSR | S_IWUSR },
436 { .name = "bank", .fops = &bank_fops, .perm = S_IRUSR | S_IWUSR },
437 { .name = "flags", .fops = &flags_fops, .perm = S_IRUSR | S_IWUSR },
438 { .name = "cpu", .fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
439 { .name = "README", .fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
442 static int __init init_mce_inject(void)
447 rdmsrl(MSR_IA32_MCG_CAP, cap);
448 n_banks = cap & MCG_BANKCNT_MASK;
450 dfs_inj = debugfs_create_dir("mce-inject", NULL);
454 for (i = 0; i < ARRAY_SIZE(dfs_fls); i++) {
455 dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name,
469 debugfs_remove(dfs_fls[i].d);
471 debugfs_remove(dfs_inj);
477 static void __exit exit_mce_inject(void)
481 for (i = 0; i < ARRAY_SIZE(dfs_fls); i++)
482 debugfs_remove(dfs_fls[i].d);
484 memset(&dfs_fls, 0, sizeof(dfs_fls));
486 debugfs_remove(dfs_inj);
489 module_init(init_mce_inject);
490 module_exit(exit_mce_inject);
492 MODULE_LICENSE("GPL");
493 MODULE_AUTHOR("Borislav Petkov <bp@alien8.de>");
494 MODULE_AUTHOR("AMD Inc.");
495 MODULE_DESCRIPTION("MCE injection facility for RAS testing");