uv_nmi.c 19 KB

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