xpc_partition.c 13 KB

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