PageRenderTime 1214ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/misc/sgi-xp/xpc_sn2.c

https://github.com/Mengqi/linux-2.6
C | 1736 lines | 1120 code | 289 blank | 327 comment | 141 complexity | a76a55d01bb0488208cd6f2f73783aef MD5 | raw file
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2008-2009 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) sn2-based functions.
  10. *
  11. * Architecture specific implementation of common functions.
  12. *
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <asm/uncached.h>
  17. #include <asm/sn/mspec.h>
  18. #include <asm/sn/sn_sal.h>
  19. #include "xpc.h"
  20. /*
  21. * Define the number of u64s required to represent all the C-brick nasids
  22. * as a bitmap. The cross-partition kernel modules deal only with
  23. * C-brick nasids, thus the need for bitmaps which don't account for
  24. * odd-numbered (non C-brick) nasids.
  25. */
  26. #define XPC_MAX_PHYSNODES_SN2 (MAX_NUMALINK_NODES / 2)
  27. #define XP_NASID_MASK_BYTES_SN2 ((XPC_MAX_PHYSNODES_SN2 + 7) / 8)
  28. #define XP_NASID_MASK_WORDS_SN2 ((XPC_MAX_PHYSNODES_SN2 + 63) / 64)
  29. /*
  30. * Memory for XPC's amo variables is allocated by the MSPEC driver. These
  31. * pages are located in the lowest granule. The lowest granule uses 4k pages
  32. * for cached references and an alternate TLB handler to never provide a
  33. * cacheable mapping for the entire region. This will prevent speculative
  34. * reading of cached copies of our lines from being issued which will cause
  35. * a PI FSB Protocol error to be generated by the SHUB. For XPC, we need 64
  36. * amo variables (based on XP_MAX_NPARTITIONS_SN2) to identify the senders of
  37. * NOTIFY IRQs, 128 amo variables (based on XP_NASID_MASK_WORDS_SN2) to identify
  38. * the senders of ACTIVATE IRQs, 1 amo variable to identify which remote
  39. * partitions (i.e., XPCs) consider themselves currently engaged with the
  40. * local XPC and 1 amo variable to request partition deactivation.
  41. */
  42. #define XPC_NOTIFY_IRQ_AMOS_SN2 0
  43. #define XPC_ACTIVATE_IRQ_AMOS_SN2 (XPC_NOTIFY_IRQ_AMOS_SN2 + \
  44. XP_MAX_NPARTITIONS_SN2)
  45. #define XPC_ENGAGED_PARTITIONS_AMO_SN2 (XPC_ACTIVATE_IRQ_AMOS_SN2 + \
  46. XP_NASID_MASK_WORDS_SN2)
  47. #define XPC_DEACTIVATE_REQUEST_AMO_SN2 (XPC_ENGAGED_PARTITIONS_AMO_SN2 + 1)
  48. /*
  49. * Buffer used to store a local copy of portions of a remote partition's
  50. * reserved page (either its header and part_nasids mask, or its vars).
  51. */
  52. static void *xpc_remote_copy_buffer_base_sn2;
  53. static char *xpc_remote_copy_buffer_sn2;
  54. static struct xpc_vars_sn2 *xpc_vars_sn2;
  55. static struct xpc_vars_part_sn2 *xpc_vars_part_sn2;
  56. static int
  57. xpc_setup_partitions_sn2(void)
  58. {
  59. /* nothing needs to be done */
  60. return 0;
  61. }
  62. static void
  63. xpc_teardown_partitions_sn2(void)
  64. {
  65. /* nothing needs to be done */
  66. }
  67. /* SH_IPI_ACCESS shub register value on startup */
  68. static u64 xpc_sh1_IPI_access_sn2;
  69. static u64 xpc_sh2_IPI_access0_sn2;
  70. static u64 xpc_sh2_IPI_access1_sn2;
  71. static u64 xpc_sh2_IPI_access2_sn2;
  72. static u64 xpc_sh2_IPI_access3_sn2;
  73. /*
  74. * Change protections to allow IPI operations.
  75. */
  76. static void
  77. xpc_allow_IPI_ops_sn2(void)
  78. {
  79. int node;
  80. int nasid;
  81. /* !!! The following should get moved into SAL. */
  82. if (is_shub2()) {
  83. xpc_sh2_IPI_access0_sn2 =
  84. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
  85. xpc_sh2_IPI_access1_sn2 =
  86. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
  87. xpc_sh2_IPI_access2_sn2 =
  88. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
  89. xpc_sh2_IPI_access3_sn2 =
  90. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
  91. for_each_online_node(node) {
  92. nasid = cnodeid_to_nasid(node);
  93. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  94. -1UL);
  95. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  96. -1UL);
  97. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  98. -1UL);
  99. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  100. -1UL);
  101. }
  102. } else {
  103. xpc_sh1_IPI_access_sn2 =
  104. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
  105. for_each_online_node(node) {
  106. nasid = cnodeid_to_nasid(node);
  107. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  108. -1UL);
  109. }
  110. }
  111. }
  112. /*
  113. * Restrict protections to disallow IPI operations.
  114. */
  115. static void
  116. xpc_disallow_IPI_ops_sn2(void)
  117. {
  118. int node;
  119. int nasid;
  120. /* !!! The following should get moved into SAL. */
  121. if (is_shub2()) {
  122. for_each_online_node(node) {
  123. nasid = cnodeid_to_nasid(node);
  124. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  125. xpc_sh2_IPI_access0_sn2);
  126. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  127. xpc_sh2_IPI_access1_sn2);
  128. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  129. xpc_sh2_IPI_access2_sn2);
  130. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  131. xpc_sh2_IPI_access3_sn2);
  132. }
  133. } else {
  134. for_each_online_node(node) {
  135. nasid = cnodeid_to_nasid(node);
  136. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  137. xpc_sh1_IPI_access_sn2);
  138. }
  139. }
  140. }
  141. /*
  142. * The following set of functions are used for the sending and receiving of
  143. * IRQs (also known as IPIs). There are two flavors of IRQs, one that is
  144. * associated with partition activity (SGI_XPC_ACTIVATE) and the other that
  145. * is associated with channel activity (SGI_XPC_NOTIFY).
  146. */
  147. static u64
  148. xpc_receive_IRQ_amo_sn2(struct amo *amo)
  149. {
  150. return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
  151. }
  152. static enum xp_retval
  153. xpc_send_IRQ_sn2(struct amo *amo, u64 flag, int nasid, int phys_cpuid,
  154. int vector)
  155. {
  156. int ret = 0;
  157. unsigned long irq_flags;
  158. local_irq_save(irq_flags);
  159. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
  160. sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
  161. /*
  162. * We must always use the nofault function regardless of whether we
  163. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  164. * didn't, we'd never know that the other partition is down and would
  165. * keep sending IRQs and amos to it until the heartbeat times out.
  166. */
  167. ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
  168. xp_nofault_PIOR_target));
  169. local_irq_restore(irq_flags);
  170. return (ret == 0) ? xpSuccess : xpPioReadError;
  171. }
  172. static struct amo *
  173. xpc_init_IRQ_amo_sn2(int index)
  174. {
  175. struct amo *amo = xpc_vars_sn2->amos_page + index;
  176. (void)xpc_receive_IRQ_amo_sn2(amo); /* clear amo variable */
  177. return amo;
  178. }
  179. /*
  180. * Functions associated with SGI_XPC_ACTIVATE IRQ.
  181. */
  182. /*
  183. * Notify the heartbeat check thread that an activate IRQ has been received.
  184. */
  185. static irqreturn_t
  186. xpc_handle_activate_IRQ_sn2(int irq, void *dev_id)
  187. {
  188. unsigned long irq_flags;
  189. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  190. xpc_activate_IRQ_rcvd++;
  191. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  192. wake_up_interruptible(&xpc_activate_IRQ_wq);
  193. return IRQ_HANDLED;
  194. }
  195. /*
  196. * Flag the appropriate amo variable and send an IRQ to the specified node.
  197. */
  198. static void
  199. xpc_send_activate_IRQ_sn2(unsigned long amos_page_pa, int from_nasid,
  200. int to_nasid, int to_phys_cpuid)
  201. {
  202. struct amo *amos = (struct amo *)__va(amos_page_pa +
  203. (XPC_ACTIVATE_IRQ_AMOS_SN2 *
  204. sizeof(struct amo)));
  205. (void)xpc_send_IRQ_sn2(&amos[BIT_WORD(from_nasid / 2)],
  206. BIT_MASK(from_nasid / 2), to_nasid,
  207. to_phys_cpuid, SGI_XPC_ACTIVATE);
  208. }
  209. static void
  210. xpc_send_local_activate_IRQ_sn2(int from_nasid)
  211. {
  212. unsigned long irq_flags;
  213. struct amo *amos = (struct amo *)__va(xpc_vars_sn2->amos_page_pa +
  214. (XPC_ACTIVATE_IRQ_AMOS_SN2 *
  215. sizeof(struct amo)));
  216. /* fake the sending and receipt of an activate IRQ from remote nasid */
  217. FETCHOP_STORE_OP(TO_AMO((u64)&amos[BIT_WORD(from_nasid / 2)].variable),
  218. FETCHOP_OR, BIT_MASK(from_nasid / 2));
  219. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  220. xpc_activate_IRQ_rcvd++;
  221. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  222. wake_up_interruptible(&xpc_activate_IRQ_wq);
  223. }
  224. /*
  225. * Functions associated with SGI_XPC_NOTIFY IRQ.
  226. */
  227. /*
  228. * Check to see if any chctl flags were sent from the specified partition.
  229. */
  230. static void
  231. xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
  232. {
  233. union xpc_channel_ctl_flags chctl;
  234. unsigned long irq_flags;
  235. chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
  236. local_chctl_amo_va);
  237. if (chctl.all_flags == 0)
  238. return;
  239. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  240. part->chctl.all_flags |= chctl.all_flags;
  241. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  242. dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
  243. "0x%llx\n", XPC_PARTID(part), chctl.all_flags);
  244. xpc_wakeup_channel_mgr(part);
  245. }
  246. /*
  247. * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
  248. * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
  249. * than one partition, we use an amo structure per partition to indicate
  250. * whether a partition has sent an IRQ or not. If it has, then wake up the
  251. * associated kthread to handle it.
  252. *
  253. * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
  254. * running on other partitions.
  255. *
  256. * Noteworthy Arguments:
  257. *
  258. * irq - Interrupt ReQuest number. NOT USED.
  259. *
  260. * dev_id - partid of IRQ's potential sender.
  261. */
  262. static irqreturn_t
  263. xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
  264. {
  265. short partid = (short)(u64)dev_id;
  266. struct xpc_partition *part = &xpc_partitions[partid];
  267. DBUG_ON(partid < 0 || partid >= XP_MAX_NPARTITIONS_SN2);
  268. if (xpc_part_ref(part)) {
  269. xpc_check_for_sent_chctl_flags_sn2(part);
  270. xpc_part_deref(part);
  271. }
  272. return IRQ_HANDLED;
  273. }
  274. /*
  275. * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
  276. * because the write to their associated amo variable completed after the IRQ
  277. * was received.
  278. */
  279. static void
  280. xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
  281. {
  282. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  283. if (xpc_part_ref(part)) {
  284. xpc_check_for_sent_chctl_flags_sn2(part);
  285. part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
  286. XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
  287. add_timer(&part_sn2->dropped_notify_IRQ_timer);
  288. xpc_part_deref(part);
  289. }
  290. }
  291. /*
  292. * Send a notify IRQ to the remote partition that is associated with the
  293. * specified channel.
  294. */
  295. static void
  296. xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
  297. char *chctl_flag_string, unsigned long *irq_flags)
  298. {
  299. struct xpc_partition *part = &xpc_partitions[ch->partid];
  300. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  301. union xpc_channel_ctl_flags chctl = { 0 };
  302. enum xp_retval ret;
  303. if (likely(part->act_state != XPC_P_AS_DEACTIVATING)) {
  304. chctl.flags[ch->number] = chctl_flag;
  305. ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
  306. chctl.all_flags,
  307. part_sn2->notify_IRQ_nasid,
  308. part_sn2->notify_IRQ_phys_cpuid,
  309. SGI_XPC_NOTIFY);
  310. dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
  311. chctl_flag_string, ch->partid, ch->number, ret);
  312. if (unlikely(ret != xpSuccess)) {
  313. if (irq_flags != NULL)
  314. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  315. XPC_DEACTIVATE_PARTITION(part, ret);
  316. if (irq_flags != NULL)
  317. spin_lock_irqsave(&ch->lock, *irq_flags);
  318. }
  319. }
  320. }
  321. #define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
  322. xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
  323. /*
  324. * Make it look like the remote partition, which is associated with the
  325. * specified channel, sent us a notify IRQ. This faked IRQ will be handled
  326. * by xpc_check_for_dropped_notify_IRQ_sn2().
  327. */
  328. static void
  329. xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
  330. char *chctl_flag_string)
  331. {
  332. struct xpc_partition *part = &xpc_partitions[ch->partid];
  333. union xpc_channel_ctl_flags chctl = { 0 };
  334. chctl.flags[ch->number] = chctl_flag;
  335. FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
  336. variable), FETCHOP_OR, chctl.all_flags);
  337. dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
  338. chctl_flag_string, ch->partid, ch->number);
  339. }
  340. #define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
  341. xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
  342. static void
  343. xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
  344. unsigned long *irq_flags)
  345. {
  346. struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
  347. args->reason = ch->reason;
  348. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
  349. }
  350. static void
  351. xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  352. {
  353. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
  354. }
  355. static void
  356. xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  357. {
  358. struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
  359. args->entry_size = ch->entry_size;
  360. args->local_nentries = ch->local_nentries;
  361. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
  362. }
  363. static void
  364. xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  365. {
  366. struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
  367. args->remote_nentries = ch->remote_nentries;
  368. args->local_nentries = ch->local_nentries;
  369. args->local_msgqueue_pa = xp_pa(ch->sn.sn2.local_msgqueue);
  370. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
  371. }
  372. static void
  373. xpc_send_chctl_opencomplete_sn2(struct xpc_channel *ch,
  374. unsigned long *irq_flags)
  375. {
  376. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENCOMPLETE, irq_flags);
  377. }
  378. static void
  379. xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
  380. {
  381. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
  382. }
  383. static void
  384. xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
  385. {
  386. XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
  387. }
  388. static enum xp_retval
  389. xpc_save_remote_msgqueue_pa_sn2(struct xpc_channel *ch,
  390. unsigned long msgqueue_pa)
  391. {
  392. ch->sn.sn2.remote_msgqueue_pa = msgqueue_pa;
  393. return xpSuccess;
  394. }
  395. /*
  396. * This next set of functions are used to keep track of when a partition is
  397. * potentially engaged in accessing memory belonging to another partition.
  398. */
  399. static void
  400. xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
  401. {
  402. unsigned long irq_flags;
  403. struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
  404. (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
  405. sizeof(struct amo)));
  406. local_irq_save(irq_flags);
  407. /* set bit corresponding to our partid in remote partition's amo */
  408. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
  409. BIT(sn_partition_id));
  410. /*
  411. * We must always use the nofault function regardless of whether we
  412. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  413. * didn't, we'd never know that the other partition is down and would
  414. * keep sending IRQs and amos to it until the heartbeat times out.
  415. */
  416. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  417. variable),
  418. xp_nofault_PIOR_target));
  419. local_irq_restore(irq_flags);
  420. }
  421. static void
  422. xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
  423. {
  424. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  425. unsigned long irq_flags;
  426. struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
  427. (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
  428. sizeof(struct amo)));
  429. local_irq_save(irq_flags);
  430. /* clear bit corresponding to our partid in remote partition's amo */
  431. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  432. ~BIT(sn_partition_id));
  433. /*
  434. * We must always use the nofault function regardless of whether we
  435. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  436. * didn't, we'd never know that the other partition is down and would
  437. * keep sending IRQs and amos to it until the heartbeat times out.
  438. */
  439. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  440. variable),
  441. xp_nofault_PIOR_target));
  442. local_irq_restore(irq_flags);
  443. /*
  444. * Send activate IRQ to get other side to see that we've cleared our
  445. * bit in their engaged partitions amo.
  446. */
  447. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  448. cnodeid_to_nasid(0),
  449. part_sn2->activate_IRQ_nasid,
  450. part_sn2->activate_IRQ_phys_cpuid);
  451. }
  452. static void
  453. xpc_assume_partition_disengaged_sn2(short partid)
  454. {
  455. struct amo *amo = xpc_vars_sn2->amos_page +
  456. XPC_ENGAGED_PARTITIONS_AMO_SN2;
  457. /* clear bit(s) based on partid mask in our partition's amo */
  458. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  459. ~BIT(partid));
  460. }
  461. static int
  462. xpc_partition_engaged_sn2(short partid)
  463. {
  464. struct amo *amo = xpc_vars_sn2->amos_page +
  465. XPC_ENGAGED_PARTITIONS_AMO_SN2;
  466. /* our partition's amo variable ANDed with partid mask */
  467. return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
  468. BIT(partid)) != 0;
  469. }
  470. static int
  471. xpc_any_partition_engaged_sn2(void)
  472. {
  473. struct amo *amo = xpc_vars_sn2->amos_page +
  474. XPC_ENGAGED_PARTITIONS_AMO_SN2;
  475. /* our partition's amo variable */
  476. return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
  477. }
  478. /* original protection values for each node */
  479. static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
  480. /*
  481. * Change protections to allow amo operations on non-Shub 1.1 systems.
  482. */
  483. static enum xp_retval
  484. xpc_allow_amo_ops_sn2(struct amo *amos_page)
  485. {
  486. enum xp_retval ret = xpSuccess;
  487. /*
  488. * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
  489. * collides with memory operations. On those systems we call
  490. * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
  491. */
  492. if (!enable_shub_wars_1_1())
  493. ret = xp_expand_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE);
  494. return ret;
  495. }
  496. /*
  497. * Change protections to allow amo operations on Shub 1.1 systems.
  498. */
  499. static void
  500. xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
  501. {
  502. int node;
  503. int nasid;
  504. if (!enable_shub_wars_1_1())
  505. return;
  506. for_each_online_node(node) {
  507. nasid = cnodeid_to_nasid(node);
  508. /* save current protection values */
  509. xpc_prot_vec_sn2[node] =
  510. (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
  511. SH1_MD_DQLP_MMR_DIR_PRIVEC0));
  512. /* open up everything */
  513. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  514. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  515. -1UL);
  516. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  517. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  518. -1UL);
  519. }
  520. }
  521. static enum xp_retval
  522. xpc_get_partition_rsvd_page_pa_sn2(void *buf, u64 *cookie, unsigned long *rp_pa,
  523. size_t *len)
  524. {
  525. s64 status;
  526. enum xp_retval ret;
  527. status = sn_partition_reserved_page_pa((u64)buf, cookie,
  528. (u64 *)rp_pa, (u64 *)len);
  529. if (status == SALRET_OK)
  530. ret = xpSuccess;
  531. else if (status == SALRET_MORE_PASSES)
  532. ret = xpNeedMoreInfo;
  533. else
  534. ret = xpSalError;
  535. return ret;
  536. }
  537. static int
  538. xpc_setup_rsvd_page_sn2(struct xpc_rsvd_page *rp)
  539. {
  540. struct amo *amos_page;
  541. int i;
  542. int ret;
  543. xpc_vars_sn2 = XPC_RP_VARS(rp);
  544. rp->sn.sn2.vars_pa = xp_pa(xpc_vars_sn2);
  545. /* vars_part array follows immediately after vars */
  546. xpc_vars_part_sn2 = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
  547. XPC_RP_VARS_SIZE);
  548. /*
  549. * Before clearing xpc_vars_sn2, see if a page of amos had been
  550. * previously allocated. If not we'll need to allocate one and set
  551. * permissions so that cross-partition amos are allowed.
  552. *
  553. * The allocated amo page needs MCA reporting to remain disabled after
  554. * XPC has unloaded. To make this work, we keep a copy of the pointer
  555. * to this page (i.e., amos_page) in the struct xpc_vars_sn2 structure,
  556. * which is pointed to by the reserved page, and re-use that saved copy
  557. * on subsequent loads of XPC. This amo page is never freed, and its
  558. * memory protections are never restricted.
  559. */
  560. amos_page = xpc_vars_sn2->amos_page;
  561. if (amos_page == NULL) {
  562. amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
  563. if (amos_page == NULL) {
  564. dev_err(xpc_part, "can't allocate page of amos\n");
  565. return -ENOMEM;
  566. }
  567. /*
  568. * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
  569. * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
  570. */
  571. ret = xpc_allow_amo_ops_sn2(amos_page);
  572. if (ret != xpSuccess) {
  573. dev_err(xpc_part, "can't allow amo operations\n");
  574. uncached_free_page(__IA64_UNCACHED_OFFSET |
  575. TO_PHYS((u64)amos_page), 1);
  576. return -EPERM;
  577. }
  578. }
  579. /* clear xpc_vars_sn2 */
  580. memset(xpc_vars_sn2, 0, sizeof(struct xpc_vars_sn2));
  581. xpc_vars_sn2->version = XPC_V_VERSION;
  582. xpc_vars_sn2->activate_IRQ_nasid = cpuid_to_nasid(0);
  583. xpc_vars_sn2->activate_IRQ_phys_cpuid = cpu_physical_id(0);
  584. xpc_vars_sn2->vars_part_pa = xp_pa(xpc_vars_part_sn2);
  585. xpc_vars_sn2->amos_page_pa = ia64_tpa((u64)amos_page);
  586. xpc_vars_sn2->amos_page = amos_page; /* save for next load of XPC */
  587. /* clear xpc_vars_part_sn2 */
  588. memset((u64 *)xpc_vars_part_sn2, 0, sizeof(struct xpc_vars_part_sn2) *
  589. XP_MAX_NPARTITIONS_SN2);
  590. /* initialize the activate IRQ related amo variables */
  591. for (i = 0; i < xpc_nasid_mask_nlongs; i++)
  592. (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
  593. /* initialize the engaged remote partitions related amo variables */
  594. (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
  595. (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
  596. return 0;
  597. }
  598. static int
  599. xpc_hb_allowed_sn2(short partid, void *heartbeating_to_mask)
  600. {
  601. return test_bit(partid, heartbeating_to_mask);
  602. }
  603. static void
  604. xpc_allow_hb_sn2(short partid)
  605. {
  606. DBUG_ON(xpc_vars_sn2 == NULL);
  607. set_bit(partid, xpc_vars_sn2->heartbeating_to_mask);
  608. }
  609. static void
  610. xpc_disallow_hb_sn2(short partid)
  611. {
  612. DBUG_ON(xpc_vars_sn2 == NULL);
  613. clear_bit(partid, xpc_vars_sn2->heartbeating_to_mask);
  614. }
  615. static void
  616. xpc_disallow_all_hbs_sn2(void)
  617. {
  618. DBUG_ON(xpc_vars_sn2 == NULL);
  619. bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, xp_max_npartitions);
  620. }
  621. static void
  622. xpc_increment_heartbeat_sn2(void)
  623. {
  624. xpc_vars_sn2->heartbeat++;
  625. }
  626. static void
  627. xpc_offline_heartbeat_sn2(void)
  628. {
  629. xpc_increment_heartbeat_sn2();
  630. xpc_vars_sn2->heartbeat_offline = 1;
  631. }
  632. static void
  633. xpc_online_heartbeat_sn2(void)
  634. {
  635. xpc_increment_heartbeat_sn2();
  636. xpc_vars_sn2->heartbeat_offline = 0;
  637. }
  638. static void
  639. xpc_heartbeat_init_sn2(void)
  640. {
  641. DBUG_ON(xpc_vars_sn2 == NULL);
  642. bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
  643. xpc_online_heartbeat_sn2();
  644. }
  645. static void
  646. xpc_heartbeat_exit_sn2(void)
  647. {
  648. xpc_offline_heartbeat_sn2();
  649. }
  650. static enum xp_retval
  651. xpc_get_remote_heartbeat_sn2(struct xpc_partition *part)
  652. {
  653. struct xpc_vars_sn2 *remote_vars;
  654. enum xp_retval ret;
  655. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
  656. /* pull the remote vars structure that contains the heartbeat */
  657. ret = xp_remote_memcpy(xp_pa(remote_vars),
  658. part->sn.sn2.remote_vars_pa,
  659. XPC_RP_VARS_SIZE);
  660. if (ret != xpSuccess)
  661. return ret;
  662. dev_dbg(xpc_part, "partid=%d, heartbeat=%lld, last_heartbeat=%lld, "
  663. "heartbeat_offline=%lld, HB_mask[0]=0x%lx\n", XPC_PARTID(part),
  664. remote_vars->heartbeat, part->last_heartbeat,
  665. remote_vars->heartbeat_offline,
  666. remote_vars->heartbeating_to_mask[0]);
  667. if ((remote_vars->heartbeat == part->last_heartbeat &&
  668. !remote_vars->heartbeat_offline) ||
  669. !xpc_hb_allowed_sn2(sn_partition_id,
  670. remote_vars->heartbeating_to_mask)) {
  671. ret = xpNoHeartbeat;
  672. } else {
  673. part->last_heartbeat = remote_vars->heartbeat;
  674. }
  675. return ret;
  676. }
  677. /*
  678. * Get a copy of the remote partition's XPC variables from the reserved page.
  679. *
  680. * remote_vars points to a buffer that is cacheline aligned for BTE copies and
  681. * assumed to be of size XPC_RP_VARS_SIZE.
  682. */
  683. static enum xp_retval
  684. xpc_get_remote_vars_sn2(unsigned long remote_vars_pa,
  685. struct xpc_vars_sn2 *remote_vars)
  686. {
  687. enum xp_retval ret;
  688. if (remote_vars_pa == 0)
  689. return xpVarsNotSet;
  690. /* pull over the cross partition variables */
  691. ret = xp_remote_memcpy(xp_pa(remote_vars), remote_vars_pa,
  692. XPC_RP_VARS_SIZE);
  693. if (ret != xpSuccess)
  694. return ret;
  695. if (XPC_VERSION_MAJOR(remote_vars->version) !=
  696. XPC_VERSION_MAJOR(XPC_V_VERSION)) {
  697. return xpBadVersion;
  698. }
  699. return xpSuccess;
  700. }
  701. static void
  702. xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
  703. unsigned long remote_rp_pa, int nasid)
  704. {
  705. xpc_send_local_activate_IRQ_sn2(nasid);
  706. }
  707. static void
  708. xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
  709. {
  710. xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
  711. }
  712. static void
  713. xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
  714. {
  715. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  716. unsigned long irq_flags;
  717. struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
  718. (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
  719. sizeof(struct amo)));
  720. local_irq_save(irq_flags);
  721. /* set bit corresponding to our partid in remote partition's amo */
  722. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
  723. BIT(sn_partition_id));
  724. /*
  725. * We must always use the nofault function regardless of whether we
  726. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  727. * didn't, we'd never know that the other partition is down and would
  728. * keep sending IRQs and amos to it until the heartbeat times out.
  729. */
  730. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  731. variable),
  732. xp_nofault_PIOR_target));
  733. local_irq_restore(irq_flags);
  734. /*
  735. * Send activate IRQ to get other side to see that we've set our
  736. * bit in their deactivate request amo.
  737. */
  738. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  739. cnodeid_to_nasid(0),
  740. part_sn2->activate_IRQ_nasid,
  741. part_sn2->activate_IRQ_phys_cpuid);
  742. }
  743. static void
  744. xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
  745. {
  746. unsigned long irq_flags;
  747. struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
  748. (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
  749. sizeof(struct amo)));
  750. local_irq_save(irq_flags);
  751. /* clear bit corresponding to our partid in remote partition's amo */
  752. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  753. ~BIT(sn_partition_id));
  754. /*
  755. * We must always use the nofault function regardless of whether we
  756. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  757. * didn't, we'd never know that the other partition is down and would
  758. * keep sending IRQs and amos to it until the heartbeat times out.
  759. */
  760. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  761. variable),
  762. xp_nofault_PIOR_target));
  763. local_irq_restore(irq_flags);
  764. }
  765. static int
  766. xpc_partition_deactivation_requested_sn2(short partid)
  767. {
  768. struct amo *amo = xpc_vars_sn2->amos_page +
  769. XPC_DEACTIVATE_REQUEST_AMO_SN2;
  770. /* our partition's amo variable ANDed with partid mask */
  771. return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
  772. BIT(partid)) != 0;
  773. }
  774. /*
  775. * Update the remote partition's info.
  776. */
  777. static void
  778. xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
  779. unsigned long *remote_rp_ts_jiffies,
  780. unsigned long remote_rp_pa,
  781. unsigned long remote_vars_pa,
  782. struct xpc_vars_sn2 *remote_vars)
  783. {
  784. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  785. part->remote_rp_version = remote_rp_version;
  786. dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
  787. part->remote_rp_version);
  788. part->remote_rp_ts_jiffies = *remote_rp_ts_jiffies;
  789. dev_dbg(xpc_part, " remote_rp_ts_jiffies = 0x%016lx\n",
  790. part->remote_rp_ts_jiffies);
  791. part->remote_rp_pa = remote_rp_pa;
  792. dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
  793. part_sn2->remote_vars_pa = remote_vars_pa;
  794. dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
  795. part_sn2->remote_vars_pa);
  796. part->last_heartbeat = remote_vars->heartbeat - 1;
  797. dev_dbg(xpc_part, " last_heartbeat = 0x%016llx\n",
  798. part->last_heartbeat);
  799. part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
  800. dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
  801. part_sn2->remote_vars_part_pa);
  802. part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
  803. dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
  804. part_sn2->activate_IRQ_nasid);
  805. part_sn2->activate_IRQ_phys_cpuid =
  806. remote_vars->activate_IRQ_phys_cpuid;
  807. dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
  808. part_sn2->activate_IRQ_phys_cpuid);
  809. part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
  810. dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
  811. part_sn2->remote_amos_page_pa);
  812. part_sn2->remote_vars_version = remote_vars->version;
  813. dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
  814. part_sn2->remote_vars_version);
  815. }
  816. /*
  817. * Prior code has determined the nasid which generated a activate IRQ.
  818. * Inspect that nasid to determine if its partition needs to be activated
  819. * or deactivated.
  820. *
  821. * A partition is considered "awaiting activation" if our partition
  822. * flags indicate it is not active and it has a heartbeat. A
  823. * partition is considered "awaiting deactivation" if our partition
  824. * flags indicate it is active but it has no heartbeat or it is not
  825. * sending its heartbeat to us.
  826. *
  827. * To determine the heartbeat, the remote nasid must have a properly
  828. * initialized reserved page.
  829. */
  830. static void
  831. xpc_identify_activate_IRQ_req_sn2(int nasid)
  832. {
  833. struct xpc_rsvd_page *remote_rp;
  834. struct xpc_vars_sn2 *remote_vars;
  835. unsigned long remote_rp_pa;
  836. unsigned long remote_vars_pa;
  837. int remote_rp_version;
  838. int reactivate = 0;
  839. unsigned long remote_rp_ts_jiffies = 0;
  840. short partid;
  841. struct xpc_partition *part;
  842. struct xpc_partition_sn2 *part_sn2;
  843. enum xp_retval ret;
  844. /* pull over the reserved page structure */
  845. remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
  846. ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
  847. if (ret != xpSuccess) {
  848. dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
  849. "which sent interrupt, reason=%d\n", nasid, ret);
  850. return;
  851. }
  852. remote_vars_pa = remote_rp->sn.sn2.vars_pa;
  853. remote_rp_version = remote_rp->version;
  854. remote_rp_ts_jiffies = remote_rp->ts_jiffies;
  855. partid = remote_rp->SAL_partid;
  856. part = &xpc_partitions[partid];
  857. part_sn2 = &part->sn.sn2;
  858. /* pull over the cross partition variables */
  859. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
  860. ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
  861. if (ret != xpSuccess) {
  862. dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
  863. "which sent interrupt, reason=%d\n", nasid, ret);
  864. XPC_DEACTIVATE_PARTITION(part, ret);
  865. return;
  866. }
  867. part->activate_IRQ_rcvd++;
  868. dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
  869. "%lld:0x%lx\n", (int)nasid, (int)partid,
  870. part->activate_IRQ_rcvd,
  871. remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
  872. if (xpc_partition_disengaged(part) &&
  873. part->act_state == XPC_P_AS_INACTIVE) {
  874. xpc_update_partition_info_sn2(part, remote_rp_version,
  875. &remote_rp_ts_jiffies,
  876. remote_rp_pa, remote_vars_pa,
  877. remote_vars);
  878. if (xpc_partition_deactivation_requested_sn2(partid)) {
  879. /*
  880. * Other side is waiting on us to deactivate even though
  881. * we already have.
  882. */
  883. return;
  884. }
  885. xpc_activate_partition(part);
  886. return;
  887. }
  888. DBUG_ON(part->remote_rp_version == 0);
  889. DBUG_ON(part_sn2->remote_vars_version == 0);
  890. if (remote_rp_ts_jiffies != part->remote_rp_ts_jiffies) {
  891. /* the other side rebooted */
  892. DBUG_ON(xpc_partition_engaged_sn2(partid));
  893. DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
  894. xpc_update_partition_info_sn2(part, remote_rp_version,
  895. &remote_rp_ts_jiffies,
  896. remote_rp_pa, remote_vars_pa,
  897. remote_vars);
  898. reactivate = 1;
  899. }
  900. if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
  901. /* still waiting on other side to disengage from us */
  902. return;
  903. }
  904. if (reactivate)
  905. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  906. else if (xpc_partition_deactivation_requested_sn2(partid))
  907. XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
  908. }
  909. /*
  910. * Loop through the activation amo variables and process any bits
  911. * which are set. Each bit indicates a nasid sending a partition
  912. * activation or deactivation request.
  913. *
  914. * Return #of IRQs detected.
  915. */
  916. int
  917. xpc_identify_activate_IRQ_sender_sn2(void)
  918. {
  919. int l;
  920. int b;
  921. unsigned long nasid_mask_long;
  922. u64 nasid; /* remote nasid */
  923. int n_IRQs_detected = 0;
  924. struct amo *act_amos;
  925. act_amos = xpc_vars_sn2->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
  926. /* scan through activate amo variables looking for non-zero entries */
  927. for (l = 0; l < xpc_nasid_mask_nlongs; l++) {
  928. if (xpc_exiting)
  929. break;
  930. nasid_mask_long = xpc_receive_IRQ_amo_sn2(&act_amos[l]);
  931. b = find_first_bit(&nasid_mask_long, BITS_PER_LONG);
  932. if (b >= BITS_PER_LONG) {
  933. /* no IRQs from nasids in this amo variable */
  934. continue;
  935. }
  936. dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", l,
  937. nasid_mask_long);
  938. /*
  939. * If this nasid has been added to the machine since
  940. * our partition was reset, this will retain the
  941. * remote nasid in our reserved pages machine mask.
  942. * This is used in the event of module reload.
  943. */
  944. xpc_mach_nasids[l] |= nasid_mask_long;
  945. /* locate the nasid(s) which sent interrupts */
  946. do {
  947. n_IRQs_detected++;
  948. nasid = (l * BITS_PER_LONG + b) * 2;
  949. dev_dbg(xpc_part, "interrupt from nasid %lld\n", nasid);
  950. xpc_identify_activate_IRQ_req_sn2(nasid);
  951. b = find_next_bit(&nasid_mask_long, BITS_PER_LONG,
  952. b + 1);
  953. } while (b < BITS_PER_LONG);
  954. }
  955. return n_IRQs_detected;
  956. }
  957. static void
  958. xpc_process_activate_IRQ_rcvd_sn2(void)
  959. {
  960. unsigned long irq_flags;
  961. int n_IRQs_expected;
  962. int n_IRQs_detected;
  963. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  964. n_IRQs_expected = xpc_activate_IRQ_rcvd;
  965. xpc_activate_IRQ_rcvd = 0;
  966. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  967. n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
  968. if (n_IRQs_detected < n_IRQs_expected) {
  969. /* retry once to help avoid missing amo */
  970. (void)xpc_identify_activate_IRQ_sender_sn2();
  971. }
  972. }
  973. /*
  974. * Setup the channel structures that are sn2 specific.
  975. */
  976. static enum xp_retval
  977. xpc_setup_ch_structures_sn2(struct xpc_partition *part)
  978. {
  979. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  980. struct xpc_channel_sn2 *ch_sn2;
  981. enum xp_retval retval;
  982. int ret;
  983. int cpuid;
  984. int ch_number;
  985. struct timer_list *timer;
  986. short partid = XPC_PARTID(part);
  987. /* allocate all the required GET/PUT values */
  988. part_sn2->local_GPs =
  989. xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE, GFP_KERNEL,
  990. &part_sn2->local_GPs_base);
  991. if (part_sn2->local_GPs == NULL) {
  992. dev_err(xpc_chan, "can't get memory for local get/put "
  993. "values\n");
  994. return xpNoMemory;
  995. }
  996. part_sn2->remote_GPs =
  997. xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE, GFP_KERNEL,
  998. &part_sn2->remote_GPs_base);
  999. if (part_sn2->remote_GPs == NULL) {
  1000. dev_err(xpc_chan, "can't get memory for remote get/put "
  1001. "values\n");
  1002. retval = xpNoMemory;
  1003. goto out_1;
  1004. }
  1005. part_sn2->remote_GPs_pa = 0;
  1006. /* allocate all the required open and close args */
  1007. part_sn2->local_openclose_args =
  1008. xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
  1009. GFP_KERNEL, &part_sn2->
  1010. local_openclose_args_base);
  1011. if (part_sn2->local_openclose_args == NULL) {
  1012. dev_err(xpc_chan, "can't get memory for local connect args\n");
  1013. retval = xpNoMemory;
  1014. goto out_2;
  1015. }
  1016. part_sn2->remote_openclose_args_pa = 0;
  1017. part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
  1018. part_sn2->notify_IRQ_nasid = 0;
  1019. part_sn2->notify_IRQ_phys_cpuid = 0;
  1020. part_sn2->remote_chctl_amo_va = NULL;
  1021. sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
  1022. ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
  1023. IRQF_SHARED, part_sn2->notify_IRQ_owner,
  1024. (void *)(u64)partid);
  1025. if (ret != 0) {
  1026. dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
  1027. "errno=%d\n", -ret);
  1028. retval = xpLackOfResources;
  1029. goto out_3;
  1030. }
  1031. /* Setup a timer to check for dropped notify IRQs */
  1032. timer = &part_sn2->dropped_notify_IRQ_timer;
  1033. init_timer(timer);
  1034. timer->function =
  1035. (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
  1036. timer->data = (unsigned long)part;
  1037. timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
  1038. add_timer(timer);
  1039. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  1040. ch_sn2 = &part->channels[ch_number].sn.sn2;
  1041. ch_sn2->local_GP = &part_sn2->local_GPs[ch_number];
  1042. ch_sn2->local_openclose_args =
  1043. &part_sn2->local_openclose_args[ch_number];
  1044. mutex_init(&ch_sn2->msg_to_pull_mutex);
  1045. }
  1046. /*
  1047. * Setup the per partition specific variables required by the
  1048. * remote partition to establish channel connections with us.
  1049. *
  1050. * The setting of the magic # indicates that these per partition
  1051. * specific variables are ready to be used.
  1052. */
  1053. xpc_vars_part_sn2[partid].GPs_pa = xp_pa(part_sn2->local_GPs);
  1054. xpc_vars_part_sn2[partid].openclose_args_pa =
  1055. xp_pa(part_sn2->local_openclose_args);
  1056. xpc_vars_part_sn2[partid].chctl_amo_pa =
  1057. xp_pa(part_sn2->local_chctl_amo_va);
  1058. cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
  1059. xpc_vars_part_sn2[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
  1060. xpc_vars_part_sn2[partid].notify_IRQ_phys_cpuid =
  1061. cpu_physical_id(cpuid);
  1062. xpc_vars_part_sn2[partid].nchannels = part->nchannels;
  1063. xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC1_SN2;
  1064. return xpSuccess;
  1065. /* setup of ch structures failed */
  1066. out_3:
  1067. kfree(part_sn2->local_openclose_args_base);
  1068. part_sn2->local_openclose_args = NULL;
  1069. out_2:
  1070. kfree(part_sn2->remote_GPs_base);
  1071. part_sn2->remote_GPs = NULL;
  1072. out_1:
  1073. kfree(part_sn2->local_GPs_base);
  1074. part_sn2->local_GPs = NULL;
  1075. return retval;
  1076. }
  1077. /*
  1078. * Teardown the channel structures that are sn2 specific.
  1079. */
  1080. static void
  1081. xpc_teardown_ch_structures_sn2(struct xpc_partition *part)
  1082. {
  1083. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1084. short partid = XPC_PARTID(part);
  1085. /*
  1086. * Indicate that the variables specific to the remote partition are no
  1087. * longer available for its use.
  1088. */
  1089. xpc_vars_part_sn2[partid].magic = 0;
  1090. /* in case we've still got outstanding timers registered... */
  1091. del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
  1092. free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
  1093. kfree(part_sn2->local_openclose_args_base);
  1094. part_sn2->local_openclose_args = NULL;
  1095. kfree(part_sn2->remote_GPs_base);
  1096. part_sn2->remote_GPs = NULL;
  1097. kfree(part_sn2->local_GPs_base);
  1098. part_sn2->local_GPs = NULL;
  1099. part_sn2->local_chctl_amo_va = NULL;
  1100. }
  1101. /*
  1102. * Create a wrapper that hides the underlying mechanism for pulling a cacheline
  1103. * (or multiple cachelines) from a remote partition.
  1104. *
  1105. * src_pa must be a cacheline aligned physical address on the remote partition.
  1106. * dst must be a cacheline aligned virtual address on this partition.
  1107. * cnt must be cacheline sized
  1108. */
  1109. /* ??? Replace this function by call to xp_remote_memcpy() or bte_copy()? */
  1110. static enum xp_retval
  1111. xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
  1112. const unsigned long src_pa, size_t cnt)
  1113. {
  1114. enum xp_retval ret;
  1115. DBUG_ON(src_pa != L1_CACHE_ALIGN(src_pa));
  1116. DBUG_ON((unsigned long)dst != L1_CACHE_ALIGN((unsigned long)dst));
  1117. DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
  1118. if (part->act_state == XPC_P_AS_DEACTIVATING)
  1119. return part->reason;
  1120. ret = xp_remote_memcpy(xp_pa(dst), src_pa, cnt);
  1121. if (ret != xpSuccess) {
  1122. dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
  1123. " ret=%d\n", XPC_PARTID(part), ret);
  1124. }
  1125. return ret;
  1126. }
  1127. /*
  1128. * Pull the remote per partition specific variables from the specified
  1129. * partition.
  1130. */
  1131. static enum xp_retval
  1132. xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
  1133. {
  1134. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1135. u8 buffer[L1_CACHE_BYTES * 2];
  1136. struct xpc_vars_part_sn2 *pulled_entry_cacheline =
  1137. (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
  1138. struct xpc_vars_part_sn2 *pulled_entry;
  1139. unsigned long remote_entry_cacheline_pa;
  1140. unsigned long remote_entry_pa;
  1141. short partid = XPC_PARTID(part);
  1142. enum xp_retval ret;
  1143. /* pull the cacheline that contains the variables we're interested in */
  1144. DBUG_ON(part_sn2->remote_vars_part_pa !=
  1145. L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
  1146. DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
  1147. remote_entry_pa = part_sn2->remote_vars_part_pa +
  1148. sn_partition_id * sizeof(struct xpc_vars_part_sn2);
  1149. remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
  1150. pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
  1151. + (remote_entry_pa &
  1152. (L1_CACHE_BYTES - 1)));
  1153. ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
  1154. remote_entry_cacheline_pa,
  1155. L1_CACHE_BYTES);
  1156. if (ret != xpSuccess) {
  1157. dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
  1158. "partition %d, ret=%d\n", partid, ret);
  1159. return ret;
  1160. }
  1161. /* see if they've been set up yet */
  1162. if (pulled_entry->magic != XPC_VP_MAGIC1_SN2 &&
  1163. pulled_entry->magic != XPC_VP_MAGIC2_SN2) {
  1164. if (pulled_entry->magic != 0) {
  1165. dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
  1166. "partition %d has bad magic value (=0x%llx)\n",
  1167. partid, sn_partition_id, pulled_entry->magic);
  1168. return xpBadMagic;
  1169. }
  1170. /* they've not been initialized yet */
  1171. return xpRetry;
  1172. }
  1173. if (xpc_vars_part_sn2[partid].magic == XPC_VP_MAGIC1_SN2) {
  1174. /* validate the variables */
  1175. if (pulled_entry->GPs_pa == 0 ||
  1176. pulled_entry->openclose_args_pa == 0 ||
  1177. pulled_entry->chctl_amo_pa == 0) {
  1178. dev_err(xpc_chan, "partition %d's XPC vars_part for "
  1179. "partition %d are not valid\n", partid,
  1180. sn_partition_id);
  1181. return xpInvalidAddress;
  1182. }
  1183. /* the variables we imported look to be valid */
  1184. part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
  1185. part_sn2->remote_openclose_args_pa =
  1186. pulled_entry->openclose_args_pa;
  1187. part_sn2->remote_chctl_amo_va =
  1188. (struct amo *)__va(pulled_entry->chctl_amo_pa);
  1189. part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
  1190. part_sn2->notify_IRQ_phys_cpuid =
  1191. pulled_entry->notify_IRQ_phys_cpuid;
  1192. if (part->nchannels > pulled_entry->nchannels)
  1193. part->nchannels = pulled_entry->nchannels;
  1194. /* let the other side know that we've pulled their variables */
  1195. xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC2_SN2;
  1196. }
  1197. if (pulled_entry->magic == XPC_VP_MAGIC1_SN2)
  1198. return xpRetry;
  1199. return xpSuccess;
  1200. }
  1201. /*
  1202. * Establish first contact with the remote partititon. This involves pulling
  1203. * the XPC per partition variables from the remote partition and waiting for
  1204. * the remote partition to pull ours.
  1205. */
  1206. static enum xp_retval
  1207. xpc_make_first_contact_sn2(struct xpc_partition *part)
  1208. {
  1209. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1210. enum xp_retval ret;
  1211. /*
  1212. * Register the remote partition's amos with SAL so it can handle
  1213. * and cleanup errors within that address range should the remote
  1214. * partition go down. We don't unregister this range because it is
  1215. * difficult to tell when outstanding writes to the remote partition
  1216. * are finished and thus when it is safe to unregister. This should
  1217. * not result in wasted space in the SAL xp_addr_region table because
  1218. * we should get the same page for remote_amos_page_pa after module
  1219. * reloads and system reboots.
  1220. */
  1221. if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
  1222. PAGE_SIZE, 1) < 0) {
  1223. dev_warn(xpc_part, "xpc_activating(%d) failed to register "
  1224. "xp_addr region\n", XPC_PARTID(part));
  1225. ret = xpPhysAddrRegFailed;
  1226. XPC_DEACTIVATE_PARTITION(part, ret);
  1227. return ret;
  1228. }
  1229. /*
  1230. * Send activate IRQ to get other side to activate if they've not
  1231. * already begun to do so.
  1232. */
  1233. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  1234. cnodeid_to_nasid(0),
  1235. part_sn2->activate_IRQ_nasid,
  1236. part_sn2->activate_IRQ_phys_cpuid);
  1237. while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
  1238. if (ret != xpRetry) {
  1239. XPC_DEACTIVATE_PARTITION(part, ret);
  1240. return ret;
  1241. }
  1242. dev_dbg(xpc_part, "waiting to make first contact with "
  1243. "partition %d\n", XPC_PARTID(part));
  1244. /* wait a 1/4 of a second or so */
  1245. (void)msleep_interruptible(250);
  1246. if (part->act_state == XPC_P_AS_DEACTIVATING)
  1247. return part->reason;
  1248. }
  1249. return xpSuccess;
  1250. }
  1251. /*
  1252. * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
  1253. */
  1254. static u64
  1255. xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
  1256. {
  1257. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1258. unsigned long irq_flags;
  1259. union xpc_channel_ctl_flags chctl;
  1260. enum xp_retval ret;
  1261. /*
  1262. * See if there are any chctl flags to be handled.
  1263. */
  1264. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  1265. chctl = part->chctl;
  1266. if (chctl.all_flags != 0)
  1267. part->chctl.all_flags = 0;
  1268. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  1269. if (xpc_any_openclose_chctl_flags_set(&chctl)) {
  1270. ret = xpc_pull_remote_cachelines_sn2(part, part->
  1271. remote_openclose_args,
  1272. part_sn2->
  1273. remote_openclose_args_pa,
  1274. XPC_OPENCLOSE_ARGS_SIZE);
  1275. if (ret != xpSuccess) {
  1276. XPC_DEACTIVATE_PARTITION(part, ret);
  1277. dev_dbg(xpc_chan, "failed to pull openclose args from "
  1278. "partition %d, ret=%d\n", XPC_PARTID(part),
  1279. ret);
  1280. /* don't bother processing chctl flags anymore */
  1281. chctl.all_flags = 0;
  1282. }
  1283. }
  1284. if (xpc_any_msg_chctl_flags_set(&chctl)) {
  1285. ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
  1286. part_sn2->remote_GPs_pa,
  1287. XPC_GP_SIZE);
  1288. if (ret != xpSuccess) {
  1289. XPC_DEACTIVATE_PARTITION(part, ret);
  1290. dev_dbg(xpc_chan, "failed to pull GPs from partition "
  1291. "%d, ret=%d\n", XPC_PARTID(part), ret);
  1292. /* don't bother processing chctl flags anymore */
  1293. chctl.all_flags = 0;
  1294. }
  1295. }
  1296. return chctl.all_flags;
  1297. }
  1298. /*
  1299. * Allocate the local message queue and the notify queue.
  1300. */
  1301. static enum xp_retval
  1302. xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
  1303. {
  1304. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1305. unsigned long irq_flags;
  1306. int nentries;
  1307. size_t nbytes;
  1308. for (nentries = ch->local_nentries; nentries > 0; nentries--) {
  1309. nbytes = nentries * ch->entry_size;
  1310. ch_sn2->local_msgqueue =
  1311. xpc_kzalloc_cacheline_aligned(nbytes, GFP_KERNEL,
  1312. &ch_sn2->local_msgqueue_base);
  1313. if (ch_sn2->local_msgqueue == NULL)
  1314. continue;
  1315. nbytes = nentries * sizeof(struct xpc_notify_sn2);
  1316. ch_sn2->notify_queue = kzalloc(nbytes, GFP_KERNEL);
  1317. if (ch_sn2->notify_queue == NULL) {
  1318. kfree(ch_sn2->local_msgqueue_base);
  1319. ch_sn2->local_msgqueue = NULL;
  1320. continue;
  1321. }
  1322. spin_lock_irqsave(&ch->lock, irq_flags);
  1323. if (nentries < ch->local_nentries) {
  1324. dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
  1325. "partid=%d, channel=%d\n", nentries,
  1326. ch->local_nentries, ch->partid, ch->number);
  1327. ch->local_nentries = nentries;
  1328. }
  1329. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1330. return xpSuccess;
  1331. }
  1332. dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
  1333. "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
  1334. return xpNoMemory;
  1335. }
  1336. /*
  1337. * Allocate the cached remote message queue.
  1338. */
  1339. static enum xp_retval
  1340. xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
  1341. {
  1342. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1343. unsigned long irq_flags;
  1344. int nentries;
  1345. size_t nbytes;
  1346. DBUG_ON(ch->remote_nentries <= 0);
  1347. for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
  1348. nbytes = nentries * ch->entry_size;
  1349. ch_sn2->remote_msgqueue =
  1350. xpc_kzalloc_cacheline_aligned(nbytes, GFP_KERNEL, &ch_sn2->
  1351. remote_msgqueue_base);
  1352. if (ch_sn2->remote_msgqueue == NULL)
  1353. continue;
  1354. spin_lock_irqsave(&ch->lock, irq_flags);
  1355. if (nentries < ch->remote_nentries) {
  1356. dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
  1357. "partid=%d, channel=%d\n", nentries,
  1358. ch->remote_nentries, ch->partid, ch->number);
  1359. ch->remote_nentries = nentries;
  1360. }
  1361. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1362. return xpSuccess;
  1363. }
  1364. dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
  1365. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1366. return xpNoMemory;
  1367. }
  1368. /*
  1369. * Allocate message queues and other stuff associated with a channel.
  1370. *
  1371. * Note: Assumes all of the channel sizes are filled in.
  1372. */
  1373. static enum xp_retval
  1374. xpc_setup_msg_structures_sn2(struct xpc_channel *ch)
  1375. {
  1376. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1377. enum xp_retval ret;
  1378. DBUG_ON(ch->flags & XPC_C_SETUP);
  1379. ret = xpc_allocate_local_msgqueue_sn2(ch);
  1380. if (ret == xpSuccess) {
  1381. ret = xpc_allocate_remote_msgqueue_sn2(ch);
  1382. if (ret != xpSuccess) {
  1383. kfree(ch_sn2->local_msgqueue_base);
  1384. ch_sn2->local_msgqueue = NULL;
  1385. kfree(ch_sn2->notify_queue);
  1386. ch_sn2->notify_queue = NULL;
  1387. }
  1388. }
  1389. return ret;
  1390. }
  1391. /*
  1392. * Free up message queues and other stuff that were allocated for the specified
  1393. * channel.
  1394. */
  1395. static void
  1396. xpc_teardown_msg_structures_sn2(struct xpc_channel *ch)
  1397. {
  1398. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1399. DBUG_ON(!spin_is_locked(&ch->lock));
  1400. ch_sn2->remote_msgqueue_pa = 0;
  1401. ch_sn2->local_GP->get = 0;
  1402. ch_sn2->local_GP->put = 0;
  1403. ch_sn2->remote_GP.get = 0;
  1404. ch_sn2->remote_GP.put = 0;
  1405. ch_sn2->w_local_GP.get = 0;
  1406. ch_sn2->w_local_GP.put = 0;
  1407. ch_sn2->w_remote_GP.get = 0;
  1408. ch_sn2->w_remote_GP.put = 0;
  1409. ch_sn2->next_msg_to_pull = 0;
  1410. if (ch->flags & XPC_C_SETUP) {
  1411. dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
  1412. ch->flags, ch->partid, ch->number);
  1413. kfree(ch_sn2->local_msgqueue_base);
  1414. ch_sn2->local_msgqueue = NULL;
  1415. kfree(ch_sn2->remote_msgqueue_base);
  1416. ch_sn2->remote_msgqueue = NULL;
  1417. kfree(ch_sn2->notify_queue);
  1418. ch_sn2->notify_queue = NULL;
  1419. }
  1420. }
  1421. /*
  1422. * Notify those who wanted to be notified upon delivery of their message.
  1423. */
  1424. static void
  1425. xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
  1426. {
  1427. struct xpc_notify_sn2 *notify;
  1428. u8 notify_type;
  1429. s64 get = ch->sn.sn2.w_remote_GP.get - 1;
  1430. while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
  1431. notify = &ch->sn.sn2.notify_queue[get % ch->local_nentries];
  1432. /*
  1433. * See if the notify entry indicates it was associated with
  1434. * a message who's sender wants to be notified. It is possible
  1435. * that it is, but someone else is doing or has done the
  1436. * notification.
  1437. */
  1438. notify_type = notify->type;
  1439. if (notify_type == 0 ||
  1440. cmpxchg(&notify->type, notify_type, 0) != notify_type) {
  1441. continue;
  1442. }
  1443. DBUG_ON(notify_type != XPC_N_CALL);
  1444. atomic_dec(&ch->n_to_notify);
  1445. if (notify->func != NULL) {
  1446. dev_dbg(xpc_chan, "notify->func() called, notify=0x%p "
  1447. "msg_number=%lld partid=%d channel=%d\n",