blob: 9f11dd3b657738a4422c3005ae9f6bcbf4520b74 [file] [log] [blame]
Don Zickus99e8b9c2011-10-13 15:14:26 -04001/*
2 * arch/x86/kernel/nmi-selftest.c
3 *
4 * Testsuite for NMI: IPIs
5 *
6 * Started by Don Zickus:
7 * (using lib/locking-selftest.c as a guide)
8 *
9 * Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
10 */
11
12#include <linux/smp.h>
13#include <linux/cpumask.h>
14#include <linux/delay.h>
Jan Beulichf0ba6622012-02-24 12:09:21 +000015#include <linux/init.h>
Don Zickus99e8b9c2011-10-13 15:14:26 -040016
17#include <asm/apic.h>
18#include <asm/nmi.h>
19
20#define SUCCESS 0
21#define FAILURE 1
22#define TIMEOUT 2
23
Jan Beulichf0ba6622012-02-24 12:09:21 +000024static int __initdata nmi_fail;
Don Zickus99e8b9c2011-10-13 15:14:26 -040025
26/* check to see if NMI IPIs work on this machine */
Jan Beulichf0ba6622012-02-24 12:09:21 +000027static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
Don Zickus99e8b9c2011-10-13 15:14:26 -040028
Jan Beulichf0ba6622012-02-24 12:09:21 +000029static int __initdata testcase_total;
30static int __initdata testcase_successes;
31static int __initdata expected_testcase_failures;
32static int __initdata unexpected_testcase_failures;
33static int __initdata unexpected_testcase_unknowns;
Don Zickus99e8b9c2011-10-13 15:14:26 -040034
Jan Beulichf0ba6622012-02-24 12:09:21 +000035static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
Don Zickus99e8b9c2011-10-13 15:14:26 -040036{
37 unexpected_testcase_unknowns++;
38 return NMI_HANDLED;
39}
40
Jan Beulichf0ba6622012-02-24 12:09:21 +000041static void __init init_nmi_testsuite(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040042{
43 /* trap all the unknown NMIs we may generate */
44 register_nmi_handler(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk");
45}
46
Jan Beulichf0ba6622012-02-24 12:09:21 +000047static void __init cleanup_nmi_testsuite(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040048{
49 unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
50}
51
Jan Beulichf0ba6622012-02-24 12:09:21 +000052static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
Don Zickus99e8b9c2011-10-13 15:14:26 -040053{
54 int cpu = raw_smp_processor_id();
55
56 if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
57 return NMI_HANDLED;
58
59 return NMI_DONE;
60}
61
Jan Beulichf0ba6622012-02-24 12:09:21 +000062static void __init test_nmi_ipi(struct cpumask *mask)
Don Zickus99e8b9c2011-10-13 15:14:26 -040063{
64 unsigned long timeout;
65
66 if (register_nmi_handler(NMI_LOCAL, test_nmi_ipi_callback,
67 NMI_FLAG_FIRST, "nmi_selftest")) {
68 nmi_fail = FAILURE;
69 return;
70 }
71
72 /* sync above data before sending NMI */
73 wmb();
74
75 apic->send_IPI_mask(mask, NMI_VECTOR);
76
77 /* Don't wait longer than a second */
78 timeout = USEC_PER_SEC;
79 while (!cpumask_empty(mask) && timeout--)
80 udelay(1);
81
82 /* What happens if we timeout, do we still unregister?? */
83 unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");
84
85 if (!timeout)
86 nmi_fail = TIMEOUT;
87 return;
88}
89
Jan Beulichf0ba6622012-02-24 12:09:21 +000090static void __init remote_ipi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040091{
92 cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
93 cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
Dan Carpenter3d6240b2011-12-07 14:06:12 +030094 if (!cpumask_empty(to_cpumask(nmi_ipi_mask)))
Don Zickus565cbc32011-12-06 13:08:59 -050095 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
Don Zickus99e8b9c2011-10-13 15:14:26 -040096}
97
Jan Beulichf0ba6622012-02-24 12:09:21 +000098static void __init local_ipi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040099{
100 cpumask_clear(to_cpumask(nmi_ipi_mask));
101 cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
102 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
103}
104
Jan Beulichf0ba6622012-02-24 12:09:21 +0000105static void __init reset_nmi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400106{
107 nmi_fail = 0;
108}
109
Jan Beulichf0ba6622012-02-24 12:09:21 +0000110static void __init dotest(void (*testcase_fn)(void), int expected)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400111{
112 testcase_fn();
113 /*
114 * Filter out expected failures:
115 */
116 if (nmi_fail != expected) {
117 unexpected_testcase_failures++;
118
119 if (nmi_fail == FAILURE)
Sasha Levin29d679f2012-05-08 17:56:12 +0200120 printk(KERN_CONT "FAILED |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400121 else if (nmi_fail == TIMEOUT)
Sasha Levin29d679f2012-05-08 17:56:12 +0200122 printk(KERN_CONT "TIMEOUT|");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400123 else
Sasha Levin29d679f2012-05-08 17:56:12 +0200124 printk(KERN_CONT "ERROR |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400125 dump_stack();
126 } else {
127 testcase_successes++;
Sasha Levin29d679f2012-05-08 17:56:12 +0200128 printk(KERN_CONT " ok |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400129 }
130 testcase_total++;
131
132 reset_nmi();
133}
134
Jan Beulichf0ba6622012-02-24 12:09:21 +0000135static inline void __init print_testname(const char *testname)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400136{
137 printk("%12s:", testname);
138}
139
Jan Beulichf0ba6622012-02-24 12:09:21 +0000140void __init nmi_selftest(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400141{
142 init_nmi_testsuite();
143
144 /*
145 * Run the testsuite:
146 */
147 printk("----------------\n");
148 printk("| NMI testsuite:\n");
149 printk("--------------------\n");
150
151 print_testname("remote IPI");
152 dotest(remote_ipi, SUCCESS);
Sasha Levin29d679f2012-05-08 17:56:12 +0200153 printk(KERN_CONT "\n");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400154 print_testname("local IPI");
155 dotest(local_ipi, SUCCESS);
Sasha Levin29d679f2012-05-08 17:56:12 +0200156 printk(KERN_CONT "\n");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400157
158 cleanup_nmi_testsuite();
159
160 if (unexpected_testcase_failures) {
161 printk("--------------------\n");
162 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
163 unexpected_testcase_failures, testcase_total);
164 printk("-----------------------------------------------------------------\n");
165 } else if (expected_testcase_failures && testcase_successes) {
166 printk("--------------------\n");
167 printk("%3d out of %3d testcases failed, as expected. |\n",
168 expected_testcase_failures, testcase_total);
169 printk("----------------------------------------------------\n");
170 } else if (expected_testcase_failures && !testcase_successes) {
171 printk("--------------------\n");
172 printk("All %3d testcases failed, as expected. |\n",
173 expected_testcase_failures);
174 printk("----------------------------------------\n");
175 } else {
176 printk("--------------------\n");
177 printk("Good, all %3d testcases passed! |\n",
178 testcase_successes);
179 printk("---------------------------------\n");
180 }
181}