uv_nmi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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/sched/debug.h>
  30. #include <linux/slab.h>
  31. #include <linux/clocksource.h>
  32. #include <asm/apic.h>
  33. #include <asm/current.h>
  34. #include <asm/kdebug.h>
  35. #include <asm/local64.h>
  36. #include <asm/nmi.h>
  37. #include <asm/traps.h>
  38. #include <asm/uv/uv.h>
  39. #include <asm/uv/uv_hub.h>
  40. #include <asm/uv/uv_mmrs.h>
  41. /*
  42. * UV handler for NMI
  43. *
  44. * Handle system-wide NMI events generated by the global 'power nmi' command.
  45. *
  46. * Basic operation is to field the NMI interrupt on each CPU and wait
  47. * until all CPU's have arrived into the nmi handler. If some CPU's do not
  48. * make it into the handler, try and force them in with the IPI(NMI) signal.
  49. *
  50. * We also have to lessen UV Hub MMR accesses as much as possible as this
  51. * disrupts the UV Hub's primary mission of directing NumaLink traffic and
  52. * can cause system problems to occur.
  53. *
  54. * To do this we register our primary NMI notifier on the NMI_UNKNOWN
  55. * chain. This reduces the number of false NMI calls when the perf
  56. * tools are running which generate an enormous number of NMIs per
  57. * second (~4M/s for 1024 CPU threads). Our secondary NMI handler is
  58. * very short as it only checks that if it has been "pinged" with the
  59. * IPI(NMI) signal as mentioned above, and does not read the UV Hub's MMR.
  60. *
  61. */
  62. static struct uv_hub_nmi_s **uv_hub_nmi_list;
  63. DEFINE_PER_CPU(struct uv_cpu_nmi_s, uv_cpu_nmi);
  64. /* UV hubless values */
  65. #define NMI_CONTROL_PORT 0x70
  66. #define NMI_DUMMY_PORT 0x71
  67. #define PAD_OWN_GPP_D_0 0x2c
  68. #define GPI_NMI_STS_GPP_D_0 0x164
  69. #define GPI_NMI_ENA_GPP_D_0 0x174
  70. #define STS_GPP_D_0_MASK 0x1
  71. #define PAD_CFG_DW0_GPP_D_0 0x4c0
  72. #define GPIROUTNMI (1ul << 17)
  73. #define PCH_PCR_GPIO_1_BASE 0xfdae0000ul
  74. #define PCH_PCR_GPIO_ADDRESS(offset) (int *)((u64)(pch_base) | (u64)(offset))
  75. static u64 *pch_base;
  76. static unsigned long nmi_mmr;
  77. static unsigned long nmi_mmr_clear;
  78. static unsigned long nmi_mmr_pending;
  79. static atomic_t uv_in_nmi;
  80. static atomic_t uv_nmi_cpu = ATOMIC_INIT(-1);
  81. static atomic_t uv_nmi_cpus_in_nmi = ATOMIC_INIT(-1);
  82. static atomic_t uv_nmi_slave_continue;
  83. static cpumask_var_t uv_nmi_cpu_mask;
  84. /* Values for uv_nmi_slave_continue */
  85. #define SLAVE_CLEAR 0
  86. #define SLAVE_CONTINUE 1
  87. #define SLAVE_EXIT 2
  88. /*
  89. * Default is all stack dumps go to the console and buffer.
  90. * Lower level to send to log buffer only.
  91. */
  92. static int uv_nmi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  93. module_param_named(dump_loglevel, uv_nmi_loglevel, int, 0644);
  94. /*
  95. * The following values show statistics on how perf events are affecting
  96. * this system.
  97. */
  98. static int param_get_local64(char *buffer, const struct kernel_param *kp)
  99. {
  100. return sprintf(buffer, "%lu\n", local64_read((local64_t *)kp->arg));
  101. }
  102. static int param_set_local64(const char *val, const struct kernel_param *kp)
  103. {
  104. /* Clear on any write */
  105. local64_set((local64_t *)kp->arg, 0);
  106. return 0;
  107. }
  108. static const struct kernel_param_ops param_ops_local64 = {
  109. .get = param_get_local64,
  110. .set = param_set_local64,
  111. };
  112. #define param_check_local64(name, p) __param_check(name, p, local64_t)
  113. static local64_t uv_nmi_count;
  114. module_param_named(nmi_count, uv_nmi_count, local64, 0644);
  115. static local64_t uv_nmi_misses;
  116. module_param_named(nmi_misses, uv_nmi_misses, local64, 0644);
  117. static local64_t uv_nmi_ping_count;
  118. module_param_named(ping_count, uv_nmi_ping_count, local64, 0644);
  119. static local64_t uv_nmi_ping_misses;
  120. module_param_named(ping_misses, uv_nmi_ping_misses, local64, 0644);
  121. /*
  122. * Following values allow tuning for large systems under heavy loading
  123. */
  124. static int uv_nmi_initial_delay = 100;
  125. module_param_named(initial_delay, uv_nmi_initial_delay, int, 0644);
  126. static int uv_nmi_slave_delay = 100;
  127. module_param_named(slave_delay, uv_nmi_slave_delay, int, 0644);
  128. static int uv_nmi_loop_delay = 100;
  129. module_param_named(loop_delay, uv_nmi_loop_delay, int, 0644);
  130. static int uv_nmi_trigger_delay = 10000;
  131. module_param_named(trigger_delay, uv_nmi_trigger_delay, int, 0644);
  132. static int uv_nmi_wait_count = 100;
  133. module_param_named(wait_count, uv_nmi_wait_count, int, 0644);
  134. static int uv_nmi_retry_count = 500;
  135. module_param_named(retry_count, uv_nmi_retry_count, int, 0644);
  136. static bool uv_pch_intr_enable = true;
  137. static bool uv_pch_intr_now_enabled;
  138. module_param_named(pch_intr_enable, uv_pch_intr_enable, bool, 0644);
  139. static bool uv_pch_init_enable = true;
  140. module_param_named(pch_init_enable, uv_pch_init_enable, bool, 0644);
  141. static int uv_nmi_debug;
  142. module_param_named(debug, uv_nmi_debug, int, 0644);
  143. #define nmi_debug(fmt, ...) \
  144. do { \
  145. if (uv_nmi_debug) \
  146. pr_info(fmt, ##__VA_ARGS__); \
  147. } while (0)
  148. /* Valid NMI Actions */
  149. #define ACTION_LEN 16
  150. static struct nmi_action {
  151. char *action;
  152. char *desc;
  153. } valid_acts[] = {
  154. { "kdump", "do kernel crash dump" },
  155. { "dump", "dump process stack for each cpu" },
  156. { "ips", "dump Inst Ptr info for each cpu" },
  157. { "kdb", "enter KDB (needs kgdboc= assignment)" },
  158. { "kgdb", "enter KGDB (needs gdb target remote)" },
  159. { "health", "check if CPUs respond to NMI" },
  160. };
  161. typedef char action_t[ACTION_LEN];
  162. static action_t uv_nmi_action = { "dump" };
  163. static int param_get_action(char *buffer, const struct kernel_param *kp)
  164. {
  165. return sprintf(buffer, "%s\n", uv_nmi_action);
  166. }
  167. static int param_set_action(const char *val, const struct kernel_param *kp)
  168. {
  169. int i;
  170. int n = ARRAY_SIZE(valid_acts);
  171. char arg[ACTION_LEN], *p;
  172. /* (remove possible '\n') */
  173. strncpy(arg, val, ACTION_LEN - 1);
  174. arg[ACTION_LEN - 1] = '\0';
  175. p = strchr(arg, '\n');
  176. if (p)
  177. *p = '\0';
  178. for (i = 0; i < n; i++)
  179. if (!strcmp(arg, valid_acts[i].action))
  180. break;
  181. if (i < n) {
  182. strcpy(uv_nmi_action, arg);
  183. pr_info("UV: New NMI action:%s\n", uv_nmi_action);
  184. return 0;
  185. }
  186. pr_err("UV: Invalid NMI action:%s, valid actions are:\n", arg);
  187. for (i = 0; i < n; i++)
  188. pr_err("UV: %-8s - %s\n",
  189. valid_acts[i].action, valid_acts[i].desc);
  190. return -EINVAL;
  191. }
  192. static const struct kernel_param_ops param_ops_action = {
  193. .get = param_get_action,
  194. .set = param_set_action,
  195. };
  196. #define param_check_action(name, p) __param_check(name, p, action_t)
  197. module_param_named(action, uv_nmi_action, action, 0644);
  198. static inline bool uv_nmi_action_is(const char *action)
  199. {
  200. return (strncmp(uv_nmi_action, action, strlen(action)) == 0);
  201. }
  202. /* Setup which NMI support is present in system */
  203. static void uv_nmi_setup_mmrs(void)
  204. {
  205. if (uv_read_local_mmr(UVH_NMI_MMRX_SUPPORTED)) {
  206. uv_write_local_mmr(UVH_NMI_MMRX_REQ,
  207. 1UL << UVH_NMI_MMRX_REQ_SHIFT);
  208. nmi_mmr = UVH_NMI_MMRX;
  209. nmi_mmr_clear = UVH_NMI_MMRX_CLEAR;
  210. nmi_mmr_pending = 1UL << UVH_NMI_MMRX_SHIFT;
  211. pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMRX_TYPE);
  212. } else {
  213. nmi_mmr = UVH_NMI_MMR;
  214. nmi_mmr_clear = UVH_NMI_MMR_CLEAR;
  215. nmi_mmr_pending = 1UL << UVH_NMI_MMR_SHIFT;
  216. pr_info("UV: SMI NMI support: %s\n", UVH_NMI_MMR_TYPE);
  217. }
  218. }
  219. /* Read NMI MMR and check if NMI flag was set by BMC. */
  220. static inline int uv_nmi_test_mmr(struct uv_hub_nmi_s *hub_nmi)
  221. {
  222. hub_nmi->nmi_value = uv_read_local_mmr(nmi_mmr);
  223. atomic_inc(&hub_nmi->read_mmr_count);
  224. return !!(hub_nmi->nmi_value & nmi_mmr_pending);
  225. }
  226. static inline void uv_local_mmr_clear_nmi(void)
  227. {
  228. uv_write_local_mmr(nmi_mmr_clear, nmi_mmr_pending);
  229. }
  230. /*
  231. * UV hubless NMI handler functions
  232. */
  233. static inline void uv_reassert_nmi(void)
  234. {
  235. /* (from arch/x86/include/asm/mach_traps.h) */
  236. outb(0x8f, NMI_CONTROL_PORT);
  237. inb(NMI_DUMMY_PORT); /* dummy read */
  238. outb(0x0f, NMI_CONTROL_PORT);
  239. inb(NMI_DUMMY_PORT); /* dummy read */
  240. }
  241. static void uv_init_hubless_pch_io(int offset, int mask, int data)
  242. {
  243. int *addr = PCH_PCR_GPIO_ADDRESS(offset);
  244. int readd = readl(addr);
  245. if (mask) { /* OR in new data */
  246. int writed = (readd & ~mask) | data;
  247. nmi_debug("UV:PCH: %p = %x & %x | %x (%x)\n",
  248. addr, readd, ~mask, data, writed);
  249. writel(writed, addr);
  250. } else if (readd & data) { /* clear status bit */
  251. nmi_debug("UV:PCH: %p = %x\n", addr, data);
  252. writel(data, addr);
  253. }
  254. (void)readl(addr); /* flush write data */
  255. }
  256. static void uv_nmi_setup_hubless_intr(void)
  257. {
  258. uv_pch_intr_now_enabled = uv_pch_intr_enable;
  259. uv_init_hubless_pch_io(
  260. PAD_CFG_DW0_GPP_D_0, GPIROUTNMI,
  261. uv_pch_intr_now_enabled ? GPIROUTNMI : 0);
  262. nmi_debug("UV:NMI: GPP_D_0 interrupt %s\n",
  263. uv_pch_intr_now_enabled ? "enabled" : "disabled");
  264. }
  265. static struct init_nmi {
  266. unsigned int offset;
  267. unsigned int mask;
  268. unsigned int data;
  269. } init_nmi[] = {
  270. { /* HOSTSW_OWN_GPP_D_0 */
  271. .offset = 0x84,
  272. .mask = 0x1,
  273. .data = 0x0, /* ACPI Mode */
  274. },
  275. /* Clear status: */
  276. { /* GPI_INT_STS_GPP_D_0 */
  277. .offset = 0x104,
  278. .mask = 0x0,
  279. .data = 0x1, /* Clear Status */
  280. },
  281. { /* GPI_GPE_STS_GPP_D_0 */
  282. .offset = 0x124,
  283. .mask = 0x0,
  284. .data = 0x1, /* Clear Status */
  285. },
  286. { /* GPI_SMI_STS_GPP_D_0 */
  287. .offset = 0x144,
  288. .mask = 0x0,
  289. .data = 0x1, /* Clear Status */
  290. },
  291. { /* GPI_NMI_STS_GPP_D_0 */
  292. .offset = 0x164,
  293. .mask = 0x0,
  294. .data = 0x1, /* Clear Status */
  295. },
  296. /* Disable interrupts: */
  297. { /* GPI_INT_EN_GPP_D_0 */
  298. .offset = 0x114,
  299. .mask = 0x1,
  300. .data = 0x0, /* Disable interrupt generation */
  301. },
  302. { /* GPI_GPE_EN_GPP_D_0 */
  303. .offset = 0x134,
  304. .mask = 0x1,
  305. .data = 0x0, /* Disable interrupt generation */
  306. },
  307. { /* GPI_SMI_EN_GPP_D_0 */
  308. .offset = 0x154,
  309. .mask = 0x1,
  310. .data = 0x0, /* Disable interrupt generation */
  311. },
  312. { /* GPI_NMI_EN_GPP_D_0 */
  313. .offset = 0x174,
  314. .mask = 0x1,
  315. .data = 0x0, /* Disable interrupt generation */
  316. },
  317. /* Setup GPP_D_0 Pad Config: */
  318. { /* PAD_CFG_DW0_GPP_D_0 */
  319. .offset = 0x4c0,
  320. .mask = 0xffffffff,
  321. .data = 0x82020100,
  322. /*
  323. * 31:30 Pad Reset Config (PADRSTCFG): = 2h # PLTRST# (default)
  324. *
  325. * 29 RX Pad State Select (RXPADSTSEL): = 0 # Raw RX pad state directly
  326. * from RX buffer (default)
  327. *
  328. * 28 RX Raw Override to '1' (RXRAW1): = 0 # No Override
  329. *
  330. * 26:25 RX Level/Edge Configuration (RXEVCFG):
  331. * = 0h # Level
  332. * = 1h # Edge
  333. *
  334. * 23 RX Invert (RXINV): = 0 # No Inversion (signal active high)
  335. *
  336. * 20 GPIO Input Route IOxAPIC (GPIROUTIOXAPIC):
  337. * = 0 # Routing does not cause peripheral IRQ...
  338. * # (we want an NMI not an IRQ)
  339. *
  340. * 19 GPIO Input Route SCI (GPIROUTSCI): = 0 # Routing does not cause SCI.
  341. * 18 GPIO Input Route SMI (GPIROUTSMI): = 0 # Routing does not cause SMI.
  342. * 17 GPIO Input Route NMI (GPIROUTNMI): = 1 # Routing can cause NMI.
  343. *
  344. * 11:10 Pad Mode (PMODE1/0): = 0h = GPIO control the Pad.
  345. * 9 GPIO RX Disable (GPIORXDIS):
  346. * = 0 # Enable the input buffer (active low enable)
  347. *
  348. * 8 GPIO TX Disable (GPIOTXDIS):
  349. * = 1 # Disable the output buffer; i.e. Hi-Z
  350. *
  351. * 1 GPIO RX State (GPIORXSTATE): This is the current internal RX pad state..
  352. * 0 GPIO TX State (GPIOTXSTATE):
  353. * = 0 # (Leave at default)
  354. */
  355. },
  356. /* Pad Config DW1 */
  357. { /* PAD_CFG_DW1_GPP_D_0 */
  358. .offset = 0x4c4,
  359. .mask = 0x3c00,
  360. .data = 0, /* Termination = none (default) */
  361. },
  362. };
  363. static void uv_init_hubless_pch_d0(void)
  364. {
  365. int i, read;
  366. read = *PCH_PCR_GPIO_ADDRESS(PAD_OWN_GPP_D_0);
  367. if (read != 0) {
  368. pr_info("UV: Hubless NMI already configured\n");
  369. return;
  370. }
  371. nmi_debug("UV: Initializing UV Hubless NMI on PCH\n");
  372. for (i = 0; i < ARRAY_SIZE(init_nmi); i++) {
  373. uv_init_hubless_pch_io(init_nmi[i].offset,
  374. init_nmi[i].mask,
  375. init_nmi[i].data);
  376. }
  377. }
  378. static int uv_nmi_test_hubless(struct uv_hub_nmi_s *hub_nmi)
  379. {
  380. int *pstat = PCH_PCR_GPIO_ADDRESS(GPI_NMI_STS_GPP_D_0);
  381. int status = *pstat;
  382. hub_nmi->nmi_value = status;
  383. atomic_inc(&hub_nmi->read_mmr_count);
  384. if (!(status & STS_GPP_D_0_MASK)) /* Not a UV external NMI */
  385. return 0;
  386. *pstat = STS_GPP_D_0_MASK; /* Is a UV NMI: clear GPP_D_0 status */
  387. (void)*pstat; /* Flush write */
  388. return 1;
  389. }
  390. static int uv_test_nmi(struct uv_hub_nmi_s *hub_nmi)
  391. {
  392. if (hub_nmi->hub_present)
  393. return uv_nmi_test_mmr(hub_nmi);
  394. if (hub_nmi->pch_owner) /* Only PCH owner can check status */
  395. return uv_nmi_test_hubless(hub_nmi);
  396. return -1;
  397. }
  398. /*
  399. * If first CPU in on this hub, set hub_nmi "in_nmi" and "owner" values and
  400. * return true. If first CPU in on the system, set global "in_nmi" flag.
  401. */
  402. static int uv_set_in_nmi(int cpu, struct uv_hub_nmi_s *hub_nmi)
  403. {
  404. int first = atomic_add_unless(&hub_nmi->in_nmi, 1, 1);
  405. if (first) {
  406. atomic_set(&hub_nmi->cpu_owner, cpu);
  407. if (atomic_add_unless(&uv_in_nmi, 1, 1))
  408. atomic_set(&uv_nmi_cpu, cpu);
  409. atomic_inc(&hub_nmi->nmi_count);
  410. }
  411. return first;
  412. }
  413. /* Check if this is a system NMI event */
  414. static int uv_check_nmi(struct uv_hub_nmi_s *hub_nmi)
  415. {
  416. int cpu = smp_processor_id();
  417. int nmi = 0;
  418. int nmi_detected = 0;
  419. local64_inc(&uv_nmi_count);
  420. this_cpu_inc(uv_cpu_nmi.queries);
  421. do {
  422. nmi = atomic_read(&hub_nmi->in_nmi);
  423. if (nmi)
  424. break;
  425. if (raw_spin_trylock(&hub_nmi->nmi_lock)) {
  426. nmi_detected = uv_test_nmi(hub_nmi);
  427. /* Check flag for UV external NMI */
  428. if (nmi_detected > 0) {
  429. uv_set_in_nmi(cpu, hub_nmi);
  430. nmi = 1;
  431. break;
  432. }
  433. /* A non-PCH node in a hubless system waits for NMI */
  434. else if (nmi_detected < 0)
  435. goto slave_wait;
  436. /* MMR/PCH NMI flag is clear */
  437. raw_spin_unlock(&hub_nmi->nmi_lock);
  438. } else {
  439. /* Wait a moment for the HUB NMI locker to set flag */
  440. slave_wait: cpu_relax();
  441. udelay(uv_nmi_slave_delay);
  442. /* Re-check hub in_nmi flag */
  443. nmi = atomic_read(&hub_nmi->in_nmi);
  444. if (nmi)
  445. break;
  446. }
  447. /*
  448. * Check if this BMC missed setting the MMR NMI flag (or)
  449. * UV hubless system where only PCH owner can check flag
  450. */
  451. if (!nmi) {
  452. nmi = atomic_read(&uv_in_nmi);
  453. if (nmi)
  454. uv_set_in_nmi(cpu, hub_nmi);
  455. }
  456. /* If we're holding the hub lock, release it now */
  457. if (nmi_detected < 0)
  458. raw_spin_unlock(&hub_nmi->nmi_lock);
  459. } while (0);
  460. if (!nmi)
  461. local64_inc(&uv_nmi_misses);
  462. return nmi;
  463. }
  464. /* Need to reset the NMI MMR register, but only once per hub. */
  465. static inline void uv_clear_nmi(int cpu)
  466. {
  467. struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
  468. if (cpu == atomic_read(&hub_nmi->cpu_owner)) {
  469. atomic_set(&hub_nmi->cpu_owner, -1);
  470. atomic_set(&hub_nmi->in_nmi, 0);
  471. if (hub_nmi->hub_present)
  472. uv_local_mmr_clear_nmi();
  473. else
  474. uv_reassert_nmi();
  475. raw_spin_unlock(&hub_nmi->nmi_lock);
  476. }
  477. }
  478. /* Ping non-responding CPU's attemping to force them into the NMI handler */
  479. static void uv_nmi_nr_cpus_ping(void)
  480. {
  481. int cpu;
  482. for_each_cpu(cpu, uv_nmi_cpu_mask)
  483. uv_cpu_nmi_per(cpu).pinging = 1;
  484. apic->send_IPI_mask(uv_nmi_cpu_mask, APIC_DM_NMI);
  485. }
  486. /* Clean up flags for CPU's that ignored both NMI and ping */
  487. static void uv_nmi_cleanup_mask(void)
  488. {
  489. int cpu;
  490. for_each_cpu(cpu, uv_nmi_cpu_mask) {
  491. uv_cpu_nmi_per(cpu).pinging = 0;
  492. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_OUT;
  493. cpumask_clear_cpu(cpu, uv_nmi_cpu_mask);
  494. }
  495. }
  496. /* Loop waiting as CPU's enter NMI handler */
  497. static int uv_nmi_wait_cpus(int first)
  498. {
  499. int i, j, k, n = num_online_cpus();
  500. int last_k = 0, waiting = 0;
  501. int cpu = smp_processor_id();
  502. if (first) {
  503. cpumask_copy(uv_nmi_cpu_mask, cpu_online_mask);
  504. k = 0;
  505. } else {
  506. k = n - cpumask_weight(uv_nmi_cpu_mask);
  507. }
  508. /* PCH NMI causes only one CPU to respond */
  509. if (first && uv_pch_intr_now_enabled) {
  510. cpumask_clear_cpu(cpu, uv_nmi_cpu_mask);
  511. return n - k - 1;
  512. }
  513. udelay(uv_nmi_initial_delay);
  514. for (i = 0; i < uv_nmi_retry_count; i++) {
  515. int loop_delay = uv_nmi_loop_delay;
  516. for_each_cpu(j, uv_nmi_cpu_mask) {
  517. if (uv_cpu_nmi_per(j).state) {
  518. cpumask_clear_cpu(j, uv_nmi_cpu_mask);
  519. if (++k >= n)
  520. break;
  521. }
  522. }
  523. if (k >= n) { /* all in? */
  524. k = n;
  525. break;
  526. }
  527. if (last_k != k) { /* abort if no new CPU's coming in */
  528. last_k = k;
  529. waiting = 0;
  530. } else if (++waiting > uv_nmi_wait_count)
  531. break;
  532. /* Extend delay if waiting only for CPU 0: */
  533. if (waiting && (n - k) == 1 &&
  534. cpumask_test_cpu(0, uv_nmi_cpu_mask))
  535. loop_delay *= 100;
  536. udelay(loop_delay);
  537. }
  538. atomic_set(&uv_nmi_cpus_in_nmi, k);
  539. return n - k;
  540. }
  541. /* Wait until all slave CPU's have entered UV NMI handler */
  542. static void uv_nmi_wait(int master)
  543. {
  544. /* Indicate this CPU is in: */
  545. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_IN);
  546. /* If not the first CPU in (the master), then we are a slave CPU */
  547. if (!master)
  548. return;
  549. do {
  550. /* Wait for all other CPU's to gather here */
  551. if (!uv_nmi_wait_cpus(1))
  552. break;
  553. /* If not all made it in, send IPI NMI to them */
  554. pr_alert("UV: Sending NMI IPI to %d CPUs: %*pbl\n",
  555. cpumask_weight(uv_nmi_cpu_mask),
  556. cpumask_pr_args(uv_nmi_cpu_mask));
  557. uv_nmi_nr_cpus_ping();
  558. /* If all CPU's are in, then done */
  559. if (!uv_nmi_wait_cpus(0))
  560. break;
  561. pr_alert("UV: %d CPUs not in NMI loop: %*pbl\n",
  562. cpumask_weight(uv_nmi_cpu_mask),
  563. cpumask_pr_args(uv_nmi_cpu_mask));
  564. } while (0);
  565. pr_alert("UV: %d of %d CPUs in NMI\n",
  566. atomic_read(&uv_nmi_cpus_in_nmi), num_online_cpus());
  567. }
  568. /* Dump Instruction Pointer header */
  569. static void uv_nmi_dump_cpu_ip_hdr(void)
  570. {
  571. pr_info("\nUV: %4s %6s %-32s %s (Note: PID 0 not listed)\n",
  572. "CPU", "PID", "COMMAND", "IP");
  573. }
  574. /* Dump Instruction Pointer info */
  575. static void uv_nmi_dump_cpu_ip(int cpu, struct pt_regs *regs)
  576. {
  577. pr_info("UV: %4d %6d %-32.32s %pS",
  578. cpu, current->pid, current->comm, (void *)regs->ip);
  579. }
  580. /*
  581. * Dump this CPU's state. If action was set to "kdump" and the crash_kexec
  582. * failed, then we provide "dump" as an alternate action. Action "dump" now
  583. * also includes the show "ips" (instruction pointers) action whereas the
  584. * action "ips" only displays instruction pointers for the non-idle CPU's.
  585. * This is an abbreviated form of the "ps" command.
  586. */
  587. static void uv_nmi_dump_state_cpu(int cpu, struct pt_regs *regs)
  588. {
  589. const char *dots = " ................................. ";
  590. if (cpu == 0)
  591. uv_nmi_dump_cpu_ip_hdr();
  592. if (current->pid != 0 || !uv_nmi_action_is("ips"))
  593. uv_nmi_dump_cpu_ip(cpu, regs);
  594. if (uv_nmi_action_is("dump")) {
  595. pr_info("UV:%sNMI process trace for CPU %d\n", dots, cpu);
  596. show_regs(regs);
  597. }
  598. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_DUMP_DONE);
  599. }
  600. /* Trigger a slave CPU to dump it's state */
  601. static void uv_nmi_trigger_dump(int cpu)
  602. {
  603. int retry = uv_nmi_trigger_delay;
  604. if (uv_cpu_nmi_per(cpu).state != UV_NMI_STATE_IN)
  605. return;
  606. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_DUMP;
  607. do {
  608. cpu_relax();
  609. udelay(10);
  610. if (uv_cpu_nmi_per(cpu).state
  611. != UV_NMI_STATE_DUMP)
  612. return;
  613. } while (--retry > 0);
  614. pr_crit("UV: CPU %d stuck in process dump function\n", cpu);
  615. uv_cpu_nmi_per(cpu).state = UV_NMI_STATE_DUMP_DONE;
  616. }
  617. /* Wait until all CPU's ready to exit */
  618. static void uv_nmi_sync_exit(int master)
  619. {
  620. atomic_dec(&uv_nmi_cpus_in_nmi);
  621. if (master) {
  622. while (atomic_read(&uv_nmi_cpus_in_nmi) > 0)
  623. cpu_relax();
  624. atomic_set(&uv_nmi_slave_continue, SLAVE_CLEAR);
  625. } else {
  626. while (atomic_read(&uv_nmi_slave_continue))
  627. cpu_relax();
  628. }
  629. }
  630. /* Current "health" check is to check which CPU's are responsive */
  631. static void uv_nmi_action_health(int cpu, struct pt_regs *regs, int master)
  632. {
  633. if (master) {
  634. int in = atomic_read(&uv_nmi_cpus_in_nmi);
  635. int out = num_online_cpus() - in;
  636. pr_alert("UV: NMI CPU health check (non-responding:%d)\n", out);
  637. atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
  638. } else {
  639. while (!atomic_read(&uv_nmi_slave_continue))
  640. cpu_relax();
  641. }
  642. uv_nmi_sync_exit(master);
  643. }
  644. /* Walk through CPU list and dump state of each */
  645. static void uv_nmi_dump_state(int cpu, struct pt_regs *regs, int master)
  646. {
  647. if (master) {
  648. int tcpu;
  649. int ignored = 0;
  650. int saved_console_loglevel = console_loglevel;
  651. pr_alert("UV: tracing %s for %d CPUs from CPU %d\n",
  652. uv_nmi_action_is("ips") ? "IPs" : "processes",
  653. atomic_read(&uv_nmi_cpus_in_nmi), cpu);
  654. console_loglevel = uv_nmi_loglevel;
  655. atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
  656. for_each_online_cpu(tcpu) {
  657. if (cpumask_test_cpu(tcpu, uv_nmi_cpu_mask))
  658. ignored++;
  659. else if (tcpu == cpu)
  660. uv_nmi_dump_state_cpu(tcpu, regs);
  661. else
  662. uv_nmi_trigger_dump(tcpu);
  663. }
  664. if (ignored)
  665. pr_alert("UV: %d CPUs ignored NMI\n", ignored);
  666. console_loglevel = saved_console_loglevel;
  667. pr_alert("UV: process trace complete\n");
  668. } else {
  669. while (!atomic_read(&uv_nmi_slave_continue))
  670. cpu_relax();
  671. while (this_cpu_read(uv_cpu_nmi.state) != UV_NMI_STATE_DUMP)
  672. cpu_relax();
  673. uv_nmi_dump_state_cpu(cpu, regs);
  674. }
  675. uv_nmi_sync_exit(master);
  676. }
  677. static void uv_nmi_touch_watchdogs(void)
  678. {
  679. touch_softlockup_watchdog_sync();
  680. clocksource_touch_watchdog();
  681. rcu_cpu_stall_reset();
  682. touch_nmi_watchdog();
  683. }
  684. static atomic_t uv_nmi_kexec_failed;
  685. #if defined(CONFIG_KEXEC_CORE)
  686. static void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
  687. {
  688. /* Call crash to dump system state */
  689. if (master) {
  690. pr_emerg("UV: NMI executing crash_kexec on CPU%d\n", cpu);
  691. crash_kexec(regs);
  692. pr_emerg("UV: crash_kexec unexpectedly returned, ");
  693. atomic_set(&uv_nmi_kexec_failed, 1);
  694. if (!kexec_crash_image) {
  695. pr_cont("crash kernel not loaded\n");
  696. return;
  697. }
  698. pr_cont("kexec busy, stalling cpus while waiting\n");
  699. }
  700. /* If crash exec fails the slaves should return, otherwise stall */
  701. while (atomic_read(&uv_nmi_kexec_failed) == 0)
  702. mdelay(10);
  703. }
  704. #else /* !CONFIG_KEXEC_CORE */
  705. static inline void uv_nmi_kdump(int cpu, int master, struct pt_regs *regs)
  706. {
  707. if (master)
  708. pr_err("UV: NMI kdump: KEXEC not supported in this kernel\n");
  709. atomic_set(&uv_nmi_kexec_failed, 1);
  710. }
  711. #endif /* !CONFIG_KEXEC_CORE */
  712. #ifdef CONFIG_KGDB
  713. #ifdef CONFIG_KGDB_KDB
  714. static inline int uv_nmi_kdb_reason(void)
  715. {
  716. return KDB_REASON_SYSTEM_NMI;
  717. }
  718. #else /* !CONFIG_KGDB_KDB */
  719. static inline int uv_nmi_kdb_reason(void)
  720. {
  721. /* Ensure user is expecting to attach gdb remote */
  722. if (uv_nmi_action_is("kgdb"))
  723. return 0;
  724. pr_err("UV: NMI error: KDB is not enabled in this kernel\n");
  725. return -1;
  726. }
  727. #endif /* CONFIG_KGDB_KDB */
  728. /*
  729. * Call KGDB/KDB from NMI handler
  730. *
  731. * Note that if both KGDB and KDB are configured, then the action of 'kgdb' or
  732. * 'kdb' has no affect on which is used. See the KGDB documention for further
  733. * information.
  734. */
  735. static void uv_call_kgdb_kdb(int cpu, struct pt_regs *regs, int master)
  736. {
  737. if (master) {
  738. int reason = uv_nmi_kdb_reason();
  739. int ret;
  740. if (reason < 0)
  741. return;
  742. /* Call KGDB NMI handler as MASTER */
  743. ret = kgdb_nmicallin(cpu, X86_TRAP_NMI, regs, reason,
  744. &uv_nmi_slave_continue);
  745. if (ret) {
  746. pr_alert("KGDB returned error, is kgdboc set?\n");
  747. atomic_set(&uv_nmi_slave_continue, SLAVE_EXIT);
  748. }
  749. } else {
  750. /* Wait for KGDB signal that it's ready for slaves to enter */
  751. int sig;
  752. do {
  753. cpu_relax();
  754. sig = atomic_read(&uv_nmi_slave_continue);
  755. } while (!sig);
  756. /* Call KGDB as slave */
  757. if (sig == SLAVE_CONTINUE)
  758. kgdb_nmicallback(cpu, regs);
  759. }
  760. uv_nmi_sync_exit(master);
  761. }
  762. #else /* !CONFIG_KGDB */
  763. static inline void uv_call_kgdb_kdb(int cpu, struct pt_regs *regs, int master)
  764. {
  765. pr_err("UV: NMI error: KGDB is not enabled in this kernel\n");
  766. }
  767. #endif /* !CONFIG_KGDB */
  768. /*
  769. * UV NMI handler
  770. */
  771. static int uv_handle_nmi(unsigned int reason, struct pt_regs *regs)
  772. {
  773. struct uv_hub_nmi_s *hub_nmi = uv_hub_nmi;
  774. int cpu = smp_processor_id();
  775. int master = 0;
  776. unsigned long flags;
  777. local_irq_save(flags);
  778. /* If not a UV System NMI, ignore */
  779. if (!this_cpu_read(uv_cpu_nmi.pinging) && !uv_check_nmi(hub_nmi)) {
  780. local_irq_restore(flags);
  781. return NMI_DONE;
  782. }
  783. /* Indicate we are the first CPU into the NMI handler */
  784. master = (atomic_read(&uv_nmi_cpu) == cpu);
  785. /* If NMI action is "kdump", then attempt to do it */
  786. if (uv_nmi_action_is("kdump")) {
  787. uv_nmi_kdump(cpu, master, regs);
  788. /* Unexpected return, revert action to "dump" */
  789. if (master)
  790. strncpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
  791. }
  792. /* Pause as all CPU's enter the NMI handler */
  793. uv_nmi_wait(master);
  794. /* Process actions other than "kdump": */
  795. if (uv_nmi_action_is("health")) {
  796. uv_nmi_action_health(cpu, regs, master);
  797. } else if (uv_nmi_action_is("ips") || uv_nmi_action_is("dump")) {
  798. uv_nmi_dump_state(cpu, regs, master);
  799. } else if (uv_nmi_action_is("kdb") || uv_nmi_action_is("kgdb")) {
  800. uv_call_kgdb_kdb(cpu, regs, master);
  801. } else {
  802. if (master)
  803. pr_alert("UV: unknown NMI action: %s\n", uv_nmi_action);
  804. uv_nmi_sync_exit(master);
  805. }
  806. /* Clear per_cpu "in_nmi" flag */
  807. this_cpu_write(uv_cpu_nmi.state, UV_NMI_STATE_OUT);
  808. /* Clear MMR NMI flag on each hub */
  809. uv_clear_nmi(cpu);
  810. /* Clear global flags */
  811. if (master) {
  812. if (cpumask_weight(uv_nmi_cpu_mask))
  813. uv_nmi_cleanup_mask();
  814. atomic_set(&uv_nmi_cpus_in_nmi, -1);
  815. atomic_set(&uv_nmi_cpu, -1);
  816. atomic_set(&uv_in_nmi, 0);
  817. atomic_set(&uv_nmi_kexec_failed, 0);
  818. atomic_set(&uv_nmi_slave_continue, SLAVE_CLEAR);
  819. }
  820. uv_nmi_touch_watchdogs();
  821. local_irq_restore(flags);
  822. return NMI_HANDLED;
  823. }
  824. /*
  825. * NMI handler for pulling in CPU's when perf events are grabbing our NMI
  826. */
  827. static int uv_handle_nmi_ping(unsigned int reason, struct pt_regs *regs)
  828. {
  829. int ret;
  830. this_cpu_inc(uv_cpu_nmi.queries);
  831. if (!this_cpu_read(uv_cpu_nmi.pinging)) {
  832. local64_inc(&uv_nmi_ping_misses);
  833. return NMI_DONE;
  834. }
  835. this_cpu_inc(uv_cpu_nmi.pings);
  836. local64_inc(&uv_nmi_ping_count);
  837. ret = uv_handle_nmi(reason, regs);
  838. this_cpu_write(uv_cpu_nmi.pinging, 0);
  839. return ret;
  840. }
  841. static void uv_register_nmi_notifier(void)
  842. {
  843. if (register_nmi_handler(NMI_UNKNOWN, uv_handle_nmi, 0, "uv"))
  844. pr_warn("UV: NMI handler failed to register\n");
  845. if (register_nmi_handler(NMI_LOCAL, uv_handle_nmi_ping, 0, "uvping"))
  846. pr_warn("UV: PING NMI handler failed to register\n");
  847. }
  848. void uv_nmi_init(void)
  849. {
  850. unsigned int value;
  851. /*
  852. * Unmask NMI on all CPU's
  853. */
  854. value = apic_read(APIC_LVT1) | APIC_DM_NMI;
  855. value &= ~APIC_LVT_MASKED;
  856. apic_write(APIC_LVT1, value);
  857. }
  858. /* Setup HUB NMI info */
  859. static void __init uv_nmi_setup_common(bool hubbed)
  860. {
  861. int size = sizeof(void *) * (1 << NODES_SHIFT);
  862. int cpu;
  863. uv_hub_nmi_list = kzalloc(size, GFP_KERNEL);
  864. nmi_debug("UV: NMI hub list @ 0x%p (%d)\n", uv_hub_nmi_list, size);
  865. BUG_ON(!uv_hub_nmi_list);
  866. size = sizeof(struct uv_hub_nmi_s);
  867. for_each_present_cpu(cpu) {
  868. int nid = cpu_to_node(cpu);
  869. if (uv_hub_nmi_list[nid] == NULL) {
  870. uv_hub_nmi_list[nid] = kzalloc_node(size,
  871. GFP_KERNEL, nid);
  872. BUG_ON(!uv_hub_nmi_list[nid]);
  873. raw_spin_lock_init(&(uv_hub_nmi_list[nid]->nmi_lock));
  874. atomic_set(&uv_hub_nmi_list[nid]->cpu_owner, -1);
  875. uv_hub_nmi_list[nid]->hub_present = hubbed;
  876. uv_hub_nmi_list[nid]->pch_owner = (nid == 0);
  877. }
  878. uv_hub_nmi_per(cpu) = uv_hub_nmi_list[nid];
  879. }
  880. BUG_ON(!alloc_cpumask_var(&uv_nmi_cpu_mask, GFP_KERNEL));
  881. }
  882. /* Setup for UV Hub systems */
  883. void __init uv_nmi_setup(void)
  884. {
  885. uv_nmi_setup_mmrs();
  886. uv_nmi_setup_common(true);
  887. uv_register_nmi_notifier();
  888. pr_info("UV: Hub NMI enabled\n");
  889. }
  890. /* Setup for UV Hubless systems */
  891. void __init uv_nmi_setup_hubless(void)
  892. {
  893. uv_nmi_setup_common(false);
  894. pch_base = xlate_dev_mem_ptr(PCH_PCR_GPIO_1_BASE);
  895. nmi_debug("UV: PCH base:%p from 0x%lx, GPP_D_0\n",
  896. pch_base, PCH_PCR_GPIO_1_BASE);
  897. if (uv_pch_init_enable)
  898. uv_init_hubless_pch_d0();
  899. uv_init_hubless_pch_io(GPI_NMI_ENA_GPP_D_0,
  900. STS_GPP_D_0_MASK, STS_GPP_D_0_MASK);
  901. uv_nmi_setup_hubless_intr();
  902. /* Ensure NMI enabled in Processor Interface Reg: */
  903. uv_reassert_nmi();
  904. uv_register_nmi_notifier();
  905. pr_info("UV: Hubless NMI enabled\n");
  906. }