xpc_partition.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. /* SH_IPI_ACCESS shub register value on startup */
  29. static u64 xpc_sh1_IPI_access;
  30. static u64 xpc_sh2_IPI_access0;
  31. static u64 xpc_sh2_IPI_access1;
  32. static u64 xpc_sh2_IPI_access2;
  33. static u64 xpc_sh2_IPI_access3;
  34. /* original protection values for each node */
  35. u64 xpc_prot_vec[MAX_NUMNODES];
  36. /* this partition's reserved page pointers */
  37. struct xpc_rsvd_page *xpc_rsvd_page;
  38. static u64 *xpc_part_nasids;
  39. static u64 *xpc_mach_nasids;
  40. /* >>> next two variables should be 'xpc_' if they remain here */
  41. static int xp_sizeof_nasid_mask; /* actual size in bytes of nasid mask */
  42. int xp_nasid_mask_words; /* actual size in words of nasid mask */
  43. struct xpc_partition *xpc_partitions;
  44. /*
  45. * Generic buffer used to store a local copy of portions of a remote
  46. * partition's reserved page (either its header and part_nasids mask,
  47. * or its vars).
  48. */
  49. char *xpc_remote_copy_buffer;
  50. void *xpc_remote_copy_buffer_base;
  51. /*
  52. * Guarantee that the kmalloc'd memory is cacheline aligned.
  53. */
  54. void *
  55. xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
  56. {
  57. /* see if kmalloc will give us cachline aligned memory by default */
  58. *base = kmalloc(size, flags);
  59. if (*base == NULL)
  60. return NULL;
  61. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  62. return *base;
  63. kfree(*base);
  64. /* nope, we'll have to do it ourselves */
  65. *base = kmalloc(size + L1_CACHE_BYTES, flags);
  66. if (*base == NULL)
  67. return NULL;
  68. return (void *)L1_CACHE_ALIGN((u64)*base);
  69. }
  70. /*
  71. * Given a nasid, get the physical address of the partition's reserved page
  72. * for that nasid. This function returns 0 on any error.
  73. */
  74. static u64
  75. xpc_get_rsvd_page_pa(int nasid)
  76. {
  77. enum xp_retval ret;
  78. s64 status;
  79. u64 cookie = 0;
  80. u64 rp_pa = nasid; /* seed with nasid */
  81. u64 len = 0;
  82. u64 buf = buf;
  83. u64 buf_len = 0;
  84. void *buf_base = NULL;
  85. while (1) {
  86. status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
  87. &len);
  88. dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
  89. "0x%016lx, address=0x%016lx, len=0x%016lx\n",
  90. status, cookie, rp_pa, len);
  91. if (status != SALRET_MORE_PASSES)
  92. break;
  93. /* >>> L1_CACHE_ALIGN() is only a sn2-bte_copy requirement */
  94. if (L1_CACHE_ALIGN(len) > buf_len) {
  95. kfree(buf_base);
  96. buf_len = L1_CACHE_ALIGN(len);
  97. buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
  98. GFP_KERNEL,
  99. &buf_base);
  100. if (buf_base == NULL) {
  101. dev_err(xpc_part, "unable to kmalloc "
  102. "len=0x%016lx\n", buf_len);
  103. status = SALRET_ERROR;
  104. break;
  105. }
  106. }
  107. ret = xp_remote_memcpy((void *)buf, (void *)rp_pa, buf_len);
  108. if (ret != xpSuccess) {
  109. dev_dbg(xpc_part, "xp_remote_memcpy failed %d\n", ret);
  110. status = SALRET_ERROR;
  111. break;
  112. }
  113. }
  114. kfree(buf_base);
  115. if (status != SALRET_OK)
  116. rp_pa = 0;
  117. dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
  118. return rp_pa;
  119. }
  120. /*
  121. * Fill the partition reserved page with the information needed by
  122. * other partitions to discover we are alive and establish initial
  123. * communications.
  124. */
  125. struct xpc_rsvd_page *
  126. xpc_setup_rsvd_page(void)
  127. {
  128. struct xpc_rsvd_page *rp;
  129. u64 rp_pa;
  130. /* get the local reserved page's address */
  131. preempt_disable();
  132. rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
  133. preempt_enable();
  134. if (rp_pa == 0) {
  135. dev_err(xpc_part, "SAL failed to locate the reserved page\n");
  136. return NULL;
  137. }
  138. rp = (struct xpc_rsvd_page *)__va(rp_pa);
  139. if (rp->SAL_version < 3) {
  140. /* SAL_versions < 3 had a SAL_partid defined as a u8 */
  141. rp->SAL_partid &= 0xff;
  142. }
  143. BUG_ON(rp->SAL_partid != sn_partition_id);
  144. if (rp->SAL_partid < 0 || rp->SAL_partid >= xp_max_npartitions) {
  145. dev_err(xpc_part, "the reserved page's partid of %d is outside "
  146. "supported range (< 0 || >= %d)\n", rp->SAL_partid,
  147. xp_max_npartitions);
  148. return NULL;
  149. }
  150. rp->version = XPC_RP_VERSION;
  151. rp->max_npartitions = xp_max_npartitions;
  152. /* establish the actual sizes of the nasid masks */
  153. if (rp->SAL_version == 1) {
  154. /* SAL_version 1 didn't set the nasids_size field */
  155. rp->SAL_nasids_size = 128;
  156. }
  157. xp_sizeof_nasid_mask = rp->SAL_nasids_size;
  158. xp_nasid_mask_words = DIV_ROUND_UP(xp_sizeof_nasid_mask,
  159. BYTES_PER_WORD);
  160. /* setup the pointers to the various items in the reserved page */
  161. xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
  162. xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
  163. if (xpc_rsvd_page_init(rp) != xpSuccess)
  164. return NULL;
  165. /*
  166. * Set timestamp of when reserved page was setup by XPC.
  167. * This signifies to the remote partition that our reserved
  168. * page is initialized.
  169. */
  170. rp->stamp = CURRENT_TIME;
  171. return rp;
  172. }
  173. /*
  174. * Change protections to allow IPI operations (and AMO operations on
  175. * Shub 1.1 systems).
  176. */
  177. void
  178. xpc_allow_IPI_ops(void)
  179. {
  180. int node;
  181. int nasid;
  182. /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
  183. if (is_shub2()) {
  184. xpc_sh2_IPI_access0 =
  185. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
  186. xpc_sh2_IPI_access1 =
  187. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
  188. xpc_sh2_IPI_access2 =
  189. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
  190. xpc_sh2_IPI_access3 =
  191. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
  192. for_each_online_node(node) {
  193. nasid = cnodeid_to_nasid(node);
  194. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  195. -1UL);
  196. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  197. -1UL);
  198. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  199. -1UL);
  200. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  201. -1UL);
  202. }
  203. } else {
  204. xpc_sh1_IPI_access =
  205. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
  206. for_each_online_node(node) {
  207. nasid = cnodeid_to_nasid(node);
  208. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  209. -1UL);
  210. /*
  211. * Since the BIST collides with memory operations on
  212. * SHUB 1.1 sn_change_memprotect() cannot be used.
  213. */
  214. if (enable_shub_wars_1_1()) {
  215. /* open up everything */
  216. xpc_prot_vec[node] = (u64)HUB_L((u64 *)
  217. GLOBAL_MMR_ADDR
  218. (nasid,
  219. SH1_MD_DQLP_MMR_DIR_PRIVEC0));
  220. HUB_S((u64 *)
  221. GLOBAL_MMR_ADDR(nasid,
  222. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  223. -1UL);
  224. HUB_S((u64 *)
  225. GLOBAL_MMR_ADDR(nasid,
  226. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  227. -1UL);
  228. }
  229. }
  230. }
  231. }
  232. /*
  233. * Restrict protections to disallow IPI operations (and AMO operations on
  234. * Shub 1.1 systems).
  235. */
  236. void
  237. xpc_restrict_IPI_ops(void)
  238. {
  239. int node;
  240. int nasid;
  241. /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
  242. if (is_shub2()) {
  243. for_each_online_node(node) {
  244. nasid = cnodeid_to_nasid(node);
  245. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  246. xpc_sh2_IPI_access0);
  247. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  248. xpc_sh2_IPI_access1);
  249. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  250. xpc_sh2_IPI_access2);
  251. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  252. xpc_sh2_IPI_access3);
  253. }
  254. } else {
  255. for_each_online_node(node) {
  256. nasid = cnodeid_to_nasid(node);
  257. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  258. xpc_sh1_IPI_access);
  259. if (enable_shub_wars_1_1()) {
  260. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  261. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  262. xpc_prot_vec[node]);
  263. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  264. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  265. xpc_prot_vec[node]);
  266. }
  267. }
  268. }
  269. }
  270. /*
  271. * At periodic intervals, scan through all active partitions and ensure
  272. * their heartbeat is still active. If not, the partition is deactivated.
  273. */
  274. void
  275. xpc_check_remote_hb(void)
  276. {
  277. struct xpc_vars *remote_vars;
  278. struct xpc_partition *part;
  279. short partid;
  280. enum xp_retval ret;
  281. remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
  282. for (partid = 0; partid < xp_max_npartitions; partid++) {
  283. if (xpc_exiting)
  284. break;
  285. if (partid == sn_partition_id)
  286. continue;
  287. part = &xpc_partitions[partid];
  288. if (part->act_state == XPC_P_INACTIVE ||
  289. part->act_state == XPC_P_DEACTIVATING) {
  290. continue;
  291. }
  292. /* pull the remote_hb cache line */
  293. ret = xp_remote_memcpy(remote_vars,
  294. (void *)part->remote_vars_pa,
  295. XPC_RP_VARS_SIZE);
  296. if (ret != xpSuccess) {
  297. XPC_DEACTIVATE_PARTITION(part, ret);
  298. continue;
  299. }
  300. dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
  301. " = %ld, heartbeat_offline = %ld, HB_mask = 0x%lx\n",
  302. partid, remote_vars->heartbeat, part->last_heartbeat,
  303. remote_vars->heartbeat_offline,
  304. remote_vars->heartbeating_to_mask);
  305. if (((remote_vars->heartbeat == part->last_heartbeat) &&
  306. (remote_vars->heartbeat_offline == 0)) ||
  307. !xpc_hb_allowed(sn_partition_id, remote_vars)) {
  308. XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
  309. continue;
  310. }
  311. part->last_heartbeat = remote_vars->heartbeat;
  312. }
  313. }
  314. /*
  315. * Get a copy of a portion of the remote partition's rsvd page.
  316. *
  317. * remote_rp points to a buffer that is cacheline aligned for BTE copies and
  318. * is large enough to contain a copy of their reserved page header and
  319. * part_nasids mask.
  320. */
  321. static enum xp_retval
  322. xpc_get_remote_rp(int nasid, u64 *discovered_nasids,
  323. struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
  324. {
  325. int i;
  326. enum xp_retval ret;
  327. /* get the reserved page's physical address */
  328. *remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
  329. if (*remote_rp_pa == 0)
  330. return xpNoRsvdPageAddr;
  331. /* pull over the reserved page header and part_nasids mask */
  332. ret = xp_remote_memcpy(remote_rp, (void *)*remote_rp_pa,
  333. XPC_RP_HEADER_SIZE + xp_sizeof_nasid_mask);
  334. if (ret != xpSuccess)
  335. return ret;
  336. if (discovered_nasids != NULL) {
  337. u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
  338. for (i = 0; i < xp_nasid_mask_words; i++)
  339. discovered_nasids[i] |= remote_part_nasids[i];
  340. }
  341. /* check that the partid is valid and is for another partition */
  342. if (remote_rp->SAL_partid < 0 ||
  343. remote_rp->SAL_partid >= xp_max_npartitions) {
  344. return xpInvalidPartid;
  345. }
  346. if (remote_rp->SAL_partid == sn_partition_id)
  347. return xpLocalPartid;
  348. /* see if the rest of the reserved page has been set up by XPC */
  349. if (timespec_equal(&remote_rp->stamp, &ZERO_STAMP))
  350. return xpRsvdPageNotSet;
  351. if (XPC_VERSION_MAJOR(remote_rp->version) !=
  352. XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
  353. return xpBadVersion;
  354. }
  355. if (remote_rp->max_npartitions <= sn_partition_id)
  356. return xpInvalidPartid;
  357. return xpSuccess;
  358. }
  359. /*
  360. * Get a copy of the remote partition's XPC variables from the reserved page.
  361. *
  362. * remote_vars points to a buffer that is cacheline aligned for BTE copies and
  363. * assumed to be of size XPC_RP_VARS_SIZE.
  364. */
  365. static enum xp_retval
  366. xpc_get_remote_vars(u64 remote_vars_pa, struct xpc_vars *remote_vars)
  367. {
  368. enum xp_retval ret;
  369. if (remote_vars_pa == 0)
  370. return xpVarsNotSet;
  371. /* pull over the cross partition variables */
  372. ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
  373. XPC_RP_VARS_SIZE);
  374. if (ret != xpSuccess)
  375. return ret;
  376. if (XPC_VERSION_MAJOR(remote_vars->version) !=
  377. XPC_VERSION_MAJOR(XPC_V_VERSION)) {
  378. return xpBadVersion;
  379. }
  380. return xpSuccess;
  381. }
  382. /*
  383. * Update the remote partition's info.
  384. */
  385. static void
  386. xpc_update_partition_info(struct xpc_partition *part, u8 remote_rp_version,
  387. struct timespec *remote_rp_stamp, u64 remote_rp_pa,
  388. u64 remote_vars_pa, struct xpc_vars *remote_vars)
  389. {
  390. part->remote_rp_version = remote_rp_version;
  391. dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
  392. part->remote_rp_version);
  393. part->remote_rp_stamp = *remote_rp_stamp;
  394. dev_dbg(xpc_part, " remote_rp_stamp (tv_sec = 0x%lx tv_nsec = 0x%lx\n",
  395. part->remote_rp_stamp.tv_sec, part->remote_rp_stamp.tv_nsec);
  396. part->remote_rp_pa = remote_rp_pa;
  397. dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
  398. part->remote_vars_pa = remote_vars_pa;
  399. dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
  400. part->remote_vars_pa);
  401. part->last_heartbeat = remote_vars->heartbeat;
  402. dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
  403. part->last_heartbeat);
  404. /* >>> remote_vars_part_pa and vars_part_pa are sn2 only!!! */
  405. part->remote_vars_part_pa = remote_vars->vars_part_pa;
  406. dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
  407. part->remote_vars_part_pa);
  408. part->remote_act_nasid = remote_vars->act_nasid;
  409. dev_dbg(xpc_part, " remote_act_nasid = 0x%x\n",
  410. part->remote_act_nasid);
  411. part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid;
  412. dev_dbg(xpc_part, " remote_act_phys_cpuid = 0x%x\n",
  413. part->remote_act_phys_cpuid);
  414. part->remote_amos_page_pa = remote_vars->amos_page_pa;
  415. dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
  416. part->remote_amos_page_pa);
  417. part->remote_vars_version = remote_vars->version;
  418. dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
  419. part->remote_vars_version);
  420. }
  421. /*
  422. * Prior code has determined the nasid which generated an IPI. Inspect
  423. * that nasid to determine if its partition needs to be activated or
  424. * deactivated.
  425. *
  426. * A partition is consider "awaiting activation" if our partition
  427. * flags indicate it is not active and it has a heartbeat. A
  428. * partition is considered "awaiting deactivation" if our partition
  429. * flags indicate it is active but it has no heartbeat or it is not
  430. * sending its heartbeat to us.
  431. *
  432. * To determine the heartbeat, the remote nasid must have a properly
  433. * initialized reserved page.
  434. */
  435. static void
  436. xpc_identify_act_IRQ_req(int nasid)
  437. {
  438. struct xpc_rsvd_page *remote_rp;
  439. struct xpc_vars *remote_vars;
  440. u64 remote_rp_pa;
  441. u64 remote_vars_pa;
  442. int remote_rp_version;
  443. int reactivate = 0;
  444. int stamp_diff;
  445. struct timespec remote_rp_stamp = { 0, 0 }; /*>>> ZERO_STAMP */
  446. short partid;
  447. struct xpc_partition *part;
  448. enum xp_retval ret;
  449. /* pull over the reserved page structure */
  450. remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
  451. ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
  452. if (ret != xpSuccess) {
  453. dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
  454. "which sent interrupt, reason=%d\n", nasid, ret);
  455. return;
  456. }
  457. remote_vars_pa = remote_rp->sn.vars_pa;
  458. remote_rp_version = remote_rp->version;
  459. if (XPC_SUPPORTS_RP_STAMP(remote_rp_version))
  460. remote_rp_stamp = remote_rp->stamp;
  461. partid = remote_rp->SAL_partid;
  462. part = &xpc_partitions[partid];
  463. /* pull over the cross partition variables */
  464. remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
  465. ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
  466. if (ret != xpSuccess) {
  467. dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
  468. "which sent interrupt, reason=%d\n", nasid, ret);
  469. XPC_DEACTIVATE_PARTITION(part, ret);
  470. return;
  471. }
  472. part->act_IRQ_rcvd++;
  473. dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
  474. "%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd,
  475. remote_vars->heartbeat, remote_vars->heartbeating_to_mask);
  476. if (xpc_partition_disengaged(part) &&
  477. part->act_state == XPC_P_INACTIVE) {
  478. xpc_update_partition_info(part, remote_rp_version,
  479. &remote_rp_stamp, remote_rp_pa,
  480. remote_vars_pa, remote_vars);
  481. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  482. if (xpc_partition_disengage_requested(1UL << partid)) {
  483. /*
  484. * Other side is waiting on us to disengage,
  485. * even though we already have.
  486. */
  487. return;
  488. }
  489. } else {
  490. /* other side doesn't support disengage requests */
  491. xpc_clear_partition_disengage_request(1UL << partid);
  492. }
  493. xpc_activate_partition(part);
  494. return;
  495. }
  496. DBUG_ON(part->remote_rp_version == 0);
  497. DBUG_ON(part->remote_vars_version == 0);
  498. if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) {
  499. DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part->
  500. remote_vars_version));
  501. if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
  502. DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
  503. version));
  504. /* see if the other side rebooted */
  505. if (part->remote_amos_page_pa ==
  506. remote_vars->amos_page_pa &&
  507. xpc_hb_allowed(sn_partition_id, remote_vars)) {
  508. /* doesn't look that way, so ignore the IPI */
  509. return;
  510. }
  511. }
  512. /*
  513. * Other side rebooted and previous XPC didn't support the
  514. * disengage request, so we don't need to do anything special.
  515. */
  516. xpc_update_partition_info(part, remote_rp_version,
  517. &remote_rp_stamp, remote_rp_pa,
  518. remote_vars_pa, remote_vars);
  519. part->reactivate_nasid = nasid;
  520. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  521. return;
  522. }
  523. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version));
  524. if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
  525. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
  526. /*
  527. * Other side rebooted and previous XPC did support the
  528. * disengage request, but the new one doesn't.
  529. */
  530. xpc_clear_partition_engaged(1UL << partid);
  531. xpc_clear_partition_disengage_request(1UL << partid);
  532. xpc_update_partition_info(part, remote_rp_version,
  533. &remote_rp_stamp, remote_rp_pa,
  534. remote_vars_pa, remote_vars);
  535. reactivate = 1;
  536. } else {
  537. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
  538. stamp_diff = xpc_compare_stamps(&part->remote_rp_stamp,
  539. &remote_rp_stamp);
  540. if (stamp_diff != 0) {
  541. DBUG_ON(stamp_diff >= 0);
  542. /*
  543. * Other side rebooted and the previous XPC did support
  544. * the disengage request, as does the new one.
  545. */
  546. DBUG_ON(xpc_partition_engaged(1UL << partid));
  547. DBUG_ON(xpc_partition_disengage_requested(1UL <<
  548. partid));
  549. xpc_update_partition_info(part, remote_rp_version,
  550. &remote_rp_stamp,
  551. remote_rp_pa, remote_vars_pa,
  552. remote_vars);
  553. reactivate = 1;
  554. }
  555. }
  556. if (part->disengage_request_timeout > 0 &&
  557. !xpc_partition_disengaged(part)) {
  558. /* still waiting on other side to disengage from us */
  559. return;
  560. }
  561. if (reactivate) {
  562. part->reactivate_nasid = nasid;
  563. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  564. } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
  565. xpc_partition_disengage_requested(1UL << partid)) {
  566. XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
  567. }
  568. }
  569. /*
  570. * Loop through the activation AMO variables and process any bits
  571. * which are set. Each bit indicates a nasid sending a partition
  572. * activation or deactivation request.
  573. *
  574. * Return #of IRQs detected.
  575. */
  576. int
  577. xpc_identify_act_IRQ_sender(void)
  578. {
  579. int word, bit;
  580. u64 nasid_mask;
  581. u64 nasid; /* remote nasid */
  582. int n_IRQs_detected = 0;
  583. AMO_t *act_amos;
  584. act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
  585. /* scan through act AMO variable looking for non-zero entries */
  586. for (word = 0; word < xp_nasid_mask_words; word++) {
  587. if (xpc_exiting)
  588. break;
  589. nasid_mask = xpc_IPI_receive(&act_amos[word]);
  590. if (nasid_mask == 0) {
  591. /* no IRQs from nasids in this variable */
  592. continue;
  593. }
  594. dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
  595. nasid_mask);
  596. /*
  597. * If this nasid has been added to the machine since
  598. * our partition was reset, this will retain the
  599. * remote nasid in our reserved pages machine mask.
  600. * This is used in the event of module reload.
  601. */
  602. xpc_mach_nasids[word] |= nasid_mask;
  603. /* locate the nasid(s) which sent interrupts */
  604. for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
  605. if (nasid_mask & (1UL << bit)) {
  606. n_IRQs_detected++;
  607. nasid = XPC_NASID_FROM_W_B(word, bit);
  608. dev_dbg(xpc_part, "interrupt from nasid %ld\n",
  609. nasid);
  610. xpc_identify_act_IRQ_req(nasid);
  611. }
  612. }
  613. }
  614. return n_IRQs_detected;
  615. }
  616. /*
  617. * See if the other side has responded to a partition disengage request
  618. * from us.
  619. */
  620. int
  621. xpc_partition_disengaged(struct xpc_partition *part)
  622. {
  623. short partid = XPC_PARTID(part);
  624. int disengaged;
  625. disengaged = (xpc_partition_engaged(1UL << partid) == 0);
  626. if (part->disengage_request_timeout) {
  627. if (!disengaged) {
  628. if (time_before(jiffies,
  629. part->disengage_request_timeout)) {
  630. /* timelimit hasn't been reached yet */
  631. return 0;
  632. }
  633. /*
  634. * Other side hasn't responded to our disengage
  635. * request in a timely fashion, so assume it's dead.
  636. */
  637. dev_info(xpc_part, "disengage from remote partition %d "
  638. "timed out\n", partid);
  639. xpc_disengage_request_timedout = 1;
  640. xpc_clear_partition_engaged(1UL << partid);
  641. disengaged = 1;
  642. }
  643. part->disengage_request_timeout = 0;
  644. /* cancel the timer function, provided it's not us */
  645. if (!in_interrupt()) {
  646. del_singleshot_timer_sync(&part->
  647. disengage_request_timer);
  648. }
  649. DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
  650. part->act_state != XPC_P_INACTIVE);
  651. if (part->act_state != XPC_P_INACTIVE)
  652. xpc_wakeup_channel_mgr(part);
  653. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version))
  654. xpc_cancel_partition_disengage_request(part);
  655. }
  656. return disengaged;
  657. }
  658. /*
  659. * Mark specified partition as active.
  660. */
  661. enum xp_retval
  662. xpc_mark_partition_active(struct xpc_partition *part)
  663. {
  664. unsigned long irq_flags;
  665. enum xp_retval ret;
  666. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  667. spin_lock_irqsave(&part->act_lock, irq_flags);
  668. if (part->act_state == XPC_P_ACTIVATING) {
  669. part->act_state = XPC_P_ACTIVE;
  670. ret = xpSuccess;
  671. } else {
  672. DBUG_ON(part->reason == xpSuccess);
  673. ret = part->reason;
  674. }
  675. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  676. return ret;
  677. }
  678. /*
  679. * Notify XPC that the partition is down.
  680. */
  681. void
  682. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  683. enum xp_retval reason)
  684. {
  685. unsigned long irq_flags;
  686. spin_lock_irqsave(&part->act_lock, irq_flags);
  687. if (part->act_state == XPC_P_INACTIVE) {
  688. XPC_SET_REASON(part, reason, line);
  689. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  690. if (reason == xpReactivating) {
  691. /* we interrupt ourselves to reactivate partition */
  692. xpc_IPI_send_reactivate(part);
  693. }
  694. return;
  695. }
  696. if (part->act_state == XPC_P_DEACTIVATING) {
  697. if ((part->reason == xpUnloading && reason != xpUnloading) ||
  698. reason == xpReactivating) {
  699. XPC_SET_REASON(part, reason, line);
  700. }
  701. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  702. return;
  703. }
  704. part->act_state = XPC_P_DEACTIVATING;
  705. XPC_SET_REASON(part, reason, line);
  706. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  707. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  708. xpc_request_partition_disengage(part);
  709. xpc_IPI_send_disengage(part);
  710. /* set a timelimit on the disengage request */
  711. part->disengage_request_timeout = jiffies +
  712. (xpc_disengage_request_timelimit * HZ);
  713. part->disengage_request_timer.expires =
  714. part->disengage_request_timeout;
  715. add_timer(&part->disengage_request_timer);
  716. }
  717. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  718. XPC_PARTID(part), reason);
  719. xpc_partition_going_down(part, reason);
  720. }
  721. /*
  722. * Mark specified partition as inactive.
  723. */
  724. void
  725. xpc_mark_partition_inactive(struct xpc_partition *part)
  726. {
  727. unsigned long irq_flags;
  728. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  729. XPC_PARTID(part));
  730. spin_lock_irqsave(&part->act_lock, irq_flags);
  731. part->act_state = XPC_P_INACTIVE;
  732. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  733. part->remote_rp_pa = 0;
  734. }
  735. /*
  736. * SAL has provided a partition and machine mask. The partition mask
  737. * contains a bit for each even nasid in our partition. The machine
  738. * mask contains a bit for each even nasid in the entire machine.
  739. *
  740. * Using those two bit arrays, we can determine which nasids are
  741. * known in the machine. Each should also have a reserved page
  742. * initialized if they are available for partitioning.
  743. */
  744. void
  745. xpc_discovery(void)
  746. {
  747. void *remote_rp_base;
  748. struct xpc_rsvd_page *remote_rp;
  749. struct xpc_vars *remote_vars;
  750. u64 remote_rp_pa;
  751. u64 remote_vars_pa;
  752. int region;
  753. int region_size;
  754. int max_regions;
  755. int nasid;
  756. struct xpc_rsvd_page *rp;
  757. short partid;
  758. struct xpc_partition *part;
  759. u64 *discovered_nasids;
  760. enum xp_retval ret;
  761. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  762. xp_sizeof_nasid_mask,
  763. GFP_KERNEL, &remote_rp_base);
  764. if (remote_rp == NULL)
  765. return;
  766. remote_vars = (struct xpc_vars *)remote_rp;
  767. discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words,
  768. GFP_KERNEL);
  769. if (discovered_nasids == NULL) {
  770. kfree(remote_rp_base);
  771. return;
  772. }
  773. rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
  774. /*
  775. * The term 'region' in this context refers to the minimum number of
  776. * nodes that can comprise an access protection grouping. The access
  777. * protection is in regards to memory, IOI and IPI.
  778. */
  779. max_regions = 64;
  780. region_size = sn_region_size;
  781. switch (region_size) {
  782. case 128:
  783. max_regions *= 2;
  784. case 64:
  785. max_regions *= 2;
  786. case 32:
  787. max_regions *= 2;
  788. region_size = 16;
  789. DBUG_ON(!is_shub2());
  790. }
  791. for (region = 0; region < max_regions; region++) {
  792. if (xpc_exiting)
  793. break;
  794. dev_dbg(xpc_part, "searching region %d\n", region);
  795. for (nasid = (region * region_size * 2);
  796. nasid < ((region + 1) * region_size * 2); nasid += 2) {
  797. if (xpc_exiting)
  798. break;
  799. dev_dbg(xpc_part, "checking nasid %d\n", nasid);
  800. if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
  801. dev_dbg(xpc_part, "PROM indicates Nasid %d is "
  802. "part of the local partition; skipping "
  803. "region\n", nasid);
  804. break;
  805. }
  806. if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
  807. dev_dbg(xpc_part, "PROM indicates Nasid %d was "
  808. "not on Numa-Link network at reset\n",
  809. nasid);
  810. continue;
  811. }
  812. if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
  813. dev_dbg(xpc_part, "Nasid %d is part of a "
  814. "partition which was previously "
  815. "discovered\n", nasid);
  816. continue;
  817. }
  818. /* pull over the reserved page structure */
  819. ret = xpc_get_remote_rp(nasid, discovered_nasids,
  820. remote_rp, &remote_rp_pa);
  821. if (ret != xpSuccess) {
  822. dev_dbg(xpc_part, "unable to get reserved page "
  823. "from nasid %d, reason=%d\n", nasid,
  824. ret);
  825. if (ret == xpLocalPartid)
  826. break;
  827. continue;
  828. }
  829. remote_vars_pa = remote_rp->sn.vars_pa;
  830. partid = remote_rp->SAL_partid;
  831. part = &xpc_partitions[partid];
  832. /* pull over the cross partition variables */
  833. ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
  834. if (ret != xpSuccess) {
  835. dev_dbg(xpc_part, "unable to get XPC variables "
  836. "from nasid %d, reason=%d\n", nasid,
  837. ret);
  838. XPC_DEACTIVATE_PARTITION(part, ret);
  839. continue;
  840. }
  841. if (part->act_state != XPC_P_INACTIVE) {
  842. dev_dbg(xpc_part, "partition %d on nasid %d is "
  843. "already activating\n", partid, nasid);
  844. break;
  845. }
  846. /*
  847. * Register the remote partition's AMOs with SAL so it
  848. * can handle and cleanup errors within that address
  849. * range should the remote partition go down. We don't
  850. * unregister this range because it is difficult to
  851. * tell when outstanding writes to the remote partition
  852. * are finished and thus when it is thus safe to
  853. * unregister. This should not result in wasted space
  854. * in the SAL xp_addr_region table because we should
  855. * get the same page for remote_act_amos_pa after
  856. * module reloads and system reboots.
  857. */
  858. if (sn_register_xp_addr_region
  859. (remote_vars->amos_page_pa, PAGE_SIZE, 1) < 0) {
  860. dev_dbg(xpc_part,
  861. "partition %d failed to "
  862. "register xp_addr region 0x%016lx\n",
  863. partid, remote_vars->amos_page_pa);
  864. XPC_SET_REASON(part, xpPhysAddrRegFailed,
  865. __LINE__);
  866. break;
  867. }
  868. /*
  869. * The remote nasid is valid and available.
  870. * Send an interrupt to that nasid to notify
  871. * it that we are ready to begin activation.
  872. */
  873. dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, "
  874. "nasid %d, phys_cpuid 0x%x\n",
  875. remote_vars->amos_page_pa,
  876. remote_vars->act_nasid,
  877. remote_vars->act_phys_cpuid);
  878. if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
  879. version)) {
  880. part->remote_amos_page_pa =
  881. remote_vars->amos_page_pa;
  882. xpc_mark_partition_disengaged(part);
  883. xpc_cancel_partition_disengage_request(part);
  884. }
  885. xpc_IPI_send_activate(remote_vars);
  886. }
  887. }
  888. kfree(discovered_nasids);
  889. kfree(remote_rp_base);
  890. }
  891. /*
  892. * Given a partid, get the nasids owned by that partition from the
  893. * remote partition's reserved page.
  894. */
  895. enum xp_retval
  896. xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
  897. {
  898. struct xpc_partition *part;
  899. u64 part_nasid_pa;
  900. part = &xpc_partitions[partid];
  901. if (part->remote_rp_pa == 0)
  902. return xpPartitionDown;
  903. memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
  904. part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
  905. return xp_remote_memcpy(nasid_mask, (void *)part_nasid_pa,
  906. xp_sizeof_nasid_mask);
  907. }