uv_nmi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  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. #include <linux/cpu.h>
  22. #include <linux/delay.h>
  23. #include <linux/kdb.h>
  24. #include <linux/kexec.h>
  25. #include <linux/kgdb.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/nmi.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/clocksource.h>
  31. #include <asm/apic.h>
  32. #include <asm/current.h>
  33. #include <asm/kdebug.h>
  34. #include <asm/local64.h>
  35. #include <asm/nmi.h>
  36. #include <asm/traps.h>
  37. #include <asm/uv/uv.h>
  38. #include <asm/uv/uv_hub.h>
  39. #include <asm/uv/uv_mmrs.h>
  40. /*
  41. * UV handler for NMI
  42. *
  43. * Handle system-wide NMI events generated by the global 'power nmi' command.
  44. *
  45. * Basic operation is to field the NMI interrupt on each cpu and wait
  46. * until all cpus have arrived into the nmi handler. If some cpus do not
  47. * make it into the handler, try and force them in with the IPI(NMI) signal.
  48. *
  49. * We also have to lessen UV Hub MMR accesses as much as possible as this
  50. * disrupts the UV Hub's primary mission of directing NumaLink traffic and
  51. * can cause system problems to occur.
  52. *
  53. * To do this we register our primary NMI notifier on the NMI_UNKNOWN
  54. * chain. This reduces the number of false NMI calls when the perf
  55. * tools are running which generate an enormous number of NMIs per
  56. * second (~4M/s for 1024 cpu threads). Our secondary NMI handler is
  57. * very short as it only checks that if it has been "pinged" with the
  58. * IPI(NMI) signal as mentioned above, and does not read the UV Hub's MMR.
  59. *
  60. */
  61. static struct uv_hub_nmi_s **uv_hub_nmi_list;
  62. DEFINE_PER_CPU(struct uv_cpu_nmi_s, uv_cpu_nmi);
  63. EXPORT_PER_CPU_SYMBOL_GPL(uv_cpu_nmi);
  64. static unsigned long nmi_mmr;
  65. static unsigned long nmi_mmr_clear;
  66. static unsigned long nmi_mmr_pending;
  67. static atomic_t uv_in_nmi;
  68. static atomic_t uv_nmi_cpu = ATOMIC_INIT(-1);
  69. static atomic_t uv_nmi_cpus_in_nmi = ATOMIC_INIT(-1);
  70. static atomic_t uv_nmi_slave_continue;
  71. static cpumask_var_t uv_nmi_cpu_mask;
  72. /* Values for uv_nmi_slave_continue */
  73. #define SLAVE_CLEAR 0
  74. #define SLAVE_CONTINUE 1
  75. #define SLAVE_EXIT 2
  76. /*
  77. * Default is all stack dumps go to the console and buffer.
  78. * Lower level to send to log buffer only.
  79. */
  80. static int uv_nmi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  81. module_param_named(dump_loglevel, uv_nmi_loglevel, int, 0644);
  82. /*
  83. * The following values show statistics on how perf events are affecting
  84. * this system.
  85. */
  86. static int param_get_local64(char *buffer, const struct kernel_param *kp)
  87. {
  88. return sprintf(buffer, "%lu\n", local64_read((local64_t *)kp->arg));
  89. }
  90. static int param_set_local64(const char *val, const struct kernel_param *kp)
  91. {
  92. /* clear on any write */
  93. local64_set((local64_t *)kp->arg, 0);
  94. return 0;
  95. }
  96. static const struct kernel_param_ops param_ops_local64 = {
  97. .get = param_get_local64,
  98. .set = param_set_local64,
  99. };
  100. #define param_check_local64(name, p) __param_check(name, p, local64_t)
  101. static local64_t uv_nmi_count;
  102. module_param_named(nmi_count, uv_nmi_count, local64, 0644);
  103. static local64_t uv_nmi_misses;
  104. module_param_named(nmi_misses, uv_nmi_misses, local64, 0644);
  105. static local64_t uv_nmi_ping_count;
  106. module_param_named(ping_count, uv_nmi_ping_count, local64, 0644);
  107. static local64_t uv_nmi_ping_misses;
  108. module_param_named(ping_misses, uv_nmi_ping_misses, local64, 0644);
  109. /*
  110. * Following values allow tuning for large systems under heavy loading
  111. */
  112. static int uv_nmi_initial_delay = 100;
  113. module_param_named(initial_delay, uv_nmi_initial_delay, int, 0644);
  114. static int uv_nmi_slave_delay = 100;
  115. module_param_named(slave_delay, uv_nmi_slave_delay, int, 0644);
  116. static int uv_nmi_loop_delay = 100;
  117. module_param_named(loop_delay, uv_nmi_loop_delay, int, 0644);
  118. static int uv_nmi_trigger_delay = 10000;
  119. module_param_named(trigger_delay, uv_nmi_trigger_delay, int, 0644);
  120. static int uv_nmi_wait_count = 100;
  121. module_param_named(wait_count, uv_nmi_wait_count, int, 0644);
  122. static int uv_nmi_retry_count = 500;
  123. module_param_named(retry_count, uv_nmi_retry_count, int, 0644);
  124. /*
  125. * Valid NMI Actions:
  126. * "dump" - dump process stack for each cpu
  127. * "ips" - dump IP info for each cpu
  128. * "kdump" - do crash dump
  129. * "kdb" - enter KDB (default)
  130. * "kgdb" - enter KGDB
  131. */
  132. static char uv_nmi_action[8] = "kdb";
  133. module_param_string(action, uv_nmi_action, sizeof(uv_nmi_action), 0644);
  134. static inline bool uv_nmi_action_is(const char *action)
  135. {
  136. return (strncmp(uv_nmi_action, action, strlen(action)) == 0);
  137. }
  138. /* Setup which NMI support is present in system */
  139. static void uv_nmi_setup_mmrs(void)
  140. {
  141. if (uv_read_local_mmr(UVH_NMI_MMRX_SUPPORTED)) {
  142. uv_write_local_mmr(UVH_NMI_MMRX_REQ,
  143. 1UL << UVH_NMI_MMRX_REQ_SHIFT);
  144. nmi_mmr = UVH_NMI_MMRX;
  145. nmi_mmr_clear = UVH_NMI_MMRX_CLEAR;
  146. nmi_mmr_pending = 1UL << UVH_NMI_MMRX_SHIFT;
  147. pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMRX_TYPE);
  148. } else {
  149. nmi_mmr = UVH_NMI_MMR;
  150. nmi_mmr_clear = UVH_NMI_MMR_CLEAR;
  151. nmi_mmr_pending = 1UL << UVH_NMI_MMR_SHIFT;
  152. pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMR_TYPE);
  153. }
  154. }
  155. /* Read NMI MMR and check if NMI flag was set by BMC. */
  156. static inline int uv_nmi_test_mmr(struct uv_hub_nmi_s *hub_nmi)
  157. {
  158. hub_nmi->nmi_value = uv_read_local_mmr(nmi_mmr);
  159. atomic_inc(&hub_nmi->read_mmr_count);
  160. return !!(hub_nmi->nmi_value & nmi_mmr_pending);
  161. }
  162. static inline void uv_local_mmr_clear_nmi(void)
  163. {
  164. uv_write_local_mmr(nmi_mmr_clear, nmi_mmr_pending);
  165. }
  166. /*
  167. * If first cpu in on this hub, set hub_nmi "in_nmi" and "owner" values and
  168. * return true. If first cpu in on the system, set global "in_nmi" flag.
  169. */
  170. static int uv_set_in_nmi(int cpu, struct uv_hub_nmi_s *hub_nmi)
  171. {
  172. int first = atomic_add_unless(&hub_nmi->in_nmi, 1, 1);
  173. if (first) {
  174. atomic_set(&hub_nmi->cpu_owner, cpu);
  175. if (atomic_add_unless(&uv_in_nmi, 1, 1))
  176. atomic_set(&uv_nmi_cpu, cpu);
  177. atomic_inc(&hub_nmi->nmi_count);
  178. }
  179. return first;
  180. }
  181. /* Check if this is a system NMI event */
  182. static int uv_check_nmi(struct uv_hub_nmi_s *hub_nmi)
  183. {
  184. int cpu = smp_processor_id();
  185. int nmi = 0;
  186. local64_inc(&uv_nmi_count);
  187. this_cpu_inc(uv_cpu_nmi.queries);
  188. do {
  189. nmi = atomic_read(&hub_nmi->in_nmi);
  190. if (nmi)
  191. break;
  192. if (raw_spin_trylock(&hub_nmi->nmi_lock)) {
  193. /* check hub MMR NMI flag */
  194. if (uv_nmi_test_mmr(hub_nmi)) {
  195. uv_set_in_nmi(cpu, hub_nmi);
  196. nmi = 1;
  197. break;
  198. }
  199. /* MMR NMI flag is clear */
  200. raw_spin_unlock(&hub_nmi->nmi_lock);
  201. } else {
  202. /* wait a moment for the hub nmi locker to set flag */
  203. cpu_relax();
  204. udelay(uv_nmi_slave_delay);
  205. /* re-check hub in_nmi flag */
  206. nmi = atomic_read(&hub_nmi->in_nmi);
  207. if (nmi)
  208. break;
  209. }
  210. /* check if this BMC missed setting the MMR NMI flag */
  211. if (!nmi) {
  212. nmi = atomic_read(&uv_in_nmi);
  213. if (nmi)
  214. uv_set_in_nmi(cpu, hub_nmi);
  215. }
  216. } while (0);
  217. if (!nmi)
  218. local64_inc(&uv_nmi_misses);
  219. return nmi;
  220. }
  221. /* Need to reset the NMI MMR register, but only once per hub. */
  222. static inline void uv_clear_nmi(int cpu)
  223. {
  224. struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
  225. if (cpu == atomic_read(&hub_nmi->cpu_owner)) {
  226. atomic_set(&hub_nmi->cpu_owner, -1);
  227. atomic_set(&hub_nmi->in_nmi, 0);
  228. uv_local_mmr_clear_nmi();
  229. raw_spin_unlock(&hub_nmi->nmi_lock);
  230. }
  231. }
  232. /* Ping non-responding cpus attemping to force them into the NMI handler */
  233. static void uv_nmi_nr_cpus_ping(void)
  234. {
  235. int cpu;
  236. for_each_cpu(cpu, uv_nmi_cpu_mask)
  237. uv_cpu_nmi_per(cpu).pinging = 1;
  238. apic->send_IPI_mask(uv_nmi_cpu_mask, APIC_DM_NMI);
  239. }
  240. /* Clean up flags for cpus that ignored both NMI and ping */
  241. static void uv_nmi_cleanup_mask(void)
  242. {
  243. int cpu;
  244. for_each_cpu(cpu, uv_nmi_cpu_mask) {
  245. uv_cpu_nmi_per(cpu).pinging = 0;
  246. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_OUT;
  247. cpumask_clear_cpu(cpu, uv_nmi_cpu_mask);
  248. }
  249. }
  250. /* Loop waiting as cpus enter nmi handler */
  251. static int uv_nmi_wait_cpus(int first)
  252. {
  253. int i, j, k, n = num_online_cpus();
  254. int last_k = 0, waiting = 0;
  255. if (first) {
  256. cpumask_copy(uv_nmi_cpu_mask, cpu_online_mask);
  257. k = 0;
  258. } else {
  259. k = n - cpumask_weight(uv_nmi_cpu_mask);
  260. }
  261. udelay(uv_nmi_initial_delay);
  262. for (i = 0; i < uv_nmi_retry_count; i++) {
  263. int loop_delay = uv_nmi_loop_delay;
  264. for_each_cpu(j, uv_nmi_cpu_mask) {
  265. if (uv_cpu_nmi_per(j).state) {
  266. cpumask_clear_cpu(j, uv_nmi_cpu_mask);
  267. if (++k >= n)
  268. break;
  269. }
  270. }
  271. if (k >= n) { /* all in? */
  272. k = n;
  273. break;
  274. }
  275. if (last_k != k) { /* abort if no new cpus coming in */
  276. last_k = k;
  277. waiting = 0;
  278. } else if (++waiting > uv_nmi_wait_count)
  279. break;
  280. /* extend delay if waiting only for cpu 0 */
  281. if (waiting && (n - k) == 1 &&
  282. cpumask_test_cpu(0, uv_nmi_cpu_mask))
  283. loop_delay *= 100;
  284. udelay(loop_delay);
  285. }
  286. atomic_set(&uv_nmi_cpus_in_nmi, k);
  287. return n - k;
  288. }
  289. /* Wait until all slave cpus have entered UV NMI handler */
  290. static void uv_nmi_wait(int master)
  291. {
  292. /* indicate this cpu is in */
  293. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_IN);
  294. /* if not the first cpu in (the master), then we are a slave cpu */
  295. if (!master)
  296. return;
  297. do {
  298. /* wait for all other cpus to gather here */
  299. if (!uv_nmi_wait_cpus(1))
  300. break;
  301. /* if not all made it in, send IPI NMI to them */
  302. pr_alert("UV: Sending NMI IPI to %d non-responding CPUs: %*pbl\n",
  303. cpumask_weight(uv_nmi_cpu_mask),
  304. cpumask_pr_args(uv_nmi_cpu_mask));
  305. uv_nmi_nr_cpus_ping();
  306. /* if all cpus are in, then done */
  307. if (!uv_nmi_wait_cpus(0))
  308. break;
  309. pr_alert("UV: %d CPUs not in NMI loop: %*pbl\n",
  310. cpumask_weight(uv_nmi_cpu_mask),
  311. cpumask_pr_args(uv_nmi_cpu_mask));
  312. } while (0);
  313. pr_alert("UV: %d of %d CPUs in NMI\n",
  314. atomic_read(&uv_nmi_cpus_in_nmi), num_online_cpus());
  315. }
  316. /* Dump Instruction Pointer header */
  317. static void uv_nmi_dump_cpu_ip_hdr(void)
  318. {
  319. pr_info("\nUV: %4s %6s %-32s %s (Note: PID 0 not listed)\n",
  320. "CPU", "PID", "COMMAND", "IP");
  321. }
  322. /* Dump Instruction Pointer info */
  323. static void uv_nmi_dump_cpu_ip(int cpu, struct pt_regs *regs)
  324. {
  325. pr_info("UV: %4d %6d %-32.32s ", cpu, current->pid, current->comm);
  326. printk_address(regs->ip);
  327. }
  328. /*
  329. * Dump this CPU's state. If action was set to "kdump" and the crash_kexec
  330. * failed, then we provide "dump" as an alternate action. Action "dump" now
  331. * also includes the show "ips" (instruction pointers) action whereas the
  332. * action "ips" only displays instruction pointers for the non-idle CPU's.
  333. * This is an abbreviated form of the "ps" command.
  334. */
  335. static void uv_nmi_dump_state_cpu(int cpu, struct pt_regs *regs)
  336. {
  337. const char *dots = " ................................. ";
  338. if (cpu == 0)
  339. uv_nmi_dump_cpu_ip_hdr();
  340. if (current->pid != 0 || !uv_nmi_action_is("ips"))
  341. uv_nmi_dump_cpu_ip(cpu, regs);
  342. if (uv_nmi_action_is("dump")) {
  343. pr_info("UV:%sNMI process trace for CPU %d\n", dots, cpu);
  344. show_regs(regs);
  345. }
  346. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_DUMP_DONE);
  347. }
  348. /* Trigger a slave cpu to dump it's state */
  349. static void uv_nmi_trigger_dump(int cpu)
  350. {
  351. int retry = uv_nmi_trigger_delay;
  352. if (uv_cpu_nmi_per(cpu).state != UV_NMI_STATE_IN)
  353. return;
  354. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_DUMP;
  355. do {
  356. cpu_relax();
  357. udelay(10);
  358. if (uv_cpu_nmi_per(cpu).state
  359. != UV_NMI_STATE_DUMP)
  360. return;
  361. } while (--retry > 0);
  362. pr_crit("UV: CPU %d stuck in process dump function\n", cpu);
  363. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_DUMP_DONE;
  364. }
  365. /* Wait until all cpus ready to exit */
  366. static void uv_nmi_sync_exit(int master)
  367. {
  368. atomic_dec(&uv_nmi_cpus_in_nmi);
  369. if (master) {
  370. while (atomic_read(&uv_nmi_cpus_in_nmi) > 0)
  371. cpu_relax();
  372. atomic_set(&uv_nmi_slave_continue, SLAVE_CLEAR);
  373. } else {
  374. while (atomic_read(&uv_nmi_slave_continue))
  375. cpu_relax();
  376. }
  377. }
  378. /* Walk through cpu list and dump state of each */
  379. static void uv_nmi_dump_state(int cpu, struct pt_regs *regs, int master)
  380. {
  381. if (master) {
  382. int tcpu;
  383. int ignored = 0;
  384. int saved_console_loglevel = console_loglevel;
  385. pr_alert("UV: tracing %s for %d CPUs from CPU %d\n",
  386. uv_nmi_action_is("ips") ? "IPs" : "processes",
  387. atomic_read(&uv_nmi_cpus_in_nmi), cpu);
  388. console_loglevel = uv_nmi_loglevel;
  389. atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
  390. for_each_online_cpu(tcpu) {
  391. if (cpumask_test_cpu(tcpu, uv_nmi_cpu_mask))
  392. ignored++;
  393. else if (tcpu == cpu)
  394. uv_nmi_dump_state_cpu(tcpu, regs);
  395. else
  396. uv_nmi_trigger_dump(tcpu);
  397. }
  398. if (ignored)
  399. pr_alert("UV: %d CPUs ignored NMI\n", ignored);
  400. console_loglevel = saved_console_loglevel;
  401. pr_alert("UV: process trace complete\n");
  402. } else {
  403. while (!atomic_read(&uv_nmi_slave_continue))
  404. cpu_relax();
  405. while (this_cpu_read(uv_cpu_nmi.state) != UV_NMI_STATE_DUMP)
  406. cpu_relax();
  407. uv_nmi_dump_state_cpu(cpu, regs);
  408. }
  409. uv_nmi_sync_exit(master);
  410. }
  411. static void uv_nmi_touch_watchdogs(void)
  412. {
  413. touch_softlockup_watchdog_sync();
  414. clocksource_touch_watchdog();
  415. rcu_cpu_stall_reset();
  416. touch_nmi_watchdog();
  417. }
  418. static atomic_t uv_nmi_kexec_failed;
  419. #if defined(CONFIG_KEXEC_CORE)
  420. static void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
  421. {
  422. /* Call crash to dump system state */
  423. if (master) {
  424. pr_emerg("UV: NMI executing crash_kexec on CPU%d\n", cpu);
  425. crash_kexec(regs);
  426. pr_emerg("UV: crash_kexec unexpectedly returned, ");
  427. atomic_set(&uv_nmi_kexec_failed, 1);
  428. if (!kexec_crash_image) {
  429. pr_cont("crash kernel not loaded\n");
  430. return;
  431. }
  432. pr_cont("kexec busy, stalling cpus while waiting\n");
  433. }
  434. /* If crash exec fails the slaves should return, otherwise stall */
  435. while (atomic_read(&uv_nmi_kexec_failed) == 0)
  436. mdelay(10);
  437. }
  438. #else /* !CONFIG_KEXEC_CORE */
  439. static inline void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
  440. {
  441. if (master)
  442. pr_err("UV: NMI kdump: KEXEC not supported in this kernel\n");
  443. atomic_set(&uv_nmi_kexec_failed, 1);
  444. }
  445. #endif /* !CONFIG_KEXEC_CORE */
  446. #ifdef CONFIG_KGDB
  447. #ifdef CONFIG_KGDB_KDB
  448. static inline int uv_nmi_kdb_reason(void)
  449. {
  450. return KDB_REASON_SYSTEM_NMI;
  451. }
  452. #else /* !CONFIG_KGDB_KDB */
  453. static inline int uv_nmi_kdb_reason(void)
  454. {
  455. /* Insure user is expecting to attach gdb remote */
  456. if (uv_nmi_action_is("kgdb"))
  457. return 0;
  458. pr_err("UV: NMI error: KDB is not enabled in this kernel\n");
  459. return -1;
  460. }
  461. #endif /* CONFIG_KGDB_KDB */
  462. /*
  463. * Call KGDB/KDB from NMI handler
  464. *
  465. * Note that if both KGDB and KDB are configured, then the action of 'kgdb' or
  466. * 'kdb' has no affect on which is used. See the KGDB documention for further
  467. * information.
  468. */
  469. static void uv_call_kgdb_kdb(int cpu, struct pt_regs *regs, int master)
  470. {
  471. if (master) {
  472. int reason = uv_nmi_kdb_reason();
  473. int ret;
  474. if (reason < 0)
  475. return;
  476. /* call KGDB NMI handler as MASTER */
  477. ret = kgdb_nmicallin(cpu, X86_TRAP_NMI, regs, reason,
  478. &uv_nmi_slave_continue);
  479. if (ret) {
  480. pr_alert("KGDB returned error, is kgdboc set?\n");
  481. atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
  482. }
  483. } else {
  484. /* wait for KGDB signal that it's ready for slaves to enter */
  485. int sig;
  486. do {
  487. cpu_relax();
  488. sig = atomic_read(&uv_nmi_slave_continue);
  489. } while (!sig);
  490. /* call KGDB as slave */
  491. if (sig == SLAVE_CONTINUE)
  492. kgdb_nmicallback(cpu, regs);
  493. }
  494. uv_nmi_sync_exit(master);
  495. }
  496. #else /* !CONFIG_KGDB */
  497. static inline void uv_call_kgdb_kdb(int cpu, struct pt_regs *regs, int master)
  498. {
  499. pr_err("UV: NMI error: KGDB is not enabled in this kernel\n");
  500. }
  501. #endif /* !CONFIG_KGDB */
  502. /*
  503. * UV NMI handler
  504. */
  505. int uv_handle_nmi(unsigned int reason, struct pt_regs *regs)
  506. {
  507. struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
  508. int cpu = smp_processor_id();
  509. int master = 0;
  510. unsigned long flags;
  511. local_irq_save(flags);
  512. /* If not a UV System NMI, ignore */
  513. if (!this_cpu_read(uv_cpu_nmi.pinging) && !uv_check_nmi(hub_nmi)) {
  514. local_irq_restore(flags);
  515. return NMI_DONE;
  516. }
  517. /* Indicate we are the first CPU into the NMI handler */
  518. master = (atomic_read(&uv_nmi_cpu) == cpu);
  519. /* If NMI action is "kdump", then attempt to do it */
  520. if (uv_nmi_action_is("kdump")) {
  521. uv_nmi_kdump(cpu, master, regs);
  522. /* Unexpected return, revert action to "dump" */
  523. if (master)
  524. strncpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
  525. }
  526. /* Pause as all cpus enter the NMI handler */
  527. uv_nmi_wait(master);
  528. /* Dump state of each cpu */
  529. if (uv_nmi_action_is("ips") || uv_nmi_action_is("dump"))
  530. uv_nmi_dump_state(cpu, regs, master);
  531. /* Call KGDB/KDB if enabled */
  532. else if (uv_nmi_action_is("kdb") || uv_nmi_action_is("kgdb"))
  533. uv_call_kgdb_kdb(cpu, regs, master);
  534. /* Clear per_cpu "in nmi" flag */
  535. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_OUT);
  536. /* Clear MMR NMI flag on each hub */
  537. uv_clear_nmi(cpu);
  538. /* Clear global flags */
  539. if (master) {
  540. if (cpumask_weight(uv_nmi_cpu_mask))
  541. uv_nmi_cleanup_mask();
  542. atomic_set(&uv_nmi_cpus_in_nmi, -1);
  543. atomic_set(&uv_nmi_cpu, -1);
  544. atomic_set(&uv_in_nmi, 0);
  545. atomic_set(&uv_nmi_kexec_failed, 0);
  546. }
  547. uv_nmi_touch_watchdogs();
  548. local_irq_restore(flags);
  549. return NMI_HANDLED;
  550. }
  551. /*
  552. * NMI handler for pulling in CPUs when perf events are grabbing our NMI
  553. */
  554. static int uv_handle_nmi_ping(unsigned int reason, struct pt_regs *regs)
  555. {
  556. int ret;
  557. this_cpu_inc(uv_cpu_nmi.queries);
  558. if (!this_cpu_read(uv_cpu_nmi.pinging)) {
  559. local64_inc(&uv_nmi_ping_misses);
  560. return NMI_DONE;
  561. }
  562. this_cpu_inc(uv_cpu_nmi.pings);
  563. local64_inc(&uv_nmi_ping_count);
  564. ret = uv_handle_nmi(reason, regs);
  565. this_cpu_write(uv_cpu_nmi.pinging, 0);
  566. return ret;
  567. }
  568. static void uv_register_nmi_notifier(void)
  569. {
  570. if (register_nmi_handler(NMI_UNKNOWN, uv_handle_nmi, 0, "uv"))
  571. pr_warn("UV: NMI handler failed to register\n");
  572. if (register_nmi_handler(NMI_LOCAL, uv_handle_nmi_ping, 0, "uvping"))
  573. pr_warn("UV: PING NMI handler failed to register\n");
  574. }
  575. void uv_nmi_init(void)
  576. {
  577. unsigned int value;
  578. /*
  579. * Unmask NMI on all cpus
  580. */
  581. value = apic_read(APIC_LVT1) | APIC_DM_NMI;
  582. value &= ~APIC_LVT_MASKED;
  583. apic_write(APIC_LVT1, value);
  584. }
  585. void uv_nmi_setup(void)
  586. {
  587. int size = sizeof(void *) * (1 << NODES_SHIFT);
  588. int cpu, nid;
  589. /* Setup hub nmi info */
  590. uv_nmi_setup_mmrs();
  591. uv_hub_nmi_list = kzalloc(size, GFP_KERNEL);
  592. pr_info("UV: NMI hub list @ 0x%p (%d)\n", uv_hub_nmi_list, size);
  593. BUG_ON(!uv_hub_nmi_list);
  594. size = sizeof(struct uv_hub_nmi_s);
  595. for_each_present_cpu(cpu) {
  596. nid = cpu_to_node(cpu);
  597. if (uv_hub_nmi_list[nid] == NULL) {
  598. uv_hub_nmi_list[nid] = kzalloc_node(size,
  599. GFP_KERNEL, nid);
  600. BUG_ON(!uv_hub_nmi_list[nid]);
  601. raw_spin_lock_init(&(uv_hub_nmi_list[nid]->nmi_lock));
  602. atomic_set(&uv_hub_nmi_list[nid]->cpu_owner, -1);
  603. }
  604. uv_hub_nmi_per(cpu) = uv_hub_nmi_list[nid];
  605. }
  606. BUG_ON(!alloc_cpumask_var(&uv_nmi_cpu_mask, GFP_KERNEL));
  607. uv_register_nmi_notifier();
  608. }