Linux 3.12.28
[linux/fpc-iii.git] / arch / x86 / kernel / nmi_selftest.c
blob6d9582ec0324936646da2b537602ae267b6a4a64
1 /*
2 * arch/x86/kernel/nmi-selftest.c
4 * Testsuite for NMI: IPIs
6 * Started by Don Zickus:
7 * (using lib/locking-selftest.c as a guide)
9 * Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
12 #include <linux/smp.h>
13 #include <linux/cpumask.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/percpu.h>
18 #include <asm/apic.h>
19 #include <asm/nmi.h>
21 #define SUCCESS 0
22 #define FAILURE 1
23 #define TIMEOUT 2
25 static int __initdata nmi_fail;
27 /* check to see if NMI IPIs work on this machine */
28 static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
30 static int __initdata testcase_total;
31 static int __initdata testcase_successes;
32 static int __initdata expected_testcase_failures;
33 static int __initdata unexpected_testcase_failures;
34 static int __initdata unexpected_testcase_unknowns;
36 static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
38 unexpected_testcase_unknowns++;
39 return NMI_HANDLED;
42 static void __init init_nmi_testsuite(void)
44 /* trap all the unknown NMIs we may generate */
45 register_nmi_handler(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk",
46 __initdata);
49 static void __init cleanup_nmi_testsuite(void)
51 unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
54 static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
56 int cpu = raw_smp_processor_id();
58 if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
59 return NMI_HANDLED;
61 return NMI_DONE;
64 static void __init test_nmi_ipi(struct cpumask *mask)
66 unsigned long timeout;
68 if (register_nmi_handler(NMI_LOCAL, test_nmi_ipi_callback,
69 NMI_FLAG_FIRST, "nmi_selftest", __initdata)) {
70 nmi_fail = FAILURE;
71 return;
74 /* sync above data before sending NMI */
75 wmb();
77 apic->send_IPI_mask(mask, NMI_VECTOR);
79 /* Don't wait longer than a second */
80 timeout = USEC_PER_SEC;
81 while (!cpumask_empty(mask) && timeout--)
82 udelay(1);
84 /* What happens if we timeout, do we still unregister?? */
85 unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");
87 if (!timeout)
88 nmi_fail = TIMEOUT;
89 return;
92 static void __init remote_ipi(void)
94 cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
95 cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
96 if (!cpumask_empty(to_cpumask(nmi_ipi_mask)))
97 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
100 static void __init local_ipi(void)
102 cpumask_clear(to_cpumask(nmi_ipi_mask));
103 cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
104 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
107 static void __init reset_nmi(void)
109 nmi_fail = 0;
112 static void __init dotest(void (*testcase_fn)(void), int expected)
114 testcase_fn();
116 * Filter out expected failures:
118 if (nmi_fail != expected) {
119 unexpected_testcase_failures++;
121 if (nmi_fail == FAILURE)
122 printk(KERN_CONT "FAILED |");
123 else if (nmi_fail == TIMEOUT)
124 printk(KERN_CONT "TIMEOUT|");
125 else
126 printk(KERN_CONT "ERROR |");
127 dump_stack();
128 } else {
129 testcase_successes++;
130 printk(KERN_CONT " ok |");
132 testcase_total++;
134 reset_nmi();
137 static inline void __init print_testname(const char *testname)
139 printk("%12s:", testname);
142 void __init nmi_selftest(void)
144 init_nmi_testsuite();
147 * Run the testsuite:
149 printk("----------------\n");
150 printk("| NMI testsuite:\n");
151 printk("--------------------\n");
153 print_testname("remote IPI");
154 dotest(remote_ipi, SUCCESS);
155 printk(KERN_CONT "\n");
156 print_testname("local IPI");
157 dotest(local_ipi, SUCCESS);
158 printk(KERN_CONT "\n");
160 cleanup_nmi_testsuite();
162 if (unexpected_testcase_failures) {
163 printk("--------------------\n");
164 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
165 unexpected_testcase_failures, testcase_total);
166 printk("-----------------------------------------------------------------\n");
167 } else if (expected_testcase_failures && testcase_successes) {
168 printk("--------------------\n");
169 printk("%3d out of %3d testcases failed, as expected. |\n",
170 expected_testcase_failures, testcase_total);
171 printk("----------------------------------------------------\n");
172 } else if (expected_testcase_failures && !testcase_successes) {
173 printk("--------------------\n");
174 printk("All %3d testcases failed, as expected. |\n",
175 expected_testcase_failures);
176 printk("----------------------------------------\n");
177 } else {
178 printk("--------------------\n");
179 printk("Good, all %3d testcases passed! |\n",
180 testcase_successes);
181 printk("---------------------------------\n");