xpc_partition.c 14 KB

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