xpc_partition.c 13 KB

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