xpc_partition.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 u64 *xpc_part_nasids;
  31. u64 *xpc_mach_nasids;
  32. static int xpc_sizeof_nasid_mask; /* actual size in bytes of nasid mask */
  33. int xpc_nasid_mask_words; /* actual size in words of 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_sizeof_nasid_mask = rp->SAL_nasids_size;
  143. xpc_nasid_mask_words = DIV_ROUND_UP(xpc_sizeof_nasid_mask,
  144. BYTES_PER_WORD);
  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, u64 *discovered_nasids,
  170. struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
  171. {
  172. int i;
  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_sizeof_nasid_mask);
  181. if (ret != xpSuccess)
  182. return ret;
  183. if (discovered_nasids != NULL) {
  184. u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
  185. for (i = 0; i < xpc_nasid_mask_words; i++)
  186. discovered_nasids[i] |= remote_part_nasids[i];
  187. }
  188. /* see if the reserved page has been set up by XPC */
  189. if (remote_rp->stamp == 0)
  190. return xpRsvdPageNotSet;
  191. if (XPC_VERSION_MAJOR(remote_rp->version) !=
  192. XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
  193. return xpBadVersion;
  194. }
  195. /* check that both remote and local partids are valid for each side */
  196. if (remote_rp->SAL_partid < 0 ||
  197. remote_rp->SAL_partid >= xp_max_npartitions ||
  198. remote_rp->max_npartitions <= sn_partition_id) {
  199. return xpInvalidPartid;
  200. }
  201. if (remote_rp->SAL_partid == sn_partition_id)
  202. return xpLocalPartid;
  203. return xpSuccess;
  204. }
  205. /*
  206. * See if the other side has responded to a partition deactivate request
  207. * from us. Though we requested the remote partition to deactivate with regard
  208. * to us, we really only need to wait for the other side to disengage from us.
  209. */
  210. int
  211. xpc_partition_disengaged(struct xpc_partition *part)
  212. {
  213. short partid = XPC_PARTID(part);
  214. int disengaged;
  215. disengaged = !xpc_partition_engaged(partid);
  216. if (part->disengage_timeout) {
  217. if (!disengaged) {
  218. if (time_is_after_jiffies(part->disengage_timeout)) {
  219. /* timelimit hasn't been reached yet */
  220. return 0;
  221. }
  222. /*
  223. * Other side hasn't responded to our deactivate
  224. * request in a timely fashion, so assume it's dead.
  225. */
  226. dev_info(xpc_part, "deactivate request to remote "
  227. "partition %d timed out\n", partid);
  228. xpc_disengage_timedout = 1;
  229. xpc_assume_partition_disengaged(partid);
  230. disengaged = 1;
  231. }
  232. part->disengage_timeout = 0;
  233. /* cancel the timer function, provided it's not us */
  234. if (!in_interrupt())
  235. del_singleshot_timer_sync(&part->disengage_timer);
  236. DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
  237. part->act_state != XPC_P_INACTIVE);
  238. if (part->act_state != XPC_P_INACTIVE)
  239. xpc_wakeup_channel_mgr(part);
  240. xpc_cancel_partition_deactivation_request(part);
  241. }
  242. return disengaged;
  243. }
  244. /*
  245. * Mark specified partition as active.
  246. */
  247. enum xp_retval
  248. xpc_mark_partition_active(struct xpc_partition *part)
  249. {
  250. unsigned long irq_flags;
  251. enum xp_retval ret;
  252. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  253. spin_lock_irqsave(&part->act_lock, irq_flags);
  254. if (part->act_state == XPC_P_ACTIVATING) {
  255. part->act_state = XPC_P_ACTIVE;
  256. ret = xpSuccess;
  257. } else {
  258. DBUG_ON(part->reason == xpSuccess);
  259. ret = part->reason;
  260. }
  261. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  262. return ret;
  263. }
  264. /*
  265. * Start the process of deactivating the specified partition.
  266. */
  267. void
  268. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  269. enum xp_retval reason)
  270. {
  271. unsigned long irq_flags;
  272. spin_lock_irqsave(&part->act_lock, irq_flags);
  273. if (part->act_state == XPC_P_INACTIVE) {
  274. XPC_SET_REASON(part, reason, line);
  275. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  276. if (reason == xpReactivating) {
  277. /* we interrupt ourselves to reactivate partition */
  278. xpc_request_partition_reactivation(part);
  279. }
  280. return;
  281. }
  282. if (part->act_state == XPC_P_DEACTIVATING) {
  283. if ((part->reason == xpUnloading && reason != xpUnloading) ||
  284. reason == xpReactivating) {
  285. XPC_SET_REASON(part, reason, line);
  286. }
  287. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  288. return;
  289. }
  290. part->act_state = XPC_P_DEACTIVATING;
  291. XPC_SET_REASON(part, reason, line);
  292. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  293. /* ask remote partition to deactivate with regard to us */
  294. xpc_request_partition_deactivation(part);
  295. /* set a timelimit on the disengage phase of the deactivation request */
  296. part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
  297. part->disengage_timer.expires = part->disengage_timeout;
  298. add_timer(&part->disengage_timer);
  299. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  300. XPC_PARTID(part), reason);
  301. xpc_partition_going_down(part, reason);
  302. }
  303. /*
  304. * Mark specified partition as inactive.
  305. */
  306. void
  307. xpc_mark_partition_inactive(struct xpc_partition *part)
  308. {
  309. unsigned long irq_flags;
  310. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  311. XPC_PARTID(part));
  312. spin_lock_irqsave(&part->act_lock, irq_flags);
  313. part->act_state = XPC_P_INACTIVE;
  314. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  315. part->remote_rp_pa = 0;
  316. }
  317. /*
  318. * SAL has provided a partition and machine mask. The partition mask
  319. * contains a bit for each even nasid in our partition. The machine
  320. * mask contains a bit for each even nasid in the entire machine.
  321. *
  322. * Using those two bit arrays, we can determine which nasids are
  323. * known in the machine. Each should also have a reserved page
  324. * initialized if they are available for partitioning.
  325. */
  326. void
  327. xpc_discovery(void)
  328. {
  329. void *remote_rp_base;
  330. struct xpc_rsvd_page *remote_rp;
  331. u64 remote_rp_pa;
  332. int region;
  333. int region_size;
  334. int max_regions;
  335. int nasid;
  336. struct xpc_rsvd_page *rp;
  337. u64 *discovered_nasids;
  338. enum xp_retval ret;
  339. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  340. xpc_sizeof_nasid_mask,
  341. GFP_KERNEL, &remote_rp_base);
  342. if (remote_rp == NULL)
  343. return;
  344. discovered_nasids = kzalloc(sizeof(u64) * xpc_nasid_mask_words,
  345. GFP_KERNEL);
  346. if (discovered_nasids == NULL) {
  347. kfree(remote_rp_base);
  348. return;
  349. }
  350. rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
  351. /*
  352. * The term 'region' in this context refers to the minimum number of
  353. * nodes that can comprise an access protection grouping. The access
  354. * protection is in regards to memory, IOI and IPI.
  355. */
  356. max_regions = 64;
  357. region_size = sn_region_size;
  358. switch (region_size) {
  359. case 128:
  360. max_regions *= 2;
  361. case 64:
  362. max_regions *= 2;
  363. case 32:
  364. max_regions *= 2;
  365. region_size = 16;
  366. DBUG_ON(!is_shub2());
  367. }
  368. for (region = 0; region < max_regions; region++) {
  369. if (xpc_exiting)
  370. break;
  371. dev_dbg(xpc_part, "searching region %d\n", region);
  372. for (nasid = (region * region_size * 2);
  373. nasid < ((region + 1) * region_size * 2); nasid += 2) {
  374. if (xpc_exiting)
  375. break;
  376. dev_dbg(xpc_part, "checking nasid %d\n", nasid);
  377. if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
  378. dev_dbg(xpc_part, "PROM indicates Nasid %d is "
  379. "part of the local partition; skipping "
  380. "region\n", nasid);
  381. break;
  382. }
  383. if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
  384. dev_dbg(xpc_part, "PROM indicates Nasid %d was "
  385. "not on Numa-Link network at reset\n",
  386. nasid);
  387. continue;
  388. }
  389. if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
  390. dev_dbg(xpc_part, "Nasid %d is part of a "
  391. "partition which was previously "
  392. "discovered\n", nasid);
  393. continue;
  394. }
  395. /* pull over the rsvd page header & part_nasids mask */
  396. ret = xpc_get_remote_rp(nasid, discovered_nasids,
  397. remote_rp, &remote_rp_pa);
  398. if (ret != xpSuccess) {
  399. dev_dbg(xpc_part, "unable to get reserved page "
  400. "from nasid %d, reason=%d\n", nasid,
  401. ret);
  402. if (ret == xpLocalPartid)
  403. break;
  404. continue;
  405. }
  406. xpc_request_partition_activation(remote_rp,
  407. remote_rp_pa, nasid);
  408. }
  409. }
  410. kfree(discovered_nasids);
  411. kfree(remote_rp_base);
  412. }
  413. /*
  414. * Given a partid, get the nasids owned by that partition from the
  415. * remote partition's reserved page.
  416. */
  417. enum xp_retval
  418. xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
  419. {
  420. struct xpc_partition *part;
  421. u64 part_nasid_pa;
  422. part = &xpc_partitions[partid];
  423. if (part->remote_rp_pa == 0)
  424. return xpPartitionDown;
  425. memset(nasid_mask, 0, xpc_sizeof_nasid_mask);
  426. part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
  427. return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa,
  428. xpc_sizeof_nasid_mask);
  429. }