xpc_partition.c 14 KB

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