blob: 2579fbd6b95c967efdf4d081bee5078d98bc6940 [file] [log] [blame]
Mike Travis1e019422013-09-23 16:25:00 -05001/*
2 * SGI NMI support routines
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright (c) 2009-2013 Silicon Graphics, Inc. All Rights Reserved.
19 * Copyright (c) Mike Travis
20 */
21
22#include <linux/cpu.h>
Mike Travis0d12ef02013-09-23 16:25:01 -050023#include <linux/delay.h>
Mike Travis12ba6c92013-09-23 16:25:03 -050024#include <linux/kexec.h>
Mike Travis0d12ef02013-09-23 16:25:01 -050025#include <linux/module.h>
Mike Travis1e019422013-09-23 16:25:00 -050026#include <linux/nmi.h>
Mike Travis0d12ef02013-09-23 16:25:01 -050027#include <linux/sched.h>
28#include <linux/slab.h>
Mike Travis1e019422013-09-23 16:25:00 -050029
30#include <asm/apic.h>
Mike Travis0d12ef02013-09-23 16:25:01 -050031#include <asm/current.h>
32#include <asm/kdebug.h>
33#include <asm/local64.h>
Mike Travis1e019422013-09-23 16:25:00 -050034#include <asm/nmi.h>
35#include <asm/uv/uv.h>
36#include <asm/uv/uv_hub.h>
37#include <asm/uv/uv_mmrs.h>
38
Mike Travis0d12ef02013-09-23 16:25:01 -050039/*
40 * UV handler for NMI
41 *
42 * Handle system-wide NMI events generated by the global 'power nmi' command.
43 *
44 * Basic operation is to field the NMI interrupt on each cpu and wait
45 * until all cpus have arrived into the nmi handler. If some cpus do not
46 * make it into the handler, try and force them in with the IPI(NMI) signal.
47 *
48 * We also have to lessen UV Hub MMR accesses as much as possible as this
49 * disrupts the UV Hub's primary mission of directing NumaLink traffic and
50 * can cause system problems to occur.
51 *
52 * To do this we register our primary NMI notifier on the NMI_UNKNOWN
53 * chain. This reduces the number of false NMI calls when the perf
54 * tools are running which generate an enormous number of NMIs per
55 * second (~4M/s for 1024 cpu threads). Our secondary NMI handler is
56 * very short as it only checks that if it has been "pinged" with the
57 * IPI(NMI) signal as mentioned above, and does not read the UV Hub's MMR.
58 *
59 */
60
61static struct uv_hub_nmi_s **uv_hub_nmi_list;
62
63DEFINE_PER_CPU(struct uv_cpu_nmi_s, __uv_cpu_nmi);
64EXPORT_PER_CPU_SYMBOL_GPL(__uv_cpu_nmi);
65
66static unsigned long nmi_mmr;
67static unsigned long nmi_mmr_clear;
68static unsigned long nmi_mmr_pending;
69
70static atomic_t uv_in_nmi;
71static atomic_t uv_nmi_cpu = ATOMIC_INIT(-1);
72static atomic_t uv_nmi_cpus_in_nmi = ATOMIC_INIT(-1);
73static atomic_t uv_nmi_slave_continue;
Mike Travis12ba6c92013-09-23 16:25:03 -050074static atomic_t uv_nmi_kexec_failed;
Mike Travis0d12ef02013-09-23 16:25:01 -050075static cpumask_var_t uv_nmi_cpu_mask;
76
77/* Values for uv_nmi_slave_continue */
78#define SLAVE_CLEAR 0
79#define SLAVE_CONTINUE 1
80#define SLAVE_EXIT 2
Mike Travis1e019422013-09-23 16:25:00 -050081
82/*
Mike Travis0d12ef02013-09-23 16:25:01 -050083 * Default is all stack dumps go to the console and buffer.
84 * Lower level to send to log buffer only.
85 */
86static int uv_nmi_loglevel = 7;
87module_param_named(dump_loglevel, uv_nmi_loglevel, int, 0644);
88
89/*
90 * The following values show statistics on how perf events are affecting
91 * this system.
92 */
93static int param_get_local64(char *buffer, const struct kernel_param *kp)
94{
95 return sprintf(buffer, "%lu\n", local64_read((local64_t *)kp->arg));
96}
97
98static int param_set_local64(const char *val, const struct kernel_param *kp)
99{
100 /* clear on any write */
101 local64_set((local64_t *)kp->arg, 0);
102 return 0;
103}
104
105static struct kernel_param_ops param_ops_local64 = {
106 .get = param_get_local64,
107 .set = param_set_local64,
108};
109#define param_check_local64(name, p) __param_check(name, p, local64_t)
110
111static local64_t uv_nmi_count;
112module_param_named(nmi_count, uv_nmi_count, local64, 0644);
113
114static local64_t uv_nmi_misses;
115module_param_named(nmi_misses, uv_nmi_misses, local64, 0644);
116
117static local64_t uv_nmi_ping_count;
118module_param_named(ping_count, uv_nmi_ping_count, local64, 0644);
119
120static local64_t uv_nmi_ping_misses;
121module_param_named(ping_misses, uv_nmi_ping_misses, local64, 0644);
122
123/*
124 * Following values allow tuning for large systems under heavy loading
125 */
126static int uv_nmi_initial_delay = 100;
127module_param_named(initial_delay, uv_nmi_initial_delay, int, 0644);
128
129static int uv_nmi_slave_delay = 100;
130module_param_named(slave_delay, uv_nmi_slave_delay, int, 0644);
131
132static int uv_nmi_loop_delay = 100;
133module_param_named(loop_delay, uv_nmi_loop_delay, int, 0644);
134
135static int uv_nmi_trigger_delay = 10000;
136module_param_named(trigger_delay, uv_nmi_trigger_delay, int, 0644);
137
138static int uv_nmi_wait_count = 100;
139module_param_named(wait_count, uv_nmi_wait_count, int, 0644);
140
141static int uv_nmi_retry_count = 500;
142module_param_named(retry_count, uv_nmi_retry_count, int, 0644);
143
Mike Travis3c121d92013-09-23 16:25:02 -0500144/*
145 * Valid NMI Actions:
146 * "dump" - dump process stack for each cpu
147 * "ips" - dump IP info for each cpu
Mike Travis12ba6c92013-09-23 16:25:03 -0500148 * "kdump" - do crash dump
Mike Travis3c121d92013-09-23 16:25:02 -0500149 */
150static char uv_nmi_action[8] = "dump";
151module_param_string(action, uv_nmi_action, sizeof(uv_nmi_action), 0644);
152
153static inline bool uv_nmi_action_is(const char *action)
154{
155 return (strncmp(uv_nmi_action, action, strlen(action)) == 0);
156}
157
Mike Travis0d12ef02013-09-23 16:25:01 -0500158/* Setup which NMI support is present in system */
159static void uv_nmi_setup_mmrs(void)
160{
161 if (uv_read_local_mmr(UVH_NMI_MMRX_SUPPORTED)) {
162 uv_write_local_mmr(UVH_NMI_MMRX_REQ,
163 1UL << UVH_NMI_MMRX_REQ_SHIFT);
164 nmi_mmr = UVH_NMI_MMRX;
165 nmi_mmr_clear = UVH_NMI_MMRX_CLEAR;
166 nmi_mmr_pending = 1UL << UVH_NMI_MMRX_SHIFT;
167 pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMRX_TYPE);
168 } else {
169 nmi_mmr = UVH_NMI_MMR;
170 nmi_mmr_clear = UVH_NMI_MMR_CLEAR;
171 nmi_mmr_pending = 1UL << UVH_NMI_MMR_SHIFT;
172 pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMR_TYPE);
173 }
174}
175
176/* Read NMI MMR and check if NMI flag was set by BMC. */
177static inline int uv_nmi_test_mmr(struct uv_hub_nmi_s *hub_nmi)
178{
179 hub_nmi->nmi_value = uv_read_local_mmr(nmi_mmr);
180 atomic_inc(&hub_nmi->read_mmr_count);
181 return !!(hub_nmi->nmi_value & nmi_mmr_pending);
182}
183
184static inline void uv_local_mmr_clear_nmi(void)
185{
186 uv_write_local_mmr(nmi_mmr_clear, nmi_mmr_pending);
187}
188
189/*
190 * If first cpu in on this hub, set hub_nmi "in_nmi" and "owner" values and
191 * return true. If first cpu in on the system, set global "in_nmi" flag.
192 */
193static int uv_set_in_nmi(int cpu, struct uv_hub_nmi_s *hub_nmi)
194{
195 int first = atomic_add_unless(&hub_nmi->in_nmi, 1, 1);
196
197 if (first) {
198 atomic_set(&hub_nmi->cpu_owner, cpu);
199 if (atomic_add_unless(&uv_in_nmi, 1, 1))
200 atomic_set(&uv_nmi_cpu, cpu);
201
202 atomic_inc(&hub_nmi->nmi_count);
203 }
204 return first;
205}
206
207/* Check if this is a system NMI event */
208static int uv_check_nmi(struct uv_hub_nmi_s *hub_nmi)
209{
210 int cpu = smp_processor_id();
211 int nmi = 0;
212
213 local64_inc(&uv_nmi_count);
214 uv_cpu_nmi.queries++;
215
216 do {
217 nmi = atomic_read(&hub_nmi->in_nmi);
218 if (nmi)
219 break;
220
221 if (raw_spin_trylock(&hub_nmi->nmi_lock)) {
222
223 /* check hub MMR NMI flag */
224 if (uv_nmi_test_mmr(hub_nmi)) {
225 uv_set_in_nmi(cpu, hub_nmi);
226 nmi = 1;
227 break;
228 }
229
230 /* MMR NMI flag is clear */
231 raw_spin_unlock(&hub_nmi->nmi_lock);
232
233 } else {
234 /* wait a moment for the hub nmi locker to set flag */
235 cpu_relax();
236 udelay(uv_nmi_slave_delay);
237
238 /* re-check hub in_nmi flag */
239 nmi = atomic_read(&hub_nmi->in_nmi);
240 if (nmi)
241 break;
242 }
243
244 /* check if this BMC missed setting the MMR NMI flag */
245 if (!nmi) {
246 nmi = atomic_read(&uv_in_nmi);
247 if (nmi)
248 uv_set_in_nmi(cpu, hub_nmi);
249 }
250
251 } while (0);
252
253 if (!nmi)
254 local64_inc(&uv_nmi_misses);
255
256 return nmi;
257}
258
259/* Need to reset the NMI MMR register, but only once per hub. */
260static inline void uv_clear_nmi(int cpu)
261{
262 struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
263
264 if (cpu == atomic_read(&hub_nmi->cpu_owner)) {
265 atomic_set(&hub_nmi->cpu_owner, -1);
266 atomic_set(&hub_nmi->in_nmi, 0);
267 uv_local_mmr_clear_nmi();
268 raw_spin_unlock(&hub_nmi->nmi_lock);
269 }
270}
271
272/* Print non-responding cpus */
273static void uv_nmi_nr_cpus_pr(char *fmt)
274{
275 static char cpu_list[1024];
276 int len = sizeof(cpu_list);
277 int c = cpumask_weight(uv_nmi_cpu_mask);
278 int n = cpulist_scnprintf(cpu_list, len, uv_nmi_cpu_mask);
279
280 if (n >= len-1)
281 strcpy(&cpu_list[len - 6], "...\n");
282
283 printk(fmt, c, cpu_list);
284}
285
286/* Ping non-responding cpus attemping to force them into the NMI handler */
287static void uv_nmi_nr_cpus_ping(void)
288{
289 int cpu;
290
291 for_each_cpu(cpu, uv_nmi_cpu_mask)
292 atomic_set(&uv_cpu_nmi_per(cpu).pinging, 1);
293
294 apic->send_IPI_mask(uv_nmi_cpu_mask, APIC_DM_NMI);
295}
296
297/* Clean up flags for cpus that ignored both NMI and ping */
298static void uv_nmi_cleanup_mask(void)
299{
300 int cpu;
301
302 for_each_cpu(cpu, uv_nmi_cpu_mask) {
303 atomic_set(&uv_cpu_nmi_per(cpu).pinging, 0);
304 atomic_set(&uv_cpu_nmi_per(cpu).state, UV_NMI_STATE_OUT);
305 cpumask_clear_cpu(cpu, uv_nmi_cpu_mask);
306 }
307}
308
309/* Loop waiting as cpus enter nmi handler */
310static int uv_nmi_wait_cpus(int first)
311{
312 int i, j, k, n = num_online_cpus();
313 int last_k = 0, waiting = 0;
314
315 if (first) {
316 cpumask_copy(uv_nmi_cpu_mask, cpu_online_mask);
317 k = 0;
318 } else {
319 k = n - cpumask_weight(uv_nmi_cpu_mask);
320 }
321
322 udelay(uv_nmi_initial_delay);
323 for (i = 0; i < uv_nmi_retry_count; i++) {
324 int loop_delay = uv_nmi_loop_delay;
325
326 for_each_cpu(j, uv_nmi_cpu_mask) {
327 if (atomic_read(&uv_cpu_nmi_per(j).state)) {
328 cpumask_clear_cpu(j, uv_nmi_cpu_mask);
329 if (++k >= n)
330 break;
331 }
332 }
333 if (k >= n) { /* all in? */
334 k = n;
335 break;
336 }
337 if (last_k != k) { /* abort if no new cpus coming in */
338 last_k = k;
339 waiting = 0;
340 } else if (++waiting > uv_nmi_wait_count)
341 break;
342
343 /* extend delay if waiting only for cpu 0 */
344 if (waiting && (n - k) == 1 &&
345 cpumask_test_cpu(0, uv_nmi_cpu_mask))
346 loop_delay *= 100;
347
348 udelay(loop_delay);
349 }
350 atomic_set(&uv_nmi_cpus_in_nmi, k);
351 return n - k;
352}
353
354/* Wait until all slave cpus have entered UV NMI handler */
355static void uv_nmi_wait(int master)
356{
357 /* indicate this cpu is in */
358 atomic_set(&uv_cpu_nmi.state, UV_NMI_STATE_IN);
359
360 /* if not the first cpu in (the master), then we are a slave cpu */
361 if (!master)
362 return;
363
364 do {
365 /* wait for all other cpus to gather here */
366 if (!uv_nmi_wait_cpus(1))
367 break;
368
369 /* if not all made it in, send IPI NMI to them */
370 uv_nmi_nr_cpus_pr(KERN_ALERT
371 "UV: Sending NMI IPI to %d non-responding CPUs: %s\n");
372 uv_nmi_nr_cpus_ping();
373
374 /* if all cpus are in, then done */
375 if (!uv_nmi_wait_cpus(0))
376 break;
377
378 uv_nmi_nr_cpus_pr(KERN_ALERT
379 "UV: %d CPUs not in NMI loop: %s\n");
380 } while (0);
381
382 pr_alert("UV: %d of %d CPUs in NMI\n",
383 atomic_read(&uv_nmi_cpus_in_nmi), num_online_cpus());
384}
385
Mike Travis3c121d92013-09-23 16:25:02 -0500386static void uv_nmi_dump_cpu_ip_hdr(void)
387{
388 printk(KERN_DEFAULT
389 "\nUV: %4s %6s %-32s %s (Note: PID 0 not listed)\n",
390 "CPU", "PID", "COMMAND", "IP");
391}
392
393static void uv_nmi_dump_cpu_ip(int cpu, struct pt_regs *regs)
394{
395 printk(KERN_DEFAULT "UV: %4d %6d %-32.32s ",
396 cpu, current->pid, current->comm);
397
398 printk_address(regs->ip, 1);
399}
400
Mike Travis0d12ef02013-09-23 16:25:01 -0500401/* Dump this cpu's state */
402static void uv_nmi_dump_state_cpu(int cpu, struct pt_regs *regs)
403{
404 const char *dots = " ................................. ";
405
Mike Travis3c121d92013-09-23 16:25:02 -0500406 if (uv_nmi_action_is("ips")) {
407 if (cpu == 0)
408 uv_nmi_dump_cpu_ip_hdr();
409
410 if (current->pid != 0)
411 uv_nmi_dump_cpu_ip(cpu, regs);
412
413 } else if (uv_nmi_action_is("dump")) {
414 printk(KERN_DEFAULT
415 "UV:%sNMI process trace for CPU %d\n", dots, cpu);
416 show_regs(regs);
417 }
Mike Travis0d12ef02013-09-23 16:25:01 -0500418 atomic_set(&uv_cpu_nmi.state, UV_NMI_STATE_DUMP_DONE);
419}
420
421/* Trigger a slave cpu to dump it's state */
422static void uv_nmi_trigger_dump(int cpu)
423{
424 int retry = uv_nmi_trigger_delay;
425
426 if (atomic_read(&uv_cpu_nmi_per(cpu).state) != UV_NMI_STATE_IN)
427 return;
428
429 atomic_set(&uv_cpu_nmi_per(cpu).state, UV_NMI_STATE_DUMP);
430 do {
431 cpu_relax();
432 udelay(10);
433 if (atomic_read(&uv_cpu_nmi_per(cpu).state)
434 != UV_NMI_STATE_DUMP)
435 return;
436 } while (--retry > 0);
437
438 pr_crit("UV: CPU %d stuck in process dump function\n", cpu);
439 atomic_set(&uv_cpu_nmi_per(cpu).state, UV_NMI_STATE_DUMP_DONE);
440}
441
442/* Wait until all cpus ready to exit */
443static void uv_nmi_sync_exit(int master)
444{
445 atomic_dec(&uv_nmi_cpus_in_nmi);
446 if (master) {
447 while (atomic_read(&uv_nmi_cpus_in_nmi) > 0)
448 cpu_relax();
449 atomic_set(&uv_nmi_slave_continue, SLAVE_CLEAR);
450 } else {
451 while (atomic_read(&uv_nmi_slave_continue))
452 cpu_relax();
453 }
454}
455
456/* Walk through cpu list and dump state of each */
457static void uv_nmi_dump_state(int cpu, struct pt_regs *regs, int master)
458{
459 if (master) {
460 int tcpu;
461 int ignored = 0;
462 int saved_console_loglevel = console_loglevel;
463
Mike Travis3c121d92013-09-23 16:25:02 -0500464 pr_alert("UV: tracing %s for %d CPUs from CPU %d\n",
465 uv_nmi_action_is("ips") ? "IPs" : "processes",
Mike Travis0d12ef02013-09-23 16:25:01 -0500466 atomic_read(&uv_nmi_cpus_in_nmi), cpu);
467
468 console_loglevel = uv_nmi_loglevel;
469 atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
470 for_each_online_cpu(tcpu) {
471 if (cpumask_test_cpu(tcpu, uv_nmi_cpu_mask))
472 ignored++;
473 else if (tcpu == cpu)
474 uv_nmi_dump_state_cpu(tcpu, regs);
475 else
476 uv_nmi_trigger_dump(tcpu);
477 }
478 if (ignored)
479 printk(KERN_DEFAULT "UV: %d CPUs ignored NMI\n",
480 ignored);
481
482 console_loglevel = saved_console_loglevel;
483 pr_alert("UV: process trace complete\n");
484 } else {
485 while (!atomic_read(&uv_nmi_slave_continue))
486 cpu_relax();
487 while (atomic_read(&uv_cpu_nmi.state) != UV_NMI_STATE_DUMP)
488 cpu_relax();
489 uv_nmi_dump_state_cpu(cpu, regs);
490 }
491 uv_nmi_sync_exit(master);
492}
493
494static void uv_nmi_touch_watchdogs(void)
495{
496 touch_softlockup_watchdog_sync();
497 clocksource_touch_watchdog();
498 rcu_cpu_stall_reset();
499 touch_nmi_watchdog();
500}
501
Mike Travis12ba6c92013-09-23 16:25:03 -0500502#if defined(CONFIG_KEXEC)
503static void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
504{
505 /* Call crash to dump system state */
506 if (master) {
507 pr_emerg("UV: NMI executing crash_kexec on CPU%d\n", cpu);
508 crash_kexec(regs);
509
510 pr_emerg("UV: crash_kexec unexpectedly returned, ");
511 if (!kexec_crash_image) {
512 pr_cont("crash kernel not loaded\n");
513 atomic_set(&uv_nmi_kexec_failed, 1);
514 uv_nmi_sync_exit(1);
515 return;
516 }
517 pr_cont("kexec busy, stalling cpus while waiting\n");
518 }
519
520 /* If crash exec fails the slaves should return, otherwise stall */
521 while (atomic_read(&uv_nmi_kexec_failed) == 0)
522 mdelay(10);
523
524 /* Crash kernel most likely not loaded, return in an orderly fashion */
525 uv_nmi_sync_exit(0);
526}
527
528#else /* !CONFIG_KEXEC */
529static inline void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
530{
531 if (master)
532 pr_err("UV: NMI kdump: KEXEC not supported in this kernel\n");
533}
534#endif /* !CONFIG_KEXEC */
535
Mike Travis0d12ef02013-09-23 16:25:01 -0500536/*
537 * UV NMI handler
Mike Travis1e019422013-09-23 16:25:00 -0500538 */
539int uv_handle_nmi(unsigned int reason, struct pt_regs *regs)
540{
Mike Travis0d12ef02013-09-23 16:25:01 -0500541 struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
542 int cpu = smp_processor_id();
543 int master = 0;
544 unsigned long flags;
Mike Travis1e019422013-09-23 16:25:00 -0500545
Mike Travis0d12ef02013-09-23 16:25:01 -0500546 local_irq_save(flags);
Mike Travis1e019422013-09-23 16:25:00 -0500547
Mike Travis0d12ef02013-09-23 16:25:01 -0500548 /* If not a UV System NMI, ignore */
549 if (!atomic_read(&uv_cpu_nmi.pinging) && !uv_check_nmi(hub_nmi)) {
550 local_irq_restore(flags);
551 return NMI_DONE;
Mike Travis1e019422013-09-23 16:25:00 -0500552 }
553
Mike Travis0d12ef02013-09-23 16:25:01 -0500554 /* Indicate we are the first CPU into the NMI handler */
555 master = (atomic_read(&uv_nmi_cpu) == cpu);
Mike Travis1e019422013-09-23 16:25:00 -0500556
Mike Travis12ba6c92013-09-23 16:25:03 -0500557 /* If NMI action is "kdump", then attempt to do it */
558 if (uv_nmi_action_is("kdump"))
559 uv_nmi_kdump(cpu, master, regs);
560
Mike Travis0d12ef02013-09-23 16:25:01 -0500561 /* Pause as all cpus enter the NMI handler */
562 uv_nmi_wait(master);
Mike Travis1e019422013-09-23 16:25:00 -0500563
Mike Travis0d12ef02013-09-23 16:25:01 -0500564 /* Dump state of each cpu */
Mike Travis3c121d92013-09-23 16:25:02 -0500565 if (uv_nmi_action_is("ips") || uv_nmi_action_is("dump"))
566 uv_nmi_dump_state(cpu, regs, master);
Mike Travis0d12ef02013-09-23 16:25:01 -0500567
568 /* Clear per_cpu "in nmi" flag */
569 atomic_set(&uv_cpu_nmi.state, UV_NMI_STATE_OUT);
570
571 /* Clear MMR NMI flag on each hub */
572 uv_clear_nmi(cpu);
573
574 /* Clear global flags */
575 if (master) {
576 if (cpumask_weight(uv_nmi_cpu_mask))
577 uv_nmi_cleanup_mask();
578 atomic_set(&uv_nmi_cpus_in_nmi, -1);
579 atomic_set(&uv_nmi_cpu, -1);
580 atomic_set(&uv_in_nmi, 0);
581 }
582
583 uv_nmi_touch_watchdogs();
584 local_irq_restore(flags);
Mike Travis1e019422013-09-23 16:25:00 -0500585
586 return NMI_HANDLED;
587}
588
Mike Travis0d12ef02013-09-23 16:25:01 -0500589/*
590 * NMI handler for pulling in CPUs when perf events are grabbing our NMI
591 */
592int uv_handle_nmi_ping(unsigned int reason, struct pt_regs *regs)
593{
594 int ret;
595
596 uv_cpu_nmi.queries++;
597 if (!atomic_read(&uv_cpu_nmi.pinging)) {
598 local64_inc(&uv_nmi_ping_misses);
599 return NMI_DONE;
600 }
601
602 uv_cpu_nmi.pings++;
603 local64_inc(&uv_nmi_ping_count);
604 ret = uv_handle_nmi(reason, regs);
605 atomic_set(&uv_cpu_nmi.pinging, 0);
606 return ret;
607}
608
Mike Travis1e019422013-09-23 16:25:00 -0500609void uv_register_nmi_notifier(void)
610{
611 if (register_nmi_handler(NMI_UNKNOWN, uv_handle_nmi, 0, "uv"))
Mike Travis0d12ef02013-09-23 16:25:01 -0500612 pr_warn("UV: NMI handler failed to register\n");
613
614 if (register_nmi_handler(NMI_LOCAL, uv_handle_nmi_ping, 0, "uvping"))
615 pr_warn("UV: PING NMI handler failed to register\n");
Mike Travis1e019422013-09-23 16:25:00 -0500616}
617
618void uv_nmi_init(void)
619{
620 unsigned int value;
621
622 /*
623 * Unmask NMI on all cpus
624 */
625 value = apic_read(APIC_LVT1) | APIC_DM_NMI;
626 value &= ~APIC_LVT_MASKED;
627 apic_write(APIC_LVT1, value);
628}
629
Mike Travis0d12ef02013-09-23 16:25:01 -0500630void uv_nmi_setup(void)
631{
632 int size = sizeof(void *) * (1 << NODES_SHIFT);
633 int cpu, nid;
634
635 /* Setup hub nmi info */
636 uv_nmi_setup_mmrs();
637 uv_hub_nmi_list = kzalloc(size, GFP_KERNEL);
638 pr_info("UV: NMI hub list @ 0x%p (%d)\n", uv_hub_nmi_list, size);
639 BUG_ON(!uv_hub_nmi_list);
640 size = sizeof(struct uv_hub_nmi_s);
641 for_each_present_cpu(cpu) {
642 nid = cpu_to_node(cpu);
643 if (uv_hub_nmi_list[nid] == NULL) {
644 uv_hub_nmi_list[nid] = kzalloc_node(size,
645 GFP_KERNEL, nid);
646 BUG_ON(!uv_hub_nmi_list[nid]);
647 raw_spin_lock_init(&(uv_hub_nmi_list[nid]->nmi_lock));
648 atomic_set(&uv_hub_nmi_list[nid]->cpu_owner, -1);
649 }
650 uv_hub_nmi_per(cpu) = uv_hub_nmi_list[nid];
651 }
652 alloc_cpumask_var(&uv_nmi_cpu_mask, GFP_KERNEL);
653 BUG_ON(!uv_nmi_cpu_mask);
654}
655
656