xpc_partition.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) partition support.
  10. *
  11. * This is the part of XPC that detects the presence/absence of
  12. * other partitions. It provides a heartbeat and monitors the
  13. * heartbeats of other partitions.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/cache.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/nodemask.h>
  21. #include <asm/sn/intr.h>
  22. #include <asm/sn/sn_sal.h>
  23. #include <asm/sn/nodepda.h>
  24. #include <asm/sn/addrs.h>
  25. #include "xpc.h"
  26. /* XPC is exiting flag */
  27. int xpc_exiting;
  28. /* this partition's reserved page pointers */
  29. struct xpc_rsvd_page *xpc_rsvd_page;
  30. static unsigned long *xpc_part_nasids;
  31. unsigned long *xpc_mach_nasids;
  32. static int xpc_nasid_mask_nbytes; /* #of bytes in nasid mask */
  33. int xpc_nasid_mask_nlongs; /* #of longs in nasid mask */
  34. struct xpc_partition *xpc_partitions;
  35. /*
  36. * Guarantee that the kmalloc'd memory is cacheline aligned.
  37. */
  38. void *
  39. xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
  40. {
  41. /* see if kmalloc will give us cachline aligned memory by default */
  42. *base = kmalloc(size, flags);
  43. if (*base == NULL)
  44. return NULL;
  45. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  46. return *base;
  47. kfree(*base);
  48. /* nope, we'll have to do it ourselves */
  49. *base = kmalloc(size + L1_CACHE_BYTES, flags);
  50. if (*base == NULL)
  51. return NULL;
  52. return (void *)L1_CACHE_ALIGN((u64)*base);
  53. }
  54. /*
  55. * Given a nasid, get the physical address of the partition's reserved page
  56. * for that nasid. This function returns 0 on any error.
  57. */
  58. static u64
  59. xpc_get_rsvd_page_pa(int nasid)
  60. {
  61. enum xp_retval ret;
  62. s64 status;
  63. u64 cookie = 0;
  64. u64 rp_pa = nasid; /* seed with nasid */
  65. u64 len = 0;
  66. u64 buf = buf;
  67. u64 buf_len = 0;
  68. void *buf_base = NULL;
  69. while (1) {
  70. status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
  71. &len);
  72. dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
  73. "0x%016lx, address=0x%016lx, len=0x%016lx\n",
  74. status, cookie, rp_pa, len);
  75. if (status != SALRET_MORE_PASSES)
  76. break;
  77. /* !!! L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
  78. if (L1_CACHE_ALIGN(len) > buf_len) {
  79. kfree(buf_base);
  80. buf_len = L1_CACHE_ALIGN(len);
  81. buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
  82. GFP_KERNEL,
  83. &buf_base);
  84. if (buf_base == NULL) {
  85. dev_err(xpc_part, "unable to kmalloc "
  86. "len=0x%016lx\n", buf_len);
  87. status = SALRET_ERROR;
  88. break;
  89. }
  90. }
  91. ret = xp_remote_memcpy((void *)buf, (void *)rp_pa, buf_len);
  92. if (ret != xpSuccess) {
  93. dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
  94. status = SALRET_ERROR;
  95. break;
  96. }
  97. }
  98. kfree(buf_base);
  99. if (status != SALRET_OK)
  100. rp_pa = 0;
  101. dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
  102. return rp_pa;
  103. }
  104. /*
  105. * Fill the partition reserved page with the information needed by
  106. * other partitions to discover we are alive and establish initial
  107. * communications.
  108. */
  109. struct xpc_rsvd_page *
  110. xpc_setup_rsvd_page(void)
  111. {
  112. struct xpc_rsvd_page *rp;
  113. u64 rp_pa;
  114. unsigned long new_stamp;
  115. /* get the local reserved page's address */
  116. preempt_disable();
  117. rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
  118. preempt_enable();
  119. if (rp_pa == 0) {
  120. dev_err(xpc_part, "SAL failed to locate the reserved page\n");
  121. return NULL;
  122. }
  123. rp = (struct xpc_rsvd_page *)__va(rp_pa);
  124. if (rp->SAL_version < 3) {
  125. /* SAL_versions < 3 had a SAL_partid defined as a u8 */
  126. rp->SAL_partid &= 0xff;
  127. }
  128. BUG_ON(rp->SAL_partid != sn_partition_id);
  129. if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
  130. dev_err(xpc_part, "the reserved page's partid of %d is outside "
  131. "supported range (< 0 || >= %d)\n", rp->SAL_partid,
  132. xp_max_npartitions);
  133. return NULL;
  134. }
  135. rp->version = XPC_RP_VERSION;
  136. rp->max_npartitions = xp_max_npartitions;
  137. /* establish the actual sizes of the nasid masks */
  138. if (rp->SAL_version == 1) {
  139. /* SAL_version 1 didn't set the nasids_size field */
  140. rp->SAL_nasids_size = 128;
  141. }
  142. xpc_nasid_mask_nbytes = rp->SAL_nasids_size;
  143. xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *
  144. BITS_PER_BYTE);
  145. /* setup the pointers to the various items in the reserved page */
  146. xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
  147. xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
  148. if (xpc_rsvd_page_init(rp) != xpSuccess)
  149. return NULL;
  150. /*
  151. * Set timestamp of when reserved page was setup by XPC.
  152. * This signifies to the remote partition that our reserved
  153. * page is initialized.
  154. */
  155. new_stamp = jiffies;
  156. if (new_stamp == 0 || new_stamp == rp->stamp)
  157. new_stamp++;
  158. rp->stamp = new_stamp;
  159. return rp;
  160. }
  161. /*
  162. * Get a copy of a portion of the remote partition's rsvd page.
  163. *
  164. * remote_rp points to a buffer that is cacheline aligned for BTE copies and
  165. * is large enough to contain a copy of their reserved page header and
  166. * part_nasids mask.
  167. */
  168. enum xp_retval
  169. xpc_get_remote_rp(int nasid, unsigned long *discovered_nasids,
  170. struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
  171. {
  172. int l;
  173. enum xp_retval ret;
  174. /* get the reserved page's physical address */
  175. *remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
  176. if (*remote_rp_pa == 0)
  177. return xpNoRsvdPageAddr;
  178. /* pull over the reserved page header and part_nasids mask */
  179. ret = xp_remote_memcpy(remote_rp, (void *)*remote_rp_pa,
  180. XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);
  181. if (ret != xpSuccess)
  182. return ret;
  183. if (discovered_nasids != NULL) {
  184. unsigned long *remote_part_nasids =
  185. XPC_RP_PART_NASIDS(remote_rp);
  186. for (l = 0; l < xpc_nasid_mask_nlongs; l++)
  187. discovered_nasids[l] |= remote_part_nasids[l];
  188. }
  189. /* see if the reserved page has been set up by XPC */
  190. if (remote_rp->stamp == 0)
  191. return xpRsvdPageNotSet;
  192. if (XPC_VERSION_MAJOR(remote_rp->version) !=
  193. XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
  194. return xpBadVersion;
  195. }
  196. /* check that both remote and local partids are valid for each side */
  197. if (remote_rp->SAL_partid < 0 ||
  198. remote_rp->SAL_partid >= xp_max_npartitions ||
  199. remote_rp->max_npartitions <= sn_partition_id) {
  200. return xpInvalidPartid;
  201. }
  202. if (remote_rp->SAL_partid == sn_partition_id)
  203. return xpLocalPartid;
  204. return xpSuccess;
  205. }
  206. /*
  207. * See if the other side has responded to a partition deactivate request
  208. * from us. Though we requested the remote partition to deactivate with regard
  209. * to us, we really only need to wait for the other side to disengage from us.
  210. */
  211. int
  212. xpc_partition_disengaged(struct xpc_partition *part)
  213. {
  214. short partid = XPC_PARTID(part);
  215. int disengaged;
  216. disengaged = !xpc_partition_engaged(partid);
  217. if (part->disengage_timeout) {
  218. if (!disengaged) {
  219. if (time_is_after_jiffies(part->disengage_timeout)) {
  220. /* timelimit hasn't been reached yet */
  221. return 0;
  222. }
  223. /*
  224. * Other side hasn't responded to our deactivate
  225. * request in a timely fashion, so assume it's dead.
  226. */
  227. dev_info(xpc_part, "deactivate request to remote "
  228. "partition %d timed out\n", partid);
  229. xpc_disengage_timedout = 1;
  230. xpc_assume_partition_disengaged(partid);
  231. disengaged = 1;
  232. }
  233. part->disengage_timeout = 0;
  234. /* cancel the timer function, provided it's not us */
  235. if (!in_interrupt())
  236. del_singleshot_timer_sync(&part->disengage_timer);
  237. DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
  238. part->act_state != XPC_P_INACTIVE);
  239. if (part->act_state != XPC_P_INACTIVE)
  240. xpc_wakeup_channel_mgr(part);
  241. xpc_cancel_partition_deactivation_request(part);
  242. }
  243. return disengaged;
  244. }
  245. /*
  246. * Mark specified partition as active.
  247. */
  248. enum xp_retval
  249. xpc_mark_partition_active(struct xpc_partition *part)
  250. {
  251. unsigned long irq_flags;
  252. enum xp_retval ret;
  253. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  254. spin_lock_irqsave(&part->act_lock, irq_flags);
  255. if (part->act_state == XPC_P_ACTIVATING) {
  256. part->act_state = XPC_P_ACTIVE;
  257. ret = xpSuccess;
  258. } else {
  259. DBUG_ON(part->reason == xpSuccess);
  260. ret = part->reason;
  261. }
  262. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  263. return ret;
  264. }
  265. /*
  266. * Start the process of deactivating the specified partition.
  267. */
  268. void
  269. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  270. enum xp_retval reason)
  271. {
  272. unsigned long irq_flags;
  273. spin_lock_irqsave(&part->act_lock, irq_flags);
  274. if (part->act_state == XPC_P_INACTIVE) {
  275. XPC_SET_REASON(part, reason, line);
  276. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  277. if (reason == xpReactivating) {
  278. /* we interrupt ourselves to reactivate partition */
  279. xpc_request_partition_reactivation(part);
  280. }
  281. return;
  282. }
  283. if (part->act_state == XPC_P_DEACTIVATING) {
  284. if ((part->reason == xpUnloading && reason != xpUnloading) ||
  285. reason == xpReactivating) {
  286. XPC_SET_REASON(part, reason, line);
  287. }
  288. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  289. return;
  290. }
  291. part->act_state = XPC_P_DEACTIVATING;
  292. XPC_SET_REASON(part, reason, line);
  293. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  294. /* ask remote partition to deactivate with regard to us */
  295. xpc_request_partition_deactivation(part);
  296. /* set a timelimit on the disengage phase of the deactivation request */
  297. part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
  298. part->disengage_timer.expires = part->disengage_timeout;
  299. add_timer(&part->disengage_timer);
  300. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  301. XPC_PARTID(part), reason);
  302. xpc_partition_going_down(part, reason);
  303. }
  304. /*
  305. * Mark specified partition as inactive.
  306. */
  307. void
  308. xpc_mark_partition_inactive(struct xpc_partition *part)
  309. {
  310. unsigned long irq_flags;
  311. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  312. XPC_PARTID(part));
  313. spin_lock_irqsave(&part->act_lock, irq_flags);
  314. part->act_state = XPC_P_INACTIVE;
  315. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  316. part->remote_rp_pa = 0;
  317. }
  318. /*
  319. * SAL has provided a partition and machine mask. The partition mask
  320. * contains a bit for each even nasid in our partition. The machine
  321. * mask contains a bit for each even nasid in the entire machine.
  322. *
  323. * Using those two bit arrays, we can determine which nasids are
  324. * known in the machine. Each should also have a reserved page
  325. * initialized if they are available for partitioning.
  326. */
  327. void
  328. xpc_discovery(void)
  329. {
  330. void *remote_rp_base;
  331. struct xpc_rsvd_page *remote_rp;
  332. u64 remote_rp_pa;
  333. int region;
  334. int region_size;
  335. int max_regions;
  336. int nasid;
  337. struct xpc_rsvd_page *rp;
  338. unsigned long *discovered_nasids;
  339. enum xp_retval ret;
  340. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  341. xpc_nasid_mask_nbytes,
  342. GFP_KERNEL, &remote_rp_base);
  343. if (remote_rp == NULL)
  344. return;
  345. discovered_nasids = kzalloc(sizeof(long) * xpc_nasid_mask_nlongs,
  346. GFP_KERNEL);
  347. if (discovered_nasids == NULL) {
  348. kfree(remote_rp_base);
  349. return;
  350. }
  351. rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
  352. /*
  353. * The term 'region' in this context refers to the minimum number of
  354. * nodes that can comprise an access protection grouping. The access
  355. * protection is in regards to memory, IOI and IPI.
  356. */
  357. max_regions = 64;
  358. region_size = sn_region_size;
  359. switch (region_size) {
  360. case 128:
  361. max_regions *= 2;
  362. case 64:
  363. max_regions *= 2;
  364. case 32:
  365. max_regions *= 2;
  366. region_size = 16;
  367. DBUG_ON(!is_shub2());
  368. }
  369. for (region = 0; region < max_regions; region++) {
  370. if (xpc_exiting)
  371. break;
  372. dev_dbg(xpc_part, "searching region %d\n", region);
  373. for (nasid = (region * region_size * 2);
  374. nasid < ((region + 1) * region_size * 2); nasid += 2) {
  375. if (xpc_exiting)
  376. break;
  377. dev_dbg(xpc_part, "checking nasid %d\n", nasid);
  378. if (test_bit(nasid / 2, xpc_part_nasids)) {
  379. dev_dbg(xpc_part, "PROM indicates Nasid %d is "
  380. "part of the local partition; skipping "
  381. "region\n", nasid);
  382. break;
  383. }
  384. if (!(test_bit(nasid / 2, xpc_mach_nasids))) {
  385. dev_dbg(xpc_part, "PROM indicates Nasid %d was "
  386. "not on Numa-Link network at reset\n",
  387. nasid);
  388. continue;
  389. }
  390. if (test_bit(nasid / 2, discovered_nasids)) {
  391. dev_dbg(xpc_part, "Nasid %d is part of a "
  392. "partition which was previously "
  393. "discovered\n", nasid);
  394. continue;
  395. }
  396. /* pull over the rsvd page header & part_nasids mask */
  397. ret = xpc_get_remote_rp(nasid, discovered_nasids,
  398. remote_rp, &remote_rp_pa);
  399. if (ret != xpSuccess) {
  400. dev_dbg(xpc_part, "unable to get reserved page "
  401. "from nasid %d, reason=%d\n", nasid,
  402. ret);
  403. if (ret == xpLocalPartid)
  404. break;
  405. continue;
  406. }
  407. xpc_request_partition_activation(remote_rp,
  408. remote_rp_pa, nasid);
  409. }
  410. }
  411. kfree(discovered_nasids);
  412. kfree(remote_rp_base);
  413. }
  414. /*
  415. * Given a partid, get the nasids owned by that partition from the
  416. * remote partition's reserved page.
  417. */
  418. enum xp_retval
  419. xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
  420. {
  421. struct xpc_partition *part;
  422. u64 part_nasid_pa;
  423. part = &xpc_partitions[partid];
  424. if (part->remote_rp_pa == 0)
  425. return xpPartitionDown;
  426. memset(nasid_mask, 0, xpc_nasid_mask_nbytes);
  427. part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
  428. return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa,
  429. xpc_nasid_mask_nbytes);
  430. }