PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/misc/sgi-xp/xpc_partition.c

http://github.com/torvalds/linux
C | 534 lines | 338 code | 93 blank | 103 comment | 86 complexity | bc02f4b2cb7f6f0df22b4281259a8daf MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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 = NULL;
  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. if (len > buf_len) {
  80. kfree(buf_base);
  81. buf_len = L1_CACHE_ALIGN(len);
  82. buf = xpc_kmalloc_cacheline_aligned(buf_len, GFP_KERNEL,
  83. &buf_base);
  84. if (buf_base == NULL) {
  85. dev_err(xpc_part, "unable to kmalloc "
  86. "len=0x%016lx\n", buf_len);
  87. ret = xpNoMemory;
  88. break;
  89. }
  90. }
  91. ret = xp_remote_memcpy(xp_pa(buf), rp_pa, len);
  92. if (ret != xpSuccess) {
  93. dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
  94. break;
  95. }
  96. }
  97. kfree(buf_base);
  98. if (ret != xpSuccess)
  99. rp_pa = 0;
  100. dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
  101. return rp_pa;
  102. }
  103. /*
  104. * Fill the partition reserved page with the information needed by
  105. * other partitions to discover we are alive and establish initial
  106. * communications.
  107. */
  108. int
  109. xpc_setup_rsvd_page(void)
  110. {
  111. int ret;
  112. struct xpc_rsvd_page *rp;
  113. unsigned long rp_pa;
  114. unsigned long new_ts_jiffies;
  115. /* get the local reserved page's address */
  116. preempt_disable();
  117. rp_pa = xpc_get_rsvd_page_pa(xp_cpu_to_nasid(smp_processor_id()));
  118. preempt_enable();
  119. if (rp_pa == 0) {
  120. dev_err(xpc_part, "SAL failed to locate the reserved page\n");
  121. return -ESRCH;
  122. }
  123. rp = (struct xpc_rsvd_page *)__va(xp_socket_pa(rp_pa));
  124. if (rp->SAL_version < 3) {
  125. /* SAL_versions < 3 had a SAL_partid defined as a u8 */
  126. rp->SAL_partid &= 0xff;
  127. }
  128. BUG_ON(rp->SAL_partid != xp_partition_id);
  129. if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
  130. dev_err(xpc_part, "the reserved page's partid of %d is outside "
  131. "supported range (< 0 || >= %d)\n", rp->SAL_partid,
  132. xp_max_npartitions);
  133. return -EINVAL;
  134. }
  135. rp->version = XPC_RP_VERSION;
  136. rp->max_npartitions = xp_max_npartitions;
  137. /* establish the actual sizes of the nasid masks */
  138. if (rp->SAL_version == 1) {
  139. /* SAL_version 1 didn't set the nasids_size field */
  140. rp->SAL_nasids_size = 128;
  141. }
  142. xpc_nasid_mask_nbytes = rp->SAL_nasids_size;
  143. xpc_nasid_mask_nlongs = BITS_TO_LONGS(rp->SAL_nasids_size *
  144. BITS_PER_BYTE);
  145. /* setup the pointers to the various items in the reserved page */
  146. xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
  147. xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
  148. ret = xpc_arch_ops.setup_rsvd_page(rp);
  149. if (ret != 0)
  150. return ret;
  151. /*
  152. * Set timestamp of when reserved page was setup by XPC.
  153. * This signifies to the remote partition that our reserved
  154. * page is initialized.
  155. */
  156. new_ts_jiffies = jiffies;
  157. if (new_ts_jiffies == 0 || new_ts_jiffies == rp->ts_jiffies)
  158. new_ts_jiffies++;
  159. rp->ts_jiffies = new_ts_jiffies;
  160. xpc_rsvd_page = rp;
  161. return 0;
  162. }
  163. void
  164. xpc_teardown_rsvd_page(void)
  165. {
  166. /* a zero timestamp indicates our rsvd page is not initialized */
  167. xpc_rsvd_page->ts_jiffies = 0;
  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, unsigned long *discovered_nasids,
  178. struct xpc_rsvd_page *remote_rp, unsigned long *remote_rp_pa)
  179. {
  180. int l;
  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(xp_pa(remote_rp), *remote_rp_pa,
  188. XPC_RP_HEADER_SIZE + xpc_nasid_mask_nbytes);
  189. if (ret != xpSuccess)
  190. return ret;
  191. if (discovered_nasids != NULL) {
  192. unsigned long *remote_part_nasids =
  193. XPC_RP_PART_NASIDS(remote_rp);
  194. for (l = 0; l < xpc_nasid_mask_nlongs; l++)
  195. discovered_nasids[l] |= remote_part_nasids[l];
  196. }
  197. /* zero timestamp indicates the reserved page has not been setup */
  198. if (remote_rp->ts_jiffies == 0)
  199. return xpRsvdPageNotSet;
  200. if (XPC_VERSION_MAJOR(remote_rp->version) !=
  201. XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
  202. return xpBadVersion;
  203. }
  204. /* check that both remote and local partids are valid for each side */
  205. if (remote_rp->SAL_partid < 0 ||
  206. remote_rp->SAL_partid >= xp_max_npartitions ||
  207. remote_rp->max_npartitions <= xp_partition_id) {
  208. return xpInvalidPartid;
  209. }
  210. if (remote_rp->SAL_partid == xp_partition_id)
  211. return xpLocalPartid;
  212. return xpSuccess;
  213. }
  214. /*
  215. * See if the other side has responded to a partition deactivate request
  216. * from us. Though we requested the remote partition to deactivate with regard
  217. * to us, we really only need to wait for the other side to disengage from us.
  218. */
  219. int
  220. xpc_partition_disengaged(struct xpc_partition *part)
  221. {
  222. short partid = XPC_PARTID(part);
  223. int disengaged;
  224. disengaged = !xpc_arch_ops.partition_engaged(partid);
  225. if (part->disengage_timeout) {
  226. if (!disengaged) {
  227. if (time_is_after_jiffies(part->disengage_timeout)) {
  228. /* timelimit hasn't been reached yet */
  229. return 0;
  230. }
  231. /*
  232. * Other side hasn't responded to our deactivate
  233. * request in a timely fashion, so assume it's dead.
  234. */
  235. dev_info(xpc_part, "deactivate request to remote "
  236. "partition %d timed out\n", partid);
  237. xpc_disengage_timedout = 1;
  238. xpc_arch_ops.assume_partition_disengaged(partid);
  239. disengaged = 1;
  240. }
  241. part->disengage_timeout = 0;
  242. /* cancel the timer function, provided it's not us */
  243. if (!in_interrupt())
  244. del_singleshot_timer_sync(&part->disengage_timer);
  245. DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING &&
  246. part->act_state != XPC_P_AS_INACTIVE);
  247. if (part->act_state != XPC_P_AS_INACTIVE)
  248. xpc_wakeup_channel_mgr(part);
  249. xpc_arch_ops.cancel_partition_deactivation_request(part);
  250. }
  251. return disengaged;
  252. }
  253. /*
  254. * Mark specified partition as active.
  255. */
  256. enum xp_retval
  257. xpc_mark_partition_active(struct xpc_partition *part)
  258. {
  259. unsigned long irq_flags;
  260. enum xp_retval ret;
  261. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  262. spin_lock_irqsave(&part->act_lock, irq_flags);
  263. if (part->act_state == XPC_P_AS_ACTIVATING) {
  264. part->act_state = XPC_P_AS_ACTIVE;
  265. ret = xpSuccess;
  266. } else {
  267. DBUG_ON(part->reason == xpSuccess);
  268. ret = part->reason;
  269. }
  270. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  271. return ret;
  272. }
  273. /*
  274. * Start the process of deactivating the specified partition.
  275. */
  276. void
  277. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  278. enum xp_retval reason)
  279. {
  280. unsigned long irq_flags;
  281. spin_lock_irqsave(&part->act_lock, irq_flags);
  282. if (part->act_state == XPC_P_AS_INACTIVE) {
  283. XPC_SET_REASON(part, reason, line);
  284. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  285. if (reason == xpReactivating) {
  286. /* we interrupt ourselves to reactivate partition */
  287. xpc_arch_ops.request_partition_reactivation(part);
  288. }
  289. return;
  290. }
  291. if (part->act_state == XPC_P_AS_DEACTIVATING) {
  292. if ((part->reason == xpUnloading && reason != xpUnloading) ||
  293. reason == xpReactivating) {
  294. XPC_SET_REASON(part, reason, line);
  295. }
  296. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  297. return;
  298. }
  299. part->act_state = XPC_P_AS_DEACTIVATING;
  300. XPC_SET_REASON(part, reason, line);
  301. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  302. /* ask remote partition to deactivate with regard to us */
  303. xpc_arch_ops.request_partition_deactivation(part);
  304. /* set a timelimit on the disengage phase of the deactivation request */
  305. part->disengage_timeout = jiffies + (xpc_disengage_timelimit * HZ);
  306. part->disengage_timer.expires = part->disengage_timeout;
  307. add_timer(&part->disengage_timer);
  308. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  309. XPC_PARTID(part), reason);
  310. xpc_partition_going_down(part, reason);
  311. }
  312. /*
  313. * Mark specified partition as inactive.
  314. */
  315. void
  316. xpc_mark_partition_inactive(struct xpc_partition *part)
  317. {
  318. unsigned long irq_flags;
  319. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  320. XPC_PARTID(part));
  321. spin_lock_irqsave(&part->act_lock, irq_flags);
  322. part->act_state = XPC_P_AS_INACTIVE;
  323. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  324. part->remote_rp_pa = 0;
  325. }
  326. /*
  327. * SAL has provided a partition and machine mask. The partition mask
  328. * contains a bit for each even nasid in our partition. The machine
  329. * mask contains a bit for each even nasid in the entire machine.
  330. *
  331. * Using those two bit arrays, we can determine which nasids are
  332. * known in the machine. Each should also have a reserved page
  333. * initialized if they are available for partitioning.
  334. */
  335. void
  336. xpc_discovery(void)
  337. {
  338. void *remote_rp_base;
  339. struct xpc_rsvd_page *remote_rp;
  340. unsigned long remote_rp_pa;
  341. int region;
  342. int region_size;
  343. int max_regions;
  344. int nasid;
  345. unsigned long *discovered_nasids;
  346. enum xp_retval ret;
  347. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  348. xpc_nasid_mask_nbytes,
  349. GFP_KERNEL, &remote_rp_base);
  350. if (remote_rp == NULL)
  351. return;
  352. discovered_nasids = kcalloc(xpc_nasid_mask_nlongs, sizeof(long),
  353. GFP_KERNEL);
  354. if (discovered_nasids == NULL) {
  355. kfree(remote_rp_base);
  356. return;
  357. }
  358. /*
  359. * The term 'region' in this context refers to the minimum number of
  360. * nodes that can comprise an access protection grouping. The access
  361. * protection is in regards to memory, IOI and IPI.
  362. */
  363. region_size = xp_region_size;
  364. if (is_uv())
  365. max_regions = 256;
  366. else {
  367. max_regions = 64;
  368. switch (region_size) {
  369. case 128:
  370. max_regions *= 2;
  371. /* fall through */
  372. case 64:
  373. max_regions *= 2;
  374. /* fall through */
  375. case 32:
  376. max_regions *= 2;
  377. region_size = 16;
  378. }
  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. }