xpc_partition.c 14 KB

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