xpc_partition.c 13 KB

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